scsiHostPhy.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #include "scsiHostPhy.h"
  2. #include "BlueSCSI_platform.h"
  3. #include "BlueSCSI_log.h"
  4. #include "BlueSCSI_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. debuglog("scsiHostPhySelect: bus is busy");
  40. scsiLogInitiatorPhaseChange(BUS_FREE);
  41. SCSI_RELEASE_OUTPUTS();
  42. return false;
  43. }
  44. }
  45. // Choose initiator ID different than target ID
  46. uint8_t initiator_id = (target_id == 7) ? 0 : 7;
  47. // Selection phase
  48. scsiLogInitiatorPhaseChange(SELECTION);
  49. debuglog("------ SELECTING ", target_id, " with initiator ID ", (int)initiator_id);
  50. SCSI_OUT(SEL, 1);
  51. delayMicroseconds(5);
  52. SCSI_OUT_DATA((1 << target_id) | (1 << initiator_id));
  53. delayMicroseconds(5);
  54. SCSI_OUT(BSY, 0);
  55. // Wait for target to respond
  56. for (int wait = 0; wait < 2500; wait++)
  57. {
  58. delayMicroseconds(100);
  59. if (SCSI_IN(BSY))
  60. {
  61. break;
  62. }
  63. }
  64. if (!SCSI_IN(BSY))
  65. {
  66. // No response
  67. SCSI_RELEASE_OUTPUTS();
  68. return false;
  69. }
  70. // We need to assert OUT_BSY to enable IO buffer U105 to read status signals.
  71. SCSI_RELEASE_DATA_REQ();
  72. SCSI_OUT(BSY, 1);
  73. SCSI_OUT(SEL, 0);
  74. return true;
  75. }
  76. // Read the current communication phase as signaled by the target
  77. int scsiHostPhyGetPhase()
  78. {
  79. static absolute_time_t last_online_time;
  80. if (g_scsiHostPhyReset)
  81. {
  82. // Reset request from watchdog timer
  83. scsiHostPhyRelease();
  84. return BUS_FREE;
  85. }
  86. int phase = 0;
  87. bool req_in = SCSI_IN(REQ);
  88. if (SCSI_IN(CD)) phase |= __scsiphase_cd;
  89. if (SCSI_IN(IO)) phase |= __scsiphase_io;
  90. if (SCSI_IN(MSG)) phase |= __scsiphase_msg;
  91. if (phase == 0 && absolute_time_diff_us(last_online_time, get_absolute_time()) > 100)
  92. {
  93. // Disable OUT_BSY for a short time to see if the target is still on line
  94. SCSI_OUT(BSY, 0);
  95. delayMicroseconds(1);
  96. if (!SCSI_IN(BSY))
  97. {
  98. scsiLogInitiatorPhaseChange(BUS_FREE);
  99. return BUS_FREE;
  100. }
  101. // Still online, re-enable OUT_BSY to enable IO buffers
  102. SCSI_OUT(BSY, 1);
  103. last_online_time = get_absolute_time();
  104. }
  105. else if (phase != 0)
  106. {
  107. last_online_time = get_absolute_time();
  108. }
  109. if (!req_in)
  110. {
  111. // Don't act on phase changes until target asserts request signal.
  112. // This filters out any spurious changes on control signals.
  113. return BUS_BUSY;
  114. }
  115. else
  116. {
  117. scsiLogInitiatorPhaseChange(phase);
  118. return phase;
  119. }
  120. }
  121. bool scsiHostRequestWaiting()
  122. {
  123. return SCSI_IN(REQ);
  124. }
  125. // Blocking data transfer
  126. #define SCSIHOST_WAIT_ACTIVE(pin) \
  127. if (!SCSI_IN(pin)) { \
  128. if (!SCSI_IN(pin)) { \
  129. while(!SCSI_IN(pin) && !g_scsiHostPhyReset); \
  130. } \
  131. }
  132. #define SCSIHOST_WAIT_INACTIVE(pin) \
  133. if (SCSI_IN(pin)) { \
  134. if (SCSI_IN(pin)) { \
  135. while(SCSI_IN(pin) && !g_scsiHostPhyReset); \
  136. } \
  137. }
  138. // Write one byte to SCSI target using the handshake mechanism
  139. static inline void scsiHostWriteOneByte(uint8_t value)
  140. {
  141. SCSIHOST_WAIT_ACTIVE(REQ);
  142. SCSI_OUT_DATA(value);
  143. delay_100ns(); // DB setup time before ACK
  144. SCSI_OUT(ACK, 1);
  145. SCSIHOST_WAIT_INACTIVE(REQ);
  146. SCSI_RELEASE_DATA_REQ();
  147. SCSI_OUT(ACK, 0);
  148. }
  149. // Read one byte from SCSI target using the handshake mechanism.
  150. static inline uint8_t scsiHostReadOneByte(int* parityError)
  151. {
  152. SCSIHOST_WAIT_ACTIVE(REQ);
  153. uint16_t r = SCSI_IN_DATA();
  154. SCSI_OUT(ACK, 1);
  155. SCSIHOST_WAIT_INACTIVE(REQ);
  156. SCSI_OUT(ACK, 0);
  157. if (parityError && r != (g_scsi_parity_lookup[r & 0xFF] ^ SCSI_IO_DATA_MASK))
  158. {
  159. log("Parity error in scsiReadOneByte(): ", (uint32_t)r);
  160. *parityError = 1;
  161. }
  162. return (uint8_t)r;
  163. }
  164. uint32_t scsiHostWrite(const uint8_t *data, uint32_t count)
  165. {
  166. scsiLogDataOut(data, count);
  167. int cd_start = SCSI_IN(CD);
  168. int msg_start = SCSI_IN(MSG);
  169. for (uint32_t i = 0; i < count; i++)
  170. {
  171. while (!SCSI_IN(REQ))
  172. {
  173. if (g_scsiHostPhyReset || SCSI_IN(IO) || SCSI_IN(CD) != cd_start || SCSI_IN(MSG) != msg_start)
  174. {
  175. // Target switched out of DATA_OUT mode
  176. log("scsiHostWrite: sent ", (int)i, " bytes, expected ", (int)count);
  177. return i;
  178. }
  179. }
  180. scsiHostWriteOneByte(data[i]);
  181. }
  182. return count;
  183. }
  184. uint32_t scsiHostRead(uint8_t *data, uint32_t count)
  185. {
  186. int parityError = 0;
  187. uint32_t fullcount = count;
  188. int cd_start = SCSI_IN(CD);
  189. int msg_start = SCSI_IN(MSG);
  190. if ((count & 1) == 0 && ((uint32_t)data & 1) == 0)
  191. {
  192. // Even number of bytes, use accelerated routine
  193. count = scsi_accel_host_read(data, count, &parityError, &g_scsiHostPhyReset);
  194. }
  195. else
  196. {
  197. for (uint32_t i = 0; i < count; i++)
  198. {
  199. while (!SCSI_IN(REQ))
  200. {
  201. if (g_scsiHostPhyReset || !SCSI_IN(IO) || SCSI_IN(CD) != cd_start || SCSI_IN(MSG) != msg_start)
  202. {
  203. // Target switched out of DATA_IN mode
  204. count = i;
  205. }
  206. }
  207. data[i] = scsiHostReadOneByte(&parityError);
  208. }
  209. }
  210. scsiLogDataIn(data, count);
  211. if (g_scsiHostPhyReset || parityError)
  212. {
  213. return 0;
  214. }
  215. else
  216. {
  217. if (count < fullcount)
  218. {
  219. log("scsiHostRead: received ", (int)count, " bytes, expected ", (int)fullcount);
  220. }
  221. return count;
  222. }
  223. }
  224. // Release all bus signals
  225. void scsiHostPhyRelease()
  226. {
  227. scsiLogInitiatorPhaseChange(BUS_FREE);
  228. SCSI_RELEASE_OUTPUTS();
  229. }