scsi_accel_host.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2022 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. # ifdef ZULUSCSI_PICO_2
  32. # include "scsi_accel_host_Pico_2.pio.h"
  33. # elif defined(ZULUSCSI_PICO)
  34. # include "scsi_accel_host_Pico.pio.h"
  35. # elif defined(ZULUSCSI_RP2350A)
  36. # include "scsi_accel_host_RP2350A.pio.h"
  37. # else
  38. # include "scsi_accel_host_RP2040.pio.h"
  39. # endif
  40. #define SCSI_PIO pio0
  41. #define SCSI_SM 0
  42. static struct {
  43. // PIO configurations
  44. uint32_t pio_offset_async_read;
  45. pio_sm_config pio_cfg_async_read;
  46. } g_scsi_host;
  47. enum scsidma_state_t { SCSIHOST_IDLE = 0,
  48. SCSIHOST_READ };
  49. static volatile scsidma_state_t g_scsi_host_state;
  50. static void scsi_accel_host_config_gpio()
  51. {
  52. if (g_scsi_host_state == SCSIHOST_IDLE)
  53. {
  54. iobank0_hw->io[SCSI_IO_DB0].ctrl = GPIO_FUNC_SIO;
  55. iobank0_hw->io[SCSI_IO_DB1].ctrl = GPIO_FUNC_SIO;
  56. iobank0_hw->io[SCSI_IO_DB2].ctrl = GPIO_FUNC_SIO;
  57. iobank0_hw->io[SCSI_IO_DB3].ctrl = GPIO_FUNC_SIO;
  58. iobank0_hw->io[SCSI_IO_DB4].ctrl = GPIO_FUNC_SIO;
  59. iobank0_hw->io[SCSI_IO_DB5].ctrl = GPIO_FUNC_SIO;
  60. iobank0_hw->io[SCSI_IO_DB6].ctrl = GPIO_FUNC_SIO;
  61. iobank0_hw->io[SCSI_IO_DB7].ctrl = GPIO_FUNC_SIO;
  62. iobank0_hw->io[SCSI_IO_DBP].ctrl = GPIO_FUNC_SIO;
  63. iobank0_hw->io[SCSI_IN_REQ].ctrl = GPIO_FUNC_SIO;
  64. iobank0_hw->io[SCSI_OUT_ACK].ctrl = GPIO_FUNC_SIO;
  65. }
  66. else if (g_scsi_host_state == SCSIHOST_READ)
  67. {
  68. // Data bus and REQ as input, ACK pin as output
  69. pio_sm_set_pins(SCSI_PIO, SCSI_SM, SCSI_IO_DATA_MASK | 1 << SCSI_IN_REQ | 1 << SCSI_OUT_ACK);
  70. pio_sm_set_consecutive_pindirs(SCSI_PIO, SCSI_SM, SCSI_IO_DB0, 9, false);
  71. pio_sm_set_consecutive_pindirs(SCSI_PIO, SCSI_SM, SCSI_IN_REQ, 1, false);
  72. pio_sm_set_consecutive_pindirs(SCSI_PIO, SCSI_SM, SCSI_OUT_ACK, 1, true);
  73. iobank0_hw->io[SCSI_IO_DB0].ctrl = GPIO_FUNC_SIO;
  74. iobank0_hw->io[SCSI_IO_DB1].ctrl = GPIO_FUNC_SIO;
  75. iobank0_hw->io[SCSI_IO_DB2].ctrl = GPIO_FUNC_SIO;
  76. iobank0_hw->io[SCSI_IO_DB3].ctrl = GPIO_FUNC_SIO;
  77. iobank0_hw->io[SCSI_IO_DB4].ctrl = GPIO_FUNC_SIO;
  78. iobank0_hw->io[SCSI_IO_DB5].ctrl = GPIO_FUNC_SIO;
  79. iobank0_hw->io[SCSI_IO_DB6].ctrl = GPIO_FUNC_SIO;
  80. iobank0_hw->io[SCSI_IO_DB7].ctrl = GPIO_FUNC_SIO;
  81. iobank0_hw->io[SCSI_IO_DBP].ctrl = GPIO_FUNC_SIO;
  82. iobank0_hw->io[SCSI_IN_REQ].ctrl = GPIO_FUNC_SIO;
  83. iobank0_hw->io[SCSI_OUT_ACK].ctrl = GPIO_FUNC_PIO0;
  84. }
  85. }
  86. uint32_t scsi_accel_host_read(uint8_t *buf, uint32_t count, int *parityError, volatile int *resetFlag)
  87. {
  88. // Currently this method just reads from the PIO RX fifo directly in software loop.
  89. // The SD card access is parallelized using DMA, so there is limited benefit from using DMA here.
  90. g_scsi_host_state = SCSIHOST_READ;
  91. int cd_start = SCSI_IN(CD);
  92. int msg_start = SCSI_IN(MSG);
  93. pio_sm_init(SCSI_PIO, SCSI_SM, g_scsi_host.pio_offset_async_read, &g_scsi_host.pio_cfg_async_read);
  94. scsi_accel_host_config_gpio();
  95. pio_sm_set_enabled(SCSI_PIO, SCSI_SM, true);
  96. // Set the number of bytes to read, must be divisible by 2.
  97. assert((count & 1) == 0);
  98. pio_sm_put(SCSI_PIO, SCSI_SM, count - 1);
  99. // Read results from PIO RX FIFO
  100. uint8_t *dst = buf;
  101. uint8_t *end = buf + count;
  102. uint32_t paritycheck = 0;
  103. while (dst < end)
  104. {
  105. uint32_t available = pio_sm_get_rx_fifo_level(SCSI_PIO, SCSI_SM);
  106. if (available == 0)
  107. {
  108. if (*resetFlag || !SCSI_IN(IO) || SCSI_IN(CD) != cd_start || SCSI_IN(MSG) != msg_start)
  109. {
  110. // Target switched out of DATA_IN mode
  111. count = dst - buf;
  112. break;
  113. }
  114. }
  115. while (available > 0)
  116. {
  117. available--;
  118. uint32_t word = pio_sm_get(SCSI_PIO, SCSI_SM);
  119. paritycheck ^= word;
  120. word = ~word;
  121. *dst++ = word & 0xFF;
  122. *dst++ = word >> 16;
  123. }
  124. }
  125. // Check parity errors in whole block
  126. // This doesn't detect if there is even number of parity errors in block.
  127. uint8_t byte0 = ~(paritycheck & 0xFF);
  128. uint8_t byte1 = ~(paritycheck >> 16);
  129. if (paritycheck != ((g_scsi_parity_lookup[byte1] << 16) | g_scsi_parity_lookup[byte0]))
  130. {
  131. logmsg("Parity error in scsi_accel_host_read(): ", paritycheck);
  132. *parityError = 1;
  133. }
  134. g_scsi_host_state = SCSIHOST_IDLE;
  135. SCSI_RELEASE_DATA_REQ();
  136. scsi_accel_host_config_gpio();
  137. pio_sm_set_enabled(SCSI_PIO, SCSI_SM, false);
  138. return count;
  139. }
  140. void scsi_accel_host_init()
  141. {
  142. g_scsi_host_state = SCSIHOST_IDLE;
  143. scsi_accel_host_config_gpio();
  144. // Load PIO programs
  145. pio_clear_instruction_memory(SCSI_PIO);
  146. // Asynchronous / synchronous SCSI read
  147. g_scsi_host.pio_offset_async_read = pio_add_program(SCSI_PIO, &scsi_host_async_read_program);
  148. g_scsi_host.pio_cfg_async_read = scsi_host_async_read_program_get_default_config(g_scsi_host.pio_offset_async_read);
  149. sm_config_set_in_pins(&g_scsi_host.pio_cfg_async_read, SCSI_IO_DB0);
  150. sm_config_set_sideset_pins(&g_scsi_host.pio_cfg_async_read, SCSI_OUT_ACK);
  151. sm_config_set_out_shift(&g_scsi_host.pio_cfg_async_read, true, false, 32);
  152. sm_config_set_in_shift(&g_scsi_host.pio_cfg_async_read, true, true, 32);
  153. }
  154. #endif // PLATFORM_HAS_INITIATOR_MODE