scsiPhy.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // Implements the low level interface to SCSI bus
  2. // Partially derived from scsiPhy.c from SCSI2SD-V6
  3. #include "scsiPhy.h"
  4. #include "AzulSCSI_platform.h"
  5. #include "scsi_accel_asm.h"
  6. #include "AzulSCSI_log.h"
  7. #include "AzulSCSI_log_trace.h"
  8. #include <scsi2sd.h>
  9. extern "C" {
  10. #include <scsi.h>
  11. #include <time.h>
  12. }
  13. static void init_irqs();
  14. /***********************/
  15. /* SCSI status signals */
  16. /***********************/
  17. extern "C" bool scsiStatusATN()
  18. {
  19. return SCSI_IN(ATN);
  20. }
  21. extern "C" bool scsiStatusBSY()
  22. {
  23. return SCSI_IN(BSY);
  24. }
  25. /************************/
  26. /* SCSI selection logic */
  27. /************************/
  28. volatile uint8_t g_scsi_sts_selection;
  29. volatile uint8_t g_scsi_ctrl_bsy;
  30. static void scsi_bsy_deassert_interrupt()
  31. {
  32. if (SCSI_IN(SEL) && !SCSI_IN(BSY))
  33. {
  34. uint8_t sel_bits = SCSI_IN_DATA();
  35. int sel_id = -1;
  36. for (int i = 0; i < S2S_MAX_TARGETS; i++)
  37. {
  38. if (scsiDev.targets[i].targetId <= 7 && scsiDev.targets[i].cfg)
  39. {
  40. if (sel_bits & (1 << scsiDev.targets[i].targetId))
  41. {
  42. sel_id = scsiDev.targets[i].targetId;
  43. break;
  44. }
  45. }
  46. }
  47. if (sel_id >= 0)
  48. {
  49. uint8_t atn_flag = SCSI_IN(ATN) ? SCSI_STS_SELECTION_ATN : 0;
  50. g_scsi_sts_selection = SCSI_STS_SELECTION_SUCCEEDED | atn_flag | sel_id;
  51. }
  52. }
  53. }
  54. extern "C" bool scsiStatusSEL()
  55. {
  56. if (g_scsi_ctrl_bsy)
  57. {
  58. // We don't have direct register access to BSY bit like SCSI2SD scsi.c expects.
  59. // Instead update the state here.
  60. // Releasing happens with bus release.
  61. g_scsi_ctrl_bsy = 0;
  62. SCSI_OUT(BSY, 1);
  63. }
  64. return SCSI_IN(SEL);
  65. }
  66. /************************/
  67. /* SCSI bus reset logic */
  68. /************************/
  69. static void scsi_rst_assert_interrupt()
  70. {
  71. bool rst1 = SCSI_IN(RST);
  72. delay_ns(500);
  73. bool rst2 = SCSI_IN(RST);
  74. if (rst1 && rst2)
  75. {
  76. scsiDev.resetFlag = 1;
  77. }
  78. }
  79. extern "C" void scsiPhyReset(void)
  80. {
  81. SCSI_RELEASE_OUTPUTS();
  82. g_scsi_sts_selection = 0;
  83. g_scsi_ctrl_bsy = 0;
  84. init_irqs();
  85. }
  86. /************************/
  87. /* SCSI bus phase logic */
  88. /************************/
  89. static SCSI_PHASE g_scsi_phase;
  90. extern "C" void scsiEnterPhase(int phase)
  91. {
  92. int delay = scsiEnterPhaseImmediate(phase);
  93. if (delay > 0)
  94. {
  95. s2s_delay_ns(delay);
  96. }
  97. }
  98. // Change state and return nanosecond delay to wait
  99. extern "C" uint32_t scsiEnterPhaseImmediate(int phase)
  100. {
  101. // ANSI INCITS 362-2002 SPI-3 10.7.1:
  102. // Phase changes are not allowed while REQ or ACK is asserted.
  103. while (likely(!scsiDev.resetFlag) && SCSI_IN(ACK)) {}
  104. if (phase != g_scsi_phase)
  105. {
  106. if (phase < 0)
  107. {
  108. // Other communication on bus or reset state
  109. SCSI_RELEASE_OUTPUTS();
  110. }
  111. else
  112. {
  113. SCSI_OUT(MSG, phase & __scsiphase_msg);
  114. SCSI_OUT(CD, phase & __scsiphase_cd);
  115. SCSI_OUT(IO, phase & __scsiphase_io);
  116. }
  117. scsiLogPhaseChange(phase);
  118. g_scsi_phase = (SCSI_PHASE)phase;
  119. // Hardcoded 100 us delay for now.
  120. return 100000;
  121. }
  122. else
  123. {
  124. return 0;
  125. }
  126. }
  127. // Release all signals
  128. void scsiEnterBusFree(void)
  129. {
  130. g_scsi_phase = BUS_FREE;
  131. g_scsi_sts_selection = 0;
  132. g_scsi_ctrl_bsy = 0;
  133. scsiDev.cdbLen = 0;
  134. SCSI_RELEASE_OUTPUTS();
  135. }
  136. /********************/
  137. /* Transmit to host */
  138. /********************/
  139. #define SCSI_WAIT_ACTIVE(pin) \
  140. if (!SCSI_IN(pin)) { \
  141. if (!SCSI_IN(pin)) { \
  142. while(!SCSI_IN(pin) && !scsiDev.resetFlag); \
  143. } \
  144. }
  145. #define SCSI_WAIT_INACTIVE(pin) \
  146. if (SCSI_IN(pin)) { \
  147. if (SCSI_IN(pin)) { \
  148. while(SCSI_IN(pin) && !scsiDev.resetFlag); \
  149. } \
  150. }
  151. static inline void scsiWriteOneByte(uint8_t value)
  152. {
  153. SCSI_OUT_DATA(value);
  154. delay_100ns(); // DB setup time before REQ
  155. SCSI_OUT(REQ, 1);
  156. SCSI_WAIT_ACTIVE(ACK);
  157. SCSI_RELEASE_DATA_REQ(); // Release data and REQ
  158. SCSI_WAIT_INACTIVE(ACK);
  159. }
  160. extern "C" void scsiWriteByte(uint8_t value)
  161. {
  162. scsiLogDataIn(&value, 1);
  163. scsiWriteOneByte(value);
  164. }
  165. extern "C" void scsiWrite(const uint8_t* data, uint32_t count)
  166. {
  167. uint32_t count_words = count / 4;
  168. scsiLogDataIn(data, count);
  169. if (count_words * 4 == count)
  170. {
  171. // Use accelerated subroutine
  172. scsi_accel_asm_send((const uint32_t*)data, count_words, &scsiDev.resetFlag);
  173. }
  174. else
  175. {
  176. for (uint32_t i = 0; i < count; i++)
  177. {
  178. if (scsiDev.resetFlag) break;
  179. scsiWriteOneByte(data[i]);
  180. }
  181. }
  182. }
  183. /*********************/
  184. /* Receive from host */
  185. /*********************/
  186. static inline uint8_t scsiReadOneByte(void)
  187. {
  188. SCSI_OUT(REQ, 1);
  189. SCSI_WAIT_ACTIVE(ACK);
  190. delay_100ns();
  191. uint8_t r = SCSI_IN_DATA();
  192. SCSI_OUT(REQ, 0);
  193. SCSI_WAIT_INACTIVE(ACK);
  194. return r;
  195. }
  196. extern "C" uint8_t scsiReadByte(void)
  197. {
  198. uint8_t r = scsiReadOneByte();
  199. scsiLogDataOut(&r, 1);
  200. return r;
  201. }
  202. extern "C" void scsiRead(uint8_t* data, uint32_t count, int* parityError)
  203. {
  204. *parityError = 0;
  205. for (uint32_t i = 0; i < count; i++)
  206. {
  207. if (scsiDev.resetFlag) break;
  208. data[i] = scsiReadOneByte();
  209. }
  210. scsiLogDataOut(data, count);
  211. }
  212. /**********************/
  213. /* Interrupt handlers */
  214. /**********************/
  215. extern "C"
  216. void SCSI_RST_IRQ (void)
  217. {
  218. if (exti_interrupt_flag_get(SCSI_RST_EXTI))
  219. {
  220. exti_interrupt_flag_clear(SCSI_RST_EXTI);
  221. scsi_rst_assert_interrupt();
  222. }
  223. if (exti_interrupt_flag_get(SCSI_BSY_EXTI))
  224. {
  225. exti_interrupt_flag_clear(SCSI_BSY_EXTI);
  226. scsi_bsy_deassert_interrupt();
  227. }
  228. }
  229. #if SCSI_RST_IRQn != SCSI_BSY_IRQn
  230. extern "C"
  231. void SCSI_BSY_IRQ (void)
  232. {
  233. SCSI_RST_IRQ();
  234. }
  235. #endif
  236. static void init_irqs()
  237. {
  238. // Falling edge of RST pin
  239. gpio_exti_source_select(SCSI_RST_EXTI_SOURCE_PORT, SCSI_RST_EXTI_SOURCE_PIN);
  240. exti_init(SCSI_RST_EXTI, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
  241. NVIC_SetPriority(SCSI_RST_IRQn, 1);
  242. NVIC_EnableIRQ(SCSI_RST_IRQn);
  243. // Rising edge of BSY pin
  244. gpio_exti_source_select(SCSI_BSY_EXTI_SOURCE_PORT, SCSI_BSY_EXTI_SOURCE_PIN);
  245. exti_init(SCSI_BSY_EXTI, EXTI_INTERRUPT, EXTI_TRIG_RISING);
  246. NVIC_SetPriority(SCSI_BSY_IRQn, 1);
  247. NVIC_EnableIRQ(SCSI_BSY_IRQn);
  248. }