scsi_accel_rp2040.cpp 42 KB

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