scsi_accel_rp2040.cpp 27 KB

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