scsiPhy.cpp 15 KB

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