Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/bouyer-socketcan]: src/sys/arch/arm/allwinner First draft of a driver fo...



details:   https://anonhg.NetBSD.org/src/rev/4917801ab336
branches:  bouyer-socketcan
changeset: 820884:4917801ab336
user:      bouyer <bouyer%NetBSD.org@localhost>
date:      Tue Apr 18 21:30:38 2017 +0000

description:
First draft of a driver for the CAN controller found in the
Allwinner A20 SoC.

diffstat:

 sys/arch/arm/allwinner/awin_can.c |  517 ++++++++++++++++++++++++++++++++++++++
 sys/arch/arm/allwinner/files.awin |    7 +-
 2 files changed, 523 insertions(+), 1 deletions(-)

diffs (truncated from 542 to 300 lines):

diff -r 6287f6c51f1b -r 4917801ab336 sys/arch/arm/allwinner/awin_can.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/arch/arm/allwinner/awin_can.c Tue Apr 18 21:30:38 2017 +0000
@@ -0,0 +1,517 @@
+/*     $NetBSD: awin_can.c,v 1.1.2.1 2017/04/18 21:30:38 bouyer Exp $  */
+
+/*-
+ * Copyright (c) 2017 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: awin_can.c,v 1.1.2.1 2017/04/18 21:30:38 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>
+
+#ifdef CAN
+#include <netcan/can.h>
+#include <netcan/can_var.h>
+#endif
+#include <net/bpf.h>
+
+#include <arm/allwinner/awin_reg.h>
+#include <arm/allwinner/awin_var.h>
+
+struct awin_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 int awin_can_match(device_t, cfdata_t, void *);
+static void awin_can_attach(device_t, device_t, void *);
+
+static int awin_can_intr(void *);
+
+static void awin_can_ifstart(struct ifnet *);
+static int awin_can_ifioctl(struct ifnet *, u_long, void *);
+static void awin_can_ifwatchdog(struct ifnet *);
+
+static void awin_can_enter_reset(struct awin_can_softc *);
+static void awin_can_exit_reset(struct awin_can_softc *);
+
+CFATTACH_DECL_NEW(awin_can, sizeof(struct awin_can_softc),
+       awin_can_match, awin_can_attach, NULL, NULL);
+
+static const struct awin_gpio_pinset awin_can_pinsets[2] = {
+        [0] = { 'A', AWIN_PIO_PA_CAN_FUNC, AWIN_PIO_PA_CAN_PINS },
+        [1] = { 'H', AWIN_PIO_PH_CAN_FUNC, AWIN_PIO_PH_CAN_PINS },
+};
+
+static inline uint32_t
+awin_can_read(struct awin_can_softc *sc, bus_size_t o)
+{
+       return bus_space_read_4(sc->sc_bst, sc->sc_bsh, o);
+}
+
+static inline void
+awin_can_write(struct awin_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
+awin_can_match(device_t parent, cfdata_t cf, void *aux)
+{
+       struct awinio_attach_args * const aio __diagused = aux;
+       const struct awin_locators * const loc __diagused = &aio->aio_loc;
+       const struct awin_gpio_pinset * const pinset =
+           &awin_can_pinsets[cf->cf_flags & 1];
+
+       KASSERT(!strcmp(cf->cf_name, loc->loc_name));
+       KASSERT(cf->cf_loc[AWINIOCF_PORT] == AWINIOCF_PORT_DEFAULT
+           || cf->cf_loc[AWINIOCF_PORT] == loc->loc_port);
+
+       if (!awin_gpio_pinset_available(pinset))
+               return 0;
+
+       return 1;
+}
+
+static void
+awin_can_attach(device_t parent, device_t self, void *aux)
+{
+       cfdata_t cf = device_cfdata(self);
+       struct awin_can_softc * const sc = device_private(self);
+       struct awinio_attach_args * const aio = aux;
+       const struct awin_locators * const loc = &aio->aio_loc;
+       const struct awin_gpio_pinset * const pinset =
+           &awin_can_pinsets[cf->cf_flags & 1];
+       struct ifnet *ifp;
+
+       sc->sc_dev = self;
+
+       awin_gpio_pinset_acquire(pinset);
+       awin_reg_set_clear(aio->aio_core_bst, aio->aio_ccm_bsh,
+           AWIN_APB1_GATING_REG, AWIN_APB_GATING1_CAN, 0);
+
+       mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_NET);
+
+       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);
+
+       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 = AWIN_REF_FREQ / 2;
+       sc->sc_timecaps.cltc_linkmode_caps =
+           CAN_LINKMODE_3SAMPLES | CAN_LINKMODE_LISTENONLY |
+           CAN_LINKMODE_LOOPBACK;
+       can_ifinit_timings(&sc->sc_cansc);
+
+       aprint_naive("\n");
+       aprint_normal(": CAN bus controller\n");
+
+       awin_can_enter_reset(sc);
+       /*
+        * Disable and then clear all interrupts
+        */
+       awin_can_write(sc, AWIN_CAN_INTE_REG, 0);
+       awin_can_write(sc, AWIN_CAN_INT_REG,
+           awin_can_read(sc, AWIN_CAN_INT_REG));
+
+       sc->sc_ih = intr_establish(loc->loc_intr, IPL_NET, IST_LEVEL,
+           awin_can_intr, sc);
+       if (sc->sc_ih == NULL) {
+               aprint_error_dev(self, "failed to establish interrupt %d\n",
+                   loc->loc_intr);
+               return;
+       }
+       aprint_normal_dev(self, "interrupting on irq %d\n", loc->loc_intr);
+
+       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 = awin_can_ifstart;
+       ifp->if_ioctl = awin_can_ifioctl;
+       ifp->if_watchdog = awin_can_ifwatchdog;
+
+       /*      
+        * Attach the interface.
+        */
+       if_attach(ifp);
+       can_ifattach(ifp);
+       bpf_attach(ifp, DLT_CAN_SOCKETCAN, 0);
+       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
+awin_can_rx_intr(struct awin_can_softc *sc)
+{
+       uint32_t reg0v;
+       struct mbuf *m;
+       struct ifnet  *ifp = sc->sc_ifp;
+       struct can_frame *cf;
+       int dlc;
+       int regd, i;
+
+       reg0v = awin_can_read(sc, AWIN_CAN_TXBUF0_REG);
+       dlc = reg0v & AWIN_CAN_TXBUF0_DL;
+
+       if (dlc > CAN_MAX_DLC) {
+               ifp->if_ierrors++;
+               return;
+       }
+               
+       m = m_gethdr(M_NOWAIT, MT_HEADER);
+       if (m == NULL) {
+               ifp->if_ierrors++;
+               return;
+       }
+       cf = mtod(m, struct can_frame *);
+       memset(cf, 0, sizeof(struct can_frame));
+
+       cf->can_dlc = dlc;
+
+       if (reg0v & AWIN_CAN_TXBUF0_EFF) {
+               cf->can_id = 
+                   (awin_can_read(sc, AWIN_CAN_TXBUF1_REG) << 21) |
+                   (awin_can_read(sc, AWIN_CAN_TXBUF2_REG) << 13) |
+                   (awin_can_read(sc, AWIN_CAN_TXBUF3_REG) << 5) |
+                   ((awin_can_read(sc, AWIN_CAN_TXBUF4_REG) >> 3) & 0x1f);
+               cf->can_id |= CAN_EFF_FLAG;
+               regd = AWIN_CAN_TXBUF5_REG;
+       } else {
+               cf->can_id = 
+                   (awin_can_read(sc, AWIN_CAN_TXBUF1_REG) << 3) |
+                   ((awin_can_read(sc, AWIN_CAN_TXBUF2_REG) << 5) & 0x7);
+               regd = AWIN_CAN_TXBUF3_REG;
+       }
+       if (reg0v & AWIN_CAN_TXBUF0_RTR) {
+               cf->can_id |= CAN_RTR_FLAG; 
+       } else {
+               for (i = 0; i < cf->can_dlc; i++) {
+                       cf->data[i] = awin_can_read(sc, regd + i * 4);
+               }
+       }
+       awin_can_write(sc, AWIN_CAN_CMD_REG, AWIN_CAN_CMD_REL_RX_BUF);
+       m->m_len = m->m_pkthdr.len =
+           offsetof(struct can_frame, data[0]) + cf->can_dlc;
+       ifp->if_ipackets++;
+       ifp->if_ibytes += m->m_len;
+       m_set_rcvif(m, ifp);
+       bpf_mtap(ifp, m);
+       can_input(ifp, m);
+}
+
+static void
+awin_can_tx_intr(struct awin_can_softc *sc)
+{
+       struct ifnet * const ifp = sc->sc_ifp;
+       struct mbuf *m;
+       struct can_frame *cf;
+       int regd;
+       uint32_t reg0val;
+       int i;
+
+       if ((m = sc->sc_m_transmit) != NULL) {
+               ifp->if_obytes += m->m_len;
+               ifp->if_opackets++;
+               can_mbuf_tag_clean(m);
+               m_set_rcvif(m, ifp);
+               can_input(ifp, m); /* loopback */
+               sc->sc_m_transmit = NULL;
+       }
+
+       IF_DEQUEUE(&ifp->if_snd, m);
+
+       if (m == NULL) {
+               ifp->if_flags &= ~IFF_OACTIVE;
+               return;



Home | Main Index | Thread Index | Old Index