of-dma.c 10.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Device tree helpers for DMA request / controller
 *
 * Based on of_gpio.c
 *
 * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/device.h>
#include <linux/err.h>
#include <linux/module.h>
16
#include <linux/mutex.h>
17 18 19 20 21
#include <linux/slab.h>
#include <linux/of.h>
#include <linux/of_dma.h>

static LIST_HEAD(of_dma_list);
22
static DEFINE_MUTEX(of_dma_lock);
23 24

/**
25
 * of_dma_find_controller - Get a DMA controller in DT DMA helpers list
26 27 28
 * @dma_spec:	pointer to DMA specifier as found in the device tree
 *
 * Finds a DMA controller with matching device node and number for dma cells
29 30 31
 * in a list of registered DMA controllers. If a match is found a valid pointer
 * to the DMA data stored is retuned. A NULL pointer is returned if no match is
 * found.
32
 */
33
static struct of_dma *of_dma_find_controller(struct of_phandle_args *dma_spec)
34 35 36
{
	struct of_dma *ofdma;

37
	list_for_each_entry(ofdma, &of_dma_list, of_dma_controllers)
38
		if (ofdma->of_node == dma_spec->np)
39
			return ofdma;
40 41 42

	pr_debug("%s: can't find DMA controller %s\n", __func__,
		 dma_spec->np->full_name);
43 44 45 46

	return NULL;
}

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
/**
 * of_dma_router_xlate - translation function for router devices
 * @dma_spec:	pointer to DMA specifier as found in the device tree
 * @of_dma:	pointer to DMA controller data (router information)
 *
 * The function creates new dma_spec to be passed to the router driver's
 * of_dma_route_allocate() function to prepare a dma_spec which will be used
 * to request channel from the real DMA controller.
 */
static struct dma_chan *of_dma_router_xlate(struct of_phandle_args *dma_spec,
					    struct of_dma *ofdma)
{
	struct dma_chan		*chan;
	struct of_dma		*ofdma_target;
	struct of_phandle_args	dma_spec_target;
	void			*route_data;

	/* translate the request for the real DMA controller */
	memcpy(&dma_spec_target, dma_spec, sizeof(dma_spec_target));
	route_data = ofdma->of_dma_route_allocate(&dma_spec_target, ofdma);
	if (IS_ERR(route_data))
		return NULL;

	ofdma_target = of_dma_find_controller(&dma_spec_target);
	if (!ofdma_target)
		return NULL;

	chan = ofdma_target->of_dma_xlate(&dma_spec_target, ofdma_target);
	if (chan) {
		chan->router = ofdma->dma_router;
		chan->route_data = route_data;
	} else {
		ofdma->dma_router->route_free(ofdma->dma_router->dev,
					      route_data);
	}

	/*
	 * Need to put the node back since the ofdma->of_dma_route_allocate
	 * has taken it for generating the new, translated dma_spec
	 */
	of_node_put(dma_spec_target.np);
	return chan;
}

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
/**
 * of_dma_controller_register - Register a DMA controller to DT DMA helpers
 * @np:			device node of DMA controller
 * @of_dma_xlate:	translation function which converts a phandle
 *			arguments list into a dma_chan structure
 * @data		pointer to controller specific data to be used by
 *			translation function
 *
 * Returns 0 on success or appropriate errno value on error.
 *
 * Allocated memory should be freed with appropriate of_dma_controller_free()
 * call.
 */
int of_dma_controller_register(struct device_node *np,
				struct dma_chan *(*of_dma_xlate)
				(struct of_phandle_args *, struct of_dma *),
				void *data)
{
	struct of_dma	*ofdma;

	if (!np || !of_dma_xlate) {
		pr_err("%s: not enough information provided\n", __func__);
		return -EINVAL;
	}

	ofdma = kzalloc(sizeof(*ofdma), GFP_KERNEL);
	if (!ofdma)
		return -ENOMEM;

	ofdma->of_node = np;
	ofdma->of_dma_xlate = of_dma_xlate;
	ofdma->of_dma_data = data;

	/* Now queue of_dma controller structure in list */
125
	mutex_lock(&of_dma_lock);
126
	list_add_tail(&ofdma->of_dma_controllers, &of_dma_list);
127
	mutex_unlock(&of_dma_lock);
128 129 130 131 132 133 134 135 136 137 138

	return 0;
}
EXPORT_SYMBOL_GPL(of_dma_controller_register);

/**
 * of_dma_controller_free - Remove a DMA controller from DT DMA helpers list
 * @np:		device node of DMA controller
 *
 * Memory allocated by of_dma_controller_register() is freed here.
 */
139
void of_dma_controller_free(struct device_node *np)
140 141 142
{
	struct of_dma *ofdma;

143
	mutex_lock(&of_dma_lock);
144 145 146 147 148

	list_for_each_entry(ofdma, &of_dma_list, of_dma_controllers)
		if (ofdma->of_node == np) {
			list_del(&ofdma->of_dma_controllers);
			kfree(ofdma);
149
			break;
150 151
		}

152
	mutex_unlock(&of_dma_lock);
153 154 155
}
EXPORT_SYMBOL_GPL(of_dma_controller_free);

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
/**
 * of_dma_router_register - Register a DMA router to DT DMA helpers as a
 *			    controller
 * @np:				device node of DMA router
 * @of_dma_route_allocate:	setup function for the router which need to
 *				modify the dma_spec for the DMA controller to
 *				use and to set up the requested route.
 * @dma_router:			pointer to dma_router structure to be used when
 *				the route need to be free up.
 *
 * Returns 0 on success or appropriate errno value on error.
 *
 * Allocated memory should be freed with appropriate of_dma_controller_free()
 * call.
 */
int of_dma_router_register(struct device_node *np,
			   void *(*of_dma_route_allocate)
			   (struct of_phandle_args *, struct of_dma *),
			   struct dma_router *dma_router)
{
	struct of_dma	*ofdma;

	if (!np || !of_dma_route_allocate || !dma_router) {
		pr_err("%s: not enough information provided\n", __func__);
		return -EINVAL;
	}

	ofdma = kzalloc(sizeof(*ofdma), GFP_KERNEL);
	if (!ofdma)
		return -ENOMEM;

	ofdma->of_node = np;
	ofdma->of_dma_xlate = of_dma_router_xlate;
	ofdma->of_dma_route_allocate = of_dma_route_allocate;
	ofdma->dma_router = dma_router;

	/* Now queue of_dma controller structure in list */
	mutex_lock(&of_dma_lock);
	list_add_tail(&ofdma->of_dma_controllers, &of_dma_list);
	mutex_unlock(&of_dma_lock);

	return 0;
}
EXPORT_SYMBOL_GPL(of_dma_router_register);

201
/**
202
 * of_dma_match_channel - Check if a DMA specifier matches name
203
 * @np:		device node to look for DMA channels
204 205
 * @name:	channel name to be matched
 * @index:	index of DMA specifier in list of DMA specifiers
206 207
 * @dma_spec:	pointer to DMA specifier as found in the device tree
 *
208 209 210
 * Check if the DMA specifier pointed to by the index in a list of DMA
 * specifiers, matches the name provided. Returns 0 if the name matches and
 * a valid pointer to the DMA specifier is found. Otherwise returns -ENODEV.
211
 */
Markus Pargmann's avatar
Markus Pargmann committed
212 213
static int of_dma_match_channel(struct device_node *np, const char *name,
				int index, struct of_phandle_args *dma_spec)
214 215 216
{
	const char *s;

217 218
	if (of_property_read_string_index(np, "dma-names", index, &s))
		return -ENODEV;
219

220 221
	if (strcmp(name, s))
		return -ENODEV;
222

223 224 225
	if (of_parse_phandle_with_args(np, "dmas", "#dma-cells", index,
				       dma_spec))
		return -ENODEV;
226

227
	return 0;
228 229 230 231 232 233 234
}

/**
 * of_dma_request_slave_channel - Get the DMA slave channel
 * @np:		device node to get DMA request from
 * @name:	name of desired channel
 *
235
 * Returns pointer to appropriate DMA channel on success or an error pointer.
236 237
 */
struct dma_chan *of_dma_request_slave_channel(struct device_node *np,
Markus Pargmann's avatar
Markus Pargmann committed
238
					      const char *name)
239 240 241 242
{
	struct of_phandle_args	dma_spec;
	struct of_dma		*ofdma;
	struct dma_chan		*chan;
243
	int			count, i, start;
244
	int			ret_no_channel = -ENODEV;
245
	static atomic_t		last_index;
246 247 248

	if (!np || !name) {
		pr_err("%s: not enough information provided\n", __func__);
249
		return ERR_PTR(-ENODEV);
250 251
	}

252 253 254 255
	/* Silently fail if there is not even the "dmas" property */
	if (!of_find_property(np, "dmas", NULL))
		return ERR_PTR(-ENODEV);

256 257
	count = of_property_count_strings(np, "dma-names");
	if (count < 0) {
258 259
		pr_err("%s: dma-names property of node '%s' missing or empty\n",
			__func__, np->full_name);
260
		return ERR_PTR(-ENODEV);
261 262
	}

263 264 265 266 267
	/*
	 * approximate an average distribution across multiple
	 * entries with the same name
	 */
	start = atomic_inc_return(&last_index);
268
	for (i = 0; i < count; i++) {
269 270 271
		if (of_dma_match_channel(np, name,
					 (i + start) % count,
					 &dma_spec))
272
			continue;
273

274 275
		mutex_lock(&of_dma_lock);
		ofdma = of_dma_find_controller(&dma_spec);
276

277
		if (ofdma) {
278
			chan = ofdma->of_dma_xlate(&dma_spec, ofdma);
279 280
		} else {
			ret_no_channel = -EPROBE_DEFER;
281
			chan = NULL;
282
		}
283 284

		mutex_unlock(&of_dma_lock);
285

286 287
		of_node_put(dma_spec.np);

288 289 290
		if (chan)
			return chan;
	}
291

292
	return ERR_PTR(ret_no_channel);
293
}
294
EXPORT_SYMBOL_GPL(of_dma_request_slave_channel);
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322

/**
 * of_dma_simple_xlate - Simple DMA engine translation function
 * @dma_spec:	pointer to DMA specifier as found in the device tree
 * @of_dma:	pointer to DMA controller data
 *
 * A simple translation function for devices that use a 32-bit value for the
 * filter_param when calling the DMA engine dma_request_channel() function.
 * Note that this translation function requires that #dma-cells is equal to 1
 * and the argument of the dma specifier is the 32-bit filter_param. Returns
 * pointer to appropriate dma channel on success or NULL on error.
 */
struct dma_chan *of_dma_simple_xlate(struct of_phandle_args *dma_spec,
						struct of_dma *ofdma)
{
	int count = dma_spec->args_count;
	struct of_dma_filter_info *info = ofdma->of_dma_data;

	if (!info || !info->filter_fn)
		return NULL;

	if (count != 1)
		return NULL;

	return dma_request_channel(info->dma_cap, info->filter_fn,
			&dma_spec->args[0]);
}
EXPORT_SYMBOL_GPL(of_dma_simple_xlate);
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357

/**
 * of_dma_xlate_by_chan_id - Translate dt property to DMA channel by channel id
 * @dma_spec:	pointer to DMA specifier as found in the device tree
 * @of_dma:	pointer to DMA controller data
 *
 * This function can be used as the of xlate callback for DMA driver which wants
 * to match the channel based on the channel id. When using this xlate function
 * the #dma-cells propety of the DMA controller dt node needs to be set to 1.
 * The data parameter of of_dma_controller_register must be a pointer to the
 * dma_device struct the function should match upon.
 *
 * Returns pointer to appropriate dma channel on success or NULL on error.
 */
struct dma_chan *of_dma_xlate_by_chan_id(struct of_phandle_args *dma_spec,
					 struct of_dma *ofdma)
{
	struct dma_device *dev = ofdma->of_dma_data;
	struct dma_chan *chan, *candidate = NULL;

	if (!dev || dma_spec->args_count != 1)
		return NULL;

	list_for_each_entry(chan, &dev->channels, device_node)
		if (chan->chan_id == dma_spec->args[0]) {
			candidate = chan;
			break;
		}

	if (!candidate)
		return NULL;

	return dma_get_slave_channel(candidate);
}
EXPORT_SYMBOL_GPL(of_dma_xlate_by_chan_id);