scsiPhy.cpp 10 KB

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