hal_dwdma.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. /*
  3. * Copyright (c) 2019-2021 Rockchip Electronics Co., Ltd.
  4. */
  5. #include "hal_conf.h"
  6. #ifdef HAL_DWDMA_MODULE_ENABLED
  7. /** @addtogroup RK_HAL_Driver
  8. * @{
  9. */
  10. /** @addtogroup DWDMA
  11. * @{
  12. */
  13. #ifndef _HAL_DWDMA_H
  14. #define _HAL_DWDMA_H
  15. #include "hal_def.h"
  16. /** @defgroup DWDMA_Exported_Definition_Group1 Basic Definition
  17. * @{
  18. */
  19. /***************************** MACRO Definition ******************************/
  20. #define NR_DESCS_PER_CHANNEL 8
  21. #define DMA_MAX_CHANNELS 8
  22. /* Bitfields in CTL_LO */
  23. #define DWC_CTLL_INT_EN (1 << 0) /**< irqs enabled? */
  24. #define DWC_CTLL_DST_WIDTH(n) ((n)<<1) /**< bytes per element */
  25. #define DWC_CTLL_SRC_WIDTH(n) ((n)<<4)
  26. #define DWC_CTLL_DST_INC (0<<7) /**< DAR update/not */
  27. #define DWC_CTLL_DST_DEC (1<<7)
  28. #define DWC_CTLL_DST_FIX (2<<7)
  29. #define DWC_CTLL_SRC_INC (0<<7) /**< SAR update/not */
  30. #define DWC_CTLL_SRC_DEC (1<<9)
  31. #define DWC_CTLL_SRC_FIX (2<<9)
  32. #define DWC_CTLL_DST_MSIZE(n) ((n)<<11) /**< burst, \#elements */
  33. #define DWC_CTLL_SRC_MSIZE(n) ((n)<<14)
  34. #define DWC_CTLL_S_GATH_EN (1 << 17) /**< src gather, !FIX */
  35. #define DWC_CTLL_D_SCAT_EN (1 << 18) /**< dst scatter, !FIX */
  36. #define DWC_CTLL_FC(n) ((n) << 20)
  37. #define DWC_CTLL_FC_M2M (0 << 20) /**< mem-to-mem */
  38. #define DWC_CTLL_FC_M2P (1 << 20) /**< mem-to-periph */
  39. #define DWC_CTLL_FC_P2M (2 << 20) /**< periph-to-mem */
  40. #define DWC_CTLL_FC_P2P (3 << 20) /**< periph-to-periph */
  41. /* plus 4 transfer types for peripheral-as-flow-controller */
  42. #define DWC_CTLL_DMS(n) ((n)<<23) /**< dst master select */
  43. #define DWC_CTLL_SMS(n) ((n)<<25) /**< src master select */
  44. #define DWC_CTLL_LLP_D_EN (1 << 27) /**< dest block chain */
  45. #define DWC_CTLL_LLP_S_EN (1 << 28) /**< src block chain */
  46. /* Bitfields in CTL_HI */
  47. #define DWC_CTLH_DONE 0x00001000
  48. #define DWC_CTLH_BLOCK_TS_MASK 0x00000fff
  49. /* Bitfields in CFG_LO */
  50. #define DWC_CFGL_CH_PRIOR_MASK (0x7 << 5) /**< priority mask */
  51. #define DWC_CFGL_CH_PRIOR(x) ((x) << 5) /**< priority */
  52. #define DWC_CFGL_CH_SUSP (1 << 8) /**< pause xfer */
  53. #define DWC_CFGL_FIFO_EMPTY (1 << 9) /**< pause xfer */
  54. #define DWC_CFGL_HS_DST (1 << 10) /**< handshake w/dst */
  55. #define DWC_CFGL_HS_SRC (1 << 11) /**< handshake w/src */
  56. #define DWC_CFGL_LOCK_CH_XFER (0 << 12) /**< scope of LOCK_CH */
  57. #define DWC_CFGL_LOCK_CH_BLOCK (1 << 12)
  58. #define DWC_CFGL_LOCK_CH_XACT (2 << 12)
  59. #define DWC_CFGL_LOCK_BUS_XFER (0 << 14) /**< scope of LOCK_BUS */
  60. #define DWC_CFGL_LOCK_BUS_BLOCK (1 << 14)
  61. #define DWC_CFGL_LOCK_BUS_XACT (2 << 14)
  62. #define DWC_CFGL_LOCK_CH (1 << 15) /**< channel lockout */
  63. #define DWC_CFGL_LOCK_BUS (1 << 16) /**< busmaster lockout */
  64. #define DWC_CFGL_HS_DST_POL (1 << 18) /**< dst handshake active low */
  65. #define DWC_CFGL_HS_SRC_POL (1 << 19) /**< src handshake active low */
  66. #define DWC_CFGL_MAX_BURST(x) ((x) << 20)
  67. #define DWC_CFGL_RELOAD_SAR (1 << 30)
  68. #define DWC_CFGL_RELOAD_DAR (1 << 31)
  69. /* Bitfields in CFG_HI */
  70. #define DWC_CFGH_FCMODE (1 << 0)
  71. #define DWC_CFGH_FIFO_MODE (1 << 1)
  72. #define DWC_CFGH_PROTCTL(x) ((x) << 2)
  73. #define DWC_CFGH_DS_UPD_EN (1 << 5)
  74. #define DWC_CFGH_SS_UPD_EN (1 << 6)
  75. #define DWC_CFGH_SRC_PER(x) ((x) << 7)
  76. #define DWC_CFGH_DST_PER(x) ((x) << 11)
  77. /* Bitfields in SGR */
  78. #define DWC_SGR_SGI(x) ((x) << 0)
  79. #define DWC_SGR_SGC(x) ((x) << 20)
  80. /* Bitfields in DSR */
  81. #define DWC_DSR_DSI(x) ((x) << 0)
  82. #define DWC_DSR_DSC(x) ((x) << 20)
  83. /* Bitfields in CFG */
  84. #define DW_CFG_DMA_EN (1 << 0)
  85. /* bursts size */
  86. typedef enum {
  87. DWDMA_MSIZE_1,
  88. DWDMA_MSIZE_4,
  89. DWDMA_MSIZE_8,
  90. DWDMA_MSIZE_16,
  91. DWDMA_MSIZE_32,
  92. DWDMA_MSIZE_64,
  93. DWDMA_MSIZE_128,
  94. DWDMA_MSIZE_256,
  95. } eDWDMA_MSIZE;
  96. /***************************** Structure Definition **************************/
  97. /**
  98. * LLI == Linked List Item; a.k.a. DMA block descriptor
  99. */
  100. struct DW_LLI {
  101. uint32_t sar; /**< values that are not changed by hardware */
  102. uint32_t dar; /**< values that are not changed by hardware */
  103. uint32_t llp; /**< chain to next lli */
  104. uint32_t ctllo;
  105. uint32_t ctlhi;
  106. uint32_t sstat; /**< values that may get written back. */
  107. uint32_t dstat; /**< values that may get written back. */
  108. };
  109. /**
  110. * struct DW_DESC - dma transfer desc
  111. */
  112. struct DW_DESC {
  113. struct DW_LLI lli; /**< FIRST values the hardware uses, must be in the first place. */
  114. uint32_t len;
  115. };
  116. /**
  117. * struct DWDMA_CHAN - dw dma channel.
  118. */
  119. struct DWDMA_CHAN {
  120. struct HAL_DWDMA_DEV *dw;
  121. struct DMA_CHAN_REGS *creg;
  122. struct DMA_SLAVE_CONFIG config;
  123. struct DW_DESC *desc;
  124. eDMA_TRANSFER_DIRECTION direction;
  125. uint8_t mask;
  126. uint8_t srcMaster;
  127. uint8_t dstMaster;
  128. uint8_t periId;
  129. uint8_t chanId;
  130. bool cyclic;
  131. bool paused;
  132. DMA_Callback callback;
  133. void *cparam;
  134. };
  135. /**
  136. * struct HAL_DWDMA_DEV - dw dma hal dev.
  137. */
  138. struct HAL_DWDMA_DEV {
  139. struct DMA_REG *pReg;
  140. struct DWDMA_CHAN chan[DMA_MAX_CHANNELS];
  141. uint8_t irq[DMA_MAX_CHANNELS];
  142. uint8_t allChanMask;
  143. uint8_t used;
  144. uint8_t maxChans;
  145. /* hardware configuration */
  146. uint8_t dataWidth;
  147. uint32_t blockSize;
  148. void *priv;
  149. };
  150. /** @} */
  151. /***************************** Function Declare ******************************/
  152. /** @defgroup DWDMA_Public_Function_Declare Public Function Declare
  153. * @{
  154. */
  155. uint32_t HAL_DWDMA_GetRawBlockStatus(struct HAL_DWDMA_DEV *dw);
  156. uint32_t HAL_DWDMA_GetRawErrStatus(struct HAL_DWDMA_DEV *dw);
  157. uint32_t HAL_DWDMA_GetRawXferStatus(struct HAL_DWDMA_DEV *dw);
  158. HAL_Status HAL_DWDMA_Init(struct HAL_DWDMA_DEV *dw);
  159. HAL_Status HAL_DWDMA_DeInit(struct HAL_DWDMA_DEV *dw);
  160. HAL_Status HAL_DWDMA_Start(struct DWDMA_CHAN *dwc);
  161. HAL_Status HAL_DWDMA_Stop(struct DWDMA_CHAN *dwc);
  162. HAL_Status HAL_DWDMA_Pause(struct DWDMA_CHAN *dwc);
  163. HAL_Status HAL_DWDMA_Resume(struct DWDMA_CHAN *dwc);
  164. struct DWDMA_CHAN *HAL_DWDMA_RequestChannel(struct HAL_DWDMA_DEV *dma, DMA_REQ_Type id);
  165. HAL_Status HAL_DWDMA_ReleaseChannel(struct DWDMA_CHAN *dwc);
  166. HAL_Status HAL_DWDMA_Config(struct DWDMA_CHAN *dwc, struct DMA_SLAVE_CONFIG *config);
  167. HAL_Status HAL_DWDMA_PrepDmaMemcpy(struct DWDMA_CHAN *dwc, uint32_t dst,
  168. uint32_t src, uint32_t len,
  169. DMA_Callback callback, void *cparam);
  170. HAL_Status HAL_DWDMA_PrepDmaCyclic(struct DWDMA_CHAN *dwc, uint32_t dmaAddr,
  171. uint32_t len, uint32_t periodLen,
  172. eDMA_TRANSFER_DIRECTION direction,
  173. DMA_Callback callback, void *cparam);
  174. HAL_Status HAL_DWDMA_PrepDmaSingle(struct DWDMA_CHAN *dwc, uint32_t dmaAddr,
  175. uint32_t len,
  176. eDMA_TRANSFER_DIRECTION direction,
  177. DMA_Callback callback, void *cparam);
  178. uint32_t HAL_DWDMA_IrqHandler(struct HAL_DWDMA_DEV *dw, uint32_t chanId);
  179. uint32_t HAL_DWDMA_HandleChan(struct HAL_DWDMA_DEV *dw, uint32_t chanId);
  180. struct DWDMA_CHAN *HAL_DWDMA_GetChannel(struct HAL_DWDMA_DEV *dw, uint32_t chanId);
  181. /** @} */
  182. #endif
  183. /** @} */
  184. /** @} */
  185. #endif /* HAL_DWDMA_MODULE_ENABLED */