scsiPhy.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // Implements the low level interface to SCSI bus
  2. // Partially derived from scsiPhy.c from SCSI2SD-V6
  3. #include "scsiPhy.h"
  4. #include "BlueSCSI_platform.h"
  5. #include "BlueSCSI_log.h"
  6. #include "BlueSCSI_log_trace.h"
  7. #include "BlueSCSI_config.h"
  8. #include <scsi2sd.h>
  9. extern "C" {
  10. #include <scsi.h>
  11. #include <scsi2sd_time.h>
  12. }
  13. /***********************/
  14. /* SCSI status signals */
  15. /***********************/
  16. extern "C" bool scsiStatusATN()
  17. {
  18. return SCSI_IN(ATN);
  19. }
  20. extern "C" bool scsiStatusBSY()
  21. {
  22. return SCSI_IN(BSY);
  23. }
  24. /************************/
  25. /* SCSI selection logic */
  26. /************************/
  27. volatile uint8_t g_scsi_sts_selection;
  28. volatile uint8_t g_scsi_ctrl_bsy;
  29. void scsi_bsy_deassert_interrupt()
  30. {
  31. if (SCSI_IN(SEL) && !SCSI_IN(BSY))
  32. {
  33. // Check if any of the targets we simulate is selected
  34. uint8_t sel_bits = SCSI_IN_DATA();
  35. int sel_id = -1;
  36. for (int i = 0; i < S2S_MAX_TARGETS; i++)
  37. {
  38. if (scsiDev.targets[i].targetId <= 7 && scsiDev.targets[i].cfg)
  39. {
  40. if (sel_bits & (1 << scsiDev.targets[i].targetId))
  41. {
  42. sel_id = scsiDev.targets[i].targetId;
  43. break;
  44. }
  45. }
  46. }
  47. if (sel_id >= 0)
  48. {
  49. uint8_t atn_flag = SCSI_IN(ATN) ? SCSI_STS_SELECTION_ATN : 0;
  50. g_scsi_sts_selection = SCSI_STS_SELECTION_SUCCEEDED | atn_flag | sel_id;
  51. }
  52. // selFlag is required for Philips P2000C which releases it after 600ns
  53. // without waiting for BSY.
  54. // Also required for some early Mac Plus roms
  55. scsiDev.selFlag = *SCSI_STS_SELECTED;
  56. }
  57. }
  58. extern "C" bool scsiStatusSEL()
  59. {
  60. if (g_scsi_ctrl_bsy)
  61. {
  62. // We don't have direct register access to BSY bit like SCSI2SD scsi.c expects.
  63. // Instead update the state here.
  64. // Releasing happens with bus release.
  65. g_scsi_ctrl_bsy = 0;
  66. SCSI_OUT(BSY, 1);
  67. }
  68. return SCSI_IN(SEL);
  69. }
  70. /************************/
  71. /* SCSI bus reset logic */
  72. /************************/
  73. static void scsi_rst_assert_interrupt()
  74. {
  75. // Glitch filtering
  76. bool rst1 = SCSI_IN(RST);
  77. delay_ns(500);
  78. bool rst2 = SCSI_IN(RST);
  79. if (rst1 && rst2)
  80. {
  81. debuglog("BUS RESET");
  82. scsiDev.resetFlag = 1;
  83. }
  84. }
  85. // This function is called to initialize the phy code.
  86. // It is called after power-on and after SCSI bus reset.
  87. extern "C" void scsiPhyReset(void)
  88. {
  89. SCSI_RELEASE_OUTPUTS();
  90. g_scsi_sts_selection = 0;
  91. g_scsi_ctrl_bsy = 0;
  92. /* Implement here code to enable two interrupts:
  93. * scsi_bsy_deassert_interrupt() on rising edge of BSY pin
  94. * scsi_rst_assert_interrupt() on falling edge of RST pin
  95. *
  96. * For SCSI-1 single-initiator support, also call:
  97. * scsi_bsy_deassert_interrupt() on falling edge of SEL pin
  98. */
  99. }
  100. /************************/
  101. /* SCSI bus phase logic */
  102. /************************/
  103. static SCSI_PHASE g_scsi_phase;
  104. extern "C" void scsiEnterPhase(int phase)
  105. {
  106. int delay = scsiEnterPhaseImmediate(phase);
  107. if (delay > 0)
  108. {
  109. s2s_delay_ns(delay);
  110. }
  111. }
  112. // Change state and return nanosecond delay to wait
  113. extern "C" uint32_t scsiEnterPhaseImmediate(int phase)
  114. {
  115. if (phase != g_scsi_phase)
  116. {
  117. // ANSI INCITS 362-2002 SPI-3 10.7.1:
  118. // Phase changes are not allowed while REQ or ACK is asserted.
  119. while (likely(!scsiDev.resetFlag) && SCSI_IN(ACK)) {}
  120. if (scsiDev.compatMode < COMPAT_SCSI2 && (phase == DATA_IN || phase == DATA_OUT))
  121. {
  122. // Akai S1000/S3000 seems to need extra delay before changing to data phase
  123. // after a command. The code in BlueSCSI_disk.cpp tries to do this while waiting
  124. // for SD card, to avoid any extra latency.
  125. s2s_delay_ns(400000);
  126. }
  127. int oldphase = g_scsi_phase;
  128. g_scsi_phase = (SCSI_PHASE)phase;
  129. scsiLogPhaseChange(phase);
  130. if (phase < 0)
  131. {
  132. // Other communication on bus or reset state
  133. SCSI_RELEASE_OUTPUTS();
  134. return 0;
  135. }
  136. else
  137. {
  138. SCSI_OUT(MSG, phase & __scsiphase_msg);
  139. SCSI_OUT(CD, phase & __scsiphase_cd);
  140. SCSI_OUT(IO, phase & __scsiphase_io);
  141. int delayNs = 400; // Bus settle delay
  142. if ((oldphase & __scsiphase_io) != (phase & __scsiphase_io))
  143. {
  144. delayNs += 400; // Data release delay
  145. }
  146. if (scsiDev.compatMode < COMPAT_SCSI2)
  147. {
  148. // EMU EMAX needs 100uS ! 10uS is not enough.
  149. delayNs += 100000;
  150. }
  151. return delayNs;
  152. }
  153. }
  154. else
  155. {
  156. return 0;
  157. }
  158. }
  159. // Release all signals
  160. void scsiEnterBusFree(void)
  161. {
  162. g_scsi_phase = BUS_FREE;
  163. g_scsi_sts_selection = 0;
  164. g_scsi_ctrl_bsy = 0;
  165. scsiDev.cdbLen = 0;
  166. SCSI_RELEASE_OUTPUTS();
  167. }
  168. /********************/
  169. /* Transmit to host */
  170. /********************/
  171. #define SCSI_WAIT_ACTIVE(pin) \
  172. if (!SCSI_IN(pin)) { \
  173. if (!SCSI_IN(pin)) { \
  174. while(!SCSI_IN(pin) && !scsiDev.resetFlag); \
  175. } \
  176. }
  177. #define SCSI_WAIT_INACTIVE(pin) \
  178. if (SCSI_IN(pin)) { \
  179. if (SCSI_IN(pin)) { \
  180. while(SCSI_IN(pin) && !scsiDev.resetFlag); \
  181. } \
  182. }
  183. // Write one byte to SCSI host using the handshake mechanism
  184. static inline void scsiWriteOneByte(uint8_t value)
  185. {
  186. SCSI_OUT_DATA(value);
  187. delay_100ns(); // DB setup time before REQ
  188. SCSI_OUT(REQ, 1);
  189. SCSI_WAIT_ACTIVE(ACK);
  190. SCSI_RELEASE_DATA_REQ();
  191. SCSI_WAIT_INACTIVE(ACK);
  192. }
  193. extern "C" void scsiWriteByte(uint8_t value)
  194. {
  195. scsiLogDataIn(&value, 1);
  196. scsiWriteOneByte(value);
  197. }
  198. extern "C" void scsiWrite(const uint8_t* data, uint32_t count)
  199. {
  200. scsiLogDataIn(data, count);
  201. for (uint32_t i = 0; i < count; i++)
  202. {
  203. if (scsiDev.resetFlag) break;
  204. scsiWriteOneByte(data[i]);
  205. }
  206. }
  207. extern "C" void scsiStartWrite(const uint8_t* data, uint32_t count)
  208. {
  209. // If the platform supports DMA for either SD card access or for SCSI bus,
  210. // this function can be used to execute SD card transfers in parallel with
  211. // SCSI transfers. This usually doubles the transfer speed.
  212. //
  213. // For simplicity, this example only implements blocking writes.
  214. scsiWrite(data, count);
  215. }
  216. extern "C" bool scsiIsWriteFinished(const uint8_t *data)
  217. {
  218. // Asynchronous writes are not implemented in this example.
  219. return true;
  220. }
  221. extern "C" void scsiFinishWrite()
  222. {
  223. // Asynchronous writes are not implemented in this example.
  224. }
  225. /*********************/
  226. /* Receive from host */
  227. /*********************/
  228. // Read one byte from SCSI host using the handshake mechanism.
  229. static inline uint8_t scsiReadOneByte(void)
  230. {
  231. SCSI_OUT(REQ, 1);
  232. SCSI_WAIT_ACTIVE(ACK);
  233. delay_100ns();
  234. uint8_t r = SCSI_IN_DATA();
  235. SCSI_OUT(REQ, 0);
  236. SCSI_WAIT_INACTIVE(ACK);
  237. return r;
  238. }
  239. extern "C" uint8_t scsiReadByte(void)
  240. {
  241. uint8_t r = scsiReadOneByte();
  242. scsiLogDataOut(&r, 1);
  243. return r;
  244. }
  245. extern "C" void scsiRead(uint8_t* data, uint32_t count, int* parityError)
  246. {
  247. *parityError = 0;
  248. for (uint32_t i = 0; i < count; i++)
  249. {
  250. if (scsiDev.resetFlag) break;
  251. data[i] = scsiReadOneByte();
  252. }
  253. scsiLogDataOut(data, count);
  254. }