]> err.no Git - linux-2.6/blob - arch/arm/kernel/dma.c
[ARM] Use core_initcall() to initialise ARM DMA
[linux-2.6] / 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 #if MAX_DMA_CHANNELS > 0
26
27 static dma_t dma_chan[MAX_DMA_CHANNELS];
28
29 /*
30  * Get dma list for /proc/dma
31  */
32 int get_dma_list(char *buf)
33 {
34         dma_t *dma;
35         char *p = buf;
36         int i;
37
38         for (i = 0, dma = dma_chan; i < MAX_DMA_CHANNELS; i++, dma++)
39                 if (dma->lock)
40                         p += sprintf(p, "%2d: %14s %s\n", i,
41                                      dma->d_ops->type, dma->device_id);
42
43         return p - buf;
44 }
45
46 /*
47  * Request DMA channel
48  *
49  * On certain platforms, we have to allocate an interrupt as well...
50  */
51 int request_dma(dmach_t channel, const char *device_id)
52 {
53         dma_t *dma = dma_chan + channel;
54         int ret;
55
56         if (channel >= MAX_DMA_CHANNELS || !dma->d_ops)
57                 goto bad_dma;
58
59         if (xchg(&dma->lock, 1) != 0)
60                 goto busy;
61
62         dma->device_id = device_id;
63         dma->active    = 0;
64         dma->invalid   = 1;
65
66         ret = 0;
67         if (dma->d_ops->request)
68                 ret = dma->d_ops->request(channel, dma);
69
70         if (ret)
71                 xchg(&dma->lock, 0);
72
73         return ret;
74
75 bad_dma:
76         printk(KERN_ERR "dma: trying to allocate DMA%d\n", channel);
77         return -EINVAL;
78
79 busy:
80         return -EBUSY;
81 }
82
83 /*
84  * Free DMA channel
85  *
86  * On certain platforms, we have to free interrupt as well...
87  */
88 void free_dma(dmach_t channel)
89 {
90         dma_t *dma = dma_chan + channel;
91
92         if (channel >= MAX_DMA_CHANNELS || !dma->d_ops)
93                 goto bad_dma;
94
95         if (dma->active) {
96                 printk(KERN_ERR "dma%d: freeing active DMA\n", channel);
97                 dma->d_ops->disable(channel, dma);
98                 dma->active = 0;
99         }
100
101         if (xchg(&dma->lock, 0) != 0) {
102                 if (dma->d_ops->free)
103                         dma->d_ops->free(channel, dma);
104                 return;
105         }
106
107         printk(KERN_ERR "dma%d: trying to free free DMA\n", channel);
108         return;
109
110 bad_dma:
111         printk(KERN_ERR "dma: trying to free DMA%d\n", channel);
112 }
113
114 /* Set DMA Scatter-Gather list
115  */
116 void set_dma_sg (dmach_t channel, struct scatterlist *sg, int nr_sg)
117 {
118         dma_t *dma = dma_chan + channel;
119
120         if (dma->active)
121                 printk(KERN_ERR "dma%d: altering DMA SG while "
122                        "DMA active\n", channel);
123
124         dma->sg = sg;
125         dma->sgcount = nr_sg;
126         dma->invalid = 1;
127 }
128
129 /* Set DMA address
130  *
131  * Copy address to the structure, and set the invalid bit
132  */
133 void __set_dma_addr (dmach_t channel, void *addr)
134 {
135         dma_t *dma = dma_chan + channel;
136
137         if (dma->active)
138                 printk(KERN_ERR "dma%d: altering DMA address while "
139                        "DMA active\n", channel);
140
141         dma->sg = NULL;
142         dma->addr = addr;
143         dma->invalid = 1;
144 }
145
146 /* Set DMA byte count
147  *
148  * Copy address to the structure, and set the invalid bit
149  */
150 void set_dma_count (dmach_t channel, unsigned long count)
151 {
152         dma_t *dma = dma_chan + channel;
153
154         if (dma->active)
155                 printk(KERN_ERR "dma%d: altering DMA count while "
156                        "DMA active\n", channel);
157
158         dma->sg = NULL;
159         dma->count = count;
160         dma->invalid = 1;
161 }
162
163 /* Set DMA direction mode
164  */
165 void set_dma_mode (dmach_t channel, dmamode_t mode)
166 {
167         dma_t *dma = dma_chan + channel;
168
169         if (dma->active)
170                 printk(KERN_ERR "dma%d: altering DMA mode while "
171                        "DMA active\n", channel);
172
173         dma->dma_mode = mode;
174         dma->invalid = 1;
175 }
176
177 /* Enable DMA channel
178  */
179 void enable_dma (dmach_t channel)
180 {
181         dma_t *dma = dma_chan + channel;
182
183         if (!dma->lock)
184                 goto free_dma;
185
186         if (dma->active == 0) {
187                 dma->active = 1;
188                 dma->d_ops->enable(channel, dma);
189         }
190         return;
191
192 free_dma:
193         printk(KERN_ERR "dma%d: trying to enable free DMA\n", channel);
194         BUG();
195 }
196
197 /* Disable DMA channel
198  */
199 void disable_dma (dmach_t channel)
200 {
201         dma_t *dma = dma_chan + channel;
202
203         if (!dma->lock)
204                 goto free_dma;
205
206         if (dma->active == 1) {
207                 dma->active = 0;
208                 dma->d_ops->disable(channel, dma);
209         }
210         return;
211
212 free_dma:
213         printk(KERN_ERR "dma%d: trying to disable free DMA\n", channel);
214         BUG();
215 }
216
217 /*
218  * Is the specified DMA channel active?
219  */
220 int dma_channel_active(dmach_t channel)
221 {
222         return dma_chan[channel].active;
223 }
224
225 void set_dma_page(dmach_t channel, char pagenr)
226 {
227         printk(KERN_ERR "dma%d: trying to set_dma_page\n", channel);
228 }
229
230 void set_dma_speed(dmach_t channel, int cycle_ns)
231 {
232         dma_t *dma = dma_chan + channel;
233         int ret = 0;
234
235         if (dma->d_ops->setspeed)
236                 ret = dma->d_ops->setspeed(channel, dma, cycle_ns);
237         dma->speed = ret;
238 }
239
240 int get_dma_residue(dmach_t channel)
241 {
242         dma_t *dma = dma_chan + channel;
243         int ret = 0;
244
245         if (dma->d_ops->residue)
246                 ret = dma->d_ops->residue(channel, dma);
247
248         return ret;
249 }
250
251 static int __init init_dma(void)
252 {
253         arch_dma_init(dma_chan);
254         return 0;
255 }
256
257 core_initcall(init_dma);
258
259 #else
260
261 int request_dma(dmach_t channel, const char *device_id)
262 {
263         return -EINVAL;
264 }
265
266 int get_dma_residue(dmach_t channel)
267 {
268         return 0;
269 }
270
271 #define GLOBAL_ALIAS(_a,_b) asm (".set " #_a "," #_b "; .globl " #_a)
272 GLOBAL_ALIAS(disable_dma, get_dma_residue);
273 GLOBAL_ALIAS(enable_dma, get_dma_residue);
274 GLOBAL_ALIAS(free_dma, get_dma_residue);
275 GLOBAL_ALIAS(get_dma_list, get_dma_residue);
276 GLOBAL_ALIAS(set_dma_mode, get_dma_residue);
277 GLOBAL_ALIAS(set_dma_page, get_dma_residue);
278 GLOBAL_ALIAS(set_dma_count, get_dma_residue);
279 GLOBAL_ALIAS(__set_dma_addr, get_dma_residue);
280 GLOBAL_ALIAS(set_dma_sg, get_dma_residue);
281 GLOBAL_ALIAS(set_dma_speed, get_dma_residue);
282
283 #endif
284
285 EXPORT_SYMBOL(request_dma);
286 EXPORT_SYMBOL(free_dma);
287 EXPORT_SYMBOL(enable_dma);
288 EXPORT_SYMBOL(disable_dma);
289 EXPORT_SYMBOL(__set_dma_addr);
290 EXPORT_SYMBOL(set_dma_count);
291 EXPORT_SYMBOL(set_dma_mode);
292 EXPORT_SYMBOL(set_dma_page);
293 EXPORT_SYMBOL(get_dma_residue);
294 EXPORT_SYMBOL(set_dma_sg);
295 EXPORT_SYMBOL(set_dma_speed);
296
297 EXPORT_SYMBOL(dma_spin_lock);