scsiPhy.cpp 16 KB

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