xref: /openbmc/qemu/hw/sd/ssi-sd.c (revision a81df1b6)
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 
22 //#define DEBUG_SSI_SD 1
23 
24 #ifdef DEBUG_SSI_SD
25 #define DPRINTF(fmt, ...) \
26 do { printf("ssi_sd: " fmt , ## __VA_ARGS__); } while (0)
27 #define BADF(fmt, ...) \
28 do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
29 #else
30 #define DPRINTF(fmt, ...) do {} while(0)
31 #define BADF(fmt, ...) \
32 do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__);} while (0)
33 #endif
34 
35 typedef enum {
36     SSI_SD_CMD = 0,
37     SSI_SD_CMDARG,
38     SSI_SD_RESPONSE,
39     SSI_SD_DATA_START,
40     SSI_SD_DATA_READ,
41 } ssi_sd_mode;
42 
43 typedef struct {
44     SSISlave ssidev;
45     uint32_t mode;
46     int cmd;
47     uint8_t cmdarg[4];
48     uint8_t response[5];
49     int32_t arglen;
50     int32_t response_pos;
51     int32_t stopping;
52     SDBus sdbus;
53 } ssi_sd_state;
54 
55 #define TYPE_SSI_SD "ssi-sd"
56 #define SSI_SD(obj) OBJECT_CHECK(ssi_sd_state, (obj), TYPE_SSI_SD)
57 
58 /* State word bits.  */
59 #define SSI_SDR_LOCKED          0x0001
60 #define SSI_SDR_WP_ERASE        0x0002
61 #define SSI_SDR_ERROR           0x0004
62 #define SSI_SDR_CC_ERROR        0x0008
63 #define SSI_SDR_ECC_FAILED      0x0010
64 #define SSI_SDR_WP_VIOLATION    0x0020
65 #define SSI_SDR_ERASE_PARAM     0x0040
66 #define SSI_SDR_OUT_OF_RANGE    0x0080
67 #define SSI_SDR_IDLE            0x0100
68 #define SSI_SDR_ERASE_RESET     0x0200
69 #define SSI_SDR_ILLEGAL_COMMAND 0x0400
70 #define SSI_SDR_COM_CRC_ERROR   0x0800
71 #define SSI_SDR_ERASE_SEQ_ERROR 0x1000
72 #define SSI_SDR_ADDRESS_ERROR   0x2000
73 #define SSI_SDR_PARAMETER_ERROR 0x4000
74 
75 static uint32_t ssi_sd_transfer(SSISlave *dev, uint32_t val)
76 {
77     ssi_sd_state *s = SSI_SD(dev);
78 
79     /* Special case: allow CMD12 (STOP TRANSMISSION) while reading data.  */
80     if (s->mode == SSI_SD_DATA_READ && val == 0x4d) {
81         s->mode = SSI_SD_CMD;
82         /* There must be at least one byte delay before the card responds.  */
83         s->stopping = 1;
84     }
85 
86     switch (s->mode) {
87     case SSI_SD_CMD:
88         if (val == 0xff) {
89             DPRINTF("NULL command\n");
90             return 0xff;
91         }
92         s->cmd = val & 0x3f;
93         s->mode = SSI_SD_CMDARG;
94         s->arglen = 0;
95         return 0xff;
96     case SSI_SD_CMDARG:
97         if (s->arglen == 4) {
98             SDRequest request;
99             uint8_t longresp[16];
100             /* FIXME: Check CRC.  */
101             request.cmd = s->cmd;
102             request.arg = ldl_be_p(s->cmdarg);
103             DPRINTF("CMD%d arg 0x%08x\n", s->cmd, request.arg);
104             s->arglen = sdbus_do_command(&s->sdbus, &request, longresp);
105             if (s->arglen <= 0) {
106                 s->arglen = 1;
107                 s->response[0] = 4;
108                 DPRINTF("SD command failed\n");
109             } else if (s->cmd == 58) {
110                 /* CMD58 returns R3 response (OCR)  */
111                 DPRINTF("Returned OCR\n");
112                 s->arglen = 5;
113                 s->response[0] = 1;
114                 memcpy(&s->response[1], longresp, 4);
115             } else if (s->arglen != 4) {
116                 BADF("Unexpected response to cmd %d\n", s->cmd);
117                 /* Illegal command is about as near as we can get.  */
118                 s->arglen = 1;
119                 s->response[0] = 4;
120             } else {
121                 /* All other commands return status.  */
122                 uint32_t cardstatus;
123                 uint16_t status;
124                 /* CMD13 returns a 2-byte statuse work. Other commands
125                    only return the first byte.  */
126                 s->arglen = (s->cmd == 13) ? 2 : 1;
127                 cardstatus = ldl_be_p(longresp);
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 (sdbus_data_ready(&s->sdbus)) {
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 = sdbus_read_data(&s->sdbus);
194         if (!sdbus_data_ready(&s->sdbus)) {
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     ERRP_GUARD();
245     ssi_sd_state *s = SSI_SD(d);
246     DeviceState *carddev;
247     DriveInfo *dinfo;
248 
249     qbus_create_inplace(&s->sdbus, sizeof(s->sdbus), TYPE_SD_BUS,
250                         DEVICE(d), "sd-bus");
251 
252     /* Create and plug in the sd card */
253     /* FIXME use a qdev drive property instead of drive_get_next() */
254     dinfo = drive_get_next(IF_SD);
255     carddev = qdev_new(TYPE_SD_CARD);
256     if (dinfo) {
257         if (!qdev_prop_set_drive_err(carddev, "drive",
258                                      blk_by_legacy_dinfo(dinfo), errp)) {
259             goto fail;
260         }
261     }
262 
263     if (!object_property_set_bool(OBJECT(carddev), "spi", true, errp)) {
264         goto fail;
265     }
266 
267     if (!qdev_realize_and_unref(carddev, BUS(&s->sdbus), errp)) {
268         goto fail;
269     }
270 
271     return;
272 
273 fail:
274     error_prepend(errp, "failed to init SD card: ");
275 }
276 
277 static void ssi_sd_reset(DeviceState *dev)
278 {
279     ssi_sd_state *s = SSI_SD(dev);
280 
281     s->mode = SSI_SD_CMD;
282     s->cmd = 0;
283     memset(s->cmdarg, 0, sizeof(s->cmdarg));
284     memset(s->response, 0, sizeof(s->response));
285     s->arglen = 0;
286     s->response_pos = 0;
287     s->stopping = 0;
288 }
289 
290 static void ssi_sd_class_init(ObjectClass *klass, void *data)
291 {
292     DeviceClass *dc = DEVICE_CLASS(klass);
293     SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
294 
295     k->realize = ssi_sd_realize;
296     k->transfer = ssi_sd_transfer;
297     k->cs_polarity = SSI_CS_LOW;
298     dc->vmsd = &vmstate_ssi_sd;
299     dc->reset = ssi_sd_reset;
300     /* Reason: init() method uses drive_get_next() */
301     dc->user_creatable = false;
302 }
303 
304 static const TypeInfo ssi_sd_info = {
305     .name          = TYPE_SSI_SD,
306     .parent        = TYPE_SSI_SLAVE,
307     .instance_size = sizeof(ssi_sd_state),
308     .class_init    = ssi_sd_class_init,
309 };
310 
311 static void ssi_sd_register_types(void)
312 {
313     type_register_static(&ssi_sd_info);
314 }
315 
316 type_init(ssi_sd_register_types)
317