ll_temac_main.c 44.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*
 * Driver for Xilinx TEMAC Ethernet device
 *
 * Copyright (c) 2008 Nissin Systems Co., Ltd.,  Yoshio Kashiwagi
 * Copyright (c) 2005-2008 DLA Systems,  David H. Lynch Jr. <dhlii@dlasys.net>
 * Copyright (c) 2008-2009 Secret Lab Technologies Ltd.
 *
 * This is a driver for the Xilinx ll_temac ipcore which is often used
 * in the Virtex and Spartan series of chips.
 *
 * Notes:
 * - The ll_temac hardware uses indirect access for many of the TEMAC
 *   registers, include the MDIO bus.  However, indirect access to MDIO
 *   registers take considerably more clock cycles than to TEMAC registers.
 *   MDIO accesses are long, so threads doing them should probably sleep
 *   rather than busywait.  However, since only one indirect access can be
 *   in progress at any given time, that means that *all* indirect accesses
 *   could end up sleeping (to wait for an MDIO access to complete).
 *   Fortunately none of the indirect accesses are on the 'hot' path for tx
 *   or rx, so this should be okay.
 *
 * TODO:
 * - Factor out locallink DMA code into separate driver
 * - Fix support for hardware checksumming.
 * - Testing.  Lots and lots of testing.
 *
 */

#include <linux/delay.h>
#include <linux/etherdevice.h>
#include <linux/mii.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/netdevice.h>
36
#include <linux/if_ether.h>
37 38
#include <linux/of.h>
#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 45 46 47 48 49 50 51
#include <linux/skbuff.h>
#include <linux/spinlock.h>
#include <linux/tcp.h>      /* needed for sizeof(tcphdr) */
#include <linux/udp.h>      /* needed for sizeof(udphdr) */
#include <linux/phy.h>
#include <linux/in.h>
#include <linux/io.h>
#include <linux/ip.h>
52
#include <linux/slab.h>
S
Stephen Rothwell 已提交
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 60

#include "ll_temac.h"

61 62 63 64 65
/* Descriptors defines for Tx and Rx DMA */
#define TX_BD_NUM_DEFAULT		64
#define RX_BD_NUM_DEFAULT		1024
#define TX_BD_NUM_MAX			4096
#define RX_BD_NUM_MAX			4096
66 67 68 69 70

/* ---------------------------------------------------------------------
 * Low level register access functions
 */

71
static u32 _temac_ior_be(struct temac_local *lp, int offset)
72
{
73
	return ioread32be(lp->regs + offset);
74 75
}

76
static void _temac_iow_be(struct temac_local *lp, int offset, u32 value)
77
{
78 79 80
	return iowrite32be(value, lp->regs + offset);
}

81
static u32 _temac_ior_le(struct temac_local *lp, int offset)
82 83 84 85
{
	return ioread32(lp->regs + offset);
}

86
static void _temac_iow_le(struct temac_local *lp, int offset, u32 value)
87 88
{
	return iowrite32(value, lp->regs + offset);
89 90
}

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
static bool hard_acs_rdy(struct temac_local *lp)
{
	return temac_ior(lp, XTE_RDY0_OFFSET) & XTE_RDY0_HARD_ACS_RDY_MASK;
}

static bool hard_acs_rdy_or_timeout(struct temac_local *lp, ktime_t timeout)
{
	ktime_t cur = ktime_get();

	return hard_acs_rdy(lp) || ktime_after(cur, timeout);
}

/* Poll for maximum 20 ms.  This is similar to the 2 jiffies @ 100 Hz
 * that was used before, and should cover MDIO bus speed down to 3200
 * Hz.
 */
#define HARD_ACS_RDY_POLL_NS (20 * NSEC_PER_MSEC)

/**
 * temac_indirect_busywait - Wait for current indirect register access
 * to complete.
 */
113 114
int temac_indirect_busywait(struct temac_local *lp)
{
115
	ktime_t timeout = ktime_add_ns(ktime_get(), HARD_ACS_RDY_POLL_NS);
116

117 118 119 120 121
	spin_until_cond(hard_acs_rdy_or_timeout(lp, timeout));
	if (WARN_ON(!hard_acs_rdy(lp)))
		return -ETIMEDOUT;
	else
		return 0;
122 123 124
}

/**
125 126
 * temac_indirect_in32 - Indirect register read access.  This function
 * must be called without lp->indirect_lock being held.
127 128 129
 */
u32 temac_indirect_in32(struct temac_local *lp, int reg)
{
130 131
	unsigned long flags;
	int val;
132

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	spin_lock_irqsave(lp->indirect_lock, flags);
	val = temac_indirect_in32_locked(lp, reg);
	spin_unlock_irqrestore(lp->indirect_lock, flags);
	return val;
}

/**
 * temac_indirect_in32_locked - Indirect register read access.  This
 * function must be called with lp->indirect_lock being held.  Use
 * this together with spin_lock_irqsave/spin_lock_irqrestore to avoid
 * repeated lock/unlock and to ensure uninterrupted access to indirect
 * registers.
 */
u32 temac_indirect_in32_locked(struct temac_local *lp, int reg)
{
	/* This initial wait should normally not spin, as we always
	 * try to wait for indirect access to complete before
	 * releasing the indirect_lock.
	 */
	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 157 158 159 160 161
	/* Wait for indirect register access to complete.  We really
	 * should not see timeouts, and could even end up causing
	 * problem for following indirect access, so let's make a bit
	 * of WARN noise.
	 */
	if (WARN_ON(temac_indirect_busywait(lp)))
162
		return -ETIMEDOUT;
163 164
	/* Value is ready now */
	return temac_ior(lp, XTE_LSW0_OFFSET);
165 166 167
}

/**
168 169
 * temac_indirect_out32 - Indirect register write access.  This function
 * must be called without lp->indirect_lock being held.
170 171 172
 */
void temac_indirect_out32(struct temac_local *lp, int reg, u32 value)
{
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
	unsigned long flags;

	spin_lock_irqsave(lp->indirect_lock, flags);
	temac_indirect_out32_locked(lp, reg, value);
	spin_unlock_irqrestore(lp->indirect_lock, flags);
}

/**
 * temac_indirect_out32_locked - Indirect register write access.  This
 * function must be called with lp->indirect_lock being held.  Use
 * this together with spin_lock_irqsave/spin_lock_irqrestore to avoid
 * repeated lock/unlock and to ensure uninterrupted access to indirect
 * registers.
 */
void temac_indirect_out32_locked(struct temac_local *lp, int reg, u32 value)
{
	/* As in temac_indirect_in32_locked(), we should normally not
	 * spin here.  And if it happens, we actually end up silently
	 * ignoring the write request.  Ouch.
	 */
	if (WARN_ON(temac_indirect_busywait(lp)))
194
		return;
195
	/* Initiate write to indirect register */
196 197
	temac_iow(lp, XTE_LSW0_OFFSET, value);
	temac_iow(lp, XTE_CTL0_OFFSET, CNTLREG_WRITE_ENABLE_MASK | reg);
198 199 200 201 202
	/* As in temac_indirect_in32_locked(), we should not see timeouts
	 * here.  And if it happens, we continue before the write has
	 * completed.  Not good.
	 */
	WARN_ON(temac_indirect_busywait(lp));
203 204
}

205
/**
206 207 208 209
 * temac_dma_in32_* - Memory mapped DMA read, these function expects a
 * register input that is based on DCR word addresses which are then
 * converted to memory mapped byte addresses.  To be assigned to
 * lp->dma_in32.
210
 */
211
static u32 temac_dma_in32_be(struct temac_local *lp, int reg)
212
{
213 214 215 216 217 218
	return ioread32be(lp->sdma_regs + (reg << 2));
}

static u32 temac_dma_in32_le(struct temac_local *lp, int reg)
{
	return ioread32(lp->sdma_regs + (reg << 2));
219 220
}

221
/**
222 223 224 225
 * temac_dma_out32_* - Memory mapped DMA read, these function expects
 * a register input that is based on DCR word addresses which are then
 * converted to memory mapped byte addresses.  To be assigned to
 * lp->dma_out32.
226
 */
227 228 229 230 231 232
static void temac_dma_out32_be(struct temac_local *lp, int reg, u32 value)
{
	iowrite32be(value, lp->sdma_regs + (reg << 2));
}

static 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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
}

/* DMA register access functions can be DCR based or memory mapped.
 * The PowerPC 440 is DCR based, the PowerPC 405 and MicroBlaze are both
 * memory mapped.
 */
#ifdef CONFIG_PPC_DCR

/**
 * temac_dma_dcr_in32 - DCR based DMA read
 */
static u32 temac_dma_dcr_in(struct temac_local *lp, int reg)
{
	return dcr_read(lp->sdma_dcrs, reg);
}

/**
 * temac_dma_dcr_out32 - DCR based DMA write
 */
static void temac_dma_dcr_out(struct temac_local *lp, int reg, u32 value)
255 256 257 258
{
	dcr_write(lp->sdma_dcrs, reg, value);
}

259 260 261 262
/**
 * temac_dcr_setup - If the DMA is DCR based, then setup the address and
 * I/O  functions
 */
263
static int temac_dcr_setup(struct temac_local *lp, struct platform_device *op,
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
				struct device_node *np)
{
	unsigned int dcrs;

	/* setup the dcr address mapping if it's in the device tree */

	dcrs = dcr_resource_start(np, 0);
	if (dcrs != 0) {
		lp->sdma_dcrs = dcr_map(np, dcrs, dcr_resource_len(np, 0));
		lp->dma_in = temac_dma_dcr_in;
		lp->dma_out = temac_dma_dcr_out;
		dev_dbg(&op->dev, "DCR base: %x\n", dcrs);
		return 0;
	}
	/* no DCR in the device tree, indicate a failure */
	return -1;
}

#else

/*
 * temac_dcr_setup - This is a stub for when DCR is not supported,
286
 * such as with MicroBlaze and x86
287
 */
288
static int temac_dcr_setup(struct temac_local *lp, struct platform_device *op,
289 290 291 292 293 294 295
				struct device_node *np)
{
	return -1;
}

#endif

296
/**
297
 * temac_dma_bd_release - Release buffer descriptor rings
298 299 300 301 302 303
 */
static void temac_dma_bd_release(struct net_device *ndev)
{
	struct temac_local *lp = netdev_priv(ndev);
	int i;

304 305 306
	/* Reset Local Link (DMA) */
	lp->dma_out(lp, DMA_CONTROL_REG, DMA_CONTROL_RST);

307
	for (i = 0; i < lp->rx_bd_num; i++) {
308 309 310 311 312 313 314 315 316 317
		if (!lp->rx_skb[i])
			break;
		else {
			dma_unmap_single(ndev->dev.parent, lp->rx_bd_v[i].phys,
					XTE_MAX_JUMBO_FRAME_SIZE, DMA_FROM_DEVICE);
			dev_kfree_skb(lp->rx_skb[i]);
		}
	}
	if (lp->rx_bd_v)
		dma_free_coherent(ndev->dev.parent,
318 319
				  sizeof(*lp->rx_bd_v) * lp->rx_bd_num,
				  lp->rx_bd_v, lp->rx_bd_p);
320 321
	if (lp->tx_bd_v)
		dma_free_coherent(ndev->dev.parent,
322 323
				  sizeof(*lp->tx_bd_v) * lp->tx_bd_num,
				  lp->tx_bd_v, lp->tx_bd_p);
324 325
}

326 327 328 329 330 331 332
/**
 * temac_dma_bd_init - Setup buffer descriptor rings
 */
static int temac_dma_bd_init(struct net_device *ndev)
{
	struct temac_local *lp = netdev_priv(ndev);
	struct sk_buff *skb;
333
	dma_addr_t skb_dma_addr;
334 335
	int i;

336 337
	lp->rx_skb = devm_kcalloc(&ndev->dev, lp->rx_bd_num,
				  sizeof(*lp->rx_skb), GFP_KERNEL);
338
	if (!lp->rx_skb)
339
		goto out;
340

341
	/* allocate the tx and rx ring buffer descriptors. */
342
	/* returns a virtual address and a physical address. */
343
	lp->tx_bd_v = dma_alloc_coherent(ndev->dev.parent,
344
					 sizeof(*lp->tx_bd_v) * lp->tx_bd_num,
345
					 &lp->tx_bd_p, GFP_KERNEL);
346
	if (!lp->tx_bd_v)
347
		goto out;
348

349
	lp->rx_bd_v = dma_alloc_coherent(ndev->dev.parent,
350
					 sizeof(*lp->rx_bd_v) * lp->rx_bd_num,
351
					 &lp->rx_bd_p, GFP_KERNEL);
352
	if (!lp->rx_bd_v)
353
		goto out;
354

355
	for (i = 0; i < lp->tx_bd_num; i++) {
356
		lp->tx_bd_v[i].next = cpu_to_be32(lp->tx_bd_p
357
			+ sizeof(*lp->tx_bd_v) * ((i + 1) % lp->tx_bd_num));
358 359
	}

360
	for (i = 0; i < lp->rx_bd_num; i++) {
361
		lp->rx_bd_v[i].next = cpu_to_be32(lp->rx_bd_p
362
			+ sizeof(*lp->rx_bd_v) * ((i + 1) % lp->rx_bd_num));
363

364 365
		skb = netdev_alloc_skb_ip_align(ndev,
						XTE_MAX_JUMBO_FRAME_SIZE);
366
		if (!skb)
367
			goto out;
368

369 370
		lp->rx_skb[i] = skb;
		/* returns physical address of skb->data */
371 372 373
		skb_dma_addr = dma_map_single(ndev->dev.parent, skb->data,
					      XTE_MAX_JUMBO_FRAME_SIZE,
					      DMA_FROM_DEVICE);
374 375
		if (dma_mapping_error(ndev->dev.parent, skb_dma_addr))
			goto out;
376 377 378
		lp->rx_bd_v[i].phys = cpu_to_be32(skb_dma_addr);
		lp->rx_bd_v[i].len = cpu_to_be32(XTE_MAX_JUMBO_FRAME_SIZE);
		lp->rx_bd_v[i].app0 = cpu_to_be32(STS_CTRL_APP0_IRQONEND);
379 380
	}

381
	/* Configure DMA channel (irq setup) */
382 383
	lp->dma_out(lp, TX_CHNL_CTRL,
		    lp->coalesce_delay_tx << 24 | lp->coalesce_count_tx << 16 |
384 385 386
		    0x00000400 | // Use 1 Bit Wide Counters. Currently Not Used!
		    CHNL_CTRL_IRQ_EN | CHNL_CTRL_IRQ_ERR_EN |
		    CHNL_CTRL_IRQ_DLY_EN | CHNL_CTRL_IRQ_COAL_EN);
387 388
	lp->dma_out(lp, RX_CHNL_CTRL,
		    lp->coalesce_delay_rx << 24 | lp->coalesce_count_rx << 16 |
389 390 391
		    CHNL_CTRL_IRQ_IOE |
		    CHNL_CTRL_IRQ_EN | CHNL_CTRL_IRQ_ERR_EN |
		    CHNL_CTRL_IRQ_DLY_EN | CHNL_CTRL_IRQ_COAL_EN);
392

393 394 395 396
	/* Init descriptor indexes */
	lp->tx_bd_ci = 0;
	lp->tx_bd_tail = 0;
	lp->rx_bd_ci = 0;
397
	lp->rx_bd_tail = lp->rx_bd_num - 1;
398

399 400 401 402
	/* Enable RX DMA transfers */
	wmb();
	lp->dma_out(lp, RX_CURDESC_PTR,  lp->rx_bd_p);
	lp->dma_out(lp, RX_TAILDESC_PTR,
403
		       lp->rx_bd_p + (sizeof(*lp->rx_bd_v) * lp->rx_bd_tail));
404 405 406 407

	/* Prepare for TX DMA transfer */
	lp->dma_out(lp, TX_CURDESC_PTR, lp->tx_bd_p);

408
	return 0;
409 410

out:
411
	temac_dma_bd_release(ndev);
412
	return -ENOMEM;
413 414 415 416 417 418
}

/* ---------------------------------------------------------------------
 * net_device_ops
 */

J
Jiri Pirko 已提交
419
static void temac_do_set_mac_address(struct net_device *ndev)
420 421
{
	struct temac_local *lp = netdev_priv(ndev);
422
	unsigned long flags;
423 424

	/* set up unicast MAC address filter set its mac address */
425 426 427 428 429 430
	spin_lock_irqsave(lp->indirect_lock, flags);
	temac_indirect_out32_locked(lp, XTE_UAW0_OFFSET,
				    (ndev->dev_addr[0]) |
				    (ndev->dev_addr[1] << 8) |
				    (ndev->dev_addr[2] << 16) |
				    (ndev->dev_addr[3] << 24));
431 432
	/* There are reserved bits in EUAW1
	 * so don't affect them Set MAC bits [47:32] in EUAW1 */
433 434 435 436
	temac_indirect_out32_locked(lp, XTE_UAW1_OFFSET,
				    (ndev->dev_addr[4] & 0x000000ff) |
				    (ndev->dev_addr[5] << 8));
	spin_unlock_irqrestore(lp->indirect_lock, flags);
J
Jiri Pirko 已提交
437
}
438

439
static int temac_init_mac_address(struct net_device *ndev, const void *address)
J
Jiri Pirko 已提交
440
{
441
	ether_addr_copy(ndev->dev_addr, address);
J
Jiri Pirko 已提交
442 443 444
	if (!is_valid_ether_addr(ndev->dev_addr))
		eth_hw_addr_random(ndev);
	temac_do_set_mac_address(ndev);
445 446 447
	return 0;
}

J
Jiri Pirko 已提交
448
static int temac_set_mac_address(struct net_device *ndev, void *p)
449 450 451
{
	struct sockaddr *addr = p;

J
Jiri Pirko 已提交
452 453 454 455 456
	if (!is_valid_ether_addr(addr->sa_data))
		return -EADDRNOTAVAIL;
	memcpy(ndev->dev_addr, addr->sa_data, ETH_ALEN);
	temac_do_set_mac_address(ndev);
	return 0;
457 458
}

459 460 461
static void temac_set_multicast_list(struct net_device *ndev)
{
	struct temac_local *lp = netdev_priv(ndev);
462
	u32 multi_addr_msw, multi_addr_lsw;
463
	int i = 0;
464 465
	unsigned long flags;
	bool promisc_mode_disabled = false;
466

467 468
	if (ndev->flags & (IFF_PROMISC | IFF_ALLMULTI) ||
	    (netdev_mc_count(ndev) > MULTICAST_CAM_TABLE_NUM)) {
469 470
		temac_indirect_out32(lp, XTE_AFM_OFFSET, XTE_AFM_EPPRM_MASK);
		dev_info(&ndev->dev, "Promiscuous mode enabled.\n");
471 472 473 474 475 476
		return;
	}

	spin_lock_irqsave(lp->indirect_lock, flags);

	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 483 484 485
			multi_addr_msw = ((ha->addr[3] << 24) |
					  (ha->addr[2] << 16) |
					  (ha->addr[1] << 8) |
					  (ha->addr[0]));
486 487
			temac_indirect_out32_locked(lp, XTE_MAW0_OFFSET,
						    multi_addr_msw);
488 489
			multi_addr_lsw = ((ha->addr[5] << 8) |
					  (ha->addr[4]) | (i << 16));
490 491
			temac_indirect_out32_locked(lp, XTE_MAW1_OFFSET,
						    multi_addr_lsw);
492
			i++;
493
		}
494 495 496 497
	}

	/* Clear all or remaining/unused address table entries */
	while (i < MULTICAST_CAM_TABLE_NUM) {
498 499
		temac_indirect_out32_locked(lp, XTE_MAW0_OFFSET, 0);
		temac_indirect_out32_locked(lp, XTE_MAW1_OFFSET, i << 16);
500
		i++;
501 502 503 504 505 506 507
	}

	/* Enable address filter block if currently disabled */
	if (temac_indirect_in32_locked(lp, XTE_AFM_OFFSET)
	    & XTE_AFM_EPPRM_MASK) {
		temac_indirect_out32_locked(lp, XTE_AFM_OFFSET, 0);
		promisc_mode_disabled = true;
508
	}
509 510 511 512 513

	spin_unlock_irqrestore(lp->indirect_lock, flags);

	if (promisc_mode_disabled)
		dev_info(&ndev->dev, "Promiscuous mode disabled.\n");
514 515
}

516
static struct temac_option {
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
	int flg;
	u32 opt;
	u32 reg;
	u32 m_or;
	u32 m_and;
} temac_options[] = {
	/* Turn on jumbo packet support for both Rx and Tx */
	{
		.opt = XTE_OPTION_JUMBO,
		.reg = XTE_TXC_OFFSET,
		.m_or = XTE_TXC_TXJMBO_MASK,
	},
	{
		.opt = XTE_OPTION_JUMBO,
		.reg = XTE_RXC1_OFFSET,
		.m_or =XTE_RXC1_RXJMBO_MASK,
	},
	/* Turn on VLAN packet support for both Rx and Tx */
	{
		.opt = XTE_OPTION_VLAN,
		.reg = XTE_TXC_OFFSET,
		.m_or =XTE_TXC_TXVLAN_MASK,
	},
	{
		.opt = XTE_OPTION_VLAN,
		.reg = XTE_RXC1_OFFSET,
		.m_or =XTE_RXC1_RXVLAN_MASK,
	},
	/* Turn on FCS stripping on receive packets */
	{
		.opt = XTE_OPTION_FCS_STRIP,
		.reg = XTE_RXC1_OFFSET,
		.m_or =XTE_RXC1_RXFCS_MASK,
	},
	/* Turn on FCS insertion on transmit packets */
	{
		.opt = XTE_OPTION_FCS_INSERT,
		.reg = XTE_TXC_OFFSET,
		.m_or =XTE_TXC_TXFCS_MASK,
	},
	/* Turn on length/type field checking on receive packets */
	{
		.opt = XTE_OPTION_LENTYPE_ERR,
		.reg = XTE_RXC1_OFFSET,
		.m_or =XTE_RXC1_RXLT_MASK,
	},
	/* Turn on flow control */
	{
		.opt = XTE_OPTION_FLOW_CONTROL,
		.reg = XTE_FCC_OFFSET,
		.m_or =XTE_FCC_RXFLO_MASK,
	},
	/* Turn on flow control */
	{
		.opt = XTE_OPTION_FLOW_CONTROL,
		.reg = XTE_FCC_OFFSET,
		.m_or =XTE_FCC_TXFLO_MASK,
	},
	/* Turn on promiscuous frame filtering (all frames are received ) */
	{
		.opt = XTE_OPTION_PROMISC,
		.reg = XTE_AFM_OFFSET,
		.m_or =XTE_AFM_EPPRM_MASK,
	},
	/* Enable transmitter if not already enabled */
	{
		.opt = XTE_OPTION_TXEN,
		.reg = XTE_TXC_OFFSET,
		.m_or =XTE_TXC_TXEN_MASK,
	},
	/* Enable receiver? */
	{
		.opt = XTE_OPTION_RXEN,
		.reg = XTE_RXC1_OFFSET,
		.m_or =XTE_RXC1_RXEN_MASK,
	},
	{}
};

/**
 * temac_setoptions
 */
static u32 temac_setoptions(struct net_device *ndev, u32 options)
{
	struct temac_local *lp = netdev_priv(ndev);
	struct temac_option *tp = &temac_options[0];
	int reg;
604
	unsigned long flags;
605

606
	spin_lock_irqsave(lp->indirect_lock, flags);
607
	while (tp->opt) {
608 609
		reg = temac_indirect_in32_locked(lp, tp->reg) & ~tp->m_or;
		if (options & tp->opt) {
610
			reg |= tp->m_or;
611 612
			temac_indirect_out32_locked(lp, tp->reg, reg);
		}
613 614
		tp++;
	}
615
	spin_unlock_irqrestore(lp->indirect_lock, flags);
616 617
	lp->options |= options;

618
	return 0;
619 620
}

621
/* Initialize temac */
622 623 624 625 626
static void temac_device_reset(struct net_device *ndev)
{
	struct temac_local *lp = netdev_priv(ndev);
	u32 timeout;
	u32 val;
627
	unsigned long flags;
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660

	/* Perform a software reset */

	/* 0x300 host enable bit ? */
	/* reset PHY through control register ?:1 */

	dev_dbg(&ndev->dev, "%s()\n", __func__);

	/* Reset the receiver and wait for it to finish reset */
	temac_indirect_out32(lp, XTE_RXC1_OFFSET, XTE_RXC1_RXRST_MASK);
	timeout = 1000;
	while (temac_indirect_in32(lp, XTE_RXC1_OFFSET) & XTE_RXC1_RXRST_MASK) {
		udelay(1);
		if (--timeout == 0) {
			dev_err(&ndev->dev,
				"temac_device_reset RX reset timeout!!\n");
			break;
		}
	}

	/* Reset the transmitter and wait for it to finish reset */
	temac_indirect_out32(lp, XTE_TXC_OFFSET, XTE_TXC_TXRST_MASK);
	timeout = 1000;
	while (temac_indirect_in32(lp, XTE_TXC_OFFSET) & XTE_TXC_TXRST_MASK) {
		udelay(1);
		if (--timeout == 0) {
			dev_err(&ndev->dev,
				"temac_device_reset TX reset timeout!!\n");
			break;
		}
	}

	/* Disable the receiver */
661 662 663 664 665
	spin_lock_irqsave(lp->indirect_lock, flags);
	val = temac_indirect_in32_locked(lp, XTE_RXC1_OFFSET);
	temac_indirect_out32_locked(lp, XTE_RXC1_OFFSET,
				    val & ~XTE_RXC1_RXEN_MASK);
	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 672 673 674 675 676 677
		udelay(1);
		if (--timeout == 0) {
			dev_err(&ndev->dev,
				"temac_device_reset DMA reset timeout!!\n");
			break;
		}
	}
678
	lp->dma_out(lp, DMA_CONTROL_REG, DMA_TAIL_ENABLE);
679

680 681 682 683
	if (temac_dma_bd_init(ndev)) {
		dev_err(&ndev->dev,
				"temac_device_reset descriptor allocation failed\n");
	}
684

685 686 687 688 689 690
	spin_lock_irqsave(lp->indirect_lock, flags);
	temac_indirect_out32_locked(lp, XTE_RXC0_OFFSET, 0);
	temac_indirect_out32_locked(lp, XTE_RXC1_OFFSET, 0);
	temac_indirect_out32_locked(lp, XTE_TXC_OFFSET, 0);
	temac_indirect_out32_locked(lp, XTE_FCC_OFFSET, XTE_FCC_RXFLO_MASK);
	spin_unlock_irqrestore(lp->indirect_lock, flags);
691 692 693 694 695 696

	/* Sync default options with HW
	 * but leave receiver and transmitter disabled.  */
	temac_setoptions(ndev,
			 lp->options & ~(XTE_OPTION_TXEN | XTE_OPTION_RXEN));

J
Jiri Pirko 已提交
697
	temac_do_set_mac_address(ndev);
698 699 700 701 702 703 704

	/* Set address filter table */
	temac_set_multicast_list(ndev);
	if (temac_setoptions(ndev, lp->options))
		dev_err(&ndev->dev, "Error setting TEMAC options\n");

	/* Init Driver variable */
705
	netif_trans_update(ndev); /* prevent tx timeout */
706 707
}

708
static void temac_adjust_link(struct net_device *ndev)
709 710
{
	struct temac_local *lp = netdev_priv(ndev);
711
	struct phy_device *phy = ndev->phydev;
712 713
	u32 mii_speed;
	int link_state;
714
	unsigned long flags;
715 716 717 718 719

	/* hash together the state values to decide if something has changed */
	link_state = phy->speed | (phy->duplex << 1) | phy->link;

	if (lp->last_link != link_state) {
720 721
		spin_lock_irqsave(lp->indirect_lock, flags);
		mii_speed = temac_indirect_in32_locked(lp, XTE_EMCFG_OFFSET);
722 723 724 725 726 727 728 729 730
		mii_speed &= ~XTE_EMCFG_LINKSPD_MASK;

		switch (phy->speed) {
		case SPEED_1000: mii_speed |= XTE_EMCFG_LINKSPD_1000; break;
		case SPEED_100: mii_speed |= XTE_EMCFG_LINKSPD_100; break;
		case SPEED_10: mii_speed |= XTE_EMCFG_LINKSPD_10; break;
		}

		/* Write new speed setting out to TEMAC */
731 732 733
		temac_indirect_out32_locked(lp, XTE_EMCFG_OFFSET, mii_speed);
		spin_unlock_irqrestore(lp->indirect_lock, flags);

734 735 736 737 738
		lp->last_link = link_state;
		phy_print_status(phy);
	}
}

739 740
#ifdef CONFIG_64BIT

741
static void ptr_to_txbd(void *p, struct cdmac_bd *bd)
742 743 744 745 746
{
	bd->app3 = (u32)(((u64)p) >> 32);
	bd->app4 = (u32)((u64)p & 0xFFFFFFFF);
}

747
static void *ptr_from_txbd(struct cdmac_bd *bd)
748 749 750 751 752 753
{
	return (void *)(((u64)(bd->app3) << 32) | bd->app4);
}

#else

754
static void ptr_to_txbd(void *p, struct cdmac_bd *bd)
755 756 757 758
{
	bd->app4 = (u32)p;
}

759
static void *ptr_from_txbd(struct cdmac_bd *bd)
760 761 762 763 764 765
{
	return (void *)(bd->app4);
}

#endif

766 767 768 769 770
static void temac_start_xmit_done(struct net_device *ndev)
{
	struct temac_local *lp = netdev_priv(ndev);
	struct cdmac_bd *cur_p;
	unsigned int stat = 0;
771
	struct sk_buff *skb;
772 773

	cur_p = &lp->tx_bd_v[lp->tx_bd_ci];
774
	stat = be32_to_cpu(cur_p->app0);
775 776

	while (stat & STS_CTRL_APP0_CMPLT) {
777 778
		dma_unmap_single(ndev->dev.parent, be32_to_cpu(cur_p->phys),
				 be32_to_cpu(cur_p->len), DMA_TO_DEVICE);
779 780 781
		skb = (struct sk_buff *)ptr_from_txbd(cur_p);
		if (skb)
			dev_consume_skb_irq(skb);
782
		cur_p->app0 = 0;
783 784 785 786
		cur_p->app1 = 0;
		cur_p->app2 = 0;
		cur_p->app3 = 0;
		cur_p->app4 = 0;
787 788

		ndev->stats.tx_packets++;
789
		ndev->stats.tx_bytes += be32_to_cpu(cur_p->len);
790 791

		lp->tx_bd_ci++;
792
		if (lp->tx_bd_ci >= lp->tx_bd_num)
793 794 795
			lp->tx_bd_ci = 0;

		cur_p = &lp->tx_bd_v[lp->tx_bd_ci];
796
		stat = be32_to_cpu(cur_p->app0);
797 798
	}

799 800 801
	/* Matches barrier in temac_start_xmit */
	smp_mb();

802 803 804
	netif_wake_queue(ndev);
}

805 806 807 808 809 810 811 812 813 814 815 816 817
static inline int temac_check_tx_bd_space(struct temac_local *lp, int num_frag)
{
	struct cdmac_bd *cur_p;
	int tail;

	tail = lp->tx_bd_tail;
	cur_p = &lp->tx_bd_v[tail];

	do {
		if (cur_p->app0)
			return NETDEV_TX_BUSY;

		tail++;
818
		if (tail >= lp->tx_bd_num)
819 820 821 822 823 824 825 826 827
			tail = 0;

		cur_p = &lp->tx_bd_v[tail];
		num_frag--;
	} while (num_frag >= 0);

	return 0;
}

828 829
static netdev_tx_t
temac_start_xmit(struct sk_buff *skb, struct net_device *ndev)
830 831 832
{
	struct temac_local *lp = netdev_priv(ndev);
	struct cdmac_bd *cur_p;
833
	dma_addr_t tail_p, skb_dma_addr;
834 835 836 837 838 839 840 841
	int ii;
	unsigned long num_frag;
	skb_frag_t *frag;

	num_frag = skb_shinfo(skb)->nr_frags;
	frag = &skb_shinfo(skb)->frags[0];
	cur_p = &lp->tx_bd_v[lp->tx_bd_tail];

842
	if (temac_check_tx_bd_space(lp, num_frag + 1)) {
843 844 845 846 847 848 849 850 851 852 853 854 855
		if (netif_queue_stopped(ndev))
			return NETDEV_TX_BUSY;

		netif_stop_queue(ndev);

		/* Matches barrier in temac_start_xmit_done */
		smp_mb();

		/* Space might have just been freed - check again */
		if (temac_check_tx_bd_space(lp, num_frag))
			return NETDEV_TX_BUSY;

		netif_wake_queue(ndev);
856 857 858 859
	}

	cur_p->app0 = 0;
	if (skb->ip_summed == CHECKSUM_PARTIAL) {
860
		unsigned int csum_start_off = skb_checksum_start_offset(skb);
861 862
		unsigned int csum_index_off = csum_start_off + skb->csum_offset;

863 864 865
		cur_p->app0 |= cpu_to_be32(0x000001); /* TX Checksum Enabled */
		cur_p->app1 = cpu_to_be32((csum_start_off << 16)
					  | csum_index_off);
866
		cur_p->app2 = 0;  /* initial checksum seed */
867
	}
868

869 870 871 872
	cur_p->app0 |= cpu_to_be32(STS_CTRL_APP0_SOP);
	skb_dma_addr = dma_map_single(ndev->dev.parent, skb->data,
				      skb_headlen(skb), DMA_TO_DEVICE);
	cur_p->len = cpu_to_be32(skb_headlen(skb));
873 874 875 876 877
	if (WARN_ON_ONCE(dma_mapping_error(ndev->dev.parent, skb_dma_addr))) {
		dev_kfree_skb_any(skb);
		ndev->stats.tx_dropped++;
		return NETDEV_TX_OK;
	}
878
	cur_p->phys = cpu_to_be32(skb_dma_addr);
879
	ptr_to_txbd((void *)skb, cur_p);
880 881

	for (ii = 0; ii < num_frag; ii++) {
882
		if (++lp->tx_bd_tail >= lp->tx_bd_num)
883 884 885
			lp->tx_bd_tail = 0;

		cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
886 887 888 889
		skb_dma_addr = dma_map_single(ndev->dev.parent,
					      skb_frag_address(frag),
					      skb_frag_size(frag),
					      DMA_TO_DEVICE);
890 891
		if (dma_mapping_error(ndev->dev.parent, skb_dma_addr)) {
			if (--lp->tx_bd_tail < 0)
892
				lp->tx_bd_tail = lp->tx_bd_num - 1;
893 894 895 896 897 898 899 900
			cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
			while (--ii >= 0) {
				--frag;
				dma_unmap_single(ndev->dev.parent,
						 be32_to_cpu(cur_p->phys),
						 skb_frag_size(frag),
						 DMA_TO_DEVICE);
				if (--lp->tx_bd_tail < 0)
901
					lp->tx_bd_tail = lp->tx_bd_num - 1;
902 903 904 905 906
				cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
			}
			dma_unmap_single(ndev->dev.parent,
					 be32_to_cpu(cur_p->phys),
					 skb_headlen(skb), DMA_TO_DEVICE);
907 908 909
			dev_kfree_skb_any(skb);
			ndev->stats.tx_dropped++;
			return NETDEV_TX_OK;
910
		}
911 912
		cur_p->phys = cpu_to_be32(skb_dma_addr);
		cur_p->len = cpu_to_be32(skb_frag_size(frag));
913 914 915
		cur_p->app0 = 0;
		frag++;
	}
916
	cur_p->app0 |= cpu_to_be32(STS_CTRL_APP0_EOP);
917 918 919

	tail_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * lp->tx_bd_tail;
	lp->tx_bd_tail++;
920
	if (lp->tx_bd_tail >= lp->tx_bd_num)
921 922
		lp->tx_bd_tail = 0;

923 924
	skb_tx_timestamp(skb);

925
	/* Kick off the transfer */
926
	wmb();
927
	lp->dma_out(lp, TX_TAILDESC_PTR, tail_p); /* DMA start */
928

929
	return NETDEV_TX_OK;
930 931
}

932 933 934 935 936 937 938 939
static int ll_temac_recv_buffers_available(struct temac_local *lp)
{
	int available;

	if (!lp->rx_skb[lp->rx_bd_ci])
		return 0;
	available = 1 + lp->rx_bd_tail - lp->rx_bd_ci;
	if (available <= 0)
940
		available += lp->rx_bd_num;
941 942
	return available;
}
943 944 945 946 947

static void ll_temac_recv(struct net_device *ndev)
{
	struct temac_local *lp = netdev_priv(ndev);
	unsigned long flags;
948 949
	int rx_bd;
	bool update_tail = false;
950 951 952

	spin_lock_irqsave(&lp->rx_lock, flags);

953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
	/* Process all received buffers, passing them on network
	 * stack.  After this, the buffer descriptors will be in an
	 * un-allocated stage, where no skb is allocated for it, and
	 * they are therefore not available for TEMAC/DMA.
	 */
	do {
		struct cdmac_bd *bd = &lp->rx_bd_v[lp->rx_bd_ci];
		struct sk_buff *skb = lp->rx_skb[lp->rx_bd_ci];
		unsigned int bdstat = be32_to_cpu(bd->app0);
		int length;

		/* While this should not normally happen, we can end
		 * here when GFP_ATOMIC allocations fail, and we
		 * therefore have un-allocated buffers.
		 */
		if (!skb)
			break;
970

971 972 973
		/* Loop over all completed buffer descriptors */
		if (!(bdstat & STS_CTRL_APP0_CMPLT))
			break;
974

975
		dma_unmap_single(ndev->dev.parent, be32_to_cpu(bd->phys),
976
				 XTE_MAX_JUMBO_FRAME_SIZE, DMA_FROM_DEVICE);
977 978 979
		/* The buffer is not valid for DMA anymore */
		bd->phys = 0;
		bd->len = 0;
980

981
		length = be32_to_cpu(bd->app4) & 0x3FFF;
982 983
		skb_put(skb, length);
		skb->protocol = eth_type_trans(skb, ndev);
984
		skb_checksum_none_assert(skb);
985

986 987
		/* if we're doing rx csum offload, set it up */
		if (((lp->temac_features & TEMAC_FEATURE_RX_CSUM) != 0) &&
988 989
		    (skb->protocol == htons(ETH_P_IP)) &&
		    (skb->len > 64)) {
990

991 992 993 994 995
			/* Convert from device endianness (be32) to cpu
			 * endiannes, and if necessary swap the bytes
			 * (back) for proper IP checksum byte order
			 * (be16).
			 */
996
			skb->csum = htons(be32_to_cpu(bd->app3) & 0xFFFF);
997 998 999
			skb->ip_summed = CHECKSUM_COMPLETE;
		}

1000 1001
		if (!skb_defer_rx_timestamp(skb))
			netif_rx(skb);
1002 1003
		/* The skb buffer is now owned by network stack above */
		lp->rx_skb[lp->rx_bd_ci] = NULL;
1004 1005 1006 1007

		ndev->stats.rx_packets++;
		ndev->stats.rx_bytes += length;

1008
		rx_bd = lp->rx_bd_ci;
1009
		if (++lp->rx_bd_ci >= lp->rx_bd_num)
1010 1011 1012
			lp->rx_bd_ci = 0;
	} while (rx_bd != lp->rx_bd_tail);

1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
	/* DMA operations will halt when the last buffer descriptor is
	 * processed (ie. the one pointed to by RX_TAILDESC_PTR).
	 * When that happens, no more interrupt events will be
	 * generated.  No IRQ_COAL or IRQ_DLY, and not even an
	 * IRQ_ERR.  To avoid stalling, we schedule a delayed work
	 * when there is a potential risk of that happening.  The work
	 * will call this function, and thus re-schedule itself until
	 * enough buffers are available again.
	 */
	if (ll_temac_recv_buffers_available(lp) < lp->coalesce_count_rx)
		schedule_delayed_work(&lp->restart_work, HZ / 1000);

1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
	/* Allocate new buffers for those buffer descriptors that were
	 * passed to network stack.  Note that GFP_ATOMIC allocations
	 * can fail (e.g. when a larger burst of GFP_ATOMIC
	 * allocations occurs), so while we try to allocate all
	 * buffers in the same interrupt where they were processed, we
	 * continue with what we could get in case of allocation
	 * failure.  Allocation of remaining buffers will be retried
	 * in following calls.
	 */
	while (1) {
		struct sk_buff *skb;
		struct cdmac_bd *bd;
		dma_addr_t skb_dma_addr;

		rx_bd = lp->rx_bd_tail + 1;
1040
		if (rx_bd >= lp->rx_bd_num)
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
			rx_bd = 0;
		bd = &lp->rx_bd_v[rx_bd];

		if (bd->phys)
			break;	/* All skb's allocated */

		skb = netdev_alloc_skb_ip_align(ndev, XTE_MAX_JUMBO_FRAME_SIZE);
		if (!skb) {
			dev_warn(&ndev->dev, "skb alloc failed\n");
			break;
1051 1052
		}

1053
		skb_dma_addr = dma_map_single(ndev->dev.parent, skb->data,
1054 1055
					      XTE_MAX_JUMBO_FRAME_SIZE,
					      DMA_FROM_DEVICE);
1056 1057 1058 1059 1060
		if (WARN_ON_ONCE(dma_mapping_error(ndev->dev.parent,
						   skb_dma_addr))) {
			dev_kfree_skb_any(skb);
			break;
		}
1061

1062 1063 1064 1065 1066 1067 1068 1069
		bd->phys = cpu_to_be32(skb_dma_addr);
		bd->len = cpu_to_be32(XTE_MAX_JUMBO_FRAME_SIZE);
		bd->app0 = cpu_to_be32(STS_CTRL_APP0_IRQONEND);
		lp->rx_skb[rx_bd] = skb;

		lp->rx_bd_tail = rx_bd;
		update_tail = true;
	}
1070

1071 1072 1073 1074
	/* Move tail pointer when buffers have been allocated */
	if (update_tail) {
		lp->dma_out(lp, RX_TAILDESC_PTR,
			lp->rx_bd_p + sizeof(*lp->rx_bd_v) * lp->rx_bd_tail);
1075 1076 1077 1078 1079
	}

	spin_unlock_irqrestore(&lp->rx_lock, flags);
}

1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
/* Function scheduled to ensure a restart in case of DMA halt
 * condition caused by running out of buffer descriptors.
 */
static void ll_temac_restart_work_func(struct work_struct *work)
{
	struct temac_local *lp = container_of(work, struct temac_local,
					      restart_work.work);
	struct net_device *ndev = lp->ndev;

	ll_temac_recv(ndev);
}

1092 1093 1094 1095 1096 1097
static irqreturn_t ll_temac_tx_irq(int irq, void *_ndev)
{
	struct net_device *ndev = _ndev;
	struct temac_local *lp = netdev_priv(ndev);
	unsigned int status;

1098 1099
	status = lp->dma_in(lp, TX_IRQ_REG);
	lp->dma_out(lp, TX_IRQ_REG, status);
1100 1101 1102

	if (status & (IRQ_COAL | IRQ_DLY))
		temac_start_xmit_done(lp->ndev);
1103 1104 1105 1106
	if (status & (IRQ_ERR | IRQ_DMAERR))
		dev_err_ratelimited(&ndev->dev,
				    "TX error 0x%x TX_CHNL_STS=0x%08x\n",
				    status, lp->dma_in(lp, TX_CHNL_STS));
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117

	return IRQ_HANDLED;
}

static irqreturn_t ll_temac_rx_irq(int irq, void *_ndev)
{
	struct net_device *ndev = _ndev;
	struct temac_local *lp = netdev_priv(ndev);
	unsigned int status;

	/* Read and clear the status registers */
1118 1119
	status = lp->dma_in(lp, RX_IRQ_REG);
	lp->dma_out(lp, RX_IRQ_REG, status);
1120 1121 1122

	if (status & (IRQ_COAL | IRQ_DLY))
		ll_temac_recv(lp->ndev);
1123 1124 1125 1126
	if (status & (IRQ_ERR | IRQ_DMAERR))
		dev_err_ratelimited(&ndev->dev,
				    "RX error 0x%x RX_CHNL_STS=0x%08x\n",
				    status, lp->dma_in(lp, RX_CHNL_STS));
1127 1128 1129 1130 1131 1132 1133

	return IRQ_HANDLED;
}

static int temac_open(struct net_device *ndev)
{
	struct temac_local *lp = netdev_priv(ndev);
1134
	struct phy_device *phydev = NULL;
1135 1136 1137 1138 1139
	int rc;

	dev_dbg(&ndev->dev, "temac_open()\n");

	if (lp->phy_node) {
1140 1141 1142
		phydev = of_phy_connect(lp->ndev, lp->phy_node,
					temac_adjust_link, 0, 0);
		if (!phydev) {
1143 1144 1145
			dev_err(lp->dev, "of_phy_connect() failed\n");
			return -ENODEV;
		}
1146 1147 1148 1149
		phy_start(phydev);
	} else if (strlen(lp->phy_name) > 0) {
		phydev = phy_connect(lp->ndev, lp->phy_name, temac_adjust_link,
				     lp->phy_interface);
1150
		if (IS_ERR(phydev)) {
1151
			dev_err(lp->dev, "phy_connect() failed\n");
1152
			return PTR_ERR(phydev);
1153
		}
1154
		phy_start(phydev);
1155 1156
	}

1157 1158
	temac_device_reset(ndev);

1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
	rc = request_irq(lp->tx_irq, ll_temac_tx_irq, 0, ndev->name, ndev);
	if (rc)
		goto err_tx_irq;
	rc = request_irq(lp->rx_irq, ll_temac_rx_irq, 0, ndev->name, ndev);
	if (rc)
		goto err_rx_irq;

	return 0;

 err_rx_irq:
	free_irq(lp->tx_irq, ndev);
 err_tx_irq:
1171 1172
	if (phydev)
		phy_disconnect(phydev);
1173 1174 1175 1176 1177 1178 1179
	dev_err(lp->dev, "request_irq() failed\n");
	return rc;
}

static int temac_stop(struct net_device *ndev)
{
	struct temac_local *lp = netdev_priv(ndev);
1180
	struct phy_device *phydev = ndev->phydev;
1181 1182 1183

	dev_dbg(&ndev->dev, "temac_close()\n");

1184 1185
	cancel_delayed_work_sync(&lp->restart_work);

1186 1187 1188
	free_irq(lp->tx_irq, ndev);
	free_irq(lp->rx_irq, ndev);

1189 1190
	if (phydev)
		phy_disconnect(phydev);
1191

1192 1193
	temac_dma_bd_release(ndev);

1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
	return 0;
}

#ifdef CONFIG_NET_POLL_CONTROLLER
static void
temac_poll_controller(struct net_device *ndev)
{
	struct temac_local *lp = netdev_priv(ndev);

	disable_irq(lp->tx_irq);
	disable_irq(lp->rx_irq);

M
Michal Simek 已提交
1206 1207
	ll_temac_rx_irq(lp->tx_irq, ndev);
	ll_temac_tx_irq(lp->rx_irq, ndev);
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217

	enable_irq(lp->tx_irq);
	enable_irq(lp->rx_irq);
}
#endif

static const struct net_device_ops temac_netdev_ops = {
	.ndo_open = temac_open,
	.ndo_stop = temac_stop,
	.ndo_start_xmit = temac_start_xmit,
1218
	.ndo_set_rx_mode = temac_set_multicast_list,
J
Jiri Pirko 已提交
1219
	.ndo_set_mac_address = temac_set_mac_address,
1220
	.ndo_validate_addr = eth_validate_addr,
1221
	.ndo_do_ioctl = phy_do_ioctl_running,
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
#ifdef CONFIG_NET_POLL_CONTROLLER
	.ndo_poll_controller = temac_poll_controller,
#endif
};

/* ---------------------------------------------------------------------
 * SYSFS device attributes
 */
static ssize_t temac_show_llink_regs(struct device *dev,
				     struct device_attribute *attr, char *buf)
{
	struct net_device *ndev = dev_get_drvdata(dev);
	struct temac_local *lp = netdev_priv(ndev);
	int i, len = 0;

	for (i = 0; i < 0x11; i++)
1238
		len += sprintf(buf + len, "%.8x%s", lp->dma_in(lp, i),
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
			       (i % 8) == 7 ? "\n" : " ");
	len += sprintf(buf + len, "\n");

	return len;
}

static DEVICE_ATTR(llink_regs, 0440, temac_show_llink_regs, NULL);

static struct attribute *temac_device_attrs[] = {
	&dev_attr_llink_regs.attr,
	NULL,
};

static const struct attribute_group temac_attr_group = {
	.attrs = temac_device_attrs,
};

1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
/* ---------------------------------------------------------------------
 * ethtool support
 */

static void ll_temac_ethtools_get_ringparam(struct net_device *ndev,
					    struct ethtool_ringparam *ering)
{
	struct temac_local *lp = netdev_priv(ndev);

	ering->rx_max_pending = RX_BD_NUM_MAX;
	ering->rx_mini_max_pending = 0;
	ering->rx_jumbo_max_pending = 0;
	ering->tx_max_pending = TX_BD_NUM_MAX;
	ering->rx_pending = lp->rx_bd_num;
	ering->rx_mini_pending = 0;
	ering->rx_jumbo_pending = 0;
	ering->tx_pending = lp->tx_bd_num;
}

static int ll_temac_ethtools_set_ringparam(struct net_device *ndev,
					   struct ethtool_ringparam *ering)
{
	struct temac_local *lp = netdev_priv(ndev);

	if (ering->rx_pending > RX_BD_NUM_MAX ||
	    ering->rx_mini_pending ||
	    ering->rx_jumbo_pending ||
	    ering->rx_pending > TX_BD_NUM_MAX)
		return -EINVAL;

	if (netif_running(ndev))
		return -EBUSY;

	lp->rx_bd_num = ering->rx_pending;
	lp->tx_bd_num = ering->tx_pending;
	return 0;
}

1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
static int ll_temac_ethtools_get_coalesce(struct net_device *ndev,
					  struct ethtool_coalesce *ec)
{
	struct temac_local *lp = netdev_priv(ndev);

	ec->rx_max_coalesced_frames = lp->coalesce_count_rx;
	ec->tx_max_coalesced_frames = lp->coalesce_count_tx;
	ec->rx_coalesce_usecs = (lp->coalesce_delay_rx * 512) / 100;
	ec->tx_coalesce_usecs = (lp->coalesce_delay_tx * 512) / 100;
	return 0;
}

static int ll_temac_ethtools_set_coalesce(struct net_device *ndev,
					  struct ethtool_coalesce *ec)
{
	struct temac_local *lp = netdev_priv(ndev);

	if (netif_running(ndev)) {
		netdev_err(ndev,
			   "Please stop netif before applying configuration\n");
		return -EFAULT;
	}

	if (ec->rx_max_coalesced_frames)
		lp->coalesce_count_rx = ec->rx_max_coalesced_frames;
	if (ec->tx_max_coalesced_frames)
		lp->coalesce_count_tx = ec->tx_max_coalesced_frames;
	/* With typical LocalLink clock speed of 200 MHz and
	 * C_PRESCALAR=1023, each delay count corresponds to 5.12 us.
	 */
	if (ec->rx_coalesce_usecs)
		lp->coalesce_delay_rx =
			min(255U, (ec->rx_coalesce_usecs * 100) / 512);
	if (ec->tx_coalesce_usecs)
		lp->coalesce_delay_tx =
			min(255U, (ec->tx_coalesce_usecs * 100) / 512);

	return 0;
}

R
Ricardo 已提交
1334
static const struct ethtool_ops temac_ethtool_ops = {
1335 1336
	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
				     ETHTOOL_COALESCE_MAX_FRAMES,
1337
	.nway_reset = phy_ethtool_nway_reset,
R
Ricardo 已提交
1338
	.get_link = ethtool_op_get_link,
1339
	.get_ts_info = ethtool_op_get_ts_info,
1340 1341
	.get_link_ksettings = phy_ethtool_get_link_ksettings,
	.set_link_ksettings = phy_ethtool_set_link_ksettings,
1342 1343
	.get_ringparam	= ll_temac_ethtools_get_ringparam,
	.set_ringparam	= ll_temac_ethtools_set_ringparam,
1344 1345
	.get_coalesce	= ll_temac_ethtools_get_coalesce,
	.set_coalesce	= ll_temac_ethtools_set_coalesce,
R
Ricardo 已提交
1346 1347
};

1348
static int temac_probe(struct platform_device *pdev)
1349
{
1350 1351
	struct ll_temac_platform_data *pdata = dev_get_platdata(&pdev->dev);
	struct device_node *temac_np = dev_of_node(&pdev->dev), *dma_np;
1352 1353
	struct temac_local *lp;
	struct net_device *ndev;
1354
	struct resource *res;
1355
	const void *addr;
1356
	__be32 *p;
1357
	bool little_endian;
1358
	int rc = 0;
1359 1360

	/* Init network device structure */
1361
	ndev = devm_alloc_etherdev(&pdev->dev, sizeof(*lp));
1362
	if (!ndev)
1363
		return -ENOMEM;
1364

1365 1366
	platform_set_drvdata(pdev, ndev);
	SET_NETDEV_DEV(ndev, &pdev->dev);
1367
	ndev->features = NETIF_F_SG;
1368
	ndev->netdev_ops = &temac_netdev_ops;
R
Ricardo 已提交
1369
	ndev->ethtool_ops = &temac_ethtool_ops;
1370 1371 1372 1373 1374
#if 0
	ndev->features |= NETIF_F_IP_CSUM; /* Can checksum TCP/UDP over IPv4. */
	ndev->features |= NETIF_F_HW_CSUM; /* Can checksum all the packets. */
	ndev->features |= NETIF_F_IPV6_CSUM; /* Can checksum IPV6 TCP/UDP */
	ndev->features |= NETIF_F_HIGHDMA; /* Can DMA to high memory. */
1375 1376 1377
	ndev->features |= NETIF_F_HW_VLAN_CTAG_TX; /* Transmit VLAN hw accel */
	ndev->features |= NETIF_F_HW_VLAN_CTAG_RX; /* Receive VLAN hw acceleration */
	ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; /* Receive VLAN filtering */
1378 1379 1380 1381 1382 1383 1384 1385 1386
	ndev->features |= NETIF_F_VLAN_CHALLENGED; /* cannot handle VLAN pkts */
	ndev->features |= NETIF_F_GSO; /* Enable software GSO. */
	ndev->features |= NETIF_F_MULTI_QUEUE; /* Has multiple TX/RX queues */
	ndev->features |= NETIF_F_LRO; /* large receive offload */
#endif

	/* setup temac private info structure */
	lp = netdev_priv(ndev);
	lp->ndev = ndev;
1387
	lp->dev = &pdev->dev;
1388
	lp->options = XTE_OPTION_DEFAULTS;
1389 1390
	lp->rx_bd_num = RX_BD_NUM_DEFAULT;
	lp->tx_bd_num = TX_BD_NUM_DEFAULT;
1391
	spin_lock_init(&lp->rx_lock);
1392
	INIT_DELAYED_WORK(&lp->restart_work, ll_temac_restart_work_func);
1393 1394 1395

	/* Setup mutex for synchronization of indirect register access */
	if (pdata) {
1396
		if (!pdata->indirect_lock) {
1397
			dev_err(&pdev->dev,
1398
				"indirect_lock missing in platform_data\n");
1399 1400
			return -EINVAL;
		}
1401
		lp->indirect_lock = pdata->indirect_lock;
1402
	} else {
1403 1404 1405 1406
		lp->indirect_lock = devm_kmalloc(&pdev->dev,
						 sizeof(*lp->indirect_lock),
						 GFP_KERNEL);
		spin_lock_init(lp->indirect_lock);
1407
	}
1408 1409

	/* map device registers */
1410
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1411
	lp->regs = devm_ioremap(&pdev->dev, res->start,
1412
					resource_size(res));
1413
	if (!lp->regs) {
1414
		dev_err(&pdev->dev, "could not map TEMAC registers\n");
1415
		return -ENOMEM;
1416 1417
	}

1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
	/* Select register access functions with the specified
	 * endianness mode.  Default for OF devices is big-endian.
	 */
	little_endian = false;
	if (temac_np) {
		if (of_get_property(temac_np, "little-endian", NULL))
			little_endian = true;
	} else if (pdata) {
		little_endian = pdata->reg_little_endian;
	}
	if (little_endian) {
		lp->temac_ior = _temac_ior_le;
		lp->temac_iow = _temac_iow_le;
	} else {
		lp->temac_ior = _temac_ior_be;
		lp->temac_iow = _temac_iow_be;
	}

1436 1437
	/* Setup checksum offload, but default to off if not specified */
	lp->temac_features = 0;
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
	if (temac_np) {
		p = (__be32 *)of_get_property(temac_np, "xlnx,txcsum", NULL);
		if (p && be32_to_cpu(*p))
			lp->temac_features |= TEMAC_FEATURE_TX_CSUM;
		p = (__be32 *)of_get_property(temac_np, "xlnx,rxcsum", NULL);
		if (p && be32_to_cpu(*p))
			lp->temac_features |= TEMAC_FEATURE_RX_CSUM;
	} else if (pdata) {
		if (pdata->txcsum)
			lp->temac_features |= TEMAC_FEATURE_TX_CSUM;
		if (pdata->rxcsum)
			lp->temac_features |= TEMAC_FEATURE_RX_CSUM;
	}
	if (lp->temac_features & TEMAC_FEATURE_TX_CSUM)
1452 1453
		/* Can checksum TCP/UDP over IPv4. */
		ndev->features |= NETIF_F_IP_CSUM;
1454

1455 1456 1457 1458 1459 1460 1461 1462
	/* Defaults for IRQ delay/coalescing setup.  These are
	 * configuration values, so does not belong in device-tree.
	 */
	lp->coalesce_delay_tx = 0x10;
	lp->coalesce_count_tx = 0x22;
	lp->coalesce_delay_rx = 0xff;
	lp->coalesce_count_rx = 0x07;

1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
	/* Setup LocalLink DMA */
	if (temac_np) {
		/* Find the DMA node, map the DMA registers, and
		 * decode the DMA IRQs.
		 */
		dma_np = of_parse_phandle(temac_np, "llink-connected", 0);
		if (!dma_np) {
			dev_err(&pdev->dev, "could not find DMA node\n");
			return -ENODEV;
		}
1473

1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
		/* Setup the DMA register accesses, could be DCR or
		 * memory mapped.
		 */
		if (temac_dcr_setup(lp, pdev, dma_np)) {
			/* no DCR in the device tree, try non-DCR */
			lp->sdma_regs = devm_of_iomap(&pdev->dev, dma_np, 0,
						      NULL);
			if (IS_ERR(lp->sdma_regs)) {
				dev_err(&pdev->dev,
					"unable to map DMA registers\n");
				of_node_put(dma_np);
				return PTR_ERR(lp->sdma_regs);
			}
1487 1488 1489 1490 1491 1492 1493
			if (of_get_property(dma_np, "little-endian", NULL)) {
				lp->dma_in = temac_dma_in32_le;
				lp->dma_out = temac_dma_out32_le;
			} else {
				lp->dma_in = temac_dma_in32_be;
				lp->dma_out = temac_dma_out32_be;
			}
1494
			dev_dbg(&pdev->dev, "MEM base: %p\n", lp->sdma_regs);
1495
		}
K
Kulikov Vasiliy 已提交
1496

1497 1498 1499 1500 1501 1502 1503 1504 1505
		/* Get DMA RX and TX interrupts */
		lp->rx_irq = irq_of_parse_and_map(dma_np, 0);
		lp->tx_irq = irq_of_parse_and_map(dma_np, 1);

		/* Finished with the DMA node; drop the reference */
		of_node_put(dma_np);
	} else if (pdata) {
		/* 2nd memory resource specifies DMA registers */
		res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1506
		lp->sdma_regs = devm_ioremap(&pdev->dev, res->start,
1507
						     resource_size(res));
1508
		if (!lp->sdma_regs) {
1509 1510
			dev_err(&pdev->dev,
				"could not map DMA registers\n");
1511
			return -ENOMEM;
1512
		}
1513 1514 1515 1516 1517 1518 1519
		if (pdata->dma_little_endian) {
			lp->dma_in = temac_dma_in32_le;
			lp->dma_out = temac_dma_out32_le;
		} else {
			lp->dma_in = temac_dma_in32_be;
			lp->dma_out = temac_dma_out32_be;
		}
K
Kulikov Vasiliy 已提交
1520

1521 1522 1523
		/* Get DMA RX and TX interrupts */
		lp->rx_irq = platform_get_irq(pdev, 0);
		lp->tx_irq = platform_get_irq(pdev, 1);
1524 1525

		/* IRQ delay/coalescing setup */
1526 1527 1528 1529
		if (pdata->tx_irq_timeout || pdata->tx_irq_count) {
			lp->coalesce_delay_tx = pdata->tx_irq_timeout;
			lp->coalesce_count_tx = pdata->tx_irq_count;
		}
1530
		if (pdata->rx_irq_timeout || pdata->rx_irq_count) {
1531
			lp->coalesce_delay_rx = pdata->rx_irq_timeout;
1532 1533
			lp->coalesce_count_rx = pdata->rx_irq_count;
		}
1534 1535
	}

1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
	/* Error handle returned DMA RX and TX interrupts */
	if (lp->rx_irq < 0) {
		if (lp->rx_irq != -EPROBE_DEFER)
			dev_err(&pdev->dev, "could not get DMA RX irq\n");
		return lp->rx_irq;
	}
	if (lp->tx_irq < 0) {
		if (lp->tx_irq != -EPROBE_DEFER)
			dev_err(&pdev->dev, "could not get DMA TX irq\n");
		return lp->tx_irq;
	}
1547

1548 1549 1550
	if (temac_np) {
		/* Retrieve the MAC address */
		addr = of_get_mac_address(temac_np);
1551
		if (IS_ERR(addr)) {
1552 1553 1554 1555 1556 1557
			dev_err(&pdev->dev, "could not find MAC address\n");
			return -ENODEV;
		}
		temac_init_mac_address(ndev, addr);
	} else if (pdata) {
		temac_init_mac_address(ndev, pdata->mac_addr);
1558 1559
	}

1560
	rc = temac_mdio_setup(lp, pdev);
1561
	if (rc)
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
		dev_warn(&pdev->dev, "error registering MDIO bus\n");

	if (temac_np) {
		lp->phy_node = of_parse_phandle(temac_np, "phy-handle", 0);
		if (lp->phy_node)
			dev_dbg(lp->dev, "using PHY node %pOF\n", temac_np);
	} else if (pdata) {
		snprintf(lp->phy_name, sizeof(lp->phy_name),
			 PHY_ID_FMT, lp->mii_bus->id, pdata->phy_addr);
		lp->phy_interface = pdata->phy_interface;
	}
1573 1574 1575 1576 1577

	/* Add the device attributes */
	rc = sysfs_create_group(&lp->dev->kobj, &temac_attr_group);
	if (rc) {
		dev_err(lp->dev, "Error creating sysfs files\n");
1578
		goto err_sysfs_create;
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
	}

	rc = register_netdev(lp->ndev);
	if (rc) {
		dev_err(lp->dev, "register_netdev() error (%i)\n", rc);
		goto err_register_ndev;
	}

	return 0;

1589
err_register_ndev:
1590
	sysfs_remove_group(&lp->dev->kobj, &temac_attr_group);
1591
err_sysfs_create:
1592 1593
	if (lp->phy_node)
		of_node_put(lp->phy_node);
1594
	temac_mdio_teardown(lp);
1595 1596 1597
	return rc;
}

1598
static int temac_remove(struct platform_device *pdev)
1599
{
1600
	struct net_device *ndev = platform_get_drvdata(pdev);
1601 1602 1603 1604
	struct temac_local *lp = netdev_priv(ndev);

	unregister_netdev(ndev);
	sysfs_remove_group(&lp->dev->kobj, &temac_attr_group);
1605 1606
	if (lp->phy_node)
		of_node_put(lp->phy_node);
1607
	temac_mdio_teardown(lp);
1608 1609 1610
	return 0;
}

1611
static const struct of_device_id temac_of_match[] = {
1612
	{ .compatible = "xlnx,xps-ll-temac-1.01.b", },
1613 1614 1615
	{ .compatible = "xlnx,xps-ll-temac-2.00.a", },
	{ .compatible = "xlnx,xps-ll-temac-2.02.a", },
	{ .compatible = "xlnx,xps-ll-temac-2.03.a", },
1616 1617 1618 1619
	{},
};
MODULE_DEVICE_TABLE(of, temac_of_match);

1620 1621 1622
static struct platform_driver temac_driver = {
	.probe = temac_probe,
	.remove = temac_remove,
1623 1624
	.driver = {
		.name = "xilinx_temac",
1625
		.of_match_table = temac_of_match,
1626 1627 1628
	},
};

1629
module_platform_driver(temac_driver);
1630 1631 1632 1633

MODULE_DESCRIPTION("Xilinx LL_TEMAC Ethernet driver");
MODULE_AUTHOR("Yoshio Kashiwagi");
MODULE_LICENSE("GPL");