sd_card_sdio.cpp 14 KB

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