sunxi_can.c revision 1.5 1 1.5 thorpej /* $NetBSD: sunxi_can.c,v 1.5 2021/01/18 02:35:49 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.5 thorpej __KERNEL_RCSID(1, "$NetBSD: sunxi_can.c,v 1.5 2021/01/18 02:35:49 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.5 thorpej static const struct device_compatible_entry compat_data[] = {
89 1.5 thorpej { .compat = "allwinner,sun4i-a10-can" },
90 1.5 thorpej
91 1.5 thorpej { 0 }
92 1.1 bouyer };
93 1.1 bouyer
94 1.1 bouyer static int sunxi_can_match(device_t, cfdata_t, void *);
95 1.1 bouyer static void sunxi_can_attach(device_t, device_t, void *);
96 1.1 bouyer
97 1.1 bouyer static int sunxi_can_intr(void *);
98 1.1 bouyer
99 1.1 bouyer static void sunxi_can_ifstart(struct ifnet *);
100 1.1 bouyer static int sunxi_can_ifioctl(struct ifnet *, u_long, void *);
101 1.1 bouyer static void sunxi_can_ifwatchdog(struct ifnet *);
102 1.1 bouyer
103 1.1 bouyer static void sunxi_can_enter_reset(struct sunxi_can_softc *);
104 1.1 bouyer static void sunxi_can_exit_reset(struct sunxi_can_softc *);
105 1.1 bouyer
106 1.1 bouyer CFATTACH_DECL_NEW(sunxi_can, sizeof(struct sunxi_can_softc),
107 1.1 bouyer sunxi_can_match, sunxi_can_attach, NULL, NULL);
108 1.1 bouyer
109 1.1 bouyer static inline uint32_t
110 1.1 bouyer sunxi_can_read(struct sunxi_can_softc *sc, bus_size_t o)
111 1.1 bouyer {
112 1.1 bouyer return bus_space_read_4(sc->sc_bst, sc->sc_bsh, o);
113 1.1 bouyer }
114 1.1 bouyer
115 1.1 bouyer static inline void
116 1.1 bouyer sunxi_can_write(struct sunxi_can_softc *sc, bus_size_t o, uint32_t v)
117 1.1 bouyer {
118 1.1 bouyer return bus_space_write_4(sc->sc_bst, sc->sc_bsh, o, v);
119 1.1 bouyer }
120 1.1 bouyer
121 1.1 bouyer static int
122 1.1 bouyer sunxi_can_match(device_t parent, cfdata_t cf, void *aux)
123 1.1 bouyer {
124 1.1 bouyer struct fdt_attach_args * const faa = aux;
125 1.1 bouyer
126 1.1 bouyer return of_match_compat_data(faa->faa_phandle, compat_data);
127 1.1 bouyer }
128 1.1 bouyer
129 1.1 bouyer static void
130 1.1 bouyer sunxi_can_attach(device_t parent, device_t self, void *aux)
131 1.1 bouyer {
132 1.1 bouyer struct sunxi_can_softc * const sc = device_private(self);
133 1.1 bouyer struct fdt_attach_args * const faa = aux;
134 1.1 bouyer struct ifnet *ifp;
135 1.1 bouyer const int phandle = faa->faa_phandle;
136 1.1 bouyer bus_addr_t addr;
137 1.1 bouyer bus_size_t size;
138 1.1 bouyer char intrstr[128];
139 1.1 bouyer struct clk *clk;
140 1.1 bouyer struct fdtbus_reset *rst;
141 1.1 bouyer
142 1.1 bouyer sc->sc_dev = self;
143 1.1 bouyer mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_NET);
144 1.1 bouyer
145 1.1 bouyer sc->sc_bst = faa->faa_bst;
146 1.1 bouyer if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
147 1.1 bouyer aprint_error(": couldn't get registers\n");
148 1.1 bouyer return;
149 1.1 bouyer }
150 1.1 bouyer
151 1.1 bouyer if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
152 1.1 bouyer aprint_error(": couldn't map registers\n");
153 1.1 bouyer return;
154 1.1 bouyer }
155 1.1 bouyer
156 1.1 bouyer if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
157 1.1 bouyer aprint_error(": failed to decode interrupt\n");
158 1.1 bouyer return;
159 1.1 bouyer }
160 1.1 bouyer
161 1.1 bouyer if ((clk = fdtbus_clock_get_index(phandle, 0)) != NULL) {
162 1.1 bouyer if (clk_enable(clk) != 0) {
163 1.1 bouyer aprint_error(": couldn't enable clock\n");
164 1.1 bouyer return;
165 1.1 bouyer }
166 1.1 bouyer }
167 1.1 bouyer
168 1.1 bouyer if ((rst = fdtbus_reset_get_index(phandle, 0)) != NULL) {
169 1.1 bouyer if (fdtbus_reset_deassert(rst) != 0) {
170 1.1 bouyer aprint_error(": couldn't de-assert reset\n");
171 1.1 bouyer return;
172 1.1 bouyer }
173 1.1 bouyer }
174 1.1 bouyer
175 1.1 bouyer sc->sc_timecaps.cltc_prop_min = 0;
176 1.1 bouyer sc->sc_timecaps.cltc_prop_max = 0;
177 1.1 bouyer sc->sc_timecaps.cltc_ps1_min = 1;
178 1.1 bouyer sc->sc_timecaps.cltc_ps1_max = 16;
179 1.1 bouyer sc->sc_timecaps.cltc_ps2_min = 1;
180 1.1 bouyer sc->sc_timecaps.cltc_ps2_max = 8;
181 1.1 bouyer sc->sc_timecaps.cltc_sjw_max = 4;
182 1.1 bouyer sc->sc_timecaps.cltc_brp_min = 1;
183 1.1 bouyer sc->sc_timecaps.cltc_brp_max = 64;
184 1.1 bouyer sc->sc_timecaps.cltc_brp_inc = 1;
185 1.1 bouyer sc->sc_timecaps.cltc_clock_freq = clk_get_rate(clk);
186 1.1 bouyer sc->sc_timecaps.cltc_linkmode_caps =
187 1.1 bouyer CAN_LINKMODE_3SAMPLES | CAN_LINKMODE_LISTENONLY |
188 1.1 bouyer CAN_LINKMODE_LOOPBACK;
189 1.1 bouyer can_ifinit_timings(&sc->sc_cansc);
190 1.1 bouyer sc->sc_timings.clt_prop = 0;
191 1.1 bouyer sc->sc_timings.clt_sjw = 1;
192 1.1 bouyer
193 1.1 bouyer aprint_naive("\n");
194 1.1 bouyer aprint_normal(": CAN bus controller\n");
195 1.1 bouyer aprint_debug_dev(self, ": clock freq %d\n",
196 1.1 bouyer sc->sc_timecaps.cltc_clock_freq);
197 1.1 bouyer
198 1.1 bouyer sunxi_can_enter_reset(sc);
199 1.1 bouyer /*
200 1.1 bouyer * Disable and then clear all interrupts
201 1.1 bouyer */
202 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_INTE_REG, 0);
203 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_INT_REG,
204 1.1 bouyer sunxi_can_read(sc, SUNXI_CAN_INT_REG));
205 1.1 bouyer
206 1.4 jmcneill sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_NET, 0,
207 1.4 jmcneill sunxi_can_intr, sc, device_xname(self));
208 1.1 bouyer if (sc->sc_ih == NULL) {
209 1.1 bouyer aprint_error_dev(self, "failed to establish interrupt on %s\n",
210 1.1 bouyer intrstr);
211 1.1 bouyer return;
212 1.1 bouyer }
213 1.1 bouyer aprint_normal_dev(self, "interrupting on %s\n", intrstr);
214 1.1 bouyer
215 1.1 bouyer ifp = if_alloc(IFT_OTHER);
216 1.1 bouyer sc->sc_ifp = ifp;
217 1.1 bouyer strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
218 1.1 bouyer ifp->if_softc = sc;
219 1.1 bouyer ifp->if_capabilities = 0;
220 1.1 bouyer ifp->if_flags = 0;
221 1.1 bouyer ifp->if_start = sunxi_can_ifstart;
222 1.1 bouyer ifp->if_ioctl = sunxi_can_ifioctl;
223 1.1 bouyer ifp->if_watchdog = sunxi_can_ifwatchdog;
224 1.1 bouyer
225 1.1 bouyer /*
226 1.1 bouyer * Attach the interface.
227 1.1 bouyer */
228 1.1 bouyer can_ifattach(ifp);
229 1.1 bouyer if_deferred_start_init(ifp, NULL);
230 1.1 bouyer bpf_mtap_softint_init(ifp);
231 1.1 bouyer rnd_attach_source(&sc->sc_rnd_source, device_xname(self),
232 1.1 bouyer RND_TYPE_NET, RND_FLAG_DEFAULT);
233 1.1 bouyer #ifdef MBUFTRACE
234 1.1 bouyer ifp->if_mowner = malloc(sizeof(struct mowner), M_DEVBUF,
235 1.1 bouyer M_WAITOK | M_ZERO);
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 ifp->if_flags &= ~IFF_OACTIVE;
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.1 bouyer * which will restart the queue or cleae OACTIVE,
332 1.1 bouyer * as appropriate
333 1.1 bouyer */
334 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_CMD_REG, SUNXI_CAN_CMD_ABT_REQ);
335 1.1 bouyer return 1;
336 1.1 bouyer }
337 1.1 bouyer return 0;
338 1.1 bouyer }
339 1.1 bouyer
340 1.1 bouyer static void
341 1.1 bouyer sunxi_can_err_intr(struct sunxi_can_softc *sc, uint32_t irq, uint32_t sts)
342 1.1 bouyer {
343 1.1 bouyer struct ifnet * const ifp = sc->sc_ifp;
344 1.1 bouyer KASSERT(mutex_owned(&sc->sc_intr_lock));
345 1.1 bouyer int txerr = 0;
346 1.1 bouyer uint32_t reg;
347 1.1 bouyer
348 1.1 bouyer if (irq & SUNXI_CAN_INT_DATA_OR) {
349 1.3 thorpej if_statinc(ifp, if_ierrors);
350 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_CMD_REG, SUNXI_CAN_CMD_CLR_OR);
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.1 bouyer
390 1.1 bouyer if (irq & SUNXI_CAN_INT_TX_FLAG) {
391 1.1 bouyer sunxi_can_tx_intr(sc);
392 1.1 bouyer }
393 1.1 bouyer if (irq & 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.1 bouyer }
399 1.1 bouyer if (irq & SUNXI_CAN_INT_ALLERRS) {
400 1.1 bouyer sunxi_can_err_intr(sc, irq, sts);
401 1.1 bouyer }
402 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_INT_REG, irq);
403 1.1 bouyer rnd_add_uint32(&sc->sc_rnd_source, irq);
404 1.1 bouyer
405 1.1 bouyer }
406 1.1 bouyer mutex_exit(&sc->sc_intr_lock);
407 1.1 bouyer
408 1.1 bouyer return rv;
409 1.1 bouyer }
410 1.1 bouyer
411 1.1 bouyer void
412 1.1 bouyer sunxi_can_ifstart(struct ifnet *ifp)
413 1.1 bouyer {
414 1.1 bouyer struct sunxi_can_softc * const sc = ifp->if_softc;
415 1.1 bouyer struct mbuf *m;
416 1.1 bouyer struct can_frame *cf;
417 1.1 bouyer int regd;
418 1.1 bouyer uint32_t reg0val;
419 1.1 bouyer int i;
420 1.1 bouyer
421 1.1 bouyer mutex_enter(&sc->sc_intr_lock);
422 1.1 bouyer if (ifp->if_flags & IFF_OACTIVE)
423 1.1 bouyer goto out;
424 1.1 bouyer
425 1.1 bouyer IF_DEQUEUE(&ifp->if_snd, m);
426 1.1 bouyer
427 1.1 bouyer if (m == NULL)
428 1.1 bouyer goto out;
429 1.1 bouyer
430 1.1 bouyer MCLAIM(m, ifp->if_mowner);
431 1.1 bouyer sc->sc_m_transmit = m;
432 1.1 bouyer
433 1.1 bouyer KASSERT((m->m_flags & M_PKTHDR) != 0);
434 1.1 bouyer KASSERT(m->m_len == m->m_pkthdr.len);
435 1.1 bouyer
436 1.1 bouyer cf = mtod(m, struct can_frame *);
437 1.1 bouyer reg0val = cf->can_dlc & SUNXI_CAN_TXBUF0_DL;
438 1.1 bouyer if (cf->can_id & CAN_RTR_FLAG)
439 1.1 bouyer reg0val |= SUNXI_CAN_TXBUF0_RTR;
440 1.1 bouyer
441 1.1 bouyer if (cf->can_id & CAN_EFF_FLAG) {
442 1.1 bouyer reg0val |= SUNXI_CAN_TXBUF0_EFF;
443 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_TXBUF1_REG,
444 1.1 bouyer (cf->can_id >> 21) & 0xff);
445 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_TXBUF2_REG,
446 1.1 bouyer (cf->can_id >> 13) & 0xff);
447 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_TXBUF3_REG,
448 1.1 bouyer (cf->can_id >> 5) & 0xff);
449 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_TXBUF4_REG,
450 1.1 bouyer (cf->can_id << 3) & 0xf8);
451 1.1 bouyer regd = SUNXI_CAN_TXBUF5_REG;
452 1.1 bouyer } else {
453 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_TXBUF1_REG,
454 1.1 bouyer (cf->can_id >> 3) & 0xff);
455 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_TXBUF2_REG,
456 1.1 bouyer (cf->can_id << 5) & 0xe0);
457 1.1 bouyer regd = SUNXI_CAN_TXBUF3_REG;
458 1.1 bouyer }
459 1.1 bouyer
460 1.1 bouyer for (i = 0; i < cf->can_dlc; i++) {
461 1.1 bouyer sunxi_can_write(sc, regd + i * 4, cf->data[i]);
462 1.1 bouyer }
463 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_TXBUF0_REG, reg0val);
464 1.1 bouyer
465 1.1 bouyer if (sc->sc_linkmodes & CAN_LINKMODE_LOOPBACK) {
466 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_CMD_REG,
467 1.1 bouyer SUNXI_CAN_CMD_TANS_REQ | SUNXI_CAN_CMD_SELF_REQ);
468 1.1 bouyer } else {
469 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_CMD_REG, SUNXI_CAN_CMD_TANS_REQ);
470 1.1 bouyer }
471 1.1 bouyer ifp->if_flags |= IFF_OACTIVE;
472 1.1 bouyer ifp->if_timer = 5;
473 1.1 bouyer can_bpf_mtap(ifp, m, 0);
474 1.1 bouyer out:
475 1.1 bouyer mutex_exit(&sc->sc_intr_lock);
476 1.1 bouyer }
477 1.1 bouyer
478 1.1 bouyer static int
479 1.1 bouyer sunxi_can_ifup(struct sunxi_can_softc * const sc)
480 1.1 bouyer {
481 1.1 bouyer uint32_t reg;
482 1.1 bouyer
483 1.1 bouyer /* setup timings and mode - has to be done in reset */
484 1.1 bouyer reg = SUNXI_CAN_MODSEL_RST;
485 1.1 bouyer if (sc->sc_linkmodes & CAN_LINKMODE_LISTENONLY)
486 1.1 bouyer reg |= SUNXI_CAN_MODSEL_LST_ONLY;
487 1.1 bouyer
488 1.1 bouyer if (sc->sc_linkmodes & CAN_LINKMODE_LOOPBACK)
489 1.1 bouyer reg |= SUNXI_CAN_MODSEL_LB_MOD;
490 1.1 bouyer
491 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_MODSEL_REG, reg);
492 1.1 bouyer
493 1.1 bouyer reg = 0;
494 1.1 bouyer if (sc->sc_timings.clt_prop != 0)
495 1.1 bouyer return EINVAL;
496 1.1 bouyer
497 1.1 bouyer if (sc->sc_timings.clt_brp > sc->sc_timecaps.cltc_brp_max ||
498 1.1 bouyer sc->sc_timings.clt_brp < sc->sc_timecaps.cltc_brp_min)
499 1.1 bouyer return EINVAL;
500 1.1 bouyer reg |= (sc->sc_timings.clt_brp - 1) << 0;
501 1.1 bouyer
502 1.1 bouyer if (sc->sc_timings.clt_ps1 > sc->sc_timecaps.cltc_ps1_max ||
503 1.1 bouyer sc->sc_timings.clt_ps1 < sc->sc_timecaps.cltc_ps1_min)
504 1.1 bouyer return EINVAL;
505 1.1 bouyer reg |= (sc->sc_timings.clt_ps1 - 1) << 16;
506 1.1 bouyer
507 1.1 bouyer if (sc->sc_timings.clt_ps2 > sc->sc_timecaps.cltc_ps2_max ||
508 1.1 bouyer sc->sc_timings.clt_ps2 < sc->sc_timecaps.cltc_ps2_min)
509 1.1 bouyer return EINVAL;
510 1.1 bouyer reg |= (sc->sc_timings.clt_ps2 - 1) << 20;
511 1.1 bouyer
512 1.1 bouyer if (sc->sc_timings.clt_sjw > sc->sc_timecaps.cltc_sjw_max ||
513 1.1 bouyer sc->sc_timings.clt_sjw < 1)
514 1.1 bouyer return EINVAL;
515 1.1 bouyer reg |= (sc->sc_timings.clt_sjw - 1) << 14;
516 1.1 bouyer
517 1.1 bouyer if (sc->sc_linkmodes & CAN_LINKMODE_3SAMPLES)
518 1.1 bouyer reg |= SUNXI_CAN_BUS_TIME_SAM;
519 1.1 bouyer
520 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_BUS_TIME_REG, reg);
521 1.1 bouyer
522 1.1 bouyer /* set filters to accept all frames */
523 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_ACPC, 0x00000000);
524 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_ACPM, 0xffffffff);
525 1.1 bouyer
526 1.1 bouyer /* clear errors counter */
527 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_REC_REG, 0);
528 1.1 bouyer
529 1.1 bouyer /* leave reset mode and enable interrupts */
530 1.1 bouyer sunxi_can_exit_reset(sc);
531 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_INTE_REG,
532 1.1 bouyer SUNXI_CAN_INT_TX_FLAG | SUNXI_CAN_INT_RX_FLAG | SUNXI_CAN_INT_ALLERRS);
533 1.1 bouyer sc->sc_ifp->if_flags |= IFF_RUNNING;
534 1.1 bouyer return 0;
535 1.1 bouyer }
536 1.1 bouyer
537 1.1 bouyer static void
538 1.1 bouyer sunxi_can_ifdown(struct sunxi_can_softc * const sc)
539 1.1 bouyer {
540 1.1 bouyer sc->sc_ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
541 1.1 bouyer sc->sc_ifp->if_timer = 0;
542 1.1 bouyer sunxi_can_enter_reset(sc);
543 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_INTE_REG, 0);
544 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_INT_REG,
545 1.1 bouyer sunxi_can_read(sc, SUNXI_CAN_INT_REG));
546 1.1 bouyer }
547 1.1 bouyer
548 1.1 bouyer static int
549 1.1 bouyer sunxi_can_ifioctl(struct ifnet *ifp, u_long cmd, void *data)
550 1.1 bouyer {
551 1.1 bouyer struct sunxi_can_softc * const sc = ifp->if_softc;
552 1.1 bouyer struct ifreq *ifr = (struct ifreq *)data;
553 1.1 bouyer int error = 0;
554 1.1 bouyer
555 1.1 bouyer mutex_enter(&sc->sc_intr_lock);
556 1.1 bouyer
557 1.1 bouyer switch (cmd) {
558 1.1 bouyer case SIOCINITIFADDR:
559 1.1 bouyer error = EAFNOSUPPORT;
560 1.1 bouyer break;
561 1.1 bouyer case SIOCSIFMTU:
562 1.1 bouyer if ((unsigned)ifr->ifr_mtu != sizeof(struct can_frame))
563 1.1 bouyer error = EINVAL;
564 1.1 bouyer break;
565 1.1 bouyer case SIOCADDMULTI:
566 1.1 bouyer case SIOCDELMULTI:
567 1.1 bouyer error = EAFNOSUPPORT;
568 1.1 bouyer break;
569 1.1 bouyer default:
570 1.1 bouyer error = ifioctl_common(ifp, cmd, data);
571 1.1 bouyer if (error == 0) {
572 1.1 bouyer if ((ifp->if_flags & IFF_UP) != 0 &&
573 1.1 bouyer (ifp->if_flags & IFF_RUNNING) == 0) {
574 1.1 bouyer error = sunxi_can_ifup(sc);
575 1.1 bouyer if (error) {
576 1.1 bouyer ifp->if_flags &= ~IFF_UP;
577 1.1 bouyer }
578 1.1 bouyer } else if ((ifp->if_flags & IFF_UP) == 0 &&
579 1.1 bouyer (ifp->if_flags & IFF_RUNNING) != 0) {
580 1.1 bouyer sunxi_can_ifdown(sc);
581 1.1 bouyer }
582 1.1 bouyer }
583 1.1 bouyer break;
584 1.1 bouyer }
585 1.1 bouyer
586 1.1 bouyer mutex_exit(&sc->sc_intr_lock);
587 1.1 bouyer return error;
588 1.1 bouyer }
589 1.1 bouyer
590 1.1 bouyer void
591 1.1 bouyer sunxi_can_ifwatchdog(struct ifnet *ifp)
592 1.1 bouyer {
593 1.1 bouyer struct sunxi_can_softc * const sc = ifp->if_softc;
594 1.1 bouyer printf("%s: watchdog timeout\n", device_xname(sc->sc_dev));
595 1.1 bouyer
596 1.1 bouyer mutex_enter(&sc->sc_intr_lock);
597 1.1 bouyer printf("irq 0x%x en 0x%x mode 0x%x status 0x%x timings 0x%x err 0x%x\n",
598 1.1 bouyer sunxi_can_read(sc, SUNXI_CAN_INT_REG),
599 1.1 bouyer sunxi_can_read(sc, SUNXI_CAN_INTE_REG),
600 1.1 bouyer sunxi_can_read(sc, SUNXI_CAN_MODSEL_REG),
601 1.1 bouyer sunxi_can_read(sc, SUNXI_CAN_STA_REG),
602 1.1 bouyer sunxi_can_read(sc, SUNXI_CAN_BUS_TIME_REG),
603 1.1 bouyer sunxi_can_read(sc, SUNXI_CAN_REC_REG));
604 1.1 bouyer /* if there is a transmit in progress abort */
605 1.1 bouyer if (sunxi_can_tx_abort(sc)) {
606 1.3 thorpej if_statinc(ifp, if_oerrors);
607 1.1 bouyer }
608 1.1 bouyer mutex_exit(&sc->sc_intr_lock);
609 1.1 bouyer }
610 1.1 bouyer
611 1.1 bouyer static void
612 1.1 bouyer sunxi_can_enter_reset(struct sunxi_can_softc *sc)
613 1.1 bouyer {
614 1.1 bouyer int i;
615 1.1 bouyer uint32_t val;
616 1.1 bouyer
617 1.1 bouyer for (i = 0; i < 1000; i++) {
618 1.1 bouyer val = sunxi_can_read(sc, SUNXI_CAN_MODSEL_REG);
619 1.1 bouyer val |= SUNXI_CAN_MODSEL_RST;
620 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_MODSEL_REG, val);
621 1.1 bouyer val = sunxi_can_read(sc, SUNXI_CAN_MODSEL_REG);
622 1.1 bouyer if (val & SUNXI_CAN_MODSEL_RST)
623 1.1 bouyer return;
624 1.1 bouyer }
625 1.1 bouyer printf("%s: couldn't enter reset mode\n", device_xname(sc->sc_dev));
626 1.1 bouyer }
627 1.1 bouyer
628 1.1 bouyer static void
629 1.1 bouyer sunxi_can_exit_reset(struct sunxi_can_softc *sc)
630 1.1 bouyer {
631 1.1 bouyer int i;
632 1.1 bouyer uint32_t val;
633 1.1 bouyer
634 1.1 bouyer for (i = 0; i < 1000; i++) {
635 1.1 bouyer val = sunxi_can_read(sc, SUNXI_CAN_MODSEL_REG);
636 1.1 bouyer val &= ~SUNXI_CAN_MODSEL_RST;
637 1.1 bouyer sunxi_can_write(sc, SUNXI_CAN_MODSEL_REG, val);
638 1.1 bouyer val = sunxi_can_read(sc, SUNXI_CAN_MODSEL_REG);
639 1.1 bouyer if ((val & SUNXI_CAN_MODSEL_RST) == 0)
640 1.1 bouyer return;
641 1.1 bouyer }
642 1.1 bouyer printf("%s: couldn't leave reset mode\n", device_xname(sc->sc_dev));
643 1.1 bouyer }
644