[ARM] Remove definition of MAX_DMA_CHANNELS to zero
[linux-2.6-block.git] / arch / arm / kernel / dma.c
1 /*
2  *  linux/arch/arm/kernel/dma.c
3  *
4  *  Copyright (C) 1995-2000 Russell King
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  *  Front-end to the DMA handling.  This handles the allocation/freeing
11  *  of DMA channels, and provides a unified interface to the machines
12  *  DMA facilities.
13  */
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/spinlock.h>
17 #include <linux/errno.h>
18
19 #include <asm/dma.h>
20
21 #include <asm/mach/dma.h>
22
23 DEFINE_SPINLOCK(dma_spin_lock);
24
25 static dma_t dma_chan[MAX_DMA_CHANNELS];
26
27 /*
28  * Get dma list for /proc/dma
29  */
30 int get_dma_list(char *buf)
31 {
32         dma_t *dma;
33         char *p = buf;
34         int i;
35
36         for (i = 0, dma = dma_chan; i < MAX_DMA_CHANNELS; i++, dma++)
37                 if (dma->lock)
38                         p += sprintf(p, "%2d: %14s %s\n", i,
39                                      dma->d_ops->type, dma->device_id);
40
41         return p - buf;
42 }
43
44 /*
45  * Request DMA channel
46  *
47  * On certain platforms, we have to allocate an interrupt as well...
48  */
49 int request_dma(dmach_t channel, const char *device_id)
50 {
51         dma_t *dma = dma_chan + channel;
52         int ret;
53
54         if (channel >= MAX_DMA_CHANNELS || !dma->d_ops)
55                 goto bad_dma;
56
57         if (xchg(&dma->lock, 1) != 0)
58                 goto busy;
59
60         dma->device_id = device_id;
61         dma->active    = 0;
62         dma->invalid   = 1;
63
64         ret = 0;
65         if (dma->d_ops->request)
66                 ret = dma->d_ops->request(channel, dma);
67
68         if (ret)
69                 xchg(&dma->lock, 0);
70
71         return ret;
72
73 bad_dma:
74         printk(KERN_ERR "dma: trying to allocate DMA%d\n", channel);
75         return -EINVAL;
76
77 busy:
78         return -EBUSY;
79 }
80
81 /*
82  * Free DMA channel
83  *
84  * On certain platforms, we have to free interrupt as well...
85  */
86 void free_dma(dmach_t channel)
87 {
88         dma_t *dma = dma_chan + channel;
89
90         if (channel >= MAX_DMA_CHANNELS || !dma->d_ops)
91                 goto bad_dma;
92
93         if (dma->active) {
94                 printk(KERN_ERR "dma%d: freeing active DMA\n", channel);
95                 dma->d_ops->disable(channel, dma);
96                 dma->active = 0;
97         }
98
99         if (xchg(&dma->lock, 0) != 0) {
100                 if (dma->d_ops->free)
101                         dma->d_ops->free(channel, dma);
102                 return;
103         }
104
105         printk(KERN_ERR "dma%d: trying to free free DMA\n", channel);
106         return;
107
108 bad_dma:
109         printk(KERN_ERR "dma: trying to free DMA%d\n", channel);
110 }
111
112 /* Set DMA Scatter-Gather list
113  */
114 void set_dma_sg (dmach_t channel, struct scatterlist *sg, int nr_sg)
115 {
116         dma_t *dma = dma_chan + channel;
117
118         if (dma->active)
119                 printk(KERN_ERR "dma%d: altering DMA SG while "
120                        "DMA active\n", channel);
121
122         dma->sg = sg;
123         dma->sgcount = nr_sg;
124         dma->invalid = 1;
125 }
126
127 /* Set DMA address
128  *
129  * Copy address to the structure, and set the invalid bit
130  */
131 void __set_dma_addr (dmach_t channel, void *addr)
132 {
133         dma_t *dma = dma_chan + channel;
134
135         if (dma->active)
136                 printk(KERN_ERR "dma%d: altering DMA address while "
137                        "DMA active\n", channel);
138
139         dma->sg = NULL;
140         dma->addr = addr;
141         dma->invalid = 1;
142 }
143
144 /* Set DMA byte count
145  *
146  * Copy address to the structure, and set the invalid bit
147  */
148 void set_dma_count (dmach_t channel, unsigned long count)
149 {
150         dma_t *dma = dma_chan + channel;
151
152         if (dma->active)
153                 printk(KERN_ERR "dma%d: altering DMA count while "
154                        "DMA active\n", channel);
155
156         dma->sg = NULL;
157         dma->count = count;
158         dma->invalid = 1;
159 }
160
161 /* Set DMA direction mode
162  */
163 void set_dma_mode (dmach_t channel, dmamode_t mode)
164 {
165         dma_t *dma = dma_chan + channel;
166
167         if (dma->active)
168                 printk(KERN_ERR "dma%d: altering DMA mode while "
169                        "DMA active\n", channel);
170
171         dma->dma_mode = mode;
172         dma->invalid = 1;
173 }
174
175 /* Enable DMA channel
176  */
177 void enable_dma (dmach_t channel)
178 {
179         dma_t *dma = dma_chan + channel;
180
181         if (!dma->lock)
182                 goto free_dma;
183
184         if (dma->active == 0) {
185                 dma->active = 1;
186                 dma->d_ops->enable(channel, dma);
187         }
188         return;
189
190 free_dma:
191         printk(KERN_ERR "dma%d: trying to enable free DMA\n", channel);
192         BUG();
193 }
194
195 /* Disable DMA channel
196  */
197 void disable_dma (dmach_t channel)
198 {
199         dma_t *dma = dma_chan + channel;
200
201         if (!dma->lock)
202                 goto free_dma;
203
204         if (dma->active == 1) {
205                 dma->active = 0;
206                 dma->d_ops->disable(channel, dma);
207         }
208         return;
209
210 free_dma:
211         printk(KERN_ERR "dma%d: trying to disable free DMA\n", channel);
212         BUG();
213 }
214
215 /*
216  * Is the specified DMA channel active?
217  */
218 int dma_channel_active(dmach_t channel)
219 {
220         return dma_chan[channel].active;
221 }
222
223 void set_dma_page(dmach_t channel, char pagenr)
224 {
225         printk(KERN_ERR "dma%d: trying to set_dma_page\n", channel);
226 }
227
228 void set_dma_speed(dmach_t channel, int cycle_ns)
229 {
230         dma_t *dma = dma_chan + channel;
231         int ret = 0;
232
233         if (dma->d_ops->setspeed)
234                 ret = dma->d_ops->setspeed(channel, dma, cycle_ns);
235         dma->speed = ret;
236 }
237
238 int get_dma_residue(dmach_t channel)
239 {
240         dma_t *dma = dma_chan + channel;
241         int ret = 0;
242
243         if (dma->d_ops->residue)
244                 ret = dma->d_ops->residue(channel, dma);
245
246         return ret;
247 }
248
249 static int __init init_dma(void)
250 {
251         arch_dma_init(dma_chan);
252         return 0;
253 }
254
255 core_initcall(init_dma);
256
257 EXPORT_SYMBOL(request_dma);
258 EXPORT_SYMBOL(free_dma);
259 EXPORT_SYMBOL(enable_dma);
260 EXPORT_SYMBOL(disable_dma);
261 EXPORT_SYMBOL(__set_dma_addr);
262 EXPORT_SYMBOL(set_dma_count);
263 EXPORT_SYMBOL(set_dma_mode);
264 EXPORT_SYMBOL(set_dma_page);
265 EXPORT_SYMBOL(get_dma_residue);
266 EXPORT_SYMBOL(set_dma_sg);
267 EXPORT_SYMBOL(set_dma_speed);
268
269 EXPORT_SYMBOL(dma_spin_lock);