scsiHostPhy.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // Copyright (c) 2022 Rabbit Hole Computing™
  2. // Copyright (c) 2024 Tech by Androda, LLC
  3. #include "scsiHostPhy.h"
  4. #include "BlueSCSI_platform.h"
  5. #include "BlueSCSI_log.h"
  6. #include "BlueSCSI_log_trace.h"
  7. #include "scsi_accel_host.h"
  8. #include <assert.h>
  9. #include <scsi2sd.h>
  10. extern "C" {
  11. #include <scsi.h>
  12. }
  13. volatile int g_scsiHostPhyReset;
  14. bool perform_parity_checking = true;
  15. // Release bus and pulse RST signal, initialize PHY to host mode.
  16. void scsiHostPhyReset(void)
  17. {
  18. SCSI_RELEASE_OUTPUTS();
  19. SCSI_ENABLE_INITIATOR();
  20. scsi_accel_host_init();
  21. SCSI_OUT(RST, 1);
  22. delay(2);
  23. SCSI_OUT(RST, 0);
  24. delay(250);
  25. g_scsiHostPhyReset = false;
  26. }
  27. // Select a device, id 0-7.
  28. // Returns true if the target answers to selection request.
  29. bool scsiHostPhySelect(int target_id, int initiator_id)
  30. {
  31. SCSI_ENABLE_INITIATOR();
  32. SCSI_RELEASE_OUTPUTS();
  33. // We can't write individual data bus bits, so use a bit modified
  34. // arbitration scheme. We always yield to any other initiator on
  35. // the bus.
  36. scsiLogInitiatorPhaseChange(BUS_BUSY);
  37. SCSI_OUT(REQ, 0);
  38. SCSI_OUT(BSY, 1);
  39. for (int wait = 0; wait < 10; wait++)
  40. {
  41. delayMicroseconds(1);
  42. if (SCSI_IN_DATA() != 0)
  43. {
  44. debuglog("scsiHostPhySelect: bus is busy");
  45. scsiLogInitiatorPhaseChange(BUS_FREE);
  46. SCSI_RELEASE_OUTPUTS();
  47. return false;
  48. }
  49. }
  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, uint32_t *parityResult)
  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. *parityError = 1;
  159. *parityResult = (uint32_t)r;
  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 parityResult;
  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, &parityResult, &g_scsiHostPhyReset);
  194. if (parityError && perform_parity_checking) {
  195. log("Parity error in scsi_accel_host_read(): ", parityResult);
  196. } else {
  197. parityError = 0;
  198. }
  199. }
  200. else
  201. {
  202. for (uint32_t i = 0; i < count; i++)
  203. {
  204. while (!SCSI_IN(REQ))
  205. {
  206. if (g_scsiHostPhyReset || !SCSI_IN(IO) || SCSI_IN(CD) != cd_start || SCSI_IN(MSG) != msg_start)
  207. {
  208. // Target switched out of DATA_IN mode
  209. count = i;
  210. }
  211. }
  212. data[i] = scsiHostReadOneByte(&parityError, &parityResult);
  213. if (parityError && perform_parity_checking) {
  214. log("Parity error in scsiReadOneByte(): ", parityResult);
  215. } else {
  216. parityError = 0;
  217. }
  218. }
  219. }
  220. scsiLogDataIn(data, count);
  221. if (g_scsiHostPhyReset || parityError)
  222. {
  223. return 0;
  224. }
  225. else
  226. {
  227. if (count < fullcount)
  228. {
  229. log("scsiHostRead: received ", (int)count, " bytes, expected ", (int)fullcount);
  230. }
  231. return count;
  232. }
  233. }
  234. // Release all bus signals
  235. void scsiHostPhyRelease()
  236. {
  237. scsiLogInitiatorPhaseChange(BUS_FREE);
  238. SCSI_RELEASE_OUTPUTS();
  239. SCSI_RELEASE_DATA_REQ();
  240. }
  241. void setInitiatorModeParityCheck(bool checkParity) {
  242. perform_parity_checking = checkParity;
  243. }