scsiPhy.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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 "scsi_accel_asm.h"
  6. #include "scsi_accel_dma.h"
  7. #include "scsi_accel_sync.h"
  8. #include "ZuluSCSI_log.h"
  9. #include "ZuluSCSI_log_trace.h"
  10. #include "ZuluSCSI_config.h"
  11. #include <minIni.h>
  12. #include <scsi2sd.h>
  13. extern "C" {
  14. #include <scsi.h>
  15. #include <scsi2sd_time.h>
  16. }
  17. // Acceleration mode in use
  18. static enum {
  19. PHY_MODE_BEST_AVAILABLE = 0,
  20. PHY_MODE_PIO = 1,
  21. PHY_MODE_DMA_TIMER = 2,
  22. PHY_MODE_GREENPAK_PIO = 3,
  23. PHY_MODE_GREENPAK_DMA = 4
  24. } g_scsi_phy_mode;
  25. static const char *g_scsi_phy_mode_names[] = {
  26. "Unknown", "PIO", "DMA_TIMER" // removing greenpak , "GREENPAK_PIO", "GREENPAK_DMA"
  27. };
  28. // State of polling write request
  29. static struct {
  30. const uint8_t *data;
  31. uint32_t count;
  32. bool use_sync_mode;
  33. } g_scsi_writereq;
  34. static void init_irqs();
  35. /***********************/
  36. /* SCSI status signals */
  37. /***********************/
  38. extern "C" bool scsiStatusATN()
  39. {
  40. return SCSI_IN(ATN);
  41. }
  42. extern "C" bool scsiStatusBSY()
  43. {
  44. return SCSI_IN(BSY);
  45. }
  46. /************************/
  47. /* SCSI selection logic */
  48. /************************/
  49. volatile uint8_t g_scsi_sts_selection;
  50. volatile uint8_t g_scsi_ctrl_bsy;
  51. static void scsi_bsy_deassert_interrupt()
  52. {
  53. if (SCSI_IN(SEL) && !SCSI_IN(BSY))
  54. {
  55. uint8_t sel_bits = SCSI_IN_DATA();
  56. int sel_id = -1;
  57. for (int i = 0; i < S2S_MAX_TARGETS; i++)
  58. {
  59. if (scsiDev.targets[i].targetId <= 7 && scsiDev.targets[i].cfg)
  60. {
  61. if (sel_bits & (1 << scsiDev.targets[i].targetId))
  62. {
  63. sel_id = scsiDev.targets[i].targetId;
  64. break;
  65. }
  66. }
  67. }
  68. if (sel_id >= 0)
  69. {
  70. uint8_t atn_flag = SCSI_IN(ATN) ? SCSI_STS_SELECTION_ATN : 0;
  71. g_scsi_sts_selection = SCSI_STS_SELECTION_SUCCEEDED | atn_flag | sel_id;
  72. }
  73. // selFlag is required for Philips P2000C which releases it after 600ns
  74. // without waiting for BSY.
  75. // Also required for some early Mac Plus roms
  76. scsiDev.selFlag = *SCSI_STS_SELECTED;
  77. }
  78. }
  79. extern "C" bool scsiStatusSEL()
  80. {
  81. if (g_scsi_ctrl_bsy)
  82. {
  83. // We don't have direct register access to BSY bit like SCSI2SD scsi.c expects.
  84. // Instead update the state here.
  85. // Releasing happens with bus release.
  86. g_scsi_ctrl_bsy = 0;
  87. SCSI_OUT(BSY, 1);
  88. }
  89. return SCSI_IN(SEL);
  90. }
  91. /************************/
  92. /* SCSI bus reset logic */
  93. /************************/
  94. static void scsi_rst_assert_interrupt()
  95. {
  96. bool rst1 = SCSI_IN(RST);
  97. delay_ns(500);
  98. bool rst2 = SCSI_IN(RST);
  99. if (rst1 && rst2)
  100. {
  101. azdbg("BUS RESET");
  102. scsiDev.resetFlag = 1;
  103. }
  104. }
  105. static void selectPhyMode()
  106. {
  107. int oldmode = g_scsi_phy_mode;
  108. int default_mode = PHY_MODE_BEST_AVAILABLE;
  109. // Read overriding setting from configuration file
  110. int wanted_mode = ini_getl("SCSI", "PhyMode", default_mode, CONFIGFILE);
  111. // Default: software GPIO bitbang, available on all revisions
  112. g_scsi_phy_mode = PHY_MODE_PIO;
  113. // Timer based DMA bitbang, available on V1.1, 2.8 MB/s
  114. #ifdef SCSI_ACCEL_DMA_AVAILABLE
  115. if (wanted_mode == PHY_MODE_BEST_AVAILABLE || wanted_mode == PHY_MODE_DMA_TIMER)
  116. {
  117. g_scsi_phy_mode = PHY_MODE_DMA_TIMER;
  118. }
  119. #endif
  120. if (g_scsi_phy_mode != oldmode)
  121. {
  122. azlog("SCSI PHY operating mode: ", g_scsi_phy_mode_names[g_scsi_phy_mode]);
  123. }
  124. }
  125. extern "C" void scsiPhyReset(void)
  126. {
  127. SCSI_RELEASE_OUTPUTS();
  128. scsi_accel_dma_stopWrite();
  129. g_scsi_sts_selection = 0;
  130. g_scsi_ctrl_bsy = 0;
  131. g_scsi_writereq.count = 0;
  132. init_irqs();
  133. #ifdef SCSI_SYNC_MODE_AVAILABLE
  134. scsi_accel_sync_init();
  135. #endif
  136. selectPhyMode();
  137. if (g_scsi_phy_mode == PHY_MODE_DMA_TIMER)
  138. {
  139. scsi_accel_timer_dma_init();
  140. }
  141. }
  142. /************************/
  143. /* SCSI bus phase logic */
  144. /************************/
  145. static SCSI_PHASE g_scsi_phase;
  146. extern "C" void scsiEnterPhase(int phase)
  147. {
  148. int delay = scsiEnterPhaseImmediate(phase);
  149. if (delay > 0)
  150. {
  151. s2s_delay_ns(delay);
  152. }
  153. }
  154. // Change state and return nanosecond delay to wait
  155. extern "C" uint32_t scsiEnterPhaseImmediate(int phase)
  156. {
  157. if (phase != g_scsi_phase)
  158. {
  159. // ANSI INCITS 362-2002 SPI-3 10.7.1:
  160. // Phase changes are not allowed while REQ or ACK is asserted.
  161. while (likely(!scsiDev.resetFlag) && SCSI_IN(ACK)) {}
  162. if (scsiDev.compatMode < COMPAT_SCSI2 && (phase == DATA_IN || phase == DATA_OUT))
  163. {
  164. // Akai S1000/S3000 seems to need extra delay before changing to data phase
  165. // after a command. The code in ZuluSCSI_disk.cpp tries to do this while waiting
  166. // for SD card, to avoid any extra latency.
  167. s2s_delay_ns(400000);
  168. }
  169. int oldphase = g_scsi_phase;
  170. g_scsi_phase = (SCSI_PHASE)phase;
  171. scsiLogPhaseChange(phase);
  172. if (phase < 0)
  173. {
  174. // Other communication on bus or reset state
  175. SCSI_RELEASE_OUTPUTS();
  176. return 0;
  177. }
  178. else
  179. {
  180. SCSI_OUT(MSG, phase & __scsiphase_msg);
  181. SCSI_OUT(CD, phase & __scsiphase_cd);
  182. SCSI_OUT(IO, phase & __scsiphase_io);
  183. int delayNs = 400; // Bus settle delay
  184. if ((oldphase & __scsiphase_io) != (phase & __scsiphase_io))
  185. {
  186. delayNs += 400; // Data release delay
  187. }
  188. if (scsiDev.compatMode < COMPAT_SCSI2)
  189. {
  190. // EMU EMAX needs 100uS ! 10uS is not enough.
  191. delayNs += 100000;
  192. }
  193. return delayNs;
  194. }
  195. }
  196. else
  197. {
  198. return 0;
  199. }
  200. }
  201. // Release all signals
  202. void scsiEnterBusFree(void)
  203. {
  204. g_scsi_phase = BUS_FREE;
  205. g_scsi_sts_selection = 0;
  206. g_scsi_ctrl_bsy = 0;
  207. scsiDev.cdbLen = 0;
  208. SCSI_RELEASE_OUTPUTS();
  209. }
  210. /********************/
  211. /* Transmit to host */
  212. /********************/
  213. #define SCSI_WAIT_ACTIVE(pin) \
  214. if (!SCSI_IN(pin)) { \
  215. if (!SCSI_IN(pin)) { \
  216. while(!SCSI_IN(pin) && !scsiDev.resetFlag); \
  217. } \
  218. }
  219. #define SCSI_WAIT_INACTIVE(pin) \
  220. if (SCSI_IN(pin)) { \
  221. if (SCSI_IN(pin)) { \
  222. while(SCSI_IN(pin) && !scsiDev.resetFlag); \
  223. } \
  224. }
  225. static inline void scsiWriteOneByte(uint8_t value)
  226. {
  227. SCSI_OUT_DATA(value);
  228. delay_100ns(); // DB setup time before REQ
  229. SCSI_OUT(REQ, 1);
  230. SCSI_WAIT_ACTIVE(ACK);
  231. SCSI_RELEASE_DATA_REQ(); // Release data and REQ
  232. SCSI_WAIT_INACTIVE(ACK);
  233. }
  234. extern "C" void scsiWriteByte(uint8_t value)
  235. {
  236. scsiLogDataIn(&value, 1);
  237. scsiWriteOneByte(value);
  238. }
  239. extern "C" void scsiWrite(const uint8_t* data, uint32_t count)
  240. {
  241. scsiStartWrite(data, count);
  242. scsiFinishWrite();
  243. }
  244. extern "C" void scsiStartWrite(const uint8_t* data, uint32_t count)
  245. {
  246. scsiLogDataIn(data, count);
  247. g_scsi_writereq.use_sync_mode = (g_scsi_phase == DATA_IN && scsiDev.target->syncOffset > 0);
  248. if (g_scsi_phy_mode == PHY_MODE_PIO
  249. || g_scsi_phy_mode == PHY_MODE_GREENPAK_PIO
  250. || g_scsi_writereq.use_sync_mode)
  251. {
  252. // Software based bit-banging.
  253. // Write requests are queued and then executed in isWriteFinished() callback.
  254. // This allows better parallelism with SD card transfers.
  255. if (g_scsi_writereq.count)
  256. {
  257. if (data == g_scsi_writereq.data + g_scsi_writereq.count)
  258. {
  259. // Combine with previous one
  260. g_scsi_writereq.count += count;
  261. return;
  262. }
  263. else
  264. {
  265. // Actually execute previous request
  266. scsiFinishWrite();
  267. }
  268. }
  269. g_scsi_writereq.data = data;
  270. g_scsi_writereq.count = count;
  271. }
  272. else if (g_scsi_phy_mode == PHY_MODE_DMA_TIMER || g_scsi_phy_mode == PHY_MODE_GREENPAK_DMA)
  273. {
  274. // Accelerated writes using DMA and timers
  275. scsi_accel_dma_startWrite(data, count, &scsiDev.resetFlag);
  276. }
  277. else
  278. {
  279. azlog("Unknown SCSI PHY mode: ", (int)g_scsi_phy_mode);
  280. }
  281. }
  282. static void processPollingWrite(uint32_t count)
  283. {
  284. if (count > g_scsi_writereq.count)
  285. count = g_scsi_writereq.count;
  286. const uint8_t *data = g_scsi_writereq.data;
  287. uint32_t count_words = count / 4;
  288. if (g_scsi_writereq.use_sync_mode)
  289. {
  290. // Synchronous mode transfer
  291. scsi_accel_sync_send(data, count, &scsiDev.resetFlag);
  292. }
  293. else if (count_words * 4 == count)
  294. {
  295. // Assembler optimized asynchronous transfer
  296. scsi_accel_asm_send((const uint32_t*)data, count_words, &scsiDev.resetFlag);
  297. }
  298. else
  299. {
  300. // Use simple loop for unaligned transfers
  301. for (uint32_t i = 0; i < count; i++)
  302. {
  303. if (scsiDev.resetFlag) break;
  304. scsiWriteOneByte(data[i]);
  305. }
  306. }
  307. g_scsi_writereq.count -= count;
  308. if (g_scsi_writereq.count)
  309. {
  310. g_scsi_writereq.data += count;
  311. }
  312. else
  313. {
  314. g_scsi_writereq.data = NULL;
  315. }
  316. }
  317. static bool isPollingWriteFinished(const uint8_t *data)
  318. {
  319. if (g_scsi_writereq.count)
  320. {
  321. if (data == NULL)
  322. {
  323. return false;
  324. }
  325. else if (data >= g_scsi_writereq.data &&
  326. data < g_scsi_writereq.data + g_scsi_writereq.count)
  327. {
  328. return false;
  329. }
  330. }
  331. return true;
  332. }
  333. extern "C" bool scsiIsWriteFinished(const uint8_t *data)
  334. {
  335. // Check if there is still a polling transfer in progress
  336. if (!isPollingWriteFinished(data) && !check_sd_read_done())
  337. {
  338. // Process the transfer piece-by-piece while waiting
  339. // for SD card to react.
  340. int max_count = g_scsi_writereq.count / 8;
  341. // Always transfer whole sectors without pause to avoid problems with some SCSI hosts.
  342. int bytesPerSector = 512;
  343. if (scsiDev.target)
  344. {
  345. bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
  346. }
  347. if (max_count % bytesPerSector != 0) max_count -= (max_count % bytesPerSector);
  348. if (max_count < bytesPerSector) max_count = bytesPerSector;
  349. // Avoid SysTick interrupt pauses during the transfer
  350. SysTick_Handle_PreEmptively();
  351. processPollingWrite(max_count);
  352. return isPollingWriteFinished(data);
  353. }
  354. if (g_scsi_phy_mode == PHY_MODE_DMA_TIMER || g_scsi_phy_mode == PHY_MODE_GREENPAK_DMA)
  355. {
  356. return scsi_accel_dma_isWriteFinished(data);
  357. }
  358. else
  359. {
  360. return true;
  361. }
  362. }
  363. extern "C" void scsiFinishWrite()
  364. {
  365. if (g_scsi_writereq.count)
  366. {
  367. // Finish previously started polling write request.
  368. processPollingWrite(g_scsi_writereq.count);
  369. }
  370. if (g_scsi_phy_mode == PHY_MODE_DMA_TIMER || g_scsi_phy_mode == PHY_MODE_GREENPAK_DMA)
  371. {
  372. scsi_accel_dma_finishWrite(&scsiDev.resetFlag);
  373. }
  374. }
  375. /*********************/
  376. /* Receive from host */
  377. /*********************/
  378. static inline uint8_t scsiReadOneByte(void)
  379. {
  380. SCSI_OUT(REQ, 1);
  381. SCSI_WAIT_ACTIVE(ACK);
  382. delay_100ns();
  383. uint8_t r = SCSI_IN_DATA();
  384. SCSI_OUT(REQ, 0);
  385. SCSI_WAIT_INACTIVE(ACK);
  386. return r;
  387. }
  388. extern "C" uint8_t scsiReadByte(void)
  389. {
  390. uint8_t r = scsiReadOneByte();
  391. scsiLogDataOut(&r, 1);
  392. return r;
  393. }
  394. extern "C" void scsiRead(uint8_t* data, uint32_t count, int* parityError)
  395. {
  396. *parityError = 0;
  397. uint32_t count_words = count / 4;
  398. SysTick_Handle_PreEmptively();
  399. if (g_scsi_phase == DATA_OUT && scsiDev.target->syncOffset > 0)
  400. {
  401. // Synchronous data transfer
  402. scsi_accel_sync_recv(data, count, parityError, &scsiDev.resetFlag);
  403. }
  404. else if (count_words * 4 == count && count_words >= 1)
  405. {
  406. // Optimized ASM subroutine can handle multiple of 4 bytes with minimum of 4 bytes.
  407. scsi_accel_asm_recv((uint32_t*)data, count_words, &scsiDev.resetFlag);
  408. }
  409. else
  410. {
  411. // Use a simple loop for short and unaligned transfers
  412. for (uint32_t i = 0; i < count; i++)
  413. {
  414. if (scsiDev.resetFlag) break;
  415. data[i] = scsiReadOneByte();
  416. }
  417. }
  418. scsiLogDataOut(data, count);
  419. }
  420. /**********************/
  421. /* Interrupt handlers */
  422. /**********************/
  423. extern "C"
  424. void SCSI_RST_IRQ (void)
  425. {
  426. if (exti_interrupt_flag_get(SCSI_RST_EXTI))
  427. {
  428. exti_interrupt_flag_clear(SCSI_RST_EXTI);
  429. scsi_rst_assert_interrupt();
  430. }
  431. if (exti_interrupt_flag_get(SCSI_BSY_EXTI))
  432. {
  433. exti_interrupt_flag_clear(SCSI_BSY_EXTI);
  434. scsi_bsy_deassert_interrupt();
  435. }
  436. if (exti_interrupt_flag_get(SCSI_SEL_EXTI))
  437. {
  438. // Check BSY line status when SEL goes active.
  439. // This is needed to handle SCSI-1 hosts that use the single initiator mode.
  440. // The host will just assert the SEL directly, without asserting BSY first.
  441. exti_interrupt_flag_clear(SCSI_SEL_EXTI);
  442. scsi_bsy_deassert_interrupt();
  443. }
  444. }
  445. #if SCSI_RST_IRQn != SCSI_BSY_IRQn
  446. extern "C"
  447. void SCSI_BSY_IRQ (void)
  448. {
  449. SCSI_RST_IRQ();
  450. }
  451. #endif
  452. #if (SCSI_SEL_IRQn != SCSI_RST_IRQn) && (SCSI_SEL_IRQn != SCSI_BSY_IRQn)
  453. extern "C"
  454. void SCSI_SEL_IRQ (void)
  455. {
  456. SCSI_RST_IRQ();
  457. }
  458. #endif
  459. static void init_irqs()
  460. {
  461. // Falling edge of RST pin
  462. gpio_mode_set(SCSI_RST_PORT, GPIO_MODE_INPUT, GPIO_PUPD_NONE, SCSI_RST_PIN);
  463. syscfg_exti_line_config(SCSI_RST_EXTI_SOURCE_PORT, SCSI_RST_EXTI_SOURCE_PIN);
  464. exti_init(SCSI_RST_EXTI, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
  465. NVIC_SetPriority(SCSI_RST_IRQn, 1);
  466. NVIC_EnableIRQ(SCSI_RST_IRQn);
  467. // Rising edge of BSY pin
  468. gpio_mode_set(SCSI_BSY_PORT, GPIO_MODE_INPUT, GPIO_PUPD_NONE, SCSI_BSY_PIN);
  469. syscfg_exti_line_config(SCSI_BSY_EXTI_SOURCE_PORT, SCSI_BSY_EXTI_SOURCE_PIN);
  470. exti_init(SCSI_BSY_EXTI, EXTI_INTERRUPT, EXTI_TRIG_RISING);
  471. NVIC_SetPriority(SCSI_BSY_IRQn, 1);
  472. NVIC_EnableIRQ(SCSI_BSY_IRQn);
  473. // Falling edge of SEL pin
  474. gpio_mode_set(SCSI_SEL_PORT, GPIO_MODE_INPUT, GPIO_PUPD_NONE, SCSI_SEL_PIN);
  475. syscfg_exti_line_config(SCSI_SEL_EXTI_SOURCE_PORT, SCSI_SEL_EXTI_SOURCE_PIN);
  476. exti_init(SCSI_SEL_EXTI, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
  477. NVIC_SetPriority(SCSI_SEL_IRQn, 1);
  478. NVIC_EnableIRQ(SCSI_SEL_IRQn);
  479. }