scsi_accel_target.cpp 45 KB

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