sd_card_sdio.cpp 8.6 KB

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