2 * Copyright © 2013 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 #include "ao_sdcard.h"
21 #define ao_sdcard_get_slow() ao_spi_get(AO_SDCARD_SPI_BUS, AO_SPI_SPEED_250kHz)
22 #define ao_sdcard_get() ao_spi_get(AO_SDCARD_SPI_BUS, AO_SPI_SPEED_FAST)
23 #define ao_sdcard_put() ao_spi_put(AO_SDCARD_SPI_BUS)
24 #define ao_sdcard_send_fixed(d,l) ao_spi_send_fixed((d), (l), AO_SDCARD_SPI_BUS)
25 #define ao_sdcard_send(d,l) ao_spi_send((d), (l), AO_SDCARD_SPI_BUS)
26 #define ao_sdcard_recv(d,l) ao_spi_recv((d), (l), AO_SDCARD_SPI_BUS)
27 #define ao_sdcard_select() ao_gpio_set(AO_SDCARD_SPI_CS_PORT,AO_SDCARD_SPI_CS_PIN,AO_SDCARD_SPI_CS,0)
28 #define ao_sdcard_deselect() ao_gpio_set(AO_SDCARD_SPI_CS_PORT,AO_SDCARD_SPI_CS_PIN,AO_SDCARD_SPI_CS,1)
30 /* Include SD card commands */
31 #define SDCARD_DEBUG 1
34 #define SDCARD_TRACE 0
36 /* Emit error and warning messages */
39 static uint8_t initialized;
40 static uint8_t present;
42 static enum ao_sdtype sdtype;
44 #define ao_sdcard_lock() ao_mutex_get(&mutex)
45 #define ao_sdcard_unlock() ao_mutex_put(&mutex)
48 #define DBG(...) printf(__VA_ARGS__)
54 #define WARN(...) printf(__VA_ARGS__)
59 #define later(x,y) ((int16_t) ((x) - (y)) >= 0)
62 * Wait while the card is busy. The card will return a stream of 0xff
63 * when it is ready to accept a command
67 ao_sdcard_wait_busy(void)
69 uint16_t timeout = ao_time() + SDCARD_BUSY_TIMEOUT;
72 ao_sdcard_recv(&reply, 1);
73 DBG("\t\twait busy %02x\n", reply);
76 if (later(ao_time(), timeout)) {
77 WARN("wait busy timeout\n");
86 * Send an SD command and await the status reply
90 ao_sdcard_send_cmd(uint8_t cmd, uint32_t arg)
97 DBG ("\tsend_cmd %d arg %08x\n", cmd, arg);
99 /* Wait for the card to not be busy */
100 if (cmd != SDCARD_GO_IDLE_STATE) {
101 if (!ao_sdcard_wait_busy())
102 return SDCARD_STATUS_TIMEOUT;
105 data[0] = cmd & 0x3f | 0x40;
110 if (cmd == SDCARD_GO_IDLE_STATE)
111 data[5] = 0x95; /* Valid for 0 arg */
112 else if (cmd == SDCARD_SEND_IF_COND)
113 data[5] = 0x87; /* Valid for 0x1aa arg */
115 data[5] = 0xff; /* no CRC */
116 ao_sdcard_send(data, 6);
118 /* The first reply byte will be the status,
119 * which must have the high bit clear
121 timeout = ao_time() + SDCARD_CMD_TIMEOUT;
123 ao_sdcard_recv(&reply, 1);
124 DBG ("\t\tgot byte %02x\n", reply);
125 if ((reply & 0x80) == 0)
127 if (later(ao_time(), timeout)) {
128 WARN("send_cmd %02x timeout\n", cmd);
129 return SDCARD_STATUS_TIMEOUT;
133 if (reply != SDCARD_STATUS_READY_STATE && reply != SDCARD_STATUS_IDLE_STATE)
134 WARN("send_cmd %d failed %02x\n", cmd, reply);
140 * Retrieve any reply, discarding the trailing CRC byte
143 ao_sdcard_recv_reply(uint8_t *reply, int len)
148 ao_sdcard_recv(reply, len);
150 ao_sdcard_recv(&discard, 1);
154 * Switch to 'idle' state. This is used to get the card into SPI mode
157 ao_sdcard_go_idle_state(void)
161 DBG ("go_idle_state\n");
163 ret = ao_sdcard_send_cmd(SDCARD_GO_IDLE_STATE, 0);
164 ao_sdcard_recv_reply(NULL, 0);
165 ao_sdcard_deselect();
166 DBG ("\tgo_idle_state status %02x\n", ret);
171 ao_sdcard_send_op_cond(void)
175 DBG ("send_op_cond\n");
177 ret = ao_sdcard_send_cmd(SDCARD_SEND_OP_COND, 0);
178 ao_sdcard_recv_reply(NULL, 0);
179 ao_sdcard_deselect();
180 DBG ("\tsend_op_cond %02x\n", ret);
185 ao_sdcard_send_if_cond(uint32_t arg, uint8_t send_if_cond_response[4])
189 DBG ("send_if_cond\n");
191 ret = ao_sdcard_send_cmd(SDCARD_SEND_IF_COND, arg);
192 if (ret != SDCARD_STATUS_IDLE_STATE) {
193 DBG ("\tsend_if_cond failed %02x\n", ret);
196 ao_sdcard_recv_reply(send_if_cond_response, 4);
197 DBG ("send_if_cond status %02x response %02x %02x %02x %02x\n",
199 send_if_cond_response[0],
200 send_if_cond_response[1],
201 send_if_cond_response[2],
202 send_if_cond_response[3]);
203 ao_sdcard_deselect();
208 * _ao_sdcard_send_status
210 * Get the 2-byte status value.
212 * Called from other functions with CS held low already,
213 * hence prefixing the name with '_'
216 _ao_sdcard_send_status(void)
221 DBG ("send_status\n");
222 ret = ao_sdcard_send_cmd(SDCARD_SEND_STATUS, 0);
223 ao_sdcard_recv_reply(&extra, 1);
224 if (ret != SDCARD_STATUS_READY_STATE)
225 DBG ("\tsend_if_cond failed %02x\n", ret);
226 return ret | (extra << 8);
230 * ao_sdcard_set_blocklen
232 * Set the block length for future read and write commands
235 ao_sdcard_set_blocklen(uint32_t blocklen)
239 DBG ("set_blocklen %d\n", blocklen);
241 ret = ao_sdcard_send_cmd(SDCARD_SET_BLOCKLEN, blocklen);
242 ao_sdcard_recv_reply(NULL, 0);
243 ao_sdcard_deselect();
244 if (ret != SDCARD_STATUS_READY_STATE)
245 DBG ("\tsend_if_cond failed %02x\n", ret);
252 * Send the app command prefix
254 * Called with the CS held low, hence
258 _ao_sdcard_app_cmd(void)
263 ret = ao_sdcard_send_cmd(SDCARD_APP_CMD, 0);
264 ao_sdcard_recv_reply(NULL, 0);
265 DBG ("\tapp_cmd status %02x\n");
270 ao_sdcard_app_send_op_cond(uint32_t arg)
274 DBG("send_op_comd\n");
276 ret = _ao_sdcard_app_cmd();
277 if (ret != SDCARD_STATUS_IDLE_STATE)
279 ret = ao_sdcard_send_cmd(SDCARD_APP_SEND_OP_COMD, arg);
280 ao_sdcard_recv_reply(NULL, 0);
282 ao_sdcard_deselect();
283 DBG ("\tapp_send_op_cond status %02x\n", ret);
288 ao_sdcard_read_ocr(uint8_t read_ocr_response[4])
294 ret = ao_sdcard_send_cmd(SDCARD_READ_OCR, 0);
295 if (ret != SDCARD_STATUS_READY_STATE)
296 DBG ("\tread_ocr failed %02x\n", ret);
298 ao_sdcard_recv_reply(read_ocr_response, 4);
299 DBG ("\tread_ocr status %02x response %02x %02x %02x %02x\n", ret,
300 read_ocr_response[0], read_ocr_response[1],
301 read_ocr_response[2], read_ocr_response[3]);
303 ao_sdcard_deselect();
308 * Follow the flow-chart defined by the SD group to
309 * initialize the card and figure out what kind it is
312 ao_sdcard_setup(void)
316 uint8_t response[10];
318 DBG ("Testing sdcard\n");
320 ao_sdcard_get_slow();
322 * min 74 clocks with CS high
324 ao_sdcard_send_fixed(0xff, 10);
326 /* Reset the card and get it into SPI mode */
327 for (i = 0; i < SDCARD_IDLE_WAIT; i++) {
328 if (ao_sdcard_go_idle_state() == SDCARD_STATUS_IDLE_STATE)
331 if (i == SDCARD_IDLE_WAIT)
334 /* Figure out what kind of card we have */
335 sdtype = ao_sdtype_unknown;
337 if (ao_sdcard_send_if_cond(0x1aa, response) == SDCARD_STATUS_IDLE_STATE) {
341 /* Check for SD version 2 */
342 if ((response[2] & 0xf) == 1 && response[3] == 0xaa) {
347 for (i = 0; i < SDCARD_IDLE_WAIT; i++) {
348 ret = ao_sdcard_app_send_op_cond(arg);
349 if (ret != SDCARD_STATUS_IDLE_STATE)
352 if (ret != SDCARD_STATUS_READY_STATE) {
354 for (i = 0; i < SDCARD_IDLE_WAIT; i++) {
355 ret = ao_sdcard_send_op_cond();
356 if (ret != SDCARD_STATUS_IDLE_STATE)
359 if (ret != SDCARD_STATUS_READY_STATE)
361 sdtype = ao_sdtype_mmc3;
365 ret = ao_sdcard_read_ocr(response);
366 if (ret != SDCARD_STATUS_READY_STATE)
368 if ((response[0] & 0xc0) == 0xc0)
369 sdtype = ao_sdtype_sd2block;
371 sdtype = ao_sdtype_sd2byte;
373 sdtype = ao_sdtype_sd1;
377 /* For everything but SDHC cards, set the block length */
378 if (sdtype != ao_sdtype_sd2block) {
379 ret = ao_sdcard_set_blocklen(512);
380 if (ret != SDCARD_STATUS_READY_STATE)
381 DBG ("set_blocklen failed, ignoring\n");
385 DBG ("SD card detected, type %d\n", sdtype);
391 _ao_sdcard_reset(void)
395 uint8_t response[10];
397 for (i = 0; i < SDCARD_IDLE_WAIT; i++) {
398 if (ao_sdcard_go_idle_state() == SDCARD_STATUS_IDLE_STATE)
401 if (i == SDCARD_IDLE_WAIT) {
406 /* Follow the setup path to get the card out of idle state and
407 * up and running again
409 if (ao_sdcard_send_if_cond(0x1aa, response) == SDCARD_STATUS_IDLE_STATE) {
413 /* Check for SD version 2 */
414 if ((response[2] & 0xf) == 1 && response[3] == 0xaa) {
419 for (i = 0; i < SDCARD_IDLE_WAIT; i++) {
420 ret = ao_sdcard_app_send_op_cond(arg);
421 if (ret != SDCARD_STATUS_IDLE_STATE)
425 if (ret != SDCARD_STATUS_READY_STATE) {
427 for (i = 0; i < SDCARD_IDLE_WAIT; i++) {
428 ret = ao_sdcard_send_op_cond();
429 if (ret != SDCARD_STATUS_IDLE_STATE)
432 if (ret != SDCARD_STATUS_READY_STATE)
436 /* For everything but SDHC cards, set the block length */
437 if (sdtype != ao_sdtype_sd2block) {
438 ret = ao_sdcard_set_blocklen(512);
439 if (ret != SDCARD_STATUS_READY_STATE)
440 DBG ("set_blocklen failed, ignoring\n");
448 * The card will send 0xff until it is ready to send
449 * the data block at which point it will send the START_BLOCK
450 * marker followed by the data. This function waits while
451 * the card is sending 0xff
454 ao_sdcard_wait_block_start(void)
457 uint16_t timeout = ao_time() + SDCARD_BLOCK_TIMEOUT;
459 DBG ("\twait_block_start\n");
461 ao_sdcard_recv(&v, 1);
462 DBG("\t\trecv %02x\n", v);
465 if (later(ao_time(), timeout)) {
466 printf ("wait block start timeout\n");
474 * Read a block of 512 bytes from the card
477 ao_sdcard_read_block(uint32_t block, uint8_t *data)
488 if (sdtype != ao_sdtype_unknown)
495 DBG("read block %d\n", block);
496 if (sdtype != ao_sdtype_sd2block)
500 for (tries = 0; tries < 10; tries++) {
503 ret = ao_sdcard_send_cmd(SDCARD_READ_BLOCK, block);
504 ao_sdcard_recv_reply(NULL, 0);
505 if (ret != SDCARD_STATUS_READY_STATE) {
507 WARN ("read block command failed %d status %02x\n", block, ret);
508 status = _ao_sdcard_send_status();
509 WARN ("\tstatus now %04x\n", status);
513 ao_sdcard_send_fixed(0xff, 1);
515 /* Wait for the data start block marker */
516 start_block = ao_sdcard_wait_block_start();
517 if (start_block != SDCARD_DATA_START_BLOCK) {
518 WARN ("wait block start failed %02x\n", start_block);
523 ao_sdcard_recv(data, 512);
524 ao_sdcard_recv(crc, 2);
526 ao_sdcard_deselect();
527 if (ret == SDCARD_STATUS_READY_STATE)
529 if (ret == SDCARD_STATUS_IDLE_STATE) {
530 ret = _ao_sdcard_reset();
531 if (ret != SDCARD_STATUS_READY_STATE)
539 if (ret != SDCARD_STATUS_READY_STATE)
540 WARN("read failed\n");
542 WARN("took %d tries to read %d\n", tries + 1, block);
545 DBG("read %s\n", ret == SDCARD_STATUS_READY_STATE ? "success" : "failure");
546 return ret == SDCARD_STATUS_READY_STATE;
550 * Write a block of 512 bytes to the card
553 ao_sdcard_write_block(uint32_t block, uint8_t *data)
557 uint8_t start_block[8];
559 static uint8_t check_data[512];
567 if (sdtype != ao_sdtype_unknown)
574 DBG("write block %d\n", block);
575 if (sdtype != ao_sdtype_sd2block)
580 for (tries = 0; tries < 10; tries++) {
583 ret = ao_sdcard_send_cmd(SDCARD_WRITE_BLOCK, block);
584 ao_sdcard_recv_reply(NULL, 0);
585 if (ret != SDCARD_STATUS_READY_STATE)
588 /* Write a pad byte followed by the data start block marker */
589 start_block[0] = 0xff;
590 start_block[1] = SDCARD_DATA_START_BLOCK;
591 ao_sdcard_send(start_block, 2);
594 ao_sdcard_send(data, 512);
597 ao_sdcard_send_fixed(0xff, 2);
599 /* See if the card liked the data */
600 ao_sdcard_recv(response, sizeof (response));
601 if ((response[0] & SDCARD_DATA_RES_MASK) != SDCARD_DATA_RES_ACCEPTED) {
603 WARN("Data not accepted, response");
604 for (i = 0; i < sizeof (response); i++)
605 WARN(" %02x", response[i]);
611 /* Wait for the bus to go idle (should be done with an interrupt?) */
612 if (!ao_sdcard_wait_busy()) {
617 /* Check the current status after the write completes */
618 status = _ao_sdcard_send_status();
619 if ((status & 0xff) != SDCARD_STATUS_READY_STATE) {
620 WARN ("send status after write %04x\n", status);
625 ao_sdcard_deselect();
626 DBG("write %s\n", ret == SDCARD_STATUS_READY_STATE ? "success" : "failure");
627 if (ret == SDCARD_STATUS_READY_STATE)
633 WARN("took %d tries to write %d\n", tries + 1, block);
635 return ret == SDCARD_STATUS_READY_STATE;
639 static uint8_t test_data[512];
642 ao_sdcard_test_read(void)
647 if (ao_cmd_status != ao_cmd_success)
650 for (i = 0; i < 100; i++) {
651 printf ("."); flush();
652 if (!ao_sdcard_read_block(ao_cmd_lex_u32+i, test_data)) {
653 printf ("read error %d\n", i);
658 for (i = 0; i < 18; i++)
659 printf (" %02x", test_data[i]);
664 ao_sdcard_test_write(void)
668 for (i = 0; i < 16; i++) {
670 printf (" %02x", test_data[i]);
673 if (!ao_sdcard_write_block(1, test_data)) {
674 printf ("write error\n");
679 static const struct ao_cmds ao_sdcard_cmds[] = {
680 { ao_sdcard_test_read, "x\0Test read" },
681 { ao_sdcard_test_write, "y\0Test read" },
689 ao_spi_init_cs(AO_SDCARD_SPI_CS_PORT, (1 << AO_SDCARD_SPI_CS_PIN));
691 ao_cmd_register(&ao_sdcard_cmds[0]);