sunxi_can.c revision 1.11 1 1.11 bouyer /* $NetBSD: sunxi_can.c,v 1.11 2022/09/21 20:21:16 bouyer 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.11 bouyer __KERNEL_RCSID(1, "$NetBSD: sunxi_can.c,v 1.11 2022/09/21 20:21:16 bouyer 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.1 bouyer ifp->if_mowner = malloc(sizeof(struct mowner), M_DEVBUF,
236 1.1 bouyer M_WAITOK | M_ZERO);
237 1.1 bouyer strlcpy(ifp->if_mowner->mo_name, ifp->if_xname,
238 1.1 bouyer sizeof(ifp->if_mowner->mo_name));
239 1.1 bouyer MOWNER_ATTACH(ifp->if_mowner);
240 1.1 bouyer #endif
241 1.1 bouyer }
242 1.1 bouyer
243 1.1 bouyer static void
244 1.1 bouyer sunxi_can_rx_intr(struct sunxi_can_softc *sc)
245 1.1 bouyer {
246 1.1 bouyer uint32_t reg0v;
247 1.1 bouyer struct mbuf *m;
248 1.1 bouyer struct ifnet *ifp = sc->sc_ifp;
249 1.1 bouyer struct can_frame *cf;
250 1.1 bouyer int dlc;
251 1.1 bouyer int regd, i;
252 1.1 bouyer
253 1.1 bouyer KASSERT(mutex_owned(&sc->sc_intr_lock));
254 1.1 bouyer reg0v = sunxi_can_read(sc, SUNXI_CAN_TXBUF0_REG);
255 1.1 bouyer dlc = reg0v & SUNXI_CAN_TXBUF0_DL;
256 1.1 bouyer
257 1.1 bouyer if (dlc > CAN_MAX_DLC) {
258 1.3 thorpej if_statinc(ifp, if_ierrors);
259 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_CMD_REG, SUNXI_CAN_CMD_REL_RX_BUF);
260 1.1 bouyer return;
261 1.1 bouyer }
262 1.1 bouyer
263 1.1 bouyer m = m_gethdr(M_NOWAIT, MT_HEADER);
264 1.1 bouyer if (m == NULL) {
265 1.3 thorpej if_statinc(ifp, if_ierrors);
266 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_CMD_REG, SUNXI_CAN_CMD_REL_RX_BUF);
267 1.1 bouyer return;
268 1.1 bouyer }
269 1.1 bouyer cf = mtod(m, struct can_frame *);
270 1.1 bouyer memset(cf, 0, sizeof(struct can_frame));
271 1.1 bouyer
272 1.1 bouyer cf->can_dlc = dlc;
273 1.1 bouyer
274 1.1 bouyer if (reg0v & SUNXI_CAN_TXBUF0_EFF) {
275 1.1 bouyer cf->can_id =
276 1.1 bouyer (sunxi_can_read(sc, SUNXI_CAN_TXBUF1_REG) << 21) |
277 1.1 bouyer (sunxi_can_read(sc, SUNXI_CAN_TXBUF2_REG) << 13) |
278 1.1 bouyer (sunxi_can_read(sc, SUNXI_CAN_TXBUF3_REG) << 5) |
279 1.1 bouyer ((sunxi_can_read(sc, SUNXI_CAN_TXBUF4_REG) >> 3) & 0x1f);
280 1.1 bouyer cf->can_id |= CAN_EFF_FLAG;
281 1.1 bouyer regd = SUNXI_CAN_TXBUF5_REG;
282 1.1 bouyer } else {
283 1.1 bouyer cf->can_id =
284 1.1 bouyer (sunxi_can_read(sc, SUNXI_CAN_TXBUF1_REG) << 3) |
285 1.1 bouyer ((sunxi_can_read(sc, SUNXI_CAN_TXBUF2_REG) << 5) & 0x7);
286 1.1 bouyer regd = SUNXI_CAN_TXBUF3_REG;
287 1.1 bouyer }
288 1.1 bouyer if (reg0v & SUNXI_CAN_TXBUF0_RTR) {
289 1.1 bouyer cf->can_id |= CAN_RTR_FLAG;
290 1.1 bouyer } else {
291 1.1 bouyer for (i = 0; i < cf->can_dlc; i++) {
292 1.1 bouyer cf->data[i] = sunxi_can_read(sc, regd + i * 4);
293 1.1 bouyer }
294 1.1 bouyer }
295 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_CMD_REG, SUNXI_CAN_CMD_REL_RX_BUF);
296 1.1 bouyer m->m_len = m->m_pkthdr.len = CAN_MTU;
297 1.3 thorpej if_statadd(ifp, if_ibytes, m->m_len);
298 1.1 bouyer m_set_rcvif(m, ifp);
299 1.1 bouyer can_bpf_mtap(ifp, m, 1);
300 1.1 bouyer can_input(ifp, m);
301 1.1 bouyer }
302 1.1 bouyer
303 1.1 bouyer static void
304 1.1 bouyer sunxi_can_tx_intr(struct sunxi_can_softc *sc)
305 1.1 bouyer {
306 1.1 bouyer struct ifnet * const ifp = sc->sc_ifp;
307 1.1 bouyer struct mbuf *m;
308 1.1 bouyer
309 1.1 bouyer KASSERT(mutex_owned(&sc->sc_intr_lock));
310 1.1 bouyer if ((m = sc->sc_m_transmit) != NULL) {
311 1.3 thorpej if_statadd2(ifp, if_obytes, m->m_len, if_opackets, 1);
312 1.1 bouyer can_mbuf_tag_clean(m);
313 1.1 bouyer m_set_rcvif(m, ifp);
314 1.1 bouyer can_input(ifp, m); /* loopback */
315 1.1 bouyer sc->sc_m_transmit = NULL;
316 1.1 bouyer ifp->if_timer = 0;
317 1.1 bouyer }
318 1.1 bouyer if_schedule_deferred_start(ifp);
319 1.1 bouyer }
320 1.1 bouyer
321 1.1 bouyer static int
322 1.1 bouyer sunxi_can_tx_abort(struct sunxi_can_softc *sc)
323 1.1 bouyer {
324 1.1 bouyer KASSERT(mutex_owned(&sc->sc_intr_lock));
325 1.1 bouyer if (sc->sc_m_transmit) {
326 1.1 bouyer m_freem(sc->sc_m_transmit);
327 1.1 bouyer sc->sc_m_transmit = NULL;
328 1.1 bouyer sc->sc_ifp->if_timer = 0;
329 1.1 bouyer /*
330 1.1 bouyer * the transmit abort will trigger a TX interrupt
331 1.9 thorpej * which will restart the queue 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.11 bouyer sunxi_can_ifdown(sc);
350 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_CMD_REG, SUNXI_CAN_CMD_CLR_OR);
351 1.11 bouyer sunxi_can_ifup(sc);
352 1.1 bouyer }
353 1.1 bouyer if (irq & SUNXI_CAN_INT_ERR) {
354 1.1 bouyer reg = sunxi_can_read(sc, SUNXI_CAN_REC_REG);
355 1.1 bouyer printf("%s: ERR interrupt status 0x%x counters 0x%x\n",
356 1.1 bouyer device_xname(sc->sc_dev), sts, reg);
357 1.1 bouyer
358 1.1 bouyer }
359 1.1 bouyer if (irq & SUNXI_CAN_INT_BERR) {
360 1.1 bouyer if (sts & SUNXI_CAN_STA_TX)
361 1.1 bouyer txerr++;
362 1.1 bouyer if (sts & SUNXI_CAN_STA_RX)
363 1.3 thorpej if_statinc(ifp, if_ierrors);
364 1.1 bouyer }
365 1.1 bouyer if (irq & SUNXI_CAN_INT_ERR_PASSIVE) {
366 1.1 bouyer printf("%s: PASSV interrupt status 0x%x\n",
367 1.1 bouyer device_xname(sc->sc_dev), sts);
368 1.1 bouyer }
369 1.1 bouyer if (irq & SUNXI_CAN_INT_ARB_LOST) {
370 1.1 bouyer txerr++;
371 1.1 bouyer }
372 1.1 bouyer if (txerr) {
373 1.3 thorpej if_statadd(ifp, if_oerrors, txerr);
374 1.1 bouyer (void) sunxi_can_tx_abort(sc);
375 1.1 bouyer }
376 1.1 bouyer }
377 1.1 bouyer
378 1.1 bouyer int
379 1.1 bouyer sunxi_can_intr(void *arg)
380 1.1 bouyer {
381 1.1 bouyer struct sunxi_can_softc * const sc = arg;
382 1.1 bouyer int rv = 0;
383 1.1 bouyer int irq;
384 1.1 bouyer
385 1.1 bouyer mutex_enter(&sc->sc_intr_lock);
386 1.1 bouyer
387 1.1 bouyer while ((irq = sunxi_can_read(sc, SUNXI_CAN_INT_REG)) != 0) {
388 1.1 bouyer uint32_t sts = sunxi_can_read(sc, SUNXI_CAN_STA_REG);
389 1.1 bouyer rv = 1;
390 1.11 bouyer rnd_add_uint32(&sc->sc_rnd_source, irq);
391 1.1 bouyer
392 1.10 bouyer if ((irq & (SUNXI_CAN_INT_RX_FLAG | SUNXI_CAN_INT_DATA_OR)) ==
393 1.10 bouyer SUNXI_CAN_INT_RX_FLAG) {
394 1.1 bouyer while (sts & SUNXI_CAN_STA_RX_RDY) {
395 1.1 bouyer sunxi_can_rx_intr(sc);
396 1.1 bouyer sts = sunxi_can_read(sc, SUNXI_CAN_STA_REG);
397 1.1 bouyer }
398 1.11 bouyer /*
399 1.11 bouyer * Don't write SUNXI_CAN_INT_RX_FLAG to the interrupt
400 1.11 bouyer * register, this may clear the RX pending flag
401 1.11 bouyer * while there is indeed a packet pending.
402 1.11 bouyer * Reading packets should have cleared the RX interrupt,
403 1.11 bouyer * so just restart the loop and re-read the interrupt
404 1.11 bouyer * register. In the common case irq will now be 0.
405 1.11 bouyer */
406 1.11 bouyer continue;
407 1.11 bouyer }
408 1.11 bouyer if (irq & SUNXI_CAN_INT_TX_FLAG) {
409 1.11 bouyer sunxi_can_tx_intr(sc);
410 1.1 bouyer }
411 1.1 bouyer if (irq & SUNXI_CAN_INT_ALLERRS) {
412 1.1 bouyer sunxi_can_err_intr(sc, irq, sts);
413 1.1 bouyer }
414 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_INT_REG, irq);
415 1.1 bouyer }
416 1.1 bouyer mutex_exit(&sc->sc_intr_lock);
417 1.1 bouyer
418 1.1 bouyer return rv;
419 1.1 bouyer }
420 1.1 bouyer
421 1.1 bouyer void
422 1.1 bouyer sunxi_can_ifstart(struct ifnet *ifp)
423 1.1 bouyer {
424 1.1 bouyer struct sunxi_can_softc * const sc = ifp->if_softc;
425 1.1 bouyer struct mbuf *m;
426 1.1 bouyer struct can_frame *cf;
427 1.1 bouyer int regd;
428 1.1 bouyer uint32_t reg0val;
429 1.1 bouyer int i;
430 1.1 bouyer
431 1.1 bouyer mutex_enter(&sc->sc_intr_lock);
432 1.9 thorpej if (sc->sc_m_transmit != NULL)
433 1.1 bouyer goto out;
434 1.1 bouyer
435 1.1 bouyer IF_DEQUEUE(&ifp->if_snd, m);
436 1.1 bouyer
437 1.1 bouyer if (m == NULL)
438 1.1 bouyer goto out;
439 1.1 bouyer
440 1.1 bouyer MCLAIM(m, ifp->if_mowner);
441 1.1 bouyer sc->sc_m_transmit = m;
442 1.1 bouyer
443 1.1 bouyer KASSERT((m->m_flags & M_PKTHDR) != 0);
444 1.1 bouyer KASSERT(m->m_len == m->m_pkthdr.len);
445 1.1 bouyer
446 1.1 bouyer cf = mtod(m, struct can_frame *);
447 1.1 bouyer reg0val = cf->can_dlc & SUNXI_CAN_TXBUF0_DL;
448 1.1 bouyer if (cf->can_id & CAN_RTR_FLAG)
449 1.1 bouyer reg0val |= SUNXI_CAN_TXBUF0_RTR;
450 1.1 bouyer
451 1.1 bouyer if (cf->can_id & CAN_EFF_FLAG) {
452 1.1 bouyer reg0val |= SUNXI_CAN_TXBUF0_EFF;
453 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_TXBUF1_REG,
454 1.1 bouyer (cf->can_id >> 21) & 0xff);
455 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_TXBUF2_REG,
456 1.1 bouyer (cf->can_id >> 13) & 0xff);
457 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_TXBUF3_REG,
458 1.1 bouyer (cf->can_id >> 5) & 0xff);
459 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_TXBUF4_REG,
460 1.1 bouyer (cf->can_id << 3) & 0xf8);
461 1.1 bouyer regd = SUNXI_CAN_TXBUF5_REG;
462 1.1 bouyer } else {
463 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_TXBUF1_REG,
464 1.1 bouyer (cf->can_id >> 3) & 0xff);
465 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_TXBUF2_REG,
466 1.1 bouyer (cf->can_id << 5) & 0xe0);
467 1.1 bouyer regd = SUNXI_CAN_TXBUF3_REG;
468 1.1 bouyer }
469 1.1 bouyer
470 1.1 bouyer for (i = 0; i < cf->can_dlc; i++) {
471 1.1 bouyer sunxi_can_write(sc, regd + i * 4, cf->data[i]);
472 1.1 bouyer }
473 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_TXBUF0_REG, reg0val);
474 1.1 bouyer
475 1.1 bouyer if (sc->sc_linkmodes & CAN_LINKMODE_LOOPBACK) {
476 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_CMD_REG,
477 1.1 bouyer SUNXI_CAN_CMD_TANS_REQ | SUNXI_CAN_CMD_SELF_REQ);
478 1.1 bouyer } else {
479 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_CMD_REG, SUNXI_CAN_CMD_TANS_REQ);
480 1.1 bouyer }
481 1.1 bouyer ifp->if_timer = 5;
482 1.1 bouyer can_bpf_mtap(ifp, m, 0);
483 1.1 bouyer out:
484 1.1 bouyer mutex_exit(&sc->sc_intr_lock);
485 1.1 bouyer }
486 1.1 bouyer
487 1.1 bouyer static int
488 1.1 bouyer sunxi_can_ifup(struct sunxi_can_softc * const sc)
489 1.1 bouyer {
490 1.1 bouyer uint32_t reg;
491 1.1 bouyer
492 1.1 bouyer /* setup timings and mode - has to be done in reset */
493 1.1 bouyer reg = SUNXI_CAN_MODSEL_RST;
494 1.1 bouyer if (sc->sc_linkmodes & CAN_LINKMODE_LISTENONLY)
495 1.1 bouyer reg |= SUNXI_CAN_MODSEL_LST_ONLY;
496 1.1 bouyer
497 1.1 bouyer if (sc->sc_linkmodes & CAN_LINKMODE_LOOPBACK)
498 1.1 bouyer reg |= SUNXI_CAN_MODSEL_LB_MOD;
499 1.1 bouyer
500 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_MODSEL_REG, reg);
501 1.1 bouyer
502 1.1 bouyer reg = 0;
503 1.1 bouyer if (sc->sc_timings.clt_prop != 0)
504 1.1 bouyer return EINVAL;
505 1.1 bouyer
506 1.1 bouyer if (sc->sc_timings.clt_brp > sc->sc_timecaps.cltc_brp_max ||
507 1.1 bouyer sc->sc_timings.clt_brp < sc->sc_timecaps.cltc_brp_min)
508 1.1 bouyer return EINVAL;
509 1.1 bouyer reg |= (sc->sc_timings.clt_brp - 1) << 0;
510 1.1 bouyer
511 1.1 bouyer if (sc->sc_timings.clt_ps1 > sc->sc_timecaps.cltc_ps1_max ||
512 1.1 bouyer sc->sc_timings.clt_ps1 < sc->sc_timecaps.cltc_ps1_min)
513 1.1 bouyer return EINVAL;
514 1.1 bouyer reg |= (sc->sc_timings.clt_ps1 - 1) << 16;
515 1.1 bouyer
516 1.1 bouyer if (sc->sc_timings.clt_ps2 > sc->sc_timecaps.cltc_ps2_max ||
517 1.1 bouyer sc->sc_timings.clt_ps2 < sc->sc_timecaps.cltc_ps2_min)
518 1.1 bouyer return EINVAL;
519 1.1 bouyer reg |= (sc->sc_timings.clt_ps2 - 1) << 20;
520 1.1 bouyer
521 1.1 bouyer if (sc->sc_timings.clt_sjw > sc->sc_timecaps.cltc_sjw_max ||
522 1.1 bouyer sc->sc_timings.clt_sjw < 1)
523 1.1 bouyer return EINVAL;
524 1.1 bouyer reg |= (sc->sc_timings.clt_sjw - 1) << 14;
525 1.1 bouyer
526 1.1 bouyer if (sc->sc_linkmodes & CAN_LINKMODE_3SAMPLES)
527 1.1 bouyer reg |= SUNXI_CAN_BUS_TIME_SAM;
528 1.1 bouyer
529 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_BUS_TIME_REG, reg);
530 1.1 bouyer
531 1.1 bouyer /* set filters to accept all frames */
532 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_ACPC, 0x00000000);
533 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_ACPM, 0xffffffff);
534 1.1 bouyer
535 1.1 bouyer /* clear errors counter */
536 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_REC_REG, 0);
537 1.1 bouyer
538 1.1 bouyer /* leave reset mode and enable interrupts */
539 1.1 bouyer sunxi_can_exit_reset(sc);
540 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_INTE_REG,
541 1.1 bouyer SUNXI_CAN_INT_TX_FLAG | SUNXI_CAN_INT_RX_FLAG | SUNXI_CAN_INT_ALLERRS);
542 1.1 bouyer sc->sc_ifp->if_flags |= IFF_RUNNING;
543 1.1 bouyer return 0;
544 1.1 bouyer }
545 1.1 bouyer
546 1.1 bouyer static void
547 1.1 bouyer sunxi_can_ifdown(struct sunxi_can_softc * const sc)
548 1.1 bouyer {
549 1.9 thorpej sc->sc_ifp->if_flags &= ~IFF_RUNNING;
550 1.1 bouyer sc->sc_ifp->if_timer = 0;
551 1.1 bouyer sunxi_can_enter_reset(sc);
552 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_INTE_REG, 0);
553 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_INT_REG,
554 1.1 bouyer sunxi_can_read(sc, SUNXI_CAN_INT_REG));
555 1.1 bouyer }
556 1.1 bouyer
557 1.1 bouyer static int
558 1.1 bouyer sunxi_can_ifioctl(struct ifnet *ifp, u_long cmd, void *data)
559 1.1 bouyer {
560 1.1 bouyer struct sunxi_can_softc * const sc = ifp->if_softc;
561 1.1 bouyer struct ifreq *ifr = (struct ifreq *)data;
562 1.1 bouyer int error = 0;
563 1.1 bouyer
564 1.1 bouyer mutex_enter(&sc->sc_intr_lock);
565 1.1 bouyer
566 1.1 bouyer switch (cmd) {
567 1.1 bouyer case SIOCINITIFADDR:
568 1.1 bouyer error = EAFNOSUPPORT;
569 1.1 bouyer break;
570 1.1 bouyer case SIOCSIFMTU:
571 1.1 bouyer if ((unsigned)ifr->ifr_mtu != sizeof(struct can_frame))
572 1.1 bouyer error = EINVAL;
573 1.1 bouyer break;
574 1.1 bouyer case SIOCADDMULTI:
575 1.1 bouyer case SIOCDELMULTI:
576 1.1 bouyer error = EAFNOSUPPORT;
577 1.1 bouyer break;
578 1.1 bouyer default:
579 1.1 bouyer error = ifioctl_common(ifp, cmd, data);
580 1.1 bouyer if (error == 0) {
581 1.1 bouyer if ((ifp->if_flags & IFF_UP) != 0 &&
582 1.1 bouyer (ifp->if_flags & IFF_RUNNING) == 0) {
583 1.1 bouyer error = sunxi_can_ifup(sc);
584 1.1 bouyer if (error) {
585 1.1 bouyer ifp->if_flags &= ~IFF_UP;
586 1.1 bouyer }
587 1.1 bouyer } else if ((ifp->if_flags & IFF_UP) == 0 &&
588 1.1 bouyer (ifp->if_flags & IFF_RUNNING) != 0) {
589 1.1 bouyer sunxi_can_ifdown(sc);
590 1.1 bouyer }
591 1.1 bouyer }
592 1.1 bouyer break;
593 1.1 bouyer }
594 1.1 bouyer
595 1.1 bouyer mutex_exit(&sc->sc_intr_lock);
596 1.1 bouyer return error;
597 1.1 bouyer }
598 1.1 bouyer
599 1.1 bouyer void
600 1.1 bouyer sunxi_can_ifwatchdog(struct ifnet *ifp)
601 1.1 bouyer {
602 1.1 bouyer struct sunxi_can_softc * const sc = ifp->if_softc;
603 1.1 bouyer printf("%s: watchdog timeout\n", device_xname(sc->sc_dev));
604 1.1 bouyer
605 1.1 bouyer mutex_enter(&sc->sc_intr_lock);
606 1.1 bouyer printf("irq 0x%x en 0x%x mode 0x%x status 0x%x timings 0x%x err 0x%x\n",
607 1.1 bouyer sunxi_can_read(sc, SUNXI_CAN_INT_REG),
608 1.1 bouyer sunxi_can_read(sc, SUNXI_CAN_INTE_REG),
609 1.1 bouyer sunxi_can_read(sc, SUNXI_CAN_MODSEL_REG),
610 1.1 bouyer sunxi_can_read(sc, SUNXI_CAN_STA_REG),
611 1.1 bouyer sunxi_can_read(sc, SUNXI_CAN_BUS_TIME_REG),
612 1.1 bouyer sunxi_can_read(sc, SUNXI_CAN_REC_REG));
613 1.1 bouyer /* if there is a transmit in progress abort */
614 1.1 bouyer if (sunxi_can_tx_abort(sc)) {
615 1.3 thorpej if_statinc(ifp, if_oerrors);
616 1.1 bouyer }
617 1.1 bouyer mutex_exit(&sc->sc_intr_lock);
618 1.1 bouyer }
619 1.1 bouyer
620 1.1 bouyer static void
621 1.1 bouyer sunxi_can_enter_reset(struct sunxi_can_softc *sc)
622 1.1 bouyer {
623 1.1 bouyer int i;
624 1.1 bouyer uint32_t val;
625 1.1 bouyer
626 1.1 bouyer for (i = 0; i < 1000; i++) {
627 1.1 bouyer val = sunxi_can_read(sc, SUNXI_CAN_MODSEL_REG);
628 1.1 bouyer val |= SUNXI_CAN_MODSEL_RST;
629 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_MODSEL_REG, val);
630 1.1 bouyer val = sunxi_can_read(sc, SUNXI_CAN_MODSEL_REG);
631 1.1 bouyer if (val & SUNXI_CAN_MODSEL_RST)
632 1.1 bouyer return;
633 1.1 bouyer }
634 1.1 bouyer printf("%s: couldn't enter reset mode\n", device_xname(sc->sc_dev));
635 1.1 bouyer }
636 1.1 bouyer
637 1.1 bouyer static void
638 1.1 bouyer sunxi_can_exit_reset(struct sunxi_can_softc *sc)
639 1.1 bouyer {
640 1.1 bouyer int i;
641 1.1 bouyer uint32_t val;
642 1.1 bouyer
643 1.1 bouyer for (i = 0; i < 1000; i++) {
644 1.1 bouyer val = sunxi_can_read(sc, SUNXI_CAN_MODSEL_REG);
645 1.1 bouyer val &= ~SUNXI_CAN_MODSEL_RST;
646 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_MODSEL_REG, val);
647 1.1 bouyer val = sunxi_can_read(sc, SUNXI_CAN_MODSEL_REG);
648 1.1 bouyer if ((val & SUNXI_CAN_MODSEL_RST) == 0)
649 1.1 bouyer return;
650 1.1 bouyer }
651 1.1 bouyer printf("%s: couldn't leave reset mode\n", device_xname(sc->sc_dev));
652 1.1 bouyer }
653