blob: 7b829d9663b1b05b624e1ceda81b927b7a529c36 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/init.h>
16#include <linux/spinlock.h>
17#include <linux/errno.h>
Russell Kingd6675222008-12-08 17:50:25 +000018#include <linux/scatterlist.h>
Russell Kinge193ba22009-12-24 18:32:13 +000019#include <linux/seq_file.h>
20#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include <asm/dma.h>
23
24#include <asm/mach/dma.h>
25
Thomas Gleixnerbd31b852009-07-03 08:44:46 -050026DEFINE_RAW_SPINLOCK(dma_spin_lock);
Russell Kingd7b4a752006-01-04 15:52:45 +000027EXPORT_SYMBOL(dma_spin_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Russell King2f757f22008-12-08 16:33:30 +000029static dma_t *dma_chan[MAX_DMA_CHANNELS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Russell King3afb6e9c2008-12-08 16:08:48 +000031static inline dma_t *dma_channel(unsigned int chan)
32{
Russell King2f757f22008-12-08 16:33:30 +000033 if (chan >= MAX_DMA_CHANNELS)
Russell King3afb6e9c2008-12-08 16:08:48 +000034 return NULL;
35
Russell King2f757f22008-12-08 16:33:30 +000036 return dma_chan[chan];
37}
38
39int __init isa_dma_add(unsigned int chan, dma_t *dma)
40{
41 if (!dma->d_ops)
42 return -EINVAL;
Russell Kingd6675222008-12-08 17:50:25 +000043
44 sg_init_table(&dma->buf, 1);
45
Russell King2f757f22008-12-08 16:33:30 +000046 if (dma_chan[chan])
47 return -EBUSY;
48 dma_chan[chan] = dma;
49 return 0;
Russell King3afb6e9c2008-12-08 16:08:48 +000050}
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 * Request DMA channel
54 *
55 * On certain platforms, we have to allocate an interrupt as well...
56 */
Russell King1df81302008-12-08 15:58:50 +000057int request_dma(unsigned int chan, const char *device_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Russell King3afb6e9c2008-12-08 16:08:48 +000059 dma_t *dma = dma_channel(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 int ret;
61
Russell King3afb6e9c2008-12-08 16:08:48 +000062 if (!dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 goto bad_dma;
64
65 if (xchg(&dma->lock, 1) != 0)
66 goto busy;
67
68 dma->device_id = device_id;
69 dma->active = 0;
70 dma->invalid = 1;
71
72 ret = 0;
73 if (dma->d_ops->request)
Russell King1df81302008-12-08 15:58:50 +000074 ret = dma->d_ops->request(chan, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 if (ret)
77 xchg(&dma->lock, 0);
78
79 return ret;
80
81bad_dma:
Russell King1df81302008-12-08 15:58:50 +000082 printk(KERN_ERR "dma: trying to allocate DMA%d\n", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return -EINVAL;
84
85busy:
86 return -EBUSY;
87}
Russell Kingd7b4a752006-01-04 15:52:45 +000088EXPORT_SYMBOL(request_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90/*
91 * Free DMA channel
92 *
93 * On certain platforms, we have to free interrupt as well...
94 */
Russell King1df81302008-12-08 15:58:50 +000095void free_dma(unsigned int chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Russell King3afb6e9c2008-12-08 16:08:48 +000097 dma_t *dma = dma_channel(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Russell King3afb6e9c2008-12-08 16:08:48 +000099 if (!dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 goto bad_dma;
101
102 if (dma->active) {
Russell King1df81302008-12-08 15:58:50 +0000103 printk(KERN_ERR "dma%d: freeing active DMA\n", chan);
104 dma->d_ops->disable(chan, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 dma->active = 0;
106 }
107
108 if (xchg(&dma->lock, 0) != 0) {
109 if (dma->d_ops->free)
Russell King1df81302008-12-08 15:58:50 +0000110 dma->d_ops->free(chan, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return;
112 }
113
Russell King1df81302008-12-08 15:58:50 +0000114 printk(KERN_ERR "dma%d: trying to free free DMA\n", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return;
116
117bad_dma:
Russell King1df81302008-12-08 15:58:50 +0000118 printk(KERN_ERR "dma: trying to free DMA%d\n", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
Russell Kingd7b4a752006-01-04 15:52:45 +0000120EXPORT_SYMBOL(free_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122/* Set DMA Scatter-Gather list
123 */
Russell King1df81302008-12-08 15:58:50 +0000124void set_dma_sg (unsigned int chan, struct scatterlist *sg, int nr_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Russell King3afb6e9c2008-12-08 16:08:48 +0000126 dma_t *dma = dma_channel(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 if (dma->active)
129 printk(KERN_ERR "dma%d: altering DMA SG while "
Russell King1df81302008-12-08 15:58:50 +0000130 "DMA active\n", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 dma->sg = sg;
133 dma->sgcount = nr_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 dma->invalid = 1;
135}
Russell Kingd7b4a752006-01-04 15:52:45 +0000136EXPORT_SYMBOL(set_dma_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138/* Set DMA address
139 *
140 * Copy address to the structure, and set the invalid bit
141 */
Russell King1df81302008-12-08 15:58:50 +0000142void __set_dma_addr (unsigned int chan, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Russell King3afb6e9c2008-12-08 16:08:48 +0000144 dma_t *dma = dma_channel(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 if (dma->active)
147 printk(KERN_ERR "dma%d: altering DMA address while "
Russell King1df81302008-12-08 15:58:50 +0000148 "DMA active\n", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Russell King7cdad482006-01-04 15:08:30 +0000150 dma->sg = NULL;
151 dma->addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 dma->invalid = 1;
153}
Russell Kingd7b4a752006-01-04 15:52:45 +0000154EXPORT_SYMBOL(__set_dma_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156/* Set DMA byte count
157 *
158 * Copy address to the structure, and set the invalid bit
159 */
Russell King1df81302008-12-08 15:58:50 +0000160void set_dma_count (unsigned int chan, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Russell King3afb6e9c2008-12-08 16:08:48 +0000162 dma_t *dma = dma_channel(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 if (dma->active)
165 printk(KERN_ERR "dma%d: altering DMA count while "
Russell King1df81302008-12-08 15:58:50 +0000166 "DMA active\n", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Russell King7cdad482006-01-04 15:08:30 +0000168 dma->sg = NULL;
169 dma->count = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 dma->invalid = 1;
171}
Russell Kingd7b4a752006-01-04 15:52:45 +0000172EXPORT_SYMBOL(set_dma_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174/* Set DMA direction mode
175 */
Russell Kingf0ffc812009-01-02 12:34:55 +0000176void set_dma_mode (unsigned int chan, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Russell King3afb6e9c2008-12-08 16:08:48 +0000178 dma_t *dma = dma_channel(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 if (dma->active)
181 printk(KERN_ERR "dma%d: altering DMA mode while "
Russell King1df81302008-12-08 15:58:50 +0000182 "DMA active\n", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 dma->dma_mode = mode;
185 dma->invalid = 1;
186}
Russell Kingd7b4a752006-01-04 15:52:45 +0000187EXPORT_SYMBOL(set_dma_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189/* Enable DMA channel
190 */
Russell King1df81302008-12-08 15:58:50 +0000191void enable_dma (unsigned int chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Russell King3afb6e9c2008-12-08 16:08:48 +0000193 dma_t *dma = dma_channel(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 if (!dma->lock)
196 goto free_dma;
197
198 if (dma->active == 0) {
199 dma->active = 1;
Russell King1df81302008-12-08 15:58:50 +0000200 dma->d_ops->enable(chan, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202 return;
203
204free_dma:
Russell King1df81302008-12-08 15:58:50 +0000205 printk(KERN_ERR "dma%d: trying to enable free DMA\n", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 BUG();
207}
Russell Kingd7b4a752006-01-04 15:52:45 +0000208EXPORT_SYMBOL(enable_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210/* Disable DMA channel
211 */
Russell King1df81302008-12-08 15:58:50 +0000212void disable_dma (unsigned int chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Russell King3afb6e9c2008-12-08 16:08:48 +0000214 dma_t *dma = dma_channel(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 if (!dma->lock)
217 goto free_dma;
218
219 if (dma->active == 1) {
220 dma->active = 0;
Russell King1df81302008-12-08 15:58:50 +0000221 dma->d_ops->disable(chan, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223 return;
224
225free_dma:
Russell King1df81302008-12-08 15:58:50 +0000226 printk(KERN_ERR "dma%d: trying to disable free DMA\n", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 BUG();
228}
Russell Kingd7b4a752006-01-04 15:52:45 +0000229EXPORT_SYMBOL(disable_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231/*
232 * Is the specified DMA channel active?
233 */
Russell King1df81302008-12-08 15:58:50 +0000234int dma_channel_active(unsigned int chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Russell King3afb6e9c2008-12-08 16:08:48 +0000236 dma_t *dma = dma_channel(chan);
237 return dma->active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
Russell Kingec14d792007-03-31 21:36:53 +0100239EXPORT_SYMBOL(dma_channel_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Russell King1df81302008-12-08 15:58:50 +0000241void set_dma_page(unsigned int chan, char pagenr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Russell King1df81302008-12-08 15:58:50 +0000243 printk(KERN_ERR "dma%d: trying to set_dma_page\n", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
Russell Kingd7b4a752006-01-04 15:52:45 +0000245EXPORT_SYMBOL(set_dma_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Russell King1df81302008-12-08 15:58:50 +0000247void set_dma_speed(unsigned int chan, int cycle_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Russell King3afb6e9c2008-12-08 16:08:48 +0000249 dma_t *dma = dma_channel(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 int ret = 0;
251
252 if (dma->d_ops->setspeed)
Russell King1df81302008-12-08 15:58:50 +0000253 ret = dma->d_ops->setspeed(chan, dma, cycle_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 dma->speed = ret;
255}
Russell Kingd7b4a752006-01-04 15:52:45 +0000256EXPORT_SYMBOL(set_dma_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Russell King1df81302008-12-08 15:58:50 +0000258int get_dma_residue(unsigned int chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Russell King3afb6e9c2008-12-08 16:08:48 +0000260 dma_t *dma = dma_channel(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 int ret = 0;
262
263 if (dma->d_ops->residue)
Russell King1df81302008-12-08 15:58:50 +0000264 ret = dma->d_ops->residue(chan, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 return ret;
267}
Russell Kingd7b4a752006-01-04 15:52:45 +0000268EXPORT_SYMBOL(get_dma_residue);
Russell Kinge193ba22009-12-24 18:32:13 +0000269
270#ifdef CONFIG_PROC_FS
271static int proc_dma_show(struct seq_file *m, void *v)
272{
273 int i;
274
275 for (i = 0 ; i < MAX_DMA_CHANNELS ; i++) {
276 dma_t *dma = dma_channel(i);
277 if (dma && dma->lock)
278 seq_printf(m, "%2d: %s\n", i, dma->device_id);
279 }
280 return 0;
281}
282
283static int proc_dma_open(struct inode *inode, struct file *file)
284{
285 return single_open(file, proc_dma_show, NULL);
286}
287
288static const struct file_operations proc_dma_operations = {
289 .open = proc_dma_open,
290 .read = seq_read,
291 .llseek = seq_lseek,
292 .release = single_release,
293};
294
295static int __init proc_dma_init(void)
296{
297 proc_create("dma", 0, NULL, &proc_dma_operations);
298 return 0;
299}
300
301__initcall(proc_dma_init);
302#endif