sdcard.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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 int sdcard_send_cmd(uint8_t opcode, uint32_t argument)
  107. {
  108. int 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 ((int8_t)status >= 0) /* Bit 7 = 0 for a valid reply */
  122. break;
  123. }
  124. dbg_putc('\n');
  125. return status;
  126. }
  127. static int sdcard_send_acmd(uint8_t opcode, uint32_t argument)
  128. {
  129. int 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)
  263. return 0;
  264. sdcard_led_on();
  265. dbg_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 & ~1) {
  271. con_printf("sdcard: read_multiple error %02x\n", rv);
  272. goto out;
  273. }
  274. while (count--) {
  275. rv = sdcard_read_block(p, SECTOR_SIZE, 200000,
  276. count ? 0 : CMD_STOP_TRANSMISSION, 0);
  277. if (rv)
  278. goto out;
  279. okcount++;
  280. p += SECTOR_SIZE;
  281. }
  282. /* The first byte after the stop command is undefined */
  283. sd_readb(SD_GO8);
  284. /* Wait for the response to the STOP TRANSMISSION command */
  285. do {
  286. resp = sd_readb(SD_GO8);
  287. } while ((int8_t)resp < 0);
  288. if (resp) {
  289. con_printf("sdcard: read_sectors: terminate command error %02x\n",
  290. resp);
  291. }
  292. out:
  293. sdcard_led_off();
  294. return okcount;
  295. }
  296. /*
  297. * Read CSD/CID
  298. */
  299. static int sdcard_read_reg(uint8_t opcode, void *buf, const char *name)
  300. {
  301. int rv;
  302. uint32_t *bp = buf;
  303. unsigned int i;
  304. memset(buf, 0, 16);
  305. con_printf("sdcard: %s:", name);
  306. rv = sdcard_send_cmd(opcode, 0);
  307. if (rv & ~1)
  308. goto err;
  309. rv = sdcard_read_block(buf, 16, 2000, 0, 0);
  310. if (rv)
  311. goto err;
  312. for (i = 0; i < 4; i++) {
  313. bp[i] = __builtin_bswap32(bp[i]);
  314. con_printf(" %08x", bp[i]);
  315. }
  316. con_putc('\n');
  317. return 0;
  318. err:
  319. con_printf(" failed, err %02x\n", rv);
  320. return rv;
  321. }
  322. static int sdcard_read_csd(void)
  323. {
  324. return sdcard_read_reg(CMD_SEND_CSD, &sdc.csd, "CSD");
  325. }
  326. static int sdcard_read_cid(void)
  327. {
  328. return sdcard_read_reg(CMD_SEND_CID, &sdc.cid, "CID");
  329. }
  330. /*
  331. * Write a number of sectors; returns the number of sectors written.
  332. * The buffer may be unaligned.
  333. */
  334. int sdcard_write_sectors(const void *buf, sector_t lba, int count)
  335. {
  336. int rv;
  337. int okcount = 0;
  338. uint16_t crc;
  339. uint8_t resp;
  340. xcptr_t p;
  341. if (!count)
  342. return 0;
  343. p.b = buf;
  344. sdcard_led_on();
  345. dbg_printf("sdcard: writing %d sectors at %u from %p\n", count, lba, buf);
  346. if (sdc.card_type == 1)
  347. lba <<= SECTOR_SHIFT; /* Convert to a byte address */
  348. rv = sdcard_send_cmd(CMD_WRITE_MULTIPLE_BLOCK, lba);
  349. if (rv) {
  350. con_printf("sdcard: write_multiple error %02x\n", rv);
  351. goto out;
  352. }
  353. while (count--) {
  354. unsigned int podd = p.a & 3;
  355. size_t endloop = (p.a + SECTOR_SIZE) & ~3;
  356. /* Start block token */
  357. sd_writeb(0xfc, SD_GO8);
  358. /* Clear the CRC generator; dummy cycle */
  359. sd_writeb(~0, SD_CLEARCRC);
  360. if (podd & 1)
  361. sd_writeb(*p.b++, SD_GO8);
  362. if (podd & 2)
  363. sd_writeh(*p.w++, SD_GO16);
  364. while (p.a < endloop)
  365. sd_writel(*p.l++, SD_GO32);
  366. podd = -podd;
  367. if (podd & 2)
  368. sd_writeh(*p.w++, SD_GO16);
  369. if (podd & 1)
  370. sd_writeb(*p.b++, SD_GO8);
  371. sd_writeh(sd_crc16_wr(), SD_GO16);
  372. /* Wait for data response token */
  373. do {
  374. resp = sd_readb(SD_GO8);
  375. } while ((resp & 0x11) != 0x01);
  376. resp &= ~0xe0;
  377. if (resp != 0x05) {
  378. /*
  379. * Things are confusing here... the spec says
  380. * that on error we are supposed to issue a
  381. * STOP_TRANSMISSION command, which isn't the normal
  382. * thing to do for a write... figure this out later.
  383. */
  384. break; /* Error */
  385. }
  386. /* Wait until the card is ready for the next block */
  387. do {
  388. resp = sd_readb(SD_GO8);
  389. } while (resp == 0x00);
  390. okcount++;
  391. }
  392. /* Send stop transmission token */
  393. sd_writeb(0xfd, SD_GO8);
  394. /* Wait for the card to go busy, then unbusy */
  395. do {
  396. resp = sd_readb(SD_GO8);
  397. } while (resp != 0x00);
  398. do {
  399. resp = sd_readb(SD_GO8);
  400. } while (resp == 0x00);
  401. out:
  402. sdcard_led_off();
  403. return okcount;
  404. }
  405. static unsigned long sdcard_compute_size(struct sdcard_info *sdi)
  406. {
  407. unsigned int c_size;
  408. unsigned int c_size_mult;
  409. unsigned int read_bl_len;
  410. unsigned long lbasize;
  411. sdi->card_type = (sdi->csd.raw[0] >> 30)+1;
  412. switch (sdi->card_type) {
  413. case 1: /* Classic SD/MMC card */
  414. c_size = ((sdi->csd.raw[2] & 0x3ff) << 2) +
  415. (sdi->csd.raw[3] >> 30);
  416. c_size_mult = (sdi->csd.raw[2] >> 15) & 7;
  417. read_bl_len = (sdi->csd.raw[1] >> 16) & 0xf;
  418. lbasize = (c_size + 1) << (c_size_mult + read_bl_len + 2 - 9);
  419. break;
  420. case 2: /* SDHC/SDXC/eMMC card */
  421. c_size = ((sdi->csd.raw[1] & 0x3f) << 16) +
  422. (sdi->csd.raw[2] >> 16);
  423. lbasize = c_size << 10;
  424. break;
  425. default:
  426. sdi->card_type = 0;
  427. return 0;
  428. }
  429. return sdi->lbasize = lbasize;
  430. }
  431. static const char *sdcard_type_name(uint8_t type)
  432. {
  433. static const char * const names[] = {
  434. "unknown",
  435. "SD/MMC",
  436. "SDHC/SDXC/eMMC"
  437. };
  438. if (type >= sizeof names/sizeof names[0])
  439. type = 0;
  440. return names[type];
  441. }
  442. static int sdcard_switch_func_cmd(uint32_t arg, void *buf)
  443. {
  444. uint32_t rv = sdcard_send_cmd(CMD_SWITCH_FUNC, arg);
  445. if (rv & ~1) {
  446. dbg_printf("sdcard: CMD6 %08x returned %02x\n", arg, rv);
  447. return -1;
  448. }
  449. if (sdcard_read_block(buf, 64, 200000, 0, 0)) {
  450. dbg_printf("sdcard: no CMD6 %08x query reply\n", arg);
  451. return -1;
  452. }
  453. #ifdef DEBUG
  454. dbg_puts("sdcard: CMD6:");
  455. for (int i = 0; i < 64; i++)
  456. dbg_printf(" %02x", ((uint8_t *)buf)[i]);
  457. dbg_putc('\n');
  458. #endif
  459. return 0;
  460. }
  461. /* Try to switch to high speed mode (50 MHz) */
  462. static void sdcard_try_high_speed(void)
  463. {
  464. int rv;
  465. uint8_t tran_speed;
  466. uint8_t cmd6data[64];
  467. /* Verify support for CMD6, part of command group 10 */
  468. if (!(sdc.csd.raw[1] & (1 << 30))) {
  469. dbg_printf("sdcard: CMD6 not supported\n");
  470. return;
  471. }
  472. /* Try to switch to access mode 1 */
  473. if (sdcard_switch_func_cmd(0x80fffff1, cmd6data))
  474. return;
  475. /* Did we? */
  476. if ((cmd6data[16] & 0x0f) != 1) {
  477. dbg_printf("sdcard: switch to high speed mode rejected\n");
  478. return;
  479. }
  480. /*
  481. * If we succeeded, we will have switched mode.
  482. * This should be reflected in the TRAN_SPEED field in the CSD.
  483. */
  484. sd_readl(SD_GO32); /* Issue at least 8 clocks; go for 32 */
  485. /* Re-read the CSD */
  486. sdcard_read_csd();
  487. /* TRAN_SPEED should have changed now */
  488. tran_speed = (uint8_t)sdc.csd.raw[0];
  489. if ((tran_speed & 7) < 2 ||
  490. ((tran_speed & 7) == 2 && (tran_speed >> 3) < 0xb)) {
  491. dbg_printf("sdcard: speed switch failed, tran_speed %02x\n",
  492. tran_speed);
  493. return;
  494. }
  495. sd_set_mode(SD_50MHZ, true);
  496. con_printf("sdcard: switched to high speed\n");
  497. }
  498. DRESULT sdcard_init(void)
  499. {
  500. uint16_t status;
  501. uint32_t try_sdhc;
  502. bool is_sd;
  503. int i, j, rv;
  504. /* So we can wait for the ready condition */
  505. SDCARD_CTL_IRQEN = SDCARD_IRQ_READY;
  506. memset(&sdc, 0, sizeof sdc);
  507. #if 0
  508. status = /* Check card detect if present */
  509. if (!(status & 0x04)) {
  510. con_printf("No memory card installed\n");
  511. return sdc.status = STA_NOINIT|STA_NODISK;
  512. }
  513. #endif
  514. sdcard_led_on();
  515. /* Allow 4 retries in case the card is in a funky state */
  516. i = 4;
  517. while (1) {
  518. /*
  519. * Generate 256 clock cycles in slow mode, with CS# high.
  520. */
  521. sd_set_mode(SD_SLOW, false);
  522. for (j = 0; j < 8; j++)
  523. sd_writel(~0, SD_GO32);
  524. /* Assert CS# and send reset command */
  525. sd_set_mode(SD_SLOW, true);
  526. sd_writeb(~0, SD_GO8); /* Dummy byte after CS# assert */
  527. rv = sdcard_send_cmd(CMD_GO_IDLE_STATE, 0);
  528. if (rv == 0x01)
  529. break; /* Success! */
  530. if (!--i) {
  531. con_printf("sdcard: reset failed, assuming no card present\n", rv);
  532. sdcard_led_off();
  533. return sdc.status = STA_NOINIT | STA_NODISK;
  534. }
  535. }
  536. /* Switch to 20 MHz */
  537. sd_set_mode(SD_20MHZ, true);
  538. /* Enable command CRC checking (ignore result) */
  539. sdcard_send_cmd(CMD_CRC_ON_OFF, 0x0001);
  540. /* Probe for extended features */
  541. /*
  542. * Bit 7:0 = check pattern
  543. * Bit 11:8 = supply voltage (3.3 V)
  544. */
  545. rv = sdcard_send_cmd(CMD_SEND_IF_COND, 0x01aa);
  546. if ((rv & 0x04) == 0) {
  547. /* CMD8 supported */
  548. if (rv & ~0x03) {
  549. dbg_printf("sdcard; CMD8 error %02x\n", rv);
  550. sdcard_led_off();
  551. return sdc.status = STA_NOINIT;
  552. }
  553. /* Shift in additional data bytes */
  554. sd_readb(SD_GO8);
  555. sd_readh(SD_GO16);
  556. sdc.if_cond = sd_readl(SD_GO16|SD_BE);
  557. dbg_printf("sdcard: CMD8 returned 0x%08x\n", sdc.if_cond);
  558. if ((sdc.if_cond & 0x1ff) != 0x1aa) {
  559. con_printf("sdcard: CMD8 reports unusable card (0x%x)\n",
  560. sdc.if_cond);
  561. sdcard_led_off();
  562. return sdc.status = STA_NOINIT;
  563. }
  564. try_sdhc = 1 << 30;
  565. } else {
  566. try_sdhc = 0;
  567. }
  568. /* Initialize card */
  569. do {
  570. rv = sdcard_send_acmd(ACMD_SD_SEND_OP_COND, try_sdhc);
  571. if (rv & 0x04)
  572. break;
  573. if (rv & ~0x01) {
  574. con_printf("sdcard: ACMD41 error %02x\n", rv);
  575. sdcard_led_off();
  576. return sdc.status = STA_NOINIT;
  577. }
  578. } while (rv);
  579. if (!rv) {
  580. /* ACMD41 successful; this is an SD card: can switch to 25 MHz */
  581. is_sd = true;
  582. sd_set_mode(SD_25MHZ, true);
  583. rv = sdcard_send_cmd(CMD_READ_OCR, try_sdhc);
  584. if (rv) {
  585. con_printf("sdcard: CMD58 error %02x\n", rv);
  586. sdcard_led_off();
  587. return sdc.status = STA_NOINIT;
  588. }
  589. /* Shift in additional data bytes */
  590. sd_readb(SD_GO8);
  591. sd_readh(SD_GO16);
  592. sdc.ocr = sd_readl(SD_GO16|SD_BE);
  593. } else {
  594. /* ACMD41 unsupported, try CMD1 */
  595. is_sd = false;
  596. do {
  597. rv = sdcard_send_cmd(CMD_SEND_OP_COND, 0);
  598. if (rv & ~0x01) {
  599. con_printf("sdcard: CMD1 error %02x\n", rv);
  600. sdcard_led_off();
  601. return sdc.status = STA_NOINIT;
  602. }
  603. } while (rv);
  604. }
  605. /*
  606. * Set block length -- some cards power up to a larger block size
  607. * than 512 bytes even though that violates the spec.
  608. */
  609. rv = sdcard_send_cmd(CMD_SET_BLOCKLEN, 512);
  610. if (rv) {
  611. con_printf("sdcard: CMD16 error %02x\n", rv);
  612. sdcard_led_off();
  613. return sdc.status = STA_NOINIT;
  614. }
  615. /*
  616. * Read the CSD to figure out what command sets are available...
  617. */
  618. sdcard_read_csd(); /* Read CSD */
  619. /*
  620. * Try to switch to 50 MHz (optional)
  621. */
  622. if (is_sd)
  623. sdcard_try_high_speed();
  624. /*
  625. * Read the CID
  626. */
  627. sdcard_read_cid();
  628. sdcard_compute_size(&sdc);
  629. con_printf("sdcard: %s card found, capacity %u sectors\n",
  630. sdcard_type_name(sdc.card_type), sdc.lbasize);
  631. sdc.status = 0;
  632. sdcard_led_off();
  633. return sdc.status;
  634. }
  635. DSTATUS disk_status(BYTE drive)
  636. {
  637. if (drive != 0)
  638. return STA_NOINIT;
  639. return sdc.status;
  640. }