sd_card_sdio.cpp 9.0 KB

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