scsi_accel_rp2040.cpp 42 KB

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