2 * QEMU model of the Milkymist SD Card Controller.
4 * Copyright (c) 2010 Michael Walle <michael@walle.cc>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Specification available at:
21 * http://www.milkymist.org/socdoc/memcard.pdf
28 #include "qemu-error.h"
33 ENABLE_CMD_TX
= (1<<0),
34 ENABLE_CMD_RX
= (1<<1),
35 ENABLE_DAT_TX
= (1<<2),
36 ENABLE_DAT_RX
= (1<<3),
40 PENDING_CMD_TX
= (1<<0),
41 PENDING_CMD_RX
= (1<<1),
42 PENDING_DAT_TX
= (1<<2),
43 PENDING_DAT_RX
= (1<<3),
47 START_CMD_TX
= (1<<0),
48 START_DAT_RX
= (1<<1),
61 struct MilkymistMemcardState
{
63 MemoryRegion regs_region
;
66 int command_write_ptr
;
67 int response_read_ptr
;
75 typedef struct MilkymistMemcardState MilkymistMemcardState
;
77 static void update_pending_bits(MilkymistMemcardState
*s
)
79 /* transmits are instantaneous, thus tx pending bits are never set */
80 s
->regs
[R_PENDING
] = 0;
81 /* if rx is enabled the corresponding pending bits are always set */
82 if (s
->regs
[R_ENABLE
] & ENABLE_CMD_RX
) {
83 s
->regs
[R_PENDING
] |= PENDING_CMD_RX
;
85 if (s
->regs
[R_ENABLE
] & ENABLE_DAT_RX
) {
86 s
->regs
[R_PENDING
] |= PENDING_DAT_RX
;
90 static void memcard_sd_command(MilkymistMemcardState
*s
)
94 req
.cmd
= s
->command
[0] & 0x3f;
95 req
.arg
= (s
->command
[1] << 24) | (s
->command
[2] << 16)
96 | (s
->command
[3] << 8) | s
->command
[4];
97 req
.crc
= s
->command
[5];
99 s
->response
[0] = req
.cmd
;
100 s
->response_len
= sd_do_command(s
->card
, &req
, s
->response
+1);
101 s
->response_read_ptr
= 0;
103 if (s
->response_len
== 16) {
105 s
->response
[0] = 0x3f;
106 s
->response_len
+= 1;
107 } else if (s
->response_len
== 4) {
108 /* no crc calculation, insert dummy byte */
110 s
->response_len
+= 2;
114 /* next write is a dummy byte to clock the initialization of the sd
116 s
->ignore_next_cmd
= 1;
120 static uint64_t memcard_read(void *opaque
, target_phys_addr_t addr
,
123 MilkymistMemcardState
*s
= opaque
;
132 r
= s
->response
[s
->response_read_ptr
++];
133 if (s
->response_read_ptr
> s
->response_len
) {
134 error_report("milkymist_memcard: "
135 "read more cmd bytes than available. Clipping.");
136 s
->response_read_ptr
= 0;
145 r
|= sd_read_data(s
->card
) << 24;
146 r
|= sd_read_data(s
->card
) << 16;
147 r
|= sd_read_data(s
->card
) << 8;
148 r
|= sd_read_data(s
->card
);
159 error_report("milkymist_memcard: read access to unknown register 0x"
160 TARGET_FMT_plx
, addr
<< 2);
164 trace_milkymist_memcard_memory_read(addr
<< 2, r
);
169 static void memcard_write(void *opaque
, target_phys_addr_t addr
, uint64_t value
,
172 MilkymistMemcardState
*s
= opaque
;
174 trace_milkymist_memcard_memory_write(addr
, value
);
179 /* clear rx pending bits */
180 s
->regs
[R_PENDING
] &= ~(value
& (PENDING_CMD_RX
| PENDING_DAT_RX
));
181 update_pending_bits(s
);
187 if (s
->ignore_next_cmd
) {
188 s
->ignore_next_cmd
= 0;
191 s
->command
[s
->command_write_ptr
] = value
& 0xff;
192 s
->command_write_ptr
= (s
->command_write_ptr
+ 1) % 6;
193 if (s
->command_write_ptr
== 0) {
194 memcard_sd_command(s
);
201 sd_write_data(s
->card
, (value
>> 24) & 0xff);
202 sd_write_data(s
->card
, (value
>> 16) & 0xff);
203 sd_write_data(s
->card
, (value
>> 8) & 0xff);
204 sd_write_data(s
->card
, value
& 0xff);
207 s
->regs
[addr
] = value
;
208 update_pending_bits(s
);
212 s
->regs
[addr
] = value
;
216 error_report("milkymist_memcard: write access to unknown register 0x"
217 TARGET_FMT_plx
, addr
<< 2);
222 static const MemoryRegionOps memcard_mmio_ops
= {
223 .read
= memcard_read
,
224 .write
= memcard_write
,
226 .min_access_size
= 4,
227 .max_access_size
= 4,
229 .endianness
= DEVICE_NATIVE_ENDIAN
,
232 static void milkymist_memcard_reset(DeviceState
*d
)
234 MilkymistMemcardState
*s
=
235 container_of(d
, MilkymistMemcardState
, busdev
.qdev
);
238 s
->command_write_ptr
= 0;
239 s
->response_read_ptr
= 0;
242 for (i
= 0; i
< R_MAX
; i
++) {
247 static int milkymist_memcard_init(SysBusDevice
*dev
)
249 MilkymistMemcardState
*s
= FROM_SYSBUS(typeof(*s
), dev
);
252 dinfo
= drive_get_next(IF_SD
);
253 s
->card
= sd_init(dinfo
? dinfo
->bdrv
: NULL
, 0);
254 s
->enabled
= dinfo
? bdrv_is_inserted(dinfo
->bdrv
) : 0;
256 memory_region_init_io(&s
->regs_region
, &memcard_mmio_ops
, s
,
257 "milkymist-memcard", R_MAX
* 4);
258 sysbus_init_mmio_region(dev
, &s
->regs_region
);
263 static const VMStateDescription vmstate_milkymist_memcard
= {
264 .name
= "milkymist-memcard",
266 .minimum_version_id
= 1,
267 .minimum_version_id_old
= 1,
268 .fields
= (VMStateField
[]) {
269 VMSTATE_INT32(command_write_ptr
, MilkymistMemcardState
),
270 VMSTATE_INT32(response_read_ptr
, MilkymistMemcardState
),
271 VMSTATE_INT32(response_len
, MilkymistMemcardState
),
272 VMSTATE_INT32(ignore_next_cmd
, MilkymistMemcardState
),
273 VMSTATE_INT32(enabled
, MilkymistMemcardState
),
274 VMSTATE_UINT8_ARRAY(command
, MilkymistMemcardState
, 6),
275 VMSTATE_UINT8_ARRAY(response
, MilkymistMemcardState
, 17),
276 VMSTATE_UINT32_ARRAY(regs
, MilkymistMemcardState
, R_MAX
),
277 VMSTATE_END_OF_LIST()
281 static SysBusDeviceInfo milkymist_memcard_info
= {
282 .init
= milkymist_memcard_init
,
283 .qdev
.name
= "milkymist-memcard",
284 .qdev
.size
= sizeof(MilkymistMemcardState
),
285 .qdev
.vmsd
= &vmstate_milkymist_memcard
,
286 .qdev
.reset
= milkymist_memcard_reset
,
289 static void milkymist_memcard_register(void)
291 sysbus_register_withprop(&milkymist_memcard_info
);
294 device_init(milkymist_memcard_register
)