scsi_accel_rp2040.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  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 <audio.h>
  18. #include <pico/multicore.h>
  19. // SCSI bus write acceleration uses up to 3 PIO state machines:
  20. // SM0: Convert data bytes to lookup addresses to add parity
  21. // SM1: Write data to SCSI bus
  22. // SM2: For synchronous mode only, count ACK pulses
  23. #define SCSI_DMA_PIO pio0
  24. #define SCSI_PARITY_SM 1
  25. #define SCSI_DATA_SM 2
  26. #define SCSI_SYNC_SM 3
  27. // SCSI bus write acceleration uses 3 or 4 DMA channels (data flow A->B->C->D):
  28. // A: Bytes from RAM to scsi_parity PIO
  29. // B: Addresses from scsi_parity PIO to lookup DMA READ_ADDR register
  30. // C: Lookup from g_scsi_parity_lookup and copy to scsi_accel_async_write or scsi_sync_write PIO
  31. // D: For sync transfers, scsi_sync_write to scsi_sync_write_pacer PIO
  32. //
  33. // SCSI bus read acceleration uses 4 DMA channels (data flow D->C->B->A):
  34. // A: Bytes from scsi_read_parity PIO to memory buffer
  35. // B: Lookup from g_scsi_parity_check_lookup and copy to scsi_read_parity PIO
  36. // C: Addresses from scsi_accel_read PIO to lookup DMA READ_ADDR register
  37. // D: From pacer to data state machine to trigger transfers
  38. #define SCSI_DMA_CH_A 6
  39. #define SCSI_DMA_CH_B 7
  40. #define SCSI_DMA_CH_C 8
  41. #define SCSI_DMA_CH_D 9
  42. static struct {
  43. uint8_t *app_buf; // Buffer provided by application
  44. uint32_t app_bytes; // Bytes available in application buffer
  45. uint32_t dma_bytes; // Bytes that have been scheduled for DMA so far
  46. uint8_t *next_app_buf; // Next buffer from application after current one finishes
  47. uint32_t next_app_bytes; // Bytes in next buffer
  48. // Synchronous mode?
  49. int syncOffset;
  50. int syncPeriod;
  51. int syncOffsetDivider; // Autopush/autopull threshold for the write pacer state machine
  52. int syncOffsetPreload; // Number of items to preload in the RX fifo of scsi_sync_write
  53. // PIO configurations
  54. uint32_t pio_offset_parity;
  55. uint32_t pio_offset_async_write;
  56. uint32_t pio_offset_sync_write_pacer;
  57. uint32_t pio_offset_sync_write;
  58. uint32_t pio_offset_read;
  59. uint32_t pio_offset_read_parity;
  60. uint32_t pio_offset_sync_read_pacer;
  61. pio_sm_config pio_cfg_parity;
  62. pio_sm_config pio_cfg_async_write;
  63. pio_sm_config pio_cfg_sync_write_pacer;
  64. pio_sm_config pio_cfg_sync_write;
  65. pio_sm_config pio_cfg_read;
  66. pio_sm_config pio_cfg_read_parity;
  67. pio_sm_config pio_cfg_sync_read_pacer;
  68. // DMA configurations for write
  69. dma_channel_config dmacfg_write_chA; // Data from RAM to scsi_parity PIO
  70. dma_channel_config dmacfg_write_chB; // Addresses from scsi_parity PIO to lookup DMA
  71. dma_channel_config dmacfg_write_chC; // Data from g_scsi_parity_lookup to scsi write PIO
  72. dma_channel_config dmacfg_write_chD; // In synchronous mode only, transfer between state machines
  73. // DMA configurations for read
  74. dma_channel_config dmacfg_read_chA; // Data to destination memory buffer
  75. dma_channel_config dmacfg_read_chB; // From lookup table to scsi_read_parity PIO
  76. dma_channel_config dmacfg_read_chC; // From scsi_accel_read to channel B READ_ADDR
  77. dma_channel_config dmacfg_read_chD; // From pacer to data state machine
  78. } g_scsi_dma;
  79. enum scsidma_state_t { SCSIDMA_IDLE = 0,
  80. SCSIDMA_WRITE, SCSIDMA_WRITE_DONE,
  81. SCSIDMA_READ, SCSIDMA_READ_DONE };
  82. static const char* scsidma_states[5] = {"IDLE", "WRITE", "WRITE_DONE", "READ", "READ_DONE"};
  83. static volatile scsidma_state_t g_scsi_dma_state;
  84. static bool g_channels_claimed = false;
  85. static void scsidma_config_gpio();
  86. void scsi_accel_log_state()
  87. {
  88. log("SCSI DMA state: ", scsidma_states[g_scsi_dma_state]);
  89. log("Current buffer: ", g_scsi_dma.dma_bytes, "/", g_scsi_dma.app_bytes, ", next ", g_scsi_dma.next_app_bytes, " bytes");
  90. log("SyncOffset: ", g_scsi_dma.syncOffset, " SyncPeriod ", g_scsi_dma.syncPeriod);
  91. log("PIO Parity SM:",
  92. " tx_fifo ", (int)pio_sm_get_tx_fifo_level(SCSI_DMA_PIO, SCSI_PARITY_SM),
  93. ", rx_fifo ", (int)pio_sm_get_rx_fifo_level(SCSI_DMA_PIO, SCSI_PARITY_SM),
  94. ", pc ", (int)pio_sm_get_pc(SCSI_DMA_PIO, SCSI_PARITY_SM),
  95. ", instr ", SCSI_DMA_PIO->sm[SCSI_PARITY_SM].instr);
  96. log("PIO Data SM:",
  97. " tx_fifo ", (int)pio_sm_get_tx_fifo_level(SCSI_DMA_PIO, SCSI_DATA_SM),
  98. ", rx_fifo ", (int)pio_sm_get_rx_fifo_level(SCSI_DMA_PIO, SCSI_DATA_SM),
  99. ", pc ", (int)pio_sm_get_pc(SCSI_DMA_PIO, SCSI_DATA_SM),
  100. ", instr ", SCSI_DMA_PIO->sm[SCSI_DATA_SM].instr);
  101. log("PIO Sync SM:",
  102. " tx_fifo ", (int)pio_sm_get_tx_fifo_level(SCSI_DMA_PIO, SCSI_SYNC_SM),
  103. ", rx_fifo ", (int)pio_sm_get_rx_fifo_level(SCSI_DMA_PIO, SCSI_SYNC_SM),
  104. ", pc ", (int)pio_sm_get_pc(SCSI_DMA_PIO, SCSI_SYNC_SM),
  105. ", instr ", SCSI_DMA_PIO->sm[SCSI_SYNC_SM].instr);
  106. log("DMA CH A:",
  107. " ctrl: ", dma_hw->ch[SCSI_DMA_CH_A].ctrl_trig,
  108. " count: ", dma_hw->ch[SCSI_DMA_CH_A].transfer_count);
  109. log("DMA CH B:",
  110. " ctrl: ", dma_hw->ch[SCSI_DMA_CH_B].ctrl_trig,
  111. " count: ", dma_hw->ch[SCSI_DMA_CH_B].transfer_count);
  112. log("DMA CH C:",
  113. " ctrl: ", dma_hw->ch[SCSI_DMA_CH_C].ctrl_trig,
  114. " count: ", dma_hw->ch[SCSI_DMA_CH_C].transfer_count);
  115. log("DMA CH D:",
  116. " ctrl: ", dma_hw->ch[SCSI_DMA_CH_D].ctrl_trig,
  117. " count: ", dma_hw->ch[SCSI_DMA_CH_D].transfer_count);
  118. log("GPIO states: ", sio_hw->gpio_in);
  119. }
  120. /****************************************/
  121. /* Accelerated writes to SCSI bus */
  122. /****************************************/
  123. // Load the SCSI parity state machine with the address of the parity lookup table.
  124. // Also sets up DMA channels B and C
  125. static void config_parity_sm_for_write()
  126. {
  127. // Load base address to state machine register X
  128. uint32_t addrbase = (uint32_t)&g_scsi_parity_lookup[0];
  129. assert((addrbase & 0x1FF) == 0);
  130. pio_sm_init(SCSI_DMA_PIO, SCSI_PARITY_SM, g_scsi_dma.pio_offset_parity, &g_scsi_dma.pio_cfg_parity);
  131. pio_sm_put(SCSI_DMA_PIO, SCSI_PARITY_SM, addrbase >> 9);
  132. pio_sm_exec(SCSI_DMA_PIO, SCSI_PARITY_SM, pio_encode_pull(false, false));
  133. pio_sm_exec(SCSI_DMA_PIO, SCSI_PARITY_SM, pio_encode_mov(pio_x, pio_osr));
  134. // DMA channel B will copy addresses from parity PIO to DMA channel C read address register.
  135. // It is triggered by the parity SM RX FIFO request
  136. dma_channel_configure(SCSI_DMA_CH_B,
  137. &g_scsi_dma.dmacfg_write_chB,
  138. &dma_hw->ch[SCSI_DMA_CH_C].al3_read_addr_trig,
  139. &SCSI_DMA_PIO->rxf[SCSI_PARITY_SM],
  140. 1, true);
  141. // DMA channel C will read g_scsi_parity_lookup to copy data + parity to SCSI write state machine.
  142. // It is triggered by SCSI write machine TX FIFO request and chains to re-enable channel B.
  143. dma_channel_configure(SCSI_DMA_CH_C,
  144. &g_scsi_dma.dmacfg_write_chC,
  145. &SCSI_DMA_PIO->txf[SCSI_DATA_SM],
  146. NULL,
  147. 1, false);
  148. }
  149. static void start_dma_write()
  150. {
  151. if (g_scsi_dma.app_bytes <= g_scsi_dma.dma_bytes)
  152. {
  153. // Buffer has been fully processed, swap it
  154. g_scsi_dma.dma_bytes = 0;
  155. g_scsi_dma.app_buf = g_scsi_dma.next_app_buf;
  156. g_scsi_dma.app_bytes = g_scsi_dma.next_app_bytes;
  157. g_scsi_dma.next_app_buf = 0;
  158. g_scsi_dma.next_app_bytes = 0;
  159. }
  160. // Check if we are all done.
  161. // From SCSIDMA_WRITE_DONE state we can either go to IDLE in stopWrite()
  162. // or back to WRITE in startWrite().
  163. uint32_t bytes_to_send = g_scsi_dma.app_bytes - g_scsi_dma.dma_bytes;
  164. if (bytes_to_send == 0)
  165. {
  166. g_scsi_dma_state = SCSIDMA_WRITE_DONE;
  167. return;
  168. }
  169. uint8_t *src_buf = &g_scsi_dma.app_buf[g_scsi_dma.dma_bytes];
  170. g_scsi_dma.dma_bytes += bytes_to_send;
  171. // Start DMA from current buffer to parity generator
  172. dma_channel_configure(SCSI_DMA_CH_A,
  173. &g_scsi_dma.dmacfg_write_chA,
  174. &SCSI_DMA_PIO->txf[SCSI_PARITY_SM],
  175. src_buf,
  176. bytes_to_send,
  177. true
  178. );
  179. }
  180. void scsi_accel_rp2040_startWrite(const uint8_t* data, uint32_t count, volatile int *resetFlag)
  181. {
  182. // Any read requests should be matched with a stopRead()
  183. assert(g_scsi_dma_state != SCSIDMA_READ && g_scsi_dma_state != SCSIDMA_READ_DONE);
  184. __disable_irq();
  185. if (g_scsi_dma_state == SCSIDMA_WRITE)
  186. {
  187. if (!g_scsi_dma.next_app_buf && data == g_scsi_dma.app_buf + g_scsi_dma.app_bytes)
  188. {
  189. // Combine with currently running request
  190. g_scsi_dma.app_bytes += count;
  191. count = 0;
  192. }
  193. else if (data == g_scsi_dma.next_app_buf + g_scsi_dma.next_app_bytes)
  194. {
  195. // Combine with queued request
  196. g_scsi_dma.next_app_bytes += count;
  197. count = 0;
  198. }
  199. else if (!g_scsi_dma.next_app_buf)
  200. {
  201. // Add as queued request
  202. g_scsi_dma.next_app_buf = (uint8_t*)data;
  203. g_scsi_dma.next_app_bytes = count;
  204. count = 0;
  205. }
  206. }
  207. __enable_irq();
  208. // Check if the request was combined
  209. if (count == 0) return;
  210. if (g_scsi_dma_state != SCSIDMA_IDLE && g_scsi_dma_state != SCSIDMA_WRITE_DONE)
  211. {
  212. // Wait for previous request to finish
  213. scsi_accel_rp2040_finishWrite(resetFlag);
  214. if (*resetFlag)
  215. {
  216. return;
  217. }
  218. }
  219. bool must_reconfig_gpio = (g_scsi_dma_state == SCSIDMA_IDLE);
  220. g_scsi_dma_state = SCSIDMA_WRITE;
  221. g_scsi_dma.app_buf = (uint8_t*)data;
  222. g_scsi_dma.app_bytes = count;
  223. g_scsi_dma.dma_bytes = 0;
  224. g_scsi_dma.next_app_buf = 0;
  225. g_scsi_dma.next_app_bytes = 0;
  226. if (must_reconfig_gpio)
  227. {
  228. SCSI_ENABLE_DATA_OUT();
  229. if (g_scsi_dma.syncOffset == 0)
  230. {
  231. // Asynchronous write
  232. config_parity_sm_for_write();
  233. pio_sm_init(SCSI_DMA_PIO, SCSI_DATA_SM, g_scsi_dma.pio_offset_async_write, &g_scsi_dma.pio_cfg_async_write);
  234. scsidma_config_gpio();
  235. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_DATA_SM, true);
  236. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_PARITY_SM, true);
  237. }
  238. else
  239. {
  240. // Synchronous write
  241. // Data state machine writes data to SCSI bus and dummy bits to its RX fifo.
  242. // Sync state machine empties the dummy bits every time ACK is received, to control the transmit pace.
  243. config_parity_sm_for_write();
  244. pio_sm_init(SCSI_DMA_PIO, SCSI_DATA_SM, g_scsi_dma.pio_offset_sync_write, &g_scsi_dma.pio_cfg_sync_write);
  245. 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);
  246. scsidma_config_gpio();
  247. // Prefill RX fifo to set the syncOffset
  248. for (int i = 0; i < g_scsi_dma.syncOffsetPreload; i++)
  249. {
  250. pio_sm_exec(SCSI_DMA_PIO, SCSI_DATA_SM,
  251. pio_encode_push(false, false) | pio_encode_sideset(1, 1));
  252. }
  253. // Fill the pacer TX fifo
  254. // DMA should start transferring only after ACK pulses are received
  255. for (int i = 0; i < 4; i++)
  256. {
  257. pio_sm_put(SCSI_DMA_PIO, SCSI_SYNC_SM, 0);
  258. }
  259. // Fill the pacer OSR
  260. pio_sm_exec(SCSI_DMA_PIO, SCSI_SYNC_SM,
  261. pio_encode_mov(pio_osr, pio_null));
  262. // Start DMA transfer to move dummy bits to write pacer
  263. dma_channel_configure(SCSI_DMA_CH_D,
  264. &g_scsi_dma.dmacfg_write_chD,
  265. &SCSI_DMA_PIO->txf[SCSI_SYNC_SM],
  266. &SCSI_DMA_PIO->rxf[SCSI_DATA_SM],
  267. 0xFFFFFFFF,
  268. true
  269. );
  270. // Enable state machines
  271. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_SYNC_SM, true);
  272. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_DATA_SM, true);
  273. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_PARITY_SM, true);
  274. }
  275. dma_channel_set_irq0_enabled(SCSI_DMA_CH_A, true);
  276. }
  277. start_dma_write();
  278. }
  279. bool scsi_accel_rp2040_isWriteFinished(const uint8_t* data)
  280. {
  281. // Check if everything has completed
  282. if (g_scsi_dma_state == SCSIDMA_IDLE || g_scsi_dma_state == SCSIDMA_WRITE_DONE)
  283. {
  284. return true;
  285. }
  286. if (!data)
  287. return false;
  288. // Check if this data item is still in queue.
  289. bool finished = true;
  290. __disable_irq();
  291. if (data >= g_scsi_dma.app_buf &&
  292. data < g_scsi_dma.app_buf + g_scsi_dma.app_bytes &&
  293. (uint32_t)data >= dma_hw->ch[SCSI_DMA_CH_A].al1_read_addr)
  294. {
  295. finished = false; // In current transfer
  296. }
  297. else if (data >= g_scsi_dma.next_app_buf &&
  298. data < g_scsi_dma.next_app_buf + g_scsi_dma.next_app_bytes)
  299. {
  300. finished = false; // In queued transfer
  301. }
  302. __enable_irq();
  303. return finished;
  304. }
  305. // Once DMA has finished, check if all PIO queues have been drained
  306. static bool scsi_accel_rp2040_isWriteDone()
  307. {
  308. // Check if data is still waiting in PIO FIFO
  309. if (!pio_sm_is_tx_fifo_empty(SCSI_DMA_PIO, SCSI_PARITY_SM) ||
  310. !pio_sm_is_rx_fifo_empty(SCSI_DMA_PIO, SCSI_PARITY_SM) ||
  311. !pio_sm_is_tx_fifo_empty(SCSI_DMA_PIO, SCSI_DATA_SM))
  312. {
  313. return false;
  314. }
  315. if (g_scsi_dma.syncOffset > 0)
  316. {
  317. // Check if all bytes of synchronous write have been acknowledged
  318. if (pio_sm_get_rx_fifo_level(SCSI_DMA_PIO, SCSI_DATA_SM) > g_scsi_dma.syncOffsetPreload)
  319. return false;
  320. }
  321. else
  322. {
  323. // Check if state machine has written out its OSR
  324. if (pio_sm_get_pc(SCSI_DMA_PIO, SCSI_DATA_SM) != g_scsi_dma.pio_offset_async_write)
  325. return false;
  326. }
  327. // Check if ACK of the final byte has finished
  328. if (SCSI_IN(ACK))
  329. return false;
  330. return true;
  331. }
  332. static void scsi_accel_rp2040_stopWrite(volatile int *resetFlag)
  333. {
  334. // Wait for TX fifo to be empty and ACK to go high
  335. // For synchronous writes wait for all ACKs to be received also
  336. uint32_t start = millis();
  337. while (!scsi_accel_rp2040_isWriteDone() && !*resetFlag)
  338. {
  339. if ((uint32_t)(millis() - start) > 5000)
  340. {
  341. log("scsi_accel_rp2040_stopWrite() timeout");
  342. scsi_accel_log_state();
  343. *resetFlag = 1;
  344. break;
  345. }
  346. }
  347. dma_channel_abort(SCSI_DMA_CH_A);
  348. dma_channel_abort(SCSI_DMA_CH_B);
  349. dma_channel_abort(SCSI_DMA_CH_C);
  350. dma_channel_abort(SCSI_DMA_CH_D);
  351. dma_channel_set_irq0_enabled(SCSI_DMA_CH_A, false);
  352. g_scsi_dma_state = SCSIDMA_IDLE;
  353. SCSI_RELEASE_DATA_REQ();
  354. scsidma_config_gpio();
  355. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_PARITY_SM, false);
  356. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_DATA_SM, false);
  357. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_SYNC_SM, false);
  358. }
  359. void scsi_accel_rp2040_finishWrite(volatile int *resetFlag)
  360. {
  361. uint32_t start = millis();
  362. while (g_scsi_dma_state != SCSIDMA_IDLE && !*resetFlag)
  363. {
  364. if ((uint32_t)(millis() - start) > 5000)
  365. {
  366. log("scsi_accel_rp2040_finishWrite() timeout");
  367. scsi_accel_log_state();
  368. *resetFlag = 1;
  369. break;
  370. }
  371. if (g_scsi_dma_state == SCSIDMA_WRITE_DONE || *resetFlag)
  372. {
  373. // DMA done, wait for PIO to finish also and reconfig GPIO.
  374. scsi_accel_rp2040_stopWrite(resetFlag);
  375. }
  376. }
  377. }
  378. /****************************************/
  379. /* Accelerated reads from SCSI bus */
  380. /****************************************/
  381. // Load the SCSI read state machine with the address of the parity lookup table.
  382. // Also sets up DMA channels B, C and D
  383. static void config_parity_sm_for_read()
  384. {
  385. // Configure parity check state machine
  386. pio_sm_init(SCSI_DMA_PIO, SCSI_PARITY_SM, g_scsi_dma.pio_offset_read_parity, &g_scsi_dma.pio_cfg_read_parity);
  387. // Load base address to state machine register X
  388. uint32_t addrbase = (uint32_t)&g_scsi_parity_check_lookup[0];
  389. assert((addrbase & 0x3FF) == 0);
  390. pio_sm_init(SCSI_DMA_PIO, SCSI_DATA_SM, g_scsi_dma.pio_offset_read, &g_scsi_dma.pio_cfg_read);
  391. pio_sm_put(SCSI_DMA_PIO, SCSI_DATA_SM, addrbase >> 10);
  392. pio_sm_exec(SCSI_DMA_PIO, SCSI_DATA_SM, pio_encode_pull(false, false) | pio_encode_sideset(1, 1));
  393. pio_sm_exec(SCSI_DMA_PIO, SCSI_DATA_SM, pio_encode_mov(pio_y, pio_osr) | pio_encode_sideset(1, 1));
  394. // For synchronous mode, the REQ pin is driven by SCSI_SYNC_SM, so disable it in SCSI_DATA_SM
  395. if (g_scsi_dma.syncOffset > 0)
  396. {
  397. pio_sm_set_sideset_pins(SCSI_DMA_PIO, SCSI_DATA_SM, 0);
  398. }
  399. // DMA channel B will read g_scsi_parity_check_lookup and write to scsi_read_parity PIO.
  400. dma_channel_configure(SCSI_DMA_CH_B,
  401. &g_scsi_dma.dmacfg_read_chB,
  402. &SCSI_DMA_PIO->txf[SCSI_PARITY_SM],
  403. NULL,
  404. 1, false);
  405. // DMA channel C will copy addresses from data PIO to DMA channel B read address register.
  406. // It is triggered by the data SM RX FIFO request.
  407. // This triggers channel B by writing to READ_ADDR_TRIG
  408. // Channel B chaining re-enables this channel.
  409. dma_channel_configure(SCSI_DMA_CH_C,
  410. &g_scsi_dma.dmacfg_read_chC,
  411. &dma_hw->ch[SCSI_DMA_CH_B].al3_read_addr_trig,
  412. &SCSI_DMA_PIO->rxf[SCSI_DATA_SM],
  413. 1, true);
  414. if (g_scsi_dma.syncOffset == 0)
  415. {
  416. // DMA channel D will copy dummy words to scsi_accel_read PIO to set the number
  417. // of bytes to transfer.
  418. static const uint32_t dummy = 0;
  419. dma_channel_configure(SCSI_DMA_CH_D,
  420. &g_scsi_dma.dmacfg_read_chD,
  421. &SCSI_DMA_PIO->txf[SCSI_DATA_SM],
  422. &dummy,
  423. 0, false);
  424. }
  425. else
  426. {
  427. pio_sm_init(SCSI_DMA_PIO, SCSI_SYNC_SM, g_scsi_dma.pio_offset_sync_read_pacer, &g_scsi_dma.pio_cfg_sync_read_pacer);
  428. // DMA channel D will copy words from scsi_sync_read_pacer to scsi_accel_read PIO
  429. // to control the offset between REQ pulses sent and ACK pulses received.
  430. dma_channel_configure(SCSI_DMA_CH_D,
  431. &g_scsi_dma.dmacfg_read_chD,
  432. &SCSI_DMA_PIO->txf[SCSI_DATA_SM],
  433. &SCSI_DMA_PIO->rxf[SCSI_SYNC_SM],
  434. 0, false);
  435. }
  436. // Clear PIO IRQ flag that is used to detect parity error
  437. SCSI_DMA_PIO->irq = 1;
  438. }
  439. static void start_dma_read()
  440. {
  441. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_PARITY_SM, false);
  442. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_DATA_SM, false);
  443. pio_sm_clear_fifos(SCSI_DMA_PIO, SCSI_PARITY_SM);
  444. pio_sm_clear_fifos(SCSI_DMA_PIO, SCSI_DATA_SM);
  445. if (g_scsi_dma.app_bytes <= g_scsi_dma.dma_bytes)
  446. {
  447. // Buffer has been fully processed, swap it
  448. g_scsi_dma.dma_bytes = 0;
  449. g_scsi_dma.app_buf = g_scsi_dma.next_app_buf;
  450. g_scsi_dma.app_bytes = g_scsi_dma.next_app_bytes;
  451. g_scsi_dma.next_app_buf = 0;
  452. g_scsi_dma.next_app_bytes = 0;
  453. }
  454. // Check if we are all done.
  455. // From SCSIDMA_READ_DONE state we can either go to IDLE in stopRead()
  456. // or back to READ in startWrite().
  457. uint32_t bytes_to_read = g_scsi_dma.app_bytes - g_scsi_dma.dma_bytes;
  458. if (bytes_to_read == 0)
  459. {
  460. g_scsi_dma_state = SCSIDMA_READ_DONE;
  461. return;
  462. }
  463. if (g_scsi_dma.syncOffset == 0)
  464. {
  465. // Start sending dummy words to scsi_accel_read state machine
  466. dma_channel_set_trans_count(SCSI_DMA_CH_D, bytes_to_read, true);
  467. }
  468. else
  469. {
  470. // Set number of bytes to receive to the scsi_sync_read_pacer state machine register X
  471. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_SYNC_SM, false);
  472. hw_clear_bits(&SCSI_DMA_PIO->sm[SCSI_SYNC_SM].shiftctrl, PIO_SM0_SHIFTCTRL_FJOIN_RX_BITS);
  473. pio_sm_put(SCSI_DMA_PIO, SCSI_SYNC_SM, bytes_to_read - 1);
  474. pio_sm_exec(SCSI_DMA_PIO, SCSI_SYNC_SM, pio_encode_pull(false, false) | pio_encode_sideset(1, 1));
  475. pio_sm_exec(SCSI_DMA_PIO, SCSI_SYNC_SM, pio_encode_mov(pio_x, pio_osr) | pio_encode_sideset(1, 1));
  476. hw_set_bits(&SCSI_DMA_PIO->sm[SCSI_SYNC_SM].shiftctrl, PIO_SM0_SHIFTCTRL_FJOIN_RX_BITS);
  477. // Prefill FIFOs to get correct syncOffset
  478. int prefill = 12 - g_scsi_dma.syncOffset;
  479. // Always at least 1 word to avoid race condition between REQ and ACK pulses
  480. if (prefill < 1) prefill = 1;
  481. // Up to 4 words in SCSI_DATA_SM TX fifo
  482. for (int i = 0; i < 4 && prefill > 0; i++)
  483. {
  484. pio_sm_put(SCSI_DMA_PIO, SCSI_DATA_SM, 0);
  485. prefill--;
  486. }
  487. // Up to 8 words in SCSI_SYNC_SM RX fifo
  488. for (int i = 0; i < 8 && prefill > 0; i++)
  489. {
  490. pio_sm_exec(SCSI_DMA_PIO, SCSI_SYNC_SM, pio_encode_push(false, false) | pio_encode_sideset(1, 1));
  491. prefill--;
  492. }
  493. pio_sm_exec(SCSI_DMA_PIO, SCSI_SYNC_SM, pio_encode_jmp(g_scsi_dma.pio_offset_sync_read_pacer) | pio_encode_sideset(1, 1));
  494. // Start transfers
  495. dma_channel_set_trans_count(SCSI_DMA_CH_D, bytes_to_read, true);
  496. }
  497. // Start DMA to fill the destination buffer
  498. uint8_t *dest_buf = &g_scsi_dma.app_buf[g_scsi_dma.dma_bytes];
  499. g_scsi_dma.dma_bytes += bytes_to_read;
  500. dma_channel_configure(SCSI_DMA_CH_A,
  501. &g_scsi_dma.dmacfg_read_chA,
  502. dest_buf,
  503. &SCSI_DMA_PIO->rxf[SCSI_PARITY_SM],
  504. bytes_to_read,
  505. true
  506. );
  507. // Ready to start the data and parity check state machines
  508. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_PARITY_SM, true);
  509. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_DATA_SM, true);
  510. if (g_scsi_dma.syncOffset > 0)
  511. {
  512. // Start sending REQ pulses
  513. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_SYNC_SM, true);
  514. }
  515. }
  516. void scsi_accel_rp2040_startRead(uint8_t *data, uint32_t count, int *parityError, volatile int *resetFlag)
  517. {
  518. // Any write requests should be matched with a stopWrite()
  519. assert(g_scsi_dma_state != SCSIDMA_WRITE && g_scsi_dma_state != SCSIDMA_WRITE_DONE);
  520. __disable_irq();
  521. if (g_scsi_dma_state == SCSIDMA_READ)
  522. {
  523. if (!g_scsi_dma.next_app_buf && data == g_scsi_dma.app_buf + g_scsi_dma.app_bytes)
  524. {
  525. // Combine with currently running request
  526. g_scsi_dma.app_bytes += count;
  527. count = 0;
  528. }
  529. else if (data == g_scsi_dma.next_app_buf + g_scsi_dma.next_app_bytes)
  530. {
  531. // Combine with queued request
  532. g_scsi_dma.next_app_bytes += count;
  533. count = 0;
  534. }
  535. else if (!g_scsi_dma.next_app_buf)
  536. {
  537. // Add as queued request
  538. g_scsi_dma.next_app_buf = (uint8_t*)data;
  539. g_scsi_dma.next_app_bytes = count;
  540. count = 0;
  541. }
  542. }
  543. __enable_irq();
  544. // Check if the request was combined
  545. if (count == 0) return;
  546. if (g_scsi_dma_state != SCSIDMA_IDLE && g_scsi_dma_state != SCSIDMA_READ_DONE)
  547. {
  548. // Wait for previous request to finish
  549. scsi_accel_rp2040_finishRead(NULL, 0, parityError, resetFlag);
  550. if (*resetFlag)
  551. {
  552. return;
  553. }
  554. }
  555. bool must_reconfig_gpio = (g_scsi_dma_state == SCSIDMA_IDLE);
  556. g_scsi_dma_state = SCSIDMA_READ;
  557. g_scsi_dma.app_buf = (uint8_t*)data;
  558. g_scsi_dma.app_bytes = count;
  559. g_scsi_dma.dma_bytes = 0;
  560. g_scsi_dma.next_app_buf = 0;
  561. g_scsi_dma.next_app_bytes = 0;
  562. if (must_reconfig_gpio)
  563. {
  564. config_parity_sm_for_read();
  565. scsidma_config_gpio();
  566. dma_channel_set_irq0_enabled(SCSI_DMA_CH_A, true);
  567. }
  568. start_dma_read();
  569. }
  570. bool scsi_accel_rp2040_isReadFinished(const uint8_t* data)
  571. {
  572. // Check if everything has completed
  573. if (g_scsi_dma_state == SCSIDMA_IDLE || g_scsi_dma_state == SCSIDMA_READ_DONE)
  574. {
  575. return true;
  576. }
  577. if (!data)
  578. return false;
  579. // Check if this data item is still in queue.
  580. bool finished = true;
  581. __disable_irq();
  582. if (data >= g_scsi_dma.app_buf &&
  583. data < g_scsi_dma.app_buf + g_scsi_dma.app_bytes &&
  584. (uint32_t)data >= dma_hw->ch[SCSI_DMA_CH_A].write_addr)
  585. {
  586. finished = false; // In current transfer
  587. }
  588. else if (data >= g_scsi_dma.next_app_buf &&
  589. data < g_scsi_dma.next_app_buf + g_scsi_dma.next_app_bytes)
  590. {
  591. finished = false; // In queued transfer
  592. }
  593. __enable_irq();
  594. return finished;
  595. }
  596. static void scsi_accel_rp2040_stopRead()
  597. {
  598. dma_channel_abort(SCSI_DMA_CH_A);
  599. dma_channel_abort(SCSI_DMA_CH_B);
  600. dma_channel_abort(SCSI_DMA_CH_C);
  601. dma_channel_abort(SCSI_DMA_CH_D);
  602. dma_channel_set_irq0_enabled(SCSI_DMA_CH_A, false);
  603. g_scsi_dma_state = SCSIDMA_IDLE;
  604. SCSI_RELEASE_DATA_REQ();
  605. scsidma_config_gpio();
  606. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_PARITY_SM, false);
  607. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_DATA_SM, false);
  608. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_SYNC_SM, false);
  609. }
  610. void scsi_accel_rp2040_finishRead(const uint8_t *data, uint32_t count, int *parityError, volatile int *resetFlag)
  611. {
  612. uint32_t start = millis();
  613. const uint8_t *query_addr = (data ? (data + count - 1) : NULL);
  614. while (!scsi_accel_rp2040_isReadFinished(query_addr) && !*resetFlag)
  615. {
  616. if ((uint32_t)(millis() - start) > 5000)
  617. {
  618. log("scsi_accel_rp2040_finishRead timeout");
  619. scsi_accel_log_state();
  620. *resetFlag = 1;
  621. break;
  622. }
  623. }
  624. if (g_scsi_dma_state == SCSIDMA_READ_DONE || *resetFlag)
  625. {
  626. // This was last buffer, release bus
  627. scsi_accel_rp2040_stopRead();
  628. }
  629. // Check if any parity errors have been detected during the transfer so far
  630. if (SCSI_DMA_PIO->irq & 1)
  631. {
  632. debuglog("scsi_accel_rp2040_finishRead(", bytearray(data, count), ") detected parity error");
  633. *parityError = true;
  634. }
  635. }
  636. /*******************************************************/
  637. /* Initialization functions common to read/write */
  638. /*******************************************************/
  639. static void scsi_dma_irq()
  640. {
  641. #ifndef ENABLE_AUDIO_OUTPUT
  642. dma_hw->ints0 = (1 << SCSI_DMA_CH_A);
  643. #else
  644. // see audio.h for whats going on here
  645. if (dma_hw->intr & (1 << SCSI_DMA_CH_A)) {
  646. dma_hw->ints0 = (1 << SCSI_DMA_CH_A);
  647. } else {
  648. audio_dma_irq();
  649. return;
  650. }
  651. #endif
  652. scsidma_state_t state = g_scsi_dma_state;
  653. if (state == SCSIDMA_WRITE)
  654. {
  655. // Start writing from next buffer, if any, or set state to SCSIDMA_WRITE_DONE
  656. start_dma_write();
  657. }
  658. else if (state == SCSIDMA_READ)
  659. {
  660. // Start reading into next buffer, if any, or set state to SCSIDMA_READ_DONE
  661. start_dma_read();
  662. }
  663. }
  664. // Select GPIO from PIO peripheral or from software controlled SIO
  665. static void scsidma_config_gpio()
  666. {
  667. if (g_scsi_dma_state == SCSIDMA_IDLE)
  668. {
  669. iobank0_hw->io[SCSI_IO_DB0].ctrl = GPIO_FUNC_SIO;
  670. iobank0_hw->io[SCSI_IO_DB1].ctrl = GPIO_FUNC_SIO;
  671. iobank0_hw->io[SCSI_IO_DB2].ctrl = GPIO_FUNC_SIO;
  672. iobank0_hw->io[SCSI_IO_DB3].ctrl = GPIO_FUNC_SIO;
  673. iobank0_hw->io[SCSI_IO_DB4].ctrl = GPIO_FUNC_SIO;
  674. iobank0_hw->io[SCSI_IO_DB5].ctrl = GPIO_FUNC_SIO;
  675. iobank0_hw->io[SCSI_IO_DB6].ctrl = GPIO_FUNC_SIO;
  676. iobank0_hw->io[SCSI_IO_DB7].ctrl = GPIO_FUNC_SIO;
  677. iobank0_hw->io[SCSI_IO_DBP].ctrl = GPIO_FUNC_SIO;
  678. iobank0_hw->io[scsi_pins.OUT_REQ].ctrl = GPIO_FUNC_SIO;
  679. }
  680. else if (g_scsi_dma_state == SCSIDMA_WRITE)
  681. {
  682. // Make sure the initial state of all pins is high and output
  683. pio_sm_set_pins(SCSI_DMA_PIO, SCSI_DATA_SM, is202309a() ? 0x801FF : 0x201FF);
  684. // Binary of 0x3FF is is 0 0 1 1 11111111
  685. // ? A R P DBP
  686. // A = ACK, R = REQ, DBP are the data pins
  687. // REQ internal state needs to be set 'high'
  688. // 100000010000000000111111111
  689. // Probably right to left here, so 0 - 9 are set 'high' and 10/11 are set 'low'
  690. pio_sm_set_consecutive_pindirs(SCSI_DMA_PIO, SCSI_DATA_SM, 0, 9, true);
  691. pio_sm_set_consecutive_pindirs(SCSI_DMA_PIO, SCSI_DATA_SM, is202309a() ? 19 : 17, 1, true);
  692. iobank0_hw->io[SCSI_IO_DB0].ctrl = GPIO_FUNC_PIO0;
  693. iobank0_hw->io[SCSI_IO_DB1].ctrl = GPIO_FUNC_PIO0;
  694. iobank0_hw->io[SCSI_IO_DB2].ctrl = GPIO_FUNC_PIO0;
  695. iobank0_hw->io[SCSI_IO_DB3].ctrl = GPIO_FUNC_PIO0;
  696. iobank0_hw->io[SCSI_IO_DB4].ctrl = GPIO_FUNC_PIO0;
  697. iobank0_hw->io[SCSI_IO_DB5].ctrl = GPIO_FUNC_PIO0;
  698. iobank0_hw->io[SCSI_IO_DB6].ctrl = GPIO_FUNC_PIO0;
  699. iobank0_hw->io[SCSI_IO_DB7].ctrl = GPIO_FUNC_PIO0;
  700. iobank0_hw->io[SCSI_IO_DBP].ctrl = GPIO_FUNC_PIO0;
  701. iobank0_hw->io[scsi_pins.OUT_REQ].ctrl = GPIO_FUNC_PIO0;
  702. }
  703. else if (g_scsi_dma_state == SCSIDMA_READ)
  704. {
  705. if (g_scsi_dma.syncOffset == 0)
  706. {
  707. // Asynchronous read
  708. // Data bus as input, REQ pin as output
  709. pio_sm_set_pins(SCSI_DMA_PIO, SCSI_DATA_SM, is202309a() ? 0x801FF : 0x201FF);
  710. pio_sm_set_consecutive_pindirs(SCSI_DMA_PIO, SCSI_DATA_SM, 0, 9, false);
  711. pio_sm_set_consecutive_pindirs(SCSI_DMA_PIO, SCSI_DATA_SM, is202309a() ? 19 : 17, 1, true);
  712. }
  713. else
  714. {
  715. // Synchronous read, REQ pin is written by SYNC_SM
  716. pio_sm_set_pins(SCSI_DMA_PIO, SCSI_SYNC_SM, is202309a() ? 0x801FF : 0x201FF);
  717. pio_sm_set_consecutive_pindirs(SCSI_DMA_PIO, SCSI_DATA_SM, 0, 9, false);
  718. pio_sm_set_consecutive_pindirs(SCSI_DMA_PIO, SCSI_SYNC_SM, is202309a() ? 19 : 17, 1, true);
  719. }
  720. iobank0_hw->io[SCSI_IO_DB0].ctrl = GPIO_FUNC_SIO;
  721. iobank0_hw->io[SCSI_IO_DB1].ctrl = GPIO_FUNC_SIO;
  722. iobank0_hw->io[SCSI_IO_DB2].ctrl = GPIO_FUNC_SIO;
  723. iobank0_hw->io[SCSI_IO_DB3].ctrl = GPIO_FUNC_SIO;
  724. iobank0_hw->io[SCSI_IO_DB4].ctrl = GPIO_FUNC_SIO;
  725. iobank0_hw->io[SCSI_IO_DB5].ctrl = GPIO_FUNC_SIO;
  726. iobank0_hw->io[SCSI_IO_DB6].ctrl = GPIO_FUNC_SIO;
  727. iobank0_hw->io[SCSI_IO_DB7].ctrl = GPIO_FUNC_SIO;
  728. iobank0_hw->io[SCSI_IO_DBP].ctrl = GPIO_FUNC_SIO;
  729. iobank0_hw->io[scsi_pins.OUT_REQ].ctrl = GPIO_FUNC_PIO0;
  730. }
  731. }
  732. void scsi_accel_rp2040_init()
  733. {
  734. g_scsi_dma_state = SCSIDMA_IDLE;
  735. scsidma_config_gpio();
  736. if (g_channels_claimed) {
  737. // Un-claim all SCSI state machines
  738. pio_sm_unclaim(SCSI_DMA_PIO, SCSI_PARITY_SM);
  739. pio_sm_unclaim(SCSI_DMA_PIO, SCSI_DATA_SM);
  740. pio_sm_unclaim(SCSI_DMA_PIO, SCSI_SYNC_SM);
  741. // Remove all SCSI programs
  742. pio_remove_program(SCSI_DMA_PIO, &scsi_parity_program, g_scsi_dma.pio_offset_parity);
  743. pio_remove_program(SCSI_DMA_PIO, &scsi_accel_async_write_program, g_scsi_dma.pio_offset_async_write);
  744. pio_remove_program(SCSI_DMA_PIO, &scsi_sync_write_pacer_program, g_scsi_dma.pio_offset_sync_write_pacer);
  745. pio_remove_program(SCSI_DMA_PIO, &scsi_sync_write_program, g_scsi_dma.pio_offset_sync_write);
  746. pio_remove_program(SCSI_DMA_PIO, &scsi_accel_read_program, g_scsi_dma.pio_offset_read);
  747. pio_remove_program(SCSI_DMA_PIO, &scsi_sync_read_pacer_program, g_scsi_dma.pio_offset_sync_read_pacer);
  748. pio_remove_program(SCSI_DMA_PIO, &scsi_read_parity_program, g_scsi_dma.pio_offset_read_parity);
  749. // Un-claim all SCSI DMA channels
  750. dma_channel_unclaim(SCSI_DMA_CH_A);
  751. dma_channel_unclaim(SCSI_DMA_CH_B);
  752. dma_channel_unclaim(SCSI_DMA_CH_C);
  753. dma_channel_unclaim(SCSI_DMA_CH_D);
  754. // Set flag to re-initialize SCSI PIO system
  755. g_channels_claimed = false;
  756. }
  757. if (!g_channels_claimed) {
  758. // Mark channels as being in use, unless it has been done already
  759. pio_sm_claim(SCSI_DMA_PIO, SCSI_PARITY_SM);
  760. pio_sm_claim(SCSI_DMA_PIO, SCSI_DATA_SM);
  761. pio_sm_claim(SCSI_DMA_PIO, SCSI_SYNC_SM);
  762. dma_channel_claim(SCSI_DMA_CH_A);
  763. dma_channel_claim(SCSI_DMA_CH_B);
  764. dma_channel_claim(SCSI_DMA_CH_C);
  765. dma_channel_claim(SCSI_DMA_CH_D);
  766. g_channels_claimed = true;
  767. }
  768. // Parity lookup generator
  769. g_scsi_dma.pio_offset_parity = pio_add_program(SCSI_DMA_PIO, &scsi_parity_program);
  770. g_scsi_dma.pio_cfg_parity = scsi_parity_program_get_default_config(g_scsi_dma.pio_offset_parity);
  771. sm_config_set_out_shift(&g_scsi_dma.pio_cfg_parity, true, false, 32);
  772. sm_config_set_in_shift(&g_scsi_dma.pio_cfg_parity, true, true, 32);
  773. // Asynchronous SCSI write
  774. g_scsi_dma.pio_offset_async_write = pio_add_program(SCSI_DMA_PIO, &scsi_accel_async_write_program);
  775. g_scsi_dma.pio_cfg_async_write = scsi_accel_async_write_program_get_default_config(g_scsi_dma.pio_offset_async_write);
  776. sm_config_set_out_pins(&g_scsi_dma.pio_cfg_async_write, SCSI_IO_DB0, 9);
  777. sm_config_set_sideset_pins(&g_scsi_dma.pio_cfg_async_write, is202309a() ? 19 : 17);
  778. sm_config_set_fifo_join(&g_scsi_dma.pio_cfg_async_write, PIO_FIFO_JOIN_TX);
  779. sm_config_set_out_shift(&g_scsi_dma.pio_cfg_async_write, true, false, 32);
  780. // Synchronous SCSI write pacer / ACK handler
  781. g_scsi_dma.pio_offset_sync_write_pacer = pio_add_program(SCSI_DMA_PIO, &scsi_sync_write_pacer_program);
  782. g_scsi_dma.pio_cfg_sync_write_pacer = scsi_sync_write_pacer_program_get_default_config(g_scsi_dma.pio_offset_sync_write_pacer);
  783. sm_config_set_out_shift(&g_scsi_dma.pio_cfg_sync_write_pacer, true, true, 1);
  784. // Synchronous SCSI data writer
  785. g_scsi_dma.pio_offset_sync_write = pio_add_program(SCSI_DMA_PIO, &scsi_sync_write_program);
  786. g_scsi_dma.pio_cfg_sync_write = scsi_sync_write_program_get_default_config(g_scsi_dma.pio_offset_sync_write);
  787. sm_config_set_out_pins(&g_scsi_dma.pio_cfg_sync_write, SCSI_IO_DB0, 9);
  788. sm_config_set_sideset_pins(&g_scsi_dma.pio_cfg_sync_write, is202309a() ? 19 : 17);
  789. sm_config_set_out_shift(&g_scsi_dma.pio_cfg_sync_write, true, true, 32);
  790. sm_config_set_in_shift(&g_scsi_dma.pio_cfg_sync_write, true, true, 1);
  791. // Asynchronous / synchronous SCSI read
  792. g_scsi_dma.pio_offset_read = pio_add_program(SCSI_DMA_PIO, &scsi_accel_read_program);
  793. g_scsi_dma.pio_cfg_read = scsi_accel_read_program_get_default_config(g_scsi_dma.pio_offset_read);
  794. sm_config_set_in_pins(&g_scsi_dma.pio_cfg_read, SCSI_IO_DB0);
  795. sm_config_set_sideset_pins(&g_scsi_dma.pio_cfg_read, is202309a() ? 19 : 17);
  796. sm_config_set_out_shift(&g_scsi_dma.pio_cfg_read, true, false, 32);
  797. sm_config_set_in_shift(&g_scsi_dma.pio_cfg_read, true, true, 32);
  798. // Synchronous SCSI read pacer
  799. g_scsi_dma.pio_offset_sync_read_pacer = pio_add_program(SCSI_DMA_PIO, &scsi_sync_read_pacer_program);
  800. g_scsi_dma.pio_cfg_sync_read_pacer = scsi_sync_read_pacer_program_get_default_config(g_scsi_dma.pio_offset_sync_read_pacer);
  801. sm_config_set_sideset_pins(&g_scsi_dma.pio_cfg_sync_read_pacer, is202309a() ? 19 : 17);
  802. // Read parity check
  803. g_scsi_dma.pio_offset_read_parity = pio_add_program(SCSI_DMA_PIO, &scsi_read_parity_program);
  804. g_scsi_dma.pio_cfg_read_parity = scsi_read_parity_program_get_default_config(g_scsi_dma.pio_offset_read_parity);
  805. sm_config_set_out_shift(&g_scsi_dma.pio_cfg_read_parity, true, true, 32);
  806. sm_config_set_in_shift(&g_scsi_dma.pio_cfg_read_parity, true, false, 32);
  807. // Create DMA channel configurations so they can be applied quickly later
  808. // For write to SCSI BUS:
  809. // Channel A: Bytes from RAM to scsi_parity PIO
  810. dma_channel_config cfg = dma_channel_get_default_config(SCSI_DMA_CH_A);
  811. channel_config_set_transfer_data_size(&cfg, DMA_SIZE_8);
  812. channel_config_set_read_increment(&cfg, true);
  813. channel_config_set_write_increment(&cfg, false);
  814. channel_config_set_dreq(&cfg, pio_get_dreq(SCSI_DMA_PIO, SCSI_PARITY_SM, true));
  815. g_scsi_dma.dmacfg_write_chA = cfg;
  816. // Channel B: Addresses from scsi_parity PIO to lookup DMA READ_ADDR register
  817. cfg = dma_channel_get_default_config(SCSI_DMA_CH_B);
  818. channel_config_set_transfer_data_size(&cfg, DMA_SIZE_32);
  819. channel_config_set_read_increment(&cfg, false);
  820. channel_config_set_write_increment(&cfg, false);
  821. channel_config_set_dreq(&cfg, pio_get_dreq(SCSI_DMA_PIO, SCSI_PARITY_SM, false));
  822. g_scsi_dma.dmacfg_write_chB = cfg;
  823. // Channel C: Lookup from g_scsi_parity_lookup and copy to scsi_accel_async_write or scsi_sync_write PIO
  824. // When done, chain to channel B
  825. cfg = dma_channel_get_default_config(SCSI_DMA_CH_C);
  826. channel_config_set_transfer_data_size(&cfg, DMA_SIZE_16);
  827. channel_config_set_read_increment(&cfg, false);
  828. channel_config_set_write_increment(&cfg, false);
  829. channel_config_set_dreq(&cfg, pio_get_dreq(SCSI_DMA_PIO, SCSI_DATA_SM, true));
  830. channel_config_set_chain_to(&cfg, SCSI_DMA_CH_B);
  831. g_scsi_dma.dmacfg_write_chC = cfg;
  832. // Channel D: In synchronous mode a second DMA channel is used to transfer dummy bits
  833. // from first state machine to second one.
  834. cfg = dma_channel_get_default_config(SCSI_DMA_CH_D);
  835. channel_config_set_transfer_data_size(&cfg, DMA_SIZE_32);
  836. channel_config_set_read_increment(&cfg, false);
  837. channel_config_set_write_increment(&cfg, false);
  838. channel_config_set_dreq(&cfg, pio_get_dreq(SCSI_DMA_PIO, SCSI_SYNC_SM, true));
  839. g_scsi_dma.dmacfg_write_chD = cfg;
  840. // For read from SCSI BUS:
  841. // Channel A: Bytes from scsi_read_parity PIO to destination memory buffer
  842. // This takes the bottom 8 bits which is the data without parity bit.
  843. // Triggered by scsi_read_parity RX FIFO.
  844. cfg = dma_channel_get_default_config(SCSI_DMA_CH_A);
  845. channel_config_set_transfer_data_size(&cfg, DMA_SIZE_8);
  846. channel_config_set_read_increment(&cfg, false);
  847. channel_config_set_write_increment(&cfg, true);
  848. channel_config_set_dreq(&cfg, pio_get_dreq(SCSI_DMA_PIO, SCSI_PARITY_SM, false));
  849. g_scsi_dma.dmacfg_read_chA = cfg;
  850. // Channel B: Lookup from g_scsi_parity_check_lookup and copy to scsi_read_parity PIO
  851. // Triggered by channel C writing to READ_ADDR_TRIG
  852. // Re-enables channel C by chaining after done.
  853. cfg = dma_channel_get_default_config(SCSI_DMA_CH_B);
  854. channel_config_set_transfer_data_size(&cfg, DMA_SIZE_16);
  855. channel_config_set_read_increment(&cfg, false);
  856. channel_config_set_write_increment(&cfg, false);
  857. channel_config_set_dreq(&cfg, DREQ_FORCE);
  858. channel_config_set_chain_to(&cfg, SCSI_DMA_CH_C);
  859. cfg.ctrl |= DMA_CH0_CTRL_TRIG_HIGH_PRIORITY_BITS;
  860. g_scsi_dma.dmacfg_read_chB = cfg;
  861. // Channel C: Addresses from scsi_read PIO to channel B READ_ADDR register
  862. // A single transfer starts when PIO RX FIFO has data.
  863. // The DMA channel is re-enabled by channel B chaining.
  864. cfg = dma_channel_get_default_config(SCSI_DMA_CH_C);
  865. channel_config_set_transfer_data_size(&cfg, DMA_SIZE_32);
  866. channel_config_set_read_increment(&cfg, false);
  867. channel_config_set_write_increment(&cfg, false);
  868. channel_config_set_dreq(&cfg, pio_get_dreq(SCSI_DMA_PIO, SCSI_DATA_SM, false));
  869. g_scsi_dma.dmacfg_read_chC = cfg;
  870. // Channel D: In synchronous mode a second DMA channel is used to transfer dummy words
  871. // from first state machine to second one to control the pace of data transfer.
  872. // In asynchronous mode this just transfers words to control the number of bytes.
  873. cfg = dma_channel_get_default_config(SCSI_DMA_CH_D);
  874. channel_config_set_transfer_data_size(&cfg, DMA_SIZE_32);
  875. channel_config_set_read_increment(&cfg, false);
  876. channel_config_set_write_increment(&cfg, false);
  877. channel_config_set_dreq(&cfg, pio_get_dreq(SCSI_DMA_PIO, SCSI_DATA_SM, true));
  878. g_scsi_dma.dmacfg_read_chD = cfg;
  879. // Interrupts are used for data buffer swapping
  880. irq_set_exclusive_handler(DMA_IRQ_0, scsi_dma_irq);
  881. irq_set_enabled(DMA_IRQ_0, true);
  882. }
  883. bool scsi_accel_rp2040_setSyncMode(int syncOffset, int syncPeriod)
  884. {
  885. if (g_scsi_dma_state != SCSIDMA_IDLE)
  886. {
  887. log("ERROR: SCSI DMA was in state ", (int)g_scsi_dma_state, " when changing sync mode, forcing bus reset");
  888. scsi_accel_log_state();
  889. return false;
  890. }
  891. if (syncOffset != g_scsi_dma.syncOffset || syncPeriod != g_scsi_dma.syncPeriod)
  892. {
  893. g_scsi_dma.syncOffset = syncOffset;
  894. g_scsi_dma.syncPeriod = syncPeriod;
  895. if (syncOffset > 0)
  896. {
  897. // Set up offset amount to PIO state machine configs.
  898. // The RX fifo of scsi_sync_write has 4 slots.
  899. // We can preload it with 0-3 items and set the autopush threshold 1, 2, 4 ... 32
  900. // to act as a divider. This allows offsets 1 to 128 bytes.
  901. // SCSI2SD code currently only uses offsets up to 15.
  902. if (syncOffset <= 4)
  903. {
  904. g_scsi_dma.syncOffsetDivider = 1;
  905. g_scsi_dma.syncOffsetPreload = 5 - syncOffset;
  906. }
  907. else if (syncOffset <= 8)
  908. {
  909. g_scsi_dma.syncOffsetDivider = 2;
  910. g_scsi_dma.syncOffsetPreload = 5 - syncOffset / 2;
  911. }
  912. else if (syncOffset <= 16)
  913. {
  914. g_scsi_dma.syncOffsetDivider = 4;
  915. g_scsi_dma.syncOffsetPreload = 5 - syncOffset / 4;
  916. }
  917. else
  918. {
  919. g_scsi_dma.syncOffsetDivider = 4;
  920. g_scsi_dma.syncOffsetPreload = 0;
  921. }
  922. // To properly detect when all bytes have been ACKed,
  923. // we need at least one vacant slot in the FIFO.
  924. if (g_scsi_dma.syncOffsetPreload > 3)
  925. g_scsi_dma.syncOffsetPreload = 3;
  926. sm_config_set_out_shift(&g_scsi_dma.pio_cfg_sync_write_pacer, true, true, g_scsi_dma.syncOffsetDivider);
  927. sm_config_set_in_shift(&g_scsi_dma.pio_cfg_sync_write, true, true, g_scsi_dma.syncOffsetDivider);
  928. // Set up the timing parameters to PIO program
  929. // The scsi_sync_write PIO program consists of three instructions.
  930. // The delays are in clock cycles, each taking 8 ns.
  931. // delay0: Delay from data write to REQ assertion
  932. // delay1: Delay from REQ assert to REQ deassert
  933. // delay2: Delay from REQ deassert to data write
  934. int delay0, delay1, delay2;
  935. int totalDelay = syncPeriod * 4 / 8;
  936. if (syncPeriod <= 25)
  937. {
  938. // Fast SCSI timing: 30 ns assertion period, 25 ns skew delay
  939. // The hardware rise and fall time require some extra delay,
  940. // the values below are tuned based on oscilloscope measurements.
  941. delay0 = 3;
  942. delay1 = 5;
  943. delay2 = totalDelay - delay0 - delay1 - 3;
  944. if (delay2 < 0) delay2 = 0;
  945. if (delay2 > 15) delay2 = 15;
  946. }
  947. else
  948. {
  949. // Slow SCSI timing: 90 ns assertion period, 55 ns skew delay
  950. delay0 = 6;
  951. delay1 = 12;
  952. delay2 = totalDelay - delay0 - delay1 - 3;
  953. if (delay2 < 0) delay2 = 0;
  954. if (delay2 > 15) delay2 = 15;
  955. }
  956. // Patch the delay values into the instructions in scsi_sync_write.
  957. // The code in scsi_accel.pio must have delay set to 0 for this to work correctly.
  958. uint16_t instr0 = scsi_sync_write_program_instructions[0] | pio_encode_delay(delay0);
  959. uint16_t instr1 = scsi_sync_write_program_instructions[1] | pio_encode_delay(delay1);
  960. uint16_t instr2 = scsi_sync_write_program_instructions[2] | pio_encode_delay(delay2);
  961. SCSI_DMA_PIO->instr_mem[g_scsi_dma.pio_offset_sync_write + 0] = instr0;
  962. SCSI_DMA_PIO->instr_mem[g_scsi_dma.pio_offset_sync_write + 1] = instr1;
  963. SCSI_DMA_PIO->instr_mem[g_scsi_dma.pio_offset_sync_write + 2] = instr2;
  964. // And similar patching for scsi_sync_read_pacer
  965. int rdelay2 = totalDelay - delay1 - 2;
  966. if (rdelay2 > 15) rdelay2 = 15;
  967. if (rdelay2 < 5) rdelay2 = 5;
  968. uint16_t rinstr0 = scsi_sync_read_pacer_program_instructions[0] | pio_encode_delay(rdelay2);
  969. uint16_t rinstr1 = (scsi_sync_read_pacer_program_instructions[1] + g_scsi_dma.pio_offset_sync_read_pacer) | pio_encode_delay(delay1);
  970. SCSI_DMA_PIO->instr_mem[g_scsi_dma.pio_offset_sync_read_pacer + 0] = rinstr0;
  971. SCSI_DMA_PIO->instr_mem[g_scsi_dma.pio_offset_sync_read_pacer + 1] = rinstr1;
  972. }
  973. }
  974. return true;
  975. }