1*4941e274Syamt /* $NetBSD: sunxi_can.c,v 1.13 2022/11/19 09:05:42 yamt Exp $ */
26bdb7968Sbouyer
36bdb7968Sbouyer /*-
46bdb7968Sbouyer * Copyright (c) 2017,2018 The NetBSD Foundation, Inc.
56bdb7968Sbouyer * All rights reserved.
66bdb7968Sbouyer *
76bdb7968Sbouyer * This code is derived from software contributed to The NetBSD Foundation
86bdb7968Sbouyer * by Manuel Bouyer.
96bdb7968Sbouyer *
106bdb7968Sbouyer * Redistribution and use in source and binary forms, with or without
116bdb7968Sbouyer * modification, are permitted provided that the following conditions
126bdb7968Sbouyer * are met:
136bdb7968Sbouyer * 1. Redistributions of source code must retain the above copyright
146bdb7968Sbouyer * notice, this list of conditions and the following disclaimer.
156bdb7968Sbouyer * 2. Redistributions in binary form must reproduce the above copyright
166bdb7968Sbouyer * notice, this list of conditions and the following disclaimer in the
176bdb7968Sbouyer * documentation and/or other materials provided with the distribution.
186bdb7968Sbouyer *
196bdb7968Sbouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
206bdb7968Sbouyer * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
216bdb7968Sbouyer * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
226bdb7968Sbouyer * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
236bdb7968Sbouyer * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
246bdb7968Sbouyer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
256bdb7968Sbouyer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
266bdb7968Sbouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
276bdb7968Sbouyer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
286bdb7968Sbouyer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
296bdb7968Sbouyer * POSSIBILITY OF SUCH DAMAGE.
306bdb7968Sbouyer */
316bdb7968Sbouyer
326bdb7968Sbouyer
336bdb7968Sbouyer #include "locators.h"
346bdb7968Sbouyer #include "opt_can.h"
356bdb7968Sbouyer
366bdb7968Sbouyer
376bdb7968Sbouyer #include <sys/cdefs.h>
386bdb7968Sbouyer
39*4941e274Syamt __KERNEL_RCSID(1, "$NetBSD: sunxi_can.c,v 1.13 2022/11/19 09:05:42 yamt Exp $");
406bdb7968Sbouyer
416bdb7968Sbouyer #include <sys/param.h>
426bdb7968Sbouyer #include <sys/bus.h>
436bdb7968Sbouyer #include <sys/device.h>
446bdb7968Sbouyer #include <sys/intr.h>
456bdb7968Sbouyer #include <sys/ioctl.h>
46*4941e274Syamt #include <sys/kmem.h>
476bdb7968Sbouyer #include <sys/mutex.h>
486bdb7968Sbouyer #include <sys/rndsource.h>
496bdb7968Sbouyer #include <sys/mbuf.h>
506bdb7968Sbouyer #include <sys/systm.h>
516bdb7968Sbouyer
526bdb7968Sbouyer #include <net/if.h>
536bdb7968Sbouyer #include <net/if_types.h>
546bdb7968Sbouyer #include <net/bpf.h>
556bdb7968Sbouyer
566bdb7968Sbouyer #ifdef CAN
576bdb7968Sbouyer #include <netcan/can.h>
586bdb7968Sbouyer #include <netcan/can_var.h>
596bdb7968Sbouyer #endif
606bdb7968Sbouyer
616bdb7968Sbouyer #include <dev/fdt/fdtvar.h>
626bdb7968Sbouyer
636bdb7968Sbouyer #include <arm/sunxi/sunxi_can.h>
646bdb7968Sbouyer
656bdb7968Sbouyer /* shortcut for all error interrupts */
666bdb7968Sbouyer #define SUNXI_CAN_INT_ALLERRS (\
676bdb7968Sbouyer SUNXI_CAN_INT_BERR | \
686bdb7968Sbouyer SUNXI_CAN_INT_ARB_LOST | \
696bdb7968Sbouyer SUNXI_CAN_INT_ERR_PASSIVE | \
706bdb7968Sbouyer SUNXI_CAN_INT_DATA_OR | \
716bdb7968Sbouyer SUNXI_CAN_INT_ERR \
726bdb7968Sbouyer )
736bdb7968Sbouyer
746bdb7968Sbouyer struct sunxi_can_softc {
756bdb7968Sbouyer struct canif_softc sc_cansc;
766bdb7968Sbouyer bus_space_tag_t sc_bst;
776bdb7968Sbouyer bus_space_handle_t sc_bsh;
786bdb7968Sbouyer kmutex_t sc_intr_lock;
796bdb7968Sbouyer void *sc_ih;
806bdb7968Sbouyer struct ifnet *sc_ifp;
816bdb7968Sbouyer krndsource_t sc_rnd_source; /* random source */
826bdb7968Sbouyer struct mbuf *sc_m_transmit; /* mbuf being transmitted */
836bdb7968Sbouyer };
846bdb7968Sbouyer #define sc_dev sc_cansc.csc_dev
856bdb7968Sbouyer #define sc_timecaps sc_cansc.csc_timecaps
866bdb7968Sbouyer #define sc_timings sc_cansc.csc_timings
876bdb7968Sbouyer #define sc_linkmodes sc_cansc.csc_linkmodes
886bdb7968Sbouyer
89646c0f59Sthorpej static const struct device_compatible_entry compat_data[] = {
90646c0f59Sthorpej { .compat = "allwinner,sun4i-a10-can" },
91ec189949Sthorpej DEVICE_COMPAT_EOL
926bdb7968Sbouyer };
936bdb7968Sbouyer
946bdb7968Sbouyer static int sunxi_can_match(device_t, cfdata_t, void *);
956bdb7968Sbouyer static void sunxi_can_attach(device_t, device_t, void *);
966bdb7968Sbouyer
976bdb7968Sbouyer static int sunxi_can_intr(void *);
986bdb7968Sbouyer
996bdb7968Sbouyer static void sunxi_can_ifstart(struct ifnet *);
1006bdb7968Sbouyer static int sunxi_can_ifioctl(struct ifnet *, u_long, void *);
1016bdb7968Sbouyer static void sunxi_can_ifwatchdog(struct ifnet *);
1026bdb7968Sbouyer
1036bdb7968Sbouyer static void sunxi_can_enter_reset(struct sunxi_can_softc *);
1046bdb7968Sbouyer static void sunxi_can_exit_reset(struct sunxi_can_softc *);
105b2461922Sbouyer static void sunxi_can_ifdown(struct sunxi_can_softc * const);
106b2461922Sbouyer static int sunxi_can_ifup(struct sunxi_can_softc * const);
1076bdb7968Sbouyer
1086bdb7968Sbouyer CFATTACH_DECL_NEW(sunxi_can, sizeof(struct sunxi_can_softc),
1096bdb7968Sbouyer sunxi_can_match, sunxi_can_attach, NULL, NULL);
1106bdb7968Sbouyer
1116bdb7968Sbouyer static inline uint32_t
sunxi_can_read(struct sunxi_can_softc * sc,bus_size_t o)1126bdb7968Sbouyer sunxi_can_read(struct sunxi_can_softc *sc, bus_size_t o)
1136bdb7968Sbouyer {
1146bdb7968Sbouyer return bus_space_read_4(sc->sc_bst, sc->sc_bsh, o);
1156bdb7968Sbouyer }
1166bdb7968Sbouyer
1176bdb7968Sbouyer static inline void
sunxi_can_write(struct sunxi_can_softc * sc,bus_size_t o,uint32_t v)1186bdb7968Sbouyer sunxi_can_write(struct sunxi_can_softc *sc, bus_size_t o, uint32_t v)
1196bdb7968Sbouyer {
1206bdb7968Sbouyer return bus_space_write_4(sc->sc_bst, sc->sc_bsh, o, v);
1216bdb7968Sbouyer }
1226bdb7968Sbouyer
1236bdb7968Sbouyer static int
sunxi_can_match(device_t parent,cfdata_t cf,void * aux)1246bdb7968Sbouyer sunxi_can_match(device_t parent, cfdata_t cf, void *aux)
1256bdb7968Sbouyer {
1266bdb7968Sbouyer struct fdt_attach_args * const faa = aux;
1276bdb7968Sbouyer
1286e54367aSthorpej return of_compatible_match(faa->faa_phandle, compat_data);
1296bdb7968Sbouyer }
1306bdb7968Sbouyer
1316bdb7968Sbouyer static void
sunxi_can_attach(device_t parent,device_t self,void * aux)1326bdb7968Sbouyer sunxi_can_attach(device_t parent, device_t self, void *aux)
1336bdb7968Sbouyer {
1346bdb7968Sbouyer struct sunxi_can_softc * const sc = device_private(self);
1356bdb7968Sbouyer struct fdt_attach_args * const faa = aux;
1366bdb7968Sbouyer struct ifnet *ifp;
1376bdb7968Sbouyer const int phandle = faa->faa_phandle;
1386bdb7968Sbouyer bus_addr_t addr;
1396bdb7968Sbouyer bus_size_t size;
1406bdb7968Sbouyer char intrstr[128];
1416bdb7968Sbouyer struct clk *clk;
1426bdb7968Sbouyer struct fdtbus_reset *rst;
1436bdb7968Sbouyer
1446bdb7968Sbouyer sc->sc_dev = self;
1456bdb7968Sbouyer mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_NET);
1466bdb7968Sbouyer
1476bdb7968Sbouyer sc->sc_bst = faa->faa_bst;
1486bdb7968Sbouyer if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
1496bdb7968Sbouyer aprint_error(": couldn't get registers\n");
1506bdb7968Sbouyer return;
1516bdb7968Sbouyer }
1526bdb7968Sbouyer
1536bdb7968Sbouyer if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
1546bdb7968Sbouyer aprint_error(": couldn't map registers\n");
1556bdb7968Sbouyer return;
1566bdb7968Sbouyer }
1576bdb7968Sbouyer
1586bdb7968Sbouyer if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
1596bdb7968Sbouyer aprint_error(": failed to decode interrupt\n");
1606bdb7968Sbouyer return;
1616bdb7968Sbouyer }
1626bdb7968Sbouyer
1636bdb7968Sbouyer if ((clk = fdtbus_clock_get_index(phandle, 0)) != NULL) {
1646bdb7968Sbouyer if (clk_enable(clk) != 0) {
1656bdb7968Sbouyer aprint_error(": couldn't enable clock\n");
1666bdb7968Sbouyer return;
1676bdb7968Sbouyer }
1686bdb7968Sbouyer }
1696bdb7968Sbouyer
1706bdb7968Sbouyer if ((rst = fdtbus_reset_get_index(phandle, 0)) != NULL) {
1716bdb7968Sbouyer if (fdtbus_reset_deassert(rst) != 0) {
1726bdb7968Sbouyer aprint_error(": couldn't de-assert reset\n");
1736bdb7968Sbouyer return;
1746bdb7968Sbouyer }
1756bdb7968Sbouyer }
1766bdb7968Sbouyer
1776bdb7968Sbouyer sc->sc_timecaps.cltc_prop_min = 0;
1786bdb7968Sbouyer sc->sc_timecaps.cltc_prop_max = 0;
1796bdb7968Sbouyer sc->sc_timecaps.cltc_ps1_min = 1;
1806bdb7968Sbouyer sc->sc_timecaps.cltc_ps1_max = 16;
1816bdb7968Sbouyer sc->sc_timecaps.cltc_ps2_min = 1;
1826bdb7968Sbouyer sc->sc_timecaps.cltc_ps2_max = 8;
1836bdb7968Sbouyer sc->sc_timecaps.cltc_sjw_max = 4;
1846bdb7968Sbouyer sc->sc_timecaps.cltc_brp_min = 1;
1856bdb7968Sbouyer sc->sc_timecaps.cltc_brp_max = 64;
1866bdb7968Sbouyer sc->sc_timecaps.cltc_brp_inc = 1;
1876bdb7968Sbouyer sc->sc_timecaps.cltc_clock_freq = clk_get_rate(clk);
1886bdb7968Sbouyer sc->sc_timecaps.cltc_linkmode_caps =
1896bdb7968Sbouyer CAN_LINKMODE_3SAMPLES | CAN_LINKMODE_LISTENONLY |
1906bdb7968Sbouyer CAN_LINKMODE_LOOPBACK;
1916bdb7968Sbouyer can_ifinit_timings(&sc->sc_cansc);
1926bdb7968Sbouyer sc->sc_timings.clt_prop = 0;
1936bdb7968Sbouyer sc->sc_timings.clt_sjw = 1;
1946bdb7968Sbouyer
1956bdb7968Sbouyer aprint_naive("\n");
1966bdb7968Sbouyer aprint_normal(": CAN bus controller\n");
1976bdb7968Sbouyer aprint_debug_dev(self, ": clock freq %d\n",
1986bdb7968Sbouyer sc->sc_timecaps.cltc_clock_freq);
1996bdb7968Sbouyer
2006bdb7968Sbouyer sunxi_can_enter_reset(sc);
2016bdb7968Sbouyer /*
2026bdb7968Sbouyer * Disable and then clear all interrupts
2036bdb7968Sbouyer */
2046bdb7968Sbouyer sunxi_can_write(sc, SUNXI_CAN_INTE_REG, 0);
2056bdb7968Sbouyer sunxi_can_write(sc, SUNXI_CAN_INT_REG,
2066bdb7968Sbouyer sunxi_can_read(sc, SUNXI_CAN_INT_REG));
2076bdb7968Sbouyer
208076a1169Sjmcneill sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_NET, 0,
209076a1169Sjmcneill sunxi_can_intr, sc, device_xname(self));
2106bdb7968Sbouyer if (sc->sc_ih == NULL) {
2116bdb7968Sbouyer aprint_error_dev(self, "failed to establish interrupt on %s\n",
2126bdb7968Sbouyer intrstr);
2136bdb7968Sbouyer return;
2146bdb7968Sbouyer }
2156bdb7968Sbouyer aprint_normal_dev(self, "interrupting on %s\n", intrstr);
2166bdb7968Sbouyer
2176bdb7968Sbouyer ifp = if_alloc(IFT_OTHER);
2186bdb7968Sbouyer sc->sc_ifp = ifp;
2196bdb7968Sbouyer strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
2206bdb7968Sbouyer ifp->if_softc = sc;
2216bdb7968Sbouyer ifp->if_capabilities = 0;
2226bdb7968Sbouyer ifp->if_flags = 0;
2236bdb7968Sbouyer ifp->if_start = sunxi_can_ifstart;
2246bdb7968Sbouyer ifp->if_ioctl = sunxi_can_ifioctl;
2256bdb7968Sbouyer ifp->if_watchdog = sunxi_can_ifwatchdog;
2266bdb7968Sbouyer
2276bdb7968Sbouyer /*
2286bdb7968Sbouyer * Attach the interface.
2296bdb7968Sbouyer */
2306bdb7968Sbouyer can_ifattach(ifp);
2316bdb7968Sbouyer if_deferred_start_init(ifp, NULL);
2326bdb7968Sbouyer bpf_mtap_softint_init(ifp);
2336bdb7968Sbouyer rnd_attach_source(&sc->sc_rnd_source, device_xname(self),
2346bdb7968Sbouyer RND_TYPE_NET, RND_FLAG_DEFAULT);
2356bdb7968Sbouyer #ifdef MBUFTRACE
236ac7141acSskrll ifp->if_mowner = kmem_zalloc(sizeof(*ifp->if_mowner), KM_SLEEP);
2376bdb7968Sbouyer strlcpy(ifp->if_mowner->mo_name, ifp->if_xname,
2386bdb7968Sbouyer sizeof(ifp->if_mowner->mo_name));
2396bdb7968Sbouyer MOWNER_ATTACH(ifp->if_mowner);
2406bdb7968Sbouyer #endif
2416bdb7968Sbouyer }
2426bdb7968Sbouyer
2436bdb7968Sbouyer static void
sunxi_can_rx_intr(struct sunxi_can_softc * sc)2446bdb7968Sbouyer sunxi_can_rx_intr(struct sunxi_can_softc *sc)
2456bdb7968Sbouyer {
2466bdb7968Sbouyer uint32_t reg0v;
2476bdb7968Sbouyer struct mbuf *m;
2486bdb7968Sbouyer struct ifnet *ifp = sc->sc_ifp;
2496bdb7968Sbouyer struct can_frame *cf;
2506bdb7968Sbouyer int dlc;
2516bdb7968Sbouyer int regd, i;
2526bdb7968Sbouyer
2536bdb7968Sbouyer KASSERT(mutex_owned(&sc->sc_intr_lock));
2546bdb7968Sbouyer reg0v = sunxi_can_read(sc, SUNXI_CAN_TXBUF0_REG);
2556bdb7968Sbouyer dlc = reg0v & SUNXI_CAN_TXBUF0_DL;
2566bdb7968Sbouyer
2576bdb7968Sbouyer if (dlc > CAN_MAX_DLC) {
258d18dc548Sthorpej if_statinc(ifp, if_ierrors);
2596bdb7968Sbouyer sunxi_can_write(sc, SUNXI_CAN_CMD_REG, SUNXI_CAN_CMD_REL_RX_BUF);
2606bdb7968Sbouyer return;
2616bdb7968Sbouyer }
2626bdb7968Sbouyer
2636bdb7968Sbouyer m = m_gethdr(M_NOWAIT, MT_HEADER);
2646bdb7968Sbouyer if (m == NULL) {
265d18dc548Sthorpej if_statinc(ifp, if_ierrors);
2666bdb7968Sbouyer sunxi_can_write(sc, SUNXI_CAN_CMD_REG, SUNXI_CAN_CMD_REL_RX_BUF);
2676bdb7968Sbouyer return;
2686bdb7968Sbouyer }
2696bdb7968Sbouyer cf = mtod(m, struct can_frame *);
2706bdb7968Sbouyer memset(cf, 0, sizeof(struct can_frame));
2716bdb7968Sbouyer
2726bdb7968Sbouyer cf->can_dlc = dlc;
2736bdb7968Sbouyer
2746bdb7968Sbouyer if (reg0v & SUNXI_CAN_TXBUF0_EFF) {
2756bdb7968Sbouyer cf->can_id =
2766bdb7968Sbouyer (sunxi_can_read(sc, SUNXI_CAN_TXBUF1_REG) << 21) |
2776bdb7968Sbouyer (sunxi_can_read(sc, SUNXI_CAN_TXBUF2_REG) << 13) |
2786bdb7968Sbouyer (sunxi_can_read(sc, SUNXI_CAN_TXBUF3_REG) << 5) |
2796bdb7968Sbouyer ((sunxi_can_read(sc, SUNXI_CAN_TXBUF4_REG) >> 3) & 0x1f);
2806bdb7968Sbouyer cf->can_id |= CAN_EFF_FLAG;
2816bdb7968Sbouyer regd = SUNXI_CAN_TXBUF5_REG;
2826bdb7968Sbouyer } else {
2836bdb7968Sbouyer cf->can_id =
2846bdb7968Sbouyer (sunxi_can_read(sc, SUNXI_CAN_TXBUF1_REG) << 3) |
2856bdb7968Sbouyer ((sunxi_can_read(sc, SUNXI_CAN_TXBUF2_REG) << 5) & 0x7);
2866bdb7968Sbouyer regd = SUNXI_CAN_TXBUF3_REG;
2876bdb7968Sbouyer }
2886bdb7968Sbouyer if (reg0v & SUNXI_CAN_TXBUF0_RTR) {
2896bdb7968Sbouyer cf->can_id |= CAN_RTR_FLAG;
2906bdb7968Sbouyer } else {
2916bdb7968Sbouyer for (i = 0; i < cf->can_dlc; i++) {
2926bdb7968Sbouyer cf->data[i] = sunxi_can_read(sc, regd + i * 4);
2936bdb7968Sbouyer }
2946bdb7968Sbouyer }
2956bdb7968Sbouyer sunxi_can_write(sc, SUNXI_CAN_CMD_REG, SUNXI_CAN_CMD_REL_RX_BUF);
2966bdb7968Sbouyer m->m_len = m->m_pkthdr.len = CAN_MTU;
297d18dc548Sthorpej if_statadd(ifp, if_ibytes, m->m_len);
2986bdb7968Sbouyer m_set_rcvif(m, ifp);
2996bdb7968Sbouyer can_bpf_mtap(ifp, m, 1);
3006bdb7968Sbouyer can_input(ifp, m);
3016bdb7968Sbouyer }
3026bdb7968Sbouyer
3036bdb7968Sbouyer static void
sunxi_can_tx_intr(struct sunxi_can_softc * sc)3046bdb7968Sbouyer sunxi_can_tx_intr(struct sunxi_can_softc *sc)
3056bdb7968Sbouyer {
3066bdb7968Sbouyer struct ifnet * const ifp = sc->sc_ifp;
3076bdb7968Sbouyer struct mbuf *m;
3086bdb7968Sbouyer
3096bdb7968Sbouyer KASSERT(mutex_owned(&sc->sc_intr_lock));
3106bdb7968Sbouyer if ((m = sc->sc_m_transmit) != NULL) {
311d18dc548Sthorpej if_statadd2(ifp, if_obytes, m->m_len, if_opackets, 1);
3126bdb7968Sbouyer can_mbuf_tag_clean(m);
3136bdb7968Sbouyer m_set_rcvif(m, ifp);
3146bdb7968Sbouyer can_input(ifp, m); /* loopback */
3156bdb7968Sbouyer sc->sc_m_transmit = NULL;
3166bdb7968Sbouyer ifp->if_timer = 0;
3176bdb7968Sbouyer }
3186bdb7968Sbouyer if_schedule_deferred_start(ifp);
3196bdb7968Sbouyer }
3206bdb7968Sbouyer
3216bdb7968Sbouyer static int
sunxi_can_tx_abort(struct sunxi_can_softc * sc)3226bdb7968Sbouyer sunxi_can_tx_abort(struct sunxi_can_softc *sc)
3236bdb7968Sbouyer {
3246bdb7968Sbouyer KASSERT(mutex_owned(&sc->sc_intr_lock));
3256bdb7968Sbouyer if (sc->sc_m_transmit) {
3266bdb7968Sbouyer m_freem(sc->sc_m_transmit);
3276bdb7968Sbouyer sc->sc_m_transmit = NULL;
3286bdb7968Sbouyer sc->sc_ifp->if_timer = 0;
3296bdb7968Sbouyer /*
3306bdb7968Sbouyer * the transmit abort will trigger a TX interrupt
331137925beSthorpej * which will restart the queue as appropriate.
3326bdb7968Sbouyer */
3336bdb7968Sbouyer sunxi_can_write(sc, SUNXI_CAN_CMD_REG, SUNXI_CAN_CMD_ABT_REQ);
3346bdb7968Sbouyer return 1;
3356bdb7968Sbouyer }
3366bdb7968Sbouyer return 0;
3376bdb7968Sbouyer }
3386bdb7968Sbouyer
3396bdb7968Sbouyer static void
sunxi_can_err_intr(struct sunxi_can_softc * sc,uint32_t irq,uint32_t sts)3406bdb7968Sbouyer sunxi_can_err_intr(struct sunxi_can_softc *sc, uint32_t irq, uint32_t sts)
3416bdb7968Sbouyer {
3426bdb7968Sbouyer struct ifnet * const ifp = sc->sc_ifp;
3436bdb7968Sbouyer KASSERT(mutex_owned(&sc->sc_intr_lock));
3446bdb7968Sbouyer int txerr = 0;
3456bdb7968Sbouyer uint32_t reg;
3466bdb7968Sbouyer
3476bdb7968Sbouyer if (irq & SUNXI_CAN_INT_DATA_OR) {
348d18dc548Sthorpej if_statinc(ifp, if_ierrors);
349b2461922Sbouyer sunxi_can_ifdown(sc);
3506bdb7968Sbouyer sunxi_can_write(sc, SUNXI_CAN_CMD_REG, SUNXI_CAN_CMD_CLR_OR);
351b2461922Sbouyer sunxi_can_ifup(sc);
3526bdb7968Sbouyer }
3536bdb7968Sbouyer if (irq & SUNXI_CAN_INT_ERR) {
3546bdb7968Sbouyer reg = sunxi_can_read(sc, SUNXI_CAN_REC_REG);
3556bdb7968Sbouyer printf("%s: ERR interrupt status 0x%x counters 0x%x\n",
3566bdb7968Sbouyer device_xname(sc->sc_dev), sts, reg);
3576bdb7968Sbouyer
3586bdb7968Sbouyer }
3596bdb7968Sbouyer if (irq & SUNXI_CAN_INT_BERR) {
3606bdb7968Sbouyer if (sts & SUNXI_CAN_STA_TX)
3616bdb7968Sbouyer txerr++;
3626bdb7968Sbouyer if (sts & SUNXI_CAN_STA_RX)
363d18dc548Sthorpej if_statinc(ifp, if_ierrors);
3646bdb7968Sbouyer }
3656bdb7968Sbouyer if (irq & SUNXI_CAN_INT_ERR_PASSIVE) {
3666bdb7968Sbouyer printf("%s: PASSV interrupt status 0x%x\n",
3676bdb7968Sbouyer device_xname(sc->sc_dev), sts);
3686bdb7968Sbouyer }
3696bdb7968Sbouyer if (irq & SUNXI_CAN_INT_ARB_LOST) {
3706bdb7968Sbouyer txerr++;
3716bdb7968Sbouyer }
3726bdb7968Sbouyer if (txerr) {
373d18dc548Sthorpej if_statadd(ifp, if_oerrors, txerr);
3746bdb7968Sbouyer (void) sunxi_can_tx_abort(sc);
3756bdb7968Sbouyer }
3766bdb7968Sbouyer }
3776bdb7968Sbouyer
3786bdb7968Sbouyer int
sunxi_can_intr(void * arg)3796bdb7968Sbouyer sunxi_can_intr(void *arg)
3806bdb7968Sbouyer {
3816bdb7968Sbouyer struct sunxi_can_softc * const sc = arg;
3826bdb7968Sbouyer int rv = 0;
3836bdb7968Sbouyer int irq;
3846bdb7968Sbouyer
3856bdb7968Sbouyer mutex_enter(&sc->sc_intr_lock);
3866bdb7968Sbouyer
3876bdb7968Sbouyer while ((irq = sunxi_can_read(sc, SUNXI_CAN_INT_REG)) != 0) {
3886bdb7968Sbouyer uint32_t sts = sunxi_can_read(sc, SUNXI_CAN_STA_REG);
3896bdb7968Sbouyer rv = 1;
390b2461922Sbouyer rnd_add_uint32(&sc->sc_rnd_source, irq);
3916bdb7968Sbouyer
39203f4ee15Sbouyer if ((irq & (SUNXI_CAN_INT_RX_FLAG | SUNXI_CAN_INT_DATA_OR)) ==
39303f4ee15Sbouyer SUNXI_CAN_INT_RX_FLAG) {
3946bdb7968Sbouyer while (sts & SUNXI_CAN_STA_RX_RDY) {
3956bdb7968Sbouyer sunxi_can_rx_intr(sc);
3966bdb7968Sbouyer sts = sunxi_can_read(sc, SUNXI_CAN_STA_REG);
3976bdb7968Sbouyer }
398b2461922Sbouyer /*
399b2461922Sbouyer * Don't write SUNXI_CAN_INT_RX_FLAG to the interrupt
400b2461922Sbouyer * register, this may clear the RX pending flag
401b2461922Sbouyer * while there is indeed a packet pending.
402b2461922Sbouyer * Reading packets should have cleared the RX interrupt,
403b2461922Sbouyer * so just restart the loop and re-read the interrupt
404b2461922Sbouyer * register. In the common case irq will now be 0.
405b2461922Sbouyer */
406b2461922Sbouyer continue;
407b2461922Sbouyer }
408b2461922Sbouyer if (irq & SUNXI_CAN_INT_TX_FLAG) {
409b2461922Sbouyer sunxi_can_tx_intr(sc);
4106bdb7968Sbouyer }
4116bdb7968Sbouyer if (irq & SUNXI_CAN_INT_ALLERRS) {
4126bdb7968Sbouyer sunxi_can_err_intr(sc, irq, sts);
4136bdb7968Sbouyer }
4146bdb7968Sbouyer sunxi_can_write(sc, SUNXI_CAN_INT_REG, irq);
4156bdb7968Sbouyer }
4166bdb7968Sbouyer mutex_exit(&sc->sc_intr_lock);
4176bdb7968Sbouyer
4186bdb7968Sbouyer return rv;
4196bdb7968Sbouyer }
4206bdb7968Sbouyer
4216bdb7968Sbouyer void
sunxi_can_ifstart(struct ifnet * ifp)4226bdb7968Sbouyer sunxi_can_ifstart(struct ifnet *ifp)
4236bdb7968Sbouyer {
4246bdb7968Sbouyer struct sunxi_can_softc * const sc = ifp->if_softc;
4256bdb7968Sbouyer struct mbuf *m;
4266bdb7968Sbouyer struct can_frame *cf;
4276bdb7968Sbouyer int regd;
4286bdb7968Sbouyer uint32_t reg0val;
4296bdb7968Sbouyer int i;
4306bdb7968Sbouyer
4316bdb7968Sbouyer mutex_enter(&sc->sc_intr_lock);
432137925beSthorpej if (sc->sc_m_transmit != NULL)
4336bdb7968Sbouyer goto out;
4346bdb7968Sbouyer
4356bdb7968Sbouyer IF_DEQUEUE(&ifp->if_snd, m);
4366bdb7968Sbouyer
4376bdb7968Sbouyer if (m == NULL)
4386bdb7968Sbouyer goto out;
4396bdb7968Sbouyer
4406bdb7968Sbouyer MCLAIM(m, ifp->if_mowner);
4416bdb7968Sbouyer sc->sc_m_transmit = m;
4426bdb7968Sbouyer
4436bdb7968Sbouyer KASSERT((m->m_flags & M_PKTHDR) != 0);
4446bdb7968Sbouyer KASSERT(m->m_len == m->m_pkthdr.len);
4456bdb7968Sbouyer
4466bdb7968Sbouyer cf = mtod(m, struct can_frame *);
4476bdb7968Sbouyer reg0val = cf->can_dlc & SUNXI_CAN_TXBUF0_DL;
4486bdb7968Sbouyer if (cf->can_id & CAN_RTR_FLAG)
4496bdb7968Sbouyer reg0val |= SUNXI_CAN_TXBUF0_RTR;
4506bdb7968Sbouyer
4516bdb7968Sbouyer if (cf->can_id & CAN_EFF_FLAG) {
4526bdb7968Sbouyer reg0val |= SUNXI_CAN_TXBUF0_EFF;
4536bdb7968Sbouyer sunxi_can_write(sc, SUNXI_CAN_TXBUF1_REG,
4546bdb7968Sbouyer (cf->can_id >> 21) & 0xff);
4556bdb7968Sbouyer sunxi_can_write(sc, SUNXI_CAN_TXBUF2_REG,
4566bdb7968Sbouyer (cf->can_id >> 13) & 0xff);
4576bdb7968Sbouyer sunxi_can_write(sc, SUNXI_CAN_TXBUF3_REG,
4586bdb7968Sbouyer (cf->can_id >> 5) & 0xff);
4596bdb7968Sbouyer sunxi_can_write(sc, SUNXI_CAN_TXBUF4_REG,
4606bdb7968Sbouyer (cf->can_id << 3) & 0xf8);
4616bdb7968Sbouyer regd = SUNXI_CAN_TXBUF5_REG;
4626bdb7968Sbouyer } else {
4636bdb7968Sbouyer sunxi_can_write(sc, SUNXI_CAN_TXBUF1_REG,
4646bdb7968Sbouyer (cf->can_id >> 3) & 0xff);
4656bdb7968Sbouyer sunxi_can_write(sc, SUNXI_CAN_TXBUF2_REG,
4666bdb7968Sbouyer (cf->can_id << 5) & 0xe0);
4676bdb7968Sbouyer regd = SUNXI_CAN_TXBUF3_REG;
4686bdb7968Sbouyer }
4696bdb7968Sbouyer
4706bdb7968Sbouyer for (i = 0; i < cf->can_dlc; i++) {
4716bdb7968Sbouyer sunxi_can_write(sc, regd + i * 4, cf->data[i]);
4726bdb7968Sbouyer }
4736bdb7968Sbouyer sunxi_can_write(sc, SUNXI_CAN_TXBUF0_REG, reg0val);
4746bdb7968Sbouyer
4756bdb7968Sbouyer if (sc->sc_linkmodes & CAN_LINKMODE_LOOPBACK) {
4766bdb7968Sbouyer sunxi_can_write(sc, SUNXI_CAN_CMD_REG,
4776bdb7968Sbouyer SUNXI_CAN_CMD_TANS_REQ | SUNXI_CAN_CMD_SELF_REQ);
4786bdb7968Sbouyer } else {
4796bdb7968Sbouyer sunxi_can_write(sc, SUNXI_CAN_CMD_REG, SUNXI_CAN_CMD_TANS_REQ);
4806bdb7968Sbouyer }
4816bdb7968Sbouyer ifp->if_timer = 5;
4826bdb7968Sbouyer can_bpf_mtap(ifp, m, 0);
4836bdb7968Sbouyer out:
4846bdb7968Sbouyer mutex_exit(&sc->sc_intr_lock);
4856bdb7968Sbouyer }
4866bdb7968Sbouyer
4876bdb7968Sbouyer static int
sunxi_can_ifup(struct sunxi_can_softc * const sc)4886bdb7968Sbouyer sunxi_can_ifup(struct sunxi_can_softc * const sc)
4896bdb7968Sbouyer {
4906bdb7968Sbouyer uint32_t reg;
4916bdb7968Sbouyer
4926bdb7968Sbouyer /* setup timings and mode - has to be done in reset */
4936bdb7968Sbouyer reg = SUNXI_CAN_MODSEL_RST;
4946bdb7968Sbouyer if (sc->sc_linkmodes & CAN_LINKMODE_LISTENONLY)
4956bdb7968Sbouyer reg |= SUNXI_CAN_MODSEL_LST_ONLY;
4966bdb7968Sbouyer
4976bdb7968Sbouyer if (sc->sc_linkmodes & CAN_LINKMODE_LOOPBACK)
4986bdb7968Sbouyer reg |= SUNXI_CAN_MODSEL_LB_MOD;
4996bdb7968Sbouyer
5006bdb7968Sbouyer sunxi_can_write(sc, SUNXI_CAN_MODSEL_REG, reg);
5016bdb7968Sbouyer
5026bdb7968Sbouyer reg = 0;
5036bdb7968Sbouyer if (sc->sc_timings.clt_prop != 0)
5046bdb7968Sbouyer return EINVAL;
5056bdb7968Sbouyer
5066bdb7968Sbouyer if (sc->sc_timings.clt_brp > sc->sc_timecaps.cltc_brp_max ||
5076bdb7968Sbouyer sc->sc_timings.clt_brp < sc->sc_timecaps.cltc_brp_min)
5086bdb7968Sbouyer return EINVAL;
5096bdb7968Sbouyer reg |= (sc->sc_timings.clt_brp - 1) << 0;
5106bdb7968Sbouyer
5116bdb7968Sbouyer if (sc->sc_timings.clt_ps1 > sc->sc_timecaps.cltc_ps1_max ||
5126bdb7968Sbouyer sc->sc_timings.clt_ps1 < sc->sc_timecaps.cltc_ps1_min)
5136bdb7968Sbouyer return EINVAL;
5146bdb7968Sbouyer reg |= (sc->sc_timings.clt_ps1 - 1) << 16;
5156bdb7968Sbouyer
5166bdb7968Sbouyer if (sc->sc_timings.clt_ps2 > sc->sc_timecaps.cltc_ps2_max ||
5176bdb7968Sbouyer sc->sc_timings.clt_ps2 < sc->sc_timecaps.cltc_ps2_min)
5186bdb7968Sbouyer return EINVAL;
5196bdb7968Sbouyer reg |= (sc->sc_timings.clt_ps2 - 1) << 20;
5206bdb7968Sbouyer
5216bdb7968Sbouyer if (sc->sc_timings.clt_sjw > sc->sc_timecaps.cltc_sjw_max ||
5226bdb7968Sbouyer sc->sc_timings.clt_sjw < 1)
5236bdb7968Sbouyer return EINVAL;
5246bdb7968Sbouyer reg |= (sc->sc_timings.clt_sjw - 1) << 14;
5256bdb7968Sbouyer
5266bdb7968Sbouyer if (sc->sc_linkmodes & CAN_LINKMODE_3SAMPLES)
5276bdb7968Sbouyer reg |= SUNXI_CAN_BUS_TIME_SAM;
5286bdb7968Sbouyer
5296bdb7968Sbouyer sunxi_can_write(sc, SUNXI_CAN_BUS_TIME_REG, reg);
5306bdb7968Sbouyer
5316bdb7968Sbouyer /* set filters to accept all frames */
5326bdb7968Sbouyer sunxi_can_write(sc, SUNXI_CAN_ACPC, 0x00000000);
5336bdb7968Sbouyer sunxi_can_write(sc, SUNXI_CAN_ACPM, 0xffffffff);
5346bdb7968Sbouyer
5356bdb7968Sbouyer /* clear errors counter */
5366bdb7968Sbouyer sunxi_can_write(sc, SUNXI_CAN_REC_REG, 0);
5376bdb7968Sbouyer
5386bdb7968Sbouyer /* leave reset mode and enable interrupts */
5396bdb7968Sbouyer sunxi_can_exit_reset(sc);
5406bdb7968Sbouyer sunxi_can_write(sc, SUNXI_CAN_INTE_REG,
5416bdb7968Sbouyer SUNXI_CAN_INT_TX_FLAG | SUNXI_CAN_INT_RX_FLAG | SUNXI_CAN_INT_ALLERRS);
5426bdb7968Sbouyer sc->sc_ifp->if_flags |= IFF_RUNNING;
5436bdb7968Sbouyer return 0;
5446bdb7968Sbouyer }
5456bdb7968Sbouyer
5466bdb7968Sbouyer static void
sunxi_can_ifdown(struct sunxi_can_softc * const sc)5476bdb7968Sbouyer sunxi_can_ifdown(struct sunxi_can_softc * const sc)
5486bdb7968Sbouyer {
549137925beSthorpej sc->sc_ifp->if_flags &= ~IFF_RUNNING;
5506bdb7968Sbouyer sc->sc_ifp->if_timer = 0;
5516bdb7968Sbouyer sunxi_can_enter_reset(sc);
5526bdb7968Sbouyer sunxi_can_write(sc, SUNXI_CAN_INTE_REG, 0);
5536bdb7968Sbouyer sunxi_can_write(sc, SUNXI_CAN_INT_REG,
5546bdb7968Sbouyer sunxi_can_read(sc, SUNXI_CAN_INT_REG));
5556bdb7968Sbouyer }
5566bdb7968Sbouyer
5576bdb7968Sbouyer static int
sunxi_can_ifioctl(struct ifnet * ifp,u_long cmd,void * data)5586bdb7968Sbouyer sunxi_can_ifioctl(struct ifnet *ifp, u_long cmd, void *data)
5596bdb7968Sbouyer {
5606bdb7968Sbouyer struct sunxi_can_softc * const sc = ifp->if_softc;
5616bdb7968Sbouyer struct ifreq *ifr = (struct ifreq *)data;
5626bdb7968Sbouyer int error = 0;
5636bdb7968Sbouyer
5646bdb7968Sbouyer mutex_enter(&sc->sc_intr_lock);
5656bdb7968Sbouyer
5666bdb7968Sbouyer switch (cmd) {
5676bdb7968Sbouyer case SIOCINITIFADDR:
5686bdb7968Sbouyer error = EAFNOSUPPORT;
5696bdb7968Sbouyer break;
5706bdb7968Sbouyer case SIOCSIFMTU:
5716bdb7968Sbouyer if ((unsigned)ifr->ifr_mtu != sizeof(struct can_frame))
5726bdb7968Sbouyer error = EINVAL;
5736bdb7968Sbouyer break;
5746bdb7968Sbouyer case SIOCADDMULTI:
5756bdb7968Sbouyer case SIOCDELMULTI:
5766bdb7968Sbouyer error = EAFNOSUPPORT;
5776bdb7968Sbouyer break;
5786bdb7968Sbouyer default:
5796bdb7968Sbouyer error = ifioctl_common(ifp, cmd, data);
5806bdb7968Sbouyer if (error == 0) {
5816bdb7968Sbouyer if ((ifp->if_flags & IFF_UP) != 0 &&
5826bdb7968Sbouyer (ifp->if_flags & IFF_RUNNING) == 0) {
5836bdb7968Sbouyer error = sunxi_can_ifup(sc);
5846bdb7968Sbouyer if (error) {
5856bdb7968Sbouyer ifp->if_flags &= ~IFF_UP;
5866bdb7968Sbouyer }
5876bdb7968Sbouyer } else if ((ifp->if_flags & IFF_UP) == 0 &&
5886bdb7968Sbouyer (ifp->if_flags & IFF_RUNNING) != 0) {
5896bdb7968Sbouyer sunxi_can_ifdown(sc);
5906bdb7968Sbouyer }
5916bdb7968Sbouyer }
5926bdb7968Sbouyer break;
5936bdb7968Sbouyer }
5946bdb7968Sbouyer
5956bdb7968Sbouyer mutex_exit(&sc->sc_intr_lock);
5966bdb7968Sbouyer return error;
5976bdb7968Sbouyer }
5986bdb7968Sbouyer
5996bdb7968Sbouyer void
sunxi_can_ifwatchdog(struct ifnet * ifp)6006bdb7968Sbouyer sunxi_can_ifwatchdog(struct ifnet *ifp)
6016bdb7968Sbouyer {
6026bdb7968Sbouyer struct sunxi_can_softc * const sc = ifp->if_softc;
6036bdb7968Sbouyer printf("%s: watchdog timeout\n", device_xname(sc->sc_dev));
6046bdb7968Sbouyer
6056bdb7968Sbouyer mutex_enter(&sc->sc_intr_lock);
6066bdb7968Sbouyer printf("irq 0x%x en 0x%x mode 0x%x status 0x%x timings 0x%x err 0x%x\n",
6076bdb7968Sbouyer sunxi_can_read(sc, SUNXI_CAN_INT_REG),
6086bdb7968Sbouyer sunxi_can_read(sc, SUNXI_CAN_INTE_REG),
6096bdb7968Sbouyer sunxi_can_read(sc, SUNXI_CAN_MODSEL_REG),
6106bdb7968Sbouyer sunxi_can_read(sc, SUNXI_CAN_STA_REG),
6116bdb7968Sbouyer sunxi_can_read(sc, SUNXI_CAN_BUS_TIME_REG),
6126bdb7968Sbouyer sunxi_can_read(sc, SUNXI_CAN_REC_REG));
6136bdb7968Sbouyer /* if there is a transmit in progress abort */
6146bdb7968Sbouyer if (sunxi_can_tx_abort(sc)) {
615d18dc548Sthorpej if_statinc(ifp, if_oerrors);
6166bdb7968Sbouyer }
6176bdb7968Sbouyer mutex_exit(&sc->sc_intr_lock);
6186bdb7968Sbouyer }
6196bdb7968Sbouyer
6206bdb7968Sbouyer static void
sunxi_can_enter_reset(struct sunxi_can_softc * sc)6216bdb7968Sbouyer sunxi_can_enter_reset(struct sunxi_can_softc *sc)
6226bdb7968Sbouyer {
6236bdb7968Sbouyer int i;
6246bdb7968Sbouyer uint32_t val;
6256bdb7968Sbouyer
6266bdb7968Sbouyer for (i = 0; i < 1000; i++) {
6276bdb7968Sbouyer val = sunxi_can_read(sc, SUNXI_CAN_MODSEL_REG);
6286bdb7968Sbouyer val |= SUNXI_CAN_MODSEL_RST;
6296bdb7968Sbouyer sunxi_can_write(sc, SUNXI_CAN_MODSEL_REG, val);
6306bdb7968Sbouyer val = sunxi_can_read(sc, SUNXI_CAN_MODSEL_REG);
6316bdb7968Sbouyer if (val & SUNXI_CAN_MODSEL_RST)
6326bdb7968Sbouyer return;
6336bdb7968Sbouyer }
6346bdb7968Sbouyer printf("%s: couldn't enter reset mode\n", device_xname(sc->sc_dev));
6356bdb7968Sbouyer }
6366bdb7968Sbouyer
6376bdb7968Sbouyer static void
sunxi_can_exit_reset(struct sunxi_can_softc * sc)6386bdb7968Sbouyer sunxi_can_exit_reset(struct sunxi_can_softc *sc)
6396bdb7968Sbouyer {
6406bdb7968Sbouyer int i;
6416bdb7968Sbouyer uint32_t val;
6426bdb7968Sbouyer
6436bdb7968Sbouyer for (i = 0; i < 1000; i++) {
6446bdb7968Sbouyer val = sunxi_can_read(sc, SUNXI_CAN_MODSEL_REG);
6456bdb7968Sbouyer val &= ~SUNXI_CAN_MODSEL_RST;
6466bdb7968Sbouyer sunxi_can_write(sc, SUNXI_CAN_MODSEL_REG, val);
6476bdb7968Sbouyer val = sunxi_can_read(sc, SUNXI_CAN_MODSEL_REG);
6486bdb7968Sbouyer if ((val & SUNXI_CAN_MODSEL_RST) == 0)
6496bdb7968Sbouyer return;
6506bdb7968Sbouyer }
6516bdb7968Sbouyer printf("%s: couldn't leave reset mode\n", device_xname(sc->sc_dev));
6526bdb7968Sbouyer }
653