drv_dma.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * File : drv_dma.c
  3. * This file is part of gkipc BSP for RT-Thread distribution.
  4. *
  5. * Copyright (c) 2017 ChengDu goke Microelectronics Co., Ltd.
  6. * All rights reserved
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * Visit http://www.goke.com to get contact with goke.
  23. *
  24. * Change Logs:
  25. * Date Author Notes
  26. */
  27. /*****************************************************************************
  28. * Include Section
  29. * add all #include here
  30. *****************************************************************************/
  31. #include "drivers/drv_dma.h"
  32. #include "gd_dma.h"
  33. /*****************************************************************************
  34. * Define section
  35. * add all #define here
  36. *****************************************************************************/
  37. //#define GK_DMA_DEBUG
  38. #ifndef GK_DMA_DEBUG
  39. #define DMA_PRINT_DBG(fmt, args...)
  40. #define DMA_PRINT_ERR(fmt, args...) rt_kprintf(fmt, ##args);
  41. #else
  42. #define DMA_PRINT_DBG(fmt, args...) rt_kprintf(fmt, ##args);
  43. #define DMA_PRINT_ERR(fmt, args...) rt_kprintf(fmt, ##args);
  44. #endif
  45. /****************************************************************************
  46. * ADT section
  47. * add definition of user defined Data Type that only be used in this file here
  48. ***************************************************************************/
  49. static struct rt_dma_device _dma_device;
  50. /******************************************************************************
  51. * Function prototype section
  52. * add prototypes for all functions called by this file,execepting those
  53. * declared in header file
  54. *****************************************************************************/
  55. /*****************************************************************************
  56. * Global variables section - Exported
  57. * add declaration of global variables that will be exported here
  58. * e.g.
  59. * int8_t foo;
  60. ****************************************************************************/
  61. /*****************************************************************************
  62. * static fun;
  63. *****************************************************************************/
  64. static rt_err_t rt_dma_init(struct rt_device *dev);
  65. static rt_err_t rt_dma_open(struct rt_device *dev, rt_uint16_t oflag);
  66. static rt_err_t rt_dma_close(struct rt_device *dev);
  67. static rt_err_t rt_dma_control(struct rt_device *dev, int cmd,void *args);
  68. /*****************************************************************************
  69. * Global variables section - Local
  70. * define global variables(will be refered only in this file) here,
  71. * static keyword should be used to limit scope of local variable to this file
  72. * e.g.
  73. * static uint8_t ufoo;
  74. *****************************************************************************/
  75. /* function body */
  76. /*****************************************************************************
  77. * Description:
  78. * add funtion description here
  79. * Parameters:
  80. * description for each argument, new argument starts at new line
  81. * Return:
  82. * what does this function returned?
  83. *****************************************************************************/
  84. static rt_err_t rt_dma_init(struct rt_device *dev)
  85. {
  86. return (RT_EOK);
  87. }
  88. static rt_err_t rt_dma_open(struct rt_device *dev, rt_uint16_t oflag)
  89. {
  90. rt_uint32_t retVal = 0;
  91. struct rt_dma_device *dma;
  92. RT_ASSERT(dev != RT_NULL);
  93. dma = (struct rt_dma_device *)dev->user_data;
  94. retVal = GD_DMA_Open((GD_DMA_OPEN_PARAM_S *)&dma->openParam,(GD_HANDLE *)&dma->handle);
  95. if(retVal != RT_EOK)
  96. {
  97. DMA_PRINT_ERR("GD_DMA_Open failed!\n");
  98. return (-RT_ERROR);
  99. }
  100. return (RT_EOK);
  101. }
  102. static rt_err_t rt_dma_close(struct rt_device *dev)
  103. {
  104. rt_uint32_t retVal = 0;
  105. struct rt_dma_device *dma;
  106. RT_ASSERT(dev != RT_NULL);
  107. dma = (struct rt_dma_device *)dev->user_data;
  108. GD_DMA_Stop((GD_HANDLE)dma->handle);
  109. retVal = GD_DMA_Close((GD_HANDLE)dma->handle);
  110. if (retVal != RT_EOK)
  111. {
  112. DMA_PRINT_ERR("GD_DMA_Close failed!\n");
  113. return (-RT_ERROR);
  114. }
  115. return (RT_EOK);
  116. }
  117. static rt_err_t rt_dma_control(struct rt_device *dev, int cmd,void *args)
  118. {
  119. rt_uint32_t retVal = 0;
  120. rt_uint32_t des = 0;
  121. RT_DMA_DESCRIPTOR_S *descriptor;
  122. struct rt_dma_device *dma;
  123. RT_ASSERT(dev != RT_NULL);
  124. dma = (struct rt_dma_device *)dev->user_data;
  125. switch(cmd)
  126. {
  127. case DMA_CMD_ADD_DESCRIPTOR:
  128. descriptor = (RT_DMA_DESCRIPTOR_S *)args;
  129. retVal = GD_DMA_AddDescriptor((GD_HANDLE)dma->handle,(GD_DMA_DESCRIPTOR_S *)descriptor);
  130. if (retVal != RT_EOK)
  131. {
  132. DMA_PRINT_ERR("GD_DMA_AddDescriptor failed!\n");
  133. return (-RT_ERROR);
  134. }
  135. break;
  136. case DMA_CMD_START:
  137. des = *(rt_uint32_t*)args;
  138. retVal = GD_DMA_Start((GD_HANDLE)dma->handle,des);
  139. if (retVal != RT_EOK)
  140. {
  141. DMA_PRINT_ERR("GD_DMA_Start failed!\n");
  142. return (-RT_ERROR);
  143. }
  144. break;
  145. case DMA_CMD_STOP:
  146. GD_DMA_Stop((GD_HANDLE)dma->handle);
  147. break;
  148. default:break;
  149. }
  150. return (RT_EOK);
  151. }
  152. /**
  153. * This function register a dma device
  154. */
  155. static rt_err_t rt_hw_dma_register(const char *name,rt_uint32_t flag)
  156. {
  157. rt_uint32_t ret;
  158. struct rt_device *device;
  159. device = &(_dma_device.parent);
  160. device->type = RT_Device_Class_Miscellaneous;
  161. device->rx_indicate = RT_NULL;
  162. device->tx_complete = RT_NULL;
  163. device->init = rt_dma_init;
  164. device->open = rt_dma_open;
  165. device->close = rt_dma_close;
  166. device->read = RT_NULL;
  167. device->write = RT_NULL;
  168. device->control = rt_dma_control;
  169. device->user_data = (void *)&_dma_device;
  170. /* register a character device */
  171. ret = rt_device_register(device, name, flag);
  172. return ret;
  173. }
  174. void rt_gk_dma_init(void)
  175. {
  176. rt_uint32_t retVal = 0;
  177. retVal = GD_DMA_Init();
  178. if (retVal != RT_EOK)
  179. {
  180. DMA_PRINT_ERR("GD_DMA_Init failed!\n");
  181. return;
  182. }
  183. rt_hw_dma_register("gk_dma",RT_DEVICE_FLAG_RDWR);
  184. }