scsi_accel.pio 4.7 KB

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