xref: /qemu/hw/sd/ssi-sd.c (revision c3abd91309b476ca539b3ec73b30887d4759aba8)
1775616c3Spbrook /*
2775616c3Spbrook  * SSI to SD card adapter.
3775616c3Spbrook  *
45493e33fSPaul Brook  * Copyright (c) 2007-2009 CodeSourcery.
5775616c3Spbrook  * Written by Paul Brook
6775616c3Spbrook  *
78e31bf38SMatthew Fernandez  * This code is licensed under the GNU GPL v2.
86b620ca3SPaolo Bonzini  *
96b620ca3SPaolo Bonzini  * Contributions after 2012-01-13 are licensed under the terms of the
106b620ca3SPaolo Bonzini  * GNU GPL, version 2 or (at your option) any later version.
11775616c3Spbrook  */
12775616c3Spbrook 
130430891cSPeter Maydell #include "qemu/osdep.h"
14fa1d36dfSMarkus Armbruster #include "sysemu/block-backend.h"
159c17d615SPaolo Bonzini #include "sysemu/blockdev.h"
168fd06719SAlistair Francis #include "hw/ssi/ssi.h"
17e3382ef0SSai Pavan Boddu #include "hw/sd/sd.h"
187673bb4cSCédric Le Goater #include "qapi/error.h"
19775616c3Spbrook 
20775616c3Spbrook //#define DEBUG_SSI_SD 1
21775616c3Spbrook 
22775616c3Spbrook #ifdef DEBUG_SSI_SD
23001faf32SBlue Swirl #define DPRINTF(fmt, ...) \
24001faf32SBlue Swirl do { printf("ssi_sd: " fmt , ## __VA_ARGS__); } while (0)
25001faf32SBlue Swirl #define BADF(fmt, ...) \
26001faf32SBlue Swirl do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
27775616c3Spbrook #else
28001faf32SBlue Swirl #define DPRINTF(fmt, ...) do {} while(0)
29001faf32SBlue Swirl #define BADF(fmt, ...) \
30001faf32SBlue Swirl do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__);} while (0)
31775616c3Spbrook #endif
32775616c3Spbrook 
33775616c3Spbrook typedef enum {
342ccfd336SDr. David Alan Gilbert     SSI_SD_CMD = 0,
35775616c3Spbrook     SSI_SD_CMDARG,
36775616c3Spbrook     SSI_SD_RESPONSE,
37775616c3Spbrook     SSI_SD_DATA_START,
38775616c3Spbrook     SSI_SD_DATA_READ,
39775616c3Spbrook } ssi_sd_mode;
40775616c3Spbrook 
41775616c3Spbrook typedef struct {
425493e33fSPaul Brook     SSISlave ssidev;
432ccfd336SDr. David Alan Gilbert     uint32_t mode;
44775616c3Spbrook     int cmd;
45775616c3Spbrook     uint8_t cmdarg[4];
46775616c3Spbrook     uint8_t response[5];
472ccfd336SDr. David Alan Gilbert     int32_t arglen;
482ccfd336SDr. David Alan Gilbert     int32_t response_pos;
492ccfd336SDr. David Alan Gilbert     int32_t stopping;
50*c3abd913SPhilippe Mathieu-Daudé     SDBus sdbus;
51775616c3Spbrook } ssi_sd_state;
52775616c3Spbrook 
538046d44fSPeter Maydell #define TYPE_SSI_SD "ssi-sd"
548046d44fSPeter Maydell #define SSI_SD(obj) OBJECT_CHECK(ssi_sd_state, (obj), TYPE_SSI_SD)
558046d44fSPeter Maydell 
56775616c3Spbrook /* State word bits.  */
57775616c3Spbrook #define SSI_SDR_LOCKED          0x0001
58775616c3Spbrook #define SSI_SDR_WP_ERASE        0x0002
59775616c3Spbrook #define SSI_SDR_ERROR           0x0004
60775616c3Spbrook #define SSI_SDR_CC_ERROR        0x0008
61775616c3Spbrook #define SSI_SDR_ECC_FAILED      0x0010
62775616c3Spbrook #define SSI_SDR_WP_VIOLATION    0x0020
63775616c3Spbrook #define SSI_SDR_ERASE_PARAM     0x0040
64775616c3Spbrook #define SSI_SDR_OUT_OF_RANGE    0x0080
65775616c3Spbrook #define SSI_SDR_IDLE            0x0100
66775616c3Spbrook #define SSI_SDR_ERASE_RESET     0x0200
67775616c3Spbrook #define SSI_SDR_ILLEGAL_COMMAND 0x0400
68775616c3Spbrook #define SSI_SDR_COM_CRC_ERROR   0x0800
69775616c3Spbrook #define SSI_SDR_ERASE_SEQ_ERROR 0x1000
70775616c3Spbrook #define SSI_SDR_ADDRESS_ERROR   0x2000
71775616c3Spbrook #define SSI_SDR_PARAMETER_ERROR 0x4000
72775616c3Spbrook 
735493e33fSPaul Brook static uint32_t ssi_sd_transfer(SSISlave *dev, uint32_t val)
74775616c3Spbrook {
755493e33fSPaul Brook     ssi_sd_state *s = FROM_SSI_SLAVE(ssi_sd_state, dev);
76775616c3Spbrook 
77775616c3Spbrook     /* Special case: allow CMD12 (STOP TRANSMISSION) while reading data.  */
78775616c3Spbrook     if (s->mode == SSI_SD_DATA_READ && val == 0x4d) {
79775616c3Spbrook         s->mode = SSI_SD_CMD;
80775616c3Spbrook         /* There must be at least one byte delay before the card responds.  */
81775616c3Spbrook         s->stopping = 1;
82775616c3Spbrook     }
83775616c3Spbrook 
84775616c3Spbrook     switch (s->mode) {
85775616c3Spbrook     case SSI_SD_CMD:
86775616c3Spbrook         if (val == 0xff) {
87775616c3Spbrook             DPRINTF("NULL command\n");
88775616c3Spbrook             return 0xff;
89775616c3Spbrook         }
90775616c3Spbrook         s->cmd = val & 0x3f;
91775616c3Spbrook         s->mode = SSI_SD_CMDARG;
92775616c3Spbrook         s->arglen = 0;
93775616c3Spbrook         return 0xff;
94775616c3Spbrook     case SSI_SD_CMDARG:
95775616c3Spbrook         if (s->arglen == 4) {
96bc24a225SPaul Brook             SDRequest request;
97775616c3Spbrook             uint8_t longresp[16];
98775616c3Spbrook             /* FIXME: Check CRC.  */
99775616c3Spbrook             request.cmd = s->cmd;
100775616c3Spbrook             request.arg = (s->cmdarg[0] << 24) | (s->cmdarg[1] << 16)
101775616c3Spbrook                            | (s->cmdarg[2] << 8) | s->cmdarg[3];
102775616c3Spbrook             DPRINTF("CMD%d arg 0x%08x\n", s->cmd, request.arg);
103*c3abd913SPhilippe Mathieu-Daudé             s->arglen = sdbus_do_command(&s->sdbus, &request, longresp);
104775616c3Spbrook             if (s->arglen <= 0) {
105775616c3Spbrook                 s->arglen = 1;
106775616c3Spbrook                 s->response[0] = 4;
107775616c3Spbrook                 DPRINTF("SD command failed\n");
108775616c3Spbrook             } else if (s->cmd == 58) {
109775616c3Spbrook                 /* CMD58 returns R3 response (OCR)  */
110775616c3Spbrook                 DPRINTF("Returned OCR\n");
111775616c3Spbrook                 s->arglen = 5;
112775616c3Spbrook                 s->response[0] = 1;
113775616c3Spbrook                 memcpy(&s->response[1], longresp, 4);
114775616c3Spbrook             } else if (s->arglen != 4) {
115775616c3Spbrook                 BADF("Unexpected response to cmd %d\n", s->cmd);
116775616c3Spbrook                 /* Illegal command is about as near as we can get.  */
117775616c3Spbrook                 s->arglen = 1;
118775616c3Spbrook                 s->response[0] = 4;
119775616c3Spbrook             } else {
120775616c3Spbrook                 /* All other commands return status.  */
121775616c3Spbrook                 uint32_t cardstatus;
122775616c3Spbrook                 uint16_t status;
123775616c3Spbrook                 /* CMD13 returns a 2-byte statuse work. Other commands
124775616c3Spbrook                    only return the first byte.  */
125775616c3Spbrook                 s->arglen = (s->cmd == 13) ? 2 : 1;
126775616c3Spbrook                 cardstatus = (longresp[0] << 24) | (longresp[1] << 16)
127775616c3Spbrook                              | (longresp[2] << 8) | longresp[3];
128775616c3Spbrook                 status = 0;
129775616c3Spbrook                 if (((cardstatus >> 9) & 0xf) < 4)
130775616c3Spbrook                     status |= SSI_SDR_IDLE;
131775616c3Spbrook                 if (cardstatus & ERASE_RESET)
132775616c3Spbrook                     status |= SSI_SDR_ERASE_RESET;
133775616c3Spbrook                 if (cardstatus & ILLEGAL_COMMAND)
134775616c3Spbrook                     status |= SSI_SDR_ILLEGAL_COMMAND;
135775616c3Spbrook                 if (cardstatus & COM_CRC_ERROR)
136775616c3Spbrook                     status |= SSI_SDR_COM_CRC_ERROR;
137775616c3Spbrook                 if (cardstatus & ERASE_SEQ_ERROR)
138775616c3Spbrook                     status |= SSI_SDR_ERASE_SEQ_ERROR;
139775616c3Spbrook                 if (cardstatus & ADDRESS_ERROR)
140775616c3Spbrook                     status |= SSI_SDR_ADDRESS_ERROR;
141775616c3Spbrook                 if (cardstatus & CARD_IS_LOCKED)
142775616c3Spbrook                     status |= SSI_SDR_LOCKED;
143775616c3Spbrook                 if (cardstatus & (LOCK_UNLOCK_FAILED | WP_ERASE_SKIP))
144775616c3Spbrook                     status |= SSI_SDR_WP_ERASE;
145775616c3Spbrook                 if (cardstatus & SD_ERROR)
146775616c3Spbrook                     status |= SSI_SDR_ERROR;
147775616c3Spbrook                 if (cardstatus & CC_ERROR)
148775616c3Spbrook                     status |= SSI_SDR_CC_ERROR;
149775616c3Spbrook                 if (cardstatus & CARD_ECC_FAILED)
150775616c3Spbrook                     status |= SSI_SDR_ECC_FAILED;
151775616c3Spbrook                 if (cardstatus & WP_VIOLATION)
152775616c3Spbrook                     status |= SSI_SDR_WP_VIOLATION;
153775616c3Spbrook                 if (cardstatus & ERASE_PARAM)
154775616c3Spbrook                     status |= SSI_SDR_ERASE_PARAM;
155775616c3Spbrook                 if (cardstatus & (OUT_OF_RANGE | CID_CSD_OVERWRITE))
156775616c3Spbrook                     status |= SSI_SDR_OUT_OF_RANGE;
157775616c3Spbrook                 /* ??? Don't know what Parameter Error really means, so
158775616c3Spbrook                    assume it's set if the second byte is nonzero.  */
159775616c3Spbrook                 if (status & 0xff)
160775616c3Spbrook                     status |= SSI_SDR_PARAMETER_ERROR;
161775616c3Spbrook                 s->response[0] = status >> 8;
162775616c3Spbrook                 s->response[1] = status;
163775616c3Spbrook                 DPRINTF("Card status 0x%02x\n", status);
164775616c3Spbrook             }
165775616c3Spbrook             s->mode = SSI_SD_RESPONSE;
166775616c3Spbrook             s->response_pos = 0;
167775616c3Spbrook         } else {
168775616c3Spbrook             s->cmdarg[s->arglen++] = val;
169775616c3Spbrook         }
170775616c3Spbrook         return 0xff;
171775616c3Spbrook     case SSI_SD_RESPONSE:
172775616c3Spbrook         if (s->stopping) {
173775616c3Spbrook             s->stopping = 0;
174775616c3Spbrook             return 0xff;
175775616c3Spbrook         }
176775616c3Spbrook         if (s->response_pos < s->arglen) {
177775616c3Spbrook             DPRINTF("Response 0x%02x\n", s->response[s->response_pos]);
178775616c3Spbrook             return s->response[s->response_pos++];
179775616c3Spbrook         }
180*c3abd913SPhilippe Mathieu-Daudé         if (sdbus_data_ready(&s->sdbus)) {
181775616c3Spbrook             DPRINTF("Data read\n");
182775616c3Spbrook             s->mode = SSI_SD_DATA_START;
183775616c3Spbrook         } else {
184775616c3Spbrook             DPRINTF("End of command\n");
185775616c3Spbrook             s->mode = SSI_SD_CMD;
186775616c3Spbrook         }
187775616c3Spbrook         return 0xff;
188775616c3Spbrook     case SSI_SD_DATA_START:
189775616c3Spbrook         DPRINTF("Start read block\n");
190775616c3Spbrook         s->mode = SSI_SD_DATA_READ;
191775616c3Spbrook         return 0xfe;
192775616c3Spbrook     case SSI_SD_DATA_READ:
193*c3abd913SPhilippe Mathieu-Daudé         val = sdbus_read_data(&s->sdbus);
194*c3abd913SPhilippe Mathieu-Daudé         if (!sdbus_data_ready(&s->sdbus)) {
195775616c3Spbrook             DPRINTF("Data read end\n");
196775616c3Spbrook             s->mode = SSI_SD_CMD;
197775616c3Spbrook         }
198775616c3Spbrook         return val;
199775616c3Spbrook     }
200775616c3Spbrook     /* Should never happen.  */
201775616c3Spbrook     return 0xff;
202775616c3Spbrook }
203775616c3Spbrook 
2042ccfd336SDr. David Alan Gilbert static int ssi_sd_post_load(void *opaque, int version_id)
20523e39294Spbrook {
20623e39294Spbrook     ssi_sd_state *s = (ssi_sd_state *)opaque;
20723e39294Spbrook 
2082ccfd336SDr. David Alan Gilbert     if (s->mode > SSI_SD_DATA_READ) {
20923e39294Spbrook         return -EINVAL;
2102ccfd336SDr. David Alan Gilbert     }
211a9c380dbSMichael S. Tsirkin     if (s->mode == SSI_SD_CMDARG &&
212a9c380dbSMichael S. Tsirkin         (s->arglen < 0 || s->arglen >= ARRAY_SIZE(s->cmdarg))) {
213a9c380dbSMichael S. Tsirkin         return -EINVAL;
214a9c380dbSMichael S. Tsirkin     }
215a9c380dbSMichael S. Tsirkin     if (s->mode == SSI_SD_RESPONSE &&
216a9c380dbSMichael S. Tsirkin         (s->response_pos < 0 || s->response_pos >= ARRAY_SIZE(s->response) ||
217a9c380dbSMichael S. Tsirkin         (!s->stopping && s->arglen > ARRAY_SIZE(s->response)))) {
218a9c380dbSMichael S. Tsirkin         return -EINVAL;
219a9c380dbSMichael S. Tsirkin     }
22023e39294Spbrook 
22123e39294Spbrook     return 0;
22223e39294Spbrook }
22323e39294Spbrook 
2242ccfd336SDr. David Alan Gilbert static const VMStateDescription vmstate_ssi_sd = {
2252ccfd336SDr. David Alan Gilbert     .name = "ssi_sd",
2262ccfd336SDr. David Alan Gilbert     .version_id = 2,
2272ccfd336SDr. David Alan Gilbert     .minimum_version_id = 2,
2282ccfd336SDr. David Alan Gilbert     .post_load = ssi_sd_post_load,
2292ccfd336SDr. David Alan Gilbert     .fields = (VMStateField []) {
2302ccfd336SDr. David Alan Gilbert         VMSTATE_UINT32(mode, ssi_sd_state),
2312ccfd336SDr. David Alan Gilbert         VMSTATE_INT32(cmd, ssi_sd_state),
2322ccfd336SDr. David Alan Gilbert         VMSTATE_UINT8_ARRAY(cmdarg, ssi_sd_state, 4),
2332ccfd336SDr. David Alan Gilbert         VMSTATE_UINT8_ARRAY(response, ssi_sd_state, 5),
2342ccfd336SDr. David Alan Gilbert         VMSTATE_INT32(arglen, ssi_sd_state),
2352ccfd336SDr. David Alan Gilbert         VMSTATE_INT32(response_pos, ssi_sd_state),
2362ccfd336SDr. David Alan Gilbert         VMSTATE_INT32(stopping, ssi_sd_state),
2372ccfd336SDr. David Alan Gilbert         VMSTATE_SSI_SLAVE(ssidev, ssi_sd_state),
2382ccfd336SDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
2392ccfd336SDr. David Alan Gilbert     }
2402ccfd336SDr. David Alan Gilbert };
2412ccfd336SDr. David Alan Gilbert 
2427673bb4cSCédric Le Goater static void ssi_sd_realize(SSISlave *d, Error **errp)
243775616c3Spbrook {
2441a7d9ee6SPeter Crosthwaite     ssi_sd_state *s = FROM_SSI_SLAVE(ssi_sd_state, d);
245*c3abd913SPhilippe Mathieu-Daudé     DeviceState *carddev;
24613839974SMarkus Armbruster     DriveInfo *dinfo;
247*c3abd913SPhilippe Mathieu-Daudé     Error *err = NULL;
248775616c3Spbrook 
249*c3abd913SPhilippe Mathieu-Daudé     qbus_create_inplace(&s->sdbus, sizeof(s->sdbus), TYPE_SD_BUS,
250*c3abd913SPhilippe Mathieu-Daudé                         DEVICE(d), "sd-bus");
251*c3abd913SPhilippe Mathieu-Daudé 
252*c3abd913SPhilippe Mathieu-Daudé     /* Create and plug in the sd card */
253af9e40aaSMarkus Armbruster     /* FIXME use a qdev drive property instead of drive_get_next() */
25413839974SMarkus Armbruster     dinfo = drive_get_next(IF_SD);
255*c3abd913SPhilippe Mathieu-Daudé     carddev = qdev_create(&s->sdbus.qbus, TYPE_SD_CARD);
256*c3abd913SPhilippe Mathieu-Daudé     if (dinfo) {
257*c3abd913SPhilippe Mathieu-Daudé         qdev_prop_set_drive(carddev, "drive", blk_by_legacy_dinfo(dinfo), &err);
258*c3abd913SPhilippe Mathieu-Daudé     }
259*c3abd913SPhilippe Mathieu-Daudé     object_property_set_bool(OBJECT(carddev), true, "spi", &err);
260*c3abd913SPhilippe Mathieu-Daudé     object_property_set_bool(OBJECT(carddev), true, "realized", &err);
261*c3abd913SPhilippe Mathieu-Daudé     if (err) {
262*c3abd913SPhilippe Mathieu-Daudé         error_setg(errp, "failed to init SD card: %s", error_get_pretty(err));
2637673bb4cSCédric Le Goater         return;
2644f8a066bSKevin Wolf     }
265775616c3Spbrook }
2665493e33fSPaul Brook 
2678046d44fSPeter Maydell static void ssi_sd_reset(DeviceState *dev)
2688046d44fSPeter Maydell {
2698046d44fSPeter Maydell     ssi_sd_state *s = SSI_SD(dev);
2708046d44fSPeter Maydell 
2718046d44fSPeter Maydell     s->mode = SSI_SD_CMD;
2728046d44fSPeter Maydell     s->cmd = 0;
2738046d44fSPeter Maydell     memset(s->cmdarg, 0, sizeof(s->cmdarg));
2748046d44fSPeter Maydell     memset(s->response, 0, sizeof(s->response));
2758046d44fSPeter Maydell     s->arglen = 0;
2768046d44fSPeter Maydell     s->response_pos = 0;
2778046d44fSPeter Maydell     s->stopping = 0;
2788046d44fSPeter Maydell }
2798046d44fSPeter Maydell 
280cd6c4cf2SAnthony Liguori static void ssi_sd_class_init(ObjectClass *klass, void *data)
281cd6c4cf2SAnthony Liguori {
2822ccfd336SDr. David Alan Gilbert     DeviceClass *dc = DEVICE_CLASS(klass);
283cd6c4cf2SAnthony Liguori     SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
284cd6c4cf2SAnthony Liguori 
2857673bb4cSCédric Le Goater     k->realize = ssi_sd_realize;
286cd6c4cf2SAnthony Liguori     k->transfer = ssi_sd_transfer;
2878120e714SPeter A. G. Crosthwaite     k->cs_polarity = SSI_CS_LOW;
2882ccfd336SDr. David Alan Gilbert     dc->vmsd = &vmstate_ssi_sd;
2898046d44fSPeter Maydell     dc->reset = ssi_sd_reset;
290cd6c4cf2SAnthony Liguori }
291cd6c4cf2SAnthony Liguori 
2928c43a6f0SAndreas Färber static const TypeInfo ssi_sd_info = {
2938046d44fSPeter Maydell     .name          = TYPE_SSI_SD,
29439bffca2SAnthony Liguori     .parent        = TYPE_SSI_SLAVE,
29539bffca2SAnthony Liguori     .instance_size = sizeof(ssi_sd_state),
296cd6c4cf2SAnthony Liguori     .class_init    = ssi_sd_class_init,
2975493e33fSPaul Brook };
2985493e33fSPaul Brook 
29983f7d43aSAndreas Färber static void ssi_sd_register_types(void)
3005493e33fSPaul Brook {
30139bffca2SAnthony Liguori     type_register_static(&ssi_sd_info);
3025493e33fSPaul Brook }
3035493e33fSPaul Brook 
30483f7d43aSAndreas Färber type_init(ssi_sd_register_types)
305