blob: ba15b86664984bf4f7eb76f3c736143acb44b57d [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/arch/arm/kernel/dma.c
4 *
5 * Copyright (C) 1995-2000 Russell King
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Front-end to the DMA handling. This handles the allocation/freeing
8 * of DMA channels, and provides a unified interface to the machines
9 * DMA facilities.
10 */
11#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/init.h>
13#include <linux/spinlock.h>
14#include <linux/errno.h>
Russell Kingd6675222008-12-08 17:50:25 +000015#include <linux/scatterlist.h>
Russell Kinge193ba22009-12-24 18:32:13 +000016#include <linux/seq_file.h>
17#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <asm/dma.h>
20
21#include <asm/mach/dma.h>
22
Thomas Gleixnerbd31b852009-07-03 08:44:46 -050023DEFINE_RAW_SPINLOCK(dma_spin_lock);
Russell Kingd7b4a752006-01-04 15:52:45 +000024EXPORT_SYMBOL(dma_spin_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Russell King2f757f22008-12-08 16:33:30 +000026static dma_t *dma_chan[MAX_DMA_CHANNELS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Russell King3afb6e9c2008-12-08 16:08:48 +000028static inline dma_t *dma_channel(unsigned int chan)
29{
Russell King2f757f22008-12-08 16:33:30 +000030 if (chan >= MAX_DMA_CHANNELS)
Russell King3afb6e9c2008-12-08 16:08:48 +000031 return NULL;
32
Russell King2f757f22008-12-08 16:33:30 +000033 return dma_chan[chan];
34}
35
36int __init isa_dma_add(unsigned int chan, dma_t *dma)
37{
38 if (!dma->d_ops)
39 return -EINVAL;
Russell Kingd6675222008-12-08 17:50:25 +000040
41 sg_init_table(&dma->buf, 1);
42
Russell King2f757f22008-12-08 16:33:30 +000043 if (dma_chan[chan])
44 return -EBUSY;
45 dma_chan[chan] = dma;
46 return 0;
Russell King3afb6e9c2008-12-08 16:08:48 +000047}
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * Request DMA channel
51 *
52 * On certain platforms, we have to allocate an interrupt as well...
53 */
Russell King1df81302008-12-08 15:58:50 +000054int request_dma(unsigned int chan, const char *device_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Russell King3afb6e9c2008-12-08 16:08:48 +000056 dma_t *dma = dma_channel(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 int ret;
58
Russell King3afb6e9c2008-12-08 16:08:48 +000059 if (!dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 goto bad_dma;
61
62 if (xchg(&dma->lock, 1) != 0)
63 goto busy;
64
65 dma->device_id = device_id;
66 dma->active = 0;
67 dma->invalid = 1;
68
69 ret = 0;
70 if (dma->d_ops->request)
Russell King1df81302008-12-08 15:58:50 +000071 ret = dma->d_ops->request(chan, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 if (ret)
74 xchg(&dma->lock, 0);
75
76 return ret;
77
78bad_dma:
Russell King4ed89f22014-10-28 11:26:42 +000079 pr_err("dma: trying to allocate DMA%d\n", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return -EINVAL;
81
82busy:
83 return -EBUSY;
84}
Russell Kingd7b4a752006-01-04 15:52:45 +000085EXPORT_SYMBOL(request_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/*
88 * Free DMA channel
89 *
90 * On certain platforms, we have to free interrupt as well...
91 */
Russell King1df81302008-12-08 15:58:50 +000092void free_dma(unsigned int chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Russell King3afb6e9c2008-12-08 16:08:48 +000094 dma_t *dma = dma_channel(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Russell King3afb6e9c2008-12-08 16:08:48 +000096 if (!dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 goto bad_dma;
98
99 if (dma->active) {
Russell King4ed89f22014-10-28 11:26:42 +0000100 pr_err("dma%d: freeing active DMA\n", chan);
Russell King1df81302008-12-08 15:58:50 +0000101 dma->d_ops->disable(chan, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 dma->active = 0;
103 }
104
105 if (xchg(&dma->lock, 0) != 0) {
106 if (dma->d_ops->free)
Russell King1df81302008-12-08 15:58:50 +0000107 dma->d_ops->free(chan, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return;
109 }
110
Russell King4ed89f22014-10-28 11:26:42 +0000111 pr_err("dma%d: trying to free free DMA\n", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return;
113
114bad_dma:
Russell King4ed89f22014-10-28 11:26:42 +0000115 pr_err("dma: trying to free DMA%d\n", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
Russell Kingd7b4a752006-01-04 15:52:45 +0000117EXPORT_SYMBOL(free_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119/* Set DMA Scatter-Gather list
120 */
Russell King1df81302008-12-08 15:58:50 +0000121void set_dma_sg (unsigned int chan, struct scatterlist *sg, int nr_sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Russell King3afb6e9c2008-12-08 16:08:48 +0000123 dma_t *dma = dma_channel(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 if (dma->active)
Russell King4ed89f22014-10-28 11:26:42 +0000126 pr_err("dma%d: altering DMA SG while DMA active\n", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 dma->sg = sg;
129 dma->sgcount = nr_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 dma->invalid = 1;
131}
Russell Kingd7b4a752006-01-04 15:52:45 +0000132EXPORT_SYMBOL(set_dma_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134/* Set DMA address
135 *
136 * Copy address to the structure, and set the invalid bit
137 */
Russell King1df81302008-12-08 15:58:50 +0000138void __set_dma_addr (unsigned int chan, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Russell King3afb6e9c2008-12-08 16:08:48 +0000140 dma_t *dma = dma_channel(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 if (dma->active)
Russell King4ed89f22014-10-28 11:26:42 +0000143 pr_err("dma%d: altering DMA address while DMA active\n", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Russell King7cdad482006-01-04 15:08:30 +0000145 dma->sg = NULL;
146 dma->addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 dma->invalid = 1;
148}
Russell Kingd7b4a752006-01-04 15:52:45 +0000149EXPORT_SYMBOL(__set_dma_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151/* Set DMA byte count
152 *
153 * Copy address to the structure, and set the invalid bit
154 */
Russell King1df81302008-12-08 15:58:50 +0000155void set_dma_count (unsigned int chan, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Russell King3afb6e9c2008-12-08 16:08:48 +0000157 dma_t *dma = dma_channel(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 if (dma->active)
Russell King4ed89f22014-10-28 11:26:42 +0000160 pr_err("dma%d: altering DMA count while DMA active\n", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Russell King7cdad482006-01-04 15:08:30 +0000162 dma->sg = NULL;
163 dma->count = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 dma->invalid = 1;
165}
Russell Kingd7b4a752006-01-04 15:52:45 +0000166EXPORT_SYMBOL(set_dma_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168/* Set DMA direction mode
169 */
Russell Kingf0ffc812009-01-02 12:34:55 +0000170void set_dma_mode (unsigned int chan, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Russell King3afb6e9c2008-12-08 16:08:48 +0000172 dma_t *dma = dma_channel(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 if (dma->active)
Russell King4ed89f22014-10-28 11:26:42 +0000175 pr_err("dma%d: altering DMA mode while DMA active\n", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 dma->dma_mode = mode;
178 dma->invalid = 1;
179}
Russell Kingd7b4a752006-01-04 15:52:45 +0000180EXPORT_SYMBOL(set_dma_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182/* Enable DMA channel
183 */
Russell King1df81302008-12-08 15:58:50 +0000184void enable_dma (unsigned int chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Russell King3afb6e9c2008-12-08 16:08:48 +0000186 dma_t *dma = dma_channel(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 if (!dma->lock)
189 goto free_dma;
190
191 if (dma->active == 0) {
192 dma->active = 1;
Russell King1df81302008-12-08 15:58:50 +0000193 dma->d_ops->enable(chan, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
195 return;
196
197free_dma:
Russell King4ed89f22014-10-28 11:26:42 +0000198 pr_err("dma%d: trying to enable free DMA\n", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 BUG();
200}
Russell Kingd7b4a752006-01-04 15:52:45 +0000201EXPORT_SYMBOL(enable_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203/* Disable DMA channel
204 */
Russell King1df81302008-12-08 15:58:50 +0000205void disable_dma (unsigned int chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Russell King3afb6e9c2008-12-08 16:08:48 +0000207 dma_t *dma = dma_channel(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 if (!dma->lock)
210 goto free_dma;
211
212 if (dma->active == 1) {
213 dma->active = 0;
Russell King1df81302008-12-08 15:58:50 +0000214 dma->d_ops->disable(chan, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216 return;
217
218free_dma:
Russell King4ed89f22014-10-28 11:26:42 +0000219 pr_err("dma%d: trying to disable free DMA\n", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 BUG();
221}
Russell Kingd7b4a752006-01-04 15:52:45 +0000222EXPORT_SYMBOL(disable_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224/*
225 * Is the specified DMA channel active?
226 */
Russell King1df81302008-12-08 15:58:50 +0000227int dma_channel_active(unsigned int chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Russell King3afb6e9c2008-12-08 16:08:48 +0000229 dma_t *dma = dma_channel(chan);
230 return dma->active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
Russell Kingec14d792007-03-31 21:36:53 +0100232EXPORT_SYMBOL(dma_channel_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Russell King1df81302008-12-08 15:58:50 +0000234void set_dma_page(unsigned int chan, char pagenr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Russell King4ed89f22014-10-28 11:26:42 +0000236 pr_err("dma%d: trying to set_dma_page\n", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
Russell Kingd7b4a752006-01-04 15:52:45 +0000238EXPORT_SYMBOL(set_dma_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Russell King1df81302008-12-08 15:58:50 +0000240void set_dma_speed(unsigned int chan, int cycle_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Russell King3afb6e9c2008-12-08 16:08:48 +0000242 dma_t *dma = dma_channel(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 int ret = 0;
244
245 if (dma->d_ops->setspeed)
Russell King1df81302008-12-08 15:58:50 +0000246 ret = dma->d_ops->setspeed(chan, dma, cycle_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 dma->speed = ret;
248}
Russell Kingd7b4a752006-01-04 15:52:45 +0000249EXPORT_SYMBOL(set_dma_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Russell King1df81302008-12-08 15:58:50 +0000251int get_dma_residue(unsigned int chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Russell King3afb6e9c2008-12-08 16:08:48 +0000253 dma_t *dma = dma_channel(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 int ret = 0;
255
256 if (dma->d_ops->residue)
Russell King1df81302008-12-08 15:58:50 +0000257 ret = dma->d_ops->residue(chan, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 return ret;
260}
Russell Kingd7b4a752006-01-04 15:52:45 +0000261EXPORT_SYMBOL(get_dma_residue);
Russell Kinge193ba22009-12-24 18:32:13 +0000262
263#ifdef CONFIG_PROC_FS
264static int proc_dma_show(struct seq_file *m, void *v)
265{
266 int i;
267
268 for (i = 0 ; i < MAX_DMA_CHANNELS ; i++) {
269 dma_t *dma = dma_channel(i);
270 if (dma && dma->lock)
271 seq_printf(m, "%2d: %s\n", i, dma->device_id);
272 }
273 return 0;
274}
275
Russell Kinge193ba22009-12-24 18:32:13 +0000276static int __init proc_dma_init(void)
277{
Christoph Hellwig3f3942a2018-05-15 15:57:23 +0200278 proc_create_single("dma", 0, NULL, proc_dma_show);
Russell Kinge193ba22009-12-24 18:32:13 +0000279 return 0;
280}
281
282__initcall(proc_dma_init);
283#endif