at91_ether.c 20.0 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
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/dma-mapping.h>
#include <linux/ethtool.h>
28
#include <linux/platform_data/macb.h>
29 30
#include <linux/platform_device.h>
#include <linux/clk.h>
31
#include <linux/gfp.h>
32
#include <linux/phy.h>
33 34 35 36 37

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

38
#include "macb.h"
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

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

/* ......................... 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)
{
91
	struct macb *lp = netdev_priv(dev);
92

93
	/* Check Specific-Address 1 */
94
	if (unpack_mac_address(dev, macb_readl(lp, SA1T), macb_readl(lp, SA1B)))
95 96
		return;
	/* Check Specific-Address 2 */
97
	if (unpack_mac_address(dev, macb_readl(lp, SA2T), macb_readl(lp, SA2B)))
98 99
		return;
	/* Check Specific-Address 3 */
100
	if (unpack_mac_address(dev, macb_readl(lp, SA3T), macb_readl(lp, SA3B)))
101 102
		return;
	/* Check Specific-Address 4 */
103
	if (unpack_mac_address(dev, macb_readl(lp, SA4T), macb_readl(lp, SA4B)))
104 105 106 107 108 109 110 111 112 113
		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)
{
114
	struct macb *lp = netdev_priv(dev);
115

116
	macb_writel(lp, SA1B, (dev->dev_addr[3] << 24) | (dev->dev_addr[2] << 16)
117
					| (dev->dev_addr[1] << 8) | (dev->dev_addr[0]));
118
	macb_writel(lp, SA1T, (dev->dev_addr[5] << 8) | (dev->dev_addr[4]));
119

120 121
	macb_writel(lp, SA2B, 0);
	macb_writel(lp, SA2T, 0);
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
}

/*
 * 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 已提交
137 138
	printk("%s: Setting MAC address to %pM\n", dev->name,
	       dev->dev_addr);
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

	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);
	}

193
	return hash_index;
194 195 196 197 198 199 200
}

/*
 * Add multicast addresses to the internal multicast-hash table.
 */
static void at91ether_sethashtable(struct net_device *dev)
{
201
	struct macb *lp = netdev_priv(dev);
202
	struct netdev_hw_addr *ha;
203
	unsigned long mc_filter[2];
204
	unsigned int bitnr;
205 206 207

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

208 209
	netdev_for_each_mc_addr(ha, dev) {
		bitnr = hash_get_index(ha->addr);
210 211 212
		mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
	}

213 214
	macb_writel(lp, HRB, mc_filter[0]);
	macb_writel(lp, HRT, mc_filter[1]);
215 216 217 218 219
}

/*
 * Enable/Disable promiscuous and multicast modes.
 */
220
static void at91ether_set_multicast_list(struct net_device *dev)
221
{
222
	struct macb *lp = netdev_priv(dev);
223 224
	unsigned long cfg;

225
	cfg = macb_readl(lp, NCFGR);
226 227

	if (dev->flags & IFF_PROMISC)			/* Enable promiscuous mode */
228
		cfg |= MACB_BIT(CAF);
229
	else if (dev->flags & (~IFF_PROMISC))		/* Disable promiscuous mode */
230
		cfg &= ~MACB_BIT(CAF);
231 232

	if (dev->flags & IFF_ALLMULTI) {		/* Enable all multicast mode */
233 234
		macb_writel(lp, HRT, -1);
		macb_writel(lp, HRB, -1);
235
		cfg |= MACB_BIT(NCFGR_MTI);
236
	} else if (!netdev_mc_empty(dev)) { /* Enable specific multicasts */
237
		at91ether_sethashtable(dev);
238
		cfg |= MACB_BIT(NCFGR_MTI);
239
	} else if (dev->flags & (~IFF_ALLMULTI)) {	/* Disable all multicast mode */
240 241
		macb_writel(lp, HRT, 0);
		macb_writel(lp, HRB, 0);
242
		cfg &= ~MACB_BIT(NCFGR_MTI);
243 244
	}

245
	macb_writel(lp, NCFGR, cfg);
246 247 248 249 250 251 252 253 254
}

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

/*
 * Initialize and start the Receiver and Transmit subsystems
 */
static void at91ether_start(struct net_device *dev)
{
255
	struct macb *lp = netdev_priv(dev);
256 257 258 259 260 261 262 263 264 265 266 267 268
	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 */
269
	dlist->descriptors[i-1].addr |= MACB_BIT(RX_WRAP);
270 271 272 273 274

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

	/* Program address of descriptor list in Rx Buffer Queue register */
275
	macb_writel(lp, RBQP, (unsigned long) dlist_phys);
276 277

	/* Enable Receive and Transmit */
278 279
	ctl = macb_readl(lp, NCR);
	macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
280 281 282 283 284 285 286
}

/*
 * Open the ethernet interface
 */
static int at91ether_open(struct net_device *dev)
{
287
	struct macb *lp = netdev_priv(dev);
288 289
	unsigned long ctl;

290 291
	if (!is_valid_ether_addr(dev->dev_addr))
		return -EADDRNOTAVAIL;
292 293

	/* Clear internal statistics */
294 295
	ctl = macb_readl(lp, NCR);
	macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
296 297 298 299 300

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

	/* Enable MAC interrupts */
301
	macb_writel(lp, IER, MACB_BIT(RCOMP) | MACB_BIT(RXUBR)
302 303
				| MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE) | MACB_BIT(TCOMP)
				| MACB_BIT(ISR_ROVR) | MACB_BIT(HRESP));
304 305

	at91ether_start(dev);
306 307 308 309

	/* schedule a link state check */
	phy_start(lp->phy_dev);

310
	netif_start_queue(dev);
311

312 313 314 315 316 317 318 319
	return 0;
}

/*
 * Close the interface
 */
static int at91ether_close(struct net_device *dev)
{
320
	struct macb *lp = netdev_priv(dev);
321 322 323
	unsigned long ctl;

	/* Disable Receiver and Transmitter */
324 325
	ctl = macb_readl(lp, NCR);
	macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
326 327

	/* Disable MAC interrupts */
328
	macb_writel(lp, IDR, MACB_BIT(RCOMP) | MACB_BIT(RXUBR)
329 330 331
				| MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)
				| MACB_BIT(TCOMP) | MACB_BIT(ISR_ROVR)
				| MACB_BIT(HRESP));
332 333 334 335 336 337 338 339 340

	netif_stop_queue(dev);

	return 0;
}

/*
 * Transmit packet.
 */
341
static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
342
{
343
	struct macb *lp = netdev_priv(dev);
344

345
	if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
346 347 348 349 350 351
		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);
352
		dev->stats.tx_bytes += skb->len;
353 354

		/* Set address of the data in the Transmit Address register */
355
		macb_writel(lp, TAR, lp->skb_physaddr);
356
		/* Set length of the packet in the Transmit Control register */
357
		macb_writel(lp, TCR, skb->len);
358 359

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

366
	return NETDEV_TX_OK;
367 368 369 370 371 372 373
}

/*
 * Update the current statistics from the internal statistics registers.
 */
static struct net_device_stats *at91ether_stats(struct net_device *dev)
{
374
	struct macb *lp = netdev_priv(dev);
375 376 377
	int ale, lenerr, seqe, lcol, ecol;

	if (netif_running(dev)) {
378 379
		dev->stats.rx_packets += macb_readl(lp, FRO);	/* Good frames received */
		ale = macb_readl(lp, ALE);
380
		dev->stats.rx_frame_errors += ale;				/* Alignment errors */
381
		lenerr = macb_readl(lp, ELE) + macb_readl(lp, USF);
382
		dev->stats.rx_length_errors += lenerr;				/* Excessive Length or Undersize Frame error */
383
		seqe = macb_readl(lp, FCSE);
384
		dev->stats.rx_crc_errors += seqe;				/* CRC error */
385
		dev->stats.rx_fifo_errors += macb_readl(lp, RRE);/* Receive buffer not available */
386
		dev->stats.rx_errors += (ale + lenerr + seqe
387
			+ macb_readl(lp, RSE) + macb_readl(lp, RJA));
388

389 390 391 392
		dev->stats.tx_packets += macb_readl(lp, FTO);	/* Frames successfully transmitted */
		dev->stats.tx_fifo_errors += macb_readl(lp, TUND);	/* Transmit FIFO underruns */
		dev->stats.tx_carrier_errors += macb_readl(lp, CSE);	/* Carrier Sense errors */
		dev->stats.tx_heartbeat_errors += macb_readl(lp, STE);/* Heartbeat error */
393

394 395
		lcol = macb_readl(lp, LCOL);
		ecol = macb_readl(lp, EXCOL);
396 397
		dev->stats.tx_window_errors += lcol;			/* Late collisions */
		dev->stats.tx_aborted_errors += ecol;			/* 16 collisions */
398

399
		dev->stats.collisions += (macb_readl(lp, SCF) + macb_readl(lp, MCF) + lcol + ecol);
400
	}
401
	return &dev->stats;
402 403 404 405 406 407 408 409
}

/*
 * Extract received frame from buffer descriptors and sent to upper layers.
 * (Called from interrupt context)
 */
static void at91ether_rx(struct net_device *dev)
{
410
	struct macb *lp = netdev_priv(dev);
411 412 413 414 415 416
	struct recv_desc_bufs *dlist;
	unsigned char *p_recv;
	struct sk_buff *skb;
	unsigned int pktlen;

	dlist = lp->dlist;
417
	while (dlist->descriptors[lp->rxBuffIndex].addr & MACB_BIT(RX_USED)) {
418 419
		p_recv = dlist->recv_buf[lp->rxBuffIndex];
		pktlen = dlist->descriptors[lp->rxBuffIndex].size & 0x7ff;	/* Length of frame including FCS */
420
		skb = netdev_alloc_skb(dev, pktlen + 2);
421 422 423 424 425
		if (skb != NULL) {
			skb_reserve(skb, 2);
			memcpy(skb_put(skb, pktlen), p_recv, pktlen);

			skb->protocol = eth_type_trans(skb, dev);
426
			dev->stats.rx_bytes += pktlen;
427 428 429
			netif_rx(skb);
		}
		else {
430
			dev->stats.rx_dropped += 1;
431 432 433
			printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
		}

434
		if (dlist->descriptors[lp->rxBuffIndex].size & MACB_BIT(RX_MHASH_MATCH))
435
			dev->stats.multicast++;
436

437
		dlist->descriptors[lp->rxBuffIndex].addr &= ~MACB_BIT(RX_USED);	/* reset ownership bit */
438 439 440 441 442 443 444 445 446 447
		if (lp->rxBuffIndex == MAX_RX_DESCR-1)				/* wrap after last buffer */
			lp->rxBuffIndex = 0;
		else
			lp->rxBuffIndex++;
	}
}

/*
 * MAC interrupt handler
 */
448
static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
449 450
{
	struct net_device *dev = (struct net_device *) dev_id;
451
	struct macb *lp = netdev_priv(dev);
452 453 454 455
	unsigned long intstatus, ctl;

	/* MAC Interrupt Status register indicates what interrupts are pending.
	   It is automatically cleared once read. */
456
	intstatus = macb_readl(lp, ISR);
457

458
	if (intstatus & MACB_BIT(RCOMP))		/* Receive complete */
459 460
		at91ether_rx(dev);

461
	if (intstatus & MACB_BIT(TCOMP)) {	/* Transmit complete */
462
		/* The TCOM bit is set even if the transmission failed. */
463
		if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
464
			dev->stats.tx_errors += 1;
465 466 467 468 469 470 471 472 473 474

		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 */
475
	if (intstatus & MACB_BIT(RXUBR)) {
476 477 478
		ctl = macb_readl(lp, NCR);
		macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
		macb_writel(lp, NCR, ctl | MACB_BIT(RE));
479 480
	}

481
	if (intstatus & MACB_BIT(ISR_ROVR))
482 483 484 485 486
		printk("%s: ROVR error\n", dev->name);

	return IRQ_HANDLED;
}

487 488 489 490 491 492 493 494 495 496 497
#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

498 499 500 501 502
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,
503
	.ndo_set_rx_mode	= at91ether_set_multicast_list,
504
	.ndo_set_mac_address	= set_mac_address,
505
	.ndo_do_ioctl		= macb_ioctl,
506 507 508 509 510 511 512
	.ndo_validate_addr	= eth_validate_addr,
	.ndo_change_mtu		= eth_change_mtu,
#ifdef CONFIG_NET_POLL_CONTROLLER
	.ndo_poll_controller	= at91ether_poll_controller,
#endif
};

513 514 515 516
/*
 * Detect MAC & PHY and perform ethernet interface initialization
 */
static int __init at91ether_probe(struct platform_device *pdev)
517
{
518
	struct macb_platform_data *board_data = pdev->dev.platform_data;
519
	struct resource *regs;
520
	struct net_device *dev;
521
	struct phy_device *phydev;
522
	struct macb *lp;
523 524
	int res;

525 526 527 528
	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!regs)
		return -ENOENT;

529
	dev = alloc_etherdev(sizeof(struct macb));
530 531 532
	if (!dev)
		return -ENOMEM;

533
	lp = netdev_priv(dev);
534 535
	lp->pdev = pdev;
	lp->dev = dev;
536 537 538 539
	lp->board_data = *board_data;
	spin_lock_init(&lp->lock);

	dev->base_addr = regs->start;		/* physical base address */
540 541
	lp->regs = ioremap(regs->start, regs->end - regs->start + 1);
	if (!lp->regs) {
542 543 544 545 546
		res = -ENOMEM;
		goto err_free_dev;
	}

	/* Clock */
547 548 549
	lp->pclk = clk_get(&pdev->dev, "ether_clk");
	if (IS_ERR(lp->pclk)) {
		res = PTR_ERR(lp->pclk);
550 551
		goto err_ioumap;
	}
552
	clk_enable(lp->pclk);
553 554

	/* Install the interrupt handler */
555
	dev->irq = platform_get_irq(pdev, 0);
556
	if (request_irq(dev->irq, at91ether_interrupt, 0, dev->name, dev)) {
557 558
		res = -EBUSY;
		goto err_disable_clock;
559 560 561 562 563
	}

	/* 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) {
564 565
		res = -ENOMEM;
		goto err_free_irq;
566 567 568
	}

	ether_setup(dev);
569
	dev->netdev_ops = &at91ether_netdev_ops;
570
	dev->ethtool_ops = &macb_ethtool_ops;
571
	platform_set_drvdata(pdev, dev);
572 573 574 575 576
	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 */

577
	macb_writel(lp, NCR, 0);
578

579
	if (board_data->is_rmii) {
580
		macb_writel(lp, NCFGR, MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG) | MACB_BIT(RM9200_RMII));
581 582
		lp->phy_interface = PHY_INTERFACE_MODE_RMII;
	} else {
583
		macb_writel(lp, NCFGR, MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG));
584
		lp->phy_interface = PHY_INTERFACE_MODE_MII;
585
	}
586

587 588
	/* Register the network interface */
	res = register_netdev(dev);
589 590
	if (res)
		goto err_free_dmamem;
591

592 593 594
	if (macb_mii_init(lp) != 0)
		goto err_out_unregister_netdev;

595 596
	netif_carrier_off(dev);		/* will be enabled in open() */

597 598 599
	phydev = lp->phy_dev;
	netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
		phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
600

601
	/* Display ethernet banner */
J
Johannes Berg 已提交
602
	printk(KERN_INFO "%s: AT91 ethernet at 0x%08x int=%d %s%s (%pM)\n",
603
	       dev->name, (uint) dev->base_addr, dev->irq,
604 605
	       macb_readl(lp, NCFGR) & MACB_BIT(SPD) ? "100-" : "10-",
	       macb_readl(lp, NCFGR) & MACB_BIT(FD) ? "FullDuplex" : "HalfDuplex",
J
Johannes Berg 已提交
606
	       dev->dev_addr);
607

608
	return 0;
609

610 611
err_out_unregister_netdev:
	unregister_netdev(dev);
612 613 614 615 616 617
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:
618 619
	clk_disable(lp->pclk);
	clk_put(lp->pclk);
620
err_ioumap:
621
	iounmap(lp->regs);
622 623 624
err_free_dev:
	free_netdev(dev);
	return res;
625 626 627 628
}

static int __devexit at91ether_remove(struct platform_device *pdev)
{
629
	struct net_device *dev = platform_get_drvdata(pdev);
630
	struct macb *lp = netdev_priv(dev);
631

632 633
	if (lp->phy_dev)
		phy_disconnect(lp->phy_dev);
634

635 636 637
	mdiobus_unregister(lp->mii_bus);
	kfree(lp->mii_bus->irq);
	mdiobus_free(lp->mii_bus);
638 639
	unregister_netdev(dev);
	free_irq(dev->irq, dev);
640
	dma_free_coherent(NULL, sizeof(struct recv_desc_bufs), lp->dlist, (dma_addr_t)lp->dlist_phys);
641 642
	iounmap(lp->regs);
	clk_disable(lp->pclk);
643
	clk_put(lp->pclk);
644
	free_netdev(dev);
645 646
	platform_set_drvdata(pdev, NULL);

647 648 649
	return 0;
}

650 651 652 653 654
#ifdef CONFIG_PM

static int at91ether_suspend(struct platform_device *pdev, pm_message_t mesg)
{
	struct net_device *net_dev = platform_get_drvdata(pdev);
655
	struct macb *lp = netdev_priv(net_dev);
656 657 658 659 660

	if (netif_running(net_dev)) {
		netif_stop_queue(net_dev);
		netif_device_detach(net_dev);

661
		clk_disable(lp->pclk);
662 663 664 665 666 667 668
	}
	return 0;
}

static int at91ether_resume(struct platform_device *pdev)
{
	struct net_device *net_dev = platform_get_drvdata(pdev);
669
	struct macb *lp = netdev_priv(net_dev);
670 671

	if (netif_running(net_dev)) {
672
		clk_enable(lp->pclk);
673 674 675 676 677 678 679 680 681 682 683 684

		netif_device_attach(net_dev);
		netif_start_queue(net_dev);
	}
	return 0;
}

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

685 686
static struct platform_driver at91ether_driver = {
	.remove		= __devexit_p(at91ether_remove),
687 688
	.suspend	= at91ether_suspend,
	.resume		= at91ether_resume,
689 690 691 692 693 694 695 696
	.driver		= {
		.name	= DRV_NAME,
		.owner	= THIS_MODULE,
	},
};

static int __init at91ether_init(void)
{
697
	return platform_driver_probe(&at91ether_driver, at91ether_probe);
698 699 700 701 702 703 704 705 706 707 708 709 710
}

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");
711
MODULE_ALIAS("platform:" DRV_NAME);