sd_card_sdio.cpp 12 KB

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