2 * Copyright (c) 2016-2018 Ruslan Bukin <br@bsdpad.com>
5 * This software was developed by SRI International and the University of
6 * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
7 * ("CTSRD"), as part of the DARPA CRASH research programme.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 /* Ingenic JZ4780 PDMA Controller. */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "opt_platform.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
44 #include <sys/mutex.h>
45 #include <sys/resource.h>
48 #include <machine/bus.h>
49 #include <machine/cache.h>
52 #include <dev/fdt/fdt_common.h>
53 #include <dev/ofw/ofw_bus.h>
54 #include <dev/ofw/ofw_bus_subr.h>
57 #include <dev/xdma/xdma.h>
59 #include <mips/ingenic/jz4780_common.h>
60 #include <mips/ingenic/jz4780_pdma.h>
68 #define dprintf(fmt, ...) printf(fmt, ##__VA_ARGS__)
70 #define dprintf(fmt, ...)
73 #define PDMA_DESC_RING_ALIGN 2048
77 struct resource *res[2];
79 bus_space_handle_t bsh;
83 struct pdma_fdt_data {
90 struct pdma_fdt_data data;
95 #define CHAN_DESCR_RELINK (1 << 0)
98 bus_dma_tag_t desc_tag;
99 bus_dmamap_t desc_map;
100 struct pdma_hwdesc *desc_ring;
101 bus_addr_t desc_ring_paddr;
104 xdma_channel_t *xchan;
105 struct xdma_request *req;
108 #define PDMA_NCHANNELS 32
109 struct pdma_channel pdma_channels[PDMA_NCHANNELS];
111 static struct resource_spec pdma_spec[] = {
112 { SYS_RES_MEMORY, 0, RF_ACTIVE },
113 { SYS_RES_IRQ, 0, RF_ACTIVE },
117 static int pdma_probe(device_t dev);
118 static int pdma_attach(device_t dev);
119 static int pdma_detach(device_t dev);
120 static int chan_start(struct pdma_softc *sc, struct pdma_channel *chan);
125 struct xdma_request *req;
126 xdma_transfer_status_t status;
127 struct pdma_channel *chan;
128 struct pdma_softc *sc;
129 xdma_channel_t *xchan;
135 pending = READ4(sc, PDMA_DIRQP);
137 /* Ack all the channels. */
138 WRITE4(sc, PDMA_DIRQP, 0);
140 for (i = 0; i < PDMA_NCHANNELS; i++) {
141 if (pending & (1 << i)) {
142 chan = &pdma_channels[i];
146 /* TODO: check for AR, HLT error bits here. */
148 /* Disable channel */
149 WRITE4(sc, PDMA_DCS(chan->index), 0);
151 if (chan->flags & CHAN_DESCR_RELINK) {
153 chan->cur_desc = (chan->cur_desc + 1) % \
155 chan_start(sc, chan);
159 xdma_callback(chan->xchan, &status);
165 pdma_probe(device_t dev)
168 if (!ofw_bus_status_okay(dev))
171 if (!ofw_bus_is_compatible(dev, "ingenic,jz4780-dma"))
174 device_set_desc(dev, "Ingenic JZ4780 PDMA Controller");
176 return (BUS_PROBE_DEFAULT);
180 pdma_attach(device_t dev)
182 struct pdma_softc *sc;
183 phandle_t xref, node;
187 sc = device_get_softc(dev);
190 if (bus_alloc_resources(dev, pdma_spec, sc->res)) {
191 device_printf(dev, "could not allocate resources for device\n");
195 /* Memory interface */
196 sc->bst = rman_get_bustag(sc->res[0]);
197 sc->bsh = rman_get_bushandle(sc->res[0]);
199 /* Setup interrupt handler */
200 err = bus_setup_intr(dev, sc->res[1], INTR_TYPE_MISC | INTR_MPSAFE,
201 NULL, pdma_intr, sc, &sc->ih);
203 device_printf(dev, "Unable to alloc interrupt resource.\n");
207 node = ofw_bus_get_node(dev);
208 xref = OF_xref_from_node(node);
209 OF_device_register_xref(xref, dev);
211 reg = READ4(sc, PDMA_DMAC);
212 reg &= ~(DMAC_HLT | DMAC_AR);
214 WRITE4(sc, PDMA_DMAC, reg);
216 WRITE4(sc, PDMA_DMACP, 0);
222 pdma_detach(device_t dev)
224 struct pdma_softc *sc;
226 sc = device_get_softc(dev);
228 bus_release_resources(dev, pdma_spec, sc->res);
234 chan_start(struct pdma_softc *sc, struct pdma_channel *chan)
236 struct xdma_channel *xchan;
240 /* 8 byte descriptor. */
241 WRITE4(sc, PDMA_DCS(chan->index), DCS_DES8);
242 WRITE4(sc, PDMA_DDA(chan->index),
243 chan->desc_ring_paddr + 8 * 4 * chan->cur_desc);
245 WRITE4(sc, PDMA_DDS, (1 << chan->index));
247 /* Channel transfer enable. */
248 WRITE4(sc, PDMA_DCS(chan->index), (DCS_DES8 | DCS_CTE));
254 chan_stop(struct pdma_softc *sc, struct pdma_channel *chan)
258 WRITE4(sc, PDMA_DCS(chan->index), 0);
263 if ((READ4(sc, PDMA_DCS(chan->index)) & DCS_CTE) == 0) {
269 device_printf(sc->dev, "%s: Can't stop channel %d\n",
270 __func__, chan->index);
277 dwc_get1paddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
282 *(bus_addr_t *)arg = segs[0].ds_addr;
286 pdma_channel_setup_descriptors(device_t dev, struct pdma_channel *chan)
288 struct pdma_softc *sc;
291 sc = device_get_softc(dev);
294 * Set up TX descriptor ring, descriptors, and dma maps.
296 error = bus_dma_tag_create(
297 bus_get_dma_tag(sc->dev), /* Parent tag. */
298 PDMA_DESC_RING_ALIGN, 0, /* alignment, boundary */
299 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
300 BUS_SPACE_MAXADDR, /* highaddr */
301 NULL, NULL, /* filter, filterarg */
302 CHAN_DESC_SIZE, 1, /* maxsize, nsegments */
303 CHAN_DESC_SIZE, /* maxsegsize */
305 NULL, NULL, /* lockfunc, lockarg */
308 device_printf(sc->dev,
309 "could not create TX ring DMA tag.\n");
313 error = bus_dmamem_alloc(chan->desc_tag, (void**)&chan->desc_ring,
314 BUS_DMA_COHERENT | BUS_DMA_WAITOK | BUS_DMA_ZERO,
317 device_printf(sc->dev,
318 "could not allocate TX descriptor ring.\n");
322 error = bus_dmamap_load(chan->desc_tag, chan->desc_map,
323 chan->desc_ring, CHAN_DESC_SIZE, dwc_get1paddr,
324 &chan->desc_ring_paddr, 0);
326 device_printf(sc->dev,
327 "could not load TX descriptor ring map.\n");
335 pdma_channel_alloc(device_t dev, struct xdma_channel *xchan)
337 struct pdma_channel *chan;
338 struct pdma_softc *sc;
341 sc = device_get_softc(dev);
343 for (i = 0; i < PDMA_NCHANNELS; i++) {
344 chan = &pdma_channels[i];
345 if (chan->used == 0) {
347 xchan->chan = (void *)chan;
351 pdma_channel_setup_descriptors(dev, chan);
361 pdma_channel_free(device_t dev, struct xdma_channel *xchan)
363 struct pdma_channel *chan;
364 struct pdma_softc *sc;
366 sc = device_get_softc(dev);
368 chan = (struct pdma_channel *)xchan->chan;
375 access_width(struct xdma_request *req, uint32_t *dcm, uint32_t *max_width)
379 *max_width = max(req->src_width, req->dst_width);
381 switch (req->src_width) {
395 switch (req->dst_width) {
409 switch (*max_width) {
427 pdma_channel_request(device_t dev, struct xdma_channel *xchan, struct xdma_request *req)
429 struct pdma_fdt_data *data;
430 struct pdma_channel *chan;
431 struct pdma_hwdesc *desc;
432 xdma_controller_t *xdma;
433 struct pdma_softc *sc;
439 sc = device_get_softc(dev);
441 dprintf("%s: block_len %d block_num %d\n",
442 __func__, req->block_len, req->block_num);
445 data = (struct pdma_fdt_data *)xdma->data;
447 chan = (struct pdma_channel *)xchan->chan;
448 /* Ensure we are not in operation */
450 if (req->operation == XDMA_CYCLIC)
451 chan->flags = CHAN_DESCR_RELINK;
455 for (i = 0; i < req->block_num; i++) {
456 desc = &chan->desc_ring[i];
458 if (req->direction == XDMA_MEM_TO_DEV) {
459 desc->dsa = req->src_addr + (i * req->block_len);
460 desc->dta = req->dst_addr;
461 desc->drt = data->tx;
463 } else if (req->direction == XDMA_DEV_TO_MEM) {
464 desc->dsa = req->src_addr;
465 desc->dta = req->dst_addr + (i * req->block_len);
466 desc->drt = data->rx;
468 } else if (req->direction == XDMA_MEM_TO_MEM) {
469 desc->dsa = req->src_addr + (i * req->block_len);
470 desc->dta = req->dst_addr + (i * req->block_len);
471 desc->drt = DRT_AUTO;
472 desc->dcm = DCM_SAI | DCM_DAI;
475 if (access_width(req, &dcm, &max_width) != 0) {
477 "%s: can't configure access width\n", __func__);
481 desc->dcm |= dcm | DCM_TIE;
482 desc->dtc = (req->block_len / max_width);
485 * TODO: bus dma pre read/write sync here
489 * PDMA does not provide interrupt after processing each descriptor,
490 * but after processing all the chain only.
491 * As a workaround we do unlink descriptors here, so our chain will
492 * consists of single descriptor only. And then we reconfigure channel
493 * on each interrupt again.
495 if ((chan->flags & CHAN_DESCR_RELINK) == 0) {
496 if (i != (req->block_num - 1)) {
497 desc->dcm |= DCM_LINK;
498 reg = ((i + 1) * sizeof(struct pdma_hwdesc));
499 desc->dtc |= (reg >> 4) << 24;
508 pdma_channel_control(device_t dev, xdma_channel_t *xchan, int cmd)
510 struct pdma_channel *chan;
511 struct pdma_softc *sc;
513 sc = device_get_softc(dev);
515 chan = (struct pdma_channel *)xchan->chan;
519 chan_start(sc, chan);
521 case XDMA_CMD_TERMINATE:
525 /* TODO: implement me */
534 pdma_ofw_md_data(device_t dev, pcell_t *cells, int ncells, void **ptr)
536 struct pdma_fdt_data *data;
542 data = malloc(sizeof(struct pdma_fdt_data), M_DEVBUF, (M_WAITOK | M_ZERO));
544 device_printf(dev, "%s: Cant allocate memory\n", __func__);
550 data->chan = cells[2];
558 static device_method_t pdma_methods[] = {
559 /* Device interface */
560 DEVMETHOD(device_probe, pdma_probe),
561 DEVMETHOD(device_attach, pdma_attach),
562 DEVMETHOD(device_detach, pdma_detach),
565 DEVMETHOD(xdma_channel_alloc, pdma_channel_alloc),
566 DEVMETHOD(xdma_channel_free, pdma_channel_free),
567 DEVMETHOD(xdma_channel_request, pdma_channel_request),
568 DEVMETHOD(xdma_channel_control, pdma_channel_control),
570 DEVMETHOD(xdma_ofw_md_data, pdma_ofw_md_data),
576 static driver_t pdma_driver = {
579 sizeof(struct pdma_softc),
582 static devclass_t pdma_devclass;
584 EARLY_DRIVER_MODULE(pdma, simplebus, pdma_driver, pdma_devclass, 0, 0,
585 BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE);