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
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>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
23 DEFINE_SPINLOCK(dma_spin_lock);
24 static LIST_HEAD(registered_dmac_list);
26 struct dma_info *get_dma_info(unsigned int chan)
28 struct dma_info *info;
31 * Look for each DMAC's range to determine who the owner of
34 list_for_each_entry(info, ®istered_dmac_list, list) {
35 if ((chan < info->first_vchannel_nr) ||
36 (chan >= info->first_vchannel_nr + info->nr_channels))
44 EXPORT_SYMBOL(get_dma_info);
46 struct dma_info *get_dma_info_by_name(const char *dmac_name)
48 struct dma_info *info;
50 list_for_each_entry(info, ®istered_dmac_list, list) {
51 if (dmac_name && (strcmp(dmac_name, info->name) != 0))
59 EXPORT_SYMBOL(get_dma_info_by_name);
61 static unsigned int get_nr_channels(void)
63 struct dma_info *info;
66 if (unlikely(list_empty(®istered_dmac_list)))
69 list_for_each_entry(info, ®istered_dmac_list, list)
70 nr += info->nr_channels;
75 struct dma_channel *get_dma_channel(unsigned int chan)
77 struct dma_info *info = get_dma_info(chan);
78 struct dma_channel *channel;
82 return ERR_PTR(-EINVAL);
84 for (i = 0; i < info->nr_channels; i++) {
85 channel = &info->channels[i];
86 if (channel->vchan == chan)
92 EXPORT_SYMBOL(get_dma_channel);
94 int get_dma_residue(unsigned int chan)
96 struct dma_info *info = get_dma_info(chan);
97 struct dma_channel *channel = get_dma_channel(chan);
99 if (info->ops->get_residue)
100 return info->ops->get_residue(channel);
104 EXPORT_SYMBOL(get_dma_residue);
106 static int search_cap(const char **haystack, const char *needle)
110 for (p = haystack; *p; p++)
111 if (strcmp(*p, needle) == 0)
118 * request_dma_bycap - Allocate a DMA channel based on its capabilities
119 * @dmac: List of DMA controllers to search
120 * @caps: List of capabilities
122 * Search all channels of all DMA controllers to find a channel which
123 * matches the requested capabilities. The result is the channel
124 * number if a match is found, or %-ENODEV if no match is found.
126 * Note that not all DMA controllers export capabilities, in which
127 * case they can never be allocated using this API, and so
128 * request_dma() must be used specifying the channel number.
130 int request_dma_bycap(const char **dmac, const char **caps, const char *dev_id)
132 unsigned int found = 0;
133 struct dma_info *info;
137 BUG_ON(!dmac || !caps);
139 list_for_each_entry(info, ®istered_dmac_list, list)
140 if (strcmp(*dmac, info->name) == 0) {
148 for (i = 0; i < info->nr_channels; i++) {
149 struct dma_channel *channel = &info->channels[i];
151 if (unlikely(!channel->caps))
154 for (p = caps; *p; p++) {
155 if (!search_cap(channel->caps, *p))
157 if (request_dma(channel->chan, dev_id) == 0)
158 return channel->chan;
164 EXPORT_SYMBOL(request_dma_bycap);
166 int dmac_search_free_channel(const char *dev_id)
168 struct dma_channel *channel = { 0 };
169 struct dma_info *info = get_dma_info(0);
172 for (i = 0; i < info->nr_channels; i++) {
173 channel = &info->channels[i];
174 if (unlikely(!channel))
177 if (atomic_read(&channel->busy) == 0)
181 if (info->ops->request) {
182 int result = info->ops->request(channel);
186 atomic_set(&channel->busy, 1);
187 return channel->chan;
193 int request_dma(unsigned int chan, const char *dev_id)
195 struct dma_channel *channel = { 0 };
196 struct dma_info *info = get_dma_info(chan);
199 channel = get_dma_channel(chan);
200 if (atomic_xchg(&channel->busy, 1))
203 strlcpy(channel->dev_id, dev_id, sizeof(channel->dev_id));
205 if (info->ops->request) {
206 result = info->ops->request(channel);
208 atomic_set(&channel->busy, 0);
215 EXPORT_SYMBOL(request_dma);
217 void free_dma(unsigned int chan)
219 struct dma_info *info = get_dma_info(chan);
220 struct dma_channel *channel = get_dma_channel(chan);
223 info->ops->free(channel);
225 atomic_set(&channel->busy, 0);
227 EXPORT_SYMBOL(free_dma);
229 void dma_wait_for_completion(unsigned int chan)
231 struct dma_info *info = get_dma_info(chan);
232 struct dma_channel *channel = get_dma_channel(chan);
234 if (channel->flags & DMA_TEI_CAPABLE) {
235 wait_event(channel->wait_queue,
236 (info->ops->get_residue(channel) == 0));
240 while (info->ops->get_residue(channel))
243 EXPORT_SYMBOL(dma_wait_for_completion);
245 int register_chan_caps(const char *dmac, struct dma_chan_caps *caps)
247 struct dma_info *info;
248 unsigned int found = 0;
251 list_for_each_entry(info, ®istered_dmac_list, list)
252 if (strcmp(dmac, info->name) == 0) {
257 if (unlikely(!found))
260 for (i = 0; i < info->nr_channels; i++, caps++) {
261 struct dma_channel *channel;
263 if ((info->first_channel_nr + i) != caps->ch_num)
266 channel = &info->channels[i];
267 channel->caps = caps->caplist;
272 EXPORT_SYMBOL(register_chan_caps);
274 void dma_configure_channel(unsigned int chan, unsigned long flags)
276 struct dma_info *info = get_dma_info(chan);
277 struct dma_channel *channel = get_dma_channel(chan);
279 if (info->ops->configure)
280 info->ops->configure(channel, flags);
282 EXPORT_SYMBOL(dma_configure_channel);
284 int dma_xfer(unsigned int chan, unsigned long from,
285 unsigned long to, size_t size, unsigned int mode)
287 struct dma_info *info = get_dma_info(chan);
288 struct dma_channel *channel = get_dma_channel(chan);
292 channel->count = size;
293 channel->mode = mode;
295 return info->ops->xfer(channel);
297 EXPORT_SYMBOL(dma_xfer);
299 int dma_extend(unsigned int chan, unsigned long op, void *param)
301 struct dma_info *info = get_dma_info(chan);
302 struct dma_channel *channel = get_dma_channel(chan);
304 if (info->ops->extend)
305 return info->ops->extend(channel, op, param);
309 EXPORT_SYMBOL(dma_extend);
311 static int dma_read_proc(char *buf, char **start, off_t off,
312 int len, int *eof, void *data)
314 struct dma_info *info;
317 if (list_empty(®istered_dmac_list))
321 * Iterate over each registered DMAC
323 list_for_each_entry(info, ®istered_dmac_list, list) {
327 * Iterate over each channel
329 for (i = 0; i < info->nr_channels; i++) {
330 struct dma_channel *channel = info->channels + i;
332 if (!(channel->flags & DMA_CONFIGURED))
335 p += sprintf(p, "%2d: %14s %s\n", i,
336 info->name, channel->dev_id);
343 int register_dmac(struct dma_info *info)
345 unsigned int total_channels, i;
347 INIT_LIST_HEAD(&info->list);
349 printk(KERN_INFO "DMA: Registering %s handler (%d channel%s).\n",
350 info->name, info->nr_channels, info->nr_channels > 1 ? "s" : "");
352 BUG_ON((info->flags & DMAC_CHANNELS_CONFIGURED) && !info->channels);
354 info->pdev = platform_device_register_simple(info->name, -1,
356 if (IS_ERR(info->pdev))
357 return PTR_ERR(info->pdev);
360 * Don't touch pre-configured channels
362 if (!(info->flags & DMAC_CHANNELS_CONFIGURED)) {
365 size = sizeof(struct dma_channel) * info->nr_channels;
367 info->channels = kzalloc(size, GFP_KERNEL);
372 total_channels = get_nr_channels();
373 info->first_vchannel_nr = total_channels;
374 for (i = 0; i < info->nr_channels; i++) {
375 struct dma_channel *chan = &info->channels[i];
377 atomic_set(&chan->busy, 0);
379 chan->chan = info->first_channel_nr + i;
380 chan->vchan = info->first_channel_nr + i + total_channels;
382 memcpy(chan->dev_id, "Unused", 7);
384 if (info->flags & DMAC_CHANNELS_TEI_CAPABLE)
385 chan->flags |= DMA_TEI_CAPABLE;
387 init_waitqueue_head(&chan->wait_queue);
388 dma_create_sysfs_files(chan, info);
391 list_add(&info->list, ®istered_dmac_list);
395 EXPORT_SYMBOL(register_dmac);
397 void unregister_dmac(struct dma_info *info)
401 for (i = 0; i < info->nr_channels; i++)
402 dma_remove_sysfs_files(info->channels + i, info);
404 if (!(info->flags & DMAC_CHANNELS_CONFIGURED))
405 kfree(info->channels);
407 list_del(&info->list);
408 platform_device_unregister(info->pdev);
410 EXPORT_SYMBOL(unregister_dmac);
412 static int __init dma_api_init(void)
414 printk(KERN_NOTICE "DMA: Registering DMA API.\n");
415 create_proc_read_entry("dma", 0, 0, dma_read_proc, 0);
418 subsys_initcall(dma_api_init);
420 MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
421 MODULE_DESCRIPTION("DMA API for SuperH");
422 MODULE_LICENSE("GPL");