scsi_accel_dma.cpp 23 KB

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