sd_card_sdio.cpp 15 KB

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