Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sys/arch fdt-ise the allwinner can and lradc drivers.
details: https://anonhg.NetBSD.org/src/rev/b0118815bfcd
branches: trunk
changeset: 321250:b0118815bfcd
user: bouyer <bouyer%NetBSD.org@localhost>
date: Wed Mar 07 20:55:31 2018 +0000
description:
fdt-ise the allwinner can and lradc drivers.
diffstat:
sys/arch/arm/sunxi/files.sunxi | 11 +-
sys/arch/arm/sunxi/sunxi_can.c | 644 +++++++++++++++++++++++++++++++++++++++
sys/arch/arm/sunxi/sunxi_can.h | 146 ++++++++
sys/arch/arm/sunxi/sunxi_lradc.c | 388 +++++++++++++++++++++++
sys/arch/arm/sunxi/sunxi_lradc.h | 68 ++++
sys/arch/evbarm/conf/SUNXI | 10 +-
6 files changed, 1265 insertions(+), 2 deletions(-)
diffs (truncated from 1319 to 300 lines):
diff -r 8027a030b3fc -r b0118815bfcd sys/arch/arm/sunxi/files.sunxi
--- a/sys/arch/arm/sunxi/files.sunxi Wed Mar 07 20:48:00 2018 +0000
+++ b/sys/arch/arm/sunxi/files.sunxi Wed Mar 07 20:55:31 2018 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: files.sunxi,v 1.42 2018/02/18 10:28:37 jmcneill Exp $
+# $NetBSD: files.sunxi,v 1.43 2018/03/07 20:55:31 bouyer Exp $
#
# Configuration info for Allwinner sunxi family SoCs
#
@@ -236,6 +236,15 @@
attach sun6ispi at fdt with sun6i_spi
file arch/arm/sunxi/sun6i_spi.c sun6i_spi
+# A10/A20 CAN
+device sunxican { } : ifnet
+attach sunxican at fdt with sunxi_can
+file arch/arm/sunxi/sunxi_can.c sunxi_can
+
+# LRADC
+device sunxilradc
+attach sunxilradc at fdt with sunxi_lradc
+file arch/arm/sunxi/sunxi_lradc.c sunxi_lradc
# SOC parameters
defflag opt_soc.h SOC_SUNXI
defflag opt_soc.h SOC_SUN4I: SOC_SUNXI
diff -r 8027a030b3fc -r b0118815bfcd sys/arch/arm/sunxi/sunxi_can.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/arch/arm/sunxi/sunxi_can.c Wed Mar 07 20:55:31 2018 +0000
@@ -0,0 +1,644 @@
+/* $NetBSD: sunxi_can.c,v 1.1 2018/03/07 20:55:31 bouyer Exp $ */
+
+/*-
+ * Copyright (c) 2017,2018 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Manuel Bouyer.
+ *
+ * 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``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 FOUNDATION OR CONTRIBUTORS
+ * 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 "locators.h"
+#include "opt_can.h"
+
+
+#include <sys/cdefs.h>
+
+__KERNEL_RCSID(1, "$NetBSD: sunxi_can.c,v 1.1 2018/03/07 20:55:31 bouyer Exp $");
+
+#include <sys/param.h>
+#include <sys/bus.h>
+#include <sys/device.h>
+#include <sys/intr.h>
+#include <sys/ioctl.h>
+#include <sys/mutex.h>
+#include <sys/rndsource.h>
+#include <sys/mbuf.h>
+#include <sys/systm.h>
+
+#include <net/if.h>
+#include <net/if_types.h>
+#include <net/bpf.h>
+
+#ifdef CAN
+#include <netcan/can.h>
+#include <netcan/can_var.h>
+#endif
+
+#include <dev/fdt/fdtvar.h>
+
+#include <arm/sunxi/sunxi_can.h>
+
+/* shortcut for all error interrupts */
+#define SUNXI_CAN_INT_ALLERRS (\
+ SUNXI_CAN_INT_BERR | \
+ SUNXI_CAN_INT_ARB_LOST | \
+ SUNXI_CAN_INT_ERR_PASSIVE | \
+ SUNXI_CAN_INT_DATA_OR | \
+ SUNXI_CAN_INT_ERR \
+ )
+
+struct sunxi_can_softc {
+ struct canif_softc sc_cansc;
+ bus_space_tag_t sc_bst;
+ bus_space_handle_t sc_bsh;
+ kmutex_t sc_intr_lock;
+ void *sc_ih;
+ struct ifnet *sc_ifp;
+ krndsource_t sc_rnd_source; /* random source */
+ struct mbuf *sc_m_transmit; /* mbuf being transmitted */
+};
+#define sc_dev sc_cansc.csc_dev
+#define sc_timecaps sc_cansc.csc_timecaps
+#define sc_timings sc_cansc.csc_timings
+#define sc_linkmodes sc_cansc.csc_linkmodes
+
+static const struct of_compat_data compat_data[] = {
+ {"allwinner,sun4i-a10-can", 0},
+ {NULL}
+};
+
+static int sunxi_can_match(device_t, cfdata_t, void *);
+static void sunxi_can_attach(device_t, device_t, void *);
+
+static int sunxi_can_intr(void *);
+
+static void sunxi_can_ifstart(struct ifnet *);
+static int sunxi_can_ifioctl(struct ifnet *, u_long, void *);
+static void sunxi_can_ifwatchdog(struct ifnet *);
+
+static void sunxi_can_enter_reset(struct sunxi_can_softc *);
+static void sunxi_can_exit_reset(struct sunxi_can_softc *);
+
+CFATTACH_DECL_NEW(sunxi_can, sizeof(struct sunxi_can_softc),
+ sunxi_can_match, sunxi_can_attach, NULL, NULL);
+
+static inline uint32_t
+sunxi_can_read(struct sunxi_can_softc *sc, bus_size_t o)
+{
+ return bus_space_read_4(sc->sc_bst, sc->sc_bsh, o);
+}
+
+static inline void
+sunxi_can_write(struct sunxi_can_softc *sc, bus_size_t o, uint32_t v)
+{
+ return bus_space_write_4(sc->sc_bst, sc->sc_bsh, o, v);
+}
+
+static int
+sunxi_can_match(device_t parent, cfdata_t cf, void *aux)
+{
+ struct fdt_attach_args * const faa = aux;
+
+ return of_match_compat_data(faa->faa_phandle, compat_data);
+}
+
+static void
+sunxi_can_attach(device_t parent, device_t self, void *aux)
+{
+ struct sunxi_can_softc * const sc = device_private(self);
+ struct fdt_attach_args * const faa = aux;
+ struct ifnet *ifp;
+ const int phandle = faa->faa_phandle;
+ bus_addr_t addr;
+ bus_size_t size;
+ char intrstr[128];
+ struct clk *clk;
+ struct fdtbus_reset *rst;
+
+ sc->sc_dev = self;
+ mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_NET);
+
+ sc->sc_bst = faa->faa_bst;
+ if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
+ aprint_error(": couldn't get registers\n");
+ return;
+ }
+
+ if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
+ aprint_error(": couldn't map registers\n");
+ return;
+ }
+
+ if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
+ aprint_error(": failed to decode interrupt\n");
+ return;
+ }
+
+ if ((clk = fdtbus_clock_get_index(phandle, 0)) != NULL) {
+ if (clk_enable(clk) != 0) {
+ aprint_error(": couldn't enable clock\n");
+ return;
+ }
+ }
+
+ if ((rst = fdtbus_reset_get_index(phandle, 0)) != NULL) {
+ if (fdtbus_reset_deassert(rst) != 0) {
+ aprint_error(": couldn't de-assert reset\n");
+ return;
+ }
+ }
+
+ sc->sc_timecaps.cltc_prop_min = 0;
+ sc->sc_timecaps.cltc_prop_max = 0;
+ sc->sc_timecaps.cltc_ps1_min = 1;
+ sc->sc_timecaps.cltc_ps1_max = 16;
+ sc->sc_timecaps.cltc_ps2_min = 1;
+ sc->sc_timecaps.cltc_ps2_max = 8;
+ sc->sc_timecaps.cltc_sjw_max = 4;
+ sc->sc_timecaps.cltc_brp_min = 1;
+ sc->sc_timecaps.cltc_brp_max = 64;
+ sc->sc_timecaps.cltc_brp_inc = 1;
+ sc->sc_timecaps.cltc_clock_freq = clk_get_rate(clk);
+ sc->sc_timecaps.cltc_linkmode_caps =
+ CAN_LINKMODE_3SAMPLES | CAN_LINKMODE_LISTENONLY |
+ CAN_LINKMODE_LOOPBACK;
+ can_ifinit_timings(&sc->sc_cansc);
+ sc->sc_timings.clt_prop = 0;
+ sc->sc_timings.clt_sjw = 1;
+
+ aprint_naive("\n");
+ aprint_normal(": CAN bus controller\n");
+ aprint_debug_dev(self, ": clock freq %d\n",
+ sc->sc_timecaps.cltc_clock_freq);
+
+ sunxi_can_enter_reset(sc);
+ /*
+ * Disable and then clear all interrupts
+ */
+ sunxi_can_write(sc, SUNXI_CAN_INTE_REG, 0);
+ sunxi_can_write(sc, SUNXI_CAN_INT_REG,
+ sunxi_can_read(sc, SUNXI_CAN_INT_REG));
+
+ sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_NET, 0,
+ sunxi_can_intr, sc);
+ if (sc->sc_ih == NULL) {
+ aprint_error_dev(self, "failed to establish interrupt on %s\n",
+ intrstr);
+ return;
+ }
+ aprint_normal_dev(self, "interrupting on %s\n", intrstr);
+
+ ifp = if_alloc(IFT_OTHER);
+ sc->sc_ifp = ifp;
+ strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
+ ifp->if_softc = sc;
+ ifp->if_capabilities = 0;
+ ifp->if_flags = 0;
+ ifp->if_start = sunxi_can_ifstart;
+ ifp->if_ioctl = sunxi_can_ifioctl;
+ ifp->if_watchdog = sunxi_can_ifwatchdog;
+
+ /*
+ * Attach the interface.
+ */
+ can_ifattach(ifp);
+ if_deferred_start_init(ifp, NULL);
+ bpf_mtap_softint_init(ifp);
+ rnd_attach_source(&sc->sc_rnd_source, device_xname(self),
+ RND_TYPE_NET, RND_FLAG_DEFAULT);
+#ifdef MBUFTRACE
+ ifp->if_mowner = malloc(sizeof(struct mowner), M_DEVBUF,
+ M_WAITOK | M_ZERO);
+ strlcpy(ifp->if_mowner->mo_name, ifp->if_xname,
+ sizeof(ifp->if_mowner->mo_name));
+ MOWNER_ATTACH(ifp->if_mowner);
+#endif
+}
+
+static void
+sunxi_can_rx_intr(struct sunxi_can_softc *sc)
+{
+ uint32_t reg0v;
+ struct mbuf *m;
+ struct ifnet *ifp = sc->sc_ifp;
+ struct can_frame *cf;
+ int dlc;
+ int regd, i;
+
+ KASSERT(mutex_owned(&sc->sc_intr_lock));
+ reg0v = sunxi_can_read(sc, SUNXI_CAN_TXBUF0_REG);
+ dlc = reg0v & SUNXI_CAN_TXBUF0_DL;
+
+ if (dlc > CAN_MAX_DLC) {
+ ifp->if_ierrors++;
+ sunxi_can_write(sc, SUNXI_CAN_CMD_REG, SUNXI_CAN_CMD_REL_RX_BUF);
+ return;
+ }
+
+ m = m_gethdr(M_NOWAIT, MT_HEADER);
+ if (m == NULL) {
+ ifp->if_ierrors++;
+ sunxi_can_write(sc, SUNXI_CAN_CMD_REG, SUNXI_CAN_CMD_REL_RX_BUF);
+ return;
+ }
+ cf = mtod(m, struct can_frame *);
+ memset(cf, 0, sizeof(struct can_frame));
+
+ cf->can_dlc = dlc;
+
Home |
Main Index |
Thread Index |
Old Index