Home | History | Annotate | Line # | Download | only in sunxi
sunxi_can.c revision 1.12
      1  1.12     skrll /*	$NetBSD: sunxi_can.c,v 1.12 2022/09/27 06:14:13 skrll Exp $	*/
      2   1.1    bouyer 
      3   1.1    bouyer /*-
      4   1.1    bouyer  * Copyright (c) 2017,2018 The NetBSD Foundation, Inc.
      5   1.1    bouyer  * All rights reserved.
      6   1.1    bouyer  *
      7   1.1    bouyer  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1    bouyer  * by Manuel Bouyer.
      9   1.1    bouyer  *
     10   1.1    bouyer  * Redistribution and use in source and binary forms, with or without
     11   1.1    bouyer  * modification, are permitted provided that the following conditions
     12   1.1    bouyer  * are met:
     13   1.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     14   1.1    bouyer  *    notice, this list of conditions and the following disclaimer.
     15   1.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1    bouyer  *    notice, this list of conditions and the following disclaimer in the
     17   1.1    bouyer  *    documentation and/or other materials provided with the distribution.
     18   1.1    bouyer  *
     19   1.1    bouyer  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1    bouyer  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1    bouyer  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1    bouyer  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1    bouyer  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1    bouyer  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1    bouyer  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1    bouyer  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1    bouyer  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1    bouyer  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1    bouyer  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1    bouyer  */
     31   1.1    bouyer 
     32   1.1    bouyer 
     33   1.1    bouyer #include "locators.h"
     34   1.1    bouyer #include "opt_can.h"
     35   1.1    bouyer 
     36   1.1    bouyer 
     37   1.1    bouyer #include <sys/cdefs.h>
     38   1.1    bouyer 
     39  1.12     skrll __KERNEL_RCSID(1, "$NetBSD: sunxi_can.c,v 1.12 2022/09/27 06:14:13 skrll Exp $");
     40   1.1    bouyer 
     41   1.1    bouyer #include <sys/param.h>
     42   1.1    bouyer #include <sys/bus.h>
     43   1.1    bouyer #include <sys/device.h>
     44   1.1    bouyer #include <sys/intr.h>
     45   1.1    bouyer #include <sys/ioctl.h>
     46   1.1    bouyer #include <sys/mutex.h>
     47   1.1    bouyer #include <sys/rndsource.h>
     48   1.1    bouyer #include <sys/mbuf.h>
     49   1.1    bouyer #include <sys/systm.h>
     50   1.1    bouyer 
     51   1.1    bouyer #include <net/if.h>
     52   1.1    bouyer #include <net/if_types.h>
     53   1.1    bouyer #include <net/bpf.h>
     54   1.1    bouyer 
     55   1.1    bouyer #ifdef CAN
     56   1.1    bouyer #include <netcan/can.h>
     57   1.1    bouyer #include <netcan/can_var.h>
     58   1.1    bouyer #endif
     59   1.1    bouyer 
     60   1.1    bouyer #include <dev/fdt/fdtvar.h>
     61   1.1    bouyer 
     62   1.1    bouyer #include <arm/sunxi/sunxi_can.h>
     63   1.1    bouyer 
     64   1.1    bouyer /* shortcut for all error interrupts */
     65   1.1    bouyer #define SUNXI_CAN_INT_ALLERRS (\
     66   1.1    bouyer 	SUNXI_CAN_INT_BERR | \
     67   1.1    bouyer 	SUNXI_CAN_INT_ARB_LOST | \
     68   1.1    bouyer 	SUNXI_CAN_INT_ERR_PASSIVE | \
     69   1.1    bouyer 	SUNXI_CAN_INT_DATA_OR | \
     70   1.1    bouyer 	SUNXI_CAN_INT_ERR \
     71   1.1    bouyer     )
     72   1.1    bouyer 
     73   1.1    bouyer struct sunxi_can_softc {
     74   1.1    bouyer 	struct canif_softc sc_cansc;
     75   1.1    bouyer 	bus_space_tag_t sc_bst;
     76   1.1    bouyer 	bus_space_handle_t sc_bsh;
     77   1.1    bouyer 	kmutex_t sc_intr_lock;
     78   1.1    bouyer 	void *sc_ih;
     79   1.1    bouyer 	struct ifnet *sc_ifp;
     80   1.1    bouyer 	krndsource_t sc_rnd_source;	/* random source */
     81   1.1    bouyer 	struct mbuf *sc_m_transmit; /* mbuf being transmitted */
     82   1.1    bouyer };
     83   1.1    bouyer #define sc_dev		sc_cansc.csc_dev
     84   1.1    bouyer #define sc_timecaps	sc_cansc.csc_timecaps
     85   1.1    bouyer #define sc_timings	sc_cansc.csc_timings
     86   1.1    bouyer #define sc_linkmodes	sc_cansc.csc_linkmodes
     87   1.1    bouyer 
     88   1.5   thorpej static const struct device_compatible_entry compat_data[] = {
     89   1.5   thorpej 	{ .compat = "allwinner,sun4i-a10-can" },
     90   1.7   thorpej 	DEVICE_COMPAT_EOL
     91   1.1    bouyer };
     92   1.1    bouyer 
     93   1.1    bouyer static int sunxi_can_match(device_t, cfdata_t, void *);
     94   1.1    bouyer static void sunxi_can_attach(device_t, device_t, void *);
     95   1.1    bouyer 
     96   1.1    bouyer static int sunxi_can_intr(void *);
     97   1.1    bouyer 
     98   1.1    bouyer static void sunxi_can_ifstart(struct ifnet *);
     99   1.1    bouyer static int sunxi_can_ifioctl(struct ifnet *, u_long, void *);
    100   1.1    bouyer static void sunxi_can_ifwatchdog(struct ifnet *);
    101   1.1    bouyer 
    102   1.1    bouyer static void sunxi_can_enter_reset(struct sunxi_can_softc *);
    103   1.1    bouyer static void sunxi_can_exit_reset(struct sunxi_can_softc *);
    104  1.11    bouyer static void sunxi_can_ifdown(struct sunxi_can_softc * const);
    105  1.11    bouyer static int sunxi_can_ifup(struct sunxi_can_softc * const);
    106   1.1    bouyer 
    107   1.1    bouyer CFATTACH_DECL_NEW(sunxi_can, sizeof(struct sunxi_can_softc),
    108   1.1    bouyer 	sunxi_can_match, sunxi_can_attach, NULL, NULL);
    109   1.1    bouyer 
    110   1.1    bouyer static inline uint32_t
    111   1.1    bouyer sunxi_can_read(struct sunxi_can_softc *sc, bus_size_t o)
    112   1.1    bouyer {
    113   1.1    bouyer 	return bus_space_read_4(sc->sc_bst, sc->sc_bsh, o);
    114   1.1    bouyer }
    115   1.1    bouyer 
    116   1.1    bouyer static inline void
    117   1.1    bouyer sunxi_can_write(struct sunxi_can_softc *sc, bus_size_t o, uint32_t v)
    118   1.1    bouyer {
    119   1.1    bouyer 	return bus_space_write_4(sc->sc_bst, sc->sc_bsh, o, v);
    120   1.1    bouyer }
    121   1.1    bouyer 
    122   1.1    bouyer static int
    123   1.1    bouyer sunxi_can_match(device_t parent, cfdata_t cf, void *aux)
    124   1.1    bouyer {
    125   1.1    bouyer 	struct fdt_attach_args * const faa = aux;
    126   1.1    bouyer 
    127   1.8   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    128   1.1    bouyer }
    129   1.1    bouyer 
    130   1.1    bouyer static void
    131   1.1    bouyer sunxi_can_attach(device_t parent, device_t self, void *aux)
    132   1.1    bouyer {
    133   1.1    bouyer 	struct sunxi_can_softc * const sc = device_private(self);
    134   1.1    bouyer 	struct fdt_attach_args * const faa = aux;
    135   1.1    bouyer 	struct ifnet *ifp;
    136   1.1    bouyer 	const int phandle = faa->faa_phandle;
    137   1.1    bouyer 	bus_addr_t addr;
    138   1.1    bouyer 	bus_size_t size;
    139   1.1    bouyer 	char intrstr[128];
    140   1.1    bouyer 	struct clk *clk;
    141   1.1    bouyer 	struct fdtbus_reset *rst;
    142   1.1    bouyer 
    143   1.1    bouyer 	sc->sc_dev = self;
    144   1.1    bouyer 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_NET);
    145   1.1    bouyer 
    146   1.1    bouyer 	sc->sc_bst = faa->faa_bst;
    147   1.1    bouyer 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    148   1.1    bouyer 		aprint_error(": couldn't get registers\n");
    149   1.1    bouyer 		return;
    150   1.1    bouyer 	}
    151   1.1    bouyer 
    152   1.1    bouyer 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    153   1.1    bouyer 		aprint_error(": couldn't map registers\n");
    154   1.1    bouyer 		return;
    155   1.1    bouyer 	}
    156   1.1    bouyer 
    157   1.1    bouyer 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    158   1.1    bouyer 		aprint_error(": failed to decode interrupt\n");
    159   1.1    bouyer 		return;
    160   1.1    bouyer 	}
    161   1.1    bouyer 
    162   1.1    bouyer 	if ((clk = fdtbus_clock_get_index(phandle, 0)) != NULL) {
    163   1.1    bouyer 		if (clk_enable(clk) != 0) {
    164   1.1    bouyer 			aprint_error(": couldn't enable clock\n");
    165   1.1    bouyer 			return;
    166   1.1    bouyer 		}
    167   1.1    bouyer 	}
    168   1.1    bouyer 
    169   1.1    bouyer 	if ((rst = fdtbus_reset_get_index(phandle, 0)) != NULL) {
    170   1.1    bouyer 		if (fdtbus_reset_deassert(rst) != 0) {
    171   1.1    bouyer 			aprint_error(": couldn't de-assert reset\n");
    172   1.1    bouyer 			return;
    173   1.1    bouyer 		}
    174   1.1    bouyer 	}
    175   1.1    bouyer 
    176   1.1    bouyer 	sc->sc_timecaps.cltc_prop_min = 0;
    177   1.1    bouyer 	sc->sc_timecaps.cltc_prop_max = 0;
    178   1.1    bouyer 	sc->sc_timecaps.cltc_ps1_min = 1;
    179   1.1    bouyer 	sc->sc_timecaps.cltc_ps1_max = 16;
    180   1.1    bouyer 	sc->sc_timecaps.cltc_ps2_min = 1;
    181   1.1    bouyer 	sc->sc_timecaps.cltc_ps2_max = 8;
    182   1.1    bouyer 	sc->sc_timecaps.cltc_sjw_max = 4;
    183   1.1    bouyer 	sc->sc_timecaps.cltc_brp_min = 1;
    184   1.1    bouyer 	sc->sc_timecaps.cltc_brp_max = 64;
    185   1.1    bouyer 	sc->sc_timecaps.cltc_brp_inc = 1;
    186   1.1    bouyer 	sc->sc_timecaps.cltc_clock_freq = clk_get_rate(clk);
    187   1.1    bouyer 	sc->sc_timecaps.cltc_linkmode_caps =
    188   1.1    bouyer 	    CAN_LINKMODE_3SAMPLES | CAN_LINKMODE_LISTENONLY |
    189   1.1    bouyer 	    CAN_LINKMODE_LOOPBACK;
    190   1.1    bouyer 	can_ifinit_timings(&sc->sc_cansc);
    191   1.1    bouyer 	sc->sc_timings.clt_prop = 0;
    192   1.1    bouyer 	sc->sc_timings.clt_sjw = 1;
    193   1.1    bouyer 
    194   1.1    bouyer 	aprint_naive("\n");
    195   1.1    bouyer 	aprint_normal(": CAN bus controller\n");
    196   1.1    bouyer 	aprint_debug_dev(self, ": clock freq %d\n",
    197   1.1    bouyer 	    sc->sc_timecaps.cltc_clock_freq);
    198   1.1    bouyer 
    199   1.1    bouyer 	sunxi_can_enter_reset(sc);
    200   1.1    bouyer 	/*
    201   1.1    bouyer 	 * Disable and then clear all interrupts
    202   1.1    bouyer 	 */
    203   1.1    bouyer 	sunxi_can_write(sc, SUNXI_CAN_INTE_REG, 0);
    204   1.1    bouyer 	sunxi_can_write(sc, SUNXI_CAN_INT_REG,
    205   1.1    bouyer 	    sunxi_can_read(sc, SUNXI_CAN_INT_REG));
    206   1.1    bouyer 
    207   1.4  jmcneill 	sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_NET, 0,
    208   1.4  jmcneill 	    sunxi_can_intr, sc, device_xname(self));
    209   1.1    bouyer 	if (sc->sc_ih == NULL) {
    210   1.1    bouyer 		aprint_error_dev(self, "failed to establish interrupt on %s\n",
    211   1.1    bouyer 		    intrstr);
    212   1.1    bouyer 		return;
    213   1.1    bouyer 	}
    214   1.1    bouyer 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    215   1.1    bouyer 
    216   1.1    bouyer 	ifp = if_alloc(IFT_OTHER);
    217   1.1    bouyer 	sc->sc_ifp = ifp;
    218   1.1    bouyer 	strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
    219   1.1    bouyer 	ifp->if_softc = sc;
    220   1.1    bouyer 	ifp->if_capabilities = 0;
    221   1.1    bouyer 	ifp->if_flags = 0;
    222   1.1    bouyer 	ifp->if_start = sunxi_can_ifstart;
    223   1.1    bouyer 	ifp->if_ioctl = sunxi_can_ifioctl;
    224   1.1    bouyer 	ifp->if_watchdog = sunxi_can_ifwatchdog;
    225   1.1    bouyer 
    226   1.1    bouyer 	/*
    227   1.1    bouyer 	 * Attach the interface.
    228   1.1    bouyer 	 */
    229   1.1    bouyer 	can_ifattach(ifp);
    230   1.1    bouyer 	if_deferred_start_init(ifp, NULL);
    231   1.1    bouyer 	bpf_mtap_softint_init(ifp);
    232   1.1    bouyer 	rnd_attach_source(&sc->sc_rnd_source, device_xname(self),
    233   1.1    bouyer 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
    234   1.1    bouyer #ifdef MBUFTRACE
    235  1.12     skrll 	ifp->if_mowner = kmem_zalloc(sizeof(*ifp->if_mowner), KM_SLEEP);
    236   1.1    bouyer 	strlcpy(ifp->if_mowner->mo_name, ifp->if_xname,
    237   1.1    bouyer 		sizeof(ifp->if_mowner->mo_name));
    238   1.1    bouyer 	MOWNER_ATTACH(ifp->if_mowner);
    239   1.1    bouyer #endif
    240   1.1    bouyer }
    241   1.1    bouyer 
    242   1.1    bouyer static void
    243   1.1    bouyer sunxi_can_rx_intr(struct sunxi_can_softc *sc)
    244   1.1    bouyer {
    245   1.1    bouyer 	uint32_t reg0v;
    246   1.1    bouyer 	struct mbuf *m;
    247   1.1    bouyer 	struct ifnet  *ifp = sc->sc_ifp;
    248   1.1    bouyer 	struct can_frame *cf;
    249   1.1    bouyer 	int dlc;
    250   1.1    bouyer 	int regd, i;
    251   1.1    bouyer 
    252   1.1    bouyer 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    253   1.1    bouyer 	reg0v = sunxi_can_read(sc, SUNXI_CAN_TXBUF0_REG);
    254   1.1    bouyer 	dlc = reg0v & SUNXI_CAN_TXBUF0_DL;
    255   1.1    bouyer 
    256   1.1    bouyer 	if (dlc > CAN_MAX_DLC) {
    257   1.3   thorpej 		if_statinc(ifp, if_ierrors);
    258   1.1    bouyer 		sunxi_can_write(sc, SUNXI_CAN_CMD_REG, SUNXI_CAN_CMD_REL_RX_BUF);
    259   1.1    bouyer 		return;
    260   1.1    bouyer 	}
    261   1.1    bouyer 
    262   1.1    bouyer 	m = m_gethdr(M_NOWAIT, MT_HEADER);
    263   1.1    bouyer 	if (m == NULL) {
    264   1.3   thorpej 		if_statinc(ifp, if_ierrors);
    265   1.1    bouyer 		sunxi_can_write(sc, SUNXI_CAN_CMD_REG, SUNXI_CAN_CMD_REL_RX_BUF);
    266   1.1    bouyer 		return;
    267   1.1    bouyer 	}
    268   1.1    bouyer 	cf = mtod(m, struct can_frame *);
    269   1.1    bouyer 	memset(cf, 0, sizeof(struct can_frame));
    270   1.1    bouyer 
    271   1.1    bouyer 	cf->can_dlc = dlc;
    272   1.1    bouyer 
    273   1.1    bouyer 	if (reg0v & SUNXI_CAN_TXBUF0_EFF) {
    274   1.1    bouyer 		cf->can_id =
    275   1.1    bouyer 		    (sunxi_can_read(sc, SUNXI_CAN_TXBUF1_REG) << 21) |
    276   1.1    bouyer 		    (sunxi_can_read(sc, SUNXI_CAN_TXBUF2_REG) << 13) |
    277   1.1    bouyer 		    (sunxi_can_read(sc, SUNXI_CAN_TXBUF3_REG) << 5) |
    278   1.1    bouyer 		    ((sunxi_can_read(sc, SUNXI_CAN_TXBUF4_REG) >> 3) & 0x1f);
    279   1.1    bouyer 		cf->can_id |= CAN_EFF_FLAG;
    280   1.1    bouyer 		regd = SUNXI_CAN_TXBUF5_REG;
    281   1.1    bouyer 	} else {
    282   1.1    bouyer 		cf->can_id =
    283   1.1    bouyer 		    (sunxi_can_read(sc, SUNXI_CAN_TXBUF1_REG) << 3) |
    284   1.1    bouyer 		    ((sunxi_can_read(sc, SUNXI_CAN_TXBUF2_REG) << 5) & 0x7);
    285   1.1    bouyer 		regd = SUNXI_CAN_TXBUF3_REG;
    286   1.1    bouyer 	}
    287   1.1    bouyer 	if (reg0v & SUNXI_CAN_TXBUF0_RTR) {
    288   1.1    bouyer 		cf->can_id |= CAN_RTR_FLAG;
    289   1.1    bouyer 	} else {
    290   1.1    bouyer 		for (i = 0; i < cf->can_dlc; i++) {
    291   1.1    bouyer 			cf->data[i] = sunxi_can_read(sc, regd + i * 4);
    292   1.1    bouyer 		}
    293   1.1    bouyer 	}
    294   1.1    bouyer 	sunxi_can_write(sc, SUNXI_CAN_CMD_REG, SUNXI_CAN_CMD_REL_RX_BUF);
    295   1.1    bouyer 	m->m_len = m->m_pkthdr.len = CAN_MTU;
    296   1.3   thorpej 	if_statadd(ifp, if_ibytes, m->m_len);
    297   1.1    bouyer 	m_set_rcvif(m, ifp);
    298   1.1    bouyer 	can_bpf_mtap(ifp, m, 1);
    299   1.1    bouyer 	can_input(ifp, m);
    300   1.1    bouyer }
    301   1.1    bouyer 
    302   1.1    bouyer static void
    303   1.1    bouyer sunxi_can_tx_intr(struct sunxi_can_softc *sc)
    304   1.1    bouyer {
    305   1.1    bouyer 	struct ifnet * const ifp = sc->sc_ifp;
    306   1.1    bouyer 	struct mbuf *m;
    307   1.1    bouyer 
    308   1.1    bouyer 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    309   1.1    bouyer 	if ((m = sc->sc_m_transmit) != NULL) {
    310   1.3   thorpej 		if_statadd2(ifp, if_obytes, m->m_len, if_opackets, 1);
    311   1.1    bouyer 		can_mbuf_tag_clean(m);
    312   1.1    bouyer 		m_set_rcvif(m, ifp);
    313   1.1    bouyer 		can_input(ifp, m); /* loopback */
    314   1.1    bouyer 		sc->sc_m_transmit = NULL;
    315   1.1    bouyer 		ifp->if_timer = 0;
    316   1.1    bouyer 	}
    317   1.1    bouyer 	if_schedule_deferred_start(ifp);
    318   1.1    bouyer }
    319   1.1    bouyer 
    320   1.1    bouyer static int
    321   1.1    bouyer sunxi_can_tx_abort(struct sunxi_can_softc *sc)
    322   1.1    bouyer {
    323   1.1    bouyer 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    324   1.1    bouyer 	if (sc->sc_m_transmit) {
    325   1.1    bouyer 		m_freem(sc->sc_m_transmit);
    326   1.1    bouyer 		sc->sc_m_transmit = NULL;
    327   1.1    bouyer 		sc->sc_ifp->if_timer = 0;
    328   1.1    bouyer 		/*
    329   1.1    bouyer 		 * the transmit abort will trigger a TX interrupt
    330   1.9   thorpej 		 * which will restart the queue as appropriate.
    331   1.1    bouyer 		 */
    332   1.1    bouyer 		sunxi_can_write(sc, SUNXI_CAN_CMD_REG, SUNXI_CAN_CMD_ABT_REQ);
    333   1.1    bouyer 		return 1;
    334   1.1    bouyer 	}
    335   1.1    bouyer 	return 0;
    336   1.1    bouyer }
    337   1.1    bouyer 
    338   1.1    bouyer static void
    339   1.1    bouyer sunxi_can_err_intr(struct sunxi_can_softc *sc, uint32_t irq, uint32_t sts)
    340   1.1    bouyer {
    341   1.1    bouyer 	struct ifnet * const ifp = sc->sc_ifp;
    342   1.1    bouyer 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    343   1.1    bouyer 	int txerr = 0;
    344   1.1    bouyer 	uint32_t reg;
    345   1.1    bouyer 
    346   1.1    bouyer 	if (irq & SUNXI_CAN_INT_DATA_OR) {
    347   1.3   thorpej 		if_statinc(ifp, if_ierrors);
    348  1.11    bouyer 		sunxi_can_ifdown(sc);
    349   1.1    bouyer 		sunxi_can_write(sc, SUNXI_CAN_CMD_REG, SUNXI_CAN_CMD_CLR_OR);
    350  1.11    bouyer 		sunxi_can_ifup(sc);
    351   1.1    bouyer 	}
    352   1.1    bouyer 	if (irq & SUNXI_CAN_INT_ERR) {
    353   1.1    bouyer 		reg = sunxi_can_read(sc, SUNXI_CAN_REC_REG);
    354   1.1    bouyer 		printf("%s: ERR interrupt status 0x%x counters 0x%x\n",
    355   1.1    bouyer 		    device_xname(sc->sc_dev), sts, reg);
    356   1.1    bouyer 
    357   1.1    bouyer 	}
    358   1.1    bouyer 	if (irq & SUNXI_CAN_INT_BERR) {
    359   1.1    bouyer 		if (sts & SUNXI_CAN_STA_TX)
    360   1.1    bouyer 			txerr++;
    361   1.1    bouyer 		if (sts & SUNXI_CAN_STA_RX)
    362   1.3   thorpej 			if_statinc(ifp, if_ierrors);
    363   1.1    bouyer 	}
    364   1.1    bouyer 	if (irq & SUNXI_CAN_INT_ERR_PASSIVE) {
    365   1.1    bouyer 		printf("%s: PASSV interrupt status 0x%x\n",
    366   1.1    bouyer 		    device_xname(sc->sc_dev), sts);
    367   1.1    bouyer 	}
    368   1.1    bouyer 	if (irq & SUNXI_CAN_INT_ARB_LOST) {
    369   1.1    bouyer 		txerr++;
    370   1.1    bouyer 	}
    371   1.1    bouyer 	if (txerr) {
    372   1.3   thorpej 		if_statadd(ifp, if_oerrors, txerr);
    373   1.1    bouyer 		(void) sunxi_can_tx_abort(sc);
    374   1.1    bouyer 	}
    375   1.1    bouyer }
    376   1.1    bouyer 
    377   1.1    bouyer int
    378   1.1    bouyer sunxi_can_intr(void *arg)
    379   1.1    bouyer {
    380   1.1    bouyer 	struct sunxi_can_softc * const sc = arg;
    381   1.1    bouyer 	int rv = 0;
    382   1.1    bouyer 	int irq;
    383   1.1    bouyer 
    384   1.1    bouyer 	mutex_enter(&sc->sc_intr_lock);
    385   1.1    bouyer 
    386   1.1    bouyer 	while ((irq = sunxi_can_read(sc, SUNXI_CAN_INT_REG)) != 0) {
    387   1.1    bouyer 		uint32_t sts = sunxi_can_read(sc, SUNXI_CAN_STA_REG);
    388   1.1    bouyer 		rv = 1;
    389  1.11    bouyer                 rnd_add_uint32(&sc->sc_rnd_source, irq);
    390   1.1    bouyer 
    391  1.10    bouyer 		if ((irq & (SUNXI_CAN_INT_RX_FLAG | SUNXI_CAN_INT_DATA_OR)) ==
    392  1.10    bouyer 		    SUNXI_CAN_INT_RX_FLAG) {
    393   1.1    bouyer 			while (sts & SUNXI_CAN_STA_RX_RDY) {
    394   1.1    bouyer 				sunxi_can_rx_intr(sc);
    395   1.1    bouyer 				sts = sunxi_can_read(sc, SUNXI_CAN_STA_REG);
    396   1.1    bouyer 			}
    397  1.11    bouyer 			/*
    398  1.11    bouyer 			 * Don't write SUNXI_CAN_INT_RX_FLAG to the interrupt
    399  1.11    bouyer 			 * register, this may clear the RX pending flag
    400  1.11    bouyer 			 * while there is indeed a packet pending.
    401  1.11    bouyer 			 * Reading packets should have cleared the RX interrupt,
    402  1.11    bouyer 			 * so just restart the loop and re-read the interrupt
    403  1.11    bouyer 			 * register. In the common case irq will now be 0.
    404  1.11    bouyer 			 */
    405  1.11    bouyer 			continue;
    406  1.11    bouyer 		}
    407  1.11    bouyer 		if (irq & SUNXI_CAN_INT_TX_FLAG) {
    408  1.11    bouyer 			sunxi_can_tx_intr(sc);
    409   1.1    bouyer 		}
    410   1.1    bouyer 		if (irq & SUNXI_CAN_INT_ALLERRS) {
    411   1.1    bouyer 			sunxi_can_err_intr(sc, irq, sts);
    412   1.1    bouyer 		}
    413   1.1    bouyer 		sunxi_can_write(sc, SUNXI_CAN_INT_REG, irq);
    414   1.1    bouyer 	}
    415   1.1    bouyer 	mutex_exit(&sc->sc_intr_lock);
    416   1.1    bouyer 
    417   1.1    bouyer 	return rv;
    418   1.1    bouyer }
    419   1.1    bouyer 
    420   1.1    bouyer void
    421   1.1    bouyer sunxi_can_ifstart(struct ifnet *ifp)
    422   1.1    bouyer {
    423   1.1    bouyer 	struct sunxi_can_softc * const sc = ifp->if_softc;
    424   1.1    bouyer 	struct mbuf *m;
    425   1.1    bouyer 	struct can_frame *cf;
    426   1.1    bouyer 	int regd;
    427   1.1    bouyer 	uint32_t reg0val;
    428   1.1    bouyer 	int i;
    429   1.1    bouyer 
    430   1.1    bouyer 	mutex_enter(&sc->sc_intr_lock);
    431   1.9   thorpej 	if (sc->sc_m_transmit != NULL)
    432   1.1    bouyer 		goto out;
    433   1.1    bouyer 
    434   1.1    bouyer 	IF_DEQUEUE(&ifp->if_snd, m);
    435   1.1    bouyer 
    436   1.1    bouyer 	if (m == NULL)
    437   1.1    bouyer 		goto out;
    438   1.1    bouyer 
    439   1.1    bouyer 	MCLAIM(m, ifp->if_mowner);
    440   1.1    bouyer 	sc->sc_m_transmit = m;
    441   1.1    bouyer 
    442   1.1    bouyer 	KASSERT((m->m_flags & M_PKTHDR) != 0);
    443   1.1    bouyer 	KASSERT(m->m_len == m->m_pkthdr.len);
    444   1.1    bouyer 
    445   1.1    bouyer 	cf = mtod(m, struct can_frame *);
    446   1.1    bouyer 	reg0val = cf->can_dlc & SUNXI_CAN_TXBUF0_DL;
    447   1.1    bouyer 	if (cf->can_id & CAN_RTR_FLAG)
    448   1.1    bouyer 		reg0val |= SUNXI_CAN_TXBUF0_RTR;
    449   1.1    bouyer 
    450   1.1    bouyer 	if (cf->can_id & CAN_EFF_FLAG) {
    451   1.1    bouyer 		reg0val |= SUNXI_CAN_TXBUF0_EFF;
    452   1.1    bouyer 		sunxi_can_write(sc, SUNXI_CAN_TXBUF1_REG,
    453   1.1    bouyer 		    (cf->can_id >> 21) & 0xff);
    454   1.1    bouyer 		sunxi_can_write(sc, SUNXI_CAN_TXBUF2_REG,
    455   1.1    bouyer 		    (cf->can_id >> 13) & 0xff);
    456   1.1    bouyer 		sunxi_can_write(sc, SUNXI_CAN_TXBUF3_REG,
    457   1.1    bouyer 		    (cf->can_id >> 5) & 0xff);
    458   1.1    bouyer 		sunxi_can_write(sc, SUNXI_CAN_TXBUF4_REG,
    459   1.1    bouyer 		    (cf->can_id << 3) & 0xf8);
    460   1.1    bouyer 		regd = SUNXI_CAN_TXBUF5_REG;
    461   1.1    bouyer 	} else {
    462   1.1    bouyer 		sunxi_can_write(sc, SUNXI_CAN_TXBUF1_REG,
    463   1.1    bouyer 		    (cf->can_id >> 3) & 0xff);
    464   1.1    bouyer 		sunxi_can_write(sc, SUNXI_CAN_TXBUF2_REG,
    465   1.1    bouyer 		    (cf->can_id << 5) & 0xe0);
    466   1.1    bouyer 		regd = SUNXI_CAN_TXBUF3_REG;
    467   1.1    bouyer 	}
    468   1.1    bouyer 
    469   1.1    bouyer 	for (i = 0; i < cf->can_dlc; i++) {
    470   1.1    bouyer 		sunxi_can_write(sc, regd + i * 4, cf->data[i]);
    471   1.1    bouyer 	}
    472   1.1    bouyer 	sunxi_can_write(sc, SUNXI_CAN_TXBUF0_REG, reg0val);
    473   1.1    bouyer 
    474   1.1    bouyer 	if (sc->sc_linkmodes & CAN_LINKMODE_LOOPBACK) {
    475   1.1    bouyer 		sunxi_can_write(sc, SUNXI_CAN_CMD_REG,
    476   1.1    bouyer 			SUNXI_CAN_CMD_TANS_REQ | SUNXI_CAN_CMD_SELF_REQ);
    477   1.1    bouyer 	} else {
    478   1.1    bouyer 		sunxi_can_write(sc, SUNXI_CAN_CMD_REG, SUNXI_CAN_CMD_TANS_REQ);
    479   1.1    bouyer 	}
    480   1.1    bouyer 	ifp->if_timer = 5;
    481   1.1    bouyer 	can_bpf_mtap(ifp, m, 0);
    482   1.1    bouyer out:
    483   1.1    bouyer 	mutex_exit(&sc->sc_intr_lock);
    484   1.1    bouyer }
    485   1.1    bouyer 
    486   1.1    bouyer static int
    487   1.1    bouyer sunxi_can_ifup(struct sunxi_can_softc * const sc)
    488   1.1    bouyer {
    489   1.1    bouyer 	uint32_t reg;
    490   1.1    bouyer 
    491   1.1    bouyer 	/* setup timings and mode - has to be done in reset */
    492   1.1    bouyer 	reg = SUNXI_CAN_MODSEL_RST;
    493   1.1    bouyer 	if (sc->sc_linkmodes & CAN_LINKMODE_LISTENONLY)
    494   1.1    bouyer 		reg |= SUNXI_CAN_MODSEL_LST_ONLY;
    495   1.1    bouyer 
    496   1.1    bouyer 	if (sc->sc_linkmodes & CAN_LINKMODE_LOOPBACK)
    497   1.1    bouyer 		reg |= SUNXI_CAN_MODSEL_LB_MOD;
    498   1.1    bouyer 
    499   1.1    bouyer 	sunxi_can_write(sc, SUNXI_CAN_MODSEL_REG, reg);
    500   1.1    bouyer 
    501   1.1    bouyer 	reg = 0;
    502   1.1    bouyer 	if (sc->sc_timings.clt_prop != 0)
    503   1.1    bouyer 		return EINVAL;
    504   1.1    bouyer 
    505   1.1    bouyer 	if (sc->sc_timings.clt_brp > sc->sc_timecaps.cltc_brp_max ||
    506   1.1    bouyer 	   sc->sc_timings.clt_brp < sc->sc_timecaps.cltc_brp_min)
    507   1.1    bouyer 		return EINVAL;
    508   1.1    bouyer 	reg |= (sc->sc_timings.clt_brp - 1) << 0;
    509   1.1    bouyer 
    510   1.1    bouyer 	if (sc->sc_timings.clt_ps1 > sc->sc_timecaps.cltc_ps1_max ||
    511   1.1    bouyer 	   sc->sc_timings.clt_ps1 < sc->sc_timecaps.cltc_ps1_min)
    512   1.1    bouyer 		return EINVAL;
    513   1.1    bouyer 	reg |= (sc->sc_timings.clt_ps1 - 1) << 16;
    514   1.1    bouyer 
    515   1.1    bouyer 	if (sc->sc_timings.clt_ps2 > sc->sc_timecaps.cltc_ps2_max ||
    516   1.1    bouyer 	   sc->sc_timings.clt_ps2 < sc->sc_timecaps.cltc_ps2_min)
    517   1.1    bouyer 		return EINVAL;
    518   1.1    bouyer 	reg |= (sc->sc_timings.clt_ps2 - 1) << 20;
    519   1.1    bouyer 
    520   1.1    bouyer 	if (sc->sc_timings.clt_sjw > sc->sc_timecaps.cltc_sjw_max ||
    521   1.1    bouyer 	   sc->sc_timings.clt_sjw < 1)
    522   1.1    bouyer 		return EINVAL;
    523   1.1    bouyer 	reg |= (sc->sc_timings.clt_sjw - 1) << 14;
    524   1.1    bouyer 
    525   1.1    bouyer 	if (sc->sc_linkmodes & CAN_LINKMODE_3SAMPLES)
    526   1.1    bouyer 		reg |= SUNXI_CAN_BUS_TIME_SAM;
    527   1.1    bouyer 
    528   1.1    bouyer 	sunxi_can_write(sc, SUNXI_CAN_BUS_TIME_REG, reg);
    529   1.1    bouyer 
    530   1.1    bouyer 	/* set filters to accept all frames */
    531   1.1    bouyer 	sunxi_can_write(sc, SUNXI_CAN_ACPC, 0x00000000);
    532   1.1    bouyer 	sunxi_can_write(sc, SUNXI_CAN_ACPM, 0xffffffff);
    533   1.1    bouyer 
    534   1.1    bouyer 	/* clear errors counter */
    535   1.1    bouyer 	sunxi_can_write(sc, SUNXI_CAN_REC_REG, 0);
    536   1.1    bouyer 
    537   1.1    bouyer 	/* leave reset mode and enable interrupts */
    538   1.1    bouyer 	sunxi_can_exit_reset(sc);
    539   1.1    bouyer 	sunxi_can_write(sc, SUNXI_CAN_INTE_REG,
    540   1.1    bouyer 	    SUNXI_CAN_INT_TX_FLAG | SUNXI_CAN_INT_RX_FLAG | SUNXI_CAN_INT_ALLERRS);
    541   1.1    bouyer 	sc->sc_ifp->if_flags |= IFF_RUNNING;
    542   1.1    bouyer 	return 0;
    543   1.1    bouyer }
    544   1.1    bouyer 
    545   1.1    bouyer static void
    546   1.1    bouyer sunxi_can_ifdown(struct sunxi_can_softc * const sc)
    547   1.1    bouyer {
    548   1.9   thorpej 	sc->sc_ifp->if_flags &= ~IFF_RUNNING;
    549   1.1    bouyer 	sc->sc_ifp->if_timer = 0;
    550   1.1    bouyer 	sunxi_can_enter_reset(sc);
    551   1.1    bouyer 	sunxi_can_write(sc, SUNXI_CAN_INTE_REG, 0);
    552   1.1    bouyer 	sunxi_can_write(sc, SUNXI_CAN_INT_REG,
    553   1.1    bouyer 	    sunxi_can_read(sc, SUNXI_CAN_INT_REG));
    554   1.1    bouyer }
    555   1.1    bouyer 
    556   1.1    bouyer static int
    557   1.1    bouyer sunxi_can_ifioctl(struct ifnet *ifp, u_long cmd, void *data)
    558   1.1    bouyer {
    559   1.1    bouyer 	struct sunxi_can_softc * const sc = ifp->if_softc;
    560   1.1    bouyer 	struct ifreq *ifr = (struct ifreq *)data;
    561   1.1    bouyer 	int error = 0;
    562   1.1    bouyer 
    563   1.1    bouyer 	mutex_enter(&sc->sc_intr_lock);
    564   1.1    bouyer 
    565   1.1    bouyer 	switch (cmd) {
    566   1.1    bouyer 	case SIOCINITIFADDR:
    567   1.1    bouyer 		error = EAFNOSUPPORT;
    568   1.1    bouyer 		break;
    569   1.1    bouyer 	case SIOCSIFMTU:
    570   1.1    bouyer 		if ((unsigned)ifr->ifr_mtu != sizeof(struct can_frame))
    571   1.1    bouyer 			error = EINVAL;
    572   1.1    bouyer 		break;
    573   1.1    bouyer 	case SIOCADDMULTI:
    574   1.1    bouyer 	case SIOCDELMULTI:
    575   1.1    bouyer 		error = EAFNOSUPPORT;
    576   1.1    bouyer 		break;
    577   1.1    bouyer 	default:
    578   1.1    bouyer 		error = ifioctl_common(ifp, cmd, data);
    579   1.1    bouyer 		if (error == 0) {
    580   1.1    bouyer 			if ((ifp->if_flags & IFF_UP) != 0 &&
    581   1.1    bouyer 			    (ifp->if_flags & IFF_RUNNING) == 0) {
    582   1.1    bouyer 				error = sunxi_can_ifup(sc);
    583   1.1    bouyer 				if (error) {
    584   1.1    bouyer 					ifp->if_flags &= ~IFF_UP;
    585   1.1    bouyer 				}
    586   1.1    bouyer 			} else if ((ifp->if_flags & IFF_UP) == 0 &&
    587   1.1    bouyer 			    (ifp->if_flags & IFF_RUNNING) != 0) {
    588   1.1    bouyer 				sunxi_can_ifdown(sc);
    589   1.1    bouyer 			}
    590   1.1    bouyer 		}
    591   1.1    bouyer 		break;
    592   1.1    bouyer 	}
    593   1.1    bouyer 
    594   1.1    bouyer 	mutex_exit(&sc->sc_intr_lock);
    595   1.1    bouyer 	return error;
    596   1.1    bouyer }
    597   1.1    bouyer 
    598   1.1    bouyer void
    599   1.1    bouyer sunxi_can_ifwatchdog(struct ifnet *ifp)
    600   1.1    bouyer {
    601   1.1    bouyer 	struct sunxi_can_softc * const sc = ifp->if_softc;
    602   1.1    bouyer 	printf("%s: watchdog timeout\n", device_xname(sc->sc_dev));
    603   1.1    bouyer 
    604   1.1    bouyer 	mutex_enter(&sc->sc_intr_lock);
    605   1.1    bouyer 	printf("irq 0x%x en 0x%x mode 0x%x status 0x%x timings 0x%x err 0x%x\n",
    606   1.1    bouyer 	    sunxi_can_read(sc, SUNXI_CAN_INT_REG),
    607   1.1    bouyer 	    sunxi_can_read(sc, SUNXI_CAN_INTE_REG),
    608   1.1    bouyer 	    sunxi_can_read(sc, SUNXI_CAN_MODSEL_REG),
    609   1.1    bouyer 	    sunxi_can_read(sc, SUNXI_CAN_STA_REG),
    610   1.1    bouyer 	    sunxi_can_read(sc, SUNXI_CAN_BUS_TIME_REG),
    611   1.1    bouyer 	    sunxi_can_read(sc, SUNXI_CAN_REC_REG));
    612   1.1    bouyer 	/* if there is a transmit in progress abort */
    613   1.1    bouyer 	if (sunxi_can_tx_abort(sc)) {
    614   1.3   thorpej 		if_statinc(ifp, if_oerrors);
    615   1.1    bouyer 	}
    616   1.1    bouyer 	mutex_exit(&sc->sc_intr_lock);
    617   1.1    bouyer }
    618   1.1    bouyer 
    619   1.1    bouyer static void
    620   1.1    bouyer sunxi_can_enter_reset(struct sunxi_can_softc *sc)
    621   1.1    bouyer {
    622   1.1    bouyer 	int i;
    623   1.1    bouyer 	uint32_t val;
    624   1.1    bouyer 
    625   1.1    bouyer 	for (i = 0; i < 1000; i++) {
    626   1.1    bouyer 		val = sunxi_can_read(sc, SUNXI_CAN_MODSEL_REG);
    627   1.1    bouyer 		val |= SUNXI_CAN_MODSEL_RST;
    628   1.1    bouyer 		sunxi_can_write(sc, SUNXI_CAN_MODSEL_REG, val);
    629   1.1    bouyer 		val = sunxi_can_read(sc, SUNXI_CAN_MODSEL_REG);
    630   1.1    bouyer 		if (val & SUNXI_CAN_MODSEL_RST)
    631   1.1    bouyer 			return;
    632   1.1    bouyer 	}
    633   1.1    bouyer 	printf("%s: couldn't enter reset mode\n", device_xname(sc->sc_dev));
    634   1.1    bouyer }
    635   1.1    bouyer 
    636   1.1    bouyer static void
    637   1.1    bouyer sunxi_can_exit_reset(struct sunxi_can_softc *sc)
    638   1.1    bouyer {
    639   1.1    bouyer 	int i;
    640   1.1    bouyer 	uint32_t val;
    641   1.1    bouyer 
    642   1.1    bouyer 	for (i = 0; i < 1000; i++) {
    643   1.1    bouyer 		val = sunxi_can_read(sc, SUNXI_CAN_MODSEL_REG);
    644   1.1    bouyer 		val &= ~SUNXI_CAN_MODSEL_RST;
    645   1.1    bouyer 		sunxi_can_write(sc, SUNXI_CAN_MODSEL_REG, val);
    646   1.1    bouyer 		val = sunxi_can_read(sc, SUNXI_CAN_MODSEL_REG);
    647   1.1    bouyer 		if ((val & SUNXI_CAN_MODSEL_RST) == 0)
    648   1.1    bouyer 			return;
    649   1.1    bouyer 	}
    650   1.1    bouyer 	printf("%s: couldn't leave reset mode\n", device_xname(sc->sc_dev));
    651   1.1    bouyer }
    652