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