of-dma.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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/mutex.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_MUTEX(of_dma_lock);
  21. /**
  22. * of_dma_find_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 a valid pointer
  27. * to the DMA data stored is retuned. A NULL pointer is returned if no match is
  28. * found.
  29. */
  30. static struct of_dma *of_dma_find_controller(struct of_phandle_args *dma_spec)
  31. {
  32. struct of_dma *ofdma;
  33. list_for_each_entry(ofdma, &of_dma_list, of_dma_controllers)
  34. if (ofdma->of_node == dma_spec->np)
  35. return ofdma;
  36. pr_debug("%s: can't find DMA controller %s\n", __func__,
  37. dma_spec->np->full_name);
  38. return NULL;
  39. }
  40. /**
  41. * of_dma_router_xlate - translation function for router devices
  42. * @dma_spec: pointer to DMA specifier as found in the device tree
  43. * @of_dma: pointer to DMA controller data (router information)
  44. *
  45. * The function creates new dma_spec to be passed to the router driver's
  46. * of_dma_route_allocate() function to prepare a dma_spec which will be used
  47. * to request channel from the real DMA controller.
  48. */
  49. static struct dma_chan *of_dma_router_xlate(struct of_phandle_args *dma_spec,
  50. struct of_dma *ofdma)
  51. {
  52. struct dma_chan *chan;
  53. struct of_dma *ofdma_target;
  54. struct of_phandle_args dma_spec_target;
  55. void *route_data;
  56. /* translate the request for the real DMA controller */
  57. memcpy(&dma_spec_target, dma_spec, sizeof(dma_spec_target));
  58. route_data = ofdma->of_dma_route_allocate(&dma_spec_target, ofdma);
  59. if (IS_ERR(route_data))
  60. return NULL;
  61. ofdma_target = of_dma_find_controller(&dma_spec_target);
  62. if (!ofdma_target)
  63. return NULL;
  64. chan = ofdma_target->of_dma_xlate(&dma_spec_target, ofdma_target);
  65. if (chan) {
  66. chan->router = ofdma->dma_router;
  67. chan->route_data = route_data;
  68. } else {
  69. ofdma->dma_router->route_free(ofdma->dma_router->dev,
  70. route_data);
  71. }
  72. /*
  73. * Need to put the node back since the ofdma->of_dma_route_allocate
  74. * has taken it for generating the new, translated dma_spec
  75. */
  76. of_node_put(dma_spec_target.np);
  77. return chan;
  78. }
  79. /**
  80. * of_dma_controller_register - Register a DMA controller to DT DMA helpers
  81. * @np: device node of DMA controller
  82. * @of_dma_xlate: translation function which converts a phandle
  83. * arguments list into a dma_chan structure
  84. * @data pointer to controller specific data to be used by
  85. * translation function
  86. *
  87. * Returns 0 on success or appropriate errno value on error.
  88. *
  89. * Allocated memory should be freed with appropriate of_dma_controller_free()
  90. * call.
  91. */
  92. int of_dma_controller_register(struct device_node *np,
  93. struct dma_chan *(*of_dma_xlate)
  94. (struct of_phandle_args *, struct of_dma *),
  95. void *data)
  96. {
  97. struct of_dma *ofdma;
  98. if (!np || !of_dma_xlate) {
  99. pr_err("%s: not enough information provided\n", __func__);
  100. return -EINVAL;
  101. }
  102. ofdma = kzalloc(sizeof(*ofdma), GFP_KERNEL);
  103. if (!ofdma)
  104. return -ENOMEM;
  105. ofdma->of_node = np;
  106. ofdma->of_dma_xlate = of_dma_xlate;
  107. ofdma->of_dma_data = data;
  108. /* Now queue of_dma controller structure in list */
  109. mutex_lock(&of_dma_lock);
  110. list_add_tail(&ofdma->of_dma_controllers, &of_dma_list);
  111. mutex_unlock(&of_dma_lock);
  112. return 0;
  113. }
  114. EXPORT_SYMBOL_GPL(of_dma_controller_register);
  115. /**
  116. * of_dma_controller_free - Remove a DMA controller from DT DMA helpers list
  117. * @np: device node of DMA controller
  118. *
  119. * Memory allocated by of_dma_controller_register() is freed here.
  120. */
  121. void of_dma_controller_free(struct device_node *np)
  122. {
  123. struct of_dma *ofdma;
  124. mutex_lock(&of_dma_lock);
  125. list_for_each_entry(ofdma, &of_dma_list, of_dma_controllers)
  126. if (ofdma->of_node == np) {
  127. list_del(&ofdma->of_dma_controllers);
  128. kfree(ofdma);
  129. break;
  130. }
  131. mutex_unlock(&of_dma_lock);
  132. }
  133. EXPORT_SYMBOL_GPL(of_dma_controller_free);
  134. /**
  135. * of_dma_router_register - Register a DMA router to DT DMA helpers as a
  136. * controller
  137. * @np: device node of DMA router
  138. * @of_dma_route_allocate: setup function for the router which need to
  139. * modify the dma_spec for the DMA controller to
  140. * use and to set up the requested route.
  141. * @dma_router: pointer to dma_router structure to be used when
  142. * the route need to be free up.
  143. *
  144. * Returns 0 on success or appropriate errno value on error.
  145. *
  146. * Allocated memory should be freed with appropriate of_dma_controller_free()
  147. * call.
  148. */
  149. int of_dma_router_register(struct device_node *np,
  150. void *(*of_dma_route_allocate)
  151. (struct of_phandle_args *, struct of_dma *),
  152. struct dma_router *dma_router)
  153. {
  154. struct of_dma *ofdma;
  155. if (!np || !of_dma_route_allocate || !dma_router) {
  156. pr_err("%s: not enough information provided\n", __func__);
  157. return -EINVAL;
  158. }
  159. ofdma = kzalloc(sizeof(*ofdma), GFP_KERNEL);
  160. if (!ofdma)
  161. return -ENOMEM;
  162. ofdma->of_node = np;
  163. ofdma->of_dma_xlate = of_dma_router_xlate;
  164. ofdma->of_dma_route_allocate = of_dma_route_allocate;
  165. ofdma->dma_router = dma_router;
  166. /* Now queue of_dma controller structure in list */
  167. mutex_lock(&of_dma_lock);
  168. list_add_tail(&ofdma->of_dma_controllers, &of_dma_list);
  169. mutex_unlock(&of_dma_lock);
  170. return 0;
  171. }
  172. EXPORT_SYMBOL_GPL(of_dma_router_register);
  173. /**
  174. * of_dma_match_channel - Check if a DMA specifier matches name
  175. * @np: device node to look for DMA channels
  176. * @name: channel name to be matched
  177. * @index: index of DMA specifier in list of DMA specifiers
  178. * @dma_spec: pointer to DMA specifier as found in the device tree
  179. *
  180. * Check if the DMA specifier pointed to by the index in a list of DMA
  181. * specifiers, matches the name provided. Returns 0 if the name matches and
  182. * a valid pointer to the DMA specifier is found. Otherwise returns -ENODEV.
  183. */
  184. static int of_dma_match_channel(struct device_node *np, const char *name,
  185. int index, struct of_phandle_args *dma_spec)
  186. {
  187. const char *s;
  188. if (of_property_read_string_index(np, "dma-names", index, &s))
  189. return -ENODEV;
  190. if (strcmp(name, s))
  191. return -ENODEV;
  192. if (of_parse_phandle_with_args(np, "dmas", "#dma-cells", index,
  193. dma_spec))
  194. return -ENODEV;
  195. return 0;
  196. }
  197. /**
  198. * of_dma_request_slave_channel - Get the DMA slave channel
  199. * @np: device node to get DMA request from
  200. * @name: name of desired channel
  201. *
  202. * Returns pointer to appropriate DMA channel on success or an error pointer.
  203. */
  204. struct dma_chan *of_dma_request_slave_channel(struct device_node *np,
  205. const char *name)
  206. {
  207. struct of_phandle_args dma_spec;
  208. struct of_dma *ofdma;
  209. struct dma_chan *chan;
  210. int count, i;
  211. int ret_no_channel = -ENODEV;
  212. if (!np || !name) {
  213. pr_err("%s: not enough information provided\n", __func__);
  214. return ERR_PTR(-ENODEV);
  215. }
  216. /* Silently fail if there is not even the "dmas" property */
  217. if (!of_find_property(np, "dmas", NULL))
  218. return ERR_PTR(-ENODEV);
  219. count = of_property_count_strings(np, "dma-names");
  220. if (count < 0) {
  221. pr_err("%s: dma-names property of node '%s' missing or empty\n",
  222. __func__, np->full_name);
  223. return ERR_PTR(-ENODEV);
  224. }
  225. for (i = 0; i < count; i++) {
  226. if (of_dma_match_channel(np, name, i, &dma_spec))
  227. continue;
  228. mutex_lock(&of_dma_lock);
  229. ofdma = of_dma_find_controller(&dma_spec);
  230. if (ofdma) {
  231. chan = ofdma->of_dma_xlate(&dma_spec, ofdma);
  232. } else {
  233. ret_no_channel = -EPROBE_DEFER;
  234. chan = NULL;
  235. }
  236. mutex_unlock(&of_dma_lock);
  237. of_node_put(dma_spec.np);
  238. if (chan)
  239. return chan;
  240. }
  241. return ERR_PTR(ret_no_channel);
  242. }
  243. EXPORT_SYMBOL_GPL(of_dma_request_slave_channel);
  244. /**
  245. * of_dma_simple_xlate - Simple DMA engine translation function
  246. * @dma_spec: pointer to DMA specifier as found in the device tree
  247. * @of_dma: pointer to DMA controller data
  248. *
  249. * A simple translation function for devices that use a 32-bit value for the
  250. * filter_param when calling the DMA engine dma_request_channel() function.
  251. * Note that this translation function requires that #dma-cells is equal to 1
  252. * and the argument of the dma specifier is the 32-bit filter_param. Returns
  253. * pointer to appropriate dma channel on success or NULL on error.
  254. */
  255. struct dma_chan *of_dma_simple_xlate(struct of_phandle_args *dma_spec,
  256. struct of_dma *ofdma)
  257. {
  258. int count = dma_spec->args_count;
  259. struct of_dma_filter_info *info = ofdma->of_dma_data;
  260. if (!info || !info->filter_fn)
  261. return NULL;
  262. if (count != 1)
  263. return NULL;
  264. return dma_request_channel(info->dma_cap, info->filter_fn,
  265. &dma_spec->args[0]);
  266. }
  267. EXPORT_SYMBOL_GPL(of_dma_simple_xlate);
  268. /**
  269. * of_dma_xlate_by_chan_id - Translate dt property to DMA channel by channel id
  270. * @dma_spec: pointer to DMA specifier as found in the device tree
  271. * @of_dma: pointer to DMA controller data
  272. *
  273. * This function can be used as the of xlate callback for DMA driver which wants
  274. * to match the channel based on the channel id. When using this xlate function
  275. * the #dma-cells propety of the DMA controller dt node needs to be set to 1.
  276. * The data parameter of of_dma_controller_register must be a pointer to the
  277. * dma_device struct the function should match upon.
  278. *
  279. * Returns pointer to appropriate dma channel on success or NULL on error.
  280. */
  281. struct dma_chan *of_dma_xlate_by_chan_id(struct of_phandle_args *dma_spec,
  282. struct of_dma *ofdma)
  283. {
  284. struct dma_device *dev = ofdma->of_dma_data;
  285. struct dma_chan *chan, *candidate = NULL;
  286. if (!dev || dma_spec->args_count != 1)
  287. return NULL;
  288. list_for_each_entry(chan, &dev->channels, device_node)
  289. if (chan->chan_id == dma_spec->args[0]) {
  290. candidate = chan;
  291. break;
  292. }
  293. if (!candidate)
  294. return NULL;
  295. return dma_get_slave_channel(candidate);
  296. }
  297. EXPORT_SYMBOL_GPL(of_dma_xlate_by_chan_id);