scsi_accel_target.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2022 Rabbit Hole Computing™
  3. *
  4. * ZuluSCSI™ firmware is licensed under the GPL version 3 or any later version. 
  5. *
  6. * https://www.gnu.org/licenses/gpl-3.0.html
  7. * ----
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version. 
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. * GNU General Public License for more details. 
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  20. **/
  21. /* Data flow in SCSI acceleration:
  22. *
  23. * 1. Application provides a buffer of bytes to send.
  24. * 2. Code in this module adds parity bit to the bytes and packs two bytes into 32 bit words.
  25. * 3. DMA controller copies the words to PIO peripheral FIFO.
  26. * 4. PIO peripheral handles low-level SCSI handshake and writes bytes and parity to GPIO.
  27. */
  28. #include "ZuluSCSI_platform.h"
  29. #include "ZuluSCSI_log.h"
  30. #include "scsi_accel_target.h"
  31. #include <hardware/pio.h>
  32. #include <hardware/dma.h>
  33. #include <hardware/irq.h>
  34. #include <hardware/structs/iobank0.h>
  35. #include <hardware/sync.h>
  36. #include <audio.h>
  37. #include <multicore.h>
  38. #ifdef ZULUSCSI_BS2
  39. #include "scsi_accel_target_BS2.pio.h"
  40. #else
  41. #include "scsi_accel_target_RP2040.pio.h"
  42. #endif
  43. // SCSI bus write acceleration uses up to 3 PIO state machines:
  44. // SM0: Convert data bytes to lookup addresses to add parity
  45. // SM1: Write data to SCSI bus
  46. // SM2: For synchronous mode only, count ACK pulses
  47. #define SCSI_DMA_PIO pio0
  48. #define SCSI_PARITY_SM 0
  49. #define SCSI_DATA_SM 1
  50. #define SCSI_SYNC_SM 2
  51. // SCSI bus write acceleration uses 3 or 4 DMA channels (data flow A->B->C->D):
  52. // A: Bytes from RAM to scsi_parity PIO
  53. // B: Addresses from scsi_parity PIO to lookup DMA READ_ADDR register
  54. // C: Lookup from g_scsi_parity_lookup and copy to scsi_accel_async_write or scsi_sync_write PIO
  55. // D: For sync transfers, scsi_sync_write to scsi_sync_write_pacer PIO
  56. //
  57. // SCSI bus read acceleration uses 4 DMA channels (data flow D->C->B->A):
  58. // A: Bytes from scsi_read_parity PIO to memory buffer
  59. // B: Lookup from g_scsi_parity_check_lookup and copy to scsi_read_parity PIO
  60. // C: Addresses from scsi_accel_read PIO to lookup DMA READ_ADDR register
  61. // D: From pacer to data state machine to trigger transfers
  62. #define SCSI_DMA_CH_A 0
  63. #define SCSI_DMA_CH_B 1
  64. #define SCSI_DMA_CH_C 2
  65. #define SCSI_DMA_CH_D 3
  66. static struct {
  67. uint8_t *app_buf; // Buffer provided by application
  68. uint32_t app_bytes; // Bytes available in application buffer
  69. uint32_t dma_bytes; // Bytes that have been scheduled for DMA so far
  70. uint8_t *next_app_buf; // Next buffer from application after current one finishes
  71. uint32_t next_app_bytes; // Bytes in next buffer
  72. // Synchronous mode?
  73. int syncOffset;
  74. int syncPeriod;
  75. int syncOffsetDivider; // Autopush/autopull threshold for the write pacer state machine
  76. int syncOffsetPreload; // Number of items to preload in the RX fifo of scsi_sync_write
  77. // PIO configurations
  78. uint32_t pio_offset_parity;
  79. uint32_t pio_offset_async_write;
  80. uint32_t pio_offset_sync_write_pacer;
  81. uint32_t pio_offset_sync_write;
  82. uint32_t pio_offset_read;
  83. uint32_t pio_offset_read_parity;
  84. uint32_t pio_offset_sync_read_pacer;
  85. pio_sm_config pio_cfg_parity;
  86. pio_sm_config pio_cfg_async_write;
  87. pio_sm_config pio_cfg_sync_write_pacer;
  88. pio_sm_config pio_cfg_sync_write;
  89. pio_sm_config pio_cfg_read;
  90. pio_sm_config pio_cfg_read_parity;
  91. pio_sm_config pio_cfg_sync_read_pacer;
  92. // DMA configurations for write
  93. dma_channel_config dmacfg_write_chA; // Data from RAM to scsi_parity PIO
  94. dma_channel_config dmacfg_write_chB; // Addresses from scsi_parity PIO to lookup DMA
  95. dma_channel_config dmacfg_write_chC; // Data from g_scsi_parity_lookup to scsi write PIO
  96. dma_channel_config dmacfg_write_chD; // In synchronous mode only, transfer between state machines
  97. // DMA configurations for read
  98. dma_channel_config dmacfg_read_chA; // Data to destination memory buffer
  99. dma_channel_config dmacfg_read_chB; // From lookup table to scsi_read_parity PIO
  100. dma_channel_config dmacfg_read_chC; // From scsi_accel_read to channel B READ_ADDR
  101. dma_channel_config dmacfg_read_chD; // From pacer to data state machine
  102. } g_scsi_dma;
  103. enum scsidma_state_t { SCSIDMA_IDLE = 0,
  104. SCSIDMA_WRITE, SCSIDMA_WRITE_DONE,
  105. SCSIDMA_READ, SCSIDMA_READ_DONE };
  106. static const char* scsidma_states[5] = {"IDLE", "WRITE", "WRITE_DONE", "READ", "READ_DONE"};
  107. static volatile scsidma_state_t g_scsi_dma_state;
  108. static bool g_channels_claimed = false;
  109. static void scsidma_config_gpio();
  110. void scsi_accel_log_state()
  111. {
  112. logmsg("SCSI DMA state: ", scsidma_states[g_scsi_dma_state]);
  113. logmsg("Current buffer: ", g_scsi_dma.dma_bytes, "/", g_scsi_dma.app_bytes, ", next ", g_scsi_dma.next_app_bytes, " bytes");
  114. logmsg("SyncOffset: ", g_scsi_dma.syncOffset, " SyncPeriod ", g_scsi_dma.syncPeriod);
  115. logmsg("PIO Parity SM:",
  116. " tx_fifo ", (int)pio_sm_get_tx_fifo_level(SCSI_DMA_PIO, SCSI_PARITY_SM),
  117. ", rx_fifo ", (int)pio_sm_get_rx_fifo_level(SCSI_DMA_PIO, SCSI_PARITY_SM),
  118. ", pc ", (int)pio_sm_get_pc(SCSI_DMA_PIO, SCSI_PARITY_SM),
  119. ", instr ", SCSI_DMA_PIO->sm[SCSI_PARITY_SM].instr);
  120. logmsg("PIO Data SM:",
  121. " tx_fifo ", (int)pio_sm_get_tx_fifo_level(SCSI_DMA_PIO, SCSI_DATA_SM),
  122. ", rx_fifo ", (int)pio_sm_get_rx_fifo_level(SCSI_DMA_PIO, SCSI_DATA_SM),
  123. ", pc ", (int)pio_sm_get_pc(SCSI_DMA_PIO, SCSI_DATA_SM),
  124. ", instr ", SCSI_DMA_PIO->sm[SCSI_DATA_SM].instr);
  125. logmsg("PIO Sync SM:",
  126. " tx_fifo ", (int)pio_sm_get_tx_fifo_level(SCSI_DMA_PIO, SCSI_SYNC_SM),
  127. ", rx_fifo ", (int)pio_sm_get_rx_fifo_level(SCSI_DMA_PIO, SCSI_SYNC_SM),
  128. ", pc ", (int)pio_sm_get_pc(SCSI_DMA_PIO, SCSI_SYNC_SM),
  129. ", instr ", SCSI_DMA_PIO->sm[SCSI_SYNC_SM].instr);
  130. logmsg("DMA CH A:",
  131. " ctrl: ", dma_hw->ch[SCSI_DMA_CH_A].ctrl_trig,
  132. " count: ", dma_hw->ch[SCSI_DMA_CH_A].transfer_count);
  133. logmsg("DMA CH B:",
  134. " ctrl: ", dma_hw->ch[SCSI_DMA_CH_B].ctrl_trig,
  135. " count: ", dma_hw->ch[SCSI_DMA_CH_B].transfer_count);
  136. logmsg("DMA CH C:",
  137. " ctrl: ", dma_hw->ch[SCSI_DMA_CH_C].ctrl_trig,
  138. " count: ", dma_hw->ch[SCSI_DMA_CH_C].transfer_count);
  139. logmsg("DMA CH D:",
  140. " ctrl: ", dma_hw->ch[SCSI_DMA_CH_D].ctrl_trig,
  141. " count: ", dma_hw->ch[SCSI_DMA_CH_D].transfer_count);
  142. logmsg("GPIO states: ", sio_hw->gpio_in);
  143. }
  144. /****************************************/
  145. /* Accelerated writes to SCSI bus */
  146. /****************************************/
  147. // Load the SCSI parity state machine with the address of the parity lookup table.
  148. // Also sets up DMA channels B and C
  149. static void config_parity_sm_for_write()
  150. {
  151. // Load base address to state machine register X
  152. uint32_t addrbase = (uint32_t)&g_scsi_parity_lookup[0];
  153. assert((addrbase & 0x1FF) == 0);
  154. pio_sm_init(SCSI_DMA_PIO, SCSI_PARITY_SM, g_scsi_dma.pio_offset_parity, &g_scsi_dma.pio_cfg_parity);
  155. pio_sm_put(SCSI_DMA_PIO, SCSI_PARITY_SM, addrbase >> 9);
  156. pio_sm_exec(SCSI_DMA_PIO, SCSI_PARITY_SM, pio_encode_pull(false, false));
  157. pio_sm_exec(SCSI_DMA_PIO, SCSI_PARITY_SM, pio_encode_mov(pio_x, pio_osr));
  158. // DMA channel B will copy addresses from parity PIO to DMA channel C read address register.
  159. // It is triggered by the parity SM RX FIFO request
  160. dma_channel_configure(SCSI_DMA_CH_B,
  161. &g_scsi_dma.dmacfg_write_chB,
  162. &dma_hw->ch[SCSI_DMA_CH_C].al3_read_addr_trig,
  163. &SCSI_DMA_PIO->rxf[SCSI_PARITY_SM],
  164. 1, true);
  165. // DMA channel C will read g_scsi_parity_lookup to copy data + parity to SCSI write state machine.
  166. // It is triggered by SCSI write machine TX FIFO request and chains to re-enable channel B.
  167. dma_channel_configure(SCSI_DMA_CH_C,
  168. &g_scsi_dma.dmacfg_write_chC,
  169. &SCSI_DMA_PIO->txf[SCSI_DATA_SM],
  170. NULL,
  171. 1, false);
  172. }
  173. static void start_dma_write()
  174. {
  175. if (g_scsi_dma.app_bytes <= g_scsi_dma.dma_bytes)
  176. {
  177. // Buffer has been fully processed, swap it
  178. g_scsi_dma.dma_bytes = 0;
  179. g_scsi_dma.app_buf = g_scsi_dma.next_app_buf;
  180. g_scsi_dma.app_bytes = g_scsi_dma.next_app_bytes;
  181. g_scsi_dma.next_app_buf = 0;
  182. g_scsi_dma.next_app_bytes = 0;
  183. }
  184. // Check if we are all done.
  185. // From SCSIDMA_WRITE_DONE state we can either go to IDLE in stopWrite()
  186. // or back to WRITE in startWrite().
  187. uint32_t bytes_to_send = g_scsi_dma.app_bytes - g_scsi_dma.dma_bytes;
  188. if (bytes_to_send == 0)
  189. {
  190. g_scsi_dma_state = SCSIDMA_WRITE_DONE;
  191. return;
  192. }
  193. uint8_t *src_buf = &g_scsi_dma.app_buf[g_scsi_dma.dma_bytes];
  194. g_scsi_dma.dma_bytes += bytes_to_send;
  195. // Start DMA from current buffer to parity generator
  196. dma_channel_configure(SCSI_DMA_CH_A,
  197. &g_scsi_dma.dmacfg_write_chA,
  198. &SCSI_DMA_PIO->txf[SCSI_PARITY_SM],
  199. src_buf,
  200. bytes_to_send,
  201. true
  202. );
  203. }
  204. void scsi_accel_rp2040_startWrite(const uint8_t* data, uint32_t count, volatile int *resetFlag)
  205. {
  206. // Any read requests should be matched with a stopRead()
  207. assert(g_scsi_dma_state != SCSIDMA_READ && g_scsi_dma_state != SCSIDMA_READ_DONE);
  208. __disable_irq();
  209. if (g_scsi_dma_state == SCSIDMA_WRITE)
  210. {
  211. if (!g_scsi_dma.next_app_buf && data == g_scsi_dma.app_buf + g_scsi_dma.app_bytes)
  212. {
  213. // Combine with currently running request
  214. g_scsi_dma.app_bytes += count;
  215. count = 0;
  216. }
  217. else if (data == g_scsi_dma.next_app_buf + g_scsi_dma.next_app_bytes)
  218. {
  219. // Combine with queued request
  220. g_scsi_dma.next_app_bytes += count;
  221. count = 0;
  222. }
  223. else if (!g_scsi_dma.next_app_buf)
  224. {
  225. // Add as queued request
  226. g_scsi_dma.next_app_buf = (uint8_t*)data;
  227. g_scsi_dma.next_app_bytes = count;
  228. count = 0;
  229. }
  230. }
  231. __enable_irq();
  232. // Check if the request was combined
  233. if (count == 0) return;
  234. if (g_scsi_dma_state != SCSIDMA_IDLE && g_scsi_dma_state != SCSIDMA_WRITE_DONE)
  235. {
  236. // Wait for previous request to finish
  237. scsi_accel_rp2040_finishWrite(resetFlag);
  238. if (*resetFlag)
  239. {
  240. return;
  241. }
  242. }
  243. bool must_reconfig_gpio = (g_scsi_dma_state == SCSIDMA_IDLE);
  244. g_scsi_dma_state = SCSIDMA_WRITE;
  245. g_scsi_dma.app_buf = (uint8_t*)data;
  246. g_scsi_dma.app_bytes = count;
  247. g_scsi_dma.dma_bytes = 0;
  248. g_scsi_dma.next_app_buf = 0;
  249. g_scsi_dma.next_app_bytes = 0;
  250. if (must_reconfig_gpio)
  251. {
  252. SCSI_ENABLE_DATA_OUT();
  253. if (g_scsi_dma.syncOffset == 0)
  254. {
  255. // Asynchronous write
  256. config_parity_sm_for_write();
  257. pio_sm_init(SCSI_DMA_PIO, SCSI_DATA_SM, g_scsi_dma.pio_offset_async_write, &g_scsi_dma.pio_cfg_async_write);
  258. scsidma_config_gpio();
  259. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_DATA_SM, true);
  260. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_PARITY_SM, true);
  261. }
  262. else
  263. {
  264. // Synchronous write
  265. // Data state machine writes data to SCSI bus and dummy bits to its RX fifo.
  266. // Sync state machine empties the dummy bits every time ACK is received, to control the transmit pace.
  267. config_parity_sm_for_write();
  268. pio_sm_init(SCSI_DMA_PIO, SCSI_DATA_SM, g_scsi_dma.pio_offset_sync_write, &g_scsi_dma.pio_cfg_sync_write);
  269. 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);
  270. scsidma_config_gpio();
  271. // Prefill RX fifo to set the syncOffset
  272. for (int i = 0; i < g_scsi_dma.syncOffsetPreload; i++)
  273. {
  274. pio_sm_exec(SCSI_DMA_PIO, SCSI_DATA_SM,
  275. pio_encode_push(false, false) | pio_encode_sideset(1, 1));
  276. }
  277. // Fill the pacer TX fifo
  278. // DMA should start transferring only after ACK pulses are received
  279. for (int i = 0; i < 4; i++)
  280. {
  281. pio_sm_put(SCSI_DMA_PIO, SCSI_SYNC_SM, 0);
  282. }
  283. // Fill the pacer OSR
  284. pio_sm_exec(SCSI_DMA_PIO, SCSI_SYNC_SM,
  285. pio_encode_mov(pio_osr, pio_null));
  286. // Start DMA transfer to move dummy bits to write pacer
  287. dma_channel_configure(SCSI_DMA_CH_D,
  288. &g_scsi_dma.dmacfg_write_chD,
  289. &SCSI_DMA_PIO->txf[SCSI_SYNC_SM],
  290. &SCSI_DMA_PIO->rxf[SCSI_DATA_SM],
  291. 0xFFFFFFFF,
  292. true
  293. );
  294. // Enable state machines
  295. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_SYNC_SM, true);
  296. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_DATA_SM, true);
  297. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_PARITY_SM, true);
  298. }
  299. dma_channel_set_irq0_enabled(SCSI_DMA_CH_A, true);
  300. }
  301. start_dma_write();
  302. }
  303. bool scsi_accel_rp2040_isWriteFinished(const uint8_t* data)
  304. {
  305. // Check if everything has completed
  306. if (g_scsi_dma_state == SCSIDMA_IDLE || g_scsi_dma_state == SCSIDMA_WRITE_DONE)
  307. {
  308. return true;
  309. }
  310. if (!data)
  311. return false;
  312. // Check if this data item is still in queue.
  313. bool finished = true;
  314. __disable_irq();
  315. if (data >= g_scsi_dma.app_buf &&
  316. data < g_scsi_dma.app_buf + g_scsi_dma.app_bytes &&
  317. (uint32_t)data >= dma_hw->ch[SCSI_DMA_CH_A].al1_read_addr)
  318. {
  319. finished = false; // In current transfer
  320. }
  321. else if (data >= g_scsi_dma.next_app_buf &&
  322. data < g_scsi_dma.next_app_buf + g_scsi_dma.next_app_bytes)
  323. {
  324. finished = false; // In queued transfer
  325. }
  326. __enable_irq();
  327. return finished;
  328. }
  329. // Once DMA has finished, check if all PIO queues have been drained
  330. static bool scsi_accel_rp2040_isWriteDone()
  331. {
  332. // Check if data is still waiting in PIO FIFO
  333. if (!pio_sm_is_tx_fifo_empty(SCSI_DMA_PIO, SCSI_PARITY_SM) ||
  334. !pio_sm_is_rx_fifo_empty(SCSI_DMA_PIO, SCSI_PARITY_SM) ||
  335. !pio_sm_is_tx_fifo_empty(SCSI_DMA_PIO, SCSI_DATA_SM))
  336. {
  337. return false;
  338. }
  339. if (g_scsi_dma.syncOffset > 0)
  340. {
  341. // Check if all bytes of synchronous write have been acknowledged
  342. if (pio_sm_get_rx_fifo_level(SCSI_DMA_PIO, SCSI_DATA_SM) > g_scsi_dma.syncOffsetPreload)
  343. return false;
  344. }
  345. else
  346. {
  347. // Check if state machine has written out its OSR
  348. if (pio_sm_get_pc(SCSI_DMA_PIO, SCSI_DATA_SM) != g_scsi_dma.pio_offset_async_write)
  349. return false;
  350. }
  351. // Check if ACK of the final byte has finished
  352. if (SCSI_IN(ACK))
  353. return false;
  354. return true;
  355. }
  356. static void scsi_accel_rp2040_stopWrite(volatile int *resetFlag)
  357. {
  358. // Wait for TX fifo to be empty and ACK to go high
  359. // For synchronous writes wait for all ACKs to be received also
  360. uint32_t start = millis();
  361. while (!scsi_accel_rp2040_isWriteDone() && !*resetFlag)
  362. {
  363. if ((uint32_t)(millis() - start) > 5000)
  364. {
  365. logmsg("scsi_accel_rp2040_stopWrite() timeout");
  366. scsi_accel_log_state();
  367. *resetFlag = 1;
  368. break;
  369. }
  370. }
  371. dma_channel_abort(SCSI_DMA_CH_A);
  372. dma_channel_abort(SCSI_DMA_CH_B);
  373. dma_channel_abort(SCSI_DMA_CH_C);
  374. dma_channel_abort(SCSI_DMA_CH_D);
  375. dma_channel_set_irq0_enabled(SCSI_DMA_CH_A, false);
  376. g_scsi_dma_state = SCSIDMA_IDLE;
  377. SCSI_RELEASE_DATA_REQ();
  378. scsidma_config_gpio();
  379. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_PARITY_SM, false);
  380. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_DATA_SM, false);
  381. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_SYNC_SM, false);
  382. }
  383. void scsi_accel_rp2040_finishWrite(volatile int *resetFlag)
  384. {
  385. uint32_t start = millis();
  386. while (g_scsi_dma_state != SCSIDMA_IDLE && !*resetFlag)
  387. {
  388. if ((uint32_t)(millis() - start) > 5000)
  389. {
  390. logmsg("scsi_accel_rp2040_finishWrite() timeout");
  391. scsi_accel_log_state();
  392. *resetFlag = 1;
  393. break;
  394. }
  395. if (g_scsi_dma_state == SCSIDMA_WRITE_DONE || *resetFlag)
  396. {
  397. // DMA done, wait for PIO to finish also and reconfig GPIO.
  398. scsi_accel_rp2040_stopWrite(resetFlag);
  399. }
  400. }
  401. }
  402. /****************************************/
  403. /* Accelerated reads from SCSI bus */
  404. /****************************************/
  405. // Load the SCSI read state machine with the address of the parity lookup table.
  406. // Also sets up DMA channels B, C and D
  407. static void config_parity_sm_for_read()
  408. {
  409. // Configure parity check state machine
  410. pio_sm_init(SCSI_DMA_PIO, SCSI_PARITY_SM, g_scsi_dma.pio_offset_read_parity, &g_scsi_dma.pio_cfg_read_parity);
  411. // Load base address to state machine register X
  412. uint32_t addrbase = (uint32_t)&g_scsi_parity_check_lookup[0];
  413. assert((addrbase & 0x3FF) == 0);
  414. pio_sm_init(SCSI_DMA_PIO, SCSI_DATA_SM, g_scsi_dma.pio_offset_read, &g_scsi_dma.pio_cfg_read);
  415. pio_sm_put(SCSI_DMA_PIO, SCSI_DATA_SM, addrbase >> 10);
  416. pio_sm_exec(SCSI_DMA_PIO, SCSI_DATA_SM, pio_encode_pull(false, false) | pio_encode_sideset(1, 1));
  417. pio_sm_exec(SCSI_DMA_PIO, SCSI_DATA_SM, pio_encode_mov(pio_y, pio_osr) | pio_encode_sideset(1, 1));
  418. // For synchronous mode, the REQ pin is driven by SCSI_SYNC_SM, so disable it in SCSI_DATA_SM
  419. if (g_scsi_dma.syncOffset > 0)
  420. {
  421. pio_sm_set_sideset_pins(SCSI_DMA_PIO, SCSI_DATA_SM, 0);
  422. }
  423. // DMA channel B will read g_scsi_parity_check_lookup and write to scsi_read_parity PIO.
  424. dma_channel_configure(SCSI_DMA_CH_B,
  425. &g_scsi_dma.dmacfg_read_chB,
  426. &SCSI_DMA_PIO->txf[SCSI_PARITY_SM],
  427. NULL,
  428. 1, false);
  429. // DMA channel C will copy addresses from data PIO to DMA channel B read address register.
  430. // It is triggered by the data SM RX FIFO request.
  431. // This triggers channel B by writing to READ_ADDR_TRIG
  432. // Channel B chaining re-enables this channel.
  433. dma_channel_configure(SCSI_DMA_CH_C,
  434. &g_scsi_dma.dmacfg_read_chC,
  435. &dma_hw->ch[SCSI_DMA_CH_B].al3_read_addr_trig,
  436. &SCSI_DMA_PIO->rxf[SCSI_DATA_SM],
  437. 1, true);
  438. if (g_scsi_dma.syncOffset == 0)
  439. {
  440. // DMA channel D will copy dummy words to scsi_accel_read PIO to set the number
  441. // of bytes to transfer.
  442. static const uint32_t dummy = 0;
  443. dma_channel_configure(SCSI_DMA_CH_D,
  444. &g_scsi_dma.dmacfg_read_chD,
  445. &SCSI_DMA_PIO->txf[SCSI_DATA_SM],
  446. &dummy,
  447. 0, false);
  448. }
  449. else
  450. {
  451. 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);
  452. // DMA channel D will copy words from scsi_sync_read_pacer to scsi_accel_read PIO
  453. // to control the offset between REQ pulses sent and ACK pulses received.
  454. dma_channel_configure(SCSI_DMA_CH_D,
  455. &g_scsi_dma.dmacfg_read_chD,
  456. &SCSI_DMA_PIO->txf[SCSI_DATA_SM],
  457. &SCSI_DMA_PIO->rxf[SCSI_SYNC_SM],
  458. 0, false);
  459. }
  460. // Clear PIO IRQ flag that is used to detect parity error
  461. SCSI_DMA_PIO->irq = 1;
  462. }
  463. static void start_dma_read()
  464. {
  465. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_PARITY_SM, false);
  466. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_DATA_SM, false);
  467. pio_sm_clear_fifos(SCSI_DMA_PIO, SCSI_PARITY_SM);
  468. pio_sm_clear_fifos(SCSI_DMA_PIO, SCSI_DATA_SM);
  469. if (g_scsi_dma.app_bytes <= g_scsi_dma.dma_bytes)
  470. {
  471. // Buffer has been fully processed, swap it
  472. g_scsi_dma.dma_bytes = 0;
  473. g_scsi_dma.app_buf = g_scsi_dma.next_app_buf;
  474. g_scsi_dma.app_bytes = g_scsi_dma.next_app_bytes;
  475. g_scsi_dma.next_app_buf = 0;
  476. g_scsi_dma.next_app_bytes = 0;
  477. }
  478. // Check if we are all done.
  479. // From SCSIDMA_READ_DONE state we can either go to IDLE in stopRead()
  480. // or back to READ in startWrite().
  481. uint32_t bytes_to_read = g_scsi_dma.app_bytes - g_scsi_dma.dma_bytes;
  482. if (bytes_to_read == 0)
  483. {
  484. g_scsi_dma_state = SCSIDMA_READ_DONE;
  485. return;
  486. }
  487. if (g_scsi_dma.syncOffset == 0)
  488. {
  489. // Start sending dummy words to scsi_accel_read state machine
  490. dma_channel_set_trans_count(SCSI_DMA_CH_D, bytes_to_read, true);
  491. }
  492. else
  493. {
  494. // Set number of bytes to receive to the scsi_sync_read_pacer state machine register X
  495. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_SYNC_SM, false);
  496. hw_clear_bits(&SCSI_DMA_PIO->sm[SCSI_SYNC_SM].shiftctrl, PIO_SM0_SHIFTCTRL_FJOIN_RX_BITS);
  497. pio_sm_put(SCSI_DMA_PIO, SCSI_SYNC_SM, bytes_to_read - 1);
  498. pio_sm_exec(SCSI_DMA_PIO, SCSI_SYNC_SM, pio_encode_pull(false, false) | pio_encode_sideset(1, 1));
  499. pio_sm_exec(SCSI_DMA_PIO, SCSI_SYNC_SM, pio_encode_mov(pio_x, pio_osr) | pio_encode_sideset(1, 1));
  500. hw_set_bits(&SCSI_DMA_PIO->sm[SCSI_SYNC_SM].shiftctrl, PIO_SM0_SHIFTCTRL_FJOIN_RX_BITS);
  501. // Prefill FIFOs to get correct syncOffset
  502. int prefill = 12 - g_scsi_dma.syncOffset;
  503. // Always at least 1 word to avoid race condition between REQ and ACK pulses
  504. if (prefill < 1) prefill = 1;
  505. // Up to 4 words in SCSI_DATA_SM TX fifo
  506. for (int i = 0; i < 4 && prefill > 0; i++)
  507. {
  508. pio_sm_put(SCSI_DMA_PIO, SCSI_DATA_SM, 0);
  509. prefill--;
  510. }
  511. // Up to 8 words in SCSI_SYNC_SM RX fifo
  512. for (int i = 0; i < 8 && prefill > 0; i++)
  513. {
  514. pio_sm_exec(SCSI_DMA_PIO, SCSI_SYNC_SM, pio_encode_push(false, false) | pio_encode_sideset(1, 1));
  515. prefill--;
  516. }
  517. 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));
  518. // Start transfers
  519. dma_channel_set_trans_count(SCSI_DMA_CH_D, bytes_to_read, true);
  520. }
  521. // Start DMA to fill the destination buffer
  522. uint8_t *dest_buf = &g_scsi_dma.app_buf[g_scsi_dma.dma_bytes];
  523. g_scsi_dma.dma_bytes += bytes_to_read;
  524. dma_channel_configure(SCSI_DMA_CH_A,
  525. &g_scsi_dma.dmacfg_read_chA,
  526. dest_buf,
  527. &SCSI_DMA_PIO->rxf[SCSI_PARITY_SM],
  528. bytes_to_read,
  529. true
  530. );
  531. // Ready to start the data and parity check state machines
  532. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_PARITY_SM, true);
  533. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_DATA_SM, true);
  534. if (g_scsi_dma.syncOffset > 0)
  535. {
  536. // Start sending REQ pulses
  537. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_SYNC_SM, true);
  538. }
  539. }
  540. void scsi_accel_rp2040_startRead(uint8_t *data, uint32_t count, int *parityError, volatile int *resetFlag)
  541. {
  542. // Any write requests should be matched with a stopWrite()
  543. assert(g_scsi_dma_state != SCSIDMA_WRITE && g_scsi_dma_state != SCSIDMA_WRITE_DONE);
  544. __disable_irq();
  545. if (g_scsi_dma_state == SCSIDMA_READ)
  546. {
  547. if (!g_scsi_dma.next_app_buf && data == g_scsi_dma.app_buf + g_scsi_dma.app_bytes)
  548. {
  549. // Combine with currently running request
  550. g_scsi_dma.app_bytes += count;
  551. count = 0;
  552. }
  553. else if (data == g_scsi_dma.next_app_buf + g_scsi_dma.next_app_bytes)
  554. {
  555. // Combine with queued request
  556. g_scsi_dma.next_app_bytes += count;
  557. count = 0;
  558. }
  559. else if (!g_scsi_dma.next_app_buf)
  560. {
  561. // Add as queued request
  562. g_scsi_dma.next_app_buf = (uint8_t*)data;
  563. g_scsi_dma.next_app_bytes = count;
  564. count = 0;
  565. }
  566. }
  567. __enable_irq();
  568. // Check if the request was combined
  569. if (count == 0) return;
  570. if (g_scsi_dma_state != SCSIDMA_IDLE && g_scsi_dma_state != SCSIDMA_READ_DONE)
  571. {
  572. // Wait for previous request to finish
  573. scsi_accel_rp2040_finishRead(NULL, 0, parityError, resetFlag);
  574. if (*resetFlag)
  575. {
  576. return;
  577. }
  578. }
  579. bool must_reconfig_gpio = (g_scsi_dma_state == SCSIDMA_IDLE);
  580. g_scsi_dma_state = SCSIDMA_READ;
  581. g_scsi_dma.app_buf = (uint8_t*)data;
  582. g_scsi_dma.app_bytes = count;
  583. g_scsi_dma.dma_bytes = 0;
  584. g_scsi_dma.next_app_buf = 0;
  585. g_scsi_dma.next_app_bytes = 0;
  586. if (must_reconfig_gpio)
  587. {
  588. config_parity_sm_for_read();
  589. scsidma_config_gpio();
  590. dma_channel_set_irq0_enabled(SCSI_DMA_CH_A, true);
  591. }
  592. start_dma_read();
  593. }
  594. bool scsi_accel_rp2040_isReadFinished(const uint8_t* data)
  595. {
  596. // Check if everything has completed
  597. if (g_scsi_dma_state == SCSIDMA_IDLE || g_scsi_dma_state == SCSIDMA_READ_DONE)
  598. {
  599. return true;
  600. }
  601. if (!data)
  602. return false;
  603. // Check if this data item is still in queue.
  604. bool finished = true;
  605. __disable_irq();
  606. if (data >= g_scsi_dma.app_buf &&
  607. data < g_scsi_dma.app_buf + g_scsi_dma.app_bytes &&
  608. (uint32_t)data >= dma_hw->ch[SCSI_DMA_CH_A].write_addr)
  609. {
  610. finished = false; // In current transfer
  611. }
  612. else if (data >= g_scsi_dma.next_app_buf &&
  613. data < g_scsi_dma.next_app_buf + g_scsi_dma.next_app_bytes)
  614. {
  615. finished = false; // In queued transfer
  616. }
  617. __enable_irq();
  618. return finished;
  619. }
  620. static void scsi_accel_rp2040_stopRead()
  621. {
  622. dma_channel_abort(SCSI_DMA_CH_A);
  623. dma_channel_abort(SCSI_DMA_CH_B);
  624. dma_channel_abort(SCSI_DMA_CH_C);
  625. dma_channel_abort(SCSI_DMA_CH_D);
  626. dma_channel_set_irq0_enabled(SCSI_DMA_CH_A, false);
  627. g_scsi_dma_state = SCSIDMA_IDLE;
  628. SCSI_RELEASE_DATA_REQ();
  629. scsidma_config_gpio();
  630. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_PARITY_SM, false);
  631. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_DATA_SM, false);
  632. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_SYNC_SM, false);
  633. }
  634. void scsi_accel_rp2040_finishRead(const uint8_t *data, uint32_t count, int *parityError, volatile int *resetFlag)
  635. {
  636. uint32_t start = millis();
  637. const uint8_t *query_addr = (data ? (data + count - 1) : NULL);
  638. while (!scsi_accel_rp2040_isReadFinished(query_addr) && !*resetFlag)
  639. {
  640. if ((uint32_t)(millis() - start) > 5000)
  641. {
  642. logmsg("scsi_accel_rp2040_finishRead timeout");
  643. scsi_accel_log_state();
  644. *resetFlag = 1;
  645. break;
  646. }
  647. }
  648. if (g_scsi_dma_state == SCSIDMA_READ_DONE || *resetFlag)
  649. {
  650. // This was last buffer, release bus
  651. scsi_accel_rp2040_stopRead();
  652. }
  653. // Check if any parity errors have been detected during the transfer so far
  654. if (parityError != NULL && (SCSI_DMA_PIO->irq & 1))
  655. {
  656. dbgmsg("scsi_accel_rp2040_finishRead(", bytearray(data, count), ") detected parity error");
  657. *parityError = true;
  658. }
  659. }
  660. /*******************************************************/
  661. /* Initialization functions common to read/write */
  662. /*******************************************************/
  663. static void scsi_dma_irq()
  664. {
  665. #ifndef ENABLE_AUDIO_OUTPUT
  666. dma_hw->ints0 = (1 << SCSI_DMA_CH_A);
  667. #else
  668. // see audio.h for whats going on here
  669. if (dma_hw->intr & (1 << SCSI_DMA_CH_A)) {
  670. dma_hw->ints0 = (1 << SCSI_DMA_CH_A);
  671. } else {
  672. audio_dma_irq();
  673. return;
  674. }
  675. #endif
  676. scsidma_state_t state = g_scsi_dma_state;
  677. if (state == SCSIDMA_WRITE)
  678. {
  679. // Start writing from next buffer, if any, or set state to SCSIDMA_WRITE_DONE
  680. start_dma_write();
  681. }
  682. else if (state == SCSIDMA_READ)
  683. {
  684. // Start reading into next buffer, if any, or set state to SCSIDMA_READ_DONE
  685. start_dma_read();
  686. }
  687. }
  688. // Select GPIO from PIO peripheral or from software controlled SIO
  689. static void scsidma_config_gpio()
  690. {
  691. if (g_scsi_dma_state == SCSIDMA_IDLE)
  692. {
  693. iobank0_hw->io[SCSI_IO_DB0].ctrl = GPIO_FUNC_SIO;
  694. iobank0_hw->io[SCSI_IO_DB1].ctrl = GPIO_FUNC_SIO;
  695. iobank0_hw->io[SCSI_IO_DB2].ctrl = GPIO_FUNC_SIO;
  696. iobank0_hw->io[SCSI_IO_DB3].ctrl = GPIO_FUNC_SIO;
  697. iobank0_hw->io[SCSI_IO_DB4].ctrl = GPIO_FUNC_SIO;
  698. iobank0_hw->io[SCSI_IO_DB5].ctrl = GPIO_FUNC_SIO;
  699. iobank0_hw->io[SCSI_IO_DB6].ctrl = GPIO_FUNC_SIO;
  700. iobank0_hw->io[SCSI_IO_DB7].ctrl = GPIO_FUNC_SIO;
  701. iobank0_hw->io[SCSI_IO_DBP].ctrl = GPIO_FUNC_SIO;
  702. iobank0_hw->io[SCSI_OUT_REQ].ctrl = GPIO_FUNC_SIO;
  703. }
  704. else if (g_scsi_dma_state == SCSIDMA_WRITE)
  705. {
  706. // Make sure the initial state of all pins is high and output
  707. pio_sm_set_pins(SCSI_DMA_PIO, SCSI_DATA_SM, SCSI_IO_DATA_MASK | (1 << SCSI_OUT_REQ));
  708. pio_sm_set_consecutive_pindirs(SCSI_DMA_PIO, SCSI_DATA_SM, SCSI_IO_DB0, 9, true);
  709. pio_sm_set_consecutive_pindirs(SCSI_DMA_PIO, SCSI_DATA_SM, SCSI_OUT_REQ, 1, true);
  710. iobank0_hw->io[SCSI_IO_DB0].ctrl = GPIO_FUNC_PIO0;
  711. iobank0_hw->io[SCSI_IO_DB1].ctrl = GPIO_FUNC_PIO0;
  712. iobank0_hw->io[SCSI_IO_DB2].ctrl = GPIO_FUNC_PIO0;
  713. iobank0_hw->io[SCSI_IO_DB3].ctrl = GPIO_FUNC_PIO0;
  714. iobank0_hw->io[SCSI_IO_DB4].ctrl = GPIO_FUNC_PIO0;
  715. iobank0_hw->io[SCSI_IO_DB5].ctrl = GPIO_FUNC_PIO0;
  716. iobank0_hw->io[SCSI_IO_DB6].ctrl = GPIO_FUNC_PIO0;
  717. iobank0_hw->io[SCSI_IO_DB7].ctrl = GPIO_FUNC_PIO0;
  718. iobank0_hw->io[SCSI_IO_DBP].ctrl = GPIO_FUNC_PIO0;
  719. iobank0_hw->io[SCSI_OUT_REQ].ctrl = GPIO_FUNC_PIO0;
  720. }
  721. else if (g_scsi_dma_state == SCSIDMA_READ)
  722. {
  723. if (g_scsi_dma.syncOffset == 0)
  724. {
  725. // Asynchronous read
  726. // Data bus as input, REQ pin as output
  727. pio_sm_set_pins(SCSI_DMA_PIO, SCSI_DATA_SM, SCSI_IO_DATA_MASK | (1 << SCSI_OUT_REQ));
  728. pio_sm_set_consecutive_pindirs(SCSI_DMA_PIO, SCSI_DATA_SM, SCSI_IO_DB0, 9, false);
  729. pio_sm_set_consecutive_pindirs(SCSI_DMA_PIO, SCSI_DATA_SM, SCSI_OUT_REQ, 1, true);
  730. }
  731. else
  732. {
  733. // Synchronous read, REQ pin is written by SYNC_SM
  734. pio_sm_set_pins(SCSI_DMA_PIO, SCSI_SYNC_SM, SCSI_IO_DATA_MASK | (1 << SCSI_OUT_REQ));
  735. pio_sm_set_consecutive_pindirs(SCSI_DMA_PIO, SCSI_DATA_SM, SCSI_IO_DB0, 9, false);
  736. pio_sm_set_consecutive_pindirs(SCSI_DMA_PIO, SCSI_SYNC_SM, SCSI_OUT_REQ, 1, true);
  737. }
  738. iobank0_hw->io[SCSI_IO_DB0].ctrl = GPIO_FUNC_SIO;
  739. iobank0_hw->io[SCSI_IO_DB1].ctrl = GPIO_FUNC_SIO;
  740. iobank0_hw->io[SCSI_IO_DB2].ctrl = GPIO_FUNC_SIO;
  741. iobank0_hw->io[SCSI_IO_DB3].ctrl = GPIO_FUNC_SIO;
  742. iobank0_hw->io[SCSI_IO_DB4].ctrl = GPIO_FUNC_SIO;
  743. iobank0_hw->io[SCSI_IO_DB5].ctrl = GPIO_FUNC_SIO;
  744. iobank0_hw->io[SCSI_IO_DB6].ctrl = GPIO_FUNC_SIO;
  745. iobank0_hw->io[SCSI_IO_DB7].ctrl = GPIO_FUNC_SIO;
  746. iobank0_hw->io[SCSI_IO_DBP].ctrl = GPIO_FUNC_SIO;
  747. iobank0_hw->io[SCSI_OUT_REQ].ctrl = GPIO_FUNC_PIO0;
  748. }
  749. }
  750. void scsi_accel_rp2040_init()
  751. {
  752. g_scsi_dma_state = SCSIDMA_IDLE;
  753. scsidma_config_gpio();
  754. // Mark channels as being in use, unless it has been done already
  755. if (!g_channels_claimed)
  756. {
  757. pio_sm_claim(SCSI_DMA_PIO, SCSI_PARITY_SM);
  758. pio_sm_claim(SCSI_DMA_PIO, SCSI_DATA_SM);
  759. pio_sm_claim(SCSI_DMA_PIO, SCSI_SYNC_SM);
  760. dma_channel_claim(SCSI_DMA_CH_A);
  761. dma_channel_claim(SCSI_DMA_CH_B);
  762. dma_channel_claim(SCSI_DMA_CH_C);
  763. dma_channel_claim(SCSI_DMA_CH_D);
  764. g_channels_claimed = true;
  765. }
  766. // Load PIO programs
  767. pio_clear_instruction_memory(SCSI_DMA_PIO);
  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, SCSI_OUT_REQ);
  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, SCSI_OUT_REQ);
  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, SCSI_OUT_REQ);
  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, SCSI_OUT_REQ);
  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. logmsg("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. }