scsi_accel_rp2040.cpp 26 KB

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