scsi_accel_rp2040.cpp 42 KB

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