dma.c 5.7 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_match_channel - Check if a DMA specifier matches name
  96. * @np: device node to look for DMA channels
  97. * @name: channel name to be matched
  98. * @index: index of DMA specifier in list of DMA specifiers
  99. * @dma_spec: pointer to DMA specifier as found in the device tree
  100. *
  101. * Check if the DMA specifier pointed to by the index in a list of DMA
  102. * specifiers, matches the name provided. Returns 0 if the name matches and
  103. * a valid pointer to the DMA specifier is found. Otherwise returns -ENODEV.
  104. */
  105. static int of_dma_match_channel(struct device_node *np, char *name, int index,
  106. struct of_phandle_args *dma_spec)
  107. {
  108. const char *s;
  109. if (of_property_read_string_index(np, "dma-names", index, &s))
  110. return -ENODEV;
  111. if (strcmp(name, s))
  112. return -ENODEV;
  113. if (of_parse_phandle_with_args(np, "dmas", "#dma-cells", index,
  114. dma_spec))
  115. return -ENODEV;
  116. return 0;
  117. }
  118. /**
  119. * of_dma_request_slave_channel - Get the DMA slave channel
  120. * @np: device node to get DMA request from
  121. * @name: name of desired channel
  122. *
  123. * Returns pointer to appropriate dma channel on success or NULL on error.
  124. */
  125. struct dma_chan *of_dma_request_slave_channel(struct device_node *np,
  126. char *name)
  127. {
  128. struct of_phandle_args dma_spec;
  129. struct of_dma *ofdma;
  130. struct dma_chan *chan;
  131. int count, i;
  132. if (!np || !name) {
  133. pr_err("%s: not enough information provided\n", __func__);
  134. return NULL;
  135. }
  136. count = of_property_count_strings(np, "dma-names");
  137. if (count < 0) {
  138. pr_err("%s: dma-names property missing or empty\n", __func__);
  139. return NULL;
  140. }
  141. for (i = 0; i < count; i++) {
  142. if (of_dma_match_channel(np, name, i, &dma_spec))
  143. continue;
  144. ofdma = of_dma_find_controller(dma_spec.np);
  145. if (!ofdma) {
  146. pr_debug("%s: can't find DMA controller %s\n",
  147. np->full_name, dma_spec.np->full_name);
  148. continue;
  149. }
  150. if (dma_spec.args_count != ofdma->of_dma_nbcells) {
  151. pr_debug("%s: wrong #dma-cells for %s\n", np->full_name,
  152. dma_spec.np->full_name);
  153. continue;
  154. }
  155. chan = ofdma->of_dma_xlate(&dma_spec, ofdma);
  156. of_node_put(dma_spec.np);
  157. if (chan)
  158. return chan;
  159. }
  160. return NULL;
  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);