scsi_accel_sync.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2022-2025 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. /* Synchronous mode SCSI implementation.
  22. *
  23. * In synchronous mode, the handshake mechanism is not used. Instead
  24. * either end of the communication will just send a bunch of bytes
  25. * and only afterwards checks that the number of acknowledgement
  26. * pulses matches.
  27. *
  28. * The receiving end should latch in the data at the falling edge of
  29. * the request pulse (on either REQ or ACK pin). We use the GD32 EXMC
  30. * peripheral to implement this latching with the NWAIT pin when
  31. * reading data from the host. NOE is used to generate the REQ pulses.
  32. *
  33. * Writing data to the host is simpler, as we can just write it out
  34. * from the GPIO port at our own pace. A timer is used for generating
  35. * the output pulses on REQ pin.
  36. */
  37. #include "scsi_accel_sync.h"
  38. #include <ZuluSCSI_log.h>
  39. #include <gd32f4xx_exmc.h>
  40. #include <scsi.h>
  41. #ifndef SCSI_SYNC_MODE_AVAILABLE
  42. void scsi_accel_sync_init() {}
  43. void scsi_accel_sync_recv(uint8_t *data, uint32_t count, int* parityError, volatile int *resetFlag) {}
  44. void scsi_accel_sync_send(const uint8_t* data, uint32_t count, volatile int *resetFlag) {}
  45. #else
  46. /********************************/
  47. /* Transfer from host to device */
  48. /********************************/
  49. #define SYNC_DMA_BUFSIZE 512
  50. static uint32_t g_sync_dma_buf[SYNC_DMA_BUFSIZE];
  51. void scsi_accel_sync_init()
  52. {
  53. rcu_periph_clock_enable(RCU_EXMC);
  54. rcu_periph_clock_enable(SCSI_EXMC_DMA_RCU);
  55. rcu_periph_clock_enable(SCSI_SYNC_TIMER_RCU);
  56. exmc_norsram_timing_parameter_struct timing_param = {
  57. .asyn_access_mode = EXMC_ACCESS_MODE_A,
  58. .syn_data_latency = EXMC_DATALAT_2_CLK,
  59. .syn_clk_division = EXMC_SYN_CLOCK_RATIO_2_CLK,
  60. .bus_latency = 1,
  61. .asyn_data_setuptime = 2,
  62. .asyn_address_holdtime = 2,
  63. .asyn_address_setuptime = 16
  64. };
  65. exmc_norsram_parameter_struct sram_param = {
  66. .norsram_region = EXMC_BANK0_NORSRAM_REGION0,
  67. .write_mode = EXMC_ASYN_WRITE,
  68. .extended_mode = DISABLE,
  69. .asyn_wait = ENABLE,
  70. .nwait_signal = ENABLE,
  71. .memory_write = DISABLE,
  72. .nwait_config = EXMC_NWAIT_CONFIG_DURING,
  73. .wrap_burst_mode = DISABLE,
  74. .nwait_polarity = EXMC_NWAIT_POLARITY_HIGH,
  75. .burst_mode = DISABLE,
  76. .databus_width = EXMC_NOR_DATABUS_WIDTH_16B,
  77. .memory_type = EXMC_MEMORY_TYPE_SRAM,
  78. .address_data_mux = DISABLE,
  79. .read_write_timing = &timing_param
  80. };
  81. EXMC_SNCTL(EXMC_BANK0_NORSRAM_REGION0) &= ~EXMC_SNCTL_NRBKEN;
  82. exmc_norsram_init(&sram_param);
  83. // DMA used to transfer data from EXMC to RAM
  84. // DMA is used so that if data transfer fails, we can at least abort by resetting CPU.
  85. // Accessing EXMC from the CPU directly hangs it totally if ACK pulses are not received.
  86. // TODO: Figure out why DMA does not work correctly with EXMC on GD32F450
  87. // dma_single_data_parameter_struct exmc_dma_config =
  88. // {
  89. // .periph_addr = EXMC_NOR_PSRAM,
  90. // .periph_inc = DMA_PERIPH_INCREASE_DISABLE,
  91. // .memory0_addr = (uint32_t)g_sync_dma_buf,
  92. // .memory_inc = DMA_MEMORY_INCREASE_ENABLE,
  93. // .periph_memory_width = DMA_PERIPH_WIDTH_16BIT,
  94. // .circular_mode = DMA_CIRCULAR_MODE_DISABLE,
  95. // .direction = DMA_MEMORY_TO_MEMORY,
  96. // .number = 0, // Filled before transfer
  97. // .priority = DMA_PRIORITY_MEDIUM
  98. // };
  99. // dma_single_data_mode_init(SCSI_EXMC_DMA, SCSI_EXMC_DMACH, &exmc_dma_config);
  100. gpio_mode_set(SCSI_IN_ACK_EXMC_NWAIT_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, SCSI_IN_ACK_EXMC_NWAIT_PIN);
  101. gpio_output_options_set(SCSI_IN_ACK_EXMC_NWAIT_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_200MHZ, SCSI_IN_ACK_EXMC_NWAIT_PIN);
  102. gpio_af_set(SCSI_IN_ACK_EXMC_NWAIT_PORT, GPIO_AF_12, SCSI_IN_ACK_EXMC_NWAIT_PIN);
  103. // TIMER1 CH0 port and pin enable
  104. gpio_mode_set(SCSI_ACK_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, SCSI_ACK_PIN);
  105. gpio_af_set(SCSI_ACK_PORT, GPIO_AF_1, SCSI_ACK_PIN);
  106. // TIMER1 is used to count ACK pulses
  107. TIMER_CTL0(SCSI_SYNC_TIMER) = 0;
  108. TIMER_SMCFG(SCSI_SYNC_TIMER) = TIMER_SLAVE_MODE_EXTERNAL0 | TIMER_SMCFG_TRGSEL_CI0FE0;
  109. TIMER_CAR(SCSI_SYNC_TIMER) = 65535;
  110. TIMER_PSC(SCSI_SYNC_TIMER) = 0;
  111. TIMER_CHCTL0(SCSI_SYNC_TIMER) = 0x0001; // CH0 as input
  112. }
  113. void scsi_accel_sync_recv(uint8_t *data, uint32_t count, int* parityError, volatile int *resetFlag)
  114. {
  115. // Set SCSI data IN pins to external memory mode
  116. gpio_mode_set(SCSI_IN_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, SCSI_IN_MASK);
  117. gpio_output_options_set(SCSI_IN_PORT, GPIO_PUPD_NONE, GPIO_OSPEED_200MHZ, SCSI_IN_MASK);
  118. gpio_af_set(SCSI_IN_PORT, GPIO_AF_12, SCSI_IN_MASK);
  119. // Enable EXMC to drive REQ from EXMC_NOE pin
  120. EXMC_SNCTL(EXMC_BANK0_NORSRAM_REGION0) |= EXMC_SNCTL_NRBKEN;
  121. // save GPIO registers to restore after method is done
  122. uint32_t oldmode_gpio_ctl = GPIO_CTL(SCSI_OUT_REQ_EXMC_NOE_PORT);
  123. uint32_t oldmode_gpio_pud = GPIO_PUD(SCSI_OUT_REQ_EXMC_NOE_PORT);
  124. uint32_t oldmode_gpio_ospd = GPIO_OSPD(SCSI_OUT_REQ_EXMC_NOE_PORT);
  125. uint32_t oldmode_gpio_omode = GPIO_OMODE(SCSI_OUT_REQ_EXMC_NOE_PORT);
  126. uint32_t oldmode_gpio_af = GPIO_AFSEL0(SCSI_OUT_REQ_EXMC_NOE_PORT);
  127. gpio_af_set(SCSI_OUT_REQ_EXMC_NOE_PORT, GPIO_AF_12, SCSI_OUT_REQ_EXMC_NOE_PIN);
  128. gpio_output_options_set(SCSI_OUT_REQ_EXMC_NOE_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_200MHZ, SCSI_OUT_REQ_EXMC_NOE_PIN);
  129. gpio_mode_set(SCSI_OUT_REQ_EXMC_NOE_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, SCSI_OUT_REQ_EXMC_NOE_PIN);
  130. while (count > 0)
  131. {
  132. uint32_t blocksize = (count > SYNC_DMA_BUFSIZE * 2) ? (SYNC_DMA_BUFSIZE * 2) : count;
  133. count -= blocksize;
  134. // dma_memory_address_config(SCSI_EXMC_DMA, SCSI_EXMC_DMACH, 0, (uint32_t)g_sync_dma_buf);
  135. // dma_transfer_number_config(SCSI_EXMC_DMA, SCSI_EXMC_DMACH, blocksize);
  136. // dma_channel_enable(SCSI_EXMC_DMA, SCSI_EXMC_DMACH);
  137. uint16_t *src = (uint16_t*)g_sync_dma_buf;
  138. uint8_t *dst = data;
  139. uint8_t *end = data + blocksize;
  140. uint32_t start = millis();
  141. while (dst < end)
  142. {
  143. // Read from EXMC and write to internal RAM
  144. // Note that this will hang the CPU if host does not send ACK pulses.
  145. uint16_t word = *(uint16_t*)EXMC_NOR_PSRAM;
  146. *dst++ = (~word) >> SCSI_EXMC_DATA_SHIFT;
  147. // TODO: Figure out why DMA does not work correctly with EXMC on GD32F450
  148. // uint32_t remain = DMA_CHCNT(SCSI_EXMC_DMA, SCSI_EXMC_DMACH);
  149. // while (dst < end - remain)
  150. // {
  151. // *dst++ = ~(*src++) >> SCSI_EXMC_DATA_SHIFT;
  152. // }
  153. // if ((uint32_t)(millis() - start) > 500 || *resetFlag)
  154. // {
  155. // // We are in a pinch here: without ACK pulses coming, the EXMC and DMA peripherals
  156. // // are locked up. The only way out is a whole system reset.
  157. // logmsg("SCSI Synchronous read timeout: resetting system");
  158. // NVIC_SystemReset();
  159. // }
  160. }
  161. // dma_channel_disable(SCSI_EXMC_DMA, SCSI_EXMC_DMACH);
  162. data = end;
  163. }
  164. // Set SCSI data IN pins back to input mode
  165. gpio_mode_set(SCSI_IN_PORT, GPIO_MODE_INPUT, GPIO_PUPD_NONE, SCSI_IN_MASK);
  166. EXMC_SNCTL(EXMC_BANK0_NORSRAM_REGION0) &= ~EXMC_SNCTL_NRBKEN;
  167. GPIO_CTL(SCSI_OUT_REQ_EXMC_NOE_PORT) = oldmode_gpio_ctl;
  168. GPIO_OSPD(SCSI_OUT_REQ_EXMC_NOE_PORT) = oldmode_gpio_ospd;
  169. GPIO_OMODE(SCSI_OUT_REQ_EXMC_NOE_PORT) = oldmode_gpio_omode;
  170. GPIO_PUD(SCSI_OUT_REQ_EXMC_NOE_PORT) = oldmode_gpio_pud;
  171. GPIO_AFSEL0(SCSI_OUT_REQ_EXMC_NOE_PORT) = oldmode_gpio_af;
  172. }
  173. /********************************/
  174. /* Transfer from device to host */
  175. /********************************/
  176. // Simple delay, about 20 ns.
  177. // This is less likely to get optimized away by CPU pipeline than nop
  178. #define ASM_DELAY() \
  179. " ldr %[tmp2], [%[reset_flag]] \n"
  180. // Take 8 bits from d and format them for writing
  181. // d is name of data operand, b is bit offset
  182. #define ASM_LOAD_DATA(b) \
  183. " ubfx %[tmp1], %[data], #" b ", #8 \n" \
  184. " ldr %[tmp1], [%[byte_lookup], %[tmp1], lsl #2] \n"
  185. // Write data to SCSI port and set REQ high
  186. #define ASM_SEND_DATA() \
  187. " str %[tmp1], [%[out_port_bop]] \n"
  188. // Set REQ low
  189. #define ASM_SET_REQ_LOW() \
  190. " mov %[tmp2], %[bop_req_low] \n" \
  191. " str %[tmp2], [%[out_port_bop]] \n"
  192. // Wait for ACK_TIMER - n to be less than num_bytes
  193. #define ASM_WAIT_ACK_TIMER(n) \
  194. "wait_acks_" n "_%=: \n" \
  195. " ldr %[tmp2], [%[ack_timer]] \n" \
  196. " sub %[tmp2], # " n " \n" \
  197. " cmp %[tmp2], %[num_bytes] \n" \
  198. " ble got_acks_" n "_%= \n" \
  199. " ldr %[tmp2], [%[reset_flag]] \n" \
  200. " cmp %[tmp2], #0 \n" \
  201. " bne all_done_%= \n" \
  202. " b wait_acks_" n "_%= \n" \
  203. "got_acks_" n "_%=: \n"
  204. // Send 4 bytes
  205. #define ASM_SEND_4BYTES() \
  206. ASM_LOAD_DATA("0") \
  207. ASM_SEND_DATA() \
  208. ASM_DELAY1() \
  209. ASM_SET_REQ_LOW() \
  210. ASM_DELAY2() \
  211. ASM_LOAD_DATA("8") \
  212. ASM_SEND_DATA() \
  213. ASM_DELAY1() \
  214. ASM_SET_REQ_LOW() \
  215. ASM_DELAY2() \
  216. ASM_LOAD_DATA("16") \
  217. ASM_SEND_DATA() \
  218. ASM_DELAY1() \
  219. ASM_SET_REQ_LOW() \
  220. ASM_DELAY2() \
  221. ASM_LOAD_DATA("24") \
  222. ASM_SEND_DATA() \
  223. ASM_DELAY1() \
  224. ASM_SET_REQ_LOW()
  225. // Send 1 byte, wait for ACK_TIMER to be less than num_bytes + n and send 3 bytes more
  226. // This interleaving minimizes the delay caused by WAIT_ACK_TIMER.
  227. #define ASM_SEND_4BYTES_WAIT(n) \
  228. ASM_LOAD_DATA("0") \
  229. ASM_SEND_DATA() \
  230. ASM_DELAY2() \
  231. ASM_LOAD_DATA("8") \
  232. ASM_SET_REQ_LOW() \
  233. ASM_DELAY2() \
  234. " ldr %[tmp2], [%[ack_timer]] \n" \
  235. " sub %[tmp2], # " n " \n" \
  236. ASM_SEND_DATA() \
  237. " cmp %[tmp2], %[num_bytes] \n" \
  238. " ble got_acks_" n "_%= \n" \
  239. ASM_WAIT_ACK_TIMER(n) \
  240. ASM_DELAY2() \
  241. ASM_SET_REQ_LOW() \
  242. ASM_DELAY2() \
  243. ASM_LOAD_DATA("16") \
  244. ASM_SEND_DATA() \
  245. ASM_DELAY1() \
  246. ASM_SET_REQ_LOW() \
  247. ASM_DELAY2() \
  248. ASM_LOAD_DATA("24") \
  249. ASM_SEND_DATA() \
  250. ASM_DELAY1() \
  251. ASM_SET_REQ_LOW() \
  252. // Specialized routine for settings:
  253. // <=100 ns period, >=15 outstanding REQs
  254. static void sync_send_100ns_15off(const uint8_t *buf, uint32_t num_bytes, volatile int *resetFlag)
  255. {
  256. volatile uint32_t *out_port_bop = (volatile uint32_t*)&GPIO_BOP(SCSI_OUT_PORT);
  257. volatile uint32_t *ack_timer = &TIMER_CNT(SCSI_SYNC_TIMER);
  258. const uint32_t *byte_lookup = g_scsi_out_byte_to_bop;
  259. register uint32_t tmp1 = 0;
  260. register uint32_t tmp2 = 0;
  261. register uint32_t data = 0;
  262. // Delay 1 is typically longest and delay 2 shortest.
  263. // Tuning these is just trial and error.
  264. #define ASM_DELAY1() ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY() \
  265. ASM_DELAY() ASM_DELAY()
  266. #define ASM_DELAY2() ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY()
  267. asm volatile (
  268. "main_loop_%=: \n"
  269. " subs %[num_bytes], %[num_bytes], #16 \n"
  270. " bmi last_bytes_%= \n"
  271. /* At each point make sure there is at most 15 bytes in flight */
  272. " ldr %[data], [%[buf]], #4 \n"
  273. ASM_SEND_4BYTES_WAIT("26")
  274. ASM_DELAY2()
  275. " ldr %[data], [%[buf]], #4 \n"
  276. ASM_SEND_4BYTES_WAIT("22")
  277. ASM_DELAY2()
  278. " ldr %[data], [%[buf]], #4 \n"
  279. ASM_SEND_4BYTES_WAIT("18")
  280. ASM_DELAY2()
  281. " ldr %[data], [%[buf]], #4 \n"
  282. ASM_SEND_4BYTES_WAIT("14")
  283. ASM_DELAY2()
  284. " cbz %[num_bytes], all_done_%= \n"
  285. " b main_loop_%= \n"
  286. "last_bytes_%=: \n"
  287. " add %[num_bytes], %[num_bytes], #16 \n"
  288. "last_bytes_loop_%=: \n"
  289. " ldrb %[data], [%[buf]], #1 \n"
  290. ASM_LOAD_DATA("0")
  291. ASM_WAIT_ACK_TIMER("15")
  292. ASM_SEND_DATA()
  293. ASM_DELAY1()
  294. ASM_SET_REQ_LOW()
  295. " subs %[num_bytes], %[num_bytes], #1 \n"
  296. " bne last_bytes_loop_%= \n"
  297. "all_done_%=: \n"
  298. ASM_DELAY()
  299. : /* Output */ [tmp1] "+l" (tmp1), [tmp2] "+l" (tmp2), [data] "+r" (data),
  300. [buf] "+r" (buf), [num_bytes] "+r" (num_bytes)
  301. : /* Input */ [ack_timer] "r" (ack_timer),
  302. [bop_req_low] "I" (SCSI_OUT_REQ << 16),
  303. [out_port_bop] "r"(out_port_bop),
  304. [byte_lookup] "r" (byte_lookup),
  305. [reset_flag] "r" (resetFlag)
  306. : /* Clobber */);
  307. #undef ASM_DELAY1
  308. #undef ASM_DELAY2
  309. SCSI_RELEASE_DATA_REQ();
  310. }
  311. // Specialized routine for settings:
  312. // <=200 ns period, >=15 outstanding REQs
  313. static void sync_send_200ns_15off(const uint8_t *buf, uint32_t num_bytes, volatile int *resetFlag)
  314. {
  315. volatile uint32_t *out_port_bop = (volatile uint32_t*)&GPIO_BOP(SCSI_OUT_PORT);
  316. volatile uint32_t *ack_timer = &TIMER_CNT(SCSI_SYNC_TIMER);
  317. const uint32_t *byte_lookup = g_scsi_out_byte_to_bop;
  318. register uint32_t tmp1 = 0;
  319. register uint32_t tmp2 = 0;
  320. register uint32_t data = 0;
  321. #define ASM_DELAY1() ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY() \
  322. ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY() \
  323. ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY() \
  324. ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY() \
  325. ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY()
  326. #define ASM_DELAY2() ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY() \
  327. ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY() \
  328. ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY()
  329. asm volatile (
  330. "main_loop_%=: \n"
  331. " subs %[num_bytes], %[num_bytes], #16 \n"
  332. " bmi last_bytes_%= \n"
  333. /* At each point make sure there is at most 15 bytes in flight */
  334. " ldr %[data], [%[buf]], #4 \n"
  335. ASM_SEND_4BYTES_WAIT("26")
  336. ASM_DELAY2()
  337. " ldr %[data], [%[buf]], #4 \n"
  338. ASM_SEND_4BYTES_WAIT("22")
  339. ASM_DELAY2()
  340. " ldr %[data], [%[buf]], #4 \n"
  341. ASM_SEND_4BYTES_WAIT("18")
  342. ASM_DELAY2()
  343. " ldr %[data], [%[buf]], #4 \n"
  344. ASM_SEND_4BYTES_WAIT("14")
  345. " cbz %[num_bytes], all_done_%= \n"
  346. " b main_loop_%= \n"
  347. "last_bytes_%=: \n"
  348. " add %[num_bytes], %[num_bytes], #16 \n"
  349. "last_bytes_loop_%=: \n"
  350. " ldrb %[data], [%[buf]], #1 \n"
  351. ASM_LOAD_DATA("0")
  352. ASM_WAIT_ACK_TIMER("15")
  353. ASM_SEND_DATA()
  354. ASM_DELAY1()
  355. ASM_SET_REQ_LOW()
  356. ASM_DELAY2()
  357. " subs %[num_bytes], %[num_bytes], #1 \n"
  358. " bne last_bytes_loop_%= \n"
  359. "all_done_%=: \n"
  360. ASM_DELAY1()
  361. : /* Output */ [tmp1] "+l" (tmp1), [tmp2] "+l" (tmp2), [data] "+r" (data),
  362. [buf] "+r" (buf), [num_bytes] "+r" (num_bytes)
  363. : /* Input */ [ack_timer] "r" (ack_timer),
  364. [bop_req_low] "I" (SCSI_OUT_REQ << 16),
  365. [out_port_bop] "r"(out_port_bop),
  366. [byte_lookup] "r" (byte_lookup),
  367. [reset_flag] "r" (resetFlag)
  368. : /* Clobber */);
  369. #undef ASM_DELAY1
  370. #undef ASM_DELAY2
  371. SCSI_RELEASE_DATA_REQ();
  372. }
  373. // Specialized routine for settings:
  374. // <=260 ns period, >=7 outstanding REQs
  375. static void sync_send_260ns_7off(const uint8_t *buf, uint32_t num_bytes, volatile int *resetFlag)
  376. {
  377. volatile uint32_t *out_port_bop = (volatile uint32_t*)&GPIO_BOP(SCSI_OUT_PORT);
  378. volatile uint32_t *ack_timer = &TIMER_CNT(SCSI_SYNC_TIMER);
  379. const uint32_t *byte_lookup = g_scsi_out_byte_to_bop;
  380. register uint32_t tmp1 = 0;
  381. register uint32_t tmp2 = 0;
  382. register uint32_t data = 0;
  383. #define ASM_DELAY1() ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY() \
  384. ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY() \
  385. ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY() \
  386. ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY() \
  387. ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY() \
  388. ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY()
  389. #define ASM_DELAY2() ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY() \
  390. ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY() \
  391. ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY() \
  392. ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY()
  393. asm volatile (
  394. "main_loop_%=: \n"
  395. " subs %[num_bytes], %[num_bytes], #4 \n"
  396. " bmi last_bytes_%= \n"
  397. /* At each point make sure there is at most 3 bytes in flight */
  398. " ldr %[data], [%[buf]], #4 \n"
  399. ASM_SEND_4BYTES_WAIT("7")
  400. ASM_DELAY2()
  401. " cbz %[num_bytes], all_done_%= \n"
  402. " b main_loop_%= \n"
  403. "last_bytes_%=: \n"
  404. " add %[num_bytes], %[num_bytes], #4 \n"
  405. "last_bytes_loop_%=: \n"
  406. " ldrb %[data], [%[buf]], #1 \n"
  407. ASM_LOAD_DATA("0")
  408. ASM_WAIT_ACK_TIMER("5")
  409. ASM_SEND_DATA()
  410. ASM_DELAY1()
  411. ASM_SET_REQ_LOW()
  412. ASM_DELAY2()
  413. " subs %[num_bytes], %[num_bytes], #1 \n"
  414. " bne last_bytes_loop_%= \n"
  415. "all_done_%=: \n"
  416. ASM_DELAY1()
  417. : /* Output */ [tmp1] "+l" (tmp1), [tmp2] "+l" (tmp2), [data] "+r" (data),
  418. [buf] "+r" (buf), [num_bytes] "+r" (num_bytes)
  419. : /* Input */ [ack_timer] "r" (ack_timer),
  420. [bop_req_low] "I" (SCSI_OUT_REQ << 16),
  421. [out_port_bop] "r"(out_port_bop),
  422. [byte_lookup] "r" (byte_lookup),
  423. [reset_flag] "r" (resetFlag)
  424. : /* Clobber */);
  425. #undef ASM_DELAY1
  426. #undef ASM_DELAY2
  427. SCSI_RELEASE_DATA_REQ();
  428. }
  429. void scsi_accel_sync_send(const uint8_t* data, uint32_t count, volatile int *resetFlag)
  430. {
  431. // Timer counts down from the initial number of bytes.
  432. TIMER_CNT(SCSI_SYNC_TIMER) = count;
  433. TIMER_CTL0(SCSI_SYNC_TIMER) = TIMER_CTL0_CEN | TIMER_CTL0_DIR;
  434. int syncOffset = scsiDev.target->syncOffset;
  435. int syncPeriod = scsiDev.target->syncPeriod;
  436. if (syncOffset >= 15 && syncPeriod <= 25)
  437. {
  438. sync_send_100ns_15off(data, count, resetFlag);
  439. }
  440. else if (syncOffset >= 15 && syncPeriod <= 50)
  441. {
  442. sync_send_200ns_15off(data, count, resetFlag);
  443. }
  444. else if (syncOffset >= 7 && syncPeriod <= 65)
  445. {
  446. sync_send_260ns_7off(data, count, resetFlag);
  447. }
  448. else
  449. {
  450. dbgmsg("No optimized routine for syncOffset=", syncOffset, " syndPeriod=", syncPeriod, ", using fallback");
  451. while (count-- > 0)
  452. {
  453. while (TIMER_CNT(SCSI_SYNC_TIMER) > count + syncOffset && !*resetFlag);
  454. SCSI_OUT_DATA(*data++);
  455. delay_ns(syncPeriod * 2);
  456. SCSI_OUT(REQ, 1);
  457. delay_ns(syncPeriod * 2);
  458. }
  459. delay_ns(syncPeriod * 2);
  460. SCSI_RELEASE_DATA_REQ();
  461. }
  462. while (TIMER_CNT(SCSI_SYNC_TIMER) > 0 && !*resetFlag);
  463. if (*resetFlag)
  464. {
  465. dbgmsg("Bus reset during sync transfer, total ", (int)count,
  466. " bytes, remaining ACK count ", (int)TIMER_CNT(SCSI_SYNC_TIMER));
  467. }
  468. TIMER_CTL0(SCSI_SYNC_TIMER) = 0;
  469. }
  470. #endif