ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / mips / au1000 / common / dbdma.c
1 /*
2  *
3  * BRIEF MODULE DESCRIPTION
4  *      The Descriptor Based DMA channel manager that first appeared
5  *      on the Au1550.  I started with dma.c, but I think all that is
6  *      left is this initial comment :-)
7  *
8  * Copyright 2004 Embedded Edge, LLC
9  *      dan@embeddededge.com
10  *
11  *  This program is free software; you can redistribute  it and/or modify it
12  *  under  the terms of  the GNU General  Public License as published by the
13  *  Free Software Foundation;  either version 2 of the  License, or (at your
14  *  option) any later version.
15  *
16  *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
17  *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
18  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
19  *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
20  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
22  *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23  *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
24  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  *  You should have received a copy of the  GNU General Public License along
28  *  with this program; if not, write  to the Free Software Foundation, Inc.,
29  *  675 Mass Ave, Cambridge, MA 02139, USA.
30  *
31  */
32
33 #include <linux/kernel.h>
34 #include <linux/errno.h>
35 #include <linux/sched.h>
36 #include <linux/slab.h>
37 #include <linux/spinlock.h>
38 #include <linux/string.h>
39 #include <linux/delay.h>
40 #include <asm/mach-au1x00/au1000.h>
41 #include <asm/mach-au1x00/au1xxx_dbdma.h>
42 #include <asm/system.h>
43
44 /*
45  * The Descriptor Based DMA supports up to 16 channels.
46  *
47  * There are 32 devices defined. We keep an internal structure
48  * of devices using these channels, along with additional
49  * information.
50  *
51  * We allocate the descriptors and allow access to them through various
52  * functions.  The drivers allocate the data buffers and assign them
53  * to the descriptors.
54  */
55 static spinlock_t au1xxx_dbdma_spin_lock = SPIN_LOCK_UNLOCKED;
56
57 /* I couldn't find a macro that did this......
58 */
59 #define ALIGN_ADDR(x, a)        ((((u32)(x)) + (a-1)) & ~(a-1))
60
61 static volatile dbdma_global_t *dbdma_gptr = (dbdma_global_t *)DDMA_GLOBAL_BASE;
62 static int dbdma_initialized;
63 static void au1xxx_dbdma_init(void);
64
65 typedef struct dbdma_device_table {
66         u32             dev_flags;
67         u32             dev_tsize;
68         u32             dev_devwidth;
69         u32             dev_physaddr;           /* If FIFO */
70         u32             dev_intlevel;
71         u32             dev_intpolarity;
72 } dbdev_tab_t;
73
74 typedef struct dbdma_chan_config {
75         u32                     chan_flags;
76         u32                     chan_index;
77         dbdev_tab_t             *chan_src;
78         dbdev_tab_t             *chan_dest;
79         au1x_dma_chan_t         *chan_ptr;
80         au1x_ddma_desc_t        *chan_desc_base;
81         au1x_ddma_desc_t        *get_ptr, *put_ptr, *cur_ptr;
82         void                    *chan_callparam;
83         void (*chan_callback)(int, void *, struct pt_regs *);
84 } chan_tab_t;
85
86 #define DEV_FLAGS_INUSE         (1 << 0)
87 #define DEV_FLAGS_ANYUSE        (1 << 1)
88 #define DEV_FLAGS_OUT           (1 << 2)
89 #define DEV_FLAGS_IN            (1 << 3)
90
91 static dbdev_tab_t dbdev_tab[] = {
92         /* UARTS */
93         { DEV_FLAGS_OUT, 0, 8, 0x11100004, 0, 0 },
94         { DEV_FLAGS_IN, 0, 8, 0x11100000, 0, 0 },
95         { DEV_FLAGS_OUT, 0, 8, 0x11400004, 0, 0 },
96         { DEV_FLAGS_IN, 0, 8, 0x11400000, 0, 0 },
97
98         /* EXT DMA */
99         { 0, 0, 0, 0x00000000, 0, 0 },
100         { 0, 0, 0, 0x00000000, 0, 0 },
101         { 0, 0, 0, 0x00000000, 0, 0 },
102         { 0, 0, 0, 0x00000000, 0, 0 },
103
104         /* USB DEV */
105         { DEV_FLAGS_IN, 4, 8, 0x10200000, 0, 0 },
106         { DEV_FLAGS_OUT, 4, 8, 0x10200004, 0, 0 },
107         { DEV_FLAGS_OUT, 4, 8, 0x10200008, 0, 0 },
108         { DEV_FLAGS_OUT, 4, 8, 0x1020000c, 0, 0 },
109         { DEV_FLAGS_IN, 4, 8, 0x10200010, 0, 0 },
110         { DEV_FLAGS_IN, 4, 8, 0x10200014, 0, 0 },
111
112         /* PSC 0 */
113         { DEV_FLAGS_OUT, 0, 0, 0x11a0001c, 0, 0 },
114         { DEV_FLAGS_IN, 0, 0, 0x11a0001c, 0, 0 },
115
116         /* PSC 1 */
117         { DEV_FLAGS_OUT, 0, 0, 0x11b0001c, 0, 0 },
118         { DEV_FLAGS_IN, 0, 0, 0x11b0001c, 0, 0 },
119
120         /* PSC 2 */
121         { DEV_FLAGS_OUT, 0, 0, 0x10a0001c, 0, 0 },
122         { DEV_FLAGS_IN, 0, 0, 0x10a0001c, 0, 0 },
123
124         /* PSC 3 */
125         { DEV_FLAGS_OUT, 0, 0, 0x10b0001c, 0, 0 },
126         { DEV_FLAGS_IN, 0, 0, 0x10b0001c, 0, 0 },
127
128         { 0, 0, 0, 0x00000000, 0, 0 },  /* PCI */
129         { 0, 0, 0, 0x00000000, 0, 0 },  /* NAND */
130
131         /* MAC 0 */
132         { DEV_FLAGS_IN, 0, 0, 0x00000000, 0, 0 },
133         { DEV_FLAGS_OUT, 0, 0, 0x00000000, 0, 0 },
134
135         /* MAC 1 */
136         { DEV_FLAGS_IN, 0, 0, 0x00000000, 0, 0 },
137         { DEV_FLAGS_OUT, 0, 0, 0x00000000, 0, 0 },
138
139         /* reserved */
140         { DEV_FLAGS_INUSE, 0, 0, 0x00000000, 0, 0 },
141         { DEV_FLAGS_INUSE, 0, 0, 0x00000000, 0, 0 },
142
143         /* Memory */
144         { DEV_FLAGS_ANYUSE, 0, 0, 0x00000000, 0, 0 },   /* throttle */
145         { DEV_FLAGS_ANYUSE, 0, 0, 0x00000000, 0, 0 },   /* always */
146 };
147
148 static chan_tab_t *chan_tab_ptr[NUM_DBDMA_CHANS];
149
150 /* Allocate a channel and return a non-zero descriptor if successful.
151 */
152 u32
153 au1xxx_dbdma_chan_alloc(u32 srcid, u32 destid,
154        void (*callback)(int, void *, struct pt_regs *), void *callparam)
155 {
156         unsigned long   flags;
157         u32             used, chan, rv;
158         u32             dcp;
159         int             i;
160         dbdev_tab_t     *stp, *dtp;
161         chan_tab_t      *ctp;
162         volatile au1x_dma_chan_t *cp;
163
164         /* We do the intialization on the first channel allocation.
165          * We have to wait because of the interrupt handler initialization
166          * which can't be done successfully during board set up.
167          */
168         if (!dbdma_initialized)
169                 au1xxx_dbdma_init();
170         dbdma_initialized = 1;
171
172         if ((srcid > DSCR_NDEV_IDS) || (destid > DSCR_NDEV_IDS))
173                 return 0;
174
175         stp = &dbdev_tab[srcid];
176         dtp = &dbdev_tab[destid];
177         used = 0;
178         rv = 0;
179
180         /* Check to see if we can get both channels.
181         */
182         spin_lock_irqsave(&au1xxx_dbdma_spin_lock, flags);
183         if (!(stp->dev_flags & DEV_FLAGS_INUSE) ||
184              (stp->dev_flags & DEV_FLAGS_ANYUSE)) {
185                 /* Got source */
186                 stp->dev_flags |= DEV_FLAGS_INUSE;
187                 if (!(dtp->dev_flags & DEV_FLAGS_INUSE) ||
188                      (dtp->dev_flags & DEV_FLAGS_ANYUSE)) {
189                         /* Got destination */
190                         dtp->dev_flags |= DEV_FLAGS_INUSE;
191                 }
192                 else {
193                         /* Can't get dest.  Release src.
194                         */
195                         stp->dev_flags &= ~DEV_FLAGS_INUSE;
196                         used++;
197                 }
198         }
199         else {
200                 used++;
201         }
202         spin_unlock_irqrestore(&au1xxx_dbdma_spin_lock, flags);
203
204         if (!used) {
205                 /* Let's see if we can allocate a channel for it.
206                 */
207                 ctp = NULL;
208                 chan = 0;
209                 spin_lock_irqsave(&au1xxx_dbdma_spin_lock, flags);
210                 for (i=0; i<NUM_DBDMA_CHANS; i++) {
211                         if (chan_tab_ptr[i] == NULL) {
212                                 /* If kmalloc fails, it is caught below same
213                                  * as a channel not available.
214                                  */
215                                 ctp = (chan_tab_t *)kmalloc(sizeof(chan_tab_t), GFP_KERNEL);
216                                 chan_tab_ptr[i] = ctp;
217                                 ctp->chan_index = chan = i;
218                                 break;
219                         }
220                 }
221                 spin_unlock_irqrestore(&au1xxx_dbdma_spin_lock, flags);
222
223                 if (ctp != NULL) {
224                         memset(ctp, 0, sizeof(chan_tab_t));
225                         dcp = DDMA_CHANNEL_BASE;
226                         dcp += (0x0100 * chan);
227                         ctp->chan_ptr = (au1x_dma_chan_t *)dcp;
228                         cp = (volatile au1x_dma_chan_t *)dcp;
229                         ctp->chan_src = stp;
230                         ctp->chan_dest = dtp;
231                         ctp->chan_callback = callback;
232                         ctp->chan_callparam = callparam;
233
234                         /* Initialize channel configuration.
235                         */
236                         i = 0;
237                         if (stp->dev_intlevel)
238                                 i |= DDMA_CFG_SED;
239                         if (stp->dev_intpolarity)
240                                 i |= DDMA_CFG_SP;
241                         if (dtp->dev_intlevel)
242                                 i |= DDMA_CFG_DED;
243                         if (dtp->dev_intpolarity)
244                                 i |= DDMA_CFG_DP;
245                         cp->ddma_cfg = i;
246                         au_sync();
247
248                         /* Return a non-zero value that can be used to
249                          * find the channel information in subsequent
250                          * operations.
251                          */
252                         rv = (u32)(&chan_tab_ptr[chan]);
253                 }
254                 else {
255                         /* Release devices.
256                         */
257                         stp->dev_flags &= ~DEV_FLAGS_INUSE;
258                         dtp->dev_flags &= ~DEV_FLAGS_INUSE;
259                 }
260         }
261         return rv;
262 }
263
264 /* Set the device width if source or destination is a FIFO.
265  * Should be 8, 16, or 32 bits.
266  */
267 u32
268 au1xxx_dbdma_set_devwidth(u32 chanid, int bits)
269 {
270         u32             rv;
271         chan_tab_t      *ctp;
272         dbdev_tab_t     *stp, *dtp;
273
274         ctp = *((chan_tab_t **)chanid);
275         stp = ctp->chan_src;
276         dtp = ctp->chan_dest;
277         rv = 0;
278
279         if (stp->dev_flags & DEV_FLAGS_IN) {    /* Source in fifo */
280                 rv = stp->dev_devwidth;
281                 stp->dev_devwidth = bits;
282         }
283         if (dtp->dev_flags & DEV_FLAGS_OUT) {   /* Destination out fifo */
284                 rv = dtp->dev_devwidth;
285                 dtp->dev_devwidth = bits;
286         }
287
288         return rv;
289 }
290
291 /* Allocate a descriptor ring, initializing as much as possible.
292 */
293 u32
294 au1xxx_dbdma_ring_alloc(u32 chanid, int entries)
295 {
296         int                     i;
297         u32                     desc_base, srcid, destid;
298         u32                     cmd0, cmd1, src1, dest1;
299         u32                     src0, dest0;
300         chan_tab_t              *ctp;
301         dbdev_tab_t             *stp, *dtp;
302         au1x_ddma_desc_t        *dp;
303
304         /* I guess we could check this to be within the
305          * range of the table......
306          */
307         ctp = *((chan_tab_t **)chanid);
308         stp = ctp->chan_src;
309         dtp = ctp->chan_dest;
310
311         /* The descriptors must be 32-byte aligned.  There is a
312          * possibility the allocation will give us such an address,
313          * and if we try that first we are likely to not waste larger
314          * slabs of memory.
315          */
316         desc_base = (u32)kmalloc(entries * sizeof(au1x_ddma_desc_t), GFP_KERNEL);
317         if (desc_base == 0)
318                 return 0;
319
320         if (desc_base & 0x1f) {
321                 /* Lost....do it again, allocate extra, and round
322                  * the address base.
323                  */
324                 kfree((const void *)desc_base);
325                 i = entries * sizeof(au1x_ddma_desc_t);
326                 i += (sizeof(au1x_ddma_desc_t) - 1);
327                 if ((desc_base = (u32)kmalloc(i, GFP_KERNEL)) == 0)
328                         return 0;
329
330                 desc_base = ALIGN_ADDR(desc_base, sizeof(au1x_ddma_desc_t));
331         }
332         dp = (au1x_ddma_desc_t *)desc_base;
333
334         /* Keep track of the base descriptor.
335         */
336         ctp->chan_desc_base = dp;
337
338         /* Initialize the rings with as much information as we know.
339          */
340         srcid = stp - dbdev_tab;        /* Index is channel device ID */
341         destid = dtp - dbdev_tab;
342
343         cmd0 = cmd1 = src1 = dest1 = 0;
344         src0 = dest0 = 0;
345
346         cmd0 |= DSCR_CMD0_SID(srcid);
347         cmd0 |= DSCR_CMD0_DID(destid);
348         cmd0 |= DSCR_CMD0_IE | DSCR_CMD0_CV;
349         cmd0 |= DSCR_CMD0_ST(DSCR_CMD0_ST_CURRENT);
350
351         switch (stp->dev_devwidth) {
352         case 8:
353                 cmd0 |= DSCR_CMD0_SW(DSCR_CMD0_BYTE);
354                 break;
355         case 16:
356                 cmd0 |= DSCR_CMD0_SW(DSCR_CMD0_HALFWORD);
357                 break;
358         case 32:
359         default:
360                 cmd0 |= DSCR_CMD0_SW(DSCR_CMD0_WORD);
361                 break;
362         }
363
364         switch (dtp->dev_devwidth) {
365         case 8:
366                 cmd0 |= DSCR_CMD0_DW(DSCR_CMD0_BYTE);
367                 break;
368         case 16:
369                 cmd0 |= DSCR_CMD0_DW(DSCR_CMD0_HALFWORD);
370                 break;
371         case 32:
372         default:
373                 cmd0 |= DSCR_CMD0_DW(DSCR_CMD0_WORD);
374                 break;
375         }
376
377         /* If the device is marked as an in/out FIFO, ensure it is
378          * set non-coherent.
379          */
380         if (stp->dev_flags & DEV_FLAGS_IN)
381                 cmd0 |= DSCR_CMD0_SN;           /* Source in fifo */
382         if (dtp->dev_flags & DEV_FLAGS_OUT)
383                 cmd0 |= DSCR_CMD0_DN;           /* Destination out fifo */
384
385         /* Set up source1.  For now, assume no stride and increment.
386          * A channel attribute update can change this later.
387          */
388         switch (stp->dev_tsize) {
389         case 1:
390                 src1 |= DSCR_SRC1_STS(DSCR_xTS_SIZE1);
391                 break;
392         case 2:
393                 src1 |= DSCR_SRC1_STS(DSCR_xTS_SIZE2);
394                 break;
395         case 4:
396                 src1 |= DSCR_SRC1_STS(DSCR_xTS_SIZE4);
397                 break;
398         case 8:
399         default:
400                 src1 |= DSCR_SRC1_STS(DSCR_xTS_SIZE8);
401                 break;
402         }
403
404         /* If source input is fifo, set static address.
405         */
406         if (stp->dev_flags & DEV_FLAGS_IN) {
407                 src0 = stp->dev_physaddr;
408                 src1 |= DSCR_SRC1_SAM(DSCR_xAM_STATIC);
409         }
410
411         /* Set up dest1.  For now, assume no stride and increment.
412          * A channel attribute update can change this later.
413          */
414         switch (dtp->dev_tsize) {
415         case 1:
416                 dest1 |= DSCR_DEST1_DTS(DSCR_xTS_SIZE1);
417                 break;
418         case 2:
419                 dest1 |= DSCR_DEST1_DTS(DSCR_xTS_SIZE2);
420                 break;
421         case 4:
422                 dest1 |= DSCR_DEST1_DTS(DSCR_xTS_SIZE4);
423                 break;
424         case 8:
425         default:
426                 dest1 |= DSCR_DEST1_DTS(DSCR_xTS_SIZE8);
427                 break;
428         }
429
430         /* If destination output is fifo, set static address.
431         */
432         if (dtp->dev_flags & DEV_FLAGS_OUT) {
433                 dest0 = dtp->dev_physaddr;
434                 dest1 |= DSCR_DEST1_DAM(DSCR_xAM_STATIC);
435         }
436
437         for (i=0; i<entries; i++) {
438                 dp->dscr_cmd0 = cmd0;
439                 dp->dscr_cmd1 = cmd1;
440                 dp->dscr_source0 = src0;
441                 dp->dscr_source1 = src1;
442                 dp->dscr_dest0 = dest0;
443                 dp->dscr_dest1 = dest1;
444                 dp->dscr_stat = 0;
445                 dp->dscr_nxtptr = DSCR_NXTPTR(virt_to_phys(dp + 1));
446                 dp++;
447         }
448
449         /* Make last descrptor point to the first.
450         */
451         dp--;
452         dp->dscr_nxtptr = DSCR_NXTPTR(virt_to_phys(ctp->chan_desc_base));
453         ctp->get_ptr = ctp->put_ptr = ctp->cur_ptr = ctp->chan_desc_base;
454
455         return (u32)(ctp->chan_desc_base);
456 }
457
458 /* Put a source buffer into the DMA ring.
459  * This updates the source pointer and byte count.  Normally used
460  * for memory to fifo transfers.
461  */
462 u32
463 au1xxx_dbdma_put_source(u32 chanid, void *buf, int nbytes)
464 {
465         chan_tab_t              *ctp;
466         au1x_ddma_desc_t        *dp;
467
468         /* I guess we could check this to be within the
469          * range of the table......
470          */
471         ctp = *((chan_tab_t **)chanid);
472
473         /* We should have multiple callers for a particular channel,
474          * an interrupt doesn't affect this pointer nor the descriptor,
475          * so no locking should be needed.
476          */
477         dp = ctp->put_ptr;
478
479         /* If the descriptor is valid, we are way ahead of the DMA
480          * engine, so just return an error condition.
481          */
482         if (dp->dscr_cmd0 & DSCR_CMD0_V) {
483                 return 0;
484         }
485
486         /* Load up buffer address and byte count.
487         */
488         dp->dscr_source0 = virt_to_phys(buf);
489         dp->dscr_cmd1 = nbytes;
490         dp->dscr_cmd0 |= DSCR_CMD0_V;   /* Let it rip */
491         ctp->chan_ptr->ddma_dbell = 0xffffffff; /* Make it go */
492
493         /* Get next descriptor pointer.
494         */
495         ctp->put_ptr = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));
496
497         /* return something not zero.
498         */
499         return nbytes;
500 }
501
502 /* Put a destination buffer into the DMA ring.
503  * This updates the destination pointer and byte count.  Normally used
504  * to place an empty buffer into the ring for fifo to memory transfers.
505  */
506 u32
507 au1xxx_dbdma_put_dest(u32 chanid, void *buf, int nbytes)
508 {
509         chan_tab_t              *ctp;
510         au1x_ddma_desc_t        *dp;
511
512         /* I guess we could check this to be within the
513          * range of the table......
514          */
515         ctp = *((chan_tab_t **)chanid);
516
517         /* We should have multiple callers for a particular channel,
518          * an interrupt doesn't affect this pointer nor the descriptor,
519          * so no locking should be needed.
520          */
521         dp = ctp->put_ptr;
522
523         /* If the descriptor is valid, we are way ahead of the DMA
524          * engine, so just return an error condition.
525          */
526         if (dp->dscr_cmd0 & DSCR_CMD0_V)
527                 return 0;
528
529         /* Load up buffer address and byte count.
530         */
531         dp->dscr_dest0 = virt_to_phys(buf);
532         dp->dscr_cmd1 = nbytes;
533         dp->dscr_cmd0 |= DSCR_CMD0_V;   /* Let it rip */
534
535         /* Get next descriptor pointer.
536         */
537         ctp->put_ptr = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));
538
539         /* return something not zero.
540         */
541         return nbytes;
542 }
543
544 /* Get a destination buffer into the DMA ring.
545  * Normally used to get a full buffer from the ring during fifo
546  * to memory transfers.  This does not set the valid bit, you will
547  * have to put another destination buffer to keep the DMA going.
548  */
549 u32
550 au1xxx_dbdma_get_dest(u32 chanid, void **buf, int *nbytes)
551 {
552         chan_tab_t              *ctp;
553         au1x_ddma_desc_t        *dp;
554         u32                     rv;
555
556         /* I guess we could check this to be within the
557          * range of the table......
558          */
559         ctp = *((chan_tab_t **)chanid);
560
561         /* We should have multiple callers for a particular channel,
562          * an interrupt doesn't affect this pointer nor the descriptor,
563          * so no locking should be needed.
564          */
565         dp = ctp->get_ptr;
566
567         /* If the descriptor is valid, we are way ahead of the DMA
568          * engine, so just return an error condition.
569          */
570         if (dp->dscr_cmd0 & DSCR_CMD0_V)
571                 return 0;
572
573         /* Return buffer address and byte count.
574         */
575         *buf = (void *)(phys_to_virt(dp->dscr_dest0));
576         *nbytes = dp->dscr_cmd1;
577         rv = dp->dscr_stat;
578
579         /* Get next descriptor pointer.
580         */
581         ctp->get_ptr = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));
582
583         /* return something not zero.
584         */
585         return rv;
586 }
587
588 void
589 au1xxx_dbdma_stop(u32 chanid)
590 {
591         chan_tab_t      *ctp;
592         volatile au1x_dma_chan_t *cp;
593         int halt_timeout = 0;
594
595         ctp = *((chan_tab_t **)chanid);
596
597         cp = ctp->chan_ptr;
598         cp->ddma_cfg &= ~DDMA_CFG_EN;   /* Disable channel */
599         au_sync();
600         while (!(cp->ddma_stat & DDMA_STAT_H)) {
601                 udelay(1);
602                 halt_timeout++;
603                 if (halt_timeout > 100) {
604                         printk("warning: DMA channel won't halt\n");
605                         break;
606                 }
607         }
608         /* clear current desc valid and doorbell */
609         cp->ddma_stat |= (DDMA_STAT_DB | DDMA_STAT_V);
610         au_sync();
611 }
612
613 /* Start using the current descriptor pointer.  If the dbdma encounters
614  * a not valid descriptor, it will stop.  In this case, we can just
615  * continue by adding a buffer to the list and starting again.
616  */
617 void
618 au1xxx_dbdma_start(u32 chanid)
619 {
620         chan_tab_t      *ctp;
621         volatile au1x_dma_chan_t *cp;
622
623         ctp = *((chan_tab_t **)chanid);
624
625         cp = ctp->chan_ptr;
626         cp->ddma_desptr = virt_to_phys(ctp->cur_ptr);
627         cp->ddma_cfg |= DDMA_CFG_EN;    /* Enable channel */
628         au_sync();
629         cp->ddma_dbell = 0xffffffff;    /* Make it go */
630         au_sync();
631 }
632
633 void
634 au1xxx_dbdma_reset(u32 chanid)
635 {
636         chan_tab_t              *ctp;
637         au1x_ddma_desc_t        *dp;
638
639         au1xxx_dbdma_stop(chanid);
640
641         ctp = *((chan_tab_t **)chanid);
642         ctp->get_ptr = ctp->put_ptr = ctp->cur_ptr = ctp->chan_desc_base;
643
644         /* Run through the descriptors and reset the valid indicator.
645         */
646         dp = ctp->chan_desc_base;
647
648         do {
649                 dp->dscr_cmd0 &= ~DSCR_CMD0_V;
650                 dp = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));
651         } while (dp != ctp->chan_desc_base);
652 }
653
654 u32
655 au1xxx_get_dma_residue(u32 chanid)
656 {
657         chan_tab_t      *ctp;
658         volatile au1x_dma_chan_t *cp;
659         u32             rv;
660
661         ctp = *((chan_tab_t **)chanid);
662         cp = ctp->chan_ptr;
663
664         /* This is only valid if the channel is stopped.
665         */
666         rv = cp->ddma_bytecnt;
667         au_sync();
668
669         return rv;
670 }
671
672 void
673 au1xxx_dbdma_chan_free(u32 chanid)
674 {
675         chan_tab_t      *ctp;
676         dbdev_tab_t     *stp, *dtp;
677
678         ctp = *((chan_tab_t **)chanid);
679         stp = ctp->chan_src;
680         dtp = ctp->chan_dest;
681
682         au1xxx_dbdma_stop(chanid);
683
684         if (ctp->chan_desc_base != NULL)
685                 kfree(ctp->chan_desc_base);
686
687         stp->dev_flags &= ~DEV_FLAGS_INUSE;
688         dtp->dev_flags &= ~DEV_FLAGS_INUSE;
689         chan_tab_ptr[ctp->chan_index] = NULL;
690
691         kfree(ctp);
692 }
693
694 static void
695 dbdma_interrupt(int irq, void *dev_id, struct pt_regs *regs)
696 {
697         u32     intstat;
698         u32     chan_index;
699         chan_tab_t              *ctp;
700         au1x_ddma_desc_t        *dp;
701         volatile au1x_dma_chan_t *cp;
702
703         intstat = dbdma_gptr->ddma_intstat;
704         au_sync();
705         chan_index = au_ffs(intstat) - 1;
706
707         ctp = chan_tab_ptr[chan_index];
708         cp = ctp->chan_ptr;
709         dp = ctp->cur_ptr;
710
711         /* Reset interrupt.
712         */
713         cp->ddma_irq = 0;
714         au_sync();
715
716         if (ctp->chan_callback)
717                 (ctp->chan_callback)(irq, ctp->chan_callparam, regs);
718
719         ctp->cur_ptr = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));
720
721 }
722
723 static void
724 au1xxx_dbdma_init(void)
725 {
726         dbdma_gptr->ddma_config = 0;
727         dbdma_gptr->ddma_throttle = 0;
728         dbdma_gptr->ddma_inten = 0xffff;
729         au_sync();
730
731         if (request_irq(AU1550_DDMA_INT, dbdma_interrupt, SA_INTERRUPT,
732                         "Au1xxx dbdma", (void *)dbdma_gptr))
733                 printk("Can't get 1550 dbdma irq");
734 }
735
736 void
737 au1xxx_dbdma_dump(u32 chanid)
738 {
739         chan_tab_t              *ctp;
740         au1x_ddma_desc_t        *dp;
741         dbdev_tab_t             *stp, *dtp;
742         volatile au1x_dma_chan_t *cp;
743
744         ctp = *((chan_tab_t **)chanid);
745         stp = ctp->chan_src;
746         dtp = ctp->chan_dest;
747         cp = ctp->chan_ptr;
748
749         printk("Chan %x, stp %x (dev %d)  dtp %x (dev %d) \n",
750                 (u32)ctp, (u32)stp, stp - dbdev_tab, (u32)dtp, dtp - dbdev_tab);
751         printk("desc base %x, get %x, put %x, cur %x\n",
752                 (u32)(ctp->chan_desc_base), (u32)(ctp->get_ptr),
753                 (u32)(ctp->put_ptr), (u32)(ctp->cur_ptr));
754
755         printk("dbdma chan %x\n", (u32)cp);
756         printk("cfg %08x, desptr %08x, statptr %08x\n",
757                 cp->ddma_cfg, cp->ddma_desptr, cp->ddma_statptr);
758         printk("dbell %08x, irq %08x, stat %08x, bytecnt %08x\n",
759                 cp->ddma_dbell, cp->ddma_irq, cp->ddma_stat, cp->ddma_bytecnt);
760
761
762         /* Run through the descriptors
763         */
764         dp = ctp->chan_desc_base;
765
766         do {
767                 printk("dp %08x, cmd0 %08x, cmd1 %08x\n",
768                         (u32)dp, dp->dscr_cmd0, dp->dscr_cmd1);
769                 printk("src0 %08x, src1 %08x, dest0 %08x\n",
770                         dp->dscr_source0, dp->dscr_source1, dp->dscr_dest0);
771                 printk("dest1 %08x, stat %08x, nxtptr %08x\n",
772                         dp->dscr_dest1, dp->dscr_stat, dp->dscr_nxtptr);
773                 dp = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));
774         } while (dp != ctp->chan_desc_base);
775 }