1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
/*
* Copyright © 2013 Keith Packard <keithp@keithp.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include "ao.h"
#include "ao_sdcard.h"
#define ao_sdcard_get_slow() ao_spi_get(AO_SDCARD_SPI_BUS, AO_SPI_SPEED_250kHz)
#define ao_sdcard_get() ao_spi_get(AO_SDCARD_SPI_BUS, AO_SPI_SPEED_FAST)
#define ao_sdcard_put() ao_spi_put(AO_SDCARD_SPI_BUS)
#define ao_sdcard_send_fixed(d,l) ao_spi_send_fixed((d), (l), AO_SDCARD_SPI_BUS)
#define ao_sdcard_send(d,l) ao_spi_send((d), (l), AO_SDCARD_SPI_BUS)
#define ao_sdcard_recv(d,l) ao_spi_recv((d), (l), AO_SDCARD_SPI_BUS)
#define ao_sdcard_select() ao_gpio_set(AO_SDCARD_SPI_CS_PORT,AO_SDCARD_SPI_CS_PIN,AO_SDCARD_SPI_CS,0)
#define ao_sdcard_deselect() ao_gpio_set(AO_SDCARD_SPI_CS_PORT,AO_SDCARD_SPI_CS_PIN,AO_SDCARD_SPI_CS,1)
static uint8_t initialized;
static uint8_t present;
static uint8_t mutex;
static enum ao_sdtype sdtype;
#define ao_sdcard_lock() ao_mutex_get(&mutex)
#define ao_sdcard_unlock() ao_mutex_put(&mutex)
#if 0
#define DBG(...) printf(__VA_ARGS__)
#else
#define DBG(...)
#endif
/*
* Send an SD command and await the status reply
*/
static uint8_t
ao_sdcard_send_cmd(uint8_t cmd, uint32_t arg)
{
uint8_t data[6];
uint8_t reply;
int i;
DBG ("\tsend_cmd %d arg %08x\n", cmd, arg);
if (cmd != SDCARD_GO_IDLE_STATE) {
for (i = 0; i < SDCARD_CMD_TIMEOUT; i++) {
ao_sdcard_recv(&reply, 1);
if (reply == 0xff)
break;
}
if (i == SDCARD_CMD_TIMEOUT)
return SDCARD_STATUS_TIMEOUT;
}
data[0] = cmd & 0x3f | 0x40;
data[1] = arg >> 24;
data[2] = arg >> 16;
data[3] = arg >> 8;
data[4] = arg;
if (cmd == SDCARD_GO_IDLE_STATE)
data[5] = 0x95; /* Valid for 0 arg */
else if (cmd == SDCARD_SEND_IF_COND)
data[5] = 0x87; /* Valid for 0x1aa arg */
else
data[5] = 0xff; /* no CRC */
ao_sdcard_send(data, 6);
/* The first reply byte will be the status,
* which must have the high bit clear
*/
for (i = 0; i < SDCARD_CMD_TIMEOUT; i++) {
ao_sdcard_recv(&reply, 1);
DBG ("\t\tgot byte %02x\n", reply);
if ((reply & 0x80) == 0)
return reply;
}
return SDCARD_STATUS_TIMEOUT;
}
/*
* Retrieve any reply, discarding the trailing CRC byte
*/
static void
ao_sdcard_recv_reply(uint8_t *reply, int len)
{
uint8_t discard;
if (len)
ao_sdcard_recv(reply, len);
/* trailing byte */
ao_sdcard_recv(&discard, 1);
}
/*
* Wait while the card is busy. The
* card will return a stream of 0xff
* until it isn't busy anymore
*/
static void
ao_sdcard_wait_busy(void)
{
uint8_t v;
do {
ao_sdcard_recv(&v, 1);
} while (v != 0xff);
ao_sdcard_send_fixed(0xff, 1);
}
static uint8_t
ao_sdcard_go_idle_state(void)
{
uint8_t ret;
DBG ("go_idle_state\n");
ao_sdcard_select();
ret = ao_sdcard_send_cmd(SDCARD_GO_IDLE_STATE, 0);
ao_sdcard_recv_reply(NULL, 0);
ao_sdcard_deselect();
DBG ("\tgo_idle_state status %02x\n", ret);
return ret;
}
static uint8_t
ao_sdcard_send_op_cond(void)
{
uint8_t ret;
DBG ("send_op_cond\n");
ao_sdcard_select();
ret = ao_sdcard_send_cmd(SDCARD_SEND_OP_COND, 0);
ao_sdcard_recv_reply(NULL, 0);
ao_sdcard_deselect();
DBG ("\tsend_op_cond %02x\n", ret);
return ret;
}
static uint8_t
ao_sdcard_send_if_cond(uint32_t arg, uint8_t send_if_cond_response[4])
{
uint8_t ret;
DBG ("send_if_cond\n");
ao_sdcard_select();
ret = ao_sdcard_send_cmd(SDCARD_SEND_IF_COND, arg);
if (ret != SDCARD_STATUS_IDLE_STATE) {
DBG ("\tsend_if_cond failed %02x\n", ret);
return ret;
}
ao_sdcard_recv_reply(send_if_cond_response, 4);
DBG ("send_if_cond status %02x response %02x %02x %02x %02x\n",
ret,
send_if_cond_response[0],
send_if_cond_response[1],
send_if_cond_response[2],
send_if_cond_response[3]);
ao_sdcard_deselect();
return ret;
}
static uint8_t
ao_sdcard_set_blocklen(uint32_t blocklen)
{
uint8_t ret;
DBG ("set_blocklen %d\n", blocklen);
ao_sdcard_select();
ret = ao_sdcard_send_cmd(SDCARD_SET_BLOCKLEN, blocklen);
ao_sdcard_recv_reply(NULL, 0);
if (ret != SDCARD_STATUS_READY_STATE)
DBG ("\tsend_if_cond failed %02x\n", ret);
return ret;
}
static uint8_t
ao_sdcard_app_cmd(void)
{
uint8_t ret;
DBG ("app_cmd\n");
ao_sdcard_select();
ret = ao_sdcard_send_cmd(SDCARD_APP_CMD, 0);
ao_sdcard_recv_reply(NULL, 0);
ao_sdcard_deselect();
DBG ("\tapp_cmd status %02x\n");
return ret;
}
static uint8_t
ao_sdcard_app_send_op_cond(uint32_t arg)
{
uint8_t ret;
ret = ao_sdcard_app_cmd();
if (ret != SDCARD_STATUS_IDLE_STATE)
return ret;
DBG("send_op_comd\n");
ao_sdcard_select();
ret = ao_sdcard_send_cmd(SDCARD_APP_SEND_OP_COMD, arg);
ao_sdcard_recv_reply(NULL, 0);
ao_sdcard_deselect();
DBG ("\tapp_send_op_cond status %02x\n", ret);
return ret;
}
static uint8_t
ao_sdcard_read_ocr(uint8_t read_ocr_response[4])
{
uint8_t ret;
DBG ("read_ocr\n");
ao_sdcard_select();
ret = ao_sdcard_send_cmd(SDCARD_READ_OCR, 0);
if (ret != SDCARD_STATUS_READY_STATE)
DBG ("\tread_ocr failed %02x\n", ret);
else {
ao_sdcard_recv_reply(read_ocr_response, 4);
DBG ("\tread_ocr status %02x response %02x %02x %02x %02x\n", ret,
read_ocr_response[0], read_ocr_response[1],
read_ocr_response[2], read_ocr_response[3]);
}
ao_sdcard_deselect();
return ret;
}
static void
ao_sdcard_setup(void)
{
int i;
uint8_t ret;
uint8_t response[10];
DBG ("Testing sdcard\n");
ao_sdcard_get_slow();
/*
* min 74 clocks with CS high
*/
ao_sdcard_send_fixed(0xff, 10);
ao_delay(AO_MS_TO_TICKS(10));
/* Reset the card and get it into SPI mode */
for (i = 0; i < SDCARD_IDLE_WAIT; i++) {
if (ao_sdcard_go_idle_state() == SDCARD_STATUS_IDLE_STATE)
break;
}
if (i == SDCARD_IDLE_WAIT)
goto bail;
/* Figure out what kind of card we have */
sdtype = ao_sdtype_unknown;
if (ao_sdcard_send_if_cond(0x1aa, response) == SDCARD_STATUS_IDLE_STATE) {
uint32_t arg = 0;
uint8_t sdver2 = 0;
/* Check for SD version 2 */
if ((response[2] & 0xf) == 1 && response[3] == 0xaa) {
arg = 0x40000000;
sdver2 = 1;
}
for (i = 0; i < SDCARD_IDLE_WAIT; i++) {
ret = ao_sdcard_app_send_op_cond(arg);
if (ret != SDCARD_STATUS_IDLE_STATE)
break;
}
if (ret != SDCARD_STATUS_READY_STATE) {
/* MMC */
for (i = 0; i < SDCARD_IDLE_WAIT; i++) {
ret = ao_sdcard_send_op_cond();
if (ret != SDCARD_STATUS_IDLE_STATE)
break;
}
if (ret != SDCARD_STATUS_READY_STATE)
goto bail;
sdtype = ao_sdtype_mmc3;
} else {
/* SD */
if (sdver2 != 0) {
ret = ao_sdcard_read_ocr(response);
if (ret != SDCARD_STATUS_READY_STATE)
goto bail;
if ((response[0] & 0xc0) == 0xc0)
sdtype = ao_sdtype_sd2block;
else
sdtype = ao_sdtype_sd2byte;
} else {
sdtype = ao_sdtype_sd1;
}
}
/* For everything but SDHC cards, set the block length */
if (sdtype != ao_sdtype_sd2block) {
ret = ao_sdcard_set_blocklen(512);
if (ret != SDCARD_STATUS_READY_STATE)
DBG ("set_blocklen failed, ignoring\n");
}
}
DBG ("SD card detected, type %d\n", sdtype);
bail:
ao_sdcard_put();
}
static uint8_t
ao_sdcard_wait_block_start(void)
{
int i;
uint8_t v;
DBG ("\twait_block_start\n");
for (i = 0; i < SDCARD_BLOCK_TIMEOUT; i++) {
ao_sdcard_recv(&v, 1);
DBG("\t\trecv %02x\n", v);
if (v != 0xff)
break;
}
return v;
}
/*
* Read a block of 512 bytes from the card
*/
uint8_t
ao_sdcard_read_block(uint32_t block, uint8_t *data)
{
uint8_t ret;
uint8_t crc[2];
ao_sdcard_lock();
if (!initialized) {
ao_sdcard_setup();
initialized = 1;
if (sdtype != ao_sdtype_unknown)
present = 1;
}
if (!present) {
ao_sdcard_unlock();
return 0;
}
if (sdtype != ao_sdtype_sd2block)
block <<= 9;
ao_sdcard_get();
ao_sdcard_select();
ret = ao_sdcard_send_cmd(SDCARD_READ_BLOCK, block);
ao_sdcard_recv_reply(NULL, 0);
if (ret != SDCARD_STATUS_READY_STATE)
goto bail;
if (ao_sdcard_wait_block_start() != 0xfe) {
ret = 0x3f;
goto bail;
}
ao_sdcard_recv(data, 512);
ao_sdcard_recv(crc, 2);
bail:
ao_sdcard_deselect();
ao_sdcard_put();
ao_sdcard_unlock();
return ret == SDCARD_STATUS_READY_STATE;
}
/*
* Write a block of 512 bytes to the card
*/
uint8_t
ao_sdcard_write_block(uint32_t block, uint8_t *data)
{
/* Not doing anything until the file system code seems reasonable
*/
return 1;
}
void
ao_sdcard_init(void)
{
ao_spi_init_cs(AO_SDCARD_SPI_CS_PORT, (1 << AO_SDCARD_SPI_CS_PIN));
}
|