1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/arch/arm/kernel/dma.c
5 * Copyright (C) 1995-2000 Russell King
7 * Front-end to the DMA handling. This handles the allocation/freeing
8 * of DMA channels, and provides a unified interface to the machines
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/spinlock.h>
14 #include <linux/errno.h>
15 #include <linux/scatterlist.h>
16 #include <linux/seq_file.h>
17 #include <linux/proc_fs.h>
21 #include <asm/mach/dma.h>
23 DEFINE_RAW_SPINLOCK(dma_spin_lock
);
24 EXPORT_SYMBOL(dma_spin_lock
);
26 static dma_t
*dma_chan
[MAX_DMA_CHANNELS
];
28 static inline dma_t
*dma_channel(unsigned int chan
)
30 if (chan
>= MAX_DMA_CHANNELS
)
33 return dma_chan
[chan
];
36 int __init
isa_dma_add(unsigned int chan
, dma_t
*dma
)
41 sg_init_table(&dma
->buf
, 1);
52 * On certain platforms, we have to allocate an interrupt as well...
54 int request_dma(unsigned int chan
, const char *device_id
)
56 dma_t
*dma
= dma_channel(chan
);
62 if (xchg(&dma
->lock
, 1) != 0)
65 dma
->device_id
= device_id
;
70 if (dma
->d_ops
->request
)
71 ret
= dma
->d_ops
->request(chan
, dma
);
79 pr_err("dma: trying to allocate DMA%d\n", chan
);
85 EXPORT_SYMBOL(request_dma
);
90 * On certain platforms, we have to free interrupt as well...
92 void free_dma(unsigned int chan
)
94 dma_t
*dma
= dma_channel(chan
);
100 pr_err("dma%d: freeing active DMA\n", chan
);
101 dma
->d_ops
->disable(chan
, dma
);
105 if (xchg(&dma
->lock
, 0) != 0) {
106 if (dma
->d_ops
->free
)
107 dma
->d_ops
->free(chan
, dma
);
111 pr_err("dma%d: trying to free free DMA\n", chan
);
115 pr_err("dma: trying to free DMA%d\n", chan
);
117 EXPORT_SYMBOL(free_dma
);
119 /* Set DMA Scatter-Gather list
121 void set_dma_sg (unsigned int chan
, struct scatterlist
*sg
, int nr_sg
)
123 dma_t
*dma
= dma_channel(chan
);
126 pr_err("dma%d: altering DMA SG while DMA active\n", chan
);
129 dma
->sgcount
= nr_sg
;
132 EXPORT_SYMBOL(set_dma_sg
);
136 * Copy address to the structure, and set the invalid bit
138 void __set_dma_addr (unsigned int chan
, void *addr
)
140 dma_t
*dma
= dma_channel(chan
);
143 pr_err("dma%d: altering DMA address while DMA active\n", chan
);
149 EXPORT_SYMBOL(__set_dma_addr
);
151 /* Set DMA byte count
153 * Copy address to the structure, and set the invalid bit
155 void set_dma_count (unsigned int chan
, unsigned long count
)
157 dma_t
*dma
= dma_channel(chan
);
160 pr_err("dma%d: altering DMA count while DMA active\n", chan
);
166 EXPORT_SYMBOL(set_dma_count
);
168 /* Set DMA direction mode
170 void set_dma_mode (unsigned int chan
, unsigned int mode
)
172 dma_t
*dma
= dma_channel(chan
);
175 pr_err("dma%d: altering DMA mode while DMA active\n", chan
);
177 dma
->dma_mode
= mode
;
180 EXPORT_SYMBOL(set_dma_mode
);
182 /* Enable DMA channel
184 void enable_dma (unsigned int chan
)
186 dma_t
*dma
= dma_channel(chan
);
191 if (dma
->active
== 0) {
193 dma
->d_ops
->enable(chan
, dma
);
198 pr_err("dma%d: trying to enable free DMA\n", chan
);
201 EXPORT_SYMBOL(enable_dma
);
203 /* Disable DMA channel
205 void disable_dma (unsigned int chan
)
207 dma_t
*dma
= dma_channel(chan
);
212 if (dma
->active
== 1) {
214 dma
->d_ops
->disable(chan
, dma
);
219 pr_err("dma%d: trying to disable free DMA\n", chan
);
222 EXPORT_SYMBOL(disable_dma
);
225 * Is the specified DMA channel active?
227 int dma_channel_active(unsigned int chan
)
229 dma_t
*dma
= dma_channel(chan
);
232 EXPORT_SYMBOL(dma_channel_active
);
234 void set_dma_page(unsigned int chan
, char pagenr
)
236 pr_err("dma%d: trying to set_dma_page\n", chan
);
238 EXPORT_SYMBOL(set_dma_page
);
240 void set_dma_speed(unsigned int chan
, int cycle_ns
)
242 dma_t
*dma
= dma_channel(chan
);
245 if (dma
->d_ops
->setspeed
)
246 ret
= dma
->d_ops
->setspeed(chan
, dma
, cycle_ns
);
249 EXPORT_SYMBOL(set_dma_speed
);
251 int get_dma_residue(unsigned int chan
)
253 dma_t
*dma
= dma_channel(chan
);
256 if (dma
->d_ops
->residue
)
257 ret
= dma
->d_ops
->residue(chan
, dma
);
261 EXPORT_SYMBOL(get_dma_residue
);
263 #ifdef CONFIG_PROC_FS
264 static int proc_dma_show(struct seq_file
*m
, void *v
)
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
);
276 static int __init
proc_dma_init(void)
278 proc_create_single("dma", 0, NULL
, proc_dma_show
);
282 __initcall(proc_dma_init
);