scsiPhy.cpp 14 KB

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