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 { 34*2ccfd336SDr. 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; 43*2ccfd336SDr. David Alan Gilbert uint32_t mode; 44775616c3Spbrook int cmd; 45775616c3Spbrook uint8_t cmdarg[4]; 46775616c3Spbrook uint8_t response[5]; 47*2ccfd336SDr. David Alan Gilbert int32_t arglen; 48*2ccfd336SDr. David Alan Gilbert int32_t response_pos; 49*2ccfd336SDr. David Alan Gilbert int32_t stopping; 50775616c3Spbrook SDState *sd; 51775616c3Spbrook } ssi_sd_state; 52775616c3Spbrook 53775616c3Spbrook /* State word bits. */ 54775616c3Spbrook #define SSI_SDR_LOCKED 0x0001 55775616c3Spbrook #define SSI_SDR_WP_ERASE 0x0002 56775616c3Spbrook #define SSI_SDR_ERROR 0x0004 57775616c3Spbrook #define SSI_SDR_CC_ERROR 0x0008 58775616c3Spbrook #define SSI_SDR_ECC_FAILED 0x0010 59775616c3Spbrook #define SSI_SDR_WP_VIOLATION 0x0020 60775616c3Spbrook #define SSI_SDR_ERASE_PARAM 0x0040 61775616c3Spbrook #define SSI_SDR_OUT_OF_RANGE 0x0080 62775616c3Spbrook #define SSI_SDR_IDLE 0x0100 63775616c3Spbrook #define SSI_SDR_ERASE_RESET 0x0200 64775616c3Spbrook #define SSI_SDR_ILLEGAL_COMMAND 0x0400 65775616c3Spbrook #define SSI_SDR_COM_CRC_ERROR 0x0800 66775616c3Spbrook #define SSI_SDR_ERASE_SEQ_ERROR 0x1000 67775616c3Spbrook #define SSI_SDR_ADDRESS_ERROR 0x2000 68775616c3Spbrook #define SSI_SDR_PARAMETER_ERROR 0x4000 69775616c3Spbrook 705493e33fSPaul Brook static uint32_t ssi_sd_transfer(SSISlave *dev, uint32_t val) 71775616c3Spbrook { 725493e33fSPaul Brook ssi_sd_state *s = FROM_SSI_SLAVE(ssi_sd_state, dev); 73775616c3Spbrook 74775616c3Spbrook /* Special case: allow CMD12 (STOP TRANSMISSION) while reading data. */ 75775616c3Spbrook if (s->mode == SSI_SD_DATA_READ && val == 0x4d) { 76775616c3Spbrook s->mode = SSI_SD_CMD; 77775616c3Spbrook /* There must be at least one byte delay before the card responds. */ 78775616c3Spbrook s->stopping = 1; 79775616c3Spbrook } 80775616c3Spbrook 81775616c3Spbrook switch (s->mode) { 82775616c3Spbrook case SSI_SD_CMD: 83775616c3Spbrook if (val == 0xff) { 84775616c3Spbrook DPRINTF("NULL command\n"); 85775616c3Spbrook return 0xff; 86775616c3Spbrook } 87775616c3Spbrook s->cmd = val & 0x3f; 88775616c3Spbrook s->mode = SSI_SD_CMDARG; 89775616c3Spbrook s->arglen = 0; 90775616c3Spbrook return 0xff; 91775616c3Spbrook case SSI_SD_CMDARG: 92775616c3Spbrook if (s->arglen == 4) { 93bc24a225SPaul Brook SDRequest request; 94775616c3Spbrook uint8_t longresp[16]; 95775616c3Spbrook /* FIXME: Check CRC. */ 96775616c3Spbrook request.cmd = s->cmd; 97775616c3Spbrook request.arg = (s->cmdarg[0] << 24) | (s->cmdarg[1] << 16) 98775616c3Spbrook | (s->cmdarg[2] << 8) | s->cmdarg[3]; 99775616c3Spbrook DPRINTF("CMD%d arg 0x%08x\n", s->cmd, request.arg); 100775616c3Spbrook s->arglen = sd_do_command(s->sd, &request, longresp); 101775616c3Spbrook if (s->arglen <= 0) { 102775616c3Spbrook s->arglen = 1; 103775616c3Spbrook s->response[0] = 4; 104775616c3Spbrook DPRINTF("SD command failed\n"); 105775616c3Spbrook } else if (s->cmd == 58) { 106775616c3Spbrook /* CMD58 returns R3 response (OCR) */ 107775616c3Spbrook DPRINTF("Returned OCR\n"); 108775616c3Spbrook s->arglen = 5; 109775616c3Spbrook s->response[0] = 1; 110775616c3Spbrook memcpy(&s->response[1], longresp, 4); 111775616c3Spbrook } else if (s->arglen != 4) { 112775616c3Spbrook BADF("Unexpected response to cmd %d\n", s->cmd); 113775616c3Spbrook /* Illegal command is about as near as we can get. */ 114775616c3Spbrook s->arglen = 1; 115775616c3Spbrook s->response[0] = 4; 116775616c3Spbrook } else { 117775616c3Spbrook /* All other commands return status. */ 118775616c3Spbrook uint32_t cardstatus; 119775616c3Spbrook uint16_t status; 120775616c3Spbrook /* CMD13 returns a 2-byte statuse work. Other commands 121775616c3Spbrook only return the first byte. */ 122775616c3Spbrook s->arglen = (s->cmd == 13) ? 2 : 1; 123775616c3Spbrook cardstatus = (longresp[0] << 24) | (longresp[1] << 16) 124775616c3Spbrook | (longresp[2] << 8) | longresp[3]; 125775616c3Spbrook status = 0; 126775616c3Spbrook if (((cardstatus >> 9) & 0xf) < 4) 127775616c3Spbrook status |= SSI_SDR_IDLE; 128775616c3Spbrook if (cardstatus & ERASE_RESET) 129775616c3Spbrook status |= SSI_SDR_ERASE_RESET; 130775616c3Spbrook if (cardstatus & ILLEGAL_COMMAND) 131775616c3Spbrook status |= SSI_SDR_ILLEGAL_COMMAND; 132775616c3Spbrook if (cardstatus & COM_CRC_ERROR) 133775616c3Spbrook status |= SSI_SDR_COM_CRC_ERROR; 134775616c3Spbrook if (cardstatus & ERASE_SEQ_ERROR) 135775616c3Spbrook status |= SSI_SDR_ERASE_SEQ_ERROR; 136775616c3Spbrook if (cardstatus & ADDRESS_ERROR) 137775616c3Spbrook status |= SSI_SDR_ADDRESS_ERROR; 138775616c3Spbrook if (cardstatus & CARD_IS_LOCKED) 139775616c3Spbrook status |= SSI_SDR_LOCKED; 140775616c3Spbrook if (cardstatus & (LOCK_UNLOCK_FAILED | WP_ERASE_SKIP)) 141775616c3Spbrook status |= SSI_SDR_WP_ERASE; 142775616c3Spbrook if (cardstatus & SD_ERROR) 143775616c3Spbrook status |= SSI_SDR_ERROR; 144775616c3Spbrook if (cardstatus & CC_ERROR) 145775616c3Spbrook status |= SSI_SDR_CC_ERROR; 146775616c3Spbrook if (cardstatus & CARD_ECC_FAILED) 147775616c3Spbrook status |= SSI_SDR_ECC_FAILED; 148775616c3Spbrook if (cardstatus & WP_VIOLATION) 149775616c3Spbrook status |= SSI_SDR_WP_VIOLATION; 150775616c3Spbrook if (cardstatus & ERASE_PARAM) 151775616c3Spbrook status |= SSI_SDR_ERASE_PARAM; 152775616c3Spbrook if (cardstatus & (OUT_OF_RANGE | CID_CSD_OVERWRITE)) 153775616c3Spbrook status |= SSI_SDR_OUT_OF_RANGE; 154775616c3Spbrook /* ??? Don't know what Parameter Error really means, so 155775616c3Spbrook assume it's set if the second byte is nonzero. */ 156775616c3Spbrook if (status & 0xff) 157775616c3Spbrook status |= SSI_SDR_PARAMETER_ERROR; 158775616c3Spbrook s->response[0] = status >> 8; 159775616c3Spbrook s->response[1] = status; 160775616c3Spbrook DPRINTF("Card status 0x%02x\n", status); 161775616c3Spbrook } 162775616c3Spbrook s->mode = SSI_SD_RESPONSE; 163775616c3Spbrook s->response_pos = 0; 164775616c3Spbrook } else { 165775616c3Spbrook s->cmdarg[s->arglen++] = val; 166775616c3Spbrook } 167775616c3Spbrook return 0xff; 168775616c3Spbrook case SSI_SD_RESPONSE: 169775616c3Spbrook if (s->stopping) { 170775616c3Spbrook s->stopping = 0; 171775616c3Spbrook return 0xff; 172775616c3Spbrook } 173775616c3Spbrook if (s->response_pos < s->arglen) { 174775616c3Spbrook DPRINTF("Response 0x%02x\n", s->response[s->response_pos]); 175775616c3Spbrook return s->response[s->response_pos++]; 176775616c3Spbrook } 177775616c3Spbrook if (sd_data_ready(s->sd)) { 178775616c3Spbrook DPRINTF("Data read\n"); 179775616c3Spbrook s->mode = SSI_SD_DATA_START; 180775616c3Spbrook } else { 181775616c3Spbrook DPRINTF("End of command\n"); 182775616c3Spbrook s->mode = SSI_SD_CMD; 183775616c3Spbrook } 184775616c3Spbrook return 0xff; 185775616c3Spbrook case SSI_SD_DATA_START: 186775616c3Spbrook DPRINTF("Start read block\n"); 187775616c3Spbrook s->mode = SSI_SD_DATA_READ; 188775616c3Spbrook return 0xfe; 189775616c3Spbrook case SSI_SD_DATA_READ: 190775616c3Spbrook val = sd_read_data(s->sd); 191775616c3Spbrook if (!sd_data_ready(s->sd)) { 192775616c3Spbrook DPRINTF("Data read end\n"); 193775616c3Spbrook s->mode = SSI_SD_CMD; 194775616c3Spbrook } 195775616c3Spbrook return val; 196775616c3Spbrook } 197775616c3Spbrook /* Should never happen. */ 198775616c3Spbrook return 0xff; 199775616c3Spbrook } 200775616c3Spbrook 201*2ccfd336SDr. David Alan Gilbert static int ssi_sd_post_load(void *opaque, int version_id) 20223e39294Spbrook { 20323e39294Spbrook ssi_sd_state *s = (ssi_sd_state *)opaque; 20423e39294Spbrook 205*2ccfd336SDr. David Alan Gilbert if (s->mode > SSI_SD_DATA_READ) { 20623e39294Spbrook return -EINVAL; 207*2ccfd336SDr. David Alan Gilbert } 208a9c380dbSMichael S. Tsirkin if (s->mode == SSI_SD_CMDARG && 209a9c380dbSMichael S. Tsirkin (s->arglen < 0 || s->arglen >= ARRAY_SIZE(s->cmdarg))) { 210a9c380dbSMichael S. Tsirkin return -EINVAL; 211a9c380dbSMichael S. Tsirkin } 212a9c380dbSMichael S. Tsirkin if (s->mode == SSI_SD_RESPONSE && 213a9c380dbSMichael S. Tsirkin (s->response_pos < 0 || s->response_pos >= ARRAY_SIZE(s->response) || 214a9c380dbSMichael S. Tsirkin (!s->stopping && s->arglen > ARRAY_SIZE(s->response)))) { 215a9c380dbSMichael S. Tsirkin return -EINVAL; 216a9c380dbSMichael S. Tsirkin } 21723e39294Spbrook 21823e39294Spbrook return 0; 21923e39294Spbrook } 22023e39294Spbrook 221*2ccfd336SDr. David Alan Gilbert static const VMStateDescription vmstate_ssi_sd = { 222*2ccfd336SDr. David Alan Gilbert .name = "ssi_sd", 223*2ccfd336SDr. David Alan Gilbert .version_id = 2, 224*2ccfd336SDr. David Alan Gilbert .minimum_version_id = 2, 225*2ccfd336SDr. David Alan Gilbert .post_load = ssi_sd_post_load, 226*2ccfd336SDr. David Alan Gilbert .fields = (VMStateField []) { 227*2ccfd336SDr. David Alan Gilbert VMSTATE_UINT32(mode, ssi_sd_state), 228*2ccfd336SDr. David Alan Gilbert VMSTATE_INT32(cmd, ssi_sd_state), 229*2ccfd336SDr. David Alan Gilbert VMSTATE_UINT8_ARRAY(cmdarg, ssi_sd_state, 4), 230*2ccfd336SDr. David Alan Gilbert VMSTATE_UINT8_ARRAY(response, ssi_sd_state, 5), 231*2ccfd336SDr. David Alan Gilbert VMSTATE_INT32(arglen, ssi_sd_state), 232*2ccfd336SDr. David Alan Gilbert VMSTATE_INT32(response_pos, ssi_sd_state), 233*2ccfd336SDr. David Alan Gilbert VMSTATE_INT32(stopping, ssi_sd_state), 234*2ccfd336SDr. David Alan Gilbert VMSTATE_SSI_SLAVE(ssidev, ssi_sd_state), 235*2ccfd336SDr. David Alan Gilbert VMSTATE_END_OF_LIST() 236*2ccfd336SDr. David Alan Gilbert } 237*2ccfd336SDr. David Alan Gilbert }; 238*2ccfd336SDr. David Alan Gilbert 2397673bb4cSCédric Le Goater static void ssi_sd_realize(SSISlave *d, Error **errp) 240775616c3Spbrook { 2411a7d9ee6SPeter Crosthwaite ssi_sd_state *s = FROM_SSI_SLAVE(ssi_sd_state, d); 24213839974SMarkus Armbruster DriveInfo *dinfo; 243775616c3Spbrook 244775616c3Spbrook s->mode = SSI_SD_CMD; 245af9e40aaSMarkus Armbruster /* FIXME use a qdev drive property instead of drive_get_next() */ 24613839974SMarkus Armbruster dinfo = drive_get_next(IF_SD); 2474be74634SMarkus Armbruster s->sd = sd_init(dinfo ? blk_by_legacy_dinfo(dinfo) : NULL, true); 2484f8a066bSKevin Wolf if (s->sd == NULL) { 2497673bb4cSCédric Le Goater error_setg(errp, "Device initialization failed."); 2507673bb4cSCédric Le Goater return; 2514f8a066bSKevin Wolf } 252775616c3Spbrook } 2535493e33fSPaul Brook 254cd6c4cf2SAnthony Liguori static void ssi_sd_class_init(ObjectClass *klass, void *data) 255cd6c4cf2SAnthony Liguori { 256*2ccfd336SDr. David Alan Gilbert DeviceClass *dc = DEVICE_CLASS(klass); 257cd6c4cf2SAnthony Liguori SSISlaveClass *k = SSI_SLAVE_CLASS(klass); 258cd6c4cf2SAnthony Liguori 2597673bb4cSCédric Le Goater k->realize = ssi_sd_realize; 260cd6c4cf2SAnthony Liguori k->transfer = ssi_sd_transfer; 2618120e714SPeter A. G. Crosthwaite k->cs_polarity = SSI_CS_LOW; 262*2ccfd336SDr. David Alan Gilbert dc->vmsd = &vmstate_ssi_sd; 263cd6c4cf2SAnthony Liguori } 264cd6c4cf2SAnthony Liguori 2658c43a6f0SAndreas Färber static const TypeInfo ssi_sd_info = { 266cd6c4cf2SAnthony Liguori .name = "ssi-sd", 26739bffca2SAnthony Liguori .parent = TYPE_SSI_SLAVE, 26839bffca2SAnthony Liguori .instance_size = sizeof(ssi_sd_state), 269cd6c4cf2SAnthony Liguori .class_init = ssi_sd_class_init, 2705493e33fSPaul Brook }; 2715493e33fSPaul Brook 27283f7d43aSAndreas Färber static void ssi_sd_register_types(void) 2735493e33fSPaul Brook { 27439bffca2SAnthony Liguori type_register_static(&ssi_sd_info); 2755493e33fSPaul Brook } 2765493e33fSPaul Brook 27783f7d43aSAndreas Färber type_init(ssi_sd_register_types) 278