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
28 #include "qemu-error.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 struct MilkymistMemcardState
{
65 int command_write_ptr
;
66 int response_read_ptr
;
74 typedef struct MilkymistMemcardState MilkymistMemcardState
;
76 static void update_pending_bits(MilkymistMemcardState
*s
)
78 /* transmits are instantaneous, thus tx pending bits are never set */
79 s
->regs
[R_PENDING
] = 0;
80 /* if rx is enabled the corresponding pending bits are always set */
81 if (s
->regs
[R_ENABLE
] & ENABLE_CMD_RX
) {
82 s
->regs
[R_PENDING
] |= PENDING_CMD_RX
;
84 if (s
->regs
[R_ENABLE
] & ENABLE_DAT_RX
) {
85 s
->regs
[R_PENDING
] |= PENDING_DAT_RX
;
89 static void memcard_sd_command(MilkymistMemcardState
*s
)
93 req
.cmd
= s
->command
[0] & 0x3f;
94 req
.arg
= (s
->command
[1] << 24) | (s
->command
[2] << 16)
95 | (s
->command
[3] << 8) | s
->command
[4];
96 req
.crc
= s
->command
[5];
98 s
->response
[0] = req
.cmd
;
99 s
->response_len
= sd_do_command(s
->card
, &req
, s
->response
+1);
100 s
->response_read_ptr
= 0;
102 if (s
->response_len
== 16) {
104 s
->response
[0] = 0x3f;
105 s
->response_len
+= 1;
106 } else if (s
->response_len
== 4) {
107 /* no crc calculation, insert dummy byte */
109 s
->response_len
+= 2;
113 /* next write is a dummy byte to clock the initialization of the sd
115 s
->ignore_next_cmd
= 1;
119 static uint32_t memcard_read(void *opaque
, target_phys_addr_t addr
)
121 MilkymistMemcardState
*s
= opaque
;
130 r
= s
->response
[s
->response_read_ptr
++];
131 if (s
->response_read_ptr
> s
->response_len
) {
132 error_report("milkymist_memcard: "
133 "read more cmd bytes than available. Clipping.");
134 s
->response_read_ptr
= 0;
143 r
|= sd_read_data(s
->card
) << 24;
144 r
|= sd_read_data(s
->card
) << 16;
145 r
|= sd_read_data(s
->card
) << 8;
146 r
|= sd_read_data(s
->card
);
157 error_report("milkymist_memcard: read access to unkown register 0x"
158 TARGET_FMT_plx
, addr
<< 2);
162 trace_milkymist_memcard_memory_read(addr
<< 2, r
);
167 static void memcard_write(void *opaque
, target_phys_addr_t addr
, uint32_t value
)
169 MilkymistMemcardState
*s
= opaque
;
171 trace_milkymist_memcard_memory_write(addr
, value
);
176 /* clear rx pending bits */
177 s
->regs
[R_PENDING
] &= ~(value
& (PENDING_CMD_RX
| PENDING_DAT_RX
));
178 update_pending_bits(s
);
184 if (s
->ignore_next_cmd
) {
185 s
->ignore_next_cmd
= 0;
188 s
->command
[s
->command_write_ptr
] = value
& 0xff;
189 s
->command_write_ptr
= (s
->command_write_ptr
+ 1) % 6;
190 if (s
->command_write_ptr
== 0) {
191 memcard_sd_command(s
);
198 sd_write_data(s
->card
, (value
>> 24) & 0xff);
199 sd_write_data(s
->card
, (value
>> 16) & 0xff);
200 sd_write_data(s
->card
, (value
>> 8) & 0xff);
201 sd_write_data(s
->card
, value
& 0xff);
204 s
->regs
[addr
] = value
;
205 update_pending_bits(s
);
209 s
->regs
[addr
] = value
;
213 error_report("milkymist_memcard: write access to unkown register 0x"
214 TARGET_FMT_plx
, addr
<< 2);
219 static CPUReadMemoryFunc
* const memcard_read_fn
[] = {
225 static CPUWriteMemoryFunc
* const memcard_write_fn
[] = {
231 static void milkymist_memcard_reset(DeviceState
*d
)
233 MilkymistMemcardState
*s
=
234 container_of(d
, MilkymistMemcardState
, busdev
.qdev
);
237 s
->command_write_ptr
= 0;
238 s
->response_read_ptr
= 0;
241 for (i
= 0; i
< R_MAX
; i
++) {
246 static int milkymist_memcard_init(SysBusDevice
*dev
)
248 MilkymistMemcardState
*s
= FROM_SYSBUS(typeof(*s
), dev
);
252 dinfo
= drive_get_next(IF_SD
);
253 s
->card
= sd_init(dinfo
? dinfo
->bdrv
: NULL
, 0);
254 s
->enabled
= dinfo
? bdrv_is_inserted(dinfo
->bdrv
) : 0;
256 memcard_regs
= cpu_register_io_memory(memcard_read_fn
, memcard_write_fn
, s
,
257 DEVICE_NATIVE_ENDIAN
);
258 sysbus_init_mmio(dev
, R_MAX
* 4, memcard_regs
);
263 static const VMStateDescription vmstate_milkymist_memcard
= {
264 .name
= "milkymist-memcard",
266 .minimum_version_id
= 1,
267 .minimum_version_id_old
= 1,
268 .fields
= (VMStateField
[]) {
269 VMSTATE_INT32(command_write_ptr
, MilkymistMemcardState
),
270 VMSTATE_INT32(response_read_ptr
, MilkymistMemcardState
),
271 VMSTATE_INT32(response_len
, MilkymistMemcardState
),
272 VMSTATE_INT32(ignore_next_cmd
, MilkymistMemcardState
),
273 VMSTATE_INT32(enabled
, MilkymistMemcardState
),
274 VMSTATE_UINT8_ARRAY(command
, MilkymistMemcardState
, 6),
275 VMSTATE_UINT8_ARRAY(response
, MilkymistMemcardState
, 17),
276 VMSTATE_UINT32_ARRAY(regs
, MilkymistMemcardState
, R_MAX
),
277 VMSTATE_END_OF_LIST()
281 static SysBusDeviceInfo milkymist_memcard_info
= {
282 .init
= milkymist_memcard_init
,
283 .qdev
.name
= "milkymist-memcard",
284 .qdev
.size
= sizeof(MilkymistMemcardState
),
285 .qdev
.vmsd
= &vmstate_milkymist_memcard
,
286 .qdev
.reset
= milkymist_memcard_reset
,
289 static void milkymist_memcard_register(void)
291 sysbus_register_withprop(&milkymist_memcard_info
);
294 device_init(milkymist_memcard_register
)