sd_card_sdio.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. // Driver for accessing SD card in SDIO mode on RP2040.
  2. //
  3. // Copyright (c) 2022 Rabbit Hole Computing™
  4. // Copyright (c) 2024 Tech by Androda, LLC
  5. #include "BlueSCSI_platform.h"
  6. #ifdef SD_USE_SDIO
  7. #include "BlueSCSI_log.h"
  8. #include "rp2040_sdio.h"
  9. #include <hardware/gpio.h>
  10. #include <SdFat.h>
  11. #include <SdCard/SdCardInfo.h>
  12. static uint32_t g_sdio_ocr; // Operating condition register from card
  13. static uint32_t g_sdio_rca; // Relative card address
  14. static cid_t g_sdio_cid;
  15. static csd_t g_sdio_csd;
  16. static int g_sdio_error_line;
  17. static sdio_status_t g_sdio_error;
  18. static uint32_t g_sdio_dma_buf[128];
  19. static uint32_t g_sdio_sector_count;
  20. uint8_t sdSpeedClass;
  21. #define checkReturnOk(call) ((g_sdio_error = (call)) == SDIO_OK ? true : logSDError(__LINE__))
  22. static bool logSDError(int line)
  23. {
  24. g_sdio_error_line = line;
  25. log("SDIO SD card error on line ", line, ", error code ", (int)g_sdio_error);
  26. return false;
  27. }
  28. // Callback used by SCSI code for simultaneous processing
  29. static sd_callback_t m_stream_callback;
  30. static const uint8_t *m_stream_buffer;
  31. static uint32_t m_stream_count;
  32. static uint32_t m_stream_count_start;
  33. void platform_set_sd_callback(sd_callback_t func, const uint8_t *buffer)
  34. {
  35. m_stream_callback = func;
  36. m_stream_buffer = buffer;
  37. m_stream_count = 0;
  38. m_stream_count_start = 0;
  39. }
  40. static sd_callback_t get_stream_callback(const uint8_t *buf, uint32_t count, const char *accesstype, uint32_t sector)
  41. {
  42. m_stream_count_start = m_stream_count;
  43. if (m_stream_callback)
  44. {
  45. if (buf == m_stream_buffer + m_stream_count)
  46. {
  47. m_stream_count += count;
  48. return m_stream_callback;
  49. }
  50. else
  51. {
  52. debuglog("SD card ", accesstype, "(", (int)sector,
  53. ") slow transfer, buffer", (uint32_t)buf, " vs. ", (uint32_t)(m_stream_buffer + m_stream_count));
  54. return NULL;
  55. }
  56. }
  57. return NULL;
  58. }
  59. bool SdioCard::begin(SdioConfig sdioConfig)
  60. {
  61. uint32_t reply;
  62. sdio_status_t status;
  63. // Initialize at 1 MHz clock speed
  64. rp2040_sdio_init(25);
  65. // Establish initial connection with the card
  66. for (int retries = 0; retries < 5; retries++)
  67. {
  68. delayMicroseconds(1000);
  69. reply = 0;
  70. rp2040_sdio_command_R1(CMD0, 0, NULL); // GO_IDLE_STATE
  71. status = rp2040_sdio_command_R1(CMD8, 0x1AA, &reply); // SEND_IF_COND
  72. if (status == SDIO_OK && reply == 0x1AA)
  73. {
  74. break;
  75. }
  76. }
  77. if (reply != 0x1AA || status != SDIO_OK)
  78. {
  79. // debuglog("SDIO not responding to CMD8 SEND_IF_COND, status ", (int)status, " reply ", reply);
  80. return false;
  81. }
  82. // Send ACMD41 to begin card initialization and wait for it to complete
  83. uint32_t start = millis();
  84. do {
  85. if (!checkReturnOk(rp2040_sdio_command_R1(CMD55, 0, &reply)) || // APP_CMD
  86. !checkReturnOk(rp2040_sdio_command_R3(ACMD41, 0xD0040000, &g_sdio_ocr))) // 3.0V voltage
  87. // !checkReturnOk(rp2040_sdio_command_R1(ACMD41, 0xC0100000, &g_sdio_ocr)))
  88. {
  89. return false;
  90. }
  91. if ((uint32_t)(millis() - start) > 1000)
  92. {
  93. log("SDIO card initialization timeout");
  94. return false;
  95. }
  96. } while (!(g_sdio_ocr & (1 << 31)));
  97. // Get CID
  98. if (!checkReturnOk(rp2040_sdio_command_R2(CMD2, 0, (uint8_t*)&g_sdio_cid)))
  99. {
  100. debuglog("SDIO failed to read CID");
  101. return false;
  102. }
  103. // Get relative card address
  104. if (!checkReturnOk(rp2040_sdio_command_R1(CMD3, 0, &g_sdio_rca)))
  105. {
  106. debuglog("SDIO failed to get RCA");
  107. return false;
  108. }
  109. // Get CSD
  110. if (!checkReturnOk(rp2040_sdio_command_R2(CMD9, g_sdio_rca, (uint8_t*)&g_sdio_csd)))
  111. {
  112. debuglog("SDIO failed to read CSD");
  113. return false;
  114. }
  115. g_sdio_sector_count = sectorCount();
  116. // Select card
  117. if (!checkReturnOk(rp2040_sdio_command_R1(CMD7, g_sdio_rca, &reply)))
  118. {
  119. debuglog("SDIO failed to select card");
  120. return false;
  121. }
  122. // Set 4-bit bus mode
  123. if (!checkReturnOk(rp2040_sdio_command_R1(CMD55, g_sdio_rca, &reply)) ||
  124. !checkReturnOk(rp2040_sdio_command_R1(ACMD6, 2, &reply)))
  125. {
  126. debuglog("SDIO failed to set bus width");
  127. return false;
  128. }
  129. // Read SD Status field
  130. sds_t sd_stat;
  131. memset(&sd_stat, 0, sizeof(sds_t));
  132. uint8_t* stat_pointer = (uint8_t*) &sd_stat;
  133. if (!checkReturnOk(rp2040_sdio_command_R1(CMD55, g_sdio_rca, &reply)) ||
  134. !checkReturnOk(rp2040_sdio_command_R1(ACMD13, 0, &reply)) ||
  135. !checkReturnOk(receive_status_register(stat_pointer)))
  136. {
  137. debuglog("SDIO failed to get SD Status");
  138. return false;
  139. }
  140. sdSpeedClass = sd_stat.speedClass();
  141. // Increase to 25 MHz clock rate
  142. rp2040_sdio_init(1);
  143. return true;
  144. }
  145. uint8_t SdioCard::errorCode() const
  146. {
  147. return g_sdio_error;
  148. }
  149. uint32_t SdioCard::errorData() const
  150. {
  151. return 0;
  152. }
  153. uint32_t SdioCard::errorLine() const
  154. {
  155. return g_sdio_error_line;
  156. }
  157. bool SdioCard::isBusy()
  158. {
  159. return (sio_hw->gpio_in & (1 << SDIO_D0)) == 0;
  160. }
  161. uint32_t SdioCard::kHzSdClk()
  162. {
  163. return 0;
  164. }
  165. bool SdioCard::readCID(cid_t* cid)
  166. {
  167. *cid = g_sdio_cid;
  168. return true;
  169. }
  170. bool SdioCard::readCSD(csd_t* csd)
  171. {
  172. *csd = g_sdio_csd;
  173. return true;
  174. }
  175. bool SdioCard::readOCR(uint32_t* ocr)
  176. {
  177. // SDIO mode does not have CMD58, but main program uses this to
  178. // poll for card presence. Return status register instead.
  179. return checkReturnOk(rp2040_sdio_command_R1(CMD13, g_sdio_rca, ocr));
  180. }
  181. bool SdioCard::readData(uint8_t* dst)
  182. {
  183. log("SdioCard::readData() called but not implemented!");
  184. return false;
  185. }
  186. bool SdioCard::readStart(uint32_t sector)
  187. {
  188. log("SdioCard::readStart() called but not implemented!");
  189. return false;
  190. }
  191. bool SdioCard::readStop()
  192. {
  193. log("SdioCard::readStop() called but not implemented!");
  194. return false;
  195. }
  196. uint32_t SdioCard::sectorCount()
  197. {
  198. return g_sdio_csd.capacity();
  199. }
  200. uint32_t SdioCard::status()
  201. {
  202. uint32_t reply;
  203. if (checkReturnOk(rp2040_sdio_command_R1(CMD13, g_sdio_rca, &reply)))
  204. return reply;
  205. else
  206. return 0;
  207. }
  208. bool SdioCard::stopTransmission(bool blocking)
  209. {
  210. uint32_t reply;
  211. if (!checkReturnOk(rp2040_sdio_command_R1(CMD12, 0, &reply)))
  212. {
  213. return false;
  214. }
  215. if (!blocking)
  216. {
  217. return true;
  218. }
  219. else
  220. {
  221. uint32_t start = millis();
  222. while ((uint32_t)(millis() - start) < 5000 && isBusy())
  223. {
  224. cycleSdClock();
  225. if (m_stream_callback)
  226. {
  227. m_stream_callback(m_stream_count);
  228. }
  229. }
  230. if (isBusy())
  231. {
  232. log("SdioCard::stopTransmission() timeout");
  233. return false;
  234. }
  235. else
  236. {
  237. return true;
  238. }
  239. }
  240. }
  241. bool SdioCard::syncDevice()
  242. {
  243. return true;
  244. }
  245. uint8_t SdioCard::type() const
  246. {
  247. if (g_sdio_ocr & (1 << 30))
  248. return SD_CARD_TYPE_SDHC;
  249. else
  250. return SD_CARD_TYPE_SD2;
  251. }
  252. bool SdioCard::writeData(const uint8_t* src)
  253. {
  254. log("SdioCard::writeData() called but not implemented!");
  255. return false;
  256. }
  257. bool SdioCard::writeStart(uint32_t sector)
  258. {
  259. log("SdioCard::writeStart() called but not implemented!");
  260. return false;
  261. }
  262. bool SdioCard::writeStop()
  263. {
  264. log("SdioCard::writeStop() called but not implemented!");
  265. return false;
  266. }
  267. bool SdioCard::erase(uint32_t firstSector, uint32_t lastSector)
  268. {
  269. log("SdioCard::erase() not implemented");
  270. return false;
  271. }
  272. bool SdioCard::cardCMD6(uint32_t arg, uint8_t* status) {
  273. log("SdioCard::cardCMD6() not implemented");
  274. return false;
  275. }
  276. bool SdioCard::readSCR(scr_t* scr) {
  277. log("SdioCard::readSCR() not implemented");
  278. return false;
  279. }
  280. /* Writing and reading, with progress callback */
  281. bool SdioCard::writeSector(uint32_t sector, const uint8_t* src)
  282. {
  283. if (((uint32_t)src & 3) != 0)
  284. {
  285. // Buffer is not aligned, need to memcpy() the data to a temporary buffer.
  286. memcpy(g_sdio_dma_buf, src, sizeof(g_sdio_dma_buf));
  287. src = (uint8_t*)g_sdio_dma_buf;
  288. }
  289. // If possible, report transfer status to application through callback.
  290. sd_callback_t callback = get_stream_callback(src, 512, "writeSector", sector);
  291. // Cards up to 2GB use byte addressing, SDHC cards use sector addressing
  292. uint32_t address = (type() == SD_CARD_TYPE_SDHC) ? sector : (sector * 512);
  293. uint32_t reply;
  294. if (!checkReturnOk(rp2040_sdio_command_R1(16, 512, &reply)) || // SET_BLOCKLEN
  295. !checkReturnOk(rp2040_sdio_command_R1(CMD24, address, &reply)) || // WRITE_BLOCK
  296. !checkReturnOk(rp2040_sdio_tx_start(src, 1))) // Start transmission
  297. {
  298. return false;
  299. }
  300. do {
  301. uint32_t bytes_done;
  302. g_sdio_error = rp2040_sdio_tx_poll(&bytes_done);
  303. if (callback)
  304. {
  305. callback(m_stream_count_start + bytes_done);
  306. }
  307. } while (g_sdio_error == SDIO_BUSY);
  308. if (g_sdio_error != SDIO_OK)
  309. {
  310. log("SdioCard::writeSector(", sector, ") failed: ", (int)g_sdio_error);
  311. }
  312. return g_sdio_error == SDIO_OK;
  313. }
  314. bool SdioCard::writeSectors(uint32_t sector, const uint8_t* src, size_t n)
  315. {
  316. if (((uint32_t)src & 3) != 0)
  317. {
  318. // Unaligned write, execute sector-by-sector
  319. for (size_t i = 0; i < n; i++)
  320. {
  321. if (!writeSector(sector + i, src + 512 * i))
  322. {
  323. return false;
  324. }
  325. }
  326. return true;
  327. }
  328. sd_callback_t callback = get_stream_callback(src, n * 512, "writeSectors", sector);
  329. // Cards up to 2GB use byte addressing, SDHC cards use sector addressing
  330. uint32_t address = (type() == SD_CARD_TYPE_SDHC) ? sector : (sector * 512);
  331. uint32_t reply;
  332. if (!checkReturnOk(rp2040_sdio_command_R1(16, 512, &reply)) || // SET_BLOCKLEN
  333. !checkReturnOk(rp2040_sdio_command_R1(CMD55, g_sdio_rca, &reply)) || // APP_CMD
  334. !checkReturnOk(rp2040_sdio_command_R1(ACMD23, n, &reply)) || // SET_WR_CLK_ERASE_COUNT
  335. !checkReturnOk(rp2040_sdio_command_R1(CMD25, address, &reply)) || // WRITE_MULTIPLE_BLOCK
  336. !checkReturnOk(rp2040_sdio_tx_start(src, n))) // Start transmission
  337. {
  338. return false;
  339. }
  340. do {
  341. uint32_t bytes_done;
  342. g_sdio_error = rp2040_sdio_tx_poll(&bytes_done);
  343. if (callback)
  344. {
  345. callback(m_stream_count_start + bytes_done);
  346. }
  347. } while (g_sdio_error == SDIO_BUSY);
  348. if (g_sdio_error != SDIO_OK)
  349. {
  350. log("SdioCard::writeSectors(", sector, ",...,", (int)n, ") failed: ", (int)g_sdio_error);
  351. stopTransmission(true);
  352. return false;
  353. }
  354. else
  355. {
  356. // TODO: Instead of CMD12 stopTransmission command, according to SD spec we should send stopTran token.
  357. // stopTransmission seems to work in practice.
  358. return stopTransmission(true);
  359. }
  360. }
  361. bool SdioCard::readSector(uint32_t sector, uint8_t* dst)
  362. {
  363. uint8_t *real_dst = dst;
  364. if (((uint32_t)dst & 3) != 0)
  365. {
  366. // Buffer is not aligned, need to memcpy() the data from a temporary buffer.
  367. dst = (uint8_t*)g_sdio_dma_buf;
  368. }
  369. sd_callback_t callback = get_stream_callback(dst, 512, "readSector", sector);
  370. // Cards up to 2GB use byte addressing, SDHC cards use sector addressing
  371. uint32_t address = (type() == SD_CARD_TYPE_SDHC) ? sector : (sector * 512);
  372. uint32_t reply;
  373. if (
  374. !checkReturnOk(rp2040_sdio_command_R1(16, 512, &reply)) || // SET_BLOCKLEN
  375. !checkReturnOk(rp2040_sdio_command_R1(CMD17, address, &reply)) || // READ_SINGLE_BLOCK
  376. !checkReturnOk(rp2040_sdio_rx_start(dst, 1, SDIO_BLOCK_SIZE)) // Prepare for reception
  377. )
  378. {
  379. return false;
  380. }
  381. do {
  382. uint32_t bytes_done;
  383. g_sdio_error = rp2040_sdio_rx_poll(&bytes_done);
  384. if (callback)
  385. {
  386. callback(m_stream_count_start + bytes_done);
  387. }
  388. } while (g_sdio_error == SDIO_BUSY);
  389. if (g_sdio_error != SDIO_OK)
  390. {
  391. log("SdioCard::readSector(", sector, ") failed: ", (int)g_sdio_error);
  392. }
  393. if (dst != real_dst)
  394. {
  395. memcpy(real_dst, g_sdio_dma_buf, sizeof(g_sdio_dma_buf));
  396. }
  397. return g_sdio_error == SDIO_OK;
  398. }
  399. bool SdioCard::readSectors(uint32_t sector, uint8_t* dst, size_t n)
  400. {
  401. if (((uint32_t)dst & 3) != 0 || sector + n >= g_sdio_sector_count)
  402. {
  403. // Unaligned read or end-of-drive read, execute sector-by-sector
  404. for (size_t i = 0; i < n; i++)
  405. {
  406. if (!readSector(sector + i, dst + 512 * i))
  407. {
  408. return false;
  409. }
  410. }
  411. return true;
  412. }
  413. sd_callback_t callback = get_stream_callback(dst, n * 512, "readSectors", sector);
  414. // Cards up to 2GB use byte addressing, SDHC cards use sector addressing
  415. uint32_t address = (type() == SD_CARD_TYPE_SDHC) ? sector : (sector * 512);
  416. uint32_t reply;
  417. if (
  418. !checkReturnOk(rp2040_sdio_command_R1(16, 512, &reply)) || // SET_BLOCKLEN
  419. !checkReturnOk(rp2040_sdio_command_R1(CMD18, address, &reply)) || // READ_MULTIPLE_BLOCK
  420. !checkReturnOk(rp2040_sdio_rx_start(dst, n, SDIO_BLOCK_SIZE)) // Prepare for reception
  421. )
  422. {
  423. return false;
  424. }
  425. do {
  426. uint32_t bytes_done;
  427. g_sdio_error = rp2040_sdio_rx_poll(&bytes_done);
  428. if (callback)
  429. {
  430. callback(m_stream_count_start + bytes_done);
  431. }
  432. } while (g_sdio_error == SDIO_BUSY);
  433. if (g_sdio_error != SDIO_OK)
  434. {
  435. log("SdioCard::readSectors(", sector, ",...,", (int)n, ") failed: ", (int)g_sdio_error);
  436. stopTransmission(true);
  437. return false;
  438. }
  439. else
  440. {
  441. return stopTransmission(true);
  442. }
  443. }
  444. // These functions are not used for SDIO mode but are needed to avoid build error.
  445. void sdCsInit(SdCsPin_t pin) {}
  446. void sdCsWrite(SdCsPin_t pin, bool level) {}
  447. // SDIO configuration for main program
  448. SdioConfig g_sd_sdio_config(DMA_SDIO);
  449. #endif