2
0

sdcard.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 2010-2021 H. Peter Anvin - All Rights Reserved
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  8. * Boston MA 02110-1301, USA; either version 2 of the License, or
  9. * (at your option) any later version; incorporated herein by reference.
  10. *
  11. * ----------------------------------------------------------------------- */
  12. /*
  13. * sdcard.c
  14. *
  15. * SD card block driver
  16. *
  17. * Note: the handling of read operations is tricky, because they pick up
  18. * the results from the *previous* transaction. Therefore there are some
  19. * serious subtleties, especially with which byte position in the shift
  20. * register the result ends up in and what the size flag should be set to.
  21. */
  22. #include <stdbool.h>
  23. #include <stddef.h>
  24. #include <string.h>
  25. #include "fw.h"
  26. #include "io.h"
  27. #include "sdcard.h"
  28. #include "systime.h"
  29. #include "console.h"
  30. #include "ff.h"
  31. #include "diskio.h"
  32. #ifndef DEBUG
  33. # define DEBUG 0
  34. #endif
  35. #if DEBUG
  36. # define dbg_printf con_printf
  37. # define dbg_puts con_puts
  38. # define dbg_putc con_putc
  39. #else
  40. # define dbg_printf(f,...) ((void)0)
  41. # define dbg_puts(x) ((void)0)
  42. # define dbg_putc(x) ((void)0)
  43. #endif
  44. #define SECTOR_SHIFT 9
  45. #define SECTOR_SIZE (1UL << SECTOR_SHIFT)
  46. /* Command codes, including the leading 01 sequence */
  47. enum sdcard_cmd {
  48. CMD_GO_IDLE_STATE = 0x40, /* a.k.a. reset */
  49. CMD_SEND_OP_COND = 0x41,
  50. CMD_SWITCH_FUNC = 0x46,
  51. CMD_SEND_IF_COND = 0x48,
  52. CMD_SEND_CSD = 0x49,
  53. CMD_SEND_CID = 0x4a,
  54. CMD_STOP_TRANSMISSION = 0x4c,
  55. CMD_SEND_STATUS = 0x4d,
  56. CMD_SET_BLOCKLEN = 0x50,
  57. CMD_READ_SINGLE_BLOCK = 0x51,
  58. CMD_READ_MULTIPLE_BLOCK = 0x52,
  59. CMD_WRITE_BLOCK = 0x58,
  60. CMD_WRITE_MULTIPLE_BLOCK = 0x59,
  61. CMD_PROGRAM_CSD = 0x5b,
  62. CMD_SET_WRITE_PROT = 0x5c,
  63. CMD_CLR_WRITE_PROT = 0x5d,
  64. CMD_SEND_WRITE_PROT = 0x5e,
  65. CMD_ERASE_WR_BLK_START_ADDR = 0x60,
  66. CMD_ERASE_WR_BLK_END_ADDR = 0x61,
  67. CMD_ERASE = 0x66,
  68. CMD_LOCK_UNLOCK = 0x6a,
  69. CMD_APP_CMD = 0x77, /* == ACMD prefix */
  70. CMD_GEN_CMD = 0x78,
  71. CMD_READ_OCR = 0x7a,
  72. CMD_CRC_ON_OFF = 0x7b
  73. };
  74. enum sdcard_acmd {
  75. ACMD_SD_STATUS = 0x4d,
  76. ACMD_SEND_NUM_WR_BLOCKS = 0x56,
  77. ACMD_SET_WR_BLK_ERASE_COUNT = 0x57,
  78. ACMD_SD_SEND_OP_COND = 0x69,
  79. ACMD_SET_CLR_CARD_DETECT = 0x6a,
  80. ACMD_SEND_SCR = 0x73
  81. };
  82. struct sdcard_csd {
  83. uint32_t raw[4];
  84. };
  85. struct sdcard_cid {
  86. uint32_t raw[4];
  87. };
  88. struct sdcard_info {
  89. DSTATUS status;
  90. int8_t card_type;
  91. unsigned long lbasize;
  92. uint32_t if_cond;
  93. uint32_t ocr;
  94. struct sdcard_csd csd;
  95. struct sdcard_cid cid;
  96. };
  97. struct sdcard_info sdc = {
  98. .status = STA_NOINIT
  99. };
  100. /* < 0 if it should be left on, 0 for off, > 0 for off after timeout */
  101. static volatile int8_t sdcard_led;
  102. /*
  103. * Called by the timer interrupt
  104. */
  105. void sdcard_timer_tick(void)
  106. {
  107. #if 0
  108. if (sdcard_led > 0) {
  109. if (--sdcard_led == 0)
  110. *IO_SYS_LED &= ~0x01;
  111. }
  112. #endif
  113. }
  114. /*
  115. * Enable LED
  116. */
  117. static void sdcard_led_on(void)
  118. {
  119. #if 0
  120. sdcard_led = -1;
  121. *IO_SYS_LED |= 0x01;
  122. #endif
  123. }
  124. /*
  125. * Disable LED after timeout
  126. */
  127. static void sdcard_led_off(void)
  128. {
  129. sdcard_led = 4; /* 4 ticks @ 64 Hz = 62.5 ms */
  130. }
  131. static int sdcard_send_cmd(uint8_t opcode, uint32_t argument)
  132. {
  133. int status = -1;
  134. int i;
  135. if (!opcode)
  136. return 0; /* No command */
  137. dbg_printf("sdcard: CMD%02u arg %08x:", opcode ^ 0x40, argument);
  138. sd_writeb(opcode, SD_GO8|SD_CLEARCRC);
  139. sd_writel(argument, SD_BE|SD_GO32);
  140. /* GO16 so we discard the shifted-in byte from during the CRC7 */
  141. sd_writeb(sd_crc7_wr(), SD_GO16);
  142. /* The spec says a reply within 8 cycles, cut it some slack */
  143. for (i = 16; i; i--) {
  144. status = sd_readb(SD_GO8);
  145. dbg_printf(" %02x", status);
  146. if ((int8_t)status >= 0) /* Bit 7 = 0 for a valid reply */
  147. break;
  148. }
  149. dbg_putc('\n');
  150. return status;
  151. }
  152. static int sdcard_send_acmd(uint8_t opcode, uint32_t argument)
  153. {
  154. int rv;
  155. /* CMD55 = application command follows */
  156. rv = sdcard_send_cmd(CMD_APP_CMD, 0);
  157. if (rv & 0x04) /* Unknown command (very old card)? */
  158. return rv;
  159. return sdcard_send_cmd(opcode, argument);
  160. }
  161. /*
  162. * Read a data block of length len, with a timeout of (timeout)
  163. * byte-times. Note that the minimum length is 4 by spec;
  164. * this function may fail if this is not the case.
  165. *
  166. * The input buffer is allowed to be unaligned.
  167. */
  168. union xptr {
  169. uint32_t *l;
  170. uint16_t *w;
  171. uint8_t *b;
  172. size_t a;
  173. };
  174. union xcptr {
  175. const uint32_t *l;
  176. const uint16_t *w;
  177. const uint8_t *b;
  178. size_t a;
  179. };
  180. static int sdcard_read_block(void *buf, int len, int timeout,
  181. uint8_t xcmd, uint32_t xarg)
  182. {
  183. uint8_t tok;
  184. uint16_t zcrc;
  185. union xptr p;
  186. int i;
  187. int badtimeout = 8;
  188. p.b = buf;
  189. /*
  190. * Are we supposed to send a command in parallel?
  191. * This is unsafe for small blocks, but we only need to do this
  192. * for full sectors (used to send STOP_TRANSMISSION).
  193. */
  194. if (xcmd)
  195. len -= 6; /* Handle the last 6 bytes specially */
  196. /*
  197. * Wait for data token
  198. */
  199. dbg_puts("sdcard: read token:");
  200. for (;;) {
  201. tok = sd_readb(SD_GO8|SD_CLEARCRC);
  202. dbg_printf(" %02x", tok);
  203. if (tok == 0xfe)
  204. break;
  205. if (tok < 0xfe && !--badtimeout) {
  206. dbg_printf("\nsdcard: read_block: bad token: %02x\n", tok);
  207. return -1; /* Bad token */
  208. }
  209. if (!--timeout) {
  210. dbg_printf("\nsdcard: read_block: reply timeout\n");
  211. return -1; /* Timeout */
  212. }
  213. }
  214. dbg_putc('\n');
  215. /*
  216. * At this point the first byte after the token is latched into
  217. * the input shift register. After dealing with alignment,
  218. * use dummy reads to shift in the rest of the first longword.
  219. */
  220. /* Shift in bytes if needed for alignment */
  221. if (p.a & 1) {
  222. *p.b++ = sd_readb(SD_GO8);
  223. len--;
  224. }
  225. sd_readb(SD_GO8); /* Now total of 2 bytes latched */
  226. if (p.a & 2) {
  227. *p.w++ = sd_readh(SD_GO16);
  228. len -= 2;
  229. }
  230. if (len >= 6) {
  231. sd_readh(SD_GO16); /* Now total of 4 bytes latched */
  232. while (len >= 6) {
  233. *p.l++ = sd_readl(SD_GO32);
  234. len -= 4;
  235. }
  236. *p.w++ = sd_readh(2); /* Consume the two least recent bytes */
  237. len -= 2;
  238. }
  239. /*
  240. * At this point, we are aligned to a 2-byte boundary with 2 bytes
  241. * in the input shift register; we need to maintain those two bytes
  242. * for the CRC.
  243. */
  244. while (len--)
  245. *p.b++ = sd_readb(SD_GO8 | 1);
  246. /*
  247. * If we're sending a command in parallel, then we have to
  248. * read in the last 6 bytes in parallel with transmitting the
  249. * command out. Because pb may be misaligned at this point,
  250. * do the latch reads/writes to memory as byte I/O.
  251. * We still have 2 bytes of data latched, and should end
  252. * with the same (for the CRC).
  253. *
  254. * To keep the logic anything remotely consistent, passively stuff
  255. * the new command into the transmit register before triggering
  256. * active read operations.
  257. *
  258. * Note that the destination buffer may be unaligned here.
  259. */
  260. if (xcmd) {
  261. sd_writeb(xcmd, SD_CLEARCRC);
  262. *p.b++ = sd_readb(SD_GO8 | 1);
  263. sd_writel(xarg, SD_BE);
  264. *p.b++ = sd_readb(SD_GO8 | 1);
  265. *p.b++ = sd_readb(SD_GO8 | 1);
  266. *p.b++ = sd_readb(SD_GO8 | 1);
  267. *p.b++ = sd_readb(SD_GO8 | 1);
  268. sd_writeb(sd_crc7_wr(), 0);
  269. *p.b++ = sd_readb(SD_GO8 | 1);
  270. }
  271. /*
  272. * Now the CRC is latched in the shift register, and the CRC
  273. * in the CRC generator should be zero.
  274. */
  275. zcrc = sd_crc16_rd();
  276. if (zcrc != 0x0000) {
  277. con_printf("sdcard_read_block: CRC error (zcrc = %04x)\n", zcrc);
  278. return -1;
  279. }
  280. /*
  281. * Shift in the first
  282. * byte after the CRC for the next round.
  283. */
  284. sd_readb(SD_GO8);
  285. return 0;
  286. }
  287. /*
  288. * Read a number of sectors; returns the number of sectors read.
  289. * The input buffer may be unaligned.
  290. */
  291. int sdcard_read_sectors(void *buf, uint32_t lba, int count)
  292. {
  293. int rv;
  294. int okcount = 0;
  295. static const uint16_t stop_transmission[3] =
  296. { (0x40|CMD_STOP_TRANSMISSION) << 8, 0x0000, 0x0061 };
  297. uint8_t resp;
  298. uint8_t *p = buf;
  299. if (!count)
  300. return 0;
  301. sdcard_led_on();
  302. con_printf("sdcard: reading %d sector%s at %u to %p\n",
  303. count, (count != 1) ? "s" : "", lba, buf);
  304. if (sdc.card_type == 1)
  305. lba <<= SECTOR_SHIFT; /* Convert to a byte address */
  306. rv = sdcard_send_cmd(CMD_READ_MULTIPLE_BLOCK, lba);
  307. if (rv & ~1) {
  308. con_printf("sdcard: read_multiple error %02x\n", rv);
  309. goto out;
  310. }
  311. while (count--) {
  312. rv = sdcard_read_block(p, SECTOR_SIZE, 200000,
  313. count ? 0 : CMD_STOP_TRANSMISSION, 0);
  314. if (rv)
  315. goto out;
  316. okcount++;
  317. p += SECTOR_SIZE;
  318. }
  319. /* The first byte after the stop command is undefined */
  320. sd_readb(SD_GO8);
  321. /* Wait for the response to the STOP TRANSMISSION command */
  322. do {
  323. resp = sd_readb(SD_GO8);
  324. } while ((int8_t)resp < 0);
  325. if (resp) {
  326. con_printf("sdcard: read_sectors: terminate command error %02x\n",
  327. resp);
  328. }
  329. out:
  330. sdcard_led_off();
  331. return okcount;
  332. }
  333. DRESULT disk_read(BYTE drive, BYTE *buffer,
  334. LBA_t sectornumber, UINT sectorcount)
  335. {
  336. int rv;
  337. (void)drive;
  338. rv = sdcard_read_sectors(buffer, sectornumber, sectorcount);
  339. return (rv == (int)sectorcount) ? RES_OK : RES_ERROR;
  340. }
  341. /*
  342. * Read CSD/CID
  343. */
  344. static int sdcard_read_reg(uint8_t opcode, void *buf, const char *name)
  345. {
  346. int rv;
  347. uint32_t *bp = buf;
  348. unsigned int i;
  349. memset(buf, 0, 16);
  350. con_printf("sdcard: %s:", name);
  351. rv = sdcard_send_cmd(opcode, 0);
  352. if (rv & ~1)
  353. goto err;
  354. rv = sdcard_read_block(buf, 16, 2000, 0, 0);
  355. if (rv)
  356. goto err;
  357. for (i = 0; i < 4; i++) {
  358. bp[i] = __builtin_bswap32(bp[i]);
  359. con_printf(" %08x", bp[i]);
  360. }
  361. con_putc('\n');
  362. return 0;
  363. err:
  364. con_printf(" failed, err %02x\n", rv);
  365. return rv;
  366. }
  367. static int sdcard_read_csd(void)
  368. {
  369. return sdcard_read_reg(CMD_SEND_CSD, &sdc.csd, "CSD");
  370. }
  371. static int sdcard_read_cid(void)
  372. {
  373. return sdcard_read_reg(CMD_SEND_CID, &sdc.cid, "CID");
  374. }
  375. /*
  376. * Write a number of sectors; returns the number of sectors written.
  377. * The buffer may be unaligned.
  378. */
  379. int sdcard_write_sectors(const void *buf, uint32_t lba, int count)
  380. {
  381. int rv;
  382. int okcount = 0;
  383. uint16_t crc;
  384. uint8_t resp;
  385. union xcptr p;
  386. if (!count)
  387. return 0;
  388. p.b = buf;
  389. sdcard_led_on();
  390. con_printf("sdcard: writing %d sectors at %u from %p\n", count, lba, buf);
  391. if (sdc.card_type == 1)
  392. lba <<= SECTOR_SHIFT; /* Convert to a byte address */
  393. rv = sdcard_send_cmd(CMD_WRITE_MULTIPLE_BLOCK, lba);
  394. if (rv) {
  395. con_printf("sdcard: write_multiple error %02x\n", rv);
  396. goto out;
  397. }
  398. while (count--) {
  399. unsigned int podd = p.a & 3;
  400. size_t endloop = (p.a + SECTOR_SIZE) & ~3;
  401. /* Start block token */
  402. sd_writeb(0xfc, SD_GO8);
  403. /* Clear the CRC generator; dummy cycle */
  404. sd_writeb(~0, SD_CLEARCRC);
  405. if (podd & 1)
  406. sd_writeb(*p.b++, SD_GO8);
  407. if (podd & 2)
  408. sd_writeh(*p.w++, SD_GO16);
  409. while (p.a < endloop)
  410. sd_writel(*p.l++, SD_GO32);
  411. podd = -podd;
  412. if (podd & 2)
  413. sd_writeh(*p.w++, SD_GO16);
  414. if (podd & 1)
  415. sd_writeb(*p.b++, SD_GO8);
  416. sd_writeh(sd_crc16_wr(), SD_GO16);
  417. /* Wait for data response token */
  418. do {
  419. resp = sd_readb(SD_GO8);
  420. } while ((resp & 0x11) != 0x01);
  421. resp &= ~0xe0;
  422. if (resp != 0x05) {
  423. /*
  424. * Things are confusing here... the spec says
  425. * that on error we are supposed to issue a
  426. * STOP_TRANSMISSION command, which isn't the normal
  427. * thing to do for a write... figure this out later.
  428. */
  429. break; /* Error */
  430. }
  431. /* Wait until the card is ready for the next block */
  432. do {
  433. resp = sd_readb(SD_GO8);
  434. } while (resp == 0x00);
  435. okcount++;
  436. }
  437. /* Send stop transmission token */
  438. sd_writeb(0xfd, SD_GO8);
  439. /* Wait for the card to go busy, then unbusy */
  440. do {
  441. resp = sd_readb(SD_GO8);
  442. } while (resp != 0x00);
  443. do {
  444. resp = sd_readb(SD_GO8);
  445. } while (resp == 0x00);
  446. out:
  447. sdcard_led_off();
  448. return okcount;
  449. }
  450. DRESULT disk_write(BYTE drive, const BYTE *buffer, LBA_t sectornumber,
  451. UINT sectorcount)
  452. {
  453. int rv;
  454. if (drive != 0)
  455. return STA_NOINIT;
  456. rv = sdcard_write_sectors(buffer, sectornumber, sectorcount);
  457. return (rv == (int)sectorcount) ? RES_OK : RES_ERROR;
  458. }
  459. DRESULT disk_ioctl(BYTE drive, BYTE command, void *buffer)
  460. {
  461. if (drive != 0)
  462. return STA_NOINIT;
  463. switch (command) {
  464. case CTRL_SYNC:
  465. return RES_OK;
  466. case GET_SECTOR_SIZE:
  467. *(WORD *)buffer = 512;
  468. return RES_OK;
  469. case GET_SECTOR_COUNT:
  470. *(DWORD *)buffer = sdc.lbasize;
  471. return RES_OK;
  472. case GET_BLOCK_SIZE:
  473. *(DWORD *)buffer = 1; /* XXX */
  474. return RES_OK;
  475. default:
  476. return RES_PARERR;
  477. }
  478. }
  479. DWORD get_fattime(void)
  480. {
  481. return SYSCLOCK_DATETIME; /* Already in FAT format */
  482. }
  483. static unsigned long sdcard_compute_size(struct sdcard_info *sdi)
  484. {
  485. unsigned int c_size;
  486. unsigned int c_size_mult;
  487. unsigned int read_bl_len;
  488. unsigned long lbasize;
  489. sdi->card_type = (sdi->csd.raw[0] >> 30)+1;
  490. switch (sdi->card_type) {
  491. case 1: /* Classic SD/MMC card */
  492. c_size = ((sdi->csd.raw[2] & 0x3ff) << 2) +
  493. (sdi->csd.raw[3] >> 30);
  494. c_size_mult = (sdi->csd.raw[2] >> 15) & 7;
  495. read_bl_len = (sdi->csd.raw[1] >> 16) & 0xf;
  496. lbasize = (c_size + 1) << (c_size_mult + read_bl_len + 2 - 9);
  497. break;
  498. case 2: /* SDHC/SDXC/eMMC card */
  499. c_size = ((sdi->csd.raw[1] & 0x3f) << 16) +
  500. (sdi->csd.raw[2] >> 16);
  501. lbasize = c_size << 10;
  502. break;
  503. default:
  504. sdi->card_type = 0;
  505. return 0;
  506. }
  507. return sdi->lbasize = lbasize;
  508. }
  509. static const char *sdcard_type_name(uint8_t type)
  510. {
  511. static const char * const names[] = {
  512. "unknown",
  513. "SD/MMC",
  514. "SDHC/SDXC/eMMC"
  515. };
  516. if (type >= sizeof names/sizeof names[0])
  517. type = 0;
  518. return names[type];
  519. }
  520. static void sdcard_try_high_speed(void)
  521. {
  522. int rv;
  523. uint8_t tran_speed;
  524. return;
  525. if (!(sdc.csd.raw[1] & (1 << 30)))
  526. return; /* Cmd group 10 = CMD6 not supported */
  527. /* Try to switch to high speed mode */
  528. rv = sdcard_send_cmd(CMD_SWITCH_FUNC, 0x80fffff1);
  529. if (rv & ~1) {
  530. dbg_printf("sdcard: CMD6 returned %02x\n", rv);
  531. return;
  532. }
  533. if (1) {
  534. /*
  535. * Despite the spec, this doesn't seem to actually happen
  536. * in SPI mode?
  537. */
  538. uint8_t swdata[64]; /* Response from CMD6 */
  539. int i;
  540. for (i = 0; i < 64; i++)
  541. swdata[i] = sd_readb(SD_GO8);
  542. if ((swdata[47] & 0x0f) != 1) {
  543. dbg_printf("sdcard: CMD6 reported %X for high speed request\n",
  544. swdata[47] & 0x0f);
  545. return; /* Failed to switch to high speed mode */
  546. }
  547. }
  548. /*
  549. * Success, we should have switched mode.
  550. * This should be refleded in the TRAN_SPEED field in the CSD.
  551. */
  552. sd_readl(SD_GO32); /* Issue at least 8 clocks; go for 32 */
  553. /* Re-read the CSD */
  554. sdcard_read_csd();
  555. /* TRAN_SPEED should have changed now */
  556. tran_speed = (uint8_t)sdc.csd.raw[0];
  557. if ((tran_speed & 7) < 2 ||
  558. ((tran_speed & 7) == 2 && (tran_speed >> 3) < 0xb)) {
  559. dbg_printf("sdcard: speed switch failed, tran_speed %02x\n",
  560. tran_speed);
  561. return; /* High speed not available */
  562. }
  563. sd_set_mode(SD_50MHZ, true);
  564. con_printf("sdcard: switched to high speed\n");
  565. }
  566. DSTATUS disk_initialize(BYTE drive)
  567. {
  568. uint16_t status;
  569. uint32_t try_sdhc;
  570. bool is_sd;
  571. int i, j, rv;
  572. if (drive != 0)
  573. return STA_NOINIT;
  574. /* So we can wait for the ready condition */
  575. SDCARD_CTL_IRQEN = SDCARD_IRQ_READY;
  576. memset(&sdc, 0, sizeof sdc);
  577. #if 0
  578. status = /* Check card detect if present */
  579. if (!(status & 0x04)) {
  580. con_printf("No memory card installed\n");
  581. return sdc.status = STA_NOINIT|STA_NODISK;
  582. }
  583. #endif
  584. sdcard_led_on();
  585. /* Allow 4 retries in case the card is in a funky state */
  586. i = 4;
  587. while (1) {
  588. /*
  589. * Generate 256 clock cycles in slow mode, with CS# high.
  590. */
  591. sd_set_mode(SD_SLOW, false);
  592. for (j = 0; j < 8; j++)
  593. sd_writel(~0, SD_GO32);
  594. /* Assert CS# and send reset command */
  595. sd_set_mode(SD_SLOW, true);
  596. sd_writeb(~0, SD_GO8); /* Dummy byte after CS# assert */
  597. rv = sdcard_send_cmd(CMD_GO_IDLE_STATE, 0);
  598. if (rv == 0x01)
  599. break; /* Success! */
  600. if (!--i) {
  601. con_printf("sdcard: reset failed, assuming no card present\n", rv);
  602. sdcard_led_off();
  603. return sdc.status = STA_NOINIT | STA_NODISK;
  604. }
  605. }
  606. /* Switch to 20 MHz */
  607. sd_set_mode(SD_20MHZ, true);
  608. /* Enable command CRC checking (ignore result) */
  609. sdcard_send_cmd(CMD_CRC_ON_OFF, 0x0001);
  610. /* Probe for extended features */
  611. /*
  612. * Bit 7:0 = check pattern
  613. * Bit 11:8 = supply voltage (3.3 V)
  614. */
  615. rv = sdcard_send_cmd(CMD_SEND_IF_COND, 0x01aa);
  616. if ((rv & 0x04) == 0) {
  617. /* CMD8 supported */
  618. if (rv & ~0x03) {
  619. dbg_printf("sdcard; CMD8 error %02x\n", rv);
  620. sdcard_led_off();
  621. return sdc.status = STA_NOINIT;
  622. }
  623. /* Shift in additional data bytes */
  624. sd_readb(SD_GO8);
  625. sd_readh(SD_GO16);
  626. sdc.if_cond = sd_readl(SD_GO16|SD_BE);
  627. dbg_printf("sdcard: CMD8 returned 0x%08x\n", sdc.if_cond);
  628. if ((sdc.if_cond & 0x1ff) != 0x1aa) {
  629. con_printf("sdcard: CMD8 reports unusable card (0x%x)\n",
  630. sdc.if_cond);
  631. sdcard_led_off();
  632. return sdc.status = STA_NOINIT;
  633. }
  634. try_sdhc = 1 << 30;
  635. } else {
  636. try_sdhc = 0;
  637. }
  638. /* Initialize card */
  639. do {
  640. rv = sdcard_send_acmd(ACMD_SD_SEND_OP_COND, try_sdhc);
  641. if (rv & 0x04)
  642. break;
  643. if (rv & ~0x01) {
  644. con_printf("sdcard: ACMD41 error %02x\n", rv);
  645. sdcard_led_off();
  646. return sdc.status = STA_NOINIT;
  647. }
  648. } while (rv);
  649. if (!rv) {
  650. /* ACMD41 successful; this is an SD card: can switch to 25 MHz */
  651. is_sd = true;
  652. sd_set_mode(SD_25MHZ, true);
  653. rv = sdcard_send_cmd(CMD_READ_OCR, try_sdhc);
  654. if (rv) {
  655. con_printf("sdcard: CMD58 error %02x\n", rv);
  656. sdcard_led_off();
  657. return sdc.status = STA_NOINIT;
  658. }
  659. /* Shift in additional data bytes */
  660. sd_readb(SD_GO8);
  661. sd_readh(SD_GO16);
  662. sdc.ocr = sd_readl(SD_GO16|SD_BE);
  663. } else {
  664. /* ACMD41 unsupported, try CMD1 */
  665. is_sd = false;
  666. do {
  667. rv = sdcard_send_cmd(CMD_SEND_OP_COND, 0);
  668. if (rv & ~0x01) {
  669. con_printf("sdcard: CMD1 error %02x\n", rv);
  670. sdcard_led_off();
  671. return sdc.status = STA_NOINIT;
  672. }
  673. } while (rv);
  674. }
  675. /*
  676. * Set block length -- some cards power up to a larger block size
  677. * than 512 bytes even though that violates the spec.
  678. */
  679. rv = sdcard_send_cmd(CMD_SET_BLOCKLEN, 512);
  680. if (rv) {
  681. con_printf("sdcard: CMD16 error %02x\n", rv);
  682. sdcard_led_off();
  683. return sdc.status = STA_NOINIT;
  684. }
  685. /*
  686. * Read the CSD to figure out what command sets are available...
  687. */
  688. sdcard_read_csd(); /* Read CSD */
  689. /*
  690. * Try to switch to 50 MHz (optional)
  691. */
  692. if (is_sd)
  693. sdcard_try_high_speed();
  694. /*
  695. * Read the CID
  696. */
  697. sdcard_read_cid();
  698. sdcard_compute_size(&sdc);
  699. con_printf("sdcard: %s card found, capacity %u sectors\n",
  700. sdcard_type_name(sdc.card_type), sdc.lbasize);
  701. sdc.status = 0;
  702. sdcard_led_off();
  703. return sdc.status;
  704. }
  705. DSTATUS disk_status(BYTE drive)
  706. {
  707. if (drive != 0)
  708. return STA_NOINIT;
  709. return sdc.status;
  710. }
  711. static FATFS sd_fs;
  712. int disk_init(void)
  713. {
  714. FRESULT rv;
  715. char label[128];
  716. uint32_t volid, freeclust;
  717. FATFS *fs;
  718. rv = f_mount(&sd_fs, "", 1);
  719. if (rv != FR_OK) {
  720. con_printf("sdcard: no volume found\n");
  721. return -1;
  722. }
  723. label[0] = '\0';
  724. volid = 0;
  725. f_getlabel("", label, &volid);
  726. con_printf("sdcard: volume found, label \"%s\", volid %08x\n", label, volid);
  727. freeclust = 0;
  728. f_getfree("", &freeclust, &fs);
  729. con_printf("sdcard: %u/%u clusters free, clusters = %u bytes\n",
  730. freeclust, fs->n_fatent - 2, fs->csize << 9);
  731. return 0;
  732. }