scsi_accel_host.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2022-2025 Rabbit Hole Computing™
  3. *
  4. * ZuluSCSI™ firmware is licensed under the GPL version 3 or any later version. 
  5. *
  6. * https://www.gnu.org/licenses/gpl-3.0.html
  7. * ----
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version. 
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. * GNU General Public License for more details. 
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  20. **/
  21. // Accelerated SCSI subroutines for SCSI initiator/host side communication
  22. #include "scsi_accel_host.h"
  23. #include "ZuluSCSI_platform.h"
  24. #include "ZuluSCSI_log.h"
  25. #include <hardware/pio.h>
  26. #include <hardware/dma.h>
  27. #include <hardware/irq.h>
  28. #include <hardware/structs/iobank0.h>
  29. #include <hardware/sync.h>
  30. #ifdef PLATFORM_HAS_INITIATOR_MODE
  31. # include "scsi_accel_host_RP2MCU.pio.h"
  32. #define SCSI_PIO pio0
  33. #define SCSI_SM 0
  34. static struct {
  35. // PIO configurations
  36. uint32_t pio_offset_async_read;
  37. pio_sm_config pio_cfg_async_read;
  38. } g_scsi_host;
  39. enum scsidma_state_t { SCSIHOST_IDLE = 0,
  40. SCSIHOST_READ };
  41. static volatile scsidma_state_t g_scsi_host_state;
  42. static void scsi_accel_host_config_gpio()
  43. {
  44. if (g_scsi_host_state == SCSIHOST_IDLE)
  45. {
  46. iobank0_hw->io[SCSI_IO_DB0].ctrl = GPIO_FUNC_SIO;
  47. iobank0_hw->io[SCSI_IO_DB1].ctrl = GPIO_FUNC_SIO;
  48. iobank0_hw->io[SCSI_IO_DB2].ctrl = GPIO_FUNC_SIO;
  49. iobank0_hw->io[SCSI_IO_DB3].ctrl = GPIO_FUNC_SIO;
  50. iobank0_hw->io[SCSI_IO_DB4].ctrl = GPIO_FUNC_SIO;
  51. iobank0_hw->io[SCSI_IO_DB5].ctrl = GPIO_FUNC_SIO;
  52. iobank0_hw->io[SCSI_IO_DB6].ctrl = GPIO_FUNC_SIO;
  53. iobank0_hw->io[SCSI_IO_DB7].ctrl = GPIO_FUNC_SIO;
  54. iobank0_hw->io[SCSI_IO_DBP].ctrl = GPIO_FUNC_SIO;
  55. iobank0_hw->io[SCSI_IN_REQ].ctrl = GPIO_FUNC_SIO;
  56. iobank0_hw->io[SCSI_OUT_ACK].ctrl = GPIO_FUNC_SIO;
  57. }
  58. else if (g_scsi_host_state == SCSIHOST_READ)
  59. {
  60. // Data bus and REQ as input, ACK pin as output
  61. pio_sm_set_pins(SCSI_PIO, SCSI_SM, SCSI_IO_DATA_MASK | 1 << SCSI_IN_REQ | 1 << SCSI_OUT_ACK);
  62. pio_sm_set_consecutive_pindirs(SCSI_PIO, SCSI_SM, SCSI_IO_DB0, 9, false);
  63. pio_sm_set_consecutive_pindirs(SCSI_PIO, SCSI_SM, SCSI_IN_REQ, 1, false);
  64. pio_sm_set_consecutive_pindirs(SCSI_PIO, SCSI_SM, SCSI_OUT_ACK, 1, true);
  65. iobank0_hw->io[SCSI_IO_DB0].ctrl = GPIO_FUNC_SIO;
  66. iobank0_hw->io[SCSI_IO_DB1].ctrl = GPIO_FUNC_SIO;
  67. iobank0_hw->io[SCSI_IO_DB2].ctrl = GPIO_FUNC_SIO;
  68. iobank0_hw->io[SCSI_IO_DB3].ctrl = GPIO_FUNC_SIO;
  69. iobank0_hw->io[SCSI_IO_DB4].ctrl = GPIO_FUNC_SIO;
  70. iobank0_hw->io[SCSI_IO_DB5].ctrl = GPIO_FUNC_SIO;
  71. iobank0_hw->io[SCSI_IO_DB6].ctrl = GPIO_FUNC_SIO;
  72. iobank0_hw->io[SCSI_IO_DB7].ctrl = GPIO_FUNC_SIO;
  73. iobank0_hw->io[SCSI_IO_DBP].ctrl = GPIO_FUNC_SIO;
  74. iobank0_hw->io[SCSI_IN_REQ].ctrl = GPIO_FUNC_SIO;
  75. iobank0_hw->io[SCSI_OUT_ACK].ctrl = GPIO_FUNC_PIO0;
  76. }
  77. }
  78. uint32_t scsi_accel_host_read(uint8_t *buf, uint32_t count, int *parityError, volatile int *resetFlag)
  79. {
  80. // Currently this method just reads from the PIO RX fifo directly in software loop.
  81. // The SD card access is parallelized using DMA, so there is limited benefit from using DMA here.
  82. g_scsi_host_state = SCSIHOST_READ;
  83. int cd_start = SCSI_IN(CD);
  84. int msg_start = SCSI_IN(MSG);
  85. pio_sm_init(SCSI_PIO, SCSI_SM, g_scsi_host.pio_offset_async_read, &g_scsi_host.pio_cfg_async_read);
  86. scsi_accel_host_config_gpio();
  87. pio_sm_set_enabled(SCSI_PIO, SCSI_SM, true);
  88. // Set the number of bytes to read, must be divisible by 2.
  89. assert((count & 1) == 0);
  90. pio_sm_put(SCSI_PIO, SCSI_SM, count - 1);
  91. // Read results from PIO RX FIFO
  92. uint8_t *dst = buf;
  93. uint8_t *end = buf + count;
  94. uint32_t paritycheck = 0;
  95. uint32_t prev_rx_time = millis();
  96. while (dst < end)
  97. {
  98. uint32_t available = pio_sm_get_rx_fifo_level(SCSI_PIO, SCSI_SM);
  99. if (available == 0)
  100. {
  101. // No new data has been received by PIO, check if there is a need to abort
  102. bool abort = false;
  103. if (*resetFlag)
  104. {
  105. dbgmsg("scsi_accel_host_read: Aborting due to reset request");
  106. abort = true;
  107. }
  108. else if ((millis() - prev_rx_time) > 10000)
  109. {
  110. dbgmsg("scsi_accel_host_read: Aborting due to timeout");
  111. abort = true;
  112. }
  113. else
  114. {
  115. // Some drives such as ST-296N may have glitches on phase signals in between
  116. // byte transfers. This is allowed by SCSI spec, and officially we should only
  117. // check the phase signals when REQ is active. However the PIO logic currently
  118. // does not do this. Instead, when we detect a phase change, wait for 10 milliseconds
  119. // to see if it is real.
  120. int debounce = 100;
  121. while (debounce > 0 && (!SCSI_IN(IO) || SCSI_IN(CD) != cd_start || SCSI_IN(MSG) != msg_start))
  122. {
  123. debounce--;
  124. delayMicroseconds(100);
  125. }
  126. if (debounce == 0)
  127. {
  128. dbgmsg("scsi_accel_host_read: aborting because target switched transfer phase (IO: ",
  129. (int)SCSI_IN(IO), ", CD: ", (int)SCSI_IN(CD), ", MSG: ", (int)SCSI_IN(MSG), ")");
  130. abort = true;
  131. }
  132. }
  133. if (abort)
  134. {
  135. count = dst - buf;
  136. break;
  137. }
  138. }
  139. while (available > 0)
  140. {
  141. available--;
  142. uint32_t word = pio_sm_get(SCSI_PIO, SCSI_SM);
  143. paritycheck ^= word;
  144. word = ~word;
  145. *dst++ = word & 0xFF;
  146. *dst++ = word >> 16;
  147. }
  148. }
  149. // Check parity errors in whole block
  150. // This doesn't detect if there is even number of parity errors in block.
  151. uint8_t byte0 = ~(paritycheck & 0xFF);
  152. uint8_t byte1 = ~(paritycheck >> 16);
  153. if (paritycheck != ((g_scsi_parity_lookup[byte1] << 16) | g_scsi_parity_lookup[byte0]))
  154. {
  155. logmsg("Parity error in scsi_accel_host_read(): ", paritycheck);
  156. *parityError = 1;
  157. }
  158. g_scsi_host_state = SCSIHOST_IDLE;
  159. SCSI_RELEASE_DATA_REQ();
  160. scsi_accel_host_config_gpio();
  161. pio_sm_set_enabled(SCSI_PIO, SCSI_SM, false);
  162. return count;
  163. }
  164. void scsi_accel_host_init()
  165. {
  166. g_scsi_host_state = SCSIHOST_IDLE;
  167. scsi_accel_host_config_gpio();
  168. // Load PIO programs
  169. pio_clear_instruction_memory(SCSI_PIO);
  170. // Asynchronous / synchronous SCSI read
  171. g_scsi_host.pio_offset_async_read = pio_add_program(SCSI_PIO, &scsi_host_async_read_program);
  172. // wait 0 gpio REQ side 1 ; Wait for REQ low
  173. uint16_t instr = pio_encode_wait_gpio(false, SCSI_IN_REQ) | pio_encode_sideset(1, 1);
  174. SCSI_PIO->instr_mem[g_scsi_host.pio_offset_async_read + 2] = instr;
  175. instr = pio_encode_wait_gpio(true, SCSI_IN_REQ) | pio_encode_sideset(1, 0);
  176. SCSI_PIO->instr_mem[g_scsi_host.pio_offset_async_read + 5] = instr;
  177. g_scsi_host.pio_cfg_async_read = scsi_host_async_read_program_get_default_config(g_scsi_host.pio_offset_async_read);
  178. sm_config_set_in_pins(&g_scsi_host.pio_cfg_async_read, SCSI_IO_DB0);
  179. sm_config_set_sideset_pins(&g_scsi_host.pio_cfg_async_read, SCSI_OUT_ACK);
  180. sm_config_set_out_shift(&g_scsi_host.pio_cfg_async_read, true, false, 32);
  181. sm_config_set_in_shift(&g_scsi_host.pio_cfg_async_read, true, true, 32);
  182. }
  183. #endif // PLATFORM_HAS_INITIATOR_MODE