scsiPhy.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // Implements the low level interface to SCSI bus
  2. // Partially derived from scsiPhy.c from SCSI2SD-V6
  3. #include "scsiPhy.h"
  4. #include "ZuluSCSI_platform.h"
  5. #include "ZuluSCSI_log.h"
  6. #include "ZuluSCSI_log_trace.h"
  7. #include "ZuluSCSI_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. azdbg("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. }
  97. /************************/
  98. /* SCSI bus phase logic */
  99. /************************/
  100. static SCSI_PHASE g_scsi_phase;
  101. extern "C" void scsiEnterPhase(int phase)
  102. {
  103. int delay = scsiEnterPhaseImmediate(phase);
  104. if (delay > 0)
  105. {
  106. s2s_delay_ns(delay);
  107. }
  108. }
  109. // Change state and return nanosecond delay to wait
  110. extern "C" uint32_t scsiEnterPhaseImmediate(int phase)
  111. {
  112. // ANSI INCITS 362-2002 SPI-3 10.7.1:
  113. // Phase changes are not allowed while REQ or ACK is asserted.
  114. while (likely(!scsiDev.resetFlag) && SCSI_IN(ACK)) {}
  115. if (phase != g_scsi_phase)
  116. {
  117. int oldphase = g_scsi_phase;
  118. g_scsi_phase = (SCSI_PHASE)phase;
  119. scsiLogPhaseChange(phase);
  120. if (phase < 0)
  121. {
  122. // Other communication on bus or reset state
  123. SCSI_RELEASE_OUTPUTS();
  124. return 0;
  125. }
  126. else
  127. {
  128. SCSI_OUT(MSG, phase & __scsiphase_msg);
  129. SCSI_OUT(CD, phase & __scsiphase_cd);
  130. SCSI_OUT(IO, phase & __scsiphase_io);
  131. int delayNs = 400; // Bus settle delay
  132. if ((oldphase & __scsiphase_io) != (phase & __scsiphase_io))
  133. {
  134. delayNs += 400; // Data release delay
  135. }
  136. if (scsiDev.compatMode < COMPAT_SCSI2)
  137. {
  138. // EMU EMAX needs 100uS ! 10uS is not enough.
  139. delayNs += 100000;
  140. }
  141. return delayNs;
  142. }
  143. }
  144. else
  145. {
  146. return 0;
  147. }
  148. }
  149. // Release all signals
  150. void scsiEnterBusFree(void)
  151. {
  152. g_scsi_phase = BUS_FREE;
  153. g_scsi_sts_selection = 0;
  154. g_scsi_ctrl_bsy = 0;
  155. scsiDev.cdbLen = 0;
  156. SCSI_RELEASE_OUTPUTS();
  157. }
  158. /********************/
  159. /* Transmit to host */
  160. /********************/
  161. #define SCSI_WAIT_ACTIVE(pin) \
  162. if (!SCSI_IN(pin)) { \
  163. if (!SCSI_IN(pin)) { \
  164. while(!SCSI_IN(pin) && !scsiDev.resetFlag); \
  165. } \
  166. }
  167. #define SCSI_WAIT_INACTIVE(pin) \
  168. if (SCSI_IN(pin)) { \
  169. if (SCSI_IN(pin)) { \
  170. while(SCSI_IN(pin) && !scsiDev.resetFlag); \
  171. } \
  172. }
  173. // Write one byte to SCSI host using the handshake mechanism
  174. static inline void scsiWriteOneByte(uint8_t value)
  175. {
  176. SCSI_OUT_DATA(value);
  177. delay_100ns(); // DB setup time before REQ
  178. SCSI_OUT(REQ, 1);
  179. SCSI_WAIT_ACTIVE(ACK);
  180. SCSI_RELEASE_DATA_REQ();
  181. SCSI_WAIT_INACTIVE(ACK);
  182. }
  183. extern "C" void scsiWriteByte(uint8_t value)
  184. {
  185. scsiLogDataIn(&value, 1);
  186. scsiWriteOneByte(value);
  187. }
  188. extern "C" void scsiWrite(const uint8_t* data, uint32_t count)
  189. {
  190. scsiLogDataIn(data, count);
  191. for (uint32_t i = 0; i < count; i++)
  192. {
  193. if (scsiDev.resetFlag) break;
  194. scsiWriteOneByte(data[i]);
  195. }
  196. }
  197. extern "C" void scsiStartWrite(const uint8_t* data, uint32_t count)
  198. {
  199. // If the platform supports DMA for either SD card access or for SCSI bus,
  200. // this function can be used to execute SD card transfers in parallel with
  201. // SCSI transfers. This usually doubles the transfer speed.
  202. //
  203. // For simplicity, this example only implements blocking writes.
  204. scsiWrite(data, count);
  205. }
  206. extern "C" bool scsiIsWriteFinished(const uint8_t *data)
  207. {
  208. // Asynchronous writes are not implemented in this example.
  209. return true;
  210. }
  211. extern "C" void scsiFinishWrite()
  212. {
  213. // Asynchronous writes are not implemented in this example.
  214. }
  215. /*********************/
  216. /* Receive from host */
  217. /*********************/
  218. // Read one byte from SCSI host using the handshake mechanism.
  219. static inline uint8_t scsiReadOneByte(void)
  220. {
  221. SCSI_OUT(REQ, 1);
  222. SCSI_WAIT_ACTIVE(ACK);
  223. delay_100ns();
  224. uint8_t r = SCSI_IN_DATA();
  225. SCSI_OUT(REQ, 0);
  226. SCSI_WAIT_INACTIVE(ACK);
  227. return r;
  228. }
  229. extern "C" uint8_t scsiReadByte(void)
  230. {
  231. uint8_t r = scsiReadOneByte();
  232. scsiLogDataOut(&r, 1);
  233. return r;
  234. }
  235. extern "C" void scsiRead(uint8_t* data, uint32_t count, int* parityError)
  236. {
  237. *parityError = 0;
  238. for (uint32_t i = 0; i < count; i++)
  239. {
  240. if (scsiDev.resetFlag) break;
  241. data[i] = scsiReadOneByte();
  242. }
  243. scsiLogDataOut(data, count);
  244. }