ssi-sd.c 8.9 KB
Newer Older
P
pbrook 已提交
1 2 3
/*
 * SSI to SD card adapter.
 *
P
Paul Brook 已提交
4
 * Copyright (c) 2007-2009 CodeSourcery.
P
pbrook 已提交
5 6
 * Written by Paul Brook
 *
M
Matthew Fernandez 已提交
7
 * This code is licensed under the GNU GPL v2.
8 9 10
 *
 * Contributions after 2012-01-13 are licensed under the terms of the
 * GNU GPL, version 2 or (at your option) any later version.
P
pbrook 已提交
11 12
 */

P
Peter Maydell 已提交
13
#include "qemu/osdep.h"
14
#include "sysemu/block-backend.h"
15
#include "sysemu/blockdev.h"
16
#include "hw/ssi/ssi.h"
17
#include "hw/sd/sd.h"
18
#include "qapi/error.h"
P
pbrook 已提交
19 20 21 22

//#define DEBUG_SSI_SD 1

#ifdef DEBUG_SSI_SD
23 24 25 26
#define DPRINTF(fmt, ...) \
do { printf("ssi_sd: " fmt , ## __VA_ARGS__); } while (0)
#define BADF(fmt, ...) \
do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
P
pbrook 已提交
27
#else
28 29 30
#define DPRINTF(fmt, ...) do {} while(0)
#define BADF(fmt, ...) \
do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__);} while (0)
P
pbrook 已提交
31 32 33
#endif

typedef enum {
D
Dr. David Alan Gilbert 已提交
34
    SSI_SD_CMD = 0,
P
pbrook 已提交
35 36 37 38 39 40 41
    SSI_SD_CMDARG,
    SSI_SD_RESPONSE,
    SSI_SD_DATA_START,
    SSI_SD_DATA_READ,
} ssi_sd_mode;

typedef struct {
P
Paul Brook 已提交
42
    SSISlave ssidev;
D
Dr. David Alan Gilbert 已提交
43
    uint32_t mode;
P
pbrook 已提交
44 45 46
    int cmd;
    uint8_t cmdarg[4];
    uint8_t response[5];
D
Dr. David Alan Gilbert 已提交
47 48 49
    int32_t arglen;
    int32_t response_pos;
    int32_t stopping;
P
pbrook 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    SDState *sd;
} ssi_sd_state;

/* State word bits.  */
#define SSI_SDR_LOCKED          0x0001
#define SSI_SDR_WP_ERASE        0x0002
#define SSI_SDR_ERROR           0x0004
#define SSI_SDR_CC_ERROR        0x0008
#define SSI_SDR_ECC_FAILED      0x0010
#define SSI_SDR_WP_VIOLATION    0x0020
#define SSI_SDR_ERASE_PARAM     0x0040
#define SSI_SDR_OUT_OF_RANGE    0x0080
#define SSI_SDR_IDLE            0x0100
#define SSI_SDR_ERASE_RESET     0x0200
#define SSI_SDR_ILLEGAL_COMMAND 0x0400
#define SSI_SDR_COM_CRC_ERROR   0x0800
#define SSI_SDR_ERASE_SEQ_ERROR 0x1000
#define SSI_SDR_ADDRESS_ERROR   0x2000
#define SSI_SDR_PARAMETER_ERROR 0x4000

P
Paul Brook 已提交
70
static uint32_t ssi_sd_transfer(SSISlave *dev, uint32_t val)
P
pbrook 已提交
71
{
P
Paul Brook 已提交
72
    ssi_sd_state *s = FROM_SSI_SLAVE(ssi_sd_state, dev);
P
pbrook 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

    /* Special case: allow CMD12 (STOP TRANSMISSION) while reading data.  */
    if (s->mode == SSI_SD_DATA_READ && val == 0x4d) {
        s->mode = SSI_SD_CMD;
        /* There must be at least one byte delay before the card responds.  */
        s->stopping = 1;
    }

    switch (s->mode) {
    case SSI_SD_CMD:
        if (val == 0xff) {
            DPRINTF("NULL command\n");
            return 0xff;
        }
        s->cmd = val & 0x3f;
        s->mode = SSI_SD_CMDARG;
        s->arglen = 0;
        return 0xff;
    case SSI_SD_CMDARG:
        if (s->arglen == 4) {
P
Paul Brook 已提交
93
            SDRequest request;
P
pbrook 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
            uint8_t longresp[16];
            /* FIXME: Check CRC.  */
            request.cmd = s->cmd;
            request.arg = (s->cmdarg[0] << 24) | (s->cmdarg[1] << 16)
                           | (s->cmdarg[2] << 8) | s->cmdarg[3];
            DPRINTF("CMD%d arg 0x%08x\n", s->cmd, request.arg);
            s->arglen = sd_do_command(s->sd, &request, longresp);
            if (s->arglen <= 0) {
                s->arglen = 1;
                s->response[0] = 4;
                DPRINTF("SD command failed\n");
            } else if (s->cmd == 58) {
                /* CMD58 returns R3 response (OCR)  */
                DPRINTF("Returned OCR\n");
                s->arglen = 5;
                s->response[0] = 1;
                memcpy(&s->response[1], longresp, 4);
            } else if (s->arglen != 4) {
                BADF("Unexpected response to cmd %d\n", s->cmd);
                /* Illegal command is about as near as we can get.  */
                s->arglen = 1;
                s->response[0] = 4;
            } else {
                /* All other commands return status.  */
                uint32_t cardstatus;
                uint16_t status;
                /* CMD13 returns a 2-byte statuse work. Other commands
                   only return the first byte.  */
                s->arglen = (s->cmd == 13) ? 2 : 1;
                cardstatus = (longresp[0] << 24) | (longresp[1] << 16)
                             | (longresp[2] << 8) | longresp[3];
                status = 0;
                if (((cardstatus >> 9) & 0xf) < 4)
                    status |= SSI_SDR_IDLE;
                if (cardstatus & ERASE_RESET)
                    status |= SSI_SDR_ERASE_RESET;
                if (cardstatus & ILLEGAL_COMMAND)
                    status |= SSI_SDR_ILLEGAL_COMMAND;
                if (cardstatus & COM_CRC_ERROR)
                    status |= SSI_SDR_COM_CRC_ERROR;
                if (cardstatus & ERASE_SEQ_ERROR)
                    status |= SSI_SDR_ERASE_SEQ_ERROR;
                if (cardstatus & ADDRESS_ERROR)
                    status |= SSI_SDR_ADDRESS_ERROR;
                if (cardstatus & CARD_IS_LOCKED)
                    status |= SSI_SDR_LOCKED;
                if (cardstatus & (LOCK_UNLOCK_FAILED | WP_ERASE_SKIP))
                    status |= SSI_SDR_WP_ERASE;
                if (cardstatus & SD_ERROR)
                    status |= SSI_SDR_ERROR;
                if (cardstatus & CC_ERROR)
                    status |= SSI_SDR_CC_ERROR;
                if (cardstatus & CARD_ECC_FAILED)
                    status |= SSI_SDR_ECC_FAILED;
                if (cardstatus & WP_VIOLATION)
                    status |= SSI_SDR_WP_VIOLATION;
                if (cardstatus & ERASE_PARAM)
                    status |= SSI_SDR_ERASE_PARAM;
                if (cardstatus & (OUT_OF_RANGE | CID_CSD_OVERWRITE))
                    status |= SSI_SDR_OUT_OF_RANGE;
                /* ??? Don't know what Parameter Error really means, so
                   assume it's set if the second byte is nonzero.  */
                if (status & 0xff)
                    status |= SSI_SDR_PARAMETER_ERROR;
                s->response[0] = status >> 8;
                s->response[1] = status;
                DPRINTF("Card status 0x%02x\n", status);
            }
            s->mode = SSI_SD_RESPONSE;
            s->response_pos = 0;
        } else {
            s->cmdarg[s->arglen++] = val;
        }
        return 0xff;
    case SSI_SD_RESPONSE:
        if (s->stopping) {
            s->stopping = 0;
            return 0xff;
        }
        if (s->response_pos < s->arglen) {
            DPRINTF("Response 0x%02x\n", s->response[s->response_pos]);
            return s->response[s->response_pos++];
        }
        if (sd_data_ready(s->sd)) {
            DPRINTF("Data read\n");
            s->mode = SSI_SD_DATA_START;
        } else {
            DPRINTF("End of command\n");
            s->mode = SSI_SD_CMD;
        }
        return 0xff;
    case SSI_SD_DATA_START:
        DPRINTF("Start read block\n");
        s->mode = SSI_SD_DATA_READ;
        return 0xfe;
    case SSI_SD_DATA_READ:
        val = sd_read_data(s->sd);
        if (!sd_data_ready(s->sd)) {
            DPRINTF("Data read end\n");
            s->mode = SSI_SD_CMD;
        }
        return val;
    }
    /* Should never happen.  */
    return 0xff;
}

D
Dr. David Alan Gilbert 已提交
201
static int ssi_sd_post_load(void *opaque, int version_id)
P
pbrook 已提交
202 203 204
{
    ssi_sd_state *s = (ssi_sd_state *)opaque;

D
Dr. David Alan Gilbert 已提交
205
    if (s->mode > SSI_SD_DATA_READ) {
P
pbrook 已提交
206
        return -EINVAL;
D
Dr. David Alan Gilbert 已提交
207
    }
208 209 210 211 212 213 214 215 216
    if (s->mode == SSI_SD_CMDARG &&
        (s->arglen < 0 || s->arglen >= ARRAY_SIZE(s->cmdarg))) {
        return -EINVAL;
    }
    if (s->mode == SSI_SD_RESPONSE &&
        (s->response_pos < 0 || s->response_pos >= ARRAY_SIZE(s->response) ||
        (!s->stopping && s->arglen > ARRAY_SIZE(s->response)))) {
        return -EINVAL;
    }
P
pbrook 已提交
217 218 219 220

    return 0;
}

D
Dr. David Alan Gilbert 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
static const VMStateDescription vmstate_ssi_sd = {
    .name = "ssi_sd",
    .version_id = 2,
    .minimum_version_id = 2,
    .post_load = ssi_sd_post_load,
    .fields = (VMStateField []) {
        VMSTATE_UINT32(mode, ssi_sd_state),
        VMSTATE_INT32(cmd, ssi_sd_state),
        VMSTATE_UINT8_ARRAY(cmdarg, ssi_sd_state, 4),
        VMSTATE_UINT8_ARRAY(response, ssi_sd_state, 5),
        VMSTATE_INT32(arglen, ssi_sd_state),
        VMSTATE_INT32(response_pos, ssi_sd_state),
        VMSTATE_INT32(stopping, ssi_sd_state),
        VMSTATE_SSI_SLAVE(ssidev, ssi_sd_state),
        VMSTATE_END_OF_LIST()
    }
};

239
static void ssi_sd_realize(SSISlave *d, Error **errp)
P
pbrook 已提交
240
{
241
    ssi_sd_state *s = FROM_SSI_SLAVE(ssi_sd_state, d);
242
    DriveInfo *dinfo;
P
pbrook 已提交
243 244

    s->mode = SSI_SD_CMD;
245
    /* FIXME use a qdev drive property instead of drive_get_next() */
246
    dinfo = drive_get_next(IF_SD);
247
    s->sd = sd_init(dinfo ? blk_by_legacy_dinfo(dinfo) : NULL, true);
248
    if (s->sd == NULL) {
249 250
        error_setg(errp, "Device initialization failed.");
        return;
251
    }
P
pbrook 已提交
252
}
P
Paul Brook 已提交
253

254 255
static void ssi_sd_class_init(ObjectClass *klass, void *data)
{
D
Dr. David Alan Gilbert 已提交
256
    DeviceClass *dc = DEVICE_CLASS(klass);
257 258
    SSISlaveClass *k = SSI_SLAVE_CLASS(klass);

259
    k->realize = ssi_sd_realize;
260
    k->transfer = ssi_sd_transfer;
261
    k->cs_polarity = SSI_CS_LOW;
D
Dr. David Alan Gilbert 已提交
262
    dc->vmsd = &vmstate_ssi_sd;
263 264
}

265
static const TypeInfo ssi_sd_info = {
266 267 268 269
    .name          = "ssi-sd",
    .parent        = TYPE_SSI_SLAVE,
    .instance_size = sizeof(ssi_sd_state),
    .class_init    = ssi_sd_class_init,
P
Paul Brook 已提交
270 271
};

A
Andreas Färber 已提交
272
static void ssi_sd_register_types(void)
P
Paul Brook 已提交
273
{
274
    type_register_static(&ssi_sd_info);
P
Paul Brook 已提交
275 276
}

A
Andreas Färber 已提交
277
type_init(ssi_sd_register_types)