scsiPhy.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // Implements the low level interface to SCSI bus
  2. // Partially derived from scsiPhy.c from SCSI2SD-V6
  3. #include "scsiPhy.h"
  4. #include "ZuluSCSI_platform.h"
  5. #include "ZuluSCSI_log.h"
  6. #include "ZuluSCSI_log_trace.h"
  7. #include "ZuluSCSI_config.h"
  8. #include <scsi2sd.h>
  9. extern "C" {
  10. #include <scsi.h>
  11. #include <scsi2sd_time.h>
  12. }
  13. /***********************/
  14. /* SCSI status signals */
  15. /***********************/
  16. extern "C" bool scsiStatusATN()
  17. {
  18. return SCSI_IN(ATN);
  19. }
  20. extern "C" bool scsiStatusBSY()
  21. {
  22. return SCSI_IN(BSY);
  23. }
  24. /************************/
  25. /* SCSI selection logic */
  26. /************************/
  27. volatile uint8_t g_scsi_sts_selection;
  28. volatile uint8_t g_scsi_ctrl_bsy;
  29. void scsi_bsy_deassert_interrupt()
  30. {
  31. if (SCSI_IN(SEL) && !SCSI_IN(BSY))
  32. {
  33. // Check if any of the targets we simulate is selected
  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. // selFlag is required for Philips P2000C which releases it after 600ns
  53. // without waiting for BSY.
  54. // Also required for some early Mac Plus roms
  55. scsiDev.selFlag = *SCSI_STS_SELECTED;
  56. }
  57. }
  58. extern "C" bool scsiStatusSEL()
  59. {
  60. if (g_scsi_ctrl_bsy)
  61. {
  62. // We don't have direct register access to BSY bit like SCSI2SD scsi.c expects.
  63. // Instead update the state here.
  64. // Releasing happens with bus release.
  65. g_scsi_ctrl_bsy = 0;
  66. SCSI_OUT(BSY, 1);
  67. // On RP2040 hardware the ATN signal is only available after OUT_BSY enables
  68. // the IO buffer U105, so check the signal status here.
  69. delay_100ns();
  70. scsiDev.atnFlag |= scsiStatusATN();
  71. }
  72. return SCSI_IN(SEL);
  73. }
  74. /************************/
  75. /* SCSI bus reset logic */
  76. /************************/
  77. static void scsi_rst_assert_interrupt()
  78. {
  79. // Glitch filtering
  80. bool rst1 = SCSI_IN(RST);
  81. delay_ns(500);
  82. bool rst2 = SCSI_IN(RST);
  83. if (rst1 && rst2)
  84. {
  85. azdbg("BUS RESET");
  86. scsiDev.resetFlag = 1;
  87. }
  88. }
  89. static void scsiPhyIRQ(uint gpio, uint32_t events)
  90. {
  91. if (gpio == SCSI_IN_BSY)
  92. {
  93. // Note BSY interrupts only when we are not driving OUT_BSY low ourselves.
  94. // The BSY input pin may be shared with other signals.
  95. if (sio_hw->gpio_out & (1 << SCSI_OUT_BSY))
  96. {
  97. scsi_bsy_deassert_interrupt();
  98. }
  99. }
  100. else if (gpio == SCSI_IN_RST)
  101. {
  102. scsi_rst_assert_interrupt();
  103. }
  104. }
  105. // This function is called to initialize the phy code.
  106. // It is called after power-on and after SCSI bus reset.
  107. extern "C" void scsiPhyReset(void)
  108. {
  109. SCSI_RELEASE_OUTPUTS();
  110. g_scsi_sts_selection = 0;
  111. g_scsi_ctrl_bsy = 0;
  112. // Enable BSY and RST interrupts
  113. // Note: RP2040 library currently supports only one callback,
  114. // so it has to be same for both pins.
  115. gpio_set_irq_enabled_with_callback(SCSI_IN_BSY, GPIO_IRQ_EDGE_RISE, true, scsiPhyIRQ);
  116. gpio_set_irq_enabled_with_callback(SCSI_IN_RST, GPIO_IRQ_EDGE_FALL, true, scsiPhyIRQ);
  117. }
  118. /************************/
  119. /* SCSI bus phase logic */
  120. /************************/
  121. static SCSI_PHASE g_scsi_phase;
  122. extern "C" void scsiEnterPhase(int phase)
  123. {
  124. int delay = scsiEnterPhaseImmediate(phase);
  125. if (delay > 0)
  126. {
  127. s2s_delay_ns(delay);
  128. }
  129. }
  130. // Change state and return nanosecond delay to wait
  131. extern "C" uint32_t scsiEnterPhaseImmediate(int phase)
  132. {
  133. // ANSI INCITS 362-2002 SPI-3 10.7.1:
  134. // Phase changes are not allowed while REQ or ACK is asserted.
  135. while (likely(!scsiDev.resetFlag) && SCSI_IN(ACK)) {}
  136. if (phase != g_scsi_phase)
  137. {
  138. int oldphase = g_scsi_phase;
  139. g_scsi_phase = (SCSI_PHASE)phase;
  140. scsiLogPhaseChange(phase);
  141. if (phase < 0)
  142. {
  143. // Other communication on bus or reset state
  144. SCSI_RELEASE_OUTPUTS();
  145. return 0;
  146. }
  147. else
  148. {
  149. SCSI_OUT(MSG, phase & __scsiphase_msg);
  150. SCSI_OUT(CD, phase & __scsiphase_cd);
  151. SCSI_OUT(IO, phase & __scsiphase_io);
  152. SCSI_ENABLE_CONTROL_OUT();
  153. int delayNs = 400; // Bus settle delay
  154. if ((oldphase & __scsiphase_io) != (phase & __scsiphase_io))
  155. {
  156. delayNs += 400; // Data release delay
  157. }
  158. if (scsiDev.compatMode < COMPAT_SCSI2)
  159. {
  160. // EMU EMAX needs 100uS ! 10uS is not enough.
  161. delayNs += 100000;
  162. }
  163. return delayNs;
  164. }
  165. }
  166. else
  167. {
  168. return 0;
  169. }
  170. }
  171. // Release all signals
  172. void scsiEnterBusFree(void)
  173. {
  174. g_scsi_phase = BUS_FREE;
  175. g_scsi_sts_selection = 0;
  176. g_scsi_ctrl_bsy = 0;
  177. scsiDev.cdbLen = 0;
  178. SCSI_RELEASE_OUTPUTS();
  179. }
  180. /********************/
  181. /* Transmit to host */
  182. /********************/
  183. #define SCSI_WAIT_ACTIVE(pin) \
  184. if (!SCSI_IN(pin)) { \
  185. if (!SCSI_IN(pin)) { \
  186. while(!SCSI_IN(pin) && !scsiDev.resetFlag); \
  187. } \
  188. }
  189. #define SCSI_WAIT_INACTIVE(pin) \
  190. if (SCSI_IN(pin)) { \
  191. if (SCSI_IN(pin)) { \
  192. while(SCSI_IN(pin) && !scsiDev.resetFlag); \
  193. } \
  194. }
  195. // Write one byte to SCSI host using the handshake mechanism
  196. static inline void scsiWriteOneByte(uint8_t value)
  197. {
  198. SCSI_OUT_DATA(value);
  199. delay_100ns(); // DB setup time before REQ
  200. SCSI_OUT(REQ, 1);
  201. SCSI_WAIT_ACTIVE(ACK);
  202. SCSI_RELEASE_DATA_REQ();
  203. SCSI_WAIT_INACTIVE(ACK);
  204. }
  205. extern "C" void scsiWriteByte(uint8_t value)
  206. {
  207. scsiLogDataIn(&value, 1);
  208. scsiWriteOneByte(value);
  209. }
  210. extern "C" void scsiWrite(const uint8_t* data, uint32_t count)
  211. {
  212. scsiLogDataIn(data, count);
  213. for (uint32_t i = 0; i < count; i++)
  214. {
  215. if (scsiDev.resetFlag) break;
  216. scsiWriteOneByte(data[i]);
  217. }
  218. }
  219. extern "C" void scsiStartWrite(const uint8_t* data, uint32_t count)
  220. {
  221. // If the platform supports DMA for either SD card access or for SCSI bus,
  222. // this function can be used to execute SD card transfers in parallel with
  223. // SCSI transfers. This usually doubles the transfer speed.
  224. //
  225. // For simplicity, this example only implements blocking writes.
  226. scsiWrite(data, count);
  227. }
  228. extern "C" bool scsiIsWriteFinished(const uint8_t *data)
  229. {
  230. // Asynchronous writes are not implemented in this example.
  231. return true;
  232. }
  233. extern "C" void scsiFinishWrite()
  234. {
  235. // Asynchronous writes are not implemented in this example.
  236. }
  237. /*********************/
  238. /* Receive from host */
  239. /*********************/
  240. // Read one byte from SCSI host using the handshake mechanism.
  241. static inline uint8_t scsiReadOneByte(void)
  242. {
  243. SCSI_OUT(REQ, 1);
  244. SCSI_WAIT_ACTIVE(ACK);
  245. delay_100ns();
  246. uint8_t r = SCSI_IN_DATA();
  247. SCSI_OUT(REQ, 0);
  248. SCSI_WAIT_INACTIVE(ACK);
  249. return r;
  250. }
  251. extern "C" uint8_t scsiReadByte(void)
  252. {
  253. uint8_t r = scsiReadOneByte();
  254. scsiLogDataOut(&r, 1);
  255. return r;
  256. }
  257. extern "C" void scsiRead(uint8_t* data, uint32_t count, int* parityError)
  258. {
  259. *parityError = 0;
  260. for (uint32_t i = 0; i < count; i++)
  261. {
  262. if (scsiDev.resetFlag) break;
  263. data[i] = scsiReadOneByte();
  264. }
  265. scsiLogDataOut(data, count);
  266. }