sd_card_sdio.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2022-2025 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 and interface for accessing SD card in SDIO mode
  22. // Used on ZuluSCSI v1.1.
  23. #include "ZuluSCSI_platform.h"
  24. #ifdef SD_USE_SDIO
  25. #include "ZuluSCSI_log.h"
  26. #include "gd32f20x_sdio.h"
  27. #include "gd32f20x_dma.h"
  28. #include "gd32_sdio_sdcard.h"
  29. #include <SdFat.h>
  30. static sd_error_enum g_sdio_error = SD_OK;
  31. static int g_sdio_error_line = 0;
  32. static uint32_t g_sdio_card_status;
  33. static uint32_t g_sdio_clk_kHz;
  34. static sdio_card_type_enum g_sdio_card_type;
  35. static uint16_t g_sdio_card_rca;
  36. static uint32_t g_sdio_sector_count;
  37. #define checkReturnOk(call) ((g_sdio_error = (call)) == SD_OK ? true : logSDError(__LINE__))
  38. static bool logSDError(int line)
  39. {
  40. g_sdio_error_line = line;
  41. dbgmsg("SDIO SD card error on line ", line, ", error code ", (int)g_sdio_error);
  42. return false;
  43. }
  44. bool SdioCard::begin(SdioConfig sdioConfig)
  45. {
  46. rcu_periph_clock_enable(RCU_SDIO);
  47. rcu_periph_clock_enable(RCU_DMA1);
  48. nvic_irq_enable(SDIO_IRQn, 0, 0);
  49. g_sdio_error = sd_init();
  50. static sd_error_enum last_error = SD_OK;
  51. if (g_sdio_error != SD_OK)
  52. {
  53. static uint32_t redisplay_error_start = millis();
  54. // Don't spam the log when main program polls for card insertion, redisplay retry every 5 seconds.
  55. if (last_error != g_sdio_error || (uint32_t)(millis() - redisplay_error_start) > 5000)
  56. {
  57. dbgmsg("sd_init() failed: ", (int)g_sdio_error);
  58. last_error = g_sdio_error;
  59. redisplay_error_start = millis();
  60. }
  61. return false;
  62. }
  63. return checkReturnOk(sd_card_information_get_short(&g_sdio_card_type, &g_sdio_card_rca))
  64. && checkReturnOk(sd_card_select_deselect(g_sdio_card_rca))
  65. && checkReturnOk(sd_cardstatus_get(&g_sdio_card_status))
  66. && checkReturnOk(sd_bus_mode_config(SDIO_BUSMODE_4BIT))
  67. && checkReturnOk(sd_transfer_mode_config(sdioConfig.useDma() ? SD_DMA_MODE : SD_POLLING_MODE))
  68. && (g_sdio_sector_count = sectorCount());
  69. }
  70. uint8_t SdioCard::errorCode() const
  71. {
  72. if (g_sdio_error == SD_OK)
  73. return SD_CARD_ERROR_NONE;
  74. else
  75. return 0x80 + g_sdio_error;
  76. }
  77. uint32_t SdioCard::errorData() const
  78. {
  79. return 0;
  80. }
  81. uint32_t SdioCard::errorLine() const
  82. {
  83. return g_sdio_error_line;
  84. }
  85. bool SdioCard::isBusy()
  86. {
  87. return (GPIO_ISTAT(SD_SDIO_DATA_PORT) & SD_SDIO_D0) == 0;
  88. }
  89. uint32_t SdioCard::kHzSdClk()
  90. {
  91. return g_sdio_clk_kHz;
  92. }
  93. bool SdioCard::readCID(cid_t* cid)
  94. {
  95. sd_cid_get((uint8_t*)cid);
  96. return true;
  97. }
  98. bool SdioCard::readCSD(csd_t* csd)
  99. {
  100. sd_csd_get((uint8_t*)csd);
  101. return true;
  102. }
  103. bool SdioCard::readSDS(sds_t* sds)
  104. {
  105. uint32_t raw_sds[64/4];
  106. if (!checkReturnOk(sd_sdstatus_get(raw_sds)))
  107. return false;
  108. // SdFat expects the data in big endian format.
  109. for (int i = 0; i < 16; i++)
  110. {
  111. ((uint8_t*)sds)[i] = (raw_sds[i / 4] >> (24 - (i % 4) * 8)) & 0xFF;
  112. }
  113. return true;
  114. }
  115. bool SdioCard::readOCR(uint32_t* ocr)
  116. {
  117. // SDIO mode does not have CMD58, but main program uses this to
  118. // poll for card presence. Return status register instead.
  119. return sd_cardstatus_get(ocr) == SD_OK;
  120. }
  121. bool SdioCard::readData(uint8_t* dst)
  122. {
  123. // logmsg("SdioCard::readData() called but not implemented!");
  124. return false;
  125. }
  126. bool SdioCard::readStart(uint32_t sector)
  127. {
  128. // logmsg("SdioCard::readStart() called but not implemented!");
  129. return false;
  130. }
  131. bool SdioCard::readStop()
  132. {
  133. // logmsg("SdioCard::readStop() called but not implemented!");
  134. return false;
  135. }
  136. uint32_t SdioCard::sectorCount()
  137. {
  138. csd_t csd;
  139. sd_csd_get((uint8_t*)&csd);
  140. return csd.capacity();
  141. }
  142. uint32_t SdioCard::status()
  143. {
  144. uint32_t status = 0;
  145. if (!checkReturnOk(sd_cardstatus_get(&status)))
  146. return 0;
  147. else
  148. return status;
  149. }
  150. bool SdioCard::stopTransmission(bool blocking)
  151. {
  152. if (!checkReturnOk(sd_transfer_stop()))
  153. return false;
  154. if (!blocking)
  155. {
  156. return true;
  157. }
  158. else
  159. {
  160. uint32_t end = millis() + 100;
  161. while (millis() < end && isBusy())
  162. {
  163. }
  164. if (isBusy())
  165. {
  166. logmsg("SdioCard::stopTransmission() timeout");
  167. return false;
  168. }
  169. else
  170. {
  171. return true;
  172. }
  173. }
  174. }
  175. bool SdioCard::syncDevice()
  176. {
  177. if (sd_transfer_state_get() != SD_NO_TRANSFER)
  178. {
  179. return stopTransmission(true);
  180. }
  181. return true;
  182. }
  183. uint8_t SdioCard::type() const
  184. {
  185. if (g_sdio_card_type == SDIO_HIGH_CAPACITY_SD_CARD)
  186. return SD_CARD_TYPE_SDHC;
  187. else if (g_sdio_card_type == SDIO_STD_CAPACITY_SD_CARD_V2_0)
  188. return SD_CARD_TYPE_SD2;
  189. else
  190. return SD_CARD_TYPE_SD1;
  191. }
  192. bool SdioCard::writeData(const uint8_t* src)
  193. {
  194. // logmsg("SdioCard::writeData() called but not implemented!");
  195. return false;
  196. }
  197. bool SdioCard::writeStart(uint32_t sector)
  198. {
  199. // logmsg("SdioCard::writeStart() called but not implemented!");
  200. return false;
  201. }
  202. bool SdioCard::writeStop()
  203. {
  204. // logmsg("SdioCard::writeStop() called but not implemented!");
  205. return false;
  206. }
  207. bool SdioCard::erase(uint32_t firstSector, uint32_t lastSector)
  208. {
  209. return checkReturnOk(sd_erase(firstSector * 512, lastSector * 512));
  210. }
  211. bool SdioCard::cardCMD6(uint32_t arg, uint8_t* status) {
  212. // logmsg("SdioCard::cardCMD6() not implemented");
  213. return false;
  214. }
  215. bool SdioCard::readSCR(scr_t* scr) {
  216. // logmsg("SdioCard::readSCR() not implemented");
  217. return false;
  218. }
  219. /* Writing and reading, with progress callback */
  220. static sd_callback_t m_stream_callback;
  221. static const uint8_t *m_stream_buffer;
  222. static uint32_t m_stream_count;
  223. static uint32_t m_stream_count_start;
  224. void platform_set_sd_callback(sd_callback_t func, const uint8_t *buffer)
  225. {
  226. m_stream_callback = func;
  227. m_stream_buffer = buffer;
  228. m_stream_count = 0;
  229. m_stream_count_start = 0;
  230. }
  231. static void sdio_callback(uint32_t complete)
  232. {
  233. if (m_stream_callback)
  234. {
  235. m_stream_callback(m_stream_count_start + complete);
  236. }
  237. }
  238. static sdio_callback_t get_stream_callback(const uint8_t *buf, uint32_t count, const char *accesstype, uint32_t sector)
  239. {
  240. m_stream_count_start = m_stream_count;
  241. if (m_stream_callback)
  242. {
  243. if (buf == m_stream_buffer + m_stream_count)
  244. {
  245. m_stream_count += count;
  246. return &sdio_callback;
  247. }
  248. else
  249. {
  250. dbgmsg("SD card ", accesstype, "(", (int)sector,
  251. ") slow transfer, buffer", (uint32_t)buf, " vs. ", (uint32_t)(m_stream_buffer + m_stream_count));
  252. return NULL;
  253. }
  254. }
  255. return NULL;
  256. }
  257. bool SdioCard::writeSector(uint32_t sector, const uint8_t* src)
  258. {
  259. return checkReturnOk(sd_block_write((uint32_t*)src, (uint64_t)sector * 512, 512,
  260. get_stream_callback(src, 512, "writeSector", sector)));
  261. }
  262. bool SdioCard::writeSectors(uint32_t sector, const uint8_t* src, size_t n)
  263. {
  264. return checkReturnOk(sd_multiblocks_write((uint32_t*)src, (uint64_t)sector * 512, 512, n,
  265. get_stream_callback(src, n * 512, "writeSectors", sector)));
  266. }
  267. bool SdioCard::readSector(uint32_t sector, uint8_t* dst)
  268. {
  269. return checkReturnOk(sd_block_read((uint32_t*)dst, (uint64_t)sector * 512, 512,
  270. get_stream_callback(dst, 512, "readSector", sector)));
  271. }
  272. bool SdioCard::readSectors(uint32_t sector, uint8_t* dst, size_t n)
  273. {
  274. if (sector + n >= g_sdio_sector_count)
  275. {
  276. // sd_multiblocks_read() seems to have trouble reading the very last sector
  277. for (int i = 0; i < n; i++)
  278. {
  279. if (!readSector(sector + i, dst + i * 512))
  280. {
  281. logmsg("End of drive read failed at ", sector, " + ", i);
  282. return false;
  283. }
  284. }
  285. return true;
  286. }
  287. return checkReturnOk(sd_multiblocks_read((uint32_t*)dst, (uint64_t)sector * 512, 512, n,
  288. get_stream_callback(dst, n * 512, "readSectors", sector)));
  289. }
  290. // Check if a DMA request for SD card read has completed.
  291. // This is used to optimize the timing of data transfers on SCSI bus.
  292. bool check_sd_read_done()
  293. {
  294. if (!m_stream_callback) return false;
  295. return (DMA_CHCTL(DMA1, DMA_CH3) & DMA_CHXCTL_CHEN)
  296. && (DMA_INTF(DMA1) & DMA_FLAG_ADD(DMA_FLAG_FTF, DMA_CH3));
  297. }
  298. // These functions are not used for SDIO mode but are needed to avoid build error.
  299. void sdCsInit(SdCsPin_t pin) {}
  300. void sdCsWrite(SdCsPin_t pin, bool level) {}
  301. // Interrupt handler for SDIO
  302. extern "C" void SDIO_IRQHandler(void)
  303. {
  304. sd_interrupts_process();
  305. }
  306. // SDIO configuration for main program
  307. SdioConfig g_sd_sdio_config(DMA_SDIO);
  308. // SDIO configuration in crash
  309. SdioConfig g_sd_sdio_config_crash(0);
  310. #endif