scsi_accel_rp2040.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. /* Data flow in SCSI acceleration:
  2. *
  3. * 1. Application provides a buffer of bytes to send.
  4. * 2. Code in this module adds parity bit to the bytes and packs two bytes into 32 bit words.
  5. * 3. DMA controller copies the words to PIO peripheral FIFO.
  6. * 4. PIO peripheral handles low-level SCSI handshake and writes bytes and parity to GPIO.
  7. */
  8. #include "ZuluSCSI_platform.h"
  9. #include "ZuluSCSI_log.h"
  10. #include "scsi_accel_rp2040.h"
  11. #include "scsi_accel.pio.h"
  12. #include <hardware/pio.h>
  13. #include <hardware/dma.h>
  14. #include <hardware/irq.h>
  15. #include <hardware/structs/iobank0.h>
  16. #include <hardware/sync.h>
  17. #include <multicore.h>
  18. #define SCSI_DMA_PIO pio0
  19. #define SCSI_DMA_SM 0
  20. #define SCSI_DMA_CH 0
  21. #define SCSI_DMA_SYNC_SM 1
  22. #define SCSI_DMA_SYNC_CH 1
  23. enum scsidma_buf_sel_t { SCSIBUF_NONE = 0, SCSIBUF_A = 1, SCSIBUF_B = 2 };
  24. #define DMA_BUF_SIZE 128
  25. static struct {
  26. uint8_t *app_buf; // Buffer provided by application
  27. uint32_t app_bytes; // Bytes available in application buffer
  28. uint32_t dma_bytes; // Bytes that have been converted to DMA buffer so far
  29. uint8_t *next_app_buf; // Next buffer from application after current one finishes
  30. uint32_t next_app_bytes; // Bytes in next buffer
  31. // Synchronous mode?
  32. int syncOffset;
  33. int syncPeriod;
  34. int syncOffsetDivider; // Autopush/autopull threshold for the write pacer state machine
  35. int syncOffsetPreload; // Number of items to preload in the RX fifo of scsi_sync_write
  36. // PIO configurations
  37. uint32_t pio_offset_async_write;
  38. uint32_t pio_offset_async_read;
  39. uint32_t pio_offset_sync_write_pacer;
  40. uint32_t pio_offset_sync_write;
  41. pio_sm_config pio_cfg_async_write;
  42. pio_sm_config pio_cfg_async_read;
  43. pio_sm_config pio_cfg_sync_write_pacer;
  44. pio_sm_config pio_cfg_sync_write;
  45. // DMA configurations
  46. dma_channel_config dma_write_config; // Data from RAM to first state machine
  47. dma_channel_config dma_write_pacer_config; // In synchronous mode only, transfer between state machines
  48. // We use two DMA buffers alternatively
  49. // The buffer contains the data bytes with parity added.
  50. scsidma_buf_sel_t dma_current_buf;
  51. uint32_t dma_countA;
  52. uint32_t dma_countB;
  53. uint32_t dma_bufA[DMA_BUF_SIZE];
  54. uint32_t dma_bufB[DMA_BUF_SIZE];
  55. // Try to offload SCSI DMA interrupts to second core if possible
  56. volatile bool core1_active;
  57. mutex_t mutex;
  58. } g_scsi_dma;
  59. enum scsidma_state_t { SCSIDMA_IDLE = 0,
  60. SCSIDMA_WRITE, SCSIDMA_WRITE_DONE,
  61. SCSIDMA_READ };
  62. static volatile scsidma_state_t g_scsi_dma_state;
  63. static bool g_channels_claimed = false;
  64. // Fill DMA buffer and return number of words ready to be transferred
  65. static uint32_t refill_dmabuf(uint32_t *buf)
  66. {
  67. if (g_scsi_dma.app_bytes == 0 && g_scsi_dma.next_app_bytes > 0)
  68. {
  69. g_scsi_dma.dma_bytes = 0;
  70. g_scsi_dma.app_buf = g_scsi_dma.next_app_buf;
  71. g_scsi_dma.app_bytes = g_scsi_dma.next_app_bytes;
  72. g_scsi_dma.next_app_buf = 0;
  73. g_scsi_dma.next_app_bytes = 0;
  74. }
  75. uint32_t count = (g_scsi_dma.app_bytes - g_scsi_dma.dma_bytes) / 2;
  76. if (count > DMA_BUF_SIZE) count = DMA_BUF_SIZE;
  77. uint16_t *src = (uint16_t*)&g_scsi_dma.app_buf[g_scsi_dma.dma_bytes];
  78. uint16_t *end = src + count;
  79. uint32_t *dst = buf;
  80. while (src < end)
  81. {
  82. uint16_t input = *src++;
  83. *dst++ = (g_scsi_parity_lookup[input & 0xFF])
  84. | ((g_scsi_parity_lookup[input >> 8]) << 16);
  85. }
  86. g_scsi_dma.dma_bytes += count * 2;
  87. // Check if this buffer has been fully processed
  88. if (g_scsi_dma.dma_bytes >= g_scsi_dma.app_bytes)
  89. {
  90. assert(g_scsi_dma.dma_bytes == g_scsi_dma.app_bytes);
  91. g_scsi_dma.dma_bytes = 0;
  92. g_scsi_dma.app_buf = g_scsi_dma.next_app_buf;
  93. g_scsi_dma.app_bytes = g_scsi_dma.next_app_bytes;
  94. g_scsi_dma.next_app_buf = 0;
  95. g_scsi_dma.next_app_bytes = 0;
  96. }
  97. return count;
  98. }
  99. // Select GPIO from PIO peripheral or from software controlled SIO
  100. static void scsidma_config_gpio()
  101. {
  102. if (g_scsi_dma_state == SCSIDMA_IDLE)
  103. {
  104. iobank0_hw->io[SCSI_IO_DB0].ctrl = GPIO_FUNC_SIO;
  105. iobank0_hw->io[SCSI_IO_DB1].ctrl = GPIO_FUNC_SIO;
  106. iobank0_hw->io[SCSI_IO_DB2].ctrl = GPIO_FUNC_SIO;
  107. iobank0_hw->io[SCSI_IO_DB3].ctrl = GPIO_FUNC_SIO;
  108. iobank0_hw->io[SCSI_IO_DB4].ctrl = GPIO_FUNC_SIO;
  109. iobank0_hw->io[SCSI_IO_DB5].ctrl = GPIO_FUNC_SIO;
  110. iobank0_hw->io[SCSI_IO_DB6].ctrl = GPIO_FUNC_SIO;
  111. iobank0_hw->io[SCSI_IO_DB7].ctrl = GPIO_FUNC_SIO;
  112. iobank0_hw->io[SCSI_IO_DBP].ctrl = GPIO_FUNC_SIO;
  113. iobank0_hw->io[SCSI_OUT_REQ].ctrl = GPIO_FUNC_SIO;
  114. }
  115. else if (g_scsi_dma_state == SCSIDMA_WRITE)
  116. {
  117. // Make sure the initial state of all pins is high and output
  118. pio_sm_set_pins(SCSI_DMA_PIO, SCSI_DMA_SM, 0x3FF);
  119. pio_sm_set_consecutive_pindirs(SCSI_DMA_PIO, SCSI_DMA_SM, 0, 10, true);
  120. iobank0_hw->io[SCSI_IO_DB0].ctrl = GPIO_FUNC_PIO0;
  121. iobank0_hw->io[SCSI_IO_DB1].ctrl = GPIO_FUNC_PIO0;
  122. iobank0_hw->io[SCSI_IO_DB2].ctrl = GPIO_FUNC_PIO0;
  123. iobank0_hw->io[SCSI_IO_DB3].ctrl = GPIO_FUNC_PIO0;
  124. iobank0_hw->io[SCSI_IO_DB4].ctrl = GPIO_FUNC_PIO0;
  125. iobank0_hw->io[SCSI_IO_DB5].ctrl = GPIO_FUNC_PIO0;
  126. iobank0_hw->io[SCSI_IO_DB6].ctrl = GPIO_FUNC_PIO0;
  127. iobank0_hw->io[SCSI_IO_DB7].ctrl = GPIO_FUNC_PIO0;
  128. iobank0_hw->io[SCSI_IO_DBP].ctrl = GPIO_FUNC_PIO0;
  129. iobank0_hw->io[SCSI_OUT_REQ].ctrl = GPIO_FUNC_PIO0;
  130. }
  131. else if (g_scsi_dma_state == SCSIDMA_READ)
  132. {
  133. // Data bus as input, REQ pin as output
  134. pio_sm_set_pins(SCSI_DMA_PIO, SCSI_DMA_SM, 0x3FF);
  135. pio_sm_set_consecutive_pindirs(SCSI_DMA_PIO, SCSI_DMA_SM, 0, 9, false);
  136. pio_sm_set_consecutive_pindirs(SCSI_DMA_PIO, SCSI_DMA_SM, 9, 1, true);
  137. iobank0_hw->io[SCSI_IO_DB0].ctrl = GPIO_FUNC_SIO;
  138. iobank0_hw->io[SCSI_IO_DB1].ctrl = GPIO_FUNC_SIO;
  139. iobank0_hw->io[SCSI_IO_DB2].ctrl = GPIO_FUNC_SIO;
  140. iobank0_hw->io[SCSI_IO_DB3].ctrl = GPIO_FUNC_SIO;
  141. iobank0_hw->io[SCSI_IO_DB4].ctrl = GPIO_FUNC_SIO;
  142. iobank0_hw->io[SCSI_IO_DB5].ctrl = GPIO_FUNC_SIO;
  143. iobank0_hw->io[SCSI_IO_DB6].ctrl = GPIO_FUNC_SIO;
  144. iobank0_hw->io[SCSI_IO_DB7].ctrl = GPIO_FUNC_SIO;
  145. iobank0_hw->io[SCSI_IO_DBP].ctrl = GPIO_FUNC_SIO;
  146. iobank0_hw->io[SCSI_OUT_REQ].ctrl = GPIO_FUNC_PIO0;
  147. }
  148. }
  149. static void start_dma_write()
  150. {
  151. // Prefill both DMA buffers
  152. g_scsi_dma.dma_countA = refill_dmabuf(g_scsi_dma.dma_bufA);
  153. g_scsi_dma.dma_countB = refill_dmabuf(g_scsi_dma.dma_bufB);
  154. if (g_scsi_dma.syncOffset == 0)
  155. {
  156. // Asynchronous mode
  157. // Start DMA from buffer A
  158. g_scsi_dma.dma_current_buf = SCSIBUF_A;
  159. dma_channel_configure(SCSI_DMA_CH,
  160. &g_scsi_dma.dma_write_config,
  161. &SCSI_DMA_PIO->txf[SCSI_DMA_SM],
  162. g_scsi_dma.dma_bufA,
  163. g_scsi_dma.dma_countA,
  164. true
  165. );
  166. // Enable state machine
  167. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_DMA_SM, true);
  168. }
  169. else
  170. {
  171. // Synchronous mode
  172. // Start DMA transfer to move dummy bits to write pacer
  173. dma_channel_configure(SCSI_DMA_SYNC_CH,
  174. &g_scsi_dma.dma_write_pacer_config,
  175. &SCSI_DMA_PIO->txf[SCSI_DMA_SYNC_CH],
  176. &SCSI_DMA_PIO->rxf[SCSI_DMA_SM],
  177. 0xFFFFFFFF,
  178. true
  179. );
  180. // Start DMA transfer to move data from buffer A to data writer
  181. g_scsi_dma.dma_current_buf = SCSIBUF_A;
  182. dma_channel_configure(SCSI_DMA_CH,
  183. &g_scsi_dma.dma_write_config,
  184. &SCSI_DMA_PIO->txf[SCSI_DMA_SM],
  185. g_scsi_dma.dma_bufA,
  186. g_scsi_dma.dma_countA,
  187. true
  188. );
  189. // Enable state machines
  190. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_DMA_SYNC_SM, true);
  191. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_DMA_SM, true);
  192. }
  193. }
  194. static void scsi_dma_write_irq()
  195. {
  196. dma_hw->ints0 = 1 << SCSI_DMA_CH;
  197. mutex_enter_blocking(&g_scsi_dma.mutex);
  198. if (g_scsi_dma.dma_current_buf == SCSIBUF_A)
  199. {
  200. // Transfer from buffer A finished
  201. g_scsi_dma.dma_countA = 0;
  202. g_scsi_dma.dma_current_buf = SCSIBUF_NONE;
  203. if (g_scsi_dma.dma_countB != 0)
  204. {
  205. // Start transferring buffer B immediately
  206. dma_channel_set_trans_count(SCSI_DMA_CH, g_scsi_dma.dma_countB, false);
  207. dma_channel_set_read_addr(SCSI_DMA_CH, g_scsi_dma.dma_bufB, true);
  208. g_scsi_dma.dma_current_buf = SCSIBUF_B;
  209. // Refill buffer A for next time
  210. g_scsi_dma.dma_countA = refill_dmabuf(g_scsi_dma.dma_bufA);
  211. }
  212. }
  213. else
  214. {
  215. // Transfer from buffer B finished
  216. g_scsi_dma.dma_countB = 0;
  217. g_scsi_dma.dma_current_buf = SCSIBUF_NONE;
  218. if (g_scsi_dma.dma_countA != 0)
  219. {
  220. // Start transferring buffer A immediately
  221. dma_channel_set_trans_count(SCSI_DMA_CH, g_scsi_dma.dma_countA, false);
  222. dma_channel_set_read_addr(SCSI_DMA_CH, g_scsi_dma.dma_bufA, true);
  223. g_scsi_dma.dma_current_buf = SCSIBUF_A;
  224. // Refill buffer B for next time
  225. g_scsi_dma.dma_countB = refill_dmabuf(g_scsi_dma.dma_bufB);
  226. }
  227. }
  228. if (g_scsi_dma.dma_current_buf == SCSIBUF_NONE)
  229. {
  230. // Both buffers are empty, check if we have more data
  231. g_scsi_dma.dma_countA = refill_dmabuf(g_scsi_dma.dma_bufA);
  232. if (g_scsi_dma.dma_countA == 0)
  233. {
  234. // End of data for DMA, but PIO may still have bytes in its buffer
  235. g_scsi_dma_state = SCSIDMA_WRITE_DONE;
  236. }
  237. else
  238. {
  239. // Start transfer from buffer A
  240. dma_channel_set_trans_count(SCSI_DMA_CH, g_scsi_dma.dma_countA, false);
  241. dma_channel_set_read_addr(SCSI_DMA_CH, g_scsi_dma.dma_bufA, true);
  242. g_scsi_dma.dma_current_buf = SCSIBUF_A;
  243. // Refill B for the next interrupt
  244. g_scsi_dma.dma_countB = refill_dmabuf(g_scsi_dma.dma_bufB);
  245. }
  246. }
  247. mutex_exit(&g_scsi_dma.mutex);
  248. }
  249. // SCSI DMA interrupts are offloaded to the second core if possible
  250. static void enable_irq_second_core()
  251. {
  252. irq_set_exclusive_handler(DMA_IRQ_0, scsi_dma_write_irq);
  253. irq_set_enabled(DMA_IRQ_0, true);
  254. g_scsi_dma.core1_active = true;
  255. }
  256. // Block the SCSI DMA interrupt from executing on either core.
  257. // Used during setting of the buffer pointers.
  258. static void scsi_dma_block_irqs()
  259. {
  260. __disable_irq();
  261. mutex_enter_blocking(&g_scsi_dma.mutex);
  262. }
  263. static void scsi_dma_unblock_irqs()
  264. {
  265. mutex_exit(&g_scsi_dma.mutex);
  266. __enable_irq();
  267. }
  268. void scsi_accel_rp2040_startWrite(const uint8_t* data, uint32_t count, volatile int *resetFlag)
  269. {
  270. // Number of bytes should always be divisible by 2.
  271. assert((count & 1) == 0);
  272. scsi_dma_block_irqs();
  273. if (g_scsi_dma_state == SCSIDMA_WRITE)
  274. {
  275. if (!g_scsi_dma.next_app_buf && data == g_scsi_dma.app_buf + g_scsi_dma.app_bytes)
  276. {
  277. // Combine with currently running request
  278. g_scsi_dma.app_bytes += count;
  279. count = 0;
  280. }
  281. else if (data == g_scsi_dma.next_app_buf + g_scsi_dma.next_app_bytes)
  282. {
  283. // Combine with queued request
  284. g_scsi_dma.next_app_bytes += count;
  285. count = 0;
  286. }
  287. else if (!g_scsi_dma.next_app_buf)
  288. {
  289. // Add as queued request
  290. g_scsi_dma.next_app_buf = (uint8_t*)data;
  291. g_scsi_dma.next_app_bytes = count;
  292. count = 0;
  293. }
  294. }
  295. scsi_dma_unblock_irqs();
  296. // Check if the request was combined
  297. if (count == 0) return;
  298. if (g_scsi_dma_state != SCSIDMA_IDLE && g_scsi_dma_state != SCSIDMA_WRITE_DONE)
  299. {
  300. // Wait for previous request to finish
  301. scsi_accel_rp2040_finishWrite(resetFlag);
  302. if (*resetFlag)
  303. {
  304. return;
  305. }
  306. }
  307. bool must_reconfig_gpio = (g_scsi_dma_state == SCSIDMA_IDLE);
  308. g_scsi_dma_state = SCSIDMA_WRITE;
  309. g_scsi_dma.app_buf = (uint8_t*)data;
  310. g_scsi_dma.app_bytes = count;
  311. g_scsi_dma.dma_bytes = 0;
  312. g_scsi_dma.next_app_buf = 0;
  313. g_scsi_dma.next_app_bytes = 0;
  314. g_scsi_dma.dma_current_buf = SCSIBUF_NONE;
  315. if (must_reconfig_gpio)
  316. {
  317. SCSI_ENABLE_DATA_OUT();
  318. if (g_scsi_dma.syncOffset == 0)
  319. {
  320. // Asynchronous write
  321. pio_sm_init(SCSI_DMA_PIO, SCSI_DMA_SM, g_scsi_dma.pio_offset_async_write, &g_scsi_dma.pio_cfg_async_write);
  322. scsidma_config_gpio();
  323. }
  324. else
  325. {
  326. // Synchronous write
  327. // First state machine writes data to SCSI bus and dummy bits to its RX fifo.
  328. // Second state machine empties the dummy bits every time ACK is received, to control the transmit pace.
  329. pio_sm_init(SCSI_DMA_PIO, SCSI_DMA_SM, g_scsi_dma.pio_offset_sync_write, &g_scsi_dma.pio_cfg_sync_write);
  330. pio_sm_init(SCSI_DMA_PIO, SCSI_DMA_SYNC_SM, g_scsi_dma.pio_offset_sync_write_pacer, &g_scsi_dma.pio_cfg_sync_write_pacer);
  331. scsidma_config_gpio();
  332. // Prefill RX fifo to set the syncOffset
  333. for (int i = 0; i < g_scsi_dma.syncOffsetPreload; i++)
  334. {
  335. pio_sm_exec(SCSI_DMA_PIO, SCSI_DMA_SM,
  336. pio_encode_push(false, false) | pio_encode_sideset(1, 1));
  337. }
  338. // Fill the pacer TX fifo
  339. // DMA should start transferring only after ACK pulses are received
  340. for (int i = 0; i < 4; i++)
  341. {
  342. pio_sm_put(SCSI_DMA_PIO, SCSI_DMA_SYNC_SM, 0);
  343. }
  344. // Fill the pacer OSR
  345. pio_sm_exec(SCSI_DMA_PIO, SCSI_DMA_SYNC_SM,
  346. pio_encode_mov(pio_osr, pio_null));
  347. }
  348. dma_channel_set_irq0_enabled(SCSI_DMA_CH, true);
  349. }
  350. start_dma_write();
  351. }
  352. bool scsi_accel_rp2040_isWriteFinished(const uint8_t* data)
  353. {
  354. // Check if everything has completed
  355. if (g_scsi_dma_state == SCSIDMA_IDLE || g_scsi_dma_state == SCSIDMA_WRITE_DONE)
  356. {
  357. return true;
  358. }
  359. if (!data)
  360. return false;
  361. // Check if this data item is still in queue.
  362. bool finished = true;
  363. scsi_dma_block_irqs();
  364. if (data >= g_scsi_dma.app_buf + g_scsi_dma.dma_bytes &&
  365. data < g_scsi_dma.app_buf + g_scsi_dma.app_bytes)
  366. {
  367. finished = false; // In current transfer
  368. }
  369. else if (data >= g_scsi_dma.next_app_buf &&
  370. data < g_scsi_dma.next_app_buf + g_scsi_dma.next_app_bytes)
  371. {
  372. finished = false; // In queued transfer
  373. }
  374. scsi_dma_unblock_irqs();
  375. return finished;
  376. }
  377. static bool scsi_accel_rp2040_isWriteDone()
  378. {
  379. // Check if data is still waiting in PIO FIFO
  380. if (!pio_sm_is_tx_fifo_empty(SCSI_DMA_PIO, SCSI_DMA_SM))
  381. return false;
  382. if (g_scsi_dma.syncOffset > 0)
  383. {
  384. // Check if all bytes of synchronous write have been acknowledged
  385. if (pio_sm_get_rx_fifo_level(SCSI_DMA_PIO, SCSI_DMA_SM) > g_scsi_dma.syncOffsetPreload)
  386. return false;
  387. }
  388. else
  389. {
  390. // Check if state machine has written out its OSR
  391. if (pio_sm_get_pc(SCSI_DMA_PIO, SCSI_DMA_SM) != g_scsi_dma.pio_offset_async_write)
  392. return false;
  393. }
  394. // Check if ACK of the final byte has finished
  395. if (SCSI_IN(ACK))
  396. return false;
  397. return true;
  398. }
  399. void scsi_accel_rp2040_stopWrite(volatile int *resetFlag)
  400. {
  401. // Wait for TX fifo to be empty and ACK to go high
  402. // For synchronous writes wait for all ACKs to be received also
  403. uint32_t start = millis();
  404. while (!scsi_accel_rp2040_isWriteDone() && !*resetFlag)
  405. {
  406. if ((uint32_t)(millis() - start) > 5000)
  407. {
  408. azlog("scsi_accel_rp2040_stopWrite() timeout, FIFO levels ",
  409. (int)pio_sm_get_tx_fifo_level(SCSI_DMA_PIO, SCSI_DMA_SM), " ",
  410. (int)pio_sm_get_rx_fifo_level(SCSI_DMA_PIO, SCSI_DMA_SM), " PC ",
  411. (int)pio_sm_get_pc(SCSI_DMA_PIO, SCSI_DMA_SM));
  412. *resetFlag = 1;
  413. break;
  414. }
  415. }
  416. dma_channel_abort(SCSI_DMA_CH);
  417. dma_channel_abort(SCSI_DMA_SYNC_CH);
  418. dma_channel_set_irq0_enabled(SCSI_DMA_CH, false);
  419. g_scsi_dma_state = SCSIDMA_IDLE;
  420. SCSI_RELEASE_DATA_REQ();
  421. scsidma_config_gpio();
  422. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_DMA_SM, false);
  423. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_DMA_SYNC_SM, false);
  424. }
  425. void scsi_accel_rp2040_finishWrite(volatile int *resetFlag)
  426. {
  427. uint32_t start = millis();
  428. while (g_scsi_dma_state != SCSIDMA_IDLE && !*resetFlag)
  429. {
  430. if ((uint32_t)(millis() - start) > 5000)
  431. {
  432. azlog("scsi_accel_rp2040_finishWrite() timeout,"
  433. " state: ", (int)g_scsi_dma_state, " ", (int)g_scsi_dma.dma_current_buf, " ", (int)g_scsi_dma.dma_countA, " ", (int)g_scsi_dma.dma_countB,
  434. " PIO PC: ", (int)pio_sm_get_pc(SCSI_DMA_PIO, SCSI_DMA_SM), " ", (int)pio_sm_get_pc(SCSI_DMA_PIO, SCSI_DMA_SYNC_SM),
  435. " PIO FIFO: ", (int)pio_sm_get_tx_fifo_level(SCSI_DMA_PIO, SCSI_DMA_SM), " ", (int)pio_sm_get_tx_fifo_level(SCSI_DMA_PIO, SCSI_DMA_SYNC_SM),
  436. " DMA counts: ", dma_hw->ch[SCSI_DMA_CH].al2_transfer_count, " ", dma_hw->ch[SCSI_DMA_SYNC_CH].al2_transfer_count);
  437. *resetFlag = 1;
  438. break;
  439. }
  440. if (g_scsi_dma_state == SCSIDMA_WRITE_DONE)
  441. {
  442. // DMA done, wait for PIO to finish also and reconfig GPIO.
  443. scsi_accel_rp2040_stopWrite(resetFlag);
  444. }
  445. }
  446. }
  447. void scsi_accel_rp2040_read(uint8_t *buf, uint32_t count, int *parityError, volatile int *resetFlag)
  448. {
  449. // The hardware would support DMA for reading from SCSI bus also, but currently
  450. // the rest of the software architecture does not. There is not much benefit
  451. // because there isn't much else to do before we get the data from the SCSI bus.
  452. //
  453. // Currently this method just reads from the PIO RX fifo directly in software loop.
  454. g_scsi_dma_state = SCSIDMA_READ;
  455. pio_sm_init(SCSI_DMA_PIO, SCSI_DMA_SM, g_scsi_dma.pio_offset_async_read, &g_scsi_dma.pio_cfg_async_read);
  456. scsidma_config_gpio();
  457. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_DMA_SM, true);
  458. // Set the number of bytes to read, must be divisible by 2.
  459. assert((count & 1) == 0);
  460. pio_sm_put(SCSI_DMA_PIO, SCSI_DMA_SM, count - 1);
  461. // Read results from PIO RX FIFO
  462. uint8_t *dst = buf;
  463. uint8_t *end = buf + count;
  464. uint32_t paritycheck = 0;
  465. while (dst < end)
  466. {
  467. if (*resetFlag)
  468. {
  469. break;
  470. }
  471. uint32_t available = pio_sm_get_rx_fifo_level(SCSI_DMA_PIO, SCSI_DMA_SM);
  472. while (available > 0)
  473. {
  474. available--;
  475. uint32_t word = pio_sm_get(SCSI_DMA_PIO, SCSI_DMA_SM);
  476. paritycheck ^= word;
  477. word = ~word;
  478. *dst++ = word & 0xFF;
  479. *dst++ = word >> 16;
  480. }
  481. }
  482. // Check parity errors in whole block
  483. // This doesn't detect if there is even number of parity errors in block.
  484. uint8_t byte0 = ~(paritycheck & 0xFF);
  485. uint8_t byte1 = ~(paritycheck >> 16);
  486. if (paritycheck != ((g_scsi_parity_lookup[byte1] << 16) | g_scsi_parity_lookup[byte0]))
  487. {
  488. azlog("Parity error in scsi_accel_rp2040_read(): ", paritycheck);
  489. *parityError = 1;
  490. }
  491. g_scsi_dma_state = SCSIDMA_IDLE;
  492. SCSI_RELEASE_DATA_REQ();
  493. scsidma_config_gpio();
  494. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_DMA_SM, false);
  495. }
  496. void scsi_accel_rp2040_init()
  497. {
  498. g_scsi_dma_state = SCSIDMA_IDLE;
  499. scsidma_config_gpio();
  500. // Mark channels as being in use, unless it has been done already
  501. if (!g_channels_claimed)
  502. {
  503. pio_sm_claim(SCSI_DMA_PIO, SCSI_DMA_SM);
  504. dma_channel_claim(SCSI_DMA_CH);
  505. mutex_init(&g_scsi_dma.mutex);
  506. g_channels_claimed = true;
  507. }
  508. // Load PIO programs
  509. pio_clear_instruction_memory(SCSI_DMA_PIO);
  510. // Asynchronous SCSI write
  511. g_scsi_dma.pio_offset_async_write = pio_add_program(SCSI_DMA_PIO, &scsi_accel_async_write_program);
  512. g_scsi_dma.pio_cfg_async_write = scsi_accel_async_write_program_get_default_config(g_scsi_dma.pio_offset_async_write);
  513. sm_config_set_out_pins(&g_scsi_dma.pio_cfg_async_write, SCSI_IO_DB0, 9);
  514. sm_config_set_sideset_pins(&g_scsi_dma.pio_cfg_async_write, SCSI_OUT_REQ);
  515. sm_config_set_fifo_join(&g_scsi_dma.pio_cfg_async_write, PIO_FIFO_JOIN_TX);
  516. sm_config_set_out_shift(&g_scsi_dma.pio_cfg_async_write, true, false, 32);
  517. // Asynchronous / synchronous SCSI read
  518. g_scsi_dma.pio_offset_async_read = pio_add_program(SCSI_DMA_PIO, &scsi_accel_async_read_program);
  519. g_scsi_dma.pio_cfg_async_read = scsi_accel_async_read_program_get_default_config(g_scsi_dma.pio_offset_async_read);
  520. sm_config_set_in_pins(&g_scsi_dma.pio_cfg_async_read, SCSI_IO_DB0);
  521. sm_config_set_sideset_pins(&g_scsi_dma.pio_cfg_async_read, SCSI_OUT_REQ);
  522. sm_config_set_out_shift(&g_scsi_dma.pio_cfg_async_read, true, false, 32);
  523. sm_config_set_in_shift(&g_scsi_dma.pio_cfg_async_read, true, true, 32);
  524. // Synchronous SCSI write pacer / ACK handler
  525. g_scsi_dma.pio_offset_sync_write_pacer = pio_add_program(SCSI_DMA_PIO, &scsi_sync_write_pacer_program);
  526. g_scsi_dma.pio_cfg_sync_write_pacer = scsi_sync_write_pacer_program_get_default_config(g_scsi_dma.pio_offset_sync_write_pacer);
  527. sm_config_set_out_shift(&g_scsi_dma.pio_cfg_sync_write_pacer, true, true, 1);
  528. // Synchronous SCSI data writer
  529. g_scsi_dma.pio_offset_sync_write = pio_add_program(SCSI_DMA_PIO, &scsi_sync_write_program);
  530. g_scsi_dma.pio_cfg_sync_write = scsi_sync_write_program_get_default_config(g_scsi_dma.pio_offset_sync_write);
  531. sm_config_set_out_pins(&g_scsi_dma.pio_cfg_sync_write, SCSI_IO_DB0, 9);
  532. sm_config_set_sideset_pins(&g_scsi_dma.pio_cfg_sync_write, SCSI_OUT_REQ);
  533. sm_config_set_out_shift(&g_scsi_dma.pio_cfg_sync_write, true, true, 32);
  534. sm_config_set_in_shift(&g_scsi_dma.pio_cfg_sync_write, true, true, 1);
  535. // Create DMA channel configuration so it can be applied quickly later
  536. dma_channel_config cfg = dma_channel_get_default_config(SCSI_DMA_CH);
  537. channel_config_set_transfer_data_size(&cfg, DMA_SIZE_32);
  538. channel_config_set_read_increment(&cfg, true);
  539. channel_config_set_write_increment(&cfg, false);
  540. channel_config_set_dreq(&cfg, pio_get_dreq(SCSI_DMA_PIO, SCSI_DMA_SM, true));
  541. g_scsi_dma.dma_write_config = cfg;
  542. // In synchronous mode a second DMA channel is used to transfer dummy bits
  543. // from first state machine to second one.
  544. cfg = dma_channel_get_default_config(SCSI_DMA_SYNC_CH);
  545. channel_config_set_transfer_data_size(&cfg, DMA_SIZE_32);
  546. channel_config_set_read_increment(&cfg, false);
  547. channel_config_set_write_increment(&cfg, false);
  548. channel_config_set_dreq(&cfg, pio_get_dreq(SCSI_DMA_PIO, SCSI_DMA_SYNC_SM, true));
  549. g_scsi_dma.dma_write_pacer_config = cfg;
  550. // Try to enable interrupt handling on second core
  551. irq_set_enabled(DMA_IRQ_0, false);
  552. g_scsi_dma.core1_active = false;
  553. multicore_reset_core1();
  554. multicore_launch_core1(&enable_irq_second_core);
  555. delay(5);
  556. if (!g_scsi_dma.core1_active)
  557. {
  558. azlog("Failed to offload SCSI DMA interrupts to second core, using first core");
  559. multicore_reset_core1();
  560. irq_set_exclusive_handler(DMA_IRQ_0, scsi_dma_write_irq);
  561. irq_set_enabled(DMA_IRQ_0, true);
  562. }
  563. }
  564. void scsi_accel_rp2040_setWriteMode(int syncOffset, int syncPeriod)
  565. {
  566. if (syncOffset != g_scsi_dma.syncOffset || syncPeriod != g_scsi_dma.syncPeriod)
  567. {
  568. g_scsi_dma.syncOffset = syncOffset;
  569. g_scsi_dma.syncPeriod = syncPeriod;
  570. if (syncOffset > 0)
  571. {
  572. // Set up offset amount to PIO state machine configs.
  573. // The RX fifo of scsi_sync_write has 4 slots.
  574. // We can preload it with 0-3 items and set the autopush threshold 1, 2, 4 ... 32
  575. // to act as a divider. This allows offsets 1 to 128 bytes.
  576. // SCSI2SD code currently only uses offsets up to 15.
  577. if (syncOffset <= 4)
  578. {
  579. g_scsi_dma.syncOffsetDivider = 1;
  580. g_scsi_dma.syncOffsetPreload = 5 - syncOffset;
  581. }
  582. else if (syncOffset <= 8)
  583. {
  584. g_scsi_dma.syncOffsetDivider = 2;
  585. g_scsi_dma.syncOffsetPreload = 5 - syncOffset / 2;
  586. }
  587. else if (syncOffset <= 16)
  588. {
  589. g_scsi_dma.syncOffsetDivider = 4;
  590. g_scsi_dma.syncOffsetPreload = 5 - syncOffset / 4;
  591. }
  592. else
  593. {
  594. g_scsi_dma.syncOffsetDivider = 4;
  595. g_scsi_dma.syncOffsetPreload = 0;
  596. }
  597. // To properly detect when all bytes have been ACKed,
  598. // we need at least one vacant slot in the FIFO.
  599. if (g_scsi_dma.syncOffsetPreload > 3)
  600. g_scsi_dma.syncOffsetPreload = 3;
  601. sm_config_set_out_shift(&g_scsi_dma.pio_cfg_sync_write_pacer, true, true, g_scsi_dma.syncOffsetDivider);
  602. sm_config_set_in_shift(&g_scsi_dma.pio_cfg_sync_write, true, true, g_scsi_dma.syncOffsetDivider);
  603. // Set up the timing parameters to PIO program
  604. // The scsi_sync_write PIO program consists of three instructions.
  605. // The delays are in clock cycles, each taking 8 ns.
  606. // delay0: Delay from data write to REQ assertion
  607. // delay1: Delay from REQ assert to REQ deassert
  608. // delay2: Delay from REQ deassert to data write
  609. int delay0, delay1, delay2;
  610. int totalDelay = syncPeriod * 4 / 8;
  611. if (syncPeriod <= 25)
  612. {
  613. // Fast SCSI timing: 30 ns assertion period, 25 ns skew delay
  614. // The hardware rise and fall time require some extra delay,
  615. // the values below are tuned based on oscilloscope measurements.
  616. delay0 = 3;
  617. delay1 = 5;
  618. delay2 = totalDelay - delay0 - delay1 - 3;
  619. if (delay2 < 0) delay2 = 0;
  620. if (delay2 > 15) delay2 = 15;
  621. }
  622. else
  623. {
  624. // Slow SCSI timing: 90 ns assertion period, 55 ns skew delay
  625. delay0 = 6;
  626. delay1 = 12;
  627. delay2 = totalDelay - delay0 - delay1 - 3;
  628. if (delay2 < 0) delay2 = 0;
  629. if (delay2 > 15) delay2 = 15;
  630. }
  631. // Patch the delay values into the instructions.
  632. // The code in scsi_accel.pio must have delay set to 0 for this to work correctly.
  633. uint16_t instr0 = scsi_sync_write_program_instructions[0] | pio_encode_delay(delay0);
  634. uint16_t instr1 = scsi_sync_write_program_instructions[1] | pio_encode_delay(delay1);
  635. uint16_t instr2 = scsi_sync_write_program_instructions[2] | pio_encode_delay(delay2);
  636. SCSI_DMA_PIO->instr_mem[g_scsi_dma.pio_offset_sync_write + 0] = instr0;
  637. SCSI_DMA_PIO->instr_mem[g_scsi_dma.pio_offset_sync_write + 1] = instr1;
  638. SCSI_DMA_PIO->instr_mem[g_scsi_dma.pio_offset_sync_write + 2] = instr2;
  639. }
  640. }
  641. }