dma.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. /**
  21. * of_dma_find_controller - Find a DMA controller in DT DMA helpers list
  22. * @np: device node of DMA controller
  23. */
  24. static struct of_dma *of_dma_find_controller(struct device_node *np)
  25. {
  26. struct of_dma *ofdma;
  27. if (list_empty(&of_dma_list)) {
  28. pr_err("empty DMA controller list\n");
  29. return NULL;
  30. }
  31. list_for_each_entry_rcu(ofdma, &of_dma_list, of_dma_controllers)
  32. if (ofdma->of_node == np)
  33. return ofdma;
  34. return NULL;
  35. }
  36. /**
  37. * of_dma_controller_register - Register a DMA controller to DT DMA helpers
  38. * @np: device node of DMA controller
  39. * @of_dma_xlate: translation function which converts a phandle
  40. * arguments list into a dma_chan structure
  41. * @data pointer to controller specific data to be used by
  42. * translation function
  43. *
  44. * Returns 0 on success or appropriate errno value on error.
  45. *
  46. * Allocated memory should be freed with appropriate of_dma_controller_free()
  47. * call.
  48. */
  49. int of_dma_controller_register(struct device_node *np,
  50. struct dma_chan *(*of_dma_xlate)
  51. (struct of_phandle_args *, struct of_dma *),
  52. void *data)
  53. {
  54. struct of_dma *ofdma;
  55. int nbcells;
  56. if (!np || !of_dma_xlate) {
  57. pr_err("%s: not enough information provided\n", __func__);
  58. return -EINVAL;
  59. }
  60. ofdma = kzalloc(sizeof(*ofdma), GFP_KERNEL);
  61. if (!ofdma)
  62. return -ENOMEM;
  63. nbcells = be32_to_cpup(of_get_property(np, "#dma-cells", NULL));
  64. if (!nbcells) {
  65. pr_err("%s: #dma-cells property is missing or invalid\n",
  66. __func__);
  67. return -EINVAL;
  68. }
  69. ofdma->of_node = np;
  70. ofdma->of_dma_nbcells = nbcells;
  71. ofdma->of_dma_xlate = of_dma_xlate;
  72. ofdma->of_dma_data = data;
  73. /* Now queue of_dma controller structure in list */
  74. list_add_tail_rcu(&ofdma->of_dma_controllers, &of_dma_list);
  75. return 0;
  76. }
  77. EXPORT_SYMBOL_GPL(of_dma_controller_register);
  78. /**
  79. * of_dma_controller_free - Remove a DMA controller from DT DMA helpers list
  80. * @np: device node of DMA controller
  81. *
  82. * Memory allocated by of_dma_controller_register() is freed here.
  83. */
  84. void of_dma_controller_free(struct device_node *np)
  85. {
  86. struct of_dma *ofdma;
  87. ofdma = of_dma_find_controller(np);
  88. if (ofdma) {
  89. list_del_rcu(&ofdma->of_dma_controllers);
  90. kfree(ofdma);
  91. }
  92. }
  93. EXPORT_SYMBOL_GPL(of_dma_controller_free);
  94. /**
  95. * of_dma_find_channel - Find a DMA channel by name
  96. * @np: device node to look for DMA channels
  97. * @name: name of desired channel
  98. * @dma_spec: pointer to DMA specifier as found in the device tree
  99. *
  100. * Find a DMA channel by the name. Returns 0 on success or appropriate
  101. * errno value on error.
  102. */
  103. static int of_dma_find_channel(struct device_node *np, char *name,
  104. struct of_phandle_args *dma_spec)
  105. {
  106. int count, i;
  107. const char *s;
  108. count = of_property_count_strings(np, "dma-names");
  109. if (count < 0)
  110. return count;
  111. for (i = 0; i < count; i++) {
  112. if (of_property_read_string_index(np, "dma-names", i, &s))
  113. continue;
  114. if (strcmp(name, s))
  115. continue;
  116. if (!of_parse_phandle_with_args(np, "dmas", "#dma-cells", i,
  117. dma_spec))
  118. return 0;
  119. }
  120. return -ENODEV;
  121. }
  122. /**
  123. * of_dma_request_slave_channel - Get the DMA slave channel
  124. * @np: device node to get DMA request from
  125. * @name: name of desired channel
  126. *
  127. * Returns pointer to appropriate dma channel on success or NULL on error.
  128. */
  129. struct dma_chan *of_dma_request_slave_channel(struct device_node *np,
  130. char *name)
  131. {
  132. struct of_phandle_args dma_spec;
  133. struct of_dma *ofdma;
  134. struct dma_chan *chan;
  135. int r;
  136. if (!np || !name) {
  137. pr_err("%s: not enough information provided\n", __func__);
  138. return NULL;
  139. }
  140. do {
  141. r = of_dma_find_channel(np, name, &dma_spec);
  142. if (r) {
  143. pr_err("%s: can't find DMA channel\n", np->full_name);
  144. return NULL;
  145. }
  146. ofdma = of_dma_find_controller(dma_spec.np);
  147. if (!ofdma) {
  148. pr_debug("%s: can't find DMA controller %s\n",
  149. np->full_name, dma_spec.np->full_name);
  150. continue;
  151. }
  152. if (dma_spec.args_count != ofdma->of_dma_nbcells) {
  153. pr_debug("%s: wrong #dma-cells for %s\n", np->full_name,
  154. dma_spec.np->full_name);
  155. continue;
  156. }
  157. chan = ofdma->of_dma_xlate(&dma_spec, ofdma);
  158. of_node_put(dma_spec.np);
  159. } while (!chan);
  160. return chan;
  161. }
  162. /**
  163. * of_dma_simple_xlate - Simple DMA engine translation function
  164. * @dma_spec: pointer to DMA specifier as found in the device tree
  165. * @of_dma: pointer to DMA controller data
  166. *
  167. * A simple translation function for devices that use a 32-bit value for the
  168. * filter_param when calling the DMA engine dma_request_channel() function.
  169. * Note that this translation function requires that #dma-cells is equal to 1
  170. * and the argument of the dma specifier is the 32-bit filter_param. Returns
  171. * pointer to appropriate dma channel on success or NULL on error.
  172. */
  173. struct dma_chan *of_dma_simple_xlate(struct of_phandle_args *dma_spec,
  174. struct of_dma *ofdma)
  175. {
  176. int count = dma_spec->args_count;
  177. struct of_dma_filter_info *info = ofdma->of_dma_data;
  178. if (!info || !info->filter_fn)
  179. return NULL;
  180. if (count != 1)
  181. return NULL;
  182. return dma_request_channel(info->dma_cap, info->filter_fn,
  183. &dma_spec->args[0]);
  184. }
  185. EXPORT_SYMBOL_GPL(of_dma_simple_xlate);