scsi_accel_target.cpp 43 KB

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