of-dma.c 7.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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>
#include <linux/rculist.h>
#include <linux/slab.h>
#include <linux/of.h>
#include <linux/of_dma.h>

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

/**
25 26 27 28 29 30 31
 * of_dma_get_controller - Get a DMA controller in DT DMA helpers list
 * @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
 * in a list of registered DMA controllers. If a match is found the use_count
 * variable is increased and 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_get_controller(struct of_phandle_args *dma_spec)
34 35 36
{
	struct of_dma *ofdma;

37 38
	spin_lock(&of_dma_lock);

39
	if (list_empty(&of_dma_list)) {
40
		spin_unlock(&of_dma_lock);
41 42 43
		return NULL;
	}

44 45 46 47 48
	list_for_each_entry(ofdma, &of_dma_list, of_dma_controllers)
		if ((ofdma->of_node == dma_spec->np) &&
		    (ofdma->of_dma_nbcells == dma_spec->args_count)) {
			ofdma->use_count++;
			spin_unlock(&of_dma_lock);
49
			return ofdma;
50 51 52 53 54 55
		}

	spin_unlock(&of_dma_lock);

	pr_debug("%s: can't find DMA controller %s\n", __func__,
		 dma_spec->np->full_name);
56 57 58 59

	return NULL;
}

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
/**
 * of_dma_put_controller - Decrement use count for a registered DMA controller
 * @of_dma:	pointer to DMA controller data
 *
 * Decrements the use_count variable in the DMA data structure. This function
 * should be called only when a valid pointer is returned from
 * of_dma_get_controller() and no further accesses to data referenced by that
 * pointer are needed.
 */
static void of_dma_put_controller(struct of_dma *ofdma)
{
	spin_lock(&of_dma_lock);
	ofdma->use_count--;
	spin_unlock(&of_dma_lock);
}

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
/**
 * 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;
	int		nbcells;

	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;

	nbcells = be32_to_cpup(of_get_property(np, "#dma-cells", NULL));
	if (!nbcells) {
		pr_err("%s: #dma-cells property is missing or invalid\n",
		       __func__);
C
Cong Ding 已提交
110
		kfree(ofdma);
111 112 113 114 115 116 117
		return -EINVAL;
	}

	ofdma->of_node = np;
	ofdma->of_dma_nbcells = nbcells;
	ofdma->of_dma_xlate = of_dma_xlate;
	ofdma->of_dma_data = data;
118
	ofdma->use_count = 0;
119 120

	/* Now queue of_dma controller structure in list */
121
	spin_lock(&of_dma_lock);
122
	list_add_tail(&ofdma->of_dma_controllers, &of_dma_list);
123
	spin_unlock(&of_dma_lock);
124 125 126 127 128 129 130 131 132 133 134

	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.
 */
135
int of_dma_controller_free(struct device_node *np)
136 137 138
{
	struct of_dma *ofdma;

139 140 141 142 143
	spin_lock(&of_dma_lock);

	if (list_empty(&of_dma_list)) {
		spin_unlock(&of_dma_lock);
		return -ENODEV;
144
	}
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

	list_for_each_entry(ofdma, &of_dma_list, of_dma_controllers)
		if (ofdma->of_node == np) {
			if (ofdma->use_count) {
				spin_unlock(&of_dma_lock);
				return -EBUSY;
			}

			list_del(&ofdma->of_dma_controllers);
			spin_unlock(&of_dma_lock);
			kfree(ofdma);
			return 0;
		}

	spin_unlock(&of_dma_lock);
	return -ENODEV;
161 162 163 164
}
EXPORT_SYMBOL_GPL(of_dma_controller_free);

/**
165
 * of_dma_match_channel - Check if a DMA specifier matches name
166
 * @np:		device node to look for DMA channels
167 168
 * @name:	channel name to be matched
 * @index:	index of DMA specifier in list of DMA specifiers
169 170
 * @dma_spec:	pointer to DMA specifier as found in the device tree
 *
171 172 173
 * 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.
174
 */
M
Markus Pargmann 已提交
175 176
static int of_dma_match_channel(struct device_node *np, const char *name,
				int index, struct of_phandle_args *dma_spec)
177 178 179
{
	const char *s;

180 181
	if (of_property_read_string_index(np, "dma-names", index, &s))
		return -ENODEV;
182

183 184
	if (strcmp(name, s))
		return -ENODEV;
185

186 187 188
	if (of_parse_phandle_with_args(np, "dmas", "#dma-cells", index,
				       dma_spec))
		return -ENODEV;
189

190
	return 0;
191 192 193 194 195 196 197 198 199 200
}

/**
 * of_dma_request_slave_channel - Get the DMA slave channel
 * @np:		device node to get DMA request from
 * @name:	name of desired channel
 *
 * Returns pointer to appropriate dma channel on success or NULL on error.
 */
struct dma_chan *of_dma_request_slave_channel(struct device_node *np,
M
Markus Pargmann 已提交
201
					      const char *name)
202 203 204 205
{
	struct of_phandle_args	dma_spec;
	struct of_dma		*ofdma;
	struct dma_chan		*chan;
206
	int			count, i;
207 208 209 210 211 212

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

213 214 215 216 217 218 219 220 221
	count = of_property_count_strings(np, "dma-names");
	if (count < 0) {
		pr_err("%s: dma-names property missing or empty\n", __func__);
		return NULL;
	}

	for (i = 0; i < count; i++) {
		if (of_dma_match_channel(np, name, i, &dma_spec))
			continue;
222

223
		ofdma = of_dma_get_controller(&dma_spec);
224

225
		if (!ofdma)
226 227 228 229
			continue;

		chan = ofdma->of_dma_xlate(&dma_spec, ofdma);

230 231
		of_dma_put_controller(ofdma);

232 233
		of_node_put(dma_spec.np);

234 235 236
		if (chan)
			return chan;
	}
237

238
	return NULL;
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
}

/**
 * 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);