sh: Move IRQ multi definition of DMAC to defconfig
[linux-2.6/mini2440.git] / arch / sh / drivers / dma / dma-api.c
blob727126e907e33c01cdf616529b3c8e0221794398
1 /*
2 * arch/sh/drivers/dma/dma-api.c
4 * SuperH-specific DMA management API
6 * Copyright (C) 2003, 2004, 2005 Paul Mundt
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file "COPYING" in the main directory of this archive
10 * for more details.
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/spinlock.h>
15 #include <linux/proc_fs.h>
16 #include <linux/list.h>
17 #include <linux/platform_device.h>
18 #include <linux/mm.h>
19 #include <linux/sched.h>
20 #include <asm/dma.h>
22 DEFINE_SPINLOCK(dma_spin_lock);
23 static LIST_HEAD(registered_dmac_list);
25 struct dma_info *get_dma_info(unsigned int chan)
27 struct dma_info *info;
30 * Look for each DMAC's range to determine who the owner of
31 * the channel is.
33 list_for_each_entry(info, &registered_dmac_list, list) {
34 if ((chan < info->first_vchannel_nr) ||
35 (chan >= info->first_vchannel_nr + info->nr_channels))
36 continue;
38 return info;
41 return NULL;
43 EXPORT_SYMBOL(get_dma_info);
45 struct dma_info *get_dma_info_by_name(const char *dmac_name)
47 struct dma_info *info;
49 list_for_each_entry(info, &registered_dmac_list, list) {
50 if (dmac_name && (strcmp(dmac_name, info->name) != 0))
51 continue;
52 else
53 return info;
56 return NULL;
58 EXPORT_SYMBOL(get_dma_info_by_name);
60 static unsigned int get_nr_channels(void)
62 struct dma_info *info;
63 unsigned int nr = 0;
65 if (unlikely(list_empty(&registered_dmac_list)))
66 return nr;
68 list_for_each_entry(info, &registered_dmac_list, list)
69 nr += info->nr_channels;
71 return nr;
74 struct dma_channel *get_dma_channel(unsigned int chan)
76 struct dma_info *info = get_dma_info(chan);
77 struct dma_channel *channel;
78 int i;
80 if (unlikely(!info))
81 return ERR_PTR(-EINVAL);
83 for (i = 0; i < info->nr_channels; i++) {
84 channel = &info->channels[i];
85 if (channel->vchan == chan)
86 return channel;
89 return NULL;
91 EXPORT_SYMBOL(get_dma_channel);
93 int get_dma_residue(unsigned int chan)
95 struct dma_info *info = get_dma_info(chan);
96 struct dma_channel *channel = get_dma_channel(chan);
98 if (info->ops->get_residue)
99 return info->ops->get_residue(channel);
101 return 0;
103 EXPORT_SYMBOL(get_dma_residue);
105 static int search_cap(const char **haystack, const char *needle)
107 const char **p;
109 for (p = haystack; *p; p++)
110 if (strcmp(*p, needle) == 0)
111 return 1;
113 return 0;
117 * request_dma_bycap - Allocate a DMA channel based on its capabilities
118 * @dmac: List of DMA controllers to search
119 * @caps: List of capabilities
121 * Search all channels of all DMA controllers to find a channel which
122 * matches the requested capabilities. The result is the channel
123 * number if a match is found, or %-ENODEV if no match is found.
125 * Note that not all DMA controllers export capabilities, in which
126 * case they can never be allocated using this API, and so
127 * request_dma() must be used specifying the channel number.
129 int request_dma_bycap(const char **dmac, const char **caps, const char *dev_id)
131 unsigned int found = 0;
132 struct dma_info *info;
133 const char **p;
134 int i;
136 BUG_ON(!dmac || !caps);
138 list_for_each_entry(info, &registered_dmac_list, list)
139 if (strcmp(*dmac, info->name) == 0) {
140 found = 1;
141 break;
144 if (!found)
145 return -ENODEV;
147 for (i = 0; i < info->nr_channels; i++) {
148 struct dma_channel *channel = &info->channels[i];
150 if (unlikely(!channel->caps))
151 continue;
153 for (p = caps; *p; p++) {
154 if (!search_cap(channel->caps, *p))
155 break;
156 if (request_dma(channel->chan, dev_id) == 0)
157 return channel->chan;
161 return -EINVAL;
163 EXPORT_SYMBOL(request_dma_bycap);
165 int dmac_search_free_channel(const char *dev_id)
167 struct dma_channel *channel = { 0 };
168 struct dma_info *info = get_dma_info(0);
169 int i;
171 for (i = 0; i < info->nr_channels; i++) {
172 channel = &info->channels[i];
173 if (unlikely(!channel))
174 return -ENODEV;
176 if (atomic_read(&channel->busy) == 0)
177 break;
180 if (info->ops->request) {
181 int result = info->ops->request(channel);
182 if (result)
183 return result;
185 atomic_set(&channel->busy, 1);
186 return channel->chan;
189 return -ENOSYS;
192 int request_dma(unsigned int chan, const char *dev_id)
194 struct dma_channel *channel = { 0 };
195 struct dma_info *info = get_dma_info(chan);
196 int result;
198 channel = get_dma_channel(chan);
199 if (atomic_xchg(&channel->busy, 1))
200 return -EBUSY;
202 strlcpy(channel->dev_id, dev_id, sizeof(channel->dev_id));
204 if (info->ops->request) {
205 result = info->ops->request(channel);
206 if (result)
207 atomic_set(&channel->busy, 0);
209 return result;
212 return 0;
214 EXPORT_SYMBOL(request_dma);
216 void free_dma(unsigned int chan)
218 struct dma_info *info = get_dma_info(chan);
219 struct dma_channel *channel = get_dma_channel(chan);
221 if (info->ops->free)
222 info->ops->free(channel);
224 atomic_set(&channel->busy, 0);
226 EXPORT_SYMBOL(free_dma);
228 void dma_wait_for_completion(unsigned int chan)
230 struct dma_info *info = get_dma_info(chan);
231 struct dma_channel *channel = get_dma_channel(chan);
233 if (channel->flags & DMA_TEI_CAPABLE) {
234 wait_event(channel->wait_queue,
235 (info->ops->get_residue(channel) == 0));
236 return;
239 while (info->ops->get_residue(channel))
240 cpu_relax();
242 EXPORT_SYMBOL(dma_wait_for_completion);
244 int register_chan_caps(const char *dmac, struct dma_chan_caps *caps)
246 struct dma_info *info;
247 unsigned int found = 0;
248 int i;
250 list_for_each_entry(info, &registered_dmac_list, list)
251 if (strcmp(dmac, info->name) == 0) {
252 found = 1;
253 break;
256 if (unlikely(!found))
257 return -ENODEV;
259 for (i = 0; i < info->nr_channels; i++, caps++) {
260 struct dma_channel *channel;
262 if ((info->first_channel_nr + i) != caps->ch_num)
263 return -EINVAL;
265 channel = &info->channels[i];
266 channel->caps = caps->caplist;
269 return 0;
271 EXPORT_SYMBOL(register_chan_caps);
273 void dma_configure_channel(unsigned int chan, unsigned long flags)
275 struct dma_info *info = get_dma_info(chan);
276 struct dma_channel *channel = get_dma_channel(chan);
278 if (info->ops->configure)
279 info->ops->configure(channel, flags);
281 EXPORT_SYMBOL(dma_configure_channel);
283 int dma_xfer(unsigned int chan, unsigned long from,
284 unsigned long to, size_t size, unsigned int mode)
286 struct dma_info *info = get_dma_info(chan);
287 struct dma_channel *channel = get_dma_channel(chan);
289 channel->sar = from;
290 channel->dar = to;
291 channel->count = size;
292 channel->mode = mode;
294 return info->ops->xfer(channel);
296 EXPORT_SYMBOL(dma_xfer);
298 int dma_extend(unsigned int chan, unsigned long op, void *param)
300 struct dma_info *info = get_dma_info(chan);
301 struct dma_channel *channel = get_dma_channel(chan);
303 if (info->ops->extend)
304 return info->ops->extend(channel, op, param);
306 return -ENOSYS;
308 EXPORT_SYMBOL(dma_extend);
310 static int dma_read_proc(char *buf, char **start, off_t off,
311 int len, int *eof, void *data)
313 struct dma_info *info;
314 char *p = buf;
316 if (list_empty(&registered_dmac_list))
317 return 0;
320 * Iterate over each registered DMAC
322 list_for_each_entry(info, &registered_dmac_list, list) {
323 int i;
326 * Iterate over each channel
328 for (i = 0; i < info->nr_channels; i++) {
329 struct dma_channel *channel = info->channels + i;
331 if (!(channel->flags & DMA_CONFIGURED))
332 continue;
334 p += sprintf(p, "%2d: %14s %s\n", i,
335 info->name, channel->dev_id);
339 return p - buf;
342 int register_dmac(struct dma_info *info)
344 unsigned int total_channels, i;
346 INIT_LIST_HEAD(&info->list);
348 printk(KERN_INFO "DMA: Registering %s handler (%d channel%s).\n",
349 info->name, info->nr_channels, info->nr_channels > 1 ? "s" : "");
351 BUG_ON((info->flags & DMAC_CHANNELS_CONFIGURED) && !info->channels);
353 info->pdev = platform_device_register_simple(info->name, -1,
354 NULL, 0);
355 if (IS_ERR(info->pdev))
356 return PTR_ERR(info->pdev);
359 * Don't touch pre-configured channels
361 if (!(info->flags & DMAC_CHANNELS_CONFIGURED)) {
362 unsigned int size;
364 size = sizeof(struct dma_channel) * info->nr_channels;
366 info->channels = kzalloc(size, GFP_KERNEL);
367 if (!info->channels)
368 return -ENOMEM;
371 total_channels = get_nr_channels();
372 info->first_vchannel_nr = total_channels;
373 for (i = 0; i < info->nr_channels; i++) {
374 struct dma_channel *chan = &info->channels[i];
376 atomic_set(&chan->busy, 0);
378 chan->chan = info->first_channel_nr + i;
379 chan->vchan = info->first_channel_nr + i + total_channels;
381 memcpy(chan->dev_id, "Unused", 7);
383 if (info->flags & DMAC_CHANNELS_TEI_CAPABLE)
384 chan->flags |= DMA_TEI_CAPABLE;
386 init_waitqueue_head(&chan->wait_queue);
387 dma_create_sysfs_files(chan, info);
390 list_add(&info->list, &registered_dmac_list);
392 return 0;
394 EXPORT_SYMBOL(register_dmac);
396 void unregister_dmac(struct dma_info *info)
398 unsigned int i;
400 for (i = 0; i < info->nr_channels; i++)
401 dma_remove_sysfs_files(info->channels + i, info);
403 if (!(info->flags & DMAC_CHANNELS_CONFIGURED))
404 kfree(info->channels);
406 list_del(&info->list);
407 platform_device_unregister(info->pdev);
409 EXPORT_SYMBOL(unregister_dmac);
411 static int __init dma_api_init(void)
413 printk(KERN_NOTICE "DMA: Registering DMA API.\n");
414 create_proc_read_entry("dma", 0, 0, dma_read_proc, 0);
415 return 0;
417 subsys_initcall(dma_api_init);
419 MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
420 MODULE_DESCRIPTION("DMA API for SuperH");
421 MODULE_LICENSE("GPL");