sd_card_sdio.cpp 12 KB

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