scsiPhy.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. // GreenPAK with software write, available on V1.1 with extra chip, 3.5 MB/s
  121. if (wanted_mode == PHY_MODE_BEST_AVAILABLE || wanted_mode == PHY_MODE_GREENPAK_PIO)
  122. {
  123. if (greenpak_is_ready())
  124. {
  125. g_scsi_phy_mode = PHY_MODE_GREENPAK_PIO;
  126. }
  127. }
  128. // GreenPAK with DMA write, available on V1.1 with extra chip
  129. #ifdef SCSI_ACCEL_DMA_AVAILABLE
  130. if (wanted_mode == PHY_MODE_BEST_AVAILABLE || wanted_mode == PHY_MODE_GREENPAK_DMA)
  131. {
  132. if (greenpak_is_ready())
  133. {
  134. g_scsi_phy_mode = PHY_MODE_GREENPAK_DMA;
  135. }
  136. }
  137. #endif
  138. if (g_scsi_phy_mode != oldmode)
  139. {
  140. azlog("SCSI PHY operating mode: ", g_scsi_phy_mode_names[g_scsi_phy_mode]);
  141. }
  142. }
  143. extern "C" void scsiPhyReset(void)
  144. {
  145. SCSI_RELEASE_OUTPUTS();
  146. scsi_accel_dma_stopWrite();
  147. g_scsi_sts_selection = 0;
  148. g_scsi_ctrl_bsy = 0;
  149. g_scsi_writereq.count = 0;
  150. init_irqs();
  151. #ifdef SCSI_SYNC_MODE_AVAILABLE
  152. scsi_accel_sync_init();
  153. #endif
  154. selectPhyMode();
  155. if (g_scsi_phy_mode == PHY_MODE_DMA_TIMER)
  156. {
  157. scsi_accel_timer_dma_init();
  158. }
  159. else if (g_scsi_phy_mode == PHY_MODE_GREENPAK_DMA)
  160. {
  161. scsi_accel_greenpak_dma_init();
  162. }
  163. }
  164. /************************/
  165. /* SCSI bus phase logic */
  166. /************************/
  167. static SCSI_PHASE g_scsi_phase;
  168. extern "C" void scsiEnterPhase(int phase)
  169. {
  170. int delay = scsiEnterPhaseImmediate(phase);
  171. if (delay > 0)
  172. {
  173. s2s_delay_ns(delay);
  174. }
  175. }
  176. // Change state and return nanosecond delay to wait
  177. extern "C" uint32_t scsiEnterPhaseImmediate(int phase)
  178. {
  179. if (phase != g_scsi_phase)
  180. {
  181. // ANSI INCITS 362-2002 SPI-3 10.7.1:
  182. // Phase changes are not allowed while REQ or ACK is asserted.
  183. while (likely(!scsiDev.resetFlag) && SCSI_IN(ACK)) {}
  184. if (scsiDev.compatMode < COMPAT_SCSI2 && (phase == DATA_IN || phase == DATA_OUT))
  185. {
  186. // Akai S1000/S3000 seems to need extra delay before changing to data phase
  187. // after a command. The code in ZuluSCSI_disk.cpp tries to do this while waiting
  188. // for SD card, to avoid any extra latency.
  189. s2s_delay_ns(400000);
  190. }
  191. int oldphase = g_scsi_phase;
  192. g_scsi_phase = (SCSI_PHASE)phase;
  193. scsiLogPhaseChange(phase);
  194. if (phase < 0)
  195. {
  196. // Other communication on bus or reset state
  197. SCSI_RELEASE_OUTPUTS();
  198. return 0;
  199. }
  200. else
  201. {
  202. SCSI_OUT(MSG, phase & __scsiphase_msg);
  203. SCSI_OUT(CD, phase & __scsiphase_cd);
  204. SCSI_OUT(IO, phase & __scsiphase_io);
  205. int delayNs = 400; // Bus settle delay
  206. if ((oldphase & __scsiphase_io) != (phase & __scsiphase_io))
  207. {
  208. delayNs += 400; // Data release delay
  209. }
  210. if (scsiDev.compatMode < COMPAT_SCSI2)
  211. {
  212. // EMU EMAX needs 100uS ! 10uS is not enough.
  213. delayNs += 100000;
  214. }
  215. return delayNs;
  216. }
  217. }
  218. else
  219. {
  220. return 0;
  221. }
  222. }
  223. // Release all signals
  224. void scsiEnterBusFree(void)
  225. {
  226. g_scsi_phase = BUS_FREE;
  227. g_scsi_sts_selection = 0;
  228. g_scsi_ctrl_bsy = 0;
  229. scsiDev.cdbLen = 0;
  230. SCSI_RELEASE_OUTPUTS();
  231. }
  232. /********************/
  233. /* Transmit to host */
  234. /********************/
  235. #define SCSI_WAIT_ACTIVE(pin) \
  236. if (!SCSI_IN(pin)) { \
  237. if (!SCSI_IN(pin)) { \
  238. while(!SCSI_IN(pin) && !scsiDev.resetFlag); \
  239. } \
  240. }
  241. #define SCSI_WAIT_INACTIVE(pin) \
  242. if (SCSI_IN(pin)) { \
  243. if (SCSI_IN(pin)) { \
  244. while(SCSI_IN(pin) && !scsiDev.resetFlag); \
  245. } \
  246. }
  247. static inline void scsiWriteOneByte(uint8_t value)
  248. {
  249. SCSI_OUT_DATA(value);
  250. delay_100ns(); // DB setup time before REQ
  251. SCSI_OUT(REQ, 1);
  252. SCSI_WAIT_ACTIVE(ACK);
  253. SCSI_RELEASE_DATA_REQ(); // Release data and REQ
  254. SCSI_WAIT_INACTIVE(ACK);
  255. }
  256. extern "C" void scsiWriteByte(uint8_t value)
  257. {
  258. scsiLogDataIn(&value, 1);
  259. scsiWriteOneByte(value);
  260. }
  261. extern "C" void scsiWrite(const uint8_t* data, uint32_t count)
  262. {
  263. scsiStartWrite(data, count);
  264. scsiFinishWrite();
  265. }
  266. extern "C" void scsiStartWrite(const uint8_t* data, uint32_t count)
  267. {
  268. scsiLogDataIn(data, count);
  269. g_scsi_writereq.use_sync_mode = (g_scsi_phase == DATA_IN && scsiDev.target->syncOffset > 0);
  270. if (g_scsi_phy_mode == PHY_MODE_PIO
  271. || g_scsi_phy_mode == PHY_MODE_GREENPAK_PIO
  272. || g_scsi_writereq.use_sync_mode)
  273. {
  274. // Software based bit-banging.
  275. // Write requests are queued and then executed in isWriteFinished() callback.
  276. // This allows better parallelism with SD card transfers.
  277. if (g_scsi_writereq.count)
  278. {
  279. if (data == g_scsi_writereq.data + g_scsi_writereq.count)
  280. {
  281. // Combine with previous one
  282. g_scsi_writereq.count += count;
  283. return;
  284. }
  285. else
  286. {
  287. // Actually execute previous request
  288. scsiFinishWrite();
  289. }
  290. }
  291. g_scsi_writereq.data = data;
  292. g_scsi_writereq.count = count;
  293. }
  294. else if (g_scsi_phy_mode == PHY_MODE_DMA_TIMER || g_scsi_phy_mode == PHY_MODE_GREENPAK_DMA)
  295. {
  296. // Accelerated writes using DMA and timers
  297. scsi_accel_dma_startWrite(data, count, &scsiDev.resetFlag);
  298. }
  299. else
  300. {
  301. azlog("Unknown SCSI PHY mode: ", (int)g_scsi_phy_mode);
  302. }
  303. }
  304. static void processPollingWrite(uint32_t count)
  305. {
  306. if (count > g_scsi_writereq.count)
  307. count = g_scsi_writereq.count;
  308. const uint8_t *data = g_scsi_writereq.data;
  309. uint32_t count_words = count / 4;
  310. if (g_scsi_writereq.use_sync_mode)
  311. {
  312. // Synchronous mode transfer
  313. scsi_accel_sync_send(data, count, &scsiDev.resetFlag);
  314. }
  315. else if (count_words * 4 == count)
  316. {
  317. if (g_scsi_phy_mode == PHY_MODE_GREENPAK_PIO)
  318. {
  319. // GreenPAK PIO accelerated asynchronous transfer
  320. scsi_accel_greenpak_send((const uint32_t*)data, count_words, &scsiDev.resetFlag);
  321. }
  322. else
  323. {
  324. // Assembler optimized asynchronous transfer
  325. scsi_accel_asm_send((const uint32_t*)data, count_words, &scsiDev.resetFlag);
  326. }
  327. }
  328. else
  329. {
  330. // Use simple loop for unaligned transfers
  331. for (uint32_t i = 0; i < count; i++)
  332. {
  333. if (scsiDev.resetFlag) break;
  334. scsiWriteOneByte(data[i]);
  335. }
  336. }
  337. g_scsi_writereq.count -= count;
  338. if (g_scsi_writereq.count)
  339. {
  340. g_scsi_writereq.data += count;
  341. }
  342. else
  343. {
  344. g_scsi_writereq.data = NULL;
  345. }
  346. }
  347. static bool isPollingWriteFinished(const uint8_t *data)
  348. {
  349. if (g_scsi_writereq.count)
  350. {
  351. if (data == NULL)
  352. {
  353. return false;
  354. }
  355. else if (data >= g_scsi_writereq.data &&
  356. data < g_scsi_writereq.data + g_scsi_writereq.count)
  357. {
  358. return false;
  359. }
  360. }
  361. return true;
  362. }
  363. extern "C" bool scsiIsWriteFinished(const uint8_t *data)
  364. {
  365. // Check if there is still a polling transfer in progress
  366. if (!isPollingWriteFinished(data) && !check_sd_read_done())
  367. {
  368. // Process the transfer piece-by-piece while waiting
  369. // for SD card to react.
  370. int max_count = g_scsi_writereq.count / 8;
  371. // Always transfer whole sectors without pause to avoid problems with some SCSI hosts.
  372. int bytesPerSector = 512;
  373. if (scsiDev.target)
  374. {
  375. bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
  376. }
  377. if (max_count % bytesPerSector != 0) max_count -= (max_count % bytesPerSector);
  378. if (max_count < bytesPerSector) max_count = bytesPerSector;
  379. // Avoid SysTick interrupt pauses during the transfer
  380. SysTick_Handle_PreEmptively();
  381. processPollingWrite(max_count);
  382. return isPollingWriteFinished(data);
  383. }
  384. if (g_scsi_phy_mode == PHY_MODE_DMA_TIMER || g_scsi_phy_mode == PHY_MODE_GREENPAK_DMA)
  385. {
  386. return scsi_accel_dma_isWriteFinished(data);
  387. }
  388. else
  389. {
  390. return true;
  391. }
  392. }
  393. extern "C" void scsiFinishWrite()
  394. {
  395. if (g_scsi_writereq.count)
  396. {
  397. // Finish previously started polling write request.
  398. processPollingWrite(g_scsi_writereq.count);
  399. }
  400. if (g_scsi_phy_mode == PHY_MODE_DMA_TIMER || g_scsi_phy_mode == PHY_MODE_GREENPAK_DMA)
  401. {
  402. scsi_accel_dma_finishWrite(&scsiDev.resetFlag);
  403. }
  404. }
  405. /*********************/
  406. /* Receive from host */
  407. /*********************/
  408. static inline uint8_t scsiReadOneByte(void)
  409. {
  410. SCSI_OUT(REQ, 1);
  411. SCSI_WAIT_ACTIVE(ACK);
  412. delay_100ns();
  413. uint8_t r = SCSI_IN_DATA();
  414. SCSI_OUT(REQ, 0);
  415. SCSI_WAIT_INACTIVE(ACK);
  416. return r;
  417. }
  418. extern "C" uint8_t scsiReadByte(void)
  419. {
  420. uint8_t r = scsiReadOneByte();
  421. scsiLogDataOut(&r, 1);
  422. return r;
  423. }
  424. extern "C" void scsiRead(uint8_t* data, uint32_t count, int* parityError)
  425. {
  426. *parityError = 0;
  427. uint32_t count_words = count / 4;
  428. bool use_greenpak = (g_scsi_phy_mode == PHY_MODE_GREENPAK_DMA || g_scsi_phy_mode == PHY_MODE_GREENPAK_PIO);
  429. SysTick_Handle_PreEmptively();
  430. if (g_scsi_phase == DATA_OUT && scsiDev.target->syncOffset > 0)
  431. {
  432. // Synchronous data transfer
  433. scsi_accel_sync_recv(data, count, parityError, &scsiDev.resetFlag);
  434. }
  435. else if (count_words * 4 == count && count_words >= 2 && use_greenpak)
  436. {
  437. // GreenPAK accelerated receive can handle a multiple of 4 bytes with minimum of 8 bytes.
  438. scsi_accel_greenpak_recv((uint32_t*)data, count_words, &scsiDev.resetFlag);
  439. }
  440. else if (count_words * 4 == count && count_words >= 1)
  441. {
  442. // Optimized ASM subroutine can handle multiple of 4 bytes with minimum of 4 bytes.
  443. scsi_accel_asm_recv((uint32_t*)data, count_words, &scsiDev.resetFlag);
  444. }
  445. else
  446. {
  447. // Use a simple loop for short and unaligned transfers
  448. for (uint32_t i = 0; i < count; i++)
  449. {
  450. if (scsiDev.resetFlag) break;
  451. data[i] = scsiReadOneByte();
  452. }
  453. }
  454. scsiLogDataOut(data, count);
  455. }
  456. /**********************/
  457. /* Interrupt handlers */
  458. /**********************/
  459. extern "C"
  460. void SCSI_RST_IRQ (void)
  461. {
  462. if (exti_interrupt_flag_get(SCSI_RST_EXTI))
  463. {
  464. exti_interrupt_flag_clear(SCSI_RST_EXTI);
  465. scsi_rst_assert_interrupt();
  466. }
  467. if (exti_interrupt_flag_get(SCSI_BSY_EXTI))
  468. {
  469. exti_interrupt_flag_clear(SCSI_BSY_EXTI);
  470. scsi_bsy_deassert_interrupt();
  471. }
  472. if (exti_interrupt_flag_get(SCSI_SEL_EXTI))
  473. {
  474. // Check BSY line status when SEL goes active.
  475. // This is needed to handle SCSI-1 hosts that use the single initiator mode.
  476. // The host will just assert the SEL directly, without asserting BSY first.
  477. exti_interrupt_flag_clear(SCSI_SEL_EXTI);
  478. scsi_bsy_deassert_interrupt();
  479. }
  480. }
  481. #if SCSI_RST_IRQn != SCSI_BSY_IRQn
  482. extern "C"
  483. void SCSI_BSY_IRQ (void)
  484. {
  485. SCSI_RST_IRQ();
  486. }
  487. #endif
  488. #if (SCSI_SEL_IRQn != SCSI_RST_IRQn) && (SCSI_SEL_IRQn != SCSI_BSY_IRQn)
  489. extern "C"
  490. void SCSI_SEL_IRQ (void)
  491. {
  492. SCSI_RST_IRQ();
  493. }
  494. #endif
  495. static void init_irqs()
  496. {
  497. // Falling edge of RST pin
  498. gpio_exti_source_select(SCSI_RST_EXTI_SOURCE_PORT, SCSI_RST_EXTI_SOURCE_PIN);
  499. exti_init(SCSI_RST_EXTI, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
  500. NVIC_SetPriority(SCSI_RST_IRQn, 1);
  501. NVIC_EnableIRQ(SCSI_RST_IRQn);
  502. // Rising edge of BSY pin
  503. gpio_exti_source_select(SCSI_BSY_EXTI_SOURCE_PORT, SCSI_BSY_EXTI_SOURCE_PIN);
  504. exti_init(SCSI_BSY_EXTI, EXTI_INTERRUPT, EXTI_TRIG_RISING);
  505. NVIC_SetPriority(SCSI_BSY_IRQn, 1);
  506. NVIC_EnableIRQ(SCSI_BSY_IRQn);
  507. // Falling edge of SEL pin
  508. gpio_exti_source_select(SCSI_SEL_EXTI_SOURCE_PORT, SCSI_SEL_EXTI_SOURCE_PIN);
  509. exti_init(SCSI_SEL_EXTI, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
  510. NVIC_SetPriority(SCSI_SEL_IRQn, 1);
  511. NVIC_EnableIRQ(SCSI_SEL_IRQn);
  512. }