scsi_accel_host.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Accelerated SCSI subroutines for SCSI initiator/host side communication
  2. // Copyright (c) 2022 Rabbit Hole Computing™
  3. // Copyright (c) 2024 Tech by Androda, LLC
  4. #include "scsi_accel_host.h"
  5. #include "BlueSCSI_platform.h"
  6. #include "BlueSCSI_log.h"
  7. #include "scsi_accel_host.pio.h"
  8. #include <hardware/pio.h>
  9. #include <hardware/dma.h>
  10. #include <hardware/irq.h>
  11. #include <hardware/structs/iobank0.h>
  12. #include <hardware/sync.h>
  13. #define SCSI_PIO pio0
  14. #define SCSI_SM 1
  15. static struct {
  16. // PIO configurations
  17. uint32_t pio_offset_async_read;
  18. pio_sm_config pio_cfg_async_read;
  19. } g_scsi_host;
  20. enum scsidma_state_t { SCSIHOST_IDLE = 0,
  21. SCSIHOST_READ };
  22. static volatile scsidma_state_t g_scsi_host_state;
  23. static void scsi_accel_host_config_gpio()
  24. {
  25. if (g_scsi_host_state == SCSIHOST_IDLE)
  26. {
  27. iobank0_hw->io[SCSI_IO_DB0].ctrl = GPIO_FUNC_SIO;
  28. iobank0_hw->io[SCSI_IO_DB1].ctrl = GPIO_FUNC_SIO;
  29. iobank0_hw->io[SCSI_IO_DB2].ctrl = GPIO_FUNC_SIO;
  30. iobank0_hw->io[SCSI_IO_DB3].ctrl = GPIO_FUNC_SIO;
  31. iobank0_hw->io[SCSI_IO_DB4].ctrl = GPIO_FUNC_SIO;
  32. iobank0_hw->io[SCSI_IO_DB5].ctrl = GPIO_FUNC_SIO;
  33. iobank0_hw->io[SCSI_IO_DB6].ctrl = GPIO_FUNC_SIO;
  34. iobank0_hw->io[SCSI_IO_DB7].ctrl = GPIO_FUNC_SIO;
  35. iobank0_hw->io[SCSI_IO_DBP].ctrl = GPIO_FUNC_SIO;
  36. iobank0_hw->io[SCSI_IN_REQ].ctrl = GPIO_FUNC_SIO;
  37. iobank0_hw->io[SCSI_OUT_ACK].ctrl = GPIO_FUNC_SIO;
  38. }
  39. else if (g_scsi_host_state == SCSIHOST_READ)
  40. {
  41. // Data bus and REQ as input, ACK pin as output
  42. // 100000010000000000111111111
  43. // ACK REQ PDB
  44. //
  45. pio_sm_set_pins(SCSI_PIO, SCSI_SM, 0x40801FF);
  46. pio_sm_set_consecutive_pindirs(SCSI_PIO, SCSI_SM, 0, 9, false); // DBP Input
  47. pio_sm_set_consecutive_pindirs(SCSI_PIO, SCSI_SM, SCSI_IN_REQ, 1, false); // REQ Input
  48. pio_sm_set_consecutive_pindirs(SCSI_PIO, SCSI_SM, SCSI_OUT_ACK, 1, true); // ACK Output
  49. iobank0_hw->io[SCSI_IO_DB0].ctrl = GPIO_FUNC_SIO;
  50. iobank0_hw->io[SCSI_IO_DB1].ctrl = GPIO_FUNC_SIO;
  51. iobank0_hw->io[SCSI_IO_DB2].ctrl = GPIO_FUNC_SIO;
  52. iobank0_hw->io[SCSI_IO_DB3].ctrl = GPIO_FUNC_SIO;
  53. iobank0_hw->io[SCSI_IO_DB4].ctrl = GPIO_FUNC_SIO;
  54. iobank0_hw->io[SCSI_IO_DB5].ctrl = GPIO_FUNC_SIO;
  55. iobank0_hw->io[SCSI_IO_DB6].ctrl = GPIO_FUNC_SIO;
  56. iobank0_hw->io[SCSI_IO_DB7].ctrl = GPIO_FUNC_SIO;
  57. iobank0_hw->io[SCSI_IO_DBP].ctrl = GPIO_FUNC_SIO;
  58. //iobank0_hw->io[SCSI_IN_REQ].ctrl = GPIO_FUNC_PIO0;
  59. iobank0_hw->io[SCSI_OUT_ACK].ctrl = GPIO_FUNC_PIO0;
  60. }
  61. }
  62. uint32_t scsi_accel_host_read(uint8_t *buf, uint32_t count, int *parityError, uint32_t *parityResult, volatile int *resetFlag)
  63. {
  64. // Currently this method just reads from the PIO RX fifo directly in software loop.
  65. // The SD card access is parallelized using DMA, so there is limited benefit from using DMA here.
  66. g_scsi_host_state = SCSIHOST_READ;
  67. int cd_start = SCSI_IN(CD);
  68. int msg_start = SCSI_IN(MSG);
  69. pio_sm_init(SCSI_PIO, SCSI_SM, g_scsi_host.pio_offset_async_read, &g_scsi_host.pio_cfg_async_read);
  70. scsi_accel_host_config_gpio();
  71. pio_sm_set_enabled(SCSI_PIO, SCSI_SM, true);
  72. // Set the number of bytes to read, must be divisible by 2.
  73. assert((count & 1) == 0);
  74. pio_sm_put(SCSI_PIO, SCSI_SM, count - 1);
  75. // Read results from PIO RX FIFO
  76. uint8_t *dst = buf;
  77. uint8_t *end = buf + count;
  78. uint32_t paritycheck = 0;
  79. while (dst < end)
  80. {
  81. uint32_t available = pio_sm_get_rx_fifo_level(SCSI_PIO, SCSI_SM);
  82. if (available == 0)
  83. {
  84. if (*resetFlag || !SCSI_IN(IO) || SCSI_IN(CD) != cd_start || SCSI_IN(MSG) != msg_start)
  85. {
  86. // Target switched out of DATA_IN mode
  87. count = dst - buf;
  88. break;
  89. }
  90. }
  91. while (available > 0)
  92. {
  93. available--;
  94. uint32_t word = pio_sm_get(SCSI_PIO, SCSI_SM);
  95. paritycheck ^= word;
  96. word = ~word;
  97. *dst++ = word & 0xFF;
  98. *dst++ = word >> 16;
  99. }
  100. }
  101. // Check parity errors in whole block
  102. // This doesn't detect if there is even number of parity errors in block.
  103. uint8_t byte0 = ~(paritycheck & 0xFF);
  104. uint8_t byte1 = ~(paritycheck >> 16);
  105. if (paritycheck != ((g_scsi_parity_lookup[byte1] << 16) | g_scsi_parity_lookup[byte0]))
  106. {
  107. *parityError = 1;
  108. *parityResult = paritycheck;
  109. }
  110. g_scsi_host_state = SCSIHOST_IDLE;
  111. SCSI_RELEASE_DATA_REQ();
  112. scsi_accel_host_config_gpio();
  113. pio_sm_set_enabled(SCSI_PIO, SCSI_SM, false);
  114. return count;
  115. }
  116. void scsi_accel_host_init()
  117. {
  118. g_scsi_host_state = SCSIHOST_IDLE;
  119. scsi_accel_host_config_gpio();
  120. // Asynchronous / synchronous SCSI read
  121. g_scsi_host.pio_offset_async_read = pio_add_program(SCSI_PIO, &scsi_host_async_read_program);
  122. g_scsi_host.pio_cfg_async_read = scsi_host_async_read_program_get_default_config(g_scsi_host.pio_offset_async_read);
  123. sm_config_set_in_pins(&g_scsi_host.pio_cfg_async_read, SCSI_IO_DB0);
  124. sm_config_set_sideset_pins(&g_scsi_host.pio_cfg_async_read, SCSI_OUT_ACK);
  125. sm_config_set_out_shift(&g_scsi_host.pio_cfg_async_read, true, false, 32);
  126. sm_config_set_in_shift(&g_scsi_host.pio_cfg_async_read, true, true, 32);
  127. }