xref: /openbmc/qemu/hw/sd/ssi-sd.c (revision 7cc25f6c)
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 "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"
21 #include "qom/object.h"
22 
23 //#define DEBUG_SSI_SD 1
24 
25 #ifdef DEBUG_SSI_SD
26 #define DPRINTF(fmt, ...) \
27 do { printf("ssi_sd: " fmt , ## __VA_ARGS__); } while (0)
28 #define BADF(fmt, ...) \
29 do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
30 #else
31 #define DPRINTF(fmt, ...) do {} while(0)
32 #define BADF(fmt, ...) \
33 do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__);} while (0)
34 #endif
35 
36 typedef enum {
37     SSI_SD_CMD = 0,
38     SSI_SD_CMDARG,
39     SSI_SD_RESPONSE,
40     SSI_SD_DATA_START,
41     SSI_SD_DATA_READ,
42 } ssi_sd_mode;
43 
44 struct ssi_sd_state {
45     SSIPeripheral ssidev;
46     uint32_t mode;
47     int cmd;
48     uint8_t cmdarg[4];
49     uint8_t response[5];
50     int32_t arglen;
51     int32_t response_pos;
52     int32_t stopping;
53     SDBus sdbus;
54 };
55 
56 #define TYPE_SSI_SD "ssi-sd"
57 OBJECT_DECLARE_SIMPLE_TYPE(ssi_sd_state, SSI_SD)
58 
59 /* State word bits.  */
60 #define SSI_SDR_LOCKED          0x0001
61 #define SSI_SDR_WP_ERASE        0x0002
62 #define SSI_SDR_ERROR           0x0004
63 #define SSI_SDR_CC_ERROR        0x0008
64 #define SSI_SDR_ECC_FAILED      0x0010
65 #define SSI_SDR_WP_VIOLATION    0x0020
66 #define SSI_SDR_ERASE_PARAM     0x0040
67 #define SSI_SDR_OUT_OF_RANGE    0x0080
68 #define SSI_SDR_IDLE            0x0100
69 #define SSI_SDR_ERASE_RESET     0x0200
70 #define SSI_SDR_ILLEGAL_COMMAND 0x0400
71 #define SSI_SDR_COM_CRC_ERROR   0x0800
72 #define SSI_SDR_ERASE_SEQ_ERROR 0x1000
73 #define SSI_SDR_ADDRESS_ERROR   0x2000
74 #define SSI_SDR_PARAMETER_ERROR 0x4000
75 
76 static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val)
77 {
78     ssi_sd_state *s = SSI_SD(dev);
79 
80     /* Special case: allow CMD12 (STOP TRANSMISSION) while reading data.  */
81     if (s->mode == SSI_SD_DATA_READ && val == 0x4d) {
82         s->mode = SSI_SD_CMD;
83         /* There must be at least one byte delay before the card responds.  */
84         s->stopping = 1;
85     }
86 
87     switch (s->mode) {
88     case SSI_SD_CMD:
89         if (val == 0xff) {
90             DPRINTF("NULL command\n");
91             return 0xff;
92         }
93         s->cmd = val & 0x3f;
94         s->mode = SSI_SD_CMDARG;
95         s->arglen = 0;
96         return 0xff;
97     case SSI_SD_CMDARG:
98         if (s->arglen == 4) {
99             SDRequest request;
100             uint8_t longresp[16];
101             /* FIXME: Check CRC.  */
102             request.cmd = s->cmd;
103             request.arg = ldl_be_p(s->cmdarg);
104             DPRINTF("CMD%d arg 0x%08x\n", s->cmd, request.arg);
105             s->arglen = sdbus_do_command(&s->sdbus, &request, longresp);
106             if (s->arglen <= 0) {
107                 s->arglen = 1;
108                 s->response[0] = 4;
109                 DPRINTF("SD command failed\n");
110             } else if (s->cmd == 58) {
111                 /* CMD58 returns R3 response (OCR)  */
112                 DPRINTF("Returned OCR\n");
113                 s->arglen = 5;
114                 s->response[0] = 1;
115                 memcpy(&s->response[1], longresp, 4);
116             } else if (s->arglen != 4) {
117                 BADF("Unexpected response to cmd %d\n", s->cmd);
118                 /* Illegal command is about as near as we can get.  */
119                 s->arglen = 1;
120                 s->response[0] = 4;
121             } else {
122                 /* All other commands return status.  */
123                 uint32_t cardstatus;
124                 uint16_t status;
125                 /* CMD13 returns a 2-byte statuse work. Other commands
126                    only return the first byte.  */
127                 s->arglen = (s->cmd == 13) ? 2 : 1;
128                 cardstatus = ldl_be_p(longresp);
129                 status = 0;
130                 if (((cardstatus >> 9) & 0xf) < 4)
131                     status |= SSI_SDR_IDLE;
132                 if (cardstatus & ERASE_RESET)
133                     status |= SSI_SDR_ERASE_RESET;
134                 if (cardstatus & ILLEGAL_COMMAND)
135                     status |= SSI_SDR_ILLEGAL_COMMAND;
136                 if (cardstatus & COM_CRC_ERROR)
137                     status |= SSI_SDR_COM_CRC_ERROR;
138                 if (cardstatus & ERASE_SEQ_ERROR)
139                     status |= SSI_SDR_ERASE_SEQ_ERROR;
140                 if (cardstatus & ADDRESS_ERROR)
141                     status |= SSI_SDR_ADDRESS_ERROR;
142                 if (cardstatus & CARD_IS_LOCKED)
143                     status |= SSI_SDR_LOCKED;
144                 if (cardstatus & (LOCK_UNLOCK_FAILED | WP_ERASE_SKIP))
145                     status |= SSI_SDR_WP_ERASE;
146                 if (cardstatus & SD_ERROR)
147                     status |= SSI_SDR_ERROR;
148                 if (cardstatus & CC_ERROR)
149                     status |= SSI_SDR_CC_ERROR;
150                 if (cardstatus & CARD_ECC_FAILED)
151                     status |= SSI_SDR_ECC_FAILED;
152                 if (cardstatus & WP_VIOLATION)
153                     status |= SSI_SDR_WP_VIOLATION;
154                 if (cardstatus & ERASE_PARAM)
155                     status |= SSI_SDR_ERASE_PARAM;
156                 if (cardstatus & (OUT_OF_RANGE | CID_CSD_OVERWRITE))
157                     status |= SSI_SDR_OUT_OF_RANGE;
158                 /* ??? Don't know what Parameter Error really means, so
159                    assume it's set if the second byte is nonzero.  */
160                 if (status & 0xff)
161                     status |= SSI_SDR_PARAMETER_ERROR;
162                 s->response[0] = status >> 8;
163                 s->response[1] = status;
164                 DPRINTF("Card status 0x%02x\n", status);
165             }
166             s->mode = SSI_SD_RESPONSE;
167             s->response_pos = 0;
168         } else {
169             s->cmdarg[s->arglen++] = val;
170         }
171         return 0xff;
172     case SSI_SD_RESPONSE:
173         if (s->stopping) {
174             s->stopping = 0;
175             return 0xff;
176         }
177         if (s->response_pos < s->arglen) {
178             DPRINTF("Response 0x%02x\n", s->response[s->response_pos]);
179             return s->response[s->response_pos++];
180         }
181         if (sdbus_data_ready(&s->sdbus)) {
182             DPRINTF("Data read\n");
183             s->mode = SSI_SD_DATA_START;
184         } else {
185             DPRINTF("End of command\n");
186             s->mode = SSI_SD_CMD;
187         }
188         return 0xff;
189     case SSI_SD_DATA_START:
190         DPRINTF("Start read block\n");
191         s->mode = SSI_SD_DATA_READ;
192         return 0xfe;
193     case SSI_SD_DATA_READ:
194         val = sdbus_read_byte(&s->sdbus);
195         if (!sdbus_data_ready(&s->sdbus)) {
196             DPRINTF("Data read end\n");
197             s->mode = SSI_SD_CMD;
198         }
199         return val;
200     }
201     /* Should never happen.  */
202     return 0xff;
203 }
204 
205 static int ssi_sd_post_load(void *opaque, int version_id)
206 {
207     ssi_sd_state *s = (ssi_sd_state *)opaque;
208 
209     if (s->mode > SSI_SD_DATA_READ) {
210         return -EINVAL;
211     }
212     if (s->mode == SSI_SD_CMDARG &&
213         (s->arglen < 0 || s->arglen >= ARRAY_SIZE(s->cmdarg))) {
214         return -EINVAL;
215     }
216     if (s->mode == SSI_SD_RESPONSE &&
217         (s->response_pos < 0 || s->response_pos >= ARRAY_SIZE(s->response) ||
218         (!s->stopping && s->arglen > ARRAY_SIZE(s->response)))) {
219         return -EINVAL;
220     }
221 
222     return 0;
223 }
224 
225 static const VMStateDescription vmstate_ssi_sd = {
226     .name = "ssi_sd",
227     .version_id = 2,
228     .minimum_version_id = 2,
229     .post_load = ssi_sd_post_load,
230     .fields = (VMStateField []) {
231         VMSTATE_UINT32(mode, ssi_sd_state),
232         VMSTATE_INT32(cmd, ssi_sd_state),
233         VMSTATE_UINT8_ARRAY(cmdarg, ssi_sd_state, 4),
234         VMSTATE_UINT8_ARRAY(response, ssi_sd_state, 5),
235         VMSTATE_INT32(arglen, ssi_sd_state),
236         VMSTATE_INT32(response_pos, ssi_sd_state),
237         VMSTATE_INT32(stopping, ssi_sd_state),
238         VMSTATE_SSI_PERIPHERAL(ssidev, ssi_sd_state),
239         VMSTATE_END_OF_LIST()
240     }
241 };
242 
243 static void ssi_sd_realize(SSIPeripheral *d, Error **errp)
244 {
245     ERRP_GUARD();
246     ssi_sd_state *s = SSI_SD(d);
247     DeviceState *carddev;
248     DriveInfo *dinfo;
249 
250     qbus_create_inplace(&s->sdbus, sizeof(s->sdbus), TYPE_SD_BUS,
251                         DEVICE(d), "sd-bus");
252 
253     /* Create and plug in the sd card */
254     /* FIXME use a qdev drive property instead of drive_get_next() */
255     dinfo = drive_get_next(IF_SD);
256     carddev = qdev_new(TYPE_SD_CARD);
257     if (dinfo) {
258         if (!qdev_prop_set_drive_err(carddev, "drive",
259                                      blk_by_legacy_dinfo(dinfo), errp)) {
260             goto fail;
261         }
262     }
263 
264     if (!object_property_set_bool(OBJECT(carddev), "spi", true, errp)) {
265         goto fail;
266     }
267 
268     if (!qdev_realize_and_unref(carddev, BUS(&s->sdbus), errp)) {
269         goto fail;
270     }
271 
272     return;
273 
274 fail:
275     error_prepend(errp, "failed to init SD card: ");
276 }
277 
278 static void ssi_sd_reset(DeviceState *dev)
279 {
280     ssi_sd_state *s = SSI_SD(dev);
281 
282     s->mode = SSI_SD_CMD;
283     s->cmd = 0;
284     memset(s->cmdarg, 0, sizeof(s->cmdarg));
285     memset(s->response, 0, sizeof(s->response));
286     s->arglen = 0;
287     s->response_pos = 0;
288     s->stopping = 0;
289 }
290 
291 static void ssi_sd_class_init(ObjectClass *klass, void *data)
292 {
293     DeviceClass *dc = DEVICE_CLASS(klass);
294     SSIPeripheralClass *k = SSI_PERIPHERAL_CLASS(klass);
295 
296     k->realize = ssi_sd_realize;
297     k->transfer = ssi_sd_transfer;
298     k->cs_polarity = SSI_CS_LOW;
299     dc->vmsd = &vmstate_ssi_sd;
300     dc->reset = ssi_sd_reset;
301     /* Reason: init() method uses drive_get_next() */
302     dc->user_creatable = false;
303 }
304 
305 static const TypeInfo ssi_sd_info = {
306     .name          = TYPE_SSI_SD,
307     .parent        = TYPE_SSI_PERIPHERAL,
308     .instance_size = sizeof(ssi_sd_state),
309     .class_init    = ssi_sd_class_init,
310 };
311 
312 static void ssi_sd_register_types(void)
313 {
314     type_register_static(&ssi_sd_info);
315 }
316 
317 type_init(ssi_sd_register_types)
318