root / hw / sd.c @ 69c8944f
History | View | Annotate | Download (45.5 kB)
1 | 5fafdf24 | ths | /*
|
---|---|---|---|
2 | a1bb27b1 | pbrook | * SD Memory Card emulation as defined in the "SD Memory Card Physical
|
3 | a1bb27b1 | pbrook | * layer specification, Version 1.10."
|
4 | a1bb27b1 | pbrook | *
|
5 | a1bb27b1 | pbrook | * Copyright (c) 2006 Andrzej Zaborowski <balrog@zabor.org>
|
6 | a1bb27b1 | pbrook | * Copyright (c) 2007 CodeSourcery
|
7 | a1bb27b1 | pbrook | *
|
8 | a1bb27b1 | pbrook | * Redistribution and use in source and binary forms, with or without
|
9 | a1bb27b1 | pbrook | * modification, are permitted provided that the following conditions
|
10 | a1bb27b1 | pbrook | * are met:
|
11 | a1bb27b1 | pbrook | *
|
12 | a1bb27b1 | pbrook | * 1. Redistributions of source code must retain the above copyright
|
13 | a1bb27b1 | pbrook | * notice, this list of conditions and the following disclaimer.
|
14 | a1bb27b1 | pbrook | * 2. Redistributions in binary form must reproduce the above copyright
|
15 | a1bb27b1 | pbrook | * notice, this list of conditions and the following disclaimer in
|
16 | a1bb27b1 | pbrook | * the documentation and/or other materials provided with the
|
17 | a1bb27b1 | pbrook | * distribution.
|
18 | a1bb27b1 | pbrook | *
|
19 | a1bb27b1 | pbrook | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
|
20 | a1bb27b1 | pbrook | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
21 | a1bb27b1 | pbrook | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
22 | a1bb27b1 | pbrook | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
|
23 | a1bb27b1 | pbrook | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
24 | a1bb27b1 | pbrook | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
25 | a1bb27b1 | pbrook | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
26 | a1bb27b1 | pbrook | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
27 | a1bb27b1 | pbrook | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
28 | a1bb27b1 | pbrook | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
29 | a1bb27b1 | pbrook | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30 | a1bb27b1 | pbrook | */
|
31 | a1bb27b1 | pbrook | |
32 | 87ecb68b | pbrook | #include "hw.h" |
33 | 87ecb68b | pbrook | #include "block.h" |
34 | 879f0655 | Stefan Weil | #include "block_int.h" |
35 | a1bb27b1 | pbrook | #include "sd.h" |
36 | a1bb27b1 | pbrook | |
37 | a1bb27b1 | pbrook | //#define DEBUG_SD 1
|
38 | a1bb27b1 | pbrook | |
39 | a1bb27b1 | pbrook | #ifdef DEBUG_SD
|
40 | 001faf32 | Blue Swirl | #define DPRINTF(fmt, ...) \
|
41 | 001faf32 | Blue Swirl | do { fprintf(stderr, "SD: " fmt , ## __VA_ARGS__); } while (0) |
42 | a1bb27b1 | pbrook | #else
|
43 | 001faf32 | Blue Swirl | #define DPRINTF(fmt, ...) do {} while(0) |
44 | a1bb27b1 | pbrook | #endif
|
45 | a1bb27b1 | pbrook | |
46 | a1bb27b1 | pbrook | typedef enum { |
47 | a1bb27b1 | pbrook | sd_r0 = 0, /* no response */ |
48 | a1bb27b1 | pbrook | sd_r1, /* normal response command */
|
49 | a1bb27b1 | pbrook | sd_r2_i, /* CID register */
|
50 | a1bb27b1 | pbrook | sd_r2_s, /* CSD register */
|
51 | a1bb27b1 | pbrook | sd_r3, /* OCR register */
|
52 | a1bb27b1 | pbrook | sd_r6 = 6, /* Published RCA response */ |
53 | 1b088995 | balrog | sd_r7, /* Operating voltage */
|
54 | a1bb27b1 | pbrook | sd_r1b = -1,
|
55 | c227f099 | Anthony Liguori | } sd_rsp_type_t; |
56 | a1bb27b1 | pbrook | |
57 | a1bb27b1 | pbrook | struct SDState {
|
58 | a1bb27b1 | pbrook | enum {
|
59 | a1bb27b1 | pbrook | sd_inactive, |
60 | a1bb27b1 | pbrook | sd_card_identification_mode, |
61 | a1bb27b1 | pbrook | sd_data_transfer_mode, |
62 | a1bb27b1 | pbrook | } mode; |
63 | a1bb27b1 | pbrook | enum {
|
64 | a1bb27b1 | pbrook | sd_inactive_state = -1,
|
65 | a1bb27b1 | pbrook | sd_idle_state = 0,
|
66 | a1bb27b1 | pbrook | sd_ready_state, |
67 | a1bb27b1 | pbrook | sd_identification_state, |
68 | a1bb27b1 | pbrook | sd_standby_state, |
69 | a1bb27b1 | pbrook | sd_transfer_state, |
70 | a1bb27b1 | pbrook | sd_sendingdata_state, |
71 | a1bb27b1 | pbrook | sd_receivingdata_state, |
72 | a1bb27b1 | pbrook | sd_programming_state, |
73 | a1bb27b1 | pbrook | sd_disconnect_state, |
74 | a1bb27b1 | pbrook | } state; |
75 | a1bb27b1 | pbrook | uint32_t ocr; |
76 | a1bb27b1 | pbrook | uint8_t scr[8];
|
77 | a1bb27b1 | pbrook | uint8_t cid[16];
|
78 | a1bb27b1 | pbrook | uint8_t csd[16];
|
79 | a1bb27b1 | pbrook | uint16_t rca; |
80 | a1bb27b1 | pbrook | uint32_t card_status; |
81 | a1bb27b1 | pbrook | uint8_t sd_status[64];
|
82 | 1b088995 | balrog | uint32_t vhs; |
83 | a1bb27b1 | pbrook | int wp_switch;
|
84 | a1bb27b1 | pbrook | int *wp_groups;
|
85 | 2d7adea4 | Andrzej Zaborowski | uint64_t size; |
86 | a1bb27b1 | pbrook | int blk_len;
|
87 | a1bb27b1 | pbrook | uint32_t erase_start; |
88 | a1bb27b1 | pbrook | uint32_t erase_end; |
89 | a1bb27b1 | pbrook | uint8_t pwd[16];
|
90 | a1bb27b1 | pbrook | int pwd_len;
|
91 | a1bb27b1 | pbrook | int function_group[6]; |
92 | a1bb27b1 | pbrook | |
93 | 775616c3 | pbrook | int spi;
|
94 | a1bb27b1 | pbrook | int current_cmd;
|
95 | a1bb27b1 | pbrook | int blk_written;
|
96 | 2d7adea4 | Andrzej Zaborowski | uint64_t data_start; |
97 | a1bb27b1 | pbrook | uint32_t data_offset; |
98 | a1bb27b1 | pbrook | uint8_t data[512];
|
99 | 02ce600c | balrog | qemu_irq readonly_cb; |
100 | 02ce600c | balrog | qemu_irq inserted_cb; |
101 | a1bb27b1 | pbrook | BlockDriverState *bdrv; |
102 | 33f00271 | balrog | uint8_t *buf; |
103 | 827df9f3 | balrog | |
104 | 827df9f3 | balrog | int enable;
|
105 | a1bb27b1 | pbrook | }; |
106 | a1bb27b1 | pbrook | |
107 | a1bb27b1 | pbrook | static void sd_set_status(SDState *sd) |
108 | a1bb27b1 | pbrook | { |
109 | a1bb27b1 | pbrook | switch (sd->state) {
|
110 | a1bb27b1 | pbrook | case sd_inactive_state:
|
111 | a1bb27b1 | pbrook | sd->mode = sd_inactive; |
112 | a1bb27b1 | pbrook | break;
|
113 | a1bb27b1 | pbrook | |
114 | a1bb27b1 | pbrook | case sd_idle_state:
|
115 | a1bb27b1 | pbrook | case sd_ready_state:
|
116 | a1bb27b1 | pbrook | case sd_identification_state:
|
117 | a1bb27b1 | pbrook | sd->mode = sd_card_identification_mode; |
118 | a1bb27b1 | pbrook | break;
|
119 | a1bb27b1 | pbrook | |
120 | a1bb27b1 | pbrook | case sd_standby_state:
|
121 | a1bb27b1 | pbrook | case sd_transfer_state:
|
122 | a1bb27b1 | pbrook | case sd_sendingdata_state:
|
123 | a1bb27b1 | pbrook | case sd_receivingdata_state:
|
124 | a1bb27b1 | pbrook | case sd_programming_state:
|
125 | a1bb27b1 | pbrook | case sd_disconnect_state:
|
126 | a1bb27b1 | pbrook | sd->mode = sd_data_transfer_mode; |
127 | a1bb27b1 | pbrook | break;
|
128 | a1bb27b1 | pbrook | } |
129 | a1bb27b1 | pbrook | |
130 | a1bb27b1 | pbrook | sd->card_status &= ~CURRENT_STATE; |
131 | a1bb27b1 | pbrook | sd->card_status |= sd->state << 9;
|
132 | a1bb27b1 | pbrook | } |
133 | a1bb27b1 | pbrook | |
134 | c227f099 | Anthony Liguori | static const sd_cmd_type_t sd_cmd_type[64] = { |
135 | a1bb27b1 | pbrook | sd_bc, sd_none, sd_bcr, sd_bcr, sd_none, sd_none, sd_none, sd_ac, |
136 | 1b088995 | balrog | sd_bcr, sd_ac, sd_ac, sd_adtc, sd_ac, sd_ac, sd_none, sd_ac, |
137 | a1bb27b1 | pbrook | sd_ac, sd_adtc, sd_adtc, sd_none, sd_none, sd_none, sd_none, sd_none, |
138 | a1bb27b1 | pbrook | sd_adtc, sd_adtc, sd_adtc, sd_adtc, sd_ac, sd_ac, sd_adtc, sd_none, |
139 | a1bb27b1 | pbrook | sd_ac, sd_ac, sd_none, sd_none, sd_none, sd_none, sd_ac, sd_none, |
140 | a1bb27b1 | pbrook | sd_none, sd_none, sd_bc, sd_none, sd_none, sd_none, sd_none, sd_none, |
141 | a1bb27b1 | pbrook | sd_none, sd_none, sd_none, sd_none, sd_none, sd_none, sd_none, sd_ac, |
142 | a1bb27b1 | pbrook | sd_adtc, sd_none, sd_none, sd_none, sd_none, sd_none, sd_none, sd_none, |
143 | a1bb27b1 | pbrook | }; |
144 | a1bb27b1 | pbrook | |
145 | c227f099 | Anthony Liguori | static const sd_cmd_type_t sd_acmd_type[64] = { |
146 | a1bb27b1 | pbrook | sd_none, sd_none, sd_none, sd_none, sd_none, sd_none, sd_ac, sd_none, |
147 | a1bb27b1 | pbrook | sd_none, sd_none, sd_none, sd_none, sd_none, sd_adtc, sd_none, sd_none, |
148 | a1bb27b1 | pbrook | sd_none, sd_none, sd_none, sd_none, sd_none, sd_none, sd_adtc, sd_ac, |
149 | a1bb27b1 | pbrook | sd_none, sd_none, sd_none, sd_none, sd_none, sd_none, sd_none, sd_none, |
150 | a1bb27b1 | pbrook | sd_none, sd_none, sd_none, sd_none, sd_none, sd_none, sd_none, sd_none, |
151 | a1bb27b1 | pbrook | sd_none, sd_bcr, sd_ac, sd_none, sd_none, sd_none, sd_none, sd_none, |
152 | a1bb27b1 | pbrook | sd_none, sd_none, sd_none, sd_adtc, sd_none, sd_none, sd_none, sd_none, |
153 | a1bb27b1 | pbrook | sd_none, sd_none, sd_none, sd_none, sd_none, sd_none, sd_none, sd_none, |
154 | a1bb27b1 | pbrook | }; |
155 | a1bb27b1 | pbrook | |
156 | a1bb27b1 | pbrook | static const int sd_cmd_class[64] = { |
157 | a1bb27b1 | pbrook | 0, 0, 0, 0, 0, 9, 10, 0, 0, 0, 0, 1, 0, 0, 0, 0, |
158 | a1bb27b1 | pbrook | 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 6, 6, 6, 6, |
159 | a1bb27b1 | pbrook | 5, 5, 10, 10, 10, 10, 5, 9, 9, 9, 7, 7, 7, 7, 7, 7, |
160 | a1bb27b1 | pbrook | 7, 7, 10, 7, 9, 9, 9, 8, 8, 10, 8, 8, 8, 8, 8, 8, |
161 | a1bb27b1 | pbrook | }; |
162 | a1bb27b1 | pbrook | |
163 | a1bb27b1 | pbrook | static uint8_t sd_crc7(void *message, size_t width) |
164 | a1bb27b1 | pbrook | { |
165 | a1bb27b1 | pbrook | int i, bit;
|
166 | a1bb27b1 | pbrook | uint8_t shift_reg = 0x00;
|
167 | a1bb27b1 | pbrook | uint8_t *msg = (uint8_t *) message; |
168 | a1bb27b1 | pbrook | |
169 | a1bb27b1 | pbrook | for (i = 0; i < width; i ++, msg ++) |
170 | a1bb27b1 | pbrook | for (bit = 7; bit >= 0; bit --) { |
171 | a1bb27b1 | pbrook | shift_reg <<= 1;
|
172 | a1bb27b1 | pbrook | if ((shift_reg >> 7) ^ ((*msg >> bit) & 1)) |
173 | a1bb27b1 | pbrook | shift_reg ^= 0x89;
|
174 | a1bb27b1 | pbrook | } |
175 | a1bb27b1 | pbrook | |
176 | a1bb27b1 | pbrook | return shift_reg;
|
177 | a1bb27b1 | pbrook | } |
178 | a1bb27b1 | pbrook | |
179 | a1bb27b1 | pbrook | static uint16_t sd_crc16(void *message, size_t width) |
180 | a1bb27b1 | pbrook | { |
181 | a1bb27b1 | pbrook | int i, bit;
|
182 | a1bb27b1 | pbrook | uint16_t shift_reg = 0x0000;
|
183 | a1bb27b1 | pbrook | uint16_t *msg = (uint16_t *) message; |
184 | a1bb27b1 | pbrook | width <<= 1;
|
185 | a1bb27b1 | pbrook | |
186 | a1bb27b1 | pbrook | for (i = 0; i < width; i ++, msg ++) |
187 | a1bb27b1 | pbrook | for (bit = 15; bit >= 0; bit --) { |
188 | a1bb27b1 | pbrook | shift_reg <<= 1;
|
189 | a1bb27b1 | pbrook | if ((shift_reg >> 15) ^ ((*msg >> bit) & 1)) |
190 | a1bb27b1 | pbrook | shift_reg ^= 0x1011;
|
191 | a1bb27b1 | pbrook | } |
192 | a1bb27b1 | pbrook | |
193 | a1bb27b1 | pbrook | return shift_reg;
|
194 | a1bb27b1 | pbrook | } |
195 | a1bb27b1 | pbrook | |
196 | a1bb27b1 | pbrook | static void sd_set_ocr(SDState *sd) |
197 | a1bb27b1 | pbrook | { |
198 | 1b088995 | balrog | /* All voltages OK, card power-up OK, Standard Capacity SD Memory Card */
|
199 | 829ef7b0 | aurel32 | sd->ocr = 0x80ffff00;
|
200 | a1bb27b1 | pbrook | } |
201 | a1bb27b1 | pbrook | |
202 | a1bb27b1 | pbrook | static void sd_set_scr(SDState *sd) |
203 | a1bb27b1 | pbrook | { |
204 | a1bb27b1 | pbrook | sd->scr[0] = 0x00; /* SCR Structure */ |
205 | a1bb27b1 | pbrook | sd->scr[1] = 0x2f; /* SD Security Support */ |
206 | a1bb27b1 | pbrook | sd->scr[2] = 0x00; |
207 | a1bb27b1 | pbrook | sd->scr[3] = 0x00; |
208 | a1bb27b1 | pbrook | sd->scr[4] = 0x00; |
209 | a1bb27b1 | pbrook | sd->scr[5] = 0x00; |
210 | a1bb27b1 | pbrook | sd->scr[6] = 0x00; |
211 | a1bb27b1 | pbrook | sd->scr[7] = 0x00; |
212 | a1bb27b1 | pbrook | } |
213 | a1bb27b1 | pbrook | |
214 | a1bb27b1 | pbrook | #define MID 0xaa |
215 | a1bb27b1 | pbrook | #define OID "XY" |
216 | a1bb27b1 | pbrook | #define PNM "QEMU!" |
217 | a1bb27b1 | pbrook | #define PRV 0x01 |
218 | a1bb27b1 | pbrook | #define MDT_YR 2006 |
219 | a1bb27b1 | pbrook | #define MDT_MON 2 |
220 | a1bb27b1 | pbrook | |
221 | a1bb27b1 | pbrook | static void sd_set_cid(SDState *sd) |
222 | a1bb27b1 | pbrook | { |
223 | a1bb27b1 | pbrook | sd->cid[0] = MID; /* Fake card manufacturer ID (MID) */ |
224 | a1bb27b1 | pbrook | sd->cid[1] = OID[0]; /* OEM/Application ID (OID) */ |
225 | a1bb27b1 | pbrook | sd->cid[2] = OID[1]; |
226 | a1bb27b1 | pbrook | sd->cid[3] = PNM[0]; /* Fake product name (PNM) */ |
227 | a1bb27b1 | pbrook | sd->cid[4] = PNM[1]; |
228 | a1bb27b1 | pbrook | sd->cid[5] = PNM[2]; |
229 | a1bb27b1 | pbrook | sd->cid[6] = PNM[3]; |
230 | a1bb27b1 | pbrook | sd->cid[7] = PNM[4]; |
231 | a1bb27b1 | pbrook | sd->cid[8] = PRV; /* Fake product revision (PRV) */ |
232 | a1bb27b1 | pbrook | sd->cid[9] = 0xde; /* Fake serial number (PSN) */ |
233 | a1bb27b1 | pbrook | sd->cid[10] = 0xad; |
234 | a1bb27b1 | pbrook | sd->cid[11] = 0xbe; |
235 | a1bb27b1 | pbrook | sd->cid[12] = 0xef; |
236 | a1bb27b1 | pbrook | sd->cid[13] = 0x00 | /* Manufacture date (MDT) */ |
237 | a1bb27b1 | pbrook | ((MDT_YR - 2000) / 10); |
238 | a1bb27b1 | pbrook | sd->cid[14] = ((MDT_YR % 10) << 4) | MDT_MON; |
239 | a1bb27b1 | pbrook | sd->cid[15] = (sd_crc7(sd->cid, 15) << 1) | 1; |
240 | a1bb27b1 | pbrook | } |
241 | a1bb27b1 | pbrook | |
242 | a1bb27b1 | pbrook | #define HWBLOCK_SHIFT 9 /* 512 bytes */ |
243 | a1bb27b1 | pbrook | #define SECTOR_SHIFT 5 /* 16 kilobytes */ |
244 | a1bb27b1 | pbrook | #define WPGROUP_SHIFT 7 /* 2 megs */ |
245 | a1bb27b1 | pbrook | #define CMULT_SHIFT 9 /* 512 times HWBLOCK_SIZE */ |
246 | a1bb27b1 | pbrook | #define WPGROUP_SIZE (1 << (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT)) |
247 | a1bb27b1 | pbrook | |
248 | a1bb27b1 | pbrook | static const uint8_t sd_csd_rw_mask[16] = { |
249 | a1bb27b1 | pbrook | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
250 | a1bb27b1 | pbrook | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xfe, |
251 | a1bb27b1 | pbrook | }; |
252 | a1bb27b1 | pbrook | |
253 | 2d7adea4 | Andrzej Zaborowski | static void sd_set_csd(SDState *sd, uint64_t size) |
254 | a1bb27b1 | pbrook | { |
255 | a1bb27b1 | pbrook | uint32_t csize = (size >> (CMULT_SHIFT + HWBLOCK_SHIFT)) - 1;
|
256 | a1bb27b1 | pbrook | uint32_t sectsize = (1 << (SECTOR_SHIFT + 1)) - 1; |
257 | a1bb27b1 | pbrook | uint32_t wpsize = (1 << (WPGROUP_SHIFT + 1)) - 1; |
258 | a1bb27b1 | pbrook | |
259 | 2d7adea4 | Andrzej Zaborowski | if (size <= 0x40000000) { /* Standard Capacity SD */ |
260 | 2d7adea4 | Andrzej Zaborowski | sd->csd[0] = 0x00; /* CSD structure */ |
261 | 2d7adea4 | Andrzej Zaborowski | sd->csd[1] = 0x26; /* Data read access-time-1 */ |
262 | 2d7adea4 | Andrzej Zaborowski | sd->csd[2] = 0x00; /* Data read access-time-2 */ |
263 | 2d7adea4 | Andrzej Zaborowski | sd->csd[3] = 0x5a; /* Max. data transfer rate */ |
264 | 2d7adea4 | Andrzej Zaborowski | sd->csd[4] = 0x5f; /* Card Command Classes */ |
265 | 2d7adea4 | Andrzej Zaborowski | sd->csd[5] = 0x50 | /* Max. read data block length */ |
266 | 2d7adea4 | Andrzej Zaborowski | HWBLOCK_SHIFT; |
267 | 2d7adea4 | Andrzej Zaborowski | sd->csd[6] = 0xe0 | /* Partial block for read allowed */ |
268 | 2d7adea4 | Andrzej Zaborowski | ((csize >> 10) & 0x03); |
269 | 2d7adea4 | Andrzej Zaborowski | sd->csd[7] = 0x00 | /* Device size */ |
270 | 2d7adea4 | Andrzej Zaborowski | ((csize >> 2) & 0xff); |
271 | 2d7adea4 | Andrzej Zaborowski | sd->csd[8] = 0x3f | /* Max. read current */ |
272 | 2d7adea4 | Andrzej Zaborowski | ((csize << 6) & 0xc0); |
273 | 2d7adea4 | Andrzej Zaborowski | sd->csd[9] = 0xfc | /* Max. write current */ |
274 | 2d7adea4 | Andrzej Zaborowski | ((CMULT_SHIFT - 2) >> 1); |
275 | 2d7adea4 | Andrzej Zaborowski | sd->csd[10] = 0x40 | /* Erase sector size */ |
276 | 2d7adea4 | Andrzej Zaborowski | (((CMULT_SHIFT - 2) << 7) & 0x80) | (sectsize >> 1); |
277 | 2d7adea4 | Andrzej Zaborowski | sd->csd[11] = 0x00 | /* Write protect group size */ |
278 | 2d7adea4 | Andrzej Zaborowski | ((sectsize << 7) & 0x80) | wpsize; |
279 | 2d7adea4 | Andrzej Zaborowski | sd->csd[12] = 0x90 | /* Write speed factor */ |
280 | 2d7adea4 | Andrzej Zaborowski | (HWBLOCK_SHIFT >> 2);
|
281 | 2d7adea4 | Andrzej Zaborowski | sd->csd[13] = 0x20 | /* Max. write data block length */ |
282 | 2d7adea4 | Andrzej Zaborowski | ((HWBLOCK_SHIFT << 6) & 0xc0); |
283 | 2d7adea4 | Andrzej Zaborowski | sd->csd[14] = 0x00; /* File format group */ |
284 | 2d7adea4 | Andrzej Zaborowski | sd->csd[15] = (sd_crc7(sd->csd, 15) << 1) | 1; |
285 | 2d7adea4 | Andrzej Zaborowski | } else { /* SDHC */ |
286 | 2d7adea4 | Andrzej Zaborowski | size /= 512 * 1024; |
287 | 2d7adea4 | Andrzej Zaborowski | size -= 1;
|
288 | 2d7adea4 | Andrzej Zaborowski | sd->csd[0] = 0x40; |
289 | 2d7adea4 | Andrzej Zaborowski | sd->csd[1] = 0x0e; |
290 | 2d7adea4 | Andrzej Zaborowski | sd->csd[2] = 0x00; |
291 | 2d7adea4 | Andrzej Zaborowski | sd->csd[3] = 0x32; |
292 | 2d7adea4 | Andrzej Zaborowski | sd->csd[4] = 0x5b; |
293 | 2d7adea4 | Andrzej Zaborowski | sd->csd[5] = 0x59; |
294 | 2d7adea4 | Andrzej Zaborowski | sd->csd[6] = 0x00; |
295 | 2d7adea4 | Andrzej Zaborowski | sd->csd[7] = (size >> 16) & 0xff; |
296 | 2d7adea4 | Andrzej Zaborowski | sd->csd[8] = (size >> 8) & 0xff; |
297 | 2d7adea4 | Andrzej Zaborowski | sd->csd[9] = (size & 0xff); |
298 | 2d7adea4 | Andrzej Zaborowski | sd->csd[10] = 0x7f; |
299 | 2d7adea4 | Andrzej Zaborowski | sd->csd[11] = 0x80; |
300 | 2d7adea4 | Andrzej Zaborowski | sd->csd[12] = 0x0a; |
301 | 2d7adea4 | Andrzej Zaborowski | sd->csd[13] = 0x40; |
302 | 2d7adea4 | Andrzej Zaborowski | sd->csd[14] = 0x00; |
303 | 2d7adea4 | Andrzej Zaborowski | sd->csd[15] = 0x00; |
304 | 2d7adea4 | Andrzej Zaborowski | sd->ocr |= 1 << 30; /* High Capacity SD Memort Card */ |
305 | 2d7adea4 | Andrzej Zaborowski | } |
306 | a1bb27b1 | pbrook | } |
307 | a1bb27b1 | pbrook | |
308 | a1bb27b1 | pbrook | static void sd_set_rca(SDState *sd) |
309 | a1bb27b1 | pbrook | { |
310 | a1bb27b1 | pbrook | sd->rca += 0x4567;
|
311 | a1bb27b1 | pbrook | } |
312 | a1bb27b1 | pbrook | |
313 | a1bb27b1 | pbrook | #define CARD_STATUS_A 0x02004100 |
314 | a1bb27b1 | pbrook | #define CARD_STATUS_B 0x00c01e00 |
315 | a1bb27b1 | pbrook | #define CARD_STATUS_C 0xfd39a028 |
316 | a1bb27b1 | pbrook | |
317 | a1bb27b1 | pbrook | static void sd_set_cardstatus(SDState *sd) |
318 | a1bb27b1 | pbrook | { |
319 | a1bb27b1 | pbrook | sd->card_status = 0x00000100;
|
320 | a1bb27b1 | pbrook | } |
321 | a1bb27b1 | pbrook | |
322 | a1bb27b1 | pbrook | static void sd_set_sdstatus(SDState *sd) |
323 | a1bb27b1 | pbrook | { |
324 | a1bb27b1 | pbrook | memset(sd->sd_status, 0, 64); |
325 | a1bb27b1 | pbrook | } |
326 | a1bb27b1 | pbrook | |
327 | bc24a225 | Paul Brook | static int sd_req_crc_validate(SDRequest *req) |
328 | a1bb27b1 | pbrook | { |
329 | a1bb27b1 | pbrook | uint8_t buffer[5];
|
330 | a1bb27b1 | pbrook | buffer[0] = 0x40 | req->cmd; |
331 | a1bb27b1 | pbrook | buffer[1] = (req->arg >> 24) & 0xff; |
332 | a1bb27b1 | pbrook | buffer[2] = (req->arg >> 16) & 0xff; |
333 | a1bb27b1 | pbrook | buffer[3] = (req->arg >> 8) & 0xff; |
334 | a1bb27b1 | pbrook | buffer[4] = (req->arg >> 0) & 0xff; |
335 | a1bb27b1 | pbrook | return 0; |
336 | a1bb27b1 | pbrook | return sd_crc7(buffer, 5) != req->crc; /* TODO */ |
337 | a1bb27b1 | pbrook | } |
338 | a1bb27b1 | pbrook | |
339 | 9596ebb7 | pbrook | static void sd_response_r1_make(SDState *sd, |
340 | 9596ebb7 | pbrook | uint8_t *response, uint32_t last_status) |
341 | a1bb27b1 | pbrook | { |
342 | a1bb27b1 | pbrook | uint32_t mask = CARD_STATUS_B ^ ILLEGAL_COMMAND; |
343 | a1bb27b1 | pbrook | uint32_t status; |
344 | a1bb27b1 | pbrook | |
345 | a1bb27b1 | pbrook | status = (sd->card_status & ~mask) | (last_status & mask); |
346 | a1bb27b1 | pbrook | sd->card_status &= ~CARD_STATUS_C | APP_CMD; |
347 | a1bb27b1 | pbrook | |
348 | a1bb27b1 | pbrook | response[0] = (status >> 24) & 0xff; |
349 | a1bb27b1 | pbrook | response[1] = (status >> 16) & 0xff; |
350 | a1bb27b1 | pbrook | response[2] = (status >> 8) & 0xff; |
351 | a1bb27b1 | pbrook | response[3] = (status >> 0) & 0xff; |
352 | a1bb27b1 | pbrook | } |
353 | a1bb27b1 | pbrook | |
354 | 9596ebb7 | pbrook | static void sd_response_r3_make(SDState *sd, uint8_t *response) |
355 | a1bb27b1 | pbrook | { |
356 | a1bb27b1 | pbrook | response[0] = (sd->ocr >> 24) & 0xff; |
357 | a1bb27b1 | pbrook | response[1] = (sd->ocr >> 16) & 0xff; |
358 | a1bb27b1 | pbrook | response[2] = (sd->ocr >> 8) & 0xff; |
359 | a1bb27b1 | pbrook | response[3] = (sd->ocr >> 0) & 0xff; |
360 | a1bb27b1 | pbrook | } |
361 | a1bb27b1 | pbrook | |
362 | 9596ebb7 | pbrook | static void sd_response_r6_make(SDState *sd, uint8_t *response) |
363 | a1bb27b1 | pbrook | { |
364 | a1bb27b1 | pbrook | uint16_t arg; |
365 | a1bb27b1 | pbrook | uint16_t status; |
366 | a1bb27b1 | pbrook | |
367 | a1bb27b1 | pbrook | arg = sd->rca; |
368 | a1bb27b1 | pbrook | status = ((sd->card_status >> 8) & 0xc000) | |
369 | a1bb27b1 | pbrook | ((sd->card_status >> 6) & 0x2000) | |
370 | a1bb27b1 | pbrook | (sd->card_status & 0x1fff);
|
371 | a1bb27b1 | pbrook | |
372 | a1bb27b1 | pbrook | response[0] = (arg >> 8) & 0xff; |
373 | a1bb27b1 | pbrook | response[1] = arg & 0xff; |
374 | a1bb27b1 | pbrook | response[2] = (status >> 8) & 0xff; |
375 | a1bb27b1 | pbrook | response[3] = status & 0xff; |
376 | a1bb27b1 | pbrook | } |
377 | a1bb27b1 | pbrook | |
378 | 1b088995 | balrog | static void sd_response_r7_make(SDState *sd, uint8_t *response) |
379 | 1b088995 | balrog | { |
380 | 1b088995 | balrog | response[0] = (sd->vhs >> 24) & 0xff; |
381 | 1b088995 | balrog | response[1] = (sd->vhs >> 16) & 0xff; |
382 | 1b088995 | balrog | response[2] = (sd->vhs >> 8) & 0xff; |
383 | 1b088995 | balrog | response[3] = (sd->vhs >> 0) & 0xff; |
384 | 1b088995 | balrog | } |
385 | 1b088995 | balrog | |
386 | a1bb27b1 | pbrook | static void sd_reset(SDState *sd, BlockDriverState *bdrv) |
387 | a1bb27b1 | pbrook | { |
388 | 2d7adea4 | Andrzej Zaborowski | uint64_t size; |
389 | a1bb27b1 | pbrook | uint64_t sect; |
390 | a1bb27b1 | pbrook | |
391 | 03f311ed | Paul Brook | if (bdrv) {
|
392 | 03f311ed | Paul Brook | bdrv_get_geometry(bdrv, §); |
393 | 03f311ed | Paul Brook | } else {
|
394 | 03f311ed | Paul Brook | sect = 0;
|
395 | 03f311ed | Paul Brook | } |
396 | a1bb27b1 | pbrook | sect <<= 9;
|
397 | a1bb27b1 | pbrook | |
398 | 2d7adea4 | Andrzej Zaborowski | size = sect + 1;
|
399 | a1bb27b1 | pbrook | |
400 | a1bb27b1 | pbrook | sect = (size >> (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT)) + 1;
|
401 | a1bb27b1 | pbrook | |
402 | a1bb27b1 | pbrook | sd->state = sd_idle_state; |
403 | a1bb27b1 | pbrook | sd->rca = 0x0000;
|
404 | a1bb27b1 | pbrook | sd_set_ocr(sd); |
405 | a1bb27b1 | pbrook | sd_set_scr(sd); |
406 | a1bb27b1 | pbrook | sd_set_cid(sd); |
407 | a1bb27b1 | pbrook | sd_set_csd(sd, size); |
408 | a1bb27b1 | pbrook | sd_set_cardstatus(sd); |
409 | a1bb27b1 | pbrook | sd_set_sdstatus(sd); |
410 | a1bb27b1 | pbrook | |
411 | a1bb27b1 | pbrook | sd->bdrv = bdrv; |
412 | a1bb27b1 | pbrook | |
413 | 257514dd | pbrook | if (sd->wp_groups)
|
414 | 257514dd | pbrook | qemu_free(sd->wp_groups); |
415 | 03f311ed | Paul Brook | sd->wp_switch = bdrv ? bdrv_is_read_only(bdrv) : 0;
|
416 | a1bb27b1 | pbrook | sd->wp_groups = (int *) qemu_mallocz(sizeof(int) * sect); |
417 | a1bb27b1 | pbrook | memset(sd->function_group, 0, sizeof(int) * 6); |
418 | a1bb27b1 | pbrook | sd->erase_start = 0;
|
419 | a1bb27b1 | pbrook | sd->erase_end = 0;
|
420 | a1bb27b1 | pbrook | sd->size = size; |
421 | a1bb27b1 | pbrook | sd->blk_len = 0x200;
|
422 | a1bb27b1 | pbrook | sd->pwd_len = 0;
|
423 | a1bb27b1 | pbrook | } |
424 | a1bb27b1 | pbrook | |
425 | db97ee6a | Christoph Hellwig | static void sd_cardchange(void *opaque, int reason) |
426 | a1bb27b1 | pbrook | { |
427 | a1bb27b1 | pbrook | SDState *sd = opaque; |
428 | db97ee6a | Christoph Hellwig | |
429 | db97ee6a | Christoph Hellwig | if (!(reason & CHANGE_MEDIA)) {
|
430 | db97ee6a | Christoph Hellwig | return;
|
431 | db97ee6a | Christoph Hellwig | } |
432 | db97ee6a | Christoph Hellwig | |
433 | 02ce600c | balrog | qemu_set_irq(sd->inserted_cb, bdrv_is_inserted(sd->bdrv)); |
434 | a1bb27b1 | pbrook | if (bdrv_is_inserted(sd->bdrv)) {
|
435 | a1bb27b1 | pbrook | sd_reset(sd, sd->bdrv); |
436 | 257514dd | pbrook | qemu_set_irq(sd->readonly_cb, sd->wp_switch); |
437 | a1bb27b1 | pbrook | } |
438 | a1bb27b1 | pbrook | } |
439 | a1bb27b1 | pbrook | |
440 | 775616c3 | pbrook | /* We do not model the chip select pin, so allow the board to select
|
441 | 8ef6367e | balrog | whether card should be in SSI or MMC/SD mode. It is also up to the
|
442 | 775616c3 | pbrook | board to ensure that ssi transfers only occur when the chip select
|
443 | 775616c3 | pbrook | is asserted. */
|
444 | 775616c3 | pbrook | SDState *sd_init(BlockDriverState *bs, int is_spi)
|
445 | a1bb27b1 | pbrook | { |
446 | a1bb27b1 | pbrook | SDState *sd; |
447 | a1bb27b1 | pbrook | |
448 | a1bb27b1 | pbrook | sd = (SDState *) qemu_mallocz(sizeof(SDState));
|
449 | 72aef731 | Christoph Hellwig | sd->buf = qemu_blockalign(bs, 512);
|
450 | 775616c3 | pbrook | sd->spi = is_spi; |
451 | 4872aa13 | balrog | sd->enable = 1;
|
452 | a1bb27b1 | pbrook | sd_reset(sd, bs); |
453 | 03f311ed | Paul Brook | if (sd->bdrv) {
|
454 | 03f311ed | Paul Brook | bdrv_set_change_cb(sd->bdrv, sd_cardchange, sd); |
455 | 03f311ed | Paul Brook | } |
456 | a1bb27b1 | pbrook | return sd;
|
457 | a1bb27b1 | pbrook | } |
458 | a1bb27b1 | pbrook | |
459 | 02ce600c | balrog | void sd_set_cb(SDState *sd, qemu_irq readonly, qemu_irq insert)
|
460 | a1bb27b1 | pbrook | { |
461 | 02ce600c | balrog | sd->readonly_cb = readonly; |
462 | 02ce600c | balrog | sd->inserted_cb = insert; |
463 | 0d2e91c1 | Aurelien Jarno | qemu_set_irq(readonly, sd->bdrv ? bdrv_is_read_only(sd->bdrv) : 0);
|
464 | 0d2e91c1 | Aurelien Jarno | qemu_set_irq(insert, sd->bdrv ? bdrv_is_inserted(sd->bdrv) : 0);
|
465 | a1bb27b1 | pbrook | } |
466 | a1bb27b1 | pbrook | |
467 | a1bb27b1 | pbrook | static void sd_erase(SDState *sd) |
468 | a1bb27b1 | pbrook | { |
469 | a1bb27b1 | pbrook | int i, start, end;
|
470 | a1bb27b1 | pbrook | if (!sd->erase_start || !sd->erase_end) {
|
471 | a1bb27b1 | pbrook | sd->card_status |= ERASE_SEQ_ERROR; |
472 | a1bb27b1 | pbrook | return;
|
473 | a1bb27b1 | pbrook | } |
474 | a1bb27b1 | pbrook | |
475 | a1bb27b1 | pbrook | start = sd->erase_start >> |
476 | a1bb27b1 | pbrook | (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT); |
477 | a1bb27b1 | pbrook | end = sd->erase_end >> |
478 | a1bb27b1 | pbrook | (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT); |
479 | a1bb27b1 | pbrook | sd->erase_start = 0;
|
480 | a1bb27b1 | pbrook | sd->erase_end = 0;
|
481 | a1bb27b1 | pbrook | sd->csd[14] |= 0x40; |
482 | a1bb27b1 | pbrook | |
483 | a1bb27b1 | pbrook | for (i = start; i <= end; i ++)
|
484 | a1bb27b1 | pbrook | if (sd->wp_groups[i])
|
485 | a1bb27b1 | pbrook | sd->card_status |= WP_ERASE_SKIP; |
486 | a1bb27b1 | pbrook | } |
487 | a1bb27b1 | pbrook | |
488 | 2d7adea4 | Andrzej Zaborowski | static uint32_t sd_wpbits(SDState *sd, uint64_t addr)
|
489 | a1bb27b1 | pbrook | { |
490 | a1bb27b1 | pbrook | uint32_t i, wpnum; |
491 | a1bb27b1 | pbrook | uint32_t ret = 0;
|
492 | a1bb27b1 | pbrook | |
493 | a1bb27b1 | pbrook | wpnum = addr >> (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT); |
494 | a1bb27b1 | pbrook | |
495 | a1bb27b1 | pbrook | for (i = 0; i < 32; i ++, wpnum ++, addr += WPGROUP_SIZE) |
496 | a1bb27b1 | pbrook | if (addr < sd->size && sd->wp_groups[wpnum])
|
497 | a1bb27b1 | pbrook | ret |= (1 << i);
|
498 | a1bb27b1 | pbrook | |
499 | a1bb27b1 | pbrook | return ret;
|
500 | a1bb27b1 | pbrook | } |
501 | a1bb27b1 | pbrook | |
502 | a1bb27b1 | pbrook | static void sd_function_switch(SDState *sd, uint32_t arg) |
503 | a1bb27b1 | pbrook | { |
504 | a1bb27b1 | pbrook | int i, mode, new_func, crc;
|
505 | a1bb27b1 | pbrook | mode = !!(arg & 0x80000000);
|
506 | a1bb27b1 | pbrook | |
507 | a1bb27b1 | pbrook | sd->data[0] = 0x00; /* Maximum current consumption */ |
508 | a1bb27b1 | pbrook | sd->data[1] = 0x01; |
509 | a1bb27b1 | pbrook | sd->data[2] = 0x80; /* Supported group 6 functions */ |
510 | a1bb27b1 | pbrook | sd->data[3] = 0x01; |
511 | a1bb27b1 | pbrook | sd->data[4] = 0x80; /* Supported group 5 functions */ |
512 | a1bb27b1 | pbrook | sd->data[5] = 0x01; |
513 | a1bb27b1 | pbrook | sd->data[6] = 0x80; /* Supported group 4 functions */ |
514 | a1bb27b1 | pbrook | sd->data[7] = 0x01; |
515 | a1bb27b1 | pbrook | sd->data[8] = 0x80; /* Supported group 3 functions */ |
516 | a1bb27b1 | pbrook | sd->data[9] = 0x01; |
517 | a1bb27b1 | pbrook | sd->data[10] = 0x80; /* Supported group 2 functions */ |
518 | a1bb27b1 | pbrook | sd->data[11] = 0x43; |
519 | a1bb27b1 | pbrook | sd->data[12] = 0x80; /* Supported group 1 functions */ |
520 | a1bb27b1 | pbrook | sd->data[13] = 0x03; |
521 | a1bb27b1 | pbrook | for (i = 0; i < 6; i ++) { |
522 | a1bb27b1 | pbrook | new_func = (arg >> (i * 4)) & 0x0f; |
523 | a1bb27b1 | pbrook | if (mode && new_func != 0x0f) |
524 | a1bb27b1 | pbrook | sd->function_group[i] = new_func; |
525 | a1bb27b1 | pbrook | sd->data[14 + (i >> 1)] = new_func << ((i * 4) & 4); |
526 | a1bb27b1 | pbrook | } |
527 | a1bb27b1 | pbrook | memset(&sd->data[17], 0, 47); |
528 | a1bb27b1 | pbrook | crc = sd_crc16(sd->data, 64);
|
529 | a1bb27b1 | pbrook | sd->data[65] = crc >> 8; |
530 | a1bb27b1 | pbrook | sd->data[66] = crc & 0xff; |
531 | a1bb27b1 | pbrook | } |
532 | a1bb27b1 | pbrook | |
533 | a1bb27b1 | pbrook | static inline int sd_wp_addr(SDState *sd, uint32_t addr) |
534 | a1bb27b1 | pbrook | { |
535 | a1bb27b1 | pbrook | return sd->wp_groups[addr >>
|
536 | a1bb27b1 | pbrook | (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT)]; |
537 | a1bb27b1 | pbrook | } |
538 | a1bb27b1 | pbrook | |
539 | a1bb27b1 | pbrook | static void sd_lock_command(SDState *sd) |
540 | a1bb27b1 | pbrook | { |
541 | a1bb27b1 | pbrook | int erase, lock, clr_pwd, set_pwd, pwd_len;
|
542 | a1bb27b1 | pbrook | erase = !!(sd->data[0] & 0x08); |
543 | a1bb27b1 | pbrook | lock = sd->data[0] & 0x04; |
544 | a1bb27b1 | pbrook | clr_pwd = sd->data[0] & 0x02; |
545 | a1bb27b1 | pbrook | set_pwd = sd->data[0] & 0x01; |
546 | a1bb27b1 | pbrook | |
547 | a1bb27b1 | pbrook | if (sd->blk_len > 1) |
548 | a1bb27b1 | pbrook | pwd_len = sd->data[1];
|
549 | a1bb27b1 | pbrook | else
|
550 | a1bb27b1 | pbrook | pwd_len = 0;
|
551 | a1bb27b1 | pbrook | |
552 | a1bb27b1 | pbrook | if (erase) {
|
553 | a1bb27b1 | pbrook | if (!(sd->card_status & CARD_IS_LOCKED) || sd->blk_len > 1 || |
554 | a1bb27b1 | pbrook | set_pwd || clr_pwd || lock || sd->wp_switch || |
555 | a1bb27b1 | pbrook | (sd->csd[14] & 0x20)) { |
556 | a1bb27b1 | pbrook | sd->card_status |= LOCK_UNLOCK_FAILED; |
557 | a1bb27b1 | pbrook | return;
|
558 | a1bb27b1 | pbrook | } |
559 | a1bb27b1 | pbrook | memset(sd->wp_groups, 0, sizeof(int) * (sd->size >> |
560 | a1bb27b1 | pbrook | (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT))); |
561 | a1bb27b1 | pbrook | sd->csd[14] &= ~0x10; |
562 | a1bb27b1 | pbrook | sd->card_status &= ~CARD_IS_LOCKED; |
563 | a1bb27b1 | pbrook | sd->pwd_len = 0;
|
564 | a1bb27b1 | pbrook | /* Erasing the entire card here! */
|
565 | 827df9f3 | balrog | fprintf(stderr, "SD: Card force-erased by CMD42\n");
|
566 | a1bb27b1 | pbrook | return;
|
567 | a1bb27b1 | pbrook | } |
568 | a1bb27b1 | pbrook | |
569 | a1bb27b1 | pbrook | if (sd->blk_len < 2 + pwd_len || |
570 | a1bb27b1 | pbrook | pwd_len <= sd->pwd_len || |
571 | a1bb27b1 | pbrook | pwd_len > sd->pwd_len + 16) {
|
572 | a1bb27b1 | pbrook | sd->card_status |= LOCK_UNLOCK_FAILED; |
573 | a1bb27b1 | pbrook | return;
|
574 | a1bb27b1 | pbrook | } |
575 | a1bb27b1 | pbrook | |
576 | a1bb27b1 | pbrook | if (sd->pwd_len && memcmp(sd->pwd, sd->data + 2, sd->pwd_len)) { |
577 | a1bb27b1 | pbrook | sd->card_status |= LOCK_UNLOCK_FAILED; |
578 | a1bb27b1 | pbrook | return;
|
579 | a1bb27b1 | pbrook | } |
580 | a1bb27b1 | pbrook | |
581 | a1bb27b1 | pbrook | pwd_len -= sd->pwd_len; |
582 | a1bb27b1 | pbrook | if ((pwd_len && !set_pwd) ||
|
583 | a1bb27b1 | pbrook | (clr_pwd && (set_pwd || lock)) || |
584 | a1bb27b1 | pbrook | (lock && !sd->pwd_len && !set_pwd) || |
585 | a1bb27b1 | pbrook | (!set_pwd && !clr_pwd && |
586 | a1bb27b1 | pbrook | (((sd->card_status & CARD_IS_LOCKED) && lock) || |
587 | a1bb27b1 | pbrook | (!(sd->card_status & CARD_IS_LOCKED) && !lock)))) { |
588 | a1bb27b1 | pbrook | sd->card_status |= LOCK_UNLOCK_FAILED; |
589 | a1bb27b1 | pbrook | return;
|
590 | a1bb27b1 | pbrook | } |
591 | a1bb27b1 | pbrook | |
592 | a1bb27b1 | pbrook | if (set_pwd) {
|
593 | a1bb27b1 | pbrook | memcpy(sd->pwd, sd->data + 2 + sd->pwd_len, pwd_len);
|
594 | a1bb27b1 | pbrook | sd->pwd_len = pwd_len; |
595 | a1bb27b1 | pbrook | } |
596 | a1bb27b1 | pbrook | |
597 | a1bb27b1 | pbrook | if (clr_pwd) {
|
598 | a1bb27b1 | pbrook | sd->pwd_len = 0;
|
599 | a1bb27b1 | pbrook | } |
600 | a1bb27b1 | pbrook | |
601 | a1bb27b1 | pbrook | if (lock)
|
602 | a1bb27b1 | pbrook | sd->card_status |= CARD_IS_LOCKED; |
603 | a1bb27b1 | pbrook | else
|
604 | a1bb27b1 | pbrook | sd->card_status &= ~CARD_IS_LOCKED; |
605 | a1bb27b1 | pbrook | } |
606 | a1bb27b1 | pbrook | |
607 | c227f099 | Anthony Liguori | static sd_rsp_type_t sd_normal_command(SDState *sd,
|
608 | bc24a225 | Paul Brook | SDRequest req) |
609 | a1bb27b1 | pbrook | { |
610 | a1bb27b1 | pbrook | uint32_t rca = 0x0000;
|
611 | 2d7adea4 | Andrzej Zaborowski | uint64_t addr = (sd->ocr & (1 << 30)) ? (uint64_t) req.arg << 9 : req.arg; |
612 | a1bb27b1 | pbrook | |
613 | a1bb27b1 | pbrook | if (sd_cmd_type[req.cmd] == sd_ac || sd_cmd_type[req.cmd] == sd_adtc)
|
614 | a1bb27b1 | pbrook | rca = req.arg >> 16;
|
615 | a1bb27b1 | pbrook | |
616 | a1bb27b1 | pbrook | DPRINTF("CMD%d 0x%08x state %d\n", req.cmd, req.arg, sd->state);
|
617 | a1bb27b1 | pbrook | switch (req.cmd) {
|
618 | a1bb27b1 | pbrook | /* Basic commands (Class 0 and Class 1) */
|
619 | a1bb27b1 | pbrook | case 0: /* CMD0: GO_IDLE_STATE */ |
620 | a1bb27b1 | pbrook | switch (sd->state) {
|
621 | a1bb27b1 | pbrook | case sd_inactive_state:
|
622 | 775616c3 | pbrook | return sd->spi ? sd_r1 : sd_r0;
|
623 | a1bb27b1 | pbrook | |
624 | a1bb27b1 | pbrook | default:
|
625 | a1bb27b1 | pbrook | sd->state = sd_idle_state; |
626 | a1bb27b1 | pbrook | sd_reset(sd, sd->bdrv); |
627 | 775616c3 | pbrook | return sd->spi ? sd_r1 : sd_r0;
|
628 | a1bb27b1 | pbrook | } |
629 | a1bb27b1 | pbrook | break;
|
630 | a1bb27b1 | pbrook | |
631 | 775616c3 | pbrook | case 1: /* CMD1: SEND_OP_CMD */ |
632 | 775616c3 | pbrook | if (!sd->spi)
|
633 | 775616c3 | pbrook | goto bad_cmd;
|
634 | 775616c3 | pbrook | |
635 | 775616c3 | pbrook | sd->state = sd_transfer_state; |
636 | 775616c3 | pbrook | return sd_r1;
|
637 | 775616c3 | pbrook | |
638 | a1bb27b1 | pbrook | case 2: /* CMD2: ALL_SEND_CID */ |
639 | 775616c3 | pbrook | if (sd->spi)
|
640 | 775616c3 | pbrook | goto bad_cmd;
|
641 | a1bb27b1 | pbrook | switch (sd->state) {
|
642 | a1bb27b1 | pbrook | case sd_ready_state:
|
643 | a1bb27b1 | pbrook | sd->state = sd_identification_state; |
644 | a1bb27b1 | pbrook | return sd_r2_i;
|
645 | a1bb27b1 | pbrook | |
646 | a1bb27b1 | pbrook | default:
|
647 | a1bb27b1 | pbrook | break;
|
648 | a1bb27b1 | pbrook | } |
649 | a1bb27b1 | pbrook | break;
|
650 | a1bb27b1 | pbrook | |
651 | a1bb27b1 | pbrook | case 3: /* CMD3: SEND_RELATIVE_ADDR */ |
652 | 775616c3 | pbrook | if (sd->spi)
|
653 | 775616c3 | pbrook | goto bad_cmd;
|
654 | a1bb27b1 | pbrook | switch (sd->state) {
|
655 | a1bb27b1 | pbrook | case sd_identification_state:
|
656 | a1bb27b1 | pbrook | case sd_standby_state:
|
657 | a1bb27b1 | pbrook | sd->state = sd_standby_state; |
658 | a1bb27b1 | pbrook | sd_set_rca(sd); |
659 | a1bb27b1 | pbrook | return sd_r6;
|
660 | a1bb27b1 | pbrook | |
661 | a1bb27b1 | pbrook | default:
|
662 | a1bb27b1 | pbrook | break;
|
663 | a1bb27b1 | pbrook | } |
664 | a1bb27b1 | pbrook | break;
|
665 | a1bb27b1 | pbrook | |
666 | a1bb27b1 | pbrook | case 4: /* CMD4: SEND_DSR */ |
667 | 775616c3 | pbrook | if (sd->spi)
|
668 | 775616c3 | pbrook | goto bad_cmd;
|
669 | a1bb27b1 | pbrook | switch (sd->state) {
|
670 | a1bb27b1 | pbrook | case sd_standby_state:
|
671 | a1bb27b1 | pbrook | break;
|
672 | a1bb27b1 | pbrook | |
673 | a1bb27b1 | pbrook | default:
|
674 | a1bb27b1 | pbrook | break;
|
675 | a1bb27b1 | pbrook | } |
676 | a1bb27b1 | pbrook | break;
|
677 | a1bb27b1 | pbrook | |
678 | 109ac32f | Juha Riihimรคki | case 5: /* CMD5: reserved for SDIO cards */ |
679 | 109ac32f | Juha Riihimรคki | sd->card_status |= ILLEGAL_COMMAND; |
680 | 109ac32f | Juha Riihimรคki | return sd_r0;
|
681 | 109ac32f | Juha Riihimรคki | |
682 | a1bb27b1 | pbrook | case 6: /* CMD6: SWITCH_FUNCTION */ |
683 | 775616c3 | pbrook | if (sd->spi)
|
684 | 775616c3 | pbrook | goto bad_cmd;
|
685 | a1bb27b1 | pbrook | switch (sd->mode) {
|
686 | a1bb27b1 | pbrook | case sd_data_transfer_mode:
|
687 | a1bb27b1 | pbrook | sd_function_switch(sd, req.arg); |
688 | a1bb27b1 | pbrook | sd->state = sd_sendingdata_state; |
689 | a1bb27b1 | pbrook | sd->data_start = 0;
|
690 | a1bb27b1 | pbrook | sd->data_offset = 0;
|
691 | a1bb27b1 | pbrook | return sd_r1;
|
692 | a1bb27b1 | pbrook | |
693 | a1bb27b1 | pbrook | default:
|
694 | a1bb27b1 | pbrook | break;
|
695 | a1bb27b1 | pbrook | } |
696 | a1bb27b1 | pbrook | break;
|
697 | a1bb27b1 | pbrook | |
698 | a1bb27b1 | pbrook | case 7: /* CMD7: SELECT/DESELECT_CARD */ |
699 | 775616c3 | pbrook | if (sd->spi)
|
700 | 775616c3 | pbrook | goto bad_cmd;
|
701 | a1bb27b1 | pbrook | switch (sd->state) {
|
702 | a1bb27b1 | pbrook | case sd_standby_state:
|
703 | a1bb27b1 | pbrook | if (sd->rca != rca)
|
704 | a1bb27b1 | pbrook | return sd_r0;
|
705 | a1bb27b1 | pbrook | |
706 | a1bb27b1 | pbrook | sd->state = sd_transfer_state; |
707 | a1bb27b1 | pbrook | return sd_r1b;
|
708 | a1bb27b1 | pbrook | |
709 | a1bb27b1 | pbrook | case sd_transfer_state:
|
710 | a1bb27b1 | pbrook | case sd_sendingdata_state:
|
711 | a1bb27b1 | pbrook | if (sd->rca == rca)
|
712 | a1bb27b1 | pbrook | break;
|
713 | a1bb27b1 | pbrook | |
714 | a1bb27b1 | pbrook | sd->state = sd_standby_state; |
715 | a1bb27b1 | pbrook | return sd_r1b;
|
716 | a1bb27b1 | pbrook | |
717 | a1bb27b1 | pbrook | case sd_disconnect_state:
|
718 | a1bb27b1 | pbrook | if (sd->rca != rca)
|
719 | a1bb27b1 | pbrook | return sd_r0;
|
720 | a1bb27b1 | pbrook | |
721 | a1bb27b1 | pbrook | sd->state = sd_programming_state; |
722 | a1bb27b1 | pbrook | return sd_r1b;
|
723 | a1bb27b1 | pbrook | |
724 | a1bb27b1 | pbrook | case sd_programming_state:
|
725 | a1bb27b1 | pbrook | if (sd->rca == rca)
|
726 | a1bb27b1 | pbrook | break;
|
727 | a1bb27b1 | pbrook | |
728 | a1bb27b1 | pbrook | sd->state = sd_disconnect_state; |
729 | a1bb27b1 | pbrook | return sd_r1b;
|
730 | a1bb27b1 | pbrook | |
731 | a1bb27b1 | pbrook | default:
|
732 | a1bb27b1 | pbrook | break;
|
733 | a1bb27b1 | pbrook | } |
734 | a1bb27b1 | pbrook | break;
|
735 | a1bb27b1 | pbrook | |
736 | 1b088995 | balrog | case 8: /* CMD8: SEND_IF_COND */ |
737 | 1b088995 | balrog | /* Physical Layer Specification Version 2.00 command */
|
738 | 1b088995 | balrog | switch (sd->state) {
|
739 | 1b088995 | balrog | case sd_idle_state:
|
740 | 1b088995 | balrog | sd->vhs = 0;
|
741 | 1b088995 | balrog | |
742 | 1b088995 | balrog | /* No response if not exactly one VHS bit is set. */
|
743 | 1b088995 | balrog | if (!(req.arg >> 8) || (req.arg >> ffs(req.arg & ~0xff))) |
744 | 1b088995 | balrog | return sd->spi ? sd_r7 : sd_r0;
|
745 | 1b088995 | balrog | |
746 | 1b088995 | balrog | /* Accept. */
|
747 | 1b088995 | balrog | sd->vhs = req.arg; |
748 | 1b088995 | balrog | return sd_r7;
|
749 | 1b088995 | balrog | |
750 | 1b088995 | balrog | default:
|
751 | 1b088995 | balrog | break;
|
752 | 1b088995 | balrog | } |
753 | 1b088995 | balrog | break;
|
754 | 1b088995 | balrog | |
755 | a1bb27b1 | pbrook | case 9: /* CMD9: SEND_CSD */ |
756 | a1bb27b1 | pbrook | switch (sd->state) {
|
757 | a1bb27b1 | pbrook | case sd_standby_state:
|
758 | a1bb27b1 | pbrook | if (sd->rca != rca)
|
759 | a1bb27b1 | pbrook | return sd_r0;
|
760 | a1bb27b1 | pbrook | |
761 | a1bb27b1 | pbrook | return sd_r2_s;
|
762 | a1bb27b1 | pbrook | |
763 | 775616c3 | pbrook | case sd_transfer_state:
|
764 | 775616c3 | pbrook | if (!sd->spi)
|
765 | 775616c3 | pbrook | break;
|
766 | 775616c3 | pbrook | sd->state = sd_sendingdata_state; |
767 | 775616c3 | pbrook | memcpy(sd->data, sd->csd, 16);
|
768 | 2d7adea4 | Andrzej Zaborowski | sd->data_start = addr; |
769 | 775616c3 | pbrook | sd->data_offset = 0;
|
770 | 775616c3 | pbrook | return sd_r1;
|
771 | 775616c3 | pbrook | |
772 | a1bb27b1 | pbrook | default:
|
773 | a1bb27b1 | pbrook | break;
|
774 | a1bb27b1 | pbrook | } |
775 | a1bb27b1 | pbrook | break;
|
776 | a1bb27b1 | pbrook | |
777 | a1bb27b1 | pbrook | case 10: /* CMD10: SEND_CID */ |
778 | a1bb27b1 | pbrook | switch (sd->state) {
|
779 | a1bb27b1 | pbrook | case sd_standby_state:
|
780 | a1bb27b1 | pbrook | if (sd->rca != rca)
|
781 | a1bb27b1 | pbrook | return sd_r0;
|
782 | a1bb27b1 | pbrook | |
783 | a1bb27b1 | pbrook | return sd_r2_i;
|
784 | a1bb27b1 | pbrook | |
785 | 775616c3 | pbrook | case sd_transfer_state:
|
786 | 775616c3 | pbrook | if (!sd->spi)
|
787 | 775616c3 | pbrook | break;
|
788 | 775616c3 | pbrook | sd->state = sd_sendingdata_state; |
789 | 775616c3 | pbrook | memcpy(sd->data, sd->cid, 16);
|
790 | 2d7adea4 | Andrzej Zaborowski | sd->data_start = addr; |
791 | 775616c3 | pbrook | sd->data_offset = 0;
|
792 | 775616c3 | pbrook | return sd_r1;
|
793 | 775616c3 | pbrook | |
794 | a1bb27b1 | pbrook | default:
|
795 | a1bb27b1 | pbrook | break;
|
796 | a1bb27b1 | pbrook | } |
797 | a1bb27b1 | pbrook | break;
|
798 | a1bb27b1 | pbrook | |
799 | a1bb27b1 | pbrook | case 11: /* CMD11: READ_DAT_UNTIL_STOP */ |
800 | 775616c3 | pbrook | if (sd->spi)
|
801 | 775616c3 | pbrook | goto bad_cmd;
|
802 | a1bb27b1 | pbrook | switch (sd->state) {
|
803 | a1bb27b1 | pbrook | case sd_transfer_state:
|
804 | a1bb27b1 | pbrook | sd->state = sd_sendingdata_state; |
805 | a1bb27b1 | pbrook | sd->data_start = req.arg; |
806 | a1bb27b1 | pbrook | sd->data_offset = 0;
|
807 | a1bb27b1 | pbrook | |
808 | a1bb27b1 | pbrook | if (sd->data_start + sd->blk_len > sd->size)
|
809 | a1bb27b1 | pbrook | sd->card_status |= ADDRESS_ERROR; |
810 | a1bb27b1 | pbrook | return sd_r0;
|
811 | a1bb27b1 | pbrook | |
812 | a1bb27b1 | pbrook | default:
|
813 | a1bb27b1 | pbrook | break;
|
814 | a1bb27b1 | pbrook | } |
815 | a1bb27b1 | pbrook | break;
|
816 | a1bb27b1 | pbrook | |
817 | a1bb27b1 | pbrook | case 12: /* CMD12: STOP_TRANSMISSION */ |
818 | a1bb27b1 | pbrook | switch (sd->state) {
|
819 | a1bb27b1 | pbrook | case sd_sendingdata_state:
|
820 | a1bb27b1 | pbrook | sd->state = sd_transfer_state; |
821 | a1bb27b1 | pbrook | return sd_r1b;
|
822 | a1bb27b1 | pbrook | |
823 | a1bb27b1 | pbrook | case sd_receivingdata_state:
|
824 | a1bb27b1 | pbrook | sd->state = sd_programming_state; |
825 | a1bb27b1 | pbrook | /* Bzzzzzzztt .... Operation complete. */
|
826 | a1bb27b1 | pbrook | sd->state = sd_transfer_state; |
827 | a1bb27b1 | pbrook | return sd_r1b;
|
828 | a1bb27b1 | pbrook | |
829 | a1bb27b1 | pbrook | default:
|
830 | a1bb27b1 | pbrook | break;
|
831 | a1bb27b1 | pbrook | } |
832 | a1bb27b1 | pbrook | break;
|
833 | a1bb27b1 | pbrook | |
834 | a1bb27b1 | pbrook | case 13: /* CMD13: SEND_STATUS */ |
835 | a1bb27b1 | pbrook | switch (sd->mode) {
|
836 | a1bb27b1 | pbrook | case sd_data_transfer_mode:
|
837 | a1bb27b1 | pbrook | if (sd->rca != rca)
|
838 | a1bb27b1 | pbrook | return sd_r0;
|
839 | a1bb27b1 | pbrook | |
840 | a1bb27b1 | pbrook | return sd_r1;
|
841 | a1bb27b1 | pbrook | |
842 | a1bb27b1 | pbrook | default:
|
843 | a1bb27b1 | pbrook | break;
|
844 | a1bb27b1 | pbrook | } |
845 | a1bb27b1 | pbrook | break;
|
846 | a1bb27b1 | pbrook | |
847 | a1bb27b1 | pbrook | case 15: /* CMD15: GO_INACTIVE_STATE */ |
848 | 775616c3 | pbrook | if (sd->spi)
|
849 | 775616c3 | pbrook | goto bad_cmd;
|
850 | a1bb27b1 | pbrook | switch (sd->mode) {
|
851 | a1bb27b1 | pbrook | case sd_data_transfer_mode:
|
852 | a1bb27b1 | pbrook | if (sd->rca != rca)
|
853 | a1bb27b1 | pbrook | return sd_r0;
|
854 | a1bb27b1 | pbrook | |
855 | a1bb27b1 | pbrook | sd->state = sd_inactive_state; |
856 | a1bb27b1 | pbrook | return sd_r0;
|
857 | a1bb27b1 | pbrook | |
858 | a1bb27b1 | pbrook | default:
|
859 | a1bb27b1 | pbrook | break;
|
860 | a1bb27b1 | pbrook | } |
861 | a1bb27b1 | pbrook | break;
|
862 | a1bb27b1 | pbrook | |
863 | a1bb27b1 | pbrook | /* Block read commands (Classs 2) */
|
864 | a1bb27b1 | pbrook | case 16: /* CMD16: SET_BLOCKLEN */ |
865 | a1bb27b1 | pbrook | switch (sd->state) {
|
866 | a1bb27b1 | pbrook | case sd_transfer_state:
|
867 | a1bb27b1 | pbrook | if (req.arg > (1 << HWBLOCK_SHIFT)) |
868 | a1bb27b1 | pbrook | sd->card_status |= BLOCK_LEN_ERROR; |
869 | a1bb27b1 | pbrook | else
|
870 | a1bb27b1 | pbrook | sd->blk_len = req.arg; |
871 | a1bb27b1 | pbrook | |
872 | a1bb27b1 | pbrook | return sd_r1;
|
873 | a1bb27b1 | pbrook | |
874 | a1bb27b1 | pbrook | default:
|
875 | a1bb27b1 | pbrook | break;
|
876 | a1bb27b1 | pbrook | } |
877 | a1bb27b1 | pbrook | break;
|
878 | a1bb27b1 | pbrook | |
879 | a1bb27b1 | pbrook | case 17: /* CMD17: READ_SINGLE_BLOCK */ |
880 | a1bb27b1 | pbrook | switch (sd->state) {
|
881 | a1bb27b1 | pbrook | case sd_transfer_state:
|
882 | a1bb27b1 | pbrook | sd->state = sd_sendingdata_state; |
883 | 2d7adea4 | Andrzej Zaborowski | sd->data_start = addr; |
884 | a1bb27b1 | pbrook | sd->data_offset = 0;
|
885 | a1bb27b1 | pbrook | |
886 | a1bb27b1 | pbrook | if (sd->data_start + sd->blk_len > sd->size)
|
887 | a1bb27b1 | pbrook | sd->card_status |= ADDRESS_ERROR; |
888 | a1bb27b1 | pbrook | return sd_r1;
|
889 | a1bb27b1 | pbrook | |
890 | a1bb27b1 | pbrook | default:
|
891 | a1bb27b1 | pbrook | break;
|
892 | a1bb27b1 | pbrook | } |
893 | a1bb27b1 | pbrook | break;
|
894 | a1bb27b1 | pbrook | |
895 | a1bb27b1 | pbrook | case 18: /* CMD18: READ_MULTIPLE_BLOCK */ |
896 | a1bb27b1 | pbrook | switch (sd->state) {
|
897 | a1bb27b1 | pbrook | case sd_transfer_state:
|
898 | a1bb27b1 | pbrook | sd->state = sd_sendingdata_state; |
899 | 2d7adea4 | Andrzej Zaborowski | sd->data_start = addr; |
900 | a1bb27b1 | pbrook | sd->data_offset = 0;
|
901 | a1bb27b1 | pbrook | |
902 | a1bb27b1 | pbrook | if (sd->data_start + sd->blk_len > sd->size)
|
903 | a1bb27b1 | pbrook | sd->card_status |= ADDRESS_ERROR; |
904 | a1bb27b1 | pbrook | return sd_r1;
|
905 | a1bb27b1 | pbrook | |
906 | a1bb27b1 | pbrook | default:
|
907 | a1bb27b1 | pbrook | break;
|
908 | a1bb27b1 | pbrook | } |
909 | a1bb27b1 | pbrook | break;
|
910 | a1bb27b1 | pbrook | |
911 | a1bb27b1 | pbrook | /* Block write commands (Class 4) */
|
912 | a1bb27b1 | pbrook | case 24: /* CMD24: WRITE_SINGLE_BLOCK */ |
913 | 775616c3 | pbrook | if (sd->spi)
|
914 | 775616c3 | pbrook | goto unimplemented_cmd;
|
915 | a1bb27b1 | pbrook | switch (sd->state) {
|
916 | a1bb27b1 | pbrook | case sd_transfer_state:
|
917 | 775616c3 | pbrook | /* Writing in SPI mode not implemented. */
|
918 | 775616c3 | pbrook | if (sd->spi)
|
919 | 775616c3 | pbrook | break;
|
920 | a1bb27b1 | pbrook | sd->state = sd_receivingdata_state; |
921 | 2d7adea4 | Andrzej Zaborowski | sd->data_start = addr; |
922 | a1bb27b1 | pbrook | sd->data_offset = 0;
|
923 | a1bb27b1 | pbrook | sd->blk_written = 0;
|
924 | a1bb27b1 | pbrook | |
925 | a1bb27b1 | pbrook | if (sd->data_start + sd->blk_len > sd->size)
|
926 | a1bb27b1 | pbrook | sd->card_status |= ADDRESS_ERROR; |
927 | a1bb27b1 | pbrook | if (sd_wp_addr(sd, sd->data_start))
|
928 | a1bb27b1 | pbrook | sd->card_status |= WP_VIOLATION; |
929 | a1bb27b1 | pbrook | if (sd->csd[14] & 0x30) |
930 | a1bb27b1 | pbrook | sd->card_status |= WP_VIOLATION; |
931 | a1bb27b1 | pbrook | return sd_r1;
|
932 | a1bb27b1 | pbrook | |
933 | a1bb27b1 | pbrook | default:
|
934 | a1bb27b1 | pbrook | break;
|
935 | a1bb27b1 | pbrook | } |
936 | a1bb27b1 | pbrook | break;
|
937 | a1bb27b1 | pbrook | |
938 | a1bb27b1 | pbrook | case 25: /* CMD25: WRITE_MULTIPLE_BLOCK */ |
939 | 775616c3 | pbrook | if (sd->spi)
|
940 | 775616c3 | pbrook | goto unimplemented_cmd;
|
941 | a1bb27b1 | pbrook | switch (sd->state) {
|
942 | a1bb27b1 | pbrook | case sd_transfer_state:
|
943 | 775616c3 | pbrook | /* Writing in SPI mode not implemented. */
|
944 | 775616c3 | pbrook | if (sd->spi)
|
945 | 775616c3 | pbrook | break;
|
946 | a1bb27b1 | pbrook | sd->state = sd_receivingdata_state; |
947 | 2d7adea4 | Andrzej Zaborowski | sd->data_start = addr; |
948 | a1bb27b1 | pbrook | sd->data_offset = 0;
|
949 | a1bb27b1 | pbrook | sd->blk_written = 0;
|
950 | a1bb27b1 | pbrook | |
951 | a1bb27b1 | pbrook | if (sd->data_start + sd->blk_len > sd->size)
|
952 | a1bb27b1 | pbrook | sd->card_status |= ADDRESS_ERROR; |
953 | a1bb27b1 | pbrook | if (sd_wp_addr(sd, sd->data_start))
|
954 | a1bb27b1 | pbrook | sd->card_status |= WP_VIOLATION; |
955 | a1bb27b1 | pbrook | if (sd->csd[14] & 0x30) |
956 | a1bb27b1 | pbrook | sd->card_status |= WP_VIOLATION; |
957 | a1bb27b1 | pbrook | return sd_r1;
|
958 | a1bb27b1 | pbrook | |
959 | a1bb27b1 | pbrook | default:
|
960 | a1bb27b1 | pbrook | break;
|
961 | a1bb27b1 | pbrook | } |
962 | a1bb27b1 | pbrook | break;
|
963 | a1bb27b1 | pbrook | |
964 | a1bb27b1 | pbrook | case 26: /* CMD26: PROGRAM_CID */ |
965 | 775616c3 | pbrook | if (sd->spi)
|
966 | 775616c3 | pbrook | goto bad_cmd;
|
967 | a1bb27b1 | pbrook | switch (sd->state) {
|
968 | a1bb27b1 | pbrook | case sd_transfer_state:
|
969 | a1bb27b1 | pbrook | sd->state = sd_receivingdata_state; |
970 | a1bb27b1 | pbrook | sd->data_start = 0;
|
971 | a1bb27b1 | pbrook | sd->data_offset = 0;
|
972 | a1bb27b1 | pbrook | return sd_r1;
|
973 | a1bb27b1 | pbrook | |
974 | a1bb27b1 | pbrook | default:
|
975 | a1bb27b1 | pbrook | break;
|
976 | a1bb27b1 | pbrook | } |
977 | a1bb27b1 | pbrook | break;
|
978 | a1bb27b1 | pbrook | |
979 | a1bb27b1 | pbrook | case 27: /* CMD27: PROGRAM_CSD */ |
980 | 775616c3 | pbrook | if (sd->spi)
|
981 | 775616c3 | pbrook | goto unimplemented_cmd;
|
982 | a1bb27b1 | pbrook | switch (sd->state) {
|
983 | a1bb27b1 | pbrook | case sd_transfer_state:
|
984 | a1bb27b1 | pbrook | sd->state = sd_receivingdata_state; |
985 | a1bb27b1 | pbrook | sd->data_start = 0;
|
986 | a1bb27b1 | pbrook | sd->data_offset = 0;
|
987 | a1bb27b1 | pbrook | return sd_r1;
|
988 | a1bb27b1 | pbrook | |
989 | a1bb27b1 | pbrook | default:
|
990 | a1bb27b1 | pbrook | break;
|
991 | a1bb27b1 | pbrook | } |
992 | a1bb27b1 | pbrook | break;
|
993 | a1bb27b1 | pbrook | |
994 | a1bb27b1 | pbrook | /* Write protection (Class 6) */
|
995 | a1bb27b1 | pbrook | case 28: /* CMD28: SET_WRITE_PROT */ |
996 | a1bb27b1 | pbrook | switch (sd->state) {
|
997 | a1bb27b1 | pbrook | case sd_transfer_state:
|
998 | 2d7adea4 | Andrzej Zaborowski | if (addr >= sd->size) {
|
999 | a1bb27b1 | pbrook | sd->card_status = ADDRESS_ERROR; |
1000 | a1bb27b1 | pbrook | return sd_r1b;
|
1001 | a1bb27b1 | pbrook | } |
1002 | a1bb27b1 | pbrook | |
1003 | a1bb27b1 | pbrook | sd->state = sd_programming_state; |
1004 | 2d7adea4 | Andrzej Zaborowski | sd->wp_groups[addr >> (HWBLOCK_SHIFT + |
1005 | a1bb27b1 | pbrook | SECTOR_SHIFT + WPGROUP_SHIFT)] = 1;
|
1006 | a1bb27b1 | pbrook | /* Bzzzzzzztt .... Operation complete. */
|
1007 | a1bb27b1 | pbrook | sd->state = sd_transfer_state; |
1008 | a1bb27b1 | pbrook | return sd_r1b;
|
1009 | a1bb27b1 | pbrook | |
1010 | a1bb27b1 | pbrook | default:
|
1011 | a1bb27b1 | pbrook | break;
|
1012 | a1bb27b1 | pbrook | } |
1013 | a1bb27b1 | pbrook | break;
|
1014 | a1bb27b1 | pbrook | |
1015 | a1bb27b1 | pbrook | case 29: /* CMD29: CLR_WRITE_PROT */ |
1016 | a1bb27b1 | pbrook | switch (sd->state) {
|
1017 | a1bb27b1 | pbrook | case sd_transfer_state:
|
1018 | 2d7adea4 | Andrzej Zaborowski | if (addr >= sd->size) {
|
1019 | a1bb27b1 | pbrook | sd->card_status = ADDRESS_ERROR; |
1020 | a1bb27b1 | pbrook | return sd_r1b;
|
1021 | a1bb27b1 | pbrook | } |
1022 | a1bb27b1 | pbrook | |
1023 | a1bb27b1 | pbrook | sd->state = sd_programming_state; |
1024 | 2d7adea4 | Andrzej Zaborowski | sd->wp_groups[addr >> (HWBLOCK_SHIFT + |
1025 | a1bb27b1 | pbrook | SECTOR_SHIFT + WPGROUP_SHIFT)] = 0;
|
1026 | a1bb27b1 | pbrook | /* Bzzzzzzztt .... Operation complete. */
|
1027 | a1bb27b1 | pbrook | sd->state = sd_transfer_state; |
1028 | a1bb27b1 | pbrook | return sd_r1b;
|
1029 | a1bb27b1 | pbrook | |
1030 | a1bb27b1 | pbrook | default:
|
1031 | a1bb27b1 | pbrook | break;
|
1032 | a1bb27b1 | pbrook | } |
1033 | a1bb27b1 | pbrook | break;
|
1034 | a1bb27b1 | pbrook | |
1035 | a1bb27b1 | pbrook | case 30: /* CMD30: SEND_WRITE_PROT */ |
1036 | a1bb27b1 | pbrook | switch (sd->state) {
|
1037 | a1bb27b1 | pbrook | case sd_transfer_state:
|
1038 | a1bb27b1 | pbrook | sd->state = sd_sendingdata_state; |
1039 | a1bb27b1 | pbrook | *(uint32_t *) sd->data = sd_wpbits(sd, req.arg); |
1040 | 2d7adea4 | Andrzej Zaborowski | sd->data_start = addr; |
1041 | a1bb27b1 | pbrook | sd->data_offset = 0;
|
1042 | a1bb27b1 | pbrook | return sd_r1b;
|
1043 | a1bb27b1 | pbrook | |
1044 | a1bb27b1 | pbrook | default:
|
1045 | a1bb27b1 | pbrook | break;
|
1046 | a1bb27b1 | pbrook | } |
1047 | a1bb27b1 | pbrook | break;
|
1048 | a1bb27b1 | pbrook | |
1049 | a1bb27b1 | pbrook | /* Erase commands (Class 5) */
|
1050 | a1bb27b1 | pbrook | case 32: /* CMD32: ERASE_WR_BLK_START */ |
1051 | a1bb27b1 | pbrook | switch (sd->state) {
|
1052 | a1bb27b1 | pbrook | case sd_transfer_state:
|
1053 | a1bb27b1 | pbrook | sd->erase_start = req.arg; |
1054 | a1bb27b1 | pbrook | return sd_r1;
|
1055 | a1bb27b1 | pbrook | |
1056 | a1bb27b1 | pbrook | default:
|
1057 | a1bb27b1 | pbrook | break;
|
1058 | a1bb27b1 | pbrook | } |
1059 | a1bb27b1 | pbrook | break;
|
1060 | a1bb27b1 | pbrook | |
1061 | a1bb27b1 | pbrook | case 33: /* CMD33: ERASE_WR_BLK_END */ |
1062 | a1bb27b1 | pbrook | switch (sd->state) {
|
1063 | a1bb27b1 | pbrook | case sd_transfer_state:
|
1064 | a1bb27b1 | pbrook | sd->erase_end = req.arg; |
1065 | a1bb27b1 | pbrook | return sd_r1;
|
1066 | a1bb27b1 | pbrook | |
1067 | a1bb27b1 | pbrook | default:
|
1068 | a1bb27b1 | pbrook | break;
|
1069 | a1bb27b1 | pbrook | } |
1070 | a1bb27b1 | pbrook | break;
|
1071 | a1bb27b1 | pbrook | |
1072 | a1bb27b1 | pbrook | case 38: /* CMD38: ERASE */ |
1073 | a1bb27b1 | pbrook | switch (sd->state) {
|
1074 | a1bb27b1 | pbrook | case sd_transfer_state:
|
1075 | a1bb27b1 | pbrook | if (sd->csd[14] & 0x30) { |
1076 | a1bb27b1 | pbrook | sd->card_status |= WP_VIOLATION; |
1077 | a1bb27b1 | pbrook | return sd_r1b;
|
1078 | a1bb27b1 | pbrook | } |
1079 | a1bb27b1 | pbrook | |
1080 | a1bb27b1 | pbrook | sd->state = sd_programming_state; |
1081 | a1bb27b1 | pbrook | sd_erase(sd); |
1082 | a1bb27b1 | pbrook | /* Bzzzzzzztt .... Operation complete. */
|
1083 | a1bb27b1 | pbrook | sd->state = sd_transfer_state; |
1084 | a1bb27b1 | pbrook | return sd_r1b;
|
1085 | a1bb27b1 | pbrook | |
1086 | a1bb27b1 | pbrook | default:
|
1087 | a1bb27b1 | pbrook | break;
|
1088 | a1bb27b1 | pbrook | } |
1089 | a1bb27b1 | pbrook | break;
|
1090 | a1bb27b1 | pbrook | |
1091 | a1bb27b1 | pbrook | /* Lock card commands (Class 7) */
|
1092 | a1bb27b1 | pbrook | case 42: /* CMD42: LOCK_UNLOCK */ |
1093 | 775616c3 | pbrook | if (sd->spi)
|
1094 | 775616c3 | pbrook | goto unimplemented_cmd;
|
1095 | a1bb27b1 | pbrook | switch (sd->state) {
|
1096 | a1bb27b1 | pbrook | case sd_transfer_state:
|
1097 | a1bb27b1 | pbrook | sd->state = sd_receivingdata_state; |
1098 | a1bb27b1 | pbrook | sd->data_start = 0;
|
1099 | a1bb27b1 | pbrook | sd->data_offset = 0;
|
1100 | a1bb27b1 | pbrook | return sd_r1;
|
1101 | a1bb27b1 | pbrook | |
1102 | a1bb27b1 | pbrook | default:
|
1103 | a1bb27b1 | pbrook | break;
|
1104 | a1bb27b1 | pbrook | } |
1105 | a1bb27b1 | pbrook | break;
|
1106 | a1bb27b1 | pbrook | |
1107 | a1bb27b1 | pbrook | /* Application specific commands (Class 8) */
|
1108 | a1bb27b1 | pbrook | case 55: /* CMD55: APP_CMD */ |
1109 | a1bb27b1 | pbrook | if (sd->rca != rca)
|
1110 | a1bb27b1 | pbrook | return sd_r0;
|
1111 | a1bb27b1 | pbrook | |
1112 | a1bb27b1 | pbrook | sd->card_status |= APP_CMD; |
1113 | a1bb27b1 | pbrook | return sd_r1;
|
1114 | a1bb27b1 | pbrook | |
1115 | a1bb27b1 | pbrook | case 56: /* CMD56: GEN_CMD */ |
1116 | 827df9f3 | balrog | fprintf(stderr, "SD: GEN_CMD 0x%08x\n", req.arg);
|
1117 | a1bb27b1 | pbrook | |
1118 | a1bb27b1 | pbrook | switch (sd->state) {
|
1119 | a1bb27b1 | pbrook | case sd_transfer_state:
|
1120 | a1bb27b1 | pbrook | sd->data_offset = 0;
|
1121 | a1bb27b1 | pbrook | if (req.arg & 1) |
1122 | a1bb27b1 | pbrook | sd->state = sd_sendingdata_state; |
1123 | a1bb27b1 | pbrook | else
|
1124 | a1bb27b1 | pbrook | sd->state = sd_receivingdata_state; |
1125 | a1bb27b1 | pbrook | return sd_r1;
|
1126 | a1bb27b1 | pbrook | |
1127 | a1bb27b1 | pbrook | default:
|
1128 | a1bb27b1 | pbrook | break;
|
1129 | a1bb27b1 | pbrook | } |
1130 | a1bb27b1 | pbrook | break;
|
1131 | a1bb27b1 | pbrook | |
1132 | a1bb27b1 | pbrook | default:
|
1133 | 775616c3 | pbrook | bad_cmd:
|
1134 | a1bb27b1 | pbrook | sd->card_status |= ILLEGAL_COMMAND; |
1135 | a1bb27b1 | pbrook | |
1136 | 827df9f3 | balrog | fprintf(stderr, "SD: Unknown CMD%i\n", req.cmd);
|
1137 | a1bb27b1 | pbrook | return sd_r0;
|
1138 | 775616c3 | pbrook | |
1139 | 775616c3 | pbrook | unimplemented_cmd:
|
1140 | 775616c3 | pbrook | /* Commands that are recognised but not yet implemented in SPI mode. */
|
1141 | 775616c3 | pbrook | sd->card_status |= ILLEGAL_COMMAND; |
1142 | 827df9f3 | balrog | fprintf(stderr, "SD: CMD%i not implemented in SPI mode\n", req.cmd);
|
1143 | 775616c3 | pbrook | return sd_r0;
|
1144 | a1bb27b1 | pbrook | } |
1145 | a1bb27b1 | pbrook | |
1146 | a1bb27b1 | pbrook | sd->card_status |= ILLEGAL_COMMAND; |
1147 | 827df9f3 | balrog | fprintf(stderr, "SD: CMD%i in a wrong state\n", req.cmd);
|
1148 | a1bb27b1 | pbrook | return sd_r0;
|
1149 | a1bb27b1 | pbrook | } |
1150 | a1bb27b1 | pbrook | |
1151 | c227f099 | Anthony Liguori | static sd_rsp_type_t sd_app_command(SDState *sd,
|
1152 | 22ed1d34 | Blue Swirl | SDRequest req) |
1153 | 22ed1d34 | Blue Swirl | { |
1154 | a1bb27b1 | pbrook | DPRINTF("ACMD%d 0x%08x\n", req.cmd, req.arg);
|
1155 | a1bb27b1 | pbrook | switch (req.cmd) {
|
1156 | a1bb27b1 | pbrook | case 6: /* ACMD6: SET_BUS_WIDTH */ |
1157 | a1bb27b1 | pbrook | switch (sd->state) {
|
1158 | a1bb27b1 | pbrook | case sd_transfer_state:
|
1159 | a1bb27b1 | pbrook | sd->sd_status[0] &= 0x3f; |
1160 | a1bb27b1 | pbrook | sd->sd_status[0] |= (req.arg & 0x03) << 6; |
1161 | a1bb27b1 | pbrook | return sd_r1;
|
1162 | a1bb27b1 | pbrook | |
1163 | a1bb27b1 | pbrook | default:
|
1164 | a1bb27b1 | pbrook | break;
|
1165 | a1bb27b1 | pbrook | } |
1166 | a1bb27b1 | pbrook | break;
|
1167 | a1bb27b1 | pbrook | |
1168 | a1bb27b1 | pbrook | case 13: /* ACMD13: SD_STATUS */ |
1169 | a1bb27b1 | pbrook | switch (sd->state) {
|
1170 | a1bb27b1 | pbrook | case sd_transfer_state:
|
1171 | fb1ba03a | Peter Maydell | sd->state = sd_sendingdata_state; |
1172 | a1bb27b1 | pbrook | sd->data_start = 0;
|
1173 | a1bb27b1 | pbrook | sd->data_offset = 0;
|
1174 | a1bb27b1 | pbrook | return sd_r1;
|
1175 | a1bb27b1 | pbrook | |
1176 | a1bb27b1 | pbrook | default:
|
1177 | a1bb27b1 | pbrook | break;
|
1178 | a1bb27b1 | pbrook | } |
1179 | a1bb27b1 | pbrook | break;
|
1180 | a1bb27b1 | pbrook | |
1181 | a1bb27b1 | pbrook | case 22: /* ACMD22: SEND_NUM_WR_BLOCKS */ |
1182 | a1bb27b1 | pbrook | switch (sd->state) {
|
1183 | a1bb27b1 | pbrook | case sd_transfer_state:
|
1184 | a1bb27b1 | pbrook | *(uint32_t *) sd->data = sd->blk_written; |
1185 | a1bb27b1 | pbrook | |
1186 | fb1ba03a | Peter Maydell | sd->state = sd_sendingdata_state; |
1187 | a1bb27b1 | pbrook | sd->data_start = 0;
|
1188 | a1bb27b1 | pbrook | sd->data_offset = 0;
|
1189 | a1bb27b1 | pbrook | return sd_r1;
|
1190 | a1bb27b1 | pbrook | |
1191 | a1bb27b1 | pbrook | default:
|
1192 | a1bb27b1 | pbrook | break;
|
1193 | a1bb27b1 | pbrook | } |
1194 | a1bb27b1 | pbrook | break;
|
1195 | a1bb27b1 | pbrook | |
1196 | a1bb27b1 | pbrook | case 23: /* ACMD23: SET_WR_BLK_ERASE_COUNT */ |
1197 | a1bb27b1 | pbrook | switch (sd->state) {
|
1198 | a1bb27b1 | pbrook | case sd_transfer_state:
|
1199 | a1bb27b1 | pbrook | return sd_r1;
|
1200 | a1bb27b1 | pbrook | |
1201 | a1bb27b1 | pbrook | default:
|
1202 | a1bb27b1 | pbrook | break;
|
1203 | a1bb27b1 | pbrook | } |
1204 | a1bb27b1 | pbrook | break;
|
1205 | a1bb27b1 | pbrook | |
1206 | a1bb27b1 | pbrook | case 41: /* ACMD41: SD_APP_OP_COND */ |
1207 | 775616c3 | pbrook | if (sd->spi) {
|
1208 | 775616c3 | pbrook | /* SEND_OP_CMD */
|
1209 | 775616c3 | pbrook | sd->state = sd_transfer_state; |
1210 | 775616c3 | pbrook | return sd_r1;
|
1211 | 775616c3 | pbrook | } |
1212 | a1bb27b1 | pbrook | switch (sd->state) {
|
1213 | a1bb27b1 | pbrook | case sd_idle_state:
|
1214 | a1bb27b1 | pbrook | /* We accept any voltage. 10000 V is nothing. */
|
1215 | a1bb27b1 | pbrook | if (req.arg)
|
1216 | a1bb27b1 | pbrook | sd->state = sd_ready_state; |
1217 | a1bb27b1 | pbrook | |
1218 | a1bb27b1 | pbrook | return sd_r3;
|
1219 | a1bb27b1 | pbrook | |
1220 | a1bb27b1 | pbrook | default:
|
1221 | a1bb27b1 | pbrook | break;
|
1222 | a1bb27b1 | pbrook | } |
1223 | a1bb27b1 | pbrook | break;
|
1224 | a1bb27b1 | pbrook | |
1225 | a1bb27b1 | pbrook | case 42: /* ACMD42: SET_CLR_CARD_DETECT */ |
1226 | a1bb27b1 | pbrook | switch (sd->state) {
|
1227 | a1bb27b1 | pbrook | case sd_transfer_state:
|
1228 | a1bb27b1 | pbrook | /* Bringing in the 50KOhm pull-up resistor... Done. */
|
1229 | a1bb27b1 | pbrook | return sd_r1;
|
1230 | a1bb27b1 | pbrook | |
1231 | a1bb27b1 | pbrook | default:
|
1232 | a1bb27b1 | pbrook | break;
|
1233 | a1bb27b1 | pbrook | } |
1234 | a1bb27b1 | pbrook | break;
|
1235 | a1bb27b1 | pbrook | |
1236 | a1bb27b1 | pbrook | case 51: /* ACMD51: SEND_SCR */ |
1237 | a1bb27b1 | pbrook | switch (sd->state) {
|
1238 | a1bb27b1 | pbrook | case sd_transfer_state:
|
1239 | a1bb27b1 | pbrook | sd->state = sd_sendingdata_state; |
1240 | a1bb27b1 | pbrook | sd->data_start = 0;
|
1241 | a1bb27b1 | pbrook | sd->data_offset = 0;
|
1242 | a1bb27b1 | pbrook | return sd_r1;
|
1243 | a1bb27b1 | pbrook | |
1244 | a1bb27b1 | pbrook | default:
|
1245 | a1bb27b1 | pbrook | break;
|
1246 | a1bb27b1 | pbrook | } |
1247 | a1bb27b1 | pbrook | break;
|
1248 | a1bb27b1 | pbrook | |
1249 | a1bb27b1 | pbrook | default:
|
1250 | a1bb27b1 | pbrook | /* Fall back to standard commands. */
|
1251 | a1bb27b1 | pbrook | sd->card_status &= ~APP_CMD; |
1252 | a1bb27b1 | pbrook | return sd_normal_command(sd, req);
|
1253 | a1bb27b1 | pbrook | } |
1254 | a1bb27b1 | pbrook | |
1255 | 827df9f3 | balrog | fprintf(stderr, "SD: ACMD%i in a wrong state\n", req.cmd);
|
1256 | a1bb27b1 | pbrook | return sd_r0;
|
1257 | a1bb27b1 | pbrook | } |
1258 | a1bb27b1 | pbrook | |
1259 | bc24a225 | Paul Brook | int sd_do_command(SDState *sd, SDRequest *req,
|
1260 | a1bb27b1 | pbrook | uint8_t *response) { |
1261 | a1bb27b1 | pbrook | uint32_t last_status = sd->card_status; |
1262 | c227f099 | Anthony Liguori | sd_rsp_type_t rtype; |
1263 | a1bb27b1 | pbrook | int rsplen;
|
1264 | a1bb27b1 | pbrook | |
1265 | 03f311ed | Paul Brook | if (!sd->bdrv || !bdrv_is_inserted(sd->bdrv) || !sd->enable) {
|
1266 | a1bb27b1 | pbrook | return 0; |
1267 | a1bb27b1 | pbrook | } |
1268 | a1bb27b1 | pbrook | |
1269 | a1bb27b1 | pbrook | if (sd_req_crc_validate(req)) {
|
1270 | a1bb27b1 | pbrook | sd->card_status &= ~COM_CRC_ERROR; |
1271 | a1bb27b1 | pbrook | return 0; |
1272 | a1bb27b1 | pbrook | } |
1273 | a1bb27b1 | pbrook | |
1274 | a1bb27b1 | pbrook | sd->card_status &= ~CARD_STATUS_B; |
1275 | a1bb27b1 | pbrook | sd_set_status(sd); |
1276 | a1bb27b1 | pbrook | |
1277 | a1bb27b1 | pbrook | if (last_status & CARD_IS_LOCKED)
|
1278 | a1bb27b1 | pbrook | if (((last_status & APP_CMD) &&
|
1279 | a1bb27b1 | pbrook | req->cmd == 41) ||
|
1280 | a1bb27b1 | pbrook | (!(last_status & APP_CMD) && |
1281 | a1bb27b1 | pbrook | (sd_cmd_class[req->cmd] == 0 ||
|
1282 | a1bb27b1 | pbrook | sd_cmd_class[req->cmd] == 7 ||
|
1283 | a1bb27b1 | pbrook | req->cmd == 16 || req->cmd == 55))) { |
1284 | a1bb27b1 | pbrook | sd->card_status |= ILLEGAL_COMMAND; |
1285 | 827df9f3 | balrog | fprintf(stderr, "SD: Card is locked\n");
|
1286 | a1bb27b1 | pbrook | return 0; |
1287 | a1bb27b1 | pbrook | } |
1288 | a1bb27b1 | pbrook | |
1289 | 724d3a8f | balrog | if (last_status & APP_CMD) {
|
1290 | a1bb27b1 | pbrook | rtype = sd_app_command(sd, *req); |
1291 | 724d3a8f | balrog | sd->card_status &= ~APP_CMD; |
1292 | 724d3a8f | balrog | } else
|
1293 | a1bb27b1 | pbrook | rtype = sd_normal_command(sd, *req); |
1294 | a1bb27b1 | pbrook | |
1295 | a1bb27b1 | pbrook | sd->current_cmd = req->cmd; |
1296 | a1bb27b1 | pbrook | |
1297 | a1bb27b1 | pbrook | switch (rtype) {
|
1298 | a1bb27b1 | pbrook | case sd_r1:
|
1299 | a1bb27b1 | pbrook | case sd_r1b:
|
1300 | a1bb27b1 | pbrook | sd_response_r1_make(sd, response, last_status); |
1301 | a1bb27b1 | pbrook | rsplen = 4;
|
1302 | a1bb27b1 | pbrook | break;
|
1303 | a1bb27b1 | pbrook | |
1304 | a1bb27b1 | pbrook | case sd_r2_i:
|
1305 | a1bb27b1 | pbrook | memcpy(response, sd->cid, sizeof(sd->cid));
|
1306 | a1bb27b1 | pbrook | rsplen = 16;
|
1307 | a1bb27b1 | pbrook | break;
|
1308 | a1bb27b1 | pbrook | |
1309 | a1bb27b1 | pbrook | case sd_r2_s:
|
1310 | a1bb27b1 | pbrook | memcpy(response, sd->csd, sizeof(sd->csd));
|
1311 | a1bb27b1 | pbrook | rsplen = 16;
|
1312 | a1bb27b1 | pbrook | break;
|
1313 | a1bb27b1 | pbrook | |
1314 | a1bb27b1 | pbrook | case sd_r3:
|
1315 | a1bb27b1 | pbrook | sd_response_r3_make(sd, response); |
1316 | a1bb27b1 | pbrook | rsplen = 4;
|
1317 | a1bb27b1 | pbrook | break;
|
1318 | a1bb27b1 | pbrook | |
1319 | a1bb27b1 | pbrook | case sd_r6:
|
1320 | a1bb27b1 | pbrook | sd_response_r6_make(sd, response); |
1321 | a1bb27b1 | pbrook | rsplen = 4;
|
1322 | a1bb27b1 | pbrook | break;
|
1323 | a1bb27b1 | pbrook | |
1324 | 1b088995 | balrog | case sd_r7:
|
1325 | 1b088995 | balrog | sd_response_r7_make(sd, response); |
1326 | 1b088995 | balrog | rsplen = 4;
|
1327 | 1b088995 | balrog | break;
|
1328 | 1b088995 | balrog | |
1329 | a1bb27b1 | pbrook | case sd_r0:
|
1330 | a1bb27b1 | pbrook | default:
|
1331 | a1bb27b1 | pbrook | rsplen = 0;
|
1332 | a1bb27b1 | pbrook | break;
|
1333 | a1bb27b1 | pbrook | } |
1334 | a1bb27b1 | pbrook | |
1335 | a1bb27b1 | pbrook | if (sd->card_status & ILLEGAL_COMMAND)
|
1336 | a1bb27b1 | pbrook | rsplen = 0;
|
1337 | a1bb27b1 | pbrook | |
1338 | a1bb27b1 | pbrook | #ifdef DEBUG_SD
|
1339 | a1bb27b1 | pbrook | if (rsplen) {
|
1340 | a1bb27b1 | pbrook | int i;
|
1341 | a1bb27b1 | pbrook | DPRINTF("Response:");
|
1342 | a1bb27b1 | pbrook | for (i = 0; i < rsplen; i++) |
1343 | a1bb27b1 | pbrook | printf(" %02x", response[i]);
|
1344 | a1bb27b1 | pbrook | printf(" state %d\n", sd->state);
|
1345 | a1bb27b1 | pbrook | } else {
|
1346 | a1bb27b1 | pbrook | DPRINTF("No response %d\n", sd->state);
|
1347 | a1bb27b1 | pbrook | } |
1348 | a1bb27b1 | pbrook | #endif
|
1349 | a1bb27b1 | pbrook | |
1350 | a1bb27b1 | pbrook | return rsplen;
|
1351 | a1bb27b1 | pbrook | } |
1352 | a1bb27b1 | pbrook | |
1353 | 2d7adea4 | Andrzej Zaborowski | static void sd_blk_read(SDState *sd, uint64_t addr, uint32_t len) |
1354 | a1bb27b1 | pbrook | { |
1355 | 2d7adea4 | Andrzej Zaborowski | uint64_t end = addr + len; |
1356 | a1bb27b1 | pbrook | |
1357 | 2d7adea4 | Andrzej Zaborowski | DPRINTF("sd_blk_read: addr = 0x%08llx, len = %d\n",
|
1358 | 2d7adea4 | Andrzej Zaborowski | (unsigned long long) addr, len); |
1359 | 33f00271 | balrog | if (!sd->bdrv || bdrv_read(sd->bdrv, addr >> 9, sd->buf, 1) == -1) { |
1360 | 827df9f3 | balrog | fprintf(stderr, "sd_blk_read: read error on host side\n");
|
1361 | a1bb27b1 | pbrook | return;
|
1362 | a1bb27b1 | pbrook | } |
1363 | a1bb27b1 | pbrook | |
1364 | a1bb27b1 | pbrook | if (end > (addr & ~511) + 512) { |
1365 | 33f00271 | balrog | memcpy(sd->data, sd->buf + (addr & 511), 512 - (addr & 511)); |
1366 | a1bb27b1 | pbrook | |
1367 | 33f00271 | balrog | if (bdrv_read(sd->bdrv, end >> 9, sd->buf, 1) == -1) { |
1368 | 827df9f3 | balrog | fprintf(stderr, "sd_blk_read: read error on host side\n");
|
1369 | a1bb27b1 | pbrook | return;
|
1370 | a1bb27b1 | pbrook | } |
1371 | 33f00271 | balrog | memcpy(sd->data + 512 - (addr & 511), sd->buf, end & 511); |
1372 | a1bb27b1 | pbrook | } else
|
1373 | 33f00271 | balrog | memcpy(sd->data, sd->buf + (addr & 511), len);
|
1374 | a1bb27b1 | pbrook | } |
1375 | a1bb27b1 | pbrook | |
1376 | 2d7adea4 | Andrzej Zaborowski | static void sd_blk_write(SDState *sd, uint64_t addr, uint32_t len) |
1377 | a1bb27b1 | pbrook | { |
1378 | 2d7adea4 | Andrzej Zaborowski | uint64_t end = addr + len; |
1379 | a1bb27b1 | pbrook | |
1380 | a1bb27b1 | pbrook | if ((addr & 511) || len < 512) |
1381 | 33f00271 | balrog | if (!sd->bdrv || bdrv_read(sd->bdrv, addr >> 9, sd->buf, 1) == -1) { |
1382 | 827df9f3 | balrog | fprintf(stderr, "sd_blk_write: read error on host side\n");
|
1383 | a1bb27b1 | pbrook | return;
|
1384 | a1bb27b1 | pbrook | } |
1385 | a1bb27b1 | pbrook | |
1386 | a1bb27b1 | pbrook | if (end > (addr & ~511) + 512) { |
1387 | 33f00271 | balrog | memcpy(sd->buf + (addr & 511), sd->data, 512 - (addr & 511)); |
1388 | 33f00271 | balrog | if (bdrv_write(sd->bdrv, addr >> 9, sd->buf, 1) == -1) { |
1389 | 827df9f3 | balrog | fprintf(stderr, "sd_blk_write: write error on host side\n");
|
1390 | a1bb27b1 | pbrook | return;
|
1391 | a1bb27b1 | pbrook | } |
1392 | a1bb27b1 | pbrook | |
1393 | 33f00271 | balrog | if (bdrv_read(sd->bdrv, end >> 9, sd->buf, 1) == -1) { |
1394 | 827df9f3 | balrog | fprintf(stderr, "sd_blk_write: read error on host side\n");
|
1395 | a1bb27b1 | pbrook | return;
|
1396 | a1bb27b1 | pbrook | } |
1397 | 33f00271 | balrog | memcpy(sd->buf, sd->data + 512 - (addr & 511), end & 511); |
1398 | 33f00271 | balrog | if (bdrv_write(sd->bdrv, end >> 9, sd->buf, 1) == -1) |
1399 | 827df9f3 | balrog | fprintf(stderr, "sd_blk_write: write error on host side\n");
|
1400 | a1bb27b1 | pbrook | } else {
|
1401 | 33f00271 | balrog | memcpy(sd->buf + (addr & 511), sd->data, len);
|
1402 | 33f00271 | balrog | if (!sd->bdrv || bdrv_write(sd->bdrv, addr >> 9, sd->buf, 1) == -1) |
1403 | 827df9f3 | balrog | fprintf(stderr, "sd_blk_write: write error on host side\n");
|
1404 | a1bb27b1 | pbrook | } |
1405 | a1bb27b1 | pbrook | } |
1406 | a1bb27b1 | pbrook | |
1407 | 33f00271 | balrog | #define BLK_READ_BLOCK(a, len) sd_blk_read(sd, a, len)
|
1408 | 33f00271 | balrog | #define BLK_WRITE_BLOCK(a, len) sd_blk_write(sd, a, len)
|
1409 | a1bb27b1 | pbrook | #define APP_READ_BLOCK(a, len) memset(sd->data, 0xec, len) |
1410 | a1bb27b1 | pbrook | #define APP_WRITE_BLOCK(a, len)
|
1411 | a1bb27b1 | pbrook | |
1412 | a1bb27b1 | pbrook | void sd_write_data(SDState *sd, uint8_t value)
|
1413 | a1bb27b1 | pbrook | { |
1414 | a1bb27b1 | pbrook | int i;
|
1415 | a1bb27b1 | pbrook | |
1416 | 827df9f3 | balrog | if (!sd->bdrv || !bdrv_is_inserted(sd->bdrv) || !sd->enable)
|
1417 | a1bb27b1 | pbrook | return;
|
1418 | a1bb27b1 | pbrook | |
1419 | a1bb27b1 | pbrook | if (sd->state != sd_receivingdata_state) {
|
1420 | 827df9f3 | balrog | fprintf(stderr, "sd_write_data: not in Receiving-Data state\n");
|
1421 | a1bb27b1 | pbrook | return;
|
1422 | a1bb27b1 | pbrook | } |
1423 | a1bb27b1 | pbrook | |
1424 | a1bb27b1 | pbrook | if (sd->card_status & (ADDRESS_ERROR | WP_VIOLATION))
|
1425 | a1bb27b1 | pbrook | return;
|
1426 | a1bb27b1 | pbrook | |
1427 | a1bb27b1 | pbrook | switch (sd->current_cmd) {
|
1428 | a1bb27b1 | pbrook | case 24: /* CMD24: WRITE_SINGLE_BLOCK */ |
1429 | a1bb27b1 | pbrook | sd->data[sd->data_offset ++] = value; |
1430 | a1bb27b1 | pbrook | if (sd->data_offset >= sd->blk_len) {
|
1431 | a1bb27b1 | pbrook | /* TODO: Check CRC before committing */
|
1432 | a1bb27b1 | pbrook | sd->state = sd_programming_state; |
1433 | a1bb27b1 | pbrook | BLK_WRITE_BLOCK(sd->data_start, sd->data_offset); |
1434 | a1bb27b1 | pbrook | sd->blk_written ++; |
1435 | a1bb27b1 | pbrook | sd->csd[14] |= 0x40; |
1436 | a1bb27b1 | pbrook | /* Bzzzzzzztt .... Operation complete. */
|
1437 | a1bb27b1 | pbrook | sd->state = sd_transfer_state; |
1438 | a1bb27b1 | pbrook | } |
1439 | a1bb27b1 | pbrook | break;
|
1440 | a1bb27b1 | pbrook | |
1441 | a1bb27b1 | pbrook | case 25: /* CMD25: WRITE_MULTIPLE_BLOCK */ |
1442 | a1bb27b1 | pbrook | sd->data[sd->data_offset ++] = value; |
1443 | a1bb27b1 | pbrook | if (sd->data_offset >= sd->blk_len) {
|
1444 | a1bb27b1 | pbrook | /* TODO: Check CRC before committing */
|
1445 | a1bb27b1 | pbrook | sd->state = sd_programming_state; |
1446 | a1bb27b1 | pbrook | BLK_WRITE_BLOCK(sd->data_start, sd->data_offset); |
1447 | a1bb27b1 | pbrook | sd->blk_written ++; |
1448 | a1bb27b1 | pbrook | sd->data_start += sd->blk_len; |
1449 | a1bb27b1 | pbrook | sd->data_offset = 0;
|
1450 | a1bb27b1 | pbrook | if (sd->data_start + sd->blk_len > sd->size) {
|
1451 | a1bb27b1 | pbrook | sd->card_status |= ADDRESS_ERROR; |
1452 | a1bb27b1 | pbrook | break;
|
1453 | a1bb27b1 | pbrook | } |
1454 | a1bb27b1 | pbrook | if (sd_wp_addr(sd, sd->data_start)) {
|
1455 | a1bb27b1 | pbrook | sd->card_status |= WP_VIOLATION; |
1456 | a1bb27b1 | pbrook | break;
|
1457 | a1bb27b1 | pbrook | } |
1458 | a1bb27b1 | pbrook | sd->csd[14] |= 0x40; |
1459 | a1bb27b1 | pbrook | |
1460 | a1bb27b1 | pbrook | /* Bzzzzzzztt .... Operation complete. */
|
1461 | a1bb27b1 | pbrook | sd->state = sd_receivingdata_state; |
1462 | a1bb27b1 | pbrook | } |
1463 | a1bb27b1 | pbrook | break;
|
1464 | a1bb27b1 | pbrook | |
1465 | a1bb27b1 | pbrook | case 26: /* CMD26: PROGRAM_CID */ |
1466 | a1bb27b1 | pbrook | sd->data[sd->data_offset ++] = value; |
1467 | a1bb27b1 | pbrook | if (sd->data_offset >= sizeof(sd->cid)) { |
1468 | a1bb27b1 | pbrook | /* TODO: Check CRC before committing */
|
1469 | a1bb27b1 | pbrook | sd->state = sd_programming_state; |
1470 | a1bb27b1 | pbrook | for (i = 0; i < sizeof(sd->cid); i ++) |
1471 | a1bb27b1 | pbrook | if ((sd->cid[i] | 0x00) != sd->data[i]) |
1472 | a1bb27b1 | pbrook | sd->card_status |= CID_CSD_OVERWRITE; |
1473 | a1bb27b1 | pbrook | |
1474 | a1bb27b1 | pbrook | if (!(sd->card_status & CID_CSD_OVERWRITE))
|
1475 | a1bb27b1 | pbrook | for (i = 0; i < sizeof(sd->cid); i ++) { |
1476 | a1bb27b1 | pbrook | sd->cid[i] |= 0x00;
|
1477 | a1bb27b1 | pbrook | sd->cid[i] &= sd->data[i]; |
1478 | a1bb27b1 | pbrook | } |
1479 | a1bb27b1 | pbrook | /* Bzzzzzzztt .... Operation complete. */
|
1480 | a1bb27b1 | pbrook | sd->state = sd_transfer_state; |
1481 | a1bb27b1 | pbrook | } |
1482 | a1bb27b1 | pbrook | break;
|
1483 | a1bb27b1 | pbrook | |
1484 | a1bb27b1 | pbrook | case 27: /* CMD27: PROGRAM_CSD */ |
1485 | a1bb27b1 | pbrook | sd->data[sd->data_offset ++] = value; |
1486 | a1bb27b1 | pbrook | if (sd->data_offset >= sizeof(sd->csd)) { |
1487 | a1bb27b1 | pbrook | /* TODO: Check CRC before committing */
|
1488 | a1bb27b1 | pbrook | sd->state = sd_programming_state; |
1489 | a1bb27b1 | pbrook | for (i = 0; i < sizeof(sd->csd); i ++) |
1490 | a1bb27b1 | pbrook | if ((sd->csd[i] | sd_csd_rw_mask[i]) !=
|
1491 | a1bb27b1 | pbrook | (sd->data[i] | sd_csd_rw_mask[i])) |
1492 | a1bb27b1 | pbrook | sd->card_status |= CID_CSD_OVERWRITE; |
1493 | a1bb27b1 | pbrook | |
1494 | a1bb27b1 | pbrook | /* Copy flag (OTP) & Permanent write protect */
|
1495 | a1bb27b1 | pbrook | if (sd->csd[14] & ~sd->data[14] & 0x60) |
1496 | a1bb27b1 | pbrook | sd->card_status |= CID_CSD_OVERWRITE; |
1497 | a1bb27b1 | pbrook | |
1498 | a1bb27b1 | pbrook | if (!(sd->card_status & CID_CSD_OVERWRITE))
|
1499 | a1bb27b1 | pbrook | for (i = 0; i < sizeof(sd->csd); i ++) { |
1500 | a1bb27b1 | pbrook | sd->csd[i] |= sd_csd_rw_mask[i]; |
1501 | a1bb27b1 | pbrook | sd->csd[i] &= sd->data[i]; |
1502 | a1bb27b1 | pbrook | } |
1503 | a1bb27b1 | pbrook | /* Bzzzzzzztt .... Operation complete. */
|
1504 | a1bb27b1 | pbrook | sd->state = sd_transfer_state; |
1505 | a1bb27b1 | pbrook | } |
1506 | a1bb27b1 | pbrook | break;
|
1507 | a1bb27b1 | pbrook | |
1508 | a1bb27b1 | pbrook | case 42: /* CMD42: LOCK_UNLOCK */ |
1509 | a1bb27b1 | pbrook | sd->data[sd->data_offset ++] = value; |
1510 | a1bb27b1 | pbrook | if (sd->data_offset >= sd->blk_len) {
|
1511 | a1bb27b1 | pbrook | /* TODO: Check CRC before committing */
|
1512 | a1bb27b1 | pbrook | sd->state = sd_programming_state; |
1513 | a1bb27b1 | pbrook | sd_lock_command(sd); |
1514 | a1bb27b1 | pbrook | /* Bzzzzzzztt .... Operation complete. */
|
1515 | a1bb27b1 | pbrook | sd->state = sd_transfer_state; |
1516 | a1bb27b1 | pbrook | } |
1517 | a1bb27b1 | pbrook | break;
|
1518 | a1bb27b1 | pbrook | |
1519 | a1bb27b1 | pbrook | case 56: /* CMD56: GEN_CMD */ |
1520 | a1bb27b1 | pbrook | sd->data[sd->data_offset ++] = value; |
1521 | a1bb27b1 | pbrook | if (sd->data_offset >= sd->blk_len) {
|
1522 | a1bb27b1 | pbrook | APP_WRITE_BLOCK(sd->data_start, sd->data_offset); |
1523 | a1bb27b1 | pbrook | sd->state = sd_transfer_state; |
1524 | a1bb27b1 | pbrook | } |
1525 | a1bb27b1 | pbrook | break;
|
1526 | a1bb27b1 | pbrook | |
1527 | a1bb27b1 | pbrook | default:
|
1528 | 827df9f3 | balrog | fprintf(stderr, "sd_write_data: unknown command\n");
|
1529 | a1bb27b1 | pbrook | break;
|
1530 | a1bb27b1 | pbrook | } |
1531 | a1bb27b1 | pbrook | } |
1532 | a1bb27b1 | pbrook | |
1533 | a1bb27b1 | pbrook | uint8_t sd_read_data(SDState *sd) |
1534 | a1bb27b1 | pbrook | { |
1535 | a1bb27b1 | pbrook | /* TODO: Append CRCs */
|
1536 | a1bb27b1 | pbrook | uint8_t ret; |
1537 | 2d7adea4 | Andrzej Zaborowski | int io_len;
|
1538 | a1bb27b1 | pbrook | |
1539 | 827df9f3 | balrog | if (!sd->bdrv || !bdrv_is_inserted(sd->bdrv) || !sd->enable)
|
1540 | a1bb27b1 | pbrook | return 0x00; |
1541 | a1bb27b1 | pbrook | |
1542 | a1bb27b1 | pbrook | if (sd->state != sd_sendingdata_state) {
|
1543 | 827df9f3 | balrog | fprintf(stderr, "sd_read_data: not in Sending-Data state\n");
|
1544 | a1bb27b1 | pbrook | return 0x00; |
1545 | a1bb27b1 | pbrook | } |
1546 | a1bb27b1 | pbrook | |
1547 | a1bb27b1 | pbrook | if (sd->card_status & (ADDRESS_ERROR | WP_VIOLATION))
|
1548 | a1bb27b1 | pbrook | return 0x00; |
1549 | a1bb27b1 | pbrook | |
1550 | 2d7adea4 | Andrzej Zaborowski | io_len = (sd->ocr & (1 << 30)) ? 512 : sd->blk_len; |
1551 | 2d7adea4 | Andrzej Zaborowski | |
1552 | a1bb27b1 | pbrook | switch (sd->current_cmd) {
|
1553 | a1bb27b1 | pbrook | case 6: /* CMD6: SWITCH_FUNCTION */ |
1554 | a1bb27b1 | pbrook | ret = sd->data[sd->data_offset ++]; |
1555 | a1bb27b1 | pbrook | |
1556 | a1bb27b1 | pbrook | if (sd->data_offset >= 64) |
1557 | a1bb27b1 | pbrook | sd->state = sd_transfer_state; |
1558 | a1bb27b1 | pbrook | break;
|
1559 | a1bb27b1 | pbrook | |
1560 | 775616c3 | pbrook | case 9: /* CMD9: SEND_CSD */ |
1561 | 775616c3 | pbrook | case 10: /* CMD10: SEND_CID */ |
1562 | 775616c3 | pbrook | ret = sd->data[sd->data_offset ++]; |
1563 | 775616c3 | pbrook | |
1564 | 775616c3 | pbrook | if (sd->data_offset >= 16) |
1565 | 775616c3 | pbrook | sd->state = sd_transfer_state; |
1566 | 775616c3 | pbrook | break;
|
1567 | 775616c3 | pbrook | |
1568 | a1bb27b1 | pbrook | case 11: /* CMD11: READ_DAT_UNTIL_STOP */ |
1569 | a1bb27b1 | pbrook | if (sd->data_offset == 0) |
1570 | 2d7adea4 | Andrzej Zaborowski | BLK_READ_BLOCK(sd->data_start, io_len); |
1571 | a1bb27b1 | pbrook | ret = sd->data[sd->data_offset ++]; |
1572 | a1bb27b1 | pbrook | |
1573 | 2d7adea4 | Andrzej Zaborowski | if (sd->data_offset >= io_len) {
|
1574 | 2d7adea4 | Andrzej Zaborowski | sd->data_start += io_len; |
1575 | a1bb27b1 | pbrook | sd->data_offset = 0;
|
1576 | 2d7adea4 | Andrzej Zaborowski | if (sd->data_start + io_len > sd->size) {
|
1577 | a1bb27b1 | pbrook | sd->card_status |= ADDRESS_ERROR; |
1578 | a1bb27b1 | pbrook | break;
|
1579 | a1bb27b1 | pbrook | } |
1580 | a1bb27b1 | pbrook | } |
1581 | a1bb27b1 | pbrook | break;
|
1582 | a1bb27b1 | pbrook | |
1583 | a1bb27b1 | pbrook | case 13: /* ACMD13: SD_STATUS */ |
1584 | a1bb27b1 | pbrook | ret = sd->sd_status[sd->data_offset ++]; |
1585 | a1bb27b1 | pbrook | |
1586 | a1bb27b1 | pbrook | if (sd->data_offset >= sizeof(sd->sd_status)) |
1587 | a1bb27b1 | pbrook | sd->state = sd_transfer_state; |
1588 | a1bb27b1 | pbrook | break;
|
1589 | a1bb27b1 | pbrook | |
1590 | a1bb27b1 | pbrook | case 17: /* CMD17: READ_SINGLE_BLOCK */ |
1591 | a1bb27b1 | pbrook | if (sd->data_offset == 0) |
1592 | 2d7adea4 | Andrzej Zaborowski | BLK_READ_BLOCK(sd->data_start, io_len); |
1593 | a1bb27b1 | pbrook | ret = sd->data[sd->data_offset ++]; |
1594 | a1bb27b1 | pbrook | |
1595 | 2d7adea4 | Andrzej Zaborowski | if (sd->data_offset >= io_len)
|
1596 | a1bb27b1 | pbrook | sd->state = sd_transfer_state; |
1597 | a1bb27b1 | pbrook | break;
|
1598 | a1bb27b1 | pbrook | |
1599 | a1bb27b1 | pbrook | case 18: /* CMD18: READ_MULTIPLE_BLOCK */ |
1600 | a1bb27b1 | pbrook | if (sd->data_offset == 0) |
1601 | 2d7adea4 | Andrzej Zaborowski | BLK_READ_BLOCK(sd->data_start, io_len); |
1602 | a1bb27b1 | pbrook | ret = sd->data[sd->data_offset ++]; |
1603 | a1bb27b1 | pbrook | |
1604 | 2d7adea4 | Andrzej Zaborowski | if (sd->data_offset >= io_len) {
|
1605 | 2d7adea4 | Andrzej Zaborowski | sd->data_start += io_len; |
1606 | a1bb27b1 | pbrook | sd->data_offset = 0;
|
1607 | 2d7adea4 | Andrzej Zaborowski | if (sd->data_start + io_len > sd->size) {
|
1608 | a1bb27b1 | pbrook | sd->card_status |= ADDRESS_ERROR; |
1609 | a1bb27b1 | pbrook | break;
|
1610 | a1bb27b1 | pbrook | } |
1611 | a1bb27b1 | pbrook | } |
1612 | a1bb27b1 | pbrook | break;
|
1613 | a1bb27b1 | pbrook | |
1614 | a1bb27b1 | pbrook | case 22: /* ACMD22: SEND_NUM_WR_BLOCKS */ |
1615 | a1bb27b1 | pbrook | ret = sd->data[sd->data_offset ++]; |
1616 | a1bb27b1 | pbrook | |
1617 | a1bb27b1 | pbrook | if (sd->data_offset >= 4) |
1618 | a1bb27b1 | pbrook | sd->state = sd_transfer_state; |
1619 | a1bb27b1 | pbrook | break;
|
1620 | a1bb27b1 | pbrook | |
1621 | a1bb27b1 | pbrook | case 30: /* CMD30: SEND_WRITE_PROT */ |
1622 | a1bb27b1 | pbrook | ret = sd->data[sd->data_offset ++]; |
1623 | a1bb27b1 | pbrook | |
1624 | a1bb27b1 | pbrook | if (sd->data_offset >= 4) |
1625 | a1bb27b1 | pbrook | sd->state = sd_transfer_state; |
1626 | a1bb27b1 | pbrook | break;
|
1627 | a1bb27b1 | pbrook | |
1628 | a1bb27b1 | pbrook | case 51: /* ACMD51: SEND_SCR */ |
1629 | a1bb27b1 | pbrook | ret = sd->scr[sd->data_offset ++]; |
1630 | a1bb27b1 | pbrook | |
1631 | a1bb27b1 | pbrook | if (sd->data_offset >= sizeof(sd->scr)) |
1632 | a1bb27b1 | pbrook | sd->state = sd_transfer_state; |
1633 | a1bb27b1 | pbrook | break;
|
1634 | a1bb27b1 | pbrook | |
1635 | a1bb27b1 | pbrook | case 56: /* CMD56: GEN_CMD */ |
1636 | a1bb27b1 | pbrook | if (sd->data_offset == 0) |
1637 | a1bb27b1 | pbrook | APP_READ_BLOCK(sd->data_start, sd->blk_len); |
1638 | a1bb27b1 | pbrook | ret = sd->data[sd->data_offset ++]; |
1639 | a1bb27b1 | pbrook | |
1640 | a1bb27b1 | pbrook | if (sd->data_offset >= sd->blk_len)
|
1641 | a1bb27b1 | pbrook | sd->state = sd_transfer_state; |
1642 | a1bb27b1 | pbrook | break;
|
1643 | a1bb27b1 | pbrook | |
1644 | a1bb27b1 | pbrook | default:
|
1645 | 827df9f3 | balrog | fprintf(stderr, "sd_read_data: unknown command\n");
|
1646 | a1bb27b1 | pbrook | return 0x00; |
1647 | a1bb27b1 | pbrook | } |
1648 | a1bb27b1 | pbrook | |
1649 | a1bb27b1 | pbrook | return ret;
|
1650 | a1bb27b1 | pbrook | } |
1651 | a1bb27b1 | pbrook | |
1652 | a1bb27b1 | pbrook | int sd_data_ready(SDState *sd)
|
1653 | a1bb27b1 | pbrook | { |
1654 | a1bb27b1 | pbrook | return sd->state == sd_sendingdata_state;
|
1655 | a1bb27b1 | pbrook | } |
1656 | 827df9f3 | balrog | |
1657 | 827df9f3 | balrog | void sd_enable(SDState *sd, int enable) |
1658 | 827df9f3 | balrog | { |
1659 | 827df9f3 | balrog | sd->enable = enable; |
1660 | 827df9f3 | balrog | } |