of-dma.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Device tree helpers for DMA request / controller
  3. *
  4. * Based on of_gpio.c
  5. *
  6. * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
  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 version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/device.h>
  13. #include <linux/err.h>
  14. #include <linux/module.h>
  15. #include <linux/rculist.h>
  16. #include <linux/slab.h>
  17. #include <linux/of.h>
  18. #include <linux/of_dma.h>
  19. static LIST_HEAD(of_dma_list);
  20. static DEFINE_SPINLOCK(of_dma_lock);
  21. /**
  22. * of_dma_get_controller - Get a DMA controller in DT DMA helpers list
  23. * @dma_spec: pointer to DMA specifier as found in the device tree
  24. *
  25. * Finds a DMA controller with matching device node and number for dma cells
  26. * in a list of registered DMA controllers. If a match is found the use_count
  27. * variable is increased and a valid pointer to the DMA data stored is retuned.
  28. * A NULL pointer is returned if no match is found.
  29. */
  30. static struct of_dma *of_dma_get_controller(struct of_phandle_args *dma_spec)
  31. {
  32. struct of_dma *ofdma;
  33. spin_lock(&of_dma_lock);
  34. if (list_empty(&of_dma_list)) {
  35. spin_unlock(&of_dma_lock);
  36. return NULL;
  37. }
  38. list_for_each_entry(ofdma, &of_dma_list, of_dma_controllers)
  39. if ((ofdma->of_node == dma_spec->np) &&
  40. (ofdma->of_dma_nbcells == dma_spec->args_count)) {
  41. ofdma->use_count++;
  42. spin_unlock(&of_dma_lock);
  43. return ofdma;
  44. }
  45. spin_unlock(&of_dma_lock);
  46. pr_debug("%s: can't find DMA controller %s\n", __func__,
  47. dma_spec->np->full_name);
  48. return NULL;
  49. }
  50. /**
  51. * of_dma_put_controller - Decrement use count for a registered DMA controller
  52. * @of_dma: pointer to DMA controller data
  53. *
  54. * Decrements the use_count variable in the DMA data structure. This function
  55. * should be called only when a valid pointer is returned from
  56. * of_dma_get_controller() and no further accesses to data referenced by that
  57. * pointer are needed.
  58. */
  59. static void of_dma_put_controller(struct of_dma *ofdma)
  60. {
  61. spin_lock(&of_dma_lock);
  62. ofdma->use_count--;
  63. spin_unlock(&of_dma_lock);
  64. }
  65. /**
  66. * of_dma_controller_register - Register a DMA controller to DT DMA helpers
  67. * @np: device node of DMA controller
  68. * @of_dma_xlate: translation function which converts a phandle
  69. * arguments list into a dma_chan structure
  70. * @data pointer to controller specific data to be used by
  71. * translation function
  72. *
  73. * Returns 0 on success or appropriate errno value on error.
  74. *
  75. * Allocated memory should be freed with appropriate of_dma_controller_free()
  76. * call.
  77. */
  78. int of_dma_controller_register(struct device_node *np,
  79. struct dma_chan *(*of_dma_xlate)
  80. (struct of_phandle_args *, struct of_dma *),
  81. void *data)
  82. {
  83. struct of_dma *ofdma;
  84. int nbcells;
  85. if (!np || !of_dma_xlate) {
  86. pr_err("%s: not enough information provided\n", __func__);
  87. return -EINVAL;
  88. }
  89. ofdma = kzalloc(sizeof(*ofdma), GFP_KERNEL);
  90. if (!ofdma)
  91. return -ENOMEM;
  92. nbcells = be32_to_cpup(of_get_property(np, "#dma-cells", NULL));
  93. if (!nbcells) {
  94. pr_err("%s: #dma-cells property is missing or invalid\n",
  95. __func__);
  96. kfree(ofdma);
  97. return -EINVAL;
  98. }
  99. ofdma->of_node = np;
  100. ofdma->of_dma_nbcells = nbcells;
  101. ofdma->of_dma_xlate = of_dma_xlate;
  102. ofdma->of_dma_data = data;
  103. ofdma->use_count = 0;
  104. /* Now queue of_dma controller structure in list */
  105. spin_lock(&of_dma_lock);
  106. list_add_tail(&ofdma->of_dma_controllers, &of_dma_list);
  107. spin_unlock(&of_dma_lock);
  108. return 0;
  109. }
  110. EXPORT_SYMBOL_GPL(of_dma_controller_register);
  111. /**
  112. * of_dma_controller_free - Remove a DMA controller from DT DMA helpers list
  113. * @np: device node of DMA controller
  114. *
  115. * Memory allocated by of_dma_controller_register() is freed here.
  116. */
  117. int of_dma_controller_free(struct device_node *np)
  118. {
  119. struct of_dma *ofdma;
  120. spin_lock(&of_dma_lock);
  121. if (list_empty(&of_dma_list)) {
  122. spin_unlock(&of_dma_lock);
  123. return -ENODEV;
  124. }
  125. list_for_each_entry(ofdma, &of_dma_list, of_dma_controllers)
  126. if (ofdma->of_node == np) {
  127. if (ofdma->use_count) {
  128. spin_unlock(&of_dma_lock);
  129. return -EBUSY;
  130. }
  131. list_del(&ofdma->of_dma_controllers);
  132. spin_unlock(&of_dma_lock);
  133. kfree(ofdma);
  134. return 0;
  135. }
  136. spin_unlock(&of_dma_lock);
  137. return -ENODEV;
  138. }
  139. EXPORT_SYMBOL_GPL(of_dma_controller_free);
  140. /**
  141. * of_dma_match_channel - Check if a DMA specifier matches name
  142. * @np: device node to look for DMA channels
  143. * @name: channel name to be matched
  144. * @index: index of DMA specifier in list of DMA specifiers
  145. * @dma_spec: pointer to DMA specifier as found in the device tree
  146. *
  147. * Check if the DMA specifier pointed to by the index in a list of DMA
  148. * specifiers, matches the name provided. Returns 0 if the name matches and
  149. * a valid pointer to the DMA specifier is found. Otherwise returns -ENODEV.
  150. */
  151. static int of_dma_match_channel(struct device_node *np, char *name, int index,
  152. struct of_phandle_args *dma_spec)
  153. {
  154. const char *s;
  155. if (of_property_read_string_index(np, "dma-names", index, &s))
  156. return -ENODEV;
  157. if (strcmp(name, s))
  158. return -ENODEV;
  159. if (of_parse_phandle_with_args(np, "dmas", "#dma-cells", index,
  160. dma_spec))
  161. return -ENODEV;
  162. return 0;
  163. }
  164. /**
  165. * of_dma_request_slave_channel - Get the DMA slave channel
  166. * @np: device node to get DMA request from
  167. * @name: name of desired channel
  168. *
  169. * Returns pointer to appropriate dma channel on success or NULL on error.
  170. */
  171. struct dma_chan *of_dma_request_slave_channel(struct device_node *np,
  172. char *name)
  173. {
  174. struct of_phandle_args dma_spec;
  175. struct of_dma *ofdma;
  176. struct dma_chan *chan;
  177. int count, i;
  178. if (!np || !name) {
  179. pr_err("%s: not enough information provided\n", __func__);
  180. return NULL;
  181. }
  182. count = of_property_count_strings(np, "dma-names");
  183. if (count < 0) {
  184. pr_err("%s: dma-names property missing or empty\n", __func__);
  185. return NULL;
  186. }
  187. for (i = 0; i < count; i++) {
  188. if (of_dma_match_channel(np, name, i, &dma_spec))
  189. continue;
  190. ofdma = of_dma_get_controller(&dma_spec);
  191. if (!ofdma)
  192. continue;
  193. chan = ofdma->of_dma_xlate(&dma_spec, ofdma);
  194. of_dma_put_controller(ofdma);
  195. of_node_put(dma_spec.np);
  196. if (chan)
  197. return chan;
  198. }
  199. return NULL;
  200. }
  201. /**
  202. * of_dma_simple_xlate - Simple DMA engine translation function
  203. * @dma_spec: pointer to DMA specifier as found in the device tree
  204. * @of_dma: pointer to DMA controller data
  205. *
  206. * A simple translation function for devices that use a 32-bit value for the
  207. * filter_param when calling the DMA engine dma_request_channel() function.
  208. * Note that this translation function requires that #dma-cells is equal to 1
  209. * and the argument of the dma specifier is the 32-bit filter_param. Returns
  210. * pointer to appropriate dma channel on success or NULL on error.
  211. */
  212. struct dma_chan *of_dma_simple_xlate(struct of_phandle_args *dma_spec,
  213. struct of_dma *ofdma)
  214. {
  215. int count = dma_spec->args_count;
  216. struct of_dma_filter_info *info = ofdma->of_dma_data;
  217. if (!info || !info->filter_fn)
  218. return NULL;
  219. if (count != 1)
  220. return NULL;
  221. return dma_request_channel(info->dma_cap, info->filter_fn,
  222. &dma_spec->args[0]);
  223. }
  224. EXPORT_SYMBOL_GPL(of_dma_simple_xlate);