scsi_accel_rp2040.cpp 42 KB

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