scsi_accel_target_RP2MCU.pio 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. ; ZuluSCSI™ - Copyright (c) 2022-2025 Rabbit Hole Computing™
  2. ;
  3. ; ZuluSCSI™ firmware is licensed under the GPL version 3 or any later version. 
  4. ;
  5. ; https://www.gnu.org/licenses/gpl-3.0.html
  6. ; ----
  7. ; This program is free software: you can redistribute it and/or modify
  8. ; it under the terms of the GNU General Public License as published by
  9. ; the Free Software Foundation, either version 3 of the License, or
  10. ; (at your option) any later version. 
  11. ;
  12. ; This program is distributed in the hope that it will be useful,
  13. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. ; GNU General Public License for more details. 
  16. ;
  17. ; You should have received a copy of the GNU General Public License
  18. ; along with this program.  If not, see <https://www.gnu.org/licenses/>.
  19. ; RP2040 PIO program for accelerating SCSI communication
  20. ; Run "pioasm scsi_accel.pio scsi_accel.pio.h" to regenerate the C header from this.
  21. ; GPIO mapping:
  22. ; - 0-7: DB0-DB7
  23. ; - 8: DBP
  24. ; Side set is REQ pin
  25. ; ACK is a dummy value, Will be rewritten on initialization
  26. .define ACK 10
  27. ; Delay from data setup to REQ assertion.
  28. ; deskew delay + cable skew delay = 55 ns minimum
  29. ; One clock cycle is x ns => delay (55 / x) clocks
  30. ; REQ_DLY is a dummy value, will be rewritten
  31. .define REQ_DLY 7
  32. ; Adds parity to data that is to be written to SCSI
  33. ; This works by generating addresses for DMA to fetch data from.
  34. ; Register X should be initialized to the base address of the lookup table.
  35. .program scsi_parity
  36. pull block
  37. in NULL, 1
  38. in OSR, 8
  39. in X, 23
  40. ; Write to SCSI bus using asynchronous handshake.
  41. ; Data is written as 32-bit words that contain the 8 data bits + 1 parity bit.
  42. ; 23 bits in each word are discarded.
  43. ; Number of bytes to send must be multiple of 2.
  44. .program scsi_accel_async_write
  45. .side_set 1
  46. pull ifempty block side 1 ; Get data from TX FIFO
  47. out pins, 9 side 1 ; Write data and parity bit
  48. out null, 23 side 1 [0] ;[REQ_DLY-2] ; Discard unused bits, wait for data preset time
  49. wait 1 gpio ACK side 1 ; Wait for ACK to be inactive
  50. wait 0 gpio ACK side 0 ; Assert REQ, wait for ACK low
  51. ; Read from SCSI bus using sync or async handshake.
  52. ; Data is returned as 32-bit words:
  53. ; - bit 0: always zero
  54. ; - bits 1-8: data byte
  55. ; - bit 9: parity bit
  56. ; - bits 10-31: lookup table address
  57. ; Lookup table address should be loaded into register Y.
  58. ; One dummy word should be written to TX fifo for every byte to receive.
  59. .program scsi_accel_read
  60. .side_set 1
  61. pull block side 1 ; Pull from TX fifo for counting bytes and pacing sync mode
  62. wait 1 gpio ACK side 1 ; Wait for ACK high
  63. in null, 1 side 0 ; Zero bit because lookup table entries are 16-bit
  64. wait 0 gpio ACK side 0 ; Assert REQ, wait for ACK low
  65. in pins, 9 side 1 ; Deassert REQ, read GPIO
  66. in y, 22 side 1 ; Copy parity lookup table address
  67. ; Data pacing state machine for synchronous writes.
  68. ; Takes one bit from ISR on every falling edge of ACK.
  69. ; The C code should set autopull threshold to match scsi_sync_write autopush threshold.
  70. ; System DMA will then move words from scsi_sync_write RX fifo to scsi_sync_write_pacer TX fifo.
  71. .program scsi_sync_write_pacer
  72. wait 1 gpio ACK
  73. wait 0 gpio ACK ; Wait for falling edge on ACK
  74. out null, 1 ; Let scsi_sync_write send one more byte
  75. ; Data pacing state machine for synchronous reads.
  76. ; The delay times will be rewritten by C code to match the negotiated SCSI sync speed.
  77. ; Number of bytes to receive minus one should be loaded into register X.
  78. ; In synchronous mode this generates the REQ pulses and dummy words.
  79. ; In asynchronous mode it just generates dummy words to feed to scsi_accel_read.
  80. .program scsi_sync_read_pacer
  81. .side_set 1
  82. start:
  83. push block [0] side 1 ; Send dummy word to scsi_accel_read, wait for transfer period
  84. jmp x-- start [0] side 0 ; Assert REQ, wait for assert time
  85. finish:
  86. jmp finish [0] side 1
  87. ; Parity checker for reads from SCSI bus.
  88. ; Receives 16-bit words from g_scsi_parity_check_lookup
  89. ; Bottom 8 bits are the data byte, which is passed to output FIFO
  90. ; The 9th bit is parity valid bit, which is 1 for valid and 0 for parity error.
  91. .program scsi_read_parity
  92. parity_valid:
  93. out isr, 8 ; Take the 8 data bits for passing to RX fifo
  94. push block ; Push the data to RX fifo
  95. out x, 24 ; Take the parity valid bit, and the rest of 32-bit word
  96. jmp x-- parity_valid ; If parity valid bit is 1, repeat from start
  97. irq set 0 ; Parity error, set interrupt flag
  98. ; Data state machine for synchronous writes.
  99. ; Takes the lowest 9 bits of each 32 bit word and writes them to bus with REQ pulse.
  100. ; The delay times will be rewritten by C code to match the negotiated SCSI sync speed.
  101. ;
  102. ; Shifts one bit to ISR per every byte transmitted. This is used to control the transfer
  103. ; pace, the RX fifo acts as a counter to keep track of unacknowledged bytes. The C code
  104. ; can set the syncOffset by changing autopush threshold, e.g. threshold 3 = 12 bytes offset.
  105. .program scsi_sync_write
  106. .side_set 1
  107. out pins, 9 [0] side 1 ; Write data and parity bit, wait for deskew delay
  108. out null, 23 [0] side 0 ; Assert REQ, wait for assert time
  109. in null, 1 [0] side 1 ; Deassert REQ, wait for transfer period, wait for space in ACK buffer