2 * CAN bus driver for IFI CANFD controller
4 * Copyright (C) 2016 Marek Vasut <marex@denx.de>
6 * Details about this controller can be found at
7 * http://www.ifi-pld.de/IP/CANFD/canfd.html
9 * This file is licensed under the terms of the GNU General Public
10 * License version 2. This program is licensed "as is" without any
11 * warranty of any kind, whether express or implied.
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/netdevice.h>
22 #include <linux/of_device.h>
23 #include <linux/platform_device.h>
25 #include <linux/can/dev.h>
27 #define IFI_CANFD_STCMD 0x0
28 #define IFI_CANFD_STCMD_HARDRESET 0xDEADCAFD
29 #define IFI_CANFD_STCMD_ENABLE BIT(0)
30 #define IFI_CANFD_STCMD_ERROR_ACTIVE BIT(2)
31 #define IFI_CANFD_STCMD_ERROR_PASSIVE BIT(3)
32 #define IFI_CANFD_STCMD_BUSOFF BIT(4)
33 #define IFI_CANFD_STCMD_BUSMONITOR BIT(16)
34 #define IFI_CANFD_STCMD_LOOPBACK BIT(18)
35 #define IFI_CANFD_STCMD_DISABLE_CANFD BIT(24)
36 #define IFI_CANFD_STCMD_ENABLE_ISO BIT(25)
37 #define IFI_CANFD_STCMD_ENABLE_7_9_8_8_TIMING BIT(26)
38 #define IFI_CANFD_STCMD_NORMAL_MODE ((u32)BIT(31))
40 #define IFI_CANFD_RXSTCMD 0x4
41 #define IFI_CANFD_RXSTCMD_REMOVE_MSG BIT(0)
42 #define IFI_CANFD_RXSTCMD_RESET BIT(7)
43 #define IFI_CANFD_RXSTCMD_EMPTY BIT(8)
44 #define IFI_CANFD_RXSTCMD_OVERFLOW BIT(13)
46 #define IFI_CANFD_TXSTCMD 0x8
47 #define IFI_CANFD_TXSTCMD_ADD_MSG BIT(0)
48 #define IFI_CANFD_TXSTCMD_HIGH_PRIO BIT(1)
49 #define IFI_CANFD_TXSTCMD_RESET BIT(7)
50 #define IFI_CANFD_TXSTCMD_EMPTY BIT(8)
51 #define IFI_CANFD_TXSTCMD_FULL BIT(12)
52 #define IFI_CANFD_TXSTCMD_OVERFLOW BIT(13)
54 #define IFI_CANFD_INTERRUPT 0xc
55 #define IFI_CANFD_INTERRUPT_ERROR_WARNING BIT(1)
56 #define IFI_CANFD_INTERRUPT_ERROR_COUNTER BIT(10)
57 #define IFI_CANFD_INTERRUPT_TXFIFO_EMPTY BIT(16)
58 #define IFI_CANFD_INTERRUPT_TXFIFO_REMOVE BIT(22)
59 #define IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY BIT(24)
60 #define IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY_PER BIT(25)
61 #define IFI_CANFD_INTERRUPT_SET_IRQ ((u32)BIT(31))
63 #define IFI_CANFD_IRQMASK 0x10
64 #define IFI_CANFD_IRQMASK_SET_ERR BIT(7)
65 #define IFI_CANFD_IRQMASK_SET_TS BIT(15)
66 #define IFI_CANFD_IRQMASK_TXFIFO_EMPTY BIT(16)
67 #define IFI_CANFD_IRQMASK_SET_TX BIT(23)
68 #define IFI_CANFD_IRQMASK_RXFIFO_NEMPTY BIT(24)
69 #define IFI_CANFD_IRQMASK_SET_RX ((u32)BIT(31))
71 #define IFI_CANFD_TIME 0x14
72 #define IFI_CANFD_FTIME 0x18
73 #define IFI_CANFD_TIME_TIMEB_OFF 0
74 #define IFI_CANFD_TIME_TIMEA_OFF 8
75 #define IFI_CANFD_TIME_PRESCALE_OFF 16
76 #define IFI_CANFD_TIME_SJW_OFF_7_9_8_8 25
77 #define IFI_CANFD_TIME_SJW_OFF_4_12_6_6 28
78 #define IFI_CANFD_TIME_SET_SJW_4_12_6_6 BIT(6)
79 #define IFI_CANFD_TIME_SET_TIMEB_4_12_6_6 BIT(7)
80 #define IFI_CANFD_TIME_SET_PRESC_4_12_6_6 BIT(14)
81 #define IFI_CANFD_TIME_SET_TIMEA_4_12_6_6 BIT(15)
83 #define IFI_CANFD_TDELAY 0x1c
84 #define IFI_CANFD_TDELAY_DEFAULT 0xb
85 #define IFI_CANFD_TDELAY_MASK 0x3fff
86 #define IFI_CANFD_TDELAY_ABS BIT(14)
87 #define IFI_CANFD_TDELAY_EN BIT(15)
89 #define IFI_CANFD_ERROR 0x20
90 #define IFI_CANFD_ERROR_TX_OFFSET 0
91 #define IFI_CANFD_ERROR_TX_MASK 0xff
92 #define IFI_CANFD_ERROR_RX_OFFSET 16
93 #define IFI_CANFD_ERROR_RX_MASK 0xff
95 #define IFI_CANFD_ERRCNT 0x24
97 #define IFI_CANFD_SUSPEND 0x28
99 #define IFI_CANFD_REPEAT 0x2c
101 #define IFI_CANFD_TRAFFIC 0x30
103 #define IFI_CANFD_TSCONTROL 0x34
105 #define IFI_CANFD_TSC 0x38
107 #define IFI_CANFD_TST 0x3c
109 #define IFI_CANFD_RES1 0x40
111 #define IFI_CANFD_ERROR_CTR 0x44
112 #define IFI_CANFD_ERROR_CTR_UNLOCK_MAGIC 0x21302899
113 #define IFI_CANFD_ERROR_CTR_OVERLOAD_FIRST BIT(0)
114 #define IFI_CANFD_ERROR_CTR_ACK_ERROR_FIRST BIT(1)
115 #define IFI_CANFD_ERROR_CTR_BIT0_ERROR_FIRST BIT(2)
116 #define IFI_CANFD_ERROR_CTR_BIT1_ERROR_FIRST BIT(3)
117 #define IFI_CANFD_ERROR_CTR_STUFF_ERROR_FIRST BIT(4)
118 #define IFI_CANFD_ERROR_CTR_CRC_ERROR_FIRST BIT(5)
119 #define IFI_CANFD_ERROR_CTR_FORM_ERROR_FIRST BIT(6)
120 #define IFI_CANFD_ERROR_CTR_OVERLOAD_ALL BIT(8)
121 #define IFI_CANFD_ERROR_CTR_ACK_ERROR_ALL BIT(9)
122 #define IFI_CANFD_ERROR_CTR_BIT0_ERROR_ALL BIT(10)
123 #define IFI_CANFD_ERROR_CTR_BIT1_ERROR_ALL BIT(11)
124 #define IFI_CANFD_ERROR_CTR_STUFF_ERROR_ALL BIT(12)
125 #define IFI_CANFD_ERROR_CTR_CRC_ERROR_ALL BIT(13)
126 #define IFI_CANFD_ERROR_CTR_FORM_ERROR_ALL BIT(14)
127 #define IFI_CANFD_ERROR_CTR_BITPOSITION_OFFSET 16
128 #define IFI_CANFD_ERROR_CTR_BITPOSITION_MASK 0xff
129 #define IFI_CANFD_ERROR_CTR_ER_RESET BIT(30)
130 #define IFI_CANFD_ERROR_CTR_ER_ENABLE ((u32)BIT(31))
132 #define IFI_CANFD_PAR 0x48
134 #define IFI_CANFD_CANCLOCK 0x4c
136 #define IFI_CANFD_SYSCLOCK 0x50
138 #define IFI_CANFD_VER 0x54
140 #define IFI_CANFD_IP_ID 0x58
141 #define IFI_CANFD_IP_ID_VALUE 0xD073CAFD
143 #define IFI_CANFD_TEST 0x5c
145 #define IFI_CANFD_RXFIFO_TS_63_32 0x60
147 #define IFI_CANFD_RXFIFO_TS_31_0 0x64
149 #define IFI_CANFD_RXFIFO_DLC 0x68
150 #define IFI_CANFD_RXFIFO_DLC_DLC_OFFSET 0
151 #define IFI_CANFD_RXFIFO_DLC_DLC_MASK 0xf
152 #define IFI_CANFD_RXFIFO_DLC_RTR BIT(4)
153 #define IFI_CANFD_RXFIFO_DLC_EDL BIT(5)
154 #define IFI_CANFD_RXFIFO_DLC_BRS BIT(6)
155 #define IFI_CANFD_RXFIFO_DLC_ESI BIT(7)
156 #define IFI_CANFD_RXFIFO_DLC_OBJ_OFFSET 8
157 #define IFI_CANFD_RXFIFO_DLC_OBJ_MASK 0x1ff
158 #define IFI_CANFD_RXFIFO_DLC_FNR_OFFSET 24
159 #define IFI_CANFD_RXFIFO_DLC_FNR_MASK 0xff
161 #define IFI_CANFD_RXFIFO_ID 0x6c
162 #define IFI_CANFD_RXFIFO_ID_ID_OFFSET 0
163 #define IFI_CANFD_RXFIFO_ID_ID_STD_MASK CAN_SFF_MASK
164 #define IFI_CANFD_RXFIFO_ID_ID_STD_OFFSET 0
165 #define IFI_CANFD_RXFIFO_ID_ID_STD_WIDTH 10
166 #define IFI_CANFD_RXFIFO_ID_ID_XTD_MASK CAN_EFF_MASK
167 #define IFI_CANFD_RXFIFO_ID_ID_XTD_OFFSET 11
168 #define IFI_CANFD_RXFIFO_ID_ID_XTD_WIDTH 18
169 #define IFI_CANFD_RXFIFO_ID_IDE BIT(29)
171 #define IFI_CANFD_RXFIFO_DATA 0x70 /* 0x70..0xac */
173 #define IFI_CANFD_TXFIFO_SUSPEND_US 0xb0
175 #define IFI_CANFD_TXFIFO_REPEATCOUNT 0xb4
177 #define IFI_CANFD_TXFIFO_DLC 0xb8
178 #define IFI_CANFD_TXFIFO_DLC_DLC_OFFSET 0
179 #define IFI_CANFD_TXFIFO_DLC_DLC_MASK 0xf
180 #define IFI_CANFD_TXFIFO_DLC_RTR BIT(4)
181 #define IFI_CANFD_TXFIFO_DLC_EDL BIT(5)
182 #define IFI_CANFD_TXFIFO_DLC_BRS BIT(6)
183 #define IFI_CANFD_TXFIFO_DLC_FNR_OFFSET 24
184 #define IFI_CANFD_TXFIFO_DLC_FNR_MASK 0xff
186 #define IFI_CANFD_TXFIFO_ID 0xbc
187 #define IFI_CANFD_TXFIFO_ID_ID_OFFSET 0
188 #define IFI_CANFD_TXFIFO_ID_ID_STD_MASK CAN_SFF_MASK
189 #define IFI_CANFD_TXFIFO_ID_ID_STD_OFFSET 0
190 #define IFI_CANFD_TXFIFO_ID_ID_STD_WIDTH 10
191 #define IFI_CANFD_TXFIFO_ID_ID_XTD_MASK CAN_EFF_MASK
192 #define IFI_CANFD_TXFIFO_ID_ID_XTD_OFFSET 11
193 #define IFI_CANFD_TXFIFO_ID_ID_XTD_WIDTH 18
194 #define IFI_CANFD_TXFIFO_ID_IDE BIT(29)
196 #define IFI_CANFD_TXFIFO_DATA 0xc0 /* 0xb0..0xfc */
198 #define IFI_CANFD_FILTER_MASK(n) (0x800 + ((n) * 8) + 0)
199 #define IFI_CANFD_FILTER_MASK_EXT BIT(29)
200 #define IFI_CANFD_FILTER_MASK_EDL BIT(30)
201 #define IFI_CANFD_FILTER_MASK_VALID ((u32)BIT(31))
203 #define IFI_CANFD_FILTER_IDENT(n) (0x800 + ((n) * 8) + 4)
204 #define IFI_CANFD_FILTER_IDENT_IDE BIT(29)
205 #define IFI_CANFD_FILTER_IDENT_CANFD BIT(30)
206 #define IFI_CANFD_FILTER_IDENT_VALID ((u32)BIT(31))
208 /* IFI CANFD private data structure */
209 struct ifi_canfd_priv {
210 struct can_priv can; /* must be the first member */
211 struct napi_struct napi;
212 struct net_device *ndev;
216 static void ifi_canfd_irq_enable(struct net_device *ndev, bool enable)
218 struct ifi_canfd_priv *priv = netdev_priv(ndev);
222 enirq = IFI_CANFD_IRQMASK_TXFIFO_EMPTY |
223 IFI_CANFD_IRQMASK_RXFIFO_NEMPTY;
224 if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
225 enirq |= IFI_CANFD_INTERRUPT_ERROR_COUNTER;
228 writel(IFI_CANFD_IRQMASK_SET_ERR |
229 IFI_CANFD_IRQMASK_SET_TS |
230 IFI_CANFD_IRQMASK_SET_TX |
231 IFI_CANFD_IRQMASK_SET_RX | enirq,
232 priv->base + IFI_CANFD_IRQMASK);
235 static void ifi_canfd_read_fifo(struct net_device *ndev)
237 struct net_device_stats *stats = &ndev->stats;
238 struct ifi_canfd_priv *priv = netdev_priv(ndev);
239 struct canfd_frame *cf;
241 const u32 rx_irq_mask = IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY |
242 IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY_PER;
247 rxdlc = readl(priv->base + IFI_CANFD_RXFIFO_DLC);
248 if (rxdlc & IFI_CANFD_RXFIFO_DLC_EDL)
249 skb = alloc_canfd_skb(ndev, &cf);
251 skb = alloc_can_skb(ndev, (struct can_frame **)&cf);
258 dlc = (rxdlc >> IFI_CANFD_RXFIFO_DLC_DLC_OFFSET) &
259 IFI_CANFD_RXFIFO_DLC_DLC_MASK;
260 if (rxdlc & IFI_CANFD_RXFIFO_DLC_EDL)
261 cf->len = can_dlc2len(dlc);
263 cf->len = get_can_dlc(dlc);
265 rxid = readl(priv->base + IFI_CANFD_RXFIFO_ID);
266 id = (rxid >> IFI_CANFD_RXFIFO_ID_ID_OFFSET);
267 if (id & IFI_CANFD_RXFIFO_ID_IDE) {
268 id &= IFI_CANFD_RXFIFO_ID_ID_XTD_MASK;
270 * In case the Extended ID frame is received, the standard
271 * and extended part of the ID are swapped in the register,
272 * so swap them back to obtain the correct ID.
274 id = (id >> IFI_CANFD_RXFIFO_ID_ID_XTD_OFFSET) |
275 ((id & IFI_CANFD_RXFIFO_ID_ID_STD_MASK) <<
276 IFI_CANFD_RXFIFO_ID_ID_XTD_WIDTH);
279 id &= IFI_CANFD_RXFIFO_ID_ID_STD_MASK;
283 if (rxdlc & IFI_CANFD_RXFIFO_DLC_ESI) {
284 cf->flags |= CANFD_ESI;
285 netdev_dbg(ndev, "ESI Error\n");
288 if (!(rxdlc & IFI_CANFD_RXFIFO_DLC_EDL) &&
289 (rxdlc & IFI_CANFD_RXFIFO_DLC_RTR)) {
290 cf->can_id |= CAN_RTR_FLAG;
292 if (rxdlc & IFI_CANFD_RXFIFO_DLC_BRS)
293 cf->flags |= CANFD_BRS;
295 for (i = 0; i < cf->len; i += 4) {
296 *(u32 *)(cf->data + i) =
297 readl(priv->base + IFI_CANFD_RXFIFO_DATA + i);
301 /* Remove the packet from FIFO */
302 writel(IFI_CANFD_RXSTCMD_REMOVE_MSG, priv->base + IFI_CANFD_RXSTCMD);
303 writel(rx_irq_mask, priv->base + IFI_CANFD_INTERRUPT);
306 stats->rx_bytes += cf->len;
308 netif_receive_skb(skb);
311 static int ifi_canfd_do_rx_poll(struct net_device *ndev, int quota)
313 struct ifi_canfd_priv *priv = netdev_priv(ndev);
317 rxst = readl(priv->base + IFI_CANFD_RXSTCMD);
318 if (rxst & IFI_CANFD_RXSTCMD_EMPTY) {
319 netdev_dbg(ndev, "No messages in RX FIFO\n");
324 if (rxst & IFI_CANFD_RXSTCMD_EMPTY)
329 ifi_canfd_read_fifo(ndev);
332 rxst = readl(priv->base + IFI_CANFD_RXSTCMD);
336 can_led_event(ndev, CAN_LED_EVENT_RX);
341 static int ifi_canfd_handle_lost_msg(struct net_device *ndev)
343 struct net_device_stats *stats = &ndev->stats;
345 struct can_frame *frame;
347 netdev_err(ndev, "RX FIFO overflow, message(s) lost.\n");
350 stats->rx_over_errors++;
352 skb = alloc_can_err_skb(ndev, &frame);
356 frame->can_id |= CAN_ERR_CRTL;
357 frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
359 netif_receive_skb(skb);
364 static int ifi_canfd_handle_lec_err(struct net_device *ndev, const u32 errctr)
366 struct ifi_canfd_priv *priv = netdev_priv(ndev);
367 struct net_device_stats *stats = &ndev->stats;
368 struct can_frame *cf;
370 const u32 errmask = IFI_CANFD_ERROR_CTR_OVERLOAD_FIRST |
371 IFI_CANFD_ERROR_CTR_ACK_ERROR_FIRST |
372 IFI_CANFD_ERROR_CTR_BIT0_ERROR_FIRST |
373 IFI_CANFD_ERROR_CTR_BIT1_ERROR_FIRST |
374 IFI_CANFD_ERROR_CTR_STUFF_ERROR_FIRST |
375 IFI_CANFD_ERROR_CTR_CRC_ERROR_FIRST |
376 IFI_CANFD_ERROR_CTR_FORM_ERROR_FIRST;
378 if (!(errctr & errmask)) /* No error happened. */
381 priv->can.can_stats.bus_error++;
384 /* Propagate the error condition to the CAN stack. */
385 skb = alloc_can_err_skb(ndev, &cf);
389 /* Read the error counter register and check for new errors. */
390 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
392 if (errctr & IFI_CANFD_ERROR_CTR_OVERLOAD_FIRST)
393 cf->data[2] |= CAN_ERR_PROT_OVERLOAD;
395 if (errctr & IFI_CANFD_ERROR_CTR_ACK_ERROR_FIRST)
396 cf->data[3] = CAN_ERR_PROT_LOC_ACK;
398 if (errctr & IFI_CANFD_ERROR_CTR_BIT0_ERROR_FIRST)
399 cf->data[2] |= CAN_ERR_PROT_BIT0;
401 if (errctr & IFI_CANFD_ERROR_CTR_BIT1_ERROR_FIRST)
402 cf->data[2] |= CAN_ERR_PROT_BIT1;
404 if (errctr & IFI_CANFD_ERROR_CTR_STUFF_ERROR_FIRST)
405 cf->data[2] |= CAN_ERR_PROT_STUFF;
407 if (errctr & IFI_CANFD_ERROR_CTR_CRC_ERROR_FIRST)
408 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
410 if (errctr & IFI_CANFD_ERROR_CTR_FORM_ERROR_FIRST)
411 cf->data[2] |= CAN_ERR_PROT_FORM;
413 /* Reset the error counter, ack the IRQ and re-enable the counter. */
414 writel(IFI_CANFD_ERROR_CTR_ER_RESET, priv->base + IFI_CANFD_ERROR_CTR);
415 writel(IFI_CANFD_INTERRUPT_ERROR_COUNTER,
416 priv->base + IFI_CANFD_INTERRUPT);
417 writel(IFI_CANFD_ERROR_CTR_ER_ENABLE, priv->base + IFI_CANFD_ERROR_CTR);
420 stats->rx_bytes += cf->can_dlc;
421 netif_receive_skb(skb);
426 static int ifi_canfd_get_berr_counter(const struct net_device *ndev,
427 struct can_berr_counter *bec)
429 struct ifi_canfd_priv *priv = netdev_priv(ndev);
432 err = readl(priv->base + IFI_CANFD_ERROR);
433 bec->rxerr = (err >> IFI_CANFD_ERROR_RX_OFFSET) &
434 IFI_CANFD_ERROR_RX_MASK;
435 bec->txerr = (err >> IFI_CANFD_ERROR_TX_OFFSET) &
436 IFI_CANFD_ERROR_TX_MASK;
441 static int ifi_canfd_handle_state_change(struct net_device *ndev,
442 enum can_state new_state)
444 struct ifi_canfd_priv *priv = netdev_priv(ndev);
445 struct net_device_stats *stats = &ndev->stats;
446 struct can_frame *cf;
448 struct can_berr_counter bec;
451 case CAN_STATE_ERROR_ACTIVE:
452 /* error warning state */
453 priv->can.can_stats.error_warning++;
454 priv->can.state = CAN_STATE_ERROR_WARNING;
456 case CAN_STATE_ERROR_PASSIVE:
457 /* error passive state */
458 priv->can.can_stats.error_passive++;
459 priv->can.state = CAN_STATE_ERROR_PASSIVE;
461 case CAN_STATE_BUS_OFF:
463 priv->can.state = CAN_STATE_BUS_OFF;
464 ifi_canfd_irq_enable(ndev, 0);
465 priv->can.can_stats.bus_off++;
472 /* propagate the error condition to the CAN stack */
473 skb = alloc_can_err_skb(ndev, &cf);
477 ifi_canfd_get_berr_counter(ndev, &bec);
480 case CAN_STATE_ERROR_ACTIVE:
481 /* error warning state */
482 cf->can_id |= CAN_ERR_CRTL;
483 cf->data[1] = (bec.txerr > bec.rxerr) ?
484 CAN_ERR_CRTL_TX_WARNING :
485 CAN_ERR_CRTL_RX_WARNING;
486 cf->data[6] = bec.txerr;
487 cf->data[7] = bec.rxerr;
489 case CAN_STATE_ERROR_PASSIVE:
490 /* error passive state */
491 cf->can_id |= CAN_ERR_CRTL;
492 cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
494 cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
495 cf->data[6] = bec.txerr;
496 cf->data[7] = bec.rxerr;
498 case CAN_STATE_BUS_OFF:
500 cf->can_id |= CAN_ERR_BUSOFF;
507 stats->rx_bytes += cf->can_dlc;
508 netif_receive_skb(skb);
513 static int ifi_canfd_handle_state_errors(struct net_device *ndev, u32 stcmd)
515 struct ifi_canfd_priv *priv = netdev_priv(ndev);
520 * The ErrWarn condition is a little special, since the bit is
521 * located in the INTERRUPT register instead of STCMD register.
523 isr = readl(priv->base + IFI_CANFD_INTERRUPT);
524 if ((isr & IFI_CANFD_INTERRUPT_ERROR_WARNING) &&
525 (priv->can.state != CAN_STATE_ERROR_WARNING)) {
526 /* Clear the interrupt */
527 writel(IFI_CANFD_INTERRUPT_ERROR_WARNING,
528 priv->base + IFI_CANFD_INTERRUPT);
529 netdev_dbg(ndev, "Error, entered warning state\n");
530 work_done += ifi_canfd_handle_state_change(ndev,
531 CAN_STATE_ERROR_WARNING);
534 if ((stcmd & IFI_CANFD_STCMD_ERROR_PASSIVE) &&
535 (priv->can.state != CAN_STATE_ERROR_PASSIVE)) {
536 netdev_dbg(ndev, "Error, entered passive state\n");
537 work_done += ifi_canfd_handle_state_change(ndev,
538 CAN_STATE_ERROR_PASSIVE);
541 if ((stcmd & IFI_CANFD_STCMD_BUSOFF) &&
542 (priv->can.state != CAN_STATE_BUS_OFF)) {
543 netdev_dbg(ndev, "Error, entered bus-off state\n");
544 work_done += ifi_canfd_handle_state_change(ndev,
551 static int ifi_canfd_poll(struct napi_struct *napi, int quota)
553 struct net_device *ndev = napi->dev;
554 struct ifi_canfd_priv *priv = netdev_priv(ndev);
555 const u32 stcmd_state_mask = IFI_CANFD_STCMD_ERROR_PASSIVE |
556 IFI_CANFD_STCMD_BUSOFF;
559 u32 stcmd = readl(priv->base + IFI_CANFD_STCMD);
560 u32 rxstcmd = readl(priv->base + IFI_CANFD_STCMD);
561 u32 errctr = readl(priv->base + IFI_CANFD_ERROR_CTR);
563 /* Handle bus state changes */
564 if ((stcmd & stcmd_state_mask) ||
565 ((stcmd & IFI_CANFD_STCMD_ERROR_ACTIVE) == 0))
566 work_done += ifi_canfd_handle_state_errors(ndev, stcmd);
568 /* Handle lost messages on RX */
569 if (rxstcmd & IFI_CANFD_RXSTCMD_OVERFLOW)
570 work_done += ifi_canfd_handle_lost_msg(ndev);
572 /* Handle lec errors on the bus */
573 if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
574 work_done += ifi_canfd_handle_lec_err(ndev, errctr);
576 /* Handle normal messages on RX */
577 if (!(rxstcmd & IFI_CANFD_RXSTCMD_EMPTY))
578 work_done += ifi_canfd_do_rx_poll(ndev, quota - work_done);
580 if (work_done < quota) {
582 ifi_canfd_irq_enable(ndev, 1);
588 static irqreturn_t ifi_canfd_isr(int irq, void *dev_id)
590 struct net_device *ndev = (struct net_device *)dev_id;
591 struct ifi_canfd_priv *priv = netdev_priv(ndev);
592 struct net_device_stats *stats = &ndev->stats;
593 const u32 rx_irq_mask = IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY |
594 IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY_PER |
595 IFI_CANFD_INTERRUPT_ERROR_WARNING |
596 IFI_CANFD_INTERRUPT_ERROR_COUNTER;
597 const u32 tx_irq_mask = IFI_CANFD_INTERRUPT_TXFIFO_EMPTY |
598 IFI_CANFD_INTERRUPT_TXFIFO_REMOVE;
599 const u32 clr_irq_mask = ~((u32)(IFI_CANFD_INTERRUPT_SET_IRQ |
600 IFI_CANFD_INTERRUPT_ERROR_WARNING));
603 isr = readl(priv->base + IFI_CANFD_INTERRUPT);
609 /* Clear all pending interrupts but ErrWarn */
610 writel(clr_irq_mask, priv->base + IFI_CANFD_INTERRUPT);
612 /* RX IRQ or bus warning, start NAPI */
613 if (isr & rx_irq_mask) {
614 ifi_canfd_irq_enable(ndev, 0);
615 napi_schedule(&priv->napi);
619 if (isr & IFI_CANFD_INTERRUPT_TXFIFO_REMOVE) {
620 stats->tx_bytes += can_get_echo_skb(ndev, 0);
622 can_led_event(ndev, CAN_LED_EVENT_TX);
625 if (isr & tx_irq_mask)
626 netif_wake_queue(ndev);
631 static const struct can_bittiming_const ifi_canfd_bittiming_const = {
632 .name = KBUILD_MODNAME,
633 .tseg1_min = 1, /* Time segment 1 = prop_seg + phase_seg1 */
635 .tseg2_min = 2, /* Time segment 2 = phase_seg2 */
643 static void ifi_canfd_set_bittiming(struct net_device *ndev)
645 struct ifi_canfd_priv *priv = netdev_priv(ndev);
646 const struct can_bittiming *bt = &priv->can.bittiming;
647 const struct can_bittiming *dbt = &priv->can.data_bittiming;
648 u16 brp, sjw, tseg1, tseg2, tdc;
650 /* Configure bit timing */
653 tseg1 = bt->prop_seg + bt->phase_seg1 - 1;
654 tseg2 = bt->phase_seg2 - 2;
655 writel((tseg2 << IFI_CANFD_TIME_TIMEB_OFF) |
656 (tseg1 << IFI_CANFD_TIME_TIMEA_OFF) |
657 (brp << IFI_CANFD_TIME_PRESCALE_OFF) |
658 (sjw << IFI_CANFD_TIME_SJW_OFF_7_9_8_8),
659 priv->base + IFI_CANFD_TIME);
661 /* Configure data bit timing */
664 tseg1 = dbt->prop_seg + dbt->phase_seg1 - 1;
665 tseg2 = dbt->phase_seg2 - 2;
666 writel((tseg2 << IFI_CANFD_TIME_TIMEB_OFF) |
667 (tseg1 << IFI_CANFD_TIME_TIMEA_OFF) |
668 (brp << IFI_CANFD_TIME_PRESCALE_OFF) |
669 (sjw << IFI_CANFD_TIME_SJW_OFF_7_9_8_8),
670 priv->base + IFI_CANFD_FTIME);
672 /* Configure transmitter delay */
673 tdc = (dbt->brp * (dbt->phase_seg1 + 1)) & IFI_CANFD_TDELAY_MASK;
674 writel(IFI_CANFD_TDELAY_EN | IFI_CANFD_TDELAY_ABS | tdc,
675 priv->base + IFI_CANFD_TDELAY);
678 static void ifi_canfd_set_filter(struct net_device *ndev, const u32 id,
679 const u32 mask, const u32 ident)
681 struct ifi_canfd_priv *priv = netdev_priv(ndev);
683 writel(mask, priv->base + IFI_CANFD_FILTER_MASK(id));
684 writel(ident, priv->base + IFI_CANFD_FILTER_IDENT(id));
687 static void ifi_canfd_set_filters(struct net_device *ndev)
689 /* Receive all CAN frames (standard ID) */
690 ifi_canfd_set_filter(ndev, 0,
691 IFI_CANFD_FILTER_MASK_VALID |
692 IFI_CANFD_FILTER_MASK_EXT,
693 IFI_CANFD_FILTER_IDENT_VALID);
695 /* Receive all CAN frames (extended ID) */
696 ifi_canfd_set_filter(ndev, 1,
697 IFI_CANFD_FILTER_MASK_VALID |
698 IFI_CANFD_FILTER_MASK_EXT,
699 IFI_CANFD_FILTER_IDENT_VALID |
700 IFI_CANFD_FILTER_IDENT_IDE);
702 /* Receive all CANFD frames */
703 ifi_canfd_set_filter(ndev, 2,
704 IFI_CANFD_FILTER_MASK_VALID |
705 IFI_CANFD_FILTER_MASK_EDL |
706 IFI_CANFD_FILTER_MASK_EXT,
707 IFI_CANFD_FILTER_IDENT_VALID |
708 IFI_CANFD_FILTER_IDENT_CANFD |
709 IFI_CANFD_FILTER_IDENT_IDE);
712 static void ifi_canfd_start(struct net_device *ndev)
714 struct ifi_canfd_priv *priv = netdev_priv(ndev);
718 writel(IFI_CANFD_STCMD_HARDRESET, priv->base + IFI_CANFD_STCMD);
719 writel(IFI_CANFD_STCMD_ENABLE_7_9_8_8_TIMING,
720 priv->base + IFI_CANFD_STCMD);
722 ifi_canfd_set_bittiming(ndev);
723 ifi_canfd_set_filters(ndev);
726 writel(IFI_CANFD_RXSTCMD_RESET, priv->base + IFI_CANFD_RXSTCMD);
727 writel(0, priv->base + IFI_CANFD_RXSTCMD);
728 writel(IFI_CANFD_TXSTCMD_RESET, priv->base + IFI_CANFD_TXSTCMD);
729 writel(0, priv->base + IFI_CANFD_TXSTCMD);
731 /* Repeat transmission until successful */
732 writel(0, priv->base + IFI_CANFD_REPEAT);
733 writel(0, priv->base + IFI_CANFD_SUSPEND);
735 /* Clear all pending interrupts */
736 writel((u32)(~IFI_CANFD_INTERRUPT_SET_IRQ),
737 priv->base + IFI_CANFD_INTERRUPT);
739 stcmd = IFI_CANFD_STCMD_ENABLE | IFI_CANFD_STCMD_NORMAL_MODE |
740 IFI_CANFD_STCMD_ENABLE_7_9_8_8_TIMING;
742 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
743 stcmd |= IFI_CANFD_STCMD_BUSMONITOR;
745 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
746 stcmd |= IFI_CANFD_STCMD_LOOPBACK;
748 if ((priv->can.ctrlmode & CAN_CTRLMODE_FD) &&
749 !(priv->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO))
750 stcmd |= IFI_CANFD_STCMD_ENABLE_ISO;
752 if (!(priv->can.ctrlmode & CAN_CTRLMODE_FD))
753 stcmd |= IFI_CANFD_STCMD_DISABLE_CANFD;
755 priv->can.state = CAN_STATE_ERROR_ACTIVE;
757 ifi_canfd_irq_enable(ndev, 1);
759 /* Unlock, reset and enable the error counter. */
760 writel(IFI_CANFD_ERROR_CTR_UNLOCK_MAGIC,
761 priv->base + IFI_CANFD_ERROR_CTR);
762 writel(IFI_CANFD_ERROR_CTR_ER_RESET, priv->base + IFI_CANFD_ERROR_CTR);
763 writel(IFI_CANFD_ERROR_CTR_ER_ENABLE, priv->base + IFI_CANFD_ERROR_CTR);
765 /* Enable controller */
766 writel(stcmd, priv->base + IFI_CANFD_STCMD);
769 static void ifi_canfd_stop(struct net_device *ndev)
771 struct ifi_canfd_priv *priv = netdev_priv(ndev);
773 /* Reset and disable the error counter. */
774 writel(IFI_CANFD_ERROR_CTR_ER_RESET, priv->base + IFI_CANFD_ERROR_CTR);
775 writel(0, priv->base + IFI_CANFD_ERROR_CTR);
778 writel(IFI_CANFD_STCMD_HARDRESET, priv->base + IFI_CANFD_STCMD);
780 /* Mask all interrupts */
781 writel(~0, priv->base + IFI_CANFD_IRQMASK);
783 /* Clear all pending interrupts */
784 writel((u32)(~IFI_CANFD_INTERRUPT_SET_IRQ),
785 priv->base + IFI_CANFD_INTERRUPT);
787 /* Set the state as STOPPED */
788 priv->can.state = CAN_STATE_STOPPED;
791 static int ifi_canfd_set_mode(struct net_device *ndev, enum can_mode mode)
795 ifi_canfd_start(ndev);
796 netif_wake_queue(ndev);
805 static int ifi_canfd_open(struct net_device *ndev)
807 struct ifi_canfd_priv *priv = netdev_priv(ndev);
810 ret = open_candev(ndev);
812 netdev_err(ndev, "Failed to open CAN device\n");
816 /* Register interrupt handler */
817 ret = request_irq(ndev->irq, ifi_canfd_isr, IRQF_SHARED,
820 netdev_err(ndev, "Failed to request interrupt\n");
824 ifi_canfd_start(ndev);
826 can_led_event(ndev, CAN_LED_EVENT_OPEN);
827 napi_enable(&priv->napi);
828 netif_start_queue(ndev);
836 static int ifi_canfd_close(struct net_device *ndev)
838 struct ifi_canfd_priv *priv = netdev_priv(ndev);
840 netif_stop_queue(ndev);
841 napi_disable(&priv->napi);
843 ifi_canfd_stop(ndev);
845 free_irq(ndev->irq, ndev);
849 can_led_event(ndev, CAN_LED_EVENT_STOP);
854 static netdev_tx_t ifi_canfd_start_xmit(struct sk_buff *skb,
855 struct net_device *ndev)
857 struct ifi_canfd_priv *priv = netdev_priv(ndev);
858 struct canfd_frame *cf = (struct canfd_frame *)skb->data;
859 u32 txst, txid, txdlc;
862 if (can_dropped_invalid_skb(ndev, skb))
865 /* Check if the TX buffer is full */
866 txst = readl(priv->base + IFI_CANFD_TXSTCMD);
867 if (txst & IFI_CANFD_TXSTCMD_FULL) {
868 netif_stop_queue(ndev);
869 netdev_err(ndev, "BUG! TX FIFO full when queue awake!\n");
870 return NETDEV_TX_BUSY;
873 netif_stop_queue(ndev);
875 if (cf->can_id & CAN_EFF_FLAG) {
876 txid = cf->can_id & CAN_EFF_MASK;
878 * In case the Extended ID frame is transmitted, the
879 * standard and extended part of the ID are swapped
880 * in the register, so swap them back to send the
883 txid = (txid >> IFI_CANFD_TXFIFO_ID_ID_XTD_WIDTH) |
884 ((txid & IFI_CANFD_TXFIFO_ID_ID_XTD_MASK) <<
885 IFI_CANFD_TXFIFO_ID_ID_XTD_OFFSET);
886 txid |= IFI_CANFD_TXFIFO_ID_IDE;
888 txid = cf->can_id & CAN_SFF_MASK;
891 txdlc = can_len2dlc(cf->len);
892 if ((priv->can.ctrlmode & CAN_CTRLMODE_FD) && can_is_canfd_skb(skb)) {
893 txdlc |= IFI_CANFD_TXFIFO_DLC_EDL;
894 if (cf->flags & CANFD_BRS)
895 txdlc |= IFI_CANFD_TXFIFO_DLC_BRS;
898 if (cf->can_id & CAN_RTR_FLAG)
899 txdlc |= IFI_CANFD_TXFIFO_DLC_RTR;
901 /* message ram configuration */
902 writel(txid, priv->base + IFI_CANFD_TXFIFO_ID);
903 writel(txdlc, priv->base + IFI_CANFD_TXFIFO_DLC);
905 for (i = 0; i < cf->len; i += 4) {
906 writel(*(u32 *)(cf->data + i),
907 priv->base + IFI_CANFD_TXFIFO_DATA + i);
910 writel(0, priv->base + IFI_CANFD_TXFIFO_REPEATCOUNT);
911 writel(0, priv->base + IFI_CANFD_TXFIFO_SUSPEND_US);
913 can_put_echo_skb(skb, ndev, 0);
915 /* Start the transmission */
916 writel(IFI_CANFD_TXSTCMD_ADD_MSG, priv->base + IFI_CANFD_TXSTCMD);
921 static const struct net_device_ops ifi_canfd_netdev_ops = {
922 .ndo_open = ifi_canfd_open,
923 .ndo_stop = ifi_canfd_close,
924 .ndo_start_xmit = ifi_canfd_start_xmit,
925 .ndo_change_mtu = can_change_mtu,
928 static int ifi_canfd_plat_probe(struct platform_device *pdev)
930 struct device *dev = &pdev->dev;
931 struct net_device *ndev;
932 struct ifi_canfd_priv *priv;
933 struct resource *res;
938 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
939 addr = devm_ioremap_resource(dev, res);
940 irq = platform_get_irq(pdev, 0);
941 if (IS_ERR(addr) || irq < 0)
944 id = readl(addr + IFI_CANFD_IP_ID);
945 if (id != IFI_CANFD_IP_ID_VALUE) {
946 dev_err(dev, "This block is not IFI CANFD, id=%08x\n", id);
950 ndev = alloc_candev(sizeof(*priv), 1);
955 ndev->flags |= IFF_ECHO; /* we support local echo */
956 ndev->netdev_ops = &ifi_canfd_netdev_ops;
958 priv = netdev_priv(ndev);
962 netif_napi_add(ndev, &priv->napi, ifi_canfd_poll, 64);
964 priv->can.state = CAN_STATE_STOPPED;
966 priv->can.clock.freq = readl(addr + IFI_CANFD_CANCLOCK);
968 priv->can.bittiming_const = &ifi_canfd_bittiming_const;
969 priv->can.data_bittiming_const = &ifi_canfd_bittiming_const;
970 priv->can.do_set_mode = ifi_canfd_set_mode;
971 priv->can.do_get_berr_counter = ifi_canfd_get_berr_counter;
973 /* IFI CANFD can do both Bosch FD and ISO FD */
974 priv->can.ctrlmode = CAN_CTRLMODE_FD;
976 /* IFI CANFD can do both Bosch FD and ISO FD */
977 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
978 CAN_CTRLMODE_LISTENONLY |
980 CAN_CTRLMODE_FD_NON_ISO |
981 CAN_CTRLMODE_BERR_REPORTING;
983 platform_set_drvdata(pdev, ndev);
984 SET_NETDEV_DEV(ndev, dev);
986 ret = register_candev(ndev);
988 dev_err(dev, "Failed to register (ret=%d)\n", ret);
992 devm_can_led_init(ndev);
994 dev_info(dev, "Driver registered: regs=%p, irq=%d, clock=%d\n",
995 priv->base, ndev->irq, priv->can.clock.freq);
1004 static int ifi_canfd_plat_remove(struct platform_device *pdev)
1006 struct net_device *ndev = platform_get_drvdata(pdev);
1008 unregister_candev(ndev);
1009 platform_set_drvdata(pdev, NULL);
1015 static const struct of_device_id ifi_canfd_of_table[] = {
1016 { .compatible = "ifi,canfd-1.0", .data = NULL },
1019 MODULE_DEVICE_TABLE(of, ifi_canfd_of_table);
1021 static struct platform_driver ifi_canfd_plat_driver = {
1023 .name = KBUILD_MODNAME,
1024 .of_match_table = ifi_canfd_of_table,
1026 .probe = ifi_canfd_plat_probe,
1027 .remove = ifi_canfd_plat_remove,
1030 module_platform_driver(ifi_canfd_plat_driver);
1032 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
1033 MODULE_LICENSE("GPL v2");
1034 MODULE_DESCRIPTION("CAN bus driver for IFI CANFD controller");