Statistics
| Branch: | Revision:

root / hw / ssi-sd.c @ 4e4fa398

History | View | Annotate | Download (8.3 kB)

1 775616c3 pbrook
/*
2 775616c3 pbrook
 * SSI to SD card adapter.
3 775616c3 pbrook
 *
4 5493e33f Paul Brook
 * Copyright (c) 2007-2009 CodeSourcery.
5 775616c3 pbrook
 * Written by Paul Brook
6 775616c3 pbrook
 *
7 8e31bf38 Matthew Fernandez
 * This code is licensed under the GNU GPL v2.
8 6b620ca3 Paolo Bonzini
 *
9 6b620ca3 Paolo Bonzini
 * Contributions after 2012-01-13 are licensed under the terms of the
10 6b620ca3 Paolo Bonzini
 * GNU GPL, version 2 or (at your option) any later version.
11 775616c3 pbrook
 */
12 775616c3 pbrook
13 13839974 Markus Armbruster
#include "blockdev.h"
14 5493e33f Paul Brook
#include "ssi.h"
15 775616c3 pbrook
#include "sd.h"
16 775616c3 pbrook
17 775616c3 pbrook
//#define DEBUG_SSI_SD 1
18 775616c3 pbrook
19 775616c3 pbrook
#ifdef DEBUG_SSI_SD
20 001faf32 Blue Swirl
#define DPRINTF(fmt, ...) \
21 001faf32 Blue Swirl
do { printf("ssi_sd: " fmt , ## __VA_ARGS__); } while (0)
22 001faf32 Blue Swirl
#define BADF(fmt, ...) \
23 001faf32 Blue Swirl
do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
24 775616c3 pbrook
#else
25 001faf32 Blue Swirl
#define DPRINTF(fmt, ...) do {} while(0)
26 001faf32 Blue Swirl
#define BADF(fmt, ...) \
27 001faf32 Blue Swirl
do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__);} while (0)
28 775616c3 pbrook
#endif
29 775616c3 pbrook
30 775616c3 pbrook
typedef enum {
31 775616c3 pbrook
    SSI_SD_CMD,
32 775616c3 pbrook
    SSI_SD_CMDARG,
33 775616c3 pbrook
    SSI_SD_RESPONSE,
34 775616c3 pbrook
    SSI_SD_DATA_START,
35 775616c3 pbrook
    SSI_SD_DATA_READ,
36 775616c3 pbrook
} ssi_sd_mode;
37 775616c3 pbrook
38 775616c3 pbrook
typedef struct {
39 5493e33f Paul Brook
    SSISlave ssidev;
40 775616c3 pbrook
    ssi_sd_mode mode;
41 775616c3 pbrook
    int cmd;
42 775616c3 pbrook
    uint8_t cmdarg[4];
43 775616c3 pbrook
    uint8_t response[5];
44 775616c3 pbrook
    int arglen;
45 775616c3 pbrook
    int response_pos;
46 775616c3 pbrook
    int stopping;
47 775616c3 pbrook
    SDState *sd;
48 775616c3 pbrook
} ssi_sd_state;
49 775616c3 pbrook
50 775616c3 pbrook
/* State word bits.  */
51 775616c3 pbrook
#define SSI_SDR_LOCKED          0x0001
52 775616c3 pbrook
#define SSI_SDR_WP_ERASE        0x0002
53 775616c3 pbrook
#define SSI_SDR_ERROR           0x0004
54 775616c3 pbrook
#define SSI_SDR_CC_ERROR        0x0008
55 775616c3 pbrook
#define SSI_SDR_ECC_FAILED      0x0010
56 775616c3 pbrook
#define SSI_SDR_WP_VIOLATION    0x0020
57 775616c3 pbrook
#define SSI_SDR_ERASE_PARAM     0x0040
58 775616c3 pbrook
#define SSI_SDR_OUT_OF_RANGE    0x0080
59 775616c3 pbrook
#define SSI_SDR_IDLE            0x0100
60 775616c3 pbrook
#define SSI_SDR_ERASE_RESET     0x0200
61 775616c3 pbrook
#define SSI_SDR_ILLEGAL_COMMAND 0x0400
62 775616c3 pbrook
#define SSI_SDR_COM_CRC_ERROR   0x0800
63 775616c3 pbrook
#define SSI_SDR_ERASE_SEQ_ERROR 0x1000
64 775616c3 pbrook
#define SSI_SDR_ADDRESS_ERROR   0x2000
65 775616c3 pbrook
#define SSI_SDR_PARAMETER_ERROR 0x4000
66 775616c3 pbrook
67 5493e33f Paul Brook
static uint32_t ssi_sd_transfer(SSISlave *dev, uint32_t val)
68 775616c3 pbrook
{
69 5493e33f Paul Brook
    ssi_sd_state *s = FROM_SSI_SLAVE(ssi_sd_state, dev);
70 775616c3 pbrook
71 775616c3 pbrook
    /* Special case: allow CMD12 (STOP TRANSMISSION) while reading data.  */
72 775616c3 pbrook
    if (s->mode == SSI_SD_DATA_READ && val == 0x4d) {
73 775616c3 pbrook
        s->mode = SSI_SD_CMD;
74 775616c3 pbrook
        /* There must be at least one byte delay before the card responds.  */
75 775616c3 pbrook
        s->stopping = 1;
76 775616c3 pbrook
    }
77 775616c3 pbrook
78 775616c3 pbrook
    switch (s->mode) {
79 775616c3 pbrook
    case SSI_SD_CMD:
80 775616c3 pbrook
        if (val == 0xff) {
81 775616c3 pbrook
            DPRINTF("NULL command\n");
82 775616c3 pbrook
            return 0xff;
83 775616c3 pbrook
        }
84 775616c3 pbrook
        s->cmd = val & 0x3f;
85 775616c3 pbrook
        s->mode = SSI_SD_CMDARG;
86 775616c3 pbrook
        s->arglen = 0;
87 775616c3 pbrook
        return 0xff;
88 775616c3 pbrook
    case SSI_SD_CMDARG:
89 775616c3 pbrook
        if (s->arglen == 4) {
90 bc24a225 Paul Brook
            SDRequest request;
91 775616c3 pbrook
            uint8_t longresp[16];
92 775616c3 pbrook
            /* FIXME: Check CRC.  */
93 775616c3 pbrook
            request.cmd = s->cmd;
94 775616c3 pbrook
            request.arg = (s->cmdarg[0] << 24) | (s->cmdarg[1] << 16)
95 775616c3 pbrook
                           | (s->cmdarg[2] << 8) | s->cmdarg[3];
96 775616c3 pbrook
            DPRINTF("CMD%d arg 0x%08x\n", s->cmd, request.arg);
97 775616c3 pbrook
            s->arglen = sd_do_command(s->sd, &request, longresp);
98 775616c3 pbrook
            if (s->arglen <= 0) {
99 775616c3 pbrook
                s->arglen = 1;
100 775616c3 pbrook
                s->response[0] = 4;
101 775616c3 pbrook
                DPRINTF("SD command failed\n");
102 775616c3 pbrook
            } else if (s->cmd == 58) {
103 775616c3 pbrook
                /* CMD58 returns R3 response (OCR)  */
104 775616c3 pbrook
                DPRINTF("Returned OCR\n");
105 775616c3 pbrook
                s->arglen = 5;
106 775616c3 pbrook
                s->response[0] = 1;
107 775616c3 pbrook
                memcpy(&s->response[1], longresp, 4);
108 775616c3 pbrook
            } else if (s->arglen != 4) {
109 775616c3 pbrook
                BADF("Unexpected response to cmd %d\n", s->cmd);
110 775616c3 pbrook
                /* Illegal command is about as near as we can get.  */
111 775616c3 pbrook
                s->arglen = 1;
112 775616c3 pbrook
                s->response[0] = 4;
113 775616c3 pbrook
            } else {
114 775616c3 pbrook
                /* All other commands return status.  */
115 775616c3 pbrook
                uint32_t cardstatus;
116 775616c3 pbrook
                uint16_t status;
117 775616c3 pbrook
                /* CMD13 returns a 2-byte statuse work. Other commands
118 775616c3 pbrook
                   only return the first byte.  */
119 775616c3 pbrook
                s->arglen = (s->cmd == 13) ? 2 : 1;
120 775616c3 pbrook
                cardstatus = (longresp[0] << 24) | (longresp[1] << 16)
121 775616c3 pbrook
                             | (longresp[2] << 8) | longresp[3];
122 775616c3 pbrook
                status = 0;
123 775616c3 pbrook
                if (((cardstatus >> 9) & 0xf) < 4)
124 775616c3 pbrook
                    status |= SSI_SDR_IDLE;
125 775616c3 pbrook
                if (cardstatus & ERASE_RESET)
126 775616c3 pbrook
                    status |= SSI_SDR_ERASE_RESET;
127 775616c3 pbrook
                if (cardstatus & ILLEGAL_COMMAND)
128 775616c3 pbrook
                    status |= SSI_SDR_ILLEGAL_COMMAND;
129 775616c3 pbrook
                if (cardstatus & COM_CRC_ERROR)
130 775616c3 pbrook
                    status |= SSI_SDR_COM_CRC_ERROR;
131 775616c3 pbrook
                if (cardstatus & ERASE_SEQ_ERROR)
132 775616c3 pbrook
                    status |= SSI_SDR_ERASE_SEQ_ERROR;
133 775616c3 pbrook
                if (cardstatus & ADDRESS_ERROR)
134 775616c3 pbrook
                    status |= SSI_SDR_ADDRESS_ERROR;
135 775616c3 pbrook
                if (cardstatus & CARD_IS_LOCKED)
136 775616c3 pbrook
                    status |= SSI_SDR_LOCKED;
137 775616c3 pbrook
                if (cardstatus & (LOCK_UNLOCK_FAILED | WP_ERASE_SKIP))
138 775616c3 pbrook
                    status |= SSI_SDR_WP_ERASE;
139 775616c3 pbrook
                if (cardstatus & SD_ERROR)
140 775616c3 pbrook
                    status |= SSI_SDR_ERROR;
141 775616c3 pbrook
                if (cardstatus & CC_ERROR)
142 775616c3 pbrook
                    status |= SSI_SDR_CC_ERROR;
143 775616c3 pbrook
                if (cardstatus & CARD_ECC_FAILED)
144 775616c3 pbrook
                    status |= SSI_SDR_ECC_FAILED;
145 775616c3 pbrook
                if (cardstatus & WP_VIOLATION)
146 775616c3 pbrook
                    status |= SSI_SDR_WP_VIOLATION;
147 775616c3 pbrook
                if (cardstatus & ERASE_PARAM)
148 775616c3 pbrook
                    status |= SSI_SDR_ERASE_PARAM;
149 775616c3 pbrook
                if (cardstatus & (OUT_OF_RANGE | CID_CSD_OVERWRITE))
150 775616c3 pbrook
                    status |= SSI_SDR_OUT_OF_RANGE;
151 775616c3 pbrook
                /* ??? Don't know what Parameter Error really means, so
152 775616c3 pbrook
                   assume it's set if the second byte is nonzero.  */
153 775616c3 pbrook
                if (status & 0xff)
154 775616c3 pbrook
                    status |= SSI_SDR_PARAMETER_ERROR;
155 775616c3 pbrook
                s->response[0] = status >> 8;
156 775616c3 pbrook
                s->response[1] = status;
157 775616c3 pbrook
                DPRINTF("Card status 0x%02x\n", status);
158 775616c3 pbrook
            }
159 775616c3 pbrook
            s->mode = SSI_SD_RESPONSE;
160 775616c3 pbrook
            s->response_pos = 0;
161 775616c3 pbrook
        } else {
162 775616c3 pbrook
            s->cmdarg[s->arglen++] = val;
163 775616c3 pbrook
        }
164 775616c3 pbrook
        return 0xff;
165 775616c3 pbrook
    case SSI_SD_RESPONSE:
166 775616c3 pbrook
        if (s->stopping) {
167 775616c3 pbrook
            s->stopping = 0;
168 775616c3 pbrook
            return 0xff;
169 775616c3 pbrook
        }
170 775616c3 pbrook
        if (s->response_pos < s->arglen) {
171 775616c3 pbrook
            DPRINTF("Response 0x%02x\n", s->response[s->response_pos]);
172 775616c3 pbrook
            return s->response[s->response_pos++];
173 775616c3 pbrook
        }
174 775616c3 pbrook
        if (sd_data_ready(s->sd)) {
175 775616c3 pbrook
            DPRINTF("Data read\n");
176 775616c3 pbrook
            s->mode = SSI_SD_DATA_START;
177 775616c3 pbrook
        } else {
178 775616c3 pbrook
            DPRINTF("End of command\n");
179 775616c3 pbrook
            s->mode = SSI_SD_CMD;
180 775616c3 pbrook
        }
181 775616c3 pbrook
        return 0xff;
182 775616c3 pbrook
    case SSI_SD_DATA_START:
183 775616c3 pbrook
        DPRINTF("Start read block\n");
184 775616c3 pbrook
        s->mode = SSI_SD_DATA_READ;
185 775616c3 pbrook
        return 0xfe;
186 775616c3 pbrook
    case SSI_SD_DATA_READ:
187 775616c3 pbrook
        val = sd_read_data(s->sd);
188 775616c3 pbrook
        if (!sd_data_ready(s->sd)) {
189 775616c3 pbrook
            DPRINTF("Data read end\n");
190 775616c3 pbrook
            s->mode = SSI_SD_CMD;
191 775616c3 pbrook
        }
192 775616c3 pbrook
        return val;
193 775616c3 pbrook
    }
194 775616c3 pbrook
    /* Should never happen.  */
195 775616c3 pbrook
    return 0xff;
196 775616c3 pbrook
}
197 775616c3 pbrook
198 23e39294 pbrook
static void ssi_sd_save(QEMUFile *f, void *opaque)
199 23e39294 pbrook
{
200 23e39294 pbrook
    ssi_sd_state *s = (ssi_sd_state *)opaque;
201 23e39294 pbrook
    int i;
202 23e39294 pbrook
203 23e39294 pbrook
    qemu_put_be32(f, s->mode);
204 23e39294 pbrook
    qemu_put_be32(f, s->cmd);
205 23e39294 pbrook
    for (i = 0; i < 4; i++)
206 23e39294 pbrook
        qemu_put_be32(f, s->cmdarg[i]);
207 23e39294 pbrook
    for (i = 0; i < 5; i++)
208 23e39294 pbrook
        qemu_put_be32(f, s->response[i]);
209 23e39294 pbrook
    qemu_put_be32(f, s->arglen);
210 23e39294 pbrook
    qemu_put_be32(f, s->response_pos);
211 23e39294 pbrook
    qemu_put_be32(f, s->stopping);
212 23e39294 pbrook
}
213 23e39294 pbrook
214 23e39294 pbrook
static int ssi_sd_load(QEMUFile *f, void *opaque, int version_id)
215 23e39294 pbrook
{
216 23e39294 pbrook
    ssi_sd_state *s = (ssi_sd_state *)opaque;
217 23e39294 pbrook
    int i;
218 23e39294 pbrook
219 23e39294 pbrook
    if (version_id != 1)
220 23e39294 pbrook
        return -EINVAL;
221 23e39294 pbrook
222 23e39294 pbrook
    s->mode = qemu_get_be32(f);
223 23e39294 pbrook
    s->cmd = qemu_get_be32(f);
224 23e39294 pbrook
    for (i = 0; i < 4; i++)
225 23e39294 pbrook
        s->cmdarg[i] = qemu_get_be32(f);
226 23e39294 pbrook
    for (i = 0; i < 5; i++)
227 23e39294 pbrook
        s->response[i] = qemu_get_be32(f);
228 23e39294 pbrook
    s->arglen = qemu_get_be32(f);
229 23e39294 pbrook
    s->response_pos = qemu_get_be32(f);
230 23e39294 pbrook
    s->stopping = qemu_get_be32(f);
231 23e39294 pbrook
232 23e39294 pbrook
    return 0;
233 23e39294 pbrook
}
234 23e39294 pbrook
235 81a322d4 Gerd Hoffmann
static int ssi_sd_init(SSISlave *dev)
236 775616c3 pbrook
{
237 5493e33f Paul Brook
    ssi_sd_state *s = FROM_SSI_SLAVE(ssi_sd_state, dev);
238 13839974 Markus Armbruster
    DriveInfo *dinfo;
239 775616c3 pbrook
240 775616c3 pbrook
    s->mode = SSI_SD_CMD;
241 13839974 Markus Armbruster
    dinfo = drive_get_next(IF_SD);
242 13839974 Markus Armbruster
    s->sd = sd_init(dinfo ? dinfo->bdrv : NULL, 1);
243 0be71e32 Alex Williamson
    register_savevm(&dev->qdev, "ssi_sd", -1, 1, ssi_sd_save, ssi_sd_load, s);
244 81a322d4 Gerd Hoffmann
    return 0;
245 775616c3 pbrook
}
246 5493e33f Paul Brook
247 cd6c4cf2 Anthony Liguori
static void ssi_sd_class_init(ObjectClass *klass, void *data)
248 cd6c4cf2 Anthony Liguori
{
249 cd6c4cf2 Anthony Liguori
    SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
250 cd6c4cf2 Anthony Liguori
251 cd6c4cf2 Anthony Liguori
    k->init = ssi_sd_init;
252 cd6c4cf2 Anthony Liguori
    k->transfer = ssi_sd_transfer;
253 cd6c4cf2 Anthony Liguori
}
254 cd6c4cf2 Anthony Liguori
255 cd6c4cf2 Anthony Liguori
static DeviceInfo ssi_sd_info = {
256 cd6c4cf2 Anthony Liguori
    .name = "ssi-sd",
257 cd6c4cf2 Anthony Liguori
    .size = sizeof(ssi_sd_state),
258 cd6c4cf2 Anthony Liguori
    .class_init = ssi_sd_class_init,
259 5493e33f Paul Brook
};
260 5493e33f Paul Brook
261 5493e33f Paul Brook
static void ssi_sd_register_devices(void)
262 5493e33f Paul Brook
{
263 074f2fff Gerd Hoffmann
    ssi_register_slave(&ssi_sd_info);
264 5493e33f Paul Brook
}
265 5493e33f Paul Brook
266 5493e33f Paul Brook
device_init(ssi_sd_register_devices)