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)
31 static uint8_t initialized;
32 static uint8_t present;
34 static enum ao_sdtype sdtype;
36 #define ao_sdcard_lock() ao_mutex_get(&mutex)
37 #define ao_sdcard_unlock() ao_mutex_put(&mutex)
40 #define DBG(...) printf(__VA_ARGS__)
46 * Send an SD command and await the status reply
50 ao_sdcard_send_cmd(uint8_t cmd, uint32_t arg)
56 DBG ("\tsend_cmd %d arg %08x\n", cmd, arg);
57 if (cmd != SDCARD_GO_IDLE_STATE) {
58 for (i = 0; i < SDCARD_CMD_TIMEOUT; i++) {
59 ao_sdcard_recv(&reply, 1);
63 if (i == SDCARD_CMD_TIMEOUT)
64 return SDCARD_STATUS_TIMEOUT;
67 data[0] = cmd & 0x3f | 0x40;
72 if (cmd == SDCARD_GO_IDLE_STATE)
73 data[5] = 0x95; /* Valid for 0 arg */
74 else if (cmd == SDCARD_SEND_IF_COND)
75 data[5] = 0x87; /* Valid for 0x1aa arg */
77 data[5] = 0xff; /* no CRC */
78 ao_sdcard_send(data, 6);
80 /* The first reply byte will be the status,
81 * which must have the high bit clear
83 for (i = 0; i < SDCARD_CMD_TIMEOUT; i++) {
84 ao_sdcard_recv(&reply, 1);
85 DBG ("\t\tgot byte %02x\n", reply);
86 if ((reply & 0x80) == 0)
89 return SDCARD_STATUS_TIMEOUT;
93 * Retrieve any reply, discarding the trailing CRC byte
96 ao_sdcard_recv_reply(uint8_t *reply, int len)
101 ao_sdcard_recv(reply, len);
103 ao_sdcard_recv(&discard, 1);
107 * Wait while the card is busy. The
108 * card will return a stream of 0xff
109 * until it isn't busy anymore
112 ao_sdcard_wait_busy(void)
117 ao_sdcard_recv(&v, 1);
119 ao_sdcard_send_fixed(0xff, 1);
123 ao_sdcard_go_idle_state(void)
127 DBG ("go_idle_state\n");
129 ret = ao_sdcard_send_cmd(SDCARD_GO_IDLE_STATE, 0);
130 ao_sdcard_recv_reply(NULL, 0);
131 ao_sdcard_deselect();
132 DBG ("\tgo_idle_state status %02x\n", ret);
137 ao_sdcard_send_op_cond(void)
141 DBG ("send_op_cond\n");
143 ret = ao_sdcard_send_cmd(SDCARD_SEND_OP_COND, 0);
144 ao_sdcard_recv_reply(NULL, 0);
145 ao_sdcard_deselect();
146 DBG ("\tsend_op_cond %02x\n", ret);
151 ao_sdcard_send_if_cond(uint32_t arg, uint8_t send_if_cond_response[4])
155 DBG ("send_if_cond\n");
157 ret = ao_sdcard_send_cmd(SDCARD_SEND_IF_COND, arg);
158 if (ret != SDCARD_STATUS_IDLE_STATE) {
159 DBG ("\tsend_if_cond failed %02x\n", ret);
162 ao_sdcard_recv_reply(send_if_cond_response, 4);
163 DBG ("send_if_cond status %02x response %02x %02x %02x %02x\n",
165 send_if_cond_response[0],
166 send_if_cond_response[1],
167 send_if_cond_response[2],
168 send_if_cond_response[3]);
169 ao_sdcard_deselect();
174 ao_sdcard_set_blocklen(uint32_t blocklen)
178 DBG ("set_blocklen %d\n", blocklen);
180 ret = ao_sdcard_send_cmd(SDCARD_SET_BLOCKLEN, blocklen);
181 ao_sdcard_recv_reply(NULL, 0);
182 if (ret != SDCARD_STATUS_READY_STATE)
183 DBG ("\tsend_if_cond failed %02x\n", ret);
189 ao_sdcard_app_cmd(void)
195 ret = ao_sdcard_send_cmd(SDCARD_APP_CMD, 0);
196 ao_sdcard_recv_reply(NULL, 0);
197 ao_sdcard_deselect();
198 DBG ("\tapp_cmd status %02x\n");
203 ao_sdcard_app_send_op_cond(uint32_t arg)
207 ret = ao_sdcard_app_cmd();
208 if (ret != SDCARD_STATUS_IDLE_STATE)
210 DBG("send_op_comd\n");
212 ret = ao_sdcard_send_cmd(SDCARD_APP_SEND_OP_COMD, arg);
213 ao_sdcard_recv_reply(NULL, 0);
214 ao_sdcard_deselect();
215 DBG ("\tapp_send_op_cond status %02x\n", ret);
220 ao_sdcard_read_ocr(uint8_t read_ocr_response[4])
226 ret = ao_sdcard_send_cmd(SDCARD_READ_OCR, 0);
227 if (ret != SDCARD_STATUS_READY_STATE)
228 DBG ("\tread_ocr failed %02x\n", ret);
230 ao_sdcard_recv_reply(read_ocr_response, 4);
231 DBG ("\tread_ocr status %02x response %02x %02x %02x %02x\n", ret,
232 read_ocr_response[0], read_ocr_response[1],
233 read_ocr_response[2], read_ocr_response[3]);
235 ao_sdcard_deselect();
240 ao_sdcard_setup(void)
244 uint8_t response[10];
246 DBG ("Testing sdcard\n");
248 ao_sdcard_get_slow();
250 * min 74 clocks with CS high
252 ao_sdcard_send_fixed(0xff, 10);
254 ao_delay(AO_MS_TO_TICKS(10));
256 /* Reset the card and get it into SPI mode */
258 for (i = 0; i < SDCARD_IDLE_WAIT; i++) {
259 if (ao_sdcard_go_idle_state() == SDCARD_STATUS_IDLE_STATE)
262 if (i == SDCARD_IDLE_WAIT)
265 /* Figure out what kind of card we have */
267 sdtype = ao_sdtype_unknown;
269 if (ao_sdcard_send_if_cond(0x1aa, response) == SDCARD_STATUS_IDLE_STATE) {
273 /* Check for SD version 2 */
274 if ((response[2] & 0xf) == 1 && response[3] == 0xaa) {
279 for (i = 0; i < SDCARD_IDLE_WAIT; i++) {
280 ret = ao_sdcard_app_send_op_cond(arg);
281 if (ret != SDCARD_STATUS_IDLE_STATE)
284 if (ret != SDCARD_STATUS_READY_STATE) {
286 for (i = 0; i < SDCARD_IDLE_WAIT; i++) {
287 ret = ao_sdcard_send_op_cond();
288 if (ret != SDCARD_STATUS_IDLE_STATE)
291 if (ret != SDCARD_STATUS_READY_STATE)
293 sdtype = ao_sdtype_mmc3;
297 ret = ao_sdcard_read_ocr(response);
298 if (ret != SDCARD_STATUS_READY_STATE)
300 if ((response[0] & 0xc0) == 0xc0)
301 sdtype = ao_sdtype_sd2block;
303 sdtype = ao_sdtype_sd2byte;
305 sdtype = ao_sdtype_sd1;
309 /* For everything but SDHC cards, set the block length */
310 if (sdtype != ao_sdtype_sd2block) {
311 ret = ao_sdcard_set_blocklen(512);
312 if (ret != SDCARD_STATUS_READY_STATE)
313 DBG ("set_blocklen failed, ignoring\n");
317 DBG ("SD card detected, type %d\n", sdtype);
323 ao_sdcard_wait_block_start(void)
328 DBG ("\twait_block_start\n");
329 for (i = 0; i < SDCARD_BLOCK_TIMEOUT; i++) {
330 ao_sdcard_recv(&v, 1);
331 DBG("\t\trecv %02x\n", v);
339 * Read a block of 512 bytes from the card
342 ao_sdcard_read_block(uint32_t block, uint8_t *data)
351 if (sdtype != ao_sdtype_unknown)
358 if (sdtype != ao_sdtype_sd2block)
362 ret = ao_sdcard_send_cmd(SDCARD_READ_BLOCK, block);
363 ao_sdcard_recv_reply(NULL, 0);
364 if (ret != SDCARD_STATUS_READY_STATE)
367 if (ao_sdcard_wait_block_start() != 0xfe) {
372 ao_sdcard_recv(data, 512);
373 ao_sdcard_recv(crc, 2);
375 ao_sdcard_deselect();
378 return ret == SDCARD_STATUS_READY_STATE;
382 * Write a block of 512 bytes to the card
385 ao_sdcard_write_block(uint32_t block, uint8_t *data)
387 /* Not doing anything until the file system code seems reasonable
395 ao_spi_init_cs(AO_SDCARD_SPI_CS_PORT, (1 << AO_SDCARD_SPI_CS_PIN));