scsi_accel_target.cpp 45 KB

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