scsiHostPhy.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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_ENABLE_INITIATOR();
  29. SCSI_RELEASE_OUTPUTS();
  30. // We can't write individual data bus bits, so use a bit modified
  31. // arbitration scheme. We always yield to any other initiator on
  32. // the bus.
  33. scsiLogInitiatorPhaseChange(BUS_BUSY);
  34. SCSI_OUT(REQ, 0);
  35. SCSI_OUT(BSY, 1);
  36. for (int wait = 0; wait < 10; wait++)
  37. {
  38. delayMicroseconds(1);
  39. if (SCSI_IN_DATA() != 0)
  40. {
  41. debuglog("scsiHostPhySelect: bus is busy");
  42. scsiLogInitiatorPhaseChange(BUS_FREE);
  43. SCSI_RELEASE_OUTPUTS();
  44. return false;
  45. }
  46. }
  47. // Choose initiator ID different than target ID
  48. uint8_t initiator_id = (target_id == 7) ? 0 : 7;
  49. // Selection phase
  50. scsiLogInitiatorPhaseChange(SELECTION);
  51. debuglog("------ SELECTING ", target_id, " with initiator ID ", (int)initiator_id);
  52. SCSI_OUT(SEL, 1);
  53. delayMicroseconds(5);
  54. SCSI_OUT_DATA((1 << target_id) | (1 << initiator_id));
  55. delayMicroseconds(5);
  56. SCSI_OUT(BSY, 0);
  57. // Wait for target to respond
  58. for (int wait = 0; wait < 2500; wait++)
  59. {
  60. delayMicroseconds(100);
  61. if (SCSI_IN(BSY))
  62. {
  63. break;
  64. }
  65. }
  66. if (!SCSI_IN(BSY))
  67. {
  68. // No response
  69. SCSI_RELEASE_OUTPUTS();
  70. return false;
  71. }
  72. SCSI_RELEASE_DATA_REQ();
  73. SCSI_OUT(SEL, 0);
  74. SCSI_ENABLE_INITIATOR();
  75. return true;
  76. }
  77. // Read the current communication phase as signaled by the target
  78. int scsiHostPhyGetPhase()
  79. {
  80. static absolute_time_t last_online_time;
  81. if (g_scsiHostPhyReset)
  82. {
  83. // Reset request from watchdog timer
  84. scsiHostPhyRelease();
  85. return BUS_FREE;
  86. }
  87. int phase = 0;
  88. bool req_in = SCSI_IN(REQ);
  89. if (SCSI_IN(CD)) phase |= __scsiphase_cd;
  90. if (SCSI_IN(IO)) phase |= __scsiphase_io;
  91. if (SCSI_IN(MSG)) phase |= __scsiphase_msg;
  92. if (phase == 0 && absolute_time_diff_us(last_online_time, get_absolute_time()) > 100)
  93. {
  94. // BlueSCSI doesn't need to assert OUT_BSY to check whether the bus is in use
  95. delayMicroseconds(1);
  96. if (!SCSI_IN(BSY))
  97. {
  98. scsiLogInitiatorPhaseChange(BUS_FREE);
  99. return BUS_FREE;
  100. }
  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. log("Parity error in scsiReadOneByte(): ", (uint32_t)r);
  158. *parityError = 1;
  159. }
  160. return (uint8_t)r;
  161. }
  162. uint32_t scsiHostWrite(const uint8_t *data, uint32_t count)
  163. {
  164. scsiLogDataOut(data, count);
  165. int cd_start = SCSI_IN(CD);
  166. int msg_start = SCSI_IN(MSG);
  167. for (uint32_t i = 0; i < count; i++)
  168. {
  169. while (!SCSI_IN(REQ))
  170. {
  171. if (g_scsiHostPhyReset || SCSI_IN(IO) || SCSI_IN(CD) != cd_start || SCSI_IN(MSG) != msg_start)
  172. {
  173. // Target switched out of DATA_OUT mode
  174. log("scsiHostWrite: sent ", (int)i, " bytes, expected ", (int)count);
  175. return i;
  176. }
  177. }
  178. scsiHostWriteOneByte(data[i]);
  179. }
  180. return count;
  181. }
  182. uint32_t scsiHostRead(uint8_t *data, uint32_t count)
  183. {
  184. int parityError = 0;
  185. uint32_t fullcount = count;
  186. int cd_start = SCSI_IN(CD);
  187. int msg_start = SCSI_IN(MSG);
  188. if ((count & 1) == 0 && ((uint32_t)data & 1) == 0)
  189. {
  190. // Even number of bytes, use accelerated routine
  191. count = scsi_accel_host_read(data, count, &parityError, &g_scsiHostPhyReset);
  192. }
  193. else
  194. {
  195. for (uint32_t i = 0; i < count; i++)
  196. {
  197. while (!SCSI_IN(REQ))
  198. {
  199. if (g_scsiHostPhyReset || !SCSI_IN(IO) || SCSI_IN(CD) != cd_start || SCSI_IN(MSG) != msg_start)
  200. {
  201. // Target switched out of DATA_IN mode
  202. count = i;
  203. }
  204. }
  205. data[i] = scsiHostReadOneByte(&parityError);
  206. }
  207. }
  208. scsiLogDataIn(data, count);
  209. if (g_scsiHostPhyReset || parityError)
  210. {
  211. return 0;
  212. }
  213. else
  214. {
  215. if (count < fullcount)
  216. {
  217. log("scsiHostRead: received ", (int)count, " bytes, expected ", (int)fullcount);
  218. }
  219. return count;
  220. }
  221. }
  222. // Release all bus signals
  223. void scsiHostPhyRelease()
  224. {
  225. scsiLogInitiatorPhaseChange(BUS_FREE);
  226. SCSI_RELEASE_OUTPUTS();
  227. }