scsiHostPhy.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // Copyright (c) 2022 Rabbit Hole Computing™
  2. #include "scsiHostPhy.h"
  3. #include "BlueSCSI_platform.h"
  4. #include "BlueSCSI_log.h"
  5. #include "BlueSCSI_log_trace.h"
  6. #include "scsi_accel_host.h"
  7. #include <assert.h>
  8. #include <scsi2sd.h>
  9. extern "C" {
  10. #include <scsi.h>
  11. }
  12. volatile int g_scsiHostPhyReset;
  13. // Release bus and pulse RST signal, initialize PHY to host mode.
  14. void scsiHostPhyReset(void)
  15. {
  16. SCSI_RELEASE_OUTPUTS();
  17. SCSI_ENABLE_INITIATOR();
  18. scsi_accel_host_init();
  19. SCSI_OUT(RST, 1);
  20. delay(2);
  21. SCSI_OUT(RST, 0);
  22. delay(250);
  23. g_scsiHostPhyReset = false;
  24. }
  25. // Select a device, id 0-7.
  26. // Returns true if the target answers to selection request.
  27. bool scsiHostPhySelect(int target_id, int initiator_id)
  28. {
  29. SCSI_ENABLE_INITIATOR();
  30. SCSI_RELEASE_OUTPUTS();
  31. // We can't write individual data bus bits, so use a bit modified
  32. // arbitration scheme. We always yield to any other initiator on
  33. // the bus.
  34. scsiLogInitiatorPhaseChange(BUS_BUSY);
  35. SCSI_OUT(REQ, 0);
  36. SCSI_OUT(BSY, 1);
  37. for (int wait = 0; wait < 10; wait++)
  38. {
  39. delayMicroseconds(1);
  40. if (SCSI_IN_DATA() != 0)
  41. {
  42. debuglog("scsiHostPhySelect: bus is busy");
  43. scsiLogInitiatorPhaseChange(BUS_FREE);
  44. SCSI_RELEASE_OUTPUTS();
  45. return false;
  46. }
  47. }
  48. // Selection phase
  49. scsiLogInitiatorPhaseChange(SELECTION);
  50. debuglog("------ SELECTING ", target_id, " with initiator ID ", (int)initiator_id);
  51. SCSI_OUT(SEL, 1);
  52. delayMicroseconds(5);
  53. SCSI_OUT_DATA((1 << target_id) | (1 << initiator_id));
  54. delayMicroseconds(5);
  55. SCSI_OUT(BSY, 0);
  56. // Wait for target to respond
  57. for (int wait = 0; wait < 2500; wait++)
  58. {
  59. delayMicroseconds(100);
  60. if (SCSI_IN(BSY))
  61. {
  62. break;
  63. }
  64. }
  65. if (!SCSI_IN(BSY))
  66. {
  67. // No response
  68. SCSI_RELEASE_OUTPUTS();
  69. return false;
  70. }
  71. SCSI_RELEASE_DATA_REQ();
  72. SCSI_OUT(SEL, 0);
  73. SCSI_ENABLE_INITIATOR();
  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. // BlueSCSI doesn't need to assert OUT_BSY to check whether the bus is in use
  94. delayMicroseconds(1);
  95. if (!SCSI_IN(BSY))
  96. {
  97. scsiLogInitiatorPhaseChange(BUS_FREE);
  98. return BUS_FREE;
  99. }
  100. last_online_time = get_absolute_time();
  101. }
  102. else if (phase != 0)
  103. {
  104. last_online_time = get_absolute_time();
  105. }
  106. if (!req_in)
  107. {
  108. // Don't act on phase changes until target asserts request signal.
  109. // This filters out any spurious changes on control signals.
  110. return BUS_BUSY;
  111. }
  112. else
  113. {
  114. scsiLogInitiatorPhaseChange(phase);
  115. return phase;
  116. }
  117. }
  118. bool scsiHostRequestWaiting()
  119. {
  120. return SCSI_IN(REQ);
  121. }
  122. // Blocking data transfer
  123. #define SCSIHOST_WAIT_ACTIVE(pin) \
  124. if (!SCSI_IN(pin)) { \
  125. if (!SCSI_IN(pin)) { \
  126. while(!SCSI_IN(pin) && !g_scsiHostPhyReset); \
  127. } \
  128. }
  129. #define SCSIHOST_WAIT_INACTIVE(pin) \
  130. if (SCSI_IN(pin)) { \
  131. if (SCSI_IN(pin)) { \
  132. while(SCSI_IN(pin) && !g_scsiHostPhyReset); \
  133. } \
  134. }
  135. // Write one byte to SCSI target using the handshake mechanism
  136. static inline void scsiHostWriteOneByte(uint8_t value)
  137. {
  138. SCSIHOST_WAIT_ACTIVE(REQ);
  139. SCSI_OUT_DATA(value);
  140. delay_100ns(); // DB setup time before ACK
  141. SCSI_OUT(ACK, 1);
  142. SCSIHOST_WAIT_INACTIVE(REQ);
  143. SCSI_RELEASE_DATA_REQ();
  144. SCSI_OUT(ACK, 0);
  145. }
  146. // Read one byte from SCSI target using the handshake mechanism.
  147. static inline uint8_t scsiHostReadOneByte(int* parityError)
  148. {
  149. SCSIHOST_WAIT_ACTIVE(REQ);
  150. uint16_t r = SCSI_IN_DATA();
  151. SCSI_OUT(ACK, 1);
  152. SCSIHOST_WAIT_INACTIVE(REQ);
  153. SCSI_OUT(ACK, 0);
  154. if (parityError && r != (g_scsi_parity_lookup[r & 0xFF] ^ SCSI_IO_DATA_MASK))
  155. {
  156. log("Parity error in scsiReadOneByte(): ", (uint32_t)r);
  157. *parityError = 1;
  158. }
  159. return (uint8_t)r;
  160. }
  161. uint32_t scsiHostWrite(const uint8_t *data, uint32_t count)
  162. {
  163. scsiLogDataOut(data, count);
  164. int cd_start = SCSI_IN(CD);
  165. int msg_start = SCSI_IN(MSG);
  166. for (uint32_t i = 0; i < count; i++)
  167. {
  168. while (!SCSI_IN(REQ))
  169. {
  170. if (g_scsiHostPhyReset || SCSI_IN(IO) || SCSI_IN(CD) != cd_start || SCSI_IN(MSG) != msg_start)
  171. {
  172. // Target switched out of DATA_OUT mode
  173. log("scsiHostWrite: sent ", (int)i, " bytes, expected ", (int)count);
  174. return i;
  175. }
  176. }
  177. scsiHostWriteOneByte(data[i]);
  178. }
  179. return count;
  180. }
  181. uint32_t scsiHostRead(uint8_t *data, uint32_t count)
  182. {
  183. int parityError = 0;
  184. uint32_t fullcount = count;
  185. int cd_start = SCSI_IN(CD);
  186. int msg_start = SCSI_IN(MSG);
  187. if ((count & 1) == 0 && ((uint32_t)data & 1) == 0)
  188. {
  189. // Even number of bytes, use accelerated routine
  190. count = scsi_accel_host_read(data, count, &parityError, &g_scsiHostPhyReset);
  191. }
  192. else
  193. {
  194. for (uint32_t i = 0; i < count; i++)
  195. {
  196. while (!SCSI_IN(REQ))
  197. {
  198. if (g_scsiHostPhyReset || !SCSI_IN(IO) || SCSI_IN(CD) != cd_start || SCSI_IN(MSG) != msg_start)
  199. {
  200. // Target switched out of DATA_IN mode
  201. count = i;
  202. }
  203. }
  204. data[i] = scsiHostReadOneByte(&parityError);
  205. }
  206. }
  207. scsiLogDataIn(data, count);
  208. if (g_scsiHostPhyReset || parityError)
  209. {
  210. return 0;
  211. }
  212. else
  213. {
  214. if (count < fullcount)
  215. {
  216. log("scsiHostRead: received ", (int)count, " bytes, expected ", (int)fullcount);
  217. }
  218. return count;
  219. }
  220. }
  221. // Release all bus signals
  222. void scsiHostPhyRelease()
  223. {
  224. scsiLogInitiatorPhaseChange(BUS_FREE);
  225. SCSI_RELEASE_OUTPUTS();
  226. SCSI_RELEASE_DATA_REQ();
  227. }