drv_dma.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2015-11-19 Urey the first version
  9. */
  10. /*********************************************************************************************************
  11. ** 头文件
  12. *********************************************************************************************************/
  13. #include <stdlib.h>
  14. #include <rthw.h>
  15. #include <rtthread.h>
  16. #include <rtdevice.h>
  17. #include <dma.h>
  18. #include <cache.h>
  19. #include "board.h"
  20. #include "drv_clock.h"
  21. #include "drv_dma.h"
  22. #define JZDMA_DEBUG 0
  23. #if JZDMA_DEBUG
  24. #include <stdio.h>
  25. #define JZDMA_DBG(...) rt_kprintf(__VA_ARGS__)
  26. #else
  27. #define JZDMA_DBG(...)
  28. #endif
  29. /* 全局变量 */
  30. static struct jzdma_master _g_jzdma_master;
  31. static struct rt_dma_channel _g_rt_dma_channel[NR_DMA_CHANNELS];
  32. /*********************************************************************************************************
  33. ** 内联函数
  34. *********************************************************************************************************/
  35. const static char dcm_tsz[7] = { 1, 2, 0, 0, 3, 4, 5 };
  36. rt_inline int _fls(int x)
  37. {
  38. __asm__("clz %0, %1" : "=r" (x) : "r" (x));
  39. return 32 - x;
  40. }
  41. static inline int ffs(int word)
  42. {
  43. if (!word)
  44. return 0;
  45. return _fls(word & -word);
  46. }
  47. static inline uint16_t get_max_tsz(uint32_t val, uint32_t *dcmp)
  48. {
  49. int ord;
  50. ord = ffs(val) - 1;
  51. if (ord < 0)
  52. ord = 0;
  53. else if (ord > 6)
  54. ord = 6;
  55. *dcmp &= ~DCM_TSZ_MSK;
  56. *dcmp |= dcm_tsz[ord] << DCM_TSZ_SHF;
  57. // rt_kprintf("dcmp = %x\n",*dcmp);
  58. /* if tsz == 8, set it to 4 */
  59. return ord == 3 ? 4 : 1 << ord;
  60. }
  61. static void jzdma_mcu_reset(struct jzdma_master *master)
  62. {
  63. uint32_t dmcs;
  64. dmcs = readl(master->base + DMCS);
  65. dmcs |= 0x1;
  66. writel(dmcs, master->base + DMCS);
  67. }
  68. static uint32_t jzdma_get_current_trans_addr(struct jzdma_channel *jz_dmac,
  69. uint32_t* dst_addr,
  70. uint32_t* src_addr,
  71. uint32_t direction)
  72. {
  73. uint32_t ret_val = 0;
  74. if (jz_dmac->status == STAT_STOPED || jz_dmac->status == STAT_PREPED)
  75. return 0;
  76. if (direction == RT_DMA_MEM_TO_DEV)
  77. {
  78. ret_val = readl(jz_dmac->iomem + CH_DSA);
  79. if (src_addr)
  80. *src_addr = ret_val;
  81. if (dst_addr)
  82. *dst_addr = readl(jz_dmac->iomem + CH_DTA);
  83. }
  84. else if (direction == RT_DMA_DEV_TO_MEM)
  85. {
  86. ret_val = readl(jz_dmac->iomem + CH_DTA);
  87. if (dst_addr)
  88. *dst_addr = ret_val;
  89. if (src_addr)
  90. *src_addr = readl(jz_dmac->iomem + CH_DSA);
  91. }
  92. else if (direction == RT_DMA_MEM_TO_MEM)
  93. {
  94. if (dst_addr)
  95. *dst_addr = readl(jz_dmac->iomem + CH_DTA);
  96. if (src_addr)
  97. *src_addr = readl(jz_dmac->iomem + CH_DSA);
  98. }
  99. return ret_val;
  100. }
  101. int jzdma_funcs_status(struct rt_dma_channel *dmac)
  102. {
  103. struct jzdma_channel *jz_dmac;
  104. RT_ASSERT(dmac != RT_NULL);
  105. jz_dmac = (struct jzdma_channel *)dmac->user_data;
  106. switch (jz_dmac->status)
  107. {
  108. case STAT_STOPED:
  109. return RT_DMA_STATUS_IDLE;
  110. break;
  111. default:
  112. break;
  113. }
  114. return RT_DMA_STATUS_BUSY;
  115. }
  116. void jzdma_funcs_reset(struct rt_dma_channel *rt_dmac)
  117. {
  118. struct jzdma_channel *jz_dmac;
  119. RT_ASSERT(rt_dmac != RT_NULL);
  120. jz_dmac = (struct jzdma_channel *)rt_dmac->user_data;
  121. /* 终止当前传输 */
  122. jz_dmac->status = STAT_STOPED;
  123. jz_dmac->desc_nr = 0;
  124. /* clear dma status */
  125. writel(0, jz_dmac->iomem + CH_DCS);
  126. /* 重新设置参数 */
  127. switch (rt_dmac->config.direction)
  128. {
  129. case RT_DMA_MEM_TO_DEV:
  130. /* MEM_TO_DEV ,按照设备的地址宽度设置DCM */
  131. switch(rt_dmac->config.dst_addr_width)
  132. {
  133. case RT_DMA_BUSWIDTH_1_BYTE:
  134. jz_dmac->desc.dcm |= DCM_SP_8 | DCM_DP_8;
  135. break;
  136. case RT_DMA_BUSWIDTH_2_BYTES:
  137. jz_dmac->desc.dcm |= DCM_SP_16 | DCM_DP_16;
  138. break;
  139. case RT_DMA_BUSWIDTH_4_BYTES:
  140. jz_dmac->desc.dcm &= ~(DCM_SP_32 | DCM_DP_32);
  141. break;
  142. default:
  143. JZDMA_DBG("bus width error. \r\n");
  144. return;
  145. }
  146. break;
  147. default:
  148. /* 其他方式 按照源地址宽度设置 DCM */
  149. switch(rt_dmac->config.src_addr_width)
  150. {
  151. case RT_DMA_BUSWIDTH_1_BYTE:
  152. jz_dmac->desc.dcm |= DCM_SP_8 | DCM_DP_8;
  153. break;
  154. case RT_DMA_BUSWIDTH_2_BYTES:
  155. jz_dmac->desc.dcm |= DCM_SP_16 | DCM_DP_16;
  156. break;
  157. case RT_DMA_BUSWIDTH_4_BYTES:
  158. jz_dmac->desc.dcm &= ~(DCM_SP_32 | DCM_DP_32);
  159. break;
  160. default:
  161. JZDMA_DBG("bus width error. \r\n");
  162. return;
  163. }
  164. break;
  165. }
  166. return;
  167. }
  168. rt_size_t jzdma_funcs_transfer(struct rt_dma_channel *rt_dmac , struct dma_message *message)
  169. {
  170. struct jzdma_channel *jz_dmac;
  171. uint32_t tsz;
  172. RT_ASSERT(rt_dmac != RT_NULL);
  173. RT_ASSERT(message != RT_NULL);
  174. jz_dmac = (struct jzdma_channel *)rt_dmac->user_data;
  175. if(jz_dmac->status == STAT_RUNNING)
  176. return -RT_EBUSY;
  177. /* 清除硬件寄存器 */
  178. // writel(0, jz_dmac->iomem + CH_DCM);
  179. // writel(0, jz_dmac->iomem + CH_DCS);
  180. /* clear dma status */
  181. writel(0, jz_dmac->iomem + CH_DCS);
  182. //刷新cache
  183. switch(rt_dmac->config.direction)
  184. {
  185. case RT_DMA_MEM_TO_DEV:
  186. case RT_DMA_MEM_TO_MEM:
  187. rt_hw_dcache_flush_range((rt_ubase_t)(message->src_addr),message->t_size);
  188. break;
  189. default:
  190. break;
  191. }
  192. // /* 重新设置参数 */
  193. // switch (rt_dmac->config.direction)
  194. // {
  195. // case RT_DMA_MEM_TO_DEV:
  196. // /* MEM_TO_DEV ,按照设备的地址宽度设置DCM */
  197. // switch(rt_dmac->config.dst_addr_width)
  198. // {
  199. // case RT_DMA_BUSWIDTH_1_BYTE:
  200. // jz_dmac->desc.dcm |= DCM_SP_8 | DCM_DP_8;
  201. // break;
  202. // case RT_DMA_BUSWIDTH_2_BYTES:
  203. // jz_dmac->desc.dcm |= DCM_SP_16 | DCM_DP_16;
  204. // break;
  205. // case RT_DMA_BUSWIDTH_4_BYTES:
  206. // jz_dmac->desc.dcm &= ~(DCM_SP_32 | DCM_DP_32);
  207. // break;
  208. // default:
  209. // JZDMA_DBG("bus width error. \r\n");
  210. // return -1;
  211. // }
  212. //
  213. // break;
  214. // default:
  215. // /* 其他方式 按照源地址宽度设置 DCM */
  216. // switch(rt_dmac->config.src_addr_width)
  217. // {
  218. // case RT_DMA_BUSWIDTH_1_BYTE:
  219. // jz_dmac->desc.dcm |= DCM_SP_8 | DCM_DP_8;
  220. // break;
  221. // case RT_DMA_BUSWIDTH_2_BYTES:
  222. // jz_dmac->desc.dcm |= DCM_SP_16 | DCM_DP_16;
  223. // break;
  224. // case RT_DMA_BUSWIDTH_4_BYTES:
  225. // jz_dmac->desc.dcm &= ~(DCM_SP_32 | DCM_DP_32);
  226. // break;
  227. // default:
  228. // JZDMA_DBG("bus width error. \r\n");
  229. // return 0;
  230. // }
  231. // break;
  232. // }
  233. /* clear LINK bit when issue pending */
  234. jz_dmac->desc.dcm |= DCM_TIE;
  235. /* Disable desc link */
  236. jz_dmac->desc.dcm &= ~DCM_LINK;
  237. /* 识别传输地址控制 */
  238. switch(message->src_option)
  239. {
  240. case RT_DMA_ADDR_INC:
  241. jz_dmac->desc.dcm |= DCM_SAI;
  242. break;
  243. case RT_DMA_ADDR_FIX:
  244. jz_dmac->desc.dcm &= ~DCM_SAI;
  245. break;
  246. default:
  247. return -RT_EIO;
  248. }
  249. switch(message->dst_option)
  250. {
  251. case RT_DMA_ADDR_INC:
  252. jz_dmac->desc.dcm |= DCM_DAI;
  253. break;
  254. case RT_DMA_ADDR_FIX:
  255. jz_dmac->desc.dcm &= ~DCM_DAI;
  256. break;
  257. default:
  258. return -RT_EIO;
  259. }
  260. /* 设置TSZ */
  261. if(rt_dmac->ch == 1)
  262. {
  263. /*
  264. * for special channel1 tsz = 7 (auto)
  265. */
  266. jz_dmac->desc.dcm |= 7 << 8;
  267. tsz = message->t_size;
  268. }
  269. else
  270. {
  271. if(rt_dmac->config.direction == RT_DMA_MEM_TO_DEV)
  272. {
  273. tsz = get_max_tsz((uint32_t)(message->src_addr) | message->t_size | rt_dmac->config.dst_maxburst, &jz_dmac->desc.dcm);
  274. tsz = message->t_size / tsz;
  275. }
  276. else
  277. {
  278. tsz = get_max_tsz((uint32_t)(message->dst_addr) | message->t_size | rt_dmac->config.src_maxburst, &jz_dmac->desc.dcm);
  279. tsz = message->t_size / tsz;
  280. }
  281. }
  282. jz_dmac->desc.dsa = (uint32_t)(message->src_addr) & 0x1FFFFFFF;
  283. JZDMA_DBG("dsa = %x\n",jz_dmac->desc.dsa);
  284. jz_dmac->desc.dta = (uint32_t)(message->dst_addr) & 0x1FFFFFFF;
  285. JZDMA_DBG("dta = %x\n",jz_dmac->desc.dta);
  286. jz_dmac->desc.dtc = tsz;
  287. JZDMA_DBG("dtc = %x\n",jz_dmac->desc.dtc);
  288. // jz_dmac->desc.drt = jz_dmac->type;
  289. jz_dmac->desc.drt = (uint32_t)message->t_mode;
  290. JZDMA_DBG("drt = %x\n",jz_dmac->desc.drt);
  291. jz_dmac->desc.sd = 0;
  292. JZDMA_DBG("dcm = %x\n",jz_dmac->desc.dcm);
  293. /* I don't want to use 8-word descriptors */
  294. writel(DCS_NDES,jz_dmac->iomem + CH_DCS);
  295. /* Update DMA Channel Register */
  296. writel(jz_dmac->desc.dsa, jz_dmac->iomem + CH_DSA);
  297. writel(jz_dmac->desc.dta, jz_dmac->iomem + CH_DTA);
  298. writel(jz_dmac->desc.dtc, jz_dmac->iomem + CH_DTC);
  299. writel(jz_dmac->desc.drt, jz_dmac->iomem + CH_DRT);
  300. jz_dmac->status = STAT_RUNNING;
  301. jz_dmac->desc.dcm &= ~DCM_LINK;
  302. jz_dmac->desc.dcm |= DCM_TIE;
  303. writel(jz_dmac->desc.dcm, jz_dmac->iomem + CH_DCM);
  304. /* DCS.CTE = 1 */
  305. writel(readl(jz_dmac->iomem + CH_DCS) | DCS_CTE,(jz_dmac->iomem + CH_DCS));
  306. return message->t_size;
  307. }
  308. static void jzdma_int_handler(int vector,void *param)
  309. {
  310. struct jzdma_master *master = &_g_jzdma_master;
  311. uint32_t pending,dcs;
  312. int i;
  313. pending = readl(master->base + DIRQP);
  314. for (i = 0; i < NR_DMA_CHANNELS; i++)
  315. {
  316. struct rt_dma_channel *rt_dmac = &_g_rt_dma_channel[i];
  317. struct jzdma_channel *jz_dmac = (struct jzdma_channel *)rt_dmac->user_data;
  318. if (!(pending & (1 << i)))
  319. continue;
  320. dcs = readl(jz_dmac->iomem + CH_DCS);
  321. jz_dmac->dcs_saved = dcs;
  322. writel(0, jz_dmac->iomem + CH_DCS);
  323. if (jz_dmac->status != STAT_RUNNING)
  324. continue;
  325. /* Address Error. */
  326. if(dcs & DCS_AR)
  327. {
  328. JZDMA_DBG("Addr Error: DCS%d=%lx\n",i,dcs);
  329. rt_dma_contex_service(rt_dmac,RT_DMA_EVENT_ERROR);
  330. }
  331. /* DMA halt */
  332. if (dcs & DCS_HLT)
  333. {
  334. JZDMA_DBG("DMA Halt: DCS%d=%lx\n", i, dcs);
  335. }
  336. /* DMA 传输已完成 */
  337. if (dcs & DCS_TT)
  338. {
  339. jz_dmac->status = STAT_STOPED;
  340. JZDMA_DBG("DMA CH%d Over\n",i);
  341. //刷新cache
  342. switch(rt_dmac->config.direction)
  343. {
  344. case RT_DMA_DEV_TO_MEM:
  345. case RT_DMA_MEM_TO_MEM:
  346. {
  347. struct dma_message *message;
  348. message = rt_dma_get_current_message(rt_dmac);
  349. if(message)
  350. {
  351. // r4k_dcache_inv((rt_ubase_t)(message->dst_addr),message->t_size);
  352. rt_hw_dcache_invalidate_range((rt_ubase_t)(message->dst_addr),message->t_size);
  353. }
  354. }
  355. break;
  356. default:
  357. break;
  358. }
  359. rt_dma_contex_service(rt_dmac,RT_DMA_EVENT_COMPLETE);
  360. }
  361. }
  362. pending = readl(master->base + DMAC);
  363. pending &= ~(DMAC_HLT | DMAC_AR);
  364. writel(pending, master->base + DMAC);
  365. writel(0, master->base + DIRQP);
  366. }
  367. /* not use */
  368. static void jzdma_link_int_handler(int irq, void *param)
  369. {
  370. struct jzdma_master *master = &_g_jzdma_master;
  371. uint32_t pending;
  372. int i;
  373. pending = readl(master->base + DESIRQP);
  374. JZDMA_DBG("Link INT \n");
  375. for (i = 0; i < NR_DMA_CHANNELS; i++)
  376. {
  377. struct rt_dma_channel *rt_dmac = &_g_rt_dma_channel[i];
  378. struct jzdma_channel *jz_dmac = (struct jzdma_channel *)rt_dmac->user_data;
  379. if (!(pending & (1 << i)))
  380. continue;
  381. if (jz_dmac->status != STAT_RUNNING)
  382. continue;
  383. }
  384. writel((readl(master->base + DIC)&(~pending)),master->base + DIC);
  385. }
  386. /* RTDMA 驱动层 接口*/
  387. struct dma_ops _g_jzdma_ops =
  388. {
  389. .reset = jzdma_funcs_reset,
  390. .trans = jzdma_funcs_transfer,
  391. .status = jzdma_funcs_status
  392. };
  393. int rt_hw_jzdma_init(void)
  394. {
  395. int i;
  396. struct jzdma_master *master = &_g_jzdma_master;
  397. uint32_t pdma_program = 0;
  398. /* 使能DMA 时钟 */
  399. master->clk = clk_get("pdma");
  400. clk_enable(master->clk);
  401. master->base = DMAC_BASE;
  402. master->irq = IRQ_PDMA;
  403. master->irq_pdmad = IRQ_PDMAD;
  404. /* ???
  405. * indeed it think we should also enable special channel<0,1>
  406. * but when you guys enable it (set bit1) the main cpu will never get interrupt from dma channel when TC count down to 0
  407. */
  408. writel(1 | (0x3f << 16), master->base + DMAC);
  409. for (i = 0; i < NR_DMA_CHANNELS; i++)
  410. {
  411. struct rt_dma_channel *rt_dmac = &(_g_rt_dma_channel[i]);
  412. struct jzdma_channel *jz_dmac = &(master->channel[i]);
  413. struct dma_config config =
  414. {
  415. .direction = RT_DMA_MEM_TO_MEM,
  416. .src_addr_width = RT_DMA_BUSWIDTH_4_BYTES,
  417. .src_maxburst = (64 * 1024),
  418. .dst_addr_width = RT_DMA_BUSWIDTH_4_BYTES,
  419. .dst_maxburst = (64 * 1024),
  420. };
  421. rt_dmac->ch = i;
  422. jz_dmac->type = JZDMA_REQ_AUTO;
  423. jz_dmac->iomem = master->base + i * 0x20;
  424. jz_dmac->status = STAT_STOPED;
  425. jz_dmac->dcm_def = 0;
  426. pdma_program |= (0x01 << i);
  427. rt_dma_drv_install(rt_dmac,&_g_jzdma_ops,&config,jz_dmac);
  428. }
  429. /* the corresponding dma channel is set programmable */
  430. // writel(pdma_program, dma->base + DMACP);
  431. jzdma_mcu_reset(master);
  432. /* 注册 DMA中断 */
  433. rt_hw_interrupt_install(IRQ_PDMA,jzdma_int_handler,RT_NULL,"PDMA");
  434. rt_hw_interrupt_umask(IRQ_PDMA);
  435. rt_hw_interrupt_install(IRQ_PDMAD,jzdma_link_int_handler,RT_NULL,"PDMAD");
  436. rt_hw_interrupt_umask(IRQ_PDMAD);
  437. return RT_EOK;
  438. }
  439. INIT_DEVICE_EXPORT(rt_hw_jzdma_init);