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>
21 DEFINE_SPINLOCK(dma_spin_lock);
22 static LIST_HEAD(registered_dmac_list);
24 struct dma_info *get_dma_info(unsigned int chan)
26 struct dma_info *info;
29 * Look for each DMAC's range to determine who the owner of
32 list_for_each_entry(info, ®istered_dmac_list, list) {
33 if ((chan < info->first_channel_nr) ||
34 (chan >= info->first_channel_nr + info->nr_channels))
42 EXPORT_SYMBOL(get_dma_info);
44 struct dma_info *get_dma_info_by_name(const char *dmac_name)
46 struct dma_info *info;
48 list_for_each_entry(info, ®istered_dmac_list, list) {
49 if (dmac_name && (strcmp(dmac_name, info->name) != 0))
57 EXPORT_SYMBOL(get_dma_info_by_name);
59 static unsigned int get_nr_channels(void)
61 struct dma_info *info;
64 if (unlikely(list_empty(®istered_dmac_list)))
67 list_for_each_entry(info, ®istered_dmac_list, list)
68 nr += info->nr_channels;
73 struct dma_channel *get_dma_channel(unsigned int chan)
75 struct dma_info *info = get_dma_info(chan);
76 struct dma_channel *channel;
80 return ERR_PTR(-EINVAL);
82 for (i = 0; i < info->nr_channels; i++) {
83 channel = &info->channels[i];
84 if (channel->chan == chan)
90 EXPORT_SYMBOL(get_dma_channel);
92 int get_dma_residue(unsigned int chan)
94 struct dma_info *info = get_dma_info(chan);
95 struct dma_channel *channel = get_dma_channel(chan);
97 if (info->ops->get_residue)
98 return info->ops->get_residue(channel);
102 EXPORT_SYMBOL(get_dma_residue);
104 static int search_cap(const char **haystack, const char *needle)
108 for (p = haystack; *p; p++)
109 if (strcmp(*p, needle) == 0)
116 * request_dma_bycap - Allocate a DMA channel based on its capabilities
117 * @dmac: List of DMA controllers to search
118 * @caps: List of capabilites
120 * Search all channels of all DMA controllers to find a channel which
121 * matches the requested capabilities. The result is the channel
122 * number if a match is found, or %-ENODEV if no match is found.
124 * Note that not all DMA controllers export capabilities, in which
125 * case they can never be allocated using this API, and so
126 * request_dma() must be used specifying the channel number.
128 int request_dma_bycap(const char **dmac, const char **caps, const char *dev_id)
130 unsigned int found = 0;
131 struct dma_info *info;
135 BUG_ON(!dmac || !caps);
137 list_for_each_entry(info, ®istered_dmac_list, list)
138 if (strcmp(*dmac, info->name) == 0) {
146 for (i = 0; i < info->nr_channels; i++) {
147 struct dma_channel *channel = &info->channels[i];
149 if (unlikely(!channel->caps))
152 for (p = caps; *p; p++) {
153 if (!search_cap(channel->caps, *p))
155 if (request_dma(channel->chan, dev_id) == 0)
156 return channel->chan;
162 EXPORT_SYMBOL(request_dma_bycap);
164 int dmac_search_free_channel(const char *dev_id)
166 struct dma_channel *channel = { 0 };
167 struct dma_info *info = get_dma_info(0);
170 for (i = 0; i < info->nr_channels; i++) {
171 channel = &info->channels[i];
172 if (unlikely(!channel))
175 if (atomic_read(&channel->busy) == 0)
179 if (info->ops->request) {
180 int result = info->ops->request(channel);
184 atomic_set(&channel->busy, 1);
185 return channel->chan;
191 int request_dma(unsigned int chan, const char *dev_id)
193 struct dma_channel *channel = { 0 };
194 struct dma_info *info = get_dma_info(chan);
197 channel = get_dma_channel(chan);
198 if (atomic_xchg(&channel->busy, 1))
201 strlcpy(channel->dev_id, dev_id, sizeof(channel->dev_id));
203 if (info->ops->request) {
204 result = info->ops->request(channel);
206 atomic_set(&channel->busy, 0);
213 EXPORT_SYMBOL(request_dma);
215 void free_dma(unsigned int chan)
217 struct dma_info *info = get_dma_info(chan);
218 struct dma_channel *channel = get_dma_channel(chan);
221 info->ops->free(channel);
223 atomic_set(&channel->busy, 0);
225 EXPORT_SYMBOL(free_dma);
227 void dma_wait_for_completion(unsigned int chan)
229 struct dma_info *info = get_dma_info(chan);
230 struct dma_channel *channel = get_dma_channel(chan);
232 if (channel->flags & DMA_TEI_CAPABLE) {
233 wait_event(channel->wait_queue,
234 (info->ops->get_residue(channel) == 0));
238 while (info->ops->get_residue(channel))
241 EXPORT_SYMBOL(dma_wait_for_completion);
243 int register_chan_caps(const char *dmac, struct dma_chan_caps *caps)
245 struct dma_info *info;
246 unsigned int found = 0;
249 list_for_each_entry(info, ®istered_dmac_list, list)
250 if (strcmp(dmac, info->name) == 0) {
255 if (unlikely(!found))
258 for (i = 0; i < info->nr_channels; i++, caps++) {
259 struct dma_channel *channel;
261 if ((info->first_channel_nr + i) != caps->ch_num)
264 channel = &info->channels[i];
265 channel->caps = caps->caplist;
270 EXPORT_SYMBOL(register_chan_caps);
272 void dma_configure_channel(unsigned int chan, unsigned long flags)
274 struct dma_info *info = get_dma_info(chan);
275 struct dma_channel *channel = get_dma_channel(chan);
277 if (info->ops->configure)
278 info->ops->configure(channel, flags);
280 EXPORT_SYMBOL(dma_configure_channel);
282 int dma_xfer(unsigned int chan, unsigned long from,
283 unsigned long to, size_t size, unsigned int mode)
285 struct dma_info *info = get_dma_info(chan);
286 struct dma_channel *channel = get_dma_channel(chan);
290 channel->count = size;
291 channel->mode = mode;
293 return info->ops->xfer(channel);
295 EXPORT_SYMBOL(dma_xfer);
297 int dma_extend(unsigned int chan, unsigned long op, void *param)
299 struct dma_info *info = get_dma_info(chan);
300 struct dma_channel *channel = get_dma_channel(chan);
302 if (info->ops->extend)
303 return info->ops->extend(channel, op, param);
307 EXPORT_SYMBOL(dma_extend);
309 static int dma_read_proc(char *buf, char **start, off_t off,
310 int len, int *eof, void *data)
312 struct dma_info *info;
315 if (list_empty(®istered_dmac_list))
319 * Iterate over each registered DMAC
321 list_for_each_entry(info, ®istered_dmac_list, list) {
325 * Iterate over each channel
327 for (i = 0; i < info->nr_channels; i++) {
328 struct dma_channel *channel = info->channels + i;
330 if (!(channel->flags & DMA_CONFIGURED))
333 p += sprintf(p, "%2d: %14s %s\n", i,
334 info->name, channel->dev_id);
341 int register_dmac(struct dma_info *info)
343 unsigned int total_channels, i;
345 INIT_LIST_HEAD(&info->list);
347 printk(KERN_INFO "DMA: Registering %s handler (%d channel%s).\n",
348 info->name, info->nr_channels, info->nr_channels > 1 ? "s" : "");
350 BUG_ON((info->flags & DMAC_CHANNELS_CONFIGURED) && !info->channels);
352 info->pdev = platform_device_register_simple((char *)info->name, -1,
354 if (IS_ERR(info->pdev))
355 return PTR_ERR(info->pdev);
358 * Don't touch pre-configured channels
360 if (!(info->flags & DMAC_CHANNELS_CONFIGURED)) {
363 size = sizeof(struct dma_channel) * info->nr_channels;
365 info->channels = kzalloc(size, GFP_KERNEL);
370 total_channels = get_nr_channels();
371 for (i = 0; i < info->nr_channels; i++) {
372 struct dma_channel *chan = &info->channels[i];
374 atomic_set(&chan->busy, 0);
376 chan->chan = info->first_channel_nr + i;
377 chan->vchan = info->first_channel_nr + i + total_channels;
379 memcpy(chan->dev_id, "Unused", 7);
381 if (info->flags & DMAC_CHANNELS_TEI_CAPABLE)
382 chan->flags |= DMA_TEI_CAPABLE;
384 init_waitqueue_head(&chan->wait_queue);
385 dma_create_sysfs_files(chan, info);
388 list_add(&info->list, ®istered_dmac_list);
392 EXPORT_SYMBOL(register_dmac);
394 void unregister_dmac(struct dma_info *info)
398 for (i = 0; i < info->nr_channels; i++)
399 dma_remove_sysfs_files(info->channels + i, info);
401 if (!(info->flags & DMAC_CHANNELS_CONFIGURED))
402 kfree(info->channels);
404 list_del(&info->list);
405 platform_device_unregister(info->pdev);
407 EXPORT_SYMBOL(unregister_dmac);
409 static int __init dma_api_init(void)
411 printk(KERN_NOTICE "DMA: Registering DMA API.\n");
412 create_proc_read_entry("dma", 0, 0, dma_read_proc, 0);
415 subsys_initcall(dma_api_init);
417 MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
418 MODULE_DESCRIPTION("DMA API for SuperH");
419 MODULE_LICENSE("GPL");