50 #include "dev/watchdog.h"
51 #include <util/delay.h>
57 #define PRINTD(...) printf(__VA_ARGS__)
63 #define PRINTF(...) printf(__VA_ARGS__)
68 #define SD_DATA_HIGH 0xFF
80 #define SDCARD_CMD10 10
82 #define SDCARD_CMD13 13
85 #define SDCARD_CMD16 16
88 #define SDCARD_CMD17 17
90 #define SDCARD_CMD18 18
93 #define SDCARD_CMD24 24
95 #define SDCARD_CMD25 25
98 #define SDCARD_CMD27 27
101 #define SDCARD_CMD32 32
103 #define SDCARD_CMD33 33
105 #define SDCARD_CMD38 38
108 #define SDCARD_CMD58 58
110 #define SDCARD_CMD59 59
113 #define SDCARD_CMD55 55
117 #define SDCARD_ACMD23 (IS_ACMD | 23)
119 #define SDCARD_ACMD41 (IS_ACMD | 41)
122 #define SDCARD_RESP1 0x01
123 #define SDCARD_RESP2 0x02
124 #define SDCARD_RESP3 0x03
125 #define SDCARD_RESP7 0x07
128 #define SD_R1_IN_IDLE_STATE 0x01
129 #define SD_R1_ERASE_RESET 0x02
130 #define SD_R1_ILLEGAL_CMD 0x04
131 #define SD_R1_COM_CRC_ERR 0x08
132 #define SD_R1_ERASE_SEQ_ERR 0x10
133 #define SD_R1_ADDR_ERR 0x20
134 #define SD_R1_PARAM_ERR 0x40
137 #define R2_OUT_OF_RANGE 0x80
138 #define R2_ERASE_PARAM 0x40
139 #define R2_WP_VIOLATION 0x20
140 #define R2_EEC_FAILED 0x10
141 #define R2_CC_ERROR 0x08
142 #define R2_ERROR 0x04
143 #define R2_ERASE_SKIP 0x02
144 #define R2_CARD_LOCKED 0x01
147 #define OCR_H_CCS 0x40
148 #define OCR_H_POWER_UP 0x80
151 #define DATA_RESP_ACCEPTED 0x05
152 #define DATA_RESP_CRC_REJECT 0x0B
153 #define DATA_RESP_WRITE_ERROR 0x0D
157 #define START_BLOCK_TOKEN 0xFE
159 #define MULTI_START_BLOCK_TOKEN 0xFC
160 #define STOP_TRAN_TOKEN 0xFD
163 #define DATA_ERROR 0x01
164 #define DATA_CC_ERROR 0x02
165 #define DATA_CARD_ECC_FAILED 0x04
166 #define DATA_OUT_OF_RANGE 0x08
172 #define BUSY_WAIT_MS 300
180 static uint16_t sdcard_block_size = 512;
185 static uint32_t sdcard_card_block_count = 0;
190 static uint8_t sdcard_sdsc_card = 1;
195 static uint8_t sdcard_crc_enable = 0;
197 static void get_csd_info(uint8_t *csd);
202 static uint8_t sdcard_busy_wait();
207 print_r1_resp(uint8_t cmd, uint8_t rsp) {
210 PRINTF(
"\nCMD%d: ", cmd & ~IS_ACMD);
214 }
else if (rsp == SD_R1_IN_IDLE_STATE) {
215 PRINTF(
"\nCMD%d: ", cmd & ~IS_ACMD);
220 PRINTD(
"\nCMD%d: ", cmd & ~IS_ACMD);
221 if (rsp & SD_R1_IN_IDLE_STATE) {
224 if (rsp & SD_R1_ERASE_RESET) {
225 PRINTD(
"\n\tErase Reset");
227 if (rsp & SD_R1_ILLEGAL_CMD) {
228 PRINTD(
"\n\tIllegal Cmd");
230 if (rsp & SD_R1_COM_CRC_ERR) {
231 PRINTD(
"\n\tCom CRC Err");
233 if (rsp & SD_R1_ERASE_SEQ_ERR) {
234 PRINTD(
"\n\tErase Seq Err");
236 if (rsp & SD_R1_ADDR_ERR) {
237 PRINTD(
"\n\tAddr Err");
239 if (rsp & SD_R1_PARAM_ERR) {
240 PRINTD(
"\n\tParam Err");
246 dbg_resp_r1(uint8_t cmd, uint8_t rsp) {
247 print_r1_resp(cmd, rsp);
251 dbg_resp_r2(uint8_t cmd, uint8_t* rsp) {
252 print_r1_resp(cmd, rsp[0]);
253 if(rsp[1] & R2_OUT_OF_RANGE) {
254 PRINTD(
"\nOut of Range");
256 if(rsp[1] & R2_ERASE_PARAM) {
257 PRINTD(
"\nErase param");
259 if(rsp[1] & R2_WP_VIOLATION) {
260 PRINTD(
"\nWP violation");
262 if(rsp[1] & R2_EEC_FAILED) {
263 PRINTD(
"\nEEC failed");
265 if(rsp[1] & R2_CC_ERROR) {
266 PRINTD(
"\nCC error");
268 if(rsp[1] & R2_ERROR) {
271 if(rsp[1] & R2_ERASE_SKIP) {
272 PRINTD(
"\nErase skip");
274 if(rsp[1] & R2_CARD_LOCKED) {
275 PRINTD(
"\nCard locked");
280 dbg_resp_r3(uint8_t cmd, uint8_t* rsp) {
281 print_r1_resp(cmd, rsp[0]);
282 PRINTF(
"\nOCR: 0x%04x", rsp[1]);
286 dbg_resp_r7(uint8_t cmd, uint8_t* rsp) {
287 print_r1_resp(cmd, rsp[0]);
288 PRINTF(
"\nR7_DATA: 0x%04x", rsp[1]);
292 dbg_data_err(uint8_t ret) {
293 if ((ret & 0xF0) == 0) {
294 if(ret & DATA_ERROR) {
297 if(ret & DATA_CC_ERROR) {
298 PRINTD(
"\nCC Error");
300 if(ret & DATA_CARD_ECC_FAILED) {
301 PRINTD(
"\nEEC Failed");
303 if(ret & DATA_OUT_OF_RANGE) {
304 PRINTD(
"\nOut of range");
311 #define dbg_resp_r1(a, b)
312 #define dbg_resp_r2(a, b)
313 #define dbg_resp_r3(a, b)
314 #define dbg_resp_r7(a, b)
315 #define dbg_data_err(ret)
321 return ((uint64_t) sdcard_card_block_count) * sdcard_block_size;
339 return sdcard_sdsc_card;
346 uint32_t arg = enable ? 1L : 0L;
351 PRINTD(
"\nsdcard_set_CRC(): ret = %u", ret);
355 sdcard_crc_enable = enable;
377 PRINTD(
"\nsdcard_read_csd(): No data token");
383 if (ret != START_BLOCK_TOKEN) {
389 for (i = 0; i < 16; i++) {
409 get_csd_info(uint8_t *csd)
411 uint8_t csd_version = 0;
412 uint8_t READ_BL_LEN = 0;
414 uint16_t C_SIZE_MULT = 0;
416 uint8_t SECTOR_SIZE = 0;
417 uint32_t capacity = 0;
421 csd_version = csd[0] >> 6;
424 if (csd_version == 0) {
426 READ_BL_LEN = csd[5] & 0x0F;
428 C_SIZE = (((uint16_t) (csd[6] & 0x03)) << 10) | (((uint16_t) csd[7]) << 2) | (csd[8] >> 6);
430 C_SIZE_MULT = ((csd[9] & 0x03) << 1) | (csd[10] >> 7);
432 SECTOR_SIZE = ((csd[11] >> 7) | ((csd[10] & 0x3F) << 1)) + 1;
434 mult = (1 << (C_SIZE_MULT + 2));
435 sdcard_card_block_count = (C_SIZE + 1) * mult;
436 sdcard_block_size = 1 << READ_BL_LEN;
437 capacity = sdcard_card_block_count * sdcard_block_size / 1024 / 1024;
440 }
else if (csd_version == 1) {
442 READ_BL_LEN = csd[5] & 0x0F;
444 C_SIZE = csd[9] + (((uint32_t) csd[8]) << 8) + (((uint32_t) csd[7] & 0x3f) << 16);
446 SECTOR_SIZE = ((csd[11] >> 7) | ((csd[10] & 0x3F) << 1)) + 1;
448 sdcard_card_block_count = (C_SIZE + 1) * 1024;
449 sdcard_block_size = 512;
450 capacity = C_SIZE * 512 / 1024;
452 PRINTD(
"\nInvalid CSD version");
456 PRINTF(
"\nget_csd_info(): CSD Version = %u", csd_version);
457 PRINTF(
"\nget_csd_info(): READ_BL_LEN = %u", READ_BL_LEN);
458 PRINTF(
"\nget_csd_info(): C_SIZE = %lu", C_SIZE);
459 PRINTF(
"\nget_csd_info(): Capacity = %lu MB", capacity);
460 PRINTF(
"\nget_csd_info(): C_SIZE_MULT = %u", C_SIZE_MULT);
461 PRINTF(
"\nget_csd_info(): mult = %lu", mult);
462 PRINTF(
"\nget_csd_info(): sdcard_card_block_count = %lu", sdcard_card_block_count);
463 PRINTF(
"\nget_csd_info(): sdcard_block_size = %u", sdcard_block_size);
464 PRINTF(
"\nget_csd_info(): SECTOR_SIZE = %u", SECTOR_SIZE);
477 uint32_t stream = (((uint32_t) cmd[0]) << 24) +
478 (((uint32_t) cmd[1]) << 16) +
479 (((uint32_t) cmd[2]) << 8) +
482 uint32_t gen = ((uint32_t) 0x89) << 24;
484 for (i = 0; i < 40; i++) {
485 if (stream & (((uint32_t) 0x80) << 24)) {
489 stream = stream << 1;
495 cmd[5] = (stream >> 24) + 1;
501 uint32_t stream = (((uint32_t) data[0]) << 24) +
502 (((uint32_t) data[1]) << 16) +
503 (((uint32_t) data[2]) << 8) +
504 ((uint32_t) data[3]);
506 uint32_t gen = ((uint32_t) 0x11021) << 15;
509 for (i = 0; i < 4096; i++) {
510 if (stream & (((uint32_t) 0x80) << 24)) {
514 stream = stream << 1;
515 if (((i + 1) % 8) == 0 && i < 4064) {
516 stream += data[((i + 1) / 8) + 3];
520 return (stream >> 16);
528 sdcard_sdsc_card = 1;
530 uint32_t cmd_arg = 0;
532 uint8_t resp[5] = {0x00, 0x00, 0x00, 0x00, 0x00};
546 for (i = 0; i < 16; i++) {
557 PRINTD(
"\nsdcard_init(): cmd0 timeout -> %d", ret);
566 cmd_arg = 0x000001AAUL;
567 resp[0] = SDCARD_RESP7;
569 if ((ret & SD_R1_ILLEGAL_CMD) && ret != 0xFF) {
570 PRINTF(
"\nsdcard_init(): cmd8 not supported -> Ver1.X or no SD Memory Card");
576 PRINTD(
"\nsdcard_init(): cmd8 timeout -> %d", ret);
581 if (ret == SD_R1_IN_IDLE_STATE) {
583 if (resp[3] != ((uint8_t) (cmd_arg >> 8) & 0x0F)) {
584 printf(
"\nsdcard_init(): Voltage not accepted");
588 if (resp[4] != ((uint8_t) (cmd_arg & 0xFF))) {
589 printf(
"\nsdcard_init(): Echo-back pattern wrong, sent %02x, rec: %02x",
590 (uint8_t) (cmd_arg & 0xFF), resp[4]);
596 if (ret & SD_R1_ILLEGAL_CMD) {
603 PRINTD(
"\nsdcard_init(): cmd1 timeout reached, last return value was %d", ret);
612 cmd_arg = 0x00000200UL;
616 PRINTF(
"\nsdcard_init(): cmd16 timeout reached, last return value was %d", ret);
627 cmd_arg = 0x40000000UL;
633 PRINTD(
"\nsdcard_init(): acmd41 timeout reached, last return value was %u", ret);
641 resp[0] = SDCARD_RESP3;
645 PRINTD(
"\nsdcard_init(): cmd58 timeout reached, last return value was %d", ret);
652 if (resp[1] & OCR_H_POWER_UP) {
653 PRINTF(
"\nsdcard_init(): acmd41: Card power up okay!");
655 if (resp[1] & OCR_H_CCS) {
656 sdcard_sdsc_card = 0;
657 PRINTF(
"\nsdcard_init(): acmd41: Card is SDHC/SDXC");
659 PRINTF(
"\nsdcard_init(): acmd41: Card is SDSC");
671 PRINTD(
"\nsdcard_init(): CSD read error");
693 if (sdcard_sdsc_card) {
694 startaddr = startaddr << 9;
695 endaddr = endaddr << 9;
703 PRINTD(
"\nCMD32 failed");
710 PRINTD(
"\nCMD33 failed");
717 PRINTD(
"\nCMD38 failed");
739 if (sdcard_sdsc_card) {
751 PRINTD(
"\nsdcard_read_block(): CMD17 failure! (%u)", i);
759 PRINTD(
"\nsdcard_read_block(): No Start Byte recieved, last was %d", ret);
764 if (ret != START_BLOCK_TOKEN) {
772 for (i = 0; i < 512; i++) {
788 sdcard_get_status(
void)
790 uint8_t resp[5] = {SDCARD_RESP2, 0x00, 0x00, 0x00, 0x00};
797 printf(
"\nFailed to read status");
801 return ((uint16_t) resp[1] << 8) + ((uint16_t) resp[0]);
815 if (sdcard_sdsc_card) {
836 for (i = 0; i < 512; i++) {
857 if (i != DATA_RESP_ACCEPTED) {
859 if (i == DATA_RESP_CRC_REJECT) {
860 PRINTD(
"\nWrite response: CRC error");
861 }
else if (i == DATA_RESP_WRITE_ERROR) {
862 PRINTD(
"\nData Resp: Write error");
864 PRINTD(
"\nData Resp %02x unknown", i);
883 if (sdcard_sdsc_card) {
893 if (num_blocks != 0) {
896 PRINTF(
"\nPre-erasing %ld blocks", num_blocks);
925 for (i = 0; i < 512; i++) {
943 if (i != DATA_RESP_ACCEPTED) {
945 if (i == DATA_RESP_CRC_REJECT) {
946 PRINTD(
"\nWrite response: CRC error");
947 }
else if (i == DATA_RESP_WRITE_ERROR) {
948 PRINTD(
"\nData Resp: Write error");
950 PRINTD(
"\nData Resp %02x unknown", i);
989 if (i >= BUSY_WAIT_MS) {
990 PRINTD(
"\nsdcard_busy_wait(): Busy wait timeout");
1005 uint8_t resp_type = 0x01;
1006 static uint8_t cmd_seq[6] = {0x40, 0x00, 0x00, 0x00, 0x00, 0xFF};
1010 if (cmd & IS_ACMD) {
1021 cmd_seq[0] = (cmd & ~IS_ACMD) | 0x40;
1024 resp_type = resp[0];
1034 cmd_seq[1] = ((*arg & 0xFF000000) >> 24);
1035 cmd_seq[2] = ((*arg & 0x00FF0000) >> 16);
1036 cmd_seq[3] = ((*arg & 0x0000FF00) >> 8);
1037 cmd_seq[4] = ((*arg & 0x000000FF));
1044 }
else if ((sdcard_crc_enable) || (cmd ==
SDCARD_CMD8)) {
1052 for (i = 0; i < 6; i++) {
1066 if ((data == 0xFF) && (idx == 0)) {
1072 dbg_resp_r1(cmd, data);
1076 switch (resp_type) {
1080 dbg_resp_r1(cmd, data);
1087 if ((idx >= 2) || (resp[0] & 0xFE) != 0) {
1088 dbg_resp_r2(cmd, resp);
1098 if ((idx >= 5) || (resp[0] & 0xFE) != 0) {
1099 if (resp_type == SDCARD_RESP3) {
1100 dbg_resp_r3(cmd, resp);
1102 dbg_resp_r7(cmd, resp);
1113 PRINTD(
"\nResponse timeout");