1 /*
2  * CAN bus driver for IFI CANFD controller
3  *
4  * Copyright (C) 2016 Marek Vasut <marex@denx.de>
5  *
6  * Details about this controller can be found at
7  * http://www.ifi-pld.de/IP/CANFD/canfd.html
8  *
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.
12  */
13 
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/ethtool.h>
17 #include <linux/interrupt.h>
18 #include <linux/io.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/netdevice.h>
22 #include <linux/of.h>
23 #include <linux/of_device.h>
24 #include <linux/platform_device.h>
25 
26 #include <linux/can/dev.h>
27 
28 #define IFI_CANFD_STCMD				0x0
29 #define IFI_CANFD_STCMD_HARDRESET		0xDEADCAFD
30 #define IFI_CANFD_STCMD_ENABLE			BIT(0)
31 #define IFI_CANFD_STCMD_ERROR_ACTIVE		BIT(2)
32 #define IFI_CANFD_STCMD_ERROR_PASSIVE		BIT(3)
33 #define IFI_CANFD_STCMD_BUSOFF			BIT(4)
34 #define IFI_CANFD_STCMD_ERROR_WARNING		BIT(5)
35 #define IFI_CANFD_STCMD_BUSMONITOR		BIT(16)
36 #define IFI_CANFD_STCMD_LOOPBACK		BIT(18)
37 #define IFI_CANFD_STCMD_DISABLE_CANFD		BIT(24)
38 #define IFI_CANFD_STCMD_ENABLE_ISO		BIT(25)
39 #define IFI_CANFD_STCMD_ENABLE_7_9_8_8_TIMING	BIT(26)
40 #define IFI_CANFD_STCMD_NORMAL_MODE		((u32)BIT(31))
41 
42 #define IFI_CANFD_RXSTCMD			0x4
43 #define IFI_CANFD_RXSTCMD_REMOVE_MSG		BIT(0)
44 #define IFI_CANFD_RXSTCMD_RESET			BIT(7)
45 #define IFI_CANFD_RXSTCMD_EMPTY			BIT(8)
46 #define IFI_CANFD_RXSTCMD_OVERFLOW		BIT(13)
47 
48 #define IFI_CANFD_TXSTCMD			0x8
49 #define IFI_CANFD_TXSTCMD_ADD_MSG		BIT(0)
50 #define IFI_CANFD_TXSTCMD_HIGH_PRIO		BIT(1)
51 #define IFI_CANFD_TXSTCMD_RESET			BIT(7)
52 #define IFI_CANFD_TXSTCMD_EMPTY			BIT(8)
53 #define IFI_CANFD_TXSTCMD_FULL			BIT(12)
54 #define IFI_CANFD_TXSTCMD_OVERFLOW		BIT(13)
55 
56 #define IFI_CANFD_INTERRUPT			0xc
57 #define IFI_CANFD_INTERRUPT_ERROR_BUSOFF	BIT(0)
58 #define IFI_CANFD_INTERRUPT_ERROR_WARNING	BIT(1)
59 #define IFI_CANFD_INTERRUPT_ERROR_STATE_CHG	BIT(2)
60 #define IFI_CANFD_INTERRUPT_ERROR_REC_TEC_INC	BIT(3)
61 #define IFI_CANFD_INTERRUPT_ERROR_COUNTER	BIT(10)
62 #define IFI_CANFD_INTERRUPT_TXFIFO_EMPTY	BIT(16)
63 #define IFI_CANFD_INTERRUPT_TXFIFO_REMOVE	BIT(22)
64 #define IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY	BIT(24)
65 #define IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY_PER	BIT(25)
66 #define IFI_CANFD_INTERRUPT_SET_IRQ		((u32)BIT(31))
67 
68 #define IFI_CANFD_IRQMASK			0x10
69 #define IFI_CANFD_IRQMASK_ERROR_BUSOFF		BIT(0)
70 #define IFI_CANFD_IRQMASK_ERROR_WARNING		BIT(1)
71 #define IFI_CANFD_IRQMASK_ERROR_STATE_CHG	BIT(2)
72 #define IFI_CANFD_IRQMASK_ERROR_REC_TEC_INC	BIT(3)
73 #define IFI_CANFD_IRQMASK_SET_ERR		BIT(7)
74 #define IFI_CANFD_IRQMASK_SET_TS		BIT(15)
75 #define IFI_CANFD_IRQMASK_TXFIFO_EMPTY		BIT(16)
76 #define IFI_CANFD_IRQMASK_SET_TX		BIT(23)
77 #define IFI_CANFD_IRQMASK_RXFIFO_NEMPTY		BIT(24)
78 #define IFI_CANFD_IRQMASK_SET_RX		((u32)BIT(31))
79 
80 #define IFI_CANFD_TIME				0x14
81 #define IFI_CANFD_FTIME				0x18
82 #define IFI_CANFD_TIME_TIMEB_OFF		0
83 #define IFI_CANFD_TIME_TIMEA_OFF		8
84 #define IFI_CANFD_TIME_PRESCALE_OFF		16
85 #define IFI_CANFD_TIME_SJW_OFF_7_9_8_8		25
86 #define IFI_CANFD_TIME_SJW_OFF_4_12_6_6		28
87 #define IFI_CANFD_TIME_SET_SJW_4_12_6_6		BIT(6)
88 #define IFI_CANFD_TIME_SET_TIMEB_4_12_6_6	BIT(7)
89 #define IFI_CANFD_TIME_SET_PRESC_4_12_6_6	BIT(14)
90 #define IFI_CANFD_TIME_SET_TIMEA_4_12_6_6	BIT(15)
91 
92 #define IFI_CANFD_TDELAY			0x1c
93 #define IFI_CANFD_TDELAY_DEFAULT		0xb
94 #define IFI_CANFD_TDELAY_MASK			0x3fff
95 #define IFI_CANFD_TDELAY_ABS			BIT(14)
96 #define IFI_CANFD_TDELAY_EN			BIT(15)
97 
98 #define IFI_CANFD_ERROR				0x20
99 #define IFI_CANFD_ERROR_TX_OFFSET		0
100 #define IFI_CANFD_ERROR_TX_MASK			0xff
101 #define IFI_CANFD_ERROR_RX_OFFSET		16
102 #define IFI_CANFD_ERROR_RX_MASK			0xff
103 
104 #define IFI_CANFD_ERRCNT			0x24
105 
106 #define IFI_CANFD_SUSPEND			0x28
107 
108 #define IFI_CANFD_REPEAT			0x2c
109 
110 #define IFI_CANFD_TRAFFIC			0x30
111 
112 #define IFI_CANFD_TSCONTROL			0x34
113 
114 #define IFI_CANFD_TSC				0x38
115 
116 #define IFI_CANFD_TST				0x3c
117 
118 #define IFI_CANFD_RES1				0x40
119 
120 #define IFI_CANFD_ERROR_CTR			0x44
121 #define IFI_CANFD_ERROR_CTR_UNLOCK_MAGIC	0x21302899
122 #define IFI_CANFD_ERROR_CTR_OVERLOAD_FIRST	BIT(0)
123 #define IFI_CANFD_ERROR_CTR_ACK_ERROR_FIRST	BIT(1)
124 #define IFI_CANFD_ERROR_CTR_BIT0_ERROR_FIRST	BIT(2)
125 #define IFI_CANFD_ERROR_CTR_BIT1_ERROR_FIRST	BIT(3)
126 #define IFI_CANFD_ERROR_CTR_STUFF_ERROR_FIRST	BIT(4)
127 #define IFI_CANFD_ERROR_CTR_CRC_ERROR_FIRST	BIT(5)
128 #define IFI_CANFD_ERROR_CTR_FORM_ERROR_FIRST	BIT(6)
129 #define IFI_CANFD_ERROR_CTR_OVERLOAD_ALL	BIT(8)
130 #define IFI_CANFD_ERROR_CTR_ACK_ERROR_ALL	BIT(9)
131 #define IFI_CANFD_ERROR_CTR_BIT0_ERROR_ALL	BIT(10)
132 #define IFI_CANFD_ERROR_CTR_BIT1_ERROR_ALL	BIT(11)
133 #define IFI_CANFD_ERROR_CTR_STUFF_ERROR_ALL	BIT(12)
134 #define IFI_CANFD_ERROR_CTR_CRC_ERROR_ALL	BIT(13)
135 #define IFI_CANFD_ERROR_CTR_FORM_ERROR_ALL	BIT(14)
136 #define IFI_CANFD_ERROR_CTR_BITPOSITION_OFFSET	16
137 #define IFI_CANFD_ERROR_CTR_BITPOSITION_MASK	0xff
138 #define IFI_CANFD_ERROR_CTR_ER_RESET		BIT(30)
139 #define IFI_CANFD_ERROR_CTR_ER_ENABLE		((u32)BIT(31))
140 
141 #define IFI_CANFD_PAR				0x48
142 
143 #define IFI_CANFD_CANCLOCK			0x4c
144 
145 #define IFI_CANFD_SYSCLOCK			0x50
146 
147 #define IFI_CANFD_VER				0x54
148 #define IFI_CANFD_VER_REV_MASK			0xff
149 #define IFI_CANFD_VER_REV_MIN_SUPPORTED		0x15
150 
151 #define IFI_CANFD_IP_ID				0x58
152 #define IFI_CANFD_IP_ID_VALUE			0xD073CAFD
153 
154 #define IFI_CANFD_TEST				0x5c
155 
156 #define IFI_CANFD_RXFIFO_TS_63_32		0x60
157 
158 #define IFI_CANFD_RXFIFO_TS_31_0		0x64
159 
160 #define IFI_CANFD_RXFIFO_DLC			0x68
161 #define IFI_CANFD_RXFIFO_DLC_DLC_OFFSET		0
162 #define IFI_CANFD_RXFIFO_DLC_DLC_MASK		0xf
163 #define IFI_CANFD_RXFIFO_DLC_RTR		BIT(4)
164 #define IFI_CANFD_RXFIFO_DLC_EDL		BIT(5)
165 #define IFI_CANFD_RXFIFO_DLC_BRS		BIT(6)
166 #define IFI_CANFD_RXFIFO_DLC_ESI		BIT(7)
167 #define IFI_CANFD_RXFIFO_DLC_OBJ_OFFSET		8
168 #define IFI_CANFD_RXFIFO_DLC_OBJ_MASK		0x1ff
169 #define IFI_CANFD_RXFIFO_DLC_FNR_OFFSET		24
170 #define IFI_CANFD_RXFIFO_DLC_FNR_MASK		0xff
171 
172 #define IFI_CANFD_RXFIFO_ID			0x6c
173 #define IFI_CANFD_RXFIFO_ID_ID_OFFSET		0
174 #define IFI_CANFD_RXFIFO_ID_ID_STD_MASK		CAN_SFF_MASK
175 #define IFI_CANFD_RXFIFO_ID_ID_STD_OFFSET	0
176 #define IFI_CANFD_RXFIFO_ID_ID_STD_WIDTH	10
177 #define IFI_CANFD_RXFIFO_ID_ID_XTD_MASK		CAN_EFF_MASK
178 #define IFI_CANFD_RXFIFO_ID_ID_XTD_OFFSET	11
179 #define IFI_CANFD_RXFIFO_ID_ID_XTD_WIDTH	18
180 #define IFI_CANFD_RXFIFO_ID_IDE			BIT(29)
181 
182 #define IFI_CANFD_RXFIFO_DATA			0x70	/* 0x70..0xac */
183 
184 #define IFI_CANFD_TXFIFO_SUSPEND_US		0xb0
185 
186 #define IFI_CANFD_TXFIFO_REPEATCOUNT		0xb4
187 
188 #define IFI_CANFD_TXFIFO_DLC			0xb8
189 #define IFI_CANFD_TXFIFO_DLC_DLC_OFFSET		0
190 #define IFI_CANFD_TXFIFO_DLC_DLC_MASK		0xf
191 #define IFI_CANFD_TXFIFO_DLC_RTR		BIT(4)
192 #define IFI_CANFD_TXFIFO_DLC_EDL		BIT(5)
193 #define IFI_CANFD_TXFIFO_DLC_BRS		BIT(6)
194 #define IFI_CANFD_TXFIFO_DLC_FNR_OFFSET		24
195 #define IFI_CANFD_TXFIFO_DLC_FNR_MASK		0xff
196 
197 #define IFI_CANFD_TXFIFO_ID			0xbc
198 #define IFI_CANFD_TXFIFO_ID_ID_OFFSET		0
199 #define IFI_CANFD_TXFIFO_ID_ID_STD_MASK		CAN_SFF_MASK
200 #define IFI_CANFD_TXFIFO_ID_ID_STD_OFFSET	0
201 #define IFI_CANFD_TXFIFO_ID_ID_STD_WIDTH	10
202 #define IFI_CANFD_TXFIFO_ID_ID_XTD_MASK		CAN_EFF_MASK
203 #define IFI_CANFD_TXFIFO_ID_ID_XTD_OFFSET	11
204 #define IFI_CANFD_TXFIFO_ID_ID_XTD_WIDTH	18
205 #define IFI_CANFD_TXFIFO_ID_IDE			BIT(29)
206 
207 #define IFI_CANFD_TXFIFO_DATA			0xc0	/* 0xb0..0xfc */
208 
209 #define IFI_CANFD_FILTER_MASK(n)		(0x800 + ((n) * 8) + 0)
210 #define IFI_CANFD_FILTER_MASK_EXT		BIT(29)
211 #define IFI_CANFD_FILTER_MASK_EDL		BIT(30)
212 #define IFI_CANFD_FILTER_MASK_VALID		((u32)BIT(31))
213 
214 #define IFI_CANFD_FILTER_IDENT(n)		(0x800 + ((n) * 8) + 4)
215 #define IFI_CANFD_FILTER_IDENT_IDE		BIT(29)
216 #define IFI_CANFD_FILTER_IDENT_CANFD		BIT(30)
217 #define IFI_CANFD_FILTER_IDENT_VALID		((u32)BIT(31))
218 
219 /* IFI CANFD private data structure */
220 struct ifi_canfd_priv {
221 	struct can_priv		can;	/* must be the first member */
222 	struct napi_struct	napi;
223 	struct net_device	*ndev;
224 	void __iomem		*base;
225 };
226 
227 static void ifi_canfd_irq_enable(struct net_device *ndev, bool enable)
228 {
229 	struct ifi_canfd_priv *priv = netdev_priv(ndev);
230 	u32 enirq = 0;
231 
232 	if (enable) {
233 		enirq = IFI_CANFD_IRQMASK_TXFIFO_EMPTY |
234 			IFI_CANFD_IRQMASK_RXFIFO_NEMPTY |
235 			IFI_CANFD_IRQMASK_ERROR_STATE_CHG |
236 			IFI_CANFD_IRQMASK_ERROR_WARNING |
237 			IFI_CANFD_IRQMASK_ERROR_BUSOFF;
238 		if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
239 			enirq |= IFI_CANFD_INTERRUPT_ERROR_COUNTER;
240 	}
241 
242 	writel(IFI_CANFD_IRQMASK_SET_ERR |
243 	       IFI_CANFD_IRQMASK_SET_TS |
244 	       IFI_CANFD_IRQMASK_SET_TX |
245 	       IFI_CANFD_IRQMASK_SET_RX | enirq,
246 	       priv->base + IFI_CANFD_IRQMASK);
247 }
248 
249 static void ifi_canfd_read_fifo(struct net_device *ndev)
250 {
251 	struct net_device_stats *stats = &ndev->stats;
252 	struct ifi_canfd_priv *priv = netdev_priv(ndev);
253 	struct canfd_frame *cf;
254 	struct sk_buff *skb;
255 	const u32 rx_irq_mask = IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY |
256 				IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY_PER;
257 	u32 rxdlc, rxid;
258 	u32 dlc, id;
259 	int i;
260 
261 	rxdlc = readl(priv->base + IFI_CANFD_RXFIFO_DLC);
262 	if (rxdlc & IFI_CANFD_RXFIFO_DLC_EDL)
263 		skb = alloc_canfd_skb(ndev, &cf);
264 	else
265 		skb = alloc_can_skb(ndev, (struct can_frame **)&cf);
266 
267 	if (!skb) {
268 		stats->rx_dropped++;
269 		return;
270 	}
271 
272 	dlc = (rxdlc >> IFI_CANFD_RXFIFO_DLC_DLC_OFFSET) &
273 	      IFI_CANFD_RXFIFO_DLC_DLC_MASK;
274 	if (rxdlc & IFI_CANFD_RXFIFO_DLC_EDL)
275 		cf->len = can_fd_dlc2len(dlc);
276 	else
277 		cf->len = can_cc_dlc2len(dlc);
278 
279 	rxid = readl(priv->base + IFI_CANFD_RXFIFO_ID);
280 	id = (rxid >> IFI_CANFD_RXFIFO_ID_ID_OFFSET);
281 	if (id & IFI_CANFD_RXFIFO_ID_IDE) {
282 		id &= IFI_CANFD_RXFIFO_ID_ID_XTD_MASK;
283 		/*
284 		 * In case the Extended ID frame is received, the standard
285 		 * and extended part of the ID are swapped in the register,
286 		 * so swap them back to obtain the correct ID.
287 		 */
288 		id = (id >> IFI_CANFD_RXFIFO_ID_ID_XTD_OFFSET) |
289 		     ((id & IFI_CANFD_RXFIFO_ID_ID_STD_MASK) <<
290 		       IFI_CANFD_RXFIFO_ID_ID_XTD_WIDTH);
291 		id |= CAN_EFF_FLAG;
292 	} else {
293 		id &= IFI_CANFD_RXFIFO_ID_ID_STD_MASK;
294 	}
295 	cf->can_id = id;
296 
297 	if (rxdlc & IFI_CANFD_RXFIFO_DLC_ESI) {
298 		cf->flags |= CANFD_ESI;
299 		netdev_dbg(ndev, "ESI Error\n");
300 	}
301 
302 	if (!(rxdlc & IFI_CANFD_RXFIFO_DLC_EDL) &&
303 	    (rxdlc & IFI_CANFD_RXFIFO_DLC_RTR)) {
304 		cf->can_id |= CAN_RTR_FLAG;
305 	} else {
306 		if (rxdlc & IFI_CANFD_RXFIFO_DLC_BRS)
307 			cf->flags |= CANFD_BRS;
308 
309 		for (i = 0; i < cf->len; i += 4) {
310 			*(u32 *)(cf->data + i) =
311 				readl(priv->base + IFI_CANFD_RXFIFO_DATA + i);
312 		}
313 
314 		stats->rx_bytes += cf->len;
315 	}
316 	stats->rx_packets++;
317 
318 	/* Remove the packet from FIFO */
319 	writel(IFI_CANFD_RXSTCMD_REMOVE_MSG, priv->base + IFI_CANFD_RXSTCMD);
320 	writel(rx_irq_mask, priv->base + IFI_CANFD_INTERRUPT);
321 
322 	netif_receive_skb(skb);
323 }
324 
325 static int ifi_canfd_do_rx_poll(struct net_device *ndev, int quota)
326 {
327 	struct ifi_canfd_priv *priv = netdev_priv(ndev);
328 	u32 pkts = 0;
329 	u32 rxst;
330 
331 	rxst = readl(priv->base + IFI_CANFD_RXSTCMD);
332 	if (rxst & IFI_CANFD_RXSTCMD_EMPTY) {
333 		netdev_dbg(ndev, "No messages in RX FIFO\n");
334 		return 0;
335 	}
336 
337 	for (;;) {
338 		if (rxst & IFI_CANFD_RXSTCMD_EMPTY)
339 			break;
340 		if (quota <= 0)
341 			break;
342 
343 		ifi_canfd_read_fifo(ndev);
344 		quota--;
345 		pkts++;
346 		rxst = readl(priv->base + IFI_CANFD_RXSTCMD);
347 	}
348 
349 	return pkts;
350 }
351 
352 static int ifi_canfd_handle_lost_msg(struct net_device *ndev)
353 {
354 	struct net_device_stats *stats = &ndev->stats;
355 	struct sk_buff *skb;
356 	struct can_frame *frame;
357 
358 	netdev_err(ndev, "RX FIFO overflow, message(s) lost.\n");
359 
360 	stats->rx_errors++;
361 	stats->rx_over_errors++;
362 
363 	skb = alloc_can_err_skb(ndev, &frame);
364 	if (unlikely(!skb))
365 		return 0;
366 
367 	frame->can_id |= CAN_ERR_CRTL;
368 	frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
369 
370 	netif_receive_skb(skb);
371 
372 	return 1;
373 }
374 
375 static int ifi_canfd_handle_lec_err(struct net_device *ndev)
376 {
377 	struct ifi_canfd_priv *priv = netdev_priv(ndev);
378 	struct net_device_stats *stats = &ndev->stats;
379 	struct can_frame *cf;
380 	struct sk_buff *skb;
381 	u32 errctr = readl(priv->base + IFI_CANFD_ERROR_CTR);
382 	const u32 errmask = IFI_CANFD_ERROR_CTR_OVERLOAD_FIRST |
383 			    IFI_CANFD_ERROR_CTR_ACK_ERROR_FIRST |
384 			    IFI_CANFD_ERROR_CTR_BIT0_ERROR_FIRST |
385 			    IFI_CANFD_ERROR_CTR_BIT1_ERROR_FIRST |
386 			    IFI_CANFD_ERROR_CTR_STUFF_ERROR_FIRST |
387 			    IFI_CANFD_ERROR_CTR_CRC_ERROR_FIRST |
388 			    IFI_CANFD_ERROR_CTR_FORM_ERROR_FIRST;
389 
390 	if (!(errctr & errmask))	/* No error happened. */
391 		return 0;
392 
393 	priv->can.can_stats.bus_error++;
394 	stats->rx_errors++;
395 
396 	/* Propagate the error condition to the CAN stack. */
397 	skb = alloc_can_err_skb(ndev, &cf);
398 	if (unlikely(!skb))
399 		return 0;
400 
401 	/* Read the error counter register and check for new errors. */
402 	cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
403 
404 	if (errctr & IFI_CANFD_ERROR_CTR_OVERLOAD_FIRST)
405 		cf->data[2] |= CAN_ERR_PROT_OVERLOAD;
406 
407 	if (errctr & IFI_CANFD_ERROR_CTR_ACK_ERROR_FIRST)
408 		cf->data[3] = CAN_ERR_PROT_LOC_ACK;
409 
410 	if (errctr & IFI_CANFD_ERROR_CTR_BIT0_ERROR_FIRST)
411 		cf->data[2] |= CAN_ERR_PROT_BIT0;
412 
413 	if (errctr & IFI_CANFD_ERROR_CTR_BIT1_ERROR_FIRST)
414 		cf->data[2] |= CAN_ERR_PROT_BIT1;
415 
416 	if (errctr & IFI_CANFD_ERROR_CTR_STUFF_ERROR_FIRST)
417 		cf->data[2] |= CAN_ERR_PROT_STUFF;
418 
419 	if (errctr & IFI_CANFD_ERROR_CTR_CRC_ERROR_FIRST)
420 		cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
421 
422 	if (errctr & IFI_CANFD_ERROR_CTR_FORM_ERROR_FIRST)
423 		cf->data[2] |= CAN_ERR_PROT_FORM;
424 
425 	/* Reset the error counter, ack the IRQ and re-enable the counter. */
426 	writel(IFI_CANFD_ERROR_CTR_ER_RESET, priv->base + IFI_CANFD_ERROR_CTR);
427 	writel(IFI_CANFD_INTERRUPT_ERROR_COUNTER,
428 	       priv->base + IFI_CANFD_INTERRUPT);
429 	writel(IFI_CANFD_ERROR_CTR_ER_ENABLE, priv->base + IFI_CANFD_ERROR_CTR);
430 
431 	netif_receive_skb(skb);
432 
433 	return 1;
434 }
435 
436 static int ifi_canfd_get_berr_counter(const struct net_device *ndev,
437 				      struct can_berr_counter *bec)
438 {
439 	struct ifi_canfd_priv *priv = netdev_priv(ndev);
440 	u32 err;
441 
442 	err = readl(priv->base + IFI_CANFD_ERROR);
443 	bec->rxerr = (err >> IFI_CANFD_ERROR_RX_OFFSET) &
444 		     IFI_CANFD_ERROR_RX_MASK;
445 	bec->txerr = (err >> IFI_CANFD_ERROR_TX_OFFSET) &
446 		     IFI_CANFD_ERROR_TX_MASK;
447 
448 	return 0;
449 }
450 
451 static int ifi_canfd_handle_state_change(struct net_device *ndev,
452 					 enum can_state new_state)
453 {
454 	struct ifi_canfd_priv *priv = netdev_priv(ndev);
455 	struct can_frame *cf;
456 	struct sk_buff *skb;
457 	struct can_berr_counter bec;
458 
459 	switch (new_state) {
460 	case CAN_STATE_ERROR_ACTIVE:
461 		/* error active state */
462 		priv->can.can_stats.error_warning++;
463 		priv->can.state = CAN_STATE_ERROR_ACTIVE;
464 		break;
465 	case CAN_STATE_ERROR_WARNING:
466 		/* error warning state */
467 		priv->can.can_stats.error_warning++;
468 		priv->can.state = CAN_STATE_ERROR_WARNING;
469 		break;
470 	case CAN_STATE_ERROR_PASSIVE:
471 		/* error passive state */
472 		priv->can.can_stats.error_passive++;
473 		priv->can.state = CAN_STATE_ERROR_PASSIVE;
474 		break;
475 	case CAN_STATE_BUS_OFF:
476 		/* bus-off state */
477 		priv->can.state = CAN_STATE_BUS_OFF;
478 		ifi_canfd_irq_enable(ndev, 0);
479 		priv->can.can_stats.bus_off++;
480 		can_bus_off(ndev);
481 		break;
482 	default:
483 		break;
484 	}
485 
486 	/* propagate the error condition to the CAN stack */
487 	skb = alloc_can_err_skb(ndev, &cf);
488 	if (unlikely(!skb))
489 		return 0;
490 
491 	ifi_canfd_get_berr_counter(ndev, &bec);
492 
493 	switch (new_state) {
494 	case CAN_STATE_ERROR_WARNING:
495 		/* error warning state */
496 		cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT;
497 		cf->data[1] = (bec.txerr > bec.rxerr) ?
498 			CAN_ERR_CRTL_TX_WARNING :
499 			CAN_ERR_CRTL_RX_WARNING;
500 		cf->data[6] = bec.txerr;
501 		cf->data[7] = bec.rxerr;
502 		break;
503 	case CAN_STATE_ERROR_PASSIVE:
504 		/* error passive state */
505 		cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT;
506 		cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
507 		if (bec.txerr > 127)
508 			cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
509 		cf->data[6] = bec.txerr;
510 		cf->data[7] = bec.rxerr;
511 		break;
512 	case CAN_STATE_BUS_OFF:
513 		/* bus-off state */
514 		cf->can_id |= CAN_ERR_BUSOFF;
515 		break;
516 	default:
517 		break;
518 	}
519 
520 	netif_receive_skb(skb);
521 
522 	return 1;
523 }
524 
525 static int ifi_canfd_handle_state_errors(struct net_device *ndev)
526 {
527 	struct ifi_canfd_priv *priv = netdev_priv(ndev);
528 	u32 stcmd = readl(priv->base + IFI_CANFD_STCMD);
529 	int work_done = 0;
530 
531 	if ((stcmd & IFI_CANFD_STCMD_ERROR_ACTIVE) &&
532 	    (priv->can.state != CAN_STATE_ERROR_ACTIVE)) {
533 		netdev_dbg(ndev, "Error, entered active state\n");
534 		work_done += ifi_canfd_handle_state_change(ndev,
535 						CAN_STATE_ERROR_ACTIVE);
536 	}
537 
538 	if ((stcmd & IFI_CANFD_STCMD_ERROR_WARNING) &&
539 	    (priv->can.state != CAN_STATE_ERROR_WARNING)) {
540 		netdev_dbg(ndev, "Error, entered warning state\n");
541 		work_done += ifi_canfd_handle_state_change(ndev,
542 						CAN_STATE_ERROR_WARNING);
543 	}
544 
545 	if ((stcmd & IFI_CANFD_STCMD_ERROR_PASSIVE) &&
546 	    (priv->can.state != CAN_STATE_ERROR_PASSIVE)) {
547 		netdev_dbg(ndev, "Error, entered passive state\n");
548 		work_done += ifi_canfd_handle_state_change(ndev,
549 						CAN_STATE_ERROR_PASSIVE);
550 	}
551 
552 	if ((stcmd & IFI_CANFD_STCMD_BUSOFF) &&
553 	    (priv->can.state != CAN_STATE_BUS_OFF)) {
554 		netdev_dbg(ndev, "Error, entered bus-off state\n");
555 		work_done += ifi_canfd_handle_state_change(ndev,
556 						CAN_STATE_BUS_OFF);
557 	}
558 
559 	return work_done;
560 }
561 
562 static int ifi_canfd_poll(struct napi_struct *napi, int quota)
563 {
564 	struct net_device *ndev = napi->dev;
565 	struct ifi_canfd_priv *priv = netdev_priv(ndev);
566 	u32 rxstcmd = readl(priv->base + IFI_CANFD_RXSTCMD);
567 	int work_done = 0;
568 
569 	/* Handle bus state changes */
570 	work_done += ifi_canfd_handle_state_errors(ndev);
571 
572 	/* Handle lost messages on RX */
573 	if (rxstcmd & IFI_CANFD_RXSTCMD_OVERFLOW)
574 		work_done += ifi_canfd_handle_lost_msg(ndev);
575 
576 	/* Handle lec errors on the bus */
577 	if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
578 		work_done += ifi_canfd_handle_lec_err(ndev);
579 
580 	/* Handle normal messages on RX */
581 	if (!(rxstcmd & IFI_CANFD_RXSTCMD_EMPTY))
582 		work_done += ifi_canfd_do_rx_poll(ndev, quota - work_done);
583 
584 	if (work_done < quota) {
585 		napi_complete_done(napi, work_done);
586 		ifi_canfd_irq_enable(ndev, 1);
587 	}
588 
589 	return work_done;
590 }
591 
592 static irqreturn_t ifi_canfd_isr(int irq, void *dev_id)
593 {
594 	struct net_device *ndev = (struct net_device *)dev_id;
595 	struct ifi_canfd_priv *priv = netdev_priv(ndev);
596 	struct net_device_stats *stats = &ndev->stats;
597 	const u32 rx_irq_mask = IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY |
598 				IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY_PER |
599 				IFI_CANFD_INTERRUPT_ERROR_COUNTER |
600 				IFI_CANFD_INTERRUPT_ERROR_STATE_CHG |
601 				IFI_CANFD_INTERRUPT_ERROR_WARNING |
602 				IFI_CANFD_INTERRUPT_ERROR_BUSOFF;
603 	const u32 tx_irq_mask = IFI_CANFD_INTERRUPT_TXFIFO_EMPTY |
604 				IFI_CANFD_INTERRUPT_TXFIFO_REMOVE;
605 	const u32 clr_irq_mask = ~((u32)IFI_CANFD_INTERRUPT_SET_IRQ);
606 	u32 isr;
607 
608 	isr = readl(priv->base + IFI_CANFD_INTERRUPT);
609 
610 	/* No interrupt */
611 	if (isr == 0)
612 		return IRQ_NONE;
613 
614 	/* Clear all pending interrupts but ErrWarn */
615 	writel(clr_irq_mask, priv->base + IFI_CANFD_INTERRUPT);
616 
617 	/* RX IRQ or bus warning, start NAPI */
618 	if (isr & rx_irq_mask) {
619 		ifi_canfd_irq_enable(ndev, 0);
620 		napi_schedule(&priv->napi);
621 	}
622 
623 	/* TX IRQ */
624 	if (isr & IFI_CANFD_INTERRUPT_TXFIFO_REMOVE) {
625 		stats->tx_bytes += can_get_echo_skb(ndev, 0, NULL);
626 		stats->tx_packets++;
627 	}
628 
629 	if (isr & tx_irq_mask)
630 		netif_wake_queue(ndev);
631 
632 	return IRQ_HANDLED;
633 }
634 
635 static const struct can_bittiming_const ifi_canfd_bittiming_const = {
636 	.name		= KBUILD_MODNAME,
637 	.tseg1_min	= 1,	/* Time segment 1 = prop_seg + phase_seg1 */
638 	.tseg1_max	= 256,
639 	.tseg2_min	= 2,	/* Time segment 2 = phase_seg2 */
640 	.tseg2_max	= 256,
641 	.sjw_max	= 128,
642 	.brp_min	= 2,
643 	.brp_max	= 512,
644 	.brp_inc	= 1,
645 };
646 
647 static void ifi_canfd_set_bittiming(struct net_device *ndev)
648 {
649 	struct ifi_canfd_priv *priv = netdev_priv(ndev);
650 	const struct can_bittiming *bt = &priv->can.bittiming;
651 	const struct can_bittiming *dbt = &priv->can.data_bittiming;
652 	u16 brp, sjw, tseg1, tseg2, tdc;
653 
654 	/* Configure bit timing */
655 	brp = bt->brp - 2;
656 	sjw = bt->sjw - 1;
657 	tseg1 = bt->prop_seg + bt->phase_seg1 - 1;
658 	tseg2 = bt->phase_seg2 - 2;
659 	writel((tseg2 << IFI_CANFD_TIME_TIMEB_OFF) |
660 	       (tseg1 << IFI_CANFD_TIME_TIMEA_OFF) |
661 	       (brp << IFI_CANFD_TIME_PRESCALE_OFF) |
662 	       (sjw << IFI_CANFD_TIME_SJW_OFF_7_9_8_8),
663 	       priv->base + IFI_CANFD_TIME);
664 
665 	/* Configure data bit timing */
666 	brp = dbt->brp - 2;
667 	sjw = dbt->sjw - 1;
668 	tseg1 = dbt->prop_seg + dbt->phase_seg1 - 1;
669 	tseg2 = dbt->phase_seg2 - 2;
670 	writel((tseg2 << IFI_CANFD_TIME_TIMEB_OFF) |
671 	       (tseg1 << IFI_CANFD_TIME_TIMEA_OFF) |
672 	       (brp << IFI_CANFD_TIME_PRESCALE_OFF) |
673 	       (sjw << IFI_CANFD_TIME_SJW_OFF_7_9_8_8),
674 	       priv->base + IFI_CANFD_FTIME);
675 
676 	/* Configure transmitter delay */
677 	tdc = dbt->brp * (dbt->prop_seg + dbt->phase_seg1);
678 	tdc &= IFI_CANFD_TDELAY_MASK;
679 	writel(IFI_CANFD_TDELAY_EN | tdc, priv->base + IFI_CANFD_TDELAY);
680 }
681 
682 static void ifi_canfd_set_filter(struct net_device *ndev, const u32 id,
683 				 const u32 mask, const u32 ident)
684 {
685 	struct ifi_canfd_priv *priv = netdev_priv(ndev);
686 
687 	writel(mask, priv->base + IFI_CANFD_FILTER_MASK(id));
688 	writel(ident, priv->base + IFI_CANFD_FILTER_IDENT(id));
689 }
690 
691 static void ifi_canfd_set_filters(struct net_device *ndev)
692 {
693 	/* Receive all CAN frames (standard ID) */
694 	ifi_canfd_set_filter(ndev, 0,
695 			     IFI_CANFD_FILTER_MASK_VALID |
696 			     IFI_CANFD_FILTER_MASK_EXT,
697 			     IFI_CANFD_FILTER_IDENT_VALID);
698 
699 	/* Receive all CAN frames (extended ID) */
700 	ifi_canfd_set_filter(ndev, 1,
701 			     IFI_CANFD_FILTER_MASK_VALID |
702 			     IFI_CANFD_FILTER_MASK_EXT,
703 			     IFI_CANFD_FILTER_IDENT_VALID |
704 			     IFI_CANFD_FILTER_IDENT_IDE);
705 
706 	/* Receive all CANFD frames */
707 	ifi_canfd_set_filter(ndev, 2,
708 			     IFI_CANFD_FILTER_MASK_VALID |
709 			     IFI_CANFD_FILTER_MASK_EDL |
710 			     IFI_CANFD_FILTER_MASK_EXT,
711 			     IFI_CANFD_FILTER_IDENT_VALID |
712 			     IFI_CANFD_FILTER_IDENT_CANFD |
713 			     IFI_CANFD_FILTER_IDENT_IDE);
714 }
715 
716 static void ifi_canfd_start(struct net_device *ndev)
717 {
718 	struct ifi_canfd_priv *priv = netdev_priv(ndev);
719 	u32 stcmd;
720 
721 	/* Reset the IP */
722 	writel(IFI_CANFD_STCMD_HARDRESET, priv->base + IFI_CANFD_STCMD);
723 	writel(IFI_CANFD_STCMD_ENABLE_7_9_8_8_TIMING,
724 	       priv->base + IFI_CANFD_STCMD);
725 
726 	ifi_canfd_set_bittiming(ndev);
727 	ifi_canfd_set_filters(ndev);
728 
729 	/* Reset FIFOs */
730 	writel(IFI_CANFD_RXSTCMD_RESET, priv->base + IFI_CANFD_RXSTCMD);
731 	writel(0, priv->base + IFI_CANFD_RXSTCMD);
732 	writel(IFI_CANFD_TXSTCMD_RESET, priv->base + IFI_CANFD_TXSTCMD);
733 	writel(0, priv->base + IFI_CANFD_TXSTCMD);
734 
735 	/* Repeat transmission until successful */
736 	writel(0, priv->base + IFI_CANFD_REPEAT);
737 	writel(0, priv->base + IFI_CANFD_SUSPEND);
738 
739 	/* Clear all pending interrupts */
740 	writel((u32)(~IFI_CANFD_INTERRUPT_SET_IRQ),
741 	       priv->base + IFI_CANFD_INTERRUPT);
742 
743 	stcmd = IFI_CANFD_STCMD_ENABLE | IFI_CANFD_STCMD_NORMAL_MODE |
744 		IFI_CANFD_STCMD_ENABLE_7_9_8_8_TIMING;
745 
746 	if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
747 		stcmd |= IFI_CANFD_STCMD_BUSMONITOR;
748 
749 	if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
750 		stcmd |= IFI_CANFD_STCMD_LOOPBACK;
751 
752 	if ((priv->can.ctrlmode & CAN_CTRLMODE_FD) &&
753 	    !(priv->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO))
754 		stcmd |= IFI_CANFD_STCMD_ENABLE_ISO;
755 
756 	if (!(priv->can.ctrlmode & CAN_CTRLMODE_FD))
757 		stcmd |= IFI_CANFD_STCMD_DISABLE_CANFD;
758 
759 	priv->can.state = CAN_STATE_ERROR_ACTIVE;
760 
761 	ifi_canfd_irq_enable(ndev, 1);
762 
763 	/* Unlock, reset and enable the error counter. */
764 	writel(IFI_CANFD_ERROR_CTR_UNLOCK_MAGIC,
765 	       priv->base + IFI_CANFD_ERROR_CTR);
766 	writel(IFI_CANFD_ERROR_CTR_ER_RESET, priv->base + IFI_CANFD_ERROR_CTR);
767 	writel(IFI_CANFD_ERROR_CTR_ER_ENABLE, priv->base + IFI_CANFD_ERROR_CTR);
768 
769 	/* Enable controller */
770 	writel(stcmd, priv->base + IFI_CANFD_STCMD);
771 }
772 
773 static void ifi_canfd_stop(struct net_device *ndev)
774 {
775 	struct ifi_canfd_priv *priv = netdev_priv(ndev);
776 
777 	/* Reset and disable the error counter. */
778 	writel(IFI_CANFD_ERROR_CTR_ER_RESET, priv->base + IFI_CANFD_ERROR_CTR);
779 	writel(0, priv->base + IFI_CANFD_ERROR_CTR);
780 
781 	/* Reset the IP */
782 	writel(IFI_CANFD_STCMD_HARDRESET, priv->base + IFI_CANFD_STCMD);
783 
784 	/* Mask all interrupts */
785 	writel(~0, priv->base + IFI_CANFD_IRQMASK);
786 
787 	/* Clear all pending interrupts */
788 	writel((u32)(~IFI_CANFD_INTERRUPT_SET_IRQ),
789 	       priv->base + IFI_CANFD_INTERRUPT);
790 
791 	/* Set the state as STOPPED */
792 	priv->can.state = CAN_STATE_STOPPED;
793 }
794 
795 static int ifi_canfd_set_mode(struct net_device *ndev, enum can_mode mode)
796 {
797 	switch (mode) {
798 	case CAN_MODE_START:
799 		ifi_canfd_start(ndev);
800 		netif_wake_queue(ndev);
801 		break;
802 	default:
803 		return -EOPNOTSUPP;
804 	}
805 
806 	return 0;
807 }
808 
809 static int ifi_canfd_open(struct net_device *ndev)
810 {
811 	struct ifi_canfd_priv *priv = netdev_priv(ndev);
812 	int ret;
813 
814 	ret = open_candev(ndev);
815 	if (ret) {
816 		netdev_err(ndev, "Failed to open CAN device\n");
817 		return ret;
818 	}
819 
820 	/* Register interrupt handler */
821 	ret = request_irq(ndev->irq, ifi_canfd_isr, IRQF_SHARED,
822 			  ndev->name, ndev);
823 	if (ret < 0) {
824 		netdev_err(ndev, "Failed to request interrupt\n");
825 		goto err_irq;
826 	}
827 
828 	ifi_canfd_start(ndev);
829 
830 	napi_enable(&priv->napi);
831 	netif_start_queue(ndev);
832 
833 	return 0;
834 err_irq:
835 	close_candev(ndev);
836 	return ret;
837 }
838 
839 static int ifi_canfd_close(struct net_device *ndev)
840 {
841 	struct ifi_canfd_priv *priv = netdev_priv(ndev);
842 
843 	netif_stop_queue(ndev);
844 	napi_disable(&priv->napi);
845 
846 	ifi_canfd_stop(ndev);
847 
848 	free_irq(ndev->irq, ndev);
849 
850 	close_candev(ndev);
851 
852 	return 0;
853 }
854 
855 static netdev_tx_t ifi_canfd_start_xmit(struct sk_buff *skb,
856 					struct net_device *ndev)
857 {
858 	struct ifi_canfd_priv *priv = netdev_priv(ndev);
859 	struct canfd_frame *cf = (struct canfd_frame *)skb->data;
860 	u32 txst, txid, txdlc;
861 	int i;
862 
863 	if (can_dev_dropped_skb(ndev, skb))
864 		return NETDEV_TX_OK;
865 
866 	/* Check if the TX buffer is full */
867 	txst = readl(priv->base + IFI_CANFD_TXSTCMD);
868 	if (txst & IFI_CANFD_TXSTCMD_FULL) {
869 		netif_stop_queue(ndev);
870 		netdev_err(ndev, "BUG! TX FIFO full when queue awake!\n");
871 		return NETDEV_TX_BUSY;
872 	}
873 
874 	netif_stop_queue(ndev);
875 
876 	if (cf->can_id & CAN_EFF_FLAG) {
877 		txid = cf->can_id & CAN_EFF_MASK;
878 		/*
879 		 * In case the Extended ID frame is transmitted, the
880 		 * standard and extended part of the ID are swapped
881 		 * in the register, so swap them back to send the
882 		 * correct ID.
883 		 */
884 		txid = (txid >> IFI_CANFD_TXFIFO_ID_ID_XTD_WIDTH) |
885 		       ((txid & IFI_CANFD_TXFIFO_ID_ID_XTD_MASK) <<
886 		         IFI_CANFD_TXFIFO_ID_ID_XTD_OFFSET);
887 		txid |= IFI_CANFD_TXFIFO_ID_IDE;
888 	} else {
889 		txid = cf->can_id & CAN_SFF_MASK;
890 	}
891 
892 	txdlc = can_fd_len2dlc(cf->len);
893 	if ((priv->can.ctrlmode & CAN_CTRLMODE_FD) && can_is_canfd_skb(skb)) {
894 		txdlc |= IFI_CANFD_TXFIFO_DLC_EDL;
895 		if (cf->flags & CANFD_BRS)
896 			txdlc |= IFI_CANFD_TXFIFO_DLC_BRS;
897 	}
898 
899 	if (cf->can_id & CAN_RTR_FLAG)
900 		txdlc |= IFI_CANFD_TXFIFO_DLC_RTR;
901 
902 	/* message ram configuration */
903 	writel(txid, priv->base + IFI_CANFD_TXFIFO_ID);
904 	writel(txdlc, priv->base + IFI_CANFD_TXFIFO_DLC);
905 
906 	for (i = 0; i < cf->len; i += 4) {
907 		writel(*(u32 *)(cf->data + i),
908 		       priv->base + IFI_CANFD_TXFIFO_DATA + i);
909 	}
910 
911 	writel(0, priv->base + IFI_CANFD_TXFIFO_REPEATCOUNT);
912 	writel(0, priv->base + IFI_CANFD_TXFIFO_SUSPEND_US);
913 
914 	can_put_echo_skb(skb, ndev, 0, 0);
915 
916 	/* Start the transmission */
917 	writel(IFI_CANFD_TXSTCMD_ADD_MSG, priv->base + IFI_CANFD_TXSTCMD);
918 
919 	return NETDEV_TX_OK;
920 }
921 
922 static const struct net_device_ops ifi_canfd_netdev_ops = {
923 	.ndo_open	= ifi_canfd_open,
924 	.ndo_stop	= ifi_canfd_close,
925 	.ndo_start_xmit	= ifi_canfd_start_xmit,
926 	.ndo_change_mtu	= can_change_mtu,
927 };
928 
929 static const struct ethtool_ops ifi_canfd_ethtool_ops = {
930 	.get_ts_info = ethtool_op_get_ts_info,
931 };
932 
933 static int ifi_canfd_plat_probe(struct platform_device *pdev)
934 {
935 	struct device *dev = &pdev->dev;
936 	struct net_device *ndev;
937 	struct ifi_canfd_priv *priv;
938 	void __iomem *addr;
939 	int irq, ret;
940 	u32 id, rev;
941 
942 	addr = devm_platform_ioremap_resource(pdev, 0);
943 	if (IS_ERR(addr))
944 		return PTR_ERR(addr);
945 
946 	irq = platform_get_irq(pdev, 0);
947 	if (irq < 0)
948 		return -EINVAL;
949 
950 	id = readl(addr + IFI_CANFD_IP_ID);
951 	if (id != IFI_CANFD_IP_ID_VALUE) {
952 		dev_err(dev, "This block is not IFI CANFD, id=%08x\n", id);
953 		return -EINVAL;
954 	}
955 
956 	rev = readl(addr + IFI_CANFD_VER) & IFI_CANFD_VER_REV_MASK;
957 	if (rev < IFI_CANFD_VER_REV_MIN_SUPPORTED) {
958 		dev_err(dev, "This block is too old (rev %i), minimum supported is rev %i\n",
959 			rev, IFI_CANFD_VER_REV_MIN_SUPPORTED);
960 		return -EINVAL;
961 	}
962 
963 	ndev = alloc_candev(sizeof(*priv), 1);
964 	if (!ndev)
965 		return -ENOMEM;
966 
967 	ndev->irq = irq;
968 	ndev->flags |= IFF_ECHO;	/* we support local echo */
969 	ndev->netdev_ops = &ifi_canfd_netdev_ops;
970 	ndev->ethtool_ops = &ifi_canfd_ethtool_ops;
971 
972 	priv = netdev_priv(ndev);
973 	priv->ndev = ndev;
974 	priv->base = addr;
975 
976 	netif_napi_add(ndev, &priv->napi, ifi_canfd_poll);
977 
978 	priv->can.state = CAN_STATE_STOPPED;
979 
980 	priv->can.clock.freq = readl(addr + IFI_CANFD_CANCLOCK);
981 
982 	priv->can.bittiming_const	= &ifi_canfd_bittiming_const;
983 	priv->can.data_bittiming_const	= &ifi_canfd_bittiming_const;
984 	priv->can.do_set_mode		= ifi_canfd_set_mode;
985 	priv->can.do_get_berr_counter	= ifi_canfd_get_berr_counter;
986 
987 	/* IFI CANFD can do both Bosch FD and ISO FD */
988 	priv->can.ctrlmode = CAN_CTRLMODE_FD;
989 
990 	/* IFI CANFD can do both Bosch FD and ISO FD */
991 	priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
992 				       CAN_CTRLMODE_LISTENONLY |
993 				       CAN_CTRLMODE_FD |
994 				       CAN_CTRLMODE_FD_NON_ISO |
995 				       CAN_CTRLMODE_BERR_REPORTING;
996 
997 	platform_set_drvdata(pdev, ndev);
998 	SET_NETDEV_DEV(ndev, dev);
999 
1000 	ret = register_candev(ndev);
1001 	if (ret) {
1002 		dev_err(dev, "Failed to register (ret=%d)\n", ret);
1003 		goto err_reg;
1004 	}
1005 
1006 	dev_info(dev, "Driver registered: regs=%p, irq=%d, clock=%d\n",
1007 		 priv->base, ndev->irq, priv->can.clock.freq);
1008 
1009 	return 0;
1010 
1011 err_reg:
1012 	free_candev(ndev);
1013 	return ret;
1014 }
1015 
1016 static int ifi_canfd_plat_remove(struct platform_device *pdev)
1017 {
1018 	struct net_device *ndev = platform_get_drvdata(pdev);
1019 
1020 	unregister_candev(ndev);
1021 	platform_set_drvdata(pdev, NULL);
1022 	free_candev(ndev);
1023 
1024 	return 0;
1025 }
1026 
1027 static const struct of_device_id ifi_canfd_of_table[] = {
1028 	{ .compatible = "ifi,canfd-1.0", .data = NULL },
1029 	{ /* sentinel */ },
1030 };
1031 MODULE_DEVICE_TABLE(of, ifi_canfd_of_table);
1032 
1033 static struct platform_driver ifi_canfd_plat_driver = {
1034 	.driver = {
1035 		.name		= KBUILD_MODNAME,
1036 		.of_match_table	= ifi_canfd_of_table,
1037 	},
1038 	.probe	= ifi_canfd_plat_probe,
1039 	.remove	= ifi_canfd_plat_remove,
1040 };
1041 
1042 module_platform_driver(ifi_canfd_plat_driver);
1043 
1044 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
1045 MODULE_LICENSE("GPL v2");
1046 MODULE_DESCRIPTION("CAN bus driver for IFI CANFD controller");
1047