dma_wrap.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (c) 2019-2025 Allwinner Technology Co., Ltd. ALL rights reserved.
  3. *
  4. * Allwinner is a trademark of Allwinner Technology Co.,Ltd., registered in
  5. * the the people's Republic of China and other countries.
  6. * All Allwinner Technology Co.,Ltd. trademarks are used with permission.
  7. *
  8. * DISCLAIMER
  9. * THIRD PARTY LICENCES MAY BE REQUIRED TO IMPLEMENT THE SOLUTION/PRODUCT.
  10. * IF YOU NEED TO INTEGRATE THIRD PARTY’S TECHNOLOGY (SONY, DTS, DOLBY, AVS OR MPEGLA, ETC.)
  11. * IN ALLWINNERS’SDK OR PRODUCTS, YOU SHALL BE SOLELY RESPONSIBLE TO OBTAIN
  12. * ALL APPROPRIATELY REQUIRED THIRD PARTY LICENCES.
  13. * ALLWINNER SHALL HAVE NO WARRANTY, INDEMNITY OR OTHER OBLIGATIONS WITH RESPECT TO MATTERS
  14. * COVERED UNDER ANY REQUIRED THIRD PARTY LICENSE.
  15. * YOU ARE SOLELY RESPONSIBLE FOR YOUR USAGE OF THIRD PARTY’S TECHNOLOGY.
  16. *
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY ALLWINNER"AS IS" AND TO THE MAXIMUM EXTENT
  19. * PERMITTED BY LAW, ALLWINNER EXPRESSLY DISCLAIMS ALL WARRANTIES OF ANY KIND,
  20. * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION REGARDING
  21. * THE TITLE, NON-INFRINGEMENT, ACCURACY, CONDITION, COMPLETENESS, PERFORMANCE
  22. * OR MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  23. * IN NO EVENT SHALL ALLWINNER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  28. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  30. * OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #ifndef __DMA_WRAP_H_
  33. #define __DMA_WRAP_H_
  34. #include <hal_dma.h>
  35. #include <hal_cache.h>
  36. #include <hal_mem.h>
  37. struct dma_chan {
  38. struct sunxi_dma_chan *dma_handle;
  39. };
  40. #if 0
  41. static inline void dma_free_coherent(void *addr)
  42. {
  43. void *malloc_ptr = NULL;
  44. if (!addr)
  45. return;
  46. malloc_ptr = *(uint32_t *)((uint32_t *)addr - 1);
  47. hal_free(malloc_ptr);
  48. }
  49. static inline void *dma_alloc_coherent(size_t size)
  50. {
  51. void *fake_ptr = NULL;
  52. void *malloc_ptr = NULL;
  53. malloc_ptr = hal_malloc(size + 64);
  54. if ((uint32_t)malloc_ptr & 0x3) {
  55. snd_err("error: krhino_mm_alloc not align to 4 byte\r\n");
  56. }
  57. fake_ptr = (uint32_t)(malloc_ptr + 64) & (~63);
  58. *(uint32_t *)((uint32_t *)fake_ptr - 1) = malloc_ptr;
  59. return fake_ptr;
  60. }
  61. #endif
  62. static inline struct dma_chan *dma_request_channel(void)
  63. {
  64. struct dma_chan *chan = NULL;
  65. hal_dma_chan_status_t status = 0;
  66. chan = calloc(1, sizeof(struct dma_chan));
  67. status = hal_dma_chan_request(&chan->dma_handle);
  68. if (status != HAL_DMA_CHAN_STATUS_FREE) {
  69. snd_err("request dma chan failed\n");
  70. free(chan);
  71. return NULL;
  72. }
  73. return chan;
  74. }
  75. static inline void dma_release_channel(struct dma_chan *chan)
  76. {
  77. hal_dma_status_t status = 0;
  78. if (!chan)
  79. return;
  80. status = hal_dma_chan_free(chan->dma_handle);
  81. if (status != HAL_DMA_STATUS_OK)
  82. snd_err("free dma chan failed\n");
  83. free(chan);
  84. }
  85. static inline enum dma_status dmaengine_tx_status(struct dma_chan *chan,
  86. uint32_t *residue)
  87. {
  88. return hal_dma_tx_status(chan->dma_handle, residue);
  89. }
  90. static inline int dmaengine_prep_dma_cyclic(
  91. struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  92. size_t period_len, enum dma_transfer_direction dir)
  93. {
  94. hal_dma_status_t status = 0;
  95. snd_print("[%s] line:%d buf_addr:0x%x, buf_len:0x%x, period_len:0x%x\n",
  96. __func__, __LINE__, buf_addr, buf_len, period_len);
  97. status = hal_dma_prep_cyclic(chan->dma_handle,
  98. (unsigned long)buf_addr, (unsigned long)buf_len,
  99. (unsigned long)period_len, dir);
  100. if (status != HAL_DMA_STATUS_OK) {
  101. snd_err("hal_dma_prep_cyclic failed\n");
  102. return -1;
  103. }
  104. return 0;
  105. }
  106. static inline int dmaengine_submit(struct dma_chan *chan,
  107. dma_callback callback, void *callback_param)
  108. {
  109. hal_dma_status_t status = 0;
  110. snd_print("\n");
  111. status = hal_dma_callback_install(chan->dma_handle,
  112. callback, callback_param);
  113. if (status != HAL_DMA_STATUS_OK) {
  114. snd_err("hal_dma_prep_cyclic failed\n");
  115. return -1;
  116. }
  117. return 0;
  118. }
  119. static inline int dmaengine_slave_config(struct dma_chan *chan,
  120. struct dma_slave_config *config)
  121. {
  122. hal_dma_status_t status = 0;
  123. snd_print("\n");
  124. status = hal_dma_slave_config(chan->dma_handle, config);
  125. if (status != HAL_DMA_STATUS_OK) {
  126. snd_err("hal_dma_slave_config failed\n");
  127. return -1;
  128. }
  129. return 0;
  130. }
  131. static inline void dma_async_issue_pending(struct dma_chan *chan)
  132. {
  133. hal_dma_status_t status = 0;
  134. snd_print("\n");
  135. status = hal_dma_start(chan->dma_handle);
  136. if (status != HAL_DMA_STATUS_OK) {
  137. snd_err("hal_dma_start failed\n");
  138. return ;
  139. }
  140. return;
  141. }
  142. static inline int dmaengine_terminate_async(struct dma_chan *chan)
  143. {
  144. hal_dma_status_t status = 0;
  145. status = hal_dma_stop(chan->dma_handle);
  146. if (status != HAL_DMA_STATUS_OK) {
  147. snd_err("hal_dma_stop failed\n");
  148. return -1;
  149. }
  150. status = hal_dma_chan_desc_free(chan->dma_handle);
  151. if (status != HAL_DMA_STATUS_OK) {
  152. snd_err("hal_dma_chan_desc_free failed, return:%d\n", status);
  153. return -1;
  154. }
  155. return 0;
  156. }
  157. static inline int dmaengine_pause(struct dma_chan *chan)
  158. {
  159. printf("dma pause not support.\n");
  160. return -1;
  161. }
  162. static inline int dmaengine_resume(struct dma_chan *chan)
  163. {
  164. printf("dma resume not support.\n");
  165. return -1;
  166. }
  167. #endif /* __DMA_WRAP_H_ */