scsiHostPhy.cpp 6.4 KB

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