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