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 13*0430891cSPeter 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" 18775616c3Spbrook 19775616c3Spbrook //#define DEBUG_SSI_SD 1 20775616c3Spbrook 21775616c3Spbrook #ifdef DEBUG_SSI_SD 22001faf32SBlue Swirl #define DPRINTF(fmt, ...) \ 23001faf32SBlue Swirl do { printf("ssi_sd: " fmt , ## __VA_ARGS__); } while (0) 24001faf32SBlue Swirl #define BADF(fmt, ...) \ 25001faf32SBlue Swirl do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__); exit(1);} while (0) 26775616c3Spbrook #else 27001faf32SBlue Swirl #define DPRINTF(fmt, ...) do {} while(0) 28001faf32SBlue Swirl #define BADF(fmt, ...) \ 29001faf32SBlue Swirl do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__);} while (0) 30775616c3Spbrook #endif 31775616c3Spbrook 32775616c3Spbrook typedef enum { 33775616c3Spbrook SSI_SD_CMD, 34775616c3Spbrook SSI_SD_CMDARG, 35775616c3Spbrook SSI_SD_RESPONSE, 36775616c3Spbrook SSI_SD_DATA_START, 37775616c3Spbrook SSI_SD_DATA_READ, 38775616c3Spbrook } ssi_sd_mode; 39775616c3Spbrook 40775616c3Spbrook typedef struct { 415493e33fSPaul Brook SSISlave ssidev; 42775616c3Spbrook ssi_sd_mode mode; 43775616c3Spbrook int cmd; 44775616c3Spbrook uint8_t cmdarg[4]; 45775616c3Spbrook uint8_t response[5]; 46775616c3Spbrook int arglen; 47775616c3Spbrook int response_pos; 48775616c3Spbrook int stopping; 49775616c3Spbrook SDState *sd; 50775616c3Spbrook } ssi_sd_state; 51775616c3Spbrook 52775616c3Spbrook /* State word bits. */ 53775616c3Spbrook #define SSI_SDR_LOCKED 0x0001 54775616c3Spbrook #define SSI_SDR_WP_ERASE 0x0002 55775616c3Spbrook #define SSI_SDR_ERROR 0x0004 56775616c3Spbrook #define SSI_SDR_CC_ERROR 0x0008 57775616c3Spbrook #define SSI_SDR_ECC_FAILED 0x0010 58775616c3Spbrook #define SSI_SDR_WP_VIOLATION 0x0020 59775616c3Spbrook #define SSI_SDR_ERASE_PARAM 0x0040 60775616c3Spbrook #define SSI_SDR_OUT_OF_RANGE 0x0080 61775616c3Spbrook #define SSI_SDR_IDLE 0x0100 62775616c3Spbrook #define SSI_SDR_ERASE_RESET 0x0200 63775616c3Spbrook #define SSI_SDR_ILLEGAL_COMMAND 0x0400 64775616c3Spbrook #define SSI_SDR_COM_CRC_ERROR 0x0800 65775616c3Spbrook #define SSI_SDR_ERASE_SEQ_ERROR 0x1000 66775616c3Spbrook #define SSI_SDR_ADDRESS_ERROR 0x2000 67775616c3Spbrook #define SSI_SDR_PARAMETER_ERROR 0x4000 68775616c3Spbrook 695493e33fSPaul Brook static uint32_t ssi_sd_transfer(SSISlave *dev, uint32_t val) 70775616c3Spbrook { 715493e33fSPaul Brook ssi_sd_state *s = FROM_SSI_SLAVE(ssi_sd_state, dev); 72775616c3Spbrook 73775616c3Spbrook /* Special case: allow CMD12 (STOP TRANSMISSION) while reading data. */ 74775616c3Spbrook if (s->mode == SSI_SD_DATA_READ && val == 0x4d) { 75775616c3Spbrook s->mode = SSI_SD_CMD; 76775616c3Spbrook /* There must be at least one byte delay before the card responds. */ 77775616c3Spbrook s->stopping = 1; 78775616c3Spbrook } 79775616c3Spbrook 80775616c3Spbrook switch (s->mode) { 81775616c3Spbrook case SSI_SD_CMD: 82775616c3Spbrook if (val == 0xff) { 83775616c3Spbrook DPRINTF("NULL command\n"); 84775616c3Spbrook return 0xff; 85775616c3Spbrook } 86775616c3Spbrook s->cmd = val & 0x3f; 87775616c3Spbrook s->mode = SSI_SD_CMDARG; 88775616c3Spbrook s->arglen = 0; 89775616c3Spbrook return 0xff; 90775616c3Spbrook case SSI_SD_CMDARG: 91775616c3Spbrook if (s->arglen == 4) { 92bc24a225SPaul Brook SDRequest request; 93775616c3Spbrook uint8_t longresp[16]; 94775616c3Spbrook /* FIXME: Check CRC. */ 95775616c3Spbrook request.cmd = s->cmd; 96775616c3Spbrook request.arg = (s->cmdarg[0] << 24) | (s->cmdarg[1] << 16) 97775616c3Spbrook | (s->cmdarg[2] << 8) | s->cmdarg[3]; 98775616c3Spbrook DPRINTF("CMD%d arg 0x%08x\n", s->cmd, request.arg); 99775616c3Spbrook s->arglen = sd_do_command(s->sd, &request, longresp); 100775616c3Spbrook if (s->arglen <= 0) { 101775616c3Spbrook s->arglen = 1; 102775616c3Spbrook s->response[0] = 4; 103775616c3Spbrook DPRINTF("SD command failed\n"); 104775616c3Spbrook } else if (s->cmd == 58) { 105775616c3Spbrook /* CMD58 returns R3 response (OCR) */ 106775616c3Spbrook DPRINTF("Returned OCR\n"); 107775616c3Spbrook s->arglen = 5; 108775616c3Spbrook s->response[0] = 1; 109775616c3Spbrook memcpy(&s->response[1], longresp, 4); 110775616c3Spbrook } else if (s->arglen != 4) { 111775616c3Spbrook BADF("Unexpected response to cmd %d\n", s->cmd); 112775616c3Spbrook /* Illegal command is about as near as we can get. */ 113775616c3Spbrook s->arglen = 1; 114775616c3Spbrook s->response[0] = 4; 115775616c3Spbrook } else { 116775616c3Spbrook /* All other commands return status. */ 117775616c3Spbrook uint32_t cardstatus; 118775616c3Spbrook uint16_t status; 119775616c3Spbrook /* CMD13 returns a 2-byte statuse work. Other commands 120775616c3Spbrook only return the first byte. */ 121775616c3Spbrook s->arglen = (s->cmd == 13) ? 2 : 1; 122775616c3Spbrook cardstatus = (longresp[0] << 24) | (longresp[1] << 16) 123775616c3Spbrook | (longresp[2] << 8) | longresp[3]; 124775616c3Spbrook status = 0; 125775616c3Spbrook if (((cardstatus >> 9) & 0xf) < 4) 126775616c3Spbrook status |= SSI_SDR_IDLE; 127775616c3Spbrook if (cardstatus & ERASE_RESET) 128775616c3Spbrook status |= SSI_SDR_ERASE_RESET; 129775616c3Spbrook if (cardstatus & ILLEGAL_COMMAND) 130775616c3Spbrook status |= SSI_SDR_ILLEGAL_COMMAND; 131775616c3Spbrook if (cardstatus & COM_CRC_ERROR) 132775616c3Spbrook status |= SSI_SDR_COM_CRC_ERROR; 133775616c3Spbrook if (cardstatus & ERASE_SEQ_ERROR) 134775616c3Spbrook status |= SSI_SDR_ERASE_SEQ_ERROR; 135775616c3Spbrook if (cardstatus & ADDRESS_ERROR) 136775616c3Spbrook status |= SSI_SDR_ADDRESS_ERROR; 137775616c3Spbrook if (cardstatus & CARD_IS_LOCKED) 138775616c3Spbrook status |= SSI_SDR_LOCKED; 139775616c3Spbrook if (cardstatus & (LOCK_UNLOCK_FAILED | WP_ERASE_SKIP)) 140775616c3Spbrook status |= SSI_SDR_WP_ERASE; 141775616c3Spbrook if (cardstatus & SD_ERROR) 142775616c3Spbrook status |= SSI_SDR_ERROR; 143775616c3Spbrook if (cardstatus & CC_ERROR) 144775616c3Spbrook status |= SSI_SDR_CC_ERROR; 145775616c3Spbrook if (cardstatus & CARD_ECC_FAILED) 146775616c3Spbrook status |= SSI_SDR_ECC_FAILED; 147775616c3Spbrook if (cardstatus & WP_VIOLATION) 148775616c3Spbrook status |= SSI_SDR_WP_VIOLATION; 149775616c3Spbrook if (cardstatus & ERASE_PARAM) 150775616c3Spbrook status |= SSI_SDR_ERASE_PARAM; 151775616c3Spbrook if (cardstatus & (OUT_OF_RANGE | CID_CSD_OVERWRITE)) 152775616c3Spbrook status |= SSI_SDR_OUT_OF_RANGE; 153775616c3Spbrook /* ??? Don't know what Parameter Error really means, so 154775616c3Spbrook assume it's set if the second byte is nonzero. */ 155775616c3Spbrook if (status & 0xff) 156775616c3Spbrook status |= SSI_SDR_PARAMETER_ERROR; 157775616c3Spbrook s->response[0] = status >> 8; 158775616c3Spbrook s->response[1] = status; 159775616c3Spbrook DPRINTF("Card status 0x%02x\n", status); 160775616c3Spbrook } 161775616c3Spbrook s->mode = SSI_SD_RESPONSE; 162775616c3Spbrook s->response_pos = 0; 163775616c3Spbrook } else { 164775616c3Spbrook s->cmdarg[s->arglen++] = val; 165775616c3Spbrook } 166775616c3Spbrook return 0xff; 167775616c3Spbrook case SSI_SD_RESPONSE: 168775616c3Spbrook if (s->stopping) { 169775616c3Spbrook s->stopping = 0; 170775616c3Spbrook return 0xff; 171775616c3Spbrook } 172775616c3Spbrook if (s->response_pos < s->arglen) { 173775616c3Spbrook DPRINTF("Response 0x%02x\n", s->response[s->response_pos]); 174775616c3Spbrook return s->response[s->response_pos++]; 175775616c3Spbrook } 176775616c3Spbrook if (sd_data_ready(s->sd)) { 177775616c3Spbrook DPRINTF("Data read\n"); 178775616c3Spbrook s->mode = SSI_SD_DATA_START; 179775616c3Spbrook } else { 180775616c3Spbrook DPRINTF("End of command\n"); 181775616c3Spbrook s->mode = SSI_SD_CMD; 182775616c3Spbrook } 183775616c3Spbrook return 0xff; 184775616c3Spbrook case SSI_SD_DATA_START: 185775616c3Spbrook DPRINTF("Start read block\n"); 186775616c3Spbrook s->mode = SSI_SD_DATA_READ; 187775616c3Spbrook return 0xfe; 188775616c3Spbrook case SSI_SD_DATA_READ: 189775616c3Spbrook val = sd_read_data(s->sd); 190775616c3Spbrook if (!sd_data_ready(s->sd)) { 191775616c3Spbrook DPRINTF("Data read end\n"); 192775616c3Spbrook s->mode = SSI_SD_CMD; 193775616c3Spbrook } 194775616c3Spbrook return val; 195775616c3Spbrook } 196775616c3Spbrook /* Should never happen. */ 197775616c3Spbrook return 0xff; 198775616c3Spbrook } 199775616c3Spbrook 20023e39294Spbrook static void ssi_sd_save(QEMUFile *f, void *opaque) 20123e39294Spbrook { 20266530953SPeter A. G. Crosthwaite SSISlave *ss = SSI_SLAVE(opaque); 20323e39294Spbrook ssi_sd_state *s = (ssi_sd_state *)opaque; 20423e39294Spbrook int i; 20523e39294Spbrook 20623e39294Spbrook qemu_put_be32(f, s->mode); 20723e39294Spbrook qemu_put_be32(f, s->cmd); 20823e39294Spbrook for (i = 0; i < 4; i++) 20923e39294Spbrook qemu_put_be32(f, s->cmdarg[i]); 21023e39294Spbrook for (i = 0; i < 5; i++) 21123e39294Spbrook qemu_put_be32(f, s->response[i]); 21223e39294Spbrook qemu_put_be32(f, s->arglen); 21323e39294Spbrook qemu_put_be32(f, s->response_pos); 21423e39294Spbrook qemu_put_be32(f, s->stopping); 21566530953SPeter A. G. Crosthwaite 21666530953SPeter A. G. Crosthwaite qemu_put_be32(f, ss->cs); 21723e39294Spbrook } 21823e39294Spbrook 21923e39294Spbrook static int ssi_sd_load(QEMUFile *f, void *opaque, int version_id) 22023e39294Spbrook { 22166530953SPeter A. G. Crosthwaite SSISlave *ss = SSI_SLAVE(opaque); 22223e39294Spbrook ssi_sd_state *s = (ssi_sd_state *)opaque; 22323e39294Spbrook int i; 22423e39294Spbrook 22523e39294Spbrook if (version_id != 1) 22623e39294Spbrook return -EINVAL; 22723e39294Spbrook 22823e39294Spbrook s->mode = qemu_get_be32(f); 22923e39294Spbrook s->cmd = qemu_get_be32(f); 23023e39294Spbrook for (i = 0; i < 4; i++) 23123e39294Spbrook s->cmdarg[i] = qemu_get_be32(f); 23223e39294Spbrook for (i = 0; i < 5; i++) 23323e39294Spbrook s->response[i] = qemu_get_be32(f); 23423e39294Spbrook s->arglen = qemu_get_be32(f); 235a9c380dbSMichael S. Tsirkin if (s->mode == SSI_SD_CMDARG && 236a9c380dbSMichael S. Tsirkin (s->arglen < 0 || s->arglen >= ARRAY_SIZE(s->cmdarg))) { 237a9c380dbSMichael S. Tsirkin return -EINVAL; 238a9c380dbSMichael S. Tsirkin } 23923e39294Spbrook s->response_pos = qemu_get_be32(f); 24023e39294Spbrook s->stopping = qemu_get_be32(f); 241a9c380dbSMichael S. Tsirkin if (s->mode == SSI_SD_RESPONSE && 242a9c380dbSMichael S. Tsirkin (s->response_pos < 0 || s->response_pos >= ARRAY_SIZE(s->response) || 243a9c380dbSMichael S. Tsirkin (!s->stopping && s->arglen > ARRAY_SIZE(s->response)))) { 244a9c380dbSMichael S. Tsirkin return -EINVAL; 245a9c380dbSMichael S. Tsirkin } 24623e39294Spbrook 24766530953SPeter A. G. Crosthwaite ss->cs = qemu_get_be32(f); 24866530953SPeter A. G. Crosthwaite 24923e39294Spbrook return 0; 25023e39294Spbrook } 25123e39294Spbrook 2521a7d9ee6SPeter Crosthwaite static int ssi_sd_init(SSISlave *d) 253775616c3Spbrook { 2541a7d9ee6SPeter Crosthwaite DeviceState *dev = DEVICE(d); 2551a7d9ee6SPeter Crosthwaite ssi_sd_state *s = FROM_SSI_SLAVE(ssi_sd_state, d); 25613839974SMarkus Armbruster DriveInfo *dinfo; 257775616c3Spbrook 258775616c3Spbrook s->mode = SSI_SD_CMD; 259af9e40aaSMarkus Armbruster /* FIXME use a qdev drive property instead of drive_get_next() */ 26013839974SMarkus Armbruster dinfo = drive_get_next(IF_SD); 2614be74634SMarkus Armbruster s->sd = sd_init(dinfo ? blk_by_legacy_dinfo(dinfo) : NULL, true); 2624f8a066bSKevin Wolf if (s->sd == NULL) { 2634f8a066bSKevin Wolf return -1; 2644f8a066bSKevin Wolf } 2651a7d9ee6SPeter Crosthwaite register_savevm(dev, "ssi_sd", -1, 1, ssi_sd_save, ssi_sd_load, s); 26681a322d4SGerd Hoffmann return 0; 267775616c3Spbrook } 2685493e33fSPaul Brook 269cd6c4cf2SAnthony Liguori static void ssi_sd_class_init(ObjectClass *klass, void *data) 270cd6c4cf2SAnthony Liguori { 271cd6c4cf2SAnthony Liguori SSISlaveClass *k = SSI_SLAVE_CLASS(klass); 272cd6c4cf2SAnthony Liguori 273cd6c4cf2SAnthony Liguori k->init = ssi_sd_init; 274cd6c4cf2SAnthony Liguori k->transfer = ssi_sd_transfer; 2758120e714SPeter A. G. Crosthwaite k->cs_polarity = SSI_CS_LOW; 276cd6c4cf2SAnthony Liguori } 277cd6c4cf2SAnthony Liguori 2788c43a6f0SAndreas Färber static const TypeInfo ssi_sd_info = { 279cd6c4cf2SAnthony Liguori .name = "ssi-sd", 28039bffca2SAnthony Liguori .parent = TYPE_SSI_SLAVE, 28139bffca2SAnthony Liguori .instance_size = sizeof(ssi_sd_state), 282cd6c4cf2SAnthony Liguori .class_init = ssi_sd_class_init, 2835493e33fSPaul Brook }; 2845493e33fSPaul Brook 28583f7d43aSAndreas Färber static void ssi_sd_register_types(void) 2865493e33fSPaul Brook { 28739bffca2SAnthony Liguori type_register_static(&ssi_sd_info); 2885493e33fSPaul Brook } 2895493e33fSPaul Brook 29083f7d43aSAndreas Färber type_init(ssi_sd_register_types) 291