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 "qemu/module.h"
27 #include "hw/sysbus.h"
28 #include "migration/vmstate.h"
30 #include "qapi/error.h"
31 #include "sysemu/block-backend.h"
32 #include "sysemu/blockdev.h"
33 #include "hw/qdev-properties.h"
37 ENABLE_CMD_TX
= (1<<0),
38 ENABLE_CMD_RX
= (1<<1),
39 ENABLE_DAT_TX
= (1<<2),
40 ENABLE_DAT_RX
= (1<<3),
44 PENDING_CMD_TX
= (1<<0),
45 PENDING_CMD_RX
= (1<<1),
46 PENDING_DAT_TX
= (1<<2),
47 PENDING_DAT_RX
= (1<<3),
51 START_CMD_TX
= (1<<0),
52 START_DAT_RX
= (1<<1),
65 #define TYPE_MILKYMIST_MEMCARD "milkymist-memcard"
66 #define MILKYMIST_MEMCARD(obj) \
67 OBJECT_CHECK(MilkymistMemcardState, (obj), TYPE_MILKYMIST_MEMCARD)
69 #define TYPE_MILKYMIST_SDBUS "milkymist-sdbus"
71 struct MilkymistMemcardState
{
72 SysBusDevice parent_obj
;
74 MemoryRegion regs_region
;
77 int command_write_ptr
;
78 int response_read_ptr
;
86 typedef struct MilkymistMemcardState MilkymistMemcardState
;
88 static void update_pending_bits(MilkymistMemcardState
*s
)
90 /* transmits are instantaneous, thus tx pending bits are never set */
91 s
->regs
[R_PENDING
] = 0;
92 /* if rx is enabled the corresponding pending bits are always set */
93 if (s
->regs
[R_ENABLE
] & ENABLE_CMD_RX
) {
94 s
->regs
[R_PENDING
] |= PENDING_CMD_RX
;
96 if (s
->regs
[R_ENABLE
] & ENABLE_DAT_RX
) {
97 s
->regs
[R_PENDING
] |= PENDING_DAT_RX
;
101 static void memcard_sd_command(MilkymistMemcardState
*s
)
105 req
.cmd
= s
->command
[0] & 0x3f;
106 req
.arg
= ldl_be_p(s
->command
+ 1);
107 req
.crc
= s
->command
[5];
109 s
->response
[0] = req
.cmd
;
110 s
->response_len
= sdbus_do_command(&s
->sdbus
, &req
, s
->response
+ 1);
111 s
->response_read_ptr
= 0;
113 if (s
->response_len
== 16) {
115 s
->response
[0] = 0x3f;
116 s
->response_len
+= 1;
117 } else if (s
->response_len
== 4) {
118 /* no crc calculation, insert dummy byte */
120 s
->response_len
+= 2;
124 /* next write is a dummy byte to clock the initialization of the sd
126 s
->ignore_next_cmd
= 1;
130 static uint64_t memcard_read(void *opaque
, hwaddr addr
,
133 MilkymistMemcardState
*s
= opaque
;
142 r
= s
->response
[s
->response_read_ptr
++];
143 if (s
->response_read_ptr
> s
->response_len
) {
144 qemu_log_mask(LOG_GUEST_ERROR
, "milkymist_memcard: "
145 "read more cmd bytes than available: clipping\n");
146 s
->response_read_ptr
= 0;
154 sdbus_read_data(&s
->sdbus
, &r
, sizeof(r
));
166 qemu_log_mask(LOG_UNIMP
, "milkymist_memcard: "
167 "read access to unknown register 0x%" HWADDR_PRIx
"\n",
172 trace_milkymist_memcard_memory_read(addr
<< 2, r
);
177 static void memcard_write(void *opaque
, hwaddr addr
, uint64_t value
,
180 MilkymistMemcardState
*s
= opaque
;
183 trace_milkymist_memcard_memory_write(addr
, value
);
188 /* clear rx pending bits */
189 s
->regs
[R_PENDING
] &= ~(value
& (PENDING_CMD_RX
| PENDING_DAT_RX
));
190 update_pending_bits(s
);
196 if (s
->ignore_next_cmd
) {
197 s
->ignore_next_cmd
= 0;
200 s
->command
[s
->command_write_ptr
] = value
& 0xff;
201 s
->command_write_ptr
= (s
->command_write_ptr
+ 1) % 6;
202 if (s
->command_write_ptr
== 0) {
203 memcard_sd_command(s
);
210 val32
= cpu_to_be32(value
);
211 sdbus_write_data(&s
->sdbus
, &val32
, sizeof(val32
));
214 s
->regs
[addr
] = value
;
215 update_pending_bits(s
);
219 s
->regs
[addr
] = value
;
223 qemu_log_mask(LOG_UNIMP
, "milkymist_memcard: "
224 "write access to unknown register 0x%" HWADDR_PRIx
" "
225 "(value 0x%" PRIx64
")\n", addr
<< 2, value
);
230 static const MemoryRegionOps memcard_mmio_ops
= {
231 .read
= memcard_read
,
232 .write
= memcard_write
,
234 .min_access_size
= 4,
235 .max_access_size
= 4,
237 .endianness
= DEVICE_NATIVE_ENDIAN
,
240 static void milkymist_memcard_reset(DeviceState
*d
)
242 MilkymistMemcardState
*s
= MILKYMIST_MEMCARD(d
);
245 s
->command_write_ptr
= 0;
246 s
->response_read_ptr
= 0;
249 for (i
= 0; i
< R_MAX
; i
++) {
254 static void milkymist_memcard_set_readonly(DeviceState
*dev
, bool level
)
256 qemu_log_mask(LOG_UNIMP
,
257 "milkymist_memcard: read-only mode not supported\n");
260 static void milkymist_memcard_set_inserted(DeviceState
*dev
, bool level
)
262 MilkymistMemcardState
*s
= MILKYMIST_MEMCARD(dev
);
264 s
->enabled
= !!level
;
267 static void milkymist_memcard_init(Object
*obj
)
269 MilkymistMemcardState
*s
= MILKYMIST_MEMCARD(obj
);
270 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
272 memory_region_init_io(&s
->regs_region
, OBJECT(s
), &memcard_mmio_ops
, s
,
273 "milkymist-memcard", R_MAX
* 4);
274 sysbus_init_mmio(dev
, &s
->regs_region
);
276 qbus_create_inplace(&s
->sdbus
, sizeof(s
->sdbus
), TYPE_SD_BUS
,
277 DEVICE(obj
), "sd-bus");
280 static const VMStateDescription vmstate_milkymist_memcard
= {
281 .name
= "milkymist-memcard",
283 .minimum_version_id
= 1,
284 .fields
= (VMStateField
[]) {
285 VMSTATE_INT32(command_write_ptr
, MilkymistMemcardState
),
286 VMSTATE_INT32(response_read_ptr
, MilkymistMemcardState
),
287 VMSTATE_INT32(response_len
, MilkymistMemcardState
),
288 VMSTATE_INT32(ignore_next_cmd
, MilkymistMemcardState
),
289 VMSTATE_INT32(enabled
, MilkymistMemcardState
),
290 VMSTATE_UINT8_ARRAY(command
, MilkymistMemcardState
, 6),
291 VMSTATE_UINT8_ARRAY(response
, MilkymistMemcardState
, 17),
292 VMSTATE_UINT32_ARRAY(regs
, MilkymistMemcardState
, R_MAX
),
293 VMSTATE_END_OF_LIST()
297 static void milkymist_memcard_class_init(ObjectClass
*klass
, void *data
)
299 DeviceClass
*dc
= DEVICE_CLASS(klass
);
301 dc
->reset
= milkymist_memcard_reset
;
302 dc
->vmsd
= &vmstate_milkymist_memcard
;
303 /* Reason: output IRQs should be wired up */
304 dc
->user_creatable
= false;
307 static const TypeInfo milkymist_memcard_info
= {
308 .name
= TYPE_MILKYMIST_MEMCARD
,
309 .parent
= TYPE_SYS_BUS_DEVICE
,
310 .instance_size
= sizeof(MilkymistMemcardState
),
311 .instance_init
= milkymist_memcard_init
,
312 .class_init
= milkymist_memcard_class_init
,
315 static void milkymist_sdbus_class_init(ObjectClass
*klass
, void *data
)
317 SDBusClass
*sbc
= SD_BUS_CLASS(klass
);
319 sbc
->set_inserted
= milkymist_memcard_set_inserted
;
320 sbc
->set_readonly
= milkymist_memcard_set_readonly
;
323 static const TypeInfo milkymist_sdbus_info
= {
324 .name
= TYPE_MILKYMIST_SDBUS
,
325 .parent
= TYPE_SD_BUS
,
326 .instance_size
= sizeof(SDBus
),
327 .class_init
= milkymist_sdbus_class_init
,
330 static void milkymist_memcard_register_types(void)
332 type_register_static(&milkymist_memcard_info
);
333 type_register_static(&milkymist_sdbus_info
);
336 type_init(milkymist_memcard_register_types
)