scsi_accel_sync.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. /* 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_af_set(SCSI_IN_ACK_EXMC_NWAIT_PORT, GPIO_AF_12, SCSI_IN_ACK_EXMC_NWAIT_PIN);
  102. // TIMER1 CH0 port and pin enable
  103. gpio_mode_set(SCSI_ACK_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, SCSI_ACK_PIN);
  104. gpio_af_set(SCSI_ACK_PORT, GPIO_AF_1, SCSI_ACK_PIN);
  105. // TIMER1 is used to count ACK pulses
  106. TIMER_CTL0(SCSI_SYNC_TIMER) = 0;
  107. TIMER_SMCFG(SCSI_SYNC_TIMER) = TIMER_SLAVE_MODE_EXTERNAL0 | TIMER_SMCFG_TRGSEL_CI0FE0;
  108. TIMER_CAR(SCSI_SYNC_TIMER) = 65535;
  109. TIMER_PSC(SCSI_SYNC_TIMER) = 0;
  110. TIMER_CHCTL0(SCSI_SYNC_TIMER) = 0x0001; // CH0 as input
  111. }
  112. void scsi_accel_sync_recv(uint8_t *data, uint32_t count, int* parityError, volatile int *resetFlag)
  113. {
  114. // Enable EXMC to drive REQ from EXMC_NOE pin
  115. EXMC_SNCTL(EXMC_BANK0_NORSRAM_REGION0) |= EXMC_SNCTL_NRBKEN;
  116. // save GPIO registers to restore after method is done
  117. uint32_t oldmode_gpio_ctl = GPIO_CTL(SCSI_OUT_REQ_EXMC_NOE_PORT);
  118. uint32_t oldmode_gpio_pud = GPIO_PUD(SCSI_OUT_REQ_EXMC_NOE_PORT);
  119. uint32_t oldmode_gpio_ospd = GPIO_OSPD(SCSI_OUT_REQ_EXMC_NOE_PORT);
  120. uint32_t oldmode_gpio_omode = GPIO_OMODE(SCSI_OUT_REQ_EXMC_NOE_PORT);
  121. uint32_t oldmode_gpio_af = GPIO_AFSEL0(SCSI_OUT_REQ_EXMC_NOE_PORT);
  122. // @TODO figure out ouput speed should be set to 200MHz
  123. gpio_af_set(SCSI_OUT_REQ_EXMC_NOE_PORT, GPIO_AF_12, SCSI_OUT_REQ_EXMC_NOE_PIN);
  124. gpio_output_options_set(SCSI_OUT_REQ_EXMC_NOE_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, SCSI_OUT_REQ_EXMC_NOE_PIN);
  125. gpio_mode_set(SCSI_OUT_REQ_EXMC_NOE_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, SCSI_OUT_REQ_EXMC_NOE_PIN);
  126. while (count > 0)
  127. {
  128. uint32_t blocksize = (count > SYNC_DMA_BUFSIZE * 2) ? (SYNC_DMA_BUFSIZE * 2) : count;
  129. count -= blocksize;
  130. // dma_memory_address_config(SCSI_EXMC_DMA, SCSI_EXMC_DMACH, 0, (uint32_t)g_sync_dma_buf);
  131. // dma_transfer_number_config(SCSI_EXMC_DMA, SCSI_EXMC_DMACH, blocksize);
  132. // dma_channel_enable(SCSI_EXMC_DMA, SCSI_EXMC_DMACH);
  133. uint16_t *src = (uint16_t*)g_sync_dma_buf;
  134. uint8_t *dst = data;
  135. uint8_t *end = data + blocksize;
  136. uint32_t start = millis();
  137. while (dst < end)
  138. {
  139. // Read from EXMC and write to internal RAM
  140. // Note that this will hang the CPU if host does not send ACK pulses.
  141. uint16_t word = *(uint16_t*)EXMC_NOR_PSRAM;
  142. *dst++ = (~word) >> SCSI_EXMC_DATA_SHIFT;
  143. // TODO: Figure out why DMA does not work correctly with EXMC on GD32F450
  144. // uint32_t remain = DMA_CHCNT(SCSI_EXMC_DMA, SCSI_EXMC_DMACH);
  145. // while (dst < end - remain)
  146. // {
  147. // *dst++ = ~(*src++) >> SCSI_EXMC_DATA_SHIFT;
  148. // }
  149. // if ((uint32_t)(millis() - start) > 500 || *resetFlag)
  150. // {
  151. // // We are in a pinch here: without ACK pulses coming, the EXMC and DMA peripherals
  152. // // are locked up. The only way out is a whole system reset.
  153. // logmsg("SCSI Synchronous read timeout: resetting system");
  154. // NVIC_SystemReset();
  155. // }
  156. }
  157. // dma_channel_disable(SCSI_EXMC_DMA, SCSI_EXMC_DMACH);
  158. data = end;
  159. }
  160. // Set SCSI data IN pins back to input mode
  161. gpio_mode_set(SCSI_IN_PORT, GPIO_MODE_INPUT, GPIO_PUPD_NONE, SCSI_IN_MASK);
  162. EXMC_SNCTL(EXMC_BANK0_NORSRAM_REGION0) &= ~EXMC_SNCTL_NRBKEN;
  163. GPIO_CTL(SCSI_OUT_REQ_EXMC_NOE_PORT) = oldmode_gpio_ctl;
  164. GPIO_OSPD(SCSI_OUT_REQ_EXMC_NOE_PORT) = oldmode_gpio_ospd;
  165. GPIO_OMODE(SCSI_OUT_REQ_EXMC_NOE_PORT) = oldmode_gpio_omode;
  166. GPIO_PUD(SCSI_OUT_REQ_EXMC_NOE_PORT) = oldmode_gpio_pud;
  167. GPIO_AFSEL0(SCSI_OUT_REQ_EXMC_NOE_PORT) = oldmode_gpio_af;
  168. }
  169. /********************************/
  170. /* Transfer from device to host */
  171. /********************************/
  172. // Simple delay, about 20 ns.
  173. // This is less likely to get optimized away by CPU pipeline than nop
  174. #define ASM_DELAY() \
  175. " ldr %[tmp2], [%[reset_flag]] \n"
  176. // Take 8 bits from d and format them for writing
  177. // d is name of data operand, b is bit offset
  178. #define ASM_LOAD_DATA(b) \
  179. " ubfx %[tmp1], %[data], #" b ", #8 \n" \
  180. " ldr %[tmp1], [%[byte_lookup], %[tmp1], lsl #2] \n"
  181. // Write data to SCSI port and set REQ high
  182. #define ASM_SEND_DATA() \
  183. " str %[tmp1], [%[out_port_bop]] \n"
  184. // Set REQ low
  185. #define ASM_SET_REQ_LOW() \
  186. " mov %[tmp2], %[bop_req_low] \n" \
  187. " str %[tmp2], [%[out_port_bop]] \n"
  188. // Wait for ACK_TIMER - n to be less than num_bytes
  189. #define ASM_WAIT_ACK_TIMER(n) \
  190. "wait_acks_" n "_%=: \n" \
  191. " ldr %[tmp2], [%[ack_timer]] \n" \
  192. " sub %[tmp2], # " n " \n" \
  193. " cmp %[tmp2], %[num_bytes] \n" \
  194. " ble got_acks_" n "_%= \n" \
  195. " ldr %[tmp2], [%[reset_flag]] \n" \
  196. " cmp %[tmp2], #0 \n" \
  197. " bne all_done_%= \n" \
  198. " b wait_acks_" n "_%= \n" \
  199. "got_acks_" n "_%=: \n"
  200. // Send 4 bytes
  201. #define ASM_SEND_4BYTES() \
  202. ASM_LOAD_DATA("0") \
  203. ASM_SEND_DATA() \
  204. ASM_DELAY1() \
  205. ASM_SET_REQ_LOW() \
  206. ASM_DELAY2() \
  207. ASM_LOAD_DATA("8") \
  208. ASM_SEND_DATA() \
  209. ASM_DELAY1() \
  210. ASM_SET_REQ_LOW() \
  211. ASM_DELAY2() \
  212. ASM_LOAD_DATA("16") \
  213. ASM_SEND_DATA() \
  214. ASM_DELAY1() \
  215. ASM_SET_REQ_LOW() \
  216. ASM_DELAY2() \
  217. ASM_LOAD_DATA("24") \
  218. ASM_SEND_DATA() \
  219. ASM_DELAY1() \
  220. ASM_SET_REQ_LOW()
  221. // Send 1 byte, wait for ACK_TIMER to be less than num_bytes + n and send 3 bytes more
  222. // This interleaving minimizes the delay caused by WAIT_ACK_TIMER.
  223. #define ASM_SEND_4BYTES_WAIT(n) \
  224. ASM_LOAD_DATA("0") \
  225. ASM_SEND_DATA() \
  226. ASM_DELAY2() \
  227. ASM_LOAD_DATA("8") \
  228. ASM_SET_REQ_LOW() \
  229. ASM_DELAY2() \
  230. " ldr %[tmp2], [%[ack_timer]] \n" \
  231. " sub %[tmp2], # " n " \n" \
  232. ASM_SEND_DATA() \
  233. " cmp %[tmp2], %[num_bytes] \n" \
  234. " ble got_acks_" n "_%= \n" \
  235. ASM_WAIT_ACK_TIMER(n) \
  236. ASM_DELAY2() \
  237. ASM_SET_REQ_LOW() \
  238. ASM_DELAY2() \
  239. ASM_LOAD_DATA("16") \
  240. ASM_SEND_DATA() \
  241. ASM_DELAY1() \
  242. ASM_SET_REQ_LOW() \
  243. ASM_DELAY2() \
  244. ASM_LOAD_DATA("24") \
  245. ASM_SEND_DATA() \
  246. ASM_DELAY1() \
  247. ASM_SET_REQ_LOW() \
  248. // Specialized routine for settings:
  249. // <=100 ns period, >=15 outstanding REQs
  250. static void sync_send_100ns_15off(const uint8_t *buf, uint32_t num_bytes, volatile int *resetFlag)
  251. {
  252. volatile uint32_t *out_port_bop = (volatile uint32_t*)&GPIO_BOP(SCSI_OUT_PORT);
  253. volatile uint32_t *ack_timer = &TIMER_CNT(SCSI_SYNC_TIMER);
  254. const uint32_t *byte_lookup = g_scsi_out_byte_to_bop;
  255. register uint32_t tmp1 = 0;
  256. register uint32_t tmp2 = 0;
  257. register uint32_t data = 0;
  258. // Delay 1 is typically longest and delay 2 shortest.
  259. // Tuning these is just trial and error.
  260. #define ASM_DELAY1() ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY() \
  261. ASM_DELAY() ASM_DELAY()
  262. #define ASM_DELAY2() ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY()
  263. asm volatile (
  264. "main_loop_%=: \n"
  265. " subs %[num_bytes], %[num_bytes], #16 \n"
  266. " bmi last_bytes_%= \n"
  267. /* At each point make sure there is at most 15 bytes in flight */
  268. " ldr %[data], [%[buf]], #4 \n"
  269. ASM_SEND_4BYTES_WAIT("26")
  270. ASM_DELAY2()
  271. " ldr %[data], [%[buf]], #4 \n"
  272. ASM_SEND_4BYTES_WAIT("22")
  273. ASM_DELAY2()
  274. " ldr %[data], [%[buf]], #4 \n"
  275. ASM_SEND_4BYTES_WAIT("18")
  276. ASM_DELAY2()
  277. " ldr %[data], [%[buf]], #4 \n"
  278. ASM_SEND_4BYTES_WAIT("14")
  279. ASM_DELAY2()
  280. " cbz %[num_bytes], all_done_%= \n"
  281. " b main_loop_%= \n"
  282. "last_bytes_%=: \n"
  283. " add %[num_bytes], %[num_bytes], #16 \n"
  284. "last_bytes_loop_%=: \n"
  285. " ldrb %[data], [%[buf]], #1 \n"
  286. ASM_LOAD_DATA("0")
  287. ASM_WAIT_ACK_TIMER("15")
  288. ASM_SEND_DATA()
  289. ASM_DELAY1()
  290. ASM_SET_REQ_LOW()
  291. " subs %[num_bytes], %[num_bytes], #1 \n"
  292. " bne last_bytes_loop_%= \n"
  293. "all_done_%=: \n"
  294. ASM_DELAY()
  295. : /* Output */ [tmp1] "+l" (tmp1), [tmp2] "+l" (tmp2), [data] "+r" (data),
  296. [buf] "+r" (buf), [num_bytes] "+r" (num_bytes)
  297. : /* Input */ [ack_timer] "r" (ack_timer),
  298. [bop_req_low] "I" (SCSI_OUT_REQ << 16),
  299. [out_port_bop] "r"(out_port_bop),
  300. [byte_lookup] "r" (byte_lookup),
  301. [reset_flag] "r" (resetFlag)
  302. : /* Clobber */);
  303. #undef ASM_DELAY1
  304. #undef ASM_DELAY2
  305. SCSI_RELEASE_DATA_REQ();
  306. }
  307. // Specialized routine for settings:
  308. // <=200 ns period, >=15 outstanding REQs
  309. static void sync_send_200ns_15off(const uint8_t *buf, uint32_t num_bytes, volatile int *resetFlag)
  310. {
  311. volatile uint32_t *out_port_bop = (volatile uint32_t*)&GPIO_BOP(SCSI_OUT_PORT);
  312. volatile uint32_t *ack_timer = &TIMER_CNT(SCSI_SYNC_TIMER);
  313. const uint32_t *byte_lookup = g_scsi_out_byte_to_bop;
  314. register uint32_t tmp1 = 0;
  315. register uint32_t tmp2 = 0;
  316. register uint32_t data = 0;
  317. #define ASM_DELAY1() ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY() \
  318. ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY() \
  319. ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY() \
  320. ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY() \
  321. ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY()
  322. #define ASM_DELAY2() 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 volatile (
  326. "main_loop_%=: \n"
  327. " subs %[num_bytes], %[num_bytes], #16 \n"
  328. " bmi last_bytes_%= \n"
  329. /* At each point make sure there is at most 15 bytes in flight */
  330. " ldr %[data], [%[buf]], #4 \n"
  331. ASM_SEND_4BYTES_WAIT("26")
  332. ASM_DELAY2()
  333. " ldr %[data], [%[buf]], #4 \n"
  334. ASM_SEND_4BYTES_WAIT("22")
  335. ASM_DELAY2()
  336. " ldr %[data], [%[buf]], #4 \n"
  337. ASM_SEND_4BYTES_WAIT("18")
  338. ASM_DELAY2()
  339. " ldr %[data], [%[buf]], #4 \n"
  340. ASM_SEND_4BYTES_WAIT("14")
  341. " cbz %[num_bytes], all_done_%= \n"
  342. " b main_loop_%= \n"
  343. "last_bytes_%=: \n"
  344. " add %[num_bytes], %[num_bytes], #16 \n"
  345. "last_bytes_loop_%=: \n"
  346. " ldrb %[data], [%[buf]], #1 \n"
  347. ASM_LOAD_DATA("0")
  348. ASM_WAIT_ACK_TIMER("15")
  349. ASM_SEND_DATA()
  350. ASM_DELAY1()
  351. ASM_SET_REQ_LOW()
  352. ASM_DELAY2()
  353. " subs %[num_bytes], %[num_bytes], #1 \n"
  354. " bne last_bytes_loop_%= \n"
  355. "all_done_%=: \n"
  356. ASM_DELAY1()
  357. : /* Output */ [tmp1] "+l" (tmp1), [tmp2] "+l" (tmp2), [data] "+r" (data),
  358. [buf] "+r" (buf), [num_bytes] "+r" (num_bytes)
  359. : /* Input */ [ack_timer] "r" (ack_timer),
  360. [bop_req_low] "I" (SCSI_OUT_REQ << 16),
  361. [out_port_bop] "r"(out_port_bop),
  362. [byte_lookup] "r" (byte_lookup),
  363. [reset_flag] "r" (resetFlag)
  364. : /* Clobber */);
  365. #undef ASM_DELAY1
  366. #undef ASM_DELAY2
  367. SCSI_RELEASE_DATA_REQ();
  368. }
  369. // Specialized routine for settings:
  370. // <=260 ns period, >=7 outstanding REQs
  371. static void sync_send_260ns_7off(const uint8_t *buf, uint32_t num_bytes, volatile int *resetFlag)
  372. {
  373. volatile uint32_t *out_port_bop = (volatile uint32_t*)&GPIO_BOP(SCSI_OUT_PORT);
  374. volatile uint32_t *ack_timer = &TIMER_CNT(SCSI_SYNC_TIMER);
  375. const uint32_t *byte_lookup = g_scsi_out_byte_to_bop;
  376. register uint32_t tmp1 = 0;
  377. register uint32_t tmp2 = 0;
  378. register uint32_t data = 0;
  379. #define ASM_DELAY1() ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY() \
  380. ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY() \
  381. ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY() \
  382. ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY() \
  383. ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY() \
  384. ASM_DELAY() ASM_DELAY() ASM_DELAY() ASM_DELAY()
  385. #define ASM_DELAY2() 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. asm volatile (
  390. "main_loop_%=: \n"
  391. " subs %[num_bytes], %[num_bytes], #4 \n"
  392. " bmi last_bytes_%= \n"
  393. /* At each point make sure there is at most 3 bytes in flight */
  394. " ldr %[data], [%[buf]], #4 \n"
  395. ASM_SEND_4BYTES_WAIT("7")
  396. ASM_DELAY2()
  397. " cbz %[num_bytes], all_done_%= \n"
  398. " b main_loop_%= \n"
  399. "last_bytes_%=: \n"
  400. " add %[num_bytes], %[num_bytes], #4 \n"
  401. "last_bytes_loop_%=: \n"
  402. " ldrb %[data], [%[buf]], #1 \n"
  403. ASM_LOAD_DATA("0")
  404. ASM_WAIT_ACK_TIMER("5")
  405. ASM_SEND_DATA()
  406. ASM_DELAY1()
  407. ASM_SET_REQ_LOW()
  408. ASM_DELAY2()
  409. " subs %[num_bytes], %[num_bytes], #1 \n"
  410. " bne last_bytes_loop_%= \n"
  411. "all_done_%=: \n"
  412. ASM_DELAY1()
  413. : /* Output */ [tmp1] "+l" (tmp1), [tmp2] "+l" (tmp2), [data] "+r" (data),
  414. [buf] "+r" (buf), [num_bytes] "+r" (num_bytes)
  415. : /* Input */ [ack_timer] "r" (ack_timer),
  416. [bop_req_low] "I" (SCSI_OUT_REQ << 16),
  417. [out_port_bop] "r"(out_port_bop),
  418. [byte_lookup] "r" (byte_lookup),
  419. [reset_flag] "r" (resetFlag)
  420. : /* Clobber */);
  421. #undef ASM_DELAY1
  422. #undef ASM_DELAY2
  423. SCSI_RELEASE_DATA_REQ();
  424. }
  425. void scsi_accel_sync_send(const uint8_t* data, uint32_t count, volatile int *resetFlag)
  426. {
  427. // Timer counts down from the initial number of bytes.
  428. TIMER_CNT(SCSI_SYNC_TIMER) = count;
  429. TIMER_CTL0(SCSI_SYNC_TIMER) = TIMER_CTL0_CEN | TIMER_CTL0_DIR;
  430. int syncOffset = scsiDev.target->syncOffset;
  431. int syncPeriod = scsiDev.target->syncPeriod;
  432. if (syncOffset >= 15 && syncPeriod <= 25)
  433. {
  434. sync_send_100ns_15off(data, count, resetFlag);
  435. }
  436. else if (syncOffset >= 15 && syncPeriod <= 50)
  437. {
  438. sync_send_200ns_15off(data, count, resetFlag);
  439. }
  440. else if (syncOffset >= 7 && syncPeriod <= 65)
  441. {
  442. sync_send_260ns_7off(data, count, resetFlag);
  443. }
  444. else
  445. {
  446. dbgmsg("No optimized routine for syncOffset=", syncOffset, " syndPeriod=", syncPeriod, ", using fallback");
  447. while (count-- > 0)
  448. {
  449. while (TIMER_CNT(SCSI_SYNC_TIMER) > count + syncOffset && !*resetFlag);
  450. SCSI_OUT_DATA(*data++);
  451. delay_ns(syncPeriod * 2);
  452. SCSI_OUT(REQ, 1);
  453. delay_ns(syncPeriod * 2);
  454. }
  455. delay_ns(syncPeriod * 2);
  456. SCSI_RELEASE_DATA_REQ();
  457. }
  458. while (TIMER_CNT(SCSI_SYNC_TIMER) > 0 && !*resetFlag);
  459. if (*resetFlag)
  460. {
  461. dbgmsg("Bus reset during sync transfer, total ", (int)count,
  462. " bytes, remaining ACK count ", (int)TIMER_CNT(SCSI_SYNC_TIMER));
  463. }
  464. TIMER_CTL0(SCSI_SYNC_TIMER) = 0;
  465. }
  466. #endif