scsiPhy.cpp 17 KB

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