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/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 
ifi_canfd_irq_enable(struct net_device * ndev,bool enable)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 
ifi_canfd_read_fifo(struct net_device * ndev)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 
ifi_canfd_do_rx_poll(struct net_device * ndev,int quota)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 	return pkts;
349 }
350 
ifi_canfd_handle_lost_msg(struct net_device * ndev)351 static int ifi_canfd_handle_lost_msg(struct net_device *ndev)
352 {
353 	struct net_device_stats *stats = &ndev->stats;
354 	struct sk_buff *skb;
355 	struct can_frame *frame;
356 
357 	netdev_err(ndev, "RX FIFO overflow, message(s) lost.\n");
358 
359 	stats->rx_errors++;
360 	stats->rx_over_errors++;
361 
362 	skb = alloc_can_err_skb(ndev, &frame);
363 	if (unlikely(!skb))
364 		return 0;
365 
366 	frame->can_id |= CAN_ERR_CRTL;
367 	frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
368 
369 	netif_receive_skb(skb);
370 
371 	return 1;
372 }
373 
ifi_canfd_handle_lec_err(struct net_device * ndev)374 static int ifi_canfd_handle_lec_err(struct net_device *ndev)
375 {
376 	struct ifi_canfd_priv *priv = netdev_priv(ndev);
377 	struct net_device_stats *stats = &ndev->stats;
378 	struct can_frame *cf;
379 	struct sk_buff *skb;
380 	u32 errctr = readl(priv->base + IFI_CANFD_ERROR_CTR);
381 	const u32 errmask = IFI_CANFD_ERROR_CTR_OVERLOAD_FIRST |
382 			    IFI_CANFD_ERROR_CTR_ACK_ERROR_FIRST |
383 			    IFI_CANFD_ERROR_CTR_BIT0_ERROR_FIRST |
384 			    IFI_CANFD_ERROR_CTR_BIT1_ERROR_FIRST |
385 			    IFI_CANFD_ERROR_CTR_STUFF_ERROR_FIRST |
386 			    IFI_CANFD_ERROR_CTR_CRC_ERROR_FIRST |
387 			    IFI_CANFD_ERROR_CTR_FORM_ERROR_FIRST;
388 
389 	if (!(errctr & errmask))	/* No error happened. */
390 		return 0;
391 
392 	priv->can.can_stats.bus_error++;
393 	stats->rx_errors++;
394 
395 	/* Propagate the error condition to the CAN stack. */
396 	skb = alloc_can_err_skb(ndev, &cf);
397 	if (unlikely(!skb))
398 		return 0;
399 
400 	/* Read the error counter register and check for new errors. */
401 	cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
402 
403 	if (errctr & IFI_CANFD_ERROR_CTR_OVERLOAD_FIRST)
404 		cf->data[2] |= CAN_ERR_PROT_OVERLOAD;
405 
406 	if (errctr & IFI_CANFD_ERROR_CTR_ACK_ERROR_FIRST)
407 		cf->data[3] = CAN_ERR_PROT_LOC_ACK;
408 
409 	if (errctr & IFI_CANFD_ERROR_CTR_BIT0_ERROR_FIRST)
410 		cf->data[2] |= CAN_ERR_PROT_BIT0;
411 
412 	if (errctr & IFI_CANFD_ERROR_CTR_BIT1_ERROR_FIRST)
413 		cf->data[2] |= CAN_ERR_PROT_BIT1;
414 
415 	if (errctr & IFI_CANFD_ERROR_CTR_STUFF_ERROR_FIRST)
416 		cf->data[2] |= CAN_ERR_PROT_STUFF;
417 
418 	if (errctr & IFI_CANFD_ERROR_CTR_CRC_ERROR_FIRST)
419 		cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
420 
421 	if (errctr & IFI_CANFD_ERROR_CTR_FORM_ERROR_FIRST)
422 		cf->data[2] |= CAN_ERR_PROT_FORM;
423 
424 	/* Reset the error counter, ack the IRQ and re-enable the counter. */
425 	writel(IFI_CANFD_ERROR_CTR_ER_RESET, priv->base + IFI_CANFD_ERROR_CTR);
426 	writel(IFI_CANFD_INTERRUPT_ERROR_COUNTER,
427 	       priv->base + IFI_CANFD_INTERRUPT);
428 	writel(IFI_CANFD_ERROR_CTR_ER_ENABLE, priv->base + IFI_CANFD_ERROR_CTR);
429 
430 	netif_receive_skb(skb);
431 
432 	return 1;
433 }
434 
ifi_canfd_get_berr_counter(const struct net_device * ndev,struct can_berr_counter * bec)435 static int ifi_canfd_get_berr_counter(const struct net_device *ndev,
436 				      struct can_berr_counter *bec)
437 {
438 	struct ifi_canfd_priv *priv = netdev_priv(ndev);
439 	u32 err;
440 
441 	err = readl(priv->base + IFI_CANFD_ERROR);
442 	bec->rxerr = (err >> IFI_CANFD_ERROR_RX_OFFSET) &
443 		     IFI_CANFD_ERROR_RX_MASK;
444 	bec->txerr = (err >> IFI_CANFD_ERROR_TX_OFFSET) &
445 		     IFI_CANFD_ERROR_TX_MASK;
446 
447 	return 0;
448 }
449 
ifi_canfd_handle_state_change(struct net_device * ndev,enum can_state new_state)450 static int ifi_canfd_handle_state_change(struct net_device *ndev,
451 					 enum can_state new_state)
452 {
453 	struct ifi_canfd_priv *priv = netdev_priv(ndev);
454 	struct can_frame *cf;
455 	struct sk_buff *skb;
456 	struct can_berr_counter bec;
457 
458 	switch (new_state) {
459 	case CAN_STATE_ERROR_ACTIVE:
460 		/* error active state */
461 		priv->can.can_stats.error_warning++;
462 		priv->can.state = CAN_STATE_ERROR_ACTIVE;
463 		break;
464 	case CAN_STATE_ERROR_WARNING:
465 		/* error warning state */
466 		priv->can.can_stats.error_warning++;
467 		priv->can.state = CAN_STATE_ERROR_WARNING;
468 		break;
469 	case CAN_STATE_ERROR_PASSIVE:
470 		/* error passive state */
471 		priv->can.can_stats.error_passive++;
472 		priv->can.state = CAN_STATE_ERROR_PASSIVE;
473 		break;
474 	case CAN_STATE_BUS_OFF:
475 		/* bus-off state */
476 		priv->can.state = CAN_STATE_BUS_OFF;
477 		ifi_canfd_irq_enable(ndev, 0);
478 		priv->can.can_stats.bus_off++;
479 		can_bus_off(ndev);
480 		break;
481 	default:
482 		break;
483 	}
484 
485 	/* propagate the error condition to the CAN stack */
486 	skb = alloc_can_err_skb(ndev, &cf);
487 	if (unlikely(!skb))
488 		return 0;
489 
490 	ifi_canfd_get_berr_counter(ndev, &bec);
491 
492 	switch (new_state) {
493 	case CAN_STATE_ERROR_WARNING:
494 		/* error warning state */
495 		cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT;
496 		cf->data[1] = (bec.txerr > bec.rxerr) ?
497 			CAN_ERR_CRTL_TX_WARNING :
498 			CAN_ERR_CRTL_RX_WARNING;
499 		cf->data[6] = bec.txerr;
500 		cf->data[7] = bec.rxerr;
501 		break;
502 	case CAN_STATE_ERROR_PASSIVE:
503 		/* error passive state */
504 		cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT;
505 		cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
506 		if (bec.txerr > 127)
507 			cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
508 		cf->data[6] = bec.txerr;
509 		cf->data[7] = bec.rxerr;
510 		break;
511 	case CAN_STATE_BUS_OFF:
512 		/* bus-off state */
513 		cf->can_id |= CAN_ERR_BUSOFF;
514 		break;
515 	default:
516 		break;
517 	}
518 
519 	netif_receive_skb(skb);
520 
521 	return 1;
522 }
523 
ifi_canfd_handle_state_errors(struct net_device * ndev)524 static int ifi_canfd_handle_state_errors(struct net_device *ndev)
525 {
526 	struct ifi_canfd_priv *priv = netdev_priv(ndev);
527 	u32 stcmd = readl(priv->base + IFI_CANFD_STCMD);
528 	int work_done = 0;
529 
530 	if ((stcmd & IFI_CANFD_STCMD_ERROR_ACTIVE) &&
531 	    (priv->can.state != CAN_STATE_ERROR_ACTIVE)) {
532 		netdev_dbg(ndev, "Error, entered active state\n");
533 		work_done += ifi_canfd_handle_state_change(ndev,
534 						CAN_STATE_ERROR_ACTIVE);
535 	}
536 
537 	if ((stcmd & IFI_CANFD_STCMD_ERROR_WARNING) &&
538 	    (priv->can.state != CAN_STATE_ERROR_WARNING)) {
539 		netdev_dbg(ndev, "Error, entered warning state\n");
540 		work_done += ifi_canfd_handle_state_change(ndev,
541 						CAN_STATE_ERROR_WARNING);
542 	}
543 
544 	if ((stcmd & IFI_CANFD_STCMD_ERROR_PASSIVE) &&
545 	    (priv->can.state != CAN_STATE_ERROR_PASSIVE)) {
546 		netdev_dbg(ndev, "Error, entered passive state\n");
547 		work_done += ifi_canfd_handle_state_change(ndev,
548 						CAN_STATE_ERROR_PASSIVE);
549 	}
550 
551 	if ((stcmd & IFI_CANFD_STCMD_BUSOFF) &&
552 	    (priv->can.state != CAN_STATE_BUS_OFF)) {
553 		netdev_dbg(ndev, "Error, entered bus-off state\n");
554 		work_done += ifi_canfd_handle_state_change(ndev,
555 						CAN_STATE_BUS_OFF);
556 	}
557 
558 	return work_done;
559 }
560 
ifi_canfd_poll(struct napi_struct * napi,int quota)561 static int ifi_canfd_poll(struct napi_struct *napi, int quota)
562 {
563 	struct net_device *ndev = napi->dev;
564 	struct ifi_canfd_priv *priv = netdev_priv(ndev);
565 	u32 rxstcmd = readl(priv->base + IFI_CANFD_RXSTCMD);
566 	int work_done = 0;
567 
568 	/* Handle bus state changes */
569 	work_done += ifi_canfd_handle_state_errors(ndev);
570 
571 	/* Handle lost messages on RX */
572 	if (rxstcmd & IFI_CANFD_RXSTCMD_OVERFLOW)
573 		work_done += ifi_canfd_handle_lost_msg(ndev);
574 
575 	/* Handle lec errors on the bus */
576 	if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
577 		work_done += ifi_canfd_handle_lec_err(ndev);
578 
579 	/* Handle normal messages on RX */
580 	if (!(rxstcmd & IFI_CANFD_RXSTCMD_EMPTY))
581 		work_done += ifi_canfd_do_rx_poll(ndev, quota - work_done);
582 
583 	if (work_done < quota) {
584 		napi_complete_done(napi, work_done);
585 		ifi_canfd_irq_enable(ndev, 1);
586 	}
587 
588 	return work_done;
589 }
590 
ifi_canfd_isr(int irq,void * dev_id)591 static irqreturn_t ifi_canfd_isr(int irq, void *dev_id)
592 {
593 	struct net_device *ndev = (struct net_device *)dev_id;
594 	struct ifi_canfd_priv *priv = netdev_priv(ndev);
595 	struct net_device_stats *stats = &ndev->stats;
596 	const u32 rx_irq_mask = IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY |
597 				IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY_PER |
598 				IFI_CANFD_INTERRUPT_ERROR_COUNTER |
599 				IFI_CANFD_INTERRUPT_ERROR_STATE_CHG |
600 				IFI_CANFD_INTERRUPT_ERROR_WARNING |
601 				IFI_CANFD_INTERRUPT_ERROR_BUSOFF;
602 	const u32 tx_irq_mask = IFI_CANFD_INTERRUPT_TXFIFO_EMPTY |
603 				IFI_CANFD_INTERRUPT_TXFIFO_REMOVE;
604 	const u32 clr_irq_mask = ~((u32)IFI_CANFD_INTERRUPT_SET_IRQ);
605 	u32 isr;
606 
607 	isr = readl(priv->base + IFI_CANFD_INTERRUPT);
608 
609 	/* No interrupt */
610 	if (isr == 0)
611 		return IRQ_NONE;
612 
613 	/* Clear all pending interrupts but ErrWarn */
614 	writel(clr_irq_mask, priv->base + IFI_CANFD_INTERRUPT);
615 
616 	/* RX IRQ or bus warning, start NAPI */
617 	if (isr & rx_irq_mask) {
618 		ifi_canfd_irq_enable(ndev, 0);
619 		napi_schedule(&priv->napi);
620 	}
621 
622 	/* TX IRQ */
623 	if (isr & IFI_CANFD_INTERRUPT_TXFIFO_REMOVE) {
624 		stats->tx_bytes += can_get_echo_skb(ndev, 0, NULL);
625 		stats->tx_packets++;
626 	}
627 
628 	if (isr & tx_irq_mask)
629 		netif_wake_queue(ndev);
630 
631 	return IRQ_HANDLED;
632 }
633 
634 static const struct can_bittiming_const ifi_canfd_bittiming_const = {
635 	.name		= KBUILD_MODNAME,
636 	.tseg1_min	= 1,	/* Time segment 1 = prop_seg + phase_seg1 */
637 	.tseg1_max	= 256,
638 	.tseg2_min	= 2,	/* Time segment 2 = phase_seg2 */
639 	.tseg2_max	= 256,
640 	.sjw_max	= 128,
641 	.brp_min	= 2,
642 	.brp_max	= 512,
643 	.brp_inc	= 1,
644 };
645 
ifi_canfd_set_bittiming(struct net_device * ndev)646 static void ifi_canfd_set_bittiming(struct net_device *ndev)
647 {
648 	struct ifi_canfd_priv *priv = netdev_priv(ndev);
649 	const struct can_bittiming *bt = &priv->can.bittiming;
650 	const struct can_bittiming *dbt = &priv->can.data_bittiming;
651 	u16 brp, sjw, tseg1, tseg2, tdc;
652 
653 	/* Configure bit timing */
654 	brp = bt->brp - 2;
655 	sjw = bt->sjw - 1;
656 	tseg1 = bt->prop_seg + bt->phase_seg1 - 1;
657 	tseg2 = bt->phase_seg2 - 2;
658 	writel((tseg2 << IFI_CANFD_TIME_TIMEB_OFF) |
659 	       (tseg1 << IFI_CANFD_TIME_TIMEA_OFF) |
660 	       (brp << IFI_CANFD_TIME_PRESCALE_OFF) |
661 	       (sjw << IFI_CANFD_TIME_SJW_OFF_7_9_8_8),
662 	       priv->base + IFI_CANFD_TIME);
663 
664 	/* Configure data bit timing */
665 	brp = dbt->brp - 2;
666 	sjw = dbt->sjw - 1;
667 	tseg1 = dbt->prop_seg + dbt->phase_seg1 - 1;
668 	tseg2 = dbt->phase_seg2 - 2;
669 	writel((tseg2 << IFI_CANFD_TIME_TIMEB_OFF) |
670 	       (tseg1 << IFI_CANFD_TIME_TIMEA_OFF) |
671 	       (brp << IFI_CANFD_TIME_PRESCALE_OFF) |
672 	       (sjw << IFI_CANFD_TIME_SJW_OFF_7_9_8_8),
673 	       priv->base + IFI_CANFD_FTIME);
674 
675 	/* Configure transmitter delay */
676 	tdc = dbt->brp * (dbt->prop_seg + dbt->phase_seg1);
677 	tdc &= IFI_CANFD_TDELAY_MASK;
678 	writel(IFI_CANFD_TDELAY_EN | tdc, priv->base + IFI_CANFD_TDELAY);
679 }
680 
ifi_canfd_set_filter(struct net_device * ndev,const u32 id,const u32 mask,const u32 ident)681 static void ifi_canfd_set_filter(struct net_device *ndev, const u32 id,
682 				 const u32 mask, const u32 ident)
683 {
684 	struct ifi_canfd_priv *priv = netdev_priv(ndev);
685 
686 	writel(mask, priv->base + IFI_CANFD_FILTER_MASK(id));
687 	writel(ident, priv->base + IFI_CANFD_FILTER_IDENT(id));
688 }
689 
ifi_canfd_set_filters(struct net_device * ndev)690 static void ifi_canfd_set_filters(struct net_device *ndev)
691 {
692 	/* Receive all CAN frames (standard ID) */
693 	ifi_canfd_set_filter(ndev, 0,
694 			     IFI_CANFD_FILTER_MASK_VALID |
695 			     IFI_CANFD_FILTER_MASK_EXT,
696 			     IFI_CANFD_FILTER_IDENT_VALID);
697 
698 	/* Receive all CAN frames (extended ID) */
699 	ifi_canfd_set_filter(ndev, 1,
700 			     IFI_CANFD_FILTER_MASK_VALID |
701 			     IFI_CANFD_FILTER_MASK_EXT,
702 			     IFI_CANFD_FILTER_IDENT_VALID |
703 			     IFI_CANFD_FILTER_IDENT_IDE);
704 
705 	/* Receive all CANFD frames */
706 	ifi_canfd_set_filter(ndev, 2,
707 			     IFI_CANFD_FILTER_MASK_VALID |
708 			     IFI_CANFD_FILTER_MASK_EDL |
709 			     IFI_CANFD_FILTER_MASK_EXT,
710 			     IFI_CANFD_FILTER_IDENT_VALID |
711 			     IFI_CANFD_FILTER_IDENT_CANFD |
712 			     IFI_CANFD_FILTER_IDENT_IDE);
713 }
714 
ifi_canfd_start(struct net_device * ndev)715 static void ifi_canfd_start(struct net_device *ndev)
716 {
717 	struct ifi_canfd_priv *priv = netdev_priv(ndev);
718 	u32 stcmd;
719 
720 	/* Reset the IP */
721 	writel(IFI_CANFD_STCMD_HARDRESET, priv->base + IFI_CANFD_STCMD);
722 	writel(IFI_CANFD_STCMD_ENABLE_7_9_8_8_TIMING,
723 	       priv->base + IFI_CANFD_STCMD);
724 
725 	ifi_canfd_set_bittiming(ndev);
726 	ifi_canfd_set_filters(ndev);
727 
728 	/* Reset FIFOs */
729 	writel(IFI_CANFD_RXSTCMD_RESET, priv->base + IFI_CANFD_RXSTCMD);
730 	writel(0, priv->base + IFI_CANFD_RXSTCMD);
731 	writel(IFI_CANFD_TXSTCMD_RESET, priv->base + IFI_CANFD_TXSTCMD);
732 	writel(0, priv->base + IFI_CANFD_TXSTCMD);
733 
734 	/* Repeat transmission until successful */
735 	writel(0, priv->base + IFI_CANFD_REPEAT);
736 	writel(0, priv->base + IFI_CANFD_SUSPEND);
737 
738 	/* Clear all pending interrupts */
739 	writel((u32)(~IFI_CANFD_INTERRUPT_SET_IRQ),
740 	       priv->base + IFI_CANFD_INTERRUPT);
741 
742 	stcmd = IFI_CANFD_STCMD_ENABLE | IFI_CANFD_STCMD_NORMAL_MODE |
743 		IFI_CANFD_STCMD_ENABLE_7_9_8_8_TIMING;
744 
745 	if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
746 		stcmd |= IFI_CANFD_STCMD_BUSMONITOR;
747 
748 	if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
749 		stcmd |= IFI_CANFD_STCMD_LOOPBACK;
750 
751 	if ((priv->can.ctrlmode & CAN_CTRLMODE_FD) &&
752 	    !(priv->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO))
753 		stcmd |= IFI_CANFD_STCMD_ENABLE_ISO;
754 
755 	if (!(priv->can.ctrlmode & CAN_CTRLMODE_FD))
756 		stcmd |= IFI_CANFD_STCMD_DISABLE_CANFD;
757 
758 	priv->can.state = CAN_STATE_ERROR_ACTIVE;
759 
760 	ifi_canfd_irq_enable(ndev, 1);
761 
762 	/* Unlock, reset and enable the error counter. */
763 	writel(IFI_CANFD_ERROR_CTR_UNLOCK_MAGIC,
764 	       priv->base + IFI_CANFD_ERROR_CTR);
765 	writel(IFI_CANFD_ERROR_CTR_ER_RESET, priv->base + IFI_CANFD_ERROR_CTR);
766 	writel(IFI_CANFD_ERROR_CTR_ER_ENABLE, priv->base + IFI_CANFD_ERROR_CTR);
767 
768 	/* Enable controller */
769 	writel(stcmd, priv->base + IFI_CANFD_STCMD);
770 }
771 
ifi_canfd_stop(struct net_device * ndev)772 static void ifi_canfd_stop(struct net_device *ndev)
773 {
774 	struct ifi_canfd_priv *priv = netdev_priv(ndev);
775 
776 	/* Reset and disable the error counter. */
777 	writel(IFI_CANFD_ERROR_CTR_ER_RESET, priv->base + IFI_CANFD_ERROR_CTR);
778 	writel(0, priv->base + IFI_CANFD_ERROR_CTR);
779 
780 	/* Reset the IP */
781 	writel(IFI_CANFD_STCMD_HARDRESET, priv->base + IFI_CANFD_STCMD);
782 
783 	/* Mask all interrupts */
784 	writel(~0, priv->base + IFI_CANFD_IRQMASK);
785 
786 	/* Clear all pending interrupts */
787 	writel((u32)(~IFI_CANFD_INTERRUPT_SET_IRQ),
788 	       priv->base + IFI_CANFD_INTERRUPT);
789 
790 	/* Set the state as STOPPED */
791 	priv->can.state = CAN_STATE_STOPPED;
792 }
793 
ifi_canfd_set_mode(struct net_device * ndev,enum can_mode mode)794 static int ifi_canfd_set_mode(struct net_device *ndev, enum can_mode mode)
795 {
796 	switch (mode) {
797 	case CAN_MODE_START:
798 		ifi_canfd_start(ndev);
799 		netif_wake_queue(ndev);
800 		break;
801 	default:
802 		return -EOPNOTSUPP;
803 	}
804 
805 	return 0;
806 }
807 
ifi_canfd_open(struct net_device * ndev)808 static int ifi_canfd_open(struct net_device *ndev)
809 {
810 	struct ifi_canfd_priv *priv = netdev_priv(ndev);
811 	int ret;
812 
813 	ret = open_candev(ndev);
814 	if (ret) {
815 		netdev_err(ndev, "Failed to open CAN device\n");
816 		return ret;
817 	}
818 
819 	/* Register interrupt handler */
820 	ret = request_irq(ndev->irq, ifi_canfd_isr, IRQF_SHARED,
821 			  ndev->name, ndev);
822 	if (ret < 0) {
823 		netdev_err(ndev, "Failed to request interrupt\n");
824 		goto err_irq;
825 	}
826 
827 	ifi_canfd_start(ndev);
828 
829 	napi_enable(&priv->napi);
830 	netif_start_queue(ndev);
831 
832 	return 0;
833 err_irq:
834 	close_candev(ndev);
835 	return ret;
836 }
837 
ifi_canfd_close(struct net_device * ndev)838 static int ifi_canfd_close(struct net_device *ndev)
839 {
840 	struct ifi_canfd_priv *priv = netdev_priv(ndev);
841 
842 	netif_stop_queue(ndev);
843 	napi_disable(&priv->napi);
844 
845 	ifi_canfd_stop(ndev);
846 
847 	free_irq(ndev->irq, ndev);
848 
849 	close_candev(ndev);
850 
851 	return 0;
852 }
853 
ifi_canfd_start_xmit(struct sk_buff * skb,struct net_device * ndev)854 static netdev_tx_t ifi_canfd_start_xmit(struct sk_buff *skb,
855 					struct net_device *ndev)
856 {
857 	struct ifi_canfd_priv *priv = netdev_priv(ndev);
858 	struct canfd_frame *cf = (struct canfd_frame *)skb->data;
859 	u32 txst, txid, txdlc;
860 	int i;
861 
862 	if (can_dev_dropped_skb(ndev, skb))
863 		return NETDEV_TX_OK;
864 
865 	/* Check if the TX buffer is full */
866 	txst = readl(priv->base + IFI_CANFD_TXSTCMD);
867 	if (txst & IFI_CANFD_TXSTCMD_FULL) {
868 		netif_stop_queue(ndev);
869 		netdev_err(ndev, "BUG! TX FIFO full when queue awake!\n");
870 		return NETDEV_TX_BUSY;
871 	}
872 
873 	netif_stop_queue(ndev);
874 
875 	if (cf->can_id & CAN_EFF_FLAG) {
876 		txid = cf->can_id & CAN_EFF_MASK;
877 		/*
878 		 * In case the Extended ID frame is transmitted, the
879 		 * standard and extended part of the ID are swapped
880 		 * in the register, so swap them back to send the
881 		 * correct ID.
882 		 */
883 		txid = (txid >> IFI_CANFD_TXFIFO_ID_ID_XTD_WIDTH) |
884 		       ((txid & IFI_CANFD_TXFIFO_ID_ID_XTD_MASK) <<
885 		         IFI_CANFD_TXFIFO_ID_ID_XTD_OFFSET);
886 		txid |= IFI_CANFD_TXFIFO_ID_IDE;
887 	} else {
888 		txid = cf->can_id & CAN_SFF_MASK;
889 	}
890 
891 	txdlc = can_fd_len2dlc(cf->len);
892 	if ((priv->can.ctrlmode & CAN_CTRLMODE_FD) && can_is_canfd_skb(skb)) {
893 		txdlc |= IFI_CANFD_TXFIFO_DLC_EDL;
894 		if (cf->flags & CANFD_BRS)
895 			txdlc |= IFI_CANFD_TXFIFO_DLC_BRS;
896 	}
897 
898 	if (cf->can_id & CAN_RTR_FLAG)
899 		txdlc |= IFI_CANFD_TXFIFO_DLC_RTR;
900 
901 	/* message ram configuration */
902 	writel(txid, priv->base + IFI_CANFD_TXFIFO_ID);
903 	writel(txdlc, priv->base + IFI_CANFD_TXFIFO_DLC);
904 
905 	for (i = 0; i < cf->len; i += 4) {
906 		writel(*(u32 *)(cf->data + i),
907 		       priv->base + IFI_CANFD_TXFIFO_DATA + i);
908 	}
909 
910 	writel(0, priv->base + IFI_CANFD_TXFIFO_REPEATCOUNT);
911 	writel(0, priv->base + IFI_CANFD_TXFIFO_SUSPEND_US);
912 
913 	can_put_echo_skb(skb, ndev, 0, 0);
914 
915 	/* Start the transmission */
916 	writel(IFI_CANFD_TXSTCMD_ADD_MSG, priv->base + IFI_CANFD_TXSTCMD);
917 
918 	return NETDEV_TX_OK;
919 }
920 
921 static const struct net_device_ops ifi_canfd_netdev_ops = {
922 	.ndo_open	= ifi_canfd_open,
923 	.ndo_stop	= ifi_canfd_close,
924 	.ndo_start_xmit	= ifi_canfd_start_xmit,
925 	.ndo_change_mtu	= can_change_mtu,
926 };
927 
928 static const struct ethtool_ops ifi_canfd_ethtool_ops = {
929 	.get_ts_info = ethtool_op_get_ts_info,
930 };
931 
ifi_canfd_plat_probe(struct platform_device * pdev)932 static int ifi_canfd_plat_probe(struct platform_device *pdev)
933 {
934 	struct device *dev = &pdev->dev;
935 	struct net_device *ndev;
936 	struct ifi_canfd_priv *priv;
937 	void __iomem *addr;
938 	int irq, ret;
939 	u32 id, rev;
940 
941 	addr = devm_platform_ioremap_resource(pdev, 0);
942 	if (IS_ERR(addr))
943 		return PTR_ERR(addr);
944 
945 	irq = platform_get_irq(pdev, 0);
946 	if (irq < 0)
947 		return -EINVAL;
948 
949 	id = readl(addr + IFI_CANFD_IP_ID);
950 	if (id != IFI_CANFD_IP_ID_VALUE) {
951 		dev_err(dev, "This block is not IFI CANFD, id=%08x\n", id);
952 		return -EINVAL;
953 	}
954 
955 	rev = readl(addr + IFI_CANFD_VER) & IFI_CANFD_VER_REV_MASK;
956 	if (rev < IFI_CANFD_VER_REV_MIN_SUPPORTED) {
957 		dev_err(dev, "This block is too old (rev %i), minimum supported is rev %i\n",
958 			rev, IFI_CANFD_VER_REV_MIN_SUPPORTED);
959 		return -EINVAL;
960 	}
961 
962 	ndev = alloc_candev(sizeof(*priv), 1);
963 	if (!ndev)
964 		return -ENOMEM;
965 
966 	ndev->irq = irq;
967 	ndev->flags |= IFF_ECHO;	/* we support local echo */
968 	ndev->netdev_ops = &ifi_canfd_netdev_ops;
969 	ndev->ethtool_ops = &ifi_canfd_ethtool_ops;
970 
971 	priv = netdev_priv(ndev);
972 	priv->ndev = ndev;
973 	priv->base = addr;
974 
975 	netif_napi_add(ndev, &priv->napi, ifi_canfd_poll);
976 
977 	priv->can.state = CAN_STATE_STOPPED;
978 
979 	priv->can.clock.freq = readl(addr + IFI_CANFD_CANCLOCK);
980 
981 	priv->can.bittiming_const	= &ifi_canfd_bittiming_const;
982 	priv->can.data_bittiming_const	= &ifi_canfd_bittiming_const;
983 	priv->can.do_set_mode		= ifi_canfd_set_mode;
984 	priv->can.do_get_berr_counter	= ifi_canfd_get_berr_counter;
985 
986 	/* IFI CANFD can do both Bosch FD and ISO FD */
987 	priv->can.ctrlmode = CAN_CTRLMODE_FD;
988 
989 	/* IFI CANFD can do both Bosch FD and ISO FD */
990 	priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
991 				       CAN_CTRLMODE_LISTENONLY |
992 				       CAN_CTRLMODE_FD |
993 				       CAN_CTRLMODE_FD_NON_ISO |
994 				       CAN_CTRLMODE_BERR_REPORTING;
995 
996 	platform_set_drvdata(pdev, ndev);
997 	SET_NETDEV_DEV(ndev, dev);
998 
999 	ret = register_candev(ndev);
1000 	if (ret) {
1001 		dev_err(dev, "Failed to register (ret=%d)\n", ret);
1002 		goto err_reg;
1003 	}
1004 
1005 	dev_info(dev, "Driver registered: regs=%p, irq=%d, clock=%d\n",
1006 		 priv->base, ndev->irq, priv->can.clock.freq);
1007 
1008 	return 0;
1009 
1010 err_reg:
1011 	free_candev(ndev);
1012 	return ret;
1013 }
1014 
ifi_canfd_plat_remove(struct platform_device * pdev)1015 static void ifi_canfd_plat_remove(struct platform_device *pdev)
1016 {
1017 	struct net_device *ndev = platform_get_drvdata(pdev);
1018 
1019 	unregister_candev(ndev);
1020 	platform_set_drvdata(pdev, NULL);
1021 	free_candev(ndev);
1022 }
1023 
1024 static const struct of_device_id ifi_canfd_of_table[] = {
1025 	{ .compatible = "ifi,canfd-1.0", .data = NULL },
1026 	{ /* sentinel */ },
1027 };
1028 MODULE_DEVICE_TABLE(of, ifi_canfd_of_table);
1029 
1030 static struct platform_driver ifi_canfd_plat_driver = {
1031 	.driver = {
1032 		.name		= KBUILD_MODNAME,
1033 		.of_match_table	= ifi_canfd_of_table,
1034 	},
1035 	.probe	= ifi_canfd_plat_probe,
1036 	.remove_new = ifi_canfd_plat_remove,
1037 };
1038 
1039 module_platform_driver(ifi_canfd_plat_driver);
1040 
1041 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
1042 MODULE_LICENSE("GPL v2");
1043 MODULE_DESCRIPTION("CAN bus driver for IFI CANFD controller");
1044