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