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
85 #define IFI_CANFD_ERROR 0x20
86 #define IFI_CANFD_ERROR_TX_OFFSET 0
87 #define IFI_CANFD_ERROR_TX_MASK 0xff
88 #define IFI_CANFD_ERROR_RX_OFFSET 16
89 #define IFI_CANFD_ERROR_RX_MASK 0xff
91 #define IFI_CANFD_ERRCNT 0x24
93 #define IFI_CANFD_SUSPEND 0x28
95 #define IFI_CANFD_REPEAT 0x2c
97 #define IFI_CANFD_TRAFFIC 0x30
99 #define IFI_CANFD_TSCONTROL 0x34
101 #define IFI_CANFD_TSC 0x38
103 #define IFI_CANFD_TST 0x3c
105 #define IFI_CANFD_RES1 0x40
107 #define IFI_CANFD_ERROR_CTR 0x44
108 #define IFI_CANFD_ERROR_CTR_UNLOCK_MAGIC 0x21302899
109 #define IFI_CANFD_ERROR_CTR_OVERLOAD_FIRST BIT(0)
110 #define IFI_CANFD_ERROR_CTR_ACK_ERROR_FIRST BIT(1)
111 #define IFI_CANFD_ERROR_CTR_BIT0_ERROR_FIRST BIT(2)
112 #define IFI_CANFD_ERROR_CTR_BIT1_ERROR_FIRST BIT(3)
113 #define IFI_CANFD_ERROR_CTR_STUFF_ERROR_FIRST BIT(4)
114 #define IFI_CANFD_ERROR_CTR_CRC_ERROR_FIRST BIT(5)
115 #define IFI_CANFD_ERROR_CTR_FORM_ERROR_FIRST BIT(6)
116 #define IFI_CANFD_ERROR_CTR_OVERLOAD_ALL BIT(8)
117 #define IFI_CANFD_ERROR_CTR_ACK_ERROR_ALL BIT(9)
118 #define IFI_CANFD_ERROR_CTR_BIT0_ERROR_ALL BIT(10)
119 #define IFI_CANFD_ERROR_CTR_BIT1_ERROR_ALL BIT(11)
120 #define IFI_CANFD_ERROR_CTR_STUFF_ERROR_ALL BIT(12)
121 #define IFI_CANFD_ERROR_CTR_CRC_ERROR_ALL BIT(13)
122 #define IFI_CANFD_ERROR_CTR_FORM_ERROR_ALL BIT(14)
123 #define IFI_CANFD_ERROR_CTR_BITPOSITION_OFFSET 16
124 #define IFI_CANFD_ERROR_CTR_BITPOSITION_MASK 0xff
125 #define IFI_CANFD_ERROR_CTR_ER_RESET BIT(30)
126 #define IFI_CANFD_ERROR_CTR_ER_ENABLE ((u32)BIT(31))
128 #define IFI_CANFD_PAR 0x48
130 #define IFI_CANFD_CANCLOCK 0x4c
132 #define IFI_CANFD_SYSCLOCK 0x50
134 #define IFI_CANFD_VER 0x54
136 #define IFI_CANFD_IP_ID 0x58
137 #define IFI_CANFD_IP_ID_VALUE 0xD073CAFD
139 #define IFI_CANFD_TEST 0x5c
141 #define IFI_CANFD_RXFIFO_TS_63_32 0x60
143 #define IFI_CANFD_RXFIFO_TS_31_0 0x64
145 #define IFI_CANFD_RXFIFO_DLC 0x68
146 #define IFI_CANFD_RXFIFO_DLC_DLC_OFFSET 0
147 #define IFI_CANFD_RXFIFO_DLC_DLC_MASK 0xf
148 #define IFI_CANFD_RXFIFO_DLC_RTR BIT(4)
149 #define IFI_CANFD_RXFIFO_DLC_EDL BIT(5)
150 #define IFI_CANFD_RXFIFO_DLC_BRS BIT(6)
151 #define IFI_CANFD_RXFIFO_DLC_ESI BIT(7)
152 #define IFI_CANFD_RXFIFO_DLC_OBJ_OFFSET 8
153 #define IFI_CANFD_RXFIFO_DLC_OBJ_MASK 0x1ff
154 #define IFI_CANFD_RXFIFO_DLC_FNR_OFFSET 24
155 #define IFI_CANFD_RXFIFO_DLC_FNR_MASK 0xff
157 #define IFI_CANFD_RXFIFO_ID 0x6c
158 #define IFI_CANFD_RXFIFO_ID_ID_OFFSET 0
159 #define IFI_CANFD_RXFIFO_ID_ID_STD_MASK CAN_SFF_MASK
160 #define IFI_CANFD_RXFIFO_ID_ID_STD_OFFSET 0
161 #define IFI_CANFD_RXFIFO_ID_ID_STD_WIDTH 10
162 #define IFI_CANFD_RXFIFO_ID_ID_XTD_MASK CAN_EFF_MASK
163 #define IFI_CANFD_RXFIFO_ID_ID_XTD_OFFSET 11
164 #define IFI_CANFD_RXFIFO_ID_ID_XTD_WIDTH 18
165 #define IFI_CANFD_RXFIFO_ID_IDE BIT(29)
167 #define IFI_CANFD_RXFIFO_DATA 0x70 /* 0x70..0xac */
169 #define IFI_CANFD_TXFIFO_SUSPEND_US 0xb0
171 #define IFI_CANFD_TXFIFO_REPEATCOUNT 0xb4
173 #define IFI_CANFD_TXFIFO_DLC 0xb8
174 #define IFI_CANFD_TXFIFO_DLC_DLC_OFFSET 0
175 #define IFI_CANFD_TXFIFO_DLC_DLC_MASK 0xf
176 #define IFI_CANFD_TXFIFO_DLC_RTR BIT(4)
177 #define IFI_CANFD_TXFIFO_DLC_EDL BIT(5)
178 #define IFI_CANFD_TXFIFO_DLC_BRS BIT(6)
179 #define IFI_CANFD_TXFIFO_DLC_FNR_OFFSET 24
180 #define IFI_CANFD_TXFIFO_DLC_FNR_MASK 0xff
182 #define IFI_CANFD_TXFIFO_ID 0xbc
183 #define IFI_CANFD_TXFIFO_ID_ID_OFFSET 0
184 #define IFI_CANFD_TXFIFO_ID_ID_STD_MASK CAN_SFF_MASK
185 #define IFI_CANFD_TXFIFO_ID_ID_STD_OFFSET 0
186 #define IFI_CANFD_TXFIFO_ID_ID_STD_WIDTH 10
187 #define IFI_CANFD_TXFIFO_ID_ID_XTD_MASK CAN_EFF_MASK
188 #define IFI_CANFD_TXFIFO_ID_ID_XTD_OFFSET 11
189 #define IFI_CANFD_TXFIFO_ID_ID_XTD_WIDTH 18
190 #define IFI_CANFD_TXFIFO_ID_IDE BIT(29)
192 #define IFI_CANFD_TXFIFO_DATA 0xc0 /* 0xb0..0xfc */
194 #define IFI_CANFD_FILTER_MASK(n) (0x800 + ((n) * 8) + 0)
195 #define IFI_CANFD_FILTER_MASK_EXT BIT(29)
196 #define IFI_CANFD_FILTER_MASK_EDL BIT(30)
197 #define IFI_CANFD_FILTER_MASK_VALID ((u32)BIT(31))
199 #define IFI_CANFD_FILTER_IDENT(n) (0x800 + ((n) * 8) + 4)
200 #define IFI_CANFD_FILTER_IDENT_IDE BIT(29)
201 #define IFI_CANFD_FILTER_IDENT_CANFD BIT(30)
202 #define IFI_CANFD_FILTER_IDENT_VALID ((u32)BIT(31))
204 /* IFI CANFD private data structure */
205 struct ifi_canfd_priv {
206 struct can_priv can; /* must be the first member */
207 struct napi_struct napi;
208 struct net_device *ndev;
212 static void ifi_canfd_irq_enable(struct net_device *ndev, bool enable)
214 struct ifi_canfd_priv *priv = netdev_priv(ndev);
218 enirq = IFI_CANFD_IRQMASK_TXFIFO_EMPTY |
219 IFI_CANFD_IRQMASK_RXFIFO_NEMPTY;
220 if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
221 enirq |= IFI_CANFD_INTERRUPT_ERROR_COUNTER;
224 writel(IFI_CANFD_IRQMASK_SET_ERR |
225 IFI_CANFD_IRQMASK_SET_TS |
226 IFI_CANFD_IRQMASK_SET_TX |
227 IFI_CANFD_IRQMASK_SET_RX | enirq,
228 priv->base + IFI_CANFD_IRQMASK);
231 static void ifi_canfd_read_fifo(struct net_device *ndev)
233 struct net_device_stats *stats = &ndev->stats;
234 struct ifi_canfd_priv *priv = netdev_priv(ndev);
235 struct canfd_frame *cf;
237 const u32 rx_irq_mask = IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY |
238 IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY_PER;
243 rxdlc = readl(priv->base + IFI_CANFD_RXFIFO_DLC);
244 if (rxdlc & IFI_CANFD_RXFIFO_DLC_EDL)
245 skb = alloc_canfd_skb(ndev, &cf);
247 skb = alloc_can_skb(ndev, (struct can_frame **)&cf);
254 dlc = (rxdlc >> IFI_CANFD_RXFIFO_DLC_DLC_OFFSET) &
255 IFI_CANFD_RXFIFO_DLC_DLC_MASK;
256 if (rxdlc & IFI_CANFD_RXFIFO_DLC_EDL)
257 cf->len = can_dlc2len(dlc);
259 cf->len = get_can_dlc(dlc);
261 rxid = readl(priv->base + IFI_CANFD_RXFIFO_ID);
262 id = (rxid >> IFI_CANFD_RXFIFO_ID_ID_OFFSET);
263 if (id & IFI_CANFD_RXFIFO_ID_IDE) {
264 id &= IFI_CANFD_RXFIFO_ID_ID_XTD_MASK;
266 * In case the Extended ID frame is received, the standard
267 * and extended part of the ID are swapped in the register,
268 * so swap them back to obtain the correct ID.
270 id = (id >> IFI_CANFD_RXFIFO_ID_ID_XTD_OFFSET) |
271 ((id & IFI_CANFD_RXFIFO_ID_ID_STD_MASK) <<
272 IFI_CANFD_RXFIFO_ID_ID_XTD_WIDTH);
275 id &= IFI_CANFD_RXFIFO_ID_ID_STD_MASK;
279 if (rxdlc & IFI_CANFD_RXFIFO_DLC_ESI) {
280 cf->flags |= CANFD_ESI;
281 netdev_dbg(ndev, "ESI Error\n");
284 if (!(rxdlc & IFI_CANFD_RXFIFO_DLC_EDL) &&
285 (rxdlc & IFI_CANFD_RXFIFO_DLC_RTR)) {
286 cf->can_id |= CAN_RTR_FLAG;
288 if (rxdlc & IFI_CANFD_RXFIFO_DLC_BRS)
289 cf->flags |= CANFD_BRS;
291 for (i = 0; i < cf->len; i += 4) {
292 *(u32 *)(cf->data + i) =
293 readl(priv->base + IFI_CANFD_RXFIFO_DATA + i);
297 /* Remove the packet from FIFO */
298 writel(IFI_CANFD_RXSTCMD_REMOVE_MSG, priv->base + IFI_CANFD_RXSTCMD);
299 writel(rx_irq_mask, priv->base + IFI_CANFD_INTERRUPT);
302 stats->rx_bytes += cf->len;
304 netif_receive_skb(skb);
307 static int ifi_canfd_do_rx_poll(struct net_device *ndev, int quota)
309 struct ifi_canfd_priv *priv = netdev_priv(ndev);
313 rxst = readl(priv->base + IFI_CANFD_RXSTCMD);
314 if (rxst & IFI_CANFD_RXSTCMD_EMPTY) {
315 netdev_dbg(ndev, "No messages in RX FIFO\n");
320 if (rxst & IFI_CANFD_RXSTCMD_EMPTY)
325 ifi_canfd_read_fifo(ndev);
328 rxst = readl(priv->base + IFI_CANFD_RXSTCMD);
332 can_led_event(ndev, CAN_LED_EVENT_RX);
337 static int ifi_canfd_handle_lost_msg(struct net_device *ndev)
339 struct net_device_stats *stats = &ndev->stats;
341 struct can_frame *frame;
343 netdev_err(ndev, "RX FIFO overflow, message(s) lost.\n");
346 stats->rx_over_errors++;
348 skb = alloc_can_err_skb(ndev, &frame);
352 frame->can_id |= CAN_ERR_CRTL;
353 frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
355 netif_receive_skb(skb);
360 static int ifi_canfd_handle_lec_err(struct net_device *ndev, const u32 errctr)
362 struct ifi_canfd_priv *priv = netdev_priv(ndev);
363 struct net_device_stats *stats = &ndev->stats;
364 struct can_frame *cf;
366 const u32 errmask = IFI_CANFD_ERROR_CTR_OVERLOAD_FIRST |
367 IFI_CANFD_ERROR_CTR_ACK_ERROR_FIRST |
368 IFI_CANFD_ERROR_CTR_BIT0_ERROR_FIRST |
369 IFI_CANFD_ERROR_CTR_BIT1_ERROR_FIRST |
370 IFI_CANFD_ERROR_CTR_STUFF_ERROR_FIRST |
371 IFI_CANFD_ERROR_CTR_CRC_ERROR_FIRST |
372 IFI_CANFD_ERROR_CTR_FORM_ERROR_FIRST;
374 if (!(errctr & errmask)) /* No error happened. */
377 priv->can.can_stats.bus_error++;
380 /* Propagate the error condition to the CAN stack. */
381 skb = alloc_can_err_skb(ndev, &cf);
385 /* Read the error counter register and check for new errors. */
386 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
388 if (errctr & IFI_CANFD_ERROR_CTR_OVERLOAD_FIRST)
389 cf->data[2] |= CAN_ERR_PROT_OVERLOAD;
391 if (errctr & IFI_CANFD_ERROR_CTR_ACK_ERROR_FIRST)
392 cf->data[3] = CAN_ERR_PROT_LOC_ACK;
394 if (errctr & IFI_CANFD_ERROR_CTR_BIT0_ERROR_FIRST)
395 cf->data[2] |= CAN_ERR_PROT_BIT0;
397 if (errctr & IFI_CANFD_ERROR_CTR_BIT1_ERROR_FIRST)
398 cf->data[2] |= CAN_ERR_PROT_BIT1;
400 if (errctr & IFI_CANFD_ERROR_CTR_STUFF_ERROR_FIRST)
401 cf->data[2] |= CAN_ERR_PROT_STUFF;
403 if (errctr & IFI_CANFD_ERROR_CTR_CRC_ERROR_FIRST)
404 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
406 if (errctr & IFI_CANFD_ERROR_CTR_FORM_ERROR_FIRST)
407 cf->data[2] |= CAN_ERR_PROT_FORM;
409 /* Reset the error counter, ack the IRQ and re-enable the counter. */
410 writel(IFI_CANFD_ERROR_CTR_ER_RESET, priv->base + IFI_CANFD_ERROR_CTR);
411 writel(IFI_CANFD_INTERRUPT_ERROR_COUNTER,
412 priv->base + IFI_CANFD_INTERRUPT);
413 writel(IFI_CANFD_ERROR_CTR_ER_ENABLE, priv->base + IFI_CANFD_ERROR_CTR);
416 stats->rx_bytes += cf->can_dlc;
417 netif_receive_skb(skb);
422 static int ifi_canfd_get_berr_counter(const struct net_device *ndev,
423 struct can_berr_counter *bec)
425 struct ifi_canfd_priv *priv = netdev_priv(ndev);
428 err = readl(priv->base + IFI_CANFD_ERROR);
429 bec->rxerr = (err >> IFI_CANFD_ERROR_RX_OFFSET) &
430 IFI_CANFD_ERROR_RX_MASK;
431 bec->txerr = (err >> IFI_CANFD_ERROR_TX_OFFSET) &
432 IFI_CANFD_ERROR_TX_MASK;
437 static int ifi_canfd_handle_state_change(struct net_device *ndev,
438 enum can_state new_state)
440 struct ifi_canfd_priv *priv = netdev_priv(ndev);
441 struct net_device_stats *stats = &ndev->stats;
442 struct can_frame *cf;
444 struct can_berr_counter bec;
447 case CAN_STATE_ERROR_ACTIVE:
448 /* error warning state */
449 priv->can.can_stats.error_warning++;
450 priv->can.state = CAN_STATE_ERROR_WARNING;
452 case CAN_STATE_ERROR_PASSIVE:
453 /* error passive state */
454 priv->can.can_stats.error_passive++;
455 priv->can.state = CAN_STATE_ERROR_PASSIVE;
457 case CAN_STATE_BUS_OFF:
459 priv->can.state = CAN_STATE_BUS_OFF;
460 ifi_canfd_irq_enable(ndev, 0);
461 priv->can.can_stats.bus_off++;
468 /* propagate the error condition to the CAN stack */
469 skb = alloc_can_err_skb(ndev, &cf);
473 ifi_canfd_get_berr_counter(ndev, &bec);
476 case CAN_STATE_ERROR_ACTIVE:
477 /* error warning state */
478 cf->can_id |= CAN_ERR_CRTL;
479 cf->data[1] = (bec.txerr > bec.rxerr) ?
480 CAN_ERR_CRTL_TX_WARNING :
481 CAN_ERR_CRTL_RX_WARNING;
482 cf->data[6] = bec.txerr;
483 cf->data[7] = bec.rxerr;
485 case CAN_STATE_ERROR_PASSIVE:
486 /* error passive state */
487 cf->can_id |= CAN_ERR_CRTL;
488 cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
490 cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
491 cf->data[6] = bec.txerr;
492 cf->data[7] = bec.rxerr;
494 case CAN_STATE_BUS_OFF:
496 cf->can_id |= CAN_ERR_BUSOFF;
503 stats->rx_bytes += cf->can_dlc;
504 netif_receive_skb(skb);
509 static int ifi_canfd_handle_state_errors(struct net_device *ndev, u32 stcmd)
511 struct ifi_canfd_priv *priv = netdev_priv(ndev);
516 * The ErrWarn condition is a little special, since the bit is
517 * located in the INTERRUPT register instead of STCMD register.
519 isr = readl(priv->base + IFI_CANFD_INTERRUPT);
520 if ((isr & IFI_CANFD_INTERRUPT_ERROR_WARNING) &&
521 (priv->can.state != CAN_STATE_ERROR_WARNING)) {
522 /* Clear the interrupt */
523 writel(IFI_CANFD_INTERRUPT_ERROR_WARNING,
524 priv->base + IFI_CANFD_INTERRUPT);
525 netdev_dbg(ndev, "Error, entered warning state\n");
526 work_done += ifi_canfd_handle_state_change(ndev,
527 CAN_STATE_ERROR_WARNING);
530 if ((stcmd & IFI_CANFD_STCMD_ERROR_PASSIVE) &&
531 (priv->can.state != CAN_STATE_ERROR_PASSIVE)) {
532 netdev_dbg(ndev, "Error, entered passive state\n");
533 work_done += ifi_canfd_handle_state_change(ndev,
534 CAN_STATE_ERROR_PASSIVE);
537 if ((stcmd & IFI_CANFD_STCMD_BUSOFF) &&
538 (priv->can.state != CAN_STATE_BUS_OFF)) {
539 netdev_dbg(ndev, "Error, entered bus-off state\n");
540 work_done += ifi_canfd_handle_state_change(ndev,
547 static int ifi_canfd_poll(struct napi_struct *napi, int quota)
549 struct net_device *ndev = napi->dev;
550 struct ifi_canfd_priv *priv = netdev_priv(ndev);
551 const u32 stcmd_state_mask = IFI_CANFD_STCMD_ERROR_PASSIVE |
552 IFI_CANFD_STCMD_BUSOFF;
555 u32 stcmd = readl(priv->base + IFI_CANFD_STCMD);
556 u32 rxstcmd = readl(priv->base + IFI_CANFD_STCMD);
557 u32 errctr = readl(priv->base + IFI_CANFD_ERROR_CTR);
559 /* Handle bus state changes */
560 if ((stcmd & stcmd_state_mask) ||
561 ((stcmd & IFI_CANFD_STCMD_ERROR_ACTIVE) == 0))
562 work_done += ifi_canfd_handle_state_errors(ndev, stcmd);
564 /* Handle lost messages on RX */
565 if (rxstcmd & IFI_CANFD_RXSTCMD_OVERFLOW)
566 work_done += ifi_canfd_handle_lost_msg(ndev);
568 /* Handle lec errors on the bus */
569 if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
570 work_done += ifi_canfd_handle_lec_err(ndev, errctr);
572 /* Handle normal messages on RX */
573 if (!(rxstcmd & IFI_CANFD_RXSTCMD_EMPTY))
574 work_done += ifi_canfd_do_rx_poll(ndev, quota - work_done);
576 if (work_done < quota) {
578 ifi_canfd_irq_enable(ndev, 1);
584 static irqreturn_t ifi_canfd_isr(int irq, void *dev_id)
586 struct net_device *ndev = (struct net_device *)dev_id;
587 struct ifi_canfd_priv *priv = netdev_priv(ndev);
588 struct net_device_stats *stats = &ndev->stats;
589 const u32 rx_irq_mask = IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY |
590 IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY_PER |
591 IFI_CANFD_INTERRUPT_ERROR_WARNING |
592 IFI_CANFD_INTERRUPT_ERROR_COUNTER;
593 const u32 tx_irq_mask = IFI_CANFD_INTERRUPT_TXFIFO_EMPTY |
594 IFI_CANFD_INTERRUPT_TXFIFO_REMOVE;
595 const u32 clr_irq_mask = ~((u32)(IFI_CANFD_INTERRUPT_SET_IRQ |
596 IFI_CANFD_INTERRUPT_ERROR_WARNING));
599 isr = readl(priv->base + IFI_CANFD_INTERRUPT);
605 /* Clear all pending interrupts but ErrWarn */
606 writel(clr_irq_mask, priv->base + IFI_CANFD_INTERRUPT);
608 /* RX IRQ or bus warning, start NAPI */
609 if (isr & rx_irq_mask) {
610 ifi_canfd_irq_enable(ndev, 0);
611 napi_schedule(&priv->napi);
615 if (isr & IFI_CANFD_INTERRUPT_TXFIFO_REMOVE) {
616 stats->tx_bytes += can_get_echo_skb(ndev, 0);
618 can_led_event(ndev, CAN_LED_EVENT_TX);
621 if (isr & tx_irq_mask)
622 netif_wake_queue(ndev);
627 static const struct can_bittiming_const ifi_canfd_bittiming_const = {
628 .name = KBUILD_MODNAME,
629 .tseg1_min = 1, /* Time segment 1 = prop_seg + phase_seg1 */
631 .tseg2_min = 2, /* Time segment 2 = phase_seg2 */
639 static void ifi_canfd_set_bittiming(struct net_device *ndev)
641 struct ifi_canfd_priv *priv = netdev_priv(ndev);
642 const struct can_bittiming *bt = &priv->can.bittiming;
643 const struct can_bittiming *dbt = &priv->can.data_bittiming;
644 u16 brp, sjw, tseg1, tseg2;
646 /* Configure bit timing */
649 tseg1 = bt->prop_seg + bt->phase_seg1 - 1;
650 tseg2 = bt->phase_seg2 - 2;
651 writel((tseg2 << IFI_CANFD_TIME_TIMEB_OFF) |
652 (tseg1 << IFI_CANFD_TIME_TIMEA_OFF) |
653 (brp << IFI_CANFD_TIME_PRESCALE_OFF) |
654 (sjw << IFI_CANFD_TIME_SJW_OFF_7_9_8_8),
655 priv->base + IFI_CANFD_TIME);
657 /* Configure data bit timing */
660 tseg1 = dbt->prop_seg + dbt->phase_seg1 - 1;
661 tseg2 = dbt->phase_seg2 - 2;
662 writel((tseg2 << IFI_CANFD_TIME_TIMEB_OFF) |
663 (tseg1 << IFI_CANFD_TIME_TIMEA_OFF) |
664 (brp << IFI_CANFD_TIME_PRESCALE_OFF) |
665 (sjw << IFI_CANFD_TIME_SJW_OFF_7_9_8_8),
666 priv->base + IFI_CANFD_FTIME);
669 static void ifi_canfd_set_filter(struct net_device *ndev, const u32 id,
670 const u32 mask, const u32 ident)
672 struct ifi_canfd_priv *priv = netdev_priv(ndev);
674 writel(mask, priv->base + IFI_CANFD_FILTER_MASK(id));
675 writel(ident, priv->base + IFI_CANFD_FILTER_IDENT(id));
678 static void ifi_canfd_set_filters(struct net_device *ndev)
680 /* Receive all CAN frames (standard ID) */
681 ifi_canfd_set_filter(ndev, 0,
682 IFI_CANFD_FILTER_MASK_VALID |
683 IFI_CANFD_FILTER_MASK_EXT,
684 IFI_CANFD_FILTER_IDENT_VALID);
686 /* Receive all CAN frames (extended ID) */
687 ifi_canfd_set_filter(ndev, 1,
688 IFI_CANFD_FILTER_MASK_VALID |
689 IFI_CANFD_FILTER_MASK_EXT,
690 IFI_CANFD_FILTER_IDENT_VALID |
691 IFI_CANFD_FILTER_IDENT_IDE);
693 /* Receive all CANFD frames */
694 ifi_canfd_set_filter(ndev, 2,
695 IFI_CANFD_FILTER_MASK_VALID |
696 IFI_CANFD_FILTER_MASK_EDL |
697 IFI_CANFD_FILTER_MASK_EXT,
698 IFI_CANFD_FILTER_IDENT_VALID |
699 IFI_CANFD_FILTER_IDENT_CANFD |
700 IFI_CANFD_FILTER_IDENT_IDE);
703 static void ifi_canfd_start(struct net_device *ndev)
705 struct ifi_canfd_priv *priv = netdev_priv(ndev);
709 writel(IFI_CANFD_STCMD_HARDRESET, priv->base + IFI_CANFD_STCMD);
710 writel(IFI_CANFD_STCMD_ENABLE_7_9_8_8_TIMING,
711 priv->base + IFI_CANFD_STCMD);
713 ifi_canfd_set_bittiming(ndev);
714 ifi_canfd_set_filters(ndev);
717 writel(IFI_CANFD_RXSTCMD_RESET, priv->base + IFI_CANFD_RXSTCMD);
718 writel(0, priv->base + IFI_CANFD_RXSTCMD);
719 writel(IFI_CANFD_TXSTCMD_RESET, priv->base + IFI_CANFD_TXSTCMD);
720 writel(0, priv->base + IFI_CANFD_TXSTCMD);
722 /* Repeat transmission until successful */
723 writel(0, priv->base + IFI_CANFD_REPEAT);
724 writel(0, priv->base + IFI_CANFD_SUSPEND);
726 /* Clear all pending interrupts */
727 writel((u32)(~IFI_CANFD_INTERRUPT_SET_IRQ),
728 priv->base + IFI_CANFD_INTERRUPT);
730 stcmd = IFI_CANFD_STCMD_ENABLE | IFI_CANFD_STCMD_NORMAL_MODE |
731 IFI_CANFD_STCMD_ENABLE_7_9_8_8_TIMING;
733 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
734 stcmd |= IFI_CANFD_STCMD_BUSMONITOR;
736 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
737 stcmd |= IFI_CANFD_STCMD_LOOPBACK;
739 if ((priv->can.ctrlmode & CAN_CTRLMODE_FD) &&
740 !(priv->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO))
741 stcmd |= IFI_CANFD_STCMD_ENABLE_ISO;
743 if (!(priv->can.ctrlmode & CAN_CTRLMODE_FD))
744 stcmd |= IFI_CANFD_STCMD_DISABLE_CANFD;
746 priv->can.state = CAN_STATE_ERROR_ACTIVE;
748 ifi_canfd_irq_enable(ndev, 1);
750 /* Unlock, reset and enable the error counter. */
751 writel(IFI_CANFD_ERROR_CTR_UNLOCK_MAGIC,
752 priv->base + IFI_CANFD_ERROR_CTR);
753 writel(IFI_CANFD_ERROR_CTR_ER_RESET, priv->base + IFI_CANFD_ERROR_CTR);
754 writel(IFI_CANFD_ERROR_CTR_ER_ENABLE, priv->base + IFI_CANFD_ERROR_CTR);
756 /* Enable controller */
757 writel(stcmd, priv->base + IFI_CANFD_STCMD);
760 static void ifi_canfd_stop(struct net_device *ndev)
762 struct ifi_canfd_priv *priv = netdev_priv(ndev);
764 /* Reset and disable the error counter. */
765 writel(IFI_CANFD_ERROR_CTR_ER_RESET, priv->base + IFI_CANFD_ERROR_CTR);
766 writel(0, priv->base + IFI_CANFD_ERROR_CTR);
769 writel(IFI_CANFD_STCMD_HARDRESET, priv->base + IFI_CANFD_STCMD);
771 /* Mask all interrupts */
772 writel(~0, priv->base + IFI_CANFD_IRQMASK);
774 /* Clear all pending interrupts */
775 writel((u32)(~IFI_CANFD_INTERRUPT_SET_IRQ),
776 priv->base + IFI_CANFD_INTERRUPT);
778 /* Set the state as STOPPED */
779 priv->can.state = CAN_STATE_STOPPED;
782 static int ifi_canfd_set_mode(struct net_device *ndev, enum can_mode mode)
786 ifi_canfd_start(ndev);
787 netif_wake_queue(ndev);
796 static int ifi_canfd_open(struct net_device *ndev)
798 struct ifi_canfd_priv *priv = netdev_priv(ndev);
801 ret = open_candev(ndev);
803 netdev_err(ndev, "Failed to open CAN device\n");
807 /* Register interrupt handler */
808 ret = request_irq(ndev->irq, ifi_canfd_isr, IRQF_SHARED,
811 netdev_err(ndev, "Failed to request interrupt\n");
815 ifi_canfd_start(ndev);
817 can_led_event(ndev, CAN_LED_EVENT_OPEN);
818 napi_enable(&priv->napi);
819 netif_start_queue(ndev);
827 static int ifi_canfd_close(struct net_device *ndev)
829 struct ifi_canfd_priv *priv = netdev_priv(ndev);
831 netif_stop_queue(ndev);
832 napi_disable(&priv->napi);
834 ifi_canfd_stop(ndev);
836 free_irq(ndev->irq, ndev);
840 can_led_event(ndev, CAN_LED_EVENT_STOP);
845 static netdev_tx_t ifi_canfd_start_xmit(struct sk_buff *skb,
846 struct net_device *ndev)
848 struct ifi_canfd_priv *priv = netdev_priv(ndev);
849 struct canfd_frame *cf = (struct canfd_frame *)skb->data;
850 u32 txst, txid, txdlc;
853 if (can_dropped_invalid_skb(ndev, skb))
856 /* Check if the TX buffer is full */
857 txst = readl(priv->base + IFI_CANFD_TXSTCMD);
858 if (txst & IFI_CANFD_TXSTCMD_FULL) {
859 netif_stop_queue(ndev);
860 netdev_err(ndev, "BUG! TX FIFO full when queue awake!\n");
861 return NETDEV_TX_BUSY;
864 netif_stop_queue(ndev);
866 if (cf->can_id & CAN_EFF_FLAG) {
867 txid = cf->can_id & CAN_EFF_MASK;
869 * In case the Extended ID frame is transmitted, the
870 * standard and extended part of the ID are swapped
871 * in the register, so swap them back to send the
874 txid = (txid >> IFI_CANFD_TXFIFO_ID_ID_XTD_WIDTH) |
875 ((txid & IFI_CANFD_TXFIFO_ID_ID_XTD_MASK) <<
876 IFI_CANFD_TXFIFO_ID_ID_XTD_OFFSET);
877 txid |= IFI_CANFD_TXFIFO_ID_IDE;
879 txid = cf->can_id & CAN_SFF_MASK;
882 txdlc = can_len2dlc(cf->len);
883 if ((priv->can.ctrlmode & CAN_CTRLMODE_FD) && can_is_canfd_skb(skb)) {
884 txdlc |= IFI_CANFD_TXFIFO_DLC_EDL;
885 if (cf->flags & CANFD_BRS)
886 txdlc |= IFI_CANFD_TXFIFO_DLC_BRS;
889 if (cf->can_id & CAN_RTR_FLAG)
890 txdlc |= IFI_CANFD_TXFIFO_DLC_RTR;
892 /* message ram configuration */
893 writel(txid, priv->base + IFI_CANFD_TXFIFO_ID);
894 writel(txdlc, priv->base + IFI_CANFD_TXFIFO_DLC);
896 for (i = 0; i < cf->len; i += 4) {
897 writel(*(u32 *)(cf->data + i),
898 priv->base + IFI_CANFD_TXFIFO_DATA + i);
901 writel(0, priv->base + IFI_CANFD_TXFIFO_REPEATCOUNT);
902 writel(0, priv->base + IFI_CANFD_TXFIFO_SUSPEND_US);
904 can_put_echo_skb(skb, ndev, 0);
906 /* Start the transmission */
907 writel(IFI_CANFD_TXSTCMD_ADD_MSG, priv->base + IFI_CANFD_TXSTCMD);
912 static const struct net_device_ops ifi_canfd_netdev_ops = {
913 .ndo_open = ifi_canfd_open,
914 .ndo_stop = ifi_canfd_close,
915 .ndo_start_xmit = ifi_canfd_start_xmit,
916 .ndo_change_mtu = can_change_mtu,
919 static int ifi_canfd_plat_probe(struct platform_device *pdev)
921 struct device *dev = &pdev->dev;
922 struct net_device *ndev;
923 struct ifi_canfd_priv *priv;
924 struct resource *res;
929 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
930 addr = devm_ioremap_resource(dev, res);
931 irq = platform_get_irq(pdev, 0);
932 if (IS_ERR(addr) || irq < 0)
935 id = readl(addr + IFI_CANFD_IP_ID);
936 if (id != IFI_CANFD_IP_ID_VALUE) {
937 dev_err(dev, "This block is not IFI CANFD, id=%08x\n", id);
941 ndev = alloc_candev(sizeof(*priv), 1);
946 ndev->flags |= IFF_ECHO; /* we support local echo */
947 ndev->netdev_ops = &ifi_canfd_netdev_ops;
949 priv = netdev_priv(ndev);
953 netif_napi_add(ndev, &priv->napi, ifi_canfd_poll, 64);
955 priv->can.state = CAN_STATE_STOPPED;
957 priv->can.clock.freq = readl(addr + IFI_CANFD_CANCLOCK);
959 priv->can.bittiming_const = &ifi_canfd_bittiming_const;
960 priv->can.data_bittiming_const = &ifi_canfd_bittiming_const;
961 priv->can.do_set_mode = ifi_canfd_set_mode;
962 priv->can.do_get_berr_counter = ifi_canfd_get_berr_counter;
964 /* IFI CANFD can do both Bosch FD and ISO FD */
965 priv->can.ctrlmode = CAN_CTRLMODE_FD;
967 /* IFI CANFD can do both Bosch FD and ISO FD */
968 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
969 CAN_CTRLMODE_LISTENONLY |
971 CAN_CTRLMODE_FD_NON_ISO |
972 CAN_CTRLMODE_BERR_REPORTING;
974 platform_set_drvdata(pdev, ndev);
975 SET_NETDEV_DEV(ndev, dev);
977 ret = register_candev(ndev);
979 dev_err(dev, "Failed to register (ret=%d)\n", ret);
983 devm_can_led_init(ndev);
985 dev_info(dev, "Driver registered: regs=%p, irq=%d, clock=%d\n",
986 priv->base, ndev->irq, priv->can.clock.freq);
995 static int ifi_canfd_plat_remove(struct platform_device *pdev)
997 struct net_device *ndev = platform_get_drvdata(pdev);
999 unregister_candev(ndev);
1000 platform_set_drvdata(pdev, NULL);
1006 static const struct of_device_id ifi_canfd_of_table[] = {
1007 { .compatible = "ifi,canfd-1.0", .data = NULL },
1010 MODULE_DEVICE_TABLE(of, ifi_canfd_of_table);
1012 static struct platform_driver ifi_canfd_plat_driver = {
1014 .name = KBUILD_MODNAME,
1015 .of_match_table = ifi_canfd_of_table,
1017 .probe = ifi_canfd_plat_probe,
1018 .remove = ifi_canfd_plat_remove,
1021 module_platform_driver(ifi_canfd_plat_driver);
1023 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
1024 MODULE_LICENSE("GPL v2");
1025 MODULE_DESCRIPTION("CAN bus driver for IFI CANFD controller");