scsi_accel_rp2040.cpp 43 KB

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