Merge remote-tracking branch 'remotes/stefanha/tags/block-pull-request' into staging
[qemu-kvm.git] / hw / sd / ssi-sd.c
bloba71fbca7140dd623c2ffa5104855756ee06145ca
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 "sysemu/block-backend.h"
14 #include "sysemu/blockdev.h"
15 #include "hw/ssi.h"
16 #include "hw/sd.h"
18 //#define DEBUG_SSI_SD 1
20 #ifdef DEBUG_SSI_SD
21 #define DPRINTF(fmt, ...) \
22 do { printf("ssi_sd: " fmt , ## __VA_ARGS__); } while (0)
23 #define BADF(fmt, ...) \
24 do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
25 #else
26 #define DPRINTF(fmt, ...) do {} while(0)
27 #define BADF(fmt, ...) \
28 do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__);} while (0)
29 #endif
31 typedef enum {
32 SSI_SD_CMD,
33 SSI_SD_CMDARG,
34 SSI_SD_RESPONSE,
35 SSI_SD_DATA_START,
36 SSI_SD_DATA_READ,
37 } ssi_sd_mode;
39 typedef struct {
40 SSISlave ssidev;
41 ssi_sd_mode mode;
42 int cmd;
43 uint8_t cmdarg[4];
44 uint8_t response[5];
45 int arglen;
46 int response_pos;
47 int stopping;
48 SDState *sd;
49 } ssi_sd_state;
51 /* State word bits. */
52 #define SSI_SDR_LOCKED 0x0001
53 #define SSI_SDR_WP_ERASE 0x0002
54 #define SSI_SDR_ERROR 0x0004
55 #define SSI_SDR_CC_ERROR 0x0008
56 #define SSI_SDR_ECC_FAILED 0x0010
57 #define SSI_SDR_WP_VIOLATION 0x0020
58 #define SSI_SDR_ERASE_PARAM 0x0040
59 #define SSI_SDR_OUT_OF_RANGE 0x0080
60 #define SSI_SDR_IDLE 0x0100
61 #define SSI_SDR_ERASE_RESET 0x0200
62 #define SSI_SDR_ILLEGAL_COMMAND 0x0400
63 #define SSI_SDR_COM_CRC_ERROR 0x0800
64 #define SSI_SDR_ERASE_SEQ_ERROR 0x1000
65 #define SSI_SDR_ADDRESS_ERROR 0x2000
66 #define SSI_SDR_PARAMETER_ERROR 0x4000
68 static uint32_t ssi_sd_transfer(SSISlave *dev, uint32_t val)
70 ssi_sd_state *s = FROM_SSI_SLAVE(ssi_sd_state, dev);
72 /* Special case: allow CMD12 (STOP TRANSMISSION) while reading data. */
73 if (s->mode == SSI_SD_DATA_READ && val == 0x4d) {
74 s->mode = SSI_SD_CMD;
75 /* There must be at least one byte delay before the card responds. */
76 s->stopping = 1;
79 switch (s->mode) {
80 case SSI_SD_CMD:
81 if (val == 0xff) {
82 DPRINTF("NULL command\n");
83 return 0xff;
85 s->cmd = val & 0x3f;
86 s->mode = SSI_SD_CMDARG;
87 s->arglen = 0;
88 return 0xff;
89 case SSI_SD_CMDARG:
90 if (s->arglen == 4) {
91 SDRequest request;
92 uint8_t longresp[16];
93 /* FIXME: Check CRC. */
94 request.cmd = s->cmd;
95 request.arg = (s->cmdarg[0] << 24) | (s->cmdarg[1] << 16)
96 | (s->cmdarg[2] << 8) | s->cmdarg[3];
97 DPRINTF("CMD%d arg 0x%08x\n", s->cmd, request.arg);
98 s->arglen = sd_do_command(s->sd, &request, longresp);
99 if (s->arglen <= 0) {
100 s->arglen = 1;
101 s->response[0] = 4;
102 DPRINTF("SD command failed\n");
103 } else if (s->cmd == 58) {
104 /* CMD58 returns R3 response (OCR) */
105 DPRINTF("Returned OCR\n");
106 s->arglen = 5;
107 s->response[0] = 1;
108 memcpy(&s->response[1], longresp, 4);
109 } else if (s->arglen != 4) {
110 BADF("Unexpected response to cmd %d\n", s->cmd);
111 /* Illegal command is about as near as we can get. */
112 s->arglen = 1;
113 s->response[0] = 4;
114 } else {
115 /* All other commands return status. */
116 uint32_t cardstatus;
117 uint16_t status;
118 /* CMD13 returns a 2-byte statuse work. Other commands
119 only return the first byte. */
120 s->arglen = (s->cmd == 13) ? 2 : 1;
121 cardstatus = (longresp[0] << 24) | (longresp[1] << 16)
122 | (longresp[2] << 8) | longresp[3];
123 status = 0;
124 if (((cardstatus >> 9) & 0xf) < 4)
125 status |= SSI_SDR_IDLE;
126 if (cardstatus & ERASE_RESET)
127 status |= SSI_SDR_ERASE_RESET;
128 if (cardstatus & ILLEGAL_COMMAND)
129 status |= SSI_SDR_ILLEGAL_COMMAND;
130 if (cardstatus & COM_CRC_ERROR)
131 status |= SSI_SDR_COM_CRC_ERROR;
132 if (cardstatus & ERASE_SEQ_ERROR)
133 status |= SSI_SDR_ERASE_SEQ_ERROR;
134 if (cardstatus & ADDRESS_ERROR)
135 status |= SSI_SDR_ADDRESS_ERROR;
136 if (cardstatus & CARD_IS_LOCKED)
137 status |= SSI_SDR_LOCKED;
138 if (cardstatus & (LOCK_UNLOCK_FAILED | WP_ERASE_SKIP))
139 status |= SSI_SDR_WP_ERASE;
140 if (cardstatus & SD_ERROR)
141 status |= SSI_SDR_ERROR;
142 if (cardstatus & CC_ERROR)
143 status |= SSI_SDR_CC_ERROR;
144 if (cardstatus & CARD_ECC_FAILED)
145 status |= SSI_SDR_ECC_FAILED;
146 if (cardstatus & WP_VIOLATION)
147 status |= SSI_SDR_WP_VIOLATION;
148 if (cardstatus & ERASE_PARAM)
149 status |= SSI_SDR_ERASE_PARAM;
150 if (cardstatus & (OUT_OF_RANGE | CID_CSD_OVERWRITE))
151 status |= SSI_SDR_OUT_OF_RANGE;
152 /* ??? Don't know what Parameter Error really means, so
153 assume it's set if the second byte is nonzero. */
154 if (status & 0xff)
155 status |= SSI_SDR_PARAMETER_ERROR;
156 s->response[0] = status >> 8;
157 s->response[1] = status;
158 DPRINTF("Card status 0x%02x\n", status);
160 s->mode = SSI_SD_RESPONSE;
161 s->response_pos = 0;
162 } else {
163 s->cmdarg[s->arglen++] = val;
165 return 0xff;
166 case SSI_SD_RESPONSE:
167 if (s->stopping) {
168 s->stopping = 0;
169 return 0xff;
171 if (s->response_pos < s->arglen) {
172 DPRINTF("Response 0x%02x\n", s->response[s->response_pos]);
173 return s->response[s->response_pos++];
175 if (sd_data_ready(s->sd)) {
176 DPRINTF("Data read\n");
177 s->mode = SSI_SD_DATA_START;
178 } else {
179 DPRINTF("End of command\n");
180 s->mode = SSI_SD_CMD;
182 return 0xff;
183 case SSI_SD_DATA_START:
184 DPRINTF("Start read block\n");
185 s->mode = SSI_SD_DATA_READ;
186 return 0xfe;
187 case SSI_SD_DATA_READ:
188 val = sd_read_data(s->sd);
189 if (!sd_data_ready(s->sd)) {
190 DPRINTF("Data read end\n");
191 s->mode = SSI_SD_CMD;
193 return val;
195 /* Should never happen. */
196 return 0xff;
199 static void ssi_sd_save(QEMUFile *f, void *opaque)
201 SSISlave *ss = SSI_SLAVE(opaque);
202 ssi_sd_state *s = (ssi_sd_state *)opaque;
203 int i;
205 qemu_put_be32(f, s->mode);
206 qemu_put_be32(f, s->cmd);
207 for (i = 0; i < 4; i++)
208 qemu_put_be32(f, s->cmdarg[i]);
209 for (i = 0; i < 5; i++)
210 qemu_put_be32(f, s->response[i]);
211 qemu_put_be32(f, s->arglen);
212 qemu_put_be32(f, s->response_pos);
213 qemu_put_be32(f, s->stopping);
215 qemu_put_be32(f, ss->cs);
218 static int ssi_sd_load(QEMUFile *f, void *opaque, int version_id)
220 SSISlave *ss = SSI_SLAVE(opaque);
221 ssi_sd_state *s = (ssi_sd_state *)opaque;
222 int i;
224 if (version_id != 1)
225 return -EINVAL;
227 s->mode = qemu_get_be32(f);
228 s->cmd = qemu_get_be32(f);
229 for (i = 0; i < 4; i++)
230 s->cmdarg[i] = qemu_get_be32(f);
231 for (i = 0; i < 5; i++)
232 s->response[i] = qemu_get_be32(f);
233 s->arglen = qemu_get_be32(f);
234 if (s->mode == SSI_SD_CMDARG &&
235 (s->arglen < 0 || s->arglen >= ARRAY_SIZE(s->cmdarg))) {
236 return -EINVAL;
238 s->response_pos = qemu_get_be32(f);
239 s->stopping = qemu_get_be32(f);
240 if (s->mode == SSI_SD_RESPONSE &&
241 (s->response_pos < 0 || s->response_pos >= ARRAY_SIZE(s->response) ||
242 (!s->stopping && s->arglen > ARRAY_SIZE(s->response)))) {
243 return -EINVAL;
246 ss->cs = qemu_get_be32(f);
248 return 0;
251 static int ssi_sd_init(SSISlave *d)
253 DeviceState *dev = DEVICE(d);
254 ssi_sd_state *s = FROM_SSI_SLAVE(ssi_sd_state, d);
255 DriveInfo *dinfo;
257 s->mode = SSI_SD_CMD;
258 dinfo = drive_get_next(IF_SD);
259 s->sd = sd_init(dinfo ? blk_by_legacy_dinfo(dinfo) : NULL, true);
260 if (s->sd == NULL) {
261 return -1;
263 register_savevm(dev, "ssi_sd", -1, 1, ssi_sd_save, ssi_sd_load, s);
264 return 0;
267 static void ssi_sd_class_init(ObjectClass *klass, void *data)
269 SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
271 k->init = ssi_sd_init;
272 k->transfer = ssi_sd_transfer;
273 k->cs_polarity = SSI_CS_LOW;
276 static const TypeInfo ssi_sd_info = {
277 .name = "ssi-sd",
278 .parent = TYPE_SSI_SLAVE,
279 .instance_size = sizeof(ssi_sd_state),
280 .class_init = ssi_sd_class_init,
283 static void ssi_sd_register_types(void)
285 type_register_static(&ssi_sd_info);
288 type_init(ssi_sd_register_types)