Path: blob/main/sys/arm/freescale/vybrid/vf_edma.c
39536 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2014 Ruslan Bukin <[email protected]>4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728/*29* Vybrid Family Enhanced Direct Memory Access Controller (eDMA)30* Chapter 21, Vybrid Reference Manual, Rev. 5, 07/201331*/3233#include <sys/param.h>34#include <sys/systm.h>35#include <sys/bus.h>36#include <sys/kernel.h>37#include <sys/module.h>38#include <sys/malloc.h>39#include <sys/rman.h>40#include <sys/timeet.h>41#include <sys/timetc.h>42#include <sys/watchdog.h>4344#include <dev/ofw/openfirm.h>45#include <dev/ofw/ofw_bus.h>46#include <dev/ofw/ofw_bus_subr.h>4748#include <machine/bus.h>49#include <machine/cpu.h>50#include <machine/intr.h>5152#include <arm/freescale/vybrid/vf_edma.h>53#include <arm/freescale/vybrid/vf_dmamux.h>54#include <arm/freescale/vybrid/vf_common.h>5556struct edma_channel {57uint32_t enabled;58uint32_t mux_num;59uint32_t mux_src;60uint32_t mux_chn;61uint32_t (*ih) (void *, int);62void *ih_user;63};6465static struct edma_channel edma_map[EDMA_NUM_CHANNELS];6667static struct resource_spec edma_spec[] = {68{ SYS_RES_MEMORY, 0, RF_ACTIVE },69{ SYS_RES_MEMORY, 1, RF_ACTIVE }, /* TCD */70{ SYS_RES_IRQ, 0, RF_ACTIVE }, /* Transfer complete */71{ SYS_RES_IRQ, 1, RF_ACTIVE }, /* Error Interrupt */72{ -1, 0 }73};7475static int76edma_probe(device_t dev)77{7879if (!ofw_bus_status_okay(dev))80return (ENXIO);8182if (!ofw_bus_is_compatible(dev, "fsl,mvf600-edma"))83return (ENXIO);8485device_set_desc(dev, "Vybrid Family eDMA Controller");86return (BUS_PROBE_DEFAULT);87}8889static void90edma_transfer_complete_intr(void *arg)91{92struct edma_channel *ch;93struct edma_softc *sc;94int interrupts;95int i;9697sc = arg;9899interrupts = READ4(sc, DMA_INT);100WRITE1(sc, DMA_CINT, CINT_CAIR);101102for (i = 0; i < EDMA_NUM_CHANNELS; i++) {103if (interrupts & (0x1 << i)) {104ch = &edma_map[i];105if (ch->enabled == 1) {106if (ch->ih != NULL) {107ch->ih(ch->ih_user, i);108}109}110}111}112}113114static void115edma_err_intr(void *arg)116{117struct edma_softc *sc;118#if 0119int reg;120#endif121122sc = arg;123124/* reg = */ READ4(sc, DMA_ERR);125126#if 0127device_printf(sc->dev, "DMA_ERR 0x%08x, ES 0x%08x\n",128reg, READ4(sc, DMA_ES));129#endif130131WRITE1(sc, DMA_CERR, CERR_CAEI);132}133134static int135channel_free(struct edma_softc *sc, int chnum)136{137struct edma_channel *ch;138139ch = &edma_map[chnum];140ch->enabled = 0;141142dmamux_configure(ch->mux_num, ch->mux_src, ch->mux_chn, 0);143144return (0);145}146147static int148channel_configure(struct edma_softc *sc, int mux_grp, int mux_src)149{150struct edma_channel *ch;151int channel_first;152int mux_num;153int chnum;154int i;155156if ((sc->device_id == 0 && mux_grp == 1) || \157(sc->device_id == 1 && mux_grp == 0)) {158channel_first = NCHAN_PER_MUX;159mux_num = (sc->device_id * 2) + 1;160} else {161channel_first = 0;162mux_num = sc->device_id * 2;163}164165/* Take first unused eDMA channel */166ch = NULL;167for (i = channel_first; i < (channel_first + NCHAN_PER_MUX); i++) {168ch = &edma_map[i];169if (ch->enabled == 0) {170break;171}172ch = NULL;173}174175if (ch == NULL) {176/* Can't find free channel */177return (-1);178}179180chnum = i;181182ch->enabled = 1;183ch->mux_num = mux_num;184ch->mux_src = mux_src;185ch->mux_chn = (chnum - channel_first); /* 0 to 15 */186187dmamux_configure(ch->mux_num, ch->mux_src, ch->mux_chn, 1);188189return (chnum);190}191192static int193dma_stop(struct edma_softc *sc, int chnum)194{195int reg;196197reg = READ4(sc, DMA_ERQ);198reg &= ~(0x1 << chnum);199WRITE4(sc, DMA_ERQ, reg);200201return (0);202}203204static int205dma_setup(struct edma_softc *sc, struct tcd_conf *tcd)206{207struct edma_channel *ch;208int chnum;209int reg;210211chnum = tcd->channel;212213ch = &edma_map[chnum];214ch->ih = tcd->ih;215ch->ih_user = tcd->ih_user;216217TCD_WRITE4(sc, DMA_TCDn_SADDR(chnum), tcd->saddr);218TCD_WRITE4(sc, DMA_TCDn_DADDR(chnum), tcd->daddr);219220reg = (tcd->smod << TCD_ATTR_SMOD_SHIFT);221reg |= (tcd->dmod << TCD_ATTR_DMOD_SHIFT);222reg |= (tcd->ssize << TCD_ATTR_SSIZE_SHIFT);223reg |= (tcd->dsize << TCD_ATTR_DSIZE_SHIFT);224TCD_WRITE2(sc, DMA_TCDn_ATTR(chnum), reg);225226TCD_WRITE2(sc, DMA_TCDn_SOFF(chnum), tcd->soff);227TCD_WRITE2(sc, DMA_TCDn_DOFF(chnum), tcd->doff);228TCD_WRITE4(sc, DMA_TCDn_SLAST(chnum), tcd->slast);229TCD_WRITE4(sc, DMA_TCDn_DLASTSGA(chnum), tcd->dlast_sga);230TCD_WRITE4(sc, DMA_TCDn_NBYTES_MLOFFYES(chnum), tcd->nbytes);231232reg = tcd->nmajor; /* Current Major Iteration Count */233TCD_WRITE2(sc, DMA_TCDn_CITER_ELINKNO(chnum), reg);234TCD_WRITE2(sc, DMA_TCDn_BITER_ELINKNO(chnum), reg);235236reg = (TCD_CSR_INTMAJOR);237if(tcd->majorelink == 1) {238reg |= TCD_CSR_MAJORELINK;239reg |= (tcd->majorelinkch << TCD_CSR_MAJORELINKCH_SHIFT);240}241TCD_WRITE2(sc, DMA_TCDn_CSR(chnum), reg);242243/* Enable requests */244reg = READ4(sc, DMA_ERQ);245reg |= (0x1 << chnum);246WRITE4(sc, DMA_ERQ, reg);247248/* Enable error interrupts */249reg = READ4(sc, DMA_EEI);250reg |= (0x1 << chnum);251WRITE4(sc, DMA_EEI, reg);252253return (0);254}255256static int257dma_request(struct edma_softc *sc, int chnum)258{259int reg;260261/* Start */262reg = TCD_READ2(sc, DMA_TCDn_CSR(chnum));263reg |= TCD_CSR_START;264TCD_WRITE2(sc, DMA_TCDn_CSR(chnum), reg);265266return (0);267}268269static int270edma_attach(device_t dev)271{272struct edma_softc *sc;273phandle_t node;274int dts_value;275int len;276277sc = device_get_softc(dev);278sc->dev = dev;279280if ((node = ofw_bus_get_node(sc->dev)) == -1)281return (ENXIO);282283if ((len = OF_getproplen(node, "device-id")) <= 0)284return (ENXIO);285286OF_getencprop(node, "device-id", &dts_value, len);287sc->device_id = dts_value;288289sc->dma_stop = dma_stop;290sc->dma_setup = dma_setup;291sc->dma_request = dma_request;292sc->channel_configure = channel_configure;293sc->channel_free = channel_free;294295if (bus_alloc_resources(dev, edma_spec, sc->res)) {296device_printf(dev, "could not allocate resources\n");297return (ENXIO);298}299300/* Memory interface */301sc->bst = rman_get_bustag(sc->res[0]);302sc->bsh = rman_get_bushandle(sc->res[0]);303sc->bst_tcd = rman_get_bustag(sc->res[1]);304sc->bsh_tcd = rman_get_bushandle(sc->res[1]);305306/* Setup interrupt handlers */307if (bus_setup_intr(dev, sc->res[2], INTR_TYPE_BIO | INTR_MPSAFE,308NULL, edma_transfer_complete_intr, sc, &sc->tc_ih)) {309device_printf(dev, "Unable to alloc DMA intr resource.\n");310return (ENXIO);311}312313if (bus_setup_intr(dev, sc->res[3], INTR_TYPE_BIO | INTR_MPSAFE,314NULL, edma_err_intr, sc, &sc->err_ih)) {315device_printf(dev, "Unable to alloc DMA Err intr resource.\n");316return (ENXIO);317}318319return (0);320}321322static device_method_t edma_methods[] = {323DEVMETHOD(device_probe, edma_probe),324DEVMETHOD(device_attach, edma_attach),325{ 0, 0 }326};327328static driver_t edma_driver = {329"edma",330edma_methods,331sizeof(struct edma_softc),332};333334DRIVER_MODULE(edma, simplebus, edma_driver, 0, 0);335336337