scsiHostPhy.cpp 7.1 KB

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