sd_card_sdio.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2022 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. logmsg("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::readOCR(uint32_t* ocr)
  100. {
  101. // SDIO mode does not have CMD58, but main program uses this to
  102. // poll for card presence. Return status register instead.
  103. return sd_cardstatus_get(ocr) == SD_OK;
  104. }
  105. bool SdioCard::readData(uint8_t* dst)
  106. {
  107. logmsg("SdioCard::readData() called but not implemented!");
  108. return false;
  109. }
  110. bool SdioCard::readStart(uint32_t sector)
  111. {
  112. logmsg("SdioCard::readStart() called but not implemented!");
  113. return false;
  114. }
  115. bool SdioCard::readStop()
  116. {
  117. logmsg("SdioCard::readStop() called but not implemented!");
  118. return false;
  119. }
  120. uint32_t SdioCard::sectorCount()
  121. {
  122. csd_t csd;
  123. sd_csd_get((uint8_t*)&csd);
  124. return csd.capacity();
  125. }
  126. uint32_t SdioCard::status()
  127. {
  128. uint32_t status = 0;
  129. if (!checkReturnOk(sd_cardstatus_get(&status)))
  130. return 0;
  131. else
  132. return status;
  133. }
  134. bool SdioCard::stopTransmission(bool blocking)
  135. {
  136. if (!checkReturnOk(sd_transfer_stop()))
  137. return false;
  138. if (!blocking)
  139. {
  140. return true;
  141. }
  142. else
  143. {
  144. uint32_t end = millis() + 100;
  145. while (millis() < end && isBusy())
  146. {
  147. }
  148. if (isBusy())
  149. {
  150. logmsg("SdioCard::stopTransmission() timeout");
  151. return false;
  152. }
  153. else
  154. {
  155. return true;
  156. }
  157. }
  158. }
  159. bool SdioCard::syncDevice()
  160. {
  161. if (sd_transfer_state_get() != SD_NO_TRANSFER)
  162. {
  163. return stopTransmission(true);
  164. }
  165. return true;
  166. }
  167. uint8_t SdioCard::type() const
  168. {
  169. if (g_sdio_card_type == SDIO_HIGH_CAPACITY_SD_CARD)
  170. return SD_CARD_TYPE_SDHC;
  171. else if (g_sdio_card_type == SDIO_STD_CAPACITY_SD_CARD_V2_0)
  172. return SD_CARD_TYPE_SD2;
  173. else
  174. return SD_CARD_TYPE_SD1;
  175. }
  176. bool SdioCard::writeData(const uint8_t* src)
  177. {
  178. logmsg("SdioCard::writeData() called but not implemented!");
  179. return false;
  180. }
  181. bool SdioCard::writeStart(uint32_t sector)
  182. {
  183. logmsg("SdioCard::writeStart() called but not implemented!");
  184. return false;
  185. }
  186. bool SdioCard::writeStop()
  187. {
  188. logmsg("SdioCard::writeStop() called but not implemented!");
  189. return false;
  190. }
  191. bool SdioCard::erase(uint32_t firstSector, uint32_t lastSector)
  192. {
  193. return checkReturnOk(sd_erase(firstSector * 512, lastSector * 512));
  194. }
  195. bool SdioCard::cardCMD6(uint32_t arg, uint8_t* status) {
  196. logmsg("SdioCard::cardCMD6() not implemented");
  197. return false;
  198. }
  199. bool SdioCard::readSCR(scr_t* scr) {
  200. logmsg("SdioCard::readSCR() not implemented");
  201. return false;
  202. }
  203. /* Writing and reading, with progress callback */
  204. static sd_callback_t m_stream_callback;
  205. static const uint8_t *m_stream_buffer;
  206. static uint32_t m_stream_count;
  207. static uint32_t m_stream_count_start;
  208. void platform_set_sd_callback(sd_callback_t func, const uint8_t *buffer)
  209. {
  210. m_stream_callback = func;
  211. m_stream_buffer = buffer;
  212. m_stream_count = 0;
  213. m_stream_count_start = 0;
  214. }
  215. static void sdio_callback(uint32_t complete)
  216. {
  217. if (m_stream_callback)
  218. {
  219. m_stream_callback(m_stream_count_start + complete);
  220. }
  221. }
  222. static sdio_callback_t get_stream_callback(const uint8_t *buf, uint32_t count, const char *accesstype, uint32_t sector)
  223. {
  224. m_stream_count_start = m_stream_count;
  225. if (m_stream_callback)
  226. {
  227. if (buf == m_stream_buffer + m_stream_count)
  228. {
  229. m_stream_count += count;
  230. return &sdio_callback;
  231. }
  232. else
  233. {
  234. dbgmsg("SD card ", accesstype, "(", (int)sector,
  235. ") slow transfer, buffer", (uint32_t)buf, " vs. ", (uint32_t)(m_stream_buffer + m_stream_count));
  236. return NULL;
  237. }
  238. }
  239. return NULL;
  240. }
  241. bool SdioCard::writeSector(uint32_t sector, const uint8_t* src)
  242. {
  243. return checkReturnOk(sd_block_write((uint32_t*)src, (uint64_t)sector * 512, 512,
  244. get_stream_callback(src, 512, "writeSector", sector)));
  245. }
  246. bool SdioCard::writeSectors(uint32_t sector, const uint8_t* src, size_t n)
  247. {
  248. return checkReturnOk(sd_multiblocks_write((uint32_t*)src, (uint64_t)sector * 512, 512, n,
  249. get_stream_callback(src, n * 512, "writeSectors", sector)));
  250. }
  251. bool SdioCard::readSector(uint32_t sector, uint8_t* dst)
  252. {
  253. return checkReturnOk(sd_block_read((uint32_t*)dst, (uint64_t)sector * 512, 512,
  254. get_stream_callback(dst, 512, "readSector", sector)));
  255. }
  256. bool SdioCard::readSectors(uint32_t sector, uint8_t* dst, size_t n)
  257. {
  258. if (sector + n >= g_sdio_sector_count)
  259. {
  260. // sd_multiblocks_read() seems to have trouble reading the very last sector
  261. for (int i = 0; i < n; i++)
  262. {
  263. if (!readSector(sector + i, dst + i * 512))
  264. {
  265. logmsg("End of drive read failed at ", sector, " + ", i);
  266. return false;
  267. }
  268. }
  269. return true;
  270. }
  271. return checkReturnOk(sd_multiblocks_read((uint32_t*)dst, (uint64_t)sector * 512, 512, n,
  272. get_stream_callback(dst, n * 512, "readSectors", sector)));
  273. }
  274. // Check if a DMA request for SD card read has completed.
  275. // This is used to optimize the timing of data transfers on SCSI bus.
  276. bool check_sd_read_done()
  277. {
  278. return (DMA_CHCTL(DMA1, DMA_CH3) & DMA_CHXCTL_CHEN)
  279. && (DMA_INTF0(DMA1) & DMA_FLAG_ADD(DMA_FLAG_FTF, DMA_CH3));
  280. }
  281. // These functions are not used for SDIO mode but are needed to avoid build error.
  282. void sdCsInit(SdCsPin_t pin) {}
  283. void sdCsWrite(SdCsPin_t pin, bool level) {}
  284. // Interrupt handler for SDIO
  285. extern "C" void SDIO_IRQHandler(void)
  286. {
  287. sd_interrupts_process();
  288. }
  289. // SDIO configuration for main program
  290. SdioConfig g_sd_sdio_config(DMA_SDIO);
  291. // SDIO configuration in crash
  292. SdioConfig g_sd_sdio_config_crash(0);
  293. #endif