2 * Copyright (c) 2016 Jared D. McNeill <jmcneill@invisible.ca>
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 ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * 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
29 * Allwinner DMA controller
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
39 #include <sys/condvar.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/endian.h>
44 #include <machine/bus.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
49 #include <arm/allwinner/a10_dmac.h>
50 #include <dev/extres/clk/clk.h>
51 #include <dev/extres/hwreset/hwreset.h>
53 #include "sunxi_dma_if.h"
55 #define DMA_IRQ_EN_REG0 0x00
56 #define DMA_IRQ_EN_REG1 0x04
57 #define DMA_IRQ_EN_REG(ch) (DMA_IRQ_EN_REG0 + ((ch) / 8) * 4)
58 #define DMA_PKG_IRQ_EN(ch) (1 << (((ch) % 8) * 4 + 1))
59 #define DMA_PKG_IRQ_MASK 0x2222222222222222ULL
60 #define DMA_IRQ_PEND_REG0 0x10
61 #define DMA_IRQ_PEND_REG1 0x14
62 #define DMA_IRQ_PEND_REG(ch) (DMA_IRQ_PEND_REG0 + ((ch) / 8) * 4)
63 #define DMA_STA_REG 0x30
64 #define DMA_EN_REG(n) (0x100 + (n) * 0x40 + 0x00)
65 #define DMA_EN (1 << 0)
66 #define DMA_PAU_REG(n) (0x100 + (n) * 0x40 + 0x04)
67 #define DMA_STAR_ADDR_REG(n) (0x100 + (n) * 0x40 + 0x08)
68 #define DMA_CFG_REG(n) (0x100 + (n) * 0x40 + 0x0c)
69 #define DMA_DEST_DATA_WIDTH (0x3 << 25)
70 #define DMA_DEST_DATA_WIDTH_SHIFT 25
71 #define DMA_DEST_BST_LEN (0x3 << 22)
72 #define DMA_DEST_BST_LEN_SHIFT 22
73 #define DMA_DEST_ADDR_MODE (0x1 << 21)
74 #define DMA_DEST_ADDR_MODE_SHIFT 21
75 #define DMA_DEST_DRQ_TYPE (0x1f << 16)
76 #define DMA_DEST_DRQ_TYPE_SHIFT 16
77 #define DMA_SRC_DATA_WIDTH (0x3 << 9)
78 #define DMA_SRC_DATA_WIDTH_SHIFT 9
79 #define DMA_SRC_BST_LEN (0x3 << 6)
80 #define DMA_SRC_BST_LEN_SHIFT 6
81 #define DMA_SRC_ADDR_MODE (0x1 << 5)
82 #define DMA_SRC_ADDR_MODE_SHIFT 5
83 #define DMA_SRC_DRQ_TYPE (0x1f << 0)
84 #define DMA_SRC_DRQ_TYPE_SHIFT 0
85 #define DMA_DATA_WIDTH_8BIT 0
86 #define DMA_DATA_WIDTH_16BIT 1
87 #define DMA_DATA_WIDTH_32BIT 2
88 #define DMA_DATA_WIDTH_64BIT 3
89 #define DMA_ADDR_MODE_LINEAR 0
90 #define DMA_ADDR_MODE_IO 1
91 #define DMA_BST_LEN_1 0
92 #define DMA_BST_LEN_4 1
93 #define DMA_BST_LEN_8 2
94 #define DMA_BST_LEN_16 3
95 #define DMA_CUR_SRC_REG(n) (0x100 + (n) * 0x40 + 0x10)
96 #define DMA_CUR_DEST_REG(n) (0x100 + (n) * 0x40 + 0x14)
97 #define DMA_BCNT_LEFT_REG(n) (0x100 + (n) * 0x40 + 0x18)
98 #define DMA_PARA_REG(n) (0x100 + (n) * 0x40 + 0x1c)
99 #define WAIT_CYC (0xff << 0)
100 #define WAIT_CYC_SHIFT 0
102 struct a31dmac_desc {
109 #define DMA_NULL 0xfffff800
112 #define DESC_SIZE sizeof(struct a31dmac_desc)
114 struct a31dmac_config {
118 static const struct a31dmac_config a31_config = { .nchans = 16 };
119 static const struct a31dmac_config h3_config = { .nchans = 12 };
120 static const struct a31dmac_config a83t_config = { .nchans = 8 };
121 static const struct a31dmac_config a64_config = { .nchans = 8 };
123 static struct ofw_compat_data compat_data[] = {
124 { "allwinner,sun6i-a31-dma", (uintptr_t)&a31_config },
125 { "allwinner,sun8i-a83t-dma", (uintptr_t)&a83t_config },
126 { "allwinner,sun8i-h3-dma", (uintptr_t)&h3_config },
127 { "allwinner,sun50i-a64-dma", (uintptr_t)&a64_config },
128 { NULL, (uintptr_t)NULL }
131 struct a31dmac_softc;
133 struct a31dmac_channel {
134 struct a31dmac_softc * sc;
136 void (*callback)(void *);
140 struct a31dmac_desc *desc;
144 struct a31dmac_softc {
145 struct resource * res[2];
152 struct a31dmac_channel * chans;
155 static struct resource_spec a31dmac_spec[] = {
156 { SYS_RES_MEMORY, 0, RF_ACTIVE },
157 { SYS_RES_IRQ, 0, RF_ACTIVE },
161 #define DMA_READ(sc, reg) bus_read_4((sc)->res[0], (reg))
162 #define DMA_WRITE(sc, reg, val) bus_write_4((sc)->res[0], (reg), (val))
164 static void a31dmac_intr(void *);
165 static void a31dmac_dmamap_cb(void *, bus_dma_segment_t *, int, int);
168 a31dmac_probe(device_t dev)
170 if (!ofw_bus_status_okay(dev))
173 if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
176 device_set_desc(dev, "Allwinner DMA controller");
177 return (BUS_PROBE_DEFAULT);
181 a31dmac_attach(device_t dev)
183 struct a31dmac_softc *sc;
184 struct a31dmac_config *conf;
190 sc = device_get_softc(dev);
191 conf = (void *)ofw_bus_search_compatible(dev, compat_data)->ocd_data;
195 if (bus_alloc_resources(dev, a31dmac_spec, sc->res)) {
196 device_printf(dev, "cannot allocate resources for device\n");
200 mtx_init(&sc->mtx, "a31 dmac", NULL, MTX_SPIN);
202 /* Clock and reset setup */
203 if (clk_get_by_ofw_index(dev, 0, 0, &clk) != 0) {
204 device_printf(dev, "cannot get clock\n");
207 if (clk_enable(clk) != 0) {
208 device_printf(dev, "cannot enable clock\n");
211 if (hwreset_get_by_ofw_idx(dev, 0, 0, &rst) != 0) {
212 device_printf(dev, "cannot get hwreset\n");
215 if (hwreset_deassert(rst) != 0) {
216 device_printf(dev, "cannot de-assert reset\n");
221 error = bus_dma_tag_create(
222 bus_get_dma_tag(dev), /* Parent tag */
223 DESC_ALIGN, 0, /* alignment, boundary */
224 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
225 BUS_SPACE_MAXADDR, /* highaddr */
226 NULL, NULL, /* filter, filterarg */
227 DESC_SIZE, 1, /* maxsize, nsegs */
228 DESC_SIZE, /* maxsegsize */
230 NULL, NULL, /* lockfunc, lockarg */
233 device_printf(dev, "cannot create dma tag\n");
237 /* Disable all interrupts and clear pending status */
238 DMA_WRITE(sc, DMA_IRQ_EN_REG0, 0);
239 DMA_WRITE(sc, DMA_IRQ_EN_REG1, 0);
240 DMA_WRITE(sc, DMA_IRQ_PEND_REG0, ~0);
241 DMA_WRITE(sc, DMA_IRQ_PEND_REG1, ~0);
243 /* Initialize channels */
244 sc->nchans = conf->nchans;
245 sc->chans = malloc(sizeof(*sc->chans) * sc->nchans, M_DEVBUF,
248 for (index = 0; index < sc->nchans; index++) {
249 sc->chans[index].sc = sc;
250 sc->chans[index].index = index;
251 sc->chans[index].callback = NULL;
252 sc->chans[index].callbackarg = NULL;
254 error = bus_dmamem_alloc(sc->dmat,
255 (void **)&sc->chans[index].desc,
256 BUS_DMA_WAITOK | BUS_DMA_COHERENT,
257 &sc->chans[index].dmamap);
259 device_printf(dev, "cannot allocate dma mem\n");
262 error = bus_dmamap_load(sc->dmat, sc->chans[index].dmamap,
263 sc->chans[index].desc, sizeof(*sc->chans[index].desc),
264 a31dmac_dmamap_cb, &sc->chans[index], BUS_DMA_WAITOK);
266 device_printf(dev, "cannot load dma map\n");
270 DMA_WRITE(sc, DMA_EN_REG(index), 0);
273 error = bus_setup_intr(dev, sc->res[1], INTR_MPSAFE | INTR_TYPE_MISC,
274 NULL, a31dmac_intr, sc, &sc->ih);
276 device_printf(dev, "could not setup interrupt handler\n");
277 bus_release_resources(dev, a31dmac_spec, sc->res);
278 mtx_destroy(&sc->mtx);
282 OF_device_register_xref(OF_xref_from_node(ofw_bus_get_node(dev)), dev);
286 for (index = 0; index < sc->nchans; index++)
287 if (sc->chans[index].desc != NULL) {
288 bus_dmamap_unload(sc->dmat, sc->chans[index].dmamap);
289 bus_dmamem_free(sc->dmat, sc->chans[index].desc,
290 sc->chans[index].dmamap);
292 if (sc->chans != NULL)
293 free(sc->chans, M_DEVBUF);
295 bus_teardown_intr(dev, sc->res[1], sc->ih);
297 hwreset_release(rst);
300 bus_release_resources(dev, a31dmac_spec, sc->res);
306 a31dmac_dmamap_cb(void *priv, bus_dma_segment_t *segs, int nsegs, int error)
308 struct a31dmac_channel *ch;
314 ch->physaddr = segs[0].ds_addr;
318 a31dmac_intr(void *priv)
320 struct a31dmac_softc *sc;
321 uint32_t pend0, pend1, bit;
326 pend0 = DMA_READ(sc, DMA_IRQ_PEND_REG0);
327 pend1 = sc->nchans > 8 ? DMA_READ(sc, DMA_IRQ_PEND_REG1) : 0;
328 if (pend0 == 0 && pend1 == 0)
332 DMA_WRITE(sc, DMA_IRQ_PEND_REG0, pend0);
334 DMA_WRITE(sc, DMA_IRQ_PEND_REG1, pend1);
336 pend = pend0 | ((uint64_t)pend1 << 32);
338 while ((bit = ffsll(pend & DMA_PKG_IRQ_MASK)) != 0) {
339 mask = (1U << (bit - 1));
341 index = (bit - 1) / 4;
343 if (index >= sc->nchans)
345 if (sc->chans[index].callback == NULL)
347 sc->chans[index].callback(sc->chans[index].callbackarg);
352 a31dmac_set_config(device_t dev, void *priv, const struct sunxi_dma_config *cfg)
354 struct a31dmac_channel *ch;
355 uint32_t config, para;
356 unsigned int dst_dw, dst_bl, dst_wc, dst_am;
357 unsigned int src_dw, src_bl, src_wc, src_am;
361 switch (cfg->dst_width) {
363 dst_dw = DMA_DATA_WIDTH_8BIT;
366 dst_dw = DMA_DATA_WIDTH_16BIT;
369 dst_dw = DMA_DATA_WIDTH_32BIT;
372 dst_dw = DMA_DATA_WIDTH_64BIT;
377 switch (cfg->dst_burst_len) {
379 dst_bl = DMA_BST_LEN_1;
382 dst_bl = DMA_BST_LEN_4;
385 dst_bl = DMA_BST_LEN_8;
388 dst_bl = DMA_BST_LEN_16;
393 switch (cfg->src_width) {
395 src_dw = DMA_DATA_WIDTH_8BIT;
398 src_dw = DMA_DATA_WIDTH_16BIT;
401 src_dw = DMA_DATA_WIDTH_32BIT;
404 src_dw = DMA_DATA_WIDTH_64BIT;
408 switch (cfg->src_burst_len) {
410 src_bl = DMA_BST_LEN_1;
413 src_bl = DMA_BST_LEN_4;
416 src_bl = DMA_BST_LEN_8;
419 src_bl = DMA_BST_LEN_16;
424 dst_am = cfg->dst_noincr ? DMA_ADDR_MODE_IO : DMA_ADDR_MODE_LINEAR;
425 src_am = cfg->src_noincr ? DMA_ADDR_MODE_IO : DMA_ADDR_MODE_LINEAR;
426 dst_wc = cfg->dst_wait_cyc;
427 src_wc = cfg->src_wait_cyc;
428 if (dst_wc != src_wc)
431 config = (dst_dw << DMA_DEST_DATA_WIDTH_SHIFT) |
432 (dst_bl << DMA_DEST_BST_LEN_SHIFT) |
433 (dst_am << DMA_DEST_ADDR_MODE_SHIFT) |
434 (cfg->dst_drqtype << DMA_DEST_DRQ_TYPE_SHIFT) |
435 (src_dw << DMA_SRC_DATA_WIDTH_SHIFT) |
436 (src_bl << DMA_SRC_BST_LEN_SHIFT) |
437 (src_am << DMA_SRC_ADDR_MODE_SHIFT) |
438 (cfg->src_drqtype << DMA_SRC_DRQ_TYPE_SHIFT);
439 para = (dst_wc << WAIT_CYC_SHIFT);
441 ch->desc->config = htole32(config);
442 ch->desc->para = htole32(para);
448 a31dmac_alloc(device_t dev, bool dedicated, void (*cb)(void *), void *cbarg)
450 struct a31dmac_softc *sc;
451 struct a31dmac_channel *ch;
455 sc = device_get_softc(dev);
458 mtx_lock_spin(&sc->mtx);
459 for (index = 0; index < sc->nchans; index++) {
460 if (sc->chans[index].callback == NULL) {
461 ch = &sc->chans[index];
463 ch->callbackarg = cbarg;
465 irqen = DMA_READ(sc, DMA_IRQ_EN_REG(index));
466 irqen |= DMA_PKG_IRQ_EN(index);
467 DMA_WRITE(sc, DMA_IRQ_EN_REG(index), irqen);
471 mtx_unlock_spin(&sc->mtx);
477 a31dmac_free(device_t dev, void *priv)
479 struct a31dmac_channel *ch;
480 struct a31dmac_softc *sc;
488 mtx_lock_spin(&sc->mtx);
490 irqen = DMA_READ(sc, DMA_IRQ_EN_REG(index));
491 irqen &= ~DMA_PKG_IRQ_EN(index);
492 DMA_WRITE(sc, DMA_IRQ_EN_REG(index), irqen);
493 DMA_WRITE(sc, DMA_IRQ_PEND_REG(index), DMA_PKG_IRQ_EN(index));
496 ch->callbackarg = NULL;
498 mtx_unlock_spin(&sc->mtx);
502 a31dmac_transfer(device_t dev, void *priv, bus_addr_t src, bus_addr_t dst,
505 struct a31dmac_channel *ch;
506 struct a31dmac_softc *sc;
511 ch->desc->srcaddr = htole32((uint32_t)src);
512 ch->desc->dstaddr = htole32((uint32_t)dst);
513 ch->desc->bcnt = htole32(nbytes);
514 ch->desc->next = htole32(DMA_NULL);
516 DMA_WRITE(sc, DMA_STAR_ADDR_REG(ch->index), (uint32_t)ch->physaddr);
517 DMA_WRITE(sc, DMA_EN_REG(ch->index), DMA_EN);
523 a31dmac_halt(device_t dev, void *priv)
525 struct a31dmac_channel *ch;
526 struct a31dmac_softc *sc;
531 DMA_WRITE(sc, DMA_EN_REG(ch->index), 0);
534 static device_method_t a31dmac_methods[] = {
535 /* Device interface */
536 DEVMETHOD(device_probe, a31dmac_probe),
537 DEVMETHOD(device_attach, a31dmac_attach),
539 /* sunxi DMA interface */
540 DEVMETHOD(sunxi_dma_alloc, a31dmac_alloc),
541 DEVMETHOD(sunxi_dma_free, a31dmac_free),
542 DEVMETHOD(sunxi_dma_set_config, a31dmac_set_config),
543 DEVMETHOD(sunxi_dma_transfer, a31dmac_transfer),
544 DEVMETHOD(sunxi_dma_halt, a31dmac_halt),
549 static driver_t a31dmac_driver = {
552 sizeof(struct a31dmac_softc)
555 static devclass_t a31dmac_devclass;
557 DRIVER_MODULE(a31dmac, simplebus, a31dmac_driver, a31dmac_devclass, 0, 0);