ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / sh / drivers / dma / dma-api.c
1 /*
2  * arch/sh/drivers/dma/dma-api.c
3  *
4  * SuperH-specific DMA management API
5  *
6  * Copyright (C) 2003  Paul Mundt
7  *
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.
11  */ 
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>
17 #include <asm/dma.h>
18
19 struct dma_info dma_info[MAX_DMA_CHANNELS] = { { 0, } };
20 spinlock_t dma_spin_lock = SPIN_LOCK_UNLOCKED;
21
22 /* 
23  * A brief note about the reasons for this API as it stands.
24  *
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.
31  *
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.
37  *
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
42  * elsewhere.
43  *
44  * The idea for this was derived from the ARM port, which acted as an excellent
45  * reference when trying to address these issues.
46  *
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
52  * confusing.
53  */
54
55 struct dma_info *get_dma_info(unsigned int chan)
56 {
57         return dma_info + chan;
58 }
59
60 int get_dma_residue(unsigned int chan)
61 {
62         struct dma_info *info = get_dma_info(chan);
63
64         if (info->ops->get_residue)
65                 return info->ops->get_residue(info);
66         
67         return 0;
68 }
69
70 int request_dma(unsigned int chan, const char *dev_id)
71 {
72         struct dma_info *info = get_dma_info(chan);
73
74         down(&info->sem);
75
76         if (!info->ops || chan >= MAX_DMA_CHANNELS) {
77                 up(&info->sem);
78                 return -EINVAL;
79         }
80         
81         atomic_set(&info->busy, 1);
82
83         info->dev_id = dev_id;
84
85         up(&info->sem);
86
87         if (info->ops->request)
88                 return info->ops->request(info);
89         
90         return 0;
91 }
92
93 void free_dma(unsigned int chan)
94 {
95         struct dma_info *info = get_dma_info(chan);
96
97         if (info->ops->free)
98                 info->ops->free(info);
99         
100         atomic_set(&info->busy, 0);
101 }
102
103 void dma_wait_for_completion(unsigned int chan)
104 {
105         struct dma_info *info = get_dma_info(chan);
106
107         if (info->tei_capable) {
108                 wait_event(info->wait_queue, (info->ops->get_residue(info) == 0));
109                 return;
110         }
111
112         while (info->ops->get_residue(info))
113                 cpu_relax();
114 }
115
116 void dma_configure_channel(unsigned int chan, unsigned long flags)
117 {
118         struct dma_info *info = get_dma_info(chan);
119
120         if (info->ops->configure)
121                 info->ops->configure(info, flags);
122 }
123
124 int dma_xfer(unsigned int chan, unsigned long from,
125              unsigned long to, size_t size, unsigned int mode)
126 {
127         struct dma_info *info = get_dma_info(chan);
128
129         info->sar       = from;
130         info->dar       = to;
131         info->count     = size;
132         info->mode      = mode;
133
134         return info->ops->xfer(info);
135 }
136
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)
140 {
141         struct dma_info *info;
142         char *p = buf;
143         int i;
144
145         for (i = 0, info = dma_info; i < MAX_DMA_CHANNELS; i++, info++) {
146                 if (!atomic_read(&info->busy))
147                         continue;
148
149                 p += sprintf(p, "%2d: %14s    %s\n", i,
150                              info->ops->name, info->dev_id);
151         }
152
153         return p - buf;
154 }
155 #endif
156
157 int __init register_dmac(struct dma_ops *ops)
158 {
159         int i;
160
161         printk("DMA: Registering %s handler.\n", ops->name);
162
163         for (i = 0; i < MAX_DMA_CHANNELS; i++) {
164                 struct dma_info *info = get_dma_info(i);
165
166                 info->chan = i;
167
168                 init_MUTEX(&info->sem);
169                 init_waitqueue_head(&info->wait_queue);
170         }
171
172         return 0;
173 }
174
175 static int __init dma_api_init(void)
176 {
177         printk("DMA: Registering DMA API.\n");
178
179 #ifdef CONFIG_PROC_FS
180         create_proc_read_entry("dma", 0, 0, dma_read_proc, 0);
181 #endif
182
183         return 0;
184 }
185
186 subsys_initcall(dma_api_init);
187
188 MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
189 MODULE_DESCRIPTION("DMA API for SuperH");
190 MODULE_LICENSE("GPL");
191
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);
199