2 * arch/arm/mach-sa1100/dma.c
4 * Support functions for the SA11x0 internal DMA channels.
6 * Copyright (C) 2000, 2001 by Nicolas Pitre
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/interrupt.h>
15 #include <linux/init.h>
16 #include <linux/spinlock.h>
17 #include <linux/errno.h>
20 #include <mach/hardware.h>
26 #define DPRINTK( s, arg... ) printk( "dma<%p>: " s, regs , ##arg )
28 #define DPRINTK( x... )
33 const char *device_id
; /* device name */
34 u_long device
; /* this channel device, 0 if unused*/
35 dma_callback_t callback
; /* to call when DMA completes */
36 void *data
; /* ... with private data ptr */
39 static sa1100_dma_t dma_chan
[SA1100_DMA_CHANNELS
];
41 static DEFINE_SPINLOCK(dma_list_lock
);
44 static irqreturn_t
dma_irq_handler(int irq
, void *dev_id
)
46 dma_regs_t
*dma_regs
= dev_id
;
47 sa1100_dma_t
*dma
= dma_chan
+ (((u_int
)dma_regs
>> 5) & 7);
48 int status
= dma_regs
->RdDCSR
;
50 if (status
& (DCSR_ERROR
)) {
51 printk(KERN_CRIT
"DMA on \"%s\" caused an error\n", dma
->device_id
);
52 dma_regs
->ClrDCSR
= DCSR_ERROR
;
55 dma_regs
->ClrDCSR
= status
& (DCSR_DONEA
| DCSR_DONEB
);
57 if (status
& DCSR_DONEA
)
58 dma
->callback(dma
->data
);
59 if (status
& DCSR_DONEB
)
60 dma
->callback(dma
->data
);
67 * sa1100_request_dma - allocate one of the SA11x0's DMA channels
68 * @device: The SA11x0 peripheral targeted by this request
69 * @device_id: An ascii name for the claiming device
70 * @callback: Function to be called when the DMA completes
71 * @data: A cookie passed back to the callback function
72 * @dma_regs: Pointer to the location of the allocated channel's identifier
74 * This function will search for a free DMA channel and returns the
75 * address of the hardware registers for that channel as the channel
76 * identifier. This identifier is written to the location pointed by
77 * @dma_regs. The list of possible values for @device are listed into
78 * arch/arm/mach-sa1100/include/mach/dma.h as a dma_device_t enum.
80 * Note that reading from a port and writing to the same port are
81 * actually considered as two different streams requiring separate
84 * The @callback function is called from interrupt context when one
85 * of the two possible DMA buffers in flight has terminated. That
86 * function has to be small and efficient while posponing more complex
87 * processing to a lower priority execution context.
89 * If no channels are available, or if the desired @device is already in
90 * use by another DMA channel, then an error code is returned. This
91 * function must be called before any other DMA calls.
94 int sa1100_request_dma (dma_device_t device
, const char *device_id
,
95 dma_callback_t callback
, void *data
,
96 dma_regs_t
**dma_regs
)
98 sa1100_dma_t
*dma
= NULL
;
105 spin_lock(&dma_list_lock
);
106 for (i
= 0; i
< SA1100_DMA_CHANNELS
; i
++) {
107 if (dma_chan
[i
].device
== device
) {
110 } else if (!dma_chan
[i
].device
&& !dma
) {
116 dma
->device
= device
;
120 spin_unlock(&dma_list_lock
);
125 regs
= (dma_regs_t
*)&DDAR(i
);
126 err
= request_irq(IRQ_DMA0
+ i
, dma_irq_handler
, IRQF_DISABLED
,
130 "%s: unable to request IRQ %d for %s\n",
131 __func__
, IRQ_DMA0
+ i
, device_id
);
137 dma
->device_id
= device_id
;
138 dma
->callback
= callback
;
142 (DCSR_DONEA
| DCSR_DONEB
| DCSR_STRTA
| DCSR_STRTB
|
143 DCSR_IE
| DCSR_ERROR
| DCSR_RUN
);
151 * sa1100_free_dma - free a SA11x0 DMA channel
152 * @regs: identifier for the channel to free
154 * This clears all activities on a given DMA channel and releases it
155 * for future requests. The @regs identifier is provided by a
156 * successful call to sa1100_request_dma().
159 void sa1100_free_dma(dma_regs_t
*regs
)
163 for (i
= 0; i
< SA1100_DMA_CHANNELS
; i
++)
164 if (regs
== (dma_regs_t
*)&DDAR(i
))
166 if (i
>= SA1100_DMA_CHANNELS
) {
167 printk(KERN_ERR
"%s: bad DMA identifier\n", __func__
);
171 if (!dma_chan
[i
].device
) {
172 printk(KERN_ERR
"%s: Trying to free free DMA\n", __func__
);
177 (DCSR_DONEA
| DCSR_DONEB
| DCSR_STRTA
| DCSR_STRTB
|
178 DCSR_IE
| DCSR_ERROR
| DCSR_RUN
);
179 free_irq(IRQ_DMA0
+ i
, regs
);
180 dma_chan
[i
].device
= 0;
185 * sa1100_start_dma - submit a data buffer for DMA
186 * @regs: identifier for the channel to use
187 * @dma_ptr: buffer physical (or bus) start address
190 * This function hands the given data buffer to the hardware for DMA
191 * access. If another buffer is already in flight then this buffer
192 * will be queued so the DMA engine will switch to it automatically
193 * when the previous one is done. The DMA engine is actually toggling
194 * between two buffers so at most 2 successful calls can be made before
195 * one of them terminates and the callback function is called.
197 * The @regs identifier is provided by a successful call to
198 * sa1100_request_dma().
200 * The @size must not be larger than %MAX_DMA_SIZE. If a given buffer
201 * is larger than that then it's the caller's responsibility to split
202 * it into smaller chunks and submit them separately. If this is the
203 * case then a @size of %CUT_DMA_SIZE is recommended to avoid ending
204 * up with too small chunks. The callback function can be used to chain
205 * submissions of buffer chunks.
207 * Error return values:
208 * %-EOVERFLOW: Given buffer size is too big.
209 * %-EBUSY: Both DMA buffers are already in use.
210 * %-EAGAIN: Both buffers were busy but one of them just completed
211 * but the interrupt handler has to execute first.
213 * This function returs 0 on success.
216 int sa1100_start_dma(dma_regs_t
*regs
, dma_addr_t dma_ptr
, u_int size
)
223 printk(KERN_WARNING
"DMA: unaligned start address (0x%08lx)\n",
224 (unsigned long)dma_ptr
);
226 if (size
> MAX_DMA_SIZE
)
229 local_irq_save(flags
);
230 status
= regs
->RdDCSR
;
232 /* If both DMA buffers are started, there's nothing else we can do. */
233 if ((status
& (DCSR_STRTA
| DCSR_STRTB
)) == (DCSR_STRTA
| DCSR_STRTB
)) {
234 DPRINTK("start: st %#x busy\n", status
);
239 if (((status
& DCSR_BIU
) && (status
& DCSR_STRTB
)) ||
240 (!(status
& DCSR_BIU
) && !(status
& DCSR_STRTA
))) {
241 if (status
& DCSR_DONEA
) {
242 /* give a chance for the interrupt to be processed */
246 regs
->DBSA
= dma_ptr
;
248 regs
->SetDCSR
= DCSR_STRTA
| DCSR_IE
| DCSR_RUN
;
249 DPRINTK("start a=%#x s=%d on A\n", dma_ptr
, size
);
251 if (status
& DCSR_DONEB
) {
252 /* give a chance for the interrupt to be processed */
256 regs
->DBSB
= dma_ptr
;
258 regs
->SetDCSR
= DCSR_STRTB
| DCSR_IE
| DCSR_RUN
;
259 DPRINTK("start a=%#x s=%d on B\n", dma_ptr
, size
);
264 local_irq_restore(flags
);
270 * sa1100_get_dma_pos - return current DMA position
271 * @regs: identifier for the channel to use
273 * This function returns the current physical (or bus) address for the
274 * given DMA channel. If the channel is running i.e. not in a stopped
275 * state then the caller must disable interrupts prior calling this
276 * function and process the returned value before re-enabling them to
277 * prevent races with the completion interrupt handler and the callback
278 * function. The validation of the returned value is the caller's
279 * responsibility as well -- the hardware seems to return out of range
280 * values when the DMA engine completes a buffer.
282 * The @regs identifier is provided by a successful call to
283 * sa1100_request_dma().
286 dma_addr_t
sa1100_get_dma_pos(dma_regs_t
*regs
)
291 * We must determine whether buffer A or B is active.
292 * Two possibilities: either we are in the middle of
293 * a buffer, or the DMA controller just switched to the
294 * next toggle but the interrupt hasn't been serviced yet.
295 * The former case is straight forward. In the later case,
296 * we'll do like if DMA is just at the end of the previous
297 * toggle since all registers haven't been reset yet.
298 * This goes around the edge case and since we're always
299 * a little behind anyways it shouldn't make a big difference.
300 * If DMA has been stopped prior calling this then the
303 status
= regs
->RdDCSR
;
304 if ((!(status
& DCSR_BIU
) && (status
& DCSR_STRTA
)) ||
305 ( (status
& DCSR_BIU
) && !(status
& DCSR_STRTB
)))
313 * sa1100_reset_dma - reset a DMA channel
314 * @regs: identifier for the channel to use
316 * This function resets and reconfigure the given DMA channel. This is
317 * particularly useful after a sleep/wakeup event.
319 * The @regs identifier is provided by a successful call to
320 * sa1100_request_dma().
323 void sa1100_reset_dma(dma_regs_t
*regs
)
327 for (i
= 0; i
< SA1100_DMA_CHANNELS
; i
++)
328 if (regs
== (dma_regs_t
*)&DDAR(i
))
330 if (i
>= SA1100_DMA_CHANNELS
) {
331 printk(KERN_ERR
"%s: bad DMA identifier\n", __func__
);
336 (DCSR_DONEA
| DCSR_DONEB
| DCSR_STRTA
| DCSR_STRTB
|
337 DCSR_IE
| DCSR_ERROR
| DCSR_RUN
);
338 regs
->DDAR
= dma_chan
[i
].device
;
342 EXPORT_SYMBOL(sa1100_request_dma
);
343 EXPORT_SYMBOL(sa1100_free_dma
);
344 EXPORT_SYMBOL(sa1100_start_dma
);
345 EXPORT_SYMBOL(sa1100_get_dma_pos
);
346 EXPORT_SYMBOL(sa1100_reset_dma
);
This page took 0.11747 seconds and 5 git commands to generate.