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 0
34 #define SDCARD_TRACE 0
36 static uint8_t initialized;
37 static uint8_t present;
39 static enum ao_sdtype sdtype;
41 #define ao_sdcard_lock() ao_mutex_get(&mutex)
42 #define ao_sdcard_unlock() ao_mutex_put(&mutex)
45 #define DBG(...) printf(__VA_ARGS__)
51 * Send an SD command and await the status reply
55 ao_sdcard_send_cmd(uint8_t cmd, uint32_t arg)
61 DBG ("\tsend_cmd %d arg %08x\n", cmd, arg);
62 if (cmd != SDCARD_GO_IDLE_STATE) {
63 for (i = 0; i < SDCARD_CMD_TIMEOUT; i++) {
64 ao_sdcard_recv(&reply, 1);
68 if (i == SDCARD_CMD_TIMEOUT)
69 return SDCARD_STATUS_TIMEOUT;
72 data[0] = cmd & 0x3f | 0x40;
77 if (cmd == SDCARD_GO_IDLE_STATE)
78 data[5] = 0x95; /* Valid for 0 arg */
79 else if (cmd == SDCARD_SEND_IF_COND)
80 data[5] = 0x87; /* Valid for 0x1aa arg */
82 data[5] = 0xff; /* no CRC */
83 ao_sdcard_send(data, 6);
85 /* The first reply byte will be the status,
86 * which must have the high bit clear
88 for (i = 0; i < SDCARD_CMD_TIMEOUT; i++) {
89 ao_sdcard_recv(&reply, 1);
90 DBG ("\t\tgot byte %02x\n", reply);
91 if ((reply & 0x80) == 0)
94 return SDCARD_STATUS_TIMEOUT;
98 * Retrieve any reply, discarding the trailing CRC byte
101 ao_sdcard_recv_reply(uint8_t *reply, int len)
106 ao_sdcard_recv(reply, len);
108 ao_sdcard_recv(&discard, 1);
112 * Wait while the card is busy. The
113 * card will return a stream of 0xff
114 * until it isn't busy anymore
117 ao_sdcard_wait_busy(void)
122 ao_sdcard_recv(&v, 1);
124 ao_sdcard_send_fixed(0xff, 1);
128 ao_sdcard_go_idle_state(void)
132 DBG ("go_idle_state\n");
134 ret = ao_sdcard_send_cmd(SDCARD_GO_IDLE_STATE, 0);
135 ao_sdcard_recv_reply(NULL, 0);
136 ao_sdcard_deselect();
137 DBG ("\tgo_idle_state status %02x\n", ret);
142 ao_sdcard_send_op_cond(void)
146 DBG ("send_op_cond\n");
148 ret = ao_sdcard_send_cmd(SDCARD_SEND_OP_COND, 0);
149 ao_sdcard_recv_reply(NULL, 0);
150 ao_sdcard_deselect();
151 DBG ("\tsend_op_cond %02x\n", ret);
156 ao_sdcard_send_if_cond(uint32_t arg, uint8_t send_if_cond_response[4])
160 DBG ("send_if_cond\n");
162 ret = ao_sdcard_send_cmd(SDCARD_SEND_IF_COND, arg);
163 if (ret != SDCARD_STATUS_IDLE_STATE) {
164 DBG ("\tsend_if_cond failed %02x\n", ret);
167 ao_sdcard_recv_reply(send_if_cond_response, 4);
168 DBG ("send_if_cond status %02x response %02x %02x %02x %02x\n",
170 send_if_cond_response[0],
171 send_if_cond_response[1],
172 send_if_cond_response[2],
173 send_if_cond_response[3]);
174 ao_sdcard_deselect();
179 ao_sdcard_send_status(void)
183 DBG ("send_status\n");
185 ret = ao_sdcard_send_cmd(SDCARD_SEND_STATUS, 0);
186 ao_sdcard_recv_reply(NULL, 0);
187 if (ret != SDCARD_STATUS_READY_STATE)
188 DBG ("\tsend_if_cond failed %02x\n", ret);
193 ao_sdcard_set_blocklen(uint32_t blocklen)
197 DBG ("set_blocklen %d\n", blocklen);
199 ret = ao_sdcard_send_cmd(SDCARD_SET_BLOCKLEN, blocklen);
200 ao_sdcard_recv_reply(NULL, 0);
201 if (ret != SDCARD_STATUS_READY_STATE)
202 DBG ("\tsend_if_cond failed %02x\n", ret);
207 ao_sdcard_app_cmd(void)
213 ret = ao_sdcard_send_cmd(SDCARD_APP_CMD, 0);
214 ao_sdcard_recv_reply(NULL, 0);
215 ao_sdcard_deselect();
216 DBG ("\tapp_cmd status %02x\n");
221 ao_sdcard_app_send_op_cond(uint32_t arg)
225 ret = ao_sdcard_app_cmd();
226 if (ret != SDCARD_STATUS_IDLE_STATE)
228 DBG("send_op_comd\n");
230 ret = ao_sdcard_send_cmd(SDCARD_APP_SEND_OP_COMD, arg);
231 ao_sdcard_recv_reply(NULL, 0);
232 ao_sdcard_deselect();
233 DBG ("\tapp_send_op_cond status %02x\n", ret);
238 ao_sdcard_read_ocr(uint8_t read_ocr_response[4])
244 ret = ao_sdcard_send_cmd(SDCARD_READ_OCR, 0);
245 if (ret != SDCARD_STATUS_READY_STATE)
246 DBG ("\tread_ocr failed %02x\n", ret);
248 ao_sdcard_recv_reply(read_ocr_response, 4);
249 DBG ("\tread_ocr status %02x response %02x %02x %02x %02x\n", ret,
250 read_ocr_response[0], read_ocr_response[1],
251 read_ocr_response[2], read_ocr_response[3]);
253 ao_sdcard_deselect();
258 ao_sdcard_setup(void)
262 uint8_t response[10];
264 DBG ("Testing sdcard\n");
266 ao_sdcard_get_slow();
268 * min 74 clocks with CS high
270 ao_sdcard_send_fixed(0xff, 10);
272 ao_delay(AO_MS_TO_TICKS(10));
274 /* Reset the card and get it into SPI mode */
276 for (i = 0; i < SDCARD_IDLE_WAIT; i++) {
277 if (ao_sdcard_go_idle_state() == SDCARD_STATUS_IDLE_STATE)
280 if (i == SDCARD_IDLE_WAIT)
283 /* Figure out what kind of card we have */
285 sdtype = ao_sdtype_unknown;
287 if (ao_sdcard_send_if_cond(0x1aa, response) == SDCARD_STATUS_IDLE_STATE) {
291 /* Check for SD version 2 */
292 if ((response[2] & 0xf) == 1 && response[3] == 0xaa) {
297 for (i = 0; i < SDCARD_IDLE_WAIT; i++) {
298 ret = ao_sdcard_app_send_op_cond(arg);
299 if (ret != SDCARD_STATUS_IDLE_STATE)
302 if (ret != SDCARD_STATUS_READY_STATE) {
304 for (i = 0; i < SDCARD_IDLE_WAIT; i++) {
305 ret = ao_sdcard_send_op_cond();
306 if (ret != SDCARD_STATUS_IDLE_STATE)
309 if (ret != SDCARD_STATUS_READY_STATE)
311 sdtype = ao_sdtype_mmc3;
315 ret = ao_sdcard_read_ocr(response);
316 if (ret != SDCARD_STATUS_READY_STATE)
318 if ((response[0] & 0xc0) == 0xc0)
319 sdtype = ao_sdtype_sd2block;
321 sdtype = ao_sdtype_sd2byte;
323 sdtype = ao_sdtype_sd1;
327 /* For everything but SDHC cards, set the block length */
328 if (sdtype != ao_sdtype_sd2block) {
329 ret = ao_sdcard_set_blocklen(512);
330 if (ret != SDCARD_STATUS_READY_STATE)
331 DBG ("set_blocklen failed, ignoring\n");
335 DBG ("SD card detected, type %d\n", sdtype);
341 ao_sdcard_wait_block_start(void)
346 DBG ("\twait_block_start\n");
347 for (i = 0; i < SDCARD_BLOCK_TIMEOUT; i++) {
348 ao_sdcard_recv(&v, 1);
349 DBG("\t\trecv %02x\n", v);
357 * Read a block of 512 bytes from the card
360 ao_sdcard_read_block(uint32_t block, uint8_t *data)
369 if (sdtype != ao_sdtype_unknown)
376 DBG("read block %d\n", block);
377 if (sdtype != ao_sdtype_sd2block)
381 ret = ao_sdcard_send_cmd(SDCARD_READ_BLOCK, block);
382 ao_sdcard_recv_reply(NULL, 0);
383 if (ret != SDCARD_STATUS_READY_STATE)
386 /* Wait for the data start block marker */
387 if (ao_sdcard_wait_block_start() != SDCARD_DATA_START_BLOCK) {
392 ao_sdcard_recv(data, 512);
393 ao_sdcard_recv(crc, 2);
395 ao_sdcard_deselect();
398 DBG("read %s\n", ret == SDCARD_STATUS_READY_STATE ? "success" : "failure");
399 return ret == SDCARD_STATUS_READY_STATE;
403 * Write a block of 512 bytes to the card
406 ao_sdcard_write_block(uint32_t block, uint8_t *data)
410 uint8_t start_block[2];
417 if (sdtype != ao_sdtype_unknown)
424 DBG("write block %d\n", block);
425 if (sdtype != ao_sdtype_sd2block)
430 ret = ao_sdcard_send_cmd(SDCARD_WRITE_BLOCK, block);
431 ao_sdcard_recv_reply(NULL, 0);
432 if (ret != SDCARD_STATUS_READY_STATE)
435 /* Write a pad byte followed by the data start block marker */
436 start_block[0] = 0xff;
437 start_block[1] = SDCARD_DATA_START_BLOCK;
438 ao_sdcard_send(start_block, 2);
441 ao_sdcard_send(data, 512);
444 ao_sdcard_send_fixed(0xff, 2);
446 /* See if the card liked the data */
447 ao_sdcard_recv(&response, 1);
448 if ((response & SDCARD_DATA_RES_MASK) != SDCARD_DATA_RES_ACCEPTED) {
453 /* Wait for the bus to go idle (should be done with an interrupt) */
454 for (i = 0; i < SDCARD_IDLE_TIMEOUT; i++) {
455 ao_sdcard_recv(&response, 1);
456 if (response == 0xff)
459 if (i == SDCARD_IDLE_TIMEOUT)
462 ao_sdcard_deselect();
465 DBG("write %s\n", ret == SDCARD_STATUS_READY_STATE ? "success" : "failure");
466 return ret == SDCARD_STATUS_READY_STATE;
470 static uint8_t test_data[512];
473 ao_sdcard_test_read(void)
476 if (!ao_sdcard_read_block(1, test_data)) {
477 printf ("read error\n");
481 for (i = 0; i < 18; i++)
482 printf (" %02x", test_data[i]);
487 ao_sdcard_test_write(void)
491 for (i = 0; i < 16; i++) {
493 printf (" %02x", test_data[i]);
496 if (!ao_sdcard_write_block(1, test_data)) {
497 printf ("write error\n");
502 static const struct ao_cmds ao_sdcard_cmds[] = {
503 { ao_sdcard_test_read, "x\0Test read" },
504 { ao_sdcard_test_write, "y\0Test read" },
512 ao_spi_init_cs(AO_SDCARD_SPI_CS_PORT, (1 << AO_SDCARD_SPI_CS_PIN));
514 ao_cmd_register(&ao_sdcard_cmds[0]);