sdio.cpp 36 KB

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