xref: /linux/drivers/net/ethernet/renesas/ravb_main.c (revision 0a149ab78ee220c75eef797abea7a29f4490e226)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Renesas Ethernet AVB device driver
3  *
4  * Copyright (C) 2014-2019 Renesas Electronics Corporation
5  * Copyright (C) 2015 Renesas Solutions Corp.
6  * Copyright (C) 2015-2016 Cogent Embedded, Inc. <source@cogentembedded.com>
7  *
8  * Based on the SuperH Ethernet driver
9  */
10 
11 #include <linux/cache.h>
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/err.h>
16 #include <linux/etherdevice.h>
17 #include <linux/ethtool.h>
18 #include <linux/if_vlan.h>
19 #include <linux/kernel.h>
20 #include <linux/list.h>
21 #include <linux/module.h>
22 #include <linux/net_tstamp.h>
23 #include <linux/of.h>
24 #include <linux/of_mdio.h>
25 #include <linux/of_net.h>
26 #include <linux/platform_device.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/slab.h>
29 #include <linux/spinlock.h>
30 #include <linux/reset.h>
31 #include <linux/math64.h>
32 
33 #include "ravb.h"
34 
35 #define RAVB_DEF_MSG_ENABLE \
36 		(NETIF_MSG_LINK	  | \
37 		 NETIF_MSG_TIMER  | \
38 		 NETIF_MSG_RX_ERR | \
39 		 NETIF_MSG_TX_ERR)
40 
41 static const char *ravb_rx_irqs[NUM_RX_QUEUE] = {
42 	"ch0", /* RAVB_BE */
43 	"ch1", /* RAVB_NC */
44 };
45 
46 static const char *ravb_tx_irqs[NUM_TX_QUEUE] = {
47 	"ch18", /* RAVB_BE */
48 	"ch19", /* RAVB_NC */
49 };
50 
51 void ravb_modify(struct net_device *ndev, enum ravb_reg reg, u32 clear,
52 		 u32 set)
53 {
54 	ravb_write(ndev, (ravb_read(ndev, reg) & ~clear) | set, reg);
55 }
56 
57 int ravb_wait(struct net_device *ndev, enum ravb_reg reg, u32 mask, u32 value)
58 {
59 	int i;
60 
61 	for (i = 0; i < 10000; i++) {
62 		if ((ravb_read(ndev, reg) & mask) == value)
63 			return 0;
64 		udelay(10);
65 	}
66 	return -ETIMEDOUT;
67 }
68 
69 static int ravb_config(struct net_device *ndev)
70 {
71 	int error;
72 
73 	/* Set config mode */
74 	ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG);
75 	/* Check if the operating mode is changed to the config mode */
76 	error = ravb_wait(ndev, CSR, CSR_OPS, CSR_OPS_CONFIG);
77 	if (error)
78 		netdev_err(ndev, "failed to switch device to config mode\n");
79 
80 	return error;
81 }
82 
83 static void ravb_set_rate_gbeth(struct net_device *ndev)
84 {
85 	struct ravb_private *priv = netdev_priv(ndev);
86 
87 	switch (priv->speed) {
88 	case 10:                /* 10BASE */
89 		ravb_write(ndev, GBETH_GECMR_SPEED_10, GECMR);
90 		break;
91 	case 100:               /* 100BASE */
92 		ravb_write(ndev, GBETH_GECMR_SPEED_100, GECMR);
93 		break;
94 	case 1000:              /* 1000BASE */
95 		ravb_write(ndev, GBETH_GECMR_SPEED_1000, GECMR);
96 		break;
97 	}
98 }
99 
100 static void ravb_set_rate_rcar(struct net_device *ndev)
101 {
102 	struct ravb_private *priv = netdev_priv(ndev);
103 
104 	switch (priv->speed) {
105 	case 100:		/* 100BASE */
106 		ravb_write(ndev, GECMR_SPEED_100, GECMR);
107 		break;
108 	case 1000:		/* 1000BASE */
109 		ravb_write(ndev, GECMR_SPEED_1000, GECMR);
110 		break;
111 	}
112 }
113 
114 static void ravb_set_buffer_align(struct sk_buff *skb)
115 {
116 	u32 reserve = (unsigned long)skb->data & (RAVB_ALIGN - 1);
117 
118 	if (reserve)
119 		skb_reserve(skb, RAVB_ALIGN - reserve);
120 }
121 
122 /* Get MAC address from the MAC address registers
123  *
124  * Ethernet AVB device doesn't have ROM for MAC address.
125  * This function gets the MAC address that was used by a bootloader.
126  */
127 static void ravb_read_mac_address(struct device_node *np,
128 				  struct net_device *ndev)
129 {
130 	int ret;
131 
132 	ret = of_get_ethdev_address(np, ndev);
133 	if (ret) {
134 		u32 mahr = ravb_read(ndev, MAHR);
135 		u32 malr = ravb_read(ndev, MALR);
136 		u8 addr[ETH_ALEN];
137 
138 		addr[0] = (mahr >> 24) & 0xFF;
139 		addr[1] = (mahr >> 16) & 0xFF;
140 		addr[2] = (mahr >>  8) & 0xFF;
141 		addr[3] = (mahr >>  0) & 0xFF;
142 		addr[4] = (malr >>  8) & 0xFF;
143 		addr[5] = (malr >>  0) & 0xFF;
144 		eth_hw_addr_set(ndev, addr);
145 	}
146 }
147 
148 static void ravb_mdio_ctrl(struct mdiobb_ctrl *ctrl, u32 mask, int set)
149 {
150 	struct ravb_private *priv = container_of(ctrl, struct ravb_private,
151 						 mdiobb);
152 
153 	ravb_modify(priv->ndev, PIR, mask, set ? mask : 0);
154 }
155 
156 /* MDC pin control */
157 static void ravb_set_mdc(struct mdiobb_ctrl *ctrl, int level)
158 {
159 	ravb_mdio_ctrl(ctrl, PIR_MDC, level);
160 }
161 
162 /* Data I/O pin control */
163 static void ravb_set_mdio_dir(struct mdiobb_ctrl *ctrl, int output)
164 {
165 	ravb_mdio_ctrl(ctrl, PIR_MMD, output);
166 }
167 
168 /* Set data bit */
169 static void ravb_set_mdio_data(struct mdiobb_ctrl *ctrl, int value)
170 {
171 	ravb_mdio_ctrl(ctrl, PIR_MDO, value);
172 }
173 
174 /* Get data bit */
175 static int ravb_get_mdio_data(struct mdiobb_ctrl *ctrl)
176 {
177 	struct ravb_private *priv = container_of(ctrl, struct ravb_private,
178 						 mdiobb);
179 
180 	return (ravb_read(priv->ndev, PIR) & PIR_MDI) != 0;
181 }
182 
183 /* MDIO bus control struct */
184 static const struct mdiobb_ops bb_ops = {
185 	.owner = THIS_MODULE,
186 	.set_mdc = ravb_set_mdc,
187 	.set_mdio_dir = ravb_set_mdio_dir,
188 	.set_mdio_data = ravb_set_mdio_data,
189 	.get_mdio_data = ravb_get_mdio_data,
190 };
191 
192 /* Free TX skb function for AVB-IP */
193 static int ravb_tx_free(struct net_device *ndev, int q, bool free_txed_only)
194 {
195 	struct ravb_private *priv = netdev_priv(ndev);
196 	struct net_device_stats *stats = &priv->stats[q];
197 	unsigned int num_tx_desc = priv->num_tx_desc;
198 	struct ravb_tx_desc *desc;
199 	unsigned int entry;
200 	int free_num = 0;
201 	u32 size;
202 
203 	for (; priv->cur_tx[q] - priv->dirty_tx[q] > 0; priv->dirty_tx[q]++) {
204 		bool txed;
205 
206 		entry = priv->dirty_tx[q] % (priv->num_tx_ring[q] *
207 					     num_tx_desc);
208 		desc = &priv->tx_ring[q][entry];
209 		txed = desc->die_dt == DT_FEMPTY;
210 		if (free_txed_only && !txed)
211 			break;
212 		/* Descriptor type must be checked before all other reads */
213 		dma_rmb();
214 		size = le16_to_cpu(desc->ds_tagl) & TX_DS;
215 		/* Free the original skb. */
216 		if (priv->tx_skb[q][entry / num_tx_desc]) {
217 			dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
218 					 size, DMA_TO_DEVICE);
219 			/* Last packet descriptor? */
220 			if (entry % num_tx_desc == num_tx_desc - 1) {
221 				entry /= num_tx_desc;
222 				dev_kfree_skb_any(priv->tx_skb[q][entry]);
223 				priv->tx_skb[q][entry] = NULL;
224 				if (txed)
225 					stats->tx_packets++;
226 			}
227 			free_num++;
228 		}
229 		if (txed)
230 			stats->tx_bytes += size;
231 		desc->die_dt = DT_EEMPTY;
232 	}
233 	return free_num;
234 }
235 
236 static void ravb_rx_ring_free_gbeth(struct net_device *ndev, int q)
237 {
238 	struct ravb_private *priv = netdev_priv(ndev);
239 	unsigned int ring_size;
240 	unsigned int i;
241 
242 	if (!priv->gbeth_rx_ring)
243 		return;
244 
245 	for (i = 0; i < priv->num_rx_ring[q]; i++) {
246 		struct ravb_rx_desc *desc = &priv->gbeth_rx_ring[i];
247 
248 		if (!dma_mapping_error(ndev->dev.parent,
249 				       le32_to_cpu(desc->dptr)))
250 			dma_unmap_single(ndev->dev.parent,
251 					 le32_to_cpu(desc->dptr),
252 					 GBETH_RX_BUFF_MAX,
253 					 DMA_FROM_DEVICE);
254 	}
255 	ring_size = sizeof(struct ravb_rx_desc) * (priv->num_rx_ring[q] + 1);
256 	dma_free_coherent(ndev->dev.parent, ring_size, priv->gbeth_rx_ring,
257 			  priv->rx_desc_dma[q]);
258 	priv->gbeth_rx_ring = NULL;
259 }
260 
261 static void ravb_rx_ring_free_rcar(struct net_device *ndev, int q)
262 {
263 	struct ravb_private *priv = netdev_priv(ndev);
264 	unsigned int ring_size;
265 	unsigned int i;
266 
267 	if (!priv->rx_ring[q])
268 		return;
269 
270 	for (i = 0; i < priv->num_rx_ring[q]; i++) {
271 		struct ravb_ex_rx_desc *desc = &priv->rx_ring[q][i];
272 
273 		if (!dma_mapping_error(ndev->dev.parent,
274 				       le32_to_cpu(desc->dptr)))
275 			dma_unmap_single(ndev->dev.parent,
276 					 le32_to_cpu(desc->dptr),
277 					 RX_BUF_SZ,
278 					 DMA_FROM_DEVICE);
279 	}
280 	ring_size = sizeof(struct ravb_ex_rx_desc) *
281 		    (priv->num_rx_ring[q] + 1);
282 	dma_free_coherent(ndev->dev.parent, ring_size, priv->rx_ring[q],
283 			  priv->rx_desc_dma[q]);
284 	priv->rx_ring[q] = NULL;
285 }
286 
287 /* Free skb's and DMA buffers for Ethernet AVB */
288 static void ravb_ring_free(struct net_device *ndev, int q)
289 {
290 	struct ravb_private *priv = netdev_priv(ndev);
291 	const struct ravb_hw_info *info = priv->info;
292 	unsigned int num_tx_desc = priv->num_tx_desc;
293 	unsigned int ring_size;
294 	unsigned int i;
295 
296 	info->rx_ring_free(ndev, q);
297 
298 	if (priv->tx_ring[q]) {
299 		ravb_tx_free(ndev, q, false);
300 
301 		ring_size = sizeof(struct ravb_tx_desc) *
302 			    (priv->num_tx_ring[q] * num_tx_desc + 1);
303 		dma_free_coherent(ndev->dev.parent, ring_size, priv->tx_ring[q],
304 				  priv->tx_desc_dma[q]);
305 		priv->tx_ring[q] = NULL;
306 	}
307 
308 	/* Free RX skb ringbuffer */
309 	if (priv->rx_skb[q]) {
310 		for (i = 0; i < priv->num_rx_ring[q]; i++)
311 			dev_kfree_skb(priv->rx_skb[q][i]);
312 	}
313 	kfree(priv->rx_skb[q]);
314 	priv->rx_skb[q] = NULL;
315 
316 	/* Free aligned TX buffers */
317 	kfree(priv->tx_align[q]);
318 	priv->tx_align[q] = NULL;
319 
320 	/* Free TX skb ringbuffer.
321 	 * SKBs are freed by ravb_tx_free() call above.
322 	 */
323 	kfree(priv->tx_skb[q]);
324 	priv->tx_skb[q] = NULL;
325 }
326 
327 static void ravb_rx_ring_format_gbeth(struct net_device *ndev, int q)
328 {
329 	struct ravb_private *priv = netdev_priv(ndev);
330 	struct ravb_rx_desc *rx_desc;
331 	unsigned int rx_ring_size;
332 	dma_addr_t dma_addr;
333 	unsigned int i;
334 
335 	rx_ring_size = sizeof(*rx_desc) * priv->num_rx_ring[q];
336 	memset(priv->gbeth_rx_ring, 0, rx_ring_size);
337 	/* Build RX ring buffer */
338 	for (i = 0; i < priv->num_rx_ring[q]; i++) {
339 		/* RX descriptor */
340 		rx_desc = &priv->gbeth_rx_ring[i];
341 		rx_desc->ds_cc = cpu_to_le16(GBETH_RX_DESC_DATA_SIZE);
342 		dma_addr = dma_map_single(ndev->dev.parent, priv->rx_skb[q][i]->data,
343 					  GBETH_RX_BUFF_MAX,
344 					  DMA_FROM_DEVICE);
345 		/* We just set the data size to 0 for a failed mapping which
346 		 * should prevent DMA from happening...
347 		 */
348 		if (dma_mapping_error(ndev->dev.parent, dma_addr))
349 			rx_desc->ds_cc = cpu_to_le16(0);
350 		rx_desc->dptr = cpu_to_le32(dma_addr);
351 		rx_desc->die_dt = DT_FEMPTY;
352 	}
353 	rx_desc = &priv->gbeth_rx_ring[i];
354 	rx_desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]);
355 	rx_desc->die_dt = DT_LINKFIX; /* type */
356 }
357 
358 static void ravb_rx_ring_format_rcar(struct net_device *ndev, int q)
359 {
360 	struct ravb_private *priv = netdev_priv(ndev);
361 	struct ravb_ex_rx_desc *rx_desc;
362 	unsigned int rx_ring_size = sizeof(*rx_desc) * priv->num_rx_ring[q];
363 	dma_addr_t dma_addr;
364 	unsigned int i;
365 
366 	memset(priv->rx_ring[q], 0, rx_ring_size);
367 	/* Build RX ring buffer */
368 	for (i = 0; i < priv->num_rx_ring[q]; i++) {
369 		/* RX descriptor */
370 		rx_desc = &priv->rx_ring[q][i];
371 		rx_desc->ds_cc = cpu_to_le16(RX_BUF_SZ);
372 		dma_addr = dma_map_single(ndev->dev.parent, priv->rx_skb[q][i]->data,
373 					  RX_BUF_SZ,
374 					  DMA_FROM_DEVICE);
375 		/* We just set the data size to 0 for a failed mapping which
376 		 * should prevent DMA from happening...
377 		 */
378 		if (dma_mapping_error(ndev->dev.parent, dma_addr))
379 			rx_desc->ds_cc = cpu_to_le16(0);
380 		rx_desc->dptr = cpu_to_le32(dma_addr);
381 		rx_desc->die_dt = DT_FEMPTY;
382 	}
383 	rx_desc = &priv->rx_ring[q][i];
384 	rx_desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]);
385 	rx_desc->die_dt = DT_LINKFIX; /* type */
386 }
387 
388 /* Format skb and descriptor buffer for Ethernet AVB */
389 static void ravb_ring_format(struct net_device *ndev, int q)
390 {
391 	struct ravb_private *priv = netdev_priv(ndev);
392 	const struct ravb_hw_info *info = priv->info;
393 	unsigned int num_tx_desc = priv->num_tx_desc;
394 	struct ravb_tx_desc *tx_desc;
395 	struct ravb_desc *desc;
396 	unsigned int tx_ring_size = sizeof(*tx_desc) * priv->num_tx_ring[q] *
397 				    num_tx_desc;
398 	unsigned int i;
399 
400 	priv->cur_rx[q] = 0;
401 	priv->cur_tx[q] = 0;
402 	priv->dirty_rx[q] = 0;
403 	priv->dirty_tx[q] = 0;
404 
405 	info->rx_ring_format(ndev, q);
406 
407 	memset(priv->tx_ring[q], 0, tx_ring_size);
408 	/* Build TX ring buffer */
409 	for (i = 0, tx_desc = priv->tx_ring[q]; i < priv->num_tx_ring[q];
410 	     i++, tx_desc++) {
411 		tx_desc->die_dt = DT_EEMPTY;
412 		if (num_tx_desc > 1) {
413 			tx_desc++;
414 			tx_desc->die_dt = DT_EEMPTY;
415 		}
416 	}
417 	tx_desc->dptr = cpu_to_le32((u32)priv->tx_desc_dma[q]);
418 	tx_desc->die_dt = DT_LINKFIX; /* type */
419 
420 	/* RX descriptor base address for best effort */
421 	desc = &priv->desc_bat[RX_QUEUE_OFFSET + q];
422 	desc->die_dt = DT_LINKFIX; /* type */
423 	desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]);
424 
425 	/* TX descriptor base address for best effort */
426 	desc = &priv->desc_bat[q];
427 	desc->die_dt = DT_LINKFIX; /* type */
428 	desc->dptr = cpu_to_le32((u32)priv->tx_desc_dma[q]);
429 }
430 
431 static void *ravb_alloc_rx_desc_gbeth(struct net_device *ndev, int q)
432 {
433 	struct ravb_private *priv = netdev_priv(ndev);
434 	unsigned int ring_size;
435 
436 	ring_size = sizeof(struct ravb_rx_desc) * (priv->num_rx_ring[q] + 1);
437 
438 	priv->gbeth_rx_ring = dma_alloc_coherent(ndev->dev.parent, ring_size,
439 						 &priv->rx_desc_dma[q],
440 						 GFP_KERNEL);
441 	return priv->gbeth_rx_ring;
442 }
443 
444 static void *ravb_alloc_rx_desc_rcar(struct net_device *ndev, int q)
445 {
446 	struct ravb_private *priv = netdev_priv(ndev);
447 	unsigned int ring_size;
448 
449 	ring_size = sizeof(struct ravb_ex_rx_desc) * (priv->num_rx_ring[q] + 1);
450 
451 	priv->rx_ring[q] = dma_alloc_coherent(ndev->dev.parent, ring_size,
452 					      &priv->rx_desc_dma[q],
453 					      GFP_KERNEL);
454 	return priv->rx_ring[q];
455 }
456 
457 /* Init skb and descriptor buffer for Ethernet AVB */
458 static int ravb_ring_init(struct net_device *ndev, int q)
459 {
460 	struct ravb_private *priv = netdev_priv(ndev);
461 	const struct ravb_hw_info *info = priv->info;
462 	unsigned int num_tx_desc = priv->num_tx_desc;
463 	unsigned int ring_size;
464 	struct sk_buff *skb;
465 	unsigned int i;
466 
467 	/* Allocate RX and TX skb rings */
468 	priv->rx_skb[q] = kcalloc(priv->num_rx_ring[q],
469 				  sizeof(*priv->rx_skb[q]), GFP_KERNEL);
470 	priv->tx_skb[q] = kcalloc(priv->num_tx_ring[q],
471 				  sizeof(*priv->tx_skb[q]), GFP_KERNEL);
472 	if (!priv->rx_skb[q] || !priv->tx_skb[q])
473 		goto error;
474 
475 	for (i = 0; i < priv->num_rx_ring[q]; i++) {
476 		skb = __netdev_alloc_skb(ndev, info->max_rx_len, GFP_KERNEL);
477 		if (!skb)
478 			goto error;
479 		ravb_set_buffer_align(skb);
480 		priv->rx_skb[q][i] = skb;
481 	}
482 
483 	if (num_tx_desc > 1) {
484 		/* Allocate rings for the aligned buffers */
485 		priv->tx_align[q] = kmalloc(DPTR_ALIGN * priv->num_tx_ring[q] +
486 					    DPTR_ALIGN - 1, GFP_KERNEL);
487 		if (!priv->tx_align[q])
488 			goto error;
489 	}
490 
491 	/* Allocate all RX descriptors. */
492 	if (!info->alloc_rx_desc(ndev, q))
493 		goto error;
494 
495 	priv->dirty_rx[q] = 0;
496 
497 	/* Allocate all TX descriptors. */
498 	ring_size = sizeof(struct ravb_tx_desc) *
499 		    (priv->num_tx_ring[q] * num_tx_desc + 1);
500 	priv->tx_ring[q] = dma_alloc_coherent(ndev->dev.parent, ring_size,
501 					      &priv->tx_desc_dma[q],
502 					      GFP_KERNEL);
503 	if (!priv->tx_ring[q])
504 		goto error;
505 
506 	return 0;
507 
508 error:
509 	ravb_ring_free(ndev, q);
510 
511 	return -ENOMEM;
512 }
513 
514 static void ravb_emac_init_gbeth(struct net_device *ndev)
515 {
516 	struct ravb_private *priv = netdev_priv(ndev);
517 
518 	if (priv->phy_interface == PHY_INTERFACE_MODE_MII) {
519 		ravb_write(ndev, (1000 << 16) | CXR35_SEL_XMII_MII, CXR35);
520 		ravb_modify(ndev, CXR31, CXR31_SEL_LINK0 | CXR31_SEL_LINK1, 0);
521 	} else {
522 		ravb_write(ndev, (1000 << 16) | CXR35_SEL_XMII_RGMII, CXR35);
523 		ravb_modify(ndev, CXR31, CXR31_SEL_LINK0 | CXR31_SEL_LINK1,
524 			    CXR31_SEL_LINK0);
525 	}
526 
527 	/* Receive frame limit set register */
528 	ravb_write(ndev, GBETH_RX_BUFF_MAX + ETH_FCS_LEN, RFLR);
529 
530 	/* EMAC Mode: PAUSE prohibition; Duplex; TX; RX; CRC Pass Through */
531 	ravb_write(ndev, ECMR_ZPF | ((priv->duplex > 0) ? ECMR_DM : 0) |
532 			 ECMR_TE | ECMR_RE | ECMR_RCPT |
533 			 ECMR_TXF | ECMR_RXF, ECMR);
534 
535 	ravb_set_rate_gbeth(ndev);
536 
537 	/* Set MAC address */
538 	ravb_write(ndev,
539 		   (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
540 		   (ndev->dev_addr[2] << 8)  | (ndev->dev_addr[3]), MAHR);
541 	ravb_write(ndev, (ndev->dev_addr[4] << 8)  | (ndev->dev_addr[5]), MALR);
542 
543 	/* E-MAC status register clear */
544 	ravb_write(ndev, ECSR_ICD | ECSR_LCHNG | ECSR_PFRI, ECSR);
545 	ravb_write(ndev, CSR0_TPE | CSR0_RPE, CSR0);
546 
547 	/* E-MAC interrupt enable register */
548 	ravb_write(ndev, ECSIPR_ICDIP, ECSIPR);
549 }
550 
551 static void ravb_emac_init_rcar(struct net_device *ndev)
552 {
553 	/* Receive frame limit set register */
554 	ravb_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN, RFLR);
555 
556 	/* EMAC Mode: PAUSE prohibition; Duplex; RX Checksum; TX; RX */
557 	ravb_write(ndev, ECMR_ZPF | ECMR_DM |
558 		   (ndev->features & NETIF_F_RXCSUM ? ECMR_RCSC : 0) |
559 		   ECMR_TE | ECMR_RE, ECMR);
560 
561 	ravb_set_rate_rcar(ndev);
562 
563 	/* Set MAC address */
564 	ravb_write(ndev,
565 		   (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
566 		   (ndev->dev_addr[2] << 8)  | (ndev->dev_addr[3]), MAHR);
567 	ravb_write(ndev,
568 		   (ndev->dev_addr[4] << 8)  | (ndev->dev_addr[5]), MALR);
569 
570 	/* E-MAC status register clear */
571 	ravb_write(ndev, ECSR_ICD | ECSR_MPD, ECSR);
572 
573 	/* E-MAC interrupt enable register */
574 	ravb_write(ndev, ECSIPR_ICDIP | ECSIPR_MPDIP | ECSIPR_LCHNGIP, ECSIPR);
575 }
576 
577 /* E-MAC init function */
578 static void ravb_emac_init(struct net_device *ndev)
579 {
580 	struct ravb_private *priv = netdev_priv(ndev);
581 	const struct ravb_hw_info *info = priv->info;
582 
583 	info->emac_init(ndev);
584 }
585 
586 static int ravb_dmac_init_gbeth(struct net_device *ndev)
587 {
588 	int error;
589 
590 	error = ravb_ring_init(ndev, RAVB_BE);
591 	if (error)
592 		return error;
593 
594 	/* Descriptor format */
595 	ravb_ring_format(ndev, RAVB_BE);
596 
597 	/* Set DMAC RX */
598 	ravb_write(ndev, 0x60000000, RCR);
599 
600 	/* Set Max Frame Length (RTC) */
601 	ravb_write(ndev, 0x7ffc0000 | GBETH_RX_BUFF_MAX, RTC);
602 
603 	/* Set FIFO size */
604 	ravb_write(ndev, 0x00222200, TGC);
605 
606 	ravb_write(ndev, 0, TCCR);
607 
608 	/* Frame receive */
609 	ravb_write(ndev, RIC0_FRE0, RIC0);
610 	/* Disable FIFO full warning */
611 	ravb_write(ndev, 0x0, RIC1);
612 	/* Receive FIFO full error, descriptor empty */
613 	ravb_write(ndev, RIC2_QFE0 | RIC2_RFFE, RIC2);
614 
615 	ravb_write(ndev, TIC_FTE0, TIC);
616 
617 	return 0;
618 }
619 
620 static int ravb_dmac_init_rcar(struct net_device *ndev)
621 {
622 	struct ravb_private *priv = netdev_priv(ndev);
623 	const struct ravb_hw_info *info = priv->info;
624 	int error;
625 
626 	error = ravb_ring_init(ndev, RAVB_BE);
627 	if (error)
628 		return error;
629 	error = ravb_ring_init(ndev, RAVB_NC);
630 	if (error) {
631 		ravb_ring_free(ndev, RAVB_BE);
632 		return error;
633 	}
634 
635 	/* Descriptor format */
636 	ravb_ring_format(ndev, RAVB_BE);
637 	ravb_ring_format(ndev, RAVB_NC);
638 
639 	/* Set AVB RX */
640 	ravb_write(ndev,
641 		   RCR_EFFS | RCR_ENCF | RCR_ETS0 | RCR_ESF | 0x18000000, RCR);
642 
643 	/* Set FIFO size */
644 	ravb_write(ndev, TGC_TQP_AVBMODE1 | 0x00112200, TGC);
645 
646 	/* Timestamp enable */
647 	ravb_write(ndev, TCCR_TFEN, TCCR);
648 
649 	/* Interrupt init: */
650 	if (info->multi_irqs) {
651 		/* Clear DIL.DPLx */
652 		ravb_write(ndev, 0, DIL);
653 		/* Set queue specific interrupt */
654 		ravb_write(ndev, CIE_CRIE | CIE_CTIE | CIE_CL0M, CIE);
655 	}
656 	/* Frame receive */
657 	ravb_write(ndev, RIC0_FRE0 | RIC0_FRE1, RIC0);
658 	/* Disable FIFO full warning */
659 	ravb_write(ndev, 0, RIC1);
660 	/* Receive FIFO full error, descriptor empty */
661 	ravb_write(ndev, RIC2_QFE0 | RIC2_QFE1 | RIC2_RFFE, RIC2);
662 	/* Frame transmitted, timestamp FIFO updated */
663 	ravb_write(ndev, TIC_FTE0 | TIC_FTE1 | TIC_TFUE, TIC);
664 
665 	return 0;
666 }
667 
668 /* Device init function for Ethernet AVB */
669 static int ravb_dmac_init(struct net_device *ndev)
670 {
671 	struct ravb_private *priv = netdev_priv(ndev);
672 	const struct ravb_hw_info *info = priv->info;
673 	int error;
674 
675 	/* Set CONFIG mode */
676 	error = ravb_config(ndev);
677 	if (error)
678 		return error;
679 
680 	error = info->dmac_init(ndev);
681 	if (error)
682 		return error;
683 
684 	/* Setting the control will start the AVB-DMAC process. */
685 	ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_OPERATION);
686 
687 	return 0;
688 }
689 
690 static void ravb_get_tx_tstamp(struct net_device *ndev)
691 {
692 	struct ravb_private *priv = netdev_priv(ndev);
693 	struct ravb_tstamp_skb *ts_skb, *ts_skb2;
694 	struct skb_shared_hwtstamps shhwtstamps;
695 	struct sk_buff *skb;
696 	struct timespec64 ts;
697 	u16 tag, tfa_tag;
698 	int count;
699 	u32 tfa2;
700 
701 	count = (ravb_read(ndev, TSR) & TSR_TFFL) >> 8;
702 	while (count--) {
703 		tfa2 = ravb_read(ndev, TFA2);
704 		tfa_tag = (tfa2 & TFA2_TST) >> 16;
705 		ts.tv_nsec = (u64)ravb_read(ndev, TFA0);
706 		ts.tv_sec = ((u64)(tfa2 & TFA2_TSV) << 32) |
707 			    ravb_read(ndev, TFA1);
708 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
709 		shhwtstamps.hwtstamp = timespec64_to_ktime(ts);
710 		list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list,
711 					 list) {
712 			skb = ts_skb->skb;
713 			tag = ts_skb->tag;
714 			list_del(&ts_skb->list);
715 			kfree(ts_skb);
716 			if (tag == tfa_tag) {
717 				skb_tstamp_tx(skb, &shhwtstamps);
718 				dev_consume_skb_any(skb);
719 				break;
720 			} else {
721 				dev_kfree_skb_any(skb);
722 			}
723 		}
724 		ravb_modify(ndev, TCCR, TCCR_TFR, TCCR_TFR);
725 	}
726 }
727 
728 static void ravb_rx_csum(struct sk_buff *skb)
729 {
730 	u8 *hw_csum;
731 
732 	/* The hardware checksum is contained in sizeof(__sum16) (2) bytes
733 	 * appended to packet data
734 	 */
735 	if (unlikely(skb->len < sizeof(__sum16)))
736 		return;
737 	hw_csum = skb_tail_pointer(skb) - sizeof(__sum16);
738 	skb->csum = csum_unfold((__force __sum16)get_unaligned_le16(hw_csum));
739 	skb->ip_summed = CHECKSUM_COMPLETE;
740 	skb_trim(skb, skb->len - sizeof(__sum16));
741 }
742 
743 static struct sk_buff *ravb_get_skb_gbeth(struct net_device *ndev, int entry,
744 					  struct ravb_rx_desc *desc)
745 {
746 	struct ravb_private *priv = netdev_priv(ndev);
747 	struct sk_buff *skb;
748 
749 	skb = priv->rx_skb[RAVB_BE][entry];
750 	priv->rx_skb[RAVB_BE][entry] = NULL;
751 	dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
752 			 ALIGN(GBETH_RX_BUFF_MAX, 16), DMA_FROM_DEVICE);
753 
754 	return skb;
755 }
756 
757 /* Packet receive function for Gigabit Ethernet */
758 static bool ravb_rx_gbeth(struct net_device *ndev, int *quota, int q)
759 {
760 	struct ravb_private *priv = netdev_priv(ndev);
761 	const struct ravb_hw_info *info = priv->info;
762 	struct net_device_stats *stats;
763 	struct ravb_rx_desc *desc;
764 	struct sk_buff *skb;
765 	dma_addr_t dma_addr;
766 	u8  desc_status;
767 	int boguscnt;
768 	u16 pkt_len;
769 	u8  die_dt;
770 	int entry;
771 	int limit;
772 
773 	entry = priv->cur_rx[q] % priv->num_rx_ring[q];
774 	boguscnt = priv->dirty_rx[q] + priv->num_rx_ring[q] - priv->cur_rx[q];
775 	stats = &priv->stats[q];
776 
777 	boguscnt = min(boguscnt, *quota);
778 	limit = boguscnt;
779 	desc = &priv->gbeth_rx_ring[entry];
780 	while (desc->die_dt != DT_FEMPTY) {
781 		/* Descriptor type must be checked before all other reads */
782 		dma_rmb();
783 		desc_status = desc->msc;
784 		pkt_len = le16_to_cpu(desc->ds_cc) & RX_DS;
785 
786 		if (--boguscnt < 0)
787 			break;
788 
789 		/* We use 0-byte descriptors to mark the DMA mapping errors */
790 		if (!pkt_len)
791 			continue;
792 
793 		if (desc_status & MSC_MC)
794 			stats->multicast++;
795 
796 		if (desc_status & (MSC_CRC | MSC_RFE | MSC_RTSF | MSC_RTLF | MSC_CEEF)) {
797 			stats->rx_errors++;
798 			if (desc_status & MSC_CRC)
799 				stats->rx_crc_errors++;
800 			if (desc_status & MSC_RFE)
801 				stats->rx_frame_errors++;
802 			if (desc_status & (MSC_RTLF | MSC_RTSF))
803 				stats->rx_length_errors++;
804 			if (desc_status & MSC_CEEF)
805 				stats->rx_missed_errors++;
806 		} else {
807 			die_dt = desc->die_dt & 0xF0;
808 			switch (die_dt) {
809 			case DT_FSINGLE:
810 				skb = ravb_get_skb_gbeth(ndev, entry, desc);
811 				skb_put(skb, pkt_len);
812 				skb->protocol = eth_type_trans(skb, ndev);
813 				napi_gro_receive(&priv->napi[q], skb);
814 				stats->rx_packets++;
815 				stats->rx_bytes += pkt_len;
816 				break;
817 			case DT_FSTART:
818 				priv->rx_1st_skb = ravb_get_skb_gbeth(ndev, entry, desc);
819 				skb_put(priv->rx_1st_skb, pkt_len);
820 				break;
821 			case DT_FMID:
822 				skb = ravb_get_skb_gbeth(ndev, entry, desc);
823 				skb_copy_to_linear_data_offset(priv->rx_1st_skb,
824 							       priv->rx_1st_skb->len,
825 							       skb->data,
826 							       pkt_len);
827 				skb_put(priv->rx_1st_skb, pkt_len);
828 				dev_kfree_skb(skb);
829 				break;
830 			case DT_FEND:
831 				skb = ravb_get_skb_gbeth(ndev, entry, desc);
832 				skb_copy_to_linear_data_offset(priv->rx_1st_skb,
833 							       priv->rx_1st_skb->len,
834 							       skb->data,
835 							       pkt_len);
836 				skb_put(priv->rx_1st_skb, pkt_len);
837 				dev_kfree_skb(skb);
838 				priv->rx_1st_skb->protocol =
839 					eth_type_trans(priv->rx_1st_skb, ndev);
840 				napi_gro_receive(&priv->napi[q],
841 						 priv->rx_1st_skb);
842 				stats->rx_packets++;
843 				stats->rx_bytes += pkt_len;
844 				break;
845 			}
846 		}
847 
848 		entry = (++priv->cur_rx[q]) % priv->num_rx_ring[q];
849 		desc = &priv->gbeth_rx_ring[entry];
850 	}
851 
852 	/* Refill the RX ring buffers. */
853 	for (; priv->cur_rx[q] - priv->dirty_rx[q] > 0; priv->dirty_rx[q]++) {
854 		entry = priv->dirty_rx[q] % priv->num_rx_ring[q];
855 		desc = &priv->gbeth_rx_ring[entry];
856 		desc->ds_cc = cpu_to_le16(GBETH_RX_DESC_DATA_SIZE);
857 
858 		if (!priv->rx_skb[q][entry]) {
859 			skb = netdev_alloc_skb(ndev, info->max_rx_len);
860 			if (!skb)
861 				break;
862 			ravb_set_buffer_align(skb);
863 			dma_addr = dma_map_single(ndev->dev.parent,
864 						  skb->data,
865 						  GBETH_RX_BUFF_MAX,
866 						  DMA_FROM_DEVICE);
867 			skb_checksum_none_assert(skb);
868 			/* We just set the data size to 0 for a failed mapping
869 			 * which should prevent DMA  from happening...
870 			 */
871 			if (dma_mapping_error(ndev->dev.parent, dma_addr))
872 				desc->ds_cc = cpu_to_le16(0);
873 			desc->dptr = cpu_to_le32(dma_addr);
874 			priv->rx_skb[q][entry] = skb;
875 		}
876 		/* Descriptor type must be set after all the above writes */
877 		dma_wmb();
878 		desc->die_dt = DT_FEMPTY;
879 	}
880 
881 	*quota -= limit - (++boguscnt);
882 
883 	return boguscnt <= 0;
884 }
885 
886 /* Packet receive function for Ethernet AVB */
887 static bool ravb_rx_rcar(struct net_device *ndev, int *quota, int q)
888 {
889 	struct ravb_private *priv = netdev_priv(ndev);
890 	const struct ravb_hw_info *info = priv->info;
891 	int entry = priv->cur_rx[q] % priv->num_rx_ring[q];
892 	int boguscnt = (priv->dirty_rx[q] + priv->num_rx_ring[q]) -
893 			priv->cur_rx[q];
894 	struct net_device_stats *stats = &priv->stats[q];
895 	struct ravb_ex_rx_desc *desc;
896 	struct sk_buff *skb;
897 	dma_addr_t dma_addr;
898 	struct timespec64 ts;
899 	u8  desc_status;
900 	u16 pkt_len;
901 	int limit;
902 
903 	boguscnt = min(boguscnt, *quota);
904 	limit = boguscnt;
905 	desc = &priv->rx_ring[q][entry];
906 	while (desc->die_dt != DT_FEMPTY) {
907 		/* Descriptor type must be checked before all other reads */
908 		dma_rmb();
909 		desc_status = desc->msc;
910 		pkt_len = le16_to_cpu(desc->ds_cc) & RX_DS;
911 
912 		if (--boguscnt < 0)
913 			break;
914 
915 		/* We use 0-byte descriptors to mark the DMA mapping errors */
916 		if (!pkt_len)
917 			continue;
918 
919 		if (desc_status & MSC_MC)
920 			stats->multicast++;
921 
922 		if (desc_status & (MSC_CRC | MSC_RFE | MSC_RTSF | MSC_RTLF |
923 				   MSC_CEEF)) {
924 			stats->rx_errors++;
925 			if (desc_status & MSC_CRC)
926 				stats->rx_crc_errors++;
927 			if (desc_status & MSC_RFE)
928 				stats->rx_frame_errors++;
929 			if (desc_status & (MSC_RTLF | MSC_RTSF))
930 				stats->rx_length_errors++;
931 			if (desc_status & MSC_CEEF)
932 				stats->rx_missed_errors++;
933 		} else {
934 			u32 get_ts = priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE;
935 
936 			skb = priv->rx_skb[q][entry];
937 			priv->rx_skb[q][entry] = NULL;
938 			dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
939 					 RX_BUF_SZ,
940 					 DMA_FROM_DEVICE);
941 			get_ts &= (q == RAVB_NC) ?
942 					RAVB_RXTSTAMP_TYPE_V2_L2_EVENT :
943 					~RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
944 			if (get_ts) {
945 				struct skb_shared_hwtstamps *shhwtstamps;
946 
947 				shhwtstamps = skb_hwtstamps(skb);
948 				memset(shhwtstamps, 0, sizeof(*shhwtstamps));
949 				ts.tv_sec = ((u64) le16_to_cpu(desc->ts_sh) <<
950 					     32) | le32_to_cpu(desc->ts_sl);
951 				ts.tv_nsec = le32_to_cpu(desc->ts_n);
952 				shhwtstamps->hwtstamp = timespec64_to_ktime(ts);
953 			}
954 
955 			skb_put(skb, pkt_len);
956 			skb->protocol = eth_type_trans(skb, ndev);
957 			if (ndev->features & NETIF_F_RXCSUM)
958 				ravb_rx_csum(skb);
959 			napi_gro_receive(&priv->napi[q], skb);
960 			stats->rx_packets++;
961 			stats->rx_bytes += pkt_len;
962 		}
963 
964 		entry = (++priv->cur_rx[q]) % priv->num_rx_ring[q];
965 		desc = &priv->rx_ring[q][entry];
966 	}
967 
968 	/* Refill the RX ring buffers. */
969 	for (; priv->cur_rx[q] - priv->dirty_rx[q] > 0; priv->dirty_rx[q]++) {
970 		entry = priv->dirty_rx[q] % priv->num_rx_ring[q];
971 		desc = &priv->rx_ring[q][entry];
972 		desc->ds_cc = cpu_to_le16(RX_BUF_SZ);
973 
974 		if (!priv->rx_skb[q][entry]) {
975 			skb = netdev_alloc_skb(ndev, info->max_rx_len);
976 			if (!skb)
977 				break;	/* Better luck next round. */
978 			ravb_set_buffer_align(skb);
979 			dma_addr = dma_map_single(ndev->dev.parent, skb->data,
980 						  le16_to_cpu(desc->ds_cc),
981 						  DMA_FROM_DEVICE);
982 			skb_checksum_none_assert(skb);
983 			/* We just set the data size to 0 for a failed mapping
984 			 * which should prevent DMA  from happening...
985 			 */
986 			if (dma_mapping_error(ndev->dev.parent, dma_addr))
987 				desc->ds_cc = cpu_to_le16(0);
988 			desc->dptr = cpu_to_le32(dma_addr);
989 			priv->rx_skb[q][entry] = skb;
990 		}
991 		/* Descriptor type must be set after all the above writes */
992 		dma_wmb();
993 		desc->die_dt = DT_FEMPTY;
994 	}
995 
996 	*quota -= limit - (++boguscnt);
997 
998 	return boguscnt <= 0;
999 }
1000 
1001 /* Packet receive function for Ethernet AVB */
1002 static bool ravb_rx(struct net_device *ndev, int *quota, int q)
1003 {
1004 	struct ravb_private *priv = netdev_priv(ndev);
1005 	const struct ravb_hw_info *info = priv->info;
1006 
1007 	return info->receive(ndev, quota, q);
1008 }
1009 
1010 static void ravb_rcv_snd_disable(struct net_device *ndev)
1011 {
1012 	/* Disable TX and RX */
1013 	ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, 0);
1014 }
1015 
1016 static void ravb_rcv_snd_enable(struct net_device *ndev)
1017 {
1018 	/* Enable TX and RX */
1019 	ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, ECMR_RE | ECMR_TE);
1020 }
1021 
1022 /* function for waiting dma process finished */
1023 static int ravb_stop_dma(struct net_device *ndev)
1024 {
1025 	struct ravb_private *priv = netdev_priv(ndev);
1026 	const struct ravb_hw_info *info = priv->info;
1027 	int error;
1028 
1029 	/* Wait for stopping the hardware TX process */
1030 	error = ravb_wait(ndev, TCCR, info->tccr_mask, 0);
1031 
1032 	if (error)
1033 		return error;
1034 
1035 	error = ravb_wait(ndev, CSR, CSR_TPO0 | CSR_TPO1 | CSR_TPO2 | CSR_TPO3,
1036 			  0);
1037 	if (error)
1038 		return error;
1039 
1040 	/* Stop the E-MAC's RX/TX processes. */
1041 	ravb_rcv_snd_disable(ndev);
1042 
1043 	/* Wait for stopping the RX DMA process */
1044 	error = ravb_wait(ndev, CSR, CSR_RPO, 0);
1045 	if (error)
1046 		return error;
1047 
1048 	/* Stop AVB-DMAC process */
1049 	return ravb_config(ndev);
1050 }
1051 
1052 /* E-MAC interrupt handler */
1053 static void ravb_emac_interrupt_unlocked(struct net_device *ndev)
1054 {
1055 	struct ravb_private *priv = netdev_priv(ndev);
1056 	u32 ecsr, psr;
1057 
1058 	ecsr = ravb_read(ndev, ECSR);
1059 	ravb_write(ndev, ecsr, ECSR);	/* clear interrupt */
1060 
1061 	if (ecsr & ECSR_MPD)
1062 		pm_wakeup_event(&priv->pdev->dev, 0);
1063 	if (ecsr & ECSR_ICD)
1064 		ndev->stats.tx_carrier_errors++;
1065 	if (ecsr & ECSR_LCHNG) {
1066 		/* Link changed */
1067 		if (priv->no_avb_link)
1068 			return;
1069 		psr = ravb_read(ndev, PSR);
1070 		if (priv->avb_link_active_low)
1071 			psr ^= PSR_LMON;
1072 		if (!(psr & PSR_LMON)) {
1073 			/* DIsable RX and TX */
1074 			ravb_rcv_snd_disable(ndev);
1075 		} else {
1076 			/* Enable RX and TX */
1077 			ravb_rcv_snd_enable(ndev);
1078 		}
1079 	}
1080 }
1081 
1082 static irqreturn_t ravb_emac_interrupt(int irq, void *dev_id)
1083 {
1084 	struct net_device *ndev = dev_id;
1085 	struct ravb_private *priv = netdev_priv(ndev);
1086 
1087 	spin_lock(&priv->lock);
1088 	ravb_emac_interrupt_unlocked(ndev);
1089 	spin_unlock(&priv->lock);
1090 	return IRQ_HANDLED;
1091 }
1092 
1093 /* Error interrupt handler */
1094 static void ravb_error_interrupt(struct net_device *ndev)
1095 {
1096 	struct ravb_private *priv = netdev_priv(ndev);
1097 	u32 eis, ris2;
1098 
1099 	eis = ravb_read(ndev, EIS);
1100 	ravb_write(ndev, ~(EIS_QFS | EIS_RESERVED), EIS);
1101 	if (eis & EIS_QFS) {
1102 		ris2 = ravb_read(ndev, RIS2);
1103 		ravb_write(ndev, ~(RIS2_QFF0 | RIS2_QFF1 | RIS2_RFFF | RIS2_RESERVED),
1104 			   RIS2);
1105 
1106 		/* Receive Descriptor Empty int */
1107 		if (ris2 & RIS2_QFF0)
1108 			priv->stats[RAVB_BE].rx_over_errors++;
1109 
1110 		/* Receive Descriptor Empty int */
1111 		if (ris2 & RIS2_QFF1)
1112 			priv->stats[RAVB_NC].rx_over_errors++;
1113 
1114 		/* Receive FIFO Overflow int */
1115 		if (ris2 & RIS2_RFFF)
1116 			priv->rx_fifo_errors++;
1117 	}
1118 }
1119 
1120 static bool ravb_queue_interrupt(struct net_device *ndev, int q)
1121 {
1122 	struct ravb_private *priv = netdev_priv(ndev);
1123 	const struct ravb_hw_info *info = priv->info;
1124 	u32 ris0 = ravb_read(ndev, RIS0);
1125 	u32 ric0 = ravb_read(ndev, RIC0);
1126 	u32 tis  = ravb_read(ndev, TIS);
1127 	u32 tic  = ravb_read(ndev, TIC);
1128 
1129 	if (((ris0 & ric0) & BIT(q)) || ((tis  & tic)  & BIT(q))) {
1130 		if (napi_schedule_prep(&priv->napi[q])) {
1131 			/* Mask RX and TX interrupts */
1132 			if (!info->irq_en_dis) {
1133 				ravb_write(ndev, ric0 & ~BIT(q), RIC0);
1134 				ravb_write(ndev, tic & ~BIT(q), TIC);
1135 			} else {
1136 				ravb_write(ndev, BIT(q), RID0);
1137 				ravb_write(ndev, BIT(q), TID);
1138 			}
1139 			__napi_schedule(&priv->napi[q]);
1140 		} else {
1141 			netdev_warn(ndev,
1142 				    "ignoring interrupt, rx status 0x%08x, rx mask 0x%08x,\n",
1143 				    ris0, ric0);
1144 			netdev_warn(ndev,
1145 				    "                    tx status 0x%08x, tx mask 0x%08x.\n",
1146 				    tis, tic);
1147 		}
1148 		return true;
1149 	}
1150 	return false;
1151 }
1152 
1153 static bool ravb_timestamp_interrupt(struct net_device *ndev)
1154 {
1155 	u32 tis = ravb_read(ndev, TIS);
1156 
1157 	if (tis & TIS_TFUF) {
1158 		ravb_write(ndev, ~(TIS_TFUF | TIS_RESERVED), TIS);
1159 		ravb_get_tx_tstamp(ndev);
1160 		return true;
1161 	}
1162 	return false;
1163 }
1164 
1165 static irqreturn_t ravb_interrupt(int irq, void *dev_id)
1166 {
1167 	struct net_device *ndev = dev_id;
1168 	struct ravb_private *priv = netdev_priv(ndev);
1169 	const struct ravb_hw_info *info = priv->info;
1170 	irqreturn_t result = IRQ_NONE;
1171 	u32 iss;
1172 
1173 	spin_lock(&priv->lock);
1174 	/* Get interrupt status */
1175 	iss = ravb_read(ndev, ISS);
1176 
1177 	/* Received and transmitted interrupts */
1178 	if (iss & (ISS_FRS | ISS_FTS | ISS_TFUS)) {
1179 		int q;
1180 
1181 		/* Timestamp updated */
1182 		if (ravb_timestamp_interrupt(ndev))
1183 			result = IRQ_HANDLED;
1184 
1185 		/* Network control and best effort queue RX/TX */
1186 		if (info->nc_queues) {
1187 			for (q = RAVB_NC; q >= RAVB_BE; q--) {
1188 				if (ravb_queue_interrupt(ndev, q))
1189 					result = IRQ_HANDLED;
1190 			}
1191 		} else {
1192 			if (ravb_queue_interrupt(ndev, RAVB_BE))
1193 				result = IRQ_HANDLED;
1194 		}
1195 	}
1196 
1197 	/* E-MAC status summary */
1198 	if (iss & ISS_MS) {
1199 		ravb_emac_interrupt_unlocked(ndev);
1200 		result = IRQ_HANDLED;
1201 	}
1202 
1203 	/* Error status summary */
1204 	if (iss & ISS_ES) {
1205 		ravb_error_interrupt(ndev);
1206 		result = IRQ_HANDLED;
1207 	}
1208 
1209 	/* gPTP interrupt status summary */
1210 	if (iss & ISS_CGIS) {
1211 		ravb_ptp_interrupt(ndev);
1212 		result = IRQ_HANDLED;
1213 	}
1214 
1215 	spin_unlock(&priv->lock);
1216 	return result;
1217 }
1218 
1219 /* Timestamp/Error/gPTP interrupt handler */
1220 static irqreturn_t ravb_multi_interrupt(int irq, void *dev_id)
1221 {
1222 	struct net_device *ndev = dev_id;
1223 	struct ravb_private *priv = netdev_priv(ndev);
1224 	irqreturn_t result = IRQ_NONE;
1225 	u32 iss;
1226 
1227 	spin_lock(&priv->lock);
1228 	/* Get interrupt status */
1229 	iss = ravb_read(ndev, ISS);
1230 
1231 	/* Timestamp updated */
1232 	if ((iss & ISS_TFUS) && ravb_timestamp_interrupt(ndev))
1233 		result = IRQ_HANDLED;
1234 
1235 	/* Error status summary */
1236 	if (iss & ISS_ES) {
1237 		ravb_error_interrupt(ndev);
1238 		result = IRQ_HANDLED;
1239 	}
1240 
1241 	/* gPTP interrupt status summary */
1242 	if (iss & ISS_CGIS) {
1243 		ravb_ptp_interrupt(ndev);
1244 		result = IRQ_HANDLED;
1245 	}
1246 
1247 	spin_unlock(&priv->lock);
1248 	return result;
1249 }
1250 
1251 static irqreturn_t ravb_dma_interrupt(int irq, void *dev_id, int q)
1252 {
1253 	struct net_device *ndev = dev_id;
1254 	struct ravb_private *priv = netdev_priv(ndev);
1255 	irqreturn_t result = IRQ_NONE;
1256 
1257 	spin_lock(&priv->lock);
1258 
1259 	/* Network control/Best effort queue RX/TX */
1260 	if (ravb_queue_interrupt(ndev, q))
1261 		result = IRQ_HANDLED;
1262 
1263 	spin_unlock(&priv->lock);
1264 	return result;
1265 }
1266 
1267 static irqreturn_t ravb_be_interrupt(int irq, void *dev_id)
1268 {
1269 	return ravb_dma_interrupt(irq, dev_id, RAVB_BE);
1270 }
1271 
1272 static irqreturn_t ravb_nc_interrupt(int irq, void *dev_id)
1273 {
1274 	return ravb_dma_interrupt(irq, dev_id, RAVB_NC);
1275 }
1276 
1277 static int ravb_poll(struct napi_struct *napi, int budget)
1278 {
1279 	struct net_device *ndev = napi->dev;
1280 	struct ravb_private *priv = netdev_priv(ndev);
1281 	const struct ravb_hw_info *info = priv->info;
1282 	bool gptp = info->gptp || info->ccc_gac;
1283 	struct ravb_rx_desc *desc;
1284 	unsigned long flags;
1285 	int q = napi - priv->napi;
1286 	int mask = BIT(q);
1287 	int quota = budget;
1288 	unsigned int entry;
1289 
1290 	if (!gptp) {
1291 		entry = priv->cur_rx[q] % priv->num_rx_ring[q];
1292 		desc = &priv->gbeth_rx_ring[entry];
1293 	}
1294 	/* Processing RX Descriptor Ring */
1295 	/* Clear RX interrupt */
1296 	ravb_write(ndev, ~(mask | RIS0_RESERVED), RIS0);
1297 	if (gptp || desc->die_dt != DT_FEMPTY) {
1298 		if (ravb_rx(ndev, &quota, q))
1299 			goto out;
1300 	}
1301 
1302 	/* Processing TX Descriptor Ring */
1303 	spin_lock_irqsave(&priv->lock, flags);
1304 	/* Clear TX interrupt */
1305 	ravb_write(ndev, ~(mask | TIS_RESERVED), TIS);
1306 	ravb_tx_free(ndev, q, true);
1307 	netif_wake_subqueue(ndev, q);
1308 	spin_unlock_irqrestore(&priv->lock, flags);
1309 
1310 	napi_complete(napi);
1311 
1312 	/* Re-enable RX/TX interrupts */
1313 	spin_lock_irqsave(&priv->lock, flags);
1314 	if (!info->irq_en_dis) {
1315 		ravb_modify(ndev, RIC0, mask, mask);
1316 		ravb_modify(ndev, TIC,  mask, mask);
1317 	} else {
1318 		ravb_write(ndev, mask, RIE0);
1319 		ravb_write(ndev, mask, TIE);
1320 	}
1321 	spin_unlock_irqrestore(&priv->lock, flags);
1322 
1323 	/* Receive error message handling */
1324 	priv->rx_over_errors =  priv->stats[RAVB_BE].rx_over_errors;
1325 	if (info->nc_queues)
1326 		priv->rx_over_errors += priv->stats[RAVB_NC].rx_over_errors;
1327 	if (priv->rx_over_errors != ndev->stats.rx_over_errors)
1328 		ndev->stats.rx_over_errors = priv->rx_over_errors;
1329 	if (priv->rx_fifo_errors != ndev->stats.rx_fifo_errors)
1330 		ndev->stats.rx_fifo_errors = priv->rx_fifo_errors;
1331 out:
1332 	return budget - quota;
1333 }
1334 
1335 static void ravb_set_duplex_gbeth(struct net_device *ndev)
1336 {
1337 	struct ravb_private *priv = netdev_priv(ndev);
1338 
1339 	ravb_modify(ndev, ECMR, ECMR_DM, priv->duplex > 0 ? ECMR_DM : 0);
1340 }
1341 
1342 /* PHY state control function */
1343 static void ravb_adjust_link(struct net_device *ndev)
1344 {
1345 	struct ravb_private *priv = netdev_priv(ndev);
1346 	const struct ravb_hw_info *info = priv->info;
1347 	struct phy_device *phydev = ndev->phydev;
1348 	bool new_state = false;
1349 	unsigned long flags;
1350 
1351 	spin_lock_irqsave(&priv->lock, flags);
1352 
1353 	/* Disable TX and RX right over here, if E-MAC change is ignored */
1354 	if (priv->no_avb_link)
1355 		ravb_rcv_snd_disable(ndev);
1356 
1357 	if (phydev->link) {
1358 		if (info->half_duplex && phydev->duplex != priv->duplex) {
1359 			new_state = true;
1360 			priv->duplex = phydev->duplex;
1361 			ravb_set_duplex_gbeth(ndev);
1362 		}
1363 
1364 		if (phydev->speed != priv->speed) {
1365 			new_state = true;
1366 			priv->speed = phydev->speed;
1367 			info->set_rate(ndev);
1368 		}
1369 		if (!priv->link) {
1370 			ravb_modify(ndev, ECMR, ECMR_TXF, 0);
1371 			new_state = true;
1372 			priv->link = phydev->link;
1373 		}
1374 	} else if (priv->link) {
1375 		new_state = true;
1376 		priv->link = 0;
1377 		priv->speed = 0;
1378 		if (info->half_duplex)
1379 			priv->duplex = -1;
1380 	}
1381 
1382 	/* Enable TX and RX right over here, if E-MAC change is ignored */
1383 	if (priv->no_avb_link && phydev->link)
1384 		ravb_rcv_snd_enable(ndev);
1385 
1386 	spin_unlock_irqrestore(&priv->lock, flags);
1387 
1388 	if (new_state && netif_msg_link(priv))
1389 		phy_print_status(phydev);
1390 }
1391 
1392 /* PHY init function */
1393 static int ravb_phy_init(struct net_device *ndev)
1394 {
1395 	struct device_node *np = ndev->dev.parent->of_node;
1396 	struct ravb_private *priv = netdev_priv(ndev);
1397 	const struct ravb_hw_info *info = priv->info;
1398 	struct phy_device *phydev;
1399 	struct device_node *pn;
1400 	phy_interface_t iface;
1401 	int err;
1402 
1403 	priv->link = 0;
1404 	priv->speed = 0;
1405 	priv->duplex = -1;
1406 
1407 	/* Try connecting to PHY */
1408 	pn = of_parse_phandle(np, "phy-handle", 0);
1409 	if (!pn) {
1410 		/* In the case of a fixed PHY, the DT node associated
1411 		 * to the PHY is the Ethernet MAC DT node.
1412 		 */
1413 		if (of_phy_is_fixed_link(np)) {
1414 			err = of_phy_register_fixed_link(np);
1415 			if (err)
1416 				return err;
1417 		}
1418 		pn = of_node_get(np);
1419 	}
1420 
1421 	iface = priv->rgmii_override ? PHY_INTERFACE_MODE_RGMII
1422 				     : priv->phy_interface;
1423 	phydev = of_phy_connect(ndev, pn, ravb_adjust_link, 0, iface);
1424 	of_node_put(pn);
1425 	if (!phydev) {
1426 		netdev_err(ndev, "failed to connect PHY\n");
1427 		err = -ENOENT;
1428 		goto err_deregister_fixed_link;
1429 	}
1430 
1431 	if (!info->half_duplex) {
1432 		/* 10BASE, Pause and Asym Pause is not supported */
1433 		phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_10baseT_Half_BIT);
1434 		phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_10baseT_Full_BIT);
1435 		phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_Pause_BIT);
1436 		phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_Asym_Pause_BIT);
1437 
1438 		/* Half Duplex is not supported */
1439 		phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
1440 		phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_100baseT_Half_BIT);
1441 	}
1442 
1443 	phy_attached_info(phydev);
1444 
1445 	return 0;
1446 
1447 err_deregister_fixed_link:
1448 	if (of_phy_is_fixed_link(np))
1449 		of_phy_deregister_fixed_link(np);
1450 
1451 	return err;
1452 }
1453 
1454 /* PHY control start function */
1455 static int ravb_phy_start(struct net_device *ndev)
1456 {
1457 	int error;
1458 
1459 	error = ravb_phy_init(ndev);
1460 	if (error)
1461 		return error;
1462 
1463 	phy_start(ndev->phydev);
1464 
1465 	return 0;
1466 }
1467 
1468 static u32 ravb_get_msglevel(struct net_device *ndev)
1469 {
1470 	struct ravb_private *priv = netdev_priv(ndev);
1471 
1472 	return priv->msg_enable;
1473 }
1474 
1475 static void ravb_set_msglevel(struct net_device *ndev, u32 value)
1476 {
1477 	struct ravb_private *priv = netdev_priv(ndev);
1478 
1479 	priv->msg_enable = value;
1480 }
1481 
1482 static const char ravb_gstrings_stats_gbeth[][ETH_GSTRING_LEN] = {
1483 	"rx_queue_0_current",
1484 	"tx_queue_0_current",
1485 	"rx_queue_0_dirty",
1486 	"tx_queue_0_dirty",
1487 	"rx_queue_0_packets",
1488 	"tx_queue_0_packets",
1489 	"rx_queue_0_bytes",
1490 	"tx_queue_0_bytes",
1491 	"rx_queue_0_mcast_packets",
1492 	"rx_queue_0_errors",
1493 	"rx_queue_0_crc_errors",
1494 	"rx_queue_0_frame_errors",
1495 	"rx_queue_0_length_errors",
1496 	"rx_queue_0_csum_offload_errors",
1497 	"rx_queue_0_over_errors",
1498 };
1499 
1500 static const char ravb_gstrings_stats[][ETH_GSTRING_LEN] = {
1501 	"rx_queue_0_current",
1502 	"tx_queue_0_current",
1503 	"rx_queue_0_dirty",
1504 	"tx_queue_0_dirty",
1505 	"rx_queue_0_packets",
1506 	"tx_queue_0_packets",
1507 	"rx_queue_0_bytes",
1508 	"tx_queue_0_bytes",
1509 	"rx_queue_0_mcast_packets",
1510 	"rx_queue_0_errors",
1511 	"rx_queue_0_crc_errors",
1512 	"rx_queue_0_frame_errors",
1513 	"rx_queue_0_length_errors",
1514 	"rx_queue_0_missed_errors",
1515 	"rx_queue_0_over_errors",
1516 
1517 	"rx_queue_1_current",
1518 	"tx_queue_1_current",
1519 	"rx_queue_1_dirty",
1520 	"tx_queue_1_dirty",
1521 	"rx_queue_1_packets",
1522 	"tx_queue_1_packets",
1523 	"rx_queue_1_bytes",
1524 	"tx_queue_1_bytes",
1525 	"rx_queue_1_mcast_packets",
1526 	"rx_queue_1_errors",
1527 	"rx_queue_1_crc_errors",
1528 	"rx_queue_1_frame_errors",
1529 	"rx_queue_1_length_errors",
1530 	"rx_queue_1_missed_errors",
1531 	"rx_queue_1_over_errors",
1532 };
1533 
1534 static int ravb_get_sset_count(struct net_device *netdev, int sset)
1535 {
1536 	struct ravb_private *priv = netdev_priv(netdev);
1537 	const struct ravb_hw_info *info = priv->info;
1538 
1539 	switch (sset) {
1540 	case ETH_SS_STATS:
1541 		return info->stats_len;
1542 	default:
1543 		return -EOPNOTSUPP;
1544 	}
1545 }
1546 
1547 static void ravb_get_ethtool_stats(struct net_device *ndev,
1548 				   struct ethtool_stats *estats, u64 *data)
1549 {
1550 	struct ravb_private *priv = netdev_priv(ndev);
1551 	const struct ravb_hw_info *info = priv->info;
1552 	int num_rx_q;
1553 	int i = 0;
1554 	int q;
1555 
1556 	num_rx_q = info->nc_queues ? NUM_RX_QUEUE : 1;
1557 	/* Device-specific stats */
1558 	for (q = RAVB_BE; q < num_rx_q; q++) {
1559 		struct net_device_stats *stats = &priv->stats[q];
1560 
1561 		data[i++] = priv->cur_rx[q];
1562 		data[i++] = priv->cur_tx[q];
1563 		data[i++] = priv->dirty_rx[q];
1564 		data[i++] = priv->dirty_tx[q];
1565 		data[i++] = stats->rx_packets;
1566 		data[i++] = stats->tx_packets;
1567 		data[i++] = stats->rx_bytes;
1568 		data[i++] = stats->tx_bytes;
1569 		data[i++] = stats->multicast;
1570 		data[i++] = stats->rx_errors;
1571 		data[i++] = stats->rx_crc_errors;
1572 		data[i++] = stats->rx_frame_errors;
1573 		data[i++] = stats->rx_length_errors;
1574 		data[i++] = stats->rx_missed_errors;
1575 		data[i++] = stats->rx_over_errors;
1576 	}
1577 }
1578 
1579 static void ravb_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1580 {
1581 	struct ravb_private *priv = netdev_priv(ndev);
1582 	const struct ravb_hw_info *info = priv->info;
1583 
1584 	switch (stringset) {
1585 	case ETH_SS_STATS:
1586 		memcpy(data, info->gstrings_stats, info->gstrings_size);
1587 		break;
1588 	}
1589 }
1590 
1591 static void ravb_get_ringparam(struct net_device *ndev,
1592 			       struct ethtool_ringparam *ring,
1593 			       struct kernel_ethtool_ringparam *kernel_ring,
1594 			       struct netlink_ext_ack *extack)
1595 {
1596 	struct ravb_private *priv = netdev_priv(ndev);
1597 
1598 	ring->rx_max_pending = BE_RX_RING_MAX;
1599 	ring->tx_max_pending = BE_TX_RING_MAX;
1600 	ring->rx_pending = priv->num_rx_ring[RAVB_BE];
1601 	ring->tx_pending = priv->num_tx_ring[RAVB_BE];
1602 }
1603 
1604 static int ravb_set_ringparam(struct net_device *ndev,
1605 			      struct ethtool_ringparam *ring,
1606 			      struct kernel_ethtool_ringparam *kernel_ring,
1607 			      struct netlink_ext_ack *extack)
1608 {
1609 	struct ravb_private *priv = netdev_priv(ndev);
1610 	const struct ravb_hw_info *info = priv->info;
1611 	int error;
1612 
1613 	if (ring->tx_pending > BE_TX_RING_MAX ||
1614 	    ring->rx_pending > BE_RX_RING_MAX ||
1615 	    ring->tx_pending < BE_TX_RING_MIN ||
1616 	    ring->rx_pending < BE_RX_RING_MIN)
1617 		return -EINVAL;
1618 	if (ring->rx_mini_pending || ring->rx_jumbo_pending)
1619 		return -EINVAL;
1620 
1621 	if (netif_running(ndev)) {
1622 		netif_device_detach(ndev);
1623 		/* Stop PTP Clock driver */
1624 		if (info->gptp)
1625 			ravb_ptp_stop(ndev);
1626 		/* Wait for DMA stopping */
1627 		error = ravb_stop_dma(ndev);
1628 		if (error) {
1629 			netdev_err(ndev,
1630 				   "cannot set ringparam! Any AVB processes are still running?\n");
1631 			return error;
1632 		}
1633 		synchronize_irq(ndev->irq);
1634 
1635 		/* Free all the skb's in the RX queue and the DMA buffers. */
1636 		ravb_ring_free(ndev, RAVB_BE);
1637 		if (info->nc_queues)
1638 			ravb_ring_free(ndev, RAVB_NC);
1639 	}
1640 
1641 	/* Set new parameters */
1642 	priv->num_rx_ring[RAVB_BE] = ring->rx_pending;
1643 	priv->num_tx_ring[RAVB_BE] = ring->tx_pending;
1644 
1645 	if (netif_running(ndev)) {
1646 		error = ravb_dmac_init(ndev);
1647 		if (error) {
1648 			netdev_err(ndev,
1649 				   "%s: ravb_dmac_init() failed, error %d\n",
1650 				   __func__, error);
1651 			return error;
1652 		}
1653 
1654 		ravb_emac_init(ndev);
1655 
1656 		/* Initialise PTP Clock driver */
1657 		if (info->gptp)
1658 			ravb_ptp_init(ndev, priv->pdev);
1659 
1660 		netif_device_attach(ndev);
1661 	}
1662 
1663 	return 0;
1664 }
1665 
1666 static int ravb_get_ts_info(struct net_device *ndev,
1667 			    struct ethtool_ts_info *info)
1668 {
1669 	struct ravb_private *priv = netdev_priv(ndev);
1670 	const struct ravb_hw_info *hw_info = priv->info;
1671 
1672 	info->so_timestamping =
1673 		SOF_TIMESTAMPING_TX_SOFTWARE |
1674 		SOF_TIMESTAMPING_RX_SOFTWARE |
1675 		SOF_TIMESTAMPING_SOFTWARE |
1676 		SOF_TIMESTAMPING_TX_HARDWARE |
1677 		SOF_TIMESTAMPING_RX_HARDWARE |
1678 		SOF_TIMESTAMPING_RAW_HARDWARE;
1679 	info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
1680 	info->rx_filters =
1681 		(1 << HWTSTAMP_FILTER_NONE) |
1682 		(1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
1683 		(1 << HWTSTAMP_FILTER_ALL);
1684 	if (hw_info->gptp || hw_info->ccc_gac)
1685 		info->phc_index = ptp_clock_index(priv->ptp.clock);
1686 
1687 	return 0;
1688 }
1689 
1690 static void ravb_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
1691 {
1692 	struct ravb_private *priv = netdev_priv(ndev);
1693 
1694 	wol->supported = WAKE_MAGIC;
1695 	wol->wolopts = priv->wol_enabled ? WAKE_MAGIC : 0;
1696 }
1697 
1698 static int ravb_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
1699 {
1700 	struct ravb_private *priv = netdev_priv(ndev);
1701 	const struct ravb_hw_info *info = priv->info;
1702 
1703 	if (!info->magic_pkt || (wol->wolopts & ~WAKE_MAGIC))
1704 		return -EOPNOTSUPP;
1705 
1706 	priv->wol_enabled = !!(wol->wolopts & WAKE_MAGIC);
1707 
1708 	device_set_wakeup_enable(&priv->pdev->dev, priv->wol_enabled);
1709 
1710 	return 0;
1711 }
1712 
1713 static const struct ethtool_ops ravb_ethtool_ops = {
1714 	.nway_reset		= phy_ethtool_nway_reset,
1715 	.get_msglevel		= ravb_get_msglevel,
1716 	.set_msglevel		= ravb_set_msglevel,
1717 	.get_link		= ethtool_op_get_link,
1718 	.get_strings		= ravb_get_strings,
1719 	.get_ethtool_stats	= ravb_get_ethtool_stats,
1720 	.get_sset_count		= ravb_get_sset_count,
1721 	.get_ringparam		= ravb_get_ringparam,
1722 	.set_ringparam		= ravb_set_ringparam,
1723 	.get_ts_info		= ravb_get_ts_info,
1724 	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
1725 	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
1726 	.get_wol		= ravb_get_wol,
1727 	.set_wol		= ravb_set_wol,
1728 };
1729 
1730 static inline int ravb_hook_irq(unsigned int irq, irq_handler_t handler,
1731 				struct net_device *ndev, struct device *dev,
1732 				const char *ch)
1733 {
1734 	char *name;
1735 	int error;
1736 
1737 	name = devm_kasprintf(dev, GFP_KERNEL, "%s:%s", ndev->name, ch);
1738 	if (!name)
1739 		return -ENOMEM;
1740 	error = request_irq(irq, handler, 0, name, ndev);
1741 	if (error)
1742 		netdev_err(ndev, "cannot request IRQ %s\n", name);
1743 
1744 	return error;
1745 }
1746 
1747 /* Network device open function for Ethernet AVB */
1748 static int ravb_open(struct net_device *ndev)
1749 {
1750 	struct ravb_private *priv = netdev_priv(ndev);
1751 	const struct ravb_hw_info *info = priv->info;
1752 	struct platform_device *pdev = priv->pdev;
1753 	struct device *dev = &pdev->dev;
1754 	int error;
1755 
1756 	napi_enable(&priv->napi[RAVB_BE]);
1757 	if (info->nc_queues)
1758 		napi_enable(&priv->napi[RAVB_NC]);
1759 
1760 	if (!info->multi_irqs) {
1761 		error = request_irq(ndev->irq, ravb_interrupt, IRQF_SHARED,
1762 				    ndev->name, ndev);
1763 		if (error) {
1764 			netdev_err(ndev, "cannot request IRQ\n");
1765 			goto out_napi_off;
1766 		}
1767 	} else {
1768 		error = ravb_hook_irq(ndev->irq, ravb_multi_interrupt, ndev,
1769 				      dev, "ch22:multi");
1770 		if (error)
1771 			goto out_napi_off;
1772 		error = ravb_hook_irq(priv->emac_irq, ravb_emac_interrupt, ndev,
1773 				      dev, "ch24:emac");
1774 		if (error)
1775 			goto out_free_irq;
1776 		error = ravb_hook_irq(priv->rx_irqs[RAVB_BE], ravb_be_interrupt,
1777 				      ndev, dev, "ch0:rx_be");
1778 		if (error)
1779 			goto out_free_irq_emac;
1780 		error = ravb_hook_irq(priv->tx_irqs[RAVB_BE], ravb_be_interrupt,
1781 				      ndev, dev, "ch18:tx_be");
1782 		if (error)
1783 			goto out_free_irq_be_rx;
1784 		error = ravb_hook_irq(priv->rx_irqs[RAVB_NC], ravb_nc_interrupt,
1785 				      ndev, dev, "ch1:rx_nc");
1786 		if (error)
1787 			goto out_free_irq_be_tx;
1788 		error = ravb_hook_irq(priv->tx_irqs[RAVB_NC], ravb_nc_interrupt,
1789 				      ndev, dev, "ch19:tx_nc");
1790 		if (error)
1791 			goto out_free_irq_nc_rx;
1792 
1793 		if (info->err_mgmt_irqs) {
1794 			error = ravb_hook_irq(priv->erra_irq, ravb_multi_interrupt,
1795 					      ndev, dev, "err_a");
1796 			if (error)
1797 				goto out_free_irq_nc_tx;
1798 			error = ravb_hook_irq(priv->mgmta_irq, ravb_multi_interrupt,
1799 					      ndev, dev, "mgmt_a");
1800 			if (error)
1801 				goto out_free_irq_erra;
1802 		}
1803 	}
1804 
1805 	/* Device init */
1806 	error = ravb_dmac_init(ndev);
1807 	if (error)
1808 		goto out_free_irq_mgmta;
1809 	ravb_emac_init(ndev);
1810 
1811 	/* Initialise PTP Clock driver */
1812 	if (info->gptp)
1813 		ravb_ptp_init(ndev, priv->pdev);
1814 
1815 	/* PHY control start */
1816 	error = ravb_phy_start(ndev);
1817 	if (error)
1818 		goto out_ptp_stop;
1819 
1820 	netif_tx_start_all_queues(ndev);
1821 
1822 	return 0;
1823 
1824 out_ptp_stop:
1825 	/* Stop PTP Clock driver */
1826 	if (info->gptp)
1827 		ravb_ptp_stop(ndev);
1828 	ravb_stop_dma(ndev);
1829 out_free_irq_mgmta:
1830 	if (!info->multi_irqs)
1831 		goto out_free_irq;
1832 	if (info->err_mgmt_irqs)
1833 		free_irq(priv->mgmta_irq, ndev);
1834 out_free_irq_erra:
1835 	if (info->err_mgmt_irqs)
1836 		free_irq(priv->erra_irq, ndev);
1837 out_free_irq_nc_tx:
1838 	free_irq(priv->tx_irqs[RAVB_NC], ndev);
1839 out_free_irq_nc_rx:
1840 	free_irq(priv->rx_irqs[RAVB_NC], ndev);
1841 out_free_irq_be_tx:
1842 	free_irq(priv->tx_irqs[RAVB_BE], ndev);
1843 out_free_irq_be_rx:
1844 	free_irq(priv->rx_irqs[RAVB_BE], ndev);
1845 out_free_irq_emac:
1846 	free_irq(priv->emac_irq, ndev);
1847 out_free_irq:
1848 	free_irq(ndev->irq, ndev);
1849 out_napi_off:
1850 	if (info->nc_queues)
1851 		napi_disable(&priv->napi[RAVB_NC]);
1852 	napi_disable(&priv->napi[RAVB_BE]);
1853 	return error;
1854 }
1855 
1856 /* Timeout function for Ethernet AVB */
1857 static void ravb_tx_timeout(struct net_device *ndev, unsigned int txqueue)
1858 {
1859 	struct ravb_private *priv = netdev_priv(ndev);
1860 
1861 	netif_err(priv, tx_err, ndev,
1862 		  "transmit timed out, status %08x, resetting...\n",
1863 		  ravb_read(ndev, ISS));
1864 
1865 	/* tx_errors count up */
1866 	ndev->stats.tx_errors++;
1867 
1868 	schedule_work(&priv->work);
1869 }
1870 
1871 static void ravb_tx_timeout_work(struct work_struct *work)
1872 {
1873 	struct ravb_private *priv = container_of(work, struct ravb_private,
1874 						 work);
1875 	const struct ravb_hw_info *info = priv->info;
1876 	struct net_device *ndev = priv->ndev;
1877 	int error;
1878 
1879 	if (!rtnl_trylock()) {
1880 		usleep_range(1000, 2000);
1881 		schedule_work(&priv->work);
1882 		return;
1883 	}
1884 
1885 	netif_tx_stop_all_queues(ndev);
1886 
1887 	/* Stop PTP Clock driver */
1888 	if (info->gptp)
1889 		ravb_ptp_stop(ndev);
1890 
1891 	/* Wait for DMA stopping */
1892 	if (ravb_stop_dma(ndev)) {
1893 		/* If ravb_stop_dma() fails, the hardware is still operating
1894 		 * for TX and/or RX. So, this should not call the following
1895 		 * functions because ravb_dmac_init() is possible to fail too.
1896 		 * Also, this should not retry ravb_stop_dma() again and again
1897 		 * here because it's possible to wait forever. So, this just
1898 		 * re-enables the TX and RX and skip the following
1899 		 * re-initialization procedure.
1900 		 */
1901 		ravb_rcv_snd_enable(ndev);
1902 		goto out;
1903 	}
1904 
1905 	ravb_ring_free(ndev, RAVB_BE);
1906 	if (info->nc_queues)
1907 		ravb_ring_free(ndev, RAVB_NC);
1908 
1909 	/* Device init */
1910 	error = ravb_dmac_init(ndev);
1911 	if (error) {
1912 		/* If ravb_dmac_init() fails, descriptors are freed. So, this
1913 		 * should return here to avoid re-enabling the TX and RX in
1914 		 * ravb_emac_init().
1915 		 */
1916 		netdev_err(ndev, "%s: ravb_dmac_init() failed, error %d\n",
1917 			   __func__, error);
1918 		goto out_unlock;
1919 	}
1920 	ravb_emac_init(ndev);
1921 
1922 out:
1923 	/* Initialise PTP Clock driver */
1924 	if (info->gptp)
1925 		ravb_ptp_init(ndev, priv->pdev);
1926 
1927 	netif_tx_start_all_queues(ndev);
1928 
1929 out_unlock:
1930 	rtnl_unlock();
1931 }
1932 
1933 /* Packet transmit function for Ethernet AVB */
1934 static netdev_tx_t ravb_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1935 {
1936 	struct ravb_private *priv = netdev_priv(ndev);
1937 	const struct ravb_hw_info *info = priv->info;
1938 	unsigned int num_tx_desc = priv->num_tx_desc;
1939 	u16 q = skb_get_queue_mapping(skb);
1940 	struct ravb_tstamp_skb *ts_skb;
1941 	struct ravb_tx_desc *desc;
1942 	unsigned long flags;
1943 	u32 dma_addr;
1944 	void *buffer;
1945 	u32 entry;
1946 	u32 len;
1947 
1948 	spin_lock_irqsave(&priv->lock, flags);
1949 	if (priv->cur_tx[q] - priv->dirty_tx[q] > (priv->num_tx_ring[q] - 1) *
1950 	    num_tx_desc) {
1951 		netif_err(priv, tx_queued, ndev,
1952 			  "still transmitting with the full ring!\n");
1953 		netif_stop_subqueue(ndev, q);
1954 		spin_unlock_irqrestore(&priv->lock, flags);
1955 		return NETDEV_TX_BUSY;
1956 	}
1957 
1958 	if (skb_put_padto(skb, ETH_ZLEN))
1959 		goto exit;
1960 
1961 	entry = priv->cur_tx[q] % (priv->num_tx_ring[q] * num_tx_desc);
1962 	priv->tx_skb[q][entry / num_tx_desc] = skb;
1963 
1964 	if (num_tx_desc > 1) {
1965 		buffer = PTR_ALIGN(priv->tx_align[q], DPTR_ALIGN) +
1966 			 entry / num_tx_desc * DPTR_ALIGN;
1967 		len = PTR_ALIGN(skb->data, DPTR_ALIGN) - skb->data;
1968 
1969 		/* Zero length DMA descriptors are problematic as they seem
1970 		 * to terminate DMA transfers. Avoid them by simply using a
1971 		 * length of DPTR_ALIGN (4) when skb data is aligned to
1972 		 * DPTR_ALIGN.
1973 		 *
1974 		 * As skb is guaranteed to have at least ETH_ZLEN (60)
1975 		 * bytes of data by the call to skb_put_padto() above this
1976 		 * is safe with respect to both the length of the first DMA
1977 		 * descriptor (len) overflowing the available data and the
1978 		 * length of the second DMA descriptor (skb->len - len)
1979 		 * being negative.
1980 		 */
1981 		if (len == 0)
1982 			len = DPTR_ALIGN;
1983 
1984 		memcpy(buffer, skb->data, len);
1985 		dma_addr = dma_map_single(ndev->dev.parent, buffer, len,
1986 					  DMA_TO_DEVICE);
1987 		if (dma_mapping_error(ndev->dev.parent, dma_addr))
1988 			goto drop;
1989 
1990 		desc = &priv->tx_ring[q][entry];
1991 		desc->ds_tagl = cpu_to_le16(len);
1992 		desc->dptr = cpu_to_le32(dma_addr);
1993 
1994 		buffer = skb->data + len;
1995 		len = skb->len - len;
1996 		dma_addr = dma_map_single(ndev->dev.parent, buffer, len,
1997 					  DMA_TO_DEVICE);
1998 		if (dma_mapping_error(ndev->dev.parent, dma_addr))
1999 			goto unmap;
2000 
2001 		desc++;
2002 	} else {
2003 		desc = &priv->tx_ring[q][entry];
2004 		len = skb->len;
2005 		dma_addr = dma_map_single(ndev->dev.parent, skb->data, skb->len,
2006 					  DMA_TO_DEVICE);
2007 		if (dma_mapping_error(ndev->dev.parent, dma_addr))
2008 			goto drop;
2009 	}
2010 	desc->ds_tagl = cpu_to_le16(len);
2011 	desc->dptr = cpu_to_le32(dma_addr);
2012 
2013 	/* TX timestamp required */
2014 	if (info->gptp || info->ccc_gac) {
2015 		if (q == RAVB_NC) {
2016 			ts_skb = kmalloc(sizeof(*ts_skb), GFP_ATOMIC);
2017 			if (!ts_skb) {
2018 				if (num_tx_desc > 1) {
2019 					desc--;
2020 					dma_unmap_single(ndev->dev.parent, dma_addr,
2021 							 len, DMA_TO_DEVICE);
2022 				}
2023 				goto unmap;
2024 			}
2025 			ts_skb->skb = skb_get(skb);
2026 			ts_skb->tag = priv->ts_skb_tag++;
2027 			priv->ts_skb_tag &= 0x3ff;
2028 			list_add_tail(&ts_skb->list, &priv->ts_skb_list);
2029 
2030 			/* TAG and timestamp required flag */
2031 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2032 			desc->tagh_tsr = (ts_skb->tag >> 4) | TX_TSR;
2033 			desc->ds_tagl |= cpu_to_le16(ts_skb->tag << 12);
2034 		}
2035 
2036 		skb_tx_timestamp(skb);
2037 	}
2038 	/* Descriptor type must be set after all the above writes */
2039 	dma_wmb();
2040 	if (num_tx_desc > 1) {
2041 		desc->die_dt = DT_FEND;
2042 		desc--;
2043 		desc->die_dt = DT_FSTART;
2044 	} else {
2045 		desc->die_dt = DT_FSINGLE;
2046 	}
2047 	ravb_modify(ndev, TCCR, TCCR_TSRQ0 << q, TCCR_TSRQ0 << q);
2048 
2049 	priv->cur_tx[q] += num_tx_desc;
2050 	if (priv->cur_tx[q] - priv->dirty_tx[q] >
2051 	    (priv->num_tx_ring[q] - 1) * num_tx_desc &&
2052 	    !ravb_tx_free(ndev, q, true))
2053 		netif_stop_subqueue(ndev, q);
2054 
2055 exit:
2056 	spin_unlock_irqrestore(&priv->lock, flags);
2057 	return NETDEV_TX_OK;
2058 
2059 unmap:
2060 	dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
2061 			 le16_to_cpu(desc->ds_tagl), DMA_TO_DEVICE);
2062 drop:
2063 	dev_kfree_skb_any(skb);
2064 	priv->tx_skb[q][entry / num_tx_desc] = NULL;
2065 	goto exit;
2066 }
2067 
2068 static u16 ravb_select_queue(struct net_device *ndev, struct sk_buff *skb,
2069 			     struct net_device *sb_dev)
2070 {
2071 	/* If skb needs TX timestamp, it is handled in network control queue */
2072 	return (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) ? RAVB_NC :
2073 							       RAVB_BE;
2074 
2075 }
2076 
2077 static struct net_device_stats *ravb_get_stats(struct net_device *ndev)
2078 {
2079 	struct ravb_private *priv = netdev_priv(ndev);
2080 	const struct ravb_hw_info *info = priv->info;
2081 	struct net_device_stats *nstats, *stats0, *stats1;
2082 
2083 	nstats = &ndev->stats;
2084 	stats0 = &priv->stats[RAVB_BE];
2085 
2086 	if (info->tx_counters) {
2087 		nstats->tx_dropped += ravb_read(ndev, TROCR);
2088 		ravb_write(ndev, 0, TROCR);	/* (write clear) */
2089 	}
2090 
2091 	if (info->carrier_counters) {
2092 		nstats->collisions += ravb_read(ndev, CXR41);
2093 		ravb_write(ndev, 0, CXR41);	/* (write clear) */
2094 		nstats->tx_carrier_errors += ravb_read(ndev, CXR42);
2095 		ravb_write(ndev, 0, CXR42);	/* (write clear) */
2096 	}
2097 
2098 	nstats->rx_packets = stats0->rx_packets;
2099 	nstats->tx_packets = stats0->tx_packets;
2100 	nstats->rx_bytes = stats0->rx_bytes;
2101 	nstats->tx_bytes = stats0->tx_bytes;
2102 	nstats->multicast = stats0->multicast;
2103 	nstats->rx_errors = stats0->rx_errors;
2104 	nstats->rx_crc_errors = stats0->rx_crc_errors;
2105 	nstats->rx_frame_errors = stats0->rx_frame_errors;
2106 	nstats->rx_length_errors = stats0->rx_length_errors;
2107 	nstats->rx_missed_errors = stats0->rx_missed_errors;
2108 	nstats->rx_over_errors = stats0->rx_over_errors;
2109 	if (info->nc_queues) {
2110 		stats1 = &priv->stats[RAVB_NC];
2111 
2112 		nstats->rx_packets += stats1->rx_packets;
2113 		nstats->tx_packets += stats1->tx_packets;
2114 		nstats->rx_bytes += stats1->rx_bytes;
2115 		nstats->tx_bytes += stats1->tx_bytes;
2116 		nstats->multicast += stats1->multicast;
2117 		nstats->rx_errors += stats1->rx_errors;
2118 		nstats->rx_crc_errors += stats1->rx_crc_errors;
2119 		nstats->rx_frame_errors += stats1->rx_frame_errors;
2120 		nstats->rx_length_errors += stats1->rx_length_errors;
2121 		nstats->rx_missed_errors += stats1->rx_missed_errors;
2122 		nstats->rx_over_errors += stats1->rx_over_errors;
2123 	}
2124 
2125 	return nstats;
2126 }
2127 
2128 /* Update promiscuous bit */
2129 static void ravb_set_rx_mode(struct net_device *ndev)
2130 {
2131 	struct ravb_private *priv = netdev_priv(ndev);
2132 	unsigned long flags;
2133 
2134 	spin_lock_irqsave(&priv->lock, flags);
2135 	ravb_modify(ndev, ECMR, ECMR_PRM,
2136 		    ndev->flags & IFF_PROMISC ? ECMR_PRM : 0);
2137 	spin_unlock_irqrestore(&priv->lock, flags);
2138 }
2139 
2140 /* Device close function for Ethernet AVB */
2141 static int ravb_close(struct net_device *ndev)
2142 {
2143 	struct device_node *np = ndev->dev.parent->of_node;
2144 	struct ravb_private *priv = netdev_priv(ndev);
2145 	const struct ravb_hw_info *info = priv->info;
2146 	struct ravb_tstamp_skb *ts_skb, *ts_skb2;
2147 
2148 	netif_tx_stop_all_queues(ndev);
2149 
2150 	/* Disable interrupts by clearing the interrupt masks. */
2151 	ravb_write(ndev, 0, RIC0);
2152 	ravb_write(ndev, 0, RIC2);
2153 	ravb_write(ndev, 0, TIC);
2154 
2155 	/* Stop PTP Clock driver */
2156 	if (info->gptp)
2157 		ravb_ptp_stop(ndev);
2158 
2159 	/* Set the config mode to stop the AVB-DMAC's processes */
2160 	if (ravb_stop_dma(ndev) < 0)
2161 		netdev_err(ndev,
2162 			   "device will be stopped after h/w processes are done.\n");
2163 
2164 	/* Clear the timestamp list */
2165 	if (info->gptp || info->ccc_gac) {
2166 		list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list, list) {
2167 			list_del(&ts_skb->list);
2168 			kfree_skb(ts_skb->skb);
2169 			kfree(ts_skb);
2170 		}
2171 	}
2172 
2173 	/* PHY disconnect */
2174 	if (ndev->phydev) {
2175 		phy_stop(ndev->phydev);
2176 		phy_disconnect(ndev->phydev);
2177 		if (of_phy_is_fixed_link(np))
2178 			of_phy_deregister_fixed_link(np);
2179 	}
2180 
2181 	cancel_work_sync(&priv->work);
2182 
2183 	if (info->multi_irqs) {
2184 		free_irq(priv->tx_irqs[RAVB_NC], ndev);
2185 		free_irq(priv->rx_irqs[RAVB_NC], ndev);
2186 		free_irq(priv->tx_irqs[RAVB_BE], ndev);
2187 		free_irq(priv->rx_irqs[RAVB_BE], ndev);
2188 		free_irq(priv->emac_irq, ndev);
2189 		if (info->err_mgmt_irqs) {
2190 			free_irq(priv->erra_irq, ndev);
2191 			free_irq(priv->mgmta_irq, ndev);
2192 		}
2193 	}
2194 	free_irq(ndev->irq, ndev);
2195 
2196 	if (info->nc_queues)
2197 		napi_disable(&priv->napi[RAVB_NC]);
2198 	napi_disable(&priv->napi[RAVB_BE]);
2199 
2200 	/* Free all the skb's in the RX queue and the DMA buffers. */
2201 	ravb_ring_free(ndev, RAVB_BE);
2202 	if (info->nc_queues)
2203 		ravb_ring_free(ndev, RAVB_NC);
2204 
2205 	return 0;
2206 }
2207 
2208 static int ravb_hwtstamp_get(struct net_device *ndev, struct ifreq *req)
2209 {
2210 	struct ravb_private *priv = netdev_priv(ndev);
2211 	struct hwtstamp_config config;
2212 
2213 	config.flags = 0;
2214 	config.tx_type = priv->tstamp_tx_ctrl ? HWTSTAMP_TX_ON :
2215 						HWTSTAMP_TX_OFF;
2216 	switch (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE) {
2217 	case RAVB_RXTSTAMP_TYPE_V2_L2_EVENT:
2218 		config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
2219 		break;
2220 	case RAVB_RXTSTAMP_TYPE_ALL:
2221 		config.rx_filter = HWTSTAMP_FILTER_ALL;
2222 		break;
2223 	default:
2224 		config.rx_filter = HWTSTAMP_FILTER_NONE;
2225 	}
2226 
2227 	return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
2228 		-EFAULT : 0;
2229 }
2230 
2231 /* Control hardware time stamping */
2232 static int ravb_hwtstamp_set(struct net_device *ndev, struct ifreq *req)
2233 {
2234 	struct ravb_private *priv = netdev_priv(ndev);
2235 	struct hwtstamp_config config;
2236 	u32 tstamp_rx_ctrl = RAVB_RXTSTAMP_ENABLED;
2237 	u32 tstamp_tx_ctrl;
2238 
2239 	if (copy_from_user(&config, req->ifr_data, sizeof(config)))
2240 		return -EFAULT;
2241 
2242 	switch (config.tx_type) {
2243 	case HWTSTAMP_TX_OFF:
2244 		tstamp_tx_ctrl = 0;
2245 		break;
2246 	case HWTSTAMP_TX_ON:
2247 		tstamp_tx_ctrl = RAVB_TXTSTAMP_ENABLED;
2248 		break;
2249 	default:
2250 		return -ERANGE;
2251 	}
2252 
2253 	switch (config.rx_filter) {
2254 	case HWTSTAMP_FILTER_NONE:
2255 		tstamp_rx_ctrl = 0;
2256 		break;
2257 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2258 		tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
2259 		break;
2260 	default:
2261 		config.rx_filter = HWTSTAMP_FILTER_ALL;
2262 		tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_ALL;
2263 	}
2264 
2265 	priv->tstamp_tx_ctrl = tstamp_tx_ctrl;
2266 	priv->tstamp_rx_ctrl = tstamp_rx_ctrl;
2267 
2268 	return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
2269 		-EFAULT : 0;
2270 }
2271 
2272 /* ioctl to device function */
2273 static int ravb_do_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
2274 {
2275 	struct phy_device *phydev = ndev->phydev;
2276 
2277 	if (!netif_running(ndev))
2278 		return -EINVAL;
2279 
2280 	if (!phydev)
2281 		return -ENODEV;
2282 
2283 	switch (cmd) {
2284 	case SIOCGHWTSTAMP:
2285 		return ravb_hwtstamp_get(ndev, req);
2286 	case SIOCSHWTSTAMP:
2287 		return ravb_hwtstamp_set(ndev, req);
2288 	}
2289 
2290 	return phy_mii_ioctl(phydev, req, cmd);
2291 }
2292 
2293 static int ravb_change_mtu(struct net_device *ndev, int new_mtu)
2294 {
2295 	struct ravb_private *priv = netdev_priv(ndev);
2296 
2297 	ndev->mtu = new_mtu;
2298 
2299 	if (netif_running(ndev)) {
2300 		synchronize_irq(priv->emac_irq);
2301 		ravb_emac_init(ndev);
2302 	}
2303 
2304 	netdev_update_features(ndev);
2305 
2306 	return 0;
2307 }
2308 
2309 static void ravb_set_rx_csum(struct net_device *ndev, bool enable)
2310 {
2311 	struct ravb_private *priv = netdev_priv(ndev);
2312 	unsigned long flags;
2313 
2314 	spin_lock_irqsave(&priv->lock, flags);
2315 
2316 	/* Disable TX and RX */
2317 	ravb_rcv_snd_disable(ndev);
2318 
2319 	/* Modify RX Checksum setting */
2320 	ravb_modify(ndev, ECMR, ECMR_RCSC, enable ? ECMR_RCSC : 0);
2321 
2322 	/* Enable TX and RX */
2323 	ravb_rcv_snd_enable(ndev);
2324 
2325 	spin_unlock_irqrestore(&priv->lock, flags);
2326 }
2327 
2328 static int ravb_set_features_gbeth(struct net_device *ndev,
2329 				   netdev_features_t features)
2330 {
2331 	/* Place holder */
2332 	return 0;
2333 }
2334 
2335 static int ravb_set_features_rcar(struct net_device *ndev,
2336 				  netdev_features_t features)
2337 {
2338 	netdev_features_t changed = ndev->features ^ features;
2339 
2340 	if (changed & NETIF_F_RXCSUM)
2341 		ravb_set_rx_csum(ndev, features & NETIF_F_RXCSUM);
2342 
2343 	ndev->features = features;
2344 
2345 	return 0;
2346 }
2347 
2348 static int ravb_set_features(struct net_device *ndev,
2349 			     netdev_features_t features)
2350 {
2351 	struct ravb_private *priv = netdev_priv(ndev);
2352 	const struct ravb_hw_info *info = priv->info;
2353 
2354 	return info->set_feature(ndev, features);
2355 }
2356 
2357 static const struct net_device_ops ravb_netdev_ops = {
2358 	.ndo_open		= ravb_open,
2359 	.ndo_stop		= ravb_close,
2360 	.ndo_start_xmit		= ravb_start_xmit,
2361 	.ndo_select_queue	= ravb_select_queue,
2362 	.ndo_get_stats		= ravb_get_stats,
2363 	.ndo_set_rx_mode	= ravb_set_rx_mode,
2364 	.ndo_tx_timeout		= ravb_tx_timeout,
2365 	.ndo_eth_ioctl		= ravb_do_ioctl,
2366 	.ndo_change_mtu		= ravb_change_mtu,
2367 	.ndo_validate_addr	= eth_validate_addr,
2368 	.ndo_set_mac_address	= eth_mac_addr,
2369 	.ndo_set_features	= ravb_set_features,
2370 };
2371 
2372 /* MDIO bus init function */
2373 static int ravb_mdio_init(struct ravb_private *priv)
2374 {
2375 	struct platform_device *pdev = priv->pdev;
2376 	struct device *dev = &pdev->dev;
2377 	struct phy_device *phydev;
2378 	struct device_node *pn;
2379 	int error;
2380 
2381 	/* Bitbang init */
2382 	priv->mdiobb.ops = &bb_ops;
2383 
2384 	/* MII controller setting */
2385 	priv->mii_bus = alloc_mdio_bitbang(&priv->mdiobb);
2386 	if (!priv->mii_bus)
2387 		return -ENOMEM;
2388 
2389 	/* Hook up MII support for ethtool */
2390 	priv->mii_bus->name = "ravb_mii";
2391 	priv->mii_bus->parent = dev;
2392 	snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
2393 		 pdev->name, pdev->id);
2394 
2395 	/* Register MDIO bus */
2396 	error = of_mdiobus_register(priv->mii_bus, dev->of_node);
2397 	if (error)
2398 		goto out_free_bus;
2399 
2400 	pn = of_parse_phandle(dev->of_node, "phy-handle", 0);
2401 	phydev = of_phy_find_device(pn);
2402 	if (phydev) {
2403 		phydev->mac_managed_pm = true;
2404 		put_device(&phydev->mdio.dev);
2405 	}
2406 	of_node_put(pn);
2407 
2408 	return 0;
2409 
2410 out_free_bus:
2411 	free_mdio_bitbang(priv->mii_bus);
2412 	return error;
2413 }
2414 
2415 /* MDIO bus release function */
2416 static int ravb_mdio_release(struct ravb_private *priv)
2417 {
2418 	/* Unregister mdio bus */
2419 	mdiobus_unregister(priv->mii_bus);
2420 
2421 	/* Free bitbang info */
2422 	free_mdio_bitbang(priv->mii_bus);
2423 
2424 	return 0;
2425 }
2426 
2427 static const struct ravb_hw_info ravb_gen3_hw_info = {
2428 	.rx_ring_free = ravb_rx_ring_free_rcar,
2429 	.rx_ring_format = ravb_rx_ring_format_rcar,
2430 	.alloc_rx_desc = ravb_alloc_rx_desc_rcar,
2431 	.receive = ravb_rx_rcar,
2432 	.set_rate = ravb_set_rate_rcar,
2433 	.set_feature = ravb_set_features_rcar,
2434 	.dmac_init = ravb_dmac_init_rcar,
2435 	.emac_init = ravb_emac_init_rcar,
2436 	.gstrings_stats = ravb_gstrings_stats,
2437 	.gstrings_size = sizeof(ravb_gstrings_stats),
2438 	.net_hw_features = NETIF_F_RXCSUM,
2439 	.net_features = NETIF_F_RXCSUM,
2440 	.stats_len = ARRAY_SIZE(ravb_gstrings_stats),
2441 	.max_rx_len = RX_BUF_SZ + RAVB_ALIGN - 1,
2442 	.tccr_mask = TCCR_TSRQ0 | TCCR_TSRQ1 | TCCR_TSRQ2 | TCCR_TSRQ3,
2443 	.rx_max_buf_size = SZ_2K,
2444 	.internal_delay = 1,
2445 	.tx_counters = 1,
2446 	.multi_irqs = 1,
2447 	.irq_en_dis = 1,
2448 	.ccc_gac = 1,
2449 	.nc_queues = 1,
2450 	.magic_pkt = 1,
2451 };
2452 
2453 static const struct ravb_hw_info ravb_gen2_hw_info = {
2454 	.rx_ring_free = ravb_rx_ring_free_rcar,
2455 	.rx_ring_format = ravb_rx_ring_format_rcar,
2456 	.alloc_rx_desc = ravb_alloc_rx_desc_rcar,
2457 	.receive = ravb_rx_rcar,
2458 	.set_rate = ravb_set_rate_rcar,
2459 	.set_feature = ravb_set_features_rcar,
2460 	.dmac_init = ravb_dmac_init_rcar,
2461 	.emac_init = ravb_emac_init_rcar,
2462 	.gstrings_stats = ravb_gstrings_stats,
2463 	.gstrings_size = sizeof(ravb_gstrings_stats),
2464 	.net_hw_features = NETIF_F_RXCSUM,
2465 	.net_features = NETIF_F_RXCSUM,
2466 	.stats_len = ARRAY_SIZE(ravb_gstrings_stats),
2467 	.max_rx_len = RX_BUF_SZ + RAVB_ALIGN - 1,
2468 	.tccr_mask = TCCR_TSRQ0 | TCCR_TSRQ1 | TCCR_TSRQ2 | TCCR_TSRQ3,
2469 	.rx_max_buf_size = SZ_2K,
2470 	.aligned_tx = 1,
2471 	.gptp = 1,
2472 	.nc_queues = 1,
2473 	.magic_pkt = 1,
2474 };
2475 
2476 static const struct ravb_hw_info ravb_rzv2m_hw_info = {
2477 	.rx_ring_free = ravb_rx_ring_free_rcar,
2478 	.rx_ring_format = ravb_rx_ring_format_rcar,
2479 	.alloc_rx_desc = ravb_alloc_rx_desc_rcar,
2480 	.receive = ravb_rx_rcar,
2481 	.set_rate = ravb_set_rate_rcar,
2482 	.set_feature = ravb_set_features_rcar,
2483 	.dmac_init = ravb_dmac_init_rcar,
2484 	.emac_init = ravb_emac_init_rcar,
2485 	.gstrings_stats = ravb_gstrings_stats,
2486 	.gstrings_size = sizeof(ravb_gstrings_stats),
2487 	.net_hw_features = NETIF_F_RXCSUM,
2488 	.net_features = NETIF_F_RXCSUM,
2489 	.stats_len = ARRAY_SIZE(ravb_gstrings_stats),
2490 	.max_rx_len = RX_BUF_SZ + RAVB_ALIGN - 1,
2491 	.tccr_mask = TCCR_TSRQ0 | TCCR_TSRQ1 | TCCR_TSRQ2 | TCCR_TSRQ3,
2492 	.rx_max_buf_size = SZ_2K,
2493 	.multi_irqs = 1,
2494 	.err_mgmt_irqs = 1,
2495 	.gptp = 1,
2496 	.gptp_ref_clk = 1,
2497 	.nc_queues = 1,
2498 	.magic_pkt = 1,
2499 };
2500 
2501 static const struct ravb_hw_info gbeth_hw_info = {
2502 	.rx_ring_free = ravb_rx_ring_free_gbeth,
2503 	.rx_ring_format = ravb_rx_ring_format_gbeth,
2504 	.alloc_rx_desc = ravb_alloc_rx_desc_gbeth,
2505 	.receive = ravb_rx_gbeth,
2506 	.set_rate = ravb_set_rate_gbeth,
2507 	.set_feature = ravb_set_features_gbeth,
2508 	.dmac_init = ravb_dmac_init_gbeth,
2509 	.emac_init = ravb_emac_init_gbeth,
2510 	.gstrings_stats = ravb_gstrings_stats_gbeth,
2511 	.gstrings_size = sizeof(ravb_gstrings_stats_gbeth),
2512 	.stats_len = ARRAY_SIZE(ravb_gstrings_stats_gbeth),
2513 	.max_rx_len = ALIGN(GBETH_RX_BUFF_MAX, RAVB_ALIGN),
2514 	.tccr_mask = TCCR_TSRQ0,
2515 	.rx_max_buf_size = SZ_8K,
2516 	.aligned_tx = 1,
2517 	.tx_counters = 1,
2518 	.carrier_counters = 1,
2519 	.half_duplex = 1,
2520 };
2521 
2522 static const struct of_device_id ravb_match_table[] = {
2523 	{ .compatible = "renesas,etheravb-r8a7790", .data = &ravb_gen2_hw_info },
2524 	{ .compatible = "renesas,etheravb-r8a7794", .data = &ravb_gen2_hw_info },
2525 	{ .compatible = "renesas,etheravb-rcar-gen2", .data = &ravb_gen2_hw_info },
2526 	{ .compatible = "renesas,etheravb-r8a7795", .data = &ravb_gen3_hw_info },
2527 	{ .compatible = "renesas,etheravb-rcar-gen3", .data = &ravb_gen3_hw_info },
2528 	{ .compatible = "renesas,etheravb-rcar-gen4", .data = &ravb_gen3_hw_info },
2529 	{ .compatible = "renesas,etheravb-rzv2m", .data = &ravb_rzv2m_hw_info },
2530 	{ .compatible = "renesas,rzg2l-gbeth", .data = &gbeth_hw_info },
2531 	{ }
2532 };
2533 MODULE_DEVICE_TABLE(of, ravb_match_table);
2534 
2535 static int ravb_set_gti(struct net_device *ndev)
2536 {
2537 	struct ravb_private *priv = netdev_priv(ndev);
2538 	const struct ravb_hw_info *info = priv->info;
2539 	struct device *dev = ndev->dev.parent;
2540 	unsigned long rate;
2541 	uint64_t inc;
2542 
2543 	if (info->gptp_ref_clk)
2544 		rate = clk_get_rate(priv->gptp_clk);
2545 	else
2546 		rate = clk_get_rate(priv->clk);
2547 	if (!rate)
2548 		return -EINVAL;
2549 
2550 	inc = div64_ul(1000000000ULL << 20, rate);
2551 
2552 	if (inc < GTI_TIV_MIN || inc > GTI_TIV_MAX) {
2553 		dev_err(dev, "gti.tiv increment 0x%llx is outside the range 0x%x - 0x%x\n",
2554 			inc, GTI_TIV_MIN, GTI_TIV_MAX);
2555 		return -EINVAL;
2556 	}
2557 
2558 	ravb_write(ndev, inc, GTI);
2559 
2560 	return 0;
2561 }
2562 
2563 static void ravb_set_config_mode(struct net_device *ndev)
2564 {
2565 	struct ravb_private *priv = netdev_priv(ndev);
2566 	const struct ravb_hw_info *info = priv->info;
2567 
2568 	if (info->gptp) {
2569 		ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG);
2570 		/* Set CSEL value */
2571 		ravb_modify(ndev, CCC, CCC_CSEL, CCC_CSEL_HPB);
2572 	} else if (info->ccc_gac) {
2573 		ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG |
2574 			    CCC_GAC | CCC_CSEL_HPB);
2575 	} else {
2576 		ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG);
2577 	}
2578 }
2579 
2580 /* Set tx and rx clock internal delay modes */
2581 static void ravb_parse_delay_mode(struct device_node *np, struct net_device *ndev)
2582 {
2583 	struct ravb_private *priv = netdev_priv(ndev);
2584 	bool explicit_delay = false;
2585 	u32 delay;
2586 
2587 	if (!of_property_read_u32(np, "rx-internal-delay-ps", &delay)) {
2588 		/* Valid values are 0 and 1800, according to DT bindings */
2589 		priv->rxcidm = !!delay;
2590 		explicit_delay = true;
2591 	}
2592 	if (!of_property_read_u32(np, "tx-internal-delay-ps", &delay)) {
2593 		/* Valid values are 0 and 2000, according to DT bindings */
2594 		priv->txcidm = !!delay;
2595 		explicit_delay = true;
2596 	}
2597 
2598 	if (explicit_delay)
2599 		return;
2600 
2601 	/* Fall back to legacy rgmii-*id behavior */
2602 	if (priv->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
2603 	    priv->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID) {
2604 		priv->rxcidm = 1;
2605 		priv->rgmii_override = 1;
2606 	}
2607 
2608 	if (priv->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
2609 	    priv->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID) {
2610 		priv->txcidm = 1;
2611 		priv->rgmii_override = 1;
2612 	}
2613 }
2614 
2615 static void ravb_set_delay_mode(struct net_device *ndev)
2616 {
2617 	struct ravb_private *priv = netdev_priv(ndev);
2618 	u32 set = 0;
2619 
2620 	if (priv->rxcidm)
2621 		set |= APSR_RDM;
2622 	if (priv->txcidm)
2623 		set |= APSR_TDM;
2624 	ravb_modify(ndev, APSR, APSR_RDM | APSR_TDM, set);
2625 }
2626 
2627 static int ravb_probe(struct platform_device *pdev)
2628 {
2629 	struct device_node *np = pdev->dev.of_node;
2630 	const struct ravb_hw_info *info;
2631 	struct reset_control *rstc;
2632 	struct ravb_private *priv;
2633 	struct net_device *ndev;
2634 	int error, irq, q;
2635 	struct resource *res;
2636 	int i;
2637 
2638 	if (!np) {
2639 		dev_err(&pdev->dev,
2640 			"this driver is required to be instantiated from device tree\n");
2641 		return -EINVAL;
2642 	}
2643 
2644 	rstc = devm_reset_control_get_optional_exclusive(&pdev->dev, NULL);
2645 	if (IS_ERR(rstc))
2646 		return dev_err_probe(&pdev->dev, PTR_ERR(rstc),
2647 				     "failed to get cpg reset\n");
2648 
2649 	ndev = alloc_etherdev_mqs(sizeof(struct ravb_private),
2650 				  NUM_TX_QUEUE, NUM_RX_QUEUE);
2651 	if (!ndev)
2652 		return -ENOMEM;
2653 
2654 	info = of_device_get_match_data(&pdev->dev);
2655 
2656 	ndev->features = info->net_features;
2657 	ndev->hw_features = info->net_hw_features;
2658 
2659 	error = reset_control_deassert(rstc);
2660 	if (error)
2661 		goto out_free_netdev;
2662 
2663 	pm_runtime_enable(&pdev->dev);
2664 	error = pm_runtime_resume_and_get(&pdev->dev);
2665 	if (error < 0)
2666 		goto out_rpm_disable;
2667 
2668 	if (info->multi_irqs) {
2669 		if (info->err_mgmt_irqs)
2670 			irq = platform_get_irq_byname(pdev, "dia");
2671 		else
2672 			irq = platform_get_irq_byname(pdev, "ch22");
2673 	} else {
2674 		irq = platform_get_irq(pdev, 0);
2675 	}
2676 	if (irq < 0) {
2677 		error = irq;
2678 		goto out_release;
2679 	}
2680 	ndev->irq = irq;
2681 
2682 	SET_NETDEV_DEV(ndev, &pdev->dev);
2683 
2684 	priv = netdev_priv(ndev);
2685 	priv->info = info;
2686 	priv->rstc = rstc;
2687 	priv->ndev = ndev;
2688 	priv->pdev = pdev;
2689 	priv->num_tx_ring[RAVB_BE] = BE_TX_RING_SIZE;
2690 	priv->num_rx_ring[RAVB_BE] = BE_RX_RING_SIZE;
2691 	if (info->nc_queues) {
2692 		priv->num_tx_ring[RAVB_NC] = NC_TX_RING_SIZE;
2693 		priv->num_rx_ring[RAVB_NC] = NC_RX_RING_SIZE;
2694 	}
2695 
2696 	priv->addr = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
2697 	if (IS_ERR(priv->addr)) {
2698 		error = PTR_ERR(priv->addr);
2699 		goto out_release;
2700 	}
2701 
2702 	/* The Ether-specific entries in the device structure. */
2703 	ndev->base_addr = res->start;
2704 
2705 	spin_lock_init(&priv->lock);
2706 	INIT_WORK(&priv->work, ravb_tx_timeout_work);
2707 
2708 	error = of_get_phy_mode(np, &priv->phy_interface);
2709 	if (error && error != -ENODEV)
2710 		goto out_release;
2711 
2712 	priv->no_avb_link = of_property_read_bool(np, "renesas,no-ether-link");
2713 	priv->avb_link_active_low =
2714 		of_property_read_bool(np, "renesas,ether-link-active-low");
2715 
2716 	if (info->multi_irqs) {
2717 		if (info->err_mgmt_irqs)
2718 			irq = platform_get_irq_byname(pdev, "line3");
2719 		else
2720 			irq = platform_get_irq_byname(pdev, "ch24");
2721 		if (irq < 0) {
2722 			error = irq;
2723 			goto out_release;
2724 		}
2725 		priv->emac_irq = irq;
2726 		for (i = 0; i < NUM_RX_QUEUE; i++) {
2727 			irq = platform_get_irq_byname(pdev, ravb_rx_irqs[i]);
2728 			if (irq < 0) {
2729 				error = irq;
2730 				goto out_release;
2731 			}
2732 			priv->rx_irqs[i] = irq;
2733 		}
2734 		for (i = 0; i < NUM_TX_QUEUE; i++) {
2735 			irq = platform_get_irq_byname(pdev, ravb_tx_irqs[i]);
2736 			if (irq < 0) {
2737 				error = irq;
2738 				goto out_release;
2739 			}
2740 			priv->tx_irqs[i] = irq;
2741 		}
2742 
2743 		if (info->err_mgmt_irqs) {
2744 			irq = platform_get_irq_byname(pdev, "err_a");
2745 			if (irq < 0) {
2746 				error = irq;
2747 				goto out_release;
2748 			}
2749 			priv->erra_irq = irq;
2750 
2751 			irq = platform_get_irq_byname(pdev, "mgmt_a");
2752 			if (irq < 0) {
2753 				error = irq;
2754 				goto out_release;
2755 			}
2756 			priv->mgmta_irq = irq;
2757 		}
2758 	}
2759 
2760 	priv->clk = devm_clk_get(&pdev->dev, NULL);
2761 	if (IS_ERR(priv->clk)) {
2762 		error = PTR_ERR(priv->clk);
2763 		goto out_release;
2764 	}
2765 
2766 	priv->refclk = devm_clk_get_optional(&pdev->dev, "refclk");
2767 	if (IS_ERR(priv->refclk)) {
2768 		error = PTR_ERR(priv->refclk);
2769 		goto out_release;
2770 	}
2771 	clk_prepare_enable(priv->refclk);
2772 
2773 	if (info->gptp_ref_clk) {
2774 		priv->gptp_clk = devm_clk_get(&pdev->dev, "gptp");
2775 		if (IS_ERR(priv->gptp_clk)) {
2776 			error = PTR_ERR(priv->gptp_clk);
2777 			goto out_disable_refclk;
2778 		}
2779 		clk_prepare_enable(priv->gptp_clk);
2780 	}
2781 
2782 	ndev->max_mtu = info->rx_max_buf_size - (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN);
2783 	ndev->min_mtu = ETH_MIN_MTU;
2784 
2785 	/* FIXME: R-Car Gen2 has 4byte alignment restriction for tx buffer
2786 	 * Use two descriptor to handle such situation. First descriptor to
2787 	 * handle aligned data buffer and second descriptor to handle the
2788 	 * overflow data because of alignment.
2789 	 */
2790 	priv->num_tx_desc = info->aligned_tx ? 2 : 1;
2791 
2792 	/* Set function */
2793 	ndev->netdev_ops = &ravb_netdev_ops;
2794 	ndev->ethtool_ops = &ravb_ethtool_ops;
2795 
2796 	/* Set AVB config mode */
2797 	ravb_set_config_mode(ndev);
2798 
2799 	if (info->gptp || info->ccc_gac) {
2800 		/* Set GTI value */
2801 		error = ravb_set_gti(ndev);
2802 		if (error)
2803 			goto out_disable_gptp_clk;
2804 
2805 		/* Request GTI loading */
2806 		ravb_modify(ndev, GCCR, GCCR_LTI, GCCR_LTI);
2807 	}
2808 
2809 	if (info->internal_delay) {
2810 		ravb_parse_delay_mode(np, ndev);
2811 		ravb_set_delay_mode(ndev);
2812 	}
2813 
2814 	/* Allocate descriptor base address table */
2815 	priv->desc_bat_size = sizeof(struct ravb_desc) * DBAT_ENTRY_NUM;
2816 	priv->desc_bat = dma_alloc_coherent(ndev->dev.parent, priv->desc_bat_size,
2817 					    &priv->desc_bat_dma, GFP_KERNEL);
2818 	if (!priv->desc_bat) {
2819 		dev_err(&pdev->dev,
2820 			"Cannot allocate desc base address table (size %d bytes)\n",
2821 			priv->desc_bat_size);
2822 		error = -ENOMEM;
2823 		goto out_disable_gptp_clk;
2824 	}
2825 	for (q = RAVB_BE; q < DBAT_ENTRY_NUM; q++)
2826 		priv->desc_bat[q].die_dt = DT_EOS;
2827 	ravb_write(ndev, priv->desc_bat_dma, DBAT);
2828 
2829 	/* Initialise HW timestamp list */
2830 	INIT_LIST_HEAD(&priv->ts_skb_list);
2831 
2832 	/* Initialise PTP Clock driver */
2833 	if (info->ccc_gac)
2834 		ravb_ptp_init(ndev, pdev);
2835 
2836 	/* Debug message level */
2837 	priv->msg_enable = RAVB_DEF_MSG_ENABLE;
2838 
2839 	/* Read and set MAC address */
2840 	ravb_read_mac_address(np, ndev);
2841 	if (!is_valid_ether_addr(ndev->dev_addr)) {
2842 		dev_warn(&pdev->dev,
2843 			 "no valid MAC address supplied, using a random one\n");
2844 		eth_hw_addr_random(ndev);
2845 	}
2846 
2847 	/* MDIO bus init */
2848 	error = ravb_mdio_init(priv);
2849 	if (error) {
2850 		dev_err(&pdev->dev, "failed to initialize MDIO\n");
2851 		goto out_dma_free;
2852 	}
2853 
2854 	netif_napi_add(ndev, &priv->napi[RAVB_BE], ravb_poll);
2855 	if (info->nc_queues)
2856 		netif_napi_add(ndev, &priv->napi[RAVB_NC], ravb_poll);
2857 
2858 	/* Network device register */
2859 	error = register_netdev(ndev);
2860 	if (error)
2861 		goto out_napi_del;
2862 
2863 	device_set_wakeup_capable(&pdev->dev, 1);
2864 
2865 	/* Print device information */
2866 	netdev_info(ndev, "Base address at %#x, %pM, IRQ %d.\n",
2867 		    (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
2868 
2869 	platform_set_drvdata(pdev, ndev);
2870 
2871 	return 0;
2872 
2873 out_napi_del:
2874 	if (info->nc_queues)
2875 		netif_napi_del(&priv->napi[RAVB_NC]);
2876 
2877 	netif_napi_del(&priv->napi[RAVB_BE]);
2878 	ravb_mdio_release(priv);
2879 out_dma_free:
2880 	dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
2881 			  priv->desc_bat_dma);
2882 
2883 	/* Stop PTP Clock driver */
2884 	if (info->ccc_gac)
2885 		ravb_ptp_stop(ndev);
2886 out_disable_gptp_clk:
2887 	clk_disable_unprepare(priv->gptp_clk);
2888 out_disable_refclk:
2889 	clk_disable_unprepare(priv->refclk);
2890 out_release:
2891 	pm_runtime_put(&pdev->dev);
2892 out_rpm_disable:
2893 	pm_runtime_disable(&pdev->dev);
2894 	reset_control_assert(rstc);
2895 out_free_netdev:
2896 	free_netdev(ndev);
2897 	return error;
2898 }
2899 
2900 static void ravb_remove(struct platform_device *pdev)
2901 {
2902 	struct net_device *ndev = platform_get_drvdata(pdev);
2903 	struct ravb_private *priv = netdev_priv(ndev);
2904 	const struct ravb_hw_info *info = priv->info;
2905 
2906 	unregister_netdev(ndev);
2907 	if (info->nc_queues)
2908 		netif_napi_del(&priv->napi[RAVB_NC]);
2909 	netif_napi_del(&priv->napi[RAVB_BE]);
2910 
2911 	ravb_mdio_release(priv);
2912 
2913 	/* Stop PTP Clock driver */
2914 	if (info->ccc_gac)
2915 		ravb_ptp_stop(ndev);
2916 
2917 	dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
2918 			  priv->desc_bat_dma);
2919 
2920 	/* Set reset mode */
2921 	ravb_write(ndev, CCC_OPC_RESET, CCC);
2922 
2923 	clk_disable_unprepare(priv->gptp_clk);
2924 	clk_disable_unprepare(priv->refclk);
2925 
2926 	pm_runtime_put_sync(&pdev->dev);
2927 	pm_runtime_disable(&pdev->dev);
2928 	reset_control_assert(priv->rstc);
2929 	free_netdev(ndev);
2930 	platform_set_drvdata(pdev, NULL);
2931 }
2932 
2933 static int ravb_wol_setup(struct net_device *ndev)
2934 {
2935 	struct ravb_private *priv = netdev_priv(ndev);
2936 	const struct ravb_hw_info *info = priv->info;
2937 
2938 	/* Disable interrupts by clearing the interrupt masks. */
2939 	ravb_write(ndev, 0, RIC0);
2940 	ravb_write(ndev, 0, RIC2);
2941 	ravb_write(ndev, 0, TIC);
2942 
2943 	/* Only allow ECI interrupts */
2944 	synchronize_irq(priv->emac_irq);
2945 	if (info->nc_queues)
2946 		napi_disable(&priv->napi[RAVB_NC]);
2947 	napi_disable(&priv->napi[RAVB_BE]);
2948 	ravb_write(ndev, ECSIPR_MPDIP, ECSIPR);
2949 
2950 	/* Enable MagicPacket */
2951 	ravb_modify(ndev, ECMR, ECMR_MPDE, ECMR_MPDE);
2952 
2953 	return enable_irq_wake(priv->emac_irq);
2954 }
2955 
2956 static int ravb_wol_restore(struct net_device *ndev)
2957 {
2958 	struct ravb_private *priv = netdev_priv(ndev);
2959 	const struct ravb_hw_info *info = priv->info;
2960 
2961 	if (info->nc_queues)
2962 		napi_enable(&priv->napi[RAVB_NC]);
2963 	napi_enable(&priv->napi[RAVB_BE]);
2964 
2965 	/* Disable MagicPacket */
2966 	ravb_modify(ndev, ECMR, ECMR_MPDE, 0);
2967 
2968 	ravb_close(ndev);
2969 
2970 	return disable_irq_wake(priv->emac_irq);
2971 }
2972 
2973 static int __maybe_unused ravb_suspend(struct device *dev)
2974 {
2975 	struct net_device *ndev = dev_get_drvdata(dev);
2976 	struct ravb_private *priv = netdev_priv(ndev);
2977 	int ret;
2978 
2979 	if (!netif_running(ndev))
2980 		return 0;
2981 
2982 	netif_device_detach(ndev);
2983 
2984 	if (priv->wol_enabled)
2985 		ret = ravb_wol_setup(ndev);
2986 	else
2987 		ret = ravb_close(ndev);
2988 
2989 	if (priv->info->ccc_gac)
2990 		ravb_ptp_stop(ndev);
2991 
2992 	return ret;
2993 }
2994 
2995 static int __maybe_unused ravb_resume(struct device *dev)
2996 {
2997 	struct net_device *ndev = dev_get_drvdata(dev);
2998 	struct ravb_private *priv = netdev_priv(ndev);
2999 	const struct ravb_hw_info *info = priv->info;
3000 	int ret = 0;
3001 
3002 	/* If WoL is enabled set reset mode to rearm the WoL logic */
3003 	if (priv->wol_enabled)
3004 		ravb_write(ndev, CCC_OPC_RESET, CCC);
3005 
3006 	/* All register have been reset to default values.
3007 	 * Restore all registers which where setup at probe time and
3008 	 * reopen device if it was running before system suspended.
3009 	 */
3010 
3011 	/* Set AVB config mode */
3012 	ravb_set_config_mode(ndev);
3013 
3014 	if (info->gptp || info->ccc_gac) {
3015 		/* Set GTI value */
3016 		ret = ravb_set_gti(ndev);
3017 		if (ret)
3018 			return ret;
3019 
3020 		/* Request GTI loading */
3021 		ravb_modify(ndev, GCCR, GCCR_LTI, GCCR_LTI);
3022 	}
3023 
3024 	if (info->internal_delay)
3025 		ravb_set_delay_mode(ndev);
3026 
3027 	/* Restore descriptor base address table */
3028 	ravb_write(ndev, priv->desc_bat_dma, DBAT);
3029 
3030 	if (priv->info->ccc_gac)
3031 		ravb_ptp_init(ndev, priv->pdev);
3032 
3033 	if (netif_running(ndev)) {
3034 		if (priv->wol_enabled) {
3035 			ret = ravb_wol_restore(ndev);
3036 			if (ret)
3037 				return ret;
3038 		}
3039 		ret = ravb_open(ndev);
3040 		if (ret < 0)
3041 			return ret;
3042 		ravb_set_rx_mode(ndev);
3043 		netif_device_attach(ndev);
3044 	}
3045 
3046 	return ret;
3047 }
3048 
3049 static int __maybe_unused ravb_runtime_nop(struct device *dev)
3050 {
3051 	/* Runtime PM callback shared between ->runtime_suspend()
3052 	 * and ->runtime_resume(). Simply returns success.
3053 	 *
3054 	 * This driver re-initializes all registers after
3055 	 * pm_runtime_get_sync() anyway so there is no need
3056 	 * to save and restore registers here.
3057 	 */
3058 	return 0;
3059 }
3060 
3061 static const struct dev_pm_ops ravb_dev_pm_ops = {
3062 	SET_SYSTEM_SLEEP_PM_OPS(ravb_suspend, ravb_resume)
3063 	SET_RUNTIME_PM_OPS(ravb_runtime_nop, ravb_runtime_nop, NULL)
3064 };
3065 
3066 static struct platform_driver ravb_driver = {
3067 	.probe		= ravb_probe,
3068 	.remove_new	= ravb_remove,
3069 	.driver = {
3070 		.name	= "ravb",
3071 		.pm	= &ravb_dev_pm_ops,
3072 		.of_match_table = ravb_match_table,
3073 	},
3074 };
3075 
3076 module_platform_driver(ravb_driver);
3077 
3078 MODULE_AUTHOR("Mitsuhiro Kimura, Masaru Nagai");
3079 MODULE_DESCRIPTION("Renesas Ethernet AVB driver");
3080 MODULE_LICENSE("GPL v2");
3081