Linux Audio

Check our new training course

Loading...
v3.15
 
   1/*
   2 * Driver for Xilinx TEMAC Ethernet device
   3 *
   4 * Copyright (c) 2008 Nissin Systems Co., Ltd.,  Yoshio Kashiwagi
   5 * Copyright (c) 2005-2008 DLA Systems,  David H. Lynch Jr. <dhlii@dlasys.net>
   6 * Copyright (c) 2008-2009 Secret Lab Technologies Ltd.
   7 *
   8 * This is a driver for the Xilinx ll_temac ipcore which is often used
   9 * in the Virtex and Spartan series of chips.
  10 *
  11 * Notes:
  12 * - The ll_temac hardware uses indirect access for many of the TEMAC
  13 *   registers, include the MDIO bus.  However, indirect access to MDIO
  14 *   registers take considerably more clock cycles than to TEMAC registers.
  15 *   MDIO accesses are long, so threads doing them should probably sleep
  16 *   rather than busywait.  However, since only one indirect access can be
  17 *   in progress at any given time, that means that *all* indirect accesses
  18 *   could end up sleeping (to wait for an MDIO access to complete).
  19 *   Fortunately none of the indirect accesses are on the 'hot' path for tx
  20 *   or rx, so this should be okay.
  21 *
  22 * TODO:
  23 * - Factor out locallink DMA code into separate driver
  24 * - Fix multicast assignment.
  25 * - Fix support for hardware checksumming.
  26 * - Testing.  Lots and lots of testing.
  27 *
  28 */
  29
  30#include <linux/delay.h>
  31#include <linux/etherdevice.h>
  32#include <linux/mii.h>
  33#include <linux/module.h>
  34#include <linux/mutex.h>
  35#include <linux/netdevice.h>
 
  36#include <linux/of.h>
  37#include <linux/of_device.h>
  38#include <linux/of_irq.h>
  39#include <linux/of_mdio.h>
 
  40#include <linux/of_platform.h>
  41#include <linux/of_address.h>
  42#include <linux/skbuff.h>
  43#include <linux/spinlock.h>
  44#include <linux/tcp.h>      /* needed for sizeof(tcphdr) */
  45#include <linux/udp.h>      /* needed for sizeof(udphdr) */
  46#include <linux/phy.h>
  47#include <linux/in.h>
  48#include <linux/io.h>
  49#include <linux/ip.h>
  50#include <linux/slab.h>
  51#include <linux/interrupt.h>
 
  52#include <linux/dma-mapping.h>
 
 
  53
  54#include "ll_temac.h"
  55
  56#define TX_BD_NUM   64
  57#define RX_BD_NUM   128
 
 
 
  58
  59/* ---------------------------------------------------------------------
  60 * Low level register access functions
  61 */
  62
  63u32 temac_ior(struct temac_local *lp, int offset)
  64{
  65	return in_be32((u32 *)(lp->regs + offset));
  66}
  67
  68void temac_iow(struct temac_local *lp, int offset, u32 value)
  69{
  70	out_be32((u32 *) (lp->regs + offset), value);
  71}
  72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  73int temac_indirect_busywait(struct temac_local *lp)
  74{
  75	long end = jiffies + 2;
 
 
 
 
  76
  77	while (!(temac_ior(lp, XTE_RDY0_OFFSET) & XTE_RDY0_HARD_ACS_RDY_MASK)) {
  78		if (end - jiffies <= 0) {
  79			WARN_ON(1);
  80			return -ETIMEDOUT;
  81		}
  82		msleep(1);
  83	}
  84	return 0;
  85}
  86
  87/**
  88 * temac_indirect_in32
  89 *
  90 * lp->indirect_mutex must be held when calling this function
  91 */
  92u32 temac_indirect_in32(struct temac_local *lp, int reg)
  93{
  94	u32 val;
 
 
 
 
 
 
 
  95
  96	if (temac_indirect_busywait(lp))
 
 
 
 
 
 
 
 
 
 
 
 
 
  97		return -ETIMEDOUT;
 
  98	temac_iow(lp, XTE_CTL0_OFFSET, reg);
  99	if (temac_indirect_busywait(lp))
 
 
 
 
 
 100		return -ETIMEDOUT;
 101	val = temac_ior(lp, XTE_LSW0_OFFSET);
 102
 103	return val;
 104}
 105
 106/**
 107 * temac_indirect_out32
 108 *
 109 * lp->indirect_mutex must be held when calling this function
 110 */
 111void temac_indirect_out32(struct temac_local *lp, int reg, u32 value)
 112{
 113	if (temac_indirect_busywait(lp))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 114		return;
 
 115	temac_iow(lp, XTE_LSW0_OFFSET, value);
 116	temac_iow(lp, XTE_CTL0_OFFSET, CNTLREG_WRITE_ENABLE_MASK | reg);
 117	temac_indirect_busywait(lp);
 
 
 
 
 118}
 119
 120/**
 121 * temac_dma_in32 - Memory mapped DMA read, this function expects a
 122 * register input that is based on DCR word addresses which
 123 * are then converted to memory mapped byte addresses
 
 124 */
 125static u32 temac_dma_in32(struct temac_local *lp, int reg)
 126{
 127	return in_be32((u32 *)(lp->sdma_regs + (reg << 2)));
 128}
 129
 130/**
 131 * temac_dma_out32 - Memory mapped DMA read, this function expects a
 132 * register input that is based on DCR word addresses which
 133 * are then converted to memory mapped byte addresses
 
 
 
 
 
 
 134 */
 135static void temac_dma_out32(struct temac_local *lp, int reg, u32 value)
 136{
 137	out_be32((u32 *)(lp->sdma_regs + (reg << 2)), value);
 
 
 
 
 
 138}
 139
 140/* DMA register access functions can be DCR based or memory mapped.
 141 * The PowerPC 440 is DCR based, the PowerPC 405 and MicroBlaze are both
 142 * memory mapped.
 143 */
 144#ifdef CONFIG_PPC_DCR
 145
 146/**
 147 * temac_dma_dcr_in32 - DCR based DMA read
 148 */
 149static u32 temac_dma_dcr_in(struct temac_local *lp, int reg)
 150{
 151	return dcr_read(lp->sdma_dcrs, reg);
 152}
 153
 154/**
 155 * temac_dma_dcr_out32 - DCR based DMA write
 156 */
 157static void temac_dma_dcr_out(struct temac_local *lp, int reg, u32 value)
 158{
 159	dcr_write(lp->sdma_dcrs, reg, value);
 160}
 161
 162/**
 163 * temac_dcr_setup - If the DMA is DCR based, then setup the address and
 164 * I/O  functions
 165 */
 166static int temac_dcr_setup(struct temac_local *lp, struct platform_device *op,
 167				struct device_node *np)
 168{
 169	unsigned int dcrs;
 170
 171	/* setup the dcr address mapping if it's in the device tree */
 172
 173	dcrs = dcr_resource_start(np, 0);
 174	if (dcrs != 0) {
 175		lp->sdma_dcrs = dcr_map(np, dcrs, dcr_resource_len(np, 0));
 176		lp->dma_in = temac_dma_dcr_in;
 177		lp->dma_out = temac_dma_dcr_out;
 178		dev_dbg(&op->dev, "DCR base: %x\n", dcrs);
 179		return 0;
 180	}
 181	/* no DCR in the device tree, indicate a failure */
 182	return -1;
 183}
 184
 185#else
 186
 187/*
 188 * temac_dcr_setup - This is a stub for when DCR is not supported,
 189 * such as with MicroBlaze
 190 */
 191static int temac_dcr_setup(struct temac_local *lp, struct platform_device *op,
 192				struct device_node *np)
 193{
 194	return -1;
 195}
 196
 197#endif
 198
 199/**
 200 * temac_dma_bd_release - Release buffer descriptor rings
 201 */
 202static void temac_dma_bd_release(struct net_device *ndev)
 203{
 204	struct temac_local *lp = netdev_priv(ndev);
 205	int i;
 206
 207	/* Reset Local Link (DMA) */
 208	lp->dma_out(lp, DMA_CONTROL_REG, DMA_CONTROL_RST);
 209
 210	for (i = 0; i < RX_BD_NUM; i++) {
 211		if (!lp->rx_skb[i])
 212			break;
 213		else {
 214			dma_unmap_single(ndev->dev.parent, lp->rx_bd_v[i].phys,
 215					XTE_MAX_JUMBO_FRAME_SIZE, DMA_FROM_DEVICE);
 216			dev_kfree_skb(lp->rx_skb[i]);
 217		}
 218	}
 219	if (lp->rx_bd_v)
 220		dma_free_coherent(ndev->dev.parent,
 221				sizeof(*lp->rx_bd_v) * RX_BD_NUM,
 222				lp->rx_bd_v, lp->rx_bd_p);
 223	if (lp->tx_bd_v)
 224		dma_free_coherent(ndev->dev.parent,
 225				sizeof(*lp->tx_bd_v) * TX_BD_NUM,
 226				lp->tx_bd_v, lp->tx_bd_p);
 227	if (lp->rx_skb)
 228		kfree(lp->rx_skb);
 229}
 230
 231/**
 232 * temac_dma_bd_init - Setup buffer descriptor rings
 233 */
 234static int temac_dma_bd_init(struct net_device *ndev)
 235{
 236	struct temac_local *lp = netdev_priv(ndev);
 237	struct sk_buff *skb;
 
 238	int i;
 239
 240	lp->rx_skb = kcalloc(RX_BD_NUM, sizeof(*lp->rx_skb), GFP_KERNEL);
 
 241	if (!lp->rx_skb)
 242		goto out;
 243
 244	/* allocate the tx and rx ring buffer descriptors. */
 245	/* returns a virtual address and a physical address. */
 246	lp->tx_bd_v = dma_zalloc_coherent(ndev->dev.parent,
 247					  sizeof(*lp->tx_bd_v) * TX_BD_NUM,
 248					  &lp->tx_bd_p, GFP_KERNEL);
 249	if (!lp->tx_bd_v)
 250		goto out;
 251
 252	lp->rx_bd_v = dma_zalloc_coherent(ndev->dev.parent,
 253					  sizeof(*lp->rx_bd_v) * RX_BD_NUM,
 254					  &lp->rx_bd_p, GFP_KERNEL);
 255	if (!lp->rx_bd_v)
 256		goto out;
 257
 258	for (i = 0; i < TX_BD_NUM; i++) {
 259		lp->tx_bd_v[i].next = lp->tx_bd_p +
 260				sizeof(*lp->tx_bd_v) * ((i + 1) % TX_BD_NUM);
 261	}
 262
 263	for (i = 0; i < RX_BD_NUM; i++) {
 264		lp->rx_bd_v[i].next = lp->rx_bd_p +
 265				sizeof(*lp->rx_bd_v) * ((i + 1) % RX_BD_NUM);
 266
 267		skb = netdev_alloc_skb_ip_align(ndev,
 268						XTE_MAX_JUMBO_FRAME_SIZE);
 
 269		if (!skb)
 270			goto out;
 271
 272		lp->rx_skb[i] = skb;
 273		/* returns physical address of skb->data */
 274		lp->rx_bd_v[i].phys = dma_map_single(ndev->dev.parent,
 275						     skb->data,
 276						     XTE_MAX_JUMBO_FRAME_SIZE,
 277						     DMA_FROM_DEVICE);
 278		lp->rx_bd_v[i].len = XTE_MAX_JUMBO_FRAME_SIZE;
 279		lp->rx_bd_v[i].app0 = STS_CTRL_APP0_IRQONEND;
 280	}
 281
 282	lp->dma_out(lp, TX_CHNL_CTRL, 0x10220400 |
 283					  CHNL_CTRL_IRQ_EN |
 284					  CHNL_CTRL_IRQ_DLY_EN |
 285					  CHNL_CTRL_IRQ_COAL_EN);
 286	/* 0x10220483 */
 287	/* 0x00100483 */
 288	lp->dma_out(lp, RX_CHNL_CTRL, 0xff070000 |
 289					  CHNL_CTRL_IRQ_EN |
 290					  CHNL_CTRL_IRQ_DLY_EN |
 291					  CHNL_CTRL_IRQ_COAL_EN |
 292					  CHNL_CTRL_IRQ_IOE);
 293	/* 0xff010283 */
 294
 295	lp->dma_out(lp, RX_CURDESC_PTR,  lp->rx_bd_p);
 296	lp->dma_out(lp, RX_TAILDESC_PTR,
 297		       lp->rx_bd_p + (sizeof(*lp->rx_bd_v) * (RX_BD_NUM - 1)));
 298	lp->dma_out(lp, TX_CURDESC_PTR, lp->tx_bd_p);
 299
 300	/* Init descriptor indexes */
 301	lp->tx_bd_ci = 0;
 302	lp->tx_bd_next = 0;
 303	lp->tx_bd_tail = 0;
 304	lp->rx_bd_ci = 0;
 
 
 
 
 
 
 
 
 
 
 305
 306	return 0;
 307
 308out:
 309	temac_dma_bd_release(ndev);
 310	return -ENOMEM;
 311}
 312
 313/* ---------------------------------------------------------------------
 314 * net_device_ops
 315 */
 316
 317static void temac_do_set_mac_address(struct net_device *ndev)
 318{
 319	struct temac_local *lp = netdev_priv(ndev);
 
 320
 321	/* set up unicast MAC address filter set its mac address */
 322	mutex_lock(&lp->indirect_mutex);
 323	temac_indirect_out32(lp, XTE_UAW0_OFFSET,
 324			     (ndev->dev_addr[0]) |
 325			     (ndev->dev_addr[1] << 8) |
 326			     (ndev->dev_addr[2] << 16) |
 327			     (ndev->dev_addr[3] << 24));
 328	/* There are reserved bits in EUAW1
 329	 * so don't affect them Set MAC bits [47:32] in EUAW1 */
 330	temac_indirect_out32(lp, XTE_UAW1_OFFSET,
 331			     (ndev->dev_addr[4] & 0x000000ff) |
 332			     (ndev->dev_addr[5] << 8));
 333	mutex_unlock(&lp->indirect_mutex);
 
 334}
 335
 336static int temac_init_mac_address(struct net_device *ndev, void *address)
 337{
 338	memcpy(ndev->dev_addr, address, ETH_ALEN);
 339	if (!is_valid_ether_addr(ndev->dev_addr))
 340		eth_hw_addr_random(ndev);
 341	temac_do_set_mac_address(ndev);
 342	return 0;
 343}
 344
 345static int temac_set_mac_address(struct net_device *ndev, void *p)
 346{
 347	struct sockaddr *addr = p;
 348
 349	if (!is_valid_ether_addr(addr->sa_data))
 350		return -EADDRNOTAVAIL;
 351	memcpy(ndev->dev_addr, addr->sa_data, ETH_ALEN);
 352	temac_do_set_mac_address(ndev);
 353	return 0;
 354}
 355
 356static void temac_set_multicast_list(struct net_device *ndev)
 357{
 358	struct temac_local *lp = netdev_priv(ndev);
 359	u32 multi_addr_msw, multi_addr_lsw, val;
 360	int i;
 
 
 361
 362	mutex_lock(&lp->indirect_mutex);
 363	if (ndev->flags & (IFF_ALLMULTI | IFF_PROMISC) ||
 364	    netdev_mc_count(ndev) > MULTICAST_CAM_TABLE_NUM) {
 365		/*
 366		 *	We must make the kernel realise we had to move
 367		 *	into promisc mode or we start all out war on
 368		 *	the cable. If it was a promisc request the
 369		 *	flag is already set. If not we assert it.
 370		 */
 371		ndev->flags |= IFF_PROMISC;
 372		temac_indirect_out32(lp, XTE_AFM_OFFSET, XTE_AFM_EPPRM_MASK);
 373		dev_info(&ndev->dev, "Promiscuous mode enabled.\n");
 374	} else if (!netdev_mc_empty(ndev)) {
 
 
 
 
 
 375		struct netdev_hw_addr *ha;
 376
 377		i = 0;
 378		netdev_for_each_mc_addr(ha, ndev) {
 379			if (i >= MULTICAST_CAM_TABLE_NUM)
 380				break;
 381			multi_addr_msw = ((ha->addr[3] << 24) |
 382					  (ha->addr[2] << 16) |
 383					  (ha->addr[1] << 8) |
 384					  (ha->addr[0]));
 385			temac_indirect_out32(lp, XTE_MAW0_OFFSET,
 386					     multi_addr_msw);
 387			multi_addr_lsw = ((ha->addr[5] << 8) |
 388					  (ha->addr[4]) | (i << 16));
 389			temac_indirect_out32(lp, XTE_MAW1_OFFSET,
 390					     multi_addr_lsw);
 391			i++;
 392		}
 393	} else {
 394		val = temac_indirect_in32(lp, XTE_AFM_OFFSET);
 395		temac_indirect_out32(lp, XTE_AFM_OFFSET,
 396				     val & ~XTE_AFM_EPPRM_MASK);
 397		temac_indirect_out32(lp, XTE_MAW0_OFFSET, 0);
 398		temac_indirect_out32(lp, XTE_MAW1_OFFSET, 0);
 399		dev_info(&ndev->dev, "Promiscuous mode disabled.\n");
 400	}
 401	mutex_unlock(&lp->indirect_mutex);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 402}
 403
 404struct temac_option {
 405	int flg;
 406	u32 opt;
 407	u32 reg;
 408	u32 m_or;
 409	u32 m_and;
 410} temac_options[] = {
 411	/* Turn on jumbo packet support for both Rx and Tx */
 412	{
 413		.opt = XTE_OPTION_JUMBO,
 414		.reg = XTE_TXC_OFFSET,
 415		.m_or = XTE_TXC_TXJMBO_MASK,
 416	},
 417	{
 418		.opt = XTE_OPTION_JUMBO,
 419		.reg = XTE_RXC1_OFFSET,
 420		.m_or =XTE_RXC1_RXJMBO_MASK,
 421	},
 422	/* Turn on VLAN packet support for both Rx and Tx */
 423	{
 424		.opt = XTE_OPTION_VLAN,
 425		.reg = XTE_TXC_OFFSET,
 426		.m_or =XTE_TXC_TXVLAN_MASK,
 427	},
 428	{
 429		.opt = XTE_OPTION_VLAN,
 430		.reg = XTE_RXC1_OFFSET,
 431		.m_or =XTE_RXC1_RXVLAN_MASK,
 432	},
 433	/* Turn on FCS stripping on receive packets */
 434	{
 435		.opt = XTE_OPTION_FCS_STRIP,
 436		.reg = XTE_RXC1_OFFSET,
 437		.m_or =XTE_RXC1_RXFCS_MASK,
 438	},
 439	/* Turn on FCS insertion on transmit packets */
 440	{
 441		.opt = XTE_OPTION_FCS_INSERT,
 442		.reg = XTE_TXC_OFFSET,
 443		.m_or =XTE_TXC_TXFCS_MASK,
 444	},
 445	/* Turn on length/type field checking on receive packets */
 446	{
 447		.opt = XTE_OPTION_LENTYPE_ERR,
 448		.reg = XTE_RXC1_OFFSET,
 449		.m_or =XTE_RXC1_RXLT_MASK,
 450	},
 451	/* Turn on flow control */
 452	{
 453		.opt = XTE_OPTION_FLOW_CONTROL,
 454		.reg = XTE_FCC_OFFSET,
 455		.m_or =XTE_FCC_RXFLO_MASK,
 456	},
 457	/* Turn on flow control */
 458	{
 459		.opt = XTE_OPTION_FLOW_CONTROL,
 460		.reg = XTE_FCC_OFFSET,
 461		.m_or =XTE_FCC_TXFLO_MASK,
 462	},
 463	/* Turn on promiscuous frame filtering (all frames are received ) */
 464	{
 465		.opt = XTE_OPTION_PROMISC,
 466		.reg = XTE_AFM_OFFSET,
 467		.m_or =XTE_AFM_EPPRM_MASK,
 468	},
 469	/* Enable transmitter if not already enabled */
 470	{
 471		.opt = XTE_OPTION_TXEN,
 472		.reg = XTE_TXC_OFFSET,
 473		.m_or =XTE_TXC_TXEN_MASK,
 474	},
 475	/* Enable receiver? */
 476	{
 477		.opt = XTE_OPTION_RXEN,
 478		.reg = XTE_RXC1_OFFSET,
 479		.m_or =XTE_RXC1_RXEN_MASK,
 480	},
 481	{}
 482};
 483
 484/**
 485 * temac_setoptions
 486 */
 487static u32 temac_setoptions(struct net_device *ndev, u32 options)
 488{
 489	struct temac_local *lp = netdev_priv(ndev);
 490	struct temac_option *tp = &temac_options[0];
 491	int reg;
 
 492
 493	mutex_lock(&lp->indirect_mutex);
 494	while (tp->opt) {
 495		reg = temac_indirect_in32(lp, tp->reg) & ~tp->m_or;
 496		if (options & tp->opt)
 497			reg |= tp->m_or;
 498		temac_indirect_out32(lp, tp->reg, reg);
 
 499		tp++;
 500	}
 
 501	lp->options |= options;
 502	mutex_unlock(&lp->indirect_mutex);
 503
 504	return 0;
 505}
 506
 507/* Initialize temac */
 508static void temac_device_reset(struct net_device *ndev)
 509{
 510	struct temac_local *lp = netdev_priv(ndev);
 511	u32 timeout;
 512	u32 val;
 
 513
 514	/* Perform a software reset */
 515
 516	/* 0x300 host enable bit ? */
 517	/* reset PHY through control register ?:1 */
 518
 519	dev_dbg(&ndev->dev, "%s()\n", __func__);
 520
 521	mutex_lock(&lp->indirect_mutex);
 522	/* Reset the receiver and wait for it to finish reset */
 523	temac_indirect_out32(lp, XTE_RXC1_OFFSET, XTE_RXC1_RXRST_MASK);
 524	timeout = 1000;
 525	while (temac_indirect_in32(lp, XTE_RXC1_OFFSET) & XTE_RXC1_RXRST_MASK) {
 526		udelay(1);
 527		if (--timeout == 0) {
 528			dev_err(&ndev->dev,
 529				"temac_device_reset RX reset timeout!!\n");
 530			break;
 531		}
 532	}
 533
 534	/* Reset the transmitter and wait for it to finish reset */
 535	temac_indirect_out32(lp, XTE_TXC_OFFSET, XTE_TXC_TXRST_MASK);
 536	timeout = 1000;
 537	while (temac_indirect_in32(lp, XTE_TXC_OFFSET) & XTE_TXC_TXRST_MASK) {
 538		udelay(1);
 539		if (--timeout == 0) {
 540			dev_err(&ndev->dev,
 541				"temac_device_reset TX reset timeout!!\n");
 542			break;
 543		}
 544	}
 545
 546	/* Disable the receiver */
 547	val = temac_indirect_in32(lp, XTE_RXC1_OFFSET);
 548	temac_indirect_out32(lp, XTE_RXC1_OFFSET, val & ~XTE_RXC1_RXEN_MASK);
 
 
 
 549
 550	/* Reset Local Link (DMA) */
 551	lp->dma_out(lp, DMA_CONTROL_REG, DMA_CONTROL_RST);
 552	timeout = 1000;
 553	while (lp->dma_in(lp, DMA_CONTROL_REG) & DMA_CONTROL_RST) {
 554		udelay(1);
 555		if (--timeout == 0) {
 556			dev_err(&ndev->dev,
 557				"temac_device_reset DMA reset timeout!!\n");
 558			break;
 559		}
 560	}
 561	lp->dma_out(lp, DMA_CONTROL_REG, DMA_TAIL_ENABLE);
 562
 563	if (temac_dma_bd_init(ndev)) {
 564		dev_err(&ndev->dev,
 565				"temac_device_reset descriptor allocation failed\n");
 566	}
 567
 568	temac_indirect_out32(lp, XTE_RXC0_OFFSET, 0);
 569	temac_indirect_out32(lp, XTE_RXC1_OFFSET, 0);
 570	temac_indirect_out32(lp, XTE_TXC_OFFSET, 0);
 571	temac_indirect_out32(lp, XTE_FCC_OFFSET, XTE_FCC_RXFLO_MASK);
 572
 573	mutex_unlock(&lp->indirect_mutex);
 574
 575	/* Sync default options with HW
 576	 * but leave receiver and transmitter disabled.  */
 
 577	temac_setoptions(ndev,
 578			 lp->options & ~(XTE_OPTION_TXEN | XTE_OPTION_RXEN));
 579
 580	temac_do_set_mac_address(ndev);
 581
 582	/* Set address filter table */
 583	temac_set_multicast_list(ndev);
 584	if (temac_setoptions(ndev, lp->options))
 585		dev_err(&ndev->dev, "Error setting TEMAC options\n");
 586
 587	/* Init Driver variable */
 588	ndev->trans_start = jiffies; /* prevent tx timeout */
 589}
 590
 591void temac_adjust_link(struct net_device *ndev)
 592{
 593	struct temac_local *lp = netdev_priv(ndev);
 594	struct phy_device *phy = lp->phy_dev;
 595	u32 mii_speed;
 596	int link_state;
 
 597
 598	/* hash together the state values to decide if something has changed */
 599	link_state = phy->speed | (phy->duplex << 1) | phy->link;
 600
 601	mutex_lock(&lp->indirect_mutex);
 602	if (lp->last_link != link_state) {
 603		mii_speed = temac_indirect_in32(lp, XTE_EMCFG_OFFSET);
 
 604		mii_speed &= ~XTE_EMCFG_LINKSPD_MASK;
 605
 606		switch (phy->speed) {
 607		case SPEED_1000: mii_speed |= XTE_EMCFG_LINKSPD_1000; break;
 608		case SPEED_100: mii_speed |= XTE_EMCFG_LINKSPD_100; break;
 609		case SPEED_10: mii_speed |= XTE_EMCFG_LINKSPD_10; break;
 
 
 
 
 
 
 610		}
 611
 612		/* Write new speed setting out to TEMAC */
 613		temac_indirect_out32(lp, XTE_EMCFG_OFFSET, mii_speed);
 
 
 614		lp->last_link = link_state;
 615		phy_print_status(phy);
 616	}
 617	mutex_unlock(&lp->indirect_mutex);
 618}
 619
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 620static void temac_start_xmit_done(struct net_device *ndev)
 621{
 622	struct temac_local *lp = netdev_priv(ndev);
 623	struct cdmac_bd *cur_p;
 624	unsigned int stat = 0;
 
 625
 626	cur_p = &lp->tx_bd_v[lp->tx_bd_ci];
 627	stat = cur_p->app0;
 628
 629	while (stat & STS_CTRL_APP0_CMPLT) {
 630		dma_unmap_single(ndev->dev.parent, cur_p->phys, cur_p->len,
 631				 DMA_TO_DEVICE);
 632		if (cur_p->app4)
 633			dev_kfree_skb_irq((struct sk_buff *)cur_p->app4);
 634		cur_p->app0 = 0;
 
 
 
 
 635		cur_p->app1 = 0;
 636		cur_p->app2 = 0;
 637		cur_p->app3 = 0;
 638		cur_p->app4 = 0;
 639
 640		ndev->stats.tx_packets++;
 641		ndev->stats.tx_bytes += cur_p->len;
 
 
 
 
 
 
 642
 643		lp->tx_bd_ci++;
 644		if (lp->tx_bd_ci >= TX_BD_NUM)
 645			lp->tx_bd_ci = 0;
 646
 647		cur_p = &lp->tx_bd_v[lp->tx_bd_ci];
 648		stat = cur_p->app0;
 649	}
 650
 
 
 
 651	netif_wake_queue(ndev);
 652}
 653
 654static inline int temac_check_tx_bd_space(struct temac_local *lp, int num_frag)
 655{
 656	struct cdmac_bd *cur_p;
 657	int tail;
 658
 659	tail = lp->tx_bd_tail;
 660	cur_p = &lp->tx_bd_v[tail];
 661
 662	do {
 663		if (cur_p->app0)
 664			return NETDEV_TX_BUSY;
 665
 
 
 
 666		tail++;
 667		if (tail >= TX_BD_NUM)
 668			tail = 0;
 669
 670		cur_p = &lp->tx_bd_v[tail];
 671		num_frag--;
 672	} while (num_frag >= 0);
 673
 674	return 0;
 675}
 676
 677static int temac_start_xmit(struct sk_buff *skb, struct net_device *ndev)
 
 678{
 679	struct temac_local *lp = netdev_priv(ndev);
 680	struct cdmac_bd *cur_p;
 681	dma_addr_t start_p, tail_p;
 682	int ii;
 683	unsigned long num_frag;
 684	skb_frag_t *frag;
 685
 686	num_frag = skb_shinfo(skb)->nr_frags;
 687	frag = &skb_shinfo(skb)->frags[0];
 688	start_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * lp->tx_bd_tail;
 689	cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
 690
 691	if (temac_check_tx_bd_space(lp, num_frag)) {
 692		if (!netif_queue_stopped(ndev)) {
 693			netif_stop_queue(ndev);
 694			return NETDEV_TX_BUSY;
 695		}
 696		return NETDEV_TX_BUSY;
 
 
 
 
 
 
 
 
 
 697	}
 698
 699	cur_p->app0 = 0;
 700	if (skb->ip_summed == CHECKSUM_PARTIAL) {
 701		unsigned int csum_start_off = skb_checksum_start_offset(skb);
 702		unsigned int csum_index_off = csum_start_off + skb->csum_offset;
 703
 704		cur_p->app0 |= 1; /* TX Checksum Enabled */
 705		cur_p->app1 = (csum_start_off << 16) | csum_index_off;
 
 706		cur_p->app2 = 0;  /* initial checksum seed */
 707	}
 708
 709	cur_p->app0 |= STS_CTRL_APP0_SOP;
 710	cur_p->len = skb_headlen(skb);
 711	cur_p->phys = dma_map_single(ndev->dev.parent, skb->data, skb->len,
 712				     DMA_TO_DEVICE);
 713	cur_p->app4 = (unsigned long)skb;
 
 
 
 
 
 714
 715	for (ii = 0; ii < num_frag; ii++) {
 716		lp->tx_bd_tail++;
 717		if (lp->tx_bd_tail >= TX_BD_NUM)
 718			lp->tx_bd_tail = 0;
 719
 720		cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
 721		cur_p->phys = dma_map_single(ndev->dev.parent,
 722					     skb_frag_address(frag),
 723					     skb_frag_size(frag), DMA_TO_DEVICE);
 724		cur_p->len = skb_frag_size(frag);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 725		cur_p->app0 = 0;
 726		frag++;
 727	}
 728	cur_p->app0 |= STS_CTRL_APP0_EOP;
 
 
 
 
 
 729
 730	tail_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * lp->tx_bd_tail;
 731	lp->tx_bd_tail++;
 732	if (lp->tx_bd_tail >= TX_BD_NUM)
 733		lp->tx_bd_tail = 0;
 734
 735	skb_tx_timestamp(skb);
 736
 737	/* Kick off the transfer */
 
 738	lp->dma_out(lp, TX_TAILDESC_PTR, tail_p); /* DMA start */
 739
 
 
 
 740	return NETDEV_TX_OK;
 741}
 742
 
 
 
 
 
 
 
 
 
 
 
 743
 744static void ll_temac_recv(struct net_device *ndev)
 745{
 746	struct temac_local *lp = netdev_priv(ndev);
 747	struct sk_buff *skb, *new_skb;
 748	unsigned int bdstat;
 749	struct cdmac_bd *cur_p;
 750	dma_addr_t tail_p;
 751	int length;
 752	unsigned long flags;
 
 
 753
 754	spin_lock_irqsave(&lp->rx_lock, flags);
 755
 756	tail_p = lp->rx_bd_p + sizeof(*lp->rx_bd_v) * lp->rx_bd_ci;
 757	cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
 758
 759	bdstat = cur_p->app0;
 760	while ((bdstat & STS_CTRL_APP0_CMPLT)) {
 
 
 
 
 
 
 
 
 
 
 
 
 761
 762		skb = lp->rx_skb[lp->rx_bd_ci];
 763		length = cur_p->app4 & 0x3FFF;
 
 764
 765		dma_unmap_single(ndev->dev.parent, cur_p->phys, length,
 766				 DMA_FROM_DEVICE);
 
 
 
 767
 
 768		skb_put(skb, length);
 769		skb->protocol = eth_type_trans(skb, ndev);
 770		skb_checksum_none_assert(skb);
 771
 772		/* if we're doing rx csum offload, set it up */
 773		if (((lp->temac_features & TEMAC_FEATURE_RX_CSUM) != 0) &&
 774		    (skb->protocol == htons(ETH_P_IP)) &&
 775		    (skb->len > 64)) {
 776
 777			skb->csum = cur_p->app3 & 0xFFFF;
 
 
 
 
 778			skb->ip_summed = CHECKSUM_COMPLETE;
 779		}
 780
 781		if (!skb_defer_rx_timestamp(skb))
 782			netif_rx(skb);
 
 
 783
 784		ndev->stats.rx_packets++;
 785		ndev->stats.rx_bytes += length;
 786
 787		new_skb = netdev_alloc_skb_ip_align(ndev,
 788						XTE_MAX_JUMBO_FRAME_SIZE);
 789		if (!new_skb) {
 790			spin_unlock_irqrestore(&lp->rx_lock, flags);
 791			return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 792		}
 793
 794		cur_p->app0 = STS_CTRL_APP0_IRQONEND;
 795		cur_p->phys = dma_map_single(ndev->dev.parent, new_skb->data,
 796					     XTE_MAX_JUMBO_FRAME_SIZE,
 797					     DMA_FROM_DEVICE);
 798		cur_p->len = XTE_MAX_JUMBO_FRAME_SIZE;
 799		lp->rx_skb[lp->rx_bd_ci] = new_skb;
 
 
 800
 801		lp->rx_bd_ci++;
 802		if (lp->rx_bd_ci >= RX_BD_NUM)
 803			lp->rx_bd_ci = 0;
 
 
 
 
 
 804
 805		cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
 806		bdstat = cur_p->app0;
 
 
 807	}
 808	lp->dma_out(lp, RX_TAILDESC_PTR, tail_p);
 809
 810	spin_unlock_irqrestore(&lp->rx_lock, flags);
 811}
 812
 
 
 
 
 
 
 
 
 
 
 
 
 813static irqreturn_t ll_temac_tx_irq(int irq, void *_ndev)
 814{
 815	struct net_device *ndev = _ndev;
 816	struct temac_local *lp = netdev_priv(ndev);
 817	unsigned int status;
 818
 819	status = lp->dma_in(lp, TX_IRQ_REG);
 820	lp->dma_out(lp, TX_IRQ_REG, status);
 821
 822	if (status & (IRQ_COAL | IRQ_DLY))
 823		temac_start_xmit_done(lp->ndev);
 824	if (status & 0x080)
 825		dev_err(&ndev->dev, "DMA error 0x%x\n", status);
 
 
 826
 827	return IRQ_HANDLED;
 828}
 829
 830static irqreturn_t ll_temac_rx_irq(int irq, void *_ndev)
 831{
 832	struct net_device *ndev = _ndev;
 833	struct temac_local *lp = netdev_priv(ndev);
 834	unsigned int status;
 835
 836	/* Read and clear the status registers */
 837	status = lp->dma_in(lp, RX_IRQ_REG);
 838	lp->dma_out(lp, RX_IRQ_REG, status);
 839
 840	if (status & (IRQ_COAL | IRQ_DLY))
 841		ll_temac_recv(lp->ndev);
 
 
 
 
 842
 843	return IRQ_HANDLED;
 844}
 845
 846static int temac_open(struct net_device *ndev)
 847{
 848	struct temac_local *lp = netdev_priv(ndev);
 
 849	int rc;
 850
 851	dev_dbg(&ndev->dev, "temac_open()\n");
 852
 853	if (lp->phy_node) {
 854		lp->phy_dev = of_phy_connect(lp->ndev, lp->phy_node,
 855					     temac_adjust_link, 0, 0);
 856		if (!lp->phy_dev) {
 857			dev_err(lp->dev, "of_phy_connect() failed\n");
 858			return -ENODEV;
 859		}
 860
 861		phy_start(lp->phy_dev);
 
 
 
 
 
 
 
 862	}
 863
 864	temac_device_reset(ndev);
 865
 866	rc = request_irq(lp->tx_irq, ll_temac_tx_irq, 0, ndev->name, ndev);
 867	if (rc)
 868		goto err_tx_irq;
 869	rc = request_irq(lp->rx_irq, ll_temac_rx_irq, 0, ndev->name, ndev);
 870	if (rc)
 871		goto err_rx_irq;
 872
 873	return 0;
 874
 875 err_rx_irq:
 876	free_irq(lp->tx_irq, ndev);
 877 err_tx_irq:
 878	if (lp->phy_dev)
 879		phy_disconnect(lp->phy_dev);
 880	lp->phy_dev = NULL;
 881	dev_err(lp->dev, "request_irq() failed\n");
 882	return rc;
 883}
 884
 885static int temac_stop(struct net_device *ndev)
 886{
 887	struct temac_local *lp = netdev_priv(ndev);
 
 888
 889	dev_dbg(&ndev->dev, "temac_close()\n");
 890
 
 
 891	free_irq(lp->tx_irq, ndev);
 892	free_irq(lp->rx_irq, ndev);
 893
 894	if (lp->phy_dev)
 895		phy_disconnect(lp->phy_dev);
 896	lp->phy_dev = NULL;
 897
 898	temac_dma_bd_release(ndev);
 899
 900	return 0;
 901}
 902
 903#ifdef CONFIG_NET_POLL_CONTROLLER
 904static void
 905temac_poll_controller(struct net_device *ndev)
 906{
 907	struct temac_local *lp = netdev_priv(ndev);
 908
 909	disable_irq(lp->tx_irq);
 910	disable_irq(lp->rx_irq);
 911
 912	ll_temac_rx_irq(lp->tx_irq, ndev);
 913	ll_temac_tx_irq(lp->rx_irq, ndev);
 914
 915	enable_irq(lp->tx_irq);
 916	enable_irq(lp->rx_irq);
 917}
 918#endif
 919
 920static int temac_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
 921{
 922	struct temac_local *lp = netdev_priv(ndev);
 923
 924	if (!netif_running(ndev))
 925		return -EINVAL;
 926
 927	if (!lp->phy_dev)
 928		return -EINVAL;
 929
 930	return phy_mii_ioctl(lp->phy_dev, rq, cmd);
 931}
 932
 933static const struct net_device_ops temac_netdev_ops = {
 934	.ndo_open = temac_open,
 935	.ndo_stop = temac_stop,
 936	.ndo_start_xmit = temac_start_xmit,
 
 937	.ndo_set_mac_address = temac_set_mac_address,
 938	.ndo_validate_addr = eth_validate_addr,
 939	.ndo_do_ioctl = temac_ioctl,
 940#ifdef CONFIG_NET_POLL_CONTROLLER
 941	.ndo_poll_controller = temac_poll_controller,
 942#endif
 943};
 944
 945/* ---------------------------------------------------------------------
 946 * SYSFS device attributes
 947 */
 948static ssize_t temac_show_llink_regs(struct device *dev,
 949				     struct device_attribute *attr, char *buf)
 950{
 951	struct net_device *ndev = dev_get_drvdata(dev);
 952	struct temac_local *lp = netdev_priv(ndev);
 953	int i, len = 0;
 954
 955	for (i = 0; i < 0x11; i++)
 956		len += sprintf(buf + len, "%.8x%s", lp->dma_in(lp, i),
 957			       (i % 8) == 7 ? "\n" : " ");
 958	len += sprintf(buf + len, "\n");
 959
 960	return len;
 961}
 962
 963static DEVICE_ATTR(llink_regs, 0440, temac_show_llink_regs, NULL);
 964
 965static struct attribute *temac_device_attrs[] = {
 966	&dev_attr_llink_regs.attr,
 967	NULL,
 968};
 969
 970static const struct attribute_group temac_attr_group = {
 971	.attrs = temac_device_attrs,
 972};
 973
 974/* ethtool support */
 975static int temac_get_settings(struct net_device *ndev, struct ethtool_cmd *cmd)
 
 
 
 
 
 
 
 976{
 977	struct temac_local *lp = netdev_priv(ndev);
 978	return phy_ethtool_gset(lp->phy_dev, cmd);
 
 
 
 
 
 
 
 
 979}
 980
 981static int temac_set_settings(struct net_device *ndev, struct ethtool_cmd *cmd)
 
 
 
 
 982{
 983	struct temac_local *lp = netdev_priv(ndev);
 984	return phy_ethtool_sset(lp->phy_dev, cmd);
 
 
 
 
 
 
 
 
 
 
 
 
 985}
 986
 987static int temac_nway_reset(struct net_device *ndev)
 
 
 
 
 988{
 989	struct temac_local *lp = netdev_priv(ndev);
 990	return phy_start_aneg(lp->phy_dev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 991}
 992
 993static const struct ethtool_ops temac_ethtool_ops = {
 994	.get_settings = temac_get_settings,
 995	.set_settings = temac_set_settings,
 996	.nway_reset = temac_nway_reset,
 997	.get_link = ethtool_op_get_link,
 998	.get_ts_info = ethtool_op_get_ts_info,
 
 
 
 
 
 
 999};
1000
1001static int temac_of_probe(struct platform_device *op)
1002{
1003	struct device_node *np;
 
1004	struct temac_local *lp;
1005	struct net_device *ndev;
1006	const void *addr;
1007	__be32 *p;
1008	int size, rc = 0;
 
1009
1010	/* Init network device structure */
1011	ndev = alloc_etherdev(sizeof(*lp));
1012	if (!ndev)
1013		return -ENOMEM;
1014
1015	ether_setup(ndev);
1016	platform_set_drvdata(op, ndev);
1017	SET_NETDEV_DEV(ndev, &op->dev);
1018	ndev->flags &= ~IFF_MULTICAST;  /* clear multicast */
1019	ndev->features = NETIF_F_SG;
1020	ndev->netdev_ops = &temac_netdev_ops;
1021	ndev->ethtool_ops = &temac_ethtool_ops;
1022#if 0
1023	ndev->features |= NETIF_F_IP_CSUM; /* Can checksum TCP/UDP over IPv4. */
1024	ndev->features |= NETIF_F_HW_CSUM; /* Can checksum all the packets. */
1025	ndev->features |= NETIF_F_IPV6_CSUM; /* Can checksum IPV6 TCP/UDP */
1026	ndev->features |= NETIF_F_HIGHDMA; /* Can DMA to high memory. */
1027	ndev->features |= NETIF_F_HW_VLAN_CTAG_TX; /* Transmit VLAN hw accel */
1028	ndev->features |= NETIF_F_HW_VLAN_CTAG_RX; /* Receive VLAN hw acceleration */
1029	ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; /* Receive VLAN filtering */
1030	ndev->features |= NETIF_F_VLAN_CHALLENGED; /* cannot handle VLAN pkts */
1031	ndev->features |= NETIF_F_GSO; /* Enable software GSO. */
1032	ndev->features |= NETIF_F_MULTI_QUEUE; /* Has multiple TX/RX queues */
1033	ndev->features |= NETIF_F_LRO; /* large receive offload */
1034#endif
1035
1036	/* setup temac private info structure */
1037	lp = netdev_priv(ndev);
1038	lp->ndev = ndev;
1039	lp->dev = &op->dev;
1040	lp->options = XTE_OPTION_DEFAULTS;
 
 
1041	spin_lock_init(&lp->rx_lock);
1042	mutex_init(&lp->indirect_mutex);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1043
1044	/* map device registers */
1045	lp->regs = of_iomap(op->dev.of_node, 0);
1046	if (!lp->regs) {
1047		dev_err(&op->dev, "could not map temac regs.\n");
1048		goto nodev;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1049	}
1050
1051	/* Setup checksum offload, but default to off if not specified */
1052	lp->temac_features = 0;
1053	p = (__be32 *)of_get_property(op->dev.of_node, "xlnx,txcsum", NULL);
1054	if (p && be32_to_cpu(*p)) {
1055		lp->temac_features |= TEMAC_FEATURE_TX_CSUM;
 
 
 
 
 
 
 
 
 
 
 
1056		/* Can checksum TCP/UDP over IPv4. */
1057		ndev->features |= NETIF_F_IP_CSUM;
1058	}
1059	p = (__be32 *)of_get_property(op->dev.of_node, "xlnx,rxcsum", NULL);
1060	if (p && be32_to_cpu(*p))
1061		lp->temac_features |= TEMAC_FEATURE_RX_CSUM;
1062
1063	/* Find the DMA node, map the DMA registers, and decode the DMA IRQs */
1064	np = of_parse_phandle(op->dev.of_node, "llink-connected", 0);
1065	if (!np) {
1066		dev_err(&op->dev, "could not find DMA node\n");
1067		goto err_iounmap;
1068	}
1069
1070	/* Setup the DMA register accesses, could be DCR or memory mapped */
1071	if (temac_dcr_setup(lp, op, np)) {
1072
1073		/* no DCR in the device tree, try non-DCR */
1074		lp->sdma_regs = of_iomap(np, 0);
1075		if (lp->sdma_regs) {
1076			lp->dma_in = temac_dma_in32;
1077			lp->dma_out = temac_dma_out32;
1078			dev_dbg(&op->dev, "MEM base: %p\n", lp->sdma_regs);
1079		} else {
1080			dev_err(&op->dev, "unable to map DMA registers\n");
1081			of_node_put(np);
1082			goto err_iounmap;
1083		}
1084	}
1085
1086	lp->rx_irq = irq_of_parse_and_map(np, 0);
1087	lp->tx_irq = irq_of_parse_and_map(np, 1);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1088
1089	of_node_put(np); /* Finished with the DMA node; drop the reference */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1090
1091	if (!lp->rx_irq || !lp->tx_irq) {
1092		dev_err(&op->dev, "could not determine irqs\n");
1093		rc = -ENOMEM;
1094		goto err_iounmap_2;
 
 
 
 
 
 
 
 
 
1095	}
1096
1097
1098	/* Retrieve the MAC address */
1099	addr = of_get_property(op->dev.of_node, "local-mac-address", &size);
1100	if ((!addr) || (size != 6)) {
1101		dev_err(&op->dev, "could not find MAC address\n");
1102		rc = -ENODEV;
1103		goto err_iounmap_2;
 
 
 
 
 
 
 
 
 
 
 
1104	}
1105	temac_init_mac_address(ndev, (void *)addr);
1106
1107	rc = temac_mdio_setup(lp, op->dev.of_node);
1108	if (rc)
1109		dev_warn(&op->dev, "error registering MDIO bus\n");
1110
1111	lp->phy_node = of_parse_phandle(op->dev.of_node, "phy-handle", 0);
1112	if (lp->phy_node)
1113		dev_dbg(lp->dev, "using PHY node %s (%p)\n", np->full_name, np);
 
 
 
 
 
 
1114
1115	/* Add the device attributes */
1116	rc = sysfs_create_group(&lp->dev->kobj, &temac_attr_group);
1117	if (rc) {
1118		dev_err(lp->dev, "Error creating sysfs files\n");
1119		goto err_iounmap_2;
1120	}
1121
1122	rc = register_netdev(lp->ndev);
1123	if (rc) {
1124		dev_err(lp->dev, "register_netdev() error (%i)\n", rc);
1125		goto err_register_ndev;
1126	}
1127
1128	return 0;
1129
1130 err_register_ndev:
1131	sysfs_remove_group(&lp->dev->kobj, &temac_attr_group);
1132 err_iounmap_2:
1133	if (lp->sdma_regs)
1134		iounmap(lp->sdma_regs);
1135 err_iounmap:
1136	iounmap(lp->regs);
1137 nodev:
1138	free_netdev(ndev);
1139	ndev = NULL;
1140	return rc;
1141}
1142
1143static int temac_of_remove(struct platform_device *op)
1144{
1145	struct net_device *ndev = platform_get_drvdata(op);
1146	struct temac_local *lp = netdev_priv(ndev);
1147
1148	temac_mdio_teardown(lp);
1149	unregister_netdev(ndev);
1150	sysfs_remove_group(&lp->dev->kobj, &temac_attr_group);
1151	if (lp->phy_node)
1152		of_node_put(lp->phy_node);
1153	lp->phy_node = NULL;
1154	iounmap(lp->regs);
1155	if (lp->sdma_regs)
1156		iounmap(lp->sdma_regs);
1157	free_netdev(ndev);
1158	return 0;
1159}
1160
1161static struct of_device_id temac_of_match[] = {
1162	{ .compatible = "xlnx,xps-ll-temac-1.01.b", },
1163	{ .compatible = "xlnx,xps-ll-temac-2.00.a", },
1164	{ .compatible = "xlnx,xps-ll-temac-2.02.a", },
1165	{ .compatible = "xlnx,xps-ll-temac-2.03.a", },
1166	{},
1167};
1168MODULE_DEVICE_TABLE(of, temac_of_match);
1169
1170static struct platform_driver temac_of_driver = {
1171	.probe = temac_of_probe,
1172	.remove = temac_of_remove,
1173	.driver = {
1174		.owner = THIS_MODULE,
1175		.name = "xilinx_temac",
1176		.of_match_table = temac_of_match,
1177	},
1178};
1179
1180module_platform_driver(temac_of_driver);
1181
1182MODULE_DESCRIPTION("Xilinx LL_TEMAC Ethernet driver");
1183MODULE_AUTHOR("Yoshio Kashiwagi");
1184MODULE_LICENSE("GPL");
v6.2
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Driver for Xilinx TEMAC Ethernet device
   4 *
   5 * Copyright (c) 2008 Nissin Systems Co., Ltd.,  Yoshio Kashiwagi
   6 * Copyright (c) 2005-2008 DLA Systems,  David H. Lynch Jr. <dhlii@dlasys.net>
   7 * Copyright (c) 2008-2009 Secret Lab Technologies Ltd.
   8 *
   9 * This is a driver for the Xilinx ll_temac ipcore which is often used
  10 * in the Virtex and Spartan series of chips.
  11 *
  12 * Notes:
  13 * - The ll_temac hardware uses indirect access for many of the TEMAC
  14 *   registers, include the MDIO bus.  However, indirect access to MDIO
  15 *   registers take considerably more clock cycles than to TEMAC registers.
  16 *   MDIO accesses are long, so threads doing them should probably sleep
  17 *   rather than busywait.  However, since only one indirect access can be
  18 *   in progress at any given time, that means that *all* indirect accesses
  19 *   could end up sleeping (to wait for an MDIO access to complete).
  20 *   Fortunately none of the indirect accesses are on the 'hot' path for tx
  21 *   or rx, so this should be okay.
  22 *
  23 * TODO:
  24 * - Factor out locallink DMA code into separate driver
 
  25 * - Fix support for hardware checksumming.
  26 * - Testing.  Lots and lots of testing.
  27 *
  28 */
  29
  30#include <linux/delay.h>
  31#include <linux/etherdevice.h>
  32#include <linux/mii.h>
  33#include <linux/module.h>
  34#include <linux/mutex.h>
  35#include <linux/netdevice.h>
  36#include <linux/if_ether.h>
  37#include <linux/of.h>
  38#include <linux/of_device.h>
  39#include <linux/of_irq.h>
  40#include <linux/of_mdio.h>
  41#include <linux/of_net.h>
  42#include <linux/of_platform.h>
  43#include <linux/of_address.h>
  44#include <linux/skbuff.h>
  45#include <linux/spinlock.h>
  46#include <linux/tcp.h>      /* needed for sizeof(tcphdr) */
  47#include <linux/udp.h>      /* needed for sizeof(udphdr) */
  48#include <linux/phy.h>
  49#include <linux/in.h>
  50#include <linux/io.h>
  51#include <linux/ip.h>
  52#include <linux/slab.h>
  53#include <linux/interrupt.h>
  54#include <linux/workqueue.h>
  55#include <linux/dma-mapping.h>
  56#include <linux/processor.h>
  57#include <linux/platform_data/xilinx-ll-temac.h>
  58
  59#include "ll_temac.h"
  60
  61/* Descriptors defines for Tx and Rx DMA */
  62#define TX_BD_NUM_DEFAULT		64
  63#define RX_BD_NUM_DEFAULT		1024
  64#define TX_BD_NUM_MAX			4096
  65#define RX_BD_NUM_MAX			4096
  66
  67/* ---------------------------------------------------------------------
  68 * Low level register access functions
  69 */
  70
  71static u32 _temac_ior_be(struct temac_local *lp, int offset)
  72{
  73	return ioread32be(lp->regs + offset);
  74}
  75
  76static void _temac_iow_be(struct temac_local *lp, int offset, u32 value)
  77{
  78	return iowrite32be(value, lp->regs + offset);
  79}
  80
  81static u32 _temac_ior_le(struct temac_local *lp, int offset)
  82{
  83	return ioread32(lp->regs + offset);
  84}
  85
  86static void _temac_iow_le(struct temac_local *lp, int offset, u32 value)
  87{
  88	return iowrite32(value, lp->regs + offset);
  89}
  90
  91static bool hard_acs_rdy(struct temac_local *lp)
  92{
  93	return temac_ior(lp, XTE_RDY0_OFFSET) & XTE_RDY0_HARD_ACS_RDY_MASK;
  94}
  95
  96static bool hard_acs_rdy_or_timeout(struct temac_local *lp, ktime_t timeout)
  97{
  98	ktime_t cur = ktime_get();
  99
 100	return hard_acs_rdy(lp) || ktime_after(cur, timeout);
 101}
 102
 103/* Poll for maximum 20 ms.  This is similar to the 2 jiffies @ 100 Hz
 104 * that was used before, and should cover MDIO bus speed down to 3200
 105 * Hz.
 106 */
 107#define HARD_ACS_RDY_POLL_NS (20 * NSEC_PER_MSEC)
 108
 109/*
 110 * temac_indirect_busywait - Wait for current indirect register access
 111 * to complete.
 112 */
 113int temac_indirect_busywait(struct temac_local *lp)
 114{
 115	ktime_t timeout = ktime_add_ns(ktime_get(), HARD_ACS_RDY_POLL_NS);
 116
 117	spin_until_cond(hard_acs_rdy_or_timeout(lp, timeout));
 118	if (WARN_ON(!hard_acs_rdy(lp)))
 119		return -ETIMEDOUT;
 120
 
 
 
 
 
 
 
 121	return 0;
 122}
 123
 124/*
 125 * temac_indirect_in32 - Indirect register read access.  This function
 126 * must be called without lp->indirect_lock being held.
 
 127 */
 128u32 temac_indirect_in32(struct temac_local *lp, int reg)
 129{
 130	unsigned long flags;
 131	int val;
 132
 133	spin_lock_irqsave(lp->indirect_lock, flags);
 134	val = temac_indirect_in32_locked(lp, reg);
 135	spin_unlock_irqrestore(lp->indirect_lock, flags);
 136	return val;
 137}
 138
 139/*
 140 * temac_indirect_in32_locked - Indirect register read access.  This
 141 * function must be called with lp->indirect_lock being held.  Use
 142 * this together with spin_lock_irqsave/spin_lock_irqrestore to avoid
 143 * repeated lock/unlock and to ensure uninterrupted access to indirect
 144 * registers.
 145 */
 146u32 temac_indirect_in32_locked(struct temac_local *lp, int reg)
 147{
 148	/* This initial wait should normally not spin, as we always
 149	 * try to wait for indirect access to complete before
 150	 * releasing the indirect_lock.
 151	 */
 152	if (WARN_ON(temac_indirect_busywait(lp)))
 153		return -ETIMEDOUT;
 154	/* Initiate read from indirect register */
 155	temac_iow(lp, XTE_CTL0_OFFSET, reg);
 156	/* Wait for indirect register access to complete.  We really
 157	 * should not see timeouts, and could even end up causing
 158	 * problem for following indirect access, so let's make a bit
 159	 * of WARN noise.
 160	 */
 161	if (WARN_ON(temac_indirect_busywait(lp)))
 162		return -ETIMEDOUT;
 163	/* Value is ready now */
 164	return temac_ior(lp, XTE_LSW0_OFFSET);
 
 165}
 166
 167/*
 168 * temac_indirect_out32 - Indirect register write access.  This function
 169 * must be called without lp->indirect_lock being held.
 
 170 */
 171void temac_indirect_out32(struct temac_local *lp, int reg, u32 value)
 172{
 173	unsigned long flags;
 174
 175	spin_lock_irqsave(lp->indirect_lock, flags);
 176	temac_indirect_out32_locked(lp, reg, value);
 177	spin_unlock_irqrestore(lp->indirect_lock, flags);
 178}
 179
 180/*
 181 * temac_indirect_out32_locked - Indirect register write access.  This
 182 * function must be called with lp->indirect_lock being held.  Use
 183 * this together with spin_lock_irqsave/spin_lock_irqrestore to avoid
 184 * repeated lock/unlock and to ensure uninterrupted access to indirect
 185 * registers.
 186 */
 187void temac_indirect_out32_locked(struct temac_local *lp, int reg, u32 value)
 188{
 189	/* As in temac_indirect_in32_locked(), we should normally not
 190	 * spin here.  And if it happens, we actually end up silently
 191	 * ignoring the write request.  Ouch.
 192	 */
 193	if (WARN_ON(temac_indirect_busywait(lp)))
 194		return;
 195	/* Initiate write to indirect register */
 196	temac_iow(lp, XTE_LSW0_OFFSET, value);
 197	temac_iow(lp, XTE_CTL0_OFFSET, CNTLREG_WRITE_ENABLE_MASK | reg);
 198	/* As in temac_indirect_in32_locked(), we should not see timeouts
 199	 * here.  And if it happens, we continue before the write has
 200	 * completed.  Not good.
 201	 */
 202	WARN_ON(temac_indirect_busywait(lp));
 203}
 204
 205/*
 206 * temac_dma_in32_* - Memory mapped DMA read, these function expects a
 207 * register input that is based on DCR word addresses which are then
 208 * converted to memory mapped byte addresses.  To be assigned to
 209 * lp->dma_in32.
 210 */
 211static u32 temac_dma_in32_be(struct temac_local *lp, int reg)
 212{
 213	return ioread32be(lp->sdma_regs + (reg << 2));
 214}
 215
 216static u32 temac_dma_in32_le(struct temac_local *lp, int reg)
 217{
 218	return ioread32(lp->sdma_regs + (reg << 2));
 219}
 220
 221/*
 222 * temac_dma_out32_* - Memory mapped DMA read, these function expects
 223 * a register input that is based on DCR word addresses which are then
 224 * converted to memory mapped byte addresses.  To be assigned to
 225 * lp->dma_out32.
 226 */
 227static void temac_dma_out32_be(struct temac_local *lp, int reg, u32 value)
 228{
 229	iowrite32be(value, lp->sdma_regs + (reg << 2));
 230}
 231
 232static void temac_dma_out32_le(struct temac_local *lp, int reg, u32 value)
 233{
 234	iowrite32(value, lp->sdma_regs + (reg << 2));
 235}
 236
 237/* DMA register access functions can be DCR based or memory mapped.
 238 * The PowerPC 440 is DCR based, the PowerPC 405 and MicroBlaze are both
 239 * memory mapped.
 240 */
 241#ifdef CONFIG_PPC_DCR
 242
 243/*
 244 * temac_dma_dcr_in32 - DCR based DMA read
 245 */
 246static u32 temac_dma_dcr_in(struct temac_local *lp, int reg)
 247{
 248	return dcr_read(lp->sdma_dcrs, reg);
 249}
 250
 251/*
 252 * temac_dma_dcr_out32 - DCR based DMA write
 253 */
 254static void temac_dma_dcr_out(struct temac_local *lp, int reg, u32 value)
 255{
 256	dcr_write(lp->sdma_dcrs, reg, value);
 257}
 258
 259/*
 260 * temac_dcr_setup - If the DMA is DCR based, then setup the address and
 261 * I/O  functions
 262 */
 263static int temac_dcr_setup(struct temac_local *lp, struct platform_device *op,
 264			   struct device_node *np)
 265{
 266	unsigned int dcrs;
 267
 268	/* setup the dcr address mapping if it's in the device tree */
 269
 270	dcrs = dcr_resource_start(np, 0);
 271	if (dcrs != 0) {
 272		lp->sdma_dcrs = dcr_map(np, dcrs, dcr_resource_len(np, 0));
 273		lp->dma_in = temac_dma_dcr_in;
 274		lp->dma_out = temac_dma_dcr_out;
 275		dev_dbg(&op->dev, "DCR base: %x\n", dcrs);
 276		return 0;
 277	}
 278	/* no DCR in the device tree, indicate a failure */
 279	return -1;
 280}
 281
 282#else
 283
 284/*
 285 * temac_dcr_setup - This is a stub for when DCR is not supported,
 286 * such as with MicroBlaze and x86
 287 */
 288static int temac_dcr_setup(struct temac_local *lp, struct platform_device *op,
 289			   struct device_node *np)
 290{
 291	return -1;
 292}
 293
 294#endif
 295
 296/*
 297 * temac_dma_bd_release - Release buffer descriptor rings
 298 */
 299static void temac_dma_bd_release(struct net_device *ndev)
 300{
 301	struct temac_local *lp = netdev_priv(ndev);
 302	int i;
 303
 304	/* Reset Local Link (DMA) */
 305	lp->dma_out(lp, DMA_CONTROL_REG, DMA_CONTROL_RST);
 306
 307	for (i = 0; i < lp->rx_bd_num; i++) {
 308		if (!lp->rx_skb[i])
 309			break;
 310		dma_unmap_single(ndev->dev.parent, lp->rx_bd_v[i].phys,
 311				 XTE_MAX_JUMBO_FRAME_SIZE, DMA_FROM_DEVICE);
 312		dev_kfree_skb(lp->rx_skb[i]);
 
 
 313	}
 314	if (lp->rx_bd_v)
 315		dma_free_coherent(ndev->dev.parent,
 316				  sizeof(*lp->rx_bd_v) * lp->rx_bd_num,
 317				  lp->rx_bd_v, lp->rx_bd_p);
 318	if (lp->tx_bd_v)
 319		dma_free_coherent(ndev->dev.parent,
 320				  sizeof(*lp->tx_bd_v) * lp->tx_bd_num,
 321				  lp->tx_bd_v, lp->tx_bd_p);
 
 
 322}
 323
 324/*
 325 * temac_dma_bd_init - Setup buffer descriptor rings
 326 */
 327static int temac_dma_bd_init(struct net_device *ndev)
 328{
 329	struct temac_local *lp = netdev_priv(ndev);
 330	struct sk_buff *skb;
 331	dma_addr_t skb_dma_addr;
 332	int i;
 333
 334	lp->rx_skb = devm_kcalloc(&ndev->dev, lp->rx_bd_num,
 335				  sizeof(*lp->rx_skb), GFP_KERNEL);
 336	if (!lp->rx_skb)
 337		goto out;
 338
 339	/* allocate the tx and rx ring buffer descriptors. */
 340	/* returns a virtual address and a physical address. */
 341	lp->tx_bd_v = dma_alloc_coherent(ndev->dev.parent,
 342					 sizeof(*lp->tx_bd_v) * lp->tx_bd_num,
 343					 &lp->tx_bd_p, GFP_KERNEL);
 344	if (!lp->tx_bd_v)
 345		goto out;
 346
 347	lp->rx_bd_v = dma_alloc_coherent(ndev->dev.parent,
 348					 sizeof(*lp->rx_bd_v) * lp->rx_bd_num,
 349					 &lp->rx_bd_p, GFP_KERNEL);
 350	if (!lp->rx_bd_v)
 351		goto out;
 352
 353	for (i = 0; i < lp->tx_bd_num; i++) {
 354		lp->tx_bd_v[i].next = cpu_to_be32(lp->tx_bd_p
 355			+ sizeof(*lp->tx_bd_v) * ((i + 1) % lp->tx_bd_num));
 356	}
 357
 358	for (i = 0; i < lp->rx_bd_num; i++) {
 359		lp->rx_bd_v[i].next = cpu_to_be32(lp->rx_bd_p
 360			+ sizeof(*lp->rx_bd_v) * ((i + 1) % lp->rx_bd_num));
 361
 362		skb = __netdev_alloc_skb_ip_align(ndev,
 363						  XTE_MAX_JUMBO_FRAME_SIZE,
 364						  GFP_KERNEL);
 365		if (!skb)
 366			goto out;
 367
 368		lp->rx_skb[i] = skb;
 369		/* returns physical address of skb->data */
 370		skb_dma_addr = dma_map_single(ndev->dev.parent, skb->data,
 371					      XTE_MAX_JUMBO_FRAME_SIZE,
 372					      DMA_FROM_DEVICE);
 373		if (dma_mapping_error(ndev->dev.parent, skb_dma_addr))
 374			goto out;
 375		lp->rx_bd_v[i].phys = cpu_to_be32(skb_dma_addr);
 376		lp->rx_bd_v[i].len = cpu_to_be32(XTE_MAX_JUMBO_FRAME_SIZE);
 377		lp->rx_bd_v[i].app0 = cpu_to_be32(STS_CTRL_APP0_IRQONEND);
 378	}
 379
 380	/* Configure DMA channel (irq setup) */
 381	lp->dma_out(lp, TX_CHNL_CTRL,
 382		    lp->coalesce_delay_tx << 24 | lp->coalesce_count_tx << 16 |
 383		    0x00000400 | // Use 1 Bit Wide Counters. Currently Not Used!
 384		    CHNL_CTRL_IRQ_EN | CHNL_CTRL_IRQ_ERR_EN |
 385		    CHNL_CTRL_IRQ_DLY_EN | CHNL_CTRL_IRQ_COAL_EN);
 386	lp->dma_out(lp, RX_CHNL_CTRL,
 387		    lp->coalesce_delay_rx << 24 | lp->coalesce_count_rx << 16 |
 388		    CHNL_CTRL_IRQ_IOE |
 389		    CHNL_CTRL_IRQ_EN | CHNL_CTRL_IRQ_ERR_EN |
 390		    CHNL_CTRL_IRQ_DLY_EN | CHNL_CTRL_IRQ_COAL_EN);
 
 
 
 
 391
 392	/* Init descriptor indexes */
 393	lp->tx_bd_ci = 0;
 
 394	lp->tx_bd_tail = 0;
 395	lp->rx_bd_ci = 0;
 396	lp->rx_bd_tail = lp->rx_bd_num - 1;
 397
 398	/* Enable RX DMA transfers */
 399	wmb();
 400	lp->dma_out(lp, RX_CURDESC_PTR,  lp->rx_bd_p);
 401	lp->dma_out(lp, RX_TAILDESC_PTR,
 402		       lp->rx_bd_p + (sizeof(*lp->rx_bd_v) * lp->rx_bd_tail));
 403
 404	/* Prepare for TX DMA transfer */
 405	lp->dma_out(lp, TX_CURDESC_PTR, lp->tx_bd_p);
 406
 407	return 0;
 408
 409out:
 410	temac_dma_bd_release(ndev);
 411	return -ENOMEM;
 412}
 413
 414/* ---------------------------------------------------------------------
 415 * net_device_ops
 416 */
 417
 418static void temac_do_set_mac_address(struct net_device *ndev)
 419{
 420	struct temac_local *lp = netdev_priv(ndev);
 421	unsigned long flags;
 422
 423	/* set up unicast MAC address filter set its mac address */
 424	spin_lock_irqsave(lp->indirect_lock, flags);
 425	temac_indirect_out32_locked(lp, XTE_UAW0_OFFSET,
 426				    (ndev->dev_addr[0]) |
 427				    (ndev->dev_addr[1] << 8) |
 428				    (ndev->dev_addr[2] << 16) |
 429				    (ndev->dev_addr[3] << 24));
 430	/* There are reserved bits in EUAW1
 431	 * so don't affect them Set MAC bits [47:32] in EUAW1
 432	 */
 433	temac_indirect_out32_locked(lp, XTE_UAW1_OFFSET,
 434				    (ndev->dev_addr[4] & 0x000000ff) |
 435				    (ndev->dev_addr[5] << 8));
 436	spin_unlock_irqrestore(lp->indirect_lock, flags);
 437}
 438
 439static int temac_init_mac_address(struct net_device *ndev, const void *address)
 440{
 441	eth_hw_addr_set(ndev, address);
 442	if (!is_valid_ether_addr(ndev->dev_addr))
 443		eth_hw_addr_random(ndev);
 444	temac_do_set_mac_address(ndev);
 445	return 0;
 446}
 447
 448static int temac_set_mac_address(struct net_device *ndev, void *p)
 449{
 450	struct sockaddr *addr = p;
 451
 452	if (!is_valid_ether_addr(addr->sa_data))
 453		return -EADDRNOTAVAIL;
 454	eth_hw_addr_set(ndev, addr->sa_data);
 455	temac_do_set_mac_address(ndev);
 456	return 0;
 457}
 458
 459static void temac_set_multicast_list(struct net_device *ndev)
 460{
 461	struct temac_local *lp = netdev_priv(ndev);
 462	u32 multi_addr_msw, multi_addr_lsw;
 463	int i = 0;
 464	unsigned long flags;
 465	bool promisc_mode_disabled = false;
 466
 467	if (ndev->flags & (IFF_PROMISC | IFF_ALLMULTI) ||
 468	    (netdev_mc_count(ndev) > MULTICAST_CAM_TABLE_NUM)) {
 
 
 
 
 
 
 
 
 469		temac_indirect_out32(lp, XTE_AFM_OFFSET, XTE_AFM_EPPRM_MASK);
 470		dev_info(&ndev->dev, "Promiscuous mode enabled.\n");
 471		return;
 472	}
 473
 474	spin_lock_irqsave(lp->indirect_lock, flags);
 475
 476	if (!netdev_mc_empty(ndev)) {
 477		struct netdev_hw_addr *ha;
 478
 
 479		netdev_for_each_mc_addr(ha, ndev) {
 480			if (WARN_ON(i >= MULTICAST_CAM_TABLE_NUM))
 481				break;
 482			multi_addr_msw = ((ha->addr[3] << 24) |
 483					  (ha->addr[2] << 16) |
 484					  (ha->addr[1] << 8) |
 485					  (ha->addr[0]));
 486			temac_indirect_out32_locked(lp, XTE_MAW0_OFFSET,
 487						    multi_addr_msw);
 488			multi_addr_lsw = ((ha->addr[5] << 8) |
 489					  (ha->addr[4]) | (i << 16));
 490			temac_indirect_out32_locked(lp, XTE_MAW1_OFFSET,
 491						    multi_addr_lsw);
 492			i++;
 493		}
 
 
 
 
 
 
 
 494	}
 495
 496	/* Clear all or remaining/unused address table entries */
 497	while (i < MULTICAST_CAM_TABLE_NUM) {
 498		temac_indirect_out32_locked(lp, XTE_MAW0_OFFSET, 0);
 499		temac_indirect_out32_locked(lp, XTE_MAW1_OFFSET, i << 16);
 500		i++;
 501	}
 502
 503	/* Enable address filter block if currently disabled */
 504	if (temac_indirect_in32_locked(lp, XTE_AFM_OFFSET)
 505	    & XTE_AFM_EPPRM_MASK) {
 506		temac_indirect_out32_locked(lp, XTE_AFM_OFFSET, 0);
 507		promisc_mode_disabled = true;
 508	}
 509
 510	spin_unlock_irqrestore(lp->indirect_lock, flags);
 511
 512	if (promisc_mode_disabled)
 513		dev_info(&ndev->dev, "Promiscuous mode disabled.\n");
 514}
 515
 516static struct temac_option {
 517	int flg;
 518	u32 opt;
 519	u32 reg;
 520	u32 m_or;
 521	u32 m_and;
 522} temac_options[] = {
 523	/* Turn on jumbo packet support for both Rx and Tx */
 524	{
 525		.opt = XTE_OPTION_JUMBO,
 526		.reg = XTE_TXC_OFFSET,
 527		.m_or = XTE_TXC_TXJMBO_MASK,
 528	},
 529	{
 530		.opt = XTE_OPTION_JUMBO,
 531		.reg = XTE_RXC1_OFFSET,
 532		.m_or = XTE_RXC1_RXJMBO_MASK,
 533	},
 534	/* Turn on VLAN packet support for both Rx and Tx */
 535	{
 536		.opt = XTE_OPTION_VLAN,
 537		.reg = XTE_TXC_OFFSET,
 538		.m_or = XTE_TXC_TXVLAN_MASK,
 539	},
 540	{
 541		.opt = XTE_OPTION_VLAN,
 542		.reg = XTE_RXC1_OFFSET,
 543		.m_or = XTE_RXC1_RXVLAN_MASK,
 544	},
 545	/* Turn on FCS stripping on receive packets */
 546	{
 547		.opt = XTE_OPTION_FCS_STRIP,
 548		.reg = XTE_RXC1_OFFSET,
 549		.m_or = XTE_RXC1_RXFCS_MASK,
 550	},
 551	/* Turn on FCS insertion on transmit packets */
 552	{
 553		.opt = XTE_OPTION_FCS_INSERT,
 554		.reg = XTE_TXC_OFFSET,
 555		.m_or = XTE_TXC_TXFCS_MASK,
 556	},
 557	/* Turn on length/type field checking on receive packets */
 558	{
 559		.opt = XTE_OPTION_LENTYPE_ERR,
 560		.reg = XTE_RXC1_OFFSET,
 561		.m_or = XTE_RXC1_RXLT_MASK,
 562	},
 563	/* Turn on flow control */
 564	{
 565		.opt = XTE_OPTION_FLOW_CONTROL,
 566		.reg = XTE_FCC_OFFSET,
 567		.m_or = XTE_FCC_RXFLO_MASK,
 568	},
 569	/* Turn on flow control */
 570	{
 571		.opt = XTE_OPTION_FLOW_CONTROL,
 572		.reg = XTE_FCC_OFFSET,
 573		.m_or = XTE_FCC_TXFLO_MASK,
 574	},
 575	/* Turn on promiscuous frame filtering (all frames are received ) */
 576	{
 577		.opt = XTE_OPTION_PROMISC,
 578		.reg = XTE_AFM_OFFSET,
 579		.m_or = XTE_AFM_EPPRM_MASK,
 580	},
 581	/* Enable transmitter if not already enabled */
 582	{
 583		.opt = XTE_OPTION_TXEN,
 584		.reg = XTE_TXC_OFFSET,
 585		.m_or = XTE_TXC_TXEN_MASK,
 586	},
 587	/* Enable receiver? */
 588	{
 589		.opt = XTE_OPTION_RXEN,
 590		.reg = XTE_RXC1_OFFSET,
 591		.m_or = XTE_RXC1_RXEN_MASK,
 592	},
 593	{}
 594};
 595
 596/*
 597 * temac_setoptions
 598 */
 599static u32 temac_setoptions(struct net_device *ndev, u32 options)
 600{
 601	struct temac_local *lp = netdev_priv(ndev);
 602	struct temac_option *tp = &temac_options[0];
 603	int reg;
 604	unsigned long flags;
 605
 606	spin_lock_irqsave(lp->indirect_lock, flags);
 607	while (tp->opt) {
 608		reg = temac_indirect_in32_locked(lp, tp->reg) & ~tp->m_or;
 609		if (options & tp->opt) {
 610			reg |= tp->m_or;
 611			temac_indirect_out32_locked(lp, tp->reg, reg);
 612		}
 613		tp++;
 614	}
 615	spin_unlock_irqrestore(lp->indirect_lock, flags);
 616	lp->options |= options;
 
 617
 618	return 0;
 619}
 620
 621/* Initialize temac */
 622static void temac_device_reset(struct net_device *ndev)
 623{
 624	struct temac_local *lp = netdev_priv(ndev);
 625	u32 timeout;
 626	u32 val;
 627	unsigned long flags;
 628
 629	/* Perform a software reset */
 630
 631	/* 0x300 host enable bit ? */
 632	/* reset PHY through control register ?:1 */
 633
 634	dev_dbg(&ndev->dev, "%s()\n", __func__);
 635
 
 636	/* Reset the receiver and wait for it to finish reset */
 637	temac_indirect_out32(lp, XTE_RXC1_OFFSET, XTE_RXC1_RXRST_MASK);
 638	timeout = 1000;
 639	while (temac_indirect_in32(lp, XTE_RXC1_OFFSET) & XTE_RXC1_RXRST_MASK) {
 640		udelay(1);
 641		if (--timeout == 0) {
 642			dev_err(&ndev->dev,
 643				"%s RX reset timeout!!\n", __func__);
 644			break;
 645		}
 646	}
 647
 648	/* Reset the transmitter and wait for it to finish reset */
 649	temac_indirect_out32(lp, XTE_TXC_OFFSET, XTE_TXC_TXRST_MASK);
 650	timeout = 1000;
 651	while (temac_indirect_in32(lp, XTE_TXC_OFFSET) & XTE_TXC_TXRST_MASK) {
 652		udelay(1);
 653		if (--timeout == 0) {
 654			dev_err(&ndev->dev,
 655				"%s TX reset timeout!!\n", __func__);
 656			break;
 657		}
 658	}
 659
 660	/* Disable the receiver */
 661	spin_lock_irqsave(lp->indirect_lock, flags);
 662	val = temac_indirect_in32_locked(lp, XTE_RXC1_OFFSET);
 663	temac_indirect_out32_locked(lp, XTE_RXC1_OFFSET,
 664				    val & ~XTE_RXC1_RXEN_MASK);
 665	spin_unlock_irqrestore(lp->indirect_lock, flags);
 666
 667	/* Reset Local Link (DMA) */
 668	lp->dma_out(lp, DMA_CONTROL_REG, DMA_CONTROL_RST);
 669	timeout = 1000;
 670	while (lp->dma_in(lp, DMA_CONTROL_REG) & DMA_CONTROL_RST) {
 671		udelay(1);
 672		if (--timeout == 0) {
 673			dev_err(&ndev->dev,
 674				"%s DMA reset timeout!!\n", __func__);
 675			break;
 676		}
 677	}
 678	lp->dma_out(lp, DMA_CONTROL_REG, DMA_TAIL_ENABLE);
 679
 680	if (temac_dma_bd_init(ndev)) {
 681		dev_err(&ndev->dev,
 682			"%s descriptor allocation failed\n", __func__);
 683	}
 684
 685	spin_lock_irqsave(lp->indirect_lock, flags);
 686	temac_indirect_out32_locked(lp, XTE_RXC0_OFFSET, 0);
 687	temac_indirect_out32_locked(lp, XTE_RXC1_OFFSET, 0);
 688	temac_indirect_out32_locked(lp, XTE_TXC_OFFSET, 0);
 689	temac_indirect_out32_locked(lp, XTE_FCC_OFFSET, XTE_FCC_RXFLO_MASK);
 690	spin_unlock_irqrestore(lp->indirect_lock, flags);
 691
 692	/* Sync default options with HW
 693	 * but leave receiver and transmitter disabled.
 694	 */
 695	temac_setoptions(ndev,
 696			 lp->options & ~(XTE_OPTION_TXEN | XTE_OPTION_RXEN));
 697
 698	temac_do_set_mac_address(ndev);
 699
 700	/* Set address filter table */
 701	temac_set_multicast_list(ndev);
 702	if (temac_setoptions(ndev, lp->options))
 703		dev_err(&ndev->dev, "Error setting TEMAC options\n");
 704
 705	/* Init Driver variable */
 706	netif_trans_update(ndev); /* prevent tx timeout */
 707}
 708
 709static void temac_adjust_link(struct net_device *ndev)
 710{
 711	struct temac_local *lp = netdev_priv(ndev);
 712	struct phy_device *phy = ndev->phydev;
 713	u32 mii_speed;
 714	int link_state;
 715	unsigned long flags;
 716
 717	/* hash together the state values to decide if something has changed */
 718	link_state = phy->speed | (phy->duplex << 1) | phy->link;
 719
 
 720	if (lp->last_link != link_state) {
 721		spin_lock_irqsave(lp->indirect_lock, flags);
 722		mii_speed = temac_indirect_in32_locked(lp, XTE_EMCFG_OFFSET);
 723		mii_speed &= ~XTE_EMCFG_LINKSPD_MASK;
 724
 725		switch (phy->speed) {
 726		case SPEED_1000:
 727			mii_speed |= XTE_EMCFG_LINKSPD_1000;
 728			break;
 729		case SPEED_100:
 730			mii_speed |= XTE_EMCFG_LINKSPD_100;
 731			break;
 732		case SPEED_10:
 733			mii_speed |= XTE_EMCFG_LINKSPD_10;
 734			break;
 735		}
 736
 737		/* Write new speed setting out to TEMAC */
 738		temac_indirect_out32_locked(lp, XTE_EMCFG_OFFSET, mii_speed);
 739		spin_unlock_irqrestore(lp->indirect_lock, flags);
 740
 741		lp->last_link = link_state;
 742		phy_print_status(phy);
 743	}
 
 744}
 745
 746#ifdef CONFIG_64BIT
 747
 748static void ptr_to_txbd(void *p, struct cdmac_bd *bd)
 749{
 750	bd->app3 = (u32)(((u64)p) >> 32);
 751	bd->app4 = (u32)((u64)p & 0xFFFFFFFF);
 752}
 753
 754static void *ptr_from_txbd(struct cdmac_bd *bd)
 755{
 756	return (void *)(((u64)(bd->app3) << 32) | bd->app4);
 757}
 758
 759#else
 760
 761static void ptr_to_txbd(void *p, struct cdmac_bd *bd)
 762{
 763	bd->app4 = (u32)p;
 764}
 765
 766static void *ptr_from_txbd(struct cdmac_bd *bd)
 767{
 768	return (void *)(bd->app4);
 769}
 770
 771#endif
 772
 773static void temac_start_xmit_done(struct net_device *ndev)
 774{
 775	struct temac_local *lp = netdev_priv(ndev);
 776	struct cdmac_bd *cur_p;
 777	unsigned int stat = 0;
 778	struct sk_buff *skb;
 779
 780	cur_p = &lp->tx_bd_v[lp->tx_bd_ci];
 781	stat = be32_to_cpu(cur_p->app0);
 782
 783	while (stat & STS_CTRL_APP0_CMPLT) {
 784		/* Make sure that the other fields are read after bd is
 785		 * released by dma
 786		 */
 787		rmb();
 788		dma_unmap_single(ndev->dev.parent, be32_to_cpu(cur_p->phys),
 789				 be32_to_cpu(cur_p->len), DMA_TO_DEVICE);
 790		skb = (struct sk_buff *)ptr_from_txbd(cur_p);
 791		if (skb)
 792			dev_consume_skb_irq(skb);
 793		cur_p->app1 = 0;
 794		cur_p->app2 = 0;
 795		cur_p->app3 = 0;
 796		cur_p->app4 = 0;
 797
 798		ndev->stats.tx_packets++;
 799		ndev->stats.tx_bytes += be32_to_cpu(cur_p->len);
 800
 801		/* app0 must be visible last, as it is used to flag
 802		 * availability of the bd
 803		 */
 804		smp_mb();
 805		cur_p->app0 = 0;
 806
 807		lp->tx_bd_ci++;
 808		if (lp->tx_bd_ci >= lp->tx_bd_num)
 809			lp->tx_bd_ci = 0;
 810
 811		cur_p = &lp->tx_bd_v[lp->tx_bd_ci];
 812		stat = be32_to_cpu(cur_p->app0);
 813	}
 814
 815	/* Matches barrier in temac_start_xmit */
 816	smp_mb();
 817
 818	netif_wake_queue(ndev);
 819}
 820
 821static inline int temac_check_tx_bd_space(struct temac_local *lp, int num_frag)
 822{
 823	struct cdmac_bd *cur_p;
 824	int tail;
 825
 826	tail = lp->tx_bd_tail;
 827	cur_p = &lp->tx_bd_v[tail];
 828
 829	do {
 830		if (cur_p->app0)
 831			return NETDEV_TX_BUSY;
 832
 833		/* Make sure to read next bd app0 after this one */
 834		rmb();
 835
 836		tail++;
 837		if (tail >= lp->tx_bd_num)
 838			tail = 0;
 839
 840		cur_p = &lp->tx_bd_v[tail];
 841		num_frag--;
 842	} while (num_frag >= 0);
 843
 844	return 0;
 845}
 846
 847static netdev_tx_t
 848temac_start_xmit(struct sk_buff *skb, struct net_device *ndev)
 849{
 850	struct temac_local *lp = netdev_priv(ndev);
 851	struct cdmac_bd *cur_p;
 852	dma_addr_t tail_p, skb_dma_addr;
 853	int ii;
 854	unsigned long num_frag;
 855	skb_frag_t *frag;
 856
 857	num_frag = skb_shinfo(skb)->nr_frags;
 858	frag = &skb_shinfo(skb)->frags[0];
 
 859	cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
 860
 861	if (temac_check_tx_bd_space(lp, num_frag + 1)) {
 862		if (netif_queue_stopped(ndev))
 
 863			return NETDEV_TX_BUSY;
 864
 865		netif_stop_queue(ndev);
 866
 867		/* Matches barrier in temac_start_xmit_done */
 868		smp_mb();
 869
 870		/* Space might have just been freed - check again */
 871		if (temac_check_tx_bd_space(lp, num_frag + 1))
 872			return NETDEV_TX_BUSY;
 873
 874		netif_wake_queue(ndev);
 875	}
 876
 877	cur_p->app0 = 0;
 878	if (skb->ip_summed == CHECKSUM_PARTIAL) {
 879		unsigned int csum_start_off = skb_checksum_start_offset(skb);
 880		unsigned int csum_index_off = csum_start_off + skb->csum_offset;
 881
 882		cur_p->app0 |= cpu_to_be32(0x000001); /* TX Checksum Enabled */
 883		cur_p->app1 = cpu_to_be32((csum_start_off << 16)
 884					  | csum_index_off);
 885		cur_p->app2 = 0;  /* initial checksum seed */
 886	}
 887
 888	cur_p->app0 |= cpu_to_be32(STS_CTRL_APP0_SOP);
 889	skb_dma_addr = dma_map_single(ndev->dev.parent, skb->data,
 890				      skb_headlen(skb), DMA_TO_DEVICE);
 891	cur_p->len = cpu_to_be32(skb_headlen(skb));
 892	if (WARN_ON_ONCE(dma_mapping_error(ndev->dev.parent, skb_dma_addr))) {
 893		dev_kfree_skb_any(skb);
 894		ndev->stats.tx_dropped++;
 895		return NETDEV_TX_OK;
 896	}
 897	cur_p->phys = cpu_to_be32(skb_dma_addr);
 898
 899	for (ii = 0; ii < num_frag; ii++) {
 900		if (++lp->tx_bd_tail >= lp->tx_bd_num)
 
 901			lp->tx_bd_tail = 0;
 902
 903		cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
 904		skb_dma_addr = dma_map_single(ndev->dev.parent,
 905					      skb_frag_address(frag),
 906					      skb_frag_size(frag),
 907					      DMA_TO_DEVICE);
 908		if (dma_mapping_error(ndev->dev.parent, skb_dma_addr)) {
 909			if (--lp->tx_bd_tail < 0)
 910				lp->tx_bd_tail = lp->tx_bd_num - 1;
 911			cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
 912			while (--ii >= 0) {
 913				--frag;
 914				dma_unmap_single(ndev->dev.parent,
 915						 be32_to_cpu(cur_p->phys),
 916						 skb_frag_size(frag),
 917						 DMA_TO_DEVICE);
 918				if (--lp->tx_bd_tail < 0)
 919					lp->tx_bd_tail = lp->tx_bd_num - 1;
 920				cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
 921			}
 922			dma_unmap_single(ndev->dev.parent,
 923					 be32_to_cpu(cur_p->phys),
 924					 skb_headlen(skb), DMA_TO_DEVICE);
 925			dev_kfree_skb_any(skb);
 926			ndev->stats.tx_dropped++;
 927			return NETDEV_TX_OK;
 928		}
 929		cur_p->phys = cpu_to_be32(skb_dma_addr);
 930		cur_p->len = cpu_to_be32(skb_frag_size(frag));
 931		cur_p->app0 = 0;
 932		frag++;
 933	}
 934	cur_p->app0 |= cpu_to_be32(STS_CTRL_APP0_EOP);
 935
 936	/* Mark last fragment with skb address, so it can be consumed
 937	 * in temac_start_xmit_done()
 938	 */
 939	ptr_to_txbd((void *)skb, cur_p);
 940
 941	tail_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * lp->tx_bd_tail;
 942	lp->tx_bd_tail++;
 943	if (lp->tx_bd_tail >= lp->tx_bd_num)
 944		lp->tx_bd_tail = 0;
 945
 946	skb_tx_timestamp(skb);
 947
 948	/* Kick off the transfer */
 949	wmb();
 950	lp->dma_out(lp, TX_TAILDESC_PTR, tail_p); /* DMA start */
 951
 952	if (temac_check_tx_bd_space(lp, MAX_SKB_FRAGS + 1))
 953		netif_stop_queue(ndev);
 954
 955	return NETDEV_TX_OK;
 956}
 957
 958static int ll_temac_recv_buffers_available(struct temac_local *lp)
 959{
 960	int available;
 961
 962	if (!lp->rx_skb[lp->rx_bd_ci])
 963		return 0;
 964	available = 1 + lp->rx_bd_tail - lp->rx_bd_ci;
 965	if (available <= 0)
 966		available += lp->rx_bd_num;
 967	return available;
 968}
 969
 970static void ll_temac_recv(struct net_device *ndev)
 971{
 972	struct temac_local *lp = netdev_priv(ndev);
 
 
 
 
 
 973	unsigned long flags;
 974	int rx_bd;
 975	bool update_tail = false;
 976
 977	spin_lock_irqsave(&lp->rx_lock, flags);
 978
 979	/* Process all received buffers, passing them on network
 980	 * stack.  After this, the buffer descriptors will be in an
 981	 * un-allocated stage, where no skb is allocated for it, and
 982	 * they are therefore not available for TEMAC/DMA.
 983	 */
 984	do {
 985		struct cdmac_bd *bd = &lp->rx_bd_v[lp->rx_bd_ci];
 986		struct sk_buff *skb = lp->rx_skb[lp->rx_bd_ci];
 987		unsigned int bdstat = be32_to_cpu(bd->app0);
 988		int length;
 989
 990		/* While this should not normally happen, we can end
 991		 * here when GFP_ATOMIC allocations fail, and we
 992		 * therefore have un-allocated buffers.
 993		 */
 994		if (!skb)
 995			break;
 996
 997		/* Loop over all completed buffer descriptors */
 998		if (!(bdstat & STS_CTRL_APP0_CMPLT))
 999			break;
1000
1001		dma_unmap_single(ndev->dev.parent, be32_to_cpu(bd->phys),
1002				 XTE_MAX_JUMBO_FRAME_SIZE, DMA_FROM_DEVICE);
1003		/* The buffer is not valid for DMA anymore */
1004		bd->phys = 0;
1005		bd->len = 0;
1006
1007		length = be32_to_cpu(bd->app4) & 0x3FFF;
1008		skb_put(skb, length);
1009		skb->protocol = eth_type_trans(skb, ndev);
1010		skb_checksum_none_assert(skb);
1011
1012		/* if we're doing rx csum offload, set it up */
1013		if (((lp->temac_features & TEMAC_FEATURE_RX_CSUM) != 0) &&
1014		    (skb->protocol == htons(ETH_P_IP)) &&
1015		    (skb->len > 64)) {
1016			/* Convert from device endianness (be32) to cpu
1017			 * endianness, and if necessary swap the bytes
1018			 * (back) for proper IP checksum byte order
1019			 * (be16).
1020			 */
1021			skb->csum = htons(be32_to_cpu(bd->app3) & 0xFFFF);
1022			skb->ip_summed = CHECKSUM_COMPLETE;
1023		}
1024
1025		if (!skb_defer_rx_timestamp(skb))
1026			netif_rx(skb);
1027		/* The skb buffer is now owned by network stack above */
1028		lp->rx_skb[lp->rx_bd_ci] = NULL;
1029
1030		ndev->stats.rx_packets++;
1031		ndev->stats.rx_bytes += length;
1032
1033		rx_bd = lp->rx_bd_ci;
1034		if (++lp->rx_bd_ci >= lp->rx_bd_num)
1035			lp->rx_bd_ci = 0;
1036	} while (rx_bd != lp->rx_bd_tail);
1037
1038	/* DMA operations will halt when the last buffer descriptor is
1039	 * processed (ie. the one pointed to by RX_TAILDESC_PTR).
1040	 * When that happens, no more interrupt events will be
1041	 * generated.  No IRQ_COAL or IRQ_DLY, and not even an
1042	 * IRQ_ERR.  To avoid stalling, we schedule a delayed work
1043	 * when there is a potential risk of that happening.  The work
1044	 * will call this function, and thus re-schedule itself until
1045	 * enough buffers are available again.
1046	 */
1047	if (ll_temac_recv_buffers_available(lp) < lp->coalesce_count_rx)
1048		schedule_delayed_work(&lp->restart_work, HZ / 1000);
1049
1050	/* Allocate new buffers for those buffer descriptors that were
1051	 * passed to network stack.  Note that GFP_ATOMIC allocations
1052	 * can fail (e.g. when a larger burst of GFP_ATOMIC
1053	 * allocations occurs), so while we try to allocate all
1054	 * buffers in the same interrupt where they were processed, we
1055	 * continue with what we could get in case of allocation
1056	 * failure.  Allocation of remaining buffers will be retried
1057	 * in following calls.
1058	 */
1059	while (1) {
1060		struct sk_buff *skb;
1061		struct cdmac_bd *bd;
1062		dma_addr_t skb_dma_addr;
1063
1064		rx_bd = lp->rx_bd_tail + 1;
1065		if (rx_bd >= lp->rx_bd_num)
1066			rx_bd = 0;
1067		bd = &lp->rx_bd_v[rx_bd];
1068
1069		if (bd->phys)
1070			break;	/* All skb's allocated */
1071
1072		skb = netdev_alloc_skb_ip_align(ndev, XTE_MAX_JUMBO_FRAME_SIZE);
1073		if (!skb) {
1074			dev_warn(&ndev->dev, "skb alloc failed\n");
1075			break;
1076		}
1077
1078		skb_dma_addr = dma_map_single(ndev->dev.parent, skb->data,
1079					      XTE_MAX_JUMBO_FRAME_SIZE,
1080					      DMA_FROM_DEVICE);
1081		if (WARN_ON_ONCE(dma_mapping_error(ndev->dev.parent,
1082						   skb_dma_addr))) {
1083			dev_kfree_skb_any(skb);
1084			break;
1085		}
1086
1087		bd->phys = cpu_to_be32(skb_dma_addr);
1088		bd->len = cpu_to_be32(XTE_MAX_JUMBO_FRAME_SIZE);
1089		bd->app0 = cpu_to_be32(STS_CTRL_APP0_IRQONEND);
1090		lp->rx_skb[rx_bd] = skb;
1091
1092		lp->rx_bd_tail = rx_bd;
1093		update_tail = true;
1094	}
1095
1096	/* Move tail pointer when buffers have been allocated */
1097	if (update_tail) {
1098		lp->dma_out(lp, RX_TAILDESC_PTR,
1099			lp->rx_bd_p + sizeof(*lp->rx_bd_v) * lp->rx_bd_tail);
1100	}
 
1101
1102	spin_unlock_irqrestore(&lp->rx_lock, flags);
1103}
1104
1105/* Function scheduled to ensure a restart in case of DMA halt
1106 * condition caused by running out of buffer descriptors.
1107 */
1108static void ll_temac_restart_work_func(struct work_struct *work)
1109{
1110	struct temac_local *lp = container_of(work, struct temac_local,
1111					      restart_work.work);
1112	struct net_device *ndev = lp->ndev;
1113
1114	ll_temac_recv(ndev);
1115}
1116
1117static irqreturn_t ll_temac_tx_irq(int irq, void *_ndev)
1118{
1119	struct net_device *ndev = _ndev;
1120	struct temac_local *lp = netdev_priv(ndev);
1121	unsigned int status;
1122
1123	status = lp->dma_in(lp, TX_IRQ_REG);
1124	lp->dma_out(lp, TX_IRQ_REG, status);
1125
1126	if (status & (IRQ_COAL | IRQ_DLY))
1127		temac_start_xmit_done(lp->ndev);
1128	if (status & (IRQ_ERR | IRQ_DMAERR))
1129		dev_err_ratelimited(&ndev->dev,
1130				    "TX error 0x%x TX_CHNL_STS=0x%08x\n",
1131				    status, lp->dma_in(lp, TX_CHNL_STS));
1132
1133	return IRQ_HANDLED;
1134}
1135
1136static irqreturn_t ll_temac_rx_irq(int irq, void *_ndev)
1137{
1138	struct net_device *ndev = _ndev;
1139	struct temac_local *lp = netdev_priv(ndev);
1140	unsigned int status;
1141
1142	/* Read and clear the status registers */
1143	status = lp->dma_in(lp, RX_IRQ_REG);
1144	lp->dma_out(lp, RX_IRQ_REG, status);
1145
1146	if (status & (IRQ_COAL | IRQ_DLY))
1147		ll_temac_recv(lp->ndev);
1148	if (status & (IRQ_ERR | IRQ_DMAERR))
1149		dev_err_ratelimited(&ndev->dev,
1150				    "RX error 0x%x RX_CHNL_STS=0x%08x\n",
1151				    status, lp->dma_in(lp, RX_CHNL_STS));
1152
1153	return IRQ_HANDLED;
1154}
1155
1156static int temac_open(struct net_device *ndev)
1157{
1158	struct temac_local *lp = netdev_priv(ndev);
1159	struct phy_device *phydev = NULL;
1160	int rc;
1161
1162	dev_dbg(&ndev->dev, "temac_open()\n");
1163
1164	if (lp->phy_node) {
1165		phydev = of_phy_connect(lp->ndev, lp->phy_node,
1166					temac_adjust_link, 0, 0);
1167		if (!phydev) {
1168			dev_err(lp->dev, "of_phy_connect() failed\n");
1169			return -ENODEV;
1170		}
1171		phy_start(phydev);
1172	} else if (strlen(lp->phy_name) > 0) {
1173		phydev = phy_connect(lp->ndev, lp->phy_name, temac_adjust_link,
1174				     lp->phy_interface);
1175		if (IS_ERR(phydev)) {
1176			dev_err(lp->dev, "phy_connect() failed\n");
1177			return PTR_ERR(phydev);
1178		}
1179		phy_start(phydev);
1180	}
1181
1182	temac_device_reset(ndev);
1183
1184	rc = request_irq(lp->tx_irq, ll_temac_tx_irq, 0, ndev->name, ndev);
1185	if (rc)
1186		goto err_tx_irq;
1187	rc = request_irq(lp->rx_irq, ll_temac_rx_irq, 0, ndev->name, ndev);
1188	if (rc)
1189		goto err_rx_irq;
1190
1191	return 0;
1192
1193 err_rx_irq:
1194	free_irq(lp->tx_irq, ndev);
1195 err_tx_irq:
1196	if (phydev)
1197		phy_disconnect(phydev);
 
1198	dev_err(lp->dev, "request_irq() failed\n");
1199	return rc;
1200}
1201
1202static int temac_stop(struct net_device *ndev)
1203{
1204	struct temac_local *lp = netdev_priv(ndev);
1205	struct phy_device *phydev = ndev->phydev;
1206
1207	dev_dbg(&ndev->dev, "temac_close()\n");
1208
1209	cancel_delayed_work_sync(&lp->restart_work);
1210
1211	free_irq(lp->tx_irq, ndev);
1212	free_irq(lp->rx_irq, ndev);
1213
1214	if (phydev)
1215		phy_disconnect(phydev);
 
1216
1217	temac_dma_bd_release(ndev);
1218
1219	return 0;
1220}
1221
1222#ifdef CONFIG_NET_POLL_CONTROLLER
1223static void
1224temac_poll_controller(struct net_device *ndev)
1225{
1226	struct temac_local *lp = netdev_priv(ndev);
1227
1228	disable_irq(lp->tx_irq);
1229	disable_irq(lp->rx_irq);
1230
1231	ll_temac_rx_irq(lp->tx_irq, ndev);
1232	ll_temac_tx_irq(lp->rx_irq, ndev);
1233
1234	enable_irq(lp->tx_irq);
1235	enable_irq(lp->rx_irq);
1236}
1237#endif
1238
 
 
 
 
 
 
 
 
 
 
 
 
 
1239static const struct net_device_ops temac_netdev_ops = {
1240	.ndo_open = temac_open,
1241	.ndo_stop = temac_stop,
1242	.ndo_start_xmit = temac_start_xmit,
1243	.ndo_set_rx_mode = temac_set_multicast_list,
1244	.ndo_set_mac_address = temac_set_mac_address,
1245	.ndo_validate_addr = eth_validate_addr,
1246	.ndo_eth_ioctl = phy_do_ioctl_running,
1247#ifdef CONFIG_NET_POLL_CONTROLLER
1248	.ndo_poll_controller = temac_poll_controller,
1249#endif
1250};
1251
1252/* ---------------------------------------------------------------------
1253 * SYSFS device attributes
1254 */
1255static ssize_t temac_show_llink_regs(struct device *dev,
1256				     struct device_attribute *attr, char *buf)
1257{
1258	struct net_device *ndev = dev_get_drvdata(dev);
1259	struct temac_local *lp = netdev_priv(ndev);
1260	int i, len = 0;
1261
1262	for (i = 0; i < 0x11; i++)
1263		len += sprintf(buf + len, "%.8x%s", lp->dma_in(lp, i),
1264			       (i % 8) == 7 ? "\n" : " ");
1265	len += sprintf(buf + len, "\n");
1266
1267	return len;
1268}
1269
1270static DEVICE_ATTR(llink_regs, 0440, temac_show_llink_regs, NULL);
1271
1272static struct attribute *temac_device_attrs[] = {
1273	&dev_attr_llink_regs.attr,
1274	NULL,
1275};
1276
1277static const struct attribute_group temac_attr_group = {
1278	.attrs = temac_device_attrs,
1279};
1280
1281/* ---------------------------------------------------------------------
1282 * ethtool support
1283 */
1284
1285static void
1286ll_temac_ethtools_get_ringparam(struct net_device *ndev,
1287				struct ethtool_ringparam *ering,
1288				struct kernel_ethtool_ringparam *kernel_ering,
1289				struct netlink_ext_ack *extack)
1290{
1291	struct temac_local *lp = netdev_priv(ndev);
1292
1293	ering->rx_max_pending = RX_BD_NUM_MAX;
1294	ering->rx_mini_max_pending = 0;
1295	ering->rx_jumbo_max_pending = 0;
1296	ering->tx_max_pending = TX_BD_NUM_MAX;
1297	ering->rx_pending = lp->rx_bd_num;
1298	ering->rx_mini_pending = 0;
1299	ering->rx_jumbo_pending = 0;
1300	ering->tx_pending = lp->tx_bd_num;
1301}
1302
1303static int
1304ll_temac_ethtools_set_ringparam(struct net_device *ndev,
1305				struct ethtool_ringparam *ering,
1306				struct kernel_ethtool_ringparam *kernel_ering,
1307				struct netlink_ext_ack *extack)
1308{
1309	struct temac_local *lp = netdev_priv(ndev);
1310
1311	if (ering->rx_pending > RX_BD_NUM_MAX ||
1312	    ering->rx_mini_pending ||
1313	    ering->rx_jumbo_pending ||
1314	    ering->rx_pending > TX_BD_NUM_MAX)
1315		return -EINVAL;
1316
1317	if (netif_running(ndev))
1318		return -EBUSY;
1319
1320	lp->rx_bd_num = ering->rx_pending;
1321	lp->tx_bd_num = ering->tx_pending;
1322	return 0;
1323}
1324
1325static int
1326ll_temac_ethtools_get_coalesce(struct net_device *ndev,
1327			       struct ethtool_coalesce *ec,
1328			       struct kernel_ethtool_coalesce *kernel_coal,
1329			       struct netlink_ext_ack *extack)
1330{
1331	struct temac_local *lp = netdev_priv(ndev);
1332
1333	ec->rx_max_coalesced_frames = lp->coalesce_count_rx;
1334	ec->tx_max_coalesced_frames = lp->coalesce_count_tx;
1335	ec->rx_coalesce_usecs = (lp->coalesce_delay_rx * 512) / 100;
1336	ec->tx_coalesce_usecs = (lp->coalesce_delay_tx * 512) / 100;
1337	return 0;
1338}
1339
1340static int
1341ll_temac_ethtools_set_coalesce(struct net_device *ndev,
1342			       struct ethtool_coalesce *ec,
1343			       struct kernel_ethtool_coalesce *kernel_coal,
1344			       struct netlink_ext_ack *extack)
1345{
1346	struct temac_local *lp = netdev_priv(ndev);
1347
1348	if (netif_running(ndev)) {
1349		netdev_err(ndev,
1350			   "Please stop netif before applying configuration\n");
1351		return -EFAULT;
1352	}
1353
1354	if (ec->rx_max_coalesced_frames)
1355		lp->coalesce_count_rx = ec->rx_max_coalesced_frames;
1356	if (ec->tx_max_coalesced_frames)
1357		lp->coalesce_count_tx = ec->tx_max_coalesced_frames;
1358	/* With typical LocalLink clock speed of 200 MHz and
1359	 * C_PRESCALAR=1023, each delay count corresponds to 5.12 us.
1360	 */
1361	if (ec->rx_coalesce_usecs)
1362		lp->coalesce_delay_rx =
1363			min(255U, (ec->rx_coalesce_usecs * 100) / 512);
1364	if (ec->tx_coalesce_usecs)
1365		lp->coalesce_delay_tx =
1366			min(255U, (ec->tx_coalesce_usecs * 100) / 512);
1367
1368	return 0;
1369}
1370
1371static const struct ethtool_ops temac_ethtool_ops = {
1372	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
1373				     ETHTOOL_COALESCE_MAX_FRAMES,
1374	.nway_reset = phy_ethtool_nway_reset,
1375	.get_link = ethtool_op_get_link,
1376	.get_ts_info = ethtool_op_get_ts_info,
1377	.get_link_ksettings = phy_ethtool_get_link_ksettings,
1378	.set_link_ksettings = phy_ethtool_set_link_ksettings,
1379	.get_ringparam	= ll_temac_ethtools_get_ringparam,
1380	.set_ringparam	= ll_temac_ethtools_set_ringparam,
1381	.get_coalesce	= ll_temac_ethtools_get_coalesce,
1382	.set_coalesce	= ll_temac_ethtools_set_coalesce,
1383};
1384
1385static int temac_probe(struct platform_device *pdev)
1386{
1387	struct ll_temac_platform_data *pdata = dev_get_platdata(&pdev->dev);
1388	struct device_node *temac_np = dev_of_node(&pdev->dev), *dma_np;
1389	struct temac_local *lp;
1390	struct net_device *ndev;
1391	u8 addr[ETH_ALEN];
1392	__be32 *p;
1393	bool little_endian;
1394	int rc = 0;
1395
1396	/* Init network device structure */
1397	ndev = devm_alloc_etherdev(&pdev->dev, sizeof(*lp));
1398	if (!ndev)
1399		return -ENOMEM;
1400
1401	platform_set_drvdata(pdev, ndev);
1402	SET_NETDEV_DEV(ndev, &pdev->dev);
 
 
1403	ndev->features = NETIF_F_SG;
1404	ndev->netdev_ops = &temac_netdev_ops;
1405	ndev->ethtool_ops = &temac_ethtool_ops;
1406#if 0
1407	ndev->features |= NETIF_F_IP_CSUM; /* Can checksum TCP/UDP over IPv4. */
1408	ndev->features |= NETIF_F_HW_CSUM; /* Can checksum all the packets. */
1409	ndev->features |= NETIF_F_IPV6_CSUM; /* Can checksum IPV6 TCP/UDP */
1410	ndev->features |= NETIF_F_HIGHDMA; /* Can DMA to high memory. */
1411	ndev->features |= NETIF_F_HW_VLAN_CTAG_TX; /* Transmit VLAN hw accel */
1412	ndev->features |= NETIF_F_HW_VLAN_CTAG_RX; /* Receive VLAN hw acceleration */
1413	ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; /* Receive VLAN filtering */
1414	ndev->features |= NETIF_F_VLAN_CHALLENGED; /* cannot handle VLAN pkts */
1415	ndev->features |= NETIF_F_GSO; /* Enable software GSO. */
1416	ndev->features |= NETIF_F_MULTI_QUEUE; /* Has multiple TX/RX queues */
1417	ndev->features |= NETIF_F_LRO; /* large receive offload */
1418#endif
1419
1420	/* setup temac private info structure */
1421	lp = netdev_priv(ndev);
1422	lp->ndev = ndev;
1423	lp->dev = &pdev->dev;
1424	lp->options = XTE_OPTION_DEFAULTS;
1425	lp->rx_bd_num = RX_BD_NUM_DEFAULT;
1426	lp->tx_bd_num = TX_BD_NUM_DEFAULT;
1427	spin_lock_init(&lp->rx_lock);
1428	INIT_DELAYED_WORK(&lp->restart_work, ll_temac_restart_work_func);
1429
1430	/* Setup mutex for synchronization of indirect register access */
1431	if (pdata) {
1432		if (!pdata->indirect_lock) {
1433			dev_err(&pdev->dev,
1434				"indirect_lock missing in platform_data\n");
1435			return -EINVAL;
1436		}
1437		lp->indirect_lock = pdata->indirect_lock;
1438	} else {
1439		lp->indirect_lock = devm_kmalloc(&pdev->dev,
1440						 sizeof(*lp->indirect_lock),
1441						 GFP_KERNEL);
1442		if (!lp->indirect_lock)
1443			return -ENOMEM;
1444		spin_lock_init(lp->indirect_lock);
1445	}
1446
1447	/* map device registers */
1448	lp->regs = devm_platform_ioremap_resource_byname(pdev, 0);
1449	if (IS_ERR(lp->regs)) {
1450		dev_err(&pdev->dev, "could not map TEMAC registers\n");
1451		return -ENOMEM;
1452	}
1453
1454	/* Select register access functions with the specified
1455	 * endianness mode.  Default for OF devices is big-endian.
1456	 */
1457	little_endian = false;
1458	if (temac_np) {
1459		if (of_get_property(temac_np, "little-endian", NULL))
1460			little_endian = true;
1461	} else if (pdata) {
1462		little_endian = pdata->reg_little_endian;
1463	}
1464	if (little_endian) {
1465		lp->temac_ior = _temac_ior_le;
1466		lp->temac_iow = _temac_iow_le;
1467	} else {
1468		lp->temac_ior = _temac_ior_be;
1469		lp->temac_iow = _temac_iow_be;
1470	}
1471
1472	/* Setup checksum offload, but default to off if not specified */
1473	lp->temac_features = 0;
1474	if (temac_np) {
1475		p = (__be32 *)of_get_property(temac_np, "xlnx,txcsum", NULL);
1476		if (p && be32_to_cpu(*p))
1477			lp->temac_features |= TEMAC_FEATURE_TX_CSUM;
1478		p = (__be32 *)of_get_property(temac_np, "xlnx,rxcsum", NULL);
1479		if (p && be32_to_cpu(*p))
1480			lp->temac_features |= TEMAC_FEATURE_RX_CSUM;
1481	} else if (pdata) {
1482		if (pdata->txcsum)
1483			lp->temac_features |= TEMAC_FEATURE_TX_CSUM;
1484		if (pdata->rxcsum)
1485			lp->temac_features |= TEMAC_FEATURE_RX_CSUM;
1486	}
1487	if (lp->temac_features & TEMAC_FEATURE_TX_CSUM)
1488		/* Can checksum TCP/UDP over IPv4. */
1489		ndev->features |= NETIF_F_IP_CSUM;
1490
1491	/* Defaults for IRQ delay/coalescing setup.  These are
1492	 * configuration values, so does not belong in device-tree.
1493	 */
1494	lp->coalesce_delay_tx = 0x10;
1495	lp->coalesce_count_tx = 0x22;
1496	lp->coalesce_delay_rx = 0xff;
1497	lp->coalesce_count_rx = 0x07;
1498
1499	/* Setup LocalLink DMA */
1500	if (temac_np) {
1501		/* Find the DMA node, map the DMA registers, and
1502		 * decode the DMA IRQs.
1503		 */
1504		dma_np = of_parse_phandle(temac_np, "llink-connected", 0);
1505		if (!dma_np) {
1506			dev_err(&pdev->dev, "could not find DMA node\n");
1507			return -ENODEV;
 
 
 
 
 
 
 
1508		}
 
1509
1510		/* Setup the DMA register accesses, could be DCR or
1511		 * memory mapped.
1512		 */
1513		if (temac_dcr_setup(lp, pdev, dma_np)) {
1514			/* no DCR in the device tree, try non-DCR */
1515			lp->sdma_regs = devm_of_iomap(&pdev->dev, dma_np, 0,
1516						      NULL);
1517			if (IS_ERR(lp->sdma_regs)) {
1518				dev_err(&pdev->dev,
1519					"unable to map DMA registers\n");
1520				of_node_put(dma_np);
1521				return PTR_ERR(lp->sdma_regs);
1522			}
1523			if (of_property_read_bool(dma_np, "little-endian")) {
1524				lp->dma_in = temac_dma_in32_le;
1525				lp->dma_out = temac_dma_out32_le;
1526			} else {
1527				lp->dma_in = temac_dma_in32_be;
1528				lp->dma_out = temac_dma_out32_be;
1529			}
1530			dev_dbg(&pdev->dev, "MEM base: %p\n", lp->sdma_regs);
1531		}
1532
1533		/* Get DMA RX and TX interrupts */
1534		lp->rx_irq = irq_of_parse_and_map(dma_np, 0);
1535		lp->tx_irq = irq_of_parse_and_map(dma_np, 1);
1536
1537		/* Finished with the DMA node; drop the reference */
1538		of_node_put(dma_np);
1539	} else if (pdata) {
1540		/* 2nd memory resource specifies DMA registers */
1541		lp->sdma_regs = devm_platform_ioremap_resource(pdev, 1);
1542		if (IS_ERR(lp->sdma_regs)) {
1543			dev_err(&pdev->dev,
1544				"could not map DMA registers\n");
1545			return PTR_ERR(lp->sdma_regs);
1546		}
1547		if (pdata->dma_little_endian) {
1548			lp->dma_in = temac_dma_in32_le;
1549			lp->dma_out = temac_dma_out32_le;
1550		} else {
1551			lp->dma_in = temac_dma_in32_be;
1552			lp->dma_out = temac_dma_out32_be;
1553		}
1554
1555		/* Get DMA RX and TX interrupts */
1556		lp->rx_irq = platform_get_irq(pdev, 0);
1557		lp->tx_irq = platform_get_irq(pdev, 1);
1558
1559		/* IRQ delay/coalescing setup */
1560		if (pdata->tx_irq_timeout || pdata->tx_irq_count) {
1561			lp->coalesce_delay_tx = pdata->tx_irq_timeout;
1562			lp->coalesce_count_tx = pdata->tx_irq_count;
1563		}
1564		if (pdata->rx_irq_timeout || pdata->rx_irq_count) {
1565			lp->coalesce_delay_rx = pdata->rx_irq_timeout;
1566			lp->coalesce_count_rx = pdata->rx_irq_count;
1567		}
1568	}
1569
1570	/* Error handle returned DMA RX and TX interrupts */
1571	if (lp->rx_irq < 0)
1572		return dev_err_probe(&pdev->dev, lp->rx_irq,
1573				     "could not get DMA RX irq\n");
1574	if (lp->tx_irq < 0)
1575		return dev_err_probe(&pdev->dev, lp->tx_irq,
1576				     "could not get DMA TX irq\n");
1577
1578	if (temac_np) {
1579		/* Retrieve the MAC address */
1580		rc = of_get_mac_address(temac_np, addr);
1581		if (rc) {
1582			dev_err(&pdev->dev, "could not find MAC address\n");
1583			return -ENODEV;
1584		}
1585		temac_init_mac_address(ndev, addr);
1586	} else if (pdata) {
1587		temac_init_mac_address(ndev, pdata->mac_addr);
1588	}
 
1589
1590	rc = temac_mdio_setup(lp, pdev);
1591	if (rc)
1592		dev_warn(&pdev->dev, "error registering MDIO bus\n");
1593
1594	if (temac_np) {
1595		lp->phy_node = of_parse_phandle(temac_np, "phy-handle", 0);
1596		if (lp->phy_node)
1597			dev_dbg(lp->dev, "using PHY node %pOF\n", temac_np);
1598	} else if (pdata) {
1599		snprintf(lp->phy_name, sizeof(lp->phy_name),
1600			 PHY_ID_FMT, lp->mii_bus->id, pdata->phy_addr);
1601		lp->phy_interface = pdata->phy_interface;
1602	}
1603
1604	/* Add the device attributes */
1605	rc = sysfs_create_group(&lp->dev->kobj, &temac_attr_group);
1606	if (rc) {
1607		dev_err(lp->dev, "Error creating sysfs files\n");
1608		goto err_sysfs_create;
1609	}
1610
1611	rc = register_netdev(lp->ndev);
1612	if (rc) {
1613		dev_err(lp->dev, "register_netdev() error (%i)\n", rc);
1614		goto err_register_ndev;
1615	}
1616
1617	return 0;
1618
1619err_register_ndev:
1620	sysfs_remove_group(&lp->dev->kobj, &temac_attr_group);
1621err_sysfs_create:
1622	if (lp->phy_node)
1623		of_node_put(lp->phy_node);
1624	temac_mdio_teardown(lp);
 
 
 
 
1625	return rc;
1626}
1627
1628static int temac_remove(struct platform_device *pdev)
1629{
1630	struct net_device *ndev = platform_get_drvdata(pdev);
1631	struct temac_local *lp = netdev_priv(ndev);
1632
 
1633	unregister_netdev(ndev);
1634	sysfs_remove_group(&lp->dev->kobj, &temac_attr_group);
1635	if (lp->phy_node)
1636		of_node_put(lp->phy_node);
1637	temac_mdio_teardown(lp);
 
 
 
 
1638	return 0;
1639}
1640
1641static const struct of_device_id temac_of_match[] = {
1642	{ .compatible = "xlnx,xps-ll-temac-1.01.b", },
1643	{ .compatible = "xlnx,xps-ll-temac-2.00.a", },
1644	{ .compatible = "xlnx,xps-ll-temac-2.02.a", },
1645	{ .compatible = "xlnx,xps-ll-temac-2.03.a", },
1646	{},
1647};
1648MODULE_DEVICE_TABLE(of, temac_of_match);
1649
1650static struct platform_driver temac_driver = {
1651	.probe = temac_probe,
1652	.remove = temac_remove,
1653	.driver = {
 
1654		.name = "xilinx_temac",
1655		.of_match_table = temac_of_match,
1656	},
1657};
1658
1659module_platform_driver(temac_driver);
1660
1661MODULE_DESCRIPTION("Xilinx LL_TEMAC Ethernet driver");
1662MODULE_AUTHOR("Yoshio Kashiwagi");
1663MODULE_LICENSE("GPL");