xref: /openbmc/qemu/hw/sd/ssi-sd.c (revision 4fe6d78b)
1 /*
2  * SSI to SD card adapter.
3  *
4  * Copyright (c) 2007-2009 CodeSourcery.
5  * Written by Paul Brook
6  *
7  * This code is licensed under the GNU GPL v2.
8  *
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.
11  */
12 
13 #include "qemu/osdep.h"
14 #include "sysemu/block-backend.h"
15 #include "sysemu/blockdev.h"
16 #include "hw/ssi/ssi.h"
17 #include "hw/sd/sd.h"
18 #include "qapi/error.h"
19 
20 //#define DEBUG_SSI_SD 1
21 
22 #ifdef DEBUG_SSI_SD
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)
27 #else
28 #define DPRINTF(fmt, ...) do {} while(0)
29 #define BADF(fmt, ...) \
30 do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__);} while (0)
31 #endif
32 
33 typedef enum {
34     SSI_SD_CMD = 0,
35     SSI_SD_CMDARG,
36     SSI_SD_RESPONSE,
37     SSI_SD_DATA_START,
38     SSI_SD_DATA_READ,
39 } ssi_sd_mode;
40 
41 typedef struct {
42     SSISlave ssidev;
43     uint32_t mode;
44     int cmd;
45     uint8_t cmdarg[4];
46     uint8_t response[5];
47     int32_t arglen;
48     int32_t response_pos;
49     int32_t stopping;
50     SDState *sd;
51 } ssi_sd_state;
52 
53 #define TYPE_SSI_SD "ssi-sd"
54 #define SSI_SD(obj) OBJECT_CHECK(ssi_sd_state, (obj), TYPE_SSI_SD)
55 
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
72 
73 static uint32_t ssi_sd_transfer(SSISlave *dev, uint32_t val)
74 {
75     ssi_sd_state *s = FROM_SSI_SLAVE(ssi_sd_state, dev);
76 
77     /* Special case: allow CMD12 (STOP TRANSMISSION) while reading data.  */
78     if (s->mode == SSI_SD_DATA_READ && val == 0x4d) {
79         s->mode = SSI_SD_CMD;
80         /* There must be at least one byte delay before the card responds.  */
81         s->stopping = 1;
82     }
83 
84     switch (s->mode) {
85     case SSI_SD_CMD:
86         if (val == 0xff) {
87             DPRINTF("NULL command\n");
88             return 0xff;
89         }
90         s->cmd = val & 0x3f;
91         s->mode = SSI_SD_CMDARG;
92         s->arglen = 0;
93         return 0xff;
94     case SSI_SD_CMDARG:
95         if (s->arglen == 4) {
96             SDRequest request;
97             uint8_t longresp[16];
98             /* FIXME: Check CRC.  */
99             request.cmd = s->cmd;
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) {
105                 s->arglen = 1;
106                 s->response[0] = 4;
107                 DPRINTF("SD command failed\n");
108             } else if (s->cmd == 58) {
109                 /* CMD58 returns R3 response (OCR)  */
110                 DPRINTF("Returned OCR\n");
111                 s->arglen = 5;
112                 s->response[0] = 1;
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.  */
117                 s->arglen = 1;
118                 s->response[0] = 4;
119             } else {
120                 /* All other commands return status.  */
121                 uint32_t cardstatus;
122                 uint16_t 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];
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);
164             }
165             s->mode = SSI_SD_RESPONSE;
166             s->response_pos = 0;
167         } else {
168             s->cmdarg[s->arglen++] = val;
169         }
170         return 0xff;
171     case SSI_SD_RESPONSE:
172         if (s->stopping) {
173             s->stopping = 0;
174             return 0xff;
175         }
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++];
179         }
180         if (sd_data_ready(s->sd)) {
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;
186         }
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 = sd_read_data(s->sd);
194         if (!sd_data_ready(s->sd)) {
195             DPRINTF("Data read end\n");
196             s->mode = SSI_SD_CMD;
197         }
198         return val;
199     }
200     /* Should never happen.  */
201     return 0xff;
202 }
203 
204 static int ssi_sd_post_load(void *opaque, int version_id)
205 {
206     ssi_sd_state *s = (ssi_sd_state *)opaque;
207 
208     if (s->mode > SSI_SD_DATA_READ) {
209         return -EINVAL;
210     }
211     if (s->mode == SSI_SD_CMDARG &&
212         (s->arglen < 0 || s->arglen >= ARRAY_SIZE(s->cmdarg))) {
213         return -EINVAL;
214     }
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;
219     }
220 
221     return 0;
222 }
223 
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()
239     }
240 };
241 
242 static void ssi_sd_realize(SSISlave *d, Error **errp)
243 {
244     ssi_sd_state *s = FROM_SSI_SLAVE(ssi_sd_state, d);
245     DriveInfo *dinfo;
246 
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);
250     if (s->sd == NULL) {
251         error_setg(errp, "Device initialization failed.");
252         return;
253     }
254 }
255 
256 static void ssi_sd_reset(DeviceState *dev)
257 {
258     ssi_sd_state *s = SSI_SD(dev);
259 
260     s->mode = SSI_SD_CMD;
261     s->cmd = 0;
262     memset(s->cmdarg, 0, sizeof(s->cmdarg));
263     memset(s->response, 0, sizeof(s->response));
264     s->arglen = 0;
265     s->response_pos = 0;
266     s->stopping = 0;
267 
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.
270      */
271     device_reset(DEVICE(s->sd));
272 }
273 
274 static void ssi_sd_class_init(ObjectClass *klass, void *data)
275 {
276     DeviceClass *dc = DEVICE_CLASS(klass);
277     SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
278 
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;
284 }
285 
286 static const TypeInfo ssi_sd_info = {
287     .name          = TYPE_SSI_SD,
288     .parent        = TYPE_SSI_SLAVE,
289     .instance_size = sizeof(ssi_sd_state),
290     .class_init    = ssi_sd_class_init,
291 };
292 
293 static void ssi_sd_register_types(void)
294 {
295     type_register_static(&ssi_sd_info);
296 }
297 
298 type_init(ssi_sd_register_types)
299