sdcard.c 19 KB

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