scsi_accel.pio 4.6 KB

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