sd_card_sdio.cpp 13 KB

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