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"
35 #include "qom/object.h"
38 ENABLE_CMD_TX
= (1<<0),
39 ENABLE_CMD_RX
= (1<<1),
40 ENABLE_DAT_TX
= (1<<2),
41 ENABLE_DAT_RX
= (1<<3),
45 PENDING_CMD_TX
= (1<<0),
46 PENDING_CMD_RX
= (1<<1),
47 PENDING_DAT_TX
= (1<<2),
48 PENDING_DAT_RX
= (1<<3),
52 START_CMD_TX
= (1<<0),
53 START_DAT_RX
= (1<<1),
66 #define TYPE_MILKYMIST_MEMCARD "milkymist-memcard"
67 OBJECT_DECLARE_SIMPLE_TYPE(MilkymistMemcardState
, 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
;
87 static void update_pending_bits(MilkymistMemcardState
*s
)
89 /* transmits are instantaneous, thus tx pending bits are never set */
90 s
->regs
[R_PENDING
] = 0;
91 /* if rx is enabled the corresponding pending bits are always set */
92 if (s
->regs
[R_ENABLE
] & ENABLE_CMD_RX
) {
93 s
->regs
[R_PENDING
] |= PENDING_CMD_RX
;
95 if (s
->regs
[R_ENABLE
] & ENABLE_DAT_RX
) {
96 s
->regs
[R_PENDING
] |= PENDING_DAT_RX
;
100 static void memcard_sd_command(MilkymistMemcardState
*s
)
104 req
.cmd
= s
->command
[0] & 0x3f;
105 req
.arg
= ldl_be_p(s
->command
+ 1);
106 req
.crc
= s
->command
[5];
108 s
->response
[0] = req
.cmd
;
109 s
->response_len
= sdbus_do_command(&s
->sdbus
, &req
, s
->response
+ 1);
110 s
->response_read_ptr
= 0;
112 if (s
->response_len
== 16) {
114 s
->response
[0] = 0x3f;
115 s
->response_len
+= 1;
116 } else if (s
->response_len
== 4) {
117 /* no crc calculation, insert dummy byte */
119 s
->response_len
+= 2;
123 /* next write is a dummy byte to clock the initialization of the sd
125 s
->ignore_next_cmd
= 1;
129 static uint64_t memcard_read(void *opaque
, hwaddr addr
,
132 MilkymistMemcardState
*s
= opaque
;
141 r
= s
->response
[s
->response_read_ptr
++];
142 if (s
->response_read_ptr
> s
->response_len
) {
143 qemu_log_mask(LOG_GUEST_ERROR
, "milkymist_memcard: "
144 "read more cmd bytes than available: clipping\n");
145 s
->response_read_ptr
= 0;
153 sdbus_read_data(&s
->sdbus
, &r
, sizeof(r
));
165 qemu_log_mask(LOG_UNIMP
, "milkymist_memcard: "
166 "read access to unknown register 0x%" HWADDR_PRIx
"\n",
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
;
182 trace_milkymist_memcard_memory_write(addr
, value
);
187 /* clear rx pending bits */
188 s
->regs
[R_PENDING
] &= ~(value
& (PENDING_CMD_RX
| PENDING_DAT_RX
));
189 update_pending_bits(s
);
195 if (s
->ignore_next_cmd
) {
196 s
->ignore_next_cmd
= 0;
199 s
->command
[s
->command_write_ptr
] = value
& 0xff;
200 s
->command_write_ptr
= (s
->command_write_ptr
+ 1) % 6;
201 if (s
->command_write_ptr
== 0) {
202 memcard_sd_command(s
);
209 val32
= cpu_to_be32(value
);
210 sdbus_write_data(&s
->sdbus
, &val32
, sizeof(val32
));
213 s
->regs
[addr
] = value
;
214 update_pending_bits(s
);
218 s
->regs
[addr
] = value
;
222 qemu_log_mask(LOG_UNIMP
, "milkymist_memcard: "
223 "write access to unknown register 0x%" HWADDR_PRIx
" "
224 "(value 0x%" PRIx64
")\n", addr
<< 2, value
);
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 void milkymist_memcard_set_readonly(DeviceState
*dev
, bool level
)
255 qemu_log_mask(LOG_UNIMP
,
256 "milkymist_memcard: read-only mode not supported\n");
259 static void milkymist_memcard_set_inserted(DeviceState
*dev
, bool level
)
261 MilkymistMemcardState
*s
= MILKYMIST_MEMCARD(dev
);
263 s
->enabled
= !!level
;
266 static void milkymist_memcard_init(Object
*obj
)
268 MilkymistMemcardState
*s
= MILKYMIST_MEMCARD(obj
);
269 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
271 memory_region_init_io(&s
->regs_region
, OBJECT(s
), &memcard_mmio_ops
, s
,
272 "milkymist-memcard", R_MAX
* 4);
273 sysbus_init_mmio(dev
, &s
->regs_region
);
275 qbus_create_inplace(&s
->sdbus
, sizeof(s
->sdbus
), TYPE_SD_BUS
,
276 DEVICE(obj
), "sd-bus");
279 static const VMStateDescription vmstate_milkymist_memcard
= {
280 .name
= "milkymist-memcard",
282 .minimum_version_id
= 1,
283 .fields
= (VMStateField
[]) {
284 VMSTATE_INT32(command_write_ptr
, MilkymistMemcardState
),
285 VMSTATE_INT32(response_read_ptr
, MilkymistMemcardState
),
286 VMSTATE_INT32(response_len
, MilkymistMemcardState
),
287 VMSTATE_INT32(ignore_next_cmd
, MilkymistMemcardState
),
288 VMSTATE_INT32(enabled
, MilkymistMemcardState
),
289 VMSTATE_UINT8_ARRAY(command
, MilkymistMemcardState
, 6),
290 VMSTATE_UINT8_ARRAY(response
, MilkymistMemcardState
, 17),
291 VMSTATE_UINT32_ARRAY(regs
, MilkymistMemcardState
, R_MAX
),
292 VMSTATE_END_OF_LIST()
296 static void milkymist_memcard_class_init(ObjectClass
*klass
, void *data
)
298 DeviceClass
*dc
= DEVICE_CLASS(klass
);
300 dc
->reset
= milkymist_memcard_reset
;
301 dc
->vmsd
= &vmstate_milkymist_memcard
;
302 /* Reason: output IRQs should be wired up */
303 dc
->user_creatable
= false;
306 static const TypeInfo milkymist_memcard_info
= {
307 .name
= TYPE_MILKYMIST_MEMCARD
,
308 .parent
= TYPE_SYS_BUS_DEVICE
,
309 .instance_size
= sizeof(MilkymistMemcardState
),
310 .instance_init
= milkymist_memcard_init
,
311 .class_init
= milkymist_memcard_class_init
,
314 static void milkymist_sdbus_class_init(ObjectClass
*klass
, void *data
)
316 SDBusClass
*sbc
= SD_BUS_CLASS(klass
);
318 sbc
->set_inserted
= milkymist_memcard_set_inserted
;
319 sbc
->set_readonly
= milkymist_memcard_set_readonly
;
322 static const TypeInfo milkymist_sdbus_info
= {
323 .name
= TYPE_MILKYMIST_SDBUS
,
324 .parent
= TYPE_SD_BUS
,
325 .instance_size
= sizeof(SDBus
),
326 .class_init
= milkymist_sdbus_class_init
,
329 static void milkymist_memcard_register_types(void)
331 type_register_static(&milkymist_memcard_info
);
332 type_register_static(&milkymist_sdbus_info
);
335 type_init(milkymist_memcard_register_types
)