Home | History | Annotate | Line # | Download | only in broadcom
      1  1.19     skrll /* $NetBSD: bcm2835_dmac.c,v 1.19 2021/01/29 14:11:14 skrll Exp $ */
      2   1.1  jmcneill 
      3   1.1  jmcneill /*-
      4   1.1  jmcneill  * Copyright (c) 2014 Jared D. McNeill <jmcneill (at) invisible.ca>
      5   1.1  jmcneill  * All rights reserved.
      6   1.1  jmcneill  *
      7   1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8   1.1  jmcneill  * modification, are permitted provided that the following conditions
      9   1.1  jmcneill  * are met:
     10   1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12   1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15   1.1  jmcneill  *
     16   1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21   1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22   1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23   1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24   1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.1  jmcneill  * SUCH DAMAGE.
     27   1.1  jmcneill  */
     28   1.1  jmcneill 
     29   1.1  jmcneill #include "opt_ddb.h"
     30   1.1  jmcneill 
     31   1.1  jmcneill #include <sys/cdefs.h>
     32  1.19     skrll __KERNEL_RCSID(0, "$NetBSD: bcm2835_dmac.c,v 1.19 2021/01/29 14:11:14 skrll Exp $");
     33   1.1  jmcneill 
     34   1.1  jmcneill #include <sys/param.h>
     35   1.1  jmcneill #include <sys/bus.h>
     36   1.1  jmcneill #include <sys/device.h>
     37   1.1  jmcneill #include <sys/intr.h>
     38   1.1  jmcneill #include <sys/systm.h>
     39   1.1  jmcneill #include <sys/kmem.h>
     40   1.1  jmcneill #include <sys/mutex.h>
     41   1.1  jmcneill 
     42   1.1  jmcneill #include <arm/broadcom/bcm2835reg.h>
     43   1.1  jmcneill #include <arm/broadcom/bcm2835_intr.h>
     44   1.1  jmcneill 
     45   1.1  jmcneill #include <arm/broadcom/bcm2835_dmac.h>
     46   1.1  jmcneill 
     47  1.16     skrll #include <dev/fdt/fdtvar.h>
     48  1.16     skrll 
     49  1.16     skrll #include <arm/fdt/arm_fdtvar.h>
     50  1.16     skrll 
     51   1.5  jmcneill #define BCM_DMAC_CHANNELMASK	0x00000fff
     52   1.1  jmcneill 
     53   1.1  jmcneill struct bcm_dmac_softc;
     54   1.1  jmcneill 
     55   1.1  jmcneill struct bcm_dmac_channel {
     56   1.1  jmcneill 	struct bcm_dmac_softc *ch_sc;
     57   1.1  jmcneill 	void *ch_ih;
     58   1.1  jmcneill 	uint8_t ch_index;
     59  1.13   mlelstv 	void (*ch_callback)(uint32_t, uint32_t, void *);
     60   1.1  jmcneill 	void *ch_callbackarg;
     61   1.1  jmcneill 	uint32_t ch_debug;
     62   1.1  jmcneill };
     63   1.1  jmcneill 
     64   1.1  jmcneill #define DMAC_CHANNEL_TYPE(ch) \
     65   1.1  jmcneill     (((ch)->ch_debug & DMAC_DEBUG_LITE) ? \
     66   1.1  jmcneill      BCM_DMAC_TYPE_LITE : BCM_DMAC_TYPE_NORMAL)
     67   1.1  jmcneill #define DMAC_CHANNEL_USED(ch) \
     68   1.1  jmcneill     ((ch)->ch_callback != NULL)
     69   1.1  jmcneill 
     70   1.1  jmcneill struct bcm_dmac_softc {
     71   1.1  jmcneill 	device_t sc_dev;
     72   1.1  jmcneill 	bus_space_tag_t sc_iot;
     73   1.1  jmcneill 	bus_space_handle_t sc_ioh;
     74  1.16     skrll 	int sc_phandle;
     75  1.16     skrll 
     76   1.1  jmcneill 	kmutex_t sc_lock;
     77   1.1  jmcneill 	struct bcm_dmac_channel *sc_channels;
     78   1.1  jmcneill 	int sc_nchannels;
     79   1.1  jmcneill 	uint32_t sc_channelmask;
     80   1.1  jmcneill };
     81   1.1  jmcneill 
     82   1.1  jmcneill #define DMAC_READ(sc, reg)		\
     83   1.1  jmcneill     bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))
     84   1.1  jmcneill #define DMAC_WRITE(sc, reg, val)		\
     85   1.1  jmcneill     bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
     86   1.1  jmcneill 
     87   1.1  jmcneill static int	bcm_dmac_match(device_t, cfdata_t, void *);
     88   1.1  jmcneill static void	bcm_dmac_attach(device_t, device_t, void *);
     89   1.1  jmcneill 
     90   1.1  jmcneill static int	bcm_dmac_intr(void *);
     91   1.1  jmcneill 
     92   1.1  jmcneill #if defined(DDB)
     93   1.1  jmcneill void		bcm_dmac_dump_regs(void);
     94   1.1  jmcneill #endif
     95   1.1  jmcneill 
     96  1.16     skrll CFATTACH_DECL_NEW(bcmdmac_fdt, sizeof(struct bcm_dmac_softc),
     97   1.1  jmcneill 	bcm_dmac_match, bcm_dmac_attach, NULL, NULL);
     98   1.1  jmcneill 
     99  1.18   thorpej static const struct device_compatible_entry compat_data[] = {
    100  1.18   thorpej 	{ .compat = "brcm,bcm2835-dma" },
    101  1.18   thorpej 	DEVICE_COMPAT_EOL
    102  1.18   thorpej };
    103  1.18   thorpej 
    104   1.1  jmcneill static int
    105   1.1  jmcneill bcm_dmac_match(device_t parent, cfdata_t cf, void *aux)
    106   1.1  jmcneill {
    107  1.16     skrll 	struct fdt_attach_args * const faa = aux;
    108   1.1  jmcneill 
    109  1.18   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    110   1.1  jmcneill }
    111   1.1  jmcneill 
    112   1.1  jmcneill static void
    113   1.1  jmcneill bcm_dmac_attach(device_t parent, device_t self, void *aux)
    114   1.1  jmcneill {
    115   1.1  jmcneill 	struct bcm_dmac_softc *sc = device_private(self);
    116   1.4  jmcneill 	const prop_dictionary_t cfg = device_properties(self);
    117  1.16     skrll 	struct fdt_attach_args * const faa = aux;
    118   1.1  jmcneill 	struct bcm_dmac_channel *ch;
    119   1.1  jmcneill 	uint32_t val;
    120   1.1  jmcneill 	int index;
    121   1.1  jmcneill 
    122  1.16     skrll 	const int phandle = faa->faa_phandle;
    123  1.16     skrll 
    124   1.1  jmcneill 	sc->sc_dev = self;
    125  1.16     skrll 	sc->sc_iot = faa->faa_bst;
    126  1.16     skrll 	sc->sc_phandle = phandle;
    127  1.16     skrll 
    128  1.16     skrll 	bus_addr_t addr;
    129  1.16     skrll 	bus_size_t size;
    130   1.1  jmcneill 
    131  1.16     skrll 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    132  1.16     skrll 		aprint_error(": missing 'reg' property\n");
    133  1.16     skrll 		return;
    134  1.16     skrll 	}
    135  1.16     skrll 
    136  1.16     skrll 	if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh)) {
    137   1.1  jmcneill 		aprint_error(": unable to map device\n");
    138   1.1  jmcneill 		return;
    139   1.1  jmcneill 	}
    140   1.1  jmcneill 
    141   1.3  jmcneill 	prop_dictionary_get_uint32(cfg, "chanmask", &sc->sc_channelmask);
    142   1.3  jmcneill 	sc->sc_channelmask &= BCM_DMAC_CHANNELMASK;
    143   1.1  jmcneill 
    144   1.1  jmcneill 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SCHED);
    145   1.1  jmcneill 
    146  1.17  jmcneill 	sc->sc_nchannels = 32 - __builtin_clz(sc->sc_channelmask);
    147   1.1  jmcneill 	sc->sc_channels = kmem_alloc(
    148   1.1  jmcneill 	    sizeof(*sc->sc_channels) * sc->sc_nchannels, KM_SLEEP);
    149   1.1  jmcneill 
    150   1.1  jmcneill 	aprint_normal(":");
    151   1.1  jmcneill 	for (index = 0; index < sc->sc_nchannels; index++) {
    152   1.1  jmcneill 		ch = &sc->sc_channels[index];
    153   1.1  jmcneill 		ch->ch_sc = sc;
    154   1.1  jmcneill 		ch->ch_index = index;
    155   1.1  jmcneill 		ch->ch_callback = NULL;
    156   1.1  jmcneill 		ch->ch_callbackarg = NULL;
    157   1.8  jmcneill 		ch->ch_ih = NULL;
    158   1.2  jmcneill 		if ((__BIT(index) & sc->sc_channelmask) == 0)
    159   1.1  jmcneill 			continue;
    160   1.1  jmcneill 
    161   1.1  jmcneill 		aprint_normal(" DMA%d", index);
    162   1.1  jmcneill 
    163   1.1  jmcneill 		ch->ch_debug = DMAC_READ(sc, DMAC_DEBUG(index));
    164   1.1  jmcneill 
    165   1.1  jmcneill 		val = DMAC_READ(sc, DMAC_CS(index));
    166   1.1  jmcneill 		val |= DMAC_CS_RESET;
    167   1.1  jmcneill 		DMAC_WRITE(sc, DMAC_CS(index), val);
    168   1.1  jmcneill 	}
    169   1.1  jmcneill 	aprint_normal("\n");
    170   1.1  jmcneill 	aprint_naive("\n");
    171   1.1  jmcneill }
    172   1.1  jmcneill 
    173   1.1  jmcneill static int
    174   1.1  jmcneill bcm_dmac_intr(void *priv)
    175   1.1  jmcneill {
    176   1.1  jmcneill 	struct bcm_dmac_channel *ch = priv;
    177   1.1  jmcneill 	struct bcm_dmac_softc *sc = ch->ch_sc;
    178  1.13   mlelstv 	uint32_t cs, ce;
    179   1.1  jmcneill 
    180   1.1  jmcneill 	cs = DMAC_READ(sc, DMAC_CS(ch->ch_index));
    181  1.13   mlelstv 	DMAC_WRITE(sc, DMAC_CS(ch->ch_index), cs);
    182  1.13   mlelstv 	cs &= DMAC_CS_INT | DMAC_CS_END | DMAC_CS_ERROR;
    183   1.1  jmcneill 
    184  1.13   mlelstv 	ce = DMAC_READ(sc, DMAC_DEBUG(ch->ch_index));
    185  1.13   mlelstv 	ce &= DMAC_DEBUG_READ_ERROR | DMAC_DEBUG_FIFO_ERROR
    186  1.13   mlelstv 	    | DMAC_DEBUG_READ_LAST_NOT_SET_ERROR;
    187  1.13   mlelstv 	DMAC_WRITE(sc, DMAC_DEBUG(ch->ch_index), ce);
    188   1.1  jmcneill 
    189   1.1  jmcneill 	if (ch->ch_callback)
    190  1.13   mlelstv 		ch->ch_callback(cs, ce, ch->ch_callbackarg);
    191   1.1  jmcneill 
    192   1.1  jmcneill 	return 1;
    193   1.1  jmcneill }
    194   1.1  jmcneill 
    195   1.1  jmcneill struct bcm_dmac_channel *
    196  1.13   mlelstv bcm_dmac_alloc(enum bcm_dmac_type type, int ipl,
    197  1.13   mlelstv     void (*cb)(uint32_t, uint32_t, void *), void *cbarg)
    198   1.1  jmcneill {
    199   1.1  jmcneill 	struct bcm_dmac_softc *sc;
    200   1.1  jmcneill 	struct bcm_dmac_channel *ch = NULL;
    201   1.1  jmcneill 	device_t dev;
    202   1.1  jmcneill 	int index;
    203   1.1  jmcneill 
    204   1.1  jmcneill 	dev = device_find_by_driver_unit("bcmdmac", 0);
    205   1.1  jmcneill 	if (dev == NULL)
    206   1.1  jmcneill 		return NULL;
    207   1.1  jmcneill 	sc = device_private(dev);
    208   1.1  jmcneill 
    209   1.1  jmcneill 	mutex_enter(&sc->sc_lock);
    210   1.1  jmcneill 	for (index = 0; index < sc->sc_nchannels; index++) {
    211   1.1  jmcneill 		if ((sc->sc_channelmask & __BIT(index)) == 0)
    212   1.1  jmcneill 			continue;
    213   1.1  jmcneill 		if (DMAC_CHANNEL_TYPE(&sc->sc_channels[index]) != type)
    214   1.1  jmcneill 			continue;
    215   1.1  jmcneill 		if (DMAC_CHANNEL_USED(&sc->sc_channels[index]))
    216   1.1  jmcneill 			continue;
    217   1.1  jmcneill 
    218   1.1  jmcneill 		ch = &sc->sc_channels[index];
    219   1.1  jmcneill 		ch->ch_callback = cb;
    220   1.1  jmcneill 		ch->ch_callbackarg = cbarg;
    221   1.1  jmcneill 		break;
    222   1.1  jmcneill 	}
    223   1.1  jmcneill 	mutex_exit(&sc->sc_lock);
    224   1.1  jmcneill 
    225   1.7  jmcneill 	if (ch == NULL)
    226   1.7  jmcneill 		return NULL;
    227   1.7  jmcneill 
    228   1.7  jmcneill 	KASSERT(ch->ch_ih == NULL);
    229  1.16     skrll 
    230  1.16     skrll 	const int phandle = sc->sc_phandle;
    231  1.16     skrll 	char intrstr[128];
    232  1.16     skrll 
    233  1.16     skrll 	if (!fdtbus_intr_str(phandle, ch->ch_index, intrstr, sizeof(intrstr))) {
    234  1.16     skrll 		aprint_error(": failed to decode interrupt\n");
    235  1.16     skrll 		return NULL;
    236  1.16     skrll 	}
    237  1.16     skrll 
    238  1.19     skrll 	char xname[16];
    239  1.19     skrll 	snprintf(xname, sizeof(xname), "%s #%u", device_xname(sc->sc_dev),
    240  1.19     skrll 	    ch->ch_index);
    241  1.19     skrll 	ch->ch_ih = fdtbus_intr_establish_xname(phandle, ch->ch_index, ipl, 0,
    242  1.19     skrll 	    bcm_dmac_intr, ch, xname);
    243   1.6  jakllsch 	if (ch->ch_ih == NULL) {
    244   1.6  jakllsch 		aprint_error_dev(sc->sc_dev,
    245  1.16     skrll 		    "failed to establish interrupt for DMA%d and %s\n", ch->ch_index,
    246  1.16     skrll 		    intrstr);
    247   1.7  jmcneill 		ch->ch_callback = NULL;
    248   1.7  jmcneill 		ch->ch_callbackarg = NULL;
    249   1.9  jmcneill 		ch = NULL;
    250   1.6  jakllsch 	}
    251   1.6  jakllsch 
    252   1.1  jmcneill 	return ch;
    253   1.1  jmcneill }
    254   1.1  jmcneill 
    255   1.1  jmcneill void
    256   1.1  jmcneill bcm_dmac_free(struct bcm_dmac_channel *ch)
    257   1.1  jmcneill {
    258   1.1  jmcneill 	struct bcm_dmac_softc *sc = ch->ch_sc;
    259   1.1  jmcneill 	uint32_t val;
    260   1.1  jmcneill 
    261   1.1  jmcneill 	bcm_dmac_halt(ch);
    262   1.1  jmcneill 
    263  1.14   mlelstv 	/* reset chip */
    264   1.1  jmcneill 	val = DMAC_READ(sc, DMAC_CS(ch->ch_index));
    265   1.1  jmcneill 	val |= DMAC_CS_RESET;
    266   1.1  jmcneill 	val &= ~DMAC_CS_ACTIVE;
    267   1.1  jmcneill 	DMAC_WRITE(sc, DMAC_CS(ch->ch_index), val);
    268   1.1  jmcneill 
    269   1.1  jmcneill 	mutex_enter(&sc->sc_lock);
    270  1.16     skrll 	fdtbus_intr_disestablish(sc->sc_phandle, ch->ch_ih);
    271   1.7  jmcneill 	ch->ch_ih = NULL;
    272   1.1  jmcneill 	ch->ch_callback = NULL;
    273   1.1  jmcneill 	ch->ch_callbackarg = NULL;
    274   1.1  jmcneill 	mutex_exit(&sc->sc_lock);
    275   1.1  jmcneill }
    276   1.1  jmcneill 
    277   1.1  jmcneill void
    278   1.1  jmcneill bcm_dmac_set_conblk_addr(struct bcm_dmac_channel *ch, bus_addr_t addr)
    279   1.1  jmcneill {
    280   1.1  jmcneill 	struct bcm_dmac_softc *sc = ch->ch_sc;
    281   1.1  jmcneill 
    282   1.1  jmcneill 	DMAC_WRITE(sc, DMAC_CONBLK_AD(ch->ch_index), addr);
    283   1.1  jmcneill }
    284   1.1  jmcneill 
    285   1.1  jmcneill int
    286   1.1  jmcneill bcm_dmac_transfer(struct bcm_dmac_channel *ch)
    287   1.1  jmcneill {
    288   1.1  jmcneill 	struct bcm_dmac_softc *sc = ch->ch_sc;
    289   1.1  jmcneill 	uint32_t val;
    290   1.1  jmcneill 
    291   1.1  jmcneill 	val = DMAC_READ(sc, DMAC_CS(ch->ch_index));
    292   1.1  jmcneill 	if (val & DMAC_CS_ACTIVE)
    293   1.1  jmcneill 		return EBUSY;
    294   1.1  jmcneill 
    295   1.1  jmcneill 	val |= DMAC_CS_ACTIVE;
    296   1.1  jmcneill 	DMAC_WRITE(sc, DMAC_CS(ch->ch_index), val);
    297   1.1  jmcneill 
    298   1.1  jmcneill 	return 0;
    299   1.1  jmcneill }
    300   1.1  jmcneill 
    301   1.1  jmcneill void
    302   1.1  jmcneill bcm_dmac_halt(struct bcm_dmac_channel *ch)
    303   1.1  jmcneill {
    304  1.12  jmcneill 	struct bcm_dmac_softc *sc = ch->ch_sc;
    305  1.14   mlelstv 	uint32_t val;
    306  1.14   mlelstv 
    307  1.14   mlelstv 	/* pause DMA */
    308  1.14   mlelstv 	val = DMAC_READ(sc, DMAC_CS(ch->ch_index));
    309  1.14   mlelstv 	val &= ~DMAC_CS_ACTIVE;
    310  1.14   mlelstv 	DMAC_WRITE(sc, DMAC_CS(ch->ch_index), val);
    311  1.14   mlelstv 
    312  1.14   mlelstv 	/* wait for paused state ? */
    313  1.12  jmcneill 
    314  1.14   mlelstv 	/* end descriptor chain */
    315  1.14   mlelstv 	DMAC_WRITE(sc, DMAC_NEXTCONBK(ch->ch_index), 0);
    316  1.14   mlelstv 
    317  1.14   mlelstv 	/* resume DMA that then stops */
    318  1.14   mlelstv 	val |= DMAC_CS_ACTIVE | DMAC_CS_ABORT;
    319  1.14   mlelstv 	DMAC_WRITE(sc, DMAC_CS(ch->ch_index), val);
    320   1.1  jmcneill }
    321   1.1  jmcneill 
    322   1.1  jmcneill #if defined(DDB)
    323   1.1  jmcneill void
    324   1.1  jmcneill bcm_dmac_dump_regs(void)
    325   1.1  jmcneill {
    326   1.1  jmcneill 	struct bcm_dmac_softc *sc;
    327   1.1  jmcneill 	device_t dev;
    328   1.1  jmcneill 	int index;
    329   1.1  jmcneill 
    330   1.1  jmcneill 	dev = device_find_by_driver_unit("bcmdmac", 0);
    331   1.1  jmcneill 	if (dev == NULL)
    332   1.1  jmcneill 		return;
    333   1.1  jmcneill 	sc = device_private(dev);
    334   1.1  jmcneill 
    335   1.1  jmcneill 	for (index = 0; index < sc->sc_nchannels; index++) {
    336   1.1  jmcneill 		if ((sc->sc_channelmask & __BIT(index)) == 0)
    337   1.1  jmcneill 			continue;
    338   1.1  jmcneill 		printf("%d_CS:        %08X\n", index,
    339   1.1  jmcneill 		    DMAC_READ(sc, DMAC_CS(index)));
    340   1.1  jmcneill 		printf("%d_CONBLK_AD: %08X\n", index,
    341   1.1  jmcneill 		    DMAC_READ(sc, DMAC_CONBLK_AD(index)));
    342   1.1  jmcneill 		printf("%d_DEBUG:     %08X\n", index,
    343   1.1  jmcneill 		    DMAC_READ(sc, DMAC_DEBUG(index)));
    344   1.1  jmcneill 	}
    345   1.1  jmcneill }
    346   1.1  jmcneill #endif
    347