dmacHw_extra.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. /*****************************************************************************
  2. * Copyright 2003 - 2008 Broadcom Corporation. All rights reserved.
  3. *
  4. * Unless you and Broadcom execute a separate written software license
  5. * agreement governing use of this software, this software is licensed to you
  6. * under the terms of the GNU General Public License version 2, available at
  7. * http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  8. *
  9. * Notwithstanding the above, under no circumstances may you combine this
  10. * software in any way with any other Broadcom software provided under a
  11. * license other than the GPL, without Broadcom's express prior written
  12. * consent.
  13. *****************************************************************************/
  14. /****************************************************************************/
  15. /**
  16. * @file dmacHw_extra.c
  17. *
  18. * @brief Extra Low level DMA controller driver routines
  19. *
  20. * @note
  21. *
  22. * These routines provide basic DMA functionality only.
  23. */
  24. /****************************************************************************/
  25. /* ---- Include Files ---------------------------------------------------- */
  26. #include <csp/stdint.h>
  27. #include <stddef.h>
  28. #include <csp/dmacHw.h>
  29. #include <mach/csp/dmacHw_reg.h>
  30. #include <mach/csp/dmacHw_priv.h>
  31. extern dmacHw_CBLK_t dmacHw_gCblk[dmacHw_MAX_CHANNEL_COUNT]; /* Declared in dmacHw.c */
  32. /* ---- External Function Prototypes ------------------------------------- */
  33. /* ---- Internal Use Function Prototypes --------------------------------- */
  34. /****************************************************************************/
  35. /**
  36. * @brief Overwrites data length in the descriptor
  37. *
  38. * This function overwrites data length in the descriptor
  39. *
  40. *
  41. * @return void
  42. *
  43. * @note
  44. * This is only used for PCM channel
  45. */
  46. /****************************************************************************/
  47. void dmacHw_setDataLength(dmacHw_CONFIG_t *pConfig, /* [ IN ] Configuration settings */
  48. void *pDescriptor, /* [ IN ] Descriptor buffer */
  49. size_t dataLen /* [ IN ] Data length in bytes */
  50. );
  51. /****************************************************************************/
  52. /**
  53. * @brief Helper function to display DMA registers
  54. *
  55. * @return void
  56. *
  57. *
  58. * @note
  59. * None
  60. */
  61. /****************************************************************************/
  62. static void DisplayRegisterContents(int module, /* [ IN ] DMA Controller unit (0-1) */
  63. int channel, /* [ IN ] DMA Channel (0-7) / -1(all) */
  64. int (*fpPrint) (const char *, ...) /* [ IN ] Callback to the print function */
  65. ) {
  66. int chan;
  67. (*fpPrint) ("Displaying register content \n\n");
  68. (*fpPrint) ("Module %d: Interrupt raw transfer 0x%X\n",
  69. module, (uint32_t) (dmacHw_REG_INT_RAW_TRAN(module)));
  70. (*fpPrint) ("Module %d: Interrupt raw block 0x%X\n",
  71. module, (uint32_t) (dmacHw_REG_INT_RAW_BLOCK(module)));
  72. (*fpPrint) ("Module %d: Interrupt raw src transfer 0x%X\n",
  73. module, (uint32_t) (dmacHw_REG_INT_RAW_STRAN(module)));
  74. (*fpPrint) ("Module %d: Interrupt raw dst transfer 0x%X\n",
  75. module, (uint32_t) (dmacHw_REG_INT_RAW_DTRAN(module)));
  76. (*fpPrint) ("Module %d: Interrupt raw error 0x%X\n",
  77. module, (uint32_t) (dmacHw_REG_INT_RAW_ERROR(module)));
  78. (*fpPrint) ("--------------------------------------------------\n");
  79. (*fpPrint) ("Module %d: Interrupt stat transfer 0x%X\n",
  80. module, (uint32_t) (dmacHw_REG_INT_STAT_TRAN(module)));
  81. (*fpPrint) ("Module %d: Interrupt stat block 0x%X\n",
  82. module, (uint32_t) (dmacHw_REG_INT_STAT_BLOCK(module)));
  83. (*fpPrint) ("Module %d: Interrupt stat src transfer 0x%X\n",
  84. module, (uint32_t) (dmacHw_REG_INT_STAT_STRAN(module)));
  85. (*fpPrint) ("Module %d: Interrupt stat dst transfer 0x%X\n",
  86. module, (uint32_t) (dmacHw_REG_INT_STAT_DTRAN(module)));
  87. (*fpPrint) ("Module %d: Interrupt stat error 0x%X\n",
  88. module, (uint32_t) (dmacHw_REG_INT_STAT_ERROR(module)));
  89. (*fpPrint) ("--------------------------------------------------\n");
  90. (*fpPrint) ("Module %d: Interrupt mask transfer 0x%X\n",
  91. module, (uint32_t) (dmacHw_REG_INT_MASK_TRAN(module)));
  92. (*fpPrint) ("Module %d: Interrupt mask block 0x%X\n",
  93. module, (uint32_t) (dmacHw_REG_INT_MASK_BLOCK(module)));
  94. (*fpPrint) ("Module %d: Interrupt mask src transfer 0x%X\n",
  95. module, (uint32_t) (dmacHw_REG_INT_MASK_STRAN(module)));
  96. (*fpPrint) ("Module %d: Interrupt mask dst transfer 0x%X\n",
  97. module, (uint32_t) (dmacHw_REG_INT_MASK_DTRAN(module)));
  98. (*fpPrint) ("Module %d: Interrupt mask error 0x%X\n",
  99. module, (uint32_t) (dmacHw_REG_INT_MASK_ERROR(module)));
  100. (*fpPrint) ("--------------------------------------------------\n");
  101. (*fpPrint) ("Module %d: Interrupt clear transfer 0x%X\n",
  102. module, (uint32_t) (dmacHw_REG_INT_CLEAR_TRAN(module)));
  103. (*fpPrint) ("Module %d: Interrupt clear block 0x%X\n",
  104. module, (uint32_t) (dmacHw_REG_INT_CLEAR_BLOCK(module)));
  105. (*fpPrint) ("Module %d: Interrupt clear src transfer 0x%X\n",
  106. module, (uint32_t) (dmacHw_REG_INT_CLEAR_STRAN(module)));
  107. (*fpPrint) ("Module %d: Interrupt clear dst transfer 0x%X\n",
  108. module, (uint32_t) (dmacHw_REG_INT_CLEAR_DTRAN(module)));
  109. (*fpPrint) ("Module %d: Interrupt clear error 0x%X\n",
  110. module, (uint32_t) (dmacHw_REG_INT_CLEAR_ERROR(module)));
  111. (*fpPrint) ("--------------------------------------------------\n");
  112. (*fpPrint) ("Module %d: SW source req 0x%X\n",
  113. module, (uint32_t) (dmacHw_REG_SW_HS_SRC_REQ(module)));
  114. (*fpPrint) ("Module %d: SW dest req 0x%X\n",
  115. module, (uint32_t) (dmacHw_REG_SW_HS_DST_REQ(module)));
  116. (*fpPrint) ("Module %d: SW source signal 0x%X\n",
  117. module, (uint32_t) (dmacHw_REG_SW_HS_SRC_SGL_REQ(module)));
  118. (*fpPrint) ("Module %d: SW dest signal 0x%X\n",
  119. module, (uint32_t) (dmacHw_REG_SW_HS_DST_SGL_REQ(module)));
  120. (*fpPrint) ("Module %d: SW source last 0x%X\n",
  121. module, (uint32_t) (dmacHw_REG_SW_HS_SRC_LST_REQ(module)));
  122. (*fpPrint) ("Module %d: SW dest last 0x%X\n",
  123. module, (uint32_t) (dmacHw_REG_SW_HS_DST_LST_REQ(module)));
  124. (*fpPrint) ("--------------------------------------------------\n");
  125. (*fpPrint) ("Module %d: misc config 0x%X\n",
  126. module, (uint32_t) (dmacHw_REG_MISC_CFG(module)));
  127. (*fpPrint) ("Module %d: misc channel enable 0x%X\n",
  128. module, (uint32_t) (dmacHw_REG_MISC_CH_ENABLE(module)));
  129. (*fpPrint) ("Module %d: misc ID 0x%X\n",
  130. module, (uint32_t) (dmacHw_REG_MISC_ID(module)));
  131. (*fpPrint) ("Module %d: misc test 0x%X\n",
  132. module, (uint32_t) (dmacHw_REG_MISC_TEST(module)));
  133. if (channel == -1) {
  134. for (chan = 0; chan < 8; chan++) {
  135. (*fpPrint)
  136. ("--------------------------------------------------\n");
  137. (*fpPrint)
  138. ("Module %d: Channel %d Source 0x%X\n",
  139. module, chan,
  140. (uint32_t) (dmacHw_REG_SAR(module, chan)));
  141. (*fpPrint)
  142. ("Module %d: Channel %d Destination 0x%X\n",
  143. module, chan,
  144. (uint32_t) (dmacHw_REG_DAR(module, chan)));
  145. (*fpPrint)
  146. ("Module %d: Channel %d LLP 0x%X\n",
  147. module, chan,
  148. (uint32_t) (dmacHw_REG_LLP(module, chan)));
  149. (*fpPrint)
  150. ("Module %d: Channel %d Control (LO) 0x%X\n",
  151. module, chan,
  152. (uint32_t) (dmacHw_REG_CTL_LO(module, chan)));
  153. (*fpPrint)
  154. ("Module %d: Channel %d Control (HI) 0x%X\n",
  155. module, chan,
  156. (uint32_t) (dmacHw_REG_CTL_HI(module, chan)));
  157. (*fpPrint)
  158. ("Module %d: Channel %d Source Stats 0x%X\n",
  159. module, chan,
  160. (uint32_t) (dmacHw_REG_SSTAT(module, chan)));
  161. (*fpPrint)
  162. ("Module %d: Channel %d Dest Stats 0x%X\n",
  163. module, chan,
  164. (uint32_t) (dmacHw_REG_DSTAT(module, chan)));
  165. (*fpPrint)
  166. ("Module %d: Channel %d Source Stats Addr 0x%X\n",
  167. module, chan,
  168. (uint32_t) (dmacHw_REG_SSTATAR(module, chan)));
  169. (*fpPrint)
  170. ("Module %d: Channel %d Dest Stats Addr 0x%X\n",
  171. module, chan,
  172. (uint32_t) (dmacHw_REG_DSTATAR(module, chan)));
  173. (*fpPrint)
  174. ("Module %d: Channel %d Config (LO) 0x%X\n",
  175. module, chan,
  176. (uint32_t) (dmacHw_REG_CFG_LO(module, chan)));
  177. (*fpPrint)
  178. ("Module %d: Channel %d Config (HI) 0x%X\n",
  179. module, chan,
  180. (uint32_t) (dmacHw_REG_CFG_HI(module, chan)));
  181. }
  182. } else {
  183. chan = channel;
  184. (*fpPrint)
  185. ("--------------------------------------------------\n");
  186. (*fpPrint)
  187. ("Module %d: Channel %d Source 0x%X\n",
  188. module, chan, (uint32_t) (dmacHw_REG_SAR(module, chan)));
  189. (*fpPrint)
  190. ("Module %d: Channel %d Destination 0x%X\n",
  191. module, chan, (uint32_t) (dmacHw_REG_DAR(module, chan)));
  192. (*fpPrint)
  193. ("Module %d: Channel %d LLP 0x%X\n",
  194. module, chan, (uint32_t) (dmacHw_REG_LLP(module, chan)));
  195. (*fpPrint)
  196. ("Module %d: Channel %d Control (LO) 0x%X\n",
  197. module, chan,
  198. (uint32_t) (dmacHw_REG_CTL_LO(module, chan)));
  199. (*fpPrint)
  200. ("Module %d: Channel %d Control (HI) 0x%X\n",
  201. module, chan,
  202. (uint32_t) (dmacHw_REG_CTL_HI(module, chan)));
  203. (*fpPrint)
  204. ("Module %d: Channel %d Source Stats 0x%X\n",
  205. module, chan, (uint32_t) (dmacHw_REG_SSTAT(module, chan)));
  206. (*fpPrint)
  207. ("Module %d: Channel %d Dest Stats 0x%X\n",
  208. module, chan, (uint32_t) (dmacHw_REG_DSTAT(module, chan)));
  209. (*fpPrint)
  210. ("Module %d: Channel %d Source Stats Addr 0x%X\n",
  211. module, chan,
  212. (uint32_t) (dmacHw_REG_SSTATAR(module, chan)));
  213. (*fpPrint)
  214. ("Module %d: Channel %d Dest Stats Addr 0x%X\n",
  215. module, chan,
  216. (uint32_t) (dmacHw_REG_DSTATAR(module, chan)));
  217. (*fpPrint)
  218. ("Module %d: Channel %d Config (LO) 0x%X\n",
  219. module, chan,
  220. (uint32_t) (dmacHw_REG_CFG_LO(module, chan)));
  221. (*fpPrint)
  222. ("Module %d: Channel %d Config (HI) 0x%X\n",
  223. module, chan,
  224. (uint32_t) (dmacHw_REG_CFG_HI(module, chan)));
  225. }
  226. }
  227. /****************************************************************************/
  228. /**
  229. * @brief Helper function to display descriptor ring
  230. *
  231. * @return void
  232. *
  233. *
  234. * @note
  235. * None
  236. */
  237. /****************************************************************************/
  238. static void DisplayDescRing(void *pDescriptor, /* [ IN ] Descriptor buffer */
  239. int (*fpPrint) (const char *, ...) /* [ IN ] Callback to the print function */
  240. ) {
  241. dmacHw_DESC_RING_t *pRing = dmacHw_GET_DESC_RING(pDescriptor);
  242. dmacHw_DESC_t *pStart;
  243. if (pRing->pHead == NULL) {
  244. return;
  245. }
  246. pStart = pRing->pHead;
  247. while ((dmacHw_DESC_t *) pStart->llp != pRing->pHead) {
  248. if (pStart == pRing->pHead) {
  249. (*fpPrint) ("Head\n");
  250. }
  251. if (pStart == pRing->pTail) {
  252. (*fpPrint) ("Tail\n");
  253. }
  254. if (pStart == pRing->pProg) {
  255. (*fpPrint) ("Prog\n");
  256. }
  257. if (pStart == pRing->pEnd) {
  258. (*fpPrint) ("End\n");
  259. }
  260. if (pStart == pRing->pFree) {
  261. (*fpPrint) ("Free\n");
  262. }
  263. (*fpPrint) ("0x%X:\n", (uint32_t) pStart);
  264. (*fpPrint) ("sar 0x%0X\n", pStart->sar);
  265. (*fpPrint) ("dar 0x%0X\n", pStart->dar);
  266. (*fpPrint) ("llp 0x%0X\n", pStart->llp);
  267. (*fpPrint) ("ctl.lo 0x%0X\n", pStart->ctl.lo);
  268. (*fpPrint) ("ctl.hi 0x%0X\n", pStart->ctl.hi);
  269. (*fpPrint) ("sstat 0x%0X\n", pStart->sstat);
  270. (*fpPrint) ("dstat 0x%0X\n", pStart->dstat);
  271. (*fpPrint) ("devCtl 0x%0X\n", pStart->devCtl);
  272. pStart = (dmacHw_DESC_t *) pStart->llp;
  273. }
  274. if (pStart == pRing->pHead) {
  275. (*fpPrint) ("Head\n");
  276. }
  277. if (pStart == pRing->pTail) {
  278. (*fpPrint) ("Tail\n");
  279. }
  280. if (pStart == pRing->pProg) {
  281. (*fpPrint) ("Prog\n");
  282. }
  283. if (pStart == pRing->pEnd) {
  284. (*fpPrint) ("End\n");
  285. }
  286. if (pStart == pRing->pFree) {
  287. (*fpPrint) ("Free\n");
  288. }
  289. (*fpPrint) ("0x%X:\n", (uint32_t) pStart);
  290. (*fpPrint) ("sar 0x%0X\n", pStart->sar);
  291. (*fpPrint) ("dar 0x%0X\n", pStart->dar);
  292. (*fpPrint) ("llp 0x%0X\n", pStart->llp);
  293. (*fpPrint) ("ctl.lo 0x%0X\n", pStart->ctl.lo);
  294. (*fpPrint) ("ctl.hi 0x%0X\n", pStart->ctl.hi);
  295. (*fpPrint) ("sstat 0x%0X\n", pStart->sstat);
  296. (*fpPrint) ("dstat 0x%0X\n", pStart->dstat);
  297. (*fpPrint) ("devCtl 0x%0X\n", pStart->devCtl);
  298. }
  299. /****************************************************************************/
  300. /**
  301. * @brief Check if DMA channel is the flow controller
  302. *
  303. * @return 1 : If DMA is a flow controller
  304. * 0 : Peripheral is the flow controller
  305. *
  306. * @note
  307. * None
  308. */
  309. /****************************************************************************/
  310. static inline int DmaIsFlowController(void *pDescriptor /* [ IN ] Descriptor buffer */
  311. ) {
  312. uint32_t ttfc =
  313. (dmacHw_GET_DESC_RING(pDescriptor))->pTail->ctl.
  314. lo & dmacHw_REG_CTL_TTFC_MASK;
  315. switch (ttfc) {
  316. case dmacHw_REG_CTL_TTFC_MM_DMAC:
  317. case dmacHw_REG_CTL_TTFC_MP_DMAC:
  318. case dmacHw_REG_CTL_TTFC_PM_DMAC:
  319. case dmacHw_REG_CTL_TTFC_PP_DMAC:
  320. return 1;
  321. }
  322. return 0;
  323. }
  324. /****************************************************************************/
  325. /**
  326. * @brief Overwrites data length in the descriptor
  327. *
  328. * This function overwrites data length in the descriptor
  329. *
  330. *
  331. * @return void
  332. *
  333. * @note
  334. * This is only used for PCM channel
  335. */
  336. /****************************************************************************/
  337. void dmacHw_setDataLength(dmacHw_CONFIG_t *pConfig, /* [ IN ] Configuration settings */
  338. void *pDescriptor, /* [ IN ] Descriptor buffer */
  339. size_t dataLen /* [ IN ] Data length in bytes */
  340. ) {
  341. dmacHw_DESC_t *pProg;
  342. dmacHw_DESC_t *pHead;
  343. int srcTs = 0;
  344. int srcTrSize = 0;
  345. pHead = (dmacHw_GET_DESC_RING(pDescriptor))->pHead;
  346. pProg = pHead;
  347. srcTrSize = dmacHw_GetTrWidthInBytes(pConfig->srcMaxTransactionWidth);
  348. srcTs = dataLen / srcTrSize;
  349. do {
  350. pProg->ctl.hi = srcTs & dmacHw_REG_CTL_BLOCK_TS_MASK;
  351. pProg = (dmacHw_DESC_t *) pProg->llp;
  352. } while (pProg != pHead);
  353. }
  354. /****************************************************************************/
  355. /**
  356. * @brief Clears the interrupt
  357. *
  358. * This function clears the DMA channel specific interrupt
  359. *
  360. *
  361. * @return void
  362. *
  363. * @note
  364. * Must be called under the context of ISR
  365. */
  366. /****************************************************************************/
  367. void dmacHw_clearInterrupt(dmacHw_HANDLE_t handle /* [ IN ] DMA Channel handle */
  368. ) {
  369. dmacHw_CBLK_t *pCblk = dmacHw_HANDLE_TO_CBLK(handle);
  370. dmacHw_TRAN_INT_CLEAR(pCblk->module, pCblk->channel);
  371. dmacHw_BLOCK_INT_CLEAR(pCblk->module, pCblk->channel);
  372. dmacHw_ERROR_INT_CLEAR(pCblk->module, pCblk->channel);
  373. }
  374. /****************************************************************************/
  375. /**
  376. * @brief Returns the cause of channel specific DMA interrupt
  377. *
  378. * This function returns the cause of interrupt
  379. *
  380. * @return Interrupt status, each bit representing a specific type of interrupt
  381. *
  382. * @note
  383. * Should be called under the context of ISR
  384. */
  385. /****************************************************************************/
  386. dmacHw_INTERRUPT_STATUS_e dmacHw_getInterruptStatus(dmacHw_HANDLE_t handle /* [ IN ] DMA Channel handle */
  387. ) {
  388. dmacHw_CBLK_t *pCblk = dmacHw_HANDLE_TO_CBLK(handle);
  389. dmacHw_INTERRUPT_STATUS_e status = dmacHw_INTERRUPT_STATUS_NONE;
  390. if (dmacHw_REG_INT_STAT_TRAN(pCblk->module) &
  391. ((0x00000001 << pCblk->channel))) {
  392. status |= dmacHw_INTERRUPT_STATUS_TRANS;
  393. }
  394. if (dmacHw_REG_INT_STAT_BLOCK(pCblk->module) &
  395. ((0x00000001 << pCblk->channel))) {
  396. status |= dmacHw_INTERRUPT_STATUS_BLOCK;
  397. }
  398. if (dmacHw_REG_INT_STAT_ERROR(pCblk->module) &
  399. ((0x00000001 << pCblk->channel))) {
  400. status |= dmacHw_INTERRUPT_STATUS_ERROR;
  401. }
  402. return status;
  403. }
  404. /****************************************************************************/
  405. /**
  406. * @brief Indentifies a DMA channel causing interrupt
  407. *
  408. * This functions returns a channel causing interrupt of type dmacHw_INTERRUPT_STATUS_e
  409. *
  410. * @return NULL : No channel causing DMA interrupt
  411. * ! NULL : Handle to a channel causing DMA interrupt
  412. * @note
  413. * dmacHw_clearInterrupt() must be called with a valid handle after calling this function
  414. */
  415. /****************************************************************************/
  416. dmacHw_HANDLE_t dmacHw_getInterruptSource(void)
  417. {
  418. uint32_t i;
  419. for (i = 0; i < dmaChannelCount_0 + dmaChannelCount_1; i++) {
  420. if ((dmacHw_REG_INT_STAT_TRAN(dmacHw_gCblk[i].module) &
  421. ((0x00000001 << dmacHw_gCblk[i].channel)))
  422. || (dmacHw_REG_INT_STAT_BLOCK(dmacHw_gCblk[i].module) &
  423. ((0x00000001 << dmacHw_gCblk[i].channel)))
  424. || (dmacHw_REG_INT_STAT_ERROR(dmacHw_gCblk[i].module) &
  425. ((0x00000001 << dmacHw_gCblk[i].channel)))
  426. ) {
  427. return dmacHw_CBLK_TO_HANDLE(&dmacHw_gCblk[i]);
  428. }
  429. }
  430. return dmacHw_CBLK_TO_HANDLE(NULL);
  431. }
  432. /****************************************************************************/
  433. /**
  434. * @brief Estimates number of descriptor needed to perform certain DMA transfer
  435. *
  436. *
  437. * @return On failure : -1
  438. * On success : Number of descriptor count
  439. *
  440. *
  441. */
  442. /****************************************************************************/
  443. int dmacHw_calculateDescriptorCount(dmacHw_CONFIG_t *pConfig, /* [ IN ] Configuration settings */
  444. void *pSrcAddr, /* [ IN ] Source (Peripheral/Memory) address */
  445. void *pDstAddr, /* [ IN ] Destination (Peripheral/Memory) address */
  446. size_t dataLen /* [ IN ] Data length in bytes */
  447. ) {
  448. int srcTs = 0;
  449. int oddSize = 0;
  450. int descCount = 0;
  451. int dstTrSize = 0;
  452. int srcTrSize = 0;
  453. uint32_t maxBlockSize = dmacHw_MAX_BLOCKSIZE;
  454. dmacHw_TRANSACTION_WIDTH_e dstTrWidth;
  455. dmacHw_TRANSACTION_WIDTH_e srcTrWidth;
  456. dstTrSize = dmacHw_GetTrWidthInBytes(pConfig->dstMaxTransactionWidth);
  457. srcTrSize = dmacHw_GetTrWidthInBytes(pConfig->srcMaxTransactionWidth);
  458. /* Skip Tx if buffer is NULL or length is unknown */
  459. if ((pSrcAddr == NULL) || (pDstAddr == NULL) || (dataLen == 0)) {
  460. /* Do not initiate transfer */
  461. return -1;
  462. }
  463. /* Ensure scatter and gather are transaction aligned */
  464. if (pConfig->srcGatherWidth % srcTrSize
  465. || pConfig->dstScatterWidth % dstTrSize) {
  466. return -1;
  467. }
  468. /*
  469. Background 1: DMAC can not perform DMA if source and destination addresses are
  470. not properly aligned with the channel's transaction width. So, for successful
  471. DMA transfer, transaction width must be set according to the alignment of the
  472. source and destination address.
  473. */
  474. /* Adjust destination transaction width if destination address is not aligned properly */
  475. dstTrWidth = pConfig->dstMaxTransactionWidth;
  476. while (dmacHw_ADDRESS_MASK(dstTrSize) & (uint32_t) pDstAddr) {
  477. dstTrWidth = dmacHw_GetNextTrWidth(dstTrWidth);
  478. dstTrSize = dmacHw_GetTrWidthInBytes(dstTrWidth);
  479. }
  480. /* Adjust source transaction width if source address is not aligned properly */
  481. srcTrWidth = pConfig->srcMaxTransactionWidth;
  482. while (dmacHw_ADDRESS_MASK(srcTrSize) & (uint32_t) pSrcAddr) {
  483. srcTrWidth = dmacHw_GetNextTrWidth(srcTrWidth);
  484. srcTrSize = dmacHw_GetTrWidthInBytes(srcTrWidth);
  485. }
  486. /* Find the maximum transaction per descriptor */
  487. if (pConfig->maxDataPerBlock
  488. && ((pConfig->maxDataPerBlock / srcTrSize) <
  489. dmacHw_MAX_BLOCKSIZE)) {
  490. maxBlockSize = pConfig->maxDataPerBlock / srcTrSize;
  491. }
  492. /* Find number of source transactions needed to complete the DMA transfer */
  493. srcTs = dataLen / srcTrSize;
  494. /* Find the odd number of bytes that need to be transferred as single byte transaction width */
  495. if (srcTs && (dstTrSize > srcTrSize)) {
  496. oddSize = dataLen % dstTrSize;
  497. /* Adjust source transaction count due to "oddSize" */
  498. srcTs = srcTs - (oddSize / srcTrSize);
  499. } else {
  500. oddSize = dataLen % srcTrSize;
  501. }
  502. /* Adjust "descCount" due to "oddSize" */
  503. if (oddSize) {
  504. descCount++;
  505. }
  506. /* Find the number of descriptor needed for total "srcTs" */
  507. if (srcTs) {
  508. descCount += ((srcTs - 1) / maxBlockSize) + 1;
  509. }
  510. return descCount;
  511. }
  512. /****************************************************************************/
  513. /**
  514. * @brief Check the existence of pending descriptor
  515. *
  516. * This function confirmes if there is any pending descriptor in the chain
  517. * to program the channel
  518. *
  519. * @return 1 : Channel need to be programmed with pending descriptor
  520. * 0 : No more pending descriptor to programe the channel
  521. *
  522. * @note
  523. * - This function should be called from ISR in case there are pending
  524. * descriptor to program the channel.
  525. *
  526. * Example:
  527. *
  528. * dmac_isr ()
  529. * {
  530. * ...
  531. * if (dmacHw_descriptorPending (handle))
  532. * {
  533. * dmacHw_initiateTransfer (handle);
  534. * }
  535. * }
  536. *
  537. */
  538. /****************************************************************************/
  539. uint32_t dmacHw_descriptorPending(dmacHw_HANDLE_t handle, /* [ IN ] DMA Channel handle */
  540. void *pDescriptor /* [ IN ] Descriptor buffer */
  541. ) {
  542. dmacHw_CBLK_t *pCblk = dmacHw_HANDLE_TO_CBLK(handle);
  543. dmacHw_DESC_RING_t *pRing = dmacHw_GET_DESC_RING(pDescriptor);
  544. /* Make sure channel is not busy */
  545. if (!CHANNEL_BUSY(pCblk->module, pCblk->channel)) {
  546. /* Check if pEnd is not processed */
  547. if (pRing->pEnd) {
  548. /* Something left for processing */
  549. return 1;
  550. }
  551. }
  552. return 0;
  553. }
  554. /****************************************************************************/
  555. /**
  556. * @brief Program channel register to stop transfer
  557. *
  558. * Ensures the channel is not doing any transfer after calling this function
  559. *
  560. * @return void
  561. *
  562. */
  563. /****************************************************************************/
  564. void dmacHw_stopTransfer(dmacHw_HANDLE_t handle /* [ IN ] DMA Channel handle */
  565. ) {
  566. dmacHw_CBLK_t *pCblk;
  567. pCblk = dmacHw_HANDLE_TO_CBLK(handle);
  568. /* Stop the channel */
  569. dmacHw_DMA_STOP(pCblk->module, pCblk->channel);
  570. }
  571. /****************************************************************************/
  572. /**
  573. * @brief Deallocates source or destination memory, allocated
  574. *
  575. * This function can be called to deallocate data memory that was DMAed successfully
  576. *
  577. * @return On failure : -1
  578. * On success : Number of buffer freed
  579. *
  580. * @note
  581. * This function will be called ONLY, when source OR destination address is pointing
  582. * to dynamic memory
  583. */
  584. /****************************************************************************/
  585. int dmacHw_freeMem(dmacHw_CONFIG_t *pConfig, /* [ IN ] Configuration settings */
  586. void *pDescriptor, /* [ IN ] Descriptor buffer */
  587. void (*fpFree) (void *) /* [ IN ] Function pointer to free data memory */
  588. ) {
  589. dmacHw_DESC_RING_t *pRing = dmacHw_GET_DESC_RING(pDescriptor);
  590. uint32_t count = 0;
  591. if (fpFree == NULL) {
  592. return -1;
  593. }
  594. while ((pRing->pFree != pRing->pTail)
  595. && (pRing->pFree->ctl.lo & dmacHw_DESC_FREE)) {
  596. if (pRing->pFree->devCtl == dmacHw_FREE_USER_MEMORY) {
  597. /* Identify, which memory to free */
  598. if (dmacHw_DST_IS_MEMORY(pConfig->transferType)) {
  599. (*fpFree) ((void *)pRing->pFree->dar);
  600. } else {
  601. /* Destination was a peripheral */
  602. (*fpFree) ((void *)pRing->pFree->sar);
  603. }
  604. /* Unmark user memory to indicate it is freed */
  605. pRing->pFree->devCtl = ~dmacHw_FREE_USER_MEMORY;
  606. }
  607. dmacHw_NEXT_DESC(pRing, pFree);
  608. count++;
  609. }
  610. return count;
  611. }
  612. /****************************************************************************/
  613. /**
  614. * @brief Prepares descriptor ring, when source peripheral working as a flow controller
  615. *
  616. * This function will update the discriptor ring by allocating buffers, when source peripheral
  617. * has to work as a flow controller to transfer data from:
  618. * - Peripheral to memory.
  619. *
  620. * @return On failure : -1
  621. * On success : Number of descriptor updated
  622. *
  623. *
  624. * @note
  625. * Channel must be configured for peripheral to memory transfer
  626. *
  627. */
  628. /****************************************************************************/
  629. int dmacHw_setVariableDataDescriptor(dmacHw_HANDLE_t handle, /* [ IN ] DMA Channel handle */
  630. dmacHw_CONFIG_t *pConfig, /* [ IN ] Configuration settings */
  631. void *pDescriptor, /* [ IN ] Descriptor buffer */
  632. uint32_t srcAddr, /* [ IN ] Source peripheral address */
  633. void *(*fpAlloc) (int len), /* [ IN ] Function pointer that provides destination memory */
  634. int len, /* [ IN ] Number of bytes "fpAlloc" will allocate for destination */
  635. int num /* [ IN ] Number of descriptor to set */
  636. ) {
  637. dmacHw_CBLK_t *pCblk = dmacHw_HANDLE_TO_CBLK(handle);
  638. dmacHw_DESC_t *pProg = NULL;
  639. dmacHw_DESC_t *pLast = NULL;
  640. dmacHw_DESC_RING_t *pRing = dmacHw_GET_DESC_RING(pDescriptor);
  641. uint32_t dstAddr;
  642. uint32_t controlParam;
  643. int i;
  644. dmacHw_ASSERT(pConfig->transferType ==
  645. dmacHw_TRANSFER_TYPE_PERIPHERAL_TO_MEM);
  646. if (num > pRing->num) {
  647. return -1;
  648. }
  649. pLast = pRing->pEnd; /* Last descriptor updated */
  650. pProg = pRing->pHead; /* First descriptor in the new list */
  651. controlParam = pConfig->srcUpdate |
  652. pConfig->dstUpdate |
  653. pConfig->srcMaxTransactionWidth |
  654. pConfig->dstMaxTransactionWidth |
  655. pConfig->srcMasterInterface |
  656. pConfig->dstMasterInterface |
  657. pConfig->srcMaxBurstWidth |
  658. pConfig->dstMaxBurstWidth |
  659. dmacHw_REG_CTL_TTFC_PM_PERI |
  660. dmacHw_REG_CTL_LLP_DST_EN |
  661. dmacHw_REG_CTL_LLP_SRC_EN | dmacHw_REG_CTL_INT_EN;
  662. for (i = 0; i < num; i++) {
  663. /* Allocate Rx buffer only for idle descriptor */
  664. if (((pRing->pHead->ctl.hi & dmacHw_DESC_FREE) == 0) ||
  665. ((dmacHw_DESC_t *) pRing->pHead->llp == pRing->pTail)
  666. ) {
  667. /* Rx descriptor is not idle */
  668. break;
  669. }
  670. /* Set source address */
  671. pRing->pHead->sar = srcAddr;
  672. if (fpAlloc) {
  673. /* Allocate memory for buffer in descriptor */
  674. dstAddr = (uint32_t) (*fpAlloc) (len);
  675. /* Check the destination address */
  676. if (dstAddr == 0) {
  677. if (i == 0) {
  678. /* Not a single descriptor is available */
  679. return -1;
  680. }
  681. break;
  682. }
  683. /* Set destination address */
  684. pRing->pHead->dar = dstAddr;
  685. }
  686. /* Set control information */
  687. pRing->pHead->ctl.lo = controlParam;
  688. /* Use "devCtl" to mark the memory that need to be freed later */
  689. pRing->pHead->devCtl = dmacHw_FREE_USER_MEMORY;
  690. /* Descriptor is now owned by the channel */
  691. pRing->pHead->ctl.hi = 0;
  692. /* Remember the descriptor last updated */
  693. pRing->pEnd = pRing->pHead;
  694. /* Update next descriptor */
  695. dmacHw_NEXT_DESC(pRing, pHead);
  696. }
  697. /* Mark the end of the list */
  698. pRing->pEnd->ctl.lo &=
  699. ~(dmacHw_REG_CTL_LLP_DST_EN | dmacHw_REG_CTL_LLP_SRC_EN);
  700. /* Connect the list */
  701. if (pLast != pProg) {
  702. pLast->ctl.lo |=
  703. dmacHw_REG_CTL_LLP_DST_EN | dmacHw_REG_CTL_LLP_SRC_EN;
  704. }
  705. /* Mark the descriptors are updated */
  706. pCblk->descUpdated = 1;
  707. if (!pCblk->varDataStarted) {
  708. /* LLP must be pointing to the first descriptor */
  709. dmacHw_SET_LLP(pCblk->module, pCblk->channel,
  710. (uint32_t) pProg - pRing->virt2PhyOffset);
  711. /* Channel, handling variable data started */
  712. pCblk->varDataStarted = 1;
  713. }
  714. return i;
  715. }
  716. /****************************************************************************/
  717. /**
  718. * @brief Read data DMAed to memory
  719. *
  720. * This function will read data that has been DMAed to memory while transferring from:
  721. * - Memory to memory
  722. * - Peripheral to memory
  723. *
  724. * @param handle -
  725. * @param ppBbuf -
  726. * @param pLen -
  727. *
  728. * @return 0 - No more data is available to read
  729. * 1 - More data might be available to read
  730. *
  731. */
  732. /****************************************************************************/
  733. int dmacHw_readTransferredData(dmacHw_HANDLE_t handle, /* [ IN ] DMA Channel handle */
  734. dmacHw_CONFIG_t *pConfig, /* [ IN ] Configuration settings */
  735. void *pDescriptor, /* [ IN ] Descriptor buffer */
  736. void **ppBbuf, /* [ OUT ] Data received */
  737. size_t *pLlen /* [ OUT ] Length of the data received */
  738. ) {
  739. dmacHw_DESC_RING_t *pRing = dmacHw_GET_DESC_RING(pDescriptor);
  740. (void)handle;
  741. if (pConfig->transferMode != dmacHw_TRANSFER_MODE_CONTINUOUS) {
  742. if (((pRing->pTail->ctl.hi & dmacHw_DESC_FREE) == 0) ||
  743. (pRing->pTail == pRing->pHead)
  744. ) {
  745. /* No receive data available */
  746. *ppBbuf = (char *)NULL;
  747. *pLlen = 0;
  748. return 0;
  749. }
  750. }
  751. /* Return read buffer and length */
  752. *ppBbuf = (char *)pRing->pTail->dar;
  753. /* Extract length of the received data */
  754. if (DmaIsFlowController(pDescriptor)) {
  755. uint32_t srcTrSize = 0;
  756. switch (pRing->pTail->ctl.lo & dmacHw_REG_CTL_SRC_TR_WIDTH_MASK) {
  757. case dmacHw_REG_CTL_SRC_TR_WIDTH_8:
  758. srcTrSize = 1;
  759. break;
  760. case dmacHw_REG_CTL_SRC_TR_WIDTH_16:
  761. srcTrSize = 2;
  762. break;
  763. case dmacHw_REG_CTL_SRC_TR_WIDTH_32:
  764. srcTrSize = 4;
  765. break;
  766. case dmacHw_REG_CTL_SRC_TR_WIDTH_64:
  767. srcTrSize = 8;
  768. break;
  769. default:
  770. dmacHw_ASSERT(0);
  771. }
  772. /* Calculate length from the block size */
  773. *pLlen =
  774. (pRing->pTail->ctl.hi & dmacHw_REG_CTL_BLOCK_TS_MASK) *
  775. srcTrSize;
  776. } else {
  777. /* Extract length from the source peripheral */
  778. *pLlen = pRing->pTail->sstat;
  779. }
  780. /* Advance tail to next descriptor */
  781. dmacHw_NEXT_DESC(pRing, pTail);
  782. return 1;
  783. }
  784. /****************************************************************************/
  785. /**
  786. * @brief Set descriptor carrying control information
  787. *
  788. * This function will be used to send specific control information to the device
  789. * using the DMA channel
  790. *
  791. *
  792. * @return -1 - On failure
  793. * 0 - On success
  794. *
  795. * @note
  796. * None
  797. */
  798. /****************************************************************************/
  799. int dmacHw_setControlDescriptor(dmacHw_CONFIG_t *pConfig, /* [ IN ] Configuration settings */
  800. void *pDescriptor, /* [ IN ] Descriptor buffer */
  801. uint32_t ctlAddress, /* [ IN ] Address of the device control register */
  802. uint32_t control /* [ IN ] Device control information */
  803. ) {
  804. dmacHw_DESC_RING_t *pRing = dmacHw_GET_DESC_RING(pDescriptor);
  805. if (ctlAddress == 0) {
  806. return -1;
  807. }
  808. /* Check the availability of descriptors in the ring */
  809. if ((pRing->pHead->ctl.hi & dmacHw_DESC_FREE) == 0) {
  810. return -1;
  811. }
  812. /* Set control information */
  813. pRing->pHead->devCtl = control;
  814. /* Set source and destination address */
  815. pRing->pHead->sar = (uint32_t) &pRing->pHead->devCtl;
  816. pRing->pHead->dar = ctlAddress;
  817. /* Set control parameters */
  818. if (pConfig->flowControler == dmacHw_FLOW_CONTROL_DMA) {
  819. pRing->pHead->ctl.lo = pConfig->transferType |
  820. dmacHw_SRC_ADDRESS_UPDATE_MODE_INC |
  821. dmacHw_DST_ADDRESS_UPDATE_MODE_INC |
  822. dmacHw_SRC_TRANSACTION_WIDTH_32 |
  823. pConfig->dstMaxTransactionWidth |
  824. dmacHw_SRC_BURST_WIDTH_0 |
  825. dmacHw_DST_BURST_WIDTH_0 |
  826. pConfig->srcMasterInterface |
  827. pConfig->dstMasterInterface | dmacHw_REG_CTL_INT_EN;
  828. } else {
  829. uint32_t transferType = 0;
  830. switch (pConfig->transferType) {
  831. case dmacHw_TRANSFER_TYPE_PERIPHERAL_TO_MEM:
  832. transferType = dmacHw_REG_CTL_TTFC_PM_PERI;
  833. break;
  834. case dmacHw_TRANSFER_TYPE_MEM_TO_PERIPHERAL:
  835. transferType = dmacHw_REG_CTL_TTFC_MP_PERI;
  836. break;
  837. default:
  838. dmacHw_ASSERT(0);
  839. }
  840. pRing->pHead->ctl.lo = transferType |
  841. dmacHw_SRC_ADDRESS_UPDATE_MODE_INC |
  842. dmacHw_DST_ADDRESS_UPDATE_MODE_INC |
  843. dmacHw_SRC_TRANSACTION_WIDTH_32 |
  844. pConfig->dstMaxTransactionWidth |
  845. dmacHw_SRC_BURST_WIDTH_0 |
  846. dmacHw_DST_BURST_WIDTH_0 |
  847. pConfig->srcMasterInterface |
  848. pConfig->dstMasterInterface |
  849. pConfig->flowControler | dmacHw_REG_CTL_INT_EN;
  850. }
  851. /* Set block transaction size to one 32 bit transaction */
  852. pRing->pHead->ctl.hi = dmacHw_REG_CTL_BLOCK_TS_MASK & 1;
  853. /* Remember the descriptor to initialize the registers */
  854. if (pRing->pProg == dmacHw_DESC_INIT) {
  855. pRing->pProg = pRing->pHead;
  856. }
  857. pRing->pEnd = pRing->pHead;
  858. /* Advance the descriptor */
  859. dmacHw_NEXT_DESC(pRing, pHead);
  860. /* Update Tail pointer if destination is a peripheral */
  861. if (!dmacHw_DST_IS_MEMORY(pConfig->transferType)) {
  862. pRing->pTail = pRing->pHead;
  863. }
  864. return 0;
  865. }
  866. /****************************************************************************/
  867. /**
  868. * @brief Sets channel specific user data
  869. *
  870. * This function associates user data to a specific DMA channel
  871. *
  872. */
  873. /****************************************************************************/
  874. void dmacHw_setChannelUserData(dmacHw_HANDLE_t handle, /* [ IN ] DMA Channel handle */
  875. void *userData /* [ IN ] User data */
  876. ) {
  877. dmacHw_CBLK_t *pCblk = dmacHw_HANDLE_TO_CBLK(handle);
  878. pCblk->userData = userData;
  879. }
  880. /****************************************************************************/
  881. /**
  882. * @brief Gets channel specific user data
  883. *
  884. * This function returns user data specific to a DMA channel
  885. *
  886. * @return user data
  887. */
  888. /****************************************************************************/
  889. void *dmacHw_getChannelUserData(dmacHw_HANDLE_t handle /* [ IN ] DMA Channel handle */
  890. ) {
  891. dmacHw_CBLK_t *pCblk = dmacHw_HANDLE_TO_CBLK(handle);
  892. return pCblk->userData;
  893. }
  894. /****************************************************************************/
  895. /**
  896. * @brief Resets descriptor control information
  897. *
  898. * @return void
  899. */
  900. /****************************************************************************/
  901. void dmacHw_resetDescriptorControl(void *pDescriptor /* [ IN ] Descriptor buffer */
  902. ) {
  903. int i;
  904. dmacHw_DESC_RING_t *pRing;
  905. dmacHw_DESC_t *pDesc;
  906. pRing = dmacHw_GET_DESC_RING(pDescriptor);
  907. pDesc = pRing->pHead;
  908. for (i = 0; i < pRing->num; i++) {
  909. /* Mark descriptor is ready to use */
  910. pDesc->ctl.hi = dmacHw_DESC_FREE;
  911. /* Look into next link list item */
  912. pDesc++;
  913. }
  914. pRing->pFree = pRing->pTail = pRing->pEnd = pRing->pHead;
  915. pRing->pProg = dmacHw_DESC_INIT;
  916. }
  917. /****************************************************************************/
  918. /**
  919. * @brief Displays channel specific registers and other control parameters
  920. *
  921. * @return void
  922. *
  923. *
  924. * @note
  925. * None
  926. */
  927. /****************************************************************************/
  928. void dmacHw_printDebugInfo(dmacHw_HANDLE_t handle, /* [ IN ] DMA Channel handle */
  929. void *pDescriptor, /* [ IN ] Descriptor buffer */
  930. int (*fpPrint) (const char *, ...) /* [ IN ] Print callback function */
  931. ) {
  932. dmacHw_CBLK_t *pCblk = dmacHw_HANDLE_TO_CBLK(handle);
  933. DisplayRegisterContents(pCblk->module, pCblk->channel, fpPrint);
  934. DisplayDescRing(pDescriptor, fpPrint);
  935. }