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