Linux 2.6.25-rc4
[linux-2.6/next.git] / arch / arm / kernel / dma.c
blobba99a20355236a06491922a63e0ef745f0bcfcc5
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>
19 #include <asm/dma.h>
21 #include <asm/mach/dma.h>
23 DEFINE_SPINLOCK(dma_spin_lock);
24 EXPORT_SYMBOL(dma_spin_lock);
26 static dma_t dma_chan[MAX_DMA_CHANNELS];
29 * Get dma list for /proc/dma
31 int get_dma_list(char *buf)
33 dma_t *dma;
34 char *p = buf;
35 int i;
37 for (i = 0, dma = dma_chan; i < MAX_DMA_CHANNELS; i++, dma++)
38 if (dma->lock)
39 p += sprintf(p, "%2d: %14s %s\n", i,
40 dma->d_ops->type, dma->device_id);
42 return p - buf;
46 * Request DMA channel
48 * On certain platforms, we have to allocate an interrupt as well...
50 int request_dma(dmach_t channel, const char *device_id)
52 dma_t *dma = dma_chan + channel;
53 int ret;
55 if (channel >= MAX_DMA_CHANNELS || !dma->d_ops)
56 goto bad_dma;
58 if (xchg(&dma->lock, 1) != 0)
59 goto busy;
61 dma->device_id = device_id;
62 dma->active = 0;
63 dma->invalid = 1;
65 ret = 0;
66 if (dma->d_ops->request)
67 ret = dma->d_ops->request(channel, dma);
69 if (ret)
70 xchg(&dma->lock, 0);
72 return ret;
74 bad_dma:
75 printk(KERN_ERR "dma: trying to allocate DMA%d\n", channel);
76 return -EINVAL;
78 busy:
79 return -EBUSY;
81 EXPORT_SYMBOL(request_dma);
84 * Free DMA channel
86 * On certain platforms, we have to free interrupt as well...
88 void free_dma(dmach_t channel)
90 dma_t *dma = dma_chan + channel;
92 if (channel >= MAX_DMA_CHANNELS || !dma->d_ops)
93 goto bad_dma;
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;
101 if (xchg(&dma->lock, 0) != 0) {
102 if (dma->d_ops->free)
103 dma->d_ops->free(channel, dma);
104 return;
107 printk(KERN_ERR "dma%d: trying to free free DMA\n", channel);
108 return;
110 bad_dma:
111 printk(KERN_ERR "dma: trying to free DMA%d\n", channel);
113 EXPORT_SYMBOL(free_dma);
115 /* Set DMA Scatter-Gather list
117 void set_dma_sg (dmach_t channel, struct scatterlist *sg, int nr_sg)
119 dma_t *dma = dma_chan + channel;
121 if (dma->active)
122 printk(KERN_ERR "dma%d: altering DMA SG while "
123 "DMA active\n", channel);
125 dma->sg = sg;
126 dma->sgcount = nr_sg;
127 dma->invalid = 1;
129 EXPORT_SYMBOL(set_dma_sg);
131 /* Set DMA address
133 * Copy address to the structure, and set the invalid bit
135 void __set_dma_addr (dmach_t channel, void *addr)
137 dma_t *dma = dma_chan + channel;
139 if (dma->active)
140 printk(KERN_ERR "dma%d: altering DMA address while "
141 "DMA active\n", channel);
143 dma->sg = NULL;
144 dma->addr = addr;
145 dma->invalid = 1;
147 EXPORT_SYMBOL(__set_dma_addr);
149 /* Set DMA byte count
151 * Copy address to the structure, and set the invalid bit
153 void set_dma_count (dmach_t channel, unsigned long count)
155 dma_t *dma = dma_chan + channel;
157 if (dma->active)
158 printk(KERN_ERR "dma%d: altering DMA count while "
159 "DMA active\n", channel);
161 dma->sg = NULL;
162 dma->count = count;
163 dma->invalid = 1;
165 EXPORT_SYMBOL(set_dma_count);
167 /* Set DMA direction mode
169 void set_dma_mode (dmach_t channel, dmamode_t mode)
171 dma_t *dma = dma_chan + channel;
173 if (dma->active)
174 printk(KERN_ERR "dma%d: altering DMA mode while "
175 "DMA active\n", channel);
177 dma->dma_mode = mode;
178 dma->invalid = 1;
180 EXPORT_SYMBOL(set_dma_mode);
182 /* Enable DMA channel
184 void enable_dma (dmach_t channel)
186 dma_t *dma = dma_chan + channel;
188 if (!dma->lock)
189 goto free_dma;
191 if (dma->active == 0) {
192 dma->active = 1;
193 dma->d_ops->enable(channel, dma);
195 return;
197 free_dma:
198 printk(KERN_ERR "dma%d: trying to enable free DMA\n", channel);
199 BUG();
201 EXPORT_SYMBOL(enable_dma);
203 /* Disable DMA channel
205 void disable_dma (dmach_t channel)
207 dma_t *dma = dma_chan + channel;
209 if (!dma->lock)
210 goto free_dma;
212 if (dma->active == 1) {
213 dma->active = 0;
214 dma->d_ops->disable(channel, dma);
216 return;
218 free_dma:
219 printk(KERN_ERR "dma%d: trying to disable free DMA\n", channel);
220 BUG();
222 EXPORT_SYMBOL(disable_dma);
225 * Is the specified DMA channel active?
227 int dma_channel_active(dmach_t channel)
229 return dma_chan[channel].active;
231 EXPORT_SYMBOL(dma_channel_active);
233 void set_dma_page(dmach_t channel, char pagenr)
235 printk(KERN_ERR "dma%d: trying to set_dma_page\n", channel);
237 EXPORT_SYMBOL(set_dma_page);
239 void set_dma_speed(dmach_t channel, int cycle_ns)
241 dma_t *dma = dma_chan + channel;
242 int ret = 0;
244 if (dma->d_ops->setspeed)
245 ret = dma->d_ops->setspeed(channel, dma, cycle_ns);
246 dma->speed = ret;
248 EXPORT_SYMBOL(set_dma_speed);
250 int get_dma_residue(dmach_t channel)
252 dma_t *dma = dma_chan + channel;
253 int ret = 0;
255 if (dma->d_ops->residue)
256 ret = dma->d_ops->residue(channel, dma);
258 return ret;
260 EXPORT_SYMBOL(get_dma_residue);
262 static int __init init_dma(void)
264 arch_dma_init(dma_chan);
265 return 0;
268 core_initcall(init_dma);