sd_card_sdio.cpp 12 KB

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