scsiPhy.cpp 17 KB

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