sdio.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2022-2025 Rabbit Hole Computing™
  3. * Copyright (c) 2024 Tech by Androda, LLC
  4. *
  5. * ZuluSCSI™ firmware is licensed under the GPL version 3 or any later version. 
  6. *
  7. * https://www.gnu.org/licenses/gpl-3.0.html
  8. * ----
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version. 
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. * GNU General Public License for more details. 
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  21. **/
  22. // Implementation of SDIO communication for RP2040 and RP23XX
  23. //
  24. // The RP2040 official work-in-progress code at
  25. // https://github.com/raspberrypi/pico-extras/tree/master/src/rp2_common/pico_sd_card
  26. // may be useful reference, but this is independent implementation.
  27. //
  28. // For official SDIO specifications, refer to:
  29. // https://www.sdcard.org/downloads/pls/
  30. // "SDIO Physical Layer Simplified Specification Version 8.00"
  31. #include <BlueSCSI_platform.h>
  32. #if defined(SD_USE_SDIO) && !defined(SD_USE_RP2350_SDIO)
  33. #include "sdio.h"
  34. #include <hardware/pio.h>
  35. #include <hardware/dma.h>
  36. //#include <hardware/gpio.h>
  37. #include <hardware/structs/scb.h>
  38. #include <BlueSCSI_platform.h>
  39. #include <BlueSCSI_log.h>
  40. #include "timings_RP2MCU.h"
  41. # include "sdio_RP2MCU.pio.h"
  42. #define SDIO_PIO pio1
  43. #define SDIO_CMD_SM 0
  44. #define SDIO_DATA_SM 1
  45. #define SDIO_DMA_CH 4
  46. #define SDIO_DMA_CHB 5
  47. // If the highest SD pin is beyond the first 32 GPIOs,
  48. // set the base GPIO to 16 to use GPIOs 16-47
  49. #if SDIO_D3 > 31
  50. # define SDIO_GPIO_BASE_HIGH
  51. # define SDIO_BASE_OFFSET 16
  52. #else
  53. # define SDIO_BASE_OFFSET 0
  54. #endif
  55. // Maximum number of 512 byte blocks to transfer in one request
  56. #define SDIO_MAX_BLOCKS 256
  57. enum sdio_transfer_state_t { SDIO_IDLE, SDIO_RX, SDIO_TX, SDIO_TX_WAIT_IDLE};
  58. static struct {
  59. uint32_t pio_cmd_rsp_clk_offset;
  60. pio_sm_config pio_cfg_cmd_rsp;
  61. uint32_t pio_data_rx_offset;
  62. pio_sm_config pio_cfg_data_rx;
  63. uint32_t pio_data_tx_offset;
  64. pio_sm_config pio_cfg_data_tx;
  65. sdio_transfer_state_t transfer_state;
  66. uint32_t transfer_start_time;
  67. uint32_t *data_buf;
  68. uint32_t blocks_done; // Number of blocks transferred so far
  69. uint32_t total_blocks; // Total number of blocks to transfer
  70. uint32_t blocks_checksumed; // Number of blocks that have had CRC calculated
  71. uint32_t checksum_errors; // Number of checksum errors detected
  72. uint8_t cmdBuf[6];
  73. // Variables for block writes
  74. uint64_t next_wr_block_checksum;
  75. uint32_t end_token_buf[3]; // CRC and end token for write block
  76. sdio_status_t wr_status;
  77. uint32_t card_response;
  78. // Variables for block reads
  79. // This is used to perform DMA into data buffers and checksum buffers separately.
  80. struct {
  81. void * write_addr;
  82. uint32_t transfer_count;
  83. } dma_blocks[SDIO_MAX_BLOCKS * 2];
  84. struct {
  85. uint32_t top;
  86. uint32_t bottom;
  87. } received_checksums[SDIO_MAX_BLOCKS];
  88. } g_sdio;
  89. void rp2040_sdio_dma_irq();
  90. /*******************************************************
  91. * Checksum algorithms
  92. *******************************************************/
  93. // Table lookup for calculating CRC-7 checksum that is used in SDIO command packets.
  94. // Usage:
  95. // uint8_t crc = 0;
  96. // crc = crc7_table[crc ^ byte];
  97. // .. repeat for every byte ..
  98. static const uint8_t crc7_table[256] = {
  99. 0x00, 0x12, 0x24, 0x36, 0x48, 0x5a, 0x6c, 0x7e,
  100. 0x90, 0x82, 0xb4, 0xa6, 0xd8, 0xca, 0xfc, 0xee,
  101. 0x32, 0x20, 0x16, 0x04, 0x7a, 0x68, 0x5e, 0x4c,
  102. 0xa2, 0xb0, 0x86, 0x94, 0xea, 0xf8, 0xce, 0xdc,
  103. 0x64, 0x76, 0x40, 0x52, 0x2c, 0x3e, 0x08, 0x1a,
  104. 0xf4, 0xe6, 0xd0, 0xc2, 0xbc, 0xae, 0x98, 0x8a,
  105. 0x56, 0x44, 0x72, 0x60, 0x1e, 0x0c, 0x3a, 0x28,
  106. 0xc6, 0xd4, 0xe2, 0xf0, 0x8e, 0x9c, 0xaa, 0xb8,
  107. 0xc8, 0xda, 0xec, 0xfe, 0x80, 0x92, 0xa4, 0xb6,
  108. 0x58, 0x4a, 0x7c, 0x6e, 0x10, 0x02, 0x34, 0x26,
  109. 0xfa, 0xe8, 0xde, 0xcc, 0xb2, 0xa0, 0x96, 0x84,
  110. 0x6a, 0x78, 0x4e, 0x5c, 0x22, 0x30, 0x06, 0x14,
  111. 0xac, 0xbe, 0x88, 0x9a, 0xe4, 0xf6, 0xc0, 0xd2,
  112. 0x3c, 0x2e, 0x18, 0x0a, 0x74, 0x66, 0x50, 0x42,
  113. 0x9e, 0x8c, 0xba, 0xa8, 0xd6, 0xc4, 0xf2, 0xe0,
  114. 0x0e, 0x1c, 0x2a, 0x38, 0x46, 0x54, 0x62, 0x70,
  115. 0x82, 0x90, 0xa6, 0xb4, 0xca, 0xd8, 0xee, 0xfc,
  116. 0x12, 0x00, 0x36, 0x24, 0x5a, 0x48, 0x7e, 0x6c,
  117. 0xb0, 0xa2, 0x94, 0x86, 0xf8, 0xea, 0xdc, 0xce,
  118. 0x20, 0x32, 0x04, 0x16, 0x68, 0x7a, 0x4c, 0x5e,
  119. 0xe6, 0xf4, 0xc2, 0xd0, 0xae, 0xbc, 0x8a, 0x98,
  120. 0x76, 0x64, 0x52, 0x40, 0x3e, 0x2c, 0x1a, 0x08,
  121. 0xd4, 0xc6, 0xf0, 0xe2, 0x9c, 0x8e, 0xb8, 0xaa,
  122. 0x44, 0x56, 0x60, 0x72, 0x0c, 0x1e, 0x28, 0x3a,
  123. 0x4a, 0x58, 0x6e, 0x7c, 0x02, 0x10, 0x26, 0x34,
  124. 0xda, 0xc8, 0xfe, 0xec, 0x92, 0x80, 0xb6, 0xa4,
  125. 0x78, 0x6a, 0x5c, 0x4e, 0x30, 0x22, 0x14, 0x06,
  126. 0xe8, 0xfa, 0xcc, 0xde, 0xa0, 0xb2, 0x84, 0x96,
  127. 0x2e, 0x3c, 0x0a, 0x18, 0x66, 0x74, 0x42, 0x50,
  128. 0xbe, 0xac, 0x9a, 0x88, 0xf6, 0xe4, 0xd2, 0xc0,
  129. 0x1c, 0x0e, 0x38, 0x2a, 0x54, 0x46, 0x70, 0x62,
  130. 0x8c, 0x9e, 0xa8, 0xba, 0xc4, 0xd6, 0xe0, 0xf2
  131. };
  132. // Calculate the CRC16 checksum for parallel 4 bit lines separately.
  133. // When the SDIO bus operates in 4-bit mode, the CRC16 algorithm
  134. // is applied to each line separately and generates total of
  135. // 4 x 16 = 64 bits of checksum.
  136. __attribute__((optimize("O3")))
  137. uint64_t sdio_crc16_4bit_checksum(uint32_t *data, uint32_t num_words)
  138. {
  139. uint64_t crc = 0;
  140. uint32_t *end = data + num_words;
  141. while (data < end)
  142. {
  143. for (int unroll = 0; unroll < 4; unroll++)
  144. {
  145. // Each 32-bit word contains 8 bits per line.
  146. // Reverse the bytes because SDIO protocol is big-endian.
  147. uint32_t data_in = __builtin_bswap32(*data++);
  148. // Shift out 8 bits for each line
  149. uint32_t data_out = crc >> 32;
  150. crc <<= 32;
  151. // XOR outgoing data to itself with 4 bit delay
  152. data_out ^= (data_out >> 16);
  153. // XOR incoming data to outgoing data with 4 bit delay
  154. data_out ^= (data_in >> 16);
  155. // XOR outgoing and incoming data to accumulator at each tap
  156. uint64_t xorred = data_out ^ data_in;
  157. crc ^= xorred;
  158. crc ^= xorred << (5 * 4);
  159. crc ^= xorred << (12 * 4);
  160. }
  161. }
  162. return crc;
  163. }
  164. /*******************************************************
  165. * Clock Runner
  166. *******************************************************/
  167. void cycleSdClock() {
  168. pio_sm_exec(SDIO_PIO, SDIO_CMD_SM, pio_encode_nop() | pio_encode_sideset_opt(1, 1) | pio_encode_delay(1));
  169. pio_sm_exec(SDIO_PIO, SDIO_CMD_SM, pio_encode_nop() | pio_encode_sideset_opt(1, 0) | pio_encode_delay(1));
  170. }
  171. /*******************************************************
  172. * Status Register Receiver
  173. *******************************************************/
  174. sdio_status_t receive_status_register(uint8_t* sds) {
  175. rp2040_sdio_rx_start(sds, 1, 64);
  176. // Wait for the DMA operation to complete, or fail if it took too long
  177. waitagain:
  178. while (dma_channel_is_busy(SDIO_DMA_CHB) || dma_channel_is_busy(SDIO_DMA_CH))
  179. {
  180. if ((uint32_t)(millis() - g_sdio.transfer_start_time) > 2)
  181. {
  182. // Reset the state machine program
  183. dma_channel_abort(SDIO_DMA_CHB);
  184. pio_sm_set_enabled(SDIO_PIO, SDIO_CMD_SM, false);
  185. pio_sm_clear_fifos(SDIO_PIO, SDIO_CMD_SM);
  186. return SDIO_ERR_RESPONSE_TIMEOUT;
  187. }
  188. }
  189. // Assert that both DMA channels are complete
  190. if(dma_channel_is_busy(SDIO_DMA_CHB) || dma_channel_is_busy(SDIO_DMA_CH)) {
  191. // Wait failure, go back.
  192. goto waitagain;
  193. }
  194. pio_sm_set_enabled(SDIO_PIO, SDIO_DATA_SM, false);
  195. g_sdio.transfer_state = SDIO_IDLE;
  196. return SDIO_OK;
  197. }
  198. /*******************************************************
  199. * Basic SDIO command execution
  200. *******************************************************/
  201. static void sdio_send_command(uint8_t command, uint32_t arg, uint8_t response_bits)
  202. {
  203. // if (command != 41 && command != 55) {
  204. // log("C: ", (int)command, " A: ", arg);
  205. // }
  206. io_wo_8* txFifo = reinterpret_cast<io_wo_8*>(&SDIO_PIO->txf[SDIO_CMD_SM]);
  207. // Reinitialize the CMD SM
  208. pio_sm_init(SDIO_PIO, SDIO_CMD_SM, g_sdio.pio_cmd_rsp_clk_offset, &g_sdio.pio_cfg_cmd_rsp);
  209. pio_sm_set_consecutive_pindirs(SDIO_PIO, SDIO_CMD_SM, SDIO_CLK, 1, true);
  210. pio_sm_set_consecutive_pindirs(SDIO_PIO, SDIO_CMD_SM, SDIO_CMD, 1, true);
  211. pio_sm_set_consecutive_pindirs(SDIO_PIO, SDIO_CMD_SM, SDIO_D0, 4, false);
  212. // Pin direction: output, initial state should be high
  213. pio_sm_exec(SDIO_PIO, SDIO_CMD_SM, pio_encode_set(pio_pins, 1));
  214. pio_sm_exec(SDIO_PIO, SDIO_CMD_SM, pio_encode_set(pio_pindirs, 1));
  215. // Write the number of tx / rx bits to the SM
  216. *txFifo = 55; // Write 56 bits total
  217. pio_sm_exec(SDIO_PIO, SDIO_CMD_SM, pio_encode_out(pio_x, 8));
  218. *txFifo = response_bits ? response_bits - 1 : 0; // Bit count to receive
  219. pio_sm_exec(SDIO_PIO, SDIO_CMD_SM, pio_encode_out(pio_y, 8));
  220. pio_sm_set_enabled(SDIO_PIO, SDIO_CMD_SM, true);
  221. // Build the command bytes (commands are 48 bits long)
  222. g_sdio.cmdBuf[0] = command | 0x40;
  223. g_sdio.cmdBuf[1] = (uint8_t)(arg >> 24U);
  224. g_sdio.cmdBuf[2] = (uint8_t)(arg >> 16U);
  225. g_sdio.cmdBuf[3] = (uint8_t)(arg >> 8U);
  226. g_sdio.cmdBuf[4] = (uint8_t)arg;
  227. // Get the SM clocking while we calculate CRCs
  228. *txFifo = 0XFF;
  229. // CRC calculation
  230. uint8_t crc = 0;
  231. for(uint8_t i = 0; i < 5; i++) {
  232. crc = crc7_table[crc ^ g_sdio.cmdBuf[i]];
  233. }
  234. crc = crc | 0x1;
  235. g_sdio.cmdBuf[5] = crc;
  236. dma_channel_config dmacfg = dma_channel_get_default_config(SDIO_DMA_CH);
  237. channel_config_set_transfer_data_size(&dmacfg, DMA_SIZE_8);
  238. channel_config_set_read_increment(&dmacfg, true);
  239. channel_config_set_write_increment(&dmacfg, false);
  240. channel_config_set_dreq(&dmacfg, pio_get_dreq(SDIO_PIO, SDIO_CMD_SM, true));
  241. dma_channel_configure(SDIO_DMA_CH, &dmacfg, &SDIO_PIO->txf[SDIO_CMD_SM], &g_sdio.cmdBuf, 6, true);
  242. }
  243. sdio_status_t rp2040_sdio_command_R1(uint8_t command, uint32_t arg, uint32_t *response)
  244. {
  245. uint32_t resp[2];
  246. if (response) {
  247. dma_channel_config dmacfg = dma_channel_get_default_config(SDIO_DMA_CHB);
  248. channel_config_set_transfer_data_size(&dmacfg, DMA_SIZE_8);
  249. channel_config_set_read_increment(&dmacfg, false);
  250. channel_config_set_write_increment(&dmacfg, true);
  251. channel_config_set_dreq(&dmacfg, pio_get_dreq(SDIO_PIO, SDIO_CMD_SM, false)); //6 * 8 = 48 bits
  252. dma_channel_configure(SDIO_DMA_CHB, &dmacfg, &resp, &SDIO_PIO->rxf[SDIO_CMD_SM], 6, true);
  253. }
  254. sdio_send_command(command, arg, response ? 48 : 0);
  255. uint32_t start = millis();
  256. if (response)
  257. {
  258. // Wait for DMA channel to receive response
  259. while (dma_channel_is_busy(SDIO_DMA_CHB))
  260. {
  261. if ((uint32_t)(millis() - start) > 2)
  262. {
  263. if (command != 8) {
  264. logmsg("Timeout waiting for response in rp2040_sdio_command_R1(", (int)command, "), ",
  265. "PIO PC: ", (int)pio_sm_get_pc(SDIO_PIO, SDIO_CMD_SM) - (int)g_sdio.pio_cmd_rsp_clk_offset,
  266. " RXF: ", (int)pio_sm_get_rx_fifo_level(SDIO_PIO, SDIO_CMD_SM),
  267. " TXF: ", (int)pio_sm_get_tx_fifo_level(SDIO_PIO, SDIO_CMD_SM));
  268. }
  269. // Reset the state machine program
  270. dma_channel_abort(SDIO_DMA_CHB);
  271. pio_sm_set_enabled(SDIO_PIO, SDIO_CMD_SM, false); // Turn off the CMD SM, there was an error
  272. pio_sm_clear_fifos(SDIO_PIO, SDIO_CMD_SM);
  273. return SDIO_ERR_RESPONSE_TIMEOUT;
  274. }
  275. }
  276. // Must bswap due to 8 bit segmentation
  277. resp[0] = __builtin_bswap32(resp[0]);
  278. resp[1] = __builtin_bswap32(resp[1]) >> 16;
  279. // dbgmsg("SDIO R1 response: ", resp0, " ", resp1);
  280. // Calculate response checksum
  281. uint8_t crc = 0;
  282. crc = crc7_table[crc ^ ((resp[0] >> 24) & 0xFF)];
  283. crc = crc7_table[crc ^ ((resp[0] >> 16) & 0xFF)];
  284. crc = crc7_table[crc ^ ((resp[0] >> 8) & 0xFF)];
  285. crc = crc7_table[crc ^ ((resp[0] >> 0) & 0xFF)];
  286. crc = crc7_table[crc ^ ((resp[1] >> 8) & 0xFF)];
  287. uint8_t actual_crc = ((resp[1] >> 0) & 0xFE);
  288. if (crc != actual_crc)
  289. {
  290. dbgmsg("rp2040_sdio_command_R1(", (int)command, "): CRC error, calculated ", crc, " packet has ", actual_crc);
  291. dbgmsg("resp[0]:", resp[0], "resp[1]:", resp[1]);
  292. return SDIO_ERR_RESPONSE_CRC;
  293. }
  294. uint8_t response_cmd = ((resp[0] >> 24) & 0xFF);
  295. if (response_cmd != command && command != 41)
  296. {
  297. dbgmsg("rp2040_sdio_command_R1(", (int)command, "): received reply for ", (int)response_cmd);
  298. return SDIO_ERR_RESPONSE_CODE;
  299. }
  300. *response = ((resp[0] & 0xFFFFFF) << 8) | ((resp[1] >> 8) & 0xFF);
  301. } else {
  302. // Wait for CMD SM TX FIFO Stall (all command bits were sent)
  303. uint32_t tx_stall_flag = 1u << (PIO_FDEBUG_TXSTALL_LSB + SDIO_CMD_SM);
  304. // Clear the stall marker
  305. SDIO_PIO->fdebug = tx_stall_flag;
  306. // Wait for the stall
  307. while (!(SDIO_PIO->fdebug & tx_stall_flag)) {
  308. if ((uint32_t)(millis() - start) > 2)
  309. {
  310. if (command != 8) {
  311. logmsg("Timeout waiting for CMD TX in rp2040_sdio_command_R1(", (int)command, "), ",
  312. "PIO PC: ", (int)pio_sm_get_pc(SDIO_PIO, SDIO_CMD_SM) - (int)g_sdio.pio_cmd_rsp_clk_offset,
  313. " RXF: ", (int)pio_sm_get_rx_fifo_level(SDIO_PIO, SDIO_CMD_SM),
  314. " TXF: ", (int)pio_sm_get_tx_fifo_level(SDIO_PIO, SDIO_CMD_SM));
  315. }
  316. // Reset the state machine program
  317. pio_sm_set_enabled(SDIO_PIO, SDIO_CMD_SM, false); // Turn off the CMD SM, there was an error
  318. pio_sm_clear_fifos(SDIO_PIO, SDIO_CMD_SM);
  319. return SDIO_ERR_RESPONSE_TIMEOUT;
  320. }
  321. }
  322. }
  323. pio_sm_set_enabled(SDIO_PIO, SDIO_CMD_SM, false);
  324. pio_sm_clear_fifos(SDIO_PIO, SDIO_CMD_SM);
  325. return SDIO_OK;
  326. }
  327. sdio_status_t rp2040_sdio_command_R2(uint8_t command, uint32_t arg, uint8_t response[16])
  328. {
  329. // The response is too long to fit in the PIO FIFO, so use DMA to receive it.
  330. uint32_t response_buf[5];
  331. dma_channel_config dmacfg = dma_channel_get_default_config(SDIO_DMA_CHB);
  332. channel_config_set_transfer_data_size(&dmacfg, DMA_SIZE_8);
  333. channel_config_set_read_increment(&dmacfg, false);
  334. channel_config_set_write_increment(&dmacfg, true);
  335. channel_config_set_dreq(&dmacfg, pio_get_dreq(SDIO_PIO, SDIO_CMD_SM, false)); //17 * 8 = 136
  336. dma_channel_configure(SDIO_DMA_CHB, &dmacfg, &response_buf, &SDIO_PIO->rxf[SDIO_CMD_SM], 17, true);
  337. sdio_send_command(command, arg, 136);
  338. uint32_t start = millis();
  339. while (dma_channel_is_busy(SDIO_DMA_CHB))
  340. {
  341. if ((uint32_t)(millis() - start) > 2)
  342. {
  343. dbgmsg("Timeout waiting for response in rp2040_sdio_command_R2(", (int)command, "), ",
  344. "PIO PC: ", (int)pio_sm_get_pc(SDIO_PIO, SDIO_CMD_SM) - (int)g_sdio.pio_cmd_rsp_clk_offset,
  345. " RXF: ", (int)pio_sm_get_rx_fifo_level(SDIO_PIO, SDIO_CMD_SM),
  346. " TXF: ", (int)pio_sm_get_tx_fifo_level(SDIO_PIO, SDIO_CMD_SM));
  347. // Reset the state machine program
  348. dma_channel_abort(SDIO_DMA_CHB);
  349. pio_sm_set_enabled(SDIO_PIO, SDIO_CMD_SM, false); // Turn off the CMD SM, there was an error
  350. pio_sm_clear_fifos(SDIO_PIO, SDIO_CMD_SM);
  351. return SDIO_ERR_RESPONSE_TIMEOUT;
  352. }
  353. }
  354. pio_sm_set_enabled(SDIO_PIO, SDIO_CMD_SM, false); // Turn off the CMD SM, its job is done
  355. pio_sm_clear_fifos(SDIO_PIO, SDIO_CMD_SM);
  356. dma_channel_abort(SDIO_DMA_CHB);
  357. // Must byte swap because receiving 8-bit chunks instead of 32 bit
  358. response_buf[0] = __builtin_bswap32(response_buf[0]);
  359. response_buf[1] = __builtin_bswap32(response_buf[1]);
  360. response_buf[2] = __builtin_bswap32(response_buf[2]);
  361. response_buf[3] = __builtin_bswap32(response_buf[3]);
  362. response_buf[4] = __builtin_bswap32(response_buf[4]) >> 24;
  363. // Copy the response payload to output buffer
  364. response[0] = ((response_buf[0] >> 16) & 0xFF);
  365. response[1] = ((response_buf[0] >> 8) & 0xFF);
  366. response[2] = ((response_buf[0] >> 0) & 0xFF);
  367. response[3] = ((response_buf[1] >> 24) & 0xFF);
  368. response[4] = ((response_buf[1] >> 16) & 0xFF);
  369. response[5] = ((response_buf[1] >> 8) & 0xFF);
  370. response[6] = ((response_buf[1] >> 0) & 0xFF);
  371. response[7] = ((response_buf[2] >> 24) & 0xFF);
  372. response[8] = ((response_buf[2] >> 16) & 0xFF);
  373. response[9] = ((response_buf[2] >> 8) & 0xFF);
  374. response[10] = ((response_buf[2] >> 0) & 0xFF);
  375. response[11] = ((response_buf[3] >> 24) & 0xFF);
  376. response[12] = ((response_buf[3] >> 16) & 0xFF);
  377. response[13] = ((response_buf[3] >> 8) & 0xFF);
  378. response[14] = ((response_buf[3] >> 0) & 0xFF);
  379. response[15] = ((response_buf[4] >> 0) & 0xFF);
  380. // Calculate checksum of the payload
  381. uint8_t crc = 0;
  382. for (int i = 0; i < 15; i++)
  383. {
  384. crc = crc7_table[crc ^ response[i]];
  385. }
  386. uint8_t actual_crc = response[15] & 0xFE;
  387. if (crc != actual_crc)
  388. {
  389. dbgmsg("rp2040_sdio_command_R2(", (int)command, "): CRC error, calculated ", crc, " packet has ", actual_crc);
  390. return SDIO_ERR_RESPONSE_CRC;
  391. }
  392. uint8_t response_cmd = ((response_buf[0] >> 24) & 0xFF);
  393. if (response_cmd != 0x3F)
  394. {
  395. dbgmsg("rp2040_sdio_command_R2(", (int)command, "): Expected reply code 0x3F");
  396. return SDIO_ERR_RESPONSE_CODE;
  397. }
  398. return SDIO_OK;
  399. }
  400. sdio_status_t rp2040_sdio_command_R3(uint8_t command, uint32_t arg, uint32_t *response)
  401. {
  402. uint32_t resp[2];
  403. dma_channel_config dmacfg = dma_channel_get_default_config(SDIO_DMA_CHB);
  404. channel_config_set_transfer_data_size(&dmacfg, DMA_SIZE_8);
  405. channel_config_set_read_increment(&dmacfg, false);
  406. channel_config_set_write_increment(&dmacfg, true);
  407. channel_config_set_dreq(&dmacfg, pio_get_dreq(SDIO_PIO, SDIO_CMD_SM, false)); //6 * 8 = 48 bits
  408. dma_channel_configure(SDIO_DMA_CHB, &dmacfg, &resp, &SDIO_PIO->rxf[SDIO_CMD_SM], 6, true);
  409. sdio_send_command(command, arg, 48);
  410. // Wait for response
  411. uint32_t start = millis();
  412. while (dma_channel_is_busy(SDIO_DMA_CHB))
  413. {
  414. if ((uint32_t)(millis() - start) > 2)
  415. {
  416. dbgmsg("Timeout waiting for response in rp2040_sdio_command_R3(", (int)command, "), ",
  417. "PIO PC: ", (int)pio_sm_get_pc(SDIO_PIO, SDIO_CMD_SM) - (int)g_sdio.pio_cmd_rsp_clk_offset,
  418. " RXF: ", (int)pio_sm_get_rx_fifo_level(SDIO_PIO, SDIO_CMD_SM),
  419. " TXF: ", (int)pio_sm_get_tx_fifo_level(SDIO_PIO, SDIO_CMD_SM));
  420. // Reset the state machine program
  421. dma_channel_abort(SDIO_DMA_CHB);
  422. pio_sm_set_enabled(SDIO_PIO, SDIO_CMD_SM, false); // Turn off the CMD SM, there was an error
  423. pio_sm_clear_fifos(SDIO_PIO, SDIO_CMD_SM);
  424. return SDIO_ERR_RESPONSE_TIMEOUT;
  425. }
  426. }
  427. pio_sm_set_enabled(SDIO_PIO, SDIO_CMD_SM, false); // Turn off the CMD SM, its job is done
  428. pio_sm_clear_fifos(SDIO_PIO, SDIO_CMD_SM);
  429. // Must bswap due to 8 bit transfer
  430. resp[0] = __builtin_bswap32(resp[0]);
  431. resp[1] = __builtin_bswap32(resp[1]) >> 16;
  432. *response = ((resp[0] & 0xFFFFFF) << 8) | ((resp[1] >> 8) & 0xFF);
  433. // debuglog("SDIO R3 response: ", resp0, " ", resp1);
  434. return SDIO_OK;
  435. }
  436. /*******************************************************
  437. * Data reception from SD card
  438. *******************************************************/
  439. sdio_status_t rp2040_sdio_rx_start(uint8_t *buffer, uint32_t num_blocks, uint32_t block_size)
  440. {
  441. // Buffer must be aligned
  442. assert(((uint32_t)buffer & 3) == 0 && num_blocks <= SDIO_MAX_BLOCKS);
  443. g_sdio.transfer_state = SDIO_RX;
  444. g_sdio.transfer_start_time = millis();
  445. g_sdio.data_buf = (uint32_t*)buffer;
  446. g_sdio.blocks_done = 0;
  447. g_sdio.total_blocks = num_blocks;
  448. g_sdio.blocks_checksumed = 0;
  449. g_sdio.checksum_errors = 0;
  450. // Create DMA block descriptors to store each block of block_size bytes of data to buffer
  451. // and then 8 bytes to g_sdio.received_checksums.
  452. for (int i = 0; i < num_blocks; i++)
  453. {
  454. g_sdio.dma_blocks[i * 2].write_addr = buffer + i * block_size;
  455. g_sdio.dma_blocks[i * 2].transfer_count = block_size / sizeof(uint32_t);
  456. g_sdio.dma_blocks[i * 2 + 1].write_addr = &g_sdio.received_checksums[i];
  457. g_sdio.dma_blocks[i * 2 + 1].transfer_count = 2;
  458. }
  459. g_sdio.dma_blocks[num_blocks * 2].write_addr = 0;
  460. g_sdio.dma_blocks[num_blocks * 2].transfer_count = 0;
  461. // Configure first DMA channel for reading from the PIO RX fifo
  462. dma_channel_config dmacfg = dma_channel_get_default_config(SDIO_DMA_CH);
  463. channel_config_set_transfer_data_size(&dmacfg, DMA_SIZE_32);
  464. channel_config_set_read_increment(&dmacfg, false);
  465. channel_config_set_write_increment(&dmacfg, true);
  466. channel_config_set_dreq(&dmacfg, pio_get_dreq(SDIO_PIO, SDIO_DATA_SM, false));
  467. channel_config_set_bswap(&dmacfg, true);
  468. channel_config_set_chain_to(&dmacfg, SDIO_DMA_CHB);
  469. dma_channel_configure(SDIO_DMA_CH, &dmacfg, 0, &SDIO_PIO->rxf[SDIO_DATA_SM], 0, false);
  470. // Configure second DMA channel for reconfiguring the first one
  471. dmacfg = dma_channel_get_default_config(SDIO_DMA_CHB);
  472. channel_config_set_transfer_data_size(&dmacfg, DMA_SIZE_32);
  473. channel_config_set_read_increment(&dmacfg, true);
  474. channel_config_set_write_increment(&dmacfg, true);
  475. channel_config_set_ring(&dmacfg, true, 3);
  476. dma_channel_configure(SDIO_DMA_CHB, &dmacfg, &dma_hw->ch[SDIO_DMA_CH].al1_write_addr,
  477. g_sdio.dma_blocks, 2, false);
  478. // Initialize PIO state machine
  479. pio_sm_init(SDIO_PIO, SDIO_DATA_SM, g_sdio.pio_data_rx_offset, &g_sdio.pio_cfg_data_rx);
  480. pio_sm_set_consecutive_pindirs(SDIO_PIO, SDIO_DATA_SM, SDIO_CLK, 1, true);
  481. pio_sm_set_consecutive_pindirs(SDIO_PIO, SDIO_DATA_SM, SDIO_D0, 4, false);
  482. // Write number of nibbles to receive to Y register
  483. pio_sm_put(SDIO_PIO, SDIO_DATA_SM, block_size * 2 + 16 - 1);
  484. pio_sm_exec(SDIO_PIO, SDIO_DATA_SM, pio_encode_out(pio_y, 32));
  485. // Enable RX FIFO join because we don't need the TX FIFO during transfer.
  486. // This gives more leeway for the DMA block switching
  487. SDIO_PIO->sm[SDIO_DATA_SM].shiftctrl |= PIO_SM0_SHIFTCTRL_FJOIN_RX_BITS;
  488. // Start PIO and DMA
  489. dma_channel_start(SDIO_DMA_CHB);
  490. pio_sm_set_enabled(SDIO_PIO, SDIO_DATA_SM, true);
  491. return SDIO_OK;
  492. }
  493. // Check checksums for received blocks
  494. static void sdio_verify_rx_checksums(uint32_t maxcount)
  495. {
  496. while (g_sdio.blocks_checksumed < g_sdio.blocks_done && maxcount-- > 0)
  497. {
  498. // Calculate checksum from received data
  499. int blockidx = g_sdio.blocks_checksumed++;
  500. uint64_t checksum = sdio_crc16_4bit_checksum(g_sdio.data_buf + blockidx * SDIO_WORDS_PER_BLOCK,
  501. SDIO_WORDS_PER_BLOCK);
  502. // Convert received checksum to little-endian format
  503. uint32_t top = __builtin_bswap32(g_sdio.received_checksums[blockidx].top);
  504. uint32_t bottom = __builtin_bswap32(g_sdio.received_checksums[blockidx].bottom);
  505. uint64_t expected = ((uint64_t)top << 32) | bottom;
  506. if (checksum != expected)
  507. {
  508. g_sdio.checksum_errors++;
  509. if (g_sdio.checksum_errors == 1)
  510. {
  511. logmsg("SDIO checksum error in reception: block ", blockidx,
  512. " calculated ", checksum, " expected ", expected);
  513. }
  514. }
  515. }
  516. }
  517. sdio_status_t rp2040_sdio_rx_poll(uint32_t *bytes_complete)
  518. {
  519. // Was everything done when the previous rx_poll() finished?
  520. if (g_sdio.blocks_done >= g_sdio.total_blocks)
  521. {
  522. g_sdio.transfer_state = SDIO_IDLE;
  523. }
  524. else
  525. {
  526. // Use the idle time to calculate checksums
  527. sdio_verify_rx_checksums(4);
  528. // Check how many DMA control blocks have been consumed
  529. uint32_t dma_ctrl_block_count = (dma_hw->ch[SDIO_DMA_CHB].read_addr - (uint32_t)&g_sdio.dma_blocks);
  530. dma_ctrl_block_count /= sizeof(g_sdio.dma_blocks[0]);
  531. // Compute how many complete 512 byte SDIO blocks have been transferred
  532. // When transfer ends, dma_ctrl_block_count == g_sdio.total_blocks * 2 + 1
  533. g_sdio.blocks_done = (dma_ctrl_block_count - 1) / 2;
  534. // NOTE: When all blocks are done, rx_poll() still returns SDIO_BUSY once.
  535. // This provides a chance to start the SCSI transfer before the last checksums
  536. // are computed. Any checksum failures can be indicated in SCSI status after
  537. // the data transfer has finished.
  538. }
  539. if (bytes_complete)
  540. {
  541. *bytes_complete = g_sdio.blocks_done * SDIO_BLOCK_SIZE;
  542. }
  543. if (g_sdio.transfer_state == SDIO_IDLE)
  544. {
  545. pio_sm_set_enabled(SDIO_PIO, SDIO_DATA_SM, false);
  546. // Verify all remaining checksums.
  547. sdio_verify_rx_checksums(g_sdio.total_blocks);
  548. if (g_sdio.checksum_errors == 0)
  549. return SDIO_OK;
  550. else
  551. return SDIO_ERR_DATA_CRC;
  552. }
  553. else if ((uint32_t)(millis() - g_sdio.transfer_start_time) > 1000)
  554. {
  555. dbgmsg("rp2040_sdio_rx_poll() timeout, "
  556. "PIO PC: ", (int)pio_sm_get_pc(SDIO_PIO, SDIO_DATA_SM) - (int)g_sdio.pio_data_rx_offset,
  557. " RXF: ", (int)pio_sm_get_rx_fifo_level(SDIO_PIO, SDIO_DATA_SM),
  558. " TXF: ", (int)pio_sm_get_tx_fifo_level(SDIO_PIO, SDIO_DATA_SM),
  559. " DMA CNT: ", dma_hw->ch[SDIO_DMA_CH].al2_transfer_count,
  560. " BD: ", g_sdio.blocks_done);
  561. rp2040_sdio_stop();
  562. return SDIO_ERR_DATA_TIMEOUT;
  563. }
  564. return SDIO_BUSY;
  565. }
  566. /*******************************************************
  567. * Data transmission to SD card
  568. *******************************************************/
  569. static void sdio_start_next_block_tx()
  570. {
  571. // Initialize PIOs
  572. pio_sm_init(SDIO_PIO, SDIO_CMD_SM, g_sdio.pio_data_tx_offset, &g_sdio.pio_cfg_data_tx);
  573. // Re-set the pin direction things here
  574. pio_sm_set_pins(SDIO_PIO, SDIO_CMD_SM, 0xF);
  575. pio_sm_set_consecutive_pindirs(SDIO_PIO, SDIO_CMD_SM, SDIO_CLK, 1, true);
  576. pio_sm_set_consecutive_pindirs(SDIO_PIO, SDIO_CMD_SM, SDIO_D0, 4, true);
  577. // Configure DMA to send the data block payload (512 bytes)
  578. dma_channel_config dmacfg = dma_channel_get_default_config(SDIO_DMA_CH);
  579. channel_config_set_transfer_data_size(&dmacfg, DMA_SIZE_32);
  580. channel_config_set_read_increment(&dmacfg, true);
  581. channel_config_set_write_increment(&dmacfg, false);
  582. channel_config_set_dreq(&dmacfg, pio_get_dreq(SDIO_PIO, SDIO_CMD_SM, true));
  583. channel_config_set_bswap(&dmacfg, true);
  584. channel_config_set_chain_to(&dmacfg, SDIO_DMA_CHB);
  585. dma_channel_configure(SDIO_DMA_CH, &dmacfg,
  586. &SDIO_PIO->txf[SDIO_CMD_SM], g_sdio.data_buf + g_sdio.blocks_done * SDIO_WORDS_PER_BLOCK,
  587. SDIO_WORDS_PER_BLOCK, false);
  588. // Prepare second DMA channel to send the CRC and block end marker
  589. uint64_t crc = g_sdio.next_wr_block_checksum;
  590. g_sdio.end_token_buf[0] = (uint32_t)(crc >> 32);
  591. g_sdio.end_token_buf[1] = (uint32_t)(crc >> 0);
  592. g_sdio.end_token_buf[2] = 0xFFFFFFFF;
  593. channel_config_set_bswap(&dmacfg, false);
  594. dma_channel_configure(SDIO_DMA_CHB, &dmacfg,
  595. &SDIO_PIO->txf[SDIO_CMD_SM], g_sdio.end_token_buf, 3, false);
  596. // Enable IRQ to trigger when block is done
  597. dma_hw->ints1 = 1 << SDIO_DMA_CHB;
  598. dma_set_irq1_channel_mask_enabled(1 << SDIO_DMA_CHB, 1);
  599. // Initialize register X with nibble count
  600. pio_sm_put(SDIO_PIO, SDIO_CMD_SM, 1048);
  601. pio_sm_exec(SDIO_PIO, SDIO_CMD_SM, pio_encode_out(pio_x, 32));
  602. // Initialize CRC receiver Y bit count
  603. pio_sm_put(SDIO_PIO, SDIO_CMD_SM, 7);
  604. pio_sm_exec(SDIO_PIO, SDIO_CMD_SM, pio_encode_out(pio_y, 32));
  605. // Initialize pins to output and high
  606. pio_sm_exec(SDIO_PIO, SDIO_CMD_SM, pio_encode_set(pio_pins, 15));
  607. pio_sm_exec(SDIO_PIO, SDIO_CMD_SM, pio_encode_set(pio_pindirs, 15));
  608. // Write start token and start the DMA transfer.
  609. pio_sm_put(SDIO_PIO, SDIO_CMD_SM, 0xFFFFFFF0);
  610. dma_channel_start(SDIO_DMA_CH);
  611. // Start state machine
  612. pio_set_sm_mask_enabled(SDIO_PIO, (1ul << SDIO_CMD_SM)/* | (1ul << SDIO_DATA_SM)*/, true);
  613. }
  614. static void sdio_compute_next_tx_checksum()
  615. {
  616. assert (g_sdio.blocks_done < g_sdio.total_blocks && g_sdio.blocks_checksumed < g_sdio.total_blocks);
  617. int blockidx = g_sdio.blocks_checksumed++;
  618. g_sdio.next_wr_block_checksum = sdio_crc16_4bit_checksum(g_sdio.data_buf + blockidx * SDIO_WORDS_PER_BLOCK,
  619. SDIO_WORDS_PER_BLOCK);
  620. }
  621. // Start transferring data from memory to SD card
  622. sdio_status_t rp2040_sdio_tx_start(const uint8_t *buffer, uint32_t num_blocks)
  623. {
  624. // Buffer must be aligned
  625. assert(((uint32_t)buffer & 3) == 0 && num_blocks <= SDIO_MAX_BLOCKS);
  626. g_sdio.transfer_state = SDIO_TX;
  627. g_sdio.transfer_start_time = millis();
  628. g_sdio.data_buf = (uint32_t*)buffer;
  629. g_sdio.blocks_done = 0;
  630. g_sdio.total_blocks = num_blocks;
  631. g_sdio.blocks_checksumed = 0;
  632. g_sdio.checksum_errors = 0;
  633. // Compute first block checksum
  634. sdio_compute_next_tx_checksum();
  635. // Start first DMA transfer and PIO
  636. sdio_start_next_block_tx();
  637. if (g_sdio.blocks_checksumed < g_sdio.total_blocks)
  638. {
  639. // Precompute second block checksum
  640. sdio_compute_next_tx_checksum();
  641. }
  642. return SDIO_OK;
  643. }
  644. sdio_status_t check_sdio_write_response(uint32_t card_response)
  645. {
  646. uint8_t wr_status = card_response & 0x1F;
  647. // 5 = 0b0101 = data accepted (11100101)
  648. // 11 = 0b1011 = CRC error (11101011)
  649. // 13 = 0b1101 = Write Error (11101101)
  650. if (wr_status == 0b101)
  651. {
  652. return SDIO_OK;
  653. }
  654. else if (wr_status == 0b1011)
  655. {
  656. logmsg("SDIO card reports write CRC error, status ", card_response);
  657. return SDIO_ERR_WRITE_CRC;
  658. }
  659. else if (wr_status == 0b1101)
  660. {
  661. logmsg("SDIO card reports write failure, status ", card_response);
  662. return SDIO_ERR_WRITE_FAIL;
  663. }
  664. else
  665. {
  666. logmsg("SDIO card reports unknown write status ", card_response);
  667. return SDIO_ERR_WRITE_FAIL;
  668. }
  669. }
  670. // When a block finishes, this IRQ handler starts the next one
  671. static void rp2040_sdio_tx_irq()
  672. {
  673. dma_hw->ints1 = 1 << SDIO_DMA_CHB;
  674. if (g_sdio.transfer_state == SDIO_TX)
  675. {
  676. if (!dma_channel_is_busy(SDIO_DMA_CH) && !dma_channel_is_busy(SDIO_DMA_CHB))
  677. {
  678. // Main data transfer is finished now.
  679. // When card is ready, PIO will put card response on RX fifo
  680. g_sdio.transfer_state = SDIO_TX_WAIT_IDLE;
  681. if (!pio_sm_is_rx_fifo_empty(SDIO_PIO, SDIO_CMD_SM))
  682. {
  683. // Card is already idle
  684. g_sdio.card_response = pio_sm_get(SDIO_PIO, SDIO_CMD_SM);
  685. }
  686. else
  687. {
  688. // Use DMA to wait for the response
  689. dma_channel_config dmacfg = dma_channel_get_default_config(SDIO_DMA_CHB);
  690. channel_config_set_transfer_data_size(&dmacfg, DMA_SIZE_8);
  691. channel_config_set_read_increment(&dmacfg, false);
  692. channel_config_set_write_increment(&dmacfg, false);
  693. channel_config_set_dreq(&dmacfg, pio_get_dreq(SDIO_PIO, SDIO_CMD_SM, false));
  694. dma_channel_configure(SDIO_DMA_CHB, &dmacfg,
  695. &g_sdio.card_response, &SDIO_PIO->rxf[SDIO_CMD_SM], 1, true);
  696. }
  697. }
  698. }
  699. if (g_sdio.transfer_state == SDIO_TX_WAIT_IDLE)
  700. {
  701. if (!dma_channel_is_busy(SDIO_DMA_CHB))
  702. {
  703. g_sdio.wr_status = check_sdio_write_response(g_sdio.card_response);
  704. if (g_sdio.wr_status != SDIO_OK)
  705. {
  706. rp2040_sdio_stop();
  707. return;
  708. }
  709. g_sdio.blocks_done++;
  710. if (g_sdio.blocks_done < g_sdio.total_blocks)
  711. {
  712. sdio_start_next_block_tx();
  713. g_sdio.transfer_state = SDIO_TX;
  714. if (g_sdio.blocks_checksumed < g_sdio.total_blocks)
  715. {
  716. // Precompute the CRC for next block so that it is ready when
  717. // we want to send it.
  718. sdio_compute_next_tx_checksum();
  719. }
  720. }
  721. else
  722. {
  723. rp2040_sdio_stop();
  724. }
  725. }
  726. }
  727. }
  728. // Check if transmission is complete
  729. sdio_status_t rp2040_sdio_tx_poll(uint32_t *bytes_complete)
  730. {
  731. // SCB_ICSR_VECTACTIVE_Msk (0x1FFUL)
  732. if (scb_hw->icsr & (0x1FFUL))
  733. {
  734. // Verify that IRQ handler gets called even if we are in hardfault handler
  735. rp2040_sdio_tx_irq();
  736. }
  737. if (bytes_complete)
  738. {
  739. *bytes_complete = g_sdio.blocks_done * SDIO_BLOCK_SIZE;
  740. }
  741. if (g_sdio.transfer_state == SDIO_IDLE)
  742. {
  743. rp2040_sdio_stop();
  744. return g_sdio.wr_status;
  745. }
  746. else if ((uint32_t)(millis() - g_sdio.transfer_start_time) > 1000)
  747. {
  748. dbgmsg("rp2040_sdio_tx_poll() timeout, "
  749. "PIO PC: ", (int)pio_sm_get_pc(SDIO_PIO, SDIO_CMD_SM) - (int)g_sdio.pio_data_tx_offset,
  750. " RXF: ", (int)pio_sm_get_rx_fifo_level(SDIO_PIO, SDIO_CMD_SM),
  751. " TXF: ", (int)pio_sm_get_tx_fifo_level(SDIO_PIO, SDIO_CMD_SM),
  752. " DMA CNT: ", dma_hw->ch[SDIO_DMA_CH].al2_transfer_count);
  753. rp2040_sdio_stop();
  754. return SDIO_ERR_DATA_TIMEOUT;
  755. }
  756. return SDIO_BUSY;
  757. }
  758. // Force everything to idle state
  759. sdio_status_t rp2040_sdio_stop()
  760. {
  761. dma_channel_abort(SDIO_DMA_CH);
  762. dma_channel_abort(SDIO_DMA_CHB);
  763. dma_set_irq1_channel_mask_enabled(1 << SDIO_DMA_CHB, 0);
  764. pio_set_sm_mask_enabled(SDIO_PIO, (1ul << SDIO_CMD_SM) | (1ul << SDIO_DATA_SM), false);
  765. g_sdio.transfer_state = SDIO_IDLE;
  766. return SDIO_OK;
  767. }
  768. void rp2040_sdio_init(int clock_divider)
  769. {
  770. #ifdef SDIO_GPIO_BASE_HIGH
  771. pio_set_gpio_base(SDIO_PIO, 16);
  772. #endif
  773. // Mark resources as being in use, unless it has been done already.
  774. static bool resources_claimed = false;
  775. if (!resources_claimed)
  776. {
  777. pio_sm_claim(SDIO_PIO, SDIO_CMD_SM);
  778. pio_sm_claim(SDIO_PIO, SDIO_DATA_SM);
  779. dma_channel_claim(SDIO_DMA_CH);
  780. dma_channel_claim(SDIO_DMA_CHB);
  781. resources_claimed = true;
  782. }
  783. memset(&g_sdio, 0, sizeof(g_sdio));
  784. dma_channel_abort(SDIO_DMA_CH);
  785. dma_channel_abort(SDIO_DMA_CHB);
  786. pio_sm_set_enabled(SDIO_PIO, SDIO_CMD_SM, false);
  787. pio_sm_set_enabled(SDIO_PIO, SDIO_DATA_SM, false);
  788. // Load PIO programs
  789. pio_clear_instruction_memory(SDIO_PIO);
  790. // Set pull resistors for all SD data lines
  791. gpio_set_pulls(SDIO_CLK, true, false);
  792. gpio_set_pulls(SDIO_CMD, true, false);
  793. gpio_set_pulls(SDIO_D0, true, false);
  794. gpio_set_pulls(SDIO_D1, true, false);
  795. gpio_set_pulls(SDIO_D2, true, false);
  796. gpio_set_pulls(SDIO_D3, true, false);
  797. // Command state machine
  798. g_sdio.pio_cmd_rsp_clk_offset = pio_add_program(SDIO_PIO, &cmd_rsp_program);
  799. g_sdio.pio_cfg_cmd_rsp = pio_cmd_rsp_program_config(g_sdio.pio_cmd_rsp_clk_offset, SDIO_CMD, SDIO_CLK, clock_divider, 0);
  800. pio_sm_init(SDIO_PIO, SDIO_CMD_SM, g_sdio.pio_cmd_rsp_clk_offset, &g_sdio.pio_cfg_cmd_rsp);
  801. pio_sm_set_pins(SDIO_PIO, SDIO_CMD_SM, 1);
  802. pio_sm_set_consecutive_pindirs(SDIO_PIO, SDIO_CMD_SM, SDIO_CLK, 1, true);
  803. pio_sm_set_consecutive_pindirs(SDIO_PIO, SDIO_CMD_SM, SDIO_CMD, 1, true);
  804. pio_sm_set_consecutive_pindirs(SDIO_PIO, SDIO_CMD_SM, SDIO_D0, 4, false);
  805. // Data reception program
  806. g_sdio.pio_data_rx_offset = pio_add_program(SDIO_PIO, &rd_data_w_clock_program);
  807. g_sdio.pio_cfg_data_rx = pio_rd_data_w_clock_program_config(g_sdio.pio_data_rx_offset, SDIO_D0, SDIO_CLK, clock_divider);
  808. // Data transmission program
  809. g_sdio.pio_data_tx_offset = pio_add_program(SDIO_PIO, &sdio_tx_w_clock_program);
  810. g_sdio.pio_cfg_data_tx = pio_sdio_tx_w_clock_program_config(g_sdio.pio_data_tx_offset, SDIO_D0, SDIO_CLK, clock_divider);
  811. // Disable SDIO pins input synchronizer.
  812. // This reduces input delay.
  813. // Because the CLK is driven synchronously to CPU clock,
  814. // there should be no metastability problems.
  815. SDIO_PIO->input_sync_bypass |= (1 << (SDIO_CLK - SDIO_BASE_OFFSET)) | (1 << (SDIO_CMD - SDIO_BASE_OFFSET))
  816. | (1 << (SDIO_D0 - SDIO_BASE_OFFSET)) | (1 << (SDIO_D1 - SDIO_BASE_OFFSET))
  817. | (1 << (SDIO_D2 - SDIO_BASE_OFFSET)) | (1 << (SDIO_D3 - SDIO_BASE_OFFSET));
  818. // Redirect GPIOs to PIO
  819. gpio_set_function(SDIO_CMD, GPIO_FUNC_PIO1);
  820. gpio_set_function(SDIO_CLK, GPIO_FUNC_PIO1);
  821. gpio_set_function(SDIO_D0, GPIO_FUNC_PIO1);
  822. gpio_set_function(SDIO_D1, GPIO_FUNC_PIO1);
  823. gpio_set_function(SDIO_D2, GPIO_FUNC_PIO1);
  824. gpio_set_function(SDIO_D3, GPIO_FUNC_PIO1);
  825. // Set up IRQ handler when DMA completes.
  826. irq_set_exclusive_handler(DMA_IRQ_1, rp2040_sdio_tx_irq);
  827. irq_set_enabled(DMA_IRQ_1, true);
  828. #if 0
  829. #ifndef ENABLE_AUDIO_OUTPUT_SPDIF
  830. irq_set_exclusive_handler(DMA_IRQ_1, rp2040_sdio_tx_irq);
  831. #else
  832. // seem to hit assertion in _exclusive_handler call due to DMA_IRQ_0 being shared?
  833. // slightly less efficient to do it this way, so investigate further at some point
  834. irq_add_shared_handler(DMA_IRQ_1, rp2040_sdio_tx_irq, 0xFF);
  835. #endif
  836. irq_set_enabled(DMA_IRQ_1, true);
  837. #endif
  838. }
  839. #endif