scsi_accel_rp2040.cpp 26 KB

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