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://milkymist.walle.cc/socdoc/memcard.pdf
24 #include "qemu/osdep.h"
26 #include "hw/sysbus.h"
27 #include "sysemu/sysemu.h"
29 #include "qemu/error-report.h"
30 #include "sysemu/block-backend.h"
31 #include "sysemu/blockdev.h"
35 ENABLE_CMD_TX
= (1<<0),
36 ENABLE_CMD_RX
= (1<<1),
37 ENABLE_DAT_TX
= (1<<2),
38 ENABLE_DAT_RX
= (1<<3),
42 PENDING_CMD_TX
= (1<<0),
43 PENDING_CMD_RX
= (1<<1),
44 PENDING_DAT_TX
= (1<<2),
45 PENDING_DAT_RX
= (1<<3),
49 START_CMD_TX
= (1<<0),
50 START_DAT_RX
= (1<<1),
63 #define TYPE_MILKYMIST_MEMCARD "milkymist-memcard"
64 #define MILKYMIST_MEMCARD(obj) \
65 OBJECT_CHECK(MilkymistMemcardState, (obj), TYPE_MILKYMIST_MEMCARD)
67 struct MilkymistMemcardState
{
68 SysBusDevice parent_obj
;
70 MemoryRegion regs_region
;
73 int command_write_ptr
;
74 int response_read_ptr
;
82 typedef struct MilkymistMemcardState MilkymistMemcardState
;
84 static void update_pending_bits(MilkymistMemcardState
*s
)
86 /* transmits are instantaneous, thus tx pending bits are never set */
87 s
->regs
[R_PENDING
] = 0;
88 /* if rx is enabled the corresponding pending bits are always set */
89 if (s
->regs
[R_ENABLE
] & ENABLE_CMD_RX
) {
90 s
->regs
[R_PENDING
] |= PENDING_CMD_RX
;
92 if (s
->regs
[R_ENABLE
] & ENABLE_DAT_RX
) {
93 s
->regs
[R_PENDING
] |= PENDING_DAT_RX
;
97 static void memcard_sd_command(MilkymistMemcardState
*s
)
101 req
.cmd
= s
->command
[0] & 0x3f;
102 req
.arg
= (s
->command
[1] << 24) | (s
->command
[2] << 16)
103 | (s
->command
[3] << 8) | s
->command
[4];
104 req
.crc
= s
->command
[5];
106 s
->response
[0] = req
.cmd
;
107 s
->response_len
= sd_do_command(s
->card
, &req
, s
->response
+1);
108 s
->response_read_ptr
= 0;
110 if (s
->response_len
== 16) {
112 s
->response
[0] = 0x3f;
113 s
->response_len
+= 1;
114 } else if (s
->response_len
== 4) {
115 /* no crc calculation, insert dummy byte */
117 s
->response_len
+= 2;
121 /* next write is a dummy byte to clock the initialization of the sd
123 s
->ignore_next_cmd
= 1;
127 static uint64_t memcard_read(void *opaque
, hwaddr addr
,
130 MilkymistMemcardState
*s
= opaque
;
139 r
= s
->response
[s
->response_read_ptr
++];
140 if (s
->response_read_ptr
> s
->response_len
) {
141 error_report("milkymist_memcard: "
142 "read more cmd bytes than available. Clipping.");
143 s
->response_read_ptr
= 0;
152 r
|= sd_read_data(s
->card
) << 24;
153 r
|= sd_read_data(s
->card
) << 16;
154 r
|= sd_read_data(s
->card
) << 8;
155 r
|= sd_read_data(s
->card
);
166 error_report("milkymist_memcard: read access to unknown register 0x"
167 TARGET_FMT_plx
, addr
<< 2);
171 trace_milkymist_memcard_memory_read(addr
<< 2, r
);
176 static void memcard_write(void *opaque
, hwaddr addr
, uint64_t value
,
179 MilkymistMemcardState
*s
= opaque
;
181 trace_milkymist_memcard_memory_write(addr
, value
);
186 /* clear rx pending bits */
187 s
->regs
[R_PENDING
] &= ~(value
& (PENDING_CMD_RX
| PENDING_DAT_RX
));
188 update_pending_bits(s
);
194 if (s
->ignore_next_cmd
) {
195 s
->ignore_next_cmd
= 0;
198 s
->command
[s
->command_write_ptr
] = value
& 0xff;
199 s
->command_write_ptr
= (s
->command_write_ptr
+ 1) % 6;
200 if (s
->command_write_ptr
== 0) {
201 memcard_sd_command(s
);
208 sd_write_data(s
->card
, (value
>> 24) & 0xff);
209 sd_write_data(s
->card
, (value
>> 16) & 0xff);
210 sd_write_data(s
->card
, (value
>> 8) & 0xff);
211 sd_write_data(s
->card
, value
& 0xff);
214 s
->regs
[addr
] = value
;
215 update_pending_bits(s
);
219 s
->regs
[addr
] = value
;
223 error_report("milkymist_memcard: write access to unknown register 0x"
224 TARGET_FMT_plx
, addr
<< 2);
229 static const MemoryRegionOps memcard_mmio_ops
= {
230 .read
= memcard_read
,
231 .write
= memcard_write
,
233 .min_access_size
= 4,
234 .max_access_size
= 4,
236 .endianness
= DEVICE_NATIVE_ENDIAN
,
239 static void milkymist_memcard_reset(DeviceState
*d
)
241 MilkymistMemcardState
*s
= MILKYMIST_MEMCARD(d
);
244 s
->command_write_ptr
= 0;
245 s
->response_read_ptr
= 0;
248 for (i
= 0; i
< R_MAX
; i
++) {
253 static int milkymist_memcard_init(SysBusDevice
*dev
)
255 MilkymistMemcardState
*s
= MILKYMIST_MEMCARD(dev
);
259 /* FIXME use a qdev drive property instead of drive_get_next() */
260 dinfo
= drive_get_next(IF_SD
);
261 blk
= dinfo
? blk_by_legacy_dinfo(dinfo
) : NULL
;
262 s
->card
= sd_init(blk
, false);
263 if (s
->card
== NULL
) {
267 s
->enabled
= blk
&& blk_is_inserted(blk
);
269 memory_region_init_io(&s
->regs_region
, OBJECT(s
), &memcard_mmio_ops
, s
,
270 "milkymist-memcard", R_MAX
* 4);
271 sysbus_init_mmio(dev
, &s
->regs_region
);
276 static const VMStateDescription vmstate_milkymist_memcard
= {
277 .name
= "milkymist-memcard",
279 .minimum_version_id
= 1,
280 .fields
= (VMStateField
[]) {
281 VMSTATE_INT32(command_write_ptr
, MilkymistMemcardState
),
282 VMSTATE_INT32(response_read_ptr
, MilkymistMemcardState
),
283 VMSTATE_INT32(response_len
, MilkymistMemcardState
),
284 VMSTATE_INT32(ignore_next_cmd
, MilkymistMemcardState
),
285 VMSTATE_INT32(enabled
, MilkymistMemcardState
),
286 VMSTATE_UINT8_ARRAY(command
, MilkymistMemcardState
, 6),
287 VMSTATE_UINT8_ARRAY(response
, MilkymistMemcardState
, 17),
288 VMSTATE_UINT32_ARRAY(regs
, MilkymistMemcardState
, R_MAX
),
289 VMSTATE_END_OF_LIST()
293 static void milkymist_memcard_class_init(ObjectClass
*klass
, void *data
)
295 DeviceClass
*dc
= DEVICE_CLASS(klass
);
296 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
298 k
->init
= milkymist_memcard_init
;
299 dc
->reset
= milkymist_memcard_reset
;
300 dc
->vmsd
= &vmstate_milkymist_memcard
;
301 /* Reason: init() method uses drive_get_next() */
302 dc
->cannot_instantiate_with_device_add_yet
= true;
305 static const TypeInfo milkymist_memcard_info
= {
306 .name
= TYPE_MILKYMIST_MEMCARD
,
307 .parent
= TYPE_SYS_BUS_DEVICE
,
308 .instance_size
= sizeof(MilkymistMemcardState
),
309 .class_init
= milkymist_memcard_class_init
,
312 static void milkymist_memcard_register_types(void)
314 type_register_static(&milkymist_memcard_info
);
317 type_init(milkymist_memcard_register_types
)