xref: /openbmc/qemu/hw/sd/ssi-sd.c (revision 64552b6b)
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/blockdev.h"
15 #include "hw/ssi/ssi.h"
16 #include "hw/sd/sd.h"
17 #include "qapi/error.h"
18 #include "qemu/module.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     SDBus sdbus;
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 = ldl_be_p(s->cmdarg);
101             DPRINTF("CMD%d arg 0x%08x\n", s->cmd, request.arg);
102             s->arglen = sdbus_do_command(&s->sdbus, &request, longresp);
103             if (s->arglen <= 0) {
104                 s->arglen = 1;
105                 s->response[0] = 4;
106                 DPRINTF("SD command failed\n");
107             } else if (s->cmd == 58) {
108                 /* CMD58 returns R3 response (OCR)  */
109                 DPRINTF("Returned OCR\n");
110                 s->arglen = 5;
111                 s->response[0] = 1;
112                 memcpy(&s->response[1], longresp, 4);
113             } else if (s->arglen != 4) {
114                 BADF("Unexpected response to cmd %d\n", s->cmd);
115                 /* Illegal command is about as near as we can get.  */
116                 s->arglen = 1;
117                 s->response[0] = 4;
118             } else {
119                 /* All other commands return status.  */
120                 uint32_t cardstatus;
121                 uint16_t status;
122                 /* CMD13 returns a 2-byte statuse work. Other commands
123                    only return the first byte.  */
124                 s->arglen = (s->cmd == 13) ? 2 : 1;
125                 cardstatus = ldl_be_p(longresp);
126                 status = 0;
127                 if (((cardstatus >> 9) & 0xf) < 4)
128                     status |= SSI_SDR_IDLE;
129                 if (cardstatus & ERASE_RESET)
130                     status |= SSI_SDR_ERASE_RESET;
131                 if (cardstatus & ILLEGAL_COMMAND)
132                     status |= SSI_SDR_ILLEGAL_COMMAND;
133                 if (cardstatus & COM_CRC_ERROR)
134                     status |= SSI_SDR_COM_CRC_ERROR;
135                 if (cardstatus & ERASE_SEQ_ERROR)
136                     status |= SSI_SDR_ERASE_SEQ_ERROR;
137                 if (cardstatus & ADDRESS_ERROR)
138                     status |= SSI_SDR_ADDRESS_ERROR;
139                 if (cardstatus & CARD_IS_LOCKED)
140                     status |= SSI_SDR_LOCKED;
141                 if (cardstatus & (LOCK_UNLOCK_FAILED | WP_ERASE_SKIP))
142                     status |= SSI_SDR_WP_ERASE;
143                 if (cardstatus & SD_ERROR)
144                     status |= SSI_SDR_ERROR;
145                 if (cardstatus & CC_ERROR)
146                     status |= SSI_SDR_CC_ERROR;
147                 if (cardstatus & CARD_ECC_FAILED)
148                     status |= SSI_SDR_ECC_FAILED;
149                 if (cardstatus & WP_VIOLATION)
150                     status |= SSI_SDR_WP_VIOLATION;
151                 if (cardstatus & ERASE_PARAM)
152                     status |= SSI_SDR_ERASE_PARAM;
153                 if (cardstatus & (OUT_OF_RANGE | CID_CSD_OVERWRITE))
154                     status |= SSI_SDR_OUT_OF_RANGE;
155                 /* ??? Don't know what Parameter Error really means, so
156                    assume it's set if the second byte is nonzero.  */
157                 if (status & 0xff)
158                     status |= SSI_SDR_PARAMETER_ERROR;
159                 s->response[0] = status >> 8;
160                 s->response[1] = status;
161                 DPRINTF("Card status 0x%02x\n", status);
162             }
163             s->mode = SSI_SD_RESPONSE;
164             s->response_pos = 0;
165         } else {
166             s->cmdarg[s->arglen++] = val;
167         }
168         return 0xff;
169     case SSI_SD_RESPONSE:
170         if (s->stopping) {
171             s->stopping = 0;
172             return 0xff;
173         }
174         if (s->response_pos < s->arglen) {
175             DPRINTF("Response 0x%02x\n", s->response[s->response_pos]);
176             return s->response[s->response_pos++];
177         }
178         if (sdbus_data_ready(&s->sdbus)) {
179             DPRINTF("Data read\n");
180             s->mode = SSI_SD_DATA_START;
181         } else {
182             DPRINTF("End of command\n");
183             s->mode = SSI_SD_CMD;
184         }
185         return 0xff;
186     case SSI_SD_DATA_START:
187         DPRINTF("Start read block\n");
188         s->mode = SSI_SD_DATA_READ;
189         return 0xfe;
190     case SSI_SD_DATA_READ:
191         val = sdbus_read_data(&s->sdbus);
192         if (!sdbus_data_ready(&s->sdbus)) {
193             DPRINTF("Data read end\n");
194             s->mode = SSI_SD_CMD;
195         }
196         return val;
197     }
198     /* Should never happen.  */
199     return 0xff;
200 }
201 
202 static int ssi_sd_post_load(void *opaque, int version_id)
203 {
204     ssi_sd_state *s = (ssi_sd_state *)opaque;
205 
206     if (s->mode > SSI_SD_DATA_READ) {
207         return -EINVAL;
208     }
209     if (s->mode == SSI_SD_CMDARG &&
210         (s->arglen < 0 || s->arglen >= ARRAY_SIZE(s->cmdarg))) {
211         return -EINVAL;
212     }
213     if (s->mode == SSI_SD_RESPONSE &&
214         (s->response_pos < 0 || s->response_pos >= ARRAY_SIZE(s->response) ||
215         (!s->stopping && s->arglen > ARRAY_SIZE(s->response)))) {
216         return -EINVAL;
217     }
218 
219     return 0;
220 }
221 
222 static const VMStateDescription vmstate_ssi_sd = {
223     .name = "ssi_sd",
224     .version_id = 2,
225     .minimum_version_id = 2,
226     .post_load = ssi_sd_post_load,
227     .fields = (VMStateField []) {
228         VMSTATE_UINT32(mode, ssi_sd_state),
229         VMSTATE_INT32(cmd, ssi_sd_state),
230         VMSTATE_UINT8_ARRAY(cmdarg, ssi_sd_state, 4),
231         VMSTATE_UINT8_ARRAY(response, ssi_sd_state, 5),
232         VMSTATE_INT32(arglen, ssi_sd_state),
233         VMSTATE_INT32(response_pos, ssi_sd_state),
234         VMSTATE_INT32(stopping, ssi_sd_state),
235         VMSTATE_SSI_SLAVE(ssidev, ssi_sd_state),
236         VMSTATE_END_OF_LIST()
237     }
238 };
239 
240 static void ssi_sd_realize(SSISlave *d, Error **errp)
241 {
242     ssi_sd_state *s = FROM_SSI_SLAVE(ssi_sd_state, d);
243     DeviceState *carddev;
244     DriveInfo *dinfo;
245     Error *err = NULL;
246 
247     qbus_create_inplace(&s->sdbus, sizeof(s->sdbus), TYPE_SD_BUS,
248                         DEVICE(d), "sd-bus");
249 
250     /* Create and plug in the sd card */
251     /* FIXME use a qdev drive property instead of drive_get_next() */
252     dinfo = drive_get_next(IF_SD);
253     carddev = qdev_create(BUS(&s->sdbus), TYPE_SD_CARD);
254     if (dinfo) {
255         qdev_prop_set_drive(carddev, "drive", blk_by_legacy_dinfo(dinfo), &err);
256     }
257     object_property_set_bool(OBJECT(carddev), true, "spi", &err);
258     object_property_set_bool(OBJECT(carddev), true, "realized", &err);
259     if (err) {
260         error_setg(errp, "failed to init SD card: %s", error_get_pretty(err));
261         return;
262     }
263 }
264 
265 static void ssi_sd_reset(DeviceState *dev)
266 {
267     ssi_sd_state *s = SSI_SD(dev);
268 
269     s->mode = SSI_SD_CMD;
270     s->cmd = 0;
271     memset(s->cmdarg, 0, sizeof(s->cmdarg));
272     memset(s->response, 0, sizeof(s->response));
273     s->arglen = 0;
274     s->response_pos = 0;
275     s->stopping = 0;
276 }
277 
278 static void ssi_sd_class_init(ObjectClass *klass, void *data)
279 {
280     DeviceClass *dc = DEVICE_CLASS(klass);
281     SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
282 
283     k->realize = ssi_sd_realize;
284     k->transfer = ssi_sd_transfer;
285     k->cs_polarity = SSI_CS_LOW;
286     dc->vmsd = &vmstate_ssi_sd;
287     dc->reset = ssi_sd_reset;
288     /* Reason: init() method uses drive_get_next() */
289     dc->user_creatable = false;
290 }
291 
292 static const TypeInfo ssi_sd_info = {
293     .name          = TYPE_SSI_SD,
294     .parent        = TYPE_SSI_SLAVE,
295     .instance_size = sizeof(ssi_sd_state),
296     .class_init    = ssi_sd_class_init,
297 };
298 
299 static void ssi_sd_register_types(void)
300 {
301     type_register_static(&ssi_sd_info);
302 }
303 
304 type_init(ssi_sd_register_types)
305