sd_card_sdio.cpp 8.9 KB

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