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
25 #include "hw/sysbus.h"
26 #include "sysemu/sysemu.h"
28 #include "qemu/error-report.h"
29 #include "sysemu/block-backend.h"
30 #include "sysemu/blockdev.h"
34 ENABLE_CMD_TX
= (1<<0),
35 ENABLE_CMD_RX
= (1<<1),
36 ENABLE_DAT_TX
= (1<<2),
37 ENABLE_DAT_RX
= (1<<3),
41 PENDING_CMD_TX
= (1<<0),
42 PENDING_CMD_RX
= (1<<1),
43 PENDING_DAT_TX
= (1<<2),
44 PENDING_DAT_RX
= (1<<3),
48 START_CMD_TX
= (1<<0),
49 START_DAT_RX
= (1<<1),
62 #define TYPE_MILKYMIST_MEMCARD "milkymist-memcard"
63 #define MILKYMIST_MEMCARD(obj) \
64 OBJECT_CHECK(MilkymistMemcardState, (obj), TYPE_MILKYMIST_MEMCARD)
66 struct MilkymistMemcardState
{
67 SysBusDevice parent_obj
;
69 MemoryRegion regs_region
;
72 int command_write_ptr
;
73 int response_read_ptr
;
81 typedef struct MilkymistMemcardState MilkymistMemcardState
;
83 static void update_pending_bits(MilkymistMemcardState
*s
)
85 /* transmits are instantaneous, thus tx pending bits are never set */
86 s
->regs
[R_PENDING
] = 0;
87 /* if rx is enabled the corresponding pending bits are always set */
88 if (s
->regs
[R_ENABLE
] & ENABLE_CMD_RX
) {
89 s
->regs
[R_PENDING
] |= PENDING_CMD_RX
;
91 if (s
->regs
[R_ENABLE
] & ENABLE_DAT_RX
) {
92 s
->regs
[R_PENDING
] |= PENDING_DAT_RX
;
96 static void memcard_sd_command(MilkymistMemcardState
*s
)
100 req
.cmd
= s
->command
[0] & 0x3f;
101 req
.arg
= (s
->command
[1] << 24) | (s
->command
[2] << 16)
102 | (s
->command
[3] << 8) | s
->command
[4];
103 req
.crc
= s
->command
[5];
105 s
->response
[0] = req
.cmd
;
106 s
->response_len
= sd_do_command(s
->card
, &req
, s
->response
+1);
107 s
->response_read_ptr
= 0;
109 if (s
->response_len
== 16) {
111 s
->response
[0] = 0x3f;
112 s
->response_len
+= 1;
113 } else if (s
->response_len
== 4) {
114 /* no crc calculation, insert dummy byte */
116 s
->response_len
+= 2;
120 /* next write is a dummy byte to clock the initialization of the sd
122 s
->ignore_next_cmd
= 1;
126 static uint64_t memcard_read(void *opaque
, hwaddr addr
,
129 MilkymistMemcardState
*s
= opaque
;
138 r
= s
->response
[s
->response_read_ptr
++];
139 if (s
->response_read_ptr
> s
->response_len
) {
140 error_report("milkymist_memcard: "
141 "read more cmd bytes than available. Clipping.");
142 s
->response_read_ptr
= 0;
151 r
|= sd_read_data(s
->card
) << 24;
152 r
|= sd_read_data(s
->card
) << 16;
153 r
|= sd_read_data(s
->card
) << 8;
154 r
|= sd_read_data(s
->card
);
165 error_report("milkymist_memcard: read access to unknown register 0x"
166 TARGET_FMT_plx
, addr
<< 2);
170 trace_milkymist_memcard_memory_read(addr
<< 2, r
);
175 static void memcard_write(void *opaque
, hwaddr addr
, uint64_t value
,
178 MilkymistMemcardState
*s
= opaque
;
180 trace_milkymist_memcard_memory_write(addr
, value
);
185 /* clear rx pending bits */
186 s
->regs
[R_PENDING
] &= ~(value
& (PENDING_CMD_RX
| PENDING_DAT_RX
));
187 update_pending_bits(s
);
193 if (s
->ignore_next_cmd
) {
194 s
->ignore_next_cmd
= 0;
197 s
->command
[s
->command_write_ptr
] = value
& 0xff;
198 s
->command_write_ptr
= (s
->command_write_ptr
+ 1) % 6;
199 if (s
->command_write_ptr
== 0) {
200 memcard_sd_command(s
);
207 sd_write_data(s
->card
, (value
>> 24) & 0xff);
208 sd_write_data(s
->card
, (value
>> 16) & 0xff);
209 sd_write_data(s
->card
, (value
>> 8) & 0xff);
210 sd_write_data(s
->card
, value
& 0xff);
213 s
->regs
[addr
] = value
;
214 update_pending_bits(s
);
218 s
->regs
[addr
] = value
;
222 error_report("milkymist_memcard: write access to unknown register 0x"
223 TARGET_FMT_plx
, addr
<< 2);
228 static const MemoryRegionOps memcard_mmio_ops
= {
229 .read
= memcard_read
,
230 .write
= memcard_write
,
232 .min_access_size
= 4,
233 .max_access_size
= 4,
235 .endianness
= DEVICE_NATIVE_ENDIAN
,
238 static void milkymist_memcard_reset(DeviceState
*d
)
240 MilkymistMemcardState
*s
= MILKYMIST_MEMCARD(d
);
243 s
->command_write_ptr
= 0;
244 s
->response_read_ptr
= 0;
247 for (i
= 0; i
< R_MAX
; i
++) {
252 static int milkymist_memcard_init(SysBusDevice
*dev
)
254 MilkymistMemcardState
*s
= MILKYMIST_MEMCARD(dev
);
258 dinfo
= drive_get_next(IF_SD
);
259 blk
= dinfo
? blk_by_legacy_dinfo(dinfo
) : NULL
;
260 s
->card
= sd_init(blk
, false);
261 if (s
->card
== NULL
) {
265 s
->enabled
= blk
&& blk_is_inserted(blk
);
267 memory_region_init_io(&s
->regs_region
, OBJECT(s
), &memcard_mmio_ops
, s
,
268 "milkymist-memcard", R_MAX
* 4);
269 sysbus_init_mmio(dev
, &s
->regs_region
);
274 static const VMStateDescription vmstate_milkymist_memcard
= {
275 .name
= "milkymist-memcard",
277 .minimum_version_id
= 1,
278 .fields
= (VMStateField
[]) {
279 VMSTATE_INT32(command_write_ptr
, MilkymistMemcardState
),
280 VMSTATE_INT32(response_read_ptr
, MilkymistMemcardState
),
281 VMSTATE_INT32(response_len
, MilkymistMemcardState
),
282 VMSTATE_INT32(ignore_next_cmd
, MilkymistMemcardState
),
283 VMSTATE_INT32(enabled
, MilkymistMemcardState
),
284 VMSTATE_UINT8_ARRAY(command
, MilkymistMemcardState
, 6),
285 VMSTATE_UINT8_ARRAY(response
, MilkymistMemcardState
, 17),
286 VMSTATE_UINT32_ARRAY(regs
, MilkymistMemcardState
, R_MAX
),
287 VMSTATE_END_OF_LIST()
291 static void milkymist_memcard_class_init(ObjectClass
*klass
, void *data
)
293 DeviceClass
*dc
= DEVICE_CLASS(klass
);
294 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
296 k
->init
= milkymist_memcard_init
;
297 dc
->reset
= milkymist_memcard_reset
;
298 dc
->vmsd
= &vmstate_milkymist_memcard
;
301 static const TypeInfo milkymist_memcard_info
= {
302 .name
= TYPE_MILKYMIST_MEMCARD
,
303 .parent
= TYPE_SYS_BUS_DEVICE
,
304 .instance_size
= sizeof(MilkymistMemcardState
),
305 .class_init
= milkymist_memcard_class_init
,
308 static void milkymist_memcard_register_types(void)
310 type_register_static(&milkymist_memcard_info
);
313 type_init(milkymist_memcard_register_types
)