sd_card_sdio.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2022 Rabbit Hole Computing™
  3. *
  4. * ZuluSCSI™ firmware is licensed under the GPL version 3 or any later version. 
  5. *
  6. * https://www.gnu.org/licenses/gpl-3.0.html
  7. * ----
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version. 
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. * GNU General Public License for more details. 
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  20. **/
  21. // Driver for accessing SD card in SDIO mode on RP2040.
  22. #include "ZuluSCSI_platform.h"
  23. #ifdef SD_USE_SDIO
  24. #include "ZuluSCSI_log.h"
  25. #include "rp2040_sdio.h"
  26. #include <hardware/gpio.h>
  27. #include <SdFat.h>
  28. #include <SdCard/SdCardInfo.h>
  29. static uint32_t g_sdio_ocr; // Operating condition register from card
  30. static uint32_t g_sdio_rca; // Relative card address
  31. static cid_t g_sdio_cid;
  32. static csd_t g_sdio_csd;
  33. static int g_sdio_error_line;
  34. static sdio_status_t g_sdio_error;
  35. static uint32_t g_sdio_dma_buf[128];
  36. static uint32_t g_sdio_sector_count;
  37. #define checkReturnOk(call) ((g_sdio_error = (call)) == SDIO_OK ? true : logSDError(__LINE__))
  38. static bool logSDError(int line)
  39. {
  40. g_sdio_error_line = line;
  41. logmsg("SDIO SD card error on line ", line, ", error code ", (int)g_sdio_error);
  42. return false;
  43. }
  44. // Callback used by SCSI code for simultaneous processing
  45. static sd_callback_t m_stream_callback;
  46. static const uint8_t *m_stream_buffer;
  47. static uint32_t m_stream_count;
  48. static uint32_t m_stream_count_start;
  49. void platform_set_sd_callback(sd_callback_t func, const uint8_t *buffer)
  50. {
  51. m_stream_callback = func;
  52. m_stream_buffer = buffer;
  53. m_stream_count = 0;
  54. m_stream_count_start = 0;
  55. }
  56. static sd_callback_t get_stream_callback(const uint8_t *buf, uint32_t count, const char *accesstype, uint32_t sector)
  57. {
  58. m_stream_count_start = m_stream_count;
  59. if (m_stream_callback)
  60. {
  61. if (buf == m_stream_buffer + m_stream_count)
  62. {
  63. m_stream_count += count;
  64. return m_stream_callback;
  65. }
  66. else
  67. {
  68. dbgmsg("SD card ", accesstype, "(", (int)sector,
  69. ") slow transfer, buffer", (uint32_t)buf, " vs. ", (uint32_t)(m_stream_buffer + m_stream_count));
  70. return NULL;
  71. }
  72. }
  73. return NULL;
  74. }
  75. bool SdioCard::begin(SdioConfig sdioConfig)
  76. {
  77. uint32_t reply;
  78. sdio_status_t status;
  79. // Initialize at 1 MHz clock speed
  80. rp2040_sdio_init(25);
  81. // Establish initial connection with the card
  82. for (int retries = 0; retries < 5; retries++)
  83. {
  84. delayMicroseconds(1000);
  85. reply = 0;
  86. rp2040_sdio_command_R1(CMD0, 0, NULL); // GO_IDLE_STATE
  87. status = rp2040_sdio_command_R1(CMD8, 0x1AA, &reply); // SEND_IF_COND
  88. if (status == SDIO_OK && reply == 0x1AA)
  89. {
  90. break;
  91. }
  92. }
  93. if (reply != 0x1AA || status != SDIO_OK)
  94. {
  95. // dbgmsg("SDIO not responding to CMD8 SEND_IF_COND, status ", (int)status, " reply ", reply);
  96. return false;
  97. }
  98. // Send ACMD41 to begin card initialization and wait for it to complete
  99. uint32_t start = millis();
  100. do {
  101. if (!checkReturnOk(rp2040_sdio_command_R1(CMD55, 0, &reply)) || // APP_CMD
  102. !checkReturnOk(rp2040_sdio_command_R3(ACMD41, 0xD0040000, &g_sdio_ocr))) // 3.0V voltage
  103. // !checkReturnOk(rp2040_sdio_command_R1(ACMD41, 0xC0100000, &g_sdio_ocr)))
  104. {
  105. return false;
  106. }
  107. if ((uint32_t)(millis() - start) > 1000)
  108. {
  109. logmsg("SDIO card initialization timeout");
  110. return false;
  111. }
  112. } while (!(g_sdio_ocr & (1 << 31)));
  113. // Get CID
  114. if (!checkReturnOk(rp2040_sdio_command_R2(CMD2, 0, (uint8_t*)&g_sdio_cid)))
  115. {
  116. dbgmsg("SDIO failed to read CID");
  117. return false;
  118. }
  119. // Get relative card address
  120. if (!checkReturnOk(rp2040_sdio_command_R1(CMD3, 0, &g_sdio_rca)))
  121. {
  122. dbgmsg("SDIO failed to get RCA");
  123. return false;
  124. }
  125. // Get CSD
  126. if (!checkReturnOk(rp2040_sdio_command_R2(CMD9, g_sdio_rca, (uint8_t*)&g_sdio_csd)))
  127. {
  128. dbgmsg("SDIO failed to read CSD");
  129. return false;
  130. }
  131. g_sdio_sector_count = sectorCount();
  132. // Select card
  133. if (!checkReturnOk(rp2040_sdio_command_R1(CMD7, g_sdio_rca, &reply)))
  134. {
  135. dbgmsg("SDIO failed to select card");
  136. return false;
  137. }
  138. // Set 4-bit bus mode
  139. if (!checkReturnOk(rp2040_sdio_command_R1(CMD55, g_sdio_rca, &reply)) ||
  140. !checkReturnOk(rp2040_sdio_command_R1(ACMD6, 2, &reply)))
  141. {
  142. dbgmsg("SDIO failed to set bus width");
  143. return false;
  144. }
  145. // Increase to 25 MHz clock rate
  146. rp2040_sdio_init(1);
  147. return true;
  148. }
  149. uint8_t SdioCard::errorCode() const
  150. {
  151. return g_sdio_error;
  152. }
  153. uint32_t SdioCard::errorData() const
  154. {
  155. return 0;
  156. }
  157. uint32_t SdioCard::errorLine() const
  158. {
  159. return g_sdio_error_line;
  160. }
  161. bool SdioCard::isBusy()
  162. {
  163. return (sio_hw->gpio_in & (1 << SDIO_D0)) == 0;
  164. }
  165. uint32_t SdioCard::kHzSdClk()
  166. {
  167. return 0;
  168. }
  169. bool SdioCard::readCID(cid_t* cid)
  170. {
  171. *cid = g_sdio_cid;
  172. return true;
  173. }
  174. bool SdioCard::readCSD(csd_t* csd)
  175. {
  176. *csd = g_sdio_csd;
  177. return true;
  178. }
  179. bool SdioCard::readOCR(uint32_t* ocr)
  180. {
  181. // SDIO mode does not have CMD58, but main program uses this to
  182. // poll for card presence. Return status register instead.
  183. return checkReturnOk(rp2040_sdio_command_R1(CMD13, g_sdio_rca, ocr));
  184. }
  185. bool SdioCard::readData(uint8_t* dst)
  186. {
  187. logmsg("SdioCard::readData() called but not implemented!");
  188. return false;
  189. }
  190. bool SdioCard::readStart(uint32_t sector)
  191. {
  192. logmsg("SdioCard::readStart() called but not implemented!");
  193. return false;
  194. }
  195. bool SdioCard::readStop()
  196. {
  197. logmsg("SdioCard::readStop() called but not implemented!");
  198. return false;
  199. }
  200. uint32_t SdioCard::sectorCount()
  201. {
  202. return g_sdio_csd.capacity();
  203. }
  204. uint32_t SdioCard::status()
  205. {
  206. uint32_t reply;
  207. if (checkReturnOk(rp2040_sdio_command_R1(CMD13, g_sdio_rca, &reply)))
  208. return reply;
  209. else
  210. return 0;
  211. }
  212. bool SdioCard::stopTransmission(bool blocking)
  213. {
  214. uint32_t reply;
  215. if (!checkReturnOk(rp2040_sdio_command_R1(CMD12, 0, &reply)))
  216. {
  217. return false;
  218. }
  219. if (!blocking)
  220. {
  221. return true;
  222. }
  223. else
  224. {
  225. uint32_t end = millis() + 100;
  226. while (millis() < end && isBusy())
  227. {
  228. if (m_stream_callback)
  229. {
  230. m_stream_callback(m_stream_count);
  231. }
  232. }
  233. if (isBusy())
  234. {
  235. logmsg("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. logmsg("SdioCard::writeData() called but not implemented!");
  258. return false;
  259. }
  260. bool SdioCard::writeStart(uint32_t sector)
  261. {
  262. logmsg("SdioCard::writeStart() called but not implemented!");
  263. return false;
  264. }
  265. bool SdioCard::writeStop()
  266. {
  267. logmsg("SdioCard::writeStop() called but not implemented!");
  268. return false;
  269. }
  270. bool SdioCard::erase(uint32_t firstSector, uint32_t lastSector)
  271. {
  272. logmsg("SdioCard::erase() not implemented");
  273. return false;
  274. }
  275. bool SdioCard::cardCMD6(uint32_t arg, uint8_t* status) {
  276. logmsg("SdioCard::cardCMD6() not implemented");
  277. return false;
  278. }
  279. bool SdioCard::readSCR(scr_t* scr) {
  280. logmsg("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. uint32_t reply;
  295. if (!checkReturnOk(rp2040_sdio_command_R1(16, 512, &reply)) || // SET_BLOCKLEN
  296. !checkReturnOk(rp2040_sdio_command_R1(CMD24, sector, &reply)) || // WRITE_BLOCK
  297. !checkReturnOk(rp2040_sdio_tx_start(src, 1))) // Start transmission
  298. {
  299. return false;
  300. }
  301. do {
  302. uint32_t bytes_done;
  303. g_sdio_error = rp2040_sdio_tx_poll(&bytes_done);
  304. if (callback)
  305. {
  306. callback(m_stream_count_start + bytes_done);
  307. }
  308. } while (g_sdio_error == SDIO_BUSY);
  309. if (g_sdio_error != SDIO_OK)
  310. {
  311. logmsg("SdioCard::writeSector(", sector, ") failed: ", (int)g_sdio_error);
  312. }
  313. return g_sdio_error == SDIO_OK;
  314. }
  315. bool SdioCard::writeSectors(uint32_t sector, const uint8_t* src, size_t n)
  316. {
  317. if (((uint32_t)src & 3) != 0)
  318. {
  319. // Unaligned write, execute sector-by-sector
  320. for (size_t i = 0; i < n; i++)
  321. {
  322. if (!writeSector(sector + i, src + 512 * i))
  323. {
  324. return false;
  325. }
  326. }
  327. return true;
  328. }
  329. sd_callback_t callback = get_stream_callback(src, n * 512, "writeSectors", sector);
  330. uint32_t reply;
  331. if (!checkReturnOk(rp2040_sdio_command_R1(16, 512, &reply)) || // SET_BLOCKLEN
  332. !checkReturnOk(rp2040_sdio_command_R1(CMD55, g_sdio_rca, &reply)) || // APP_CMD
  333. !checkReturnOk(rp2040_sdio_command_R1(ACMD23, n, &reply)) || // SET_WR_CLK_ERASE_COUNT
  334. !checkReturnOk(rp2040_sdio_command_R1(CMD25, sector, &reply)) || // WRITE_MULTIPLE_BLOCK
  335. !checkReturnOk(rp2040_sdio_tx_start(src, n))) // Start transmission
  336. {
  337. return false;
  338. }
  339. do {
  340. uint32_t bytes_done;
  341. g_sdio_error = rp2040_sdio_tx_poll(&bytes_done);
  342. if (callback)
  343. {
  344. callback(m_stream_count_start + bytes_done);
  345. }
  346. } while (g_sdio_error == SDIO_BUSY);
  347. if (g_sdio_error != SDIO_OK)
  348. {
  349. logmsg("SdioCard::writeSectors(", sector, ",...,", (int)n, ") failed: ", (int)g_sdio_error);
  350. stopTransmission(true);
  351. return false;
  352. }
  353. else
  354. {
  355. return stopTransmission(true);
  356. }
  357. }
  358. bool SdioCard::readSector(uint32_t sector, uint8_t* dst)
  359. {
  360. uint8_t *real_dst = dst;
  361. if (((uint32_t)dst & 3) != 0)
  362. {
  363. // Buffer is not aligned, need to memcpy() the data from a temporary buffer.
  364. dst = (uint8_t*)g_sdio_dma_buf;
  365. }
  366. sd_callback_t callback = get_stream_callback(dst, 512, "readSector", sector);
  367. uint32_t reply;
  368. if (!checkReturnOk(rp2040_sdio_command_R1(16, 512, &reply)) || // SET_BLOCKLEN
  369. !checkReturnOk(rp2040_sdio_rx_start(dst, 1)) || // Prepare for reception
  370. !checkReturnOk(rp2040_sdio_command_R1(CMD17, sector, &reply))) // READ_SINGLE_BLOCK
  371. {
  372. return false;
  373. }
  374. do {
  375. uint32_t bytes_done;
  376. g_sdio_error = rp2040_sdio_rx_poll(&bytes_done);
  377. if (callback)
  378. {
  379. callback(m_stream_count_start + bytes_done);
  380. }
  381. } while (g_sdio_error == SDIO_BUSY);
  382. if (g_sdio_error != SDIO_OK)
  383. {
  384. logmsg("SdioCard::readSector(", sector, ") failed: ", (int)g_sdio_error);
  385. }
  386. if (dst != real_dst)
  387. {
  388. memcpy(real_dst, g_sdio_dma_buf, sizeof(g_sdio_dma_buf));
  389. }
  390. return g_sdio_error == SDIO_OK;
  391. }
  392. bool SdioCard::readSectors(uint32_t sector, uint8_t* dst, size_t n)
  393. {
  394. if (((uint32_t)dst & 3) != 0 || sector + n >= g_sdio_sector_count)
  395. {
  396. // Unaligned read or end-of-drive read, execute sector-by-sector
  397. for (size_t i = 0; i < n; i++)
  398. {
  399. if (!readSector(sector + i, dst + 512 * i))
  400. {
  401. return false;
  402. }
  403. }
  404. return true;
  405. }
  406. sd_callback_t callback = get_stream_callback(dst, n * 512, "readSectors", sector);
  407. uint32_t reply;
  408. if (!checkReturnOk(rp2040_sdio_command_R1(16, 512, &reply)) || // SET_BLOCKLEN
  409. !checkReturnOk(rp2040_sdio_rx_start(dst, n)) || // Prepare for reception
  410. !checkReturnOk(rp2040_sdio_command_R1(CMD18, sector, &reply))) // READ_MULTIPLE_BLOCK
  411. {
  412. return false;
  413. }
  414. do {
  415. uint32_t bytes_done;
  416. g_sdio_error = rp2040_sdio_rx_poll(&bytes_done);
  417. if (callback)
  418. {
  419. callback(m_stream_count_start + bytes_done);
  420. }
  421. } while (g_sdio_error == SDIO_BUSY);
  422. if (g_sdio_error != SDIO_OK)
  423. {
  424. logmsg("SdioCard::readSectors(", sector, ",...,", (int)n, ") failed: ", (int)g_sdio_error);
  425. stopTransmission(true);
  426. return false;
  427. }
  428. else
  429. {
  430. return stopTransmission(true);
  431. }
  432. }
  433. // These functions are not used for SDIO mode but are needed to avoid build error.
  434. void sdCsInit(SdCsPin_t pin) {}
  435. void sdCsWrite(SdCsPin_t pin, bool level) {}
  436. // SDIO configuration for main program
  437. SdioConfig g_sd_sdio_config(DMA_SDIO);
  438. #endif