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/block-backend.h"
15 #include "sysemu/blockdev.h"
16 #include "hw/ssi/ssi.h"
18 #include "qapi/error.h"
20 //#define DEBUG_SSI_SD 1
23 #define DPRINTF(fmt, ...) \
24 do { printf("ssi_sd: " fmt , ## __VA_ARGS__); } while (0)
25 #define BADF(fmt, ...) \
26 do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
28 #define DPRINTF(fmt, ...) do {} while(0)
29 #define BADF(fmt, ...) \
30 do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__);} while (0)
53 #define TYPE_SSI_SD "ssi-sd"
54 #define SSI_SD(obj) OBJECT_CHECK(ssi_sd_state, (obj), TYPE_SSI_SD)
56 /* State word bits. */
57 #define SSI_SDR_LOCKED 0x0001
58 #define SSI_SDR_WP_ERASE 0x0002
59 #define SSI_SDR_ERROR 0x0004
60 #define SSI_SDR_CC_ERROR 0x0008
61 #define SSI_SDR_ECC_FAILED 0x0010
62 #define SSI_SDR_WP_VIOLATION 0x0020
63 #define SSI_SDR_ERASE_PARAM 0x0040
64 #define SSI_SDR_OUT_OF_RANGE 0x0080
65 #define SSI_SDR_IDLE 0x0100
66 #define SSI_SDR_ERASE_RESET 0x0200
67 #define SSI_SDR_ILLEGAL_COMMAND 0x0400
68 #define SSI_SDR_COM_CRC_ERROR 0x0800
69 #define SSI_SDR_ERASE_SEQ_ERROR 0x1000
70 #define SSI_SDR_ADDRESS_ERROR 0x2000
71 #define SSI_SDR_PARAMETER_ERROR 0x4000
73 static uint32_t ssi_sd_transfer(SSISlave *dev, uint32_t val)
75 ssi_sd_state *s = FROM_SSI_SLAVE(ssi_sd_state, dev);
77 /* Special case: allow CMD12 (STOP TRANSMISSION) while reading data. */
78 if (s->mode == SSI_SD_DATA_READ && val == 0x4d) {
80 /* There must be at least one byte delay before the card responds. */
87 DPRINTF("NULL command\n");
91 s->mode = SSI_SD_CMDARG;
98 /* FIXME: Check CRC. */
100 request.arg = (s->cmdarg[0] << 24) | (s->cmdarg[1] << 16)
101 | (s->cmdarg[2] << 8) | s->cmdarg[3];
102 DPRINTF("CMD%d arg 0x%08x\n", s->cmd, request.arg);
103 s->arglen = sd_do_command(s->sd, &request, longresp);
104 if (s->arglen <= 0) {
107 DPRINTF("SD command failed\n");
108 } else if (s->cmd == 58) {
109 /* CMD58 returns R3 response (OCR) */
110 DPRINTF("Returned OCR\n");
113 memcpy(&s->response[1], longresp, 4);
114 } else if (s->arglen != 4) {
115 BADF("Unexpected response to cmd %d\n", s->cmd);
116 /* Illegal command is about as near as we can get. */
120 /* All other commands return status. */
123 /* CMD13 returns a 2-byte statuse work. Other commands
124 only return the first byte. */
125 s->arglen = (s->cmd == 13) ? 2 : 1;
126 cardstatus = (longresp[0] << 24) | (longresp[1] << 16)
127 | (longresp[2] << 8) | longresp[3];
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. */
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;
168 s->cmdarg[s->arglen++] = val;
171 case SSI_SD_RESPONSE:
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 (sd_data_ready(s->sd)) {
181 DPRINTF("Data read\n");
182 s->mode = SSI_SD_DATA_START;
184 DPRINTF("End of command\n");
185 s->mode = SSI_SD_CMD;
188 case SSI_SD_DATA_START:
189 DPRINTF("Start read block\n");
190 s->mode = SSI_SD_DATA_READ;
192 case SSI_SD_DATA_READ:
193 val = sd_read_data(s->sd);
194 if (!sd_data_ready(s->sd)) {
195 DPRINTF("Data read end\n");
196 s->mode = SSI_SD_CMD;
200 /* Should never happen. */
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) {
211 if (s->mode == SSI_SD_CMDARG &&
212 (s->arglen < 0 || s->arglen >= ARRAY_SIZE(s->cmdarg))) {
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)))) {
224 static const VMStateDescription vmstate_ssi_sd = {
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);
247 /* FIXME use a qdev drive property instead of drive_get_next() */
248 dinfo = drive_get_next(IF_SD);
249 s->sd = sd_init(dinfo ? blk_by_legacy_dinfo(dinfo) : NULL, true);
251 error_setg(errp, "Device initialization failed.");
256 static void ssi_sd_reset(DeviceState *dev)
258 ssi_sd_state *s = SSI_SD(dev);
260 s->mode = SSI_SD_CMD;
262 memset(s->cmdarg, 0, sizeof(s->cmdarg));
263 memset(s->response, 0, sizeof(s->response));
268 /* Since we're still using the legacy SD API the card is not plugged
269 * into any bus, and we must reset it manually.
271 device_reset(DEVICE(s->sd));
274 static void ssi_sd_class_init(ObjectClass *klass, void *data)
276 DeviceClass *dc = DEVICE_CLASS(klass);
277 SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
279 k->realize = ssi_sd_realize;
280 k->transfer = ssi_sd_transfer;
281 k->cs_polarity = SSI_CS_LOW;
282 dc->vmsd = &vmstate_ssi_sd;
283 dc->reset = ssi_sd_reset;
286 static const TypeInfo ssi_sd_info = {
288 .parent = TYPE_SSI_SLAVE,
289 .instance_size = sizeof(ssi_sd_state),
290 .class_init = ssi_sd_class_init,
293 static void ssi_sd_register_types(void)
295 type_register_static(&ssi_sd_info);
298 type_init(ssi_sd_register_types)