drv_dma.c 15 KB

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