at91_ether.c 38.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Ethernet driver for the Atmel AT91RM9200 (Thunder)
 *
 *  Copyright (C) 2003 SAN People (Pty) Ltd
 *
 * Based on an earlier Atmel EMAC macrocell driver by Atmel and Lineo Inc.
 * Initial version by Rick Bronson 01/11/2003
 *
 * Intel LXT971A PHY support by Christopher Bahns & David Knickerbocker
 *   (Polaroid Corporation)
 *
 * Realtek RTL8201(B)L PHY support by Roman Avramenko <roman@imsystems.ru>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

#include <linux/module.h>
#include <linux/init.h>
22
#include <linux/interrupt.h>
23 24 25 26 27 28
#include <linux/mii.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/dma-mapping.h>
#include <linux/ethtool.h>
29
#include <linux/platform_data/macb.h>
30 31
#include <linux/platform_device.h>
#include <linux/clk.h>
32
#include <linux/gfp.h>
33
#include <linux/phy.h>
34 35 36 37 38

#include <asm/io.h>
#include <asm/uaccess.h>
#include <asm/mach-types.h>

39
#include <mach/at91rm9200_emac.h>
40
#include <asm/gpio.h>
41
#include <mach/board.h>
42 43 44 45 46 47

#include "at91_ether.h"

#define DRV_NAME	"at91_ether"
#define DRV_VERSION	"1.0"

48 49
#define LINK_POLL_INTERVAL	(HZ)

50 51 52 53 54
/* ..................................................................... */

/*
 * Read from a EMAC register.
 */
55
static inline unsigned long at91_emac_read(struct at91_private *lp, unsigned int reg)
56
{
57
	return __raw_readl(lp->emac_base + reg);
58 59 60 61 62
}

/*
 * Write to a EMAC register.
 */
63
static inline void at91_emac_write(struct at91_private *lp, unsigned int reg, unsigned long value)
64
{
65
	__raw_writel(value, lp->emac_base + reg);
66 67 68 69 70 71 72 73 74
}

/* ........................... PHY INTERFACE ........................... */

/*
 * Enable the MDIO bit in MAC control register
 * When not called from an interrupt-handler, access to the PHY must be
 *  protected by a spinlock.
 */
75
static void enable_mdi(struct at91_private *lp)
76 77 78
{
	unsigned long ctl;

79 80
	ctl = at91_emac_read(lp, AT91_EMAC_CTL);
	at91_emac_write(lp, AT91_EMAC_CTL, ctl | AT91_EMAC_MPE);	/* enable management port */
81 82 83 84 85
}

/*
 * Disable the MDIO bit in the MAC control register
 */
86
static void disable_mdi(struct at91_private *lp)
87 88 89
{
	unsigned long ctl;

90 91
	ctl = at91_emac_read(lp, AT91_EMAC_CTL);
	at91_emac_write(lp, AT91_EMAC_CTL, ctl & ~AT91_EMAC_MPE);	/* disable management port */
92 93 94 95 96
}

/*
 * Wait until the PHY operation is complete.
 */
97 98
static inline void at91_phy_wait(struct at91_private *lp)
{
99 100
	unsigned long timeout = jiffies + 2;

101
	while (!(at91_emac_read(lp, AT91_EMAC_SR) & AT91_EMAC_SR_IDLE)) {
102 103 104 105 106 107 108 109 110 111 112 113
		if (time_after(jiffies, timeout)) {
			printk("at91_ether: MIO timeout\n");
			break;
		}
		cpu_relax();
	}
}

/*
 * Write value to the a PHY register
 * Note: MDI interface is assumed to already have been enabled.
 */
114
static void write_phy(struct at91_private *lp, unsigned char phy_addr, unsigned char address, unsigned int value)
115
{
116
	at91_emac_write(lp, AT91_EMAC_MAN, AT91_EMAC_MAN_802_3 | AT91_EMAC_RW_W
117 118 119
		| ((phy_addr & 0x1f) << 23) | (address << 18) | (value & AT91_EMAC_DATA));

	/* Wait until IDLE bit in Network Status register is cleared */
120
	at91_phy_wait(lp);
121 122 123 124 125 126
}

/*
 * Read value stored in a PHY register.
 * Note: MDI interface is assumed to already have been enabled.
 */
127
static void read_phy(struct at91_private *lp, unsigned char phy_addr, unsigned char address, unsigned int *value)
128
{
129
	at91_emac_write(lp, AT91_EMAC_MAN, AT91_EMAC_MAN_802_3 | AT91_EMAC_RW_R
130 131 132
		| ((phy_addr & 0x1f) << 23) | (address << 18));

	/* Wait until IDLE bit in Network Status register is cleared */
133
	at91_phy_wait(lp);
134

135
	*value = at91_emac_read(lp, AT91_EMAC_MAN) & AT91_EMAC_DATA;
136 137 138 139 140 141 142 143 144
}

/* ........................... PHY MANAGEMENT .......................... */

/*
 * Access the PHY to determine the current link speed and mode, and update the
 * MAC accordingly.
 * If no link or auto-negotiation is busy, then no changes are made.
 */
145
static void update_linkspeed(struct net_device *dev, int silent)
146
{
147
	struct at91_private *lp = netdev_priv(dev);
148 149 150 151 152
	unsigned int bmsr, bmcr, lpa, mac_cfg;
	unsigned int speed, duplex;

	if (!mii_link_ok(&lp->mii)) {		/* no link */
		netif_carrier_off(dev);
153 154
		if (!silent)
			printk(KERN_INFO "%s: Link down.\n", dev->name);
155 156 157 158
		return;
	}

	/* Link up, or auto-negotiation still in progress */
159 160
	read_phy(lp, lp->phy_address, MII_BMSR, &bmsr);
	read_phy(lp, lp->phy_address, MII_BMCR, &bmcr);
161 162 163 164
	if (bmcr & BMCR_ANENABLE) {				/* AutoNegotiation is enabled */
		if (!(bmsr & BMSR_ANEGCOMPLETE))
			return;			/* Do nothing - another interrupt generated when negotiation complete */

165
		read_phy(lp, lp->phy_address, MII_LPA, &lpa);
166 167 168 169 170 171 172 173 174 175
		if ((lpa & LPA_100FULL) || (lpa & LPA_100HALF)) speed = SPEED_100;
		else speed = SPEED_10;
		if ((lpa & LPA_100FULL) || (lpa & LPA_10FULL)) duplex = DUPLEX_FULL;
		else duplex = DUPLEX_HALF;
	} else {
		speed = (bmcr & BMCR_SPEED100) ? SPEED_100 : SPEED_10;
		duplex = (bmcr & BMCR_FULLDPLX) ? DUPLEX_FULL : DUPLEX_HALF;
	}

	/* Update the MAC */
176
	mac_cfg = at91_emac_read(lp, AT91_EMAC_CFG) & ~(AT91_EMAC_SPD | AT91_EMAC_FD);
177 178 179 180 181 182 183 184 185 186
	if (speed == SPEED_100) {
		if (duplex == DUPLEX_FULL)		/* 100 Full Duplex */
			mac_cfg |= AT91_EMAC_SPD | AT91_EMAC_FD;
		else					/* 100 Half Duplex */
			mac_cfg |= AT91_EMAC_SPD;
	} else {
		if (duplex == DUPLEX_FULL)		/* 10 Full Duplex */
			mac_cfg |= AT91_EMAC_FD;
		else {}					/* 10 Half Duplex */
	}
187
	at91_emac_write(lp, AT91_EMAC_CFG, mac_cfg);
188

189 190
	if (!silent)
		printk(KERN_INFO "%s: Link now %i-%s\n", dev->name, speed, (duplex == DUPLEX_FULL) ? "FullDuplex" : "HalfDuplex");
191 192 193 194 195 196
	netif_carrier_on(dev);
}

/*
 * Handle interrupts from the PHY
 */
197
static irqreturn_t at91ether_phy_interrupt(int irq, void *dev_id)
198 199
{
	struct net_device *dev = (struct net_device *) dev_id;
200
	struct at91_private *lp = netdev_priv(dev);
201 202 203 204 205 206 207
	unsigned int phy;

	/*
	 * This hander is triggered on both edges, but the PHY chips expect
	 * level-triggering.  We therefore have to check if the PHY actually has
	 * an IRQ pending.
	 */
208
	enable_mdi(lp);
209
	if ((lp->phy_type == MII_DM9161_ID) || (lp->phy_type == MII_DM9161A_ID)) {
210
		read_phy(lp, lp->phy_address, MII_DSINTR_REG, &phy);	/* ack interrupt in Davicom PHY */
211 212 213 214
		if (!(phy & (1 << 0)))
			goto done;
	}
	else if (lp->phy_type == MII_LXT971A_ID) {
215
		read_phy(lp, lp->phy_address, MII_ISINTS_REG, &phy);	/* ack interrupt in Intel PHY */
216 217 218 219
		if (!(phy & (1 << 2)))
			goto done;
	}
	else if (lp->phy_type == MII_BCM5221_ID) {
220
		read_phy(lp, lp->phy_address, MII_BCMINTR_REG, &phy);	/* ack interrupt in Broadcom PHY */
221 222 223 224
		if (!(phy & (1 << 0)))
			goto done;
	}
	else if (lp->phy_type == MII_KS8721_ID) {
225
		read_phy(lp, lp->phy_address, MII_TPISTATUS, &phy);		/* ack interrupt in Micrel PHY */
226 227 228
		if (!(phy & ((1 << 2) | 1)))
			goto done;
	}
229 230
	else if (lp->phy_type == MII_T78Q21x3_ID) {					/* ack interrupt in Teridian PHY */
		read_phy(lp, lp->phy_address, MII_T78Q21INT_REG, &phy);
231 232 233 234
		if (!(phy & ((1 << 2) | 1)))
			goto done;
	}
	else if (lp->phy_type == MII_DP83848_ID) {
235
		read_phy(lp, lp->phy_address, MII_DPPHYSTS_REG, &phy);	/* ack interrupt in DP83848 PHY */
236 237 238
		if (!(phy & (1 << 7)))
			goto done;
	}
239

240
	update_linkspeed(dev, 0);
241 242

done:
243
	disable_mdi(lp);
244 245 246 247 248 249 250 251 252

	return IRQ_HANDLED;
}

/*
 * Initialize and enable the PHY interrupt for link-state changes
 */
static void enable_phyirq(struct net_device *dev)
{
253
	struct at91_private *lp = netdev_priv(dev);
254 255 256
	unsigned int dsintr, irq_number;
	int status;

257
	if (!gpio_is_valid(lp->board_data.phy_irq_pin)) {
258 259 260 261
		/*
		 * PHY doesn't have an IRQ pin (RTL8201, DP83847, AC101L),
		 * or board does not have it connected.
		 */
262
		mod_timer(&lp->check_timer, jiffies + LINK_POLL_INTERVAL);
263
		return;
264
	}
265

266
	irq_number = gpio_to_irq(lp->board_data.phy_irq_pin);
267 268 269 270 271 272 273
	status = request_irq(irq_number, at91ether_phy_interrupt, 0, dev->name, dev);
	if (status) {
		printk(KERN_ERR "at91_ether: PHY IRQ %d request failed - status %d!\n", irq_number, status);
		return;
	}

	spin_lock_irq(&lp->lock);
274
	enable_mdi(lp);
275 276

	if ((lp->phy_type == MII_DM9161_ID) || (lp->phy_type == MII_DM9161A_ID)) {	/* for Davicom PHY */
277
		read_phy(lp, lp->phy_address, MII_DSINTR_REG, &dsintr);
278
		dsintr = dsintr & ~0xf00;		/* clear bits 8..11 */
279
		write_phy(lp, lp->phy_address, MII_DSINTR_REG, dsintr);
280 281
	}
	else if (lp->phy_type == MII_LXT971A_ID) {	/* for Intel PHY */
282
		read_phy(lp, lp->phy_address, MII_ISINTE_REG, &dsintr);
283
		dsintr = dsintr | 0xf2;			/* set bits 1, 4..7 */
284
		write_phy(lp, lp->phy_address, MII_ISINTE_REG, dsintr);
285 286 287
	}
	else if (lp->phy_type == MII_BCM5221_ID) {	/* for Broadcom PHY */
		dsintr = (1 << 15) | ( 1 << 14);
288
		write_phy(lp, lp->phy_address, MII_BCMINTR_REG, dsintr);
289 290 291
	}
	else if (lp->phy_type == MII_KS8721_ID) {	/* for Micrel PHY */
		dsintr = (1 << 10) | ( 1 << 8);
292
		write_phy(lp, lp->phy_address, MII_TPISTATUS, dsintr);
293
	}
294
	else if (lp->phy_type == MII_T78Q21x3_ID) {	/* for Teridian PHY */
295
		read_phy(lp, lp->phy_address, MII_T78Q21INT_REG, &dsintr);
296
		dsintr = dsintr | 0x500;		/* set bits 8, 10 */
297
		write_phy(lp, lp->phy_address, MII_T78Q21INT_REG, dsintr);
298 299
	}
	else if (lp->phy_type == MII_DP83848_ID) {	/* National Semiconductor DP83848 PHY */
300
		read_phy(lp, lp->phy_address, MII_DPMISR_REG, &dsintr);
301
		dsintr = dsintr | 0x3c;			/* set bits 2..5 */
302 303
		write_phy(lp, lp->phy_address, MII_DPMISR_REG, dsintr);
		read_phy(lp, lp->phy_address, MII_DPMICR_REG, &dsintr);
304
		dsintr = dsintr | 0x3;			/* set bits 0,1 */
305
		write_phy(lp, lp->phy_address, MII_DPMICR_REG, dsintr);
306
	}
307

308
	disable_mdi(lp);
309 310 311 312 313 314 315 316
	spin_unlock_irq(&lp->lock);
}

/*
 * Disable the PHY interrupt
 */
static void disable_phyirq(struct net_device *dev)
{
317
	struct at91_private *lp = netdev_priv(dev);
318 319 320
	unsigned int dsintr;
	unsigned int irq_number;

321
	if (!gpio_is_valid(lp->board_data.phy_irq_pin)) {
322
		del_timer_sync(&lp->check_timer);
323
		return;
324
	}
325 326

	spin_lock_irq(&lp->lock);
327
	enable_mdi(lp);
328 329

	if ((lp->phy_type == MII_DM9161_ID) || (lp->phy_type == MII_DM9161A_ID)) {	/* for Davicom PHY */
330
		read_phy(lp, lp->phy_address, MII_DSINTR_REG, &dsintr);
331
		dsintr = dsintr | 0xf00;			/* set bits 8..11 */
332
		write_phy(lp, lp->phy_address, MII_DSINTR_REG, dsintr);
333 334
	}
	else if (lp->phy_type == MII_LXT971A_ID) {	/* for Intel PHY */
335
		read_phy(lp, lp->phy_address, MII_ISINTE_REG, &dsintr);
336
		dsintr = dsintr & ~0xf2;			/* clear bits 1, 4..7 */
337
		write_phy(lp, lp->phy_address, MII_ISINTE_REG, dsintr);
338 339
	}
	else if (lp->phy_type == MII_BCM5221_ID) {	/* for Broadcom PHY */
340
		read_phy(lp, lp->phy_address, MII_BCMINTR_REG, &dsintr);
341
		dsintr = ~(1 << 14);
342
		write_phy(lp, lp->phy_address, MII_BCMINTR_REG, dsintr);
343 344
	}
	else if (lp->phy_type == MII_KS8721_ID) {	/* for Micrel PHY */
345
		read_phy(lp, lp->phy_address, MII_TPISTATUS, &dsintr);
346
		dsintr = ~((1 << 10) | (1 << 8));
347
		write_phy(lp, lp->phy_address, MII_TPISTATUS, dsintr);
348
	}
349
	else if (lp->phy_type == MII_T78Q21x3_ID) {	/* for Teridian PHY */
350
		read_phy(lp, lp->phy_address, MII_T78Q21INT_REG, &dsintr);
351
		dsintr = dsintr & ~0x500;			/* clear bits 8, 10 */
352
		write_phy(lp, lp->phy_address, MII_T78Q21INT_REG, dsintr);
353 354
	}
	else if (lp->phy_type == MII_DP83848_ID) {	/* National Semiconductor DP83848 PHY */
355
		read_phy(lp, lp->phy_address, MII_DPMICR_REG, &dsintr);
356
		dsintr = dsintr & ~0x3;				/* clear bits 0, 1 */
357 358
		write_phy(lp, lp->phy_address, MII_DPMICR_REG, dsintr);
		read_phy(lp, lp->phy_address, MII_DPMISR_REG, &dsintr);
359
		dsintr = dsintr & ~0x3c;			/* clear bits 2..5 */
360
		write_phy(lp, lp->phy_address, MII_DPMISR_REG, dsintr);
361
	}
362

363
	disable_mdi(lp);
364 365
	spin_unlock_irq(&lp->lock);

366
	irq_number = gpio_to_irq(lp->board_data.phy_irq_pin);
367 368 369 370 371 372 373 374 375
	free_irq(irq_number, dev);			/* Free interrupt handler */
}

/*
 * Perform a software reset of the PHY.
 */
#if 0
static void reset_phy(struct net_device *dev)
{
376
	struct at91_private *lp = netdev_priv(dev);
377 378 379
	unsigned int bmcr;

	spin_lock_irq(&lp->lock);
380
	enable_mdi(lp);
381 382

	/* Perform PHY reset */
383
	write_phy(lp, lp->phy_address, MII_BMCR, BMCR_RESET);
384 385 386

	/* Wait until PHY reset is complete */
	do {
387
		read_phy(lp, lp->phy_address, MII_BMCR, &bmcr);
388
	} while (!(bmcr & BMCR_RESET));
389

390
	disable_mdi(lp);
391 392 393 394
	spin_unlock_irq(&lp->lock);
}
#endif

395 396 397
static void at91ether_check_link(unsigned long dev_id)
{
	struct net_device *dev = (struct net_device *) dev_id;
398
	struct at91_private *lp = netdev_priv(dev);
399

400
	enable_mdi(lp);
401
	update_linkspeed(dev, 1);
402
	disable_mdi(lp);
403

404
	mod_timer(&lp->check_timer, jiffies + LINK_POLL_INTERVAL);
405 406
}

407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
/*
 * Perform any PHY-specific initialization.
 */
static void __init initialize_phy(struct at91_private *lp)
{
	unsigned int val;

	spin_lock_irq(&lp->lock);
	enable_mdi(lp);

	if ((lp->phy_type == MII_DM9161_ID) || (lp->phy_type == MII_DM9161A_ID)) {
		read_phy(lp, lp->phy_address, MII_DSCR_REG, &val);
		if ((val & (1 << 10)) == 0)			/* DSCR bit 10 is 0 -- fiber mode */
			lp->phy_media = PORT_FIBRE;
	} else if (machine_is_csb337()) {
		/* mix link activity status into LED2 link state */
		write_phy(lp, lp->phy_address, MII_LEDCTRL_REG, 0x0d22);
	} else if (machine_is_ecbat91())
		write_phy(lp, lp->phy_address, MII_LEDCTRL_REG, 0x156A);

	disable_mdi(lp);
	spin_unlock_irq(&lp->lock);
}

431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
/* ......................... ADDRESS MANAGEMENT ........................ */

/*
 * NOTE: Your bootloader must always set the MAC address correctly before
 * booting into Linux.
 *
 * - It must always set the MAC address after reset, even if it doesn't
 *   happen to access the Ethernet while it's booting.  Some versions of
 *   U-Boot on the AT91RM9200-DK do not do this.
 *
 * - Likewise it must store the addresses in the correct byte order.
 *   MicroMonitor (uMon) on the CSB337 does this incorrectly (and
 *   continues to do so, for bug-compatibility).
 */

static short __init unpack_mac_address(struct net_device *dev, unsigned int hi, unsigned int lo)
{
	char addr[6];

	if (machine_is_csb337()) {
		addr[5] = (lo & 0xff);			/* The CSB337 bootloader stores the MAC the wrong-way around */
		addr[4] = (lo & 0xff00) >> 8;
		addr[3] = (lo & 0xff0000) >> 16;
		addr[2] = (lo & 0xff000000) >> 24;
		addr[1] = (hi & 0xff);
		addr[0] = (hi & 0xff00) >> 8;
	}
	else {
		addr[0] = (lo & 0xff);
		addr[1] = (lo & 0xff00) >> 8;
		addr[2] = (lo & 0xff0000) >> 16;
		addr[3] = (lo & 0xff000000) >> 24;
		addr[4] = (hi & 0xff);
		addr[5] = (hi & 0xff00) >> 8;
	}

	if (is_valid_ether_addr(addr)) {
		memcpy(dev->dev_addr, &addr, 6);
		return 1;
	}
	return 0;
}

/*
 * Set the ethernet MAC address in dev->dev_addr
 */
static void __init get_mac_address(struct net_device *dev)
{
479 480
	struct at91_private *lp = netdev_priv(dev);

481
	/* Check Specific-Address 1 */
482
	if (unpack_mac_address(dev, at91_emac_read(lp, AT91_EMAC_SA1H), at91_emac_read(lp, AT91_EMAC_SA1L)))
483 484
		return;
	/* Check Specific-Address 2 */
485
	if (unpack_mac_address(dev, at91_emac_read(lp, AT91_EMAC_SA2H), at91_emac_read(lp, AT91_EMAC_SA2L)))
486 487
		return;
	/* Check Specific-Address 3 */
488
	if (unpack_mac_address(dev, at91_emac_read(lp, AT91_EMAC_SA3H), at91_emac_read(lp, AT91_EMAC_SA3L)))
489 490
		return;
	/* Check Specific-Address 4 */
491
	if (unpack_mac_address(dev, at91_emac_read(lp, AT91_EMAC_SA4H), at91_emac_read(lp, AT91_EMAC_SA4L)))
492 493 494 495 496 497 498 499 500 501
		return;

	printk(KERN_ERR "at91_ether: Your bootloader did not configure a MAC address.\n");
}

/*
 * Program the hardware MAC address from dev->dev_addr.
 */
static void update_mac_address(struct net_device *dev)
{
502
	struct at91_private *lp = netdev_priv(dev);
503

504 505 506 507 508
	at91_emac_write(lp, AT91_EMAC_SA1L, (dev->dev_addr[3] << 24) | (dev->dev_addr[2] << 16) | (dev->dev_addr[1] << 8) | (dev->dev_addr[0]));
	at91_emac_write(lp, AT91_EMAC_SA1H, (dev->dev_addr[5] << 8) | (dev->dev_addr[4]));

	at91_emac_write(lp, AT91_EMAC_SA2L, 0);
	at91_emac_write(lp, AT91_EMAC_SA2H, 0);
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
}

/*
 * Store the new hardware address in dev->dev_addr, and update the MAC.
 */
static int set_mac_address(struct net_device *dev, void* addr)
{
	struct sockaddr *address = addr;

	if (!is_valid_ether_addr(address->sa_data))
		return -EADDRNOTAVAIL;

	memcpy(dev->dev_addr, address->sa_data, dev->addr_len);
	update_mac_address(dev);

J
Johannes Berg 已提交
524 525
	printk("%s: Setting MAC address to %pM\n", dev->name,
	       dev->dev_addr);
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

	return 0;
}

static int inline hash_bit_value(int bitnr, __u8 *addr)
{
	if (addr[bitnr / 8] & (1 << (bitnr % 8)))
		return 1;
	return 0;
}

/*
 * The hash address register is 64 bits long and takes up two locations in the memory map.
 * The least significant bits are stored in EMAC_HSL and the most significant
 * bits in EMAC_HSH.
 *
 * The unicast hash enable and the multicast hash enable bits in the network configuration
 *  register enable the reception of hash matched frames. The destination address is
 *  reduced to a 6 bit index into the 64 bit hash register using the following hash function.
 * The hash function is an exclusive or of every sixth bit of the destination address.
 *   hash_index[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
 *   hash_index[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
 *   hash_index[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
 *   hash_index[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
 *   hash_index[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
 *   hash_index[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
 * da[0] represents the least significant bit of the first byte received, that is, the multicast/
 *  unicast indicator, and da[47] represents the most significant bit of the last byte
 *  received.
 * If the hash index points to a bit that is set in the hash register then the frame will be
 *  matched according to whether the frame is multicast or unicast.
 * A multicast match will be signalled if the multicast hash enable bit is set, da[0] is 1 and
 *  the hash index points to a bit set in the hash register.
 * A unicast match will be signalled if the unicast hash enable bit is set, da[0] is 0 and the
 *  hash index points to a bit set in the hash register.
 * To receive all multicast frames, the hash register should be set with all ones and the
 *  multicast hash enable bit should be set in the network configuration register.
 */

/*
 * Return the hash index value for the specified address.
 */
static int hash_get_index(__u8 *addr)
{
	int i, j, bitval;
	int hash_index = 0;

	for (j = 0; j < 6; j++) {
		for (i = 0, bitval = 0; i < 8; i++)
			bitval ^= hash_bit_value(i*6 + j, addr);

		hash_index |= (bitval << j);
	}

580
	return hash_index;
581 582 583 584 585 586 587
}

/*
 * Add multicast addresses to the internal multicast-hash table.
 */
static void at91ether_sethashtable(struct net_device *dev)
{
588
	struct at91_private *lp = netdev_priv(dev);
589
	struct netdev_hw_addr *ha;
590
	unsigned long mc_filter[2];
591
	unsigned int bitnr;
592 593 594

	mc_filter[0] = mc_filter[1] = 0;

595 596
	netdev_for_each_mc_addr(ha, dev) {
		bitnr = hash_get_index(ha->addr);
597 598 599
		mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
	}

600 601
	at91_emac_write(lp, AT91_EMAC_HSL, mc_filter[0]);
	at91_emac_write(lp, AT91_EMAC_HSH, mc_filter[1]);
602 603 604 605 606
}

/*
 * Enable/Disable promiscuous and multicast modes.
 */
607
static void at91ether_set_multicast_list(struct net_device *dev)
608
{
609
	struct at91_private *lp = netdev_priv(dev);
610 611
	unsigned long cfg;

612
	cfg = at91_emac_read(lp, AT91_EMAC_CFG);
613 614 615 616 617 618 619

	if (dev->flags & IFF_PROMISC)			/* Enable promiscuous mode */
		cfg |= AT91_EMAC_CAF;
	else if (dev->flags & (~IFF_PROMISC))		/* Disable promiscuous mode */
		cfg &= ~AT91_EMAC_CAF;

	if (dev->flags & IFF_ALLMULTI) {		/* Enable all multicast mode */
620 621
		at91_emac_write(lp, AT91_EMAC_HSH, -1);
		at91_emac_write(lp, AT91_EMAC_HSL, -1);
622
		cfg |= AT91_EMAC_MTI;
623
	} else if (!netdev_mc_empty(dev)) { /* Enable specific multicasts */
624 625 626
		at91ether_sethashtable(dev);
		cfg |= AT91_EMAC_MTI;
	} else if (dev->flags & (~IFF_ALLMULTI)) {	/* Disable all multicast mode */
627 628
		at91_emac_write(lp, AT91_EMAC_HSH, 0);
		at91_emac_write(lp, AT91_EMAC_HSL, 0);
629 630 631
		cfg &= ~AT91_EMAC_MTI;
	}

632
	at91_emac_write(lp, AT91_EMAC_CFG, cfg);
633 634 635 636 637 638
}

/* ......................... ETHTOOL SUPPORT ........................... */

static int mdio_read(struct net_device *dev, int phy_id, int location)
{
639
	struct at91_private *lp = netdev_priv(dev);
640 641
	unsigned int value;

642
	read_phy(lp, phy_id, location, &value);
643 644 645 646 647
	return value;
}

static void mdio_write(struct net_device *dev, int phy_id, int location, int value)
{
648 649 650
	struct at91_private *lp = netdev_priv(dev);

	write_phy(lp, phy_id, location, value);
651 652 653 654
}

static int at91ether_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
{
655
	struct at91_private *lp = netdev_priv(dev);
656 657 658
	int ret;

	spin_lock_irq(&lp->lock);
659
	enable_mdi(lp);
660 661 662

	ret = mii_ethtool_gset(&lp->mii, cmd);

663
	disable_mdi(lp);
664 665 666 667 668 669 670 671 672 673 674 675
	spin_unlock_irq(&lp->lock);

	if (lp->phy_media == PORT_FIBRE) {		/* override media type since mii.c doesn't know */
		cmd->supported = SUPPORTED_FIBRE;
		cmd->port = PORT_FIBRE;
	}

	return ret;
}

static int at91ether_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
{
676
	struct at91_private *lp = netdev_priv(dev);
677 678 679
	int ret;

	spin_lock_irq(&lp->lock);
680
	enable_mdi(lp);
681 682 683

	ret = mii_ethtool_sset(&lp->mii, cmd);

684
	disable_mdi(lp);
685 686 687 688 689 690 691
	spin_unlock_irq(&lp->lock);

	return ret;
}

static int at91ether_nwayreset(struct net_device *dev)
{
692
	struct at91_private *lp = netdev_priv(dev);
693 694 695
	int ret;

	spin_lock_irq(&lp->lock);
696
	enable_mdi(lp);
697 698 699

	ret = mii_nway_restart(&lp->mii);

700
	disable_mdi(lp);
701 702 703 704 705 706 707 708 709
	spin_unlock_irq(&lp->lock);

	return ret;
}

static void at91ether_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
{
	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
710
	strlcpy(info->bus_info, dev_name(dev->dev.parent), sizeof(info->bus_info));
711 712
}

713
static const struct ethtool_ops at91ether_ethtool_ops = {
714 715 716 717 718 719 720
	.get_settings	= at91ether_get_settings,
	.set_settings	= at91ether_set_settings,
	.get_drvinfo	= at91ether_get_drvinfo,
	.nway_reset	= at91ether_nwayreset,
	.get_link	= ethtool_op_get_link,
};

721 722
static int at91ether_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
{
723
	struct at91_private *lp = netdev_priv(dev);
724 725 726 727 728 729
	int res;

	if (!netif_running(dev))
		return -EINVAL;

	spin_lock_irq(&lp->lock);
730
	enable_mdi(lp);
731
	res = generic_mii_ioctl(&lp->mii, if_mii(rq), cmd, NULL);
732
	disable_mdi(lp);
733 734 735 736
	spin_unlock_irq(&lp->lock);

	return res;
}
737 738 739 740 741 742 743 744

/* ................................ MAC ................................ */

/*
 * Initialize and start the Receiver and Transmit subsystems
 */
static void at91ether_start(struct net_device *dev)
{
745
	struct at91_private *lp = netdev_priv(dev);
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
	struct recv_desc_bufs *dlist, *dlist_phys;
	int i;
	unsigned long ctl;

	dlist = lp->dlist;
	dlist_phys = lp->dlist_phys;

	for (i = 0; i < MAX_RX_DESCR; i++) {
		dlist->descriptors[i].addr = (unsigned int) &dlist_phys->recv_buf[i][0];
		dlist->descriptors[i].size = 0;
	}

	/* Set the Wrap bit on the last descriptor */
	dlist->descriptors[i-1].addr |= EMAC_DESC_WRAP;

	/* Reset buffer index */
	lp->rxBuffIndex = 0;

	/* Program address of descriptor list in Rx Buffer Queue register */
765
	at91_emac_write(lp, AT91_EMAC_RBQP, (unsigned long) dlist_phys);
766 767

	/* Enable Receive and Transmit */
768 769
	ctl = at91_emac_read(lp, AT91_EMAC_CTL);
	at91_emac_write(lp, AT91_EMAC_CTL, ctl | AT91_EMAC_RE | AT91_EMAC_TE);
770 771 772 773 774 775 776
}

/*
 * Open the ethernet interface
 */
static int at91ether_open(struct net_device *dev)
{
777
	struct at91_private *lp = netdev_priv(dev);
778 779
	unsigned long ctl;

780 781
	if (!is_valid_ether_addr(dev->dev_addr))
		return -EADDRNOTAVAIL;
782

783
	clk_enable(lp->ether_clk);		/* Re-enable Peripheral clock */
784 785

	/* Clear internal statistics */
786 787
	ctl = at91_emac_read(lp, AT91_EMAC_CTL);
	at91_emac_write(lp, AT91_EMAC_CTL, ctl | AT91_EMAC_CSR);
788 789 790 791 792 793 794 795

	/* Update the MAC address (incase user has changed it) */
	update_mac_address(dev);

	/* Enable PHY interrupt */
	enable_phyirq(dev);

	/* Enable MAC interrupts */
796
	at91_emac_write(lp, AT91_EMAC_IER, AT91_EMAC_RCOM | AT91_EMAC_RBNA
797 798 799 800 801
				| AT91_EMAC_TUND | AT91_EMAC_RTRY | AT91_EMAC_TCOM
				| AT91_EMAC_ROVR | AT91_EMAC_ABT);

	/* Determine current link speed */
	spin_lock_irq(&lp->lock);
802
	enable_mdi(lp);
803
	update_linkspeed(dev, 0);
804
	disable_mdi(lp);
805 806 807 808 809 810 811 812 813 814 815 816
	spin_unlock_irq(&lp->lock);

	at91ether_start(dev);
	netif_start_queue(dev);
	return 0;
}

/*
 * Close the interface
 */
static int at91ether_close(struct net_device *dev)
{
817
	struct at91_private *lp = netdev_priv(dev);
818 819 820
	unsigned long ctl;

	/* Disable Receiver and Transmitter */
821 822
	ctl = at91_emac_read(lp, AT91_EMAC_CTL);
	at91_emac_write(lp, AT91_EMAC_CTL, ctl & ~(AT91_EMAC_TE | AT91_EMAC_RE));
823 824 825 826 827

	/* Disable PHY interrupt */
	disable_phyirq(dev);

	/* Disable MAC interrupts */
828
	at91_emac_write(lp, AT91_EMAC_IDR, AT91_EMAC_RCOM | AT91_EMAC_RBNA
829 830 831 832 833
				| AT91_EMAC_TUND | AT91_EMAC_RTRY | AT91_EMAC_TCOM
				| AT91_EMAC_ROVR | AT91_EMAC_ABT);

	netif_stop_queue(dev);

834
	clk_disable(lp->ether_clk);		/* Disable Peripheral clock */
835 836 837 838 839 840 841

	return 0;
}

/*
 * Transmit packet.
 */
842
static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
843
{
844
	struct at91_private *lp = netdev_priv(dev);
845

846
	if (at91_emac_read(lp, AT91_EMAC_TSR) & AT91_EMAC_TSR_BNQ) {
847 848 849 850 851 852
		netif_stop_queue(dev);

		/* Store packet information (to free when Tx completed) */
		lp->skb = skb;
		lp->skb_length = skb->len;
		lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len, DMA_TO_DEVICE);
853
		dev->stats.tx_bytes += skb->len;
854 855

		/* Set address of the data in the Transmit Address register */
856
		at91_emac_write(lp, AT91_EMAC_TAR, lp->skb_physaddr);
857
		/* Set length of the packet in the Transmit Control register */
858
		at91_emac_write(lp, AT91_EMAC_TCR, skb->len);
859 860

	} else {
861
		printk(KERN_ERR "at91_ether.c: at91ether_start_xmit() called, but device is busy!\n");
862
		return NETDEV_TX_BUSY;	/* if we return anything but zero, dev.c:1055 calls kfree_skb(skb)
863 864 865 866
				on this skb, he also reports -ENETDOWN and printk's, so either
				we free and return(0) or don't free and return 1 */
	}

867
	return NETDEV_TX_OK;
868 869 870 871 872 873 874
}

/*
 * Update the current statistics from the internal statistics registers.
 */
static struct net_device_stats *at91ether_stats(struct net_device *dev)
{
875
	struct at91_private *lp = netdev_priv(dev);
876 877 878
	int ale, lenerr, seqe, lcol, ecol;

	if (netif_running(dev)) {
879 880
		dev->stats.rx_packets += at91_emac_read(lp, AT91_EMAC_OK);	/* Good frames received */
		ale = at91_emac_read(lp, AT91_EMAC_ALE);
881
		dev->stats.rx_frame_errors += ale;				/* Alignment errors */
882
		lenerr = at91_emac_read(lp, AT91_EMAC_ELR) + at91_emac_read(lp, AT91_EMAC_USF);
883
		dev->stats.rx_length_errors += lenerr;				/* Excessive Length or Undersize Frame error */
884
		seqe = at91_emac_read(lp, AT91_EMAC_SEQE);
885
		dev->stats.rx_crc_errors += seqe;				/* CRC error */
886
		dev->stats.rx_fifo_errors += at91_emac_read(lp, AT91_EMAC_DRFC);/* Receive buffer not available */
887
		dev->stats.rx_errors += (ale + lenerr + seqe
888
			+ at91_emac_read(lp, AT91_EMAC_CDE) + at91_emac_read(lp, AT91_EMAC_RJB));
889

890 891 892 893
		dev->stats.tx_packets += at91_emac_read(lp, AT91_EMAC_FRA);	/* Frames successfully transmitted */
		dev->stats.tx_fifo_errors += at91_emac_read(lp, AT91_EMAC_TUE);	/* Transmit FIFO underruns */
		dev->stats.tx_carrier_errors += at91_emac_read(lp, AT91_EMAC_CSE);	/* Carrier Sense errors */
		dev->stats.tx_heartbeat_errors += at91_emac_read(lp, AT91_EMAC_SQEE);/* Heartbeat error */
894

895 896
		lcol = at91_emac_read(lp, AT91_EMAC_LCOL);
		ecol = at91_emac_read(lp, AT91_EMAC_ECOL);
897 898
		dev->stats.tx_window_errors += lcol;			/* Late collisions */
		dev->stats.tx_aborted_errors += ecol;			/* 16 collisions */
899

900
		dev->stats.collisions += (at91_emac_read(lp, AT91_EMAC_SCOL) + at91_emac_read(lp, AT91_EMAC_MCOL) + lcol + ecol);
901
	}
902
	return &dev->stats;
903 904 905 906 907 908 909 910
}

/*
 * Extract received frame from buffer descriptors and sent to upper layers.
 * (Called from interrupt context)
 */
static void at91ether_rx(struct net_device *dev)
{
911
	struct at91_private *lp = netdev_priv(dev);
912 913 914 915 916 917 918 919 920
	struct recv_desc_bufs *dlist;
	unsigned char *p_recv;
	struct sk_buff *skb;
	unsigned int pktlen;

	dlist = lp->dlist;
	while (dlist->descriptors[lp->rxBuffIndex].addr & EMAC_DESC_DONE) {
		p_recv = dlist->recv_buf[lp->rxBuffIndex];
		pktlen = dlist->descriptors[lp->rxBuffIndex].size & 0x7ff;	/* Length of frame including FCS */
921
		skb = netdev_alloc_skb(dev, pktlen + 2);
922 923 924 925 926
		if (skb != NULL) {
			skb_reserve(skb, 2);
			memcpy(skb_put(skb, pktlen), p_recv, pktlen);

			skb->protocol = eth_type_trans(skb, dev);
927
			dev->stats.rx_bytes += pktlen;
928 929 930
			netif_rx(skb);
		}
		else {
931
			dev->stats.rx_dropped += 1;
932 933 934 935
			printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
		}

		if (dlist->descriptors[lp->rxBuffIndex].size & EMAC_MULTICAST)
936
			dev->stats.multicast++;
937 938 939 940 941 942 943 944 945 946 947 948

		dlist->descriptors[lp->rxBuffIndex].addr &= ~EMAC_DESC_DONE;	/* reset ownership bit */
		if (lp->rxBuffIndex == MAX_RX_DESCR-1)				/* wrap after last buffer */
			lp->rxBuffIndex = 0;
		else
			lp->rxBuffIndex++;
	}
}

/*
 * MAC interrupt handler
 */
949
static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
950 951
{
	struct net_device *dev = (struct net_device *) dev_id;
952
	struct at91_private *lp = netdev_priv(dev);
953 954 955 956
	unsigned long intstatus, ctl;

	/* MAC Interrupt Status register indicates what interrupts are pending.
	   It is automatically cleared once read. */
957
	intstatus = at91_emac_read(lp, AT91_EMAC_ISR);
958 959 960 961

	if (intstatus & AT91_EMAC_RCOM)		/* Receive complete */
		at91ether_rx(dev);

962
	if (intstatus & AT91_EMAC_TCOM) {	/* Transmit complete */
963 964
		/* The TCOM bit is set even if the transmission failed. */
		if (intstatus & (AT91_EMAC_TUND | AT91_EMAC_RTRY))
965
			dev->stats.tx_errors += 1;
966 967 968 969 970 971 972 973 974 975 976

		if (lp->skb) {
			dev_kfree_skb_irq(lp->skb);
			lp->skb = NULL;
			dma_unmap_single(NULL, lp->skb_physaddr, lp->skb_length, DMA_TO_DEVICE);
		}
		netif_wake_queue(dev);
	}

	/* Work-around for Errata #11 */
	if (intstatus & AT91_EMAC_RBNA) {
977 978 979
		ctl = at91_emac_read(lp, AT91_EMAC_CTL);
		at91_emac_write(lp, AT91_EMAC_CTL, ctl & ~AT91_EMAC_RE);
		at91_emac_write(lp, AT91_EMAC_CTL, ctl | AT91_EMAC_RE);
980 981 982 983 984 985 986 987
	}

	if (intstatus & AT91_EMAC_ROVR)
		printk("%s: ROVR error\n", dev->name);

	return IRQ_HANDLED;
}

988 989 990 991 992 993 994 995 996 997 998
#ifdef CONFIG_NET_POLL_CONTROLLER
static void at91ether_poll_controller(struct net_device *dev)
{
	unsigned long flags;

	local_irq_save(flags);
	at91ether_interrupt(dev->irq, dev);
	local_irq_restore(flags);
}
#endif

999 1000 1001 1002 1003
static const struct net_device_ops at91ether_netdev_ops = {
	.ndo_open		= at91ether_open,
	.ndo_stop		= at91ether_close,
	.ndo_start_xmit		= at91ether_start_xmit,
	.ndo_get_stats		= at91ether_stats,
1004
	.ndo_set_rx_mode	= at91ether_set_multicast_list,
1005 1006 1007 1008 1009 1010 1011 1012 1013
	.ndo_set_mac_address	= set_mac_address,
	.ndo_do_ioctl		= at91ether_ioctl,
	.ndo_validate_addr	= eth_validate_addr,
	.ndo_change_mtu		= eth_change_mtu,
#ifdef CONFIG_NET_POLL_CONTROLLER
	.ndo_poll_controller	= at91ether_poll_controller,
#endif
};

1014
/*
1015
 * Detect the PHY type, and its address.
1016
 */
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
static int __init at91ether_phy_detect(struct at91_private *lp)
{
	unsigned int phyid1, phyid2;
	unsigned long phy_id;
	unsigned short phy_address = 0;

	while (phy_address < PHY_MAX_ADDR) {
		/* Read the PHY ID registers */
		enable_mdi(lp);
		read_phy(lp, phy_address, MII_PHYSID1, &phyid1);
		read_phy(lp, phy_address, MII_PHYSID2, &phyid2);
		disable_mdi(lp);

		phy_id = (phyid1 << 16) | (phyid2 & 0xfff0);
		switch (phy_id) {
			case MII_DM9161_ID:		/* Davicom 9161: PHY_ID1 = 0x181, PHY_ID2 = B881 */
			case MII_DM9161A_ID:		/* Davicom 9161A: PHY_ID1 = 0x181, PHY_ID2 = B8A0 */
			case MII_LXT971A_ID:		/* Intel LXT971A: PHY_ID1 = 0x13, PHY_ID2 = 78E0 */
			case MII_RTL8201_ID:		/* Realtek RTL8201: PHY_ID1 = 0, PHY_ID2 = 0x8201 */
			case MII_BCM5221_ID:		/* Broadcom BCM5221: PHY_ID1 = 0x40, PHY_ID2 = 0x61e0 */
			case MII_DP83847_ID:		/* National Semiconductor DP83847:  */
			case MII_DP83848_ID:		/* National Semiconductor DP83848:  */
			case MII_AC101L_ID:		/* Altima AC101L: PHY_ID1 = 0x22, PHY_ID2 = 0x5520 */
			case MII_KS8721_ID:		/* Micrel KS8721: PHY_ID1 = 0x22, PHY_ID2 = 0x1610 */
			case MII_T78Q21x3_ID:		/* Teridian 78Q21x3: PHY_ID1 = 0x0E, PHY_ID2 = 7237 */
			case MII_LAN83C185_ID:		/* SMSC LAN83C185: PHY_ID1 = 0x0007, PHY_ID2 = 0xC0A1 */
				/* store detected values */
				lp->phy_type = phy_id;		/* Type of PHY connected */
				lp->phy_address = phy_address;	/* MDI address of PHY */
				return 1;
		}

		phy_address++;
	}

	return 0;		/* not detected */
}


/*
 * Detect MAC & PHY and perform ethernet interface initialization
 */
static int __init at91ether_probe(struct platform_device *pdev)
1060
{
1061
	struct macb_platform_data *board_data = pdev->dev.platform_data;
1062
	struct resource *regs;
1063 1064 1065 1066
	struct net_device *dev;
	struct at91_private *lp;
	int res;

1067 1068 1069 1070
	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!regs)
		return -ENOENT;

1071 1072 1073 1074
	dev = alloc_etherdev(sizeof(struct at91_private));
	if (!dev)
		return -ENOMEM;

1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
	lp = netdev_priv(dev);
	lp->board_data = *board_data;
	spin_lock_init(&lp->lock);

	dev->base_addr = regs->start;		/* physical base address */
	lp->emac_base = ioremap(regs->start, regs->end - regs->start + 1);
	if (!lp->emac_base) {
		res = -ENOMEM;
		goto err_free_dev;
	}

	/* Clock */
	lp->ether_clk = clk_get(&pdev->dev, "ether_clk");
	if (IS_ERR(lp->ether_clk)) {
1089
		res = PTR_ERR(lp->ether_clk);
1090 1091 1092
		goto err_ioumap;
	}
	clk_enable(lp->ether_clk);
1093 1094

	/* Install the interrupt handler */
1095
	dev->irq = platform_get_irq(pdev, 0);
1096
	if (request_irq(dev->irq, at91ether_interrupt, 0, dev->name, dev)) {
1097 1098
		res = -EBUSY;
		goto err_disable_clock;
1099 1100 1101 1102 1103
	}

	/* Allocate memory for DMA Receive descriptors */
	lp->dlist = (struct recv_desc_bufs *) dma_alloc_coherent(NULL, sizeof(struct recv_desc_bufs), (dma_addr_t *) &lp->dlist_phys, GFP_KERNEL);
	if (lp->dlist == NULL) {
1104 1105
		res = -ENOMEM;
		goto err_free_irq;
1106 1107 1108
	}

	ether_setup(dev);
1109
	dev->netdev_ops = &at91ether_netdev_ops;
1110
	dev->ethtool_ops = &at91ether_ethtool_ops;
1111
	platform_set_drvdata(pdev, dev);
1112 1113 1114 1115 1116
	SET_NETDEV_DEV(dev, &pdev->dev);

	get_mac_address(dev);		/* Get ethernet address and store it in dev->dev_addr */
	update_mac_address(dev);	/* Program ethernet address into MAC */

1117
	at91_emac_write(lp, AT91_EMAC_CTL, 0);
1118

1119 1120
	if (board_data->is_rmii)
		at91_emac_write(lp, AT91_EMAC_CFG, AT91_EMAC_CLK_DIV32 | AT91_EMAC_BIG | AT91_EMAC_RMII);
1121
	else
1122
		at91_emac_write(lp, AT91_EMAC_CFG, AT91_EMAC_CLK_DIV32 | AT91_EMAC_BIG);
1123

1124 1125 1126 1127 1128 1129
	/* Detect PHY */
	if (!at91ether_phy_detect(lp)) {
		printk(KERN_ERR "at91_ether: Could not detect ethernet PHY\n");
		res = -ENODEV;
		goto err_free_dmamem;
	}
1130

1131
	initialize_phy(lp);
1132 1133 1134 1135

	lp->mii.dev = dev;		/* Support for ethtool */
	lp->mii.mdio_read = mdio_read;
	lp->mii.mdio_write = mdio_write;
1136
	lp->mii.phy_id = lp->phy_address;
1137 1138
	lp->mii.phy_id_mask = 0x1f;
	lp->mii.reg_num_mask = 0x1f;
1139 1140 1141

	/* Register the network interface */
	res = register_netdev(dev);
1142 1143
	if (res)
		goto err_free_dmamem;
1144 1145 1146

	/* Determine current link speed */
	spin_lock_irq(&lp->lock);
1147
	enable_mdi(lp);
1148
	update_linkspeed(dev, 0);
1149
	disable_mdi(lp);
1150 1151 1152
	spin_unlock_irq(&lp->lock);
	netif_carrier_off(dev);		/* will be enabled in open() */

1153
	/* If board has no PHY IRQ, use a timer to poll the PHY */
1154 1155 1156 1157
	if (gpio_is_valid(lp->board_data.phy_irq_pin)) {
		gpio_request(board_data->phy_irq_pin, "ethernet_phy");
	} else {
		/* If board has no PHY IRQ, use a timer to poll the PHY */
1158 1159 1160
		init_timer(&lp->check_timer);
		lp->check_timer.data = (unsigned long)dev;
		lp->check_timer.function = at91ether_check_link;
1161
	}
1162

1163
	/* Display ethernet banner */
J
Johannes Berg 已提交
1164
	printk(KERN_INFO "%s: AT91 ethernet at 0x%08x int=%d %s%s (%pM)\n",
1165
	       dev->name, (uint) dev->base_addr, dev->irq,
1166 1167
	       at91_emac_read(lp, AT91_EMAC_CFG) & AT91_EMAC_SPD ? "100-" : "10-",
	       at91_emac_read(lp, AT91_EMAC_CFG) & AT91_EMAC_FD ? "FullDuplex" : "HalfDuplex",
J
Johannes Berg 已提交
1168
	       dev->dev_addr);
1169
	if ((lp->phy_type == MII_DM9161_ID) || (lp->phy_type == MII_DM9161A_ID))
1170
		printk(KERN_INFO "%s: Davicom 9161 PHY %s\n", dev->name, (lp->phy_media == PORT_FIBRE) ? "(Fiber)" : "(Copper)");
1171
	else if (lp->phy_type == MII_LXT971A_ID)
1172
		printk(KERN_INFO "%s: Intel LXT971A PHY\n", dev->name);
1173
	else if (lp->phy_type == MII_RTL8201_ID)
1174
		printk(KERN_INFO "%s: Realtek RTL8201(B)L PHY\n", dev->name);
1175
	else if (lp->phy_type == MII_BCM5221_ID)
1176
		printk(KERN_INFO "%s: Broadcom BCM5221 PHY\n", dev->name);
1177
	else if (lp->phy_type == MII_DP83847_ID)
1178
		printk(KERN_INFO "%s: National Semiconductor DP83847 PHY\n", dev->name);
1179
	else if (lp->phy_type == MII_DP83848_ID)
1180
		printk(KERN_INFO "%s: National Semiconductor DP83848 PHY\n", dev->name);
1181
	else if (lp->phy_type == MII_AC101L_ID)
1182
		printk(KERN_INFO "%s: Altima AC101L PHY\n", dev->name);
1183
	else if (lp->phy_type == MII_KS8721_ID)
1184
		printk(KERN_INFO "%s: Micrel KS8721 PHY\n", dev->name);
1185
	else if (lp->phy_type == MII_T78Q21x3_ID)
1186
		printk(KERN_INFO "%s: Teridian 78Q21x3 PHY\n", dev->name);
1187
	else if (lp->phy_type == MII_LAN83C185_ID)
1188
		printk(KERN_INFO "%s: SMSC LAN83C185 PHY\n", dev->name);
1189

1190
	clk_disable(lp->ether_clk);					/* Disable Peripheral clock */
1191

1192
	return 0;
1193 1194


1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
err_free_dmamem:
	platform_set_drvdata(pdev, NULL);
	dma_free_coherent(NULL, sizeof(struct recv_desc_bufs), lp->dlist, (dma_addr_t)lp->dlist_phys);
err_free_irq:
	free_irq(dev->irq, dev);
err_disable_clock:
	clk_disable(lp->ether_clk);
	clk_put(lp->ether_clk);
err_ioumap:
	iounmap(lp->emac_base);
err_free_dev:
	free_netdev(dev);
	return res;
1208 1209 1210 1211
}

static int __devexit at91ether_remove(struct platform_device *pdev)
{
1212 1213
	struct net_device *dev = platform_get_drvdata(pdev);
	struct at91_private *lp = netdev_priv(dev);
1214

1215
	if (gpio_is_valid(lp->board_data.phy_irq_pin))
1216 1217
		gpio_free(lp->board_data.phy_irq_pin);

1218 1219
	unregister_netdev(dev);
	free_irq(dev->irq, dev);
1220
	dma_free_coherent(NULL, sizeof(struct recv_desc_bufs), lp->dlist, (dma_addr_t)lp->dlist_phys);
1221
	clk_put(lp->ether_clk);
1222

1223 1224
	platform_set_drvdata(pdev, NULL);
	free_netdev(dev);
1225 1226 1227
	return 0;
}

1228 1229 1230 1231 1232
#ifdef CONFIG_PM

static int at91ether_suspend(struct platform_device *pdev, pm_message_t mesg)
{
	struct net_device *net_dev = platform_get_drvdata(pdev);
1233
	struct at91_private *lp = netdev_priv(net_dev);
1234 1235

	if (netif_running(net_dev)) {
1236
		if (gpio_is_valid(lp->board_data.phy_irq_pin)) {
1237
			int phy_irq = gpio_to_irq(lp->board_data.phy_irq_pin);
1238
			disable_irq(phy_irq);
1239
		}
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251

		netif_stop_queue(net_dev);
		netif_device_detach(net_dev);

		clk_disable(lp->ether_clk);
	}
	return 0;
}

static int at91ether_resume(struct platform_device *pdev)
{
	struct net_device *net_dev = platform_get_drvdata(pdev);
1252
	struct at91_private *lp = netdev_priv(net_dev);
1253 1254 1255 1256 1257 1258 1259

	if (netif_running(net_dev)) {
		clk_enable(lp->ether_clk);

		netif_device_attach(net_dev);
		netif_start_queue(net_dev);

1260
		if (gpio_is_valid(lp->board_data.phy_irq_pin)) {
1261
			int phy_irq = gpio_to_irq(lp->board_data.phy_irq_pin);
1262
			enable_irq(phy_irq);
1263
		}
1264 1265 1266 1267 1268 1269 1270 1271 1272
	}
	return 0;
}

#else
#define at91ether_suspend	NULL
#define at91ether_resume	NULL
#endif

1273 1274
static struct platform_driver at91ether_driver = {
	.remove		= __devexit_p(at91ether_remove),
1275 1276
	.suspend	= at91ether_suspend,
	.resume		= at91ether_resume,
1277 1278 1279 1280 1281 1282 1283 1284
	.driver		= {
		.name	= DRV_NAME,
		.owner	= THIS_MODULE,
	},
};

static int __init at91ether_init(void)
{
1285
	return platform_driver_probe(&at91ether_driver, at91ether_probe);
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
}

static void __exit at91ether_exit(void)
{
	platform_driver_unregister(&at91ether_driver);
}

module_init(at91ether_init)
module_exit(at91ether_exit)

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("AT91RM9200 EMAC Ethernet driver");
MODULE_AUTHOR("Andrew Victor");
1299
MODULE_ALIAS("platform:" DRV_NAME);