2 * Copyright (c) 2013 Daisuke Aoyama <aoyama@peach.ne.jp>
3 * Copyright (c) 2013 Oleksandr Tymoshenko <gonzo@bluezbox.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
34 #include <sys/kernel.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>
43 #include <dev/ofw/openfirm.h>
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
49 #include <machine/bus.h>
51 #include "bcm2835_dma.h"
52 #include "bcm2835_vcbus.h"
57 #define BCM_DMA_CH_USED 0x00000001
58 #define BCM_DMA_CH_FREE 0x40000000
59 #define BCM_DMA_CH_UNMAP 0x80000000
61 /* Register Map (4.2.1.2) */
62 #define BCM_DMA_CS(n) (0x100*(n) + 0x00)
63 #define CS_ACTIVE (1 << 0)
64 #define CS_END (1 << 1)
65 #define CS_INT (1 << 2)
66 #define CS_DREQ (1 << 3)
67 #define CS_ISPAUSED (1 << 4)
68 #define CS_ISHELD (1 << 5)
69 #define CS_ISWAIT (1 << 6)
70 #define CS_ERR (1 << 8)
71 #define CS_WAITWRT (1 << 28)
72 #define CS_DISDBG (1 << 29)
73 #define CS_ABORT (1 << 30)
74 #define CS_RESET (1U << 31)
75 #define BCM_DMA_CBADDR(n) (0x100*(n) + 0x04)
76 #define BCM_DMA_INFO(n) (0x100*(n) + 0x08)
77 #define INFO_INT_EN (1 << 0)
78 #define INFO_TDMODE (1 << 1)
79 #define INFO_WAIT_RESP (1 << 3)
80 #define INFO_D_INC (1 << 4)
81 #define INFO_D_WIDTH (1 << 5)
82 #define INFO_D_DREQ (1 << 6)
83 #define INFO_S_INC (1 << 8)
84 #define INFO_S_WIDTH (1 << 9)
85 #define INFO_S_DREQ (1 << 10)
86 #define INFO_WAITS_SHIFT (21)
87 #define INFO_PERMAP_SHIFT (16)
88 #define INFO_PERMAP_MASK (0x1f << INFO_PERMAP_SHIFT)
90 #define BCM_DMA_SRC(n) (0x100*(n) + 0x0C)
91 #define BCM_DMA_DST(n) (0x100*(n) + 0x10)
92 #define BCM_DMA_LEN(n) (0x100*(n) + 0x14)
93 #define BCM_DMA_STRIDE(n) (0x100*(n) + 0x18)
94 #define BCM_DMA_CBNEXT(n) (0x100*(n) + 0x1C)
95 #define BCM_DMA_DEBUG(n) (0x100*(n) + 0x20)
96 #define DEBUG_ERROR_MASK (7)
98 #define BCM_DMA_INT_STATUS 0xfe0
99 #define BCM_DMA_ENABLE 0xff0
101 /* relative offset from BCM_VC_DMA0_BASE (p.39) */
102 #define BCM_DMA_CH(n) (0x100*(n))
104 /* channels used by GPU */
105 #define BCM_DMA_CH_BULK 0
106 #define BCM_DMA_CH_FAST1 2
107 #define BCM_DMA_CH_FAST2 3
109 #define BCM_DMA_CH_GPU_MASK ((1 << BCM_DMA_CH_BULK) | \
110 (1 << BCM_DMA_CH_FAST1) | \
111 (1 << BCM_DMA_CH_FAST2))
113 /* DMA Control Block - 256bit aligned (p.40) */
115 uint32_t info; /* Transfer Information */
116 uint32_t src; /* Source Address */
117 uint32_t dst; /* Destination Address */
118 uint32_t len; /* Transfer Length */
119 uint32_t stride; /* 2D Mode Stride */
120 uint32_t next; /* Next Control Block Address */
121 uint32_t rsvd1; /* Reserved */
122 uint32_t rsvd2; /* Reserved */
126 static void bcm_dma_cb_dump(struct bcm_dma_cb *cb);
127 static void bcm_dma_reg_dump(int ch);
130 /* DMA channel private info */
134 struct bcm_dma_cb * cb;
136 bus_dmamap_t dma_map;
137 void (*intr_func)(int, void *);
141 struct bcm_dma_softc {
144 struct resource * sc_mem;
145 struct resource * sc_irq[BCM_DMA_CH_MAX];
146 void * sc_intrhand[BCM_DMA_CH_MAX];
147 struct bcm_dma_ch sc_dma_ch[BCM_DMA_CH_MAX];
148 bus_dma_tag_t sc_dma_tag;
151 static struct bcm_dma_softc *bcm_dma_sc = NULL;
152 static uint32_t bcm_dma_channel_mask;
154 static struct ofw_compat_data compat_data[] = {
155 {"broadcom,bcm2835-dma", 1},
156 {"brcm,bcm2835-dma", 1},
161 bcm_dmamap_cb(void *arg, bus_dma_segment_t *segs,
169 addr = (bus_addr_t*)arg;
170 *addr = PHYS_TO_VCBUS(segs[0].ds_addr);
174 bcm_dma_reset(device_t dev, int ch)
176 struct bcm_dma_softc *sc = device_get_softc(dev);
177 struct bcm_dma_cb *cb;
181 if (ch < 0 || ch >= BCM_DMA_CH_MAX)
184 cs = bus_read_4(sc->sc_mem, BCM_DMA_CS(ch));
186 if (cs & CS_ACTIVE) {
187 /* pause current task */
188 bus_write_4(sc->sc_mem, BCM_DMA_CS(ch), 0);
192 cs = bus_read_4(sc->sc_mem, BCM_DMA_CS(ch));
193 } while (!(cs & CS_ISPAUSED) && (count-- > 0));
195 if (!(cs & CS_ISPAUSED)) {
197 "Can't abort DMA transfer at channel %d\n", ch);
200 bus_write_4(sc->sc_mem, BCM_DMA_CBNEXT(ch), 0);
202 /* Complete everything, clear interrupt */
203 bus_write_4(sc->sc_mem, BCM_DMA_CS(ch),
204 CS_ABORT | CS_INT | CS_END| CS_ACTIVE);
207 /* clear control blocks */
208 bus_write_4(sc->sc_mem, BCM_DMA_CBADDR(ch), 0);
209 bus_write_4(sc->sc_mem, BCM_DMA_CBNEXT(ch), 0);
211 /* Reset control block */
212 cb = sc->sc_dma_ch[ch].cb;
213 bzero(cb, sizeof(*cb));
214 cb->info = INFO_WAIT_RESP;
218 bcm_dma_init(device_t dev)
220 struct bcm_dma_softc *sc = device_get_softc(dev);
222 struct bcm_dma_ch *ch;
229 * Only channels set in bcm_dma_channel_mask can be controlled by us.
230 * The others are out of our control as well as the corresponding bits
231 * in both BCM_DMA_ENABLE and BCM_DMA_INT_STATUS global registers. As
232 * these registers are RW ones, there is no safe way how to write only
233 * the bits which can be controlled by us.
235 * Fortunately, after reset, all channels are enabled in BCM_DMA_ENABLE
236 * register and all statuses are cleared in BCM_DMA_INT_STATUS one.
237 * Not touching these registers is a trade off between correct
238 * initialization which does not count on anything and not messing up
239 * something we have no control over.
241 reg = bus_read_4(sc->sc_mem, BCM_DMA_ENABLE);
242 if ((reg & bcm_dma_channel_mask) != bcm_dma_channel_mask)
243 device_printf(dev, "channels are not enabled\n");
244 reg = bus_read_4(sc->sc_mem, BCM_DMA_INT_STATUS);
245 if ((reg & bcm_dma_channel_mask) != 0)
246 device_printf(dev, "statuses are not cleared\n");
248 /* Allocate DMA chunks control blocks */
249 /* p.40 of spec - control block should be 32-bit aligned */
250 err = bus_dma_tag_create(bus_get_dma_tag(dev),
251 1, 0, BUS_SPACE_MAXADDR_32BIT,
252 BUS_SPACE_MAXADDR, NULL, NULL,
253 sizeof(struct bcm_dma_cb), 1,
254 sizeof(struct bcm_dma_cb),
255 BUS_DMA_ALLOCNOW, NULL, NULL,
259 device_printf(dev, "failed allocate DMA tag\n");
263 /* setup initial settings */
264 for (i = 0; i < BCM_DMA_CH_MAX; i++) {
265 ch = &sc->sc_dma_ch[i];
267 bzero(ch, sizeof(struct bcm_dma_ch));
269 ch->flags = BCM_DMA_CH_UNMAP;
271 if ((bcm_dma_channel_mask & (1 << i)) == 0)
274 err = bus_dmamem_alloc(sc->sc_dma_tag, &cb_virt,
275 BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
278 device_printf(dev, "cannot allocate DMA memory\n");
283 * Least alignment for busdma-allocated stuff is cache
284 * line size, so just make sure nothing stupid happened
285 * and we got properly aligned address
287 if ((uintptr_t)cb_virt & 0x1f) {
289 "DMA address is not 32-bytes aligned: %p\n",
294 err = bus_dmamap_load(sc->sc_dma_tag, ch->dma_map, cb_virt,
295 sizeof(struct bcm_dma_cb), bcm_dmamap_cb, &cb_phys,
298 device_printf(dev, "cannot load DMA memory\n");
304 ch->flags = BCM_DMA_CH_FREE;
305 ch->cb->info = INFO_WAIT_RESP;
307 /* reset DMA engine */
308 bus_write_4(sc->sc_mem, BCM_DMA_CS(i), CS_RESET);
315 * Allocate DMA channel for further use, returns channel # or
319 bcm_dma_allocate(int req_ch)
321 struct bcm_dma_softc *sc = bcm_dma_sc;
322 int ch = BCM_DMA_CH_INVALID;
325 if (req_ch >= BCM_DMA_CH_MAX)
326 return (BCM_DMA_CH_INVALID);
328 /* Auto(req_ch < 0) or CH specified */
329 mtx_lock(&sc->sc_mtx);
332 for (i = 0; i < BCM_DMA_CH_MAX; i++) {
333 if (sc->sc_dma_ch[i].flags & BCM_DMA_CH_FREE) {
335 sc->sc_dma_ch[ch].flags &= ~BCM_DMA_CH_FREE;
336 sc->sc_dma_ch[ch].flags |= BCM_DMA_CH_USED;
342 if (sc->sc_dma_ch[req_ch].flags & BCM_DMA_CH_FREE) {
344 sc->sc_dma_ch[ch].flags &= ~BCM_DMA_CH_FREE;
345 sc->sc_dma_ch[ch].flags |= BCM_DMA_CH_USED;
349 mtx_unlock(&sc->sc_mtx);
354 * Frees allocated channel. Returns 0 on success, -1 otherwise
359 struct bcm_dma_softc *sc = bcm_dma_sc;
361 if (ch < 0 || ch >= BCM_DMA_CH_MAX)
364 mtx_lock(&sc->sc_mtx);
365 if (sc->sc_dma_ch[ch].flags & BCM_DMA_CH_USED) {
366 sc->sc_dma_ch[ch].flags |= BCM_DMA_CH_FREE;
367 sc->sc_dma_ch[ch].flags &= ~BCM_DMA_CH_USED;
368 sc->sc_dma_ch[ch].intr_func = NULL;
369 sc->sc_dma_ch[ch].intr_arg = NULL;
371 /* reset DMA engine */
372 bcm_dma_reset(sc->sc_dev, ch);
375 mtx_unlock(&sc->sc_mtx);
380 * Assign handler function for channel interrupt
381 * Returns 0 on success, -1 otherwise
384 bcm_dma_setup_intr(int ch, void (*func)(int, void *), void *arg)
386 struct bcm_dma_softc *sc = bcm_dma_sc;
387 struct bcm_dma_cb *cb;
389 if (ch < 0 || ch >= BCM_DMA_CH_MAX)
392 if (!(sc->sc_dma_ch[ch].flags & BCM_DMA_CH_USED))
395 sc->sc_dma_ch[ch].intr_func = func;
396 sc->sc_dma_ch[ch].intr_arg = arg;
397 cb = sc->sc_dma_ch[ch].cb;
398 cb->info |= INFO_INT_EN;
404 * Setup DMA source parameters
405 * ch - channel number
406 * dreq - hardware DREQ # or BCM_DMA_DREQ_NONE if
407 * source is physical memory
408 * inc_addr - BCM_DMA_INC_ADDR if source address
409 * should be increased after each access or
410 * BCM_DMA_SAME_ADDR if address should remain
412 * width - size of read operation, BCM_DMA_32BIT
413 * for 32bit bursts, BCM_DMA_128BIT for 128 bits
415 * Returns 0 on success, -1 otherwise
418 bcm_dma_setup_src(int ch, int dreq, int inc_addr, int width)
420 struct bcm_dma_softc *sc = bcm_dma_sc;
423 if (ch < 0 || ch >= BCM_DMA_CH_MAX)
426 if (!(sc->sc_dma_ch[ch].flags & BCM_DMA_CH_USED))
429 info = sc->sc_dma_ch[ch].cb->info;
430 info &= ~INFO_PERMAP_MASK;
431 info |= (dreq << INFO_PERMAP_SHIFT) & INFO_PERMAP_MASK;
436 info &= ~INFO_S_DREQ;
438 if (width == BCM_DMA_128BIT)
439 info |= INFO_S_WIDTH;
441 info &= ~INFO_S_WIDTH;
443 if (inc_addr == BCM_DMA_INC_ADDR)
448 sc->sc_dma_ch[ch].cb->info = info;
454 * Setup DMA destination parameters
455 * ch - channel number
456 * dreq - hardware DREQ # or BCM_DMA_DREQ_NONE if
457 * destination is physical memory
458 * inc_addr - BCM_DMA_INC_ADDR if source address
459 * should be increased after each access or
460 * BCM_DMA_SAME_ADDR if address should remain
462 * width - size of write operation, BCM_DMA_32BIT
463 * for 32bit bursts, BCM_DMA_128BIT for 128 bits
465 * Returns 0 on success, -1 otherwise
468 bcm_dma_setup_dst(int ch, int dreq, int inc_addr, int width)
470 struct bcm_dma_softc *sc = bcm_dma_sc;
473 if (ch < 0 || ch >= BCM_DMA_CH_MAX)
476 if (!(sc->sc_dma_ch[ch].flags & BCM_DMA_CH_USED))
479 info = sc->sc_dma_ch[ch].cb->info;
480 info &= ~INFO_PERMAP_MASK;
481 info |= (dreq << INFO_PERMAP_SHIFT) & INFO_PERMAP_MASK;
486 info &= ~INFO_D_DREQ;
488 if (width == BCM_DMA_128BIT)
489 info |= INFO_D_WIDTH;
491 info &= ~INFO_D_WIDTH;
493 if (inc_addr == BCM_DMA_INC_ADDR)
498 sc->sc_dma_ch[ch].cb->info = info;
505 bcm_dma_cb_dump(struct bcm_dma_cb *cb)
509 printf("INFO: %8.8x ", cb->info);
510 printf("SRC: %8.8x ", cb->src);
511 printf("DST: %8.8x ", cb->dst);
512 printf("LEN: %8.8x ", cb->len);
514 printf("STRIDE: %8.8x ", cb->stride);
515 printf("NEXT: %8.8x ", cb->next);
516 printf("RSVD1: %8.8x ", cb->rsvd1);
517 printf("RSVD2: %8.8x ", cb->rsvd2);
522 bcm_dma_reg_dump(int ch)
524 struct bcm_dma_softc *sc = bcm_dma_sc;
528 if (ch < 0 || ch >= BCM_DMA_CH_MAX)
531 printf("DMA%d: ", ch);
532 for (i = 0; i < MAX_REG; i++) {
533 reg = bus_read_4(sc->sc_mem, BCM_DMA_CH(ch) + i*4);
534 printf("%8.8x ", reg);
541 * Start DMA transaction
542 * ch - channel number
543 * src, dst - source and destination address in
544 * ARM physical memory address space.
545 * len - amount of bytes to be transferred
547 * Returns 0 on success, -1 otherwise
550 bcm_dma_start(int ch, vm_paddr_t src, vm_paddr_t dst, int len)
552 struct bcm_dma_softc *sc = bcm_dma_sc;
553 struct bcm_dma_cb *cb;
555 if (ch < 0 || ch >= BCM_DMA_CH_MAX)
558 if (!(sc->sc_dma_ch[ch].flags & BCM_DMA_CH_USED))
561 cb = sc->sc_dma_ch[ch].cb;
562 if (BCM2835_ARM_IS_IO(src))
563 cb->src = IO_TO_VCBUS(src);
565 cb->src = PHYS_TO_VCBUS(src);
566 if (BCM2835_ARM_IS_IO(dst))
567 cb->dst = IO_TO_VCBUS(dst);
569 cb->dst = PHYS_TO_VCBUS(dst);
572 bus_dmamap_sync(sc->sc_dma_tag,
573 sc->sc_dma_ch[ch].dma_map, BUS_DMASYNC_PREWRITE);
575 bus_write_4(sc->sc_mem, BCM_DMA_CBADDR(ch),
576 sc->sc_dma_ch[ch].vc_cb);
577 bus_write_4(sc->sc_mem, BCM_DMA_CS(ch), CS_ACTIVE);
580 bcm_dma_cb_dump(sc->sc_dma_ch[ch].cb);
581 bcm_dma_reg_dump(ch);
588 * Get length requested for DMA transaction
589 * ch - channel number
591 * Returns size of transaction, 0 if channel is invalid
594 bcm_dma_length(int ch)
596 struct bcm_dma_softc *sc = bcm_dma_sc;
597 struct bcm_dma_cb *cb;
599 if (ch < 0 || ch >= BCM_DMA_CH_MAX)
602 if (!(sc->sc_dma_ch[ch].flags & BCM_DMA_CH_USED))
605 cb = sc->sc_dma_ch[ch].cb;
611 bcm_dma_intr(void *arg)
613 struct bcm_dma_softc *sc = bcm_dma_sc;
614 struct bcm_dma_ch *ch = (struct bcm_dma_ch *)arg;
618 cs = bus_read_4(sc->sc_mem, BCM_DMA_CS(ch->ch));
620 if (!(cs & (CS_INT | CS_ERR))) {
621 device_printf(sc->sc_dev,
622 "unexpected DMA intr CH=%d, CS=%x\n", ch->ch, cs);
627 if (!(ch->flags & BCM_DMA_CH_USED)) {
628 device_printf(sc->sc_dev,
629 "unused DMA intr CH=%d, CS=%x\n", ch->ch, cs);
634 debug = bus_read_4(sc->sc_mem, BCM_DMA_DEBUG(ch->ch));
635 device_printf(sc->sc_dev, "DMA error %d on CH%d\n",
636 debug & DEBUG_ERROR_MASK, ch->ch);
637 bus_write_4(sc->sc_mem, BCM_DMA_DEBUG(ch->ch),
638 debug & DEBUG_ERROR_MASK);
639 bcm_dma_reset(sc->sc_dev, ch->ch);
643 /* acknowledge interrupt */
644 bus_write_4(sc->sc_mem, BCM_DMA_CS(ch->ch),
647 /* Prepare for possible access to len field */
648 bus_dmamap_sync(sc->sc_dma_tag, ch->dma_map,
649 BUS_DMASYNC_POSTWRITE);
651 /* save callback function and argument */
653 ch->intr_func(ch->ch, ch->intr_arg);
658 bcm_dma_probe(device_t dev)
661 if (!ofw_bus_status_okay(dev))
664 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
667 device_set_desc(dev, "BCM2835 DMA Controller");
668 return (BUS_PROBE_DEFAULT);
672 bcm_dma_attach(device_t dev)
674 struct bcm_dma_softc *sc = device_get_softc(dev);
684 for (i = 0; i < BCM_DMA_CH_MAX; i++) {
685 sc->sc_irq[i] = NULL;
686 sc->sc_intrhand[i] = NULL;
689 /* Get DMA channel mask. */
690 node = ofw_bus_get_node(sc->sc_dev);
691 if (OF_getencprop(node, "brcm,dma-channel-mask", &bcm_dma_channel_mask,
692 sizeof(bcm_dma_channel_mask)) == -1 &&
693 OF_getencprop(node, "broadcom,channels", &bcm_dma_channel_mask,
694 sizeof(bcm_dma_channel_mask)) == -1) {
695 device_printf(dev, "could not get channel mask property\n");
699 /* Mask out channels used by GPU. */
700 bcm_dma_channel_mask &= ~BCM_DMA_CH_GPU_MASK;
704 sc->sc_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
705 if (sc->sc_mem == NULL) {
706 device_printf(dev, "could not allocate memory resource\n");
710 /* IRQ DMA0 - DMA11 XXX NOT USE DMA12(spurious?) */
711 for (rid = 0; rid < BCM_DMA_CH_MAX; rid++) {
712 if ((bcm_dma_channel_mask & (1 << rid)) == 0)
715 sc->sc_irq[rid] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
717 if (sc->sc_irq[rid] == NULL) {
718 device_printf(dev, "cannot allocate interrupt\n");
722 if (bus_setup_intr(dev, sc->sc_irq[rid], INTR_TYPE_MISC | INTR_MPSAFE,
723 NULL, bcm_dma_intr, &sc->sc_dma_ch[rid],
724 &sc->sc_intrhand[rid])) {
725 device_printf(dev, "cannot setup interrupt handler\n");
731 mtx_init(&sc->sc_mtx, "bcmdma", "bcmdma", MTX_DEF);
734 err = bcm_dma_init(dev);
742 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem);
744 for (i = 0; i < BCM_DMA_CH_MAX; i++) {
745 if (sc->sc_intrhand[i])
746 bus_teardown_intr(dev, sc->sc_irq[i], sc->sc_intrhand[i]);
748 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq[i]);
754 static device_method_t bcm_dma_methods[] = {
755 DEVMETHOD(device_probe, bcm_dma_probe),
756 DEVMETHOD(device_attach, bcm_dma_attach),
760 static driver_t bcm_dma_driver = {
763 sizeof(struct bcm_dma_softc),
766 static devclass_t bcm_dma_devclass;
768 DRIVER_MODULE(bcm_dma, simplebus, bcm_dma_driver, bcm_dma_devclass, 0, 0);
769 MODULE_VERSION(bcm_dma, 1);