scsiPhy.cpp 14 KB

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