scsi_accel_dma.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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. #include "scsi_accel_dma.h"
  22. #include <ZuluSCSI_log.h>
  23. #include <gd32f4xx_timer.h>
  24. #include <gd32f4xx_rcu.h>
  25. #include <gd32f4xx_gpio.h>
  26. #include <assert.h>
  27. #include <string.h>
  28. #ifndef SCSI_ACCEL_DMA_AVAILABLE
  29. void scsi_accel_timer_dma_init() {}
  30. void scsi_accel_greenpak_dma_init() {}
  31. void scsi_accel_dma_startWrite(const uint8_t* data, uint32_t count, volatile int *resetFlag) {}
  32. void scsi_accel_dma_stopWrite() {}
  33. void scsi_accel_dma_finishWrite(volatile int *resetFlag) {}
  34. bool scsi_accel_dma_isWriteFinished(const uint8_t* data) { return true; }
  35. #else
  36. static void greenpak_refill_dmabuf();
  37. static void greenpak_start_dma();
  38. static void greenpak_stop_dma();
  39. enum greenpak_state_t { GREENPAK_IO1_LOW = 0, GREENPAK_IO1_HIGH, GREENPAK_STOP};
  40. #define DMA_BUF_SIZE 256
  41. #define DMA_BUF_MASK (DMA_BUF_SIZE - 1)
  42. static struct {
  43. uint8_t *app_buf; // Buffer provided by application
  44. uint32_t dma_buf[DMA_BUF_SIZE]; // Buffer of data formatted for GPIO BOP register
  45. uint32_t dma_idx; // Write index to DMA buffer
  46. uint32_t dma_fillto; // Point up to which DMA buffer is available for refilling
  47. uint32_t timer_buf; // Control value for timer SWEVG register
  48. uint32_t bytes_app; // Bytes available in application buffer
  49. uint32_t bytes_dma; // Bytes (words) written so far to DMA buffer
  50. uint32_t scheduled_dma; // Bytes (words) that DMA data count was last set to
  51. greenpak_state_t greenpak_state; // Toggle signal state for greenpak
  52. bool incomplete_buf; // Did not have enough data to fill DMA buffer
  53. uint8_t *next_app_buf; // Next buffer from application after current one finishes
  54. uint32_t next_app_bytes; // Bytes in next buffer
  55. } g_scsi_dma;
  56. enum scsidma_state_t { SCSIDMA_IDLE = 0, SCSIDMA_WRITE };
  57. static volatile scsidma_state_t g_scsi_dma_state;
  58. static bool g_scsi_dma_use_greenpak;
  59. void scsi_accel_timer_dma_init()
  60. {
  61. g_scsi_dma_state = SCSIDMA_IDLE;
  62. g_scsi_dma_use_greenpak = false;
  63. rcu_periph_clock_enable(SCSI_TIMER_RCU);
  64. rcu_periph_clock_enable(SCSI_TIMER_DMA_RCU);
  65. // DMA Channel A: data copy
  66. // GPIO DMA copies data from memory buffer to GPIO BOP register.
  67. // The memory buffer is filled by interrupt routine.
  68. dma_multi_data_parameter_struct gpio_dma_config =
  69. {
  70. .periph_addr = (uint32_t)&GPIO_BOP(SCSI_OUT_PORT),
  71. .periph_width = DMA_PERIPH_WIDTH_32BIT,
  72. .periph_inc = DMA_PERIPH_INCREASE_DISABLE,
  73. .memory0_addr = 0, // Filled before transfer
  74. .memory_width = DMA_MEMORY_WIDTH_32BIT,
  75. .memory_inc = DMA_MEMORY_INCREASE_ENABLE,
  76. .memory_burst_width = DMA_MEMORY_BURST_SINGLE,
  77. .periph_burst_width = DMA_PERIPH_BURST_SINGLE,
  78. .critical_value = DMA_FIFO_1_WORD,
  79. .circular_mode = DMA_CIRCULAR_MODE_ENABLE,
  80. .direction = DMA_MEMORY_TO_PERIPH,
  81. .number = DMA_BUF_SIZE,
  82. .priority = DMA_PRIORITY_ULTRA_HIGH
  83. };
  84. dma_multi_data_mode_init(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA, &gpio_dma_config);
  85. dma_channel_subperipheral_select(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA, SCSI_TIMER_DMACHA_SUB_PERIPH);
  86. NVIC_SetPriority(SCSI_TIMER_DMACHA_IRQn, 1);
  87. NVIC_EnableIRQ(SCSI_TIMER_DMACHA_IRQn);
  88. // DMA Channel B: timer update
  89. // Timer DMA causes update event to restart timer after
  90. // GPIO DMA operation is done.
  91. dma_multi_data_parameter_struct timer_dma_config =
  92. {
  93. .periph_addr = (uint32_t)&TIMER_SWEVG(SCSI_TIMER),
  94. .periph_width = DMA_PERIPH_WIDTH_32BIT,
  95. .periph_inc = DMA_PERIPH_INCREASE_DISABLE,
  96. .memory0_addr = (uint32_t)&g_scsi_dma.timer_buf,
  97. .memory_width = DMA_MEMORY_WIDTH_32BIT,
  98. .memory_inc = DMA_PERIPH_INCREASE_DISABLE,
  99. .memory_burst_width = DMA_MEMORY_BURST_SINGLE,
  100. .periph_burst_width = DMA_PERIPH_BURST_SINGLE,
  101. .critical_value = DMA_FIFO_1_WORD,
  102. .circular_mode = DMA_CIRCULAR_MODE_DISABLE,
  103. .direction = DMA_MEMORY_TO_PERIPH,
  104. .number = DMA_BUF_SIZE,
  105. .priority = DMA_PRIORITY_HIGH
  106. };
  107. dma_multi_data_mode_init(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB, &timer_dma_config);
  108. dma_channel_subperipheral_select(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB, SCSI_TIMER_DMACHB_SUB_PERIPH);
  109. NVIC_SetPriority(SCSI_TIMER_DMACHB_IRQn, 128); // Priority = 128 to make sure this is lower priority independent of priority grouping
  110. NVIC_EnableIRQ(SCSI_TIMER_DMACHB_IRQn);
  111. g_scsi_dma.timer_buf = TIMER_SWEVG_UPG;
  112. // Timer is used to toggle the request signal based on external trigger input.
  113. // OUT_REQ is driven by timer output.
  114. // 1. On timer update event, REQ is set low.
  115. // 2. When ACK goes low, timer counts and OUT_REQ is set high.
  116. // Simultaneously a DMA request is triggered to write next data to GPIO.
  117. // 3. When ACK goes high, a DMA request is triggered to cause timer update event.
  118. // The DMA request priority is set so that 2. always completes before it.
  119. TIMER_CTL0(SCSI_TIMER) = 0;
  120. TIMER_SMCFG(SCSI_TIMER) = TIMER_SLAVE_MODE_EXTERNAL0 | TIMER_SMCFG_TRGSEL_CI0F_ED;
  121. TIMER_CAR(SCSI_TIMER) = 65535;
  122. TIMER_PSC(SCSI_TIMER) = 0;
  123. TIMER_DMAINTEN(SCSI_TIMER) = 0;
  124. TIMER_CHCTL0(SCSI_TIMER) = 0x6001; // CH0 as input, CH1 as DMA trigger
  125. TIMER_CHCTL1(SCSI_TIMER) = 0x6074; // CH2 as fast PWM output, CH3 as DMA trigger
  126. TIMER_CHCTL2(SCSI_TIMER) = TIMER_CHCTL2_CH2NEN;
  127. TIMER_CCHP(SCSI_TIMER) = TIMER_CCHP_POEN;
  128. TIMER_CH1CV(SCSI_TIMER) = 1; // Copy data when ACK goes low
  129. TIMER_CH2CV(SCSI_TIMER) = 1; // REQ is low until ACK goes low
  130. TIMER_CH3CV(SCSI_TIMER) = 2; // Reset timer after ACK goes high & previous DMA is complete
  131. gpio_mode_set(SCSI_TIMER_IN_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, SCSI_TIMER_IN_PIN);
  132. gpio_af_set(SCSI_TIMER_IN_PORT, SCSI_TIMER_IN_AF, SCSI_TIMER_IN_PIN);
  133. scsi_accel_dma_stopWrite();
  134. }
  135. // Select whether OUT_REQ is connected to timer or GPIO port
  136. static void scsi_dma_gpio_config(bool enable)
  137. {
  138. if (enable)
  139. {
  140. if (g_scsi_dma_use_greenpak)
  141. {
  142. GPIO_BC(SCSI_OUT_PORT) = GREENPAK_PLD_IO1;
  143. GPIO_BOP(SCSI_OUT_PORT) = GREENPAK_PLD_IO2;
  144. }
  145. else
  146. {
  147. gpio_mode_set(SCSI_OUT_PORT, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, SCSI_OUT_REQ);
  148. gpio_mode_set(SCSI_TIMER_OUT_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, SCSI_TIMER_OUT_PIN);
  149. gpio_output_options_set(SCSI_TIMER_OUT_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_200MHZ, SCSI_TIMER_OUT_PIN);
  150. gpio_af_set(SCSI_TIMER_OUT_PORT, SCSI_TIMER_OUT_AF, SCSI_TIMER_OUT_PIN);
  151. }
  152. }
  153. else
  154. {
  155. GPIO_BC(SCSI_OUT_PORT) = GREENPAK_PLD_IO2;
  156. gpio_mode_set(SCSI_TIMER_OUT_PORT, GPIO_MODE_INPUT, GPIO_PUPD_NONE, SCSI_TIMER_OUT_PIN);
  157. gpio_output_options_set(SCSI_TIMER_OUT_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_200MHZ, SCSI_TIMER_OUT_PIN);
  158. gpio_mode_set(SCSI_OUT_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, SCSI_OUT_REQ);
  159. gpio_output_options_set(SCSI_OUT_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_200MHZ, SCSI_OUT_REQ);
  160. }
  161. }
  162. // Convert input bytes into BOP values in the DMA buffer
  163. static void refill_dmabuf()
  164. {
  165. if (g_scsi_dma_use_greenpak)
  166. {
  167. greenpak_refill_dmabuf();
  168. return;
  169. }
  170. // Check how many bytes we have available from the application
  171. uint32_t count = g_scsi_dma.bytes_app - g_scsi_dma.bytes_dma;
  172. // Check amount of free space in DMA buffer
  173. uint32_t max = g_scsi_dma.dma_fillto - g_scsi_dma.dma_idx;
  174. if (count > max) count = max;
  175. if (count == 0) return;
  176. uint8_t *src = g_scsi_dma.app_buf + g_scsi_dma.bytes_dma;
  177. uint32_t *dst = g_scsi_dma.dma_buf;
  178. uint32_t pos = g_scsi_dma.dma_idx;
  179. uint32_t end = pos + count;
  180. g_scsi_dma.dma_idx = end;
  181. g_scsi_dma.bytes_dma += count;
  182. while (pos + 4 <= end)
  183. {
  184. uint32_t input = *(uint32_t*)src;
  185. src += 4;
  186. dst[(pos++) & DMA_BUF_MASK] = g_scsi_out_byte_to_bop[(input >> 0) & 0xFF];
  187. dst[(pos++) & DMA_BUF_MASK] = g_scsi_out_byte_to_bop[(input >> 8) & 0xFF];
  188. dst[(pos++) & DMA_BUF_MASK] = g_scsi_out_byte_to_bop[(input >> 16) & 0xFF];
  189. dst[(pos++) & DMA_BUF_MASK] = g_scsi_out_byte_to_bop[(input >> 24) & 0xFF];
  190. }
  191. while (pos < end)
  192. {
  193. dst[(pos++) & DMA_BUF_MASK] = g_scsi_out_byte_to_bop[*src++];
  194. }
  195. if (end < g_scsi_dma.dma_fillto)
  196. {
  197. // Partial buffer fill, this will get refilled from interrupt if we
  198. // get more data. Set next byte to an invalid parity value so that
  199. // any race conditions will get caught as parity error.
  200. dst[pos & DMA_BUF_MASK] = g_scsi_out_byte_to_bop[0] ^ SCSI_OUT_DBP;
  201. }
  202. }
  203. // Start DMA transfer
  204. static void start_dma()
  205. {
  206. if (g_scsi_dma_use_greenpak)
  207. {
  208. greenpak_start_dma();
  209. return;
  210. }
  211. // Disable channels while configuring
  212. dma_channel_disable(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA);
  213. dma_channel_disable(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB);
  214. TIMER_CTL0(SCSI_TIMER) = 0;
  215. // Set new buffer address and size
  216. // CHA / Data channel is in circular mode and always has DMA_BUF_SIZE buffer size.
  217. // CHB / Update channel limits the number of data.
  218. dma_memory_address_config(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA, DMA_MEMORY_0, (uint32_t)g_scsi_dma.dma_buf);
  219. dma_transfer_number_config(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA, DMA_BUF_SIZE);
  220. uint32_t dma_to_schedule = g_scsi_dma.bytes_app - g_scsi_dma.scheduled_dma;
  221. dma_transfer_number_config(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB, dma_to_schedule);
  222. g_scsi_dma.scheduled_dma += dma_to_schedule;
  223. // Clear pending DMA events
  224. TIMER_DMAINTEN(SCSI_TIMER) = 0;
  225. TIMER_DMAINTEN(SCSI_TIMER) = TIMER_DMAINTEN_CH1DEN | TIMER_DMAINTEN_CH3DEN;
  226. // Clear and enable interrupt
  227. dma_interrupt_flag_clear(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA, DMA_FLAG_HTF | DMA_FLAG_FTF | DMA_FLAG_FEE);
  228. dma_interrupt_flag_clear(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB, DMA_FLAG_HTF | DMA_FLAG_FTF | DMA_FLAG_FEE);
  229. dma_interrupt_enable(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA, DMA_CHXCTL_FTFIE | DMA_CHXCTL_HTFIE);
  230. dma_interrupt_enable(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB, DMA_CHXCTL_FTFIE);
  231. // Enable channels
  232. dma_channel_enable(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA);
  233. dma_channel_enable(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB);
  234. // Make sure REQ is initially high
  235. TIMER_CNT(SCSI_TIMER) = 16;
  236. TIMER_CHCTL1(SCSI_TIMER) = 0x6050;
  237. TIMER_CHCTL1(SCSI_TIMER) = 0x6074;
  238. // Enable timer
  239. timer_enable(SCSI_TIMER);
  240. // Generate first events
  241. TIMER_SWEVG(SCSI_TIMER) = TIMER_SWEVG_CH1G;
  242. TIMER_SWEVG(SCSI_TIMER) = TIMER_SWEVG_CH3G;
  243. }
  244. // Stop DMA transfer
  245. static void stop_dma()
  246. {
  247. greenpak_stop_dma();
  248. dma_channel_disable(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA);
  249. dma_channel_disable(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB);
  250. dma_interrupt_disable(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA, DMA_CHXCTL_FTFIE | DMA_CHXCTL_HTFIE);
  251. dma_interrupt_disable(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB, DMA_CHXCTL_FTFIE);
  252. // Wait for ACK of the last byte
  253. volatile int timeout = 10000;
  254. while (TIMER_CNT(SCSI_TIMER) < 2 && timeout > 0)
  255. {
  256. timeout--;
  257. }
  258. timer_disable(SCSI_TIMER);
  259. g_scsi_dma_state = SCSIDMA_IDLE;
  260. SCSI_RELEASE_DATA_REQ();
  261. }
  262. static void check_dma_next_buffer()
  263. {
  264. // Check if we are at the end of the application buffer
  265. if (g_scsi_dma.next_app_buf && g_scsi_dma.bytes_dma == g_scsi_dma.bytes_app)
  266. {
  267. // Switch to next buffer
  268. assert(g_scsi_dma.scheduled_dma == g_scsi_dma.bytes_app);
  269. g_scsi_dma.app_buf = g_scsi_dma.next_app_buf;
  270. g_scsi_dma.bytes_app = g_scsi_dma.next_app_bytes;
  271. g_scsi_dma.bytes_dma = 0;
  272. g_scsi_dma.scheduled_dma = 0;
  273. g_scsi_dma.next_app_buf = 0;
  274. g_scsi_dma.next_app_bytes = 0;
  275. refill_dmabuf();
  276. }
  277. }
  278. // Convert new data from application buffer to DMA buffer
  279. extern "C" void SCSI_TIMER_DMACHA_IRQ()
  280. {
  281. uint32_t intf0 = DMA_INTF0(SCSI_TIMER_DMA);
  282. uint32_t intf1 = DMA_INTF1(SCSI_TIMER_DMA);
  283. if (dma_interrupt_flag_get(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA, DMA_FLAG_HTF))
  284. {
  285. if (dma_interrupt_flag_get(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA, DMA_FLAG_FTF))
  286. {
  287. logmsg("ERROR: SCSI DMA overrun: intf0 :", intf0,
  288. " intf1: ", intf1,
  289. " bytes_app: ", g_scsi_dma.bytes_app,
  290. " bytes_dma: ", g_scsi_dma.bytes_dma,
  291. " dma_idx: ", g_scsi_dma.dma_idx,
  292. " sched_dma: ", g_scsi_dma.scheduled_dma);
  293. stop_dma();
  294. return;
  295. }
  296. dma_interrupt_flag_clear(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA, DMA_FLAG_HTF);
  297. g_scsi_dma.dma_fillto += DMA_BUF_SIZE / 2;
  298. }
  299. else if (dma_interrupt_flag_get(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA, DMA_FLAG_FTF))
  300. {
  301. dma_interrupt_flag_clear(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA, DMA_FLAG_FTF);
  302. g_scsi_dma.dma_fillto += DMA_BUF_SIZE / 2;
  303. }
  304. if (!g_scsi_dma.incomplete_buf)
  305. {
  306. // Fill DMA buffer with data from current application buffer
  307. refill_dmabuf();
  308. check_dma_next_buffer();
  309. }
  310. if (g_scsi_dma.dma_idx < g_scsi_dma.dma_fillto)
  311. {
  312. // We weren't able to fill the DMA buffer completely during the interrupt.
  313. // This can cause DMA to prefetch words that have not yet been written.
  314. // The DMA CHA must be restarted in CHB interrupt handler.
  315. g_scsi_dma.incomplete_buf = true;
  316. }
  317. }
  318. // Check if enough data is available to continue DMA transfer
  319. extern "C" void SCSI_TIMER_DMACHB_IRQ()
  320. {
  321. if (dma_interrupt_flag_get(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB, DMA_FLAG_FTF))
  322. {
  323. dma_interrupt_flag_clear(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB, DMA_FLAG_FTF);
  324. if (g_scsi_dma.bytes_app > g_scsi_dma.scheduled_dma)
  325. {
  326. if (g_scsi_dma.incomplete_buf)
  327. {
  328. // Previous request didn't have a complete buffer worth of data.
  329. // The multiword DMA has already loaded next bytes from RAM, so we need
  330. // to reinitialize DMA channel A.
  331. __disable_irq();
  332. dma_channel_disable(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA);
  333. dma_memory_address_config(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA, DMA_MEMORY_0, (uint32_t)g_scsi_dma.dma_buf);
  334. dma_transfer_number_config(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA, DMA_BUF_SIZE);
  335. g_scsi_dma.bytes_dma = g_scsi_dma.scheduled_dma;
  336. g_scsi_dma.dma_idx = 0;
  337. g_scsi_dma.dma_fillto = DMA_BUF_SIZE;
  338. g_scsi_dma.incomplete_buf = false;
  339. refill_dmabuf();
  340. // Enable channel and generate event to transfer first byte
  341. dma_interrupt_flag_clear(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA, DMA_FLAG_HTF | DMA_FLAG_FTF | DMA_FLAG_FEE);
  342. dma_channel_enable(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA);
  343. TIMER_SWEVG(SCSI_TIMER) = TIMER_SWEVG_CH1G;
  344. __enable_irq();
  345. }
  346. // Update the total number of bytes available for DMA
  347. uint32_t dma_to_schedule = g_scsi_dma.bytes_app - g_scsi_dma.scheduled_dma;
  348. dma_channel_disable(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB);
  349. dma_transfer_number_config(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB, dma_to_schedule);
  350. dma_channel_enable(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB);
  351. g_scsi_dma.scheduled_dma += dma_to_schedule;
  352. }
  353. else
  354. {
  355. // No more data available
  356. stop_dma();
  357. }
  358. }
  359. }
  360. void scsi_accel_dma_startWrite(const uint8_t* data, uint32_t count, volatile int *resetFlag)
  361. {
  362. __disable_irq();
  363. if (g_scsi_dma_state == SCSIDMA_WRITE)
  364. {
  365. if (!g_scsi_dma.next_app_buf && data == g_scsi_dma.app_buf + g_scsi_dma.bytes_app)
  366. {
  367. // Combine with currently running request
  368. g_scsi_dma.bytes_app += count;
  369. count = 0;
  370. }
  371. else if (data == g_scsi_dma.next_app_buf + g_scsi_dma.next_app_bytes)
  372. {
  373. // Combine with queued request
  374. g_scsi_dma.next_app_bytes += count;
  375. count = 0;
  376. }
  377. else if (!g_scsi_dma.next_app_buf)
  378. {
  379. // Add as queued request
  380. g_scsi_dma.next_app_buf = (uint8_t*)data;
  381. g_scsi_dma.next_app_bytes = count;
  382. count = 0;
  383. }
  384. }
  385. __enable_irq();
  386. // Check if the request was combined
  387. if (count == 0) return;
  388. if (g_scsi_dma_state != SCSIDMA_IDLE)
  389. {
  390. // Wait for previous request to finish
  391. scsi_accel_dma_finishWrite(resetFlag);
  392. if (*resetFlag)
  393. {
  394. return;
  395. }
  396. }
  397. // dbgmsg("Starting DMA write of ", (int)count, " bytes");
  398. scsi_dma_gpio_config(true);
  399. g_scsi_dma_state = SCSIDMA_WRITE;
  400. g_scsi_dma.app_buf = (uint8_t*)data;
  401. g_scsi_dma.dma_idx = 0;
  402. g_scsi_dma.dma_fillto = DMA_BUF_SIZE;
  403. g_scsi_dma.bytes_app = count;
  404. g_scsi_dma.bytes_dma = 0;
  405. g_scsi_dma.scheduled_dma = 0;
  406. g_scsi_dma.incomplete_buf = false;
  407. g_scsi_dma.next_app_buf = NULL;
  408. g_scsi_dma.next_app_bytes = 0;
  409. g_scsi_dma.greenpak_state = GREENPAK_IO1_LOW;
  410. refill_dmabuf();
  411. start_dma();
  412. }
  413. bool scsi_accel_dma_isWriteFinished(const uint8_t* data)
  414. {
  415. // Check if everything has completed
  416. if (g_scsi_dma_state == SCSIDMA_IDLE)
  417. {
  418. return true;
  419. }
  420. if (!data)
  421. return false;
  422. // Check if this data item is still in queue.
  423. __disable_irq();
  424. bool finished = true;
  425. if (data >= g_scsi_dma.app_buf + g_scsi_dma.bytes_dma &&
  426. data < g_scsi_dma.app_buf + g_scsi_dma.bytes_app)
  427. {
  428. finished = false; // In current transfer
  429. }
  430. else if (data >= g_scsi_dma.next_app_buf &&
  431. data < g_scsi_dma.next_app_buf + g_scsi_dma.next_app_bytes)
  432. {
  433. finished = false; // In queued transfer
  434. }
  435. __enable_irq();
  436. return finished;
  437. }
  438. void scsi_accel_dma_stopWrite()
  439. {
  440. stop_dma();
  441. scsi_dma_gpio_config(false);
  442. }
  443. void scsi_accel_dma_finishWrite(volatile int *resetFlag)
  444. {
  445. uint32_t start = millis();
  446. while (g_scsi_dma_state != SCSIDMA_IDLE && !*resetFlag)
  447. {
  448. if ((uint32_t)(millis() - start) > 5000)
  449. {
  450. logmsg("scsi_accel_dma_finishWrite() timeout, DMA counts ",
  451. dma_transfer_number_get(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA), " ",
  452. dma_transfer_number_get(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB), " ",
  453. TIMER_CNT(SCSI_TIMER));
  454. *resetFlag = 1;
  455. break;
  456. }
  457. }
  458. scsi_accel_dma_stopWrite();
  459. }
  460. /************************************************/
  461. /* Functions using external GreenPAK logic chip */
  462. /************************************************/
  463. // @TODO properly handle greenpak DMA
  464. void scsi_accel_greenpak_dma_init()
  465. {
  466. g_scsi_dma_state = SCSIDMA_IDLE;
  467. g_scsi_dma_use_greenpak = true;
  468. rcu_periph_clock_enable(SCSI_TIMER_RCU);
  469. rcu_periph_clock_enable(SCSI_TIMER_DMA_RCU);
  470. // DMA Channel A: data copy
  471. // GPIO DMA copies data from memory buffer to GPIO BOP register.
  472. // The memory buffer is filled by interrupt routine.
  473. /* @TODO replace with gd32F4xx dma code
  474. dma_parameter_struct gpio_dma_config =
  475. {
  476. .periph_addr = (uint32_t)&GPIO_BOP(SCSI_OUT_PORT),
  477. .periph_width = DMA_PERIPHERAL_WIDTH_32BIT,
  478. .memory_addr = (uint32_t)g_scsi_dma.dma_buf,
  479. .memory_width = DMA_MEMORY_WIDTH_32BIT,
  480. .number = DMA_BUF_SIZE,
  481. .priority = DMA_PRIORITY_ULTRA_HIGH,
  482. .periph_inc = DMA_PERIPH_INCREASE_DISABLE,
  483. .memory_inc = DMA_MEMORY_INCREASE_ENABLE,
  484. .direction = DMA_MEMORY_TO_PERIPHERAL
  485. };
  486. dma_init(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA, &gpio_dma_config);
  487. dma_circulation_enable(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA);
  488. */
  489. NVIC_SetPriority(SCSI_TIMER_DMACHA_IRQn, 2);
  490. NVIC_EnableIRQ(SCSI_TIMER_DMACHA_IRQn);
  491. NVIC_DisableIRQ(SCSI_TIMER_DMACHB_IRQn);
  492. // EXTI channel is used to trigger when we reach end of the transfer.
  493. // Because the main DMA is circular and transfer size may not be even
  494. // multiple of it, we cannot trigger the end at the DMA interrupt.
  495. syscfg_exti_line_config(GREENPAK_PLD_IO2_EXTI_SOURCE_PORT, GREENPAK_PLD_IO2_EXTI_SOURCE_PIN);
  496. exti_init(GREENPAK_PLD_IO2_EXTI, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
  497. exti_interrupt_flag_clear(GREENPAK_PLD_IO2_EXTI);
  498. exti_interrupt_disable(GREENPAK_PLD_IO2_EXTI);
  499. NVIC_SetPriority(GREENPAK_IRQn, 1);
  500. NVIC_EnableIRQ(GREENPAK_IRQn);
  501. // Timer is used to trigger DMA requests
  502. // OUT_REQ is driven by timer output.
  503. // 1. On timer update event, REQ is set low.
  504. // 2. When ACK goes low, timer counts and OUT_REQ is set high.
  505. // Simultaneously a DMA request is triggered to write next data to GPIO.
  506. // 3. When ACK goes high, a DMA request is triggered to cause timer update event.
  507. // The DMA request priority is set so that 2. always completes before it.
  508. TIMER_CTL0(SCSI_TIMER) = 0;
  509. TIMER_SMCFG(SCSI_TIMER) = TIMER_SLAVE_MODE_EXTERNAL0 | TIMER_SMCFG_TRGSEL_CI0F_ED;
  510. TIMER_CAR(SCSI_TIMER) = 1;
  511. TIMER_PSC(SCSI_TIMER) = 0;
  512. TIMER_DMAINTEN(SCSI_TIMER) = 0;
  513. TIMER_CHCTL0(SCSI_TIMER) = 0x6001; // CH0 as input, CH1 as DMA trigger
  514. TIMER_CHCTL1(SCSI_TIMER) = 0;
  515. TIMER_CHCTL2(SCSI_TIMER) = 0;
  516. TIMER_CCHP(SCSI_TIMER) = 0;
  517. TIMER_CH1CV(SCSI_TIMER) = 1; // Copy data when ACK goes low
  518. //TODO figure out if this needs to be set to an alternate fucntion like a timer
  519. gpio_mode_set(SCSI_TIMER_IN_PORT, GPIO_MODE_INPUT, GPIO_PUPD_NONE, SCSI_TIMER_IN_PIN);
  520. }
  521. extern const uint32_t g_scsi_out_byte_to_bop_pld1hi[256];
  522. extern const uint32_t g_scsi_out_byte_to_bop_pld1lo[256];
  523. static void greenpak_refill_dmabuf()
  524. {
  525. if (g_scsi_dma.greenpak_state == GREENPAK_STOP)
  526. {
  527. // Wait for previous DMA block to end first
  528. return;
  529. }
  530. // Check how many bytes we have available from the application
  531. uint32_t count = g_scsi_dma.bytes_app - g_scsi_dma.bytes_dma;
  532. // Check amount of free space in DMA buffer
  533. uint32_t max = g_scsi_dma.dma_fillto - g_scsi_dma.dma_idx;
  534. if (count > max) count = max;
  535. uint8_t *src = g_scsi_dma.app_buf + g_scsi_dma.bytes_dma;
  536. uint32_t *dst = g_scsi_dma.dma_buf;
  537. uint32_t pos = g_scsi_dma.dma_idx;
  538. uint32_t end = pos + count;
  539. g_scsi_dma.dma_idx = end;
  540. g_scsi_dma.bytes_dma += count;
  541. g_scsi_dma.scheduled_dma = g_scsi_dma.bytes_dma;
  542. if (pos < end && g_scsi_dma.greenpak_state == GREENPAK_IO1_HIGH)
  543. {
  544. // Fix alignment so that main loop begins with PLD1HI
  545. dst[(pos++) & DMA_BUF_MASK] = g_scsi_out_byte_to_bop_pld1lo[*src++];
  546. g_scsi_dma.greenpak_state = GREENPAK_IO1_LOW;
  547. }
  548. while (pos + 4 <= end)
  549. {
  550. uint32_t input = *(uint32_t*)src;
  551. src += 4;
  552. dst[(pos++) & DMA_BUF_MASK] = g_scsi_out_byte_to_bop_pld1hi[(input >> 0) & 0xFF];
  553. dst[(pos++) & DMA_BUF_MASK] = g_scsi_out_byte_to_bop_pld1lo[(input >> 8) & 0xFF];
  554. dst[(pos++) & DMA_BUF_MASK] = g_scsi_out_byte_to_bop_pld1hi[(input >> 16) & 0xFF];
  555. dst[(pos++) & DMA_BUF_MASK] = g_scsi_out_byte_to_bop_pld1lo[(input >> 24) & 0xFF];
  556. }
  557. while (pos < end)
  558. {
  559. if (g_scsi_dma.greenpak_state == GREENPAK_IO1_HIGH)
  560. {
  561. dst[(pos++) & DMA_BUF_MASK] = g_scsi_out_byte_to_bop_pld1lo[*src++];
  562. g_scsi_dma.greenpak_state = GREENPAK_IO1_LOW;
  563. }
  564. else
  565. {
  566. dst[(pos++) & DMA_BUF_MASK] = g_scsi_out_byte_to_bop_pld1hi[*src++];
  567. g_scsi_dma.greenpak_state = GREENPAK_IO1_HIGH;
  568. }
  569. }
  570. uint32_t remain = g_scsi_dma.dma_fillto - g_scsi_dma.dma_idx;
  571. if (!g_scsi_dma.next_app_buf && remain > 0)
  572. {
  573. // Mark the end of transfer by turning PD2 off
  574. dst[(pos++) & DMA_BUF_MASK] = (GREENPAK_PLD_IO2 << 16) | (GREENPAK_PLD_IO1 << 16);
  575. g_scsi_dma.dma_idx = pos;
  576. g_scsi_dma.greenpak_state = GREENPAK_STOP;
  577. }
  578. }
  579. extern "C" void GREENPAK_IRQ()
  580. {
  581. if (EXTI_PD & GREENPAK_PLD_IO2_EXTI)
  582. {
  583. EXTI_PD = GREENPAK_PLD_IO2_EXTI;
  584. if (g_scsi_dma.bytes_app > g_scsi_dma.bytes_dma || g_scsi_dma.next_app_buf)
  585. {
  586. assert(g_scsi_dma.greenpak_state == GREENPAK_STOP);
  587. g_scsi_dma.greenpak_state = GREENPAK_IO1_LOW;
  588. // More data is available
  589. check_dma_next_buffer();
  590. refill_dmabuf();
  591. // Continue transferring
  592. GPIO_BOP(SCSI_OUT_PORT) = GREENPAK_PLD_IO2;
  593. TIMER_SWEVG(SCSI_TIMER) = TIMER_SWEVG_CH1G;
  594. }
  595. else
  596. {
  597. stop_dma();
  598. }
  599. }
  600. }
  601. static void greenpak_start_dma()
  602. {
  603. //TODO rewrite with GD32F4xx DMA methods
  604. /*
  605. // Disable channels while configuring
  606. DMA_CHCTL(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA) &= ~DMA_CHXCTL_CHEN;
  607. DMA_CHCTL(SCSI_TIMER_DMA, SCSI_TIMER_DMACHB) &= ~DMA_CHXCTL_CHEN;
  608. TIMER_CTL0(SCSI_TIMER) = 0;
  609. // Set buffer address and size
  610. DMA_CHMADDR(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA) = (uint32_t)g_scsi_dma.dma_buf;
  611. DMA_CHCNT(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA) = DMA_BUF_SIZE;
  612. // Clear pending DMA events
  613. TIMER_DMAINTEN(SCSI_TIMER) = 0;
  614. TIMER_DMAINTEN(SCSI_TIMER) = TIMER_DMAINTEN_CH1DEN | TIMER_DMAINTEN_CH3DEN;
  615. // Clear and enable interrupt
  616. DMA_INTC(SCSI_TIMER_DMA) = DMA_FLAG_ADD(DMA_FLAG_HTF | DMA_FLAG_FTF | DMA_FLAG_ERR, SCSI_TIMER_DMACHA);
  617. DMA_CHCTL(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA) |= DMA_CHXCTL_FTFIE | DMA_CHXCTL_HTFIE;
  618. exti_interrupt_flag_clear(GREENPAK_PLD_IO2_EXTI);
  619. exti_interrupt_enable(GREENPAK_PLD_IO2_EXTI);
  620. // Enable channels
  621. DMA_CHCTL(SCSI_TIMER_DMA, SCSI_TIMER_DMACHA) |= DMA_CHXCTL_CHEN;
  622. // Enable timer
  623. TIMER_CNT(SCSI_TIMER) = 0;
  624. TIMER_CTL0(SCSI_TIMER) |= TIMER_CTL0_CEN;
  625. // Generate first event
  626. TIMER_SWEVG(SCSI_TIMER) = TIMER_SWEVG_CH1G;
  627. */
  628. }
  629. static void greenpak_stop_dma()
  630. {
  631. exti_interrupt_disable(GREENPAK_PLD_IO2_EXTI);
  632. }
  633. #endif