scsi_accel_rp2040.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 "ZuluSCSI_platform.h"
  9. #include "ZuluSCSI_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. #define SCSI_DMA_PIO pio0
  17. #define SCSI_DMA_SM 0
  18. #define SCSI_DMA_CH 0
  19. enum scsidma_buf_sel_t { SCSIBUF_NONE = 0, SCSIBUF_A = 1, SCSIBUF_B = 2 };
  20. #define DMA_BUF_SIZE 128
  21. static struct {
  22. uint8_t *app_buf; // Buffer provided by application
  23. uint32_t app_bytes; // Bytes available in application buffer
  24. uint32_t dma_bytes; // Bytes that have been converted to DMA buffer so far
  25. uint8_t *next_app_buf; // Next buffer from application after current one finishes
  26. uint32_t next_app_bytes; // Bytes in next buffer
  27. // PIO configurations
  28. uint32_t pio_offset_async_write;
  29. uint32_t pio_offset_async_read;
  30. pio_sm_config pio_cfg_async_write;
  31. pio_sm_config pio_cfg_async_read;
  32. // DMA configurations
  33. dma_channel_config dma_write_config;
  34. // We use two DMA buffers alternatively
  35. // The buffer contains the data bytes with parity added.
  36. scsidma_buf_sel_t dma_current_buf;
  37. uint32_t dma_countA;
  38. uint32_t dma_countB;
  39. uint32_t dma_bufA[DMA_BUF_SIZE];
  40. uint32_t dma_bufB[DMA_BUF_SIZE];
  41. } g_scsi_dma;
  42. enum scsidma_state_t { SCSIDMA_IDLE = 0,
  43. SCSIDMA_WRITE, SCSIDMA_WRITE_DONE,
  44. SCSIDMA_READ };
  45. static volatile scsidma_state_t g_scsi_dma_state;
  46. static bool g_channels_claimed = false;
  47. // Fill DMA buffer and return number of words ready to be transferred
  48. static uint32_t refill_dmabuf(uint32_t *buf)
  49. {
  50. uint32_t count = (g_scsi_dma.app_bytes - g_scsi_dma.dma_bytes) / 2;
  51. if (count > DMA_BUF_SIZE) count = DMA_BUF_SIZE;
  52. uint16_t *src = (uint16_t*)&g_scsi_dma.app_buf[g_scsi_dma.dma_bytes];
  53. uint16_t *end = src + count;
  54. uint32_t *dst = buf;
  55. while (src < end)
  56. {
  57. uint16_t input = *src++;
  58. *dst++ = (g_scsi_parity_lookup[input & 0xFF])
  59. | ((g_scsi_parity_lookup[input >> 8]) << 16);
  60. }
  61. g_scsi_dma.dma_bytes += count * 2;
  62. // Check if this buffer has been fully processed
  63. if (g_scsi_dma.dma_bytes >= g_scsi_dma.app_bytes)
  64. {
  65. assert(g_scsi_dma.dma_bytes == g_scsi_dma.app_bytes);
  66. g_scsi_dma.dma_bytes = 0;
  67. g_scsi_dma.app_buf = g_scsi_dma.next_app_buf;
  68. g_scsi_dma.app_bytes = g_scsi_dma.next_app_bytes;
  69. g_scsi_dma.next_app_buf = 0;
  70. g_scsi_dma.next_app_bytes = 0;
  71. }
  72. return count;
  73. }
  74. // Select GPIO from PIO peripheral or from software controlled SIO
  75. static void scsidma_config_gpio()
  76. {
  77. if (g_scsi_dma_state == SCSIDMA_IDLE)
  78. {
  79. iobank0_hw->io[SCSI_IO_DB0].ctrl = GPIO_FUNC_SIO;
  80. iobank0_hw->io[SCSI_IO_DB1].ctrl = GPIO_FUNC_SIO;
  81. iobank0_hw->io[SCSI_IO_DB2].ctrl = GPIO_FUNC_SIO;
  82. iobank0_hw->io[SCSI_IO_DB3].ctrl = GPIO_FUNC_SIO;
  83. iobank0_hw->io[SCSI_IO_DB4].ctrl = GPIO_FUNC_SIO;
  84. iobank0_hw->io[SCSI_IO_DB5].ctrl = GPIO_FUNC_SIO;
  85. iobank0_hw->io[SCSI_IO_DB6].ctrl = GPIO_FUNC_SIO;
  86. iobank0_hw->io[SCSI_IO_DB7].ctrl = GPIO_FUNC_SIO;
  87. iobank0_hw->io[SCSI_IO_DBP].ctrl = GPIO_FUNC_SIO;
  88. iobank0_hw->io[SCSI_OUT_REQ].ctrl = GPIO_FUNC_SIO;
  89. }
  90. else if (g_scsi_dma_state == SCSIDMA_WRITE)
  91. {
  92. // Make sure the initial state of all pins is high and output
  93. pio_sm_set_pins(SCSI_DMA_PIO, SCSI_DMA_SM, 0x3FF);
  94. pio_sm_set_consecutive_pindirs(SCSI_DMA_PIO, SCSI_DMA_SM, 0, 10, true);
  95. iobank0_hw->io[SCSI_IO_DB0].ctrl = GPIO_FUNC_PIO0;
  96. iobank0_hw->io[SCSI_IO_DB1].ctrl = GPIO_FUNC_PIO0;
  97. iobank0_hw->io[SCSI_IO_DB2].ctrl = GPIO_FUNC_PIO0;
  98. iobank0_hw->io[SCSI_IO_DB3].ctrl = GPIO_FUNC_PIO0;
  99. iobank0_hw->io[SCSI_IO_DB4].ctrl = GPIO_FUNC_PIO0;
  100. iobank0_hw->io[SCSI_IO_DB5].ctrl = GPIO_FUNC_PIO0;
  101. iobank0_hw->io[SCSI_IO_DB6].ctrl = GPIO_FUNC_PIO0;
  102. iobank0_hw->io[SCSI_IO_DB7].ctrl = GPIO_FUNC_PIO0;
  103. iobank0_hw->io[SCSI_IO_DBP].ctrl = GPIO_FUNC_PIO0;
  104. iobank0_hw->io[SCSI_OUT_REQ].ctrl = GPIO_FUNC_PIO0;
  105. }
  106. else if (g_scsi_dma_state == SCSIDMA_READ)
  107. {
  108. // Data bus as input, REQ pin as output
  109. pio_sm_set_pins(SCSI_DMA_PIO, SCSI_DMA_SM, 0x3FF);
  110. pio_sm_set_consecutive_pindirs(SCSI_DMA_PIO, SCSI_DMA_SM, 0, 9, false);
  111. pio_sm_set_consecutive_pindirs(SCSI_DMA_PIO, SCSI_DMA_SM, 9, 1, true);
  112. iobank0_hw->io[SCSI_IO_DB0].ctrl = GPIO_FUNC_SIO;
  113. iobank0_hw->io[SCSI_IO_DB1].ctrl = GPIO_FUNC_SIO;
  114. iobank0_hw->io[SCSI_IO_DB2].ctrl = GPIO_FUNC_SIO;
  115. iobank0_hw->io[SCSI_IO_DB3].ctrl = GPIO_FUNC_SIO;
  116. iobank0_hw->io[SCSI_IO_DB4].ctrl = GPIO_FUNC_SIO;
  117. iobank0_hw->io[SCSI_IO_DB5].ctrl = GPIO_FUNC_SIO;
  118. iobank0_hw->io[SCSI_IO_DB6].ctrl = GPIO_FUNC_SIO;
  119. iobank0_hw->io[SCSI_IO_DB7].ctrl = GPIO_FUNC_SIO;
  120. iobank0_hw->io[SCSI_IO_DBP].ctrl = GPIO_FUNC_SIO;
  121. iobank0_hw->io[SCSI_OUT_REQ].ctrl = GPIO_FUNC_PIO0;
  122. }
  123. }
  124. static void start_dma_write()
  125. {
  126. // Prefill both DMA buffers
  127. g_scsi_dma.dma_countA = refill_dmabuf(g_scsi_dma.dma_bufA);
  128. g_scsi_dma.dma_countB = refill_dmabuf(g_scsi_dma.dma_bufB);
  129. // Start DMA from buffer A
  130. g_scsi_dma.dma_current_buf = SCSIBUF_A;
  131. dma_channel_configure(SCSI_DMA_CH,
  132. &g_scsi_dma.dma_write_config,
  133. &SCSI_DMA_PIO->txf[SCSI_DMA_SM],
  134. g_scsi_dma.dma_bufA,
  135. g_scsi_dma.dma_countA,
  136. true
  137. );
  138. }
  139. static void scsi_dma_write_irq()
  140. {
  141. dma_hw->ints0 = 1 << SCSI_DMA_CH;
  142. if (g_scsi_dma.dma_current_buf == SCSIBUF_A)
  143. {
  144. // Transfer from buffer A finished
  145. g_scsi_dma.dma_countA = 0;
  146. g_scsi_dma.dma_current_buf = SCSIBUF_NONE;
  147. if (g_scsi_dma.dma_countB != 0)
  148. {
  149. // Start transferring buffer B immediately
  150. dma_channel_set_trans_count(SCSI_DMA_CH, g_scsi_dma.dma_countB, false);
  151. dma_channel_set_read_addr(SCSI_DMA_CH, g_scsi_dma.dma_bufB, true);
  152. g_scsi_dma.dma_current_buf = SCSIBUF_B;
  153. // Refill buffer A for next time
  154. g_scsi_dma.dma_countA = refill_dmabuf(g_scsi_dma.dma_bufA);
  155. }
  156. }
  157. else
  158. {
  159. // Transfer from buffer B finished
  160. g_scsi_dma.dma_countB = 0;
  161. g_scsi_dma.dma_current_buf = SCSIBUF_NONE;
  162. if (g_scsi_dma.dma_countA != 0)
  163. {
  164. // Start transferring buffer A immediately
  165. dma_channel_set_trans_count(SCSI_DMA_CH, g_scsi_dma.dma_countA, false);
  166. dma_channel_set_read_addr(SCSI_DMA_CH, g_scsi_dma.dma_bufA, true);
  167. g_scsi_dma.dma_current_buf = SCSIBUF_A;
  168. // Refill buffer B for next time
  169. g_scsi_dma.dma_countB = refill_dmabuf(g_scsi_dma.dma_bufB);
  170. }
  171. }
  172. if (g_scsi_dma.dma_current_buf == SCSIBUF_NONE)
  173. {
  174. // Both buffers are empty, check if we have more data
  175. g_scsi_dma.dma_countA = refill_dmabuf(g_scsi_dma.dma_bufA);
  176. if (g_scsi_dma.dma_countA == 0)
  177. {
  178. // End of data for DMA, but PIO may still have bytes in its buffer
  179. g_scsi_dma_state = SCSIDMA_WRITE_DONE;
  180. }
  181. else
  182. {
  183. // Start transfer from buffer A
  184. dma_channel_set_trans_count(SCSI_DMA_CH, g_scsi_dma.dma_countA, false);
  185. dma_channel_set_read_addr(SCSI_DMA_CH, g_scsi_dma.dma_bufA, true);
  186. g_scsi_dma.dma_current_buf = SCSIBUF_A;
  187. // Refill B for the next interrupt
  188. g_scsi_dma.dma_countB = refill_dmabuf(g_scsi_dma.dma_bufB);
  189. }
  190. }
  191. }
  192. void scsi_accel_rp2040_startWrite(const uint8_t* data, uint32_t count, volatile int *resetFlag)
  193. {
  194. // Number of bytes should always be divisible by 2.
  195. assert((count & 1) == 0);
  196. __disable_irq();
  197. if (g_scsi_dma_state == SCSIDMA_WRITE)
  198. {
  199. if (!g_scsi_dma.next_app_buf && data == g_scsi_dma.app_buf + g_scsi_dma.app_bytes)
  200. {
  201. // Combine with currently running request
  202. g_scsi_dma.app_bytes += count;
  203. count = 0;
  204. }
  205. else if (data == g_scsi_dma.next_app_buf + g_scsi_dma.next_app_bytes)
  206. {
  207. // Combine with queued request
  208. g_scsi_dma.next_app_bytes += count;
  209. count = 0;
  210. }
  211. else if (!g_scsi_dma.next_app_buf)
  212. {
  213. // Add as queued request
  214. g_scsi_dma.next_app_buf = (uint8_t*)data;
  215. g_scsi_dma.next_app_bytes = count;
  216. count = 0;
  217. }
  218. }
  219. __enable_irq();
  220. // Check if the request was combined
  221. if (count == 0) return;
  222. if (g_scsi_dma_state != SCSIDMA_IDLE && g_scsi_dma_state != SCSIDMA_WRITE_DONE)
  223. {
  224. // Wait for previous request to finish
  225. scsi_accel_rp2040_finishWrite(resetFlag);
  226. if (*resetFlag)
  227. {
  228. return;
  229. }
  230. }
  231. bool must_reconfig_gpio = (g_scsi_dma_state == SCSIDMA_IDLE);
  232. g_scsi_dma_state = SCSIDMA_WRITE;
  233. g_scsi_dma.app_buf = (uint8_t*)data;
  234. g_scsi_dma.app_bytes = count;
  235. g_scsi_dma.dma_bytes = 0;
  236. g_scsi_dma.next_app_buf = 0;
  237. g_scsi_dma.next_app_bytes = 0;
  238. g_scsi_dma.dma_current_buf = SCSIBUF_NONE;
  239. if (must_reconfig_gpio)
  240. {
  241. SCSI_ENABLE_DATA_OUT();
  242. pio_sm_init(SCSI_DMA_PIO, SCSI_DMA_SM, g_scsi_dma.pio_offset_async_write, &g_scsi_dma.pio_cfg_async_write);
  243. scsidma_config_gpio();
  244. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_DMA_SM, true);
  245. dma_channel_set_irq0_enabled(SCSI_DMA_CH, true);
  246. irq_set_exclusive_handler(DMA_IRQ_0, scsi_dma_write_irq);
  247. irq_set_enabled(DMA_IRQ_0, true);
  248. }
  249. start_dma_write();
  250. }
  251. bool scsi_accel_rp2040_isWriteFinished(const uint8_t* data)
  252. {
  253. // Check if everything has completed
  254. if (g_scsi_dma_state == SCSIDMA_IDLE || g_scsi_dma_state == SCSIDMA_WRITE_DONE)
  255. {
  256. return true;
  257. }
  258. if (!data)
  259. return false;
  260. // Check if this data item is still in queue.
  261. __disable_irq();
  262. bool finished = true;
  263. if (data >= g_scsi_dma.app_buf + g_scsi_dma.dma_bytes &&
  264. data < g_scsi_dma.app_buf + g_scsi_dma.app_bytes)
  265. {
  266. finished = false; // In current transfer
  267. }
  268. else if (data >= g_scsi_dma.next_app_buf &&
  269. data < g_scsi_dma.next_app_buf + g_scsi_dma.next_app_bytes)
  270. {
  271. finished = false; // In queued transfer
  272. }
  273. __enable_irq();
  274. return finished;
  275. }
  276. void scsi_accel_rp2040_stopWrite(volatile int *resetFlag)
  277. {
  278. // Wait for TX fifo to be empty and ACK to go high
  279. uint32_t start = millis();
  280. while ((!pio_sm_is_tx_fifo_empty(SCSI_DMA_PIO, SCSI_DMA_SM) || SCSI_IN(ACK)) && !*resetFlag)
  281. {
  282. if ((uint32_t)(millis() - start) > 5000)
  283. {
  284. azlog("scsi_accel_rp2040_stopWrite() timeout");
  285. *resetFlag = 1;
  286. break;
  287. }
  288. }
  289. dma_channel_abort(SCSI_DMA_CH);
  290. dma_channel_set_irq0_enabled(SCSI_DMA_CH, false);
  291. g_scsi_dma_state = SCSIDMA_IDLE;
  292. SCSI_RELEASE_DATA_REQ();
  293. scsidma_config_gpio();
  294. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_DMA_SM, false);
  295. }
  296. void scsi_accel_rp2040_finishWrite(volatile int *resetFlag)
  297. {
  298. uint32_t start = millis();
  299. while (g_scsi_dma_state != SCSIDMA_IDLE && !*resetFlag)
  300. {
  301. if ((uint32_t)(millis() - start) > 5000)
  302. {
  303. azlog("scsi_accel_rp2040_finishWrite() timeout");
  304. *resetFlag = 1;
  305. break;
  306. }
  307. if (g_scsi_dma_state == SCSIDMA_WRITE_DONE)
  308. {
  309. // DMA done, wait for PIO to finish also and reconfig GPIO.
  310. scsi_accel_rp2040_stopWrite(resetFlag);
  311. }
  312. }
  313. }
  314. void scsi_accel_rp2040_read(uint8_t *buf, uint32_t count, int *parityError, volatile int *resetFlag)
  315. {
  316. // The hardware would support DMA for reading from SCSI bus also, but currently
  317. // the rest of the software architecture does not. There is not much benefit
  318. // because there isn't much else to do before we get the data from the SCSI bus.
  319. //
  320. // Currently this method just reads from the PIO RX fifo directly in software loop.
  321. g_scsi_dma_state = SCSIDMA_READ;
  322. pio_sm_init(SCSI_DMA_PIO, SCSI_DMA_SM, g_scsi_dma.pio_offset_async_read, &g_scsi_dma.pio_cfg_async_read);
  323. scsidma_config_gpio();
  324. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_DMA_SM, true);
  325. // Set the number of bytes to read, must be divisible by 2.
  326. assert((count & 1) == 0);
  327. pio_sm_put(SCSI_DMA_PIO, SCSI_DMA_SM, count - 1);
  328. // Read results from PIO RX FIFO
  329. uint8_t *dst = buf;
  330. uint8_t *end = buf + count;
  331. uint32_t paritycheck = 0;
  332. while (dst < end)
  333. {
  334. if (*resetFlag)
  335. {
  336. break;
  337. }
  338. uint32_t available = pio_sm_get_rx_fifo_level(SCSI_DMA_PIO, SCSI_DMA_SM);
  339. while (available > 0)
  340. {
  341. available--;
  342. uint32_t word = pio_sm_get(SCSI_DMA_PIO, SCSI_DMA_SM);
  343. paritycheck ^= word;
  344. word = ~word;
  345. *dst++ = word & 0xFF;
  346. *dst++ = word >> 16;
  347. }
  348. }
  349. // Check parity errors in whole block
  350. // This doesn't detect if there is even number of parity errors in block.
  351. uint8_t byte0 = ~(paritycheck & 0xFF);
  352. uint8_t byte1 = ~(paritycheck >> 16);
  353. if (paritycheck != ((g_scsi_parity_lookup[byte1] << 16) | g_scsi_parity_lookup[byte0]))
  354. {
  355. azlog("Parity error in scsi_accel_rp2040_read(): ", paritycheck);
  356. *parityError = 1;
  357. }
  358. g_scsi_dma_state = SCSIDMA_IDLE;
  359. SCSI_RELEASE_DATA_REQ();
  360. scsidma_config_gpio();
  361. pio_sm_set_enabled(SCSI_DMA_PIO, SCSI_DMA_SM, false);
  362. }
  363. void scsi_accel_rp2040_init()
  364. {
  365. g_scsi_dma_state = SCSIDMA_IDLE;
  366. scsidma_config_gpio();
  367. // Mark channels as being in use, unless it has been done already
  368. if (!g_channels_claimed)
  369. {
  370. pio_sm_claim(SCSI_DMA_PIO, SCSI_DMA_SM);
  371. dma_channel_claim(SCSI_DMA_CH);
  372. g_channels_claimed = true;
  373. }
  374. // Load PIO programs
  375. pio_clear_instruction_memory(SCSI_DMA_PIO);
  376. // Asynchronous SCSI write
  377. g_scsi_dma.pio_offset_async_write = pio_add_program(SCSI_DMA_PIO, &scsi_accel_async_write_program);
  378. g_scsi_dma.pio_cfg_async_write = scsi_accel_async_write_program_get_default_config(g_scsi_dma.pio_offset_async_write);
  379. sm_config_set_out_pins(&g_scsi_dma.pio_cfg_async_write, SCSI_IO_DB0, 9);
  380. sm_config_set_sideset_pins(&g_scsi_dma.pio_cfg_async_write, SCSI_OUT_REQ);
  381. sm_config_set_fifo_join(&g_scsi_dma.pio_cfg_async_write, PIO_FIFO_JOIN_TX);
  382. sm_config_set_out_shift(&g_scsi_dma.pio_cfg_async_write, true, false, 32);
  383. // Asynchronous SCSI read
  384. g_scsi_dma.pio_offset_async_read = pio_add_program(SCSI_DMA_PIO, &scsi_accel_async_read_program);
  385. g_scsi_dma.pio_cfg_async_read = scsi_accel_async_read_program_get_default_config(g_scsi_dma.pio_offset_async_read);
  386. sm_config_set_in_pins(&g_scsi_dma.pio_cfg_async_read, SCSI_IO_DB0);
  387. sm_config_set_sideset_pins(&g_scsi_dma.pio_cfg_async_read, SCSI_OUT_REQ);
  388. sm_config_set_out_shift(&g_scsi_dma.pio_cfg_async_write, true, false, 32);
  389. sm_config_set_in_shift(&g_scsi_dma.pio_cfg_async_read, true, true, 32);
  390. // Create DMA channel configuration so it can be applied quickly later
  391. dma_channel_config cfg = dma_channel_get_default_config(SCSI_DMA_CH);
  392. channel_config_set_transfer_data_size(&cfg, DMA_SIZE_32);
  393. channel_config_set_read_increment(&cfg, true);
  394. channel_config_set_write_increment(&cfg, false);
  395. channel_config_set_dreq(&cfg, pio_get_dreq(SCSI_DMA_PIO, SCSI_DMA_SM, true));
  396. g_scsi_dma.dma_write_config = cfg;
  397. }