scsiPhy.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 "scsi_accel_rp2040.h"
  9. #include <scsi2sd.h>
  10. extern "C" {
  11. #include <scsi.h>
  12. #include <scsi2sd_time.h>
  13. }
  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. void scsi_bsy_deassert_interrupt()
  31. {
  32. if (SCSI_IN(SEL) && !SCSI_IN(BSY))
  33. {
  34. // Check if any of the targets we simulate is selected
  35. uint8_t sel_bits = SCSI_IN_DATA();
  36. int sel_id = -1;
  37. for (int i = 0; i < S2S_MAX_TARGETS; i++)
  38. {
  39. if (scsiDev.targets[i].targetId <= 7 && scsiDev.targets[i].cfg)
  40. {
  41. if (sel_bits & (1 << scsiDev.targets[i].targetId))
  42. {
  43. sel_id = scsiDev.targets[i].targetId;
  44. break;
  45. }
  46. }
  47. }
  48. if (sel_id >= 0)
  49. {
  50. // Set ATN flag here unconditionally, real value is only known after
  51. // OUT_BSY is enabled in scsiStatusSEL() below.
  52. g_scsi_sts_selection = SCSI_STS_SELECTION_SUCCEEDED | SCSI_STS_SELECTION_ATN | sel_id;
  53. }
  54. // selFlag is required for Philips P2000C which releases it after 600ns
  55. // without waiting for BSY.
  56. // Also required for some early Mac Plus roms
  57. scsiDev.selFlag = *SCSI_STS_SELECTED;
  58. }
  59. }
  60. extern "C" bool scsiStatusSEL()
  61. {
  62. if (g_scsi_ctrl_bsy)
  63. {
  64. // We don't have direct register access to BSY bit like SCSI2SD scsi.c expects.
  65. // Instead update the state here.
  66. // Releasing happens with bus release.
  67. g_scsi_ctrl_bsy = 0;
  68. SCSI_OUT(BSY, 1);
  69. // On RP2040 hardware the ATN signal is only available after OUT_BSY enables
  70. // the IO buffer U105, so check the signal status here.
  71. delay_100ns();
  72. if (!scsiStatusATN())
  73. {
  74. // This is a SCSI1 host that does send IDENTIFY message
  75. scsiDev.atnFlag = 0;
  76. scsiDev.target->unitAttention = 0;
  77. scsiDev.compatMode = COMPAT_SCSI1;
  78. }
  79. }
  80. return SCSI_IN(SEL);
  81. }
  82. /************************/
  83. /* SCSI bus reset logic */
  84. /************************/
  85. static void scsi_rst_assert_interrupt()
  86. {
  87. // Glitch filtering
  88. bool rst1 = SCSI_IN(RST);
  89. delay_ns(500);
  90. bool rst2 = SCSI_IN(RST);
  91. if (rst1 && rst2)
  92. {
  93. azdbg("BUS RESET");
  94. scsiDev.resetFlag = 1;
  95. }
  96. }
  97. static void scsiPhyIRQ(uint gpio, uint32_t events)
  98. {
  99. if (gpio == SCSI_IN_BSY || gpio == SCSI_IN_SEL)
  100. {
  101. // Note BSY / SEL interrupts only when we are not driving OUT_BSY low ourselves.
  102. // The BSY input pin may be shared with other signals.
  103. if (sio_hw->gpio_out & (1 << SCSI_OUT_BSY))
  104. {
  105. scsi_bsy_deassert_interrupt();
  106. }
  107. }
  108. else if (gpio == SCSI_IN_RST)
  109. {
  110. scsi_rst_assert_interrupt();
  111. }
  112. }
  113. // This function is called to initialize the phy code.
  114. // It is called after power-on and after SCSI bus reset.
  115. extern "C" void scsiPhyReset(void)
  116. {
  117. SCSI_RELEASE_OUTPUTS();
  118. g_scsi_sts_selection = 0;
  119. g_scsi_ctrl_bsy = 0;
  120. scsi_accel_rp2040_init();
  121. // Enable BSY, RST and SEL interrupts
  122. // Note: RP2040 library currently supports only one callback,
  123. // so it has to be same for both pins.
  124. gpio_set_irq_enabled_with_callback(SCSI_IN_BSY, GPIO_IRQ_EDGE_RISE, true, scsiPhyIRQ);
  125. gpio_set_irq_enabled(SCSI_IN_RST, GPIO_IRQ_EDGE_FALL, true);
  126. // Check BSY line status when SEL goes active.
  127. // This is needed to handle SCSI-1 hosts that use the single initiator mode.
  128. // The host will just assert the SEL directly, without asserting BSY first.
  129. gpio_set_irq_enabled(SCSI_IN_SEL, GPIO_IRQ_EDGE_FALL, true);
  130. }
  131. /************************/
  132. /* SCSI bus phase logic */
  133. /************************/
  134. static SCSI_PHASE g_scsi_phase;
  135. extern "C" void scsiEnterPhase(int phase)
  136. {
  137. int delay = scsiEnterPhaseImmediate(phase);
  138. if (delay > 0)
  139. {
  140. s2s_delay_ns(delay);
  141. }
  142. }
  143. // Change state and return nanosecond delay to wait
  144. extern "C" uint32_t scsiEnterPhaseImmediate(int phase)
  145. {
  146. // ANSI INCITS 362-2002 SPI-3 10.7.1:
  147. // Phase changes are not allowed while REQ or ACK is asserted.
  148. while (likely(!scsiDev.resetFlag) && SCSI_IN(ACK)) {}
  149. if (phase != g_scsi_phase)
  150. {
  151. int oldphase = g_scsi_phase;
  152. g_scsi_phase = (SCSI_PHASE)phase;
  153. scsiLogPhaseChange(phase);
  154. // Select between synchronous vs. asynchronous SCSI writes
  155. if (g_scsi_phase == DATA_IN && scsiDev.target->syncOffset > 0)
  156. {
  157. scsi_accel_rp2040_setWriteMode(scsiDev.target->syncOffset, scsiDev.target->syncPeriod);
  158. }
  159. else
  160. {
  161. scsi_accel_rp2040_setWriteMode(0, 0);
  162. }
  163. if (phase < 0)
  164. {
  165. // Other communication on bus or reset state
  166. SCSI_RELEASE_OUTPUTS();
  167. return 0;
  168. }
  169. else
  170. {
  171. SCSI_OUT(MSG, phase & __scsiphase_msg);
  172. SCSI_OUT(CD, phase & __scsiphase_cd);
  173. SCSI_OUT(IO, phase & __scsiphase_io);
  174. SCSI_ENABLE_CONTROL_OUT();
  175. int delayNs = 400; // Bus settle delay
  176. if ((oldphase & __scsiphase_io) != (phase & __scsiphase_io))
  177. {
  178. delayNs += 400; // Data release delay
  179. }
  180. if (scsiDev.compatMode < COMPAT_SCSI2)
  181. {
  182. // EMU EMAX needs 100uS ! 10uS is not enough.
  183. delayNs += 100000;
  184. }
  185. return delayNs;
  186. }
  187. }
  188. else
  189. {
  190. return 0;
  191. }
  192. }
  193. // Release all signals
  194. void scsiEnterBusFree(void)
  195. {
  196. g_scsi_phase = BUS_FREE;
  197. g_scsi_sts_selection = 0;
  198. g_scsi_ctrl_bsy = 0;
  199. scsiDev.cdbLen = 0;
  200. SCSI_RELEASE_OUTPUTS();
  201. }
  202. /********************/
  203. /* Transmit to host */
  204. /********************/
  205. #define SCSI_WAIT_ACTIVE(pin) \
  206. if (!SCSI_IN(pin)) { \
  207. if (!SCSI_IN(pin)) { \
  208. while(!SCSI_IN(pin) && !scsiDev.resetFlag); \
  209. } \
  210. }
  211. #define SCSI_WAIT_INACTIVE(pin) \
  212. if (SCSI_IN(pin)) { \
  213. if (SCSI_IN(pin)) { \
  214. while(SCSI_IN(pin) && !scsiDev.resetFlag); \
  215. } \
  216. }
  217. // Write one byte to SCSI host using the handshake mechanism
  218. static inline void scsiWriteOneByte(uint8_t value)
  219. {
  220. SCSI_OUT_DATA(value);
  221. delay_100ns(); // DB setup time before REQ
  222. SCSI_OUT(REQ, 1);
  223. SCSI_WAIT_ACTIVE(ACK);
  224. SCSI_RELEASE_DATA_REQ();
  225. SCSI_WAIT_INACTIVE(ACK);
  226. }
  227. extern "C" void scsiWriteByte(uint8_t value)
  228. {
  229. scsiLogDataIn(&value, 1);
  230. scsiWriteOneByte(value);
  231. }
  232. extern "C" void scsiWrite(const uint8_t* data, uint32_t count)
  233. {
  234. scsiStartWrite(data, count);
  235. scsiFinishWrite();
  236. }
  237. extern "C" void scsiStartWrite(const uint8_t* data, uint32_t count)
  238. {
  239. scsiLogDataIn(data, count);
  240. if ((count & 1) != 0 || ((uint32_t)data & 1) != 0)
  241. {
  242. // Unaligned write, do it byte-by-byte
  243. scsiFinishWrite();
  244. for (uint32_t i = 0; i < count; i++)
  245. {
  246. if (scsiDev.resetFlag) break;
  247. scsiWriteOneByte(data[i]);
  248. }
  249. }
  250. else
  251. {
  252. // Use accelerated routine
  253. scsi_accel_rp2040_startWrite(data, count, &scsiDev.resetFlag);
  254. }
  255. }
  256. extern "C" bool scsiIsWriteFinished(const uint8_t *data)
  257. {
  258. return scsi_accel_rp2040_isWriteFinished(data);
  259. }
  260. extern "C" void scsiFinishWrite()
  261. {
  262. scsi_accel_rp2040_finishWrite(&scsiDev.resetFlag);
  263. }
  264. /*********************/
  265. /* Receive from host */
  266. /*********************/
  267. // Read one byte from SCSI host using the handshake mechanism.
  268. static inline uint8_t scsiReadOneByte(int* parityError)
  269. {
  270. SCSI_OUT(REQ, 1);
  271. SCSI_WAIT_ACTIVE(ACK);
  272. delay_100ns();
  273. uint16_t r = SCSI_IN_DATA();
  274. SCSI_OUT(REQ, 0);
  275. SCSI_WAIT_INACTIVE(ACK);
  276. if (parityError && r != (g_scsi_parity_lookup[r & 0xFF] ^ SCSI_IO_DATA_MASK))
  277. {
  278. azlog("Parity error in scsiReadOneByte(): ", (uint32_t)r);
  279. *parityError = 1;
  280. }
  281. return (uint8_t)r;
  282. }
  283. extern "C" uint8_t scsiReadByte(void)
  284. {
  285. uint8_t r = scsiReadOneByte(NULL);
  286. scsiLogDataOut(&r, 1);
  287. return r;
  288. }
  289. extern "C" void scsiRead(uint8_t* data, uint32_t count, int* parityError)
  290. {
  291. *parityError = 0;
  292. if ((count & 1) != 0 || ((uint32_t)data & 1) != 0)
  293. {
  294. // Unaligned transfer, do byte by byte
  295. for (uint32_t i = 0; i < count; i++)
  296. {
  297. if (scsiDev.resetFlag) break;
  298. data[i] = scsiReadOneByte(parityError);
  299. }
  300. }
  301. else
  302. {
  303. // Use accelerated routine
  304. scsi_accel_rp2040_read(data, count, parityError, &scsiDev.resetFlag);
  305. }
  306. scsiLogDataOut(data, count);
  307. }