2 * arch/sh/drivers/dma/dma-api.c
4 * SuperH-specific DMA management API
6 * Copyright (C) 2003 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/interrupt.h>
15 #include <linux/spinlock.h>
16 #include <linux/proc_fs.h>
19 struct dma_info dma_info[MAX_DMA_CHANNELS] = { { 0, } };
20 spinlock_t dma_spin_lock = SPIN_LOCK_UNLOCKED;
23 * A brief note about the reasons for this API as it stands.
25 * For starters, the old ISA DMA API didn't work for us for a number of
26 * reasons, for one, the vast majority of channels on the SH DMAC are
27 * dual-address mode only, and both the new and the old DMA APIs are after the
28 * concept of managing a DMA buffer, which doesn't overly fit this model very
29 * well. In addition to which, the new API is largely geared at IOMMUs and
30 * GARTs, and doesn't even support the channel notion very well.
32 * The other thing that's a marginal issue, is the sheer number of random DMA
33 * engines that are present (ie, in boards like the Dreamcast), some of which
34 * cascade off of the SH DMAC, and others do not. As such, there was a real
35 * need for a scalable subsystem that could deal with both single and
36 * dual-address mode usage, in addition to interoperating with cascaded DMACs.
38 * There really isn't any reason why this needs to be SH specific, though I'm
39 * not aware of too many other processors (with the exception of some MIPS)
40 * that have the same concept of a dual address mode, or any real desire to
41 * actually make use of the DMAC even if such a subsystem were exposed
44 * The idea for this was derived from the ARM port, which acted as an excellent
45 * reference when trying to address these issues.
47 * It should also be noted that the decision to add Yet Another DMA API(tm) to
48 * the kernel wasn't made easily, and was only decided upon after conferring
49 * with jejb with regards to the state of the old and new APIs as they applied
50 * to these circumstances. Philip Blundell was also a great help in figuring
51 * out some single-address mode DMA semantics that were otherwise rather
55 struct dma_info *get_dma_info(unsigned int chan)
57 return dma_info + chan;
60 int get_dma_residue(unsigned int chan)
62 struct dma_info *info = get_dma_info(chan);
64 if (info->ops->get_residue)
65 return info->ops->get_residue(info);
70 int request_dma(unsigned int chan, const char *dev_id)
72 struct dma_info *info = get_dma_info(chan);
76 if (!info->ops || chan >= MAX_DMA_CHANNELS) {
81 atomic_set(&info->busy, 1);
83 info->dev_id = dev_id;
87 if (info->ops->request)
88 return info->ops->request(info);
93 void free_dma(unsigned int chan)
95 struct dma_info *info = get_dma_info(chan);
98 info->ops->free(info);
100 atomic_set(&info->busy, 0);
103 void dma_wait_for_completion(unsigned int chan)
105 struct dma_info *info = get_dma_info(chan);
107 if (info->tei_capable) {
108 wait_event(info->wait_queue, (info->ops->get_residue(info) == 0));
112 while (info->ops->get_residue(info))
116 void dma_configure_channel(unsigned int chan, unsigned long flags)
118 struct dma_info *info = get_dma_info(chan);
120 if (info->ops->configure)
121 info->ops->configure(info, flags);
124 int dma_xfer(unsigned int chan, unsigned long from,
125 unsigned long to, size_t size, unsigned int mode)
127 struct dma_info *info = get_dma_info(chan);
134 return info->ops->xfer(info);
137 #ifdef CONFIG_PROC_FS
138 static int dma_read_proc(char *buf, char **start, off_t off,
139 int len, int *eof, void *data)
141 struct dma_info *info;
145 for (i = 0, info = dma_info; i < MAX_DMA_CHANNELS; i++, info++) {
146 if (!atomic_read(&info->busy))
149 p += sprintf(p, "%2d: %14s %s\n", i,
150 info->ops->name, info->dev_id);
157 int __init register_dmac(struct dma_ops *ops)
161 printk("DMA: Registering %s handler.\n", ops->name);
163 for (i = 0; i < MAX_DMA_CHANNELS; i++) {
164 struct dma_info *info = get_dma_info(i);
168 init_MUTEX(&info->sem);
169 init_waitqueue_head(&info->wait_queue);
175 static int __init dma_api_init(void)
177 printk("DMA: Registering DMA API.\n");
179 #ifdef CONFIG_PROC_FS
180 create_proc_read_entry("dma", 0, 0, dma_read_proc, 0);
186 subsys_initcall(dma_api_init);
188 MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
189 MODULE_DESCRIPTION("DMA API for SuperH");
190 MODULE_LICENSE("GPL");
192 EXPORT_SYMBOL(request_dma);
193 EXPORT_SYMBOL(free_dma);
194 EXPORT_SYMBOL(get_dma_residue);
195 EXPORT_SYMBOL(get_dma_info);
196 EXPORT_SYMBOL(dma_xfer);
197 EXPORT_SYMBOL(dma_wait_for_completion);
198 EXPORT_SYMBOL(dma_configure_channel);