Statistics
| Branch: | Revision:

root / hw / ssi-sd.c @ ff753bb9

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