scsiPhy.cpp 14 KB

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