hpm_dma_drv.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Copyright (c) 2021 - 2022 hpmicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_DMA_DRV_H
  8. #define HPM_DMA_DRV_H
  9. #include "hpm_common.h"
  10. #include "hpm_soc_feature.h"
  11. #include "hpm_dma_regs.h"
  12. /**
  13. *
  14. * @brief DMA driver APIs
  15. * @defgroup dma_interface DMA driver APIs
  16. * @ingroup io_interfaces
  17. * @{
  18. */
  19. #define DMA_NUM_TRANSFER_PER_BURST_1T (0U)
  20. #define DMA_NUM_TRANSFER_PER_BURST_2T (1U)
  21. #define DMA_NUM_TRANSFER_PER_BURST_4T (2U)
  22. #define DMA_NUM_TRANSFER_PER_BURST_8T (3U)
  23. #define DMA_NUM_TRANSFER_PER_BURST_16T (4U)
  24. #define DMA_NUM_TRANSFER_PER_BURST_32T (5U)
  25. #define DMA_NUM_TRANSFER_PER_BURST_64T (6U)
  26. #define DMA_NUM_TRANSFER_PER_BURST_128T (7U)
  27. #define DMA_NUM_TRANSFER_PER_BURST_256T (8U)
  28. #define DMA_NUM_TRANSFER_PER_BURST_512T (9U)
  29. #define DMA_NUM_TRANSFER_PER_BURST_1024T (10U)
  30. #define DMA_TRANSFER_WIDTH_BYTE (0U)
  31. #define DMA_TRANSFER_WIDTH_HALF_WORD (1U)
  32. #define DMA_TRANSFER_WIDTH_WORD (2U)
  33. #define DMA_TRANSFER_WIDTH_DOUBLE_WORD (3U)
  34. #define DMA_TRANSFER_WIDTH_QUAD_WORD (4U)
  35. #define DMA_TRANSFER_WIDTH_EIGHT_WORD (5U)
  36. #define DMA_STATUS_ERROR_SHIFT (0U)
  37. #define DMA_STATUS_ABORT_SHIFT (8U)
  38. #define DMA_STATUS_TC_SHIFT (16U)
  39. #define DMA_CHANNEL_STATUS_ONGOING (1U)
  40. #define DMA_CHANNEL_STATUS_ERROR (2U)
  41. #define DMA_CHANNEL_STATUS_ABORT (4U)
  42. #define DMA_CHANNEL_STATUS_TC (8U)
  43. #define DMA_CHANNEL_IRQ_STATUS_ERROR(x) (uint32_t)(1 << (DMA_STATUS_ERROR_SHIFT + x))
  44. #define DMA_CHANNEL_IRQ_STATUS_ABORT(x) (uint32_t)(1 << (DMA_STATUS_ABORT_SHIFT + x))
  45. #define DMA_CHANNEL_IRQ_STATUS_TC(x) (uint32_t)(1 << (DMA_STATUS_TC_SHIFT + x))
  46. #define DMA_CHANNEL_IRQ_STATUS(x) (uint32_t)(DMA_CHANNEL_IRQ_STATUS_TC(x) | \
  47. DMA_CHANNEL_IRQ_STATUS_ABORT(x) | \
  48. DMA_CHANNEL_IRQ_STATUS_ERROR(x))
  49. #define DMA_CHANNEL_IRQ_STATUS_GET_ALL_TC(x) ((x) & (((0x01UL << DMA_SOC_CHANNEL_NUM) - 1) << DMA_STATUS_TC_SHIFT))
  50. #define DMA_CHANNEL_IRQ_STATUS_GET_ALL_ABORT(x) ((x) & (((0x01UL << DMA_SOC_CHANNEL_NUM) - 1) << DMA_STATUS_ABORT_SHIFT))
  51. #define DMA_CHANNEL_IRQ_STATUS_GET_ALL_ERROR(x) ((x) & (((0x01UL << DMA_SOC_CHANNEL_NUM) - 1) << DMA_STATUS_ERROR_SHIFT))
  52. #define DMA_HANDSHAKE_MODE_HANDSHAKE (1U)
  53. #define DMA_HANDSHAKE_MODE_NORMAL (0U)
  54. #define DMA_ADDRESS_CONTROL_INCREMENT (0U)
  55. #define DMA_ADDRESS_CONTROL_DECREMENT (1U)
  56. #define DMA_ADDRESS_CONTROL_FIXED (2U)
  57. #define DMA_INTERRUPT_MASK_NONE (0U)
  58. #define DMA_INTERRUPT_MASK_ERROR DMA_CHCTRL_CTRL_INTERRMASK_MASK
  59. #define DMA_INTERRUPT_MASK_ABORT DMA_CHCTRL_CTRL_INTABTMASK_MASK
  60. #define DMA_INTERRUPT_MASK_TERMINAL_COUNT DMA_CHCTRL_CTRL_INTTCMASK_MASK
  61. #define DMA_INTERRUPT_MASK_ALL \
  62. (uint8_t)(DMA_INTERRUPT_MASK_TERMINAL_COUNT \
  63. | DMA_INTERRUPT_MASK_ABORT \
  64. | DMA_INTERRUPT_MASK_ERROR)
  65. #ifndef DMA_SUPPORT_64BIT_ADDR
  66. #define DMA_SUPPORT_64BIT_ADDR (0)
  67. #endif
  68. /**
  69. * @brief Linked descriptor
  70. *
  71. * It is consumed by DMA controlled directly
  72. */
  73. typedef struct dma_linked_descriptor {
  74. uint32_t ctrl; /**< Control */
  75. uint32_t trans_size; /**< Transfer size in source width */
  76. uint32_t src_addr; /**< Source address */
  77. uint32_t src_addr_high; /**< Source address high 32-bit, only valid when bus width > 32bits */
  78. uint32_t dst_addr; /**< Destination address */
  79. uint32_t dst_addr_high; /**< Destination address high 32-bit, only valid when bus width > 32bits */
  80. uint32_t linked_ptr; /**< Linked descriptor address */
  81. uint32_t linked_ptr_high; /**< Linked descriptor address high 32-bit, , only valid when bus width > 32bits */
  82. } dma_linked_descriptor_t;
  83. /* @brief Channel config */
  84. typedef struct dma_channel_config {
  85. uint8_t priority; /**< Channel priority */
  86. uint8_t src_burst_size; /**< Source burst size */
  87. uint8_t src_mode; /**< Source work mode */
  88. uint8_t dst_mode; /**< Destination work mode */
  89. uint8_t src_width; /**< Source width */
  90. uint8_t dst_width; /**< Destination width */
  91. uint8_t src_addr_ctrl; /**< Source address control */
  92. uint8_t dst_addr_ctrl; /**< Destination address control */
  93. uint16_t interrupt_mask; /**< Interrupt mask */
  94. uint32_t src_addr; /**< Source address */
  95. uint32_t dst_addr; /**< Destination address */
  96. uint32_t linked_ptr; /**< Next linked descriptor */
  97. uint32_t size_in_byte; /**< Total size to be transferred in byte */
  98. #if DMA_SUPPORT_64BIT_ADDR
  99. uint32_t src_addr_high; /**< Source address high 32bits */
  100. uint32_t dst_addr_high; /**< Destination address high 32bits */
  101. uint32_t linked_ptr_high; /**< Linked descriptor high 32bits */
  102. #endif
  103. } dma_channel_config_t;
  104. /* @brief Channel config */
  105. typedef struct dma_handshake_config {
  106. uint32_t dst;
  107. uint32_t src;
  108. uint32_t size_in_byte;
  109. uint8_t ch_index;
  110. bool dst_fixed;
  111. bool src_fixed;
  112. } dma_handshake_config_t;
  113. /* @brief DMA specific status */
  114. enum {
  115. status_dma_transfer_done = MAKE_STATUS(status_group_dma, 0),
  116. status_dma_transfer_error = MAKE_STATUS(status_group_dma, 1),
  117. status_dma_transfer_abort = MAKE_STATUS(status_group_dma, 2),
  118. status_dma_transfer_ongoing = MAKE_STATUS(status_group_dma, 3),
  119. status_dma_alignment_error = MAKE_STATUS(status_group_dma, 4),
  120. };
  121. #ifdef __cplusplus
  122. extern "C" {
  123. #endif
  124. /**
  125. * @brief Reset DMA
  126. *
  127. * @param[in] ptr DMA base address
  128. */
  129. static inline void dma_reset(DMA_Type *ptr)
  130. {
  131. ptr->DMACTRL |= DMA_DMACTRL_RESET_MASK;
  132. }
  133. /**
  134. * @brief Enable DMA channel
  135. *
  136. * @param[in] ptr DMA base address
  137. * @param[in] ch_index Index of the channel to be enabled
  138. *
  139. * @return status_success if everything's okay
  140. */
  141. static inline hpm_stat_t dma_enable_channel(DMA_Type *ptr, uint32_t ch_index)
  142. {
  143. ptr->CHCTRL[ch_index].CTRL |= DMA_CHCTRL_CTRL_ENABLE_MASK;
  144. if ((ptr->CHEN == 0) || !(ptr->CHEN & 1 << ch_index)) {
  145. return status_fail;
  146. }
  147. return status_success;
  148. }
  149. /**
  150. * @brief Disable DMA channel
  151. *
  152. * @param[in] ptr DMA base address
  153. * @param[in] ch_index Index of the channel to be disabled
  154. *
  155. */
  156. static inline void dma_disable_channel(DMA_Type *ptr, uint32_t ch_index)
  157. {
  158. ptr->CHCTRL[ch_index].CTRL &= ~DMA_CHCTRL_CTRL_ENABLE_MASK;
  159. }
  160. /**
  161. * @brief Abort channel transfer with mask
  162. *
  163. * @param[in] ptr DMA base address
  164. * @param[in] ch_index_mask Mask of channels to be aborted
  165. */
  166. static inline void dma_abort_channel(DMA_Type *ptr, uint32_t ch_index_mask)
  167. {
  168. ptr->CHABORT |= DMA_CHABORT_CHABORT_SET(ch_index_mask);
  169. }
  170. /**
  171. * @brief Check if channels are enabled with mask
  172. *
  173. * @param[in] ptr DMA base address
  174. * @param[in] ch_index_mask Mask of channels to be checked
  175. *
  176. * @return Enabled channel mask
  177. */
  178. static inline uint32_t dma_check_enabled_channel(DMA_Type *ptr,
  179. uint32_t ch_index_mask)
  180. {
  181. return (ch_index_mask & ptr->CHEN);
  182. }
  183. /**
  184. * @brief Check if linked pointer has been configured
  185. *
  186. * @param[in] ptr DMA base address
  187. * @param[in] ch_index Target channel index to be checked
  188. *
  189. * @return true if linked pointer has been configured
  190. */
  191. static inline bool dma_has_linked_pointer_configured(DMA_Type *ptr, uint32_t ch_index)
  192. {
  193. return ptr->CHCTRL[ch_index].LLPOINTER != 0;
  194. }
  195. /**
  196. * @brief Check transfer status
  197. *
  198. * @param[in] ptr DMA base address
  199. * @param[in] ch_index Target channel index to be checked
  200. *
  201. * @retval 1 if transfer is still ongoing
  202. * @retval 2 if any error occurred during transferring
  203. * @retval 4 if transfer is aborted
  204. * @retval 8 if transfer is finished without error
  205. */
  206. static inline uint32_t dma_check_transfer_status(DMA_Type *ptr, uint8_t ch_index)
  207. {
  208. volatile uint32_t tmp = ptr->INTSTATUS;
  209. volatile uint32_t tmp_channel;
  210. uint32_t dma_status;
  211. dma_status = 0;
  212. tmp_channel = tmp & (1 << (DMA_STATUS_TC_SHIFT + ch_index));
  213. if (tmp_channel) {
  214. dma_status |= DMA_CHANNEL_STATUS_TC;
  215. ptr->INTSTATUS = tmp_channel;
  216. }
  217. tmp_channel = tmp & (1 << (DMA_STATUS_ERROR_SHIFT + ch_index));
  218. if (tmp_channel) {
  219. dma_status |= DMA_CHANNEL_STATUS_ERROR;
  220. ptr->INTSTATUS = tmp_channel;
  221. }
  222. tmp_channel = tmp & (1 << (DMA_STATUS_ABORT_SHIFT + ch_index));
  223. if (tmp_channel) {
  224. dma_status |= DMA_CHANNEL_STATUS_ABORT;
  225. ptr->INTSTATUS = tmp_channel;
  226. }
  227. if (dma_status == 0) {
  228. dma_status = DMA_CHANNEL_STATUS_ONGOING;
  229. }
  230. return dma_status;
  231. }
  232. /**
  233. * @brief Enable DMA Channel interrupt
  234. *
  235. * @param [in] ptr DMA base address
  236. * @param [in] ch_index Target channel index
  237. * @param [in] interrupt_mask Interrupt mask
  238. */
  239. static inline void dma_enable_channel_interrupt(DMA_Type *ptr, uint8_t ch_index, int32_t interrupt_mask)
  240. {
  241. ptr->CHCTRL[ch_index].CTRL &= ~(interrupt_mask & (DMA_INTERRUPT_MASK_ERROR | DMA_INTERRUPT_MASK_ABORT | DMA_INTERRUPT_MASK_TERMINAL_COUNT));
  242. }
  243. /**
  244. * @brief Disable DMA Channel interrupt
  245. *
  246. * @param [in] ptr DMA base address
  247. * @param [in] ch_index Target channel index
  248. * @param [in] interrupt_mask Interrupt mask
  249. */
  250. static inline void dma_disable_channel_interrupt(DMA_Type *ptr, uint8_t ch_index, int32_t interrupt_mask)
  251. {
  252. ptr->CHCTRL[ch_index].CTRL |= (interrupt_mask & (DMA_INTERRUPT_MASK_ERROR | DMA_INTERRUPT_MASK_ABORT | DMA_INTERRUPT_MASK_TERMINAL_COUNT));
  253. }
  254. /**
  255. * @brief Check Channel interrupt master
  256. *
  257. * @param[in] ptr DMA base address
  258. * @param[in] ch_index Target channel index to be checked
  259. * @return uint32_t Interrupt mask
  260. */
  261. static inline uint32_t dma_check_channel_interrupt_mask(DMA_Type *ptr, uint8_t ch_index)
  262. {
  263. return ptr->CHCTRL[ch_index].CTRL & (DMA_INTERRUPT_MASK_ERROR | DMA_INTERRUPT_MASK_ABORT | DMA_INTERRUPT_MASK_TERMINAL_COUNT);
  264. }
  265. /**
  266. * @brief Get clear IRQ status
  267. *
  268. * @param[in] ptr DMA base address
  269. * @param[in] mask irq mask to be cleared
  270. */
  271. static inline void dma_clear_irq_status(DMA_Type *ptr, uint32_t mask)
  272. {
  273. ptr->INTSTATUS = mask; /* Write-1-Clear */
  274. }
  275. /**
  276. * @brief Get IRQ status
  277. *
  278. * @param[in] ptr DMA base address
  279. */
  280. static inline uint32_t dma_get_irq_status(DMA_Type *ptr)
  281. {
  282. return ptr->INTSTATUS;
  283. }
  284. /**
  285. * @brief Get default channel config
  286. *
  287. * @param[in] ptr DMA base address
  288. * @param[in] ch Channel config
  289. */
  290. void dma_default_channel_config(DMA_Type *ptr, dma_channel_config_t *ch);
  291. /**
  292. * @brief Setup DMA channel
  293. *
  294. * @param[in] ptr DMA base address
  295. * @param[in] ch_index Target channel index to be configured
  296. * @param[in] ch Channel config
  297. *
  298. * @return status_success if everything is okay
  299. */
  300. hpm_stat_t dma_setup_channel(DMA_Type *ptr, uint32_t ch_index,
  301. dma_channel_config_t *ch);
  302. /**
  303. * @brief Start DMA copy
  304. *
  305. * @param[in] ptr DMA base address
  306. * @param[in] ch_index Target channel index
  307. * @param[in] dst Destination address
  308. * @param[in] src Source Address
  309. * @param[in] size_in_byte Size in byte
  310. * @param[in] burst_len_in_byte Burst length in byte
  311. *
  312. * @return status_success if everthing is okay
  313. * @note: dst, src, size should be aligned with burst_len_in_byte
  314. */
  315. hpm_stat_t dma_start_memcpy(DMA_Type *ptr, uint8_t ch_index,
  316. uint32_t dst, uint32_t src,
  317. uint32_t size_in_byte, uint32_t burst_len_in_byte);
  318. /**
  319. * @brief config dma handshake function
  320. *
  321. * @param[in] ptr DMA base address
  322. * @param[in] pconfig dma handshake config pointer
  323. *
  324. * @return status_success if everthing is okay
  325. */
  326. hpm_stat_t dma_setup_handshake(DMA_Type *ptr, dma_handshake_config_t *pconfig);
  327. #ifdef __cplusplus
  328. }
  329. #endif
  330. /**
  331. * @}
  332. */
  333. #endif /* HPM_DMA_DRV_H */