MIPS: SB1250: Include correct header and fix a warning
[linux-2.6/linux-mips.git] / arch / arm / kernel / dma.c
blob7d5b9fb01e71d9d5b9e8dab4e62d3302fd3fde79
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/init.h>
16 #include <linux/spinlock.h>
17 #include <linux/errno.h>
18 #include <linux/scatterlist.h>
20 #include <asm/dma.h>
22 #include <asm/mach/dma.h>
24 DEFINE_SPINLOCK(dma_spin_lock);
25 EXPORT_SYMBOL(dma_spin_lock);
27 static dma_t *dma_chan[MAX_DMA_CHANNELS];
29 static inline dma_t *dma_channel(unsigned int chan)
31 if (chan >= MAX_DMA_CHANNELS)
32 return NULL;
34 return dma_chan[chan];
37 int __init isa_dma_add(unsigned int chan, dma_t *dma)
39 if (!dma->d_ops)
40 return -EINVAL;
42 sg_init_table(&dma->buf, 1);
44 if (dma_chan[chan])
45 return -EBUSY;
46 dma_chan[chan] = dma;
47 return 0;
51 * Request DMA channel
53 * On certain platforms, we have to allocate an interrupt as well...
55 int request_dma(unsigned int chan, const char *device_id)
57 dma_t *dma = dma_channel(chan);
58 int ret;
60 if (!dma)
61 goto bad_dma;
63 if (xchg(&dma->lock, 1) != 0)
64 goto busy;
66 dma->device_id = device_id;
67 dma->active = 0;
68 dma->invalid = 1;
70 ret = 0;
71 if (dma->d_ops->request)
72 ret = dma->d_ops->request(chan, dma);
74 if (ret)
75 xchg(&dma->lock, 0);
77 return ret;
79 bad_dma:
80 printk(KERN_ERR "dma: trying to allocate DMA%d\n", chan);
81 return -EINVAL;
83 busy:
84 return -EBUSY;
86 EXPORT_SYMBOL(request_dma);
89 * Free DMA channel
91 * On certain platforms, we have to free interrupt as well...
93 void free_dma(unsigned int chan)
95 dma_t *dma = dma_channel(chan);
97 if (!dma)
98 goto bad_dma;
100 if (dma->active) {
101 printk(KERN_ERR "dma%d: freeing active DMA\n", chan);
102 dma->d_ops->disable(chan, dma);
103 dma->active = 0;
106 if (xchg(&dma->lock, 0) != 0) {
107 if (dma->d_ops->free)
108 dma->d_ops->free(chan, dma);
109 return;
112 printk(KERN_ERR "dma%d: trying to free free DMA\n", chan);
113 return;
115 bad_dma:
116 printk(KERN_ERR "dma: trying to free DMA%d\n", chan);
118 EXPORT_SYMBOL(free_dma);
120 /* Set DMA Scatter-Gather list
122 void set_dma_sg (unsigned int chan, struct scatterlist *sg, int nr_sg)
124 dma_t *dma = dma_channel(chan);
126 if (dma->active)
127 printk(KERN_ERR "dma%d: altering DMA SG while "
128 "DMA active\n", chan);
130 dma->sg = sg;
131 dma->sgcount = nr_sg;
132 dma->invalid = 1;
134 EXPORT_SYMBOL(set_dma_sg);
136 /* Set DMA address
138 * Copy address to the structure, and set the invalid bit
140 void __set_dma_addr (unsigned int chan, void *addr)
142 dma_t *dma = dma_channel(chan);
144 if (dma->active)
145 printk(KERN_ERR "dma%d: altering DMA address while "
146 "DMA active\n", chan);
148 dma->sg = NULL;
149 dma->addr = addr;
150 dma->invalid = 1;
152 EXPORT_SYMBOL(__set_dma_addr);
154 /* Set DMA byte count
156 * Copy address to the structure, and set the invalid bit
158 void set_dma_count (unsigned int chan, unsigned long count)
160 dma_t *dma = dma_channel(chan);
162 if (dma->active)
163 printk(KERN_ERR "dma%d: altering DMA count while "
164 "DMA active\n", chan);
166 dma->sg = NULL;
167 dma->count = count;
168 dma->invalid = 1;
170 EXPORT_SYMBOL(set_dma_count);
172 /* Set DMA direction mode
174 void set_dma_mode (unsigned int chan, unsigned int mode)
176 dma_t *dma = dma_channel(chan);
178 if (dma->active)
179 printk(KERN_ERR "dma%d: altering DMA mode while "
180 "DMA active\n", chan);
182 dma->dma_mode = mode;
183 dma->invalid = 1;
185 EXPORT_SYMBOL(set_dma_mode);
187 /* Enable DMA channel
189 void enable_dma (unsigned int chan)
191 dma_t *dma = dma_channel(chan);
193 if (!dma->lock)
194 goto free_dma;
196 if (dma->active == 0) {
197 dma->active = 1;
198 dma->d_ops->enable(chan, dma);
200 return;
202 free_dma:
203 printk(KERN_ERR "dma%d: trying to enable free DMA\n", chan);
204 BUG();
206 EXPORT_SYMBOL(enable_dma);
208 /* Disable DMA channel
210 void disable_dma (unsigned int chan)
212 dma_t *dma = dma_channel(chan);
214 if (!dma->lock)
215 goto free_dma;
217 if (dma->active == 1) {
218 dma->active = 0;
219 dma->d_ops->disable(chan, dma);
221 return;
223 free_dma:
224 printk(KERN_ERR "dma%d: trying to disable free DMA\n", chan);
225 BUG();
227 EXPORT_SYMBOL(disable_dma);
230 * Is the specified DMA channel active?
232 int dma_channel_active(unsigned int chan)
234 dma_t *dma = dma_channel(chan);
235 return dma->active;
237 EXPORT_SYMBOL(dma_channel_active);
239 void set_dma_page(unsigned int chan, char pagenr)
241 printk(KERN_ERR "dma%d: trying to set_dma_page\n", chan);
243 EXPORT_SYMBOL(set_dma_page);
245 void set_dma_speed(unsigned int chan, int cycle_ns)
247 dma_t *dma = dma_channel(chan);
248 int ret = 0;
250 if (dma->d_ops->setspeed)
251 ret = dma->d_ops->setspeed(chan, dma, cycle_ns);
252 dma->speed = ret;
254 EXPORT_SYMBOL(set_dma_speed);
256 int get_dma_residue(unsigned int chan)
258 dma_t *dma = dma_channel(chan);
259 int ret = 0;
261 if (dma->d_ops->residue)
262 ret = dma->d_ops->residue(chan, dma);
264 return ret;
266 EXPORT_SYMBOL(get_dma_residue);