scsiPhy.cpp 12 KB

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