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