scsiPhy.cpp 13 KB

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