scsiHostPhy.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. // Selection phase
  77. scsiLogInitiatorPhaseChange(SELECTION);
  78. dbgmsg("------ SELECTING ", target_id);
  79. SCSI_OUT(SEL, 1);
  80. delayMicroseconds(5);
  81. SCSI_OUT_DATA(1 << target_id);
  82. delayMicroseconds(5);
  83. SCSI_OUT(BSY, 0);
  84. // Wait for target to respond
  85. for (int wait = 0; wait < 2500; wait++)
  86. {
  87. delayMicroseconds(100);
  88. if (SCSI_IN(BSY))
  89. {
  90. break;
  91. }
  92. }
  93. if (!SCSI_IN(BSY))
  94. {
  95. // No response
  96. SCSI_RELEASE_OUTPUTS();
  97. return false;
  98. }
  99. // We need to assert OUT_BSY to enable IO buffer U105 to read status signals.
  100. SCSI_RELEASE_DATA_REQ();
  101. SCSI_OUT(BSY, 1);
  102. SCSI_OUT(SEL, 0);
  103. return true;
  104. }
  105. // Read the current communication phase as signaled by the target
  106. int scsiHostPhyGetPhase()
  107. {
  108. static absolute_time_t last_online_time;
  109. if (g_scsiHostPhyReset)
  110. {
  111. // Reset request from watchdog timer
  112. scsiHostPhyRelease();
  113. return BUS_FREE;
  114. }
  115. int phase = 0;
  116. bool req_in = SCSI_IN(REQ);
  117. if (SCSI_IN(CD)) phase |= __scsiphase_cd;
  118. if (SCSI_IN(IO)) phase |= __scsiphase_io;
  119. if (SCSI_IN(MSG)) phase |= __scsiphase_msg;
  120. if (phase == 0 && absolute_time_diff_us(last_online_time, get_absolute_time()) > 100)
  121. {
  122. // Disable OUT_BSY for a short time to see if the target is still on line
  123. SCSI_OUT(BSY, 0);
  124. delayMicroseconds(1);
  125. if (!SCSI_IN(BSY))
  126. {
  127. scsiLogInitiatorPhaseChange(BUS_FREE);
  128. return BUS_FREE;
  129. }
  130. // Still online, re-enable OUT_BSY to enable IO buffers
  131. SCSI_OUT(BSY, 1);
  132. last_online_time = get_absolute_time();
  133. }
  134. else if (phase != 0)
  135. {
  136. last_online_time = get_absolute_time();
  137. }
  138. if (!req_in)
  139. {
  140. // Don't act on phase changes until target asserts request signal.
  141. // This filters out any spurious changes on control signals.
  142. return BUS_BUSY;
  143. }
  144. else
  145. {
  146. scsiLogInitiatorPhaseChange(phase);
  147. return phase;
  148. }
  149. }
  150. bool scsiHostRequestWaiting()
  151. {
  152. return SCSI_IN(REQ);
  153. }
  154. // Blocking data transfer
  155. #define SCSIHOST_WAIT_ACTIVE(pin) \
  156. if (!SCSI_IN(pin)) { \
  157. if (!SCSI_IN(pin)) { \
  158. while(!SCSI_IN(pin) && !g_scsiHostPhyReset); \
  159. } \
  160. }
  161. #define SCSIHOST_WAIT_INACTIVE(pin) \
  162. if (SCSI_IN(pin)) { \
  163. if (SCSI_IN(pin)) { \
  164. while(SCSI_IN(pin) && !g_scsiHostPhyReset); \
  165. } \
  166. }
  167. // Write one byte to SCSI target using the handshake mechanism
  168. static inline void scsiHostWriteOneByte(uint8_t value)
  169. {
  170. SCSIHOST_WAIT_ACTIVE(REQ);
  171. SCSI_OUT_DATA(value);
  172. delay_100ns(); // DB setup time before ACK
  173. SCSI_OUT(ACK, 1);
  174. SCSIHOST_WAIT_INACTIVE(REQ);
  175. SCSI_RELEASE_DATA_REQ();
  176. SCSI_OUT(ACK, 0);
  177. }
  178. // Read one byte from SCSI target using the handshake mechanism.
  179. static inline uint8_t scsiHostReadOneByte(int* parityError)
  180. {
  181. SCSIHOST_WAIT_ACTIVE(REQ);
  182. uint16_t r = SCSI_IN_DATA();
  183. SCSI_OUT(ACK, 1);
  184. SCSIHOST_WAIT_INACTIVE(REQ);
  185. SCSI_OUT(ACK, 0);
  186. if (parityError && r != (g_scsi_parity_lookup[r & 0xFF] ^ SCSI_IO_DATA_MASK))
  187. {
  188. logmsg("Parity error in scsiReadOneByte(): ", (uint32_t)r);
  189. *parityError = 1;
  190. }
  191. return (uint8_t)r;
  192. }
  193. uint32_t scsiHostWrite(const uint8_t *data, uint32_t count)
  194. {
  195. scsiLogDataOut(data, count);
  196. int cd_start = SCSI_IN(CD);
  197. int msg_start = SCSI_IN(MSG);
  198. for (uint32_t i = 0; i < count; i++)
  199. {
  200. while (!SCSI_IN(REQ))
  201. {
  202. if (g_scsiHostPhyReset || SCSI_IN(IO) || SCSI_IN(CD) != cd_start || SCSI_IN(MSG) != msg_start)
  203. {
  204. // Target switched out of DATA_OUT mode
  205. logmsg("scsiHostWrite: sent ", (int)i, " bytes, expected ", (int)count);
  206. return i;
  207. }
  208. }
  209. scsiHostWriteOneByte(data[i]);
  210. }
  211. return count;
  212. }
  213. uint32_t scsiHostRead(uint8_t *data, uint32_t count)
  214. {
  215. int parityError = 0;
  216. uint32_t fullcount = count;
  217. int cd_start = SCSI_IN(CD);
  218. int msg_start = SCSI_IN(MSG);
  219. if ((count & 1) == 0 && ((uint32_t)data & 1) == 0)
  220. {
  221. // Even number of bytes, use accelerated routine
  222. count = scsi_accel_host_read(data, count, &parityError, &g_scsiHostPhyReset);
  223. }
  224. else
  225. {
  226. for (uint32_t i = 0; i < count; i++)
  227. {
  228. while (!SCSI_IN(REQ))
  229. {
  230. if (g_scsiHostPhyReset || !SCSI_IN(IO) || SCSI_IN(CD) != cd_start || SCSI_IN(MSG) != msg_start)
  231. {
  232. // Target switched out of DATA_IN mode
  233. count = i;
  234. }
  235. }
  236. data[i] = scsiHostReadOneByte(&parityError);
  237. }
  238. }
  239. scsiLogDataIn(data, count);
  240. if (g_scsiHostPhyReset || parityError)
  241. {
  242. return 0;
  243. }
  244. else
  245. {
  246. if (count < fullcount)
  247. {
  248. logmsg("scsiHostRead: received ", (int)count, " bytes, expected ", (int)fullcount);
  249. }
  250. return count;
  251. }
  252. }
  253. // Release all bus signals
  254. void scsiHostPhyRelease()
  255. {
  256. scsiLogInitiatorPhaseChange(BUS_FREE);
  257. SCSI_RELEASE_OUTPUTS();
  258. }
  259. #endif