Module Name:    src
Committed By:   jmcneill
Date:           Sat Sep  6 00:15:34 UTC 2014

Modified Files:
        src/sys/arch/arm/allwinner: awin_io.c awin_reg.h awin_var.h files.awin
Added Files:
        src/sys/arch/arm/allwinner: awin_dma.c

Log Message:
add driver for DMA controller


To generate a diff of this commit:
cvs rdiff -u -r0 -r1.1 src/sys/arch/arm/allwinner/awin_dma.c
cvs rdiff -u -r1.9 -r1.10 src/sys/arch/arm/allwinner/awin_io.c \
    src/sys/arch/arm/allwinner/files.awin
cvs rdiff -u -r1.17 -r1.18 src/sys/arch/arm/allwinner/awin_reg.h
cvs rdiff -u -r1.11 -r1.12 src/sys/arch/arm/allwinner/awin_var.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/arm/allwinner/awin_io.c
diff -u src/sys/arch/arm/allwinner/awin_io.c:1.9 src/sys/arch/arm/allwinner/awin_io.c:1.10
--- src/sys/arch/arm/allwinner/awin_io.c:1.9	Thu Sep  4 02:38:18 2014
+++ src/sys/arch/arm/allwinner/awin_io.c	Sat Sep  6 00:15:34 2014
@@ -31,7 +31,7 @@
 
 #include <sys/cdefs.h>
 
-__KERNEL_RCSID(1, "$NetBSD: awin_io.c,v 1.9 2014/09/04 02:38:18 jmcneill Exp $");
+__KERNEL_RCSID(1, "$NetBSD: awin_io.c,v 1.10 2014/09/06 00:15:34 jmcneill Exp $");
 
 #include <sys/param.h>
 #include <sys/bus.h>
@@ -93,6 +93,7 @@ awinio_print(void *aux, const char *pnp)
 static const struct awin_locators awin_locators[] = {
 	{ "awinicu", OFFANDSIZE(INTC), NOPORT, NOINTR, A10|REQ },
 	{ "awingpio", OFFANDSIZE(PIO), NOPORT, NOINTR, AANY|REQ },
+	{ "awindma", OFFANDSIZE(DMA), NOPORT, AWIN_IRQ_DMA, AANY|REQ },
 	{ "awintmr", OFFANDSIZE(TMR), NOPORT, AWIN_IRQ_TMR0, A10 },
 	{ "com", OFFANDSIZE(UART0), 0, AWIN_IRQ_UART0, AANY },
 	{ "com", OFFANDSIZE(UART1), 1, AWIN_IRQ_UART1, AANY },
Index: src/sys/arch/arm/allwinner/files.awin
diff -u src/sys/arch/arm/allwinner/files.awin:1.9 src/sys/arch/arm/allwinner/files.awin:1.10
--- src/sys/arch/arm/allwinner/files.awin:1.9	Thu Sep  4 02:38:18 2014
+++ src/sys/arch/arm/allwinner/files.awin	Sat Sep  6 00:15:34 2014
@@ -1,4 +1,4 @@
-#	$NetBSD: files.awin,v 1.9 2014/09/04 02:38:18 jmcneill Exp $
+#	$NetBSD: files.awin,v 1.10 2014/09/06 00:15:34 jmcneill Exp $
 #
 # Configuration info for Allwinner ARM Peripherals
 #
@@ -57,6 +57,11 @@ device	awingpio : gpiobus
 attach	awingpio at awinio with awin_gpio
 file	arch/arm/allwinner/awin_gpio.c		awin_gpio | awin_io  needs-flag
 
+# A10/A20 DMA
+device	awindma
+attach	awindma at awinio with awin_dma
+file	arch/arm/allwinner/awin_dma.c		awin_dma
+
 # A10/A20 TWI (IIC)
 device	awiniic : i2cbus, i2cexec, mvi2c
 attach	awiniic at awinio with awin_twi

Index: src/sys/arch/arm/allwinner/awin_reg.h
diff -u src/sys/arch/arm/allwinner/awin_reg.h:1.17 src/sys/arch/arm/allwinner/awin_reg.h:1.18
--- src/sys/arch/arm/allwinner/awin_reg.h:1.17	Thu Sep  4 02:35:26 2014
+++ src/sys/arch/arm/allwinner/awin_reg.h	Sat Sep  6 00:15:34 2014
@@ -248,8 +248,12 @@
 #define AWIN_DDMA_BC_REG		0x000c
 #define AWIN_DDMA_PARA_REG		0x0018
 
+#define AWIN_DMA_IRQ_END_MASK		0xaaaaaaaa
+#define AWIN_DMA_IRQ_HF_MASK		0x55555555
+#define AWIN_DMA_IRQ_DDMA		__BITS(31,16)
 #define AWIN_DMA_IRQ_DDMA_END(n)	__BIT(17+2*(n))
 #define AWIN_DMA_IRQ_DDMA_HF(n)		__BIT(16+2*(n))
+#define AWIN_DMA_IRQ_NDMA		__BITS(15,0)
 #define AWIN_DMA_IRQ_NDMA_END(n)	__BIT(1+2*(n))
 #define AWIN_DMA_IRQ_NDMA_HF(n)		__BIT(0+2*(n))
 
@@ -730,11 +734,9 @@
 #define AWIN_PLL1_SIG_DELT_PAT_IN	__BIT(3)
 #define AWIN_PLL1_SIG_DELT_PAT_EN	__BIT(2)
 
-#define AWIN_PLL2_CFG_PREVDIV		__BITS(4,0)
-#define AWIN_PLL2_CFG_FACTOR_N		__BITS(14,8)
-#define AWIN_PLL2_CFG_PLLBIAS		__BITS(20,16)
-#define AWIN_PLL2_CFG_VCOBIAS		__BITS(25,21)
 #define AWIN_PLL2_CFG_POSTDIV		__BITS(29,26)
+#define AWIN_PLL2_CFG_FACTOR_N		__BITS(14,8)
+#define AWIN_PLL2_CFG_PREVDIV		__BITS(4,0)
 
 #define AWIN_PLL5_CFG_DDR_CLK_EN	__BIT(29)
 #define AWIN_PLL5_CFG_LDO_EN		__BIT(7)

Index: src/sys/arch/arm/allwinner/awin_var.h
diff -u src/sys/arch/arm/allwinner/awin_var.h:1.11 src/sys/arch/arm/allwinner/awin_var.h:1.12
--- src/sys/arch/arm/allwinner/awin_var.h:1.11	Thu Sep  4 02:36:08 2014
+++ src/sys/arch/arm/allwinner/awin_var.h	Sat Sep  6 00:15:34 2014
@@ -1,4 +1,4 @@
-/* $NetBSD: awin_var.h,v 1.11 2014/09/04 02:36:08 jmcneill Exp $ */
+/* $NetBSD: awin_var.h,v 1.12 2014/09/06 00:15:34 jmcneill Exp $ */
 /*-
  * Copyright (c) 2013 The NetBSD Foundation, Inc.
  * All rights reserved.
@@ -72,6 +72,13 @@ struct awin_gpio_pindata {
 	int pd_pin;
 };
 
+enum awin_dma_type {
+	AWIN_DMA_TYPE_NDMA,
+	AWIN_DMA_TYPE_DDMA,
+};
+
+struct awin_dma_channel;
+
 extern struct bus_space awin_bs_tag;
 extern struct bus_space awin_a4x_bs_tag;
 extern bus_space_handle_t awin_core_bsh;
@@ -91,6 +98,13 @@ void	awin_gpio_pinset_acquire(const stru
 void	awin_gpio_pinset_release(const struct awin_gpio_pinset *);
 bool	awin_gpio_pin_reserve(const char *, struct awin_gpio_pindata *);
 
+struct awin_dma_channel *awin_dma_alloc(enum awin_dma_type,
+					      void (*)(void *), void *);
+void	awin_dma_free(struct awin_dma_channel *);
+uint32_t awin_dma_get_config(struct awin_dma_channel *);
+void	awin_dma_set_config(struct awin_dma_channel *, uint32_t);
+int	awin_dma_transfer(struct awin_dma_channel *, paddr_t, paddr_t, size_t);
+
 void	awin_wdog_reset(void);
 void	awin_tmr_cpu_init(struct cpu_info *);
 

Added files:

Index: src/sys/arch/arm/allwinner/awin_dma.c
diff -u /dev/null src/sys/arch/arm/allwinner/awin_dma.c:1.1
--- /dev/null	Sat Sep  6 00:15:34 2014
+++ src/sys/arch/arm/allwinner/awin_dma.c	Sat Sep  6 00:15:34 2014
@@ -0,0 +1,276 @@
+/* $NetBSD: awin_dma.c,v 1.1 2014/09/06 00:15:34 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2014 Jared D. McNeill <[email protected]>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: awin_dma.c,v 1.1 2014/09/06 00:15:34 jmcneill Exp $");
+
+#include <sys/param.h>
+#include <sys/bus.h>
+#include <sys/device.h>
+#include <sys/intr.h>
+#include <sys/systm.h>
+#include <sys/mutex.h>
+
+#include <arm/allwinner/awin_reg.h>
+#include <arm/allwinner/awin_var.h>
+
+#define NDMA_CHANNELS	8
+#define DDMA_CHANNELS	8
+
+struct awin_dma_channel {
+	uint8_t ch_index;
+	enum awin_dma_type ch_type;
+	void (*ch_callback)(void *);
+	void *ch_callbackarg;
+	uint32_t ch_regoff;
+};
+
+struct awin_dma_softc {
+	device_t sc_dev;
+	bus_space_tag_t sc_bst;
+	bus_space_handle_t sc_bsh;
+	void *sc_ih;
+};
+
+#define DMA_READ(reg)			\
+    bus_space_read_4(awin_dma_sc->sc_bst, awin_dma_sc->sc_bsh, (reg))
+#define DMA_WRITE(reg, val)		\
+    bus_space_write_4(awin_dma_sc->sc_bst, awin_dma_sc->sc_bsh, (reg), (val))
+#define DMACH_READ(ch, reg)		\
+    DMA_READ((reg) + (ch)->ch_regoff)
+#define DMACH_WRITE(ch, reg, val)	\
+    DMA_WRITE((reg) + (ch)->ch_regoff, (val))
+
+static struct awin_dma_softc *awin_dma_sc;
+static kmutex_t awin_dma_lock;
+static struct awin_dma_channel awin_ndma_channels[NDMA_CHANNELS];
+static struct awin_dma_channel awin_ddma_channels[DDMA_CHANNELS];
+
+static int	awin_dma_match(device_t, cfdata_t, void *);
+static void	awin_dma_attach(device_t, device_t, void *);
+
+static int	awin_dma_intr(void *);
+
+CFATTACH_DECL_NEW(awin_dma, sizeof(struct awin_dma_softc),
+	awin_dma_match, awin_dma_attach, NULL, NULL);
+
+static int
+awin_dma_match(device_t parent, cfdata_t cf, void *aux)
+{
+	return awin_dma_sc == NULL;
+}
+
+static void
+awin_dma_attach(device_t parent, device_t self, void *aux)
+{
+	struct awin_dma_softc *sc = device_private(self);
+	struct awinio_attach_args * const aio = aux;
+	const struct awin_locators * const loc = &aio->aio_loc;
+	uint8_t index;
+
+	KASSERT(awin_dma_sc == NULL);
+	awin_dma_sc = sc;
+
+	sc->sc_dev = self;
+	sc->sc_bst = aio->aio_core_bst;
+	bus_space_subregion(sc->sc_bst, aio->aio_core_bsh,
+	    loc->loc_offset, loc->loc_size, &sc->sc_bsh);
+
+	aprint_naive("\n");
+	aprint_normal(": DMA\n");
+
+	mutex_init(&awin_dma_lock, MUTEX_DEFAULT, IPL_SCHED);
+
+	awin_reg_set_clear(aio->aio_core_bst, aio->aio_ccm_bsh,
+	    AWIN_AHB_GATING0_REG, AWIN_AHB_GATING0_DMA, 0);
+
+	DMA_WRITE(AWIN_DMA_IRQ_EN_REG, 0);
+	DMA_WRITE(AWIN_DMA_IRQ_PEND_STA_REG, ~0);
+
+	for (index = 0; index < NDMA_CHANNELS; index++) {
+		awin_ndma_channels[index].ch_index = index;
+		awin_ndma_channels[index].ch_type = AWIN_DMA_TYPE_NDMA;
+		awin_ndma_channels[index].ch_callback = NULL;
+		awin_ndma_channels[index].ch_callbackarg = NULL;
+		awin_ndma_channels[index].ch_regoff = AWIN_NDMA_REG(index);
+		DMACH_WRITE(&awin_ndma_channels[index], AWIN_NDMA_CTL_REG, 0);
+	}
+	for (index = 0; index < DDMA_CHANNELS; index++) {
+		awin_ddma_channels[index].ch_index = index;
+		awin_ddma_channels[index].ch_type = AWIN_DMA_TYPE_DDMA;
+		awin_ddma_channels[index].ch_callback = NULL;
+		awin_ddma_channels[index].ch_callbackarg = NULL;
+		awin_ddma_channels[index].ch_regoff = AWIN_DDMA_REG(index);
+		DMACH_WRITE(&awin_ddma_channels[index], AWIN_DDMA_CTL_REG, 0);
+	}
+
+	sc->sc_ih = intr_establish(loc->loc_intr, IPL_SCHED, IST_LEVEL,
+	    awin_dma_intr, sc);
+	if (sc->sc_ih == NULL) {
+		aprint_error_dev(self, "couldn't establish interrupt %d\n",
+		    loc->loc_intr);
+		return;
+	}
+}
+
+static int
+awin_dma_intr(void *priv)
+{
+	uint32_t sta, bit, mask;
+	uint8_t index;
+
+	sta = DMA_READ(AWIN_DMA_IRQ_PEND_STA_REG);
+	if (!sta)
+		return 0;
+
+	DMA_WRITE(AWIN_DMA_IRQ_PEND_STA_REG, sta);
+
+	while ((bit = ffs(sta & AWIN_DMA_IRQ_END_MASK)) != 0) {
+		mask = __BIT(bit - 1);
+		sta &= ~mask;
+		index = (bit / 2) & 7;
+		if (mask & AWIN_DMA_IRQ_NDMA) {
+			if (awin_ndma_channels[index].ch_callback == NULL)
+				continue;
+			awin_ndma_channels[index].ch_callback(
+			    awin_ndma_channels[index].ch_callbackarg);
+		} else {
+			if (awin_ddma_channels[index].ch_callback == NULL)
+				continue;
+			awin_ddma_channels[index].ch_callback(
+			    awin_ddma_channels[index].ch_callbackarg);
+		}
+	}
+
+	return 1;
+}
+
+struct awin_dma_channel *
+awin_dma_alloc(enum awin_dma_type type, void (*cb)(void *), void *cbarg)
+{
+	struct awin_dma_channel *ch_list;
+	struct awin_dma_channel *ch = NULL;
+	uint32_t irqen;
+	uint8_t ch_count, index;
+
+	if (type == AWIN_DMA_TYPE_NDMA) {
+		ch_list = awin_ndma_channels;
+		ch_count = NDMA_CHANNELS;
+	} else {
+		ch_list = awin_ndma_channels;
+		ch_count = DDMA_CHANNELS;
+	}
+
+	mutex_enter(&awin_dma_lock);
+	for (index = 0; index < ch_count; index++) {
+		if (ch_list[index].ch_callback == NULL) {
+			ch = &ch_list[index];
+			ch->ch_callback = cb;
+			ch->ch_callbackarg = cbarg;
+
+			irqen = DMA_READ(AWIN_DMA_IRQ_EN_REG);
+			if (type == AWIN_DMA_TYPE_NDMA)
+				irqen |= AWIN_DMA_IRQ_NDMA_END(index);
+			else
+				irqen |= AWIN_DMA_IRQ_DDMA_END(index);
+			DMA_WRITE(AWIN_DMA_IRQ_EN_REG, irqen);
+
+			break;
+		}
+	}
+	mutex_exit(&awin_dma_lock);
+
+	return ch;
+}
+
+void
+awin_dma_free(struct awin_dma_channel *ch)
+{
+	uint32_t irqen, cfg;
+
+	irqen = DMA_READ(AWIN_DMA_IRQ_EN_REG);
+	cfg = awin_dma_get_config(ch);
+	if (ch->ch_type == AWIN_DMA_TYPE_NDMA) {
+		irqen &= ~AWIN_DMA_IRQ_NDMA_END(ch->ch_index);
+		cfg &= ~AWIN_NDMA_CTL_DMA_LOADING;
+	} else {
+		irqen &= ~AWIN_DMA_IRQ_DDMA_END(ch->ch_index);
+		cfg &= ~AWIN_DDMA_CTL_DMA_LOADING;
+	}
+	awin_dma_set_config(ch, cfg);
+	DMA_WRITE(AWIN_DMA_IRQ_EN_REG, irqen);
+
+	mutex_enter(&awin_dma_lock);
+	ch->ch_callback = NULL;
+	ch->ch_callbackarg = NULL;
+	mutex_exit(&awin_dma_lock);
+}
+
+uint32_t
+awin_dma_get_config(struct awin_dma_channel *ch)
+{
+	return DMACH_READ(ch, AWIN_NDMA_CTL_REG);
+}
+
+void
+awin_dma_set_config(struct awin_dma_channel *ch, uint32_t val)
+{
+	DMACH_WRITE(ch, AWIN_NDMA_CTL_REG, val);
+}
+
+int
+awin_dma_transfer(struct awin_dma_channel *ch, paddr_t src, paddr_t dst,
+    size_t nbytes)
+{
+	uint32_t cfg;
+
+	cfg = awin_dma_get_config(ch);
+	if (ch->ch_type == AWIN_DMA_TYPE_NDMA) {
+		if (cfg & AWIN_NDMA_CTL_DMA_LOADING)
+			return EBUSY;
+
+		DMACH_WRITE(ch, AWIN_NDMA_SRC_ADDR_REG, src);
+		DMACH_WRITE(ch, AWIN_NDMA_DEST_ADDR_REG, dst);
+		DMACH_WRITE(ch, AWIN_NDMA_BC_REG, nbytes);
+
+		cfg |= AWIN_NDMA_CTL_DMA_LOADING;
+		awin_dma_set_config(ch, cfg);
+	} else {
+		if (cfg & AWIN_DDMA_CTL_DMA_LOADING)
+			return EBUSY;
+
+		DMACH_WRITE(ch, AWIN_DDMA_SRC_START_ADDR_REG, src);
+		DMACH_WRITE(ch, AWIN_DDMA_DEST_START_ADDR_REG, dst);
+		DMACH_WRITE(ch, AWIN_DDMA_BC_REG, nbytes);
+
+		cfg |= AWIN_DDMA_CTL_DMA_LOADING;
+		awin_dma_set_config(ch, cfg);
+	}
+
+	return 0;
+}

Reply via email to