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