scsi_accel_target_Pico_2.pio 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. ; ZuluSCSI™ - Copyright (c) 2022 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. .define REQ 17
  26. .define ACK 26
  27. ; Delay from data setup to REQ assertion.
  28. ; deskew delay + cable skew delay = 55 ns minimum
  29. ; One clock cycle is 6.67 ns (150MHz) => delay 8.25 clocks or 9 clocks
  30. .define REQ_DLY 9
  31. ; Adds parity to data that is to be written to SCSI
  32. ; This works by generating addresses for DMA to fetch data from.
  33. ; Register X should be initialized to the base address of the lookup table.
  34. .program scsi_parity
  35. pull block
  36. in NULL, 1
  37. in OSR, 8
  38. in X, 23
  39. ; Write to SCSI bus using asynchronous handshake.
  40. ; Data is written as 32-bit words that contain the 8 data bits + 1 parity bit.
  41. ; 23 bits in each word are discarded.
  42. ; Number of bytes to send must be multiple of 2.
  43. .program scsi_accel_async_write
  44. .side_set 1
  45. pull ifempty block side 1 ; Get data from TX FIFO
  46. out pins, 9 side 1 ; Write data and parity bit
  47. out null, 23 [REQ_DLY-2] side 1 ; Discard unused bits, wait for data preset time
  48. wait 1 gpio ACK side 1 ; Wait for ACK to be inactive
  49. wait 0 gpio ACK side 0 ; Assert REQ, wait for ACK low
  50. ; Read from SCSI bus using sync or async handshake.
  51. ; Data is returned as 32-bit words:
  52. ; - bit 0: always zero
  53. ; - bits 1-8: data byte
  54. ; - bit 9: parity bit
  55. ; - bits 10-31: lookup table address
  56. ; Lookup table address should be loaded into register Y.
  57. ; One dummy word should be written to TX fifo for every byte to receive.
  58. .program scsi_accel_read
  59. .side_set 1
  60. pull block side 1 ; Pull from TX fifo for counting bytes and pacing sync mode
  61. wait 1 gpio ACK side 1 ; Wait for ACK high
  62. in null, 1 side 0 ; Zero bit because lookup table entries are 16-bit
  63. wait 0 gpio ACK side 0 ; Assert REQ, wait for ACK low
  64. in pins, 9 side 1 ; Deassert REQ, read GPIO
  65. in y, 22 side 1 ; Copy parity lookup table address
  66. ; Data state machine for synchronous writes.
  67. ; Takes the lowest 9 bits of each 32 bit word and writes them to bus with REQ pulse.
  68. ; The delay times will be rewritten by C code to match the negotiated SCSI sync speed.
  69. ;
  70. ; Shifts one bit to ISR per every byte transmitted. This is used to control the transfer
  71. ; pace, the RX fifo acts as a counter to keep track of unacknowledged bytes. The C code
  72. ; can set the syncOffset by changing autopush threshold, e.g. threshold 3 = 12 bytes offset.
  73. .program scsi_sync_write
  74. .side_set 1
  75. out pins, 9 [0] side 1 ; Write data and parity bit, wait for deskew delay
  76. out null, 23 [0] side 0 ; Assert REQ, wait for assert time
  77. in null, 1 [0] side 1 ; Deassert REQ, wait for transfer period, wait for space in ACK buffer
  78. ; Data pacing state machine for synchronous writes.
  79. ; Takes one bit from ISR on every falling edge of ACK.
  80. ; The C code should set autopull threshold to match scsi_sync_write autopush threshold.
  81. ; System DMA will then move words from scsi_sync_write RX fifo to scsi_sync_write_pacer TX fifo.
  82. .program scsi_sync_write_pacer
  83. wait 1 gpio ACK
  84. wait 0 gpio ACK ; Wait for falling edge on ACK
  85. out null, 1 ; Let scsi_sync_write send one more byte
  86. ; Data pacing state machine for synchronous reads.
  87. ; The delay times will be rewritten by C code to match the negotiated SCSI sync speed.
  88. ; Number of bytes to receive minus one should be loaded into register X.
  89. ; In synchronous mode this generates the REQ pulses and dummy words.
  90. ; In asynchronous mode it just generates dummy words to feed to scsi_accel_read.
  91. .program scsi_sync_read_pacer
  92. .side_set 1
  93. start:
  94. push block [0] side 1 ; Send dummy word to scsi_accel_read, wait for transfer period
  95. jmp x-- start [0] side 0 ; Assert REQ, wait for assert time
  96. finish:
  97. jmp finish [0] side 1
  98. ; Parity checker for reads from SCSI bus.
  99. ; Receives 16-bit words from g_scsi_parity_check_lookup
  100. ; Bottom 8 bits are the data byte, which is passed to output FIFO
  101. ; The 9th bit is parity valid bit, which is 1 for valid and 0 for parity error.
  102. .program scsi_read_parity
  103. parity_valid:
  104. out isr, 8 ; Take the 8 data bits for passing to RX fifo
  105. push block ; Push the data to RX fifo
  106. out x, 24 ; Take the parity valid bit, and the rest of 32-bit word
  107. jmp x-- parity_valid ; If parity valid bit is 1, repeat from start
  108. irq set 0 ; Parity error, set interrupt flag