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/blockdev.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 #define TYPE_MILKYMIST_MEMCARD "milkymist-memcard"
62 #define MILKYMIST_MEMCARD(obj) \
63 OBJECT_CHECK(MilkymistMemcardState, (obj), TYPE_MILKYMIST_MEMCARD)
65 struct MilkymistMemcardState
{
66 SysBusDevice parent_obj
;
68 MemoryRegion regs_region
;
71 int command_write_ptr
;
72 int response_read_ptr
;
80 typedef struct MilkymistMemcardState MilkymistMemcardState
;
82 static void update_pending_bits(MilkymistMemcardState
*s
)
84 /* transmits are instantaneous, thus tx pending bits are never set */
85 s
->regs
[R_PENDING
] = 0;
86 /* if rx is enabled the corresponding pending bits are always set */
87 if (s
->regs
[R_ENABLE
] & ENABLE_CMD_RX
) {
88 s
->regs
[R_PENDING
] |= PENDING_CMD_RX
;
90 if (s
->regs
[R_ENABLE
] & ENABLE_DAT_RX
) {
91 s
->regs
[R_PENDING
] |= PENDING_DAT_RX
;
95 static void memcard_sd_command(MilkymistMemcardState
*s
)
99 req
.cmd
= s
->command
[0] & 0x3f;
100 req
.arg
= (s
->command
[1] << 24) | (s
->command
[2] << 16)
101 | (s
->command
[3] << 8) | s
->command
[4];
102 req
.crc
= s
->command
[5];
104 s
->response
[0] = req
.cmd
;
105 s
->response_len
= sd_do_command(s
->card
, &req
, s
->response
+1);
106 s
->response_read_ptr
= 0;
108 if (s
->response_len
== 16) {
110 s
->response
[0] = 0x3f;
111 s
->response_len
+= 1;
112 } else if (s
->response_len
== 4) {
113 /* no crc calculation, insert dummy byte */
115 s
->response_len
+= 2;
119 /* next write is a dummy byte to clock the initialization of the sd
121 s
->ignore_next_cmd
= 1;
125 static uint64_t memcard_read(void *opaque
, hwaddr addr
,
128 MilkymistMemcardState
*s
= opaque
;
137 r
= s
->response
[s
->response_read_ptr
++];
138 if (s
->response_read_ptr
> s
->response_len
) {
139 error_report("milkymist_memcard: "
140 "read more cmd bytes than available. Clipping.");
141 s
->response_read_ptr
= 0;
150 r
|= sd_read_data(s
->card
) << 24;
151 r
|= sd_read_data(s
->card
) << 16;
152 r
|= sd_read_data(s
->card
) << 8;
153 r
|= sd_read_data(s
->card
);
164 error_report("milkymist_memcard: read access to unknown register 0x"
165 TARGET_FMT_plx
, addr
<< 2);
169 trace_milkymist_memcard_memory_read(addr
<< 2, r
);
174 static void memcard_write(void *opaque
, hwaddr addr
, uint64_t value
,
177 MilkymistMemcardState
*s
= opaque
;
179 trace_milkymist_memcard_memory_write(addr
, value
);
184 /* clear rx pending bits */
185 s
->regs
[R_PENDING
] &= ~(value
& (PENDING_CMD_RX
| PENDING_DAT_RX
));
186 update_pending_bits(s
);
192 if (s
->ignore_next_cmd
) {
193 s
->ignore_next_cmd
= 0;
196 s
->command
[s
->command_write_ptr
] = value
& 0xff;
197 s
->command_write_ptr
= (s
->command_write_ptr
+ 1) % 6;
198 if (s
->command_write_ptr
== 0) {
199 memcard_sd_command(s
);
206 sd_write_data(s
->card
, (value
>> 24) & 0xff);
207 sd_write_data(s
->card
, (value
>> 16) & 0xff);
208 sd_write_data(s
->card
, (value
>> 8) & 0xff);
209 sd_write_data(s
->card
, value
& 0xff);
212 s
->regs
[addr
] = value
;
213 update_pending_bits(s
);
217 s
->regs
[addr
] = value
;
221 error_report("milkymist_memcard: write access to unknown register 0x"
222 TARGET_FMT_plx
, addr
<< 2);
227 static const MemoryRegionOps memcard_mmio_ops
= {
228 .read
= memcard_read
,
229 .write
= memcard_write
,
231 .min_access_size
= 4,
232 .max_access_size
= 4,
234 .endianness
= DEVICE_NATIVE_ENDIAN
,
237 static void milkymist_memcard_reset(DeviceState
*d
)
239 MilkymistMemcardState
*s
= MILKYMIST_MEMCARD(d
);
242 s
->command_write_ptr
= 0;
243 s
->response_read_ptr
= 0;
246 for (i
= 0; i
< R_MAX
; i
++) {
251 static int milkymist_memcard_init(SysBusDevice
*dev
)
253 MilkymistMemcardState
*s
= MILKYMIST_MEMCARD(dev
);
256 dinfo
= drive_get_next(IF_SD
);
257 s
->card
= sd_init(dinfo
? dinfo
->bdrv
: NULL
, false);
258 s
->enabled
= dinfo
? bdrv_is_inserted(dinfo
->bdrv
) : 0;
260 memory_region_init_io(&s
->regs_region
, OBJECT(s
), &memcard_mmio_ops
, s
,
261 "milkymist-memcard", R_MAX
* 4);
262 sysbus_init_mmio(dev
, &s
->regs_region
);
267 static const VMStateDescription vmstate_milkymist_memcard
= {
268 .name
= "milkymist-memcard",
270 .minimum_version_id
= 1,
271 .minimum_version_id_old
= 1,
272 .fields
= (VMStateField
[]) {
273 VMSTATE_INT32(command_write_ptr
, MilkymistMemcardState
),
274 VMSTATE_INT32(response_read_ptr
, MilkymistMemcardState
),
275 VMSTATE_INT32(response_len
, MilkymistMemcardState
),
276 VMSTATE_INT32(ignore_next_cmd
, MilkymistMemcardState
),
277 VMSTATE_INT32(enabled
, MilkymistMemcardState
),
278 VMSTATE_UINT8_ARRAY(command
, MilkymistMemcardState
, 6),
279 VMSTATE_UINT8_ARRAY(response
, MilkymistMemcardState
, 17),
280 VMSTATE_UINT32_ARRAY(regs
, MilkymistMemcardState
, R_MAX
),
281 VMSTATE_END_OF_LIST()
285 static void milkymist_memcard_class_init(ObjectClass
*klass
, void *data
)
287 DeviceClass
*dc
= DEVICE_CLASS(klass
);
288 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
290 k
->init
= milkymist_memcard_init
;
291 dc
->reset
= milkymist_memcard_reset
;
292 dc
->vmsd
= &vmstate_milkymist_memcard
;
295 static const TypeInfo milkymist_memcard_info
= {
296 .name
= TYPE_MILKYMIST_MEMCARD
,
297 .parent
= TYPE_SYS_BUS_DEVICE
,
298 .instance_size
= sizeof(MilkymistMemcardState
),
299 .class_init
= milkymist_memcard_class_init
,
302 static void milkymist_memcard_register_types(void)
304 type_register_static(&milkymist_memcard_info
);
307 type_init(milkymist_memcard_register_types
)