sd_card_sdio.cpp 15 KB

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