scsiPhy.cpp 11 KB

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