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>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/endian.h>
46 #include <machine/bus.h>
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
51 #include <arm/allwinner/a10_dmac.h>
52 #include <dev/extres/clk/clk.h>
53 #include <dev/extres/hwreset/hwreset.h>
55 #include "sunxi_dma_if.h"
57 #define DMA_IRQ_EN_REG0 0x00
58 #define DMA_IRQ_EN_REG1 0x04
59 #define DMA_IRQ_EN_REG(ch) (DMA_IRQ_EN_REG0 + ((ch) / 8) * 4)
60 #define DMA_PKG_IRQ_EN(ch) (1 << (((ch) % 8) * 4 + 1))
61 #define DMA_PKG_IRQ_MASK 0x2222222222222222ULL
62 #define DMA_IRQ_PEND_REG0 0x10
63 #define DMA_IRQ_PEND_REG1 0x14
64 #define DMA_IRQ_PEND_REG(ch) (DMA_IRQ_PEND_REG0 + ((ch) / 8) * 4)
65 #define DMA_STA_REG 0x30
66 #define DMA_EN_REG(n) (0x100 + (n) * 0x40 + 0x00)
67 #define DMA_EN (1 << 0)
68 #define DMA_PAU_REG(n) (0x100 + (n) * 0x40 + 0x04)
69 #define DMA_STAR_ADDR_REG(n) (0x100 + (n) * 0x40 + 0x08)
70 #define DMA_CFG_REG(n) (0x100 + (n) * 0x40 + 0x0c)
71 #define DMA_DEST_DATA_WIDTH (0x3 << 25)
72 #define DMA_DEST_DATA_WIDTH_SHIFT 25
73 #define DMA_DEST_BST_LEN (0x3 << 22)
74 #define DMA_DEST_BST_LEN_SHIFT 22
75 #define DMA_DEST_ADDR_MODE (0x1 << 21)
76 #define DMA_DEST_ADDR_MODE_SHIFT 21
77 #define DMA_DEST_DRQ_TYPE (0x1f << 16)
78 #define DMA_DEST_DRQ_TYPE_SHIFT 16
79 #define DMA_SRC_DATA_WIDTH (0x3 << 9)
80 #define DMA_SRC_DATA_WIDTH_SHIFT 9
81 #define DMA_SRC_BST_LEN (0x3 << 6)
82 #define DMA_SRC_BST_LEN_SHIFT 6
83 #define DMA_SRC_ADDR_MODE (0x1 << 5)
84 #define DMA_SRC_ADDR_MODE_SHIFT 5
85 #define DMA_SRC_DRQ_TYPE (0x1f << 0)
86 #define DMA_SRC_DRQ_TYPE_SHIFT 0
87 #define DMA_DATA_WIDTH_8BIT 0
88 #define DMA_DATA_WIDTH_16BIT 1
89 #define DMA_DATA_WIDTH_32BIT 2
90 #define DMA_DATA_WIDTH_64BIT 3
91 #define DMA_ADDR_MODE_LINEAR 0
92 #define DMA_ADDR_MODE_IO 1
93 #define DMA_BST_LEN_1 0
94 #define DMA_BST_LEN_4 1
95 #define DMA_BST_LEN_8 2
96 #define DMA_BST_LEN_16 3
97 #define DMA_CUR_SRC_REG(n) (0x100 + (n) * 0x40 + 0x10)
98 #define DMA_CUR_DEST_REG(n) (0x100 + (n) * 0x40 + 0x14)
99 #define DMA_BCNT_LEFT_REG(n) (0x100 + (n) * 0x40 + 0x18)
100 #define DMA_PARA_REG(n) (0x100 + (n) * 0x40 + 0x1c)
101 #define WAIT_CYC (0xff << 0)
102 #define WAIT_CYC_SHIFT 0
104 struct a31dmac_desc {
111 #define DMA_NULL 0xfffff800
114 #define DESC_SIZE sizeof(struct a31dmac_desc)
116 struct a31dmac_config {
120 static const struct a31dmac_config a31_config = { .nchans = 16 };
121 static const struct a31dmac_config h3_config = { .nchans = 12 };
122 static const struct a31dmac_config a83t_config = { .nchans = 8 };
123 static const struct a31dmac_config a64_config = { .nchans = 8 };
125 static struct ofw_compat_data compat_data[] = {
126 { "allwinner,sun6i-a31-dma", (uintptr_t)&a31_config },
127 { "allwinner,sun8i-a83t-dma", (uintptr_t)&a83t_config },
128 { "allwinner,sun8i-h3-dma", (uintptr_t)&h3_config },
129 { "allwinner,sun50i-a64-dma", (uintptr_t)&a64_config },
130 { NULL, (uintptr_t)NULL }
133 struct a31dmac_softc;
135 struct a31dmac_channel {
136 struct a31dmac_softc * sc;
138 void (*callback)(void *);
142 struct a31dmac_desc *desc;
146 struct a31dmac_softc {
147 struct resource * res[2];
154 struct a31dmac_channel * chans;
157 static struct resource_spec a31dmac_spec[] = {
158 { SYS_RES_MEMORY, 0, RF_ACTIVE },
159 { SYS_RES_IRQ, 0, RF_ACTIVE },
163 #define DMA_READ(sc, reg) bus_read_4((sc)->res[0], (reg))
164 #define DMA_WRITE(sc, reg, val) bus_write_4((sc)->res[0], (reg), (val))
166 static void a31dmac_intr(void *);
167 static void a31dmac_dmamap_cb(void *, bus_dma_segment_t *, int, int);
170 a31dmac_probe(device_t dev)
172 if (!ofw_bus_status_okay(dev))
175 if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
178 device_set_desc(dev, "Allwinner DMA controller");
179 return (BUS_PROBE_DEFAULT);
183 a31dmac_attach(device_t dev)
185 struct a31dmac_softc *sc;
186 struct a31dmac_config *conf;
192 sc = device_get_softc(dev);
193 conf = (void *)ofw_bus_search_compatible(dev, compat_data)->ocd_data;
197 if (bus_alloc_resources(dev, a31dmac_spec, sc->res)) {
198 device_printf(dev, "cannot allocate resources for device\n");
202 mtx_init(&sc->mtx, "a31 dmac", NULL, MTX_SPIN);
204 /* Clock and reset setup */
205 if (clk_get_by_ofw_index(dev, 0, 0, &clk) != 0) {
206 device_printf(dev, "cannot get clock\n");
209 if (clk_enable(clk) != 0) {
210 device_printf(dev, "cannot enable clock\n");
213 if (hwreset_get_by_ofw_idx(dev, 0, 0, &rst) != 0) {
214 device_printf(dev, "cannot get hwreset\n");
217 if (hwreset_deassert(rst) != 0) {
218 device_printf(dev, "cannot de-assert reset\n");
223 error = bus_dma_tag_create(
224 bus_get_dma_tag(dev), /* Parent tag */
225 DESC_ALIGN, 0, /* alignment, boundary */
226 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
227 BUS_SPACE_MAXADDR, /* highaddr */
228 NULL, NULL, /* filter, filterarg */
229 DESC_SIZE, 1, /* maxsize, nsegs */
230 DESC_SIZE, /* maxsegsize */
232 NULL, NULL, /* lockfunc, lockarg */
235 device_printf(dev, "cannot create dma tag\n");
239 /* Disable all interrupts and clear pending status */
240 DMA_WRITE(sc, DMA_IRQ_EN_REG0, 0);
241 DMA_WRITE(sc, DMA_IRQ_EN_REG1, 0);
242 DMA_WRITE(sc, DMA_IRQ_PEND_REG0, ~0);
243 DMA_WRITE(sc, DMA_IRQ_PEND_REG1, ~0);
245 /* Initialize channels */
246 sc->nchans = conf->nchans;
247 sc->chans = malloc(sizeof(*sc->chans) * sc->nchans, M_DEVBUF,
250 for (index = 0; index < sc->nchans; index++) {
251 sc->chans[index].sc = sc;
252 sc->chans[index].index = index;
253 sc->chans[index].callback = NULL;
254 sc->chans[index].callbackarg = NULL;
256 error = bus_dmamem_alloc(sc->dmat,
257 (void **)&sc->chans[index].desc,
258 BUS_DMA_WAITOK | BUS_DMA_COHERENT,
259 &sc->chans[index].dmamap);
261 device_printf(dev, "cannot allocate dma mem\n");
264 error = bus_dmamap_load(sc->dmat, sc->chans[index].dmamap,
265 sc->chans[index].desc, sizeof(*sc->chans[index].desc),
266 a31dmac_dmamap_cb, &sc->chans[index], BUS_DMA_WAITOK);
268 device_printf(dev, "cannot load dma map\n");
272 DMA_WRITE(sc, DMA_EN_REG(index), 0);
275 error = bus_setup_intr(dev, sc->res[1], INTR_MPSAFE | INTR_TYPE_MISC,
276 NULL, a31dmac_intr, sc, &sc->ih);
278 device_printf(dev, "could not setup interrupt handler\n");
279 bus_release_resources(dev, a31dmac_spec, sc->res);
280 mtx_destroy(&sc->mtx);
284 OF_device_register_xref(OF_xref_from_node(ofw_bus_get_node(dev)), dev);
288 for (index = 0; index < sc->nchans; index++)
289 if (sc->chans[index].desc != NULL) {
290 bus_dmamap_unload(sc->dmat, sc->chans[index].dmamap);
291 bus_dmamem_free(sc->dmat, sc->chans[index].desc,
292 sc->chans[index].dmamap);
294 if (sc->chans != NULL)
295 free(sc->chans, M_DEVBUF);
297 bus_teardown_intr(dev, sc->res[1], sc->ih);
299 hwreset_release(rst);
302 bus_release_resources(dev, a31dmac_spec, sc->res);
308 a31dmac_dmamap_cb(void *priv, bus_dma_segment_t *segs, int nsegs, int error)
310 struct a31dmac_channel *ch;
316 ch->physaddr = segs[0].ds_addr;
320 a31dmac_intr(void *priv)
322 struct a31dmac_softc *sc;
323 uint32_t pend0, pend1, bit;
328 pend0 = DMA_READ(sc, DMA_IRQ_PEND_REG0);
329 pend1 = sc->nchans > 8 ? DMA_READ(sc, DMA_IRQ_PEND_REG1) : 0;
330 if (pend0 == 0 && pend1 == 0)
334 DMA_WRITE(sc, DMA_IRQ_PEND_REG0, pend0);
336 DMA_WRITE(sc, DMA_IRQ_PEND_REG1, pend1);
338 pend = pend0 | ((uint64_t)pend1 << 32);
340 while ((bit = ffsll(pend & DMA_PKG_IRQ_MASK)) != 0) {
341 mask = (1U << (bit - 1));
343 index = (bit - 1) / 4;
345 if (index >= sc->nchans)
347 if (sc->chans[index].callback == NULL)
349 sc->chans[index].callback(sc->chans[index].callbackarg);
354 a31dmac_set_config(device_t dev, void *priv, const struct sunxi_dma_config *cfg)
356 struct a31dmac_channel *ch;
357 uint32_t config, para;
358 unsigned int dst_dw, dst_bl, dst_wc, dst_am;
359 unsigned int src_dw, src_bl, src_wc, src_am;
363 switch (cfg->dst_width) {
365 dst_dw = DMA_DATA_WIDTH_8BIT;
368 dst_dw = DMA_DATA_WIDTH_16BIT;
371 dst_dw = DMA_DATA_WIDTH_32BIT;
374 dst_dw = DMA_DATA_WIDTH_64BIT;
379 switch (cfg->dst_burst_len) {
381 dst_bl = DMA_BST_LEN_1;
384 dst_bl = DMA_BST_LEN_4;
387 dst_bl = DMA_BST_LEN_8;
390 dst_bl = DMA_BST_LEN_16;
395 switch (cfg->src_width) {
397 src_dw = DMA_DATA_WIDTH_8BIT;
400 src_dw = DMA_DATA_WIDTH_16BIT;
403 src_dw = DMA_DATA_WIDTH_32BIT;
406 src_dw = DMA_DATA_WIDTH_64BIT;
410 switch (cfg->src_burst_len) {
412 src_bl = DMA_BST_LEN_1;
415 src_bl = DMA_BST_LEN_4;
418 src_bl = DMA_BST_LEN_8;
421 src_bl = DMA_BST_LEN_16;
426 dst_am = cfg->dst_noincr ? DMA_ADDR_MODE_IO : DMA_ADDR_MODE_LINEAR;
427 src_am = cfg->src_noincr ? DMA_ADDR_MODE_IO : DMA_ADDR_MODE_LINEAR;
428 dst_wc = cfg->dst_wait_cyc;
429 src_wc = cfg->src_wait_cyc;
430 if (dst_wc != src_wc)
433 config = (dst_dw << DMA_DEST_DATA_WIDTH_SHIFT) |
434 (dst_bl << DMA_DEST_BST_LEN_SHIFT) |
435 (dst_am << DMA_DEST_ADDR_MODE_SHIFT) |
436 (cfg->dst_drqtype << DMA_DEST_DRQ_TYPE_SHIFT) |
437 (src_dw << DMA_SRC_DATA_WIDTH_SHIFT) |
438 (src_bl << DMA_SRC_BST_LEN_SHIFT) |
439 (src_am << DMA_SRC_ADDR_MODE_SHIFT) |
440 (cfg->src_drqtype << DMA_SRC_DRQ_TYPE_SHIFT);
441 para = (dst_wc << WAIT_CYC_SHIFT);
443 ch->desc->config = htole32(config);
444 ch->desc->para = htole32(para);
450 a31dmac_alloc(device_t dev, bool dedicated, void (*cb)(void *), void *cbarg)
452 struct a31dmac_softc *sc;
453 struct a31dmac_channel *ch;
457 sc = device_get_softc(dev);
460 mtx_lock_spin(&sc->mtx);
461 for (index = 0; index < sc->nchans; index++) {
462 if (sc->chans[index].callback == NULL) {
463 ch = &sc->chans[index];
465 ch->callbackarg = cbarg;
467 irqen = DMA_READ(sc, DMA_IRQ_EN_REG(index));
468 irqen |= DMA_PKG_IRQ_EN(index);
469 DMA_WRITE(sc, DMA_IRQ_EN_REG(index), irqen);
473 mtx_unlock_spin(&sc->mtx);
479 a31dmac_free(device_t dev, void *priv)
481 struct a31dmac_channel *ch;
482 struct a31dmac_softc *sc;
490 mtx_lock_spin(&sc->mtx);
492 irqen = DMA_READ(sc, DMA_IRQ_EN_REG(index));
493 irqen &= ~DMA_PKG_IRQ_EN(index);
494 DMA_WRITE(sc, DMA_IRQ_EN_REG(index), irqen);
495 DMA_WRITE(sc, DMA_IRQ_PEND_REG(index), DMA_PKG_IRQ_EN(index));
498 ch->callbackarg = NULL;
500 mtx_unlock_spin(&sc->mtx);
504 a31dmac_transfer(device_t dev, void *priv, bus_addr_t src, bus_addr_t dst,
507 struct a31dmac_channel *ch;
508 struct a31dmac_softc *sc;
513 ch->desc->srcaddr = htole32((uint32_t)src);
514 ch->desc->dstaddr = htole32((uint32_t)dst);
515 ch->desc->bcnt = htole32(nbytes);
516 ch->desc->next = htole32(DMA_NULL);
518 DMA_WRITE(sc, DMA_STAR_ADDR_REG(ch->index), (uint32_t)ch->physaddr);
519 DMA_WRITE(sc, DMA_EN_REG(ch->index), DMA_EN);
525 a31dmac_halt(device_t dev, void *priv)
527 struct a31dmac_channel *ch;
528 struct a31dmac_softc *sc;
533 DMA_WRITE(sc, DMA_EN_REG(ch->index), 0);
536 static device_method_t a31dmac_methods[] = {
537 /* Device interface */
538 DEVMETHOD(device_probe, a31dmac_probe),
539 DEVMETHOD(device_attach, a31dmac_attach),
541 /* sunxi DMA interface */
542 DEVMETHOD(sunxi_dma_alloc, a31dmac_alloc),
543 DEVMETHOD(sunxi_dma_free, a31dmac_free),
544 DEVMETHOD(sunxi_dma_set_config, a31dmac_set_config),
545 DEVMETHOD(sunxi_dma_transfer, a31dmac_transfer),
546 DEVMETHOD(sunxi_dma_halt, a31dmac_halt),
551 static driver_t a31dmac_driver = {
554 sizeof(struct a31dmac_softc)
557 static devclass_t a31dmac_devclass;
559 DRIVER_MODULE(a31dmac, simplebus, a31dmac_driver, a31dmac_devclass, 0, 0);