sd_card_sdio.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. static uint8_t cardType;
  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. // Increase to 25 MHz clock rate
  130. rp2040_sdio_init(1);
  131. return true;
  132. }
  133. uint8_t SdioCard::errorCode() const
  134. {
  135. return g_sdio_error;
  136. }
  137. uint32_t SdioCard::errorData() const
  138. {
  139. return 0;
  140. }
  141. uint32_t SdioCard::errorLine() const
  142. {
  143. return g_sdio_error_line;
  144. }
  145. bool SdioCard::isBusy()
  146. {
  147. return (sio_hw->gpio_in & (1 << SDIO_D0)) == 0;
  148. }
  149. uint32_t SdioCard::kHzSdClk()
  150. {
  151. return 0;
  152. }
  153. bool SdioCard::readCID(cid_t* cid)
  154. {
  155. *cid = g_sdio_cid;
  156. return true;
  157. }
  158. bool SdioCard::readCSD(csd_t* csd)
  159. {
  160. *csd = g_sdio_csd;
  161. return true;
  162. }
  163. bool SdioCard::readOCR(uint32_t* ocr)
  164. {
  165. // SDIO mode does not have CMD58, but main program uses this to
  166. // poll for card presence. Return status register instead.
  167. return checkReturnOk(rp2040_sdio_command_R1(CMD13, g_sdio_rca, ocr));
  168. }
  169. bool SdioCard::readData(uint8_t* dst)
  170. {
  171. log("SdioCard::readData() called but not implemented!");
  172. return false;
  173. }
  174. bool SdioCard::readStart(uint32_t sector)
  175. {
  176. log("SdioCard::readStart() called but not implemented!");
  177. return false;
  178. }
  179. bool SdioCard::readStop()
  180. {
  181. log("SdioCard::readStop() called but not implemented!");
  182. return false;
  183. }
  184. uint32_t SdioCard::sectorCount()
  185. {
  186. return g_sdio_csd.capacity();
  187. }
  188. uint32_t SdioCard::status()
  189. {
  190. uint32_t reply;
  191. if (checkReturnOk(rp2040_sdio_command_R1(CMD13, g_sdio_rca, &reply)))
  192. return reply;
  193. else
  194. return 0;
  195. }
  196. bool SdioCard::stopTransmission(bool blocking)
  197. {
  198. uint32_t reply;
  199. if (!checkReturnOk(rp2040_sdio_command_R1(CMD12, 0, &reply)))
  200. {
  201. return false;
  202. }
  203. if (!blocking)
  204. {
  205. return true;
  206. }
  207. else
  208. {
  209. uint32_t start = millis();
  210. while ((uint32_t)(millis() - start) < 5000 && isBusy())
  211. {
  212. cycleSdClock();
  213. if (m_stream_callback)
  214. {
  215. m_stream_callback(m_stream_count);
  216. }
  217. }
  218. if (isBusy())
  219. {
  220. log("SdioCard::stopTransmission() timeout");
  221. return false;
  222. }
  223. else
  224. {
  225. return true;
  226. }
  227. }
  228. }
  229. bool SdioCard::syncDevice()
  230. {
  231. return true;
  232. }
  233. uint8_t SdioCard::type() const
  234. {
  235. if (g_sdio_ocr & (1 << 30))
  236. return SD_CARD_TYPE_SDHC;
  237. else
  238. return SD_CARD_TYPE_SD2;
  239. }
  240. bool SdioCard::writeData(const uint8_t* src)
  241. {
  242. log("SdioCard::writeData() called but not implemented!");
  243. return false;
  244. }
  245. bool SdioCard::writeStart(uint32_t sector)
  246. {
  247. log("SdioCard::writeStart() called but not implemented!");
  248. return false;
  249. }
  250. bool SdioCard::writeStop()
  251. {
  252. log("SdioCard::writeStop() called but not implemented!");
  253. return false;
  254. }
  255. bool SdioCard::erase(uint32_t firstSector, uint32_t lastSector)
  256. {
  257. log("SdioCard::erase() not implemented");
  258. return false;
  259. }
  260. bool SdioCard::cardCMD6(uint32_t arg, uint8_t* status) {
  261. log("SdioCard::cardCMD6() not implemented");
  262. return false;
  263. }
  264. bool SdioCard::readSCR(scr_t* scr) {
  265. log("SdioCard::readSCR() not implemented");
  266. return false;
  267. }
  268. /* Writing and reading, with progress callback */
  269. bool SdioCard::writeSector(uint32_t sector, const uint8_t* src)
  270. {
  271. if (((uint32_t)src & 3) != 0)
  272. {
  273. // Buffer is not aligned, need to memcpy() the data to a temporary buffer.
  274. memcpy(g_sdio_dma_buf, src, sizeof(g_sdio_dma_buf));
  275. src = (uint8_t*)g_sdio_dma_buf;
  276. }
  277. // If possible, report transfer status to application through callback.
  278. sd_callback_t callback = get_stream_callback(src, 512, "writeSector", sector);
  279. // Cards up to 2GB use byte addressing, SDHC cards use sector addressing
  280. uint32_t address = (type() == SD_CARD_TYPE_SDHC) ? sector : (sector * 512);
  281. uint32_t reply;
  282. if (!checkReturnOk(rp2040_sdio_command_R1(16, 512, &reply)) || // SET_BLOCKLEN
  283. !checkReturnOk(rp2040_sdio_command_R1(CMD24, address, &reply)) || // WRITE_BLOCK
  284. !checkReturnOk(rp2040_sdio_tx_start(src, 1))) // Start transmission
  285. {
  286. return false;
  287. }
  288. do {
  289. uint32_t bytes_done;
  290. g_sdio_error = rp2040_sdio_tx_poll(&bytes_done);
  291. if (callback)
  292. {
  293. callback(m_stream_count_start + bytes_done);
  294. }
  295. } while (g_sdio_error == SDIO_BUSY);
  296. if (g_sdio_error != SDIO_OK)
  297. {
  298. log("SdioCard::writeSector(", sector, ") failed: ", (int)g_sdio_error);
  299. }
  300. return g_sdio_error == SDIO_OK;
  301. }
  302. bool SdioCard::writeSectors(uint32_t sector, const uint8_t* src, size_t n)
  303. {
  304. if (((uint32_t)src & 3) != 0)
  305. {
  306. // Unaligned write, execute sector-by-sector
  307. for (size_t i = 0; i < n; i++)
  308. {
  309. if (!writeSector(sector + i, src + 512 * i))
  310. {
  311. return false;
  312. }
  313. }
  314. return true;
  315. }
  316. sd_callback_t callback = get_stream_callback(src, n * 512, "writeSectors", sector);
  317. // Cards up to 2GB use byte addressing, SDHC cards use sector addressing
  318. uint32_t address = (type() == SD_CARD_TYPE_SDHC) ? sector : (sector * 512);
  319. uint32_t reply;
  320. if (!checkReturnOk(rp2040_sdio_command_R1(16, 512, &reply)) || // SET_BLOCKLEN
  321. !checkReturnOk(rp2040_sdio_command_R1(CMD55, g_sdio_rca, &reply)) || // APP_CMD
  322. !checkReturnOk(rp2040_sdio_command_R1(ACMD23, n, &reply)) || // SET_WR_CLK_ERASE_COUNT
  323. !checkReturnOk(rp2040_sdio_command_R1(CMD25, address, &reply)) || // WRITE_MULTIPLE_BLOCK
  324. !checkReturnOk(rp2040_sdio_tx_start(src, n))) // Start transmission
  325. {
  326. return false;
  327. }
  328. do {
  329. uint32_t bytes_done;
  330. g_sdio_error = rp2040_sdio_tx_poll(&bytes_done);
  331. if (callback)
  332. {
  333. callback(m_stream_count_start + bytes_done);
  334. }
  335. } while (g_sdio_error == SDIO_BUSY);
  336. if (g_sdio_error != SDIO_OK)
  337. {
  338. log("SdioCard::writeSectors(", sector, ",...,", (int)n, ") failed: ", (int)g_sdio_error);
  339. stopTransmission(true);
  340. return false;
  341. }
  342. else
  343. {
  344. // TODO: Instead of CMD12 stopTransmission command, according to SD spec we should send stopTran token.
  345. // stopTransmission seems to work in practice.
  346. return stopTransmission(true);
  347. }
  348. }
  349. bool SdioCard::readSector(uint32_t sector, uint8_t* dst)
  350. {
  351. uint8_t *real_dst = dst;
  352. if (((uint32_t)dst & 3) != 0)
  353. {
  354. // Buffer is not aligned, need to memcpy() the data from a temporary buffer.
  355. dst = (uint8_t*)g_sdio_dma_buf;
  356. }
  357. sd_callback_t callback = get_stream_callback(dst, 512, "readSector", sector);
  358. // Cards up to 2GB use byte addressing, SDHC cards use sector addressing
  359. uint32_t address = (type() == SD_CARD_TYPE_SDHC) ? sector : (sector * 512);
  360. uint32_t reply;
  361. // Honestly CMD16 feels partially unnecessary. Default block length is 512. SDHC, SDXC, SDUC, *always* use 512 and this does nothing.
  362. // Set length is valid for memory access commands only if partial block read operation are allowed in CSD.
  363. // We do have the CSD, so CMD16 should only be run if actually necessary
  364. if (
  365. !checkReturnOk(rp2040_sdio_command_R1(16, 512, &reply)) || // SET_BLOCKLEN
  366. !checkReturnOk(rp2040_sdio_command_R1(CMD17, address, &reply)) || // READ_SINGLE_BLOCK
  367. !checkReturnOk(rp2040_sdio_rx_start(dst, 1)) // Prepare for reception
  368. )
  369. {
  370. return false;
  371. }
  372. do {
  373. uint32_t bytes_done;
  374. g_sdio_error = rp2040_sdio_rx_poll(&bytes_done);
  375. if (callback)
  376. {
  377. callback(m_stream_count_start + bytes_done);
  378. }
  379. } while (g_sdio_error == SDIO_BUSY);
  380. if (g_sdio_error != SDIO_OK)
  381. {
  382. log("SdioCard::readSector(", sector, ") failed: ", (int)g_sdio_error);
  383. }
  384. if (dst != real_dst)
  385. {
  386. memcpy(real_dst, g_sdio_dma_buf, sizeof(g_sdio_dma_buf));
  387. }
  388. return g_sdio_error == SDIO_OK;
  389. }
  390. bool SdioCard::readSectors(uint32_t sector, uint8_t* dst, size_t n)
  391. {
  392. if (((uint32_t)dst & 3) != 0 || sector + n >= g_sdio_sector_count)
  393. {
  394. // Unaligned read or end-of-drive read, execute sector-by-sector
  395. for (size_t i = 0; i < n; i++)
  396. {
  397. if (!readSector(sector + i, dst + 512 * i))
  398. {
  399. return false;
  400. }
  401. }
  402. return true;
  403. }
  404. sd_callback_t callback = get_stream_callback(dst, n * 512, "readSectors", sector);
  405. // Cards up to 2GB use byte addressing, SDHC cards use sector addressing
  406. uint32_t address = (type() == SD_CARD_TYPE_SDHC) ? sector : (sector * 512);
  407. uint32_t reply;
  408. if (
  409. !checkReturnOk(rp2040_sdio_command_R1(16, 512, &reply)) || // SET_BLOCKLEN
  410. !checkReturnOk(rp2040_sdio_command_R1(CMD18, address, &reply)) || // READ_MULTIPLE_BLOCK
  411. !checkReturnOk(rp2040_sdio_rx_start(dst, n)) // Prepare for reception
  412. )
  413. {
  414. return false;
  415. }
  416. do {
  417. uint32_t bytes_done;
  418. g_sdio_error = rp2040_sdio_rx_poll(&bytes_done);
  419. if (callback)
  420. {
  421. callback(m_stream_count_start + bytes_done);
  422. }
  423. } while (g_sdio_error == SDIO_BUSY);
  424. if (g_sdio_error != SDIO_OK)
  425. {
  426. log("SdioCard::readSectors(", sector, ",...,", (int)n, ") failed: ", (int)g_sdio_error);
  427. stopTransmission(true);
  428. return false;
  429. }
  430. else
  431. {
  432. return stopTransmission(true);
  433. }
  434. }
  435. // These functions are not used for SDIO mode but are needed to avoid build error.
  436. void sdCsInit(SdCsPin_t pin) {}
  437. void sdCsWrite(SdCsPin_t pin, bool level) {}
  438. // SDIO configuration for main program
  439. SdioConfig g_sd_sdio_config(DMA_SDIO);
  440. #endif