]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/arm/broadcom/bcm2835/bcm2835_dma.c
MFC: r266470, r273546, r276017, r277932, r279153, r279778, r279780, r278797,
[FreeBSD/stable/10.git] / sys / arm / broadcom / bcm2835 / bcm2835_dma.c
1 /*
2  * Copyright (c) 2013 Daisuke Aoyama <aoyama@peach.ne.jp>
3  * Copyright (c) 2013 Oleksandr Tymoshenko <gonzo@bluezbox.com>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/queue.h>
40 #include <sys/resource.h>
41 #include <sys/rman.h>
42
43 #include <dev/fdt/fdt_common.h>
44 #include <dev/ofw/openfirm.h>
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47
48 #include <vm/vm.h>
49 #include <vm/pmap.h>
50 #include <machine/bus.h>
51 #include <machine/cpu.h>
52 #include <machine/cpufunc.h>
53
54 #include "bcm2835_dma.h"
55 #include "bcm2835_vcbus.h"
56
57 #define MAX_REG                 9
58
59 /* private flags */
60 #define BCM_DMA_CH_USED         0x00000001
61 #define BCM_DMA_CH_FREE         0x40000000
62 #define BCM_DMA_CH_UNMAP        0x80000000
63
64 /* Register Map (4.2.1.2) */
65 #define BCM_DMA_CS(n)           (0x100*(n) + 0x00)
66 #define         CS_ACTIVE               (1 <<  0)
67 #define         CS_END                  (1 <<  1)
68 #define         CS_INT                  (1 <<  2)
69 #define         CS_DREQ                 (1 <<  3)
70 #define         CS_ISPAUSED             (1 <<  4)
71 #define         CS_ISHELD               (1 <<  5)
72 #define         CS_ISWAIT               (1 <<  6)
73 #define         CS_ERR                  (1 <<  8)
74 #define         CS_WAITWRT              (1 << 28)
75 #define         CS_DISDBG               (1 << 29)
76 #define         CS_ABORT                (1 << 30)
77 #define         CS_RESET                (1U << 31)
78 #define BCM_DMA_CBADDR(n)       (0x100*(n) + 0x04)
79 #define BCM_DMA_INFO(n)         (0x100*(n) + 0x08)
80 #define         INFO_INT_EN             (1 << 0)
81 #define         INFO_TDMODE             (1 << 1)
82 #define         INFO_WAIT_RESP          (1 << 3)
83 #define         INFO_D_INC              (1 << 4)
84 #define         INFO_D_WIDTH            (1 << 5)
85 #define         INFO_D_DREQ             (1 << 6)
86 #define         INFO_S_INC              (1 << 8)
87 #define         INFO_S_WIDTH            (1 << 9)
88 #define         INFO_S_DREQ             (1 << 10)
89 #define         INFO_WAITS_SHIFT        (21)
90 #define         INFO_PERMAP_SHIFT       (16)
91 #define         INFO_PERMAP_MASK        (0x1f << INFO_PERMAP_SHIFT)
92
93 #define BCM_DMA_SRC(n)          (0x100*(n) + 0x0C)
94 #define BCM_DMA_DST(n)          (0x100*(n) + 0x10)
95 #define BCM_DMA_LEN(n)          (0x100*(n) + 0x14)
96 #define BCM_DMA_STRIDE(n)       (0x100*(n) + 0x18)
97 #define BCM_DMA_CBNEXT(n)       (0x100*(n) + 0x1C)
98 #define BCM_DMA_DEBUG(n)        (0x100*(n) + 0x20)
99 #define         DEBUG_ERROR_MASK        (7)
100
101 #define BCM_DMA_INT_STATUS      0xfe0
102 #define BCM_DMA_ENABLE          0xff0
103
104 /* relative offset from BCM_VC_DMA0_BASE (p.39) */
105 #define BCM_DMA_CH(n)           (0x100*(n))
106
107 /* channels used by GPU */
108 #define BCM_DMA_CH_BULK         0
109 #define BCM_DMA_CH_FAST1        2
110 #define BCM_DMA_CH_FAST2        3
111
112 #define BCM_DMA_CH_GPU_MASK     ((1 << BCM_DMA_CH_BULK) |       \
113                                  (1 << BCM_DMA_CH_FAST1) |      \
114                                  (1 << BCM_DMA_CH_FAST2))
115
116 /* DMA Control Block - 256bit aligned (p.40) */
117 struct bcm_dma_cb {
118         uint32_t info;          /* Transfer Information */
119         uint32_t src;           /* Source Address */
120         uint32_t dst;           /* Destination Address */
121         uint32_t len;           /* Transfer Length */
122         uint32_t stride;        /* 2D Mode Stride */
123         uint32_t next;          /* Next Control Block Address */
124         uint32_t rsvd1;         /* Reserved */
125         uint32_t rsvd2;         /* Reserved */
126 };
127
128 #ifdef DEBUG
129 static void bcm_dma_cb_dump(struct bcm_dma_cb *cb);
130 static void bcm_dma_reg_dump(int ch);
131 #endif
132
133 /* DMA channel private info */
134 struct bcm_dma_ch {
135         int                     ch;
136         uint32_t                flags;
137         struct bcm_dma_cb *     cb;
138         uint32_t                vc_cb;
139         bus_dmamap_t            dma_map;
140         void                    (*intr_func)(int, void *);
141         void *                  intr_arg;
142 };
143
144 struct bcm_dma_softc {
145         device_t                sc_dev;
146         struct mtx              sc_mtx;
147         struct resource *       sc_mem;
148         struct resource *       sc_irq[BCM_DMA_CH_MAX];
149         void *                  sc_intrhand[BCM_DMA_CH_MAX];
150         struct bcm_dma_ch       sc_dma_ch[BCM_DMA_CH_MAX];
151         bus_dma_tag_t           sc_dma_tag;
152 };
153
154 static struct bcm_dma_softc *bcm_dma_sc = NULL;
155 static uint32_t bcm_dma_channel_mask;
156
157 static struct ofw_compat_data compat_data[] = {
158         {"broadcom,bcm2835-dma",        1},
159         {"brcm,bcm2835-dma",            1},
160         {NULL,                          0}
161 };
162
163 static void
164 bcm_dmamap_cb(void *arg, bus_dma_segment_t *segs,
165         int nseg, int err)
166 {
167         bus_addr_t *addr;
168
169         if (err)
170                 return;
171
172         addr = (bus_addr_t*)arg;
173         *addr = PHYS_TO_VCBUS(segs[0].ds_addr);
174 }
175
176 static void
177 bcm_dma_reset(device_t dev, int ch)
178 {
179         struct bcm_dma_softc *sc = device_get_softc(dev);
180         struct bcm_dma_cb *cb;
181         uint32_t cs;
182         int count;
183
184         if (ch < 0 || ch >= BCM_DMA_CH_MAX)
185                 return;
186
187         cs = bus_read_4(sc->sc_mem, BCM_DMA_CS(ch));
188
189         if (cs & CS_ACTIVE) {
190                 /* pause current task */
191                 bus_write_4(sc->sc_mem, BCM_DMA_CS(ch), 0);
192
193                 count = 1000;
194                 do {
195                         cs = bus_read_4(sc->sc_mem, BCM_DMA_CS(ch));
196                 } while (!(cs & CS_ISPAUSED) && (count-- > 0));
197
198                 if (!(cs & CS_ISPAUSED)) {
199                         device_printf(dev,
200                             "Can't abort DMA transfer at channel %d\n", ch);
201                 }
202
203                 bus_write_4(sc->sc_mem, BCM_DMA_CBNEXT(ch), 0);
204
205                 /* Complete everything, clear interrupt */
206                 bus_write_4(sc->sc_mem, BCM_DMA_CS(ch),
207                     CS_ABORT | CS_INT | CS_END| CS_ACTIVE);
208         }
209
210         /* clear control blocks */
211         bus_write_4(sc->sc_mem, BCM_DMA_CBADDR(ch), 0);
212         bus_write_4(sc->sc_mem, BCM_DMA_CBNEXT(ch), 0);
213
214         /* Reset control block */
215         cb = sc->sc_dma_ch[ch].cb;
216         bzero(cb, sizeof(*cb));
217         cb->info = INFO_WAIT_RESP;
218 }
219
220 static int
221 bcm_dma_init(device_t dev)
222 {
223         struct bcm_dma_softc *sc = device_get_softc(dev);
224         uint32_t reg;
225         struct bcm_dma_ch *ch;
226         void *cb_virt;
227         vm_paddr_t cb_phys;
228         int err;
229         int i;
230
231         /*
232          * Only channels set in bcm_dma_channel_mask can be controlled by us.
233          * The others are out of our control as well as the corresponding bits
234          * in both BCM_DMA_ENABLE and BCM_DMA_INT_STATUS global registers. As
235          * these registers are RW ones, there is no safe way how to write only
236          * the bits which can be controlled by us.
237          *
238          * Fortunately, after reset, all channels are enabled in BCM_DMA_ENABLE
239          * register and all statuses are cleared in BCM_DMA_INT_STATUS one.
240          * Not touching these registers is a trade off between correct
241          * initialization which does not count on anything and not messing up
242          * something we have no control over.
243          */
244         reg = bus_read_4(sc->sc_mem, BCM_DMA_ENABLE);
245         if ((reg & bcm_dma_channel_mask) != bcm_dma_channel_mask)
246                 device_printf(dev, "channels are not enabled\n");
247         reg = bus_read_4(sc->sc_mem, BCM_DMA_INT_STATUS);
248         if ((reg & bcm_dma_channel_mask) != 0)
249                 device_printf(dev, "statuses are not cleared\n");
250
251         /* Allocate DMA chunks control blocks */
252         /* p.40 of spec - control block should be 32-bit aligned */
253         err = bus_dma_tag_create(bus_get_dma_tag(dev),
254             1, 0, BUS_SPACE_MAXADDR_32BIT,
255             BUS_SPACE_MAXADDR, NULL, NULL,
256             sizeof(struct bcm_dma_cb), 1,
257             sizeof(struct bcm_dma_cb),
258             BUS_DMA_ALLOCNOW, NULL, NULL,
259             &sc->sc_dma_tag);
260
261         if (err) {
262                 device_printf(dev, "failed allocate DMA tag\n");
263                 return (err);
264         }
265
266         /* setup initial settings */
267         for (i = 0; i < BCM_DMA_CH_MAX; i++) {
268                 ch = &sc->sc_dma_ch[i];
269
270                 bzero(ch, sizeof(struct bcm_dma_ch));
271                 ch->ch = i;
272                 ch->flags = BCM_DMA_CH_UNMAP;
273
274                 if ((bcm_dma_channel_mask & (1 << i)) == 0)
275                         continue;
276
277                 err = bus_dmamem_alloc(sc->sc_dma_tag, &cb_virt,
278                     BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
279                     &ch->dma_map);
280                 if (err) {
281                         device_printf(dev, "cannot allocate DMA memory\n");
282                         break;
283                 }
284
285                 /* 
286                  * Least alignment for busdma-allocated stuff is cache 
287                  * line size, so just make sure nothing stupid happend
288                  * and we got properly aligned address
289                  */
290                 if ((uintptr_t)cb_virt & 0x1f) {
291                         device_printf(dev,
292                             "DMA address is not 32-bytes aligned: %p\n",
293                             (void*)cb_virt);
294                         break;
295                 }
296
297                 err = bus_dmamap_load(sc->sc_dma_tag, ch->dma_map, cb_virt,
298                     sizeof(struct bcm_dma_cb), bcm_dmamap_cb, &cb_phys,
299                     BUS_DMA_WAITOK);
300                 if (err) {
301                         device_printf(dev, "cannot load DMA memory\n");
302                         break;
303                 }
304
305                 ch->cb = cb_virt;
306                 ch->vc_cb = cb_phys;
307                 ch->flags = BCM_DMA_CH_FREE;
308                 ch->cb->info = INFO_WAIT_RESP;
309
310                 /* reset DMA engine */
311                 bus_write_4(sc->sc_mem, BCM_DMA_CS(i), CS_RESET);
312         }
313
314         return (0);
315 }
316
317 /*
318  * Allocate DMA channel for further use, returns channel # or
319  *     BCM_DMA_CH_INVALID
320  */
321 int
322 bcm_dma_allocate(int req_ch)
323 {
324         struct bcm_dma_softc *sc = bcm_dma_sc;
325         int ch = BCM_DMA_CH_INVALID;
326         int i;
327
328         if (req_ch >= BCM_DMA_CH_MAX)
329                 return (BCM_DMA_CH_INVALID);
330
331         /* Auto(req_ch < 0) or CH specified */
332         mtx_lock(&sc->sc_mtx);
333
334         if (req_ch < 0) {
335                 for (i = 0; i < BCM_DMA_CH_MAX; i++) {
336                         if (sc->sc_dma_ch[i].flags & BCM_DMA_CH_FREE) {
337                                 ch = i;
338                                 sc->sc_dma_ch[ch].flags &= ~BCM_DMA_CH_FREE;
339                                 sc->sc_dma_ch[ch].flags |= BCM_DMA_CH_USED;
340                                 break;
341                         }
342                 }
343         }
344         else {
345                 if (sc->sc_dma_ch[req_ch].flags & BCM_DMA_CH_FREE) {
346                         ch = req_ch;
347                         sc->sc_dma_ch[ch].flags &= ~BCM_DMA_CH_FREE;
348                         sc->sc_dma_ch[ch].flags |= BCM_DMA_CH_USED;
349                 }
350         }
351
352         mtx_unlock(&sc->sc_mtx);
353         return (ch);
354 }
355
356 /*
357  * Frees allocated channel. Returns 0 on success, -1 otherwise
358  */
359 int
360 bcm_dma_free(int ch)
361 {
362         struct bcm_dma_softc *sc = bcm_dma_sc;
363
364         if (ch < 0 || ch >= BCM_DMA_CH_MAX)
365                 return (-1);
366
367         mtx_lock(&sc->sc_mtx);
368         if (sc->sc_dma_ch[ch].flags & BCM_DMA_CH_USED) {
369                 sc->sc_dma_ch[ch].flags |= BCM_DMA_CH_FREE;
370                 sc->sc_dma_ch[ch].flags &= ~BCM_DMA_CH_USED;
371                 sc->sc_dma_ch[ch].intr_func = NULL;
372                 sc->sc_dma_ch[ch].intr_arg = NULL;
373
374                 /* reset DMA engine */
375                 bcm_dma_reset(sc->sc_dev, ch);
376         }
377
378         mtx_unlock(&sc->sc_mtx);
379         return (0);
380 }
381
382 /*
383  * Assign handler function for channel interrupt
384  * Returns 0 on success, -1 otherwise
385  */
386 int
387 bcm_dma_setup_intr(int ch, void (*func)(int, void *), void *arg)
388 {
389         struct bcm_dma_softc *sc = bcm_dma_sc;
390         struct bcm_dma_cb *cb;
391
392         if (ch < 0 || ch >= BCM_DMA_CH_MAX)
393                 return (-1);
394
395         if (!(sc->sc_dma_ch[ch].flags & BCM_DMA_CH_USED))
396                 return (-1);
397
398         sc->sc_dma_ch[ch].intr_func = func;
399         sc->sc_dma_ch[ch].intr_arg = arg;
400         cb = sc->sc_dma_ch[ch].cb;
401         cb->info |= INFO_INT_EN;
402
403         return (0);
404 }
405
406 /*
407  * Setup DMA source parameters
408  *     ch - channel number
409  *     dreq - hardware DREQ # or BCM_DMA_DREQ_NONE if
410  *         source is physical memory
411  *     inc_addr - BCM_DMA_INC_ADDR if source address
412  *         should be increased after each access or 
413  *         BCM_DMA_SAME_ADDR if address should remain 
414  *         the same
415  *     width - size of read operation, BCM_DMA_32BIT
416  *         for 32bit bursts, BCM_DMA_128BIT for 128 bits
417  *        
418  * Returns 0 on success, -1 otherwise
419  */
420 int
421 bcm_dma_setup_src(int ch, int dreq, int inc_addr, int width)
422 {
423         struct bcm_dma_softc *sc = bcm_dma_sc;
424         uint32_t info;
425
426         if (ch < 0 || ch >= BCM_DMA_CH_MAX)
427                 return (-1);
428
429         if (!(sc->sc_dma_ch[ch].flags & BCM_DMA_CH_USED))
430                 return (-1);
431
432         info = sc->sc_dma_ch[ch].cb->info;
433         info &= ~INFO_PERMAP_MASK;
434         info |= (dreq << INFO_PERMAP_SHIFT) & INFO_PERMAP_MASK;
435
436         if (dreq)
437                 info |= INFO_S_DREQ;
438         else
439                 info &= ~INFO_S_DREQ;
440
441         if (width == BCM_DMA_128BIT)
442                 info |= INFO_S_WIDTH;
443         else
444                 info &= ~INFO_S_WIDTH;
445
446         if (inc_addr == BCM_DMA_INC_ADDR)
447                 info |= INFO_S_INC;
448         else
449                 info &= ~INFO_S_INC;
450
451         sc->sc_dma_ch[ch].cb->info = info;
452
453         return (0);
454 }
455
456 /*
457  * Setup DMA destination parameters
458  *     ch - channel number
459  *     dreq - hardware DREQ # or BCM_DMA_DREQ_NONE if
460  *         destination is physical memory
461  *     inc_addr - BCM_DMA_INC_ADDR if source address
462  *         should be increased after each access or 
463  *         BCM_DMA_SAME_ADDR if address should remain 
464  *         the same
465  *     width - size of write operation, BCM_DMA_32BIT
466  *         for 32bit bursts, BCM_DMA_128BIT for 128 bits
467  *        
468  * Returns 0 on success, -1 otherwise
469  */
470 int
471 bcm_dma_setup_dst(int ch, int dreq, int inc_addr, int width)
472 {
473         struct bcm_dma_softc *sc = bcm_dma_sc;
474         uint32_t info;
475
476         if (ch < 0 || ch >= BCM_DMA_CH_MAX)
477                 return (-1);
478
479         if (!(sc->sc_dma_ch[ch].flags & BCM_DMA_CH_USED))
480                 return (-1);
481
482         info = sc->sc_dma_ch[ch].cb->info;
483         info &= ~INFO_PERMAP_MASK;
484         info |= (dreq << INFO_PERMAP_SHIFT) & INFO_PERMAP_MASK;
485
486         if (dreq)
487                 info |= INFO_D_DREQ;
488         else
489                 info &= ~INFO_D_DREQ;
490
491         if (width == BCM_DMA_128BIT)
492                 info |= INFO_D_WIDTH;
493         else
494                 info &= ~INFO_D_WIDTH;
495
496         if (inc_addr == BCM_DMA_INC_ADDR)
497                 info |= INFO_D_INC;
498         else
499                 info &= ~INFO_D_INC;
500
501         sc->sc_dma_ch[ch].cb->info = info;
502
503         return (0);
504 }
505
506 #ifdef DEBUG
507 void
508 bcm_dma_cb_dump(struct bcm_dma_cb *cb)
509 {
510
511         printf("DMA CB ");
512         printf("INFO: %8.8x ", cb->info);
513         printf("SRC: %8.8x ", cb->src);
514         printf("DST: %8.8x ", cb->dst);
515         printf("LEN: %8.8x ", cb->len);
516         printf("\n");
517         printf("STRIDE: %8.8x ", cb->stride);
518         printf("NEXT: %8.8x ", cb->next);
519         printf("RSVD1: %8.8x ", cb->rsvd1);
520         printf("RSVD2: %8.8x ", cb->rsvd2);
521         printf("\n");
522 }
523
524 void
525 bcm_dma_reg_dump(int ch)
526 {
527         struct bcm_dma_softc *sc = bcm_dma_sc;
528         int i;
529         uint32_t reg;
530
531         if (ch < 0 || ch >= BCM_DMA_CH_MAX)
532                 return;
533
534         printf("DMA%d: ", ch);
535         for (i = 0; i < MAX_REG; i++) {
536                 reg = bus_read_4(sc->sc_mem, BCM_DMA_CH(ch) + i*4);
537                 printf("%8.8x ", reg);
538         }
539         printf("\n");
540 }
541 #endif
542
543 /*
544  * Start DMA transaction
545  *     ch - channel number
546  *     src, dst - source and destination address in
547  *         ARM physical memory address space. 
548  *     len - amount of bytes to be transfered
549  *        
550  * Returns 0 on success, -1 otherwise
551  */
552 int
553 bcm_dma_start(int ch, vm_paddr_t src, vm_paddr_t dst, int len)
554 {
555         struct bcm_dma_softc *sc = bcm_dma_sc;
556         struct bcm_dma_cb *cb;
557
558         if (ch < 0 || ch >= BCM_DMA_CH_MAX)
559                 return (-1);
560
561         if (!(sc->sc_dma_ch[ch].flags & BCM_DMA_CH_USED))
562                 return (-1);
563
564         cb = sc->sc_dma_ch[ch].cb;
565         if (BCM2835_ARM_IS_IO(src))
566                 cb->src = IO_TO_VCBUS(src);
567         else
568                 cb->src = PHYS_TO_VCBUS(src);
569         if (BCM2835_ARM_IS_IO(dst))
570                 cb->dst = IO_TO_VCBUS(dst);
571         else
572                 cb->dst = PHYS_TO_VCBUS(dst);
573         cb->len = len;
574
575         bus_dmamap_sync(sc->sc_dma_tag,
576             sc->sc_dma_ch[ch].dma_map, BUS_DMASYNC_PREWRITE);
577
578         bus_write_4(sc->sc_mem, BCM_DMA_CBADDR(ch),
579             sc->sc_dma_ch[ch].vc_cb);
580         bus_write_4(sc->sc_mem, BCM_DMA_CS(ch), CS_ACTIVE);
581
582 #ifdef DEBUG
583         bcm_dma_cb_dump(sc->sc_dma_ch[ch].cb);
584         bcm_dma_reg_dump(ch);
585 #endif
586
587         return (0);
588 }
589
590 /*
591  * Get length requested for DMA transaction
592  *     ch - channel number
593  *        
594  * Returns size of transaction, 0 if channel is invalid
595  */
596 uint32_t
597 bcm_dma_length(int ch)
598 {
599         struct bcm_dma_softc *sc = bcm_dma_sc;
600         struct bcm_dma_cb *cb;
601
602         if (ch < 0 || ch >= BCM_DMA_CH_MAX)
603                 return (0);
604
605         if (!(sc->sc_dma_ch[ch].flags & BCM_DMA_CH_USED))
606                 return (0);
607
608         cb = sc->sc_dma_ch[ch].cb;
609
610         return (cb->len);
611 }
612
613 static void
614 bcm_dma_intr(void *arg)
615 {
616         struct bcm_dma_softc *sc = bcm_dma_sc;
617         struct bcm_dma_ch *ch = (struct bcm_dma_ch *)arg;
618         uint32_t cs, debug;
619
620         /* my interrupt? */
621         cs = bus_read_4(sc->sc_mem, BCM_DMA_CS(ch->ch));
622
623         if (!(cs & (CS_INT | CS_ERR))) {
624                 device_printf(sc->sc_dev,
625                     "unexpected DMA intr CH=%d, CS=%x\n", ch->ch, cs);
626                 return;
627         }
628
629         /* running? */
630         if (!(ch->flags & BCM_DMA_CH_USED)) {
631                 device_printf(sc->sc_dev,
632                     "unused DMA intr CH=%d, CS=%x\n", ch->ch, cs);
633                 return;
634         }
635
636         if (cs & CS_ERR) {
637                 debug = bus_read_4(sc->sc_mem, BCM_DMA_DEBUG(ch->ch));
638                 device_printf(sc->sc_dev, "DMA error %d on CH%d\n",
639                         debug & DEBUG_ERROR_MASK, ch->ch);
640                 bus_write_4(sc->sc_mem, BCM_DMA_DEBUG(ch->ch), 
641                     debug & DEBUG_ERROR_MASK);
642                 bcm_dma_reset(sc->sc_dev, ch->ch);
643         }
644
645         if (cs & CS_INT) {
646                 /* acknowledge interrupt */
647                 bus_write_4(sc->sc_mem, BCM_DMA_CS(ch->ch), 
648                     CS_INT | CS_END);
649
650                 /* Prepare for possible access to len field */
651                 bus_dmamap_sync(sc->sc_dma_tag, ch->dma_map,
652                     BUS_DMASYNC_POSTWRITE);
653
654                 /* save callback function and argument */
655                 if (ch->intr_func)
656                         ch->intr_func(ch->ch, ch->intr_arg);
657         }
658 }
659
660 static int
661 bcm_dma_probe(device_t dev)
662 {
663
664         if (!ofw_bus_status_okay(dev))
665                 return (ENXIO);
666
667         if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
668                 return (ENXIO);
669
670         device_set_desc(dev, "BCM2835 DMA Controller");
671         return (BUS_PROBE_DEFAULT);
672 }
673
674 static int
675 bcm_dma_attach(device_t dev)
676 {
677         struct bcm_dma_softc *sc = device_get_softc(dev);
678         phandle_t node;
679         int rid, err = 0;
680         int i;
681
682         sc->sc_dev = dev;
683
684         if (bcm_dma_sc)
685                 return (ENXIO);
686
687         for (i = 0; i < BCM_DMA_CH_MAX; i++) {
688                 sc->sc_irq[i] = NULL;
689                 sc->sc_intrhand[i] = NULL;
690         }
691
692         /* Get DMA channel mask. */
693         node = ofw_bus_get_node(sc->sc_dev);
694         if (OF_getencprop(node, "brcm,dma-channel-mask", &bcm_dma_channel_mask,
695             sizeof(bcm_dma_channel_mask)) == -1 &&
696             OF_getencprop(node, "broadcom,channels", &bcm_dma_channel_mask,
697             sizeof(bcm_dma_channel_mask)) == -1) {
698                 device_printf(dev, "could not get channel mask property\n");
699                 return (ENXIO);
700         }
701
702         /* Mask out channels used by GPU. */
703         bcm_dma_channel_mask &= ~BCM_DMA_CH_GPU_MASK;
704
705         /* DMA0 - DMA14 */
706         rid = 0;
707         sc->sc_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
708         if (sc->sc_mem == NULL) {
709                 device_printf(dev, "could not allocate memory resource\n");
710                 return (ENXIO);
711         }
712
713         /* IRQ DMA0 - DMA11 XXX NOT USE DMA12(spurious?) */
714         for (rid = 0; rid < BCM_DMA_CH_MAX; rid++) {
715                 if ((bcm_dma_channel_mask & (1 << rid)) == 0)
716                         continue;
717
718                 sc->sc_irq[rid] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
719                                                        RF_ACTIVE);
720                 if (sc->sc_irq[rid] == NULL) {
721                         device_printf(dev, "cannot allocate interrupt\n");
722                         err = ENXIO;
723                         goto fail;
724                 }
725                 if (bus_setup_intr(dev, sc->sc_irq[rid], INTR_TYPE_MISC | INTR_MPSAFE,
726                                    NULL, bcm_dma_intr, &sc->sc_dma_ch[rid],
727                                    &sc->sc_intrhand[rid])) {
728                         device_printf(dev, "cannot setup interrupt handler\n");
729                         err = ENXIO;
730                         goto fail;
731                 }
732         }
733
734         mtx_init(&sc->sc_mtx, "bcmdma", "bcmdma", MTX_DEF);
735         bcm_dma_sc = sc;
736
737         err = bcm_dma_init(dev);
738         if (err)
739                 goto fail;
740
741         return (err);
742
743 fail:
744         if (sc->sc_mem)
745                 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem);
746
747         for (i = 0; i < BCM_DMA_CH_MAX; i++) {
748                 if (sc->sc_intrhand[i])
749                         bus_teardown_intr(dev, sc->sc_irq[i], sc->sc_intrhand[i]);
750                 if (sc->sc_irq[i])
751                         bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq[i]);
752         }
753
754         return (err);
755 }
756
757 static device_method_t bcm_dma_methods[] = {
758         DEVMETHOD(device_probe,         bcm_dma_probe),
759         DEVMETHOD(device_attach,        bcm_dma_attach),
760         { 0, 0 }
761 };
762
763 static driver_t bcm_dma_driver = {
764         "bcm_dma",
765         bcm_dma_methods,
766         sizeof(struct bcm_dma_softc),
767 };
768
769 static devclass_t bcm_dma_devclass;
770
771 DRIVER_MODULE(bcm_dma, simplebus, bcm_dma_driver, bcm_dma_devclass, 0, 0);
772 MODULE_VERSION(bcm_dma, 1);