scsi_accel_rp2040.cpp 42 KB

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