sd_card_sdio.cpp 15 KB

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