1139749Simp// SPDX-License-Identifier: GPL-2.0-only 2113584Ssimokawa/* 3103285Sikob * Device tree helpers for DMA request / controller 4103285Sikob * 5103285Sikob * Based on of_gpio.c 6103285Sikob * 7103285Sikob * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/ 8103285Sikob */ 9103285Sikob 10103285Sikob#include <linux/device.h> 11103285Sikob#include <linux/err.h> 12103285Sikob#include <linux/module.h> 13103285Sikob#include <linux/mutex.h> 14103285Sikob#include <linux/slab.h> 15103285Sikob#include <linux/of.h> 16103285Sikob#include <linux/of_dma.h> 17103285Sikob 18103285Sikob#include "dmaengine.h" 19103285Sikob 20103285Sikobstatic LIST_HEAD(of_dma_list); 21103285Sikobstatic DEFINE_MUTEX(of_dma_lock); 22103285Sikob 23103285Sikob/** 24103285Sikob * of_dma_find_controller - Get a DMA controller in DT DMA helpers list 25103285Sikob * @dma_spec: pointer to DMA specifier as found in the device tree 26103285Sikob * 27103285Sikob * Finds a DMA controller with matching device node and number for dma cells 28103285Sikob * in a list of registered DMA controllers. If a match is found a valid pointer 29103285Sikob * to the DMA data stored is retuned. A NULL pointer is returned if no match is 30103285Sikob * found. 31103285Sikob */ 32103285Sikobstatic struct of_dma *of_dma_find_controller(const struct of_phandle_args *dma_spec) 33103285Sikob{ 34103285Sikob struct of_dma *ofdma; 35103285Sikob 36103285Sikob list_for_each_entry(ofdma, &of_dma_list, of_dma_controllers) 37103285Sikob if (ofdma->of_node == dma_spec->np) 38127468Ssimokawa return ofdma; 39127468Ssimokawa 40127468Ssimokawa pr_debug("%s: can't find DMA controller %pOF\n", __func__, 41127468Ssimokawa dma_spec->np); 42103285Sikob 43103285Sikob return NULL; 44103285Sikob} 45103285Sikob 46103285Sikob/** 47103285Sikob * of_dma_router_xlate - translation function for router devices 48103285Sikob * @dma_spec: pointer to DMA specifier as found in the device tree 49113584Ssimokawa * @ofdma: pointer to DMA controller data (router information) 50170374Ssimokawa * 51170374Ssimokawa * The function creates new dma_spec to be passed to the router driver's 52113584Ssimokawa * of_dma_route_allocate() function to prepare a dma_spec which will be used 53103285Sikob * to request channel from the real DMA controller. 54103285Sikob */ 55169130Ssimokawastatic struct dma_chan *of_dma_router_xlate(struct of_phandle_args *dma_spec, 56169130Ssimokawa struct of_dma *ofdma) 57103285Sikob{ 58129585Sdfr struct dma_chan *chan; 59103285Sikob struct of_dma *ofdma_target; 60129585Sdfr struct of_phandle_args dma_spec_target; 61129585Sdfr void *route_data; 62129585Sdfr 63129585Sdfr /* translate the request for the real DMA controller */ 64103285Sikob memcpy(&dma_spec_target, dma_spec, sizeof(dma_spec_target)); 65103285Sikob route_data = ofdma->of_dma_route_allocate(&dma_spec_target, ofdma); 66103285Sikob if (IS_ERR(route_data)) 67129585Sdfr return NULL; 68103285Sikob 69106810Ssimokawa ofdma_target = of_dma_find_controller(&dma_spec_target); 70129585Sdfr if (!ofdma_target) { 71103285Sikob ofdma->dma_router->route_free(ofdma->dma_router->dev, 72103285Sikob route_data); 73103285Sikob chan = ERR_PTR(-EPROBE_DEFER); 74110193Ssimokawa goto err; 75103285Sikob } 76109645Ssimokawa 77103285Sikob chan = ofdma_target->of_dma_xlate(&dma_spec_target, ofdma_target); 78127468Ssimokawa if (IS_ERR_OR_NULL(chan)) { 79130585Sphk ofdma->dma_router->route_free(ofdma->dma_router->dev, 80103285Sikob route_data); 81103285Sikob } else { 82103285Sikob int ret = 0; 83109645Ssimokawa 84103285Sikob chan->router = ofdma->dma_router; 85103285Sikob chan->route_data = route_data; 86103285Sikob 87109645Ssimokawa if (chan->device->device_router_config) 88103285Sikob ret = chan->device->device_router_config(chan); 89103285Sikob 90103285Sikob if (ret) { 91124169Ssimokawa dma_release_channel(chan); 92124169Ssimokawa chan = ERR_PTR(ret); 93103285Sikob } 94103285Sikob } 95103285Sikob 96103285Sikoberr: 97103285Sikob /* 98103285Sikob * Need to put the node back since the ofdma->of_dma_route_allocate 99103285Sikob * has taken it for generating the new, translated dma_spec 100103285Sikob */ 101103285Sikob of_node_put(dma_spec_target.np); 102103285Sikob return chan; 103170374Ssimokawa} 104103285Sikob 105103285Sikob/** 106103285Sikob * of_dma_controller_register - Register a DMA controller to DT DMA helpers 107103285Sikob * @np: device node of DMA controller 108103285Sikob * @of_dma_xlate: translation function which converts a phandle 109129585Sdfr * arguments list into a dma_chan structure 110103285Sikob * @data: pointer to controller specific data to be used by 111103285Sikob * translation function 112103285Sikob * 113103285Sikob * Returns 0 on success or appropriate errno value on error. 114103285Sikob * 115103285Sikob * Allocated memory should be freed with appropriate of_dma_controller_free() 116103285Sikob * call. 117103285Sikob */ 118103285Sikobint of_dma_controller_register(struct device_node *np, 119129585Sdfr struct dma_chan *(*of_dma_xlate) 120169806Ssimokawa (struct of_phandle_args *, struct of_dma *), 121116978Ssimokawa void *data) 122103285Sikob{ 123103285Sikob struct of_dma *ofdma; 124103285Sikob 125103285Sikob if (!np || !of_dma_xlate) { 126103285Sikob pr_err("%s: not enough information provided\n", __func__); 127103285Sikob return -EINVAL; 128103285Sikob } 129103285Sikob 130103285Sikob ofdma = kzalloc(sizeof(*ofdma), GFP_KERNEL); 131103285Sikob if (!ofdma) 132109814Ssimokawa return -ENOMEM; 133103285Sikob 134103285Sikob ofdma->of_node = np; 135169130Ssimokawa ofdma->of_dma_xlate = of_dma_xlate; 136171457Ssimokawa ofdma->of_dma_data = data; 137103285Sikob 138110193Ssimokawa /* Now queue of_dma controller structure in list */ 139103285Sikob mutex_lock(&of_dma_lock); 140103285Sikob list_add_tail(&ofdma->of_dma_controllers, &of_dma_list); 141129585Sdfr mutex_unlock(&of_dma_lock); 142103285Sikob 143129585Sdfr return 0; 144116376Ssimokawa} 145116376SsimokawaEXPORT_SYMBOL_GPL(of_dma_controller_register); 146116376Ssimokawa 147103285Sikob/** 148103285Sikob * of_dma_controller_free - Remove a DMA controller from DT DMA helpers list 149108853Ssimokawa * @np: device node of DMA controller 150110193Ssimokawa * 151110193Ssimokawa * Memory allocated by of_dma_controller_register() is freed here. 152170374Ssimokawa */ 153129585Sdfrvoid of_dma_controller_free(struct device_node *np) 154124169Ssimokawa{ 155129585Sdfr struct of_dma *ofdma; 156130585Sphk 157124169Ssimokawa mutex_lock(&of_dma_lock); 158124169Ssimokawa 159124169Ssimokawa list_for_each_entry(ofdma, &of_dma_list, of_dma_controllers) 160124169Ssimokawa if (ofdma->of_node == np) { 161124169Ssimokawa list_del(&ofdma->of_dma_controllers); 162124169Ssimokawa kfree(ofdma); 163124169Ssimokawa break; 164129585Sdfr } 165129585Sdfr 166103285Sikob mutex_unlock(&of_dma_lock); 167113584Ssimokawa} 168170374SsimokawaEXPORT_SYMBOL_GPL(of_dma_controller_free); 169170374Ssimokawa 170170374Ssimokawa/** 171170374Ssimokawa * of_dma_router_register - Register a DMA router to DT DMA helpers as a 172103285Sikob * controller 173103285Sikob * @np: device node of DMA router 174103285Sikob * @of_dma_route_allocate: setup function for the router which need to 175170374Ssimokawa * modify the dma_spec for the DMA controller to 176170374Ssimokawa * use and to set up the requested route. 177170374Ssimokawa * @dma_router: pointer to dma_router structure to be used when 178170374Ssimokawa * the route need to be free up. 179170374Ssimokawa * 180109645Ssimokawa * Returns 0 on success or appropriate errno value on error. 181109645Ssimokawa * 182109645Ssimokawa * Allocated memory should be freed with appropriate of_dma_controller_free() 183109645Ssimokawa * call. 184109645Ssimokawa */ 185109645Ssimokawaint of_dma_router_register(struct device_node *np, 186109645Ssimokawa void *(*of_dma_route_allocate) 187109645Ssimokawa (struct of_phandle_args *, struct of_dma *), 188109645Ssimokawa struct dma_router *dma_router) 189109645Ssimokawa{ 190109645Ssimokawa struct of_dma *ofdma; 191109645Ssimokawa 192109645Ssimokawa if (!np || !of_dma_route_allocate || !dma_router) { 193109645Ssimokawa pr_err("%s: not enough information provided\n", __func__); 194124169Ssimokawa return -EINVAL; 195118293Ssimokawa } 196169130Ssimokawa 197109645Ssimokawa ofdma = kzalloc(sizeof(*ofdma), GFP_KERNEL); 198109645Ssimokawa if (!ofdma) 199109645Ssimokawa return -ENOMEM; 200113584Ssimokawa 201109645Ssimokawa ofdma->of_node = np; 202109645Ssimokawa ofdma->of_dma_xlate = of_dma_router_xlate; 203109645Ssimokawa ofdma->of_dma_route_allocate = of_dma_route_allocate; 204109645Ssimokawa ofdma->dma_router = dma_router; 205109645Ssimokawa 206109890Ssimokawa /* Now queue of_dma controller structure in list */ 207109645Ssimokawa mutex_lock(&of_dma_lock); 208109645Ssimokawa list_add_tail(&ofdma->of_dma_controllers, &of_dma_list); 209109645Ssimokawa mutex_unlock(&of_dma_lock); 210124169Ssimokawa 211109645Ssimokawa return 0; 212109645Ssimokawa} 213109645SsimokawaEXPORT_SYMBOL_GPL(of_dma_router_register); 214113584Ssimokawa 215111942Ssimokawa/** 216109645Ssimokawa * of_dma_match_channel - Check if a DMA specifier matches name 217109645Ssimokawa * @np: device node to look for DMA channels 218109645Ssimokawa * @name: channel name to be matched 219111942Ssimokawa * @index: index of DMA specifier in list of DMA specifiers 220109645Ssimokawa * @dma_spec: pointer to DMA specifier as found in the device tree 221109645Ssimokawa * 222109645Ssimokawa * Check if the DMA specifier pointed to by the index in a list of DMA 223120660Ssimokawa * specifiers, matches the name provided. Returns 0 if the name matches and 224120660Ssimokawa * a valid pointer to the DMA specifier is found. Otherwise returns -ENODEV. 225169130Ssimokawa */ 226109645Ssimokawastatic int of_dma_match_channel(struct device_node *np, const char *name, 227109645Ssimokawa int index, struct of_phandle_args *dma_spec) 228169130Ssimokawa{ 229109645Ssimokawa const char *s; 230109645Ssimokawa 231103285Sikob if (of_property_read_string_index(np, "dma-names", index, &s)) 232103285Sikob return -ENODEV; 233103285Sikob 234103285Sikob if (strcmp(name, s)) 235110577Ssimokawa return -ENODEV; 236113584Ssimokawa 237170374Ssimokawa if (of_parse_phandle_with_args(np, "dmas", "#dma-cells", index, 238170374Ssimokawa dma_spec)) 239170374Ssimokawa return -ENODEV; 240170374Ssimokawa 241170374Ssimokawa return 0; 242170374Ssimokawa} 243170374Ssimokawa 244170374Ssimokawa/** 245170374Ssimokawa * of_dma_request_slave_channel - Get the DMA slave channel 246170374Ssimokawa * @np: device node to get DMA request from 247169119Ssimokawa * @name: name of desired channel 248167632Ssimokawa * 249103285Sikob * Returns pointer to appropriate DMA channel on success or an error pointer. 250120660Ssimokawa */ 251129585Sdfrstruct dma_chan *of_dma_request_slave_channel(struct device_node *np, 252129585Sdfr const char *name) 253129585Sdfr{ 254103285Sikob struct of_phandle_args dma_spec; 255103285Sikob struct of_dma *ofdma; 256103285Sikob struct dma_chan *chan; 257169119Ssimokawa int count, i, start; 258110269Ssimokawa int ret_no_channel = -ENODEV; 259103285Sikob static atomic_t last_index; 260120660Ssimokawa 261120660Ssimokawa if (!np || !name) { 262120660Ssimokawa pr_err("%s: not enough information provided\n", __func__); 263120660Ssimokawa return ERR_PTR(-ENODEV); 264120660Ssimokawa } 265120660Ssimokawa 266129585Sdfr /* Silently fail if there is not even the "dmas" property */ 267120660Ssimokawa if (!of_property_present(np, "dmas")) 268120660Ssimokawa return ERR_PTR(-ENODEV); 269129585Sdfr 270124169Ssimokawa count = of_property_count_strings(np, "dma-names"); 271124169Ssimokawa if (count < 0) { 272124169Ssimokawa pr_err("%s: dma-names property of node '%pOF' missing or empty\n", 273124169Ssimokawa __func__, np); 274124169Ssimokawa return ERR_PTR(-ENODEV); 275124169Ssimokawa } 276124169Ssimokawa 277124169Ssimokawa /* 278124169Ssimokawa * approximate an average distribution across multiple 279124169Ssimokawa * entries with the same name 280124169Ssimokawa */ 281169130Ssimokawa start = atomic_inc_return(&last_index); 282169130Ssimokawa for (i = 0; i < count; i++) { 283169130Ssimokawa if (of_dma_match_channel(np, name, 284124169Ssimokawa (i + start) % count, 285169117Ssimokawa &dma_spec)) 286129585Sdfr continue; 287124169Ssimokawa 288124169Ssimokawa mutex_lock(&of_dma_lock); 289170374Ssimokawa ofdma = of_dma_find_controller(&dma_spec); 290170374Ssimokawa 291124169Ssimokawa if (ofdma) { 292124169Ssimokawa chan = ofdma->of_dma_xlate(&dma_spec, ofdma); 293124169Ssimokawa } else { 294129585Sdfr ret_no_channel = -EPROBE_DEFER; 295124169Ssimokawa chan = NULL; 296124169Ssimokawa } 297124169Ssimokawa 298148868Srwatson mutex_unlock(&of_dma_lock); 299170374Ssimokawa 300103285Sikob of_node_put(dma_spec.np); 301103285Sikob 302103285Sikob if (chan) 303170400Ssimokawa return chan; 304103285Sikob } 305127468Ssimokawa 306127468Ssimokawa return ERR_PTR(ret_no_channel); 307127468Ssimokawa} 308103285SikobEXPORT_SYMBOL_GPL(of_dma_request_slave_channel); 309127468Ssimokawa 310103285Sikob/** 311127468Ssimokawa * of_dma_simple_xlate - Simple DMA engine translation function 312127468Ssimokawa * @dma_spec: pointer to DMA specifier as found in the device tree 313127468Ssimokawa * @ofdma: pointer to DMA controller data 314170374Ssimokawa * 315111615Ssimokawa * A simple translation function for devices that use a 32-bit value for the 316111615Ssimokawa * filter_param when calling the DMA engine dma_request_channel() function. 317127468Ssimokawa * Note that this translation function requires that #dma-cells is equal to 1 318120660Ssimokawa * and the argument of the dma specifier is the 32-bit filter_param. Returns 319120660Ssimokawa * pointer to appropriate dma channel on success or NULL on error. 320120660Ssimokawa */ 321120660Ssimokawastruct dma_chan *of_dma_simple_xlate(struct of_phandle_args *dma_spec, 322120660Ssimokawa struct of_dma *ofdma) 323120660Ssimokawa{ 324120660Ssimokawa int count = dma_spec->args_count; 325120660Ssimokawa struct of_dma_filter_info *info = ofdma->of_dma_data; 326120660Ssimokawa 327120660Ssimokawa if (!info || !info->filter_fn) 328120660Ssimokawa return NULL; 329120660Ssimokawa 330120660Ssimokawa if (count != 1) 331120660Ssimokawa return NULL; 332120660Ssimokawa 333121780Ssimokawa return __dma_request_channel(&info->dma_cap, info->filter_fn, 334120660Ssimokawa &dma_spec->args[0], dma_spec->np); 335120660Ssimokawa} 336110195SsimokawaEXPORT_SYMBOL_GPL(of_dma_simple_xlate); 337110269Ssimokawa 338/** 339 * of_dma_xlate_by_chan_id - Translate dt property to DMA channel by channel id 340 * @dma_spec: pointer to DMA specifier as found in the device tree 341 * @ofdma: pointer to DMA controller data 342 * 343 * This function can be used as the of xlate callback for DMA driver which wants 344 * to match the channel based on the channel id. When using this xlate function 345 * the #dma-cells propety of the DMA controller dt node needs to be set to 1. 346 * The data parameter of of_dma_controller_register must be a pointer to the 347 * dma_device struct the function should match upon. 348 * 349 * Returns pointer to appropriate dma channel on success or NULL on error. 350 */ 351struct dma_chan *of_dma_xlate_by_chan_id(struct of_phandle_args *dma_spec, 352 struct of_dma *ofdma) 353{ 354 struct dma_device *dev = ofdma->of_dma_data; 355 struct dma_chan *chan, *candidate = NULL; 356 357 if (!dev || dma_spec->args_count != 1) 358 return NULL; 359 360 list_for_each_entry(chan, &dev->channels, device_node) 361 if (chan->chan_id == dma_spec->args[0]) { 362 candidate = chan; 363 break; 364 } 365 366 if (!candidate) 367 return NULL; 368 369 return dma_get_slave_channel(candidate); 370} 371EXPORT_SYMBOL_GPL(of_dma_xlate_by_chan_id); 372