2
0

sdcard.c 19 KB

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