2
0

sdcard.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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 "console.h"
  27. #include "io.h"
  28. #include "systime.h"
  29. #include "ff.h"
  30. #include "diskio.h"
  31. #define SECTOR_SHIFT 9
  32. #define SECTOR_SIZE (1UL << SECTOR_SHIFT)
  33. /* Command codes, including the leading 01 sequence */
  34. enum sdcard_cmd {
  35. CMD_GO_IDLE_STATE = 0x40, /* a.k.a. reset */
  36. CMD_SEND_OP_COND = 0x41,
  37. CMD_SWITCH_FUNC = 0x46,
  38. CMD_SEND_IF_COND = 0x48,
  39. CMD_SEND_CSD = 0x49,
  40. CMD_SEND_CID = 0x4a,
  41. CMD_STOP_TRANSMISSION = 0x4c,
  42. CMD_SEND_STATUS = 0x4d,
  43. CMD_SET_BLOCKLEN = 0x50,
  44. CMD_READ_SINGLE_BLOCK = 0x51,
  45. CMD_READ_MULTIPLE_BLOCK = 0x52,
  46. CMD_WRITE_BLOCK = 0x58,
  47. CMD_WRITE_MULTIPLE_BLOCK = 0x59,
  48. CMD_PROGRAM_CSD = 0x5b,
  49. CMD_SET_WRITE_PROT = 0x5c,
  50. CMD_CLR_WRITE_PROT = 0x5d,
  51. CMD_SEND_WRITE_PROT = 0x5e,
  52. CMD_ERASE_WR_BLK_START_ADDR = 0x60,
  53. CMD_ERASE_WR_BLK_END_ADDR = 0x61,
  54. CMD_ERASE = 0x66,
  55. CMD_LOCK_UNLOCK = 0x6a,
  56. CMD_APP_CMD = 0x77, /* == ACMD prefix */
  57. CMD_GEN_CMD = 0x78,
  58. CMD_READ_OCR = 0x7a,
  59. CMD_CRC_ON_OFF = 0x7b
  60. };
  61. enum sdcard_acmd {
  62. ACMD_SD_STATUS = 0x4d,
  63. ACMD_SEND_NUM_WR_BLOCKS = 0x56,
  64. ACMD_SET_WR_BLK_ERASE_COUNT = 0x57,
  65. ACMD_SD_SEND_OP_COND = 0x69,
  66. ACMD_SET_CLR_CARD_DETECT = 0x6a,
  67. ACMD_SEND_SCR = 0x73
  68. };
  69. struct sdcard_csd {
  70. uint32_t raw[4];
  71. };
  72. struct sdcard_cid {
  73. uint32_t raw[4];
  74. };
  75. struct sdcard_info {
  76. DSTATUS status;
  77. int8_t card_type;
  78. unsigned long lbasize;
  79. uint32_t if_cond;
  80. uint32_t ocr;
  81. struct sdcard_csd csd;
  82. struct sdcard_cid cid;
  83. };
  84. struct sdcard_info sdc = {
  85. .status = STA_NOINIT
  86. };
  87. /* < 0 if it should be left on, 0 for off, > 0 for off after timeout */
  88. static volatile int8_t sdcard_led;
  89. /*
  90. * Called by the timer interrupt
  91. */
  92. void sdcard_timer_tick(void)
  93. {
  94. #if 0
  95. if (sdcard_led > 0) {
  96. if (--sdcard_led == 0)
  97. *IO_SYS_LED &= ~0x01;
  98. }
  99. #endif
  100. }
  101. /*
  102. * Enable LED
  103. */
  104. static void sdcard_led_on(void)
  105. {
  106. #if 0
  107. sdcard_led = -1;
  108. *IO_SYS_LED |= 0x01;
  109. #endif
  110. }
  111. /*
  112. * Disable LED after timeout
  113. */
  114. static void sdcard_led_off(void)
  115. {
  116. sdcard_led = 4; /* 4 ticks @ 64 Hz = 62.5 ms */
  117. }
  118. static int sdcard_send_cmd(uint8_t opcode, uint32_t argument)
  119. {
  120. int i;
  121. if (!opcode)
  122. return 0; /* No command */
  123. sd_writeb(opcode, SD_GO8|SD_CLEARCRC);
  124. sd_writel(argument, SD_BE|SD_GO32);
  125. /*
  126. * GO16 here is to shift in the first byte after into the read
  127. * shift register.
  128. */
  129. sd_writeb(sd_crc7_wr(), SD_GO16);
  130. /* The spec says a reply within 8 cycles, cut it some slack */
  131. for (i = 16; i; i--) {
  132. int8_t status = sd_readb(SD_GO8);
  133. if (status >= 0) /* Bit 7 = 0 for a valid reply */
  134. return status;
  135. }
  136. return -1; /* Error */
  137. }
  138. static int sdcard_send_acmd(uint8_t opcode, uint32_t argument)
  139. {
  140. int rv;
  141. /* CMD55 = application command follows */
  142. rv = sdcard_send_cmd(CMD_APP_CMD, 0);
  143. if (rv & 0x04) /* Unknown command (very old card)? */
  144. return rv;
  145. return sdcard_send_cmd(opcode, argument);
  146. }
  147. /*
  148. * Read a data block of length len, with a timeout of (timeout)
  149. * byte-times. Note that the minimum length is 4 by spec;
  150. * this function may fail if this is not the case.
  151. *
  152. * The input buffer is allowed to be unaligned.
  153. */
  154. union xptr {
  155. uint32_t *l;
  156. uint16_t *w;
  157. uint8_t *b;
  158. size_t a;
  159. };
  160. union xcptr {
  161. const uint32_t *l;
  162. const uint16_t *w;
  163. const uint8_t *b;
  164. size_t a;
  165. };
  166. static int sdcard_read_block(void *buf, int len, int timeout,
  167. uint8_t xcmd, uint32_t xarg)
  168. {
  169. uint8_t tok;
  170. uint16_t zcrc;
  171. union xptr p;
  172. int i;
  173. p.b = buf;
  174. for (;;) {
  175. tok = sd_readb(SD_GO8|SD_CLEARCRC);
  176. if (tok == 0xfe)
  177. break;
  178. if (tok < 0xfe) {
  179. con_printf("sdcard_read_block: bad token: %02x\n", tok);
  180. return -1; /* Bad token */
  181. }
  182. if (!--timeout) {
  183. con_printf("sdcard_read_block: reply timeout\n");
  184. return -1; /* Timeout */
  185. }
  186. }
  187. /*
  188. * At this point the first byte after the token is latched into
  189. * the input shift register. After dealing with alignment,
  190. * use dummy reads to shift in the rest of the first longword.
  191. */
  192. /* Shift in bytes if needed for alignment */
  193. if (p.a & 1) {
  194. *p.b++ = sd_readb(SD_GO8);
  195. len--;
  196. }
  197. sd_readb(SD_GO8); /* Now total of 2 bytes latched */
  198. if (p.a & 2) {
  199. *p.w++ = sd_readh(SD_GO16);
  200. len -= 2;
  201. }
  202. sd_readh(SD_GO16); /* Now total of 4 bytes latched */
  203. /*
  204. * Are we supposed to send a command in parallel?
  205. * This is unsafe for small blocks, but we only need to do this
  206. * for full sectors (used to send STOP_TRANSMISSION).
  207. */
  208. if (xcmd)
  209. len -= 6; /* Handle the last 6 bytes specially */
  210. while (len >= 8) {
  211. *p.l++ = sd_readl(SD_GO32);
  212. len -= 4;
  213. }
  214. if (len & 4)
  215. *p.l++ = sd_readl(SD_GO16); /* Consume latched lword + shift in CRC */
  216. if (len & 2)
  217. *p.w++ = sd_readh(SD_GO16);
  218. if (len & 1)
  219. *p.b++ = sd_readb(SD_GO8 | 1);
  220. /*
  221. * If we're sending a command in parallel, then we have to
  222. * read in the last 6 bytes in parallel with transmitting the
  223. * command out. Because pb may be misaligned at this point,
  224. * do the latch reads/writes to memory as byte I/O.
  225. * We still have 2 bytes of data latched, and should end
  226. * with the same (for the CRC).
  227. */
  228. if (xcmd) {
  229. uint8_t crc7;
  230. *p.b++ = sd_readb(1);
  231. *p.b++ = sd_readb(0);
  232. sd_writeb(xcmd, SD_GO8|SD_CLEARCRC);
  233. *p.b++ = sd_readb(0);
  234. sd_writel(xarg, SD_GO32|SD_BE);
  235. *p.b++ = sd_readb(3);
  236. *p.b++ = sd_readb(2);
  237. *p.b++ = sd_readb(1);
  238. crc7 = sd_crc7_wr();
  239. sd_writeb(crc7, SD_GO8);
  240. }
  241. /*
  242. * Now the CRC is latched in the shift register, and the CRC
  243. * in the CRC generator should be zero. Shift in the first
  244. * byte after the CRC for the next round.
  245. */
  246. zcrc = sd_crc16_rd();
  247. sd_readb(SD_GO8);
  248. if (zcrc != 0x0000) {
  249. con_printf("sdcard_read_block: CRC error (zcrc = %04x)\n", zcrc);
  250. return -1;
  251. }
  252. return 0;
  253. }
  254. /*
  255. * Read a number of sectors; returns the number of sectors read.
  256. * The input buffer may be unaligned.
  257. */
  258. int sdcard_read_sectors(void *buf, uint32_t lba, int count)
  259. {
  260. int rv;
  261. int okcount = 0;
  262. static const uint16_t stop_transmission[3] =
  263. { (0x40|CMD_STOP_TRANSMISSION) << 8, 0x0000, 0x0061 };
  264. uint8_t resp;
  265. uint8_t *p = buf;
  266. if (!count)
  267. return 0;
  268. sdcard_led_on();
  269. con_printf("sdcard: reading %d sectors at %u to %p\n", count, lba, buf);
  270. if (sdc.card_type == 1)
  271. lba <<= SECTOR_SHIFT; /* Convert to a byte address */
  272. rv = sdcard_send_cmd(CMD_READ_MULTIPLE_BLOCK, lba);
  273. if (rv) {
  274. con_printf("sdcard: read_multiple error %02x\n", rv);
  275. goto out;
  276. }
  277. while (count--) {
  278. rv = sdcard_read_block(p, SECTOR_SIZE, 200000,
  279. count ? 0 : CMD_STOP_TRANSMISSION, 0);
  280. if (rv)
  281. goto out;
  282. okcount++;
  283. p += SECTOR_SIZE;
  284. }
  285. /* The first byte after the stop command is undefined */
  286. sd_readb(SD_GO8);
  287. /* Wait for the response to the STOP TRANSMISSION command */
  288. do {
  289. resp = sd_readb(SD_GO8);
  290. } while ((int8_t)resp < 0);
  291. if (resp) {
  292. con_printf("sdcard: read_sectors: terminate command error %02x\n",
  293. resp);
  294. }
  295. out:
  296. sdcard_led_off();
  297. return okcount;
  298. }
  299. DRESULT disk_read(BYTE drive, BYTE *buffer,
  300. LBA_t sectornumber, UINT sectorcount)
  301. {
  302. int rv;
  303. (void)drive;
  304. rv = sdcard_read_sectors(buffer, sectornumber, sectorcount);
  305. return (rv == sectorcount) ? RES_OK : RES_ERROR;
  306. }
  307. /*
  308. * Read CSD/CID
  309. */
  310. static int sdcard_read_reg(uint8_t opcode, void *buf)
  311. {
  312. int rv;
  313. uint32_t *bp = buf;
  314. unsigned int i;
  315. rv = sdcard_send_cmd(opcode, 0);
  316. if (rv)
  317. return rv;
  318. rv = sdcard_read_block(buf, 16, 2000, 0, 0);
  319. if (rv)
  320. return rv;
  321. for (i = 0; i < 4; i++)
  322. bp[i] = __builtin_bswap32(bp[i]);
  323. }
  324. /*
  325. * Write a number of sectors; returns the number of sectors written.
  326. * The buffer may be unaligned.
  327. */
  328. int sdcard_write_sectors(const void *buf, uint32_t lba, int count)
  329. {
  330. int rv;
  331. int okcount = 0;
  332. uint16_t crc;
  333. uint8_t resp;
  334. union xcptr p;
  335. if (!count)
  336. return 0;
  337. p.b = buf;
  338. sdcard_led_on();
  339. con_printf("sdcard: writing %d sectors at %u from %p\n", count, lba, buf);
  340. if (sdc.card_type == 1)
  341. lba <<= SECTOR_SHIFT; /* Convert to a byte address */
  342. rv = sdcard_send_cmd(CMD_WRITE_MULTIPLE_BLOCK, lba);
  343. if (rv) {
  344. con_printf("sdcard: write_multiple error %02x\n", rv);
  345. goto out;
  346. }
  347. while (count--) {
  348. unsigned int podd = p.a & 3;
  349. size_t endloop = (p.a + SECTOR_SIZE) & ~3;
  350. /* Start block token */
  351. sd_writeb(0xfc, SD_GO8);
  352. /* Clear the CRC generator; dummy cycle */
  353. sd_writeb(~0, SD_CLEARCRC);
  354. if (podd & 1)
  355. sd_writeb(*p.b++, SD_GO8);
  356. if (podd & 2)
  357. sd_writeh(*p.w++, SD_GO16);
  358. while (p.a < endloop)
  359. sd_writel(*p.l++, SD_GO32);
  360. podd = -podd;
  361. if (podd & 2)
  362. sd_writeh(*p.w++, SD_GO16);
  363. if (podd & 1)
  364. sd_writeb(*p.b++, SD_GO8);
  365. sd_writeh(sd_crc16_wr(), SD_GO16);
  366. /* Wait for data response token */
  367. do {
  368. resp = sd_readb(SD_GO8);
  369. } while ((resp & 0x11) != 0x01);
  370. resp &= ~0xe0;
  371. if (resp != 0x05) {
  372. /*
  373. * Things are confusing here... the spec says
  374. * that on error we are supposed to issue a
  375. * STOP_TRANSMISSION command, which isn't the normal
  376. * thing to do for a write... figure this out later.
  377. */
  378. break; /* Error */
  379. }
  380. /* Wait until the card is ready for the next block */
  381. do {
  382. resp = sd_readb(SD_GO8);
  383. } while (resp == 0x00);
  384. okcount++;
  385. }
  386. /* Send stop transmission token */
  387. sd_writeb(0xfd, SD_GO8);
  388. /* Wait for the card to go busy, then unbusy */
  389. do {
  390. resp = sd_readb(SD_GO8);
  391. } while (resp != 0x00);
  392. do {
  393. resp = sd_readb(SD_GO8);
  394. } while (resp == 0x00);
  395. out:
  396. sdcard_led_off();
  397. return okcount;
  398. }
  399. DRESULT disk_write(BYTE drive, const BYTE *buffer, LBA_t sectornumber,
  400. UINT sectorcount)
  401. {
  402. int rv;
  403. if (drive != 0)
  404. return STA_NOINIT;
  405. rv = sdcard_write_sectors(buffer, sectornumber, sectorcount);
  406. return (rv == sectorcount) ? RES_OK : RES_ERROR;
  407. }
  408. DRESULT disk_ioctl(BYTE drive, BYTE command, void *buffer)
  409. {
  410. if (drive != 0)
  411. return STA_NOINIT;
  412. switch (command) {
  413. case CTRL_SYNC:
  414. return RES_OK;
  415. case GET_SECTOR_SIZE:
  416. *(WORD *)buffer = 512;
  417. return RES_OK;
  418. case GET_SECTOR_COUNT:
  419. *(DWORD *)buffer = sdc.lbasize;
  420. return RES_OK;
  421. case GET_BLOCK_SIZE:
  422. *(DWORD *)buffer = 1; /* XXX */
  423. return RES_OK;
  424. default:
  425. return RES_PARERR;
  426. }
  427. }
  428. DWORD get_fattime(void)
  429. {
  430. return SYSCLOCK_DATETIME; /* Already in FAT format */
  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 void sdcard_try_high_speed(void)
  470. {
  471. int rv;
  472. uint8_t swdata[64]; /* Response from CMD6 */
  473. if (!(sdc.csd.raw[1] & (1 << 30)))
  474. return; /* Cmd group 10 = CMD6 not supported */
  475. /* Try to switch to high speed mode */
  476. rv = sdcard_send_cmd(CMD_SWITCH_FUNC, 0x80fffff1);
  477. if (rv)
  478. return;
  479. rv = sdcard_read_block(swdata, sizeof swdata, 2000, 0, 0);
  480. if (rv)
  481. return;
  482. if ((swdata[47] & 0x0f) != 1)
  483. return; /* Failed to switch to high speed mode */
  484. /* Success, now switch mode! */
  485. sd_readl(SD_GO32); /* Issue at least 8 clocks; go for 32 */
  486. sd_set_mode(SD_50MHZ, true);
  487. con_printf("sdcard: switched to high speed\n");
  488. }
  489. DSTATUS disk_initialize(BYTE drive)
  490. {
  491. uint16_t status;
  492. uint32_t try_sdhc;
  493. bool is_sd;
  494. int i, j, rv;
  495. if (drive != 0)
  496. return STA_NOINIT;
  497. memset(&sdc, 0, sizeof sdc);
  498. #if 0
  499. status = /* Check card detect if present */
  500. if (!(status & 0x04)) {
  501. con_printf("No memory card installed\n");
  502. return sdc.status = STA_NOINIT|STA_NODISK;
  503. }
  504. #endif
  505. sdcard_led_on();
  506. /* This doesn't work for MMC, but speeds up debugging */
  507. #undef SD_SLOW
  508. #define SD_SLOW SD_20MHZ
  509. /* Allow 4 retries in case the card is in a funky state */
  510. i = 4;
  511. while (1) {
  512. /* Generate 256 clock cycles in slow mode, with CS# high */
  513. sd_set_mode(SD_SLOW, false);
  514. for (j = 0; j < 8; j++)
  515. sd_writel(~0, SD_GO32);
  516. /* Assert CS# and send reset command */
  517. sd_set_mode(SD_SLOW, true);
  518. sd_writeb(~0, SD_GO8); /* Dummy byte after CS# assert */
  519. rv = sdcard_send_cmd(CMD_GO_IDLE_STATE, 0);
  520. if (rv == 0x01)
  521. break; /* Success! */
  522. if (!--i) {
  523. con_printf("sdcard: CMD0 error %02x\n", rv);
  524. sdcard_led_off();
  525. return sdc.status = STA_NOINIT | STA_NODISK;
  526. }
  527. }
  528. /* Switch to 20 MHz */
  529. sd_set_mode(SD_20MHZ, true);
  530. /* Enable command CRC checking (ignore result) */
  531. sdcard_send_cmd(CMD_CRC_ON_OFF, 0x0001);
  532. /* Probe for extended features */
  533. /*
  534. * Bit 7:0 = check pattern
  535. * Bit 11:8 = supply voltage (3.3 V)
  536. */
  537. rv = sdcard_send_cmd(CMD_SEND_IF_COND, 0x01aa);
  538. if ((rv & 0x04) == 0) {
  539. /* CMD8 supported */
  540. if (rv & ~0x03) {
  541. con_printf("sdcard; CMD8 error %02x\n", rv);
  542. sdcard_led_off();
  543. return sdc.status = STA_NOINIT;
  544. }
  545. /* Shift in additional data bytes */
  546. sd_readb(SD_GO8);
  547. sd_readh(SD_GO16);
  548. sdc.if_cond = sd_readl(SD_GO16|SD_BE);
  549. con_printf("sdcard: CMD8 returned 0x%08x\n", sdc.if_cond);
  550. if ((sdc.if_cond & 0x1ff) != 0x1aa) {
  551. con_printf("sdcard: CMD8 reports unusable card (0x%x)\n",
  552. sdc.if_cond);
  553. sdcard_led_off();
  554. return sdc.status = STA_NOINIT;
  555. }
  556. try_sdhc = 1 << 30;
  557. } else {
  558. try_sdhc = 0;
  559. }
  560. /* Initialize card */
  561. do {
  562. rv = sdcard_send_acmd(ACMD_SD_SEND_OP_COND, try_sdhc);
  563. if (rv & 0x04)
  564. break;
  565. if (rv & ~0x01) {
  566. con_printf("sdcard: ACMD41 error %02x\n", rv);
  567. sdcard_led_off();
  568. return sdc.status = STA_NOINIT;
  569. }
  570. } while (rv);
  571. if (!rv) {
  572. /* ACMD41 successful; this is an SD card: can switch to 25 MHz */
  573. is_sd = true;
  574. sd_set_mode(SD_25MHZ, true);
  575. rv = sdcard_send_cmd(CMD_READ_OCR, try_sdhc);
  576. if (rv) {
  577. con_printf("sdcard: CMD58 error %02x\n", rv);
  578. sdcard_led_off();
  579. return sdc.status = STA_NOINIT;
  580. }
  581. /* Shift in additional data bytes */
  582. sd_readb(SD_GO8);
  583. sd_readh(SD_GO16);
  584. sdc.ocr = sd_readl(SD_GO16|SD_BE);
  585. } else {
  586. /* ACMD41 unsupported, try CMD1 */
  587. is_sd = false;
  588. do {
  589. rv = sdcard_send_cmd(CMD_SEND_OP_COND, 0);
  590. if (rv & ~0x01) {
  591. con_printf("sdcard: CMD1 error %02x\n", rv);
  592. sdcard_led_off();
  593. return sdc.status = STA_NOINIT;
  594. }
  595. } while (rv);
  596. }
  597. /*
  598. * Set block length -- some cards power up to a larger block size
  599. * than 512 bytes even though that violates the spec.
  600. */
  601. rv = sdcard_send_cmd(CMD_SET_BLOCKLEN, 512);
  602. if (rv) {
  603. con_printf("sdcard: CMD16 error %02x\n", rv);
  604. sdcard_led_off();
  605. return sdc.status = STA_NOINIT;
  606. }
  607. /*
  608. * Read CSD and CID
  609. */
  610. if (sdcard_read_reg(CMD_SEND_CSD, &sdc.csd))
  611. memset(&sdc.csd, 0, sizeof sdc.csd);
  612. con_printf("sdcard: CSD: %08x %08x %08x %08x\n",
  613. sdc.csd.raw[0], sdc.csd.raw[1], sdc.csd.raw[2], sdc.csd.raw[3]);
  614. if (sdcard_read_reg(CMD_SEND_CID, &sdc.cid))
  615. memset(&sdc.cid, 0, sizeof sdc.cid);
  616. con_printf("sdcard: CID: %08x %08x %08x %08x\n",
  617. sdc.cid.raw[0], sdc.cid.raw[1], sdc.cid.raw[2], sdc.cid.raw[3]);
  618. sdcard_compute_size(&sdc);
  619. con_printf("sdcard: %s card found, capacity %u sectors\n",
  620. sdcard_type_name(sdc.card_type), sdc.lbasize);
  621. /*
  622. * Try to switch to 50 MHz (optional)
  623. */
  624. if (is_sd)
  625. sdcard_try_high_speed();
  626. sdc.status = 0;
  627. sdcard_led_off();
  628. return sdc.status;
  629. }
  630. DSTATUS disk_status(BYTE drive)
  631. {
  632. if (drive != 0)
  633. return STA_NOINIT;
  634. return sdc.status;
  635. }
  636. static FATFS sd_fs;
  637. int disk_init(void)
  638. {
  639. return f_mount(&sd_fs, "", 1);
  640. }