Home | History | Annotate | Line # | Download | only in sunxi
sunxi_can.c revision 1.3
      1  1.3  thorpej /*	$NetBSD: sunxi_can.c,v 1.3 2020/01/29 06:05:31 thorpej 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.3  thorpej __KERNEL_RCSID(1, "$NetBSD: sunxi_can.c,v 1.3 2020/01/29 06:05:31 thorpej 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.1   bouyer static const struct of_compat_data compat_data[] = {
     89  1.1   bouyer 	{"allwinner,sun4i-a10-can", 0},
     90  1.1   bouyer 	{NULL}
     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.1   bouyer 
    105  1.1   bouyer CFATTACH_DECL_NEW(sunxi_can, sizeof(struct sunxi_can_softc),
    106  1.1   bouyer 	sunxi_can_match, sunxi_can_attach, NULL, NULL);
    107  1.1   bouyer 
    108  1.1   bouyer static inline uint32_t
    109  1.1   bouyer sunxi_can_read(struct sunxi_can_softc *sc, bus_size_t o)
    110  1.1   bouyer {
    111  1.1   bouyer 	return bus_space_read_4(sc->sc_bst, sc->sc_bsh, o);
    112  1.1   bouyer }
    113  1.1   bouyer 
    114  1.1   bouyer static inline void
    115  1.1   bouyer sunxi_can_write(struct sunxi_can_softc *sc, bus_size_t o, uint32_t v)
    116  1.1   bouyer {
    117  1.1   bouyer 	return bus_space_write_4(sc->sc_bst, sc->sc_bsh, o, v);
    118  1.1   bouyer }
    119  1.1   bouyer 
    120  1.1   bouyer static int
    121  1.1   bouyer sunxi_can_match(device_t parent, cfdata_t cf, void *aux)
    122  1.1   bouyer {
    123  1.1   bouyer 	struct fdt_attach_args * const faa = aux;
    124  1.1   bouyer 
    125  1.1   bouyer 	return of_match_compat_data(faa->faa_phandle, compat_data);
    126  1.1   bouyer }
    127  1.1   bouyer 
    128  1.1   bouyer static void
    129  1.1   bouyer sunxi_can_attach(device_t parent, device_t self, void *aux)
    130  1.1   bouyer {
    131  1.1   bouyer 	struct sunxi_can_softc * const sc = device_private(self);
    132  1.1   bouyer 	struct fdt_attach_args * const faa = aux;
    133  1.1   bouyer 	struct ifnet *ifp;
    134  1.1   bouyer 	const int phandle = faa->faa_phandle;
    135  1.1   bouyer 	bus_addr_t addr;
    136  1.1   bouyer 	bus_size_t size;
    137  1.1   bouyer 	char intrstr[128];
    138  1.1   bouyer 	struct clk *clk;
    139  1.1   bouyer 	struct fdtbus_reset *rst;
    140  1.1   bouyer 
    141  1.1   bouyer 	sc->sc_dev = self;
    142  1.1   bouyer 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_NET);
    143  1.1   bouyer 
    144  1.1   bouyer 	sc->sc_bst = faa->faa_bst;
    145  1.1   bouyer 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    146  1.1   bouyer 		aprint_error(": couldn't get registers\n");
    147  1.1   bouyer 		return;
    148  1.1   bouyer 	}
    149  1.1   bouyer 
    150  1.1   bouyer 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    151  1.1   bouyer 		aprint_error(": couldn't map registers\n");
    152  1.1   bouyer 		return;
    153  1.1   bouyer 	}
    154  1.1   bouyer 
    155  1.1   bouyer 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    156  1.1   bouyer 		aprint_error(": failed to decode interrupt\n");
    157  1.1   bouyer 		return;
    158  1.1   bouyer 	}
    159  1.1   bouyer 
    160  1.1   bouyer 	if ((clk = fdtbus_clock_get_index(phandle, 0)) != NULL) {
    161  1.1   bouyer 		if (clk_enable(clk) != 0) {
    162  1.1   bouyer 			aprint_error(": couldn't enable clock\n");
    163  1.1   bouyer 			return;
    164  1.1   bouyer 		}
    165  1.1   bouyer 	}
    166  1.1   bouyer 
    167  1.1   bouyer 	if ((rst = fdtbus_reset_get_index(phandle, 0)) != NULL) {
    168  1.1   bouyer 		if (fdtbus_reset_deassert(rst) != 0) {
    169  1.1   bouyer 			aprint_error(": couldn't de-assert reset\n");
    170  1.1   bouyer 			return;
    171  1.1   bouyer 		}
    172  1.1   bouyer 	}
    173  1.1   bouyer 
    174  1.1   bouyer 	sc->sc_timecaps.cltc_prop_min = 0;
    175  1.1   bouyer 	sc->sc_timecaps.cltc_prop_max = 0;
    176  1.1   bouyer 	sc->sc_timecaps.cltc_ps1_min = 1;
    177  1.1   bouyer 	sc->sc_timecaps.cltc_ps1_max = 16;
    178  1.1   bouyer 	sc->sc_timecaps.cltc_ps2_min = 1;
    179  1.1   bouyer 	sc->sc_timecaps.cltc_ps2_max = 8;
    180  1.1   bouyer 	sc->sc_timecaps.cltc_sjw_max = 4;
    181  1.1   bouyer 	sc->sc_timecaps.cltc_brp_min = 1;
    182  1.1   bouyer 	sc->sc_timecaps.cltc_brp_max = 64;
    183  1.1   bouyer 	sc->sc_timecaps.cltc_brp_inc = 1;
    184  1.1   bouyer 	sc->sc_timecaps.cltc_clock_freq = clk_get_rate(clk);
    185  1.1   bouyer 	sc->sc_timecaps.cltc_linkmode_caps =
    186  1.1   bouyer 	    CAN_LINKMODE_3SAMPLES | CAN_LINKMODE_LISTENONLY |
    187  1.1   bouyer 	    CAN_LINKMODE_LOOPBACK;
    188  1.1   bouyer 	can_ifinit_timings(&sc->sc_cansc);
    189  1.1   bouyer 	sc->sc_timings.clt_prop = 0;
    190  1.1   bouyer 	sc->sc_timings.clt_sjw = 1;
    191  1.1   bouyer 
    192  1.1   bouyer 	aprint_naive("\n");
    193  1.1   bouyer 	aprint_normal(": CAN bus controller\n");
    194  1.1   bouyer 	aprint_debug_dev(self, ": clock freq %d\n",
    195  1.1   bouyer 	    sc->sc_timecaps.cltc_clock_freq);
    196  1.1   bouyer 
    197  1.1   bouyer 	sunxi_can_enter_reset(sc);
    198  1.1   bouyer 	/*
    199  1.1   bouyer 	 * Disable and then clear all interrupts
    200  1.1   bouyer 	 */
    201  1.1   bouyer 	sunxi_can_write(sc, SUNXI_CAN_INTE_REG, 0);
    202  1.1   bouyer 	sunxi_can_write(sc, SUNXI_CAN_INT_REG,
    203  1.1   bouyer 	    sunxi_can_read(sc, SUNXI_CAN_INT_REG));
    204  1.1   bouyer 
    205  1.1   bouyer 	sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_NET, 0,
    206  1.1   bouyer 	    sunxi_can_intr, sc);
    207  1.1   bouyer 	if (sc->sc_ih == NULL) {
    208  1.1   bouyer 		aprint_error_dev(self, "failed to establish interrupt on %s\n",
    209  1.1   bouyer 		    intrstr);
    210  1.1   bouyer 		return;
    211  1.1   bouyer 	}
    212  1.1   bouyer 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    213  1.1   bouyer 
    214  1.1   bouyer 	ifp = if_alloc(IFT_OTHER);
    215  1.1   bouyer 	sc->sc_ifp = ifp;
    216  1.1   bouyer 	strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
    217  1.1   bouyer 	ifp->if_softc = sc;
    218  1.1   bouyer 	ifp->if_capabilities = 0;
    219  1.1   bouyer 	ifp->if_flags = 0;
    220  1.1   bouyer 	ifp->if_start = sunxi_can_ifstart;
    221  1.1   bouyer 	ifp->if_ioctl = sunxi_can_ifioctl;
    222  1.1   bouyer 	ifp->if_watchdog = sunxi_can_ifwatchdog;
    223  1.1   bouyer 
    224  1.1   bouyer 	/*
    225  1.1   bouyer 	 * Attach the interface.
    226  1.1   bouyer 	 */
    227  1.1   bouyer 	can_ifattach(ifp);
    228  1.1   bouyer 	if_deferred_start_init(ifp, NULL);
    229  1.1   bouyer 	bpf_mtap_softint_init(ifp);
    230  1.1   bouyer 	rnd_attach_source(&sc->sc_rnd_source, device_xname(self),
    231  1.1   bouyer 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
    232  1.1   bouyer #ifdef MBUFTRACE
    233  1.1   bouyer 	ifp->if_mowner = malloc(sizeof(struct mowner), M_DEVBUF,
    234  1.1   bouyer 	    M_WAITOK | M_ZERO);
    235  1.1   bouyer 	strlcpy(ifp->if_mowner->mo_name, ifp->if_xname,
    236  1.1   bouyer 		sizeof(ifp->if_mowner->mo_name));
    237  1.1   bouyer 	MOWNER_ATTACH(ifp->if_mowner);
    238  1.1   bouyer #endif
    239  1.1   bouyer }
    240  1.1   bouyer 
    241  1.1   bouyer static void
    242  1.1   bouyer sunxi_can_rx_intr(struct sunxi_can_softc *sc)
    243  1.1   bouyer {
    244  1.1   bouyer 	uint32_t reg0v;
    245  1.1   bouyer 	struct mbuf *m;
    246  1.1   bouyer 	struct ifnet  *ifp = sc->sc_ifp;
    247  1.1   bouyer 	struct can_frame *cf;
    248  1.1   bouyer 	int dlc;
    249  1.1   bouyer 	int regd, i;
    250  1.1   bouyer 
    251  1.1   bouyer 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    252  1.1   bouyer 	reg0v = sunxi_can_read(sc, SUNXI_CAN_TXBUF0_REG);
    253  1.1   bouyer 	dlc = reg0v & SUNXI_CAN_TXBUF0_DL;
    254  1.1   bouyer 
    255  1.1   bouyer 	if (dlc > CAN_MAX_DLC) {
    256  1.3  thorpej 		if_statinc(ifp, if_ierrors);
    257  1.1   bouyer 		sunxi_can_write(sc, SUNXI_CAN_CMD_REG, SUNXI_CAN_CMD_REL_RX_BUF);
    258  1.1   bouyer 		return;
    259  1.1   bouyer 	}
    260  1.1   bouyer 
    261  1.1   bouyer 	m = m_gethdr(M_NOWAIT, MT_HEADER);
    262  1.1   bouyer 	if (m == NULL) {
    263  1.3  thorpej 		if_statinc(ifp, if_ierrors);
    264  1.1   bouyer 		sunxi_can_write(sc, SUNXI_CAN_CMD_REG, SUNXI_CAN_CMD_REL_RX_BUF);
    265  1.1   bouyer 		return;
    266  1.1   bouyer 	}
    267  1.1   bouyer 	cf = mtod(m, struct can_frame *);
    268  1.1   bouyer 	memset(cf, 0, sizeof(struct can_frame));
    269  1.1   bouyer 
    270  1.1   bouyer 	cf->can_dlc = dlc;
    271  1.1   bouyer 
    272  1.1   bouyer 	if (reg0v & SUNXI_CAN_TXBUF0_EFF) {
    273  1.1   bouyer 		cf->can_id =
    274  1.1   bouyer 		    (sunxi_can_read(sc, SUNXI_CAN_TXBUF1_REG) << 21) |
    275  1.1   bouyer 		    (sunxi_can_read(sc, SUNXI_CAN_TXBUF2_REG) << 13) |
    276  1.1   bouyer 		    (sunxi_can_read(sc, SUNXI_CAN_TXBUF3_REG) << 5) |
    277  1.1   bouyer 		    ((sunxi_can_read(sc, SUNXI_CAN_TXBUF4_REG) >> 3) & 0x1f);
    278  1.1   bouyer 		cf->can_id |= CAN_EFF_FLAG;
    279  1.1   bouyer 		regd = SUNXI_CAN_TXBUF5_REG;
    280  1.1   bouyer 	} else {
    281  1.1   bouyer 		cf->can_id =
    282  1.1   bouyer 		    (sunxi_can_read(sc, SUNXI_CAN_TXBUF1_REG) << 3) |
    283  1.1   bouyer 		    ((sunxi_can_read(sc, SUNXI_CAN_TXBUF2_REG) << 5) & 0x7);
    284  1.1   bouyer 		regd = SUNXI_CAN_TXBUF3_REG;
    285  1.1   bouyer 	}
    286  1.1   bouyer 	if (reg0v & SUNXI_CAN_TXBUF0_RTR) {
    287  1.1   bouyer 		cf->can_id |= CAN_RTR_FLAG;
    288  1.1   bouyer 	} else {
    289  1.1   bouyer 		for (i = 0; i < cf->can_dlc; i++) {
    290  1.1   bouyer 			cf->data[i] = sunxi_can_read(sc, regd + i * 4);
    291  1.1   bouyer 		}
    292  1.1   bouyer 	}
    293  1.1   bouyer 	sunxi_can_write(sc, SUNXI_CAN_CMD_REG, SUNXI_CAN_CMD_REL_RX_BUF);
    294  1.1   bouyer 	m->m_len = m->m_pkthdr.len = CAN_MTU;
    295  1.3  thorpej 	if_statadd(ifp, if_ibytes, m->m_len);
    296  1.1   bouyer 	m_set_rcvif(m, ifp);
    297  1.1   bouyer 	can_bpf_mtap(ifp, m, 1);
    298  1.1   bouyer 	can_input(ifp, m);
    299  1.1   bouyer }
    300  1.1   bouyer 
    301  1.1   bouyer static void
    302  1.1   bouyer sunxi_can_tx_intr(struct sunxi_can_softc *sc)
    303  1.1   bouyer {
    304  1.1   bouyer 	struct ifnet * const ifp = sc->sc_ifp;
    305  1.1   bouyer 	struct mbuf *m;
    306  1.1   bouyer 
    307  1.1   bouyer 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    308  1.1   bouyer 	if ((m = sc->sc_m_transmit) != NULL) {
    309  1.3  thorpej 		if_statadd2(ifp, if_obytes, m->m_len, if_opackets, 1);
    310  1.1   bouyer 		can_mbuf_tag_clean(m);
    311  1.1   bouyer 		m_set_rcvif(m, ifp);
    312  1.1   bouyer 		can_input(ifp, m); /* loopback */
    313  1.1   bouyer 		sc->sc_m_transmit = NULL;
    314  1.1   bouyer 		ifp->if_timer = 0;
    315  1.1   bouyer 	}
    316  1.1   bouyer 	ifp->if_flags &= ~IFF_OACTIVE;
    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.1   bouyer 		 * which will restart the queue or cleae OACTIVE,
    331  1.1   bouyer 		 * as appropriate
    332  1.1   bouyer 		 */
    333  1.1   bouyer 		sunxi_can_write(sc, SUNXI_CAN_CMD_REG, SUNXI_CAN_CMD_ABT_REQ);
    334  1.1   bouyer 		return 1;
    335  1.1   bouyer 	}
    336  1.1   bouyer 	return 0;
    337  1.1   bouyer }
    338  1.1   bouyer 
    339  1.1   bouyer static void
    340  1.1   bouyer sunxi_can_err_intr(struct sunxi_can_softc *sc, uint32_t irq, uint32_t sts)
    341  1.1   bouyer {
    342  1.1   bouyer 	struct ifnet * const ifp = sc->sc_ifp;
    343  1.1   bouyer 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    344  1.1   bouyer 	int txerr = 0;
    345  1.1   bouyer 	uint32_t reg;
    346  1.1   bouyer 
    347  1.1   bouyer 	if (irq & SUNXI_CAN_INT_DATA_OR) {
    348  1.3  thorpej 		if_statinc(ifp, if_ierrors);
    349  1.1   bouyer 		sunxi_can_write(sc, SUNXI_CAN_CMD_REG, SUNXI_CAN_CMD_CLR_OR);
    350  1.1   bouyer 	}
    351  1.1   bouyer 	if (irq & SUNXI_CAN_INT_ERR) {
    352  1.1   bouyer 		reg = sunxi_can_read(sc, SUNXI_CAN_REC_REG);
    353  1.1   bouyer 		printf("%s: ERR interrupt status 0x%x counters 0x%x\n",
    354  1.1   bouyer 		    device_xname(sc->sc_dev), sts, reg);
    355  1.1   bouyer 
    356  1.1   bouyer 	}
    357  1.1   bouyer 	if (irq & SUNXI_CAN_INT_BERR) {
    358  1.1   bouyer 		if (sts & SUNXI_CAN_STA_TX)
    359  1.1   bouyer 			txerr++;
    360  1.1   bouyer 		if (sts & SUNXI_CAN_STA_RX)
    361  1.3  thorpej 			if_statinc(ifp, if_ierrors);
    362  1.1   bouyer 	}
    363  1.1   bouyer 	if (irq & SUNXI_CAN_INT_ERR_PASSIVE) {
    364  1.1   bouyer 		printf("%s: PASSV interrupt status 0x%x\n",
    365  1.1   bouyer 		    device_xname(sc->sc_dev), sts);
    366  1.1   bouyer 	}
    367  1.1   bouyer 	if (irq & SUNXI_CAN_INT_ARB_LOST) {
    368  1.1   bouyer 		txerr++;
    369  1.1   bouyer 	}
    370  1.1   bouyer 	if (txerr) {
    371  1.3  thorpej 		if_statadd(ifp, if_oerrors, txerr);
    372  1.1   bouyer 		(void) sunxi_can_tx_abort(sc);
    373  1.1   bouyer 	}
    374  1.1   bouyer }
    375  1.1   bouyer 
    376  1.1   bouyer int
    377  1.1   bouyer sunxi_can_intr(void *arg)
    378  1.1   bouyer {
    379  1.1   bouyer 	struct sunxi_can_softc * const sc = arg;
    380  1.1   bouyer 	int rv = 0;
    381  1.1   bouyer 	int irq;
    382  1.1   bouyer 
    383  1.1   bouyer 	mutex_enter(&sc->sc_intr_lock);
    384  1.1   bouyer 
    385  1.1   bouyer 	while ((irq = sunxi_can_read(sc, SUNXI_CAN_INT_REG)) != 0) {
    386  1.1   bouyer 		uint32_t sts = sunxi_can_read(sc, SUNXI_CAN_STA_REG);
    387  1.1   bouyer 		rv = 1;
    388  1.1   bouyer 
    389  1.1   bouyer 		if (irq & SUNXI_CAN_INT_TX_FLAG) {
    390  1.1   bouyer 			sunxi_can_tx_intr(sc);
    391  1.1   bouyer 		}
    392  1.1   bouyer 		if (irq & 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.1   bouyer 		}
    398  1.1   bouyer 		if (irq & SUNXI_CAN_INT_ALLERRS) {
    399  1.1   bouyer 			sunxi_can_err_intr(sc, irq, sts);
    400  1.1   bouyer 		}
    401  1.1   bouyer 		sunxi_can_write(sc, SUNXI_CAN_INT_REG, irq);
    402  1.1   bouyer                 rnd_add_uint32(&sc->sc_rnd_source, irq);
    403  1.1   bouyer 
    404  1.1   bouyer 	}
    405  1.1   bouyer 	mutex_exit(&sc->sc_intr_lock);
    406  1.1   bouyer 
    407  1.1   bouyer 	return rv;
    408  1.1   bouyer }
    409  1.1   bouyer 
    410  1.1   bouyer void
    411  1.1   bouyer sunxi_can_ifstart(struct ifnet *ifp)
    412  1.1   bouyer {
    413  1.1   bouyer 	struct sunxi_can_softc * const sc = ifp->if_softc;
    414  1.1   bouyer 	struct mbuf *m;
    415  1.1   bouyer 	struct can_frame *cf;
    416  1.1   bouyer 	int regd;
    417  1.1   bouyer 	uint32_t reg0val;
    418  1.1   bouyer 	int i;
    419  1.1   bouyer 
    420  1.1   bouyer 	mutex_enter(&sc->sc_intr_lock);
    421  1.1   bouyer 	if (ifp->if_flags & IFF_OACTIVE)
    422  1.1   bouyer 		goto out;
    423  1.1   bouyer 
    424  1.1   bouyer 	IF_DEQUEUE(&ifp->if_snd, m);
    425  1.1   bouyer 
    426  1.1   bouyer 	if (m == NULL)
    427  1.1   bouyer 		goto out;
    428  1.1   bouyer 
    429  1.1   bouyer 	MCLAIM(m, ifp->if_mowner);
    430  1.1   bouyer 	sc->sc_m_transmit = m;
    431  1.1   bouyer 
    432  1.1   bouyer 	KASSERT((m->m_flags & M_PKTHDR) != 0);
    433  1.1   bouyer 	KASSERT(m->m_len == m->m_pkthdr.len);
    434  1.1   bouyer 
    435  1.1   bouyer 	cf = mtod(m, struct can_frame *);
    436  1.1   bouyer 	reg0val = cf->can_dlc & SUNXI_CAN_TXBUF0_DL;
    437  1.1   bouyer 	if (cf->can_id & CAN_RTR_FLAG)
    438  1.1   bouyer 		reg0val |= SUNXI_CAN_TXBUF0_RTR;
    439  1.1   bouyer 
    440  1.1   bouyer 	if (cf->can_id & CAN_EFF_FLAG) {
    441  1.1   bouyer 		reg0val |= SUNXI_CAN_TXBUF0_EFF;
    442  1.1   bouyer 		sunxi_can_write(sc, SUNXI_CAN_TXBUF1_REG,
    443  1.1   bouyer 		    (cf->can_id >> 21) & 0xff);
    444  1.1   bouyer 		sunxi_can_write(sc, SUNXI_CAN_TXBUF2_REG,
    445  1.1   bouyer 		    (cf->can_id >> 13) & 0xff);
    446  1.1   bouyer 		sunxi_can_write(sc, SUNXI_CAN_TXBUF3_REG,
    447  1.1   bouyer 		    (cf->can_id >> 5) & 0xff);
    448  1.1   bouyer 		sunxi_can_write(sc, SUNXI_CAN_TXBUF4_REG,
    449  1.1   bouyer 		    (cf->can_id << 3) & 0xf8);
    450  1.1   bouyer 		regd = SUNXI_CAN_TXBUF5_REG;
    451  1.1   bouyer 	} else {
    452  1.1   bouyer 		sunxi_can_write(sc, SUNXI_CAN_TXBUF1_REG,
    453  1.1   bouyer 		    (cf->can_id >> 3) & 0xff);
    454  1.1   bouyer 		sunxi_can_write(sc, SUNXI_CAN_TXBUF2_REG,
    455  1.1   bouyer 		    (cf->can_id << 5) & 0xe0);
    456  1.1   bouyer 		regd = SUNXI_CAN_TXBUF3_REG;
    457  1.1   bouyer 	}
    458  1.1   bouyer 
    459  1.1   bouyer 	for (i = 0; i < cf->can_dlc; i++) {
    460  1.1   bouyer 		sunxi_can_write(sc, regd + i * 4, cf->data[i]);
    461  1.1   bouyer 	}
    462  1.1   bouyer 	sunxi_can_write(sc, SUNXI_CAN_TXBUF0_REG, reg0val);
    463  1.1   bouyer 
    464  1.1   bouyer 	if (sc->sc_linkmodes & CAN_LINKMODE_LOOPBACK) {
    465  1.1   bouyer 		sunxi_can_write(sc, SUNXI_CAN_CMD_REG,
    466  1.1   bouyer 			SUNXI_CAN_CMD_TANS_REQ | SUNXI_CAN_CMD_SELF_REQ);
    467  1.1   bouyer 	} else {
    468  1.1   bouyer 		sunxi_can_write(sc, SUNXI_CAN_CMD_REG, SUNXI_CAN_CMD_TANS_REQ);
    469  1.1   bouyer 	}
    470  1.1   bouyer 	ifp->if_flags |= IFF_OACTIVE;
    471  1.1   bouyer 	ifp->if_timer = 5;
    472  1.1   bouyer 	can_bpf_mtap(ifp, m, 0);
    473  1.1   bouyer out:
    474  1.1   bouyer 	mutex_exit(&sc->sc_intr_lock);
    475  1.1   bouyer }
    476  1.1   bouyer 
    477  1.1   bouyer static int
    478  1.1   bouyer sunxi_can_ifup(struct sunxi_can_softc * const sc)
    479  1.1   bouyer {
    480  1.1   bouyer 	uint32_t reg;
    481  1.1   bouyer 
    482  1.1   bouyer 	/* setup timings and mode - has to be done in reset */
    483  1.1   bouyer 	reg = SUNXI_CAN_MODSEL_RST;
    484  1.1   bouyer 	if (sc->sc_linkmodes & CAN_LINKMODE_LISTENONLY)
    485  1.1   bouyer 		reg |= SUNXI_CAN_MODSEL_LST_ONLY;
    486  1.1   bouyer 
    487  1.1   bouyer 	if (sc->sc_linkmodes & CAN_LINKMODE_LOOPBACK)
    488  1.1   bouyer 		reg |= SUNXI_CAN_MODSEL_LB_MOD;
    489  1.1   bouyer 
    490  1.1   bouyer 	sunxi_can_write(sc, SUNXI_CAN_MODSEL_REG, reg);
    491  1.1   bouyer 
    492  1.1   bouyer 	reg = 0;
    493  1.1   bouyer 	if (sc->sc_timings.clt_prop != 0)
    494  1.1   bouyer 		return EINVAL;
    495  1.1   bouyer 
    496  1.1   bouyer 	if (sc->sc_timings.clt_brp > sc->sc_timecaps.cltc_brp_max ||
    497  1.1   bouyer 	   sc->sc_timings.clt_brp < sc->sc_timecaps.cltc_brp_min)
    498  1.1   bouyer 		return EINVAL;
    499  1.1   bouyer 	reg |= (sc->sc_timings.clt_brp - 1) << 0;
    500  1.1   bouyer 
    501  1.1   bouyer 	if (sc->sc_timings.clt_ps1 > sc->sc_timecaps.cltc_ps1_max ||
    502  1.1   bouyer 	   sc->sc_timings.clt_ps1 < sc->sc_timecaps.cltc_ps1_min)
    503  1.1   bouyer 		return EINVAL;
    504  1.1   bouyer 	reg |= (sc->sc_timings.clt_ps1 - 1) << 16;
    505  1.1   bouyer 
    506  1.1   bouyer 	if (sc->sc_timings.clt_ps2 > sc->sc_timecaps.cltc_ps2_max ||
    507  1.1   bouyer 	   sc->sc_timings.clt_ps2 < sc->sc_timecaps.cltc_ps2_min)
    508  1.1   bouyer 		return EINVAL;
    509  1.1   bouyer 	reg |= (sc->sc_timings.clt_ps2 - 1) << 20;
    510  1.1   bouyer 
    511  1.1   bouyer 	if (sc->sc_timings.clt_sjw > sc->sc_timecaps.cltc_sjw_max ||
    512  1.1   bouyer 	   sc->sc_timings.clt_sjw < 1)
    513  1.1   bouyer 		return EINVAL;
    514  1.1   bouyer 	reg |= (sc->sc_timings.clt_sjw - 1) << 14;
    515  1.1   bouyer 
    516  1.1   bouyer 	if (sc->sc_linkmodes & CAN_LINKMODE_3SAMPLES)
    517  1.1   bouyer 		reg |= SUNXI_CAN_BUS_TIME_SAM;
    518  1.1   bouyer 
    519  1.1   bouyer 	sunxi_can_write(sc, SUNXI_CAN_BUS_TIME_REG, reg);
    520  1.1   bouyer 
    521  1.1   bouyer 	/* set filters to accept all frames */
    522  1.1   bouyer 	sunxi_can_write(sc, SUNXI_CAN_ACPC, 0x00000000);
    523  1.1   bouyer 	sunxi_can_write(sc, SUNXI_CAN_ACPM, 0xffffffff);
    524  1.1   bouyer 
    525  1.1   bouyer 	/* clear errors counter */
    526  1.1   bouyer 	sunxi_can_write(sc, SUNXI_CAN_REC_REG, 0);
    527  1.1   bouyer 
    528  1.1   bouyer 	/* leave reset mode and enable interrupts */
    529  1.1   bouyer 	sunxi_can_exit_reset(sc);
    530  1.1   bouyer 	sunxi_can_write(sc, SUNXI_CAN_INTE_REG,
    531  1.1   bouyer 	    SUNXI_CAN_INT_TX_FLAG | SUNXI_CAN_INT_RX_FLAG | SUNXI_CAN_INT_ALLERRS);
    532  1.1   bouyer 	sc->sc_ifp->if_flags |= IFF_RUNNING;
    533  1.1   bouyer 	return 0;
    534  1.1   bouyer }
    535  1.1   bouyer 
    536  1.1   bouyer static void
    537  1.1   bouyer sunxi_can_ifdown(struct sunxi_can_softc * const sc)
    538  1.1   bouyer {
    539  1.1   bouyer 	sc->sc_ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    540  1.1   bouyer 	sc->sc_ifp->if_timer = 0;
    541  1.1   bouyer 	sunxi_can_enter_reset(sc);
    542  1.1   bouyer 	sunxi_can_write(sc, SUNXI_CAN_INTE_REG, 0);
    543  1.1   bouyer 	sunxi_can_write(sc, SUNXI_CAN_INT_REG,
    544  1.1   bouyer 	    sunxi_can_read(sc, SUNXI_CAN_INT_REG));
    545  1.1   bouyer }
    546  1.1   bouyer 
    547  1.1   bouyer static int
    548  1.1   bouyer sunxi_can_ifioctl(struct ifnet *ifp, u_long cmd, void *data)
    549  1.1   bouyer {
    550  1.1   bouyer 	struct sunxi_can_softc * const sc = ifp->if_softc;
    551  1.1   bouyer 	struct ifreq *ifr = (struct ifreq *)data;
    552  1.1   bouyer 	int error = 0;
    553  1.1   bouyer 
    554  1.1   bouyer 	mutex_enter(&sc->sc_intr_lock);
    555  1.1   bouyer 
    556  1.1   bouyer 	switch (cmd) {
    557  1.1   bouyer 	case SIOCINITIFADDR:
    558  1.1   bouyer 		error = EAFNOSUPPORT;
    559  1.1   bouyer 		break;
    560  1.1   bouyer 	case SIOCSIFMTU:
    561  1.1   bouyer 		if ((unsigned)ifr->ifr_mtu != sizeof(struct can_frame))
    562  1.1   bouyer 			error = EINVAL;
    563  1.1   bouyer 		break;
    564  1.1   bouyer 	case SIOCADDMULTI:
    565  1.1   bouyer 	case SIOCDELMULTI:
    566  1.1   bouyer 		error = EAFNOSUPPORT;
    567  1.1   bouyer 		break;
    568  1.1   bouyer 	default:
    569  1.1   bouyer 		error = ifioctl_common(ifp, cmd, data);
    570  1.1   bouyer 		if (error == 0) {
    571  1.1   bouyer 			if ((ifp->if_flags & IFF_UP) != 0 &&
    572  1.1   bouyer 			    (ifp->if_flags & IFF_RUNNING) == 0) {
    573  1.1   bouyer 				error = sunxi_can_ifup(sc);
    574  1.1   bouyer 				if (error) {
    575  1.1   bouyer 					ifp->if_flags &= ~IFF_UP;
    576  1.1   bouyer 				}
    577  1.1   bouyer 			} else if ((ifp->if_flags & IFF_UP) == 0 &&
    578  1.1   bouyer 			    (ifp->if_flags & IFF_RUNNING) != 0) {
    579  1.1   bouyer 				sunxi_can_ifdown(sc);
    580  1.1   bouyer 			}
    581  1.1   bouyer 		}
    582  1.1   bouyer 		break;
    583  1.1   bouyer 	}
    584  1.1   bouyer 
    585  1.1   bouyer 	mutex_exit(&sc->sc_intr_lock);
    586  1.1   bouyer 	return error;
    587  1.1   bouyer }
    588  1.1   bouyer 
    589  1.1   bouyer void
    590  1.1   bouyer sunxi_can_ifwatchdog(struct ifnet *ifp)
    591  1.1   bouyer {
    592  1.1   bouyer 	struct sunxi_can_softc * const sc = ifp->if_softc;
    593  1.1   bouyer 	printf("%s: watchdog timeout\n", device_xname(sc->sc_dev));
    594  1.1   bouyer 
    595  1.1   bouyer 	mutex_enter(&sc->sc_intr_lock);
    596  1.1   bouyer 	printf("irq 0x%x en 0x%x mode 0x%x status 0x%x timings 0x%x err 0x%x\n",
    597  1.1   bouyer 	    sunxi_can_read(sc, SUNXI_CAN_INT_REG),
    598  1.1   bouyer 	    sunxi_can_read(sc, SUNXI_CAN_INTE_REG),
    599  1.1   bouyer 	    sunxi_can_read(sc, SUNXI_CAN_MODSEL_REG),
    600  1.1   bouyer 	    sunxi_can_read(sc, SUNXI_CAN_STA_REG),
    601  1.1   bouyer 	    sunxi_can_read(sc, SUNXI_CAN_BUS_TIME_REG),
    602  1.1   bouyer 	    sunxi_can_read(sc, SUNXI_CAN_REC_REG));
    603  1.1   bouyer 	/* if there is a transmit in progress abort */
    604  1.1   bouyer 	if (sunxi_can_tx_abort(sc)) {
    605  1.3  thorpej 		if_statinc(ifp, if_oerrors);
    606  1.1   bouyer 	}
    607  1.1   bouyer 	mutex_exit(&sc->sc_intr_lock);
    608  1.1   bouyer }
    609  1.1   bouyer 
    610  1.1   bouyer static void
    611  1.1   bouyer sunxi_can_enter_reset(struct sunxi_can_softc *sc)
    612  1.1   bouyer {
    613  1.1   bouyer 	int i;
    614  1.1   bouyer 	uint32_t val;
    615  1.1   bouyer 
    616  1.1   bouyer 	for (i = 0; i < 1000; i++) {
    617  1.1   bouyer 		val = sunxi_can_read(sc, SUNXI_CAN_MODSEL_REG);
    618  1.1   bouyer 		val |= SUNXI_CAN_MODSEL_RST;
    619  1.1   bouyer 		sunxi_can_write(sc, SUNXI_CAN_MODSEL_REG, val);
    620  1.1   bouyer 		val = sunxi_can_read(sc, SUNXI_CAN_MODSEL_REG);
    621  1.1   bouyer 		if (val & SUNXI_CAN_MODSEL_RST)
    622  1.1   bouyer 			return;
    623  1.1   bouyer 	}
    624  1.1   bouyer 	printf("%s: couldn't enter reset mode\n", device_xname(sc->sc_dev));
    625  1.1   bouyer }
    626  1.1   bouyer 
    627  1.1   bouyer static void
    628  1.1   bouyer sunxi_can_exit_reset(struct sunxi_can_softc *sc)
    629  1.1   bouyer {
    630  1.1   bouyer 	int i;
    631  1.1   bouyer 	uint32_t val;
    632  1.1   bouyer 
    633  1.1   bouyer 	for (i = 0; i < 1000; i++) {
    634  1.1   bouyer 		val = sunxi_can_read(sc, SUNXI_CAN_MODSEL_REG);
    635  1.1   bouyer 		val &= ~SUNXI_CAN_MODSEL_RST;
    636  1.1   bouyer 		sunxi_can_write(sc, SUNXI_CAN_MODSEL_REG, val);
    637  1.1   bouyer 		val = sunxi_can_read(sc, SUNXI_CAN_MODSEL_REG);
    638  1.1   bouyer 		if ((val & SUNXI_CAN_MODSEL_RST) == 0)
    639  1.1   bouyer 			return;
    640  1.1   bouyer 	}
    641  1.1   bouyer 	printf("%s: couldn't leave reset mode\n", device_xname(sc->sc_dev));
    642  1.1   bouyer }
    643