scsiPhy.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. // Copyright (C) 2013 Michael McMaster <michael@codesrc.com>
  2. //
  3. // This file is part of SCSI2SD.
  4. //
  5. // SCSI2SD is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // SCSI2SD is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with SCSI2SD. If not, see <http://www.gnu.org/licenses/>.
  17. #include "stm32f2xx.h"
  18. #include "stm32f2xx_hal.h"
  19. #include "stm32f2xx_hal_dma.h"
  20. #include "scsi.h"
  21. #include "scsiPhy.h"
  22. #include "time.h"
  23. #include "fpga.h"
  24. #include "led.h"
  25. #include <string.h>
  26. static uint8_t asyncTimings[][4] =
  27. {
  28. /* Speed, Assert, Deskew, Hold, Glitch */
  29. {/*1.5MB/s*/ 28, 18, 7, 15},
  30. //{/*1.5MB/s*/ 63, 31, 7, 15},
  31. {/*3.3MB/s*/ 13, 6, 6, 13},
  32. {/*5MB/s*/ 9, 6, 6, 6}, // 80ns
  33. {/*safe*/ 3, 6, 6, 6}, // Probably safe
  34. {/*turbo*/ 3, 3, 3, 2}
  35. };
  36. #define SCSI_ASYNC_15 0
  37. #define SCSI_ASYNC_33 1
  38. #define SCSI_ASYNC_50 2
  39. #define SCSI_ASYNC_SAFE 3
  40. #define SCSI_ASYNC_TURBO 4
  41. // 5MB/s synchronous timing
  42. #define SCSI_FAST5_DESKEW 6 // 55ns
  43. #define SCSI_FAST5_HOLD 6 // 53ns
  44. // 10MB/s synchronous timing
  45. // 2:0 Deskew count, 25ns
  46. // 6:4 Hold count, 33ns
  47. // 3:0 Assertion count, 30ns
  48. // We want deskew + hold + assert + 3 to add up to 11 clocks
  49. // the fpga code has 1 clock of overhead when transitioning from deskew to
  50. // assert to hold
  51. #define SCSI_FAST10_DESKEW 2 // 25ns
  52. #define SCSI_FAST10_HOLD 3 // 33ns
  53. #define SCSI_FAST10_WRITE_ASSERT 3 // 30ns. Overall clocks only works if fpga overhead is 3.
  54. // Slow down the cycle to be valid. 2x assert period is TOO FAST when
  55. // reading data. It's ok when writing due to the deskew.
  56. // 50ns. ie. 100ns / 2. Rounded down because there's likely a few extra cycles
  57. // here and there.
  58. #define SCSI_FAST10_READ_ASSERT 5
  59. // Fastest possible timing, probably not 20MB/s
  60. #define SCSI_FAST20_DESKEW 1
  61. #define SCSI_FAST20_HOLD 2
  62. #define SCSI_FAST20_ASSERT 2
  63. #define syncDeskew(period) ((period) < 35 ? \
  64. SCSI_FAST10_DESKEW : SCSI_FAST5_DESKEW)
  65. #define syncHold(period) ((period) < 35 ? \
  66. ((period) == 25 ? SCSI_FAST10_HOLD : 4) /* 25ns/33ns */\
  67. : SCSI_FAST5_HOLD)
  68. // Number of overhead cycles per period.
  69. #define FPGA_OVERHEAD 2
  70. #define FPGA_CYCLES_PER_NS 9
  71. #define SCSI_PERIOD_CLKS(period) ((((int)period * 4) + (FPGA_CYCLES_PER_NS/2)) / FPGA_CYCLES_PER_NS)
  72. // 3.125MB/s (80 period) to < 10MB/s sync
  73. // Assumes a 108MHz fpga clock. (9 ns)
  74. // 3:0 Assertion count, variable
  75. #define syncAssertionWrite(period,deskew) ((SCSI_PERIOD_CLKS(period) - deskew - FPGA_OVERHEAD + 1) / 2)
  76. #define syncAssertionRead(period) syncAssertionWrite(period,0)
  77. // Time until we consider ourselves selected
  78. // 400ns at 108MHz
  79. #define SCSI_DEFAULT_SELECTION 43
  80. #define SCSI_FAST_SELECTION 5
  81. // Private DMA variables.
  82. static int dmaInProgress = 0;
  83. static DMA_HandleTypeDef memToFSMC;
  84. static DMA_HandleTypeDef fsmcToMem;
  85. volatile uint8_t scsiRxDMAComplete;
  86. volatile uint8_t scsiTxDMAComplete;
  87. // scsi IRQ handler is initialised by the STM32 HAL. Connected to
  88. // PE4
  89. // Note: naming is important to ensure this function is listed in the
  90. // vector table.
  91. void EXTI4_IRQHandler()
  92. {
  93. // Make sure that interrupt flag is set
  94. if (__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_4) != RESET) {
  95. // Clear interrupt flag
  96. __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_4);
  97. uint8_t statusFlags = *SCSI_STS_SCSI;
  98. scsiDev.resetFlag = scsiDev.resetFlag || (statusFlags & 0x04);
  99. // selFlag is required for Philips P2000C which releases it after 600ns
  100. // without waiting for BSY.
  101. // Also required for some early Mac Plus roms
  102. if (statusFlags & 0x08) // Check SEL flag
  103. {
  104. scsiDev.selFlag = *SCSI_STS_SELECTED;
  105. }
  106. }
  107. }
  108. void
  109. scsiSetDataCount(uint32_t count)
  110. {
  111. *SCSI_DATA_CNT_HI = (count >> 16) & 0xff;
  112. *SCSI_DATA_CNT_MID = (count >> 8) & 0xff;
  113. *SCSI_DATA_CNT_LO = count & 0xff;
  114. *SCSI_DATA_CNT_SET = 1;
  115. }
  116. int scsiFifoReady(void)
  117. {
  118. __NOP();
  119. HAL_GPIO_ReadPin(GPIOE, FPGA_GPIO3_Pin);
  120. __NOP();
  121. return HAL_GPIO_ReadPin(GPIOE, FPGA_GPIO3_Pin) != 0;
  122. }
  123. uint8_t
  124. scsiReadByte(void)
  125. {
  126. scsiSetDataCount(1);
  127. // Ready immediately. setDataCount resets fifos
  128. while (!scsiPhyComplete() && likely(!scsiDev.resetFlag))
  129. {
  130. __WFI(); // Wait for interrupt
  131. }
  132. __enable_irq();
  133. uint8_t val = scsiPhyRx();
  134. // TODO scsiDev.parityError = scsiDev.parityError || SCSI_Parity_Error_Read();
  135. return val;
  136. }
  137. void
  138. scsiReadPIO(uint8_t* data, uint32_t count, int* parityError)
  139. {
  140. uint16_t* fifoData = (uint16_t*)data;
  141. uint32_t count16 = (count + 1) / 2;
  142. int i = 0;
  143. while ((i < count16) && likely(!scsiDev.resetFlag))
  144. {
  145. // Wait until FIFO is full (or complete)
  146. while (!scsiFifoReady() && likely(!scsiDev.resetFlag))
  147. {
  148. // spin
  149. }
  150. if (count16 - i >= SCSI_FIFO_DEPTH16)
  151. {
  152. uint32_t chunk16 = SCSI_FIFO_DEPTH16;
  153. // Let gcc unroll the loop as much as possible.
  154. for (uint32_t k = 0; k + 128 <= chunk16; k += 128)
  155. {
  156. fifoData[i + k] = scsiPhyRx();
  157. fifoData[i + k + 1] = scsiPhyRx();
  158. fifoData[i + k + 2] = scsiPhyRx();
  159. fifoData[i + k + 3] = scsiPhyRx();
  160. fifoData[i + k + 4] = scsiPhyRx();
  161. fifoData[i + k + 5] = scsiPhyRx();
  162. fifoData[i + k + 6] = scsiPhyRx();
  163. fifoData[i + k + 7] = scsiPhyRx();
  164. fifoData[i + k + 8] = scsiPhyRx();
  165. fifoData[i + k + 9] = scsiPhyRx();
  166. fifoData[i + k + 10] = scsiPhyRx();
  167. fifoData[i + k + 11] = scsiPhyRx();
  168. fifoData[i + k + 12] = scsiPhyRx();
  169. fifoData[i + k + 13] = scsiPhyRx();
  170. fifoData[i + k + 14] = scsiPhyRx();
  171. fifoData[i + k + 15] = scsiPhyRx();
  172. fifoData[i + k + 16] = scsiPhyRx();
  173. fifoData[i + k + 17] = scsiPhyRx();
  174. fifoData[i + k + 18] = scsiPhyRx();
  175. fifoData[i + k + 19] = scsiPhyRx();
  176. fifoData[i + k + 20] = scsiPhyRx();
  177. fifoData[i + k + 21] = scsiPhyRx();
  178. fifoData[i + k + 22] = scsiPhyRx();
  179. fifoData[i + k + 23] = scsiPhyRx();
  180. fifoData[i + k + 24] = scsiPhyRx();
  181. fifoData[i + k + 25] = scsiPhyRx();
  182. fifoData[i + k + 26] = scsiPhyRx();
  183. fifoData[i + k + 27] = scsiPhyRx();
  184. fifoData[i + k + 28] = scsiPhyRx();
  185. fifoData[i + k + 29] = scsiPhyRx();
  186. fifoData[i + k + 30] = scsiPhyRx();
  187. fifoData[i + k + 31] = scsiPhyRx();
  188. fifoData[i + k + 32] = scsiPhyRx();
  189. fifoData[i + k + 33] = scsiPhyRx();
  190. fifoData[i + k + 34] = scsiPhyRx();
  191. fifoData[i + k + 35] = scsiPhyRx();
  192. fifoData[i + k + 36] = scsiPhyRx();
  193. fifoData[i + k + 37] = scsiPhyRx();
  194. fifoData[i + k + 38] = scsiPhyRx();
  195. fifoData[i + k + 39] = scsiPhyRx();
  196. fifoData[i + k + 40] = scsiPhyRx();
  197. fifoData[i + k + 41] = scsiPhyRx();
  198. fifoData[i + k + 42] = scsiPhyRx();
  199. fifoData[i + k + 43] = scsiPhyRx();
  200. fifoData[i + k + 44] = scsiPhyRx();
  201. fifoData[i + k + 45] = scsiPhyRx();
  202. fifoData[i + k + 46] = scsiPhyRx();
  203. fifoData[i + k + 47] = scsiPhyRx();
  204. fifoData[i + k + 48] = scsiPhyRx();
  205. fifoData[i + k + 49] = scsiPhyRx();
  206. fifoData[i + k + 50] = scsiPhyRx();
  207. fifoData[i + k + 51] = scsiPhyRx();
  208. fifoData[i + k + 52] = scsiPhyRx();
  209. fifoData[i + k + 53] = scsiPhyRx();
  210. fifoData[i + k + 54] = scsiPhyRx();
  211. fifoData[i + k + 55] = scsiPhyRx();
  212. fifoData[i + k + 56] = scsiPhyRx();
  213. fifoData[i + k + 57] = scsiPhyRx();
  214. fifoData[i + k + 58] = scsiPhyRx();
  215. fifoData[i + k + 59] = scsiPhyRx();
  216. fifoData[i + k + 60] = scsiPhyRx();
  217. fifoData[i + k + 61] = scsiPhyRx();
  218. fifoData[i + k + 62] = scsiPhyRx();
  219. fifoData[i + k + 63] = scsiPhyRx();
  220. fifoData[i + k + 64] = scsiPhyRx();
  221. fifoData[i + k + 65] = scsiPhyRx();
  222. fifoData[i + k + 66] = scsiPhyRx();
  223. fifoData[i + k + 67] = scsiPhyRx();
  224. fifoData[i + k + 68] = scsiPhyRx();
  225. fifoData[i + k + 69] = scsiPhyRx();
  226. fifoData[i + k + 70] = scsiPhyRx();
  227. fifoData[i + k + 71] = scsiPhyRx();
  228. fifoData[i + k + 72] = scsiPhyRx();
  229. fifoData[i + k + 73] = scsiPhyRx();
  230. fifoData[i + k + 74] = scsiPhyRx();
  231. fifoData[i + k + 75] = scsiPhyRx();
  232. fifoData[i + k + 76] = scsiPhyRx();
  233. fifoData[i + k + 77] = scsiPhyRx();
  234. fifoData[i + k + 78] = scsiPhyRx();
  235. fifoData[i + k + 79] = scsiPhyRx();
  236. fifoData[i + k + 80] = scsiPhyRx();
  237. fifoData[i + k + 81] = scsiPhyRx();
  238. fifoData[i + k + 82] = scsiPhyRx();
  239. fifoData[i + k + 83] = scsiPhyRx();
  240. fifoData[i + k + 84] = scsiPhyRx();
  241. fifoData[i + k + 85] = scsiPhyRx();
  242. fifoData[i + k + 86] = scsiPhyRx();
  243. fifoData[i + k + 87] = scsiPhyRx();
  244. fifoData[i + k + 88] = scsiPhyRx();
  245. fifoData[i + k + 89] = scsiPhyRx();
  246. fifoData[i + k + 90] = scsiPhyRx();
  247. fifoData[i + k + 91] = scsiPhyRx();
  248. fifoData[i + k + 92] = scsiPhyRx();
  249. fifoData[i + k + 93] = scsiPhyRx();
  250. fifoData[i + k + 94] = scsiPhyRx();
  251. fifoData[i + k + 95] = scsiPhyRx();
  252. fifoData[i + k + 96] = scsiPhyRx();
  253. fifoData[i + k + 97] = scsiPhyRx();
  254. fifoData[i + k + 98] = scsiPhyRx();
  255. fifoData[i + k + 99] = scsiPhyRx();
  256. fifoData[i + k + 100] = scsiPhyRx();
  257. fifoData[i + k + 101] = scsiPhyRx();
  258. fifoData[i + k + 102] = scsiPhyRx();
  259. fifoData[i + k + 103] = scsiPhyRx();
  260. fifoData[i + k + 104] = scsiPhyRx();
  261. fifoData[i + k + 105] = scsiPhyRx();
  262. fifoData[i + k + 106] = scsiPhyRx();
  263. fifoData[i + k + 107] = scsiPhyRx();
  264. fifoData[i + k + 108] = scsiPhyRx();
  265. fifoData[i + k + 109] = scsiPhyRx();
  266. fifoData[i + k + 110] = scsiPhyRx();
  267. fifoData[i + k + 111] = scsiPhyRx();
  268. fifoData[i + k + 112] = scsiPhyRx();
  269. fifoData[i + k + 113] = scsiPhyRx();
  270. fifoData[i + k + 114] = scsiPhyRx();
  271. fifoData[i + k + 115] = scsiPhyRx();
  272. fifoData[i + k + 116] = scsiPhyRx();
  273. fifoData[i + k + 117] = scsiPhyRx();
  274. fifoData[i + k + 118] = scsiPhyRx();
  275. fifoData[i + k + 119] = scsiPhyRx();
  276. fifoData[i + k + 120] = scsiPhyRx();
  277. fifoData[i + k + 121] = scsiPhyRx();
  278. fifoData[i + k + 122] = scsiPhyRx();
  279. fifoData[i + k + 123] = scsiPhyRx();
  280. fifoData[i + k + 124] = scsiPhyRx();
  281. fifoData[i + k + 125] = scsiPhyRx();
  282. fifoData[i + k + 126] = scsiPhyRx();
  283. fifoData[i + k + 127] = scsiPhyRx();
  284. }
  285. i += chunk16;
  286. }
  287. else
  288. {
  289. uint32_t chunk16 = count16 - i;
  290. uint32_t k = 0;
  291. for (; k + 4 <= chunk16; k += 4)
  292. {
  293. fifoData[i + k] = scsiPhyRx();
  294. fifoData[i + 1 + k] = scsiPhyRx();
  295. fifoData[i + 2 + k] = scsiPhyRx();
  296. fifoData[i + 3 + k] = scsiPhyRx();
  297. }
  298. for (; k < chunk16; ++k)
  299. {
  300. fifoData[i + k] = scsiPhyRx();
  301. }
  302. i += chunk16;
  303. }
  304. }
  305. *parityError |= scsiParityError();
  306. }
  307. void
  308. scsiRead(uint8_t* data, uint32_t count, int* parityError)
  309. {
  310. int i = 0;
  311. *parityError = 0;
  312. while (i < count && likely(!scsiDev.resetFlag))
  313. {
  314. uint32_t chunk = ((count - i) > SCSI_XFER_MAX)
  315. ? SCSI_XFER_MAX : (count - i);
  316. scsiSetDataCount(chunk);
  317. scsiReadPIO(data + i, chunk, parityError);
  318. __disable_irq();
  319. while (!scsiPhyComplete() && likely(!scsiDev.resetFlag))
  320. {
  321. __WFI();
  322. }
  323. __enable_irq();
  324. i += chunk;
  325. }
  326. }
  327. void
  328. scsiWriteByte(uint8_t value)
  329. {
  330. scsiSetDataCount(1);
  331. scsiPhyTx(value);
  332. __disable_irq();
  333. while (!scsiPhyComplete() && likely(!scsiDev.resetFlag))
  334. {
  335. __WFI();
  336. }
  337. __enable_irq();
  338. }
  339. void
  340. scsiWritePIO(const uint8_t* data, uint32_t count)
  341. {
  342. uint16_t* fifoData = (uint16_t*)data;
  343. uint32_t count16 = (count + 1) / 2;
  344. int i = 0;
  345. while ((i < count16) && likely(!scsiDev.resetFlag))
  346. {
  347. while (!scsiFifoReady() && likely(!scsiDev.resetFlag))
  348. {
  349. // Spin
  350. }
  351. if (count16 - i >= SCSI_FIFO_DEPTH16)
  352. {
  353. uint32_t chunk16 = SCSI_FIFO_DEPTH16;
  354. // Let gcc unroll the loop as much as possible.
  355. for (uint32_t k = 0; k + 128 <= chunk16; k += 128)
  356. {
  357. scsiPhyTx32(fifoData[i + k], fifoData[i + k + 1]);
  358. scsiPhyTx32(fifoData[i + 2 + k], fifoData[i + k + 3]);
  359. scsiPhyTx32(fifoData[i + 4 + k], fifoData[i + k + 5]);
  360. scsiPhyTx32(fifoData[i + 6 + k], fifoData[i + k + 7]);
  361. scsiPhyTx32(fifoData[i + 8 + k], fifoData[i + k + 9]);
  362. scsiPhyTx32(fifoData[i + 10 + k], fifoData[i + k + 11]);
  363. scsiPhyTx32(fifoData[i + 12 + k], fifoData[i + k + 13]);
  364. scsiPhyTx32(fifoData[i + 14 + k], fifoData[i + k + 15]);
  365. scsiPhyTx32(fifoData[i + 16 + k], fifoData[i + k + 17]);
  366. scsiPhyTx32(fifoData[i + 18 + k], fifoData[i + k + 19]);
  367. scsiPhyTx32(fifoData[i + 20 + k], fifoData[i + k + 21]);
  368. scsiPhyTx32(fifoData[i + 22 + k], fifoData[i + k + 23]);
  369. scsiPhyTx32(fifoData[i + 24 + k], fifoData[i + k + 25]);
  370. scsiPhyTx32(fifoData[i + 26 + k], fifoData[i + k + 27]);
  371. scsiPhyTx32(fifoData[i + 28 + k], fifoData[i + k + 29]);
  372. scsiPhyTx32(fifoData[i + 30 + k], fifoData[i + k + 31]);
  373. scsiPhyTx32(fifoData[i + 32 + k], fifoData[i + k + 33]);
  374. scsiPhyTx32(fifoData[i + 34 + k], fifoData[i + k + 35]);
  375. scsiPhyTx32(fifoData[i + 36 + k], fifoData[i + k + 37]);
  376. scsiPhyTx32(fifoData[i + 38 + k], fifoData[i + k + 39]);
  377. scsiPhyTx32(fifoData[i + 40 + k], fifoData[i + k + 41]);
  378. scsiPhyTx32(fifoData[i + 42 + k], fifoData[i + k + 43]);
  379. scsiPhyTx32(fifoData[i + 44 + k], fifoData[i + k + 45]);
  380. scsiPhyTx32(fifoData[i + 46 + k], fifoData[i + k + 47]);
  381. scsiPhyTx32(fifoData[i + 48 + k], fifoData[i + k + 49]);
  382. scsiPhyTx32(fifoData[i + 50 + k], fifoData[i + k + 51]);
  383. scsiPhyTx32(fifoData[i + 52 + k], fifoData[i + k + 53]);
  384. scsiPhyTx32(fifoData[i + 54 + k], fifoData[i + k + 55]);
  385. scsiPhyTx32(fifoData[i + 56 + k], fifoData[i + k + 57]);
  386. scsiPhyTx32(fifoData[i + 58 + k], fifoData[i + k + 59]);
  387. scsiPhyTx32(fifoData[i + 60 + k], fifoData[i + k + 61]);
  388. scsiPhyTx32(fifoData[i + 62 + k], fifoData[i + k + 63]);
  389. scsiPhyTx32(fifoData[i + 64 + k], fifoData[i + k + 65]);
  390. scsiPhyTx32(fifoData[i + 66 + k], fifoData[i + k + 67]);
  391. scsiPhyTx32(fifoData[i + 68 + k], fifoData[i + k + 69]);
  392. scsiPhyTx32(fifoData[i + 70 + k], fifoData[i + k + 71]);
  393. scsiPhyTx32(fifoData[i + 72 + k], fifoData[i + k + 73]);
  394. scsiPhyTx32(fifoData[i + 74 + k], fifoData[i + k + 75]);
  395. scsiPhyTx32(fifoData[i + 76 + k], fifoData[i + k + 77]);
  396. scsiPhyTx32(fifoData[i + 78 + k], fifoData[i + k + 79]);
  397. scsiPhyTx32(fifoData[i + 80 + k], fifoData[i + k + 81]);
  398. scsiPhyTx32(fifoData[i + 82 + k], fifoData[i + k + 83]);
  399. scsiPhyTx32(fifoData[i + 84 + k], fifoData[i + k + 85]);
  400. scsiPhyTx32(fifoData[i + 86 + k], fifoData[i + k + 87]);
  401. scsiPhyTx32(fifoData[i + 88 + k], fifoData[i + k + 89]);
  402. scsiPhyTx32(fifoData[i + 90 + k], fifoData[i + k + 91]);
  403. scsiPhyTx32(fifoData[i + 92 + k], fifoData[i + k + 93]);
  404. scsiPhyTx32(fifoData[i + 94 + k], fifoData[i + k + 95]);
  405. scsiPhyTx32(fifoData[i + 96 + k], fifoData[i + k + 97]);
  406. scsiPhyTx32(fifoData[i + 98 + k], fifoData[i + k + 99]);
  407. scsiPhyTx32(fifoData[i + 100 + k], fifoData[i + k + 101]);
  408. scsiPhyTx32(fifoData[i + 102 + k], fifoData[i + k + 103]);
  409. scsiPhyTx32(fifoData[i + 104 + k], fifoData[i + k + 105]);
  410. scsiPhyTx32(fifoData[i + 106 + k], fifoData[i + k + 107]);
  411. scsiPhyTx32(fifoData[i + 108 + k], fifoData[i + k + 109]);
  412. scsiPhyTx32(fifoData[i + 110 + k], fifoData[i + k + 111]);
  413. scsiPhyTx32(fifoData[i + 112 + k], fifoData[i + k + 113]);
  414. scsiPhyTx32(fifoData[i + 114 + k], fifoData[i + k + 115]);
  415. scsiPhyTx32(fifoData[i + 116 + k], fifoData[i + k + 117]);
  416. scsiPhyTx32(fifoData[i + 118 + k], fifoData[i + k + 119]);
  417. scsiPhyTx32(fifoData[i + 120 + k], fifoData[i + k + 121]);
  418. scsiPhyTx32(fifoData[i + 122 + k], fifoData[i + k + 123]);
  419. scsiPhyTx32(fifoData[i + 124 + k], fifoData[i + k + 125]);
  420. scsiPhyTx32(fifoData[i + 126 + k], fifoData[i + k + 127]);
  421. }
  422. i += chunk16;
  423. }
  424. else
  425. {
  426. uint32_t chunk16 = count16 - i;
  427. uint32_t k = 0;
  428. for (; k + 4 <= chunk16; k += 4)
  429. {
  430. scsiPhyTx32(fifoData[i + k], fifoData[i + k + 1]);
  431. scsiPhyTx32(fifoData[i + k + 2], fifoData[i + k + 3]);
  432. }
  433. for (; k < chunk16; ++k)
  434. {
  435. scsiPhyTx(fifoData[i + k]);
  436. }
  437. i += chunk16;
  438. }
  439. }
  440. }
  441. void
  442. scsiWrite(const uint8_t* data, uint32_t count)
  443. {
  444. int i = 0;
  445. while (i < count && likely(!scsiDev.resetFlag))
  446. {
  447. uint32_t chunk = ((count - i) > SCSI_XFER_MAX)
  448. ? SCSI_XFER_MAX : (count - i);
  449. scsiSetDataCount(chunk);
  450. scsiWritePIO(data + i, chunk);
  451. __disable_irq();
  452. while (!scsiPhyComplete() && likely(!scsiDev.resetFlag))
  453. {
  454. __WFI();
  455. }
  456. __enable_irq();
  457. i += chunk;
  458. }
  459. }
  460. static inline void busSettleDelay(void)
  461. {
  462. // Data Release time (switching IO) = 400ns
  463. // + Bus Settle time (switching phase) = 400ns.
  464. s2s_delay_us(1); // Close enough.
  465. }
  466. void scsiEnterBusFree()
  467. {
  468. *SCSI_CTRL_BSY = 0x00;
  469. // We now have a Bus Clear Delay of 800ns to release remaining signals.
  470. *SCSI_CTRL_PHASE = 0;
  471. }
  472. static void
  473. scsiSetTiming(
  474. uint8_t assertClocks,
  475. uint8_t deskew,
  476. uint8_t hold,
  477. uint8_t glitch)
  478. {
  479. *SCSI_CTRL_DESKEW = ((hold & 7) << 5) | (deskew & 0x1F);
  480. *SCSI_CTRL_TIMING = (assertClocks & 0x3F);
  481. *SCSI_CTRL_TIMING3 = (glitch & 0xF);
  482. }
  483. static void
  484. scsiSetDefaultTiming()
  485. {
  486. const uint8_t* asyncTiming = asyncTimings[0];
  487. scsiSetTiming(
  488. asyncTiming[0],
  489. asyncTiming[1],
  490. asyncTiming[2],
  491. asyncTiming[3]);
  492. }
  493. void scsiEnterPhase(int newPhase)
  494. {
  495. uint32_t delay = scsiEnterPhaseImmediate(newPhase);
  496. if (delay > 0)
  497. {
  498. s2s_delay_us(delay);
  499. }
  500. }
  501. // Returns microsecond delay
  502. uint32_t scsiEnterPhaseImmediate(int newPhase)
  503. {
  504. // ANSI INCITS 362-2002 SPI-3 10.7.1:
  505. // Phase changes are not allowed while REQ or ACK is asserted.
  506. while (likely(!scsiDev.resetFlag) && scsiStatusACK()) {}
  507. int oldPhase = *SCSI_CTRL_PHASE;
  508. if (newPhase != oldPhase)
  509. {
  510. if ((newPhase == DATA_IN || newPhase == DATA_OUT) &&
  511. scsiDev.target->syncOffset)
  512. {
  513. if (scsiDev.target->syncPeriod < 23)
  514. {
  515. scsiSetTiming(SCSI_FAST20_ASSERT, SCSI_FAST20_DESKEW, SCSI_FAST20_HOLD, 1);
  516. }
  517. else if (scsiDev.target->syncPeriod <= 25)
  518. {
  519. if (newPhase == DATA_IN)
  520. {
  521. scsiSetTiming(SCSI_FAST10_WRITE_ASSERT, SCSI_FAST10_DESKEW, SCSI_FAST10_HOLD, 1);
  522. }
  523. else
  524. {
  525. scsiSetTiming(SCSI_FAST10_READ_ASSERT, SCSI_FAST10_DESKEW, SCSI_FAST10_HOLD, 1);
  526. }
  527. }
  528. else
  529. {
  530. // Amiga A3000 OS3.9 sets period to 35 and fails with
  531. // glitch == 1.
  532. int glitch =
  533. scsiDev.target->syncPeriod < 35 ? 1 :
  534. (scsiDev.target->syncPeriod < 45 ? 2 : 5);
  535. int deskew = syncDeskew(scsiDev.target->syncPeriod);
  536. int assertion;
  537. if (newPhase == DATA_IN)
  538. {
  539. assertion = syncAssertionWrite(scsiDev.target->syncPeriod, deskew);
  540. }
  541. else
  542. {
  543. assertion = syncAssertionRead(scsiDev.target->syncPeriod);
  544. }
  545. scsiSetTiming(
  546. assertion,
  547. deskew,
  548. syncHold(scsiDev.target->syncPeriod),
  549. glitch);
  550. }
  551. *SCSI_CTRL_SYNC_OFFSET = scsiDev.target->syncOffset;
  552. }
  553. else if (newPhase >= 0)
  554. {
  555. *SCSI_CTRL_SYNC_OFFSET = 0;
  556. const uint8_t* asyncTiming;
  557. if (scsiDev.boardCfg.scsiSpeed == S2S_CFG_SPEED_NoLimit)
  558. {
  559. asyncTiming = asyncTimings[SCSI_ASYNC_SAFE];
  560. }
  561. else if (scsiDev.boardCfg.scsiSpeed >= S2S_CFG_SPEED_TURBO)
  562. {
  563. asyncTiming = asyncTimings[SCSI_ASYNC_TURBO];
  564. }
  565. else if (scsiDev.boardCfg.scsiSpeed >= S2S_CFG_SPEED_ASYNC_50)
  566. {
  567. asyncTiming = asyncTimings[SCSI_ASYNC_50];
  568. } else if (scsiDev.boardCfg.scsiSpeed >= S2S_CFG_SPEED_ASYNC_33) {
  569. asyncTiming = asyncTimings[SCSI_ASYNC_33];
  570. } else {
  571. asyncTiming = asyncTimings[SCSI_ASYNC_15];
  572. }
  573. scsiSetTiming(
  574. asyncTiming[0],
  575. asyncTiming[1],
  576. asyncTiming[2],
  577. asyncTiming[3]);
  578. }
  579. uint32_t delayUs = 0;
  580. if (newPhase >= 0)
  581. {
  582. *SCSI_CTRL_PHASE = newPhase;
  583. delayUs += 1; // busSettleDelay
  584. if (scsiDev.compatMode < COMPAT_SCSI2)
  585. {
  586. // EMU EMAX needs 100uS ! 10uS is not enough.
  587. delayUs += 100;
  588. }
  589. }
  590. else
  591. {
  592. *SCSI_CTRL_PHASE = 0;
  593. }
  594. return delayUs;
  595. }
  596. return 0; // No change
  597. }
  598. // Returns a "safe" estimate of the host SCSI speed of
  599. // theoretical speed / 2
  600. uint32_t s2s_getScsiRateKBs()
  601. {
  602. if (scsiDev.target->syncOffset)
  603. {
  604. if (scsiDev.target->syncPeriod < 23)
  605. {
  606. return 20 / 2;
  607. }
  608. else if (scsiDev.target->syncPeriod <= 25)
  609. {
  610. return 10 / 2;
  611. }
  612. else
  613. {
  614. // 1000000000 / (scsiDev.target->syncPeriod * 4) bytes per second
  615. // (1000000000 / (scsiDev.target->syncPeriod * 4)) / 1000 kB/s
  616. return (1000000 / (scsiDev.target->syncPeriod * 4)) / 2;
  617. }
  618. }
  619. else
  620. {
  621. return 0;
  622. }
  623. }
  624. void scsiPhyReset()
  625. {
  626. if (dmaInProgress)
  627. {
  628. HAL_DMA_Abort(&memToFSMC);
  629. HAL_DMA_Abort(&fsmcToMem);
  630. dmaInProgress = 0;
  631. }
  632. s2s_fpgaReset(); // Clears fifos etc.
  633. *SCSI_CTRL_PHASE = 0x00;
  634. *SCSI_CTRL_BSY = 0x00;
  635. *SCSI_CTRL_DBX = 0;
  636. *SCSI_CTRL_SYNC_OFFSET = 0;
  637. scsiSetDefaultTiming();
  638. // DMA Benchmark code
  639. // Currently 14.9MB/s.
  640. #ifdef DMA_BENCHMARK
  641. while(1)
  642. {
  643. s2s_ledOn();
  644. // 100MB
  645. for (int i = 0; i < (100LL * 1024 * 1024 / SCSI_FIFO_DEPTH); ++i)
  646. {
  647. HAL_DMA_Start(
  648. &memToFSMC,
  649. (uint32_t) &scsiDev.data[0],
  650. (uint32_t) SCSI_FIFO_DATA,
  651. SCSI_FIFO_DEPTH / 4);
  652. HAL_DMA_PollForTransfer(
  653. &memToFSMC,
  654. HAL_DMA_FULL_TRANSFER,
  655. 0xffffffff);
  656. s2s_fpgaReset();
  657. }
  658. s2s_ledOff();
  659. for(int i = 0; i < 10; ++i) s2s_delay_ms(1000);
  660. }
  661. #endif
  662. // PIO Benchmark code
  663. // Currently 16.7MB/s.
  664. //#define PIO_BENCHMARK 1
  665. #ifdef PIO_BENCHMARK
  666. while(1)
  667. {
  668. s2s_ledOn();
  669. scsiEnterPhase(DATA_IN); // Need IO flag set for fifo ready flag
  670. // 100MB
  671. for (int i = 0; i < (100LL * 1024 * 1024 / SCSI_FIFO_DEPTH); ++i)
  672. {
  673. scsiSetDataCount(1); // Resets fifos.
  674. // Shouldn't block
  675. scsiDev.resetFlag = 0;
  676. scsiWritePIO(&scsiDev.data[0], SCSI_FIFO_DEPTH);
  677. }
  678. s2s_ledOff();
  679. for(int i = 0; i < 10; ++i) s2s_delay_ms(1000);
  680. }
  681. #endif
  682. #ifdef SCSI_FREQ_TEST
  683. while(1)
  684. {
  685. *SCSI_CTRL_DBX = 0xAA;
  686. *SCSI_CTRL_DBX = 0x55;
  687. }
  688. #endif
  689. }
  690. static void scsiPhyInitDMA()
  691. {
  692. // One-time init only.
  693. static uint8_t init = 0;
  694. if (init == 0)
  695. {
  696. init = 1;
  697. // Memory to memory transfers can only be done using DMA2
  698. __DMA2_CLK_ENABLE();
  699. // Transmit SCSI data. The source data is treated as the
  700. // peripheral (even though this is memory-to-memory)
  701. memToFSMC.Instance = DMA2_Stream0;
  702. memToFSMC.Init.Channel = DMA_CHANNEL_0;
  703. memToFSMC.Init.Direction = DMA_MEMORY_TO_MEMORY;
  704. memToFSMC.Init.PeriphInc = DMA_PINC_ENABLE;
  705. memToFSMC.Init.MemInc = DMA_MINC_DISABLE;
  706. memToFSMC.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
  707. memToFSMC.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
  708. memToFSMC.Init.Mode = DMA_NORMAL;
  709. memToFSMC.Init.Priority = DMA_PRIORITY_LOW;
  710. // FIFO mode is needed to allow conversion from 32bit words to the
  711. // 16bit FSMC interface.
  712. memToFSMC.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
  713. // We only use 1 word (4 bytes) in the fifo at a time. Normally it's
  714. // better to let the DMA fifo fill up then do burst transfers, but
  715. // bursting out the FSMC interface will be very slow and may starve
  716. // other (faster) transfers. We don't want to risk the SDIO transfers
  717. // from overrun/underrun conditions.
  718. memToFSMC.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_1QUARTERFULL;
  719. memToFSMC.Init.MemBurst = DMA_MBURST_SINGLE;
  720. memToFSMC.Init.PeriphBurst = DMA_PBURST_SINGLE;
  721. HAL_DMA_Init(&memToFSMC);
  722. // Receive SCSI data. The source data (fsmc) is treated as the
  723. // peripheral (even though this is memory-to-memory)
  724. fsmcToMem.Instance = DMA2_Stream1;
  725. fsmcToMem.Init.Channel = DMA_CHANNEL_0;
  726. fsmcToMem.Init.Direction = DMA_MEMORY_TO_MEMORY;
  727. fsmcToMem.Init.PeriphInc = DMA_PINC_DISABLE;
  728. fsmcToMem.Init.MemInc = DMA_MINC_ENABLE;
  729. fsmcToMem.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
  730. fsmcToMem.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
  731. fsmcToMem.Init.Mode = DMA_NORMAL;
  732. fsmcToMem.Init.Priority = DMA_PRIORITY_LOW;
  733. fsmcToMem.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
  734. fsmcToMem.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_1QUARTERFULL;
  735. fsmcToMem.Init.MemBurst = DMA_MBURST_SINGLE;
  736. fsmcToMem.Init.PeriphBurst = DMA_PBURST_SINGLE;
  737. HAL_DMA_Init(&fsmcToMem);
  738. // TODO configure IRQs
  739. }
  740. }
  741. void scsiPhyInit()
  742. {
  743. scsiPhyInitDMA();
  744. *SCSI_CTRL_IDMASK = 0x00; // Reset in scsiPhyConfig
  745. *SCSI_CTRL_PHASE = 0x00;
  746. *SCSI_CTRL_BSY = 0x00;
  747. *SCSI_CTRL_DBX = 0;
  748. *SCSI_CTRL_SYNC_OFFSET = 0;
  749. scsiSetDefaultTiming();
  750. *SCSI_CTRL_SEL_TIMING = SCSI_DEFAULT_SELECTION;
  751. }
  752. void scsiPhyConfig()
  753. {
  754. if (scsiDev.boardCfg.flags6 & S2S_CFG_ENABLE_TERMINATOR)
  755. {
  756. HAL_GPIO_WritePin(nTERM_EN_GPIO_Port, nTERM_EN_Pin, GPIO_PIN_RESET);
  757. }
  758. else
  759. {
  760. HAL_GPIO_WritePin(nTERM_EN_GPIO_Port, nTERM_EN_Pin, GPIO_PIN_SET);
  761. }
  762. uint8_t idMask = 0;
  763. for (int i = 0; i < 8; ++i)
  764. {
  765. const S2S_TargetCfg* cfg = s2s_getConfigById(i);
  766. if (cfg && (cfg->scsiId & S2S_CFG_TARGET_ENABLED))
  767. {
  768. idMask |= (1 << i);
  769. }
  770. }
  771. *SCSI_CTRL_IDMASK = idMask;
  772. *SCSI_CTRL_FLAGS =
  773. ((scsiDev.boardCfg.flags & S2S_CFG_DISABLE_GLITCH) ?
  774. SCSI_CTRL_FLAGS_DISABLE_GLITCH : 0) |
  775. ((scsiDev.boardCfg.flags & S2S_CFG_ENABLE_PARITY) ?
  776. SCSI_CTRL_FLAGS_ENABLE_PARITY : 0);
  777. *SCSI_CTRL_SEL_TIMING =
  778. (scsiDev.boardCfg.flags & S2S_CFG_ENABLE_SEL_LATCH) ?
  779. SCSI_FAST_SELECTION : SCSI_DEFAULT_SELECTION;
  780. }
  781. // 1 = DBx error
  782. // 2 = Parity error
  783. // 4 = MSG error
  784. // 8 = CD error
  785. // 16 = IO error
  786. // 32 = other error
  787. // 64 = fpga comms error
  788. int scsiSelfTest()
  789. {
  790. if (scsiDev.phase != BUS_FREE)
  791. {
  792. return 32;
  793. }
  794. // Acquire the SCSI bus.
  795. for (int i = 0; i < 100; ++i)
  796. {
  797. if (scsiStatusBSY())
  798. {
  799. s2s_delay_ms(1);
  800. }
  801. }
  802. if (scsiStatusBSY())
  803. {
  804. // Error, couldn't acquire scsi bus
  805. return 32;
  806. }
  807. *SCSI_CTRL_BSY = 1;
  808. s2s_delay_ms(1);
  809. if (! scsiStatusBSY())
  810. {
  811. *SCSI_CTRL_BSY = 0;
  812. // Error, BSY doesn't work.
  813. return 32;
  814. }
  815. // Should be safe to use the bus now.
  816. int result = 0;
  817. *SCSI_CTRL_DBX = 0;
  818. busSettleDelay();
  819. if ((*SCSI_STS_DBX & 0xff) != 0)
  820. {
  821. result = 1;
  822. }
  823. *SCSI_CTRL_BSY = 0;
  824. return result;
  825. }