sd_card_sdio.cpp 15 KB

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