scsiHostPhy.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2022 Rabbit Hole Computing™
  3. *
  4. * ZuluSCSI™ firmware is licensed under the GPL version 3 or any later version. 
  5. *
  6. * https://www.gnu.org/licenses/gpl-3.0.html
  7. * ----
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version. 
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. * GNU General Public License for more details. 
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  20. **/
  21. #include "scsiHostPhy.h"
  22. #include "ZuluSCSI_platform.h"
  23. #include "ZuluSCSI_log.h"
  24. #include "ZuluSCSI_log_trace.h"
  25. #include "scsi_accel_host.h"
  26. #include <assert.h>
  27. #include <scsi2sd.h>
  28. extern "C" {
  29. #include <scsi.h>
  30. }
  31. volatile int g_scsiHostPhyReset;
  32. #ifndef PLATFORM_HAS_INITIATOR_MODE
  33. // Dummy functions for platforms without hardware support for
  34. // SCSI initiator mode.
  35. void scsiHostPhyReset(void) {}
  36. bool scsiHostPhySelect(int target_id) { return false; }
  37. int scsiHostPhyGetPhase() { return 0; }
  38. bool scsiHostRequestWaiting() { return false; }
  39. uint32_t scsiHostWrite(const uint8_t *data, uint32_t count) { return 0; }
  40. uint32_t scsiHostRead(uint8_t *data, uint32_t count) { return 0; }
  41. void scsiHostPhyRelease();
  42. #else
  43. // Release bus and pulse RST signal, initialize PHY to host mode.
  44. void scsiHostPhyReset(void)
  45. {
  46. SCSI_RELEASE_OUTPUTS();
  47. SCSI_ENABLE_INITIATOR();
  48. scsi_accel_host_init();
  49. SCSI_OUT(RST, 1);
  50. delay(2);
  51. SCSI_OUT(RST, 0);
  52. delay(250);
  53. g_scsiHostPhyReset = false;
  54. }
  55. // Select a device, id 0-7.
  56. // Returns true if the target answers to selection request.
  57. bool scsiHostPhySelect(int target_id)
  58. {
  59. SCSI_RELEASE_OUTPUTS();
  60. // We can't write individual data bus bits, so use a bit modified
  61. // arbitration scheme. We always yield to any other initiator on
  62. // the bus.
  63. scsiLogInitiatorPhaseChange(BUS_BUSY);
  64. SCSI_OUT(BSY, 1);
  65. for (int wait = 0; wait < 10; wait++)
  66. {
  67. delayMicroseconds(1);
  68. if (SCSI_IN_DATA() != 0)
  69. {
  70. dbgmsg("scsiHostPhySelect: bus is busy");
  71. scsiLogInitiatorPhaseChange(BUS_FREE);
  72. SCSI_RELEASE_OUTPUTS();
  73. return false;
  74. }
  75. }
  76. // Choose initiator ID different than target ID
  77. uint8_t initiator_id = (target_id == 7) ? 0 : 7;
  78. // Selection phase
  79. scsiLogInitiatorPhaseChange(SELECTION);
  80. dbgmsg("------ SELECTING ", target_id, " with initiator ID ", (int)initiator_id);
  81. SCSI_OUT(SEL, 1);
  82. delayMicroseconds(5);
  83. SCSI_OUT_DATA((1 << target_id) | (1 << initiator_id));
  84. delayMicroseconds(5);
  85. SCSI_OUT(BSY, 0);
  86. // Wait for target to respond
  87. for (int wait = 0; wait < 2500; wait++)
  88. {
  89. delayMicroseconds(100);
  90. if (SCSI_IN(BSY))
  91. {
  92. break;
  93. }
  94. }
  95. if (!SCSI_IN(BSY))
  96. {
  97. // No response
  98. SCSI_RELEASE_OUTPUTS();
  99. return false;
  100. }
  101. // We need to assert OUT_BSY to enable IO buffer U105 to read status signals.
  102. SCSI_RELEASE_DATA_REQ();
  103. SCSI_OUT(BSY, 1);
  104. SCSI_OUT(SEL, 0);
  105. return true;
  106. }
  107. // Read the current communication phase as signaled by the target
  108. int scsiHostPhyGetPhase()
  109. {
  110. static absolute_time_t last_online_time;
  111. if (g_scsiHostPhyReset)
  112. {
  113. // Reset request from watchdog timer
  114. scsiHostPhyRelease();
  115. return BUS_FREE;
  116. }
  117. int phase = 0;
  118. bool req_in = SCSI_IN(REQ);
  119. if (SCSI_IN(CD)) phase |= __scsiphase_cd;
  120. if (SCSI_IN(IO)) phase |= __scsiphase_io;
  121. if (SCSI_IN(MSG)) phase |= __scsiphase_msg;
  122. if (phase == 0 && absolute_time_diff_us(last_online_time, get_absolute_time()) > 100)
  123. {
  124. // Disable OUT_BSY for a short time to see if the target is still on line
  125. SCSI_OUT(BSY, 0);
  126. delayMicroseconds(1);
  127. if (!SCSI_IN(BSY))
  128. {
  129. scsiLogInitiatorPhaseChange(BUS_FREE);
  130. return BUS_FREE;
  131. }
  132. // Still online, re-enable OUT_BSY to enable IO buffers
  133. SCSI_OUT(BSY, 1);
  134. last_online_time = get_absolute_time();
  135. }
  136. else if (phase != 0)
  137. {
  138. last_online_time = get_absolute_time();
  139. }
  140. if (!req_in)
  141. {
  142. // Don't act on phase changes until target asserts request signal.
  143. // This filters out any spurious changes on control signals.
  144. return BUS_BUSY;
  145. }
  146. else
  147. {
  148. scsiLogInitiatorPhaseChange(phase);
  149. return phase;
  150. }
  151. }
  152. bool scsiHostRequestWaiting()
  153. {
  154. return SCSI_IN(REQ);
  155. }
  156. // Blocking data transfer
  157. #define SCSIHOST_WAIT_ACTIVE(pin) \
  158. if (!SCSI_IN(pin)) { \
  159. if (!SCSI_IN(pin)) { \
  160. while(!SCSI_IN(pin) && !g_scsiHostPhyReset); \
  161. } \
  162. }
  163. #define SCSIHOST_WAIT_INACTIVE(pin) \
  164. if (SCSI_IN(pin)) { \
  165. if (SCSI_IN(pin)) { \
  166. while(SCSI_IN(pin) && !g_scsiHostPhyReset); \
  167. } \
  168. }
  169. // Write one byte to SCSI target using the handshake mechanism
  170. static inline void scsiHostWriteOneByte(uint8_t value)
  171. {
  172. SCSIHOST_WAIT_ACTIVE(REQ);
  173. SCSI_OUT_DATA(value);
  174. delay_100ns(); // DB setup time before ACK
  175. SCSI_OUT(ACK, 1);
  176. SCSIHOST_WAIT_INACTIVE(REQ);
  177. SCSI_RELEASE_DATA_REQ();
  178. SCSI_OUT(ACK, 0);
  179. }
  180. // Read one byte from SCSI target using the handshake mechanism.
  181. static inline uint8_t scsiHostReadOneByte(int* parityError)
  182. {
  183. SCSIHOST_WAIT_ACTIVE(REQ);
  184. uint16_t r = SCSI_IN_DATA();
  185. SCSI_OUT(ACK, 1);
  186. SCSIHOST_WAIT_INACTIVE(REQ);
  187. SCSI_OUT(ACK, 0);
  188. if (parityError && r != (g_scsi_parity_lookup[r & 0xFF] ^ SCSI_IO_DATA_MASK))
  189. {
  190. logmsg("Parity error in scsiReadOneByte(): ", (uint32_t)r);
  191. *parityError = 1;
  192. }
  193. return (uint8_t)r;
  194. }
  195. uint32_t scsiHostWrite(const uint8_t *data, uint32_t count)
  196. {
  197. scsiLogDataOut(data, count);
  198. int cd_start = SCSI_IN(CD);
  199. int msg_start = SCSI_IN(MSG);
  200. for (uint32_t i = 0; i < count; i++)
  201. {
  202. while (!SCSI_IN(REQ))
  203. {
  204. if (g_scsiHostPhyReset || SCSI_IN(IO) || SCSI_IN(CD) != cd_start || SCSI_IN(MSG) != msg_start)
  205. {
  206. // Target switched out of DATA_OUT mode
  207. logmsg("scsiHostWrite: sent ", (int)i, " bytes, expected ", (int)count);
  208. return i;
  209. }
  210. }
  211. scsiHostWriteOneByte(data[i]);
  212. }
  213. return count;
  214. }
  215. uint32_t scsiHostRead(uint8_t *data, uint32_t count)
  216. {
  217. int parityError = 0;
  218. uint32_t fullcount = count;
  219. int cd_start = SCSI_IN(CD);
  220. int msg_start = SCSI_IN(MSG);
  221. if ((count & 1) == 0 && ((uint32_t)data & 1) == 0)
  222. {
  223. // Even number of bytes, use accelerated routine
  224. count = scsi_accel_host_read(data, count, &parityError, &g_scsiHostPhyReset);
  225. }
  226. else
  227. {
  228. for (uint32_t i = 0; i < count; i++)
  229. {
  230. while (!SCSI_IN(REQ))
  231. {
  232. if (g_scsiHostPhyReset || !SCSI_IN(IO) || SCSI_IN(CD) != cd_start || SCSI_IN(MSG) != msg_start)
  233. {
  234. // Target switched out of DATA_IN mode
  235. count = i;
  236. }
  237. }
  238. data[i] = scsiHostReadOneByte(&parityError);
  239. }
  240. }
  241. scsiLogDataIn(data, count);
  242. if (g_scsiHostPhyReset || parityError)
  243. {
  244. return 0;
  245. }
  246. else
  247. {
  248. if (count < fullcount)
  249. {
  250. logmsg("scsiHostRead: received ", (int)count, " bytes, expected ", (int)fullcount);
  251. }
  252. return count;
  253. }
  254. }
  255. // Release all bus signals
  256. void scsiHostPhyRelease()
  257. {
  258. scsiLogInitiatorPhaseChange(BUS_FREE);
  259. SCSI_RELEASE_OUTPUTS();
  260. }
  261. #endif