Home | History | Annotate | Line # | Download | only in sunxi
sun6i_dma.c revision 1.9
      1  1.9  jakllsch /* $NetBSD: sun6i_dma.c,v 1.9 2019/03/06 19:16:53 jakllsch Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2014-2017 Jared 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.2  jmcneill #include "opt_ddb.h"
     30  1.2  jmcneill 
     31  1.1  jmcneill #include <sys/cdefs.h>
     32  1.9  jakllsch __KERNEL_RCSID(0, "$NetBSD: sun6i_dma.c,v 1.9 2019/03/06 19:16:53 jakllsch 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/mutex.h>
     40  1.1  jmcneill #include <sys/bitops.h>
     41  1.1  jmcneill #include <sys/kmem.h>
     42  1.1  jmcneill 
     43  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     44  1.1  jmcneill 
     45  1.1  jmcneill #define DMA_IRQ_EN_REG0_REG		0x0000
     46  1.1  jmcneill #define DMA_IRQ_EN_REG1_REG		0x0004
     47  1.1  jmcneill #define  DMA_IRQ_EN_REG0_QUEUE_IRQ_EN(n)	__BIT(n * 4 + 2)
     48  1.1  jmcneill #define  DMA_IRQ_EN_REG0_PKG_IRQ_EN(n)		__BIT(n * 4 + 1)
     49  1.1  jmcneill #define  DMA_IRQ_EN_REG0_HLAF_IRQ_EN(n)		__BIT(n * 4 + 0)
     50  1.1  jmcneill #define  DMA_IRQ_EN_REG1_QUEUE_IRQ_EN(n)	__BIT((n - 8) * 4 + 2)
     51  1.1  jmcneill #define  DMA_IRQ_EN_REG1_PKG_IRQ_EN(n)		__BIT((n - 8) * 4 + 1)
     52  1.1  jmcneill #define  DMA_IRQ_EN_REG1_HLAF_IRQ_EN(n)		__BIT((n - 8) * 4 + 0)
     53  1.1  jmcneill #define DMA_IRQ_PEND_REG0_REG		0x0010
     54  1.1  jmcneill #define DMA_IRQ_PEND_REG1_REG		0x0014
     55  1.1  jmcneill #define  DMA_IRQ_QUEUE_MASK			0x4444444444444444ULL
     56  1.1  jmcneill #define  DMA_IRQ_PKG_MASK			0x2222222222222222ULL
     57  1.1  jmcneill #define  DMA_IRQ_HF_MASK			0x1111111111111111ULL
     58  1.1  jmcneill #define DMA_STA_REG			0x0030
     59  1.1  jmcneill #define DMA_EN_REG(n)			(0x0100 + (n) * 0x40 + 0x00)
     60  1.1  jmcneill #define  DMA_EN_EN				__BIT(0)
     61  1.1  jmcneill #define DMA_PAU_REG(n)			(0x0100 + (n) * 0x40 + 0x04)
     62  1.1  jmcneill #define  DMA_PAU_PAUSE				__BIT(0)
     63  1.1  jmcneill #define DMA_START_ADDR_REG(n)		(0x0100 + (n) * 0x40 + 0x08)
     64  1.1  jmcneill #define DMA_CFG_REG(n)			(0x0100 + (n) * 0x40 + 0x0C)
     65  1.1  jmcneill #define  DMA_CFG_DEST_DATA_WIDTH		__BITS(26,25)
     66  1.1  jmcneill #define   DMA_CFG_DATA_WIDTH(n)			((n) >> 4)
     67  1.1  jmcneill #define	  DMA_CFG_BST_LEN(n)			((n) == 1 ? 0 : (((n) >> 3) + 1))
     68  1.1  jmcneill #define  DMA_CFG_DEST_ADDR_MODE			__BITS(22,21)
     69  1.1  jmcneill #define   DMA_CFG_ADDR_MODE_LINEAR		0
     70  1.1  jmcneill #define   DMA_CFG_ADDR_MODE_IO			1
     71  1.1  jmcneill #define  DMA_CFG_DEST_DRQ_TYPE			__BITS(20,16)
     72  1.1  jmcneill #define	  DMA_CFG_DRQ_TYPE_SDRAM		1
     73  1.1  jmcneill #define  DMA_CFG_SRC_DATA_WIDTH			__BITS(10,9)
     74  1.1  jmcneill #define  DMA_CFG_SRC_ADDR_MODE			__BITS(6,5)
     75  1.1  jmcneill #define  DMA_CFG_SRC_DRQ_TYPE			__BITS(4,0)
     76  1.1  jmcneill #define DMA_CUR_SRC_REG(n)		(0x0100 + (n) * 0x40 + 0x10)
     77  1.1  jmcneill #define DMA_CUR_DEST_REG(n)		(0x0100 + (n) * 0x40 + 0x14)
     78  1.1  jmcneill #define DMA_BCNT_LEFT_REG(n)		(0x0100 + (n) * 0x40 + 0x18)
     79  1.1  jmcneill #define DMA_PARA_REG(n)			(0x0100 + (n) * 0x40 + 0x1C)
     80  1.1  jmcneill #define  DMA_PARA_DATA_BLK_SIZE			__BITS(15,8)
     81  1.1  jmcneill #define  DMA_PARA_WAIT_CYC			__BITS(7,0)
     82  1.8  jakllsch #define DMA_MODE_REG(n)			(0x0100 + (n) * 0x40 + 0x28)
     83  1.8  jakllsch #define  MODE_WAIT				0b0
     84  1.8  jakllsch #define  MODE_HANDSHAKE				0b1
     85  1.8  jakllsch #define  DMA_MODE_DST(m)			__SHIFTIN((m), __BIT(3))
     86  1.8  jakllsch #define  DMA_MODE_SRC(m)			__SHIFTIN((m), __BIT(2))
     87  1.8  jakllsch #define DMA_FDESC_ADDR_REG(n)		(0x0100 + (n) * 0x40 + 0x2C)
     88  1.8  jakllsch #define DMA_PKG_NUM_REG(n)		(0x0100 + (n) * 0x40 + 0x30)
     89  1.1  jmcneill 
     90  1.1  jmcneill struct sun6idma_desc {
     91  1.1  jmcneill 	uint32_t	dma_config;
     92  1.1  jmcneill 	uint32_t	dma_srcaddr;
     93  1.1  jmcneill 	uint32_t	dma_dstaddr;
     94  1.1  jmcneill 	uint32_t	dma_bcnt;
     95  1.1  jmcneill 	uint32_t	dma_para;
     96  1.1  jmcneill 	uint32_t	dma_next;
     97  1.1  jmcneill #define DMA_NULL	0xfffff800
     98  1.1  jmcneill };
     99  1.1  jmcneill 
    100  1.4  jmcneill struct sun6idma_config {
    101  1.4  jmcneill 	u_int		num_channels;
    102  1.4  jmcneill 	bool		autogate;
    103  1.8  jakllsch 	uint8_t		bursts;
    104  1.8  jakllsch 	uint8_t		widths;
    105  1.4  jmcneill 	bus_size_t	autogate_reg;
    106  1.4  jmcneill 	uint32_t	autogate_mask;
    107  1.6  jmcneill 	uint32_t	burst_mask;
    108  1.4  jmcneill };
    109  1.4  jmcneill 
    110  1.8  jakllsch #define IL2B(x)			__BIT(ilog2(x))
    111  1.8  jakllsch #define IL2B_RANGE(x, y)	__BITS(ilog2(x), ilog2(y))
    112  1.8  jakllsch #define WIDTHS_1_2_4		IL2B_RANGE(4, 1)
    113  1.8  jakllsch #define WIDTHS_1_2_4_8		IL2B_RANGE(8, 1)
    114  1.8  jakllsch #define BURSTS_1_8		(IL2B(8)|IL2B(1))
    115  1.8  jakllsch #define BURSTS_1_4_8_16		(IL2B(16)|IL2B(8)|IL2B(4)|IL2B(1))
    116  1.8  jakllsch 
    117  1.4  jmcneill static const struct sun6idma_config sun6i_a31_dma_config = {
    118  1.6  jmcneill 	.num_channels = 16,
    119  1.6  jmcneill 	.burst_mask = __BITS(8,7),
    120  1.8  jakllsch 	.bursts = BURSTS_1_8,
    121  1.8  jakllsch 	.widths = WIDTHS_1_2_4,
    122  1.4  jmcneill };
    123  1.4  jmcneill 
    124  1.4  jmcneill static const struct sun6idma_config sun8i_a83t_dma_config = {
    125  1.4  jmcneill 	.num_channels = 8,
    126  1.4  jmcneill 	.autogate = true,
    127  1.4  jmcneill 	.autogate_reg = 0x20,
    128  1.4  jmcneill 	.autogate_mask = 0x4,
    129  1.6  jmcneill 	.burst_mask = __BITS(8,7),
    130  1.8  jakllsch 	.bursts = BURSTS_1_8,
    131  1.8  jakllsch 	.widths = WIDTHS_1_2_4,
    132  1.4  jmcneill };
    133  1.4  jmcneill 
    134  1.4  jmcneill static const struct sun6idma_config sun8i_h3_dma_config = {
    135  1.4  jmcneill 	.num_channels = 12,
    136  1.4  jmcneill 	.autogate = true,
    137  1.4  jmcneill 	.autogate_reg = 0x28,
    138  1.4  jmcneill 	.autogate_mask = 0x4,
    139  1.6  jmcneill 	.burst_mask = __BITS(7,6),
    140  1.8  jakllsch 	.bursts = BURSTS_1_4_8_16,
    141  1.8  jakllsch 	.widths = WIDTHS_1_2_4_8,
    142  1.4  jmcneill };
    143  1.4  jmcneill 
    144  1.4  jmcneill static const struct sun6idma_config sun50i_a64_dma_config = {
    145  1.4  jmcneill 	.num_channels = 8,
    146  1.4  jmcneill 	.autogate = true,
    147  1.4  jmcneill 	.autogate_reg = 0x28,
    148  1.4  jmcneill 	.autogate_mask = 0x4,
    149  1.6  jmcneill 	.burst_mask = __BITS(7,6),
    150  1.8  jakllsch 	.bursts = BURSTS_1_4_8_16,
    151  1.8  jakllsch 	.widths = WIDTHS_1_2_4_8,
    152  1.4  jmcneill };
    153  1.4  jmcneill 
    154  1.1  jmcneill static const struct of_compat_data compat_data[] = {
    155  1.4  jmcneill 	{ "allwinner,sun6i-a31-dma",	(uintptr_t)&sun6i_a31_dma_config },
    156  1.4  jmcneill 	{ "allwinner,sun8i-a83t-dma",	(uintptr_t)&sun8i_a83t_dma_config },
    157  1.4  jmcneill 	{ "allwinner,sun8i-h3-dma",	(uintptr_t)&sun8i_h3_dma_config },
    158  1.4  jmcneill 	{ "allwinner,sun50i-a64-dma",	(uintptr_t)&sun50i_a64_dma_config },
    159  1.1  jmcneill 	{ NULL }
    160  1.1  jmcneill };
    161  1.1  jmcneill 
    162  1.1  jmcneill struct sun6idma_channel {
    163  1.1  jmcneill 	uint8_t			ch_index;
    164  1.1  jmcneill 	void			(*ch_callback)(void *);
    165  1.1  jmcneill 	void			*ch_callbackarg;
    166  1.1  jmcneill 	u_int			ch_portid;
    167  1.1  jmcneill 	void			*ch_dmadesc;
    168  1.1  jmcneill };
    169  1.1  jmcneill 
    170  1.1  jmcneill struct sun6idma_softc {
    171  1.1  jmcneill 	device_t		sc_dev;
    172  1.1  jmcneill 	bus_space_tag_t		sc_bst;
    173  1.1  jmcneill 	bus_space_handle_t	sc_bsh;
    174  1.1  jmcneill 	bus_dma_tag_t		sc_dmat;
    175  1.1  jmcneill 	int			sc_phandle;
    176  1.1  jmcneill 	void			*sc_ih;
    177  1.1  jmcneill 
    178  1.6  jmcneill 	uint32_t		sc_burst_mask;
    179  1.6  jmcneill 
    180  1.1  jmcneill 	kmutex_t		sc_lock;
    181  1.1  jmcneill 
    182  1.1  jmcneill 	struct sun6idma_channel	*sc_chan;
    183  1.1  jmcneill 	u_int			sc_nchan;
    184  1.7  jakllsch 	u_int			sc_ndesc_ch;
    185  1.8  jakllsch 	uint8_t			sc_widths;
    186  1.8  jakllsch 	uint8_t			sc_bursts;
    187  1.7  jakllsch 
    188  1.7  jakllsch 	bus_dma_segment_t	sc_dmasegs[1];
    189  1.7  jakllsch 	bus_dmamap_t		sc_dmamap;
    190  1.7  jakllsch 	void			*sc_dmadescs;
    191  1.1  jmcneill };
    192  1.1  jmcneill 
    193  1.1  jmcneill #define DMA_READ(sc, reg)		\
    194  1.1  jmcneill     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    195  1.1  jmcneill #define DMA_WRITE(sc, reg, val)		\
    196  1.1  jmcneill     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    197  1.1  jmcneill 
    198  1.7  jakllsch #define DESC_NUM			((MAXPHYS / MIN_PAGE_SIZE + 1) + 1)
    199  1.7  jakllsch #define DESC_LEN(n)			\
    200  1.7  jakllsch     (sizeof(struct sun6idma_desc) * (n))
    201  1.7  jakllsch #define DESC_OFFS(ch, n)		\
    202  1.7  jakllsch     ((ch) * roundup2(DESC_LEN(DESC_NUM), COHERENCY_UNIT) + DESC_LEN(n))
    203  1.7  jakllsch #define DESC_ADDR(sc, chp, n)		\
    204  1.7  jakllsch     ((sc)->sc_dmamap->dm_segs[0].ds_addr + DESC_OFFS((chp)->ch_index, (n)))
    205  1.7  jakllsch 
    206  1.1  jmcneill static void *
    207  1.1  jmcneill sun6idma_acquire(device_t dev, const void *data, size_t len,
    208  1.1  jmcneill     void (*cb)(void *), void *cbarg)
    209  1.1  jmcneill {
    210  1.1  jmcneill 	struct sun6idma_softc *sc = device_private(dev);
    211  1.1  jmcneill 	struct sun6idma_channel *ch = NULL;
    212  1.1  jmcneill 	uint32_t irqen;
    213  1.1  jmcneill 	uint8_t index;
    214  1.1  jmcneill 
    215  1.1  jmcneill 	if (len != 4)
    216  1.1  jmcneill 		return NULL;
    217  1.1  jmcneill 
    218  1.1  jmcneill 	const u_int portid = be32dec(data);
    219  1.1  jmcneill 	if (portid > __SHIFTOUT_MASK(DMA_CFG_SRC_DRQ_TYPE))
    220  1.1  jmcneill 		return NULL;
    221  1.1  jmcneill 
    222  1.1  jmcneill 	mutex_enter(&sc->sc_lock);
    223  1.1  jmcneill 
    224  1.1  jmcneill 	for (index = 0; index < sc->sc_nchan; index++) {
    225  1.1  jmcneill 		if (sc->sc_chan[index].ch_callback == NULL) {
    226  1.1  jmcneill 			ch = &sc->sc_chan[index];
    227  1.1  jmcneill 			ch->ch_callback = cb;
    228  1.1  jmcneill 			ch->ch_callbackarg = cbarg;
    229  1.1  jmcneill 			ch->ch_portid = portid;
    230  1.1  jmcneill 
    231  1.1  jmcneill 			irqen = DMA_READ(sc, index < 8 ?
    232  1.1  jmcneill 			    DMA_IRQ_EN_REG0_REG :
    233  1.1  jmcneill 			    DMA_IRQ_EN_REG1_REG);
    234  1.1  jmcneill 			irqen |= (index < 8 ?
    235  1.1  jmcneill 			    DMA_IRQ_EN_REG0_PKG_IRQ_EN(index) :
    236  1.1  jmcneill 			    DMA_IRQ_EN_REG1_PKG_IRQ_EN(index));
    237  1.1  jmcneill 			DMA_WRITE(sc, index < 8 ?
    238  1.1  jmcneill 			    DMA_IRQ_EN_REG0_REG :
    239  1.1  jmcneill 			    DMA_IRQ_EN_REG1_REG, irqen);
    240  1.1  jmcneill 
    241  1.1  jmcneill 			break;
    242  1.1  jmcneill 		}
    243  1.1  jmcneill 	}
    244  1.1  jmcneill 
    245  1.1  jmcneill 	mutex_exit(&sc->sc_lock);
    246  1.1  jmcneill 
    247  1.1  jmcneill 	return ch;
    248  1.1  jmcneill }
    249  1.1  jmcneill 
    250  1.1  jmcneill static void
    251  1.1  jmcneill sun6idma_release(device_t dev, void *priv)
    252  1.1  jmcneill {
    253  1.1  jmcneill 	struct sun6idma_softc *sc = device_private(dev);
    254  1.1  jmcneill 	struct sun6idma_channel *ch = priv;
    255  1.1  jmcneill 	uint32_t irqen;
    256  1.1  jmcneill 	uint8_t index = ch->ch_index;
    257  1.1  jmcneill 
    258  1.1  jmcneill 	mutex_enter(&sc->sc_lock);
    259  1.1  jmcneill 
    260  1.1  jmcneill 	irqen = DMA_READ(sc, index < 8 ?
    261  1.1  jmcneill 	    DMA_IRQ_EN_REG0_REG :
    262  1.1  jmcneill 	    DMA_IRQ_EN_REG1_REG);
    263  1.1  jmcneill 	irqen &= ~(index < 8 ?
    264  1.1  jmcneill 	    DMA_IRQ_EN_REG0_PKG_IRQ_EN(index) :
    265  1.1  jmcneill 	    DMA_IRQ_EN_REG1_PKG_IRQ_EN(index));
    266  1.1  jmcneill 	DMA_WRITE(sc, index < 8 ?
    267  1.1  jmcneill 	    DMA_IRQ_EN_REG0_REG :
    268  1.1  jmcneill 	    DMA_IRQ_EN_REG1_REG, irqen);
    269  1.1  jmcneill 
    270  1.1  jmcneill 	ch->ch_callback = NULL;
    271  1.1  jmcneill 	ch->ch_callbackarg = NULL;
    272  1.1  jmcneill 
    273  1.1  jmcneill 	mutex_exit(&sc->sc_lock);
    274  1.1  jmcneill }
    275  1.1  jmcneill 
    276  1.1  jmcneill static int
    277  1.1  jmcneill sun6idma_transfer(device_t dev, void *priv, struct fdtbus_dma_req *req)
    278  1.1  jmcneill {
    279  1.1  jmcneill 	struct sun6idma_softc *sc = device_private(dev);
    280  1.1  jmcneill 	struct sun6idma_channel *ch = priv;
    281  1.1  jmcneill 	struct sun6idma_desc *desc = ch->ch_dmadesc;
    282  1.1  jmcneill 	uint32_t src, dst, len, cfg, mem_cfg, dev_cfg;
    283  1.1  jmcneill 	uint32_t mem_width, dev_width, mem_burst, dev_burst;
    284  1.1  jmcneill 
    285  1.7  jakllsch 	if (req->dreq_nsegs > sc->sc_ndesc_ch)
    286  1.1  jmcneill 		return EINVAL;
    287  1.1  jmcneill 
    288  1.8  jakllsch 	if ((sc->sc_widths &
    289  1.8  jakllsch 	    IL2B(req->dreq_mem_opt.opt_bus_width/NBBY)) == 0)
    290  1.8  jakllsch 		return EINVAL;
    291  1.8  jakllsch 	if ((sc->sc_widths &
    292  1.8  jakllsch 	    IL2B(req->dreq_dev_opt.opt_bus_width/NBBY)) == 0)
    293  1.8  jakllsch 		return EINVAL;
    294  1.8  jakllsch 	if ((sc->sc_bursts &
    295  1.8  jakllsch 	    IL2B(req->dreq_mem_opt.opt_burst_len)) == 0)
    296  1.8  jakllsch 		return EINVAL;
    297  1.8  jakllsch 	if ((sc->sc_bursts &
    298  1.8  jakllsch 	    IL2B(req->dreq_dev_opt.opt_burst_len)) == 0)
    299  1.8  jakllsch 		return EINVAL;
    300  1.8  jakllsch 
    301  1.1  jmcneill 	mem_width = DMA_CFG_DATA_WIDTH(req->dreq_mem_opt.opt_bus_width);
    302  1.1  jmcneill 	dev_width = DMA_CFG_DATA_WIDTH(req->dreq_dev_opt.opt_bus_width);
    303  1.2  jmcneill 	mem_burst = DMA_CFG_BST_LEN(req->dreq_mem_opt.opt_burst_len);
    304  1.2  jmcneill 	dev_burst = DMA_CFG_BST_LEN(req->dreq_dev_opt.opt_burst_len);
    305  1.1  jmcneill 
    306  1.1  jmcneill 	mem_cfg = __SHIFTIN(mem_width, DMA_CFG_SRC_DATA_WIDTH) |
    307  1.6  jmcneill 	    __SHIFTIN(mem_burst, sc->sc_burst_mask) |
    308  1.1  jmcneill 	    __SHIFTIN(DMA_CFG_ADDR_MODE_LINEAR, DMA_CFG_SRC_ADDR_MODE) |
    309  1.1  jmcneill 	    __SHIFTIN(DMA_CFG_DRQ_TYPE_SDRAM, DMA_CFG_SRC_DRQ_TYPE);
    310  1.1  jmcneill 	dev_cfg = __SHIFTIN(dev_width, DMA_CFG_SRC_DATA_WIDTH) |
    311  1.6  jmcneill 	    __SHIFTIN(dev_burst, sc->sc_burst_mask) |
    312  1.1  jmcneill 	    __SHIFTIN(DMA_CFG_ADDR_MODE_IO, DMA_CFG_SRC_ADDR_MODE) |
    313  1.1  jmcneill 	    __SHIFTIN(ch->ch_portid, DMA_CFG_SRC_DRQ_TYPE);
    314  1.1  jmcneill 
    315  1.7  jakllsch 	for (size_t j = 0; j < req->dreq_nsegs; j++) {
    316  1.7  jakllsch 		if (req->dreq_dir == FDT_DMA_READ) {
    317  1.7  jakllsch 			src = req->dreq_dev_phys;
    318  1.7  jakllsch 			dst = req->dreq_segs[j].ds_addr;
    319  1.7  jakllsch 			cfg = mem_cfg << 16 | dev_cfg;
    320  1.7  jakllsch 		} else {
    321  1.7  jakllsch 			src = req->dreq_segs[j].ds_addr;
    322  1.7  jakllsch 			dst = req->dreq_dev_phys;
    323  1.7  jakllsch 			cfg = dev_cfg << 16 | mem_cfg;
    324  1.7  jakllsch 		}
    325  1.7  jakllsch 		len = req->dreq_segs[j].ds_len;
    326  1.7  jakllsch 
    327  1.7  jakllsch 		desc[j].dma_config = htole32(cfg);
    328  1.7  jakllsch 		desc[j].dma_srcaddr = htole32(src);
    329  1.7  jakllsch 		desc[j].dma_dstaddr = htole32(dst);
    330  1.7  jakllsch 		desc[j].dma_bcnt = htole32(len);
    331  1.7  jakllsch 		desc[j].dma_para = htole32(0);
    332  1.7  jakllsch 		if (j < req->dreq_nsegs - 1)
    333  1.7  jakllsch 			desc[j].dma_next = htole32(DESC_ADDR(sc, ch, j + 1));
    334  1.7  jakllsch 		else
    335  1.7  jakllsch 			desc[j].dma_next = htole32(DMA_NULL);
    336  1.7  jakllsch 	}
    337  1.1  jmcneill 
    338  1.9  jakllsch #if notyet && maybenever
    339  1.8  jakllsch 	DMA_WRITE(sc, DMA_MODE_REG(ch->ch_index),
    340  1.8  jakllsch 	    DMA_MODE_DST(MODE_HANDSHAKE)|DMA_MODE_SRC(MODE_HANDSHAKE));
    341  1.8  jakllsch #endif
    342  1.8  jakllsch 
    343  1.7  jakllsch 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, DESC_OFFS(ch->ch_index, 0),
    344  1.7  jakllsch 	    DESC_LEN(req->dreq_nsegs), BUS_DMASYNC_PREWRITE);
    345  1.1  jmcneill 
    346  1.1  jmcneill 	DMA_WRITE(sc, DMA_START_ADDR_REG(ch->ch_index),
    347  1.7  jakllsch 	    DESC_ADDR(sc, ch, 0));
    348  1.1  jmcneill 	DMA_WRITE(sc, DMA_EN_REG(ch->ch_index), DMA_EN_EN);
    349  1.1  jmcneill 
    350  1.2  jmcneill 	if ((DMA_READ(sc, DMA_EN_REG(ch->ch_index)) & DMA_EN_EN) == 0) {
    351  1.2  jmcneill 		aprint_error_dev(sc->sc_dev,
    352  1.2  jmcneill 		    "DMA Channel %u failed to start\n", ch->ch_index);
    353  1.2  jmcneill 		return EIO;
    354  1.2  jmcneill 	}
    355  1.2  jmcneill 
    356  1.1  jmcneill 	return 0;
    357  1.1  jmcneill }
    358  1.1  jmcneill 
    359  1.1  jmcneill static void
    360  1.1  jmcneill sun6idma_halt(device_t dev, void *priv)
    361  1.1  jmcneill {
    362  1.1  jmcneill 	struct sun6idma_softc *sc = device_private(dev);
    363  1.1  jmcneill 	struct sun6idma_channel *ch = priv;
    364  1.1  jmcneill 
    365  1.1  jmcneill 	DMA_WRITE(sc, DMA_EN_REG(ch->ch_index), 0);
    366  1.1  jmcneill }
    367  1.1  jmcneill 
    368  1.1  jmcneill static const struct fdtbus_dma_controller_func sun6idma_funcs = {
    369  1.1  jmcneill 	.acquire = sun6idma_acquire,
    370  1.1  jmcneill 	.release = sun6idma_release,
    371  1.1  jmcneill 	.transfer = sun6idma_transfer,
    372  1.1  jmcneill 	.halt = sun6idma_halt
    373  1.1  jmcneill };
    374  1.1  jmcneill 
    375  1.1  jmcneill static int
    376  1.1  jmcneill sun6idma_intr(void *priv)
    377  1.1  jmcneill {
    378  1.1  jmcneill 	struct sun6idma_softc *sc = priv;
    379  1.1  jmcneill 	uint32_t pend0, pend1, bit;
    380  1.1  jmcneill 	uint64_t pend, mask;
    381  1.1  jmcneill 	uint8_t index;
    382  1.1  jmcneill 
    383  1.1  jmcneill 	pend0 = DMA_READ(sc, DMA_IRQ_PEND_REG0_REG);
    384  1.1  jmcneill 	pend1 = DMA_READ(sc, DMA_IRQ_PEND_REG1_REG);
    385  1.1  jmcneill 	if (!pend0 && !pend1)
    386  1.1  jmcneill 		return 0;
    387  1.1  jmcneill 
    388  1.1  jmcneill 	DMA_WRITE(sc, DMA_IRQ_PEND_REG0_REG, pend0);
    389  1.1  jmcneill 	DMA_WRITE(sc, DMA_IRQ_PEND_REG1_REG, pend1);
    390  1.1  jmcneill 
    391  1.1  jmcneill 	pend = pend0 | ((uint64_t)pend1 << 32);
    392  1.1  jmcneill 
    393  1.1  jmcneill 	while ((bit = ffs64(pend & DMA_IRQ_PKG_MASK)) != 0) {
    394  1.1  jmcneill 		mask = __BIT(bit - 1);
    395  1.1  jmcneill 		pend &= ~mask;
    396  1.1  jmcneill 		index = (bit - 1) / 4;
    397  1.1  jmcneill 
    398  1.1  jmcneill 		if (sc->sc_chan[index].ch_callback == NULL)
    399  1.1  jmcneill 			continue;
    400  1.1  jmcneill 		sc->sc_chan[index].ch_callback(
    401  1.1  jmcneill 		    sc->sc_chan[index].ch_callbackarg);
    402  1.1  jmcneill 	}
    403  1.1  jmcneill 
    404  1.1  jmcneill 	return 1;
    405  1.1  jmcneill }
    406  1.1  jmcneill 
    407  1.1  jmcneill static int
    408  1.1  jmcneill sun6idma_match(device_t parent, cfdata_t cf, void *aux)
    409  1.1  jmcneill {
    410  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    411  1.1  jmcneill 
    412  1.1  jmcneill 	return of_match_compat_data(faa->faa_phandle, compat_data);
    413  1.1  jmcneill }
    414  1.1  jmcneill 
    415  1.1  jmcneill static void
    416  1.1  jmcneill sun6idma_attach(device_t parent, device_t self, void *aux)
    417  1.1  jmcneill {
    418  1.1  jmcneill 	struct sun6idma_softc * const sc = device_private(self);
    419  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    420  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    421  1.7  jakllsch 	size_t desclen;
    422  1.4  jmcneill 	const struct sun6idma_config *conf;
    423  1.1  jmcneill 	struct fdtbus_reset *rst;
    424  1.1  jmcneill 	struct clk *clk;
    425  1.1  jmcneill 	char intrstr[128];
    426  1.1  jmcneill 	bus_addr_t addr;
    427  1.1  jmcneill 	bus_size_t size;
    428  1.1  jmcneill 	int error, nsegs;
    429  1.1  jmcneill 	u_int index;
    430  1.1  jmcneill 
    431  1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    432  1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    433  1.1  jmcneill 		return;
    434  1.1  jmcneill 	}
    435  1.1  jmcneill 
    436  1.1  jmcneill 	if ((clk = fdtbus_clock_get_index(phandle, 0)) == NULL ||
    437  1.1  jmcneill 	    clk_enable(clk) != 0) {
    438  1.1  jmcneill 		aprint_error(": couldn't enable clock\n");
    439  1.1  jmcneill 		return;
    440  1.1  jmcneill 	}
    441  1.1  jmcneill 	if ((rst = fdtbus_reset_get_index(phandle, 0)) == NULL ||
    442  1.1  jmcneill 	    fdtbus_reset_deassert(rst) != 0) {
    443  1.1  jmcneill 		aprint_error(": couldn't de-assert reset\n");
    444  1.1  jmcneill 		return;
    445  1.1  jmcneill 	}
    446  1.1  jmcneill 
    447  1.1  jmcneill 	sc->sc_dev = self;
    448  1.1  jmcneill 	sc->sc_phandle = phandle;
    449  1.1  jmcneill 	sc->sc_dmat = faa->faa_dmat;
    450  1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    451  1.1  jmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    452  1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    453  1.1  jmcneill 		return;
    454  1.1  jmcneill 	}
    455  1.1  jmcneill 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SCHED);
    456  1.1  jmcneill 
    457  1.1  jmcneill 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    458  1.1  jmcneill 		aprint_error(": failed to decode interrupt\n");
    459  1.1  jmcneill 		return;
    460  1.1  jmcneill 	}
    461  1.1  jmcneill 
    462  1.5  jmcneill 	conf = (void *)of_search_compatible(phandle, compat_data)->data;
    463  1.4  jmcneill 
    464  1.6  jmcneill 	sc->sc_burst_mask = conf->burst_mask;
    465  1.4  jmcneill 	sc->sc_nchan = conf->num_channels;
    466  1.8  jakllsch 	sc->sc_widths = conf->widths;
    467  1.8  jakllsch 	sc->sc_bursts = conf->bursts;
    468  1.1  jmcneill 	sc->sc_chan = kmem_alloc(sizeof(*sc->sc_chan) * sc->sc_nchan, KM_SLEEP);
    469  1.7  jakllsch 	desclen = DESC_OFFS(sc->sc_nchan, 0);
    470  1.7  jakllsch 	sc->sc_ndesc_ch = DESC_OFFS(1, 0) / sizeof(struct sun6idma_desc);
    471  1.1  jmcneill 
    472  1.1  jmcneill 	aprint_naive("\n");
    473  1.1  jmcneill 	aprint_normal(": DMA controller (%u channels)\n", sc->sc_nchan);
    474  1.1  jmcneill 
    475  1.1  jmcneill 	DMA_WRITE(sc, DMA_IRQ_EN_REG0_REG, 0);
    476  1.1  jmcneill 	DMA_WRITE(sc, DMA_IRQ_EN_REG1_REG, 0);
    477  1.1  jmcneill 	DMA_WRITE(sc, DMA_IRQ_PEND_REG0_REG, ~0);
    478  1.1  jmcneill 	DMA_WRITE(sc, DMA_IRQ_PEND_REG1_REG, ~0);
    479  1.1  jmcneill 
    480  1.7  jakllsch 	error = bus_dmamem_alloc(sc->sc_dmat, desclen, 0, 0,
    481  1.7  jakllsch 	    sc->sc_dmasegs, 1, &nsegs, BUS_DMA_WAITOK);
    482  1.7  jakllsch 	if (error)
    483  1.7  jakllsch 		panic("bus_dmamem_alloc failed: %d", error);
    484  1.7  jakllsch 	error = bus_dmamem_map(sc->sc_dmat, sc->sc_dmasegs, nsegs,
    485  1.7  jakllsch 	    desclen, (void **)&sc->sc_dmadescs, BUS_DMA_WAITOK);
    486  1.7  jakllsch 	if (error)
    487  1.7  jakllsch 		panic("bus_dmamem_map failed: %d", error);
    488  1.7  jakllsch 	error = bus_dmamap_create(sc->sc_dmat, desclen, 1, desclen, 0,
    489  1.7  jakllsch 	    BUS_DMA_WAITOK, &sc->sc_dmamap);
    490  1.7  jakllsch 	if (error)
    491  1.7  jakllsch 		panic("bus_dmamap_create failed: %d", error);
    492  1.7  jakllsch 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap,
    493  1.7  jakllsch 	    sc->sc_dmadescs, desclen, NULL, BUS_DMA_WAITOK);
    494  1.7  jakllsch 	if (error)
    495  1.7  jakllsch 		panic("bus_dmamap_load failed: %d", error);
    496  1.7  jakllsch 
    497  1.1  jmcneill 	for (index = 0; index < sc->sc_nchan; index++) {
    498  1.1  jmcneill 		struct sun6idma_channel *ch = &sc->sc_chan[index];
    499  1.1  jmcneill 		ch->ch_index = index;
    500  1.7  jakllsch 		ch->ch_dmadesc = (void *)((uintptr_t)sc->sc_dmadescs + DESC_OFFS(index, 0));
    501  1.1  jmcneill 		ch->ch_callback = NULL;
    502  1.1  jmcneill 		ch->ch_callbackarg = NULL;
    503  1.1  jmcneill 
    504  1.1  jmcneill 		DMA_WRITE(sc, DMA_EN_REG(index), 0);
    505  1.1  jmcneill 	}
    506  1.1  jmcneill 
    507  1.4  jmcneill 	if (conf->autogate)
    508  1.4  jmcneill 		DMA_WRITE(sc, conf->autogate_reg, conf->autogate_mask);
    509  1.4  jmcneill 
    510  1.1  jmcneill 	sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_SCHED, FDT_INTR_MPSAFE,
    511  1.1  jmcneill 	    sun6idma_intr, sc);
    512  1.1  jmcneill 	if (sc->sc_ih == NULL) {
    513  1.1  jmcneill 		aprint_error_dev(sc->sc_dev,
    514  1.1  jmcneill 		    "couldn't establish interrupt on %s\n", intrstr);
    515  1.1  jmcneill 		return;
    516  1.1  jmcneill 	}
    517  1.1  jmcneill 	aprint_normal_dev(sc->sc_dev, "interrupting on %s\n", intrstr);
    518  1.1  jmcneill 
    519  1.1  jmcneill 	fdtbus_register_dma_controller(self, phandle, &sun6idma_funcs);
    520  1.1  jmcneill }
    521  1.1  jmcneill 
    522  1.1  jmcneill CFATTACH_DECL_NEW(sun6i_dma, sizeof(struct sun6idma_softc),
    523  1.1  jmcneill         sun6idma_match, sun6idma_attach, NULL, NULL);
    524  1.2  jmcneill 
    525  1.2  jmcneill #ifdef DDB
    526  1.2  jmcneill void sun6idma_dump(void);
    527  1.2  jmcneill 
    528  1.2  jmcneill void
    529  1.2  jmcneill sun6idma_dump(void)
    530  1.2  jmcneill {
    531  1.2  jmcneill 	struct sun6idma_softc *sc;
    532  1.2  jmcneill 	device_t dev;
    533  1.2  jmcneill 	u_int index;
    534  1.2  jmcneill 
    535  1.2  jmcneill 	dev = device_find_by_driver_unit("sun6idma", 0);
    536  1.2  jmcneill 	if (dev == NULL)
    537  1.2  jmcneill 		return;
    538  1.2  jmcneill 	sc = device_private(dev);
    539  1.2  jmcneill 
    540  1.2  jmcneill 	device_printf(dev, "DMA_IRQ_EN_REG0_REG:   %08x\n", DMA_READ(sc, DMA_IRQ_EN_REG0_REG));
    541  1.2  jmcneill 	device_printf(dev, "DMA_IRQ_EN_REG1_REG:   %08x\n", DMA_READ(sc, DMA_IRQ_EN_REG1_REG));
    542  1.2  jmcneill 	device_printf(dev, "DMA_IRQ_PEND_REG0_REG: %08x\n", DMA_READ(sc, DMA_IRQ_PEND_REG0_REG));
    543  1.2  jmcneill 	device_printf(dev, "DMA_IRQ_PEND_REG1_REG: %08x\n", DMA_READ(sc, DMA_IRQ_PEND_REG1_REG));
    544  1.2  jmcneill 	device_printf(dev, "DMA_STA_REG:           %08x\n", DMA_READ(sc, DMA_STA_REG));
    545  1.2  jmcneill 
    546  1.2  jmcneill 	for (index = 0; index < sc->sc_nchan; index++) {
    547  1.2  jmcneill 		struct sun6idma_channel *ch = &sc->sc_chan[index];
    548  1.2  jmcneill 		if (ch->ch_callback == NULL)
    549  1.2  jmcneill 			continue;
    550  1.2  jmcneill 		device_printf(dev, " %2d: DMA_EN_REG:         %08x\n", index, DMA_READ(sc, DMA_EN_REG(index)));
    551  1.2  jmcneill 		device_printf(dev, " %2d: DMA_PAU_REG:        %08x\n", index, DMA_READ(sc, DMA_PAU_REG(index)));
    552  1.2  jmcneill 		device_printf(dev, " %2d: DMA_START_ADDR_REG: %08x\n", index, DMA_READ(sc, DMA_START_ADDR_REG(index)));
    553  1.2  jmcneill 		device_printf(dev, " %2d: DMA_CFG_REG:        %08x\n", index, DMA_READ(sc, DMA_CFG_REG(index)));
    554  1.2  jmcneill 		device_printf(dev, " %2d: DMA_CUR_SRC_REG:    %08x\n", index, DMA_READ(sc, DMA_CUR_SRC_REG(index)));
    555  1.2  jmcneill 		device_printf(dev, " %2d: DMA_CUR_DEST_REG:   %08x\n", index, DMA_READ(sc, DMA_CUR_DEST_REG(index)));
    556  1.2  jmcneill 		device_printf(dev, " %2d: DMA_BCNT_LEFT_REG:  %08x\n", index, DMA_READ(sc, DMA_BCNT_LEFT_REG(index)));
    557  1.2  jmcneill 		device_printf(dev, " %2d: DMA_PARA_REG:       %08x\n", index, DMA_READ(sc, DMA_PARA_REG(index)));
    558  1.8  jakllsch 		device_printf(dev, " %2d: DMA_MODE_REG:       %08x\n", index, DMA_READ(sc, DMA_MODE_REG(index)));
    559  1.8  jakllsch 		device_printf(dev, " %2d: DMA_FDESC_ADDR_REG: %08x\n", index, DMA_READ(sc, DMA_FDESC_ADDR_REG(index)));
    560  1.8  jakllsch 		device_printf(dev, " %2d: DMA_PKG_NUM_REG:    %08x\n", index, DMA_READ(sc, DMA_PKG_NUM_REG(index)));
    561  1.2  jmcneill 	}
    562  1.2  jmcneill }
    563  1.2  jmcneill #endif
    564