scsiPhy.c 25 KB

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