of-dma.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. const __be32 *prop;
  86. if (!np || !of_dma_xlate) {
  87. pr_err("%s: not enough information provided\n", __func__);
  88. return -EINVAL;
  89. }
  90. ofdma = kzalloc(sizeof(*ofdma), GFP_KERNEL);
  91. if (!ofdma)
  92. return -ENOMEM;
  93. prop = of_get_property(np, "#dma-cells", NULL);
  94. if (prop)
  95. nbcells = be32_to_cpup(prop);
  96. if (!prop || !nbcells) {
  97. pr_err("%s: #dma-cells property is missing or invalid\n",
  98. __func__);
  99. kfree(ofdma);
  100. return -EINVAL;
  101. }
  102. ofdma->of_node = np;
  103. ofdma->of_dma_nbcells = nbcells;
  104. ofdma->of_dma_xlate = of_dma_xlate;
  105. ofdma->of_dma_data = data;
  106. ofdma->use_count = 0;
  107. /* Now queue of_dma controller structure in list */
  108. spin_lock(&of_dma_lock);
  109. list_add_tail(&ofdma->of_dma_controllers, &of_dma_list);
  110. spin_unlock(&of_dma_lock);
  111. return 0;
  112. }
  113. EXPORT_SYMBOL_GPL(of_dma_controller_register);
  114. /**
  115. * of_dma_controller_free - Remove a DMA controller from DT DMA helpers list
  116. * @np: device node of DMA controller
  117. *
  118. * Memory allocated by of_dma_controller_register() is freed here.
  119. */
  120. int of_dma_controller_free(struct device_node *np)
  121. {
  122. struct of_dma *ofdma;
  123. spin_lock(&of_dma_lock);
  124. if (list_empty(&of_dma_list)) {
  125. spin_unlock(&of_dma_lock);
  126. return -ENODEV;
  127. }
  128. list_for_each_entry(ofdma, &of_dma_list, of_dma_controllers)
  129. if (ofdma->of_node == np) {
  130. if (ofdma->use_count) {
  131. spin_unlock(&of_dma_lock);
  132. return -EBUSY;
  133. }
  134. list_del(&ofdma->of_dma_controllers);
  135. spin_unlock(&of_dma_lock);
  136. kfree(ofdma);
  137. return 0;
  138. }
  139. spin_unlock(&of_dma_lock);
  140. return -ENODEV;
  141. }
  142. EXPORT_SYMBOL_GPL(of_dma_controller_free);
  143. /**
  144. * of_dma_match_channel - Check if a DMA specifier matches name
  145. * @np: device node to look for DMA channels
  146. * @name: channel name to be matched
  147. * @index: index of DMA specifier in list of DMA specifiers
  148. * @dma_spec: pointer to DMA specifier as found in the device tree
  149. *
  150. * Check if the DMA specifier pointed to by the index in a list of DMA
  151. * specifiers, matches the name provided. Returns 0 if the name matches and
  152. * a valid pointer to the DMA specifier is found. Otherwise returns -ENODEV.
  153. */
  154. static int of_dma_match_channel(struct device_node *np, const char *name,
  155. int index, struct of_phandle_args *dma_spec)
  156. {
  157. const char *s;
  158. if (of_property_read_string_index(np, "dma-names", index, &s))
  159. return -ENODEV;
  160. if (strcmp(name, s))
  161. return -ENODEV;
  162. if (of_parse_phandle_with_args(np, "dmas", "#dma-cells", index,
  163. dma_spec))
  164. return -ENODEV;
  165. return 0;
  166. }
  167. /**
  168. * of_dma_request_slave_channel - Get the DMA slave channel
  169. * @np: device node to get DMA request from
  170. * @name: name of desired channel
  171. *
  172. * Returns pointer to appropriate dma channel on success or NULL on error.
  173. */
  174. struct dma_chan *of_dma_request_slave_channel(struct device_node *np,
  175. const char *name)
  176. {
  177. struct of_phandle_args dma_spec;
  178. struct of_dma *ofdma;
  179. struct dma_chan *chan;
  180. int count, i;
  181. if (!np || !name) {
  182. pr_err("%s: not enough information provided\n", __func__);
  183. return NULL;
  184. }
  185. count = of_property_count_strings(np, "dma-names");
  186. if (count < 0) {
  187. pr_err("%s: dma-names property missing or empty\n", __func__);
  188. return NULL;
  189. }
  190. for (i = 0; i < count; i++) {
  191. if (of_dma_match_channel(np, name, i, &dma_spec))
  192. continue;
  193. ofdma = of_dma_get_controller(&dma_spec);
  194. if (!ofdma)
  195. continue;
  196. chan = ofdma->of_dma_xlate(&dma_spec, ofdma);
  197. of_dma_put_controller(ofdma);
  198. of_node_put(dma_spec.np);
  199. if (chan)
  200. return chan;
  201. }
  202. return NULL;
  203. }
  204. /**
  205. * of_dma_simple_xlate - Simple DMA engine translation function
  206. * @dma_spec: pointer to DMA specifier as found in the device tree
  207. * @of_dma: pointer to DMA controller data
  208. *
  209. * A simple translation function for devices that use a 32-bit value for the
  210. * filter_param when calling the DMA engine dma_request_channel() function.
  211. * Note that this translation function requires that #dma-cells is equal to 1
  212. * and the argument of the dma specifier is the 32-bit filter_param. Returns
  213. * pointer to appropriate dma channel on success or NULL on error.
  214. */
  215. struct dma_chan *of_dma_simple_xlate(struct of_phandle_args *dma_spec,
  216. struct of_dma *ofdma)
  217. {
  218. int count = dma_spec->args_count;
  219. struct of_dma_filter_info *info = ofdma->of_dma_data;
  220. if (!info || !info->filter_fn)
  221. return NULL;
  222. if (count != 1)
  223. return NULL;
  224. return dma_request_channel(info->dma_cap, info->filter_fn,
  225. &dma_spec->args[0]);
  226. }
  227. EXPORT_SYMBOL_GPL(of_dma_simple_xlate);