Linux Audio

Check our new training course

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