sd_card_sdio.cpp 12 KB

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