scsiPhy.cpp 11 KB

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