Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / arch / arm / kernel / dma.c
blobad311020f081e232ff730b76e9fc59391b69f7a6
1 /*
2 * linux/arch/arm/kernel/dma.c
4 * Copyright (C) 1995-2000 Russell King
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.
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.
14 #include <linux/module.h>
15 #include <linux/malloc.h>
16 #include <linux/sched.h>
17 #include <linux/mman.h>
18 #include <linux/init.h>
19 #include <linux/spinlock.h>
21 #include <asm/dma.h>
23 #include <asm/mach/dma.h>
25 spinlock_t dma_spin_lock = SPIN_LOCK_UNLOCKED;
27 #if MAX_DMA_CHANNELS > 0
29 static dma_t dma_chan[MAX_DMA_CHANNELS];
32 * Get dma list for /proc/dma
34 int get_dma_list(char *buf)
36 dma_t *dma;
37 char *p = buf;
38 int i;
40 for (i = 0, dma = dma_chan; i < MAX_DMA_CHANNELS; i++, dma++)
41 if (dma->lock)
42 p += sprintf(p, "%2d: %14s %s\n", i,
43 dma->d_ops->type, dma->device_id);
45 return p - buf;
49 * Request DMA channel
51 * On certain platforms, we have to allocate an interrupt as well...
53 int request_dma(dmach_t channel, const char *device_id)
55 dma_t *dma = dma_chan + channel;
56 int ret;
58 if (channel >= MAX_DMA_CHANNELS || !dma->d_ops)
59 goto bad_dma;
61 if (xchg(&dma->lock, 1) != 0)
62 goto busy;
64 dma->device_id = device_id;
65 dma->active = 0;
66 dma->invalid = 1;
68 ret = 0;
69 if (dma->d_ops->request)
70 ret = dma->d_ops->request(channel, dma);
72 if (ret)
73 xchg(&dma->lock, 0);
75 return ret;
77 bad_dma:
78 printk(KERN_ERR "dma: trying to allocate DMA%d\n", channel);
79 return -EINVAL;
81 busy:
82 return -EBUSY;
86 * Free DMA channel
88 * On certain platforms, we have to free interrupt as well...
90 void free_dma(dmach_t channel)
92 dma_t *dma = dma_chan + channel;
94 if (channel >= MAX_DMA_CHANNELS || !dma->d_ops)
95 goto bad_dma;
97 if (dma->active) {
98 printk(KERN_ERR "dma%d: freeing active DMA\n", channel);
99 dma->d_ops->disable(channel, dma);
100 dma->active = 0;
103 if (xchg(&dma->lock, 0) != 0) {
104 if (dma->d_ops->free)
105 dma->d_ops->free(channel, dma);
106 return;
109 printk(KERN_ERR "dma%d: trying to free free DMA\n", channel);
110 return;
112 bad_dma:
113 printk(KERN_ERR "dma: trying to free DMA%d\n", channel);
116 /* Set DMA Scatter-Gather list
118 void set_dma_sg (dmach_t channel, struct scatterlist *sg, int nr_sg)
120 dma_t *dma = dma_chan + channel;
122 dma->sg = sg;
123 dma->sgcount = nr_sg;
124 dma->using_sg = 1;
125 dma->invalid = 1;
128 /* Set DMA address
130 * Copy address to the structure, and set the invalid bit
132 void set_dma_addr (dmach_t channel, unsigned long physaddr)
134 dma_t *dma = dma_chan + channel;
136 if (dma->active)
137 printk(KERN_ERR "dma%d: altering DMA address while "
138 "DMA active\n", channel);
140 dma->sg = &dma->buf;
141 dma->sgcount = 1;
142 dma->buf.address = bus_to_virt(physaddr);
143 dma->using_sg = 0;
144 dma->invalid = 1;
147 /* Set DMA byte count
149 * Copy address to the structure, and set the invalid bit
151 void set_dma_count (dmach_t channel, unsigned long count)
153 dma_t *dma = dma_chan + channel;
155 if (dma->active)
156 printk(KERN_ERR "dma%d: altering DMA count while "
157 "DMA active\n", channel);
159 dma->sg = &dma->buf;
160 dma->sgcount = 1;
161 dma->buf.length = count;
162 dma->using_sg = 0;
163 dma->invalid = 1;
166 /* Set DMA direction mode
168 void set_dma_mode (dmach_t channel, dmamode_t mode)
170 dma_t *dma = dma_chan + channel;
172 if (dma->active)
173 printk(KERN_ERR "dma%d: altering DMA mode while "
174 "DMA active\n", channel);
176 dma->dma_mode = mode;
177 dma->invalid = 1;
180 /* Enable DMA channel
182 void enable_dma (dmach_t channel)
184 dma_t *dma = dma_chan + channel;
186 if (!dma->lock)
187 goto free_dma;
189 if (dma->active == 0) {
190 dma->active = 1;
191 dma->d_ops->enable(channel, dma);
193 return;
195 free_dma:
196 printk(KERN_ERR "dma%d: trying to enable free DMA\n", channel);
197 BUG();
200 /* Disable DMA channel
202 void disable_dma (dmach_t channel)
204 dma_t *dma = dma_chan + channel;
206 if (!dma->lock)
207 goto free_dma;
209 if (dma->active == 1) {
210 dma->active = 0;
211 dma->d_ops->disable(channel, dma);
213 return;
215 free_dma:
216 printk(KERN_ERR "dma%d: trying to disable free DMA\n", channel);
217 BUG();
220 void set_dma_page(dmach_t channel, char pagenr)
222 printk(KERN_ERR "dma%d: trying to set_dma_page\n", channel);
225 void set_dma_speed(dmach_t channel, int cycle_ns)
227 dma_t *dma = dma_chan + channel;
228 int ret = 0;
230 if (dma->d_ops->setspeed)
231 ret = dma->d_ops->setspeed(channel, dma, cycle_ns);
232 dma->speed = ret;
235 int get_dma_residue(dmach_t channel)
237 dma_t *dma = dma_chan + channel;
238 int ret = 0;
240 if (dma->d_ops->residue)
241 ret = dma->d_ops->residue(channel, dma);
243 return ret;
246 void __init init_dma(void)
248 arch_dma_init(dma_chan);
251 #else
253 int request_dma(dmach_t channel, const char *device_id)
255 return -EINVAL;
258 int get_dma_residue(dmach_t channel)
260 return 0;
263 #define GLOBAL_ALIAS(_a,_b) asm (".set " #_a "," #_b "; .globl " #_a)
264 GLOBAL_ALIAS(disable_dma, get_dma_residue);
265 GLOBAL_ALIAS(enable_dma, get_dma_residue);
266 GLOBAL_ALIAS(free_dma, get_dma_residue);
267 GLOBAL_ALIAS(get_dma_list, get_dma_residue);
268 GLOBAL_ALIAS(set_dma_mode, get_dma_residue);
269 GLOBAL_ALIAS(set_dma_page, get_dma_residue);
270 GLOBAL_ALIAS(set_dma_count, get_dma_residue);
271 GLOBAL_ALIAS(set_dma_addr, get_dma_residue);
272 GLOBAL_ALIAS(set_dma_sg, get_dma_residue);
273 GLOBAL_ALIAS(set_dma_speed, get_dma_residue);
274 GLOBAL_ALIAS(init_dma, get_dma_residue);
276 #endif
278 EXPORT_SYMBOL(enable_dma);
279 EXPORT_SYMBOL(disable_dma);
280 EXPORT_SYMBOL(set_dma_addr);
281 EXPORT_SYMBOL(set_dma_count);
282 EXPORT_SYMBOL(set_dma_mode);
283 EXPORT_SYMBOL(set_dma_page);
284 EXPORT_SYMBOL(get_dma_residue);
285 EXPORT_SYMBOL(set_dma_sg);
286 EXPORT_SYMBOL(set_dma_speed);