scsiHostPhy.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include "scsiHostPhy.h"
  2. #include "ZuluSCSI_platform.h"
  3. #include "ZuluSCSI_log.h"
  4. #include "ZuluSCSI_log_trace.h"
  5. #include "scsi_accel_host.h"
  6. #include <assert.h>
  7. #include <scsi2sd.h>
  8. extern "C" {
  9. #include <scsi.h>
  10. }
  11. volatile int g_scsiHostPhyReset;
  12. // Release bus and pulse RST signal, initialize PHY to host mode.
  13. void scsiHostPhyReset(void)
  14. {
  15. SCSI_RELEASE_OUTPUTS();
  16. SCSI_ENABLE_INITIATOR();
  17. scsi_accel_host_init();
  18. SCSI_OUT(RST, 1);
  19. delay(2);
  20. SCSI_OUT(RST, 0);
  21. delay(250);
  22. g_scsiHostPhyReset = false;
  23. }
  24. // Select a device, id 0-7.
  25. // Returns true if the target answers to selection request.
  26. bool scsiHostPhySelect(int target_id)
  27. {
  28. SCSI_RELEASE_OUTPUTS();
  29. // We can't write individual data bus bits, so use a bit modified
  30. // arbitration scheme. We always yield to any other initiator on
  31. // the bus.
  32. scsiLogInitiatorPhaseChange(BUS_BUSY);
  33. SCSI_OUT(BSY, 1);
  34. for (int wait = 0; wait < 10; wait++)
  35. {
  36. delayMicroseconds(1);
  37. if (SCSI_IN_DATA() != 0)
  38. {
  39. azdbg("scsiHostPhySelect: bus is busy");
  40. scsiLogInitiatorPhaseChange(BUS_FREE);
  41. SCSI_RELEASE_OUTPUTS();
  42. return false;
  43. }
  44. }
  45. // Selection phase
  46. scsiLogInitiatorPhaseChange(SELECTION);
  47. azdbg("------ SELECTING ", target_id);
  48. SCSI_OUT(SEL, 1);
  49. delayMicroseconds(5);
  50. SCSI_OUT_DATA(1 << target_id);
  51. delayMicroseconds(5);
  52. SCSI_OUT(BSY, 0);
  53. // Wait for target to respond
  54. for (int wait = 0; wait < 2500; wait++)
  55. {
  56. delayMicroseconds(100);
  57. if (SCSI_IN(BSY))
  58. {
  59. break;
  60. }
  61. }
  62. if (!SCSI_IN(BSY))
  63. {
  64. // No response
  65. SCSI_RELEASE_OUTPUTS();
  66. return false;
  67. }
  68. // We need to assert OUT_BSY to enable IO buffer U105 to read status signals.
  69. SCSI_RELEASE_DATA_REQ();
  70. SCSI_OUT(BSY, 1);
  71. SCSI_OUT(SEL, 0);
  72. return true;
  73. }
  74. // Read the current communication phase as signaled by the target
  75. int scsiHostPhyGetPhase()
  76. {
  77. static absolute_time_t last_online_time;
  78. if (g_scsiHostPhyReset)
  79. {
  80. // Reset request from watchdog timer
  81. scsiHostPhyRelease();
  82. return BUS_FREE;
  83. }
  84. int phase = 0;
  85. bool req_in = SCSI_IN(REQ);
  86. if (SCSI_IN(CD)) phase |= __scsiphase_cd;
  87. if (SCSI_IN(IO)) phase |= __scsiphase_io;
  88. if (SCSI_IN(MSG)) phase |= __scsiphase_msg;
  89. if (phase == 0 && absolute_time_diff_us(last_online_time, get_absolute_time()) > 100)
  90. {
  91. // Disable OUT_BSY for a short time to see if the target is still on line
  92. SCSI_OUT(BSY, 0);
  93. delayMicroseconds(1);
  94. if (!SCSI_IN(BSY))
  95. {
  96. scsiLogInitiatorPhaseChange(BUS_FREE);
  97. return BUS_FREE;
  98. }
  99. // Still online, re-enable OUT_BSY to enable IO buffers
  100. SCSI_OUT(BSY, 1);
  101. last_online_time = get_absolute_time();
  102. }
  103. else if (phase != 0)
  104. {
  105. last_online_time = get_absolute_time();
  106. }
  107. if (!req_in)
  108. {
  109. // Don't act on phase changes until target asserts request signal.
  110. // This filters out any spurious changes on control signals.
  111. return BUS_BUSY;
  112. }
  113. else
  114. {
  115. scsiLogInitiatorPhaseChange(phase);
  116. return phase;
  117. }
  118. }
  119. bool scsiHostRequestWaiting()
  120. {
  121. return SCSI_IN(REQ);
  122. }
  123. // Blocking data transfer
  124. #define SCSIHOST_WAIT_ACTIVE(pin) \
  125. if (!SCSI_IN(pin)) { \
  126. if (!SCSI_IN(pin)) { \
  127. while(!SCSI_IN(pin) && !g_scsiHostPhyReset); \
  128. } \
  129. }
  130. #define SCSIHOST_WAIT_INACTIVE(pin) \
  131. if (SCSI_IN(pin)) { \
  132. if (SCSI_IN(pin)) { \
  133. while(SCSI_IN(pin) && !g_scsiHostPhyReset); \
  134. } \
  135. }
  136. // Write one byte to SCSI target using the handshake mechanism
  137. static inline void scsiHostWriteOneByte(uint8_t value)
  138. {
  139. SCSIHOST_WAIT_ACTIVE(REQ);
  140. SCSI_OUT_DATA(value);
  141. delay_100ns(); // DB setup time before ACK
  142. SCSI_OUT(ACK, 1);
  143. SCSIHOST_WAIT_INACTIVE(REQ);
  144. SCSI_RELEASE_DATA_REQ();
  145. SCSI_OUT(ACK, 0);
  146. }
  147. // Read one byte from SCSI target using the handshake mechanism.
  148. static inline uint8_t scsiHostReadOneByte(int* parityError)
  149. {
  150. SCSIHOST_WAIT_ACTIVE(REQ);
  151. uint16_t r = SCSI_IN_DATA();
  152. SCSI_OUT(ACK, 1);
  153. SCSIHOST_WAIT_INACTIVE(REQ);
  154. SCSI_OUT(ACK, 0);
  155. if (parityError && r != (g_scsi_parity_lookup[r & 0xFF] ^ SCSI_IO_DATA_MASK))
  156. {
  157. azlog("Parity error in scsiReadOneByte(): ", (uint32_t)r);
  158. *parityError = 1;
  159. }
  160. return (uint8_t)r;
  161. }
  162. bool scsiHostWrite(const uint8_t *data, uint32_t count)
  163. {
  164. scsiLogDataOut(data, count);
  165. for (uint32_t i = 0; i < count; i++)
  166. {
  167. if (g_scsiHostPhyReset) return false;
  168. scsiHostWriteOneByte(data[i]);
  169. }
  170. return true;
  171. }
  172. bool scsiHostRead(uint8_t *data, uint32_t count)
  173. {
  174. int parityError = 0;
  175. if ((count & 1) == 0 && ((uint32_t)data & 1) == 0)
  176. {
  177. // Even number of bytes, use accelerated routine
  178. scsi_accel_host_read(data, count, &parityError, &g_scsiHostPhyReset);
  179. }
  180. else
  181. {
  182. for (uint32_t i = 0; i < count; i++)
  183. {
  184. if (g_scsiHostPhyReset) return false;
  185. data[i] = scsiHostReadOneByte(&parityError);
  186. }
  187. }
  188. if (parityError || g_scsiHostPhyReset)
  189. {
  190. return false;
  191. }
  192. else
  193. {
  194. scsiLogDataIn(data, count);
  195. return true;
  196. }
  197. }
  198. // Release all bus signals
  199. void scsiHostPhyRelease()
  200. {
  201. scsiLogInitiatorPhaseChange(BUS_FREE);
  202. SCSI_RELEASE_OUTPUTS();
  203. }