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 #define SDCARD_DEBUG 0
32 static uint8_t initialized;
33 static uint8_t present;
35 static enum ao_sdtype sdtype;
37 #define ao_sdcard_lock() ao_mutex_get(&mutex)
38 #define ao_sdcard_unlock() ao_mutex_put(&mutex)
41 #define DBG(...) printf(__VA_ARGS__)
47 * Send an SD command and await the status reply
51 ao_sdcard_send_cmd(uint8_t cmd, uint32_t arg)
57 DBG ("\tsend_cmd %d arg %08x\n", cmd, arg);
58 if (cmd != SDCARD_GO_IDLE_STATE) {
59 for (i = 0; i < SDCARD_CMD_TIMEOUT; i++) {
60 ao_sdcard_recv(&reply, 1);
64 if (i == SDCARD_CMD_TIMEOUT)
65 return SDCARD_STATUS_TIMEOUT;
68 data[0] = cmd & 0x3f | 0x40;
73 if (cmd == SDCARD_GO_IDLE_STATE)
74 data[5] = 0x95; /* Valid for 0 arg */
75 else if (cmd == SDCARD_SEND_IF_COND)
76 data[5] = 0x87; /* Valid for 0x1aa arg */
78 data[5] = 0xff; /* no CRC */
79 ao_sdcard_send(data, 6);
81 /* The first reply byte will be the status,
82 * which must have the high bit clear
84 for (i = 0; i < SDCARD_CMD_TIMEOUT; i++) {
85 ao_sdcard_recv(&reply, 1);
86 DBG ("\t\tgot byte %02x\n", reply);
87 if ((reply & 0x80) == 0)
90 return SDCARD_STATUS_TIMEOUT;
94 * Retrieve any reply, discarding the trailing CRC byte
97 ao_sdcard_recv_reply(uint8_t *reply, int len)
102 ao_sdcard_recv(reply, len);
104 ao_sdcard_recv(&discard, 1);
108 * Wait while the card is busy. The
109 * card will return a stream of 0xff
110 * until it isn't busy anymore
113 ao_sdcard_wait_busy(void)
118 ao_sdcard_recv(&v, 1);
120 ao_sdcard_send_fixed(0xff, 1);
124 ao_sdcard_go_idle_state(void)
128 DBG ("go_idle_state\n");
130 ret = ao_sdcard_send_cmd(SDCARD_GO_IDLE_STATE, 0);
131 ao_sdcard_recv_reply(NULL, 0);
132 ao_sdcard_deselect();
133 DBG ("\tgo_idle_state status %02x\n", ret);
138 ao_sdcard_send_op_cond(void)
142 DBG ("send_op_cond\n");
144 ret = ao_sdcard_send_cmd(SDCARD_SEND_OP_COND, 0);
145 ao_sdcard_recv_reply(NULL, 0);
146 ao_sdcard_deselect();
147 DBG ("\tsend_op_cond %02x\n", ret);
152 ao_sdcard_send_if_cond(uint32_t arg, uint8_t send_if_cond_response[4])
156 DBG ("send_if_cond\n");
158 ret = ao_sdcard_send_cmd(SDCARD_SEND_IF_COND, arg);
159 if (ret != SDCARD_STATUS_IDLE_STATE) {
160 DBG ("\tsend_if_cond failed %02x\n", ret);
163 ao_sdcard_recv_reply(send_if_cond_response, 4);
164 DBG ("send_if_cond status %02x response %02x %02x %02x %02x\n",
166 send_if_cond_response[0],
167 send_if_cond_response[1],
168 send_if_cond_response[2],
169 send_if_cond_response[3]);
170 ao_sdcard_deselect();
175 ao_sdcard_send_status(void)
179 DBG ("send_status\n");
181 ret = ao_sdcard_send_cmd(SDCARD_SEND_STATUS, 0);
182 ao_sdcard_recv_reply(NULL, 0);
183 if (ret != SDCARD_STATUS_READY_STATE)
184 DBG ("\tsend_if_cond failed %02x\n", ret);
189 ao_sdcard_set_blocklen(uint32_t blocklen)
193 DBG ("set_blocklen %d\n", blocklen);
195 ret = ao_sdcard_send_cmd(SDCARD_SET_BLOCKLEN, blocklen);
196 ao_sdcard_recv_reply(NULL, 0);
197 if (ret != SDCARD_STATUS_READY_STATE)
198 DBG ("\tsend_if_cond failed %02x\n", ret);
203 ao_sdcard_app_cmd(void)
209 ret = ao_sdcard_send_cmd(SDCARD_APP_CMD, 0);
210 ao_sdcard_recv_reply(NULL, 0);
211 ao_sdcard_deselect();
212 DBG ("\tapp_cmd status %02x\n");
217 ao_sdcard_app_send_op_cond(uint32_t arg)
221 ret = ao_sdcard_app_cmd();
222 if (ret != SDCARD_STATUS_IDLE_STATE)
224 DBG("send_op_comd\n");
226 ret = ao_sdcard_send_cmd(SDCARD_APP_SEND_OP_COMD, arg);
227 ao_sdcard_recv_reply(NULL, 0);
228 ao_sdcard_deselect();
229 DBG ("\tapp_send_op_cond status %02x\n", ret);
234 ao_sdcard_read_ocr(uint8_t read_ocr_response[4])
240 ret = ao_sdcard_send_cmd(SDCARD_READ_OCR, 0);
241 if (ret != SDCARD_STATUS_READY_STATE)
242 DBG ("\tread_ocr failed %02x\n", ret);
244 ao_sdcard_recv_reply(read_ocr_response, 4);
245 DBG ("\tread_ocr status %02x response %02x %02x %02x %02x\n", ret,
246 read_ocr_response[0], read_ocr_response[1],
247 read_ocr_response[2], read_ocr_response[3]);
249 ao_sdcard_deselect();
254 ao_sdcard_setup(void)
258 uint8_t response[10];
260 DBG ("Testing sdcard\n");
262 ao_sdcard_get_slow();
264 * min 74 clocks with CS high
266 ao_sdcard_send_fixed(0xff, 10);
268 ao_delay(AO_MS_TO_TICKS(10));
270 /* Reset the card and get it into SPI mode */
272 for (i = 0; i < SDCARD_IDLE_WAIT; i++) {
273 if (ao_sdcard_go_idle_state() == SDCARD_STATUS_IDLE_STATE)
276 if (i == SDCARD_IDLE_WAIT)
279 /* Figure out what kind of card we have */
281 sdtype = ao_sdtype_unknown;
283 if (ao_sdcard_send_if_cond(0x1aa, response) == SDCARD_STATUS_IDLE_STATE) {
287 /* Check for SD version 2 */
288 if ((response[2] & 0xf) == 1 && response[3] == 0xaa) {
293 for (i = 0; i < SDCARD_IDLE_WAIT; i++) {
294 ret = ao_sdcard_app_send_op_cond(arg);
295 if (ret != SDCARD_STATUS_IDLE_STATE)
298 if (ret != SDCARD_STATUS_READY_STATE) {
300 for (i = 0; i < SDCARD_IDLE_WAIT; i++) {
301 ret = ao_sdcard_send_op_cond();
302 if (ret != SDCARD_STATUS_IDLE_STATE)
305 if (ret != SDCARD_STATUS_READY_STATE)
307 sdtype = ao_sdtype_mmc3;
311 ret = ao_sdcard_read_ocr(response);
312 if (ret != SDCARD_STATUS_READY_STATE)
314 if ((response[0] & 0xc0) == 0xc0)
315 sdtype = ao_sdtype_sd2block;
317 sdtype = ao_sdtype_sd2byte;
319 sdtype = ao_sdtype_sd1;
323 /* For everything but SDHC cards, set the block length */
324 if (sdtype != ao_sdtype_sd2block) {
325 ret = ao_sdcard_set_blocklen(512);
326 if (ret != SDCARD_STATUS_READY_STATE)
327 DBG ("set_blocklen failed, ignoring\n");
331 DBG ("SD card detected, type %d\n", sdtype);
337 ao_sdcard_wait_block_start(void)
342 DBG ("\twait_block_start\n");
343 for (i = 0; i < SDCARD_BLOCK_TIMEOUT; i++) {
344 ao_sdcard_recv(&v, 1);
345 DBG("\t\trecv %02x\n", v);
353 * Read a block of 512 bytes from the card
356 ao_sdcard_read_block(uint32_t block, uint8_t *data)
365 if (sdtype != ao_sdtype_unknown)
372 if (sdtype != ao_sdtype_sd2block)
376 ret = ao_sdcard_send_cmd(SDCARD_READ_BLOCK, block);
377 ao_sdcard_recv_reply(NULL, 0);
378 if (ret != SDCARD_STATUS_READY_STATE)
381 /* Wait for the data start block marker */
382 if (ao_sdcard_wait_block_start() != SDCARD_DATA_START_BLOCK) {
387 ao_sdcard_recv(data, 512);
388 ao_sdcard_recv(crc, 2);
390 ao_sdcard_deselect();
393 return ret == SDCARD_STATUS_READY_STATE;
397 * Write a block of 512 bytes to the card
400 ao_sdcard_write_block(uint32_t block, uint8_t *data)
404 uint8_t start_block[2];
411 if (sdtype != ao_sdtype_unknown)
418 if (sdtype != ao_sdtype_sd2block)
423 ret = ao_sdcard_send_cmd(SDCARD_WRITE_BLOCK, block);
424 ao_sdcard_recv_reply(NULL, 0);
425 if (ret != SDCARD_STATUS_READY_STATE)
428 /* Write a pad byte followed by the data start block marker */
429 start_block[0] = 0xff;
430 start_block[1] = SDCARD_DATA_START_BLOCK;
431 ao_sdcard_send(start_block, 2);
434 ao_sdcard_send(data, 512);
437 ao_sdcard_send_fixed(0xff, 2);
439 /* See if the card liked the data */
440 ao_sdcard_recv(&response, 1);
441 if ((response & SDCARD_DATA_RES_MASK) != SDCARD_DATA_RES_ACCEPTED) {
446 /* Wait for the bus to go idle (should be done with an interrupt) */
447 for (i = 0; i < SDCARD_IDLE_TIMEOUT; i++) {
448 ao_sdcard_recv(&response, 1);
449 if (response == 0xff)
452 if (i == SDCARD_IDLE_TIMEOUT)
455 ao_sdcard_deselect();
458 return ret == SDCARD_STATUS_READY_STATE;
462 static uint8_t test_data[512];
465 ao_sdcard_test_read(void)
468 if (!ao_sdcard_read_block(1, test_data)) {
469 printf ("read error\n");
473 for (i = 0; i < 18; i++)
474 printf (" %02x", test_data[i]);
479 ao_sdcard_test_write(void)
483 for (i = 0; i < 16; i++) {
485 printf (" %02x", test_data[i]);
488 if (!ao_sdcard_write_block(1, test_data)) {
489 printf ("write error\n");
494 static const struct ao_cmds ao_sdcard_cmds[] = {
495 { ao_sdcard_test_read, "x\0Test read" },
496 { ao_sdcard_test_write, "y\0Test read" },
504 ao_spi_init_cs(AO_SDCARD_SPI_CS_PORT, (1 << AO_SDCARD_SPI_CS_PIN));
506 ao_cmd_register(&ao_sdcard_cmds[0]);