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