vl: merge -accel processing into configure_accelerators
[qemu/ar7.git] / hw / sd / ssi-sd.c
blob91db069212fa1ca6c3849c083e3d1997128fff40
1 /*
2 * SSI to SD card adapter.
4 * Copyright (c) 2007-2009 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GNU GPL v2.
9 * Contributions after 2012-01-13 are licensed under the terms of the
10 * GNU GPL, version 2 or (at your option) any later version.
13 #include "qemu/osdep.h"
14 #include "sysemu/blockdev.h"
15 #include "hw/ssi/ssi.h"
16 #include "migration/vmstate.h"
17 #include "hw/qdev-properties.h"
18 #include "hw/sd/sd.h"
19 #include "qapi/error.h"
20 #include "qemu/module.h"
22 //#define DEBUG_SSI_SD 1
24 #ifdef DEBUG_SSI_SD
25 #define DPRINTF(fmt, ...) \
26 do { printf("ssi_sd: " fmt , ## __VA_ARGS__); } while (0)
27 #define BADF(fmt, ...) \
28 do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
29 #else
30 #define DPRINTF(fmt, ...) do {} while(0)
31 #define BADF(fmt, ...) \
32 do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__);} while (0)
33 #endif
35 typedef enum {
36 SSI_SD_CMD = 0,
37 SSI_SD_CMDARG,
38 SSI_SD_RESPONSE,
39 SSI_SD_DATA_START,
40 SSI_SD_DATA_READ,
41 } ssi_sd_mode;
43 typedef struct {
44 SSISlave ssidev;
45 uint32_t mode;
46 int cmd;
47 uint8_t cmdarg[4];
48 uint8_t response[5];
49 int32_t arglen;
50 int32_t response_pos;
51 int32_t stopping;
52 SDBus sdbus;
53 } ssi_sd_state;
55 #define TYPE_SSI_SD "ssi-sd"
56 #define SSI_SD(obj) OBJECT_CHECK(ssi_sd_state, (obj), TYPE_SSI_SD)
58 /* State word bits. */
59 #define SSI_SDR_LOCKED 0x0001
60 #define SSI_SDR_WP_ERASE 0x0002
61 #define SSI_SDR_ERROR 0x0004
62 #define SSI_SDR_CC_ERROR 0x0008
63 #define SSI_SDR_ECC_FAILED 0x0010
64 #define SSI_SDR_WP_VIOLATION 0x0020
65 #define SSI_SDR_ERASE_PARAM 0x0040
66 #define SSI_SDR_OUT_OF_RANGE 0x0080
67 #define SSI_SDR_IDLE 0x0100
68 #define SSI_SDR_ERASE_RESET 0x0200
69 #define SSI_SDR_ILLEGAL_COMMAND 0x0400
70 #define SSI_SDR_COM_CRC_ERROR 0x0800
71 #define SSI_SDR_ERASE_SEQ_ERROR 0x1000
72 #define SSI_SDR_ADDRESS_ERROR 0x2000
73 #define SSI_SDR_PARAMETER_ERROR 0x4000
75 static uint32_t ssi_sd_transfer(SSISlave *dev, uint32_t val)
77 ssi_sd_state *s = FROM_SSI_SLAVE(ssi_sd_state, dev);
79 /* Special case: allow CMD12 (STOP TRANSMISSION) while reading data. */
80 if (s->mode == SSI_SD_DATA_READ && val == 0x4d) {
81 s->mode = SSI_SD_CMD;
82 /* There must be at least one byte delay before the card responds. */
83 s->stopping = 1;
86 switch (s->mode) {
87 case SSI_SD_CMD:
88 if (val == 0xff) {
89 DPRINTF("NULL command\n");
90 return 0xff;
92 s->cmd = val & 0x3f;
93 s->mode = SSI_SD_CMDARG;
94 s->arglen = 0;
95 return 0xff;
96 case SSI_SD_CMDARG:
97 if (s->arglen == 4) {
98 SDRequest request;
99 uint8_t longresp[16];
100 /* FIXME: Check CRC. */
101 request.cmd = s->cmd;
102 request.arg = ldl_be_p(s->cmdarg);
103 DPRINTF("CMD%d arg 0x%08x\n", s->cmd, request.arg);
104 s->arglen = sdbus_do_command(&s->sdbus, &request, longresp);
105 if (s->arglen <= 0) {
106 s->arglen = 1;
107 s->response[0] = 4;
108 DPRINTF("SD command failed\n");
109 } else if (s->cmd == 58) {
110 /* CMD58 returns R3 response (OCR) */
111 DPRINTF("Returned OCR\n");
112 s->arglen = 5;
113 s->response[0] = 1;
114 memcpy(&s->response[1], longresp, 4);
115 } else if (s->arglen != 4) {
116 BADF("Unexpected response to cmd %d\n", s->cmd);
117 /* Illegal command is about as near as we can get. */
118 s->arglen = 1;
119 s->response[0] = 4;
120 } else {
121 /* All other commands return status. */
122 uint32_t cardstatus;
123 uint16_t status;
124 /* CMD13 returns a 2-byte statuse work. Other commands
125 only return the first byte. */
126 s->arglen = (s->cmd == 13) ? 2 : 1;
127 cardstatus = ldl_be_p(longresp);
128 status = 0;
129 if (((cardstatus >> 9) & 0xf) < 4)
130 status |= SSI_SDR_IDLE;
131 if (cardstatus & ERASE_RESET)
132 status |= SSI_SDR_ERASE_RESET;
133 if (cardstatus & ILLEGAL_COMMAND)
134 status |= SSI_SDR_ILLEGAL_COMMAND;
135 if (cardstatus & COM_CRC_ERROR)
136 status |= SSI_SDR_COM_CRC_ERROR;
137 if (cardstatus & ERASE_SEQ_ERROR)
138 status |= SSI_SDR_ERASE_SEQ_ERROR;
139 if (cardstatus & ADDRESS_ERROR)
140 status |= SSI_SDR_ADDRESS_ERROR;
141 if (cardstatus & CARD_IS_LOCKED)
142 status |= SSI_SDR_LOCKED;
143 if (cardstatus & (LOCK_UNLOCK_FAILED | WP_ERASE_SKIP))
144 status |= SSI_SDR_WP_ERASE;
145 if (cardstatus & SD_ERROR)
146 status |= SSI_SDR_ERROR;
147 if (cardstatus & CC_ERROR)
148 status |= SSI_SDR_CC_ERROR;
149 if (cardstatus & CARD_ECC_FAILED)
150 status |= SSI_SDR_ECC_FAILED;
151 if (cardstatus & WP_VIOLATION)
152 status |= SSI_SDR_WP_VIOLATION;
153 if (cardstatus & ERASE_PARAM)
154 status |= SSI_SDR_ERASE_PARAM;
155 if (cardstatus & (OUT_OF_RANGE | CID_CSD_OVERWRITE))
156 status |= SSI_SDR_OUT_OF_RANGE;
157 /* ??? Don't know what Parameter Error really means, so
158 assume it's set if the second byte is nonzero. */
159 if (status & 0xff)
160 status |= SSI_SDR_PARAMETER_ERROR;
161 s->response[0] = status >> 8;
162 s->response[1] = status;
163 DPRINTF("Card status 0x%02x\n", status);
165 s->mode = SSI_SD_RESPONSE;
166 s->response_pos = 0;
167 } else {
168 s->cmdarg[s->arglen++] = val;
170 return 0xff;
171 case SSI_SD_RESPONSE:
172 if (s->stopping) {
173 s->stopping = 0;
174 return 0xff;
176 if (s->response_pos < s->arglen) {
177 DPRINTF("Response 0x%02x\n", s->response[s->response_pos]);
178 return s->response[s->response_pos++];
180 if (sdbus_data_ready(&s->sdbus)) {
181 DPRINTF("Data read\n");
182 s->mode = SSI_SD_DATA_START;
183 } else {
184 DPRINTF("End of command\n");
185 s->mode = SSI_SD_CMD;
187 return 0xff;
188 case SSI_SD_DATA_START:
189 DPRINTF("Start read block\n");
190 s->mode = SSI_SD_DATA_READ;
191 return 0xfe;
192 case SSI_SD_DATA_READ:
193 val = sdbus_read_data(&s->sdbus);
194 if (!sdbus_data_ready(&s->sdbus)) {
195 DPRINTF("Data read end\n");
196 s->mode = SSI_SD_CMD;
198 return val;
200 /* Should never happen. */
201 return 0xff;
204 static int ssi_sd_post_load(void *opaque, int version_id)
206 ssi_sd_state *s = (ssi_sd_state *)opaque;
208 if (s->mode > SSI_SD_DATA_READ) {
209 return -EINVAL;
211 if (s->mode == SSI_SD_CMDARG &&
212 (s->arglen < 0 || s->arglen >= ARRAY_SIZE(s->cmdarg))) {
213 return -EINVAL;
215 if (s->mode == SSI_SD_RESPONSE &&
216 (s->response_pos < 0 || s->response_pos >= ARRAY_SIZE(s->response) ||
217 (!s->stopping && s->arglen > ARRAY_SIZE(s->response)))) {
218 return -EINVAL;
221 return 0;
224 static const VMStateDescription vmstate_ssi_sd = {
225 .name = "ssi_sd",
226 .version_id = 2,
227 .minimum_version_id = 2,
228 .post_load = ssi_sd_post_load,
229 .fields = (VMStateField []) {
230 VMSTATE_UINT32(mode, ssi_sd_state),
231 VMSTATE_INT32(cmd, ssi_sd_state),
232 VMSTATE_UINT8_ARRAY(cmdarg, ssi_sd_state, 4),
233 VMSTATE_UINT8_ARRAY(response, ssi_sd_state, 5),
234 VMSTATE_INT32(arglen, ssi_sd_state),
235 VMSTATE_INT32(response_pos, ssi_sd_state),
236 VMSTATE_INT32(stopping, ssi_sd_state),
237 VMSTATE_SSI_SLAVE(ssidev, ssi_sd_state),
238 VMSTATE_END_OF_LIST()
242 static void ssi_sd_realize(SSISlave *d, Error **errp)
244 ssi_sd_state *s = FROM_SSI_SLAVE(ssi_sd_state, d);
245 DeviceState *carddev;
246 DriveInfo *dinfo;
247 Error *err = NULL;
249 qbus_create_inplace(&s->sdbus, sizeof(s->sdbus), TYPE_SD_BUS,
250 DEVICE(d), "sd-bus");
252 /* Create and plug in the sd card */
253 /* FIXME use a qdev drive property instead of drive_get_next() */
254 dinfo = drive_get_next(IF_SD);
255 carddev = qdev_create(BUS(&s->sdbus), TYPE_SD_CARD);
256 if (dinfo) {
257 qdev_prop_set_drive(carddev, "drive", blk_by_legacy_dinfo(dinfo), &err);
259 object_property_set_bool(OBJECT(carddev), true, "spi", &err);
260 object_property_set_bool(OBJECT(carddev), true, "realized", &err);
261 if (err) {
262 error_setg(errp, "failed to init SD card: %s", error_get_pretty(err));
263 return;
267 static void ssi_sd_reset(DeviceState *dev)
269 ssi_sd_state *s = SSI_SD(dev);
271 s->mode = SSI_SD_CMD;
272 s->cmd = 0;
273 memset(s->cmdarg, 0, sizeof(s->cmdarg));
274 memset(s->response, 0, sizeof(s->response));
275 s->arglen = 0;
276 s->response_pos = 0;
277 s->stopping = 0;
280 static void ssi_sd_class_init(ObjectClass *klass, void *data)
282 DeviceClass *dc = DEVICE_CLASS(klass);
283 SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
285 k->realize = ssi_sd_realize;
286 k->transfer = ssi_sd_transfer;
287 k->cs_polarity = SSI_CS_LOW;
288 dc->vmsd = &vmstate_ssi_sd;
289 dc->reset = ssi_sd_reset;
290 /* Reason: init() method uses drive_get_next() */
291 dc->user_creatable = false;
294 static const TypeInfo ssi_sd_info = {
295 .name = TYPE_SSI_SD,
296 .parent = TYPE_SSI_SLAVE,
297 .instance_size = sizeof(ssi_sd_state),
298 .class_init = ssi_sd_class_init,
301 static void ssi_sd_register_types(void)
303 type_register_static(&ssi_sd_info);
306 type_init(ssi_sd_register_types)