sdio.cpp 36 KB

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