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