blob: 56e13333512b385f9d1e15ccb8563ef1f65c3595 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * arch/arm/mach-sa1100/dma.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Support functions for the SA11x0 internal DMA channels.
5 *
6 * Copyright (C) 2000, 2001 by Nicolas Pitre
7 *
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.
11 */
12
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>
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/irq.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010020#include <mach/hardware.h>
Russell Kingdcea83a2008-11-29 11:40:28 +000021#include <mach/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23
24#undef DEBUG
25#ifdef DEBUG
26#define DPRINTK( s, arg... ) printk( "dma<%p>: " s, regs , ##arg )
27#else
28#define DPRINTK( x... )
29#endif
30
31
32typedef struct {
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 */
37} sa1100_dma_t;
38
39static sa1100_dma_t dma_chan[SA1100_DMA_CHANNELS];
40
Dmitry Artamonow4275c132009-09-19 05:58:54 +010041static DEFINE_SPINLOCK(dma_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43
Linus Torvalds0cd61b62006-10-06 10:53:39 -070044static irqreturn_t dma_irq_handler(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
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;
49
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;
53 }
54
55 dma_regs->ClrDCSR = status & (DCSR_DONEA | DCSR_DONEB);
56 if (dma->callback) {
57 if (status & DCSR_DONEA)
58 dma->callback(dma->data);
59 if (status & DCSR_DONEB)
60 dma->callback(dma->data);
61 }
62 return IRQ_HANDLED;
63}
64
65
66/**
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020067 * sa1100_request_dma - allocate one of the SA11x0's DMA channels
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 * @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
73 *
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
Russell Kinga09e64f2008-08-05 16:14:15 +010078 * arch/arm/mach-sa1100/include/mach/dma.h as a dma_device_t enum.
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 *
80 * Note that reading from a port and writing to the same port are
81 * actually considered as two different streams requiring separate
82 * DMA registrations.
83 *
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.
88 *
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.
92 **/
93
94int sa1100_request_dma (dma_device_t device, const char *device_id,
95 dma_callback_t callback, void *data,
96 dma_regs_t **dma_regs)
97{
98 sa1100_dma_t *dma = NULL;
99 dma_regs_t *regs;
100 int i, err;
101
102 *dma_regs = NULL;
103
104 err = 0;
105 spin_lock(&dma_list_lock);
106 for (i = 0; i < SA1100_DMA_CHANNELS; i++) {
107 if (dma_chan[i].device == device) {
108 err = -EBUSY;
109 break;
110 } else if (!dma_chan[i].device && !dma) {
111 dma = &dma_chan[i];
112 }
113 }
114 if (!err) {
Kristoffer Ericson93982532008-11-26 20:58:43 +0100115 if (dma)
116 dma->device = device;
117 else
118 err = -ENOSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
120 spin_unlock(&dma_list_lock);
121 if (err)
122 return err;
123
124 i = dma - dma_chan;
125 regs = (dma_regs_t *)&DDAR(i);
Thomas Gleixner52e405e2006-07-03 02:20:05 +0200126 err = request_irq(IRQ_DMA0 + i, dma_irq_handler, IRQF_DISABLED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 device_id, regs);
128 if (err) {
129 printk(KERN_ERR
130 "%s: unable to request IRQ %d for %s\n",
Harvey Harrison8e86f422008-03-04 15:08:02 -0800131 __func__, IRQ_DMA0 + i, device_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 dma->device = 0;
133 return err;
134 }
135
136 *dma_regs = regs;
137 dma->device_id = device_id;
138 dma->callback = callback;
139 dma->data = data;
140
141 regs->ClrDCSR =
142 (DCSR_DONEA | DCSR_DONEB | DCSR_STRTA | DCSR_STRTB |
143 DCSR_IE | DCSR_ERROR | DCSR_RUN);
144 regs->DDAR = device;
145
146 return 0;
147}
148
149
150/**
151 * sa1100_free_dma - free a SA11x0 DMA channel
152 * @regs: identifier for the channel to free
153 *
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().
157 **/
158
159void sa1100_free_dma(dma_regs_t *regs)
160{
161 int i;
162
163 for (i = 0; i < SA1100_DMA_CHANNELS; i++)
164 if (regs == (dma_regs_t *)&DDAR(i))
165 break;
166 if (i >= SA1100_DMA_CHANNELS) {
Harvey Harrison8e86f422008-03-04 15:08:02 -0800167 printk(KERN_ERR "%s: bad DMA identifier\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return;
169 }
170
171 if (!dma_chan[i].device) {
Harvey Harrison8e86f422008-03-04 15:08:02 -0800172 printk(KERN_ERR "%s: Trying to free free DMA\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return;
174 }
175
176 regs->ClrDCSR =
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;
181}
182
183
184/**
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
188 * @size: buffer size
189 *
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.
196 *
197 * The @regs identifier is provided by a successful call to
198 * sa1100_request_dma().
199 *
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.
206 *
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.
212 *
213 * This function returs 0 on success.
214 **/
215
216int sa1100_start_dma(dma_regs_t *regs, dma_addr_t dma_ptr, u_int size)
217{
218 unsigned long flags;
219 u_long status;
220 int ret;
221
222 if (dma_ptr & 3)
223 printk(KERN_WARNING "DMA: unaligned start address (0x%08lx)\n",
224 (unsigned long)dma_ptr);
225
226 if (size > MAX_DMA_SIZE)
227 return -EOVERFLOW;
228
229 local_irq_save(flags);
230 status = regs->RdDCSR;
231
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);
235 ret = -EBUSY;
236 goto out;
237 }
238
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 */
243 ret = -EAGAIN;
244 goto out;
245 }
246 regs->DBSA = dma_ptr;
247 regs->DBTA = size;
248 regs->SetDCSR = DCSR_STRTA | DCSR_IE | DCSR_RUN;
249 DPRINTK("start a=%#x s=%d on A\n", dma_ptr, size);
250 } else {
251 if (status & DCSR_DONEB) {
252 /* give a chance for the interrupt to be processed */
253 ret = -EAGAIN;
254 goto out;
255 }
256 regs->DBSB = dma_ptr;
257 regs->DBTB = size;
258 regs->SetDCSR = DCSR_STRTB | DCSR_IE | DCSR_RUN;
259 DPRINTK("start a=%#x s=%d on B\n", dma_ptr, size);
260 }
261 ret = 0;
262
263out:
264 local_irq_restore(flags);
265 return ret;
266}
267
268
269/**
270 * sa1100_get_dma_pos - return current DMA position
271 * @regs: identifier for the channel to use
272 *
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.
281 *
282 * The @regs identifier is provided by a successful call to
283 * sa1100_request_dma().
284 **/
285
286dma_addr_t sa1100_get_dma_pos(dma_regs_t *regs)
287{
288 int status;
289
290 /*
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
301 * position is exact.
302 */
303 status = regs->RdDCSR;
304 if ((!(status & DCSR_BIU) && (status & DCSR_STRTA)) ||
305 ( (status & DCSR_BIU) && !(status & DCSR_STRTB)))
306 return regs->DBSA;
307 else
308 return regs->DBSB;
309}
310
311
312/**
313 * sa1100_reset_dma - reset a DMA channel
314 * @regs: identifier for the channel to use
315 *
316 * This function resets and reconfigure the given DMA channel. This is
317 * particularly useful after a sleep/wakeup event.
318 *
319 * The @regs identifier is provided by a successful call to
320 * sa1100_request_dma().
321 **/
322
323void sa1100_reset_dma(dma_regs_t *regs)
324{
325 int i;
326
327 for (i = 0; i < SA1100_DMA_CHANNELS; i++)
328 if (regs == (dma_regs_t *)&DDAR(i))
329 break;
330 if (i >= SA1100_DMA_CHANNELS) {
Harvey Harrison8e86f422008-03-04 15:08:02 -0800331 printk(KERN_ERR "%s: bad DMA identifier\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return;
333 }
334
335 regs->ClrDCSR =
336 (DCSR_DONEA | DCSR_DONEB | DCSR_STRTA | DCSR_STRTB |
337 DCSR_IE | DCSR_ERROR | DCSR_RUN);
338 regs->DDAR = dma_chan[i].device;
339}
340
341
342EXPORT_SYMBOL(sa1100_request_dma);
343EXPORT_SYMBOL(sa1100_free_dma);
344EXPORT_SYMBOL(sa1100_start_dma);
345EXPORT_SYMBOL(sa1100_get_dma_pos);
346EXPORT_SYMBOL(sa1100_reset_dma);
347