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