kvmvapic: Catch invalid ROM size
[qemu/ar7.git] / hw / sd / milkymist-memcard.c
blob42613b3aff5790b0d36d4c7f16aee30b12bc4a00
1 /*
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
24 #include "hw/hw.h"
25 #include "hw/sysbus.h"
26 #include "sysemu/sysemu.h"
27 #include "trace.h"
28 #include "qemu/error-report.h"
29 #include "sysemu/blockdev.h"
30 #include "hw/sd.h"
32 enum {
33 ENABLE_CMD_TX = (1<<0),
34 ENABLE_CMD_RX = (1<<1),
35 ENABLE_DAT_TX = (1<<2),
36 ENABLE_DAT_RX = (1<<3),
39 enum {
40 PENDING_CMD_TX = (1<<0),
41 PENDING_CMD_RX = (1<<1),
42 PENDING_DAT_TX = (1<<2),
43 PENDING_DAT_RX = (1<<3),
46 enum {
47 START_CMD_TX = (1<<0),
48 START_DAT_RX = (1<<1),
51 enum {
52 R_CLK2XDIV = 0,
53 R_ENABLE,
54 R_PENDING,
55 R_START,
56 R_CMD,
57 R_DAT,
58 R_MAX
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;
69 SDState *card;
71 int command_write_ptr;
72 int response_read_ptr;
73 int response_len;
74 int ignore_next_cmd;
75 int enabled;
76 uint8_t command[6];
77 uint8_t response[17];
78 uint32_t regs[R_MAX];
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)
97 SDRequest req;
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) {
109 /* R2 response */
110 s->response[0] = 0x3f;
111 s->response_len += 1;
112 } else if (s->response_len == 4) {
113 /* no crc calculation, insert dummy byte */
114 s->response[5] = 0;
115 s->response_len += 2;
118 if (req.cmd == 0) {
119 /* next write is a dummy byte to clock the initialization of the sd
120 * card */
121 s->ignore_next_cmd = 1;
125 static uint64_t memcard_read(void *opaque, hwaddr addr,
126 unsigned size)
128 MilkymistMemcardState *s = opaque;
129 uint32_t r = 0;
131 addr >>= 2;
132 switch (addr) {
133 case R_CMD:
134 if (!s->enabled) {
135 r = 0xff;
136 } else {
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;
144 break;
145 case R_DAT:
146 if (!s->enabled) {
147 r = 0xffffffff;
148 } else {
149 r = 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);
155 break;
156 case R_CLK2XDIV:
157 case R_ENABLE:
158 case R_PENDING:
159 case R_START:
160 r = s->regs[addr];
161 break;
163 default:
164 error_report("milkymist_memcard: read access to unknown register 0x"
165 TARGET_FMT_plx, addr << 2);
166 break;
169 trace_milkymist_memcard_memory_read(addr << 2, r);
171 return r;
174 static void memcard_write(void *opaque, hwaddr addr, uint64_t value,
175 unsigned size)
177 MilkymistMemcardState *s = opaque;
179 trace_milkymist_memcard_memory_write(addr, value);
181 addr >>= 2;
182 switch (addr) {
183 case R_PENDING:
184 /* clear rx pending bits */
185 s->regs[R_PENDING] &= ~(value & (PENDING_CMD_RX | PENDING_DAT_RX));
186 update_pending_bits(s);
187 break;
188 case R_CMD:
189 if (!s->enabled) {
190 break;
192 if (s->ignore_next_cmd) {
193 s->ignore_next_cmd = 0;
194 break;
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);
201 break;
202 case R_DAT:
203 if (!s->enabled) {
204 break;
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);
210 break;
211 case R_ENABLE:
212 s->regs[addr] = value;
213 update_pending_bits(s);
214 break;
215 case R_CLK2XDIV:
216 case R_START:
217 s->regs[addr] = value;
218 break;
220 default:
221 error_report("milkymist_memcard: write access to unknown register 0x"
222 TARGET_FMT_plx, addr << 2);
223 break;
227 static const MemoryRegionOps memcard_mmio_ops = {
228 .read = memcard_read,
229 .write = memcard_write,
230 .valid = {
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);
240 int i;
242 s->command_write_ptr = 0;
243 s->response_read_ptr = 0;
244 s->response_len = 0;
246 for (i = 0; i < R_MAX; i++) {
247 s->regs[i] = 0;
251 static int milkymist_memcard_init(SysBusDevice *dev)
253 MilkymistMemcardState *s = MILKYMIST_MEMCARD(dev);
254 DriveInfo *dinfo;
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);
264 return 0;
267 static const VMStateDescription vmstate_milkymist_memcard = {
268 .name = "milkymist-memcard",
269 .version_id = 1,
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)