at91_ether.c 17.7 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
#include <linux/io.h>
34 35 36
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_net.h>
37

38
#include "macb.h"
39 40 41 42

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

43 44 45 46 47
/* 1518 rounded up */
#define MAX_RBUFF_SZ	0x600
/* max number of receive buffers */
#define MAX_RX_DESCR	9

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
/* ......................... 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)
{
65
	struct macb *lp = netdev_priv(dev);
66 67
	char addr[6];

68
	if (lp->board_data.rev_eth_addr) {
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
		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)
{
97
	struct macb *lp = netdev_priv(dev);
98

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

122
	macb_writel(lp, SA1B, (dev->dev_addr[3] << 24) | (dev->dev_addr[2] << 16)
123
					| (dev->dev_addr[1] << 8) | (dev->dev_addr[0]));
124
	macb_writel(lp, SA1T, (dev->dev_addr[5] << 8) | (dev->dev_addr[4]));
125

126 127
	macb_writel(lp, SA2B, 0);
	macb_writel(lp, SA2T, 0);
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
}

/*
 * 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 已提交
143 144
	printk("%s: Setting MAC address to %pM\n", dev->name,
	       dev->dev_addr);
145 146 147 148 149 150 151 152 153

	return 0;
}

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

/*
 * Initialize and start the Receiver and Transmit subsystems
 */
154
static int at91ether_start(struct net_device *dev)
155
{
156
	struct macb *lp = netdev_priv(dev);
157
	unsigned long ctl;
158 159 160 161
	dma_addr_t addr;
	int i;

	lp->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
162
					MAX_RX_DESCR * sizeof(struct macb_dma_desc),
163 164 165 166 167
					&lp->rx_ring_dma, GFP_KERNEL);
	if (!lp->rx_ring) {
		netdev_err(lp->dev, "unable to alloc rx ring DMA buffer\n");
		return -ENOMEM;
	}
168

169 170 171 172 173
	lp->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
					MAX_RX_DESCR * MAX_RBUFF_SZ,
					&lp->rx_buffers_dma, GFP_KERNEL);
	if (!lp->rx_buffers) {
		netdev_err(lp->dev, "unable to alloc rx data DMA buffer\n");
174

175
		dma_free_coherent(&lp->pdev->dev,
176
					MAX_RX_DESCR * sizeof(struct macb_dma_desc),
177 178 179 180 181 182
					lp->rx_ring, lp->rx_ring_dma);
		lp->rx_ring = NULL;
		return -ENOMEM;
	}

	addr = lp->rx_buffers_dma;
183
	for (i = 0; i < MAX_RX_DESCR; i++) {
184 185 186
		lp->rx_ring[i].addr = addr;
		lp->rx_ring[i].ctrl = 0;
		addr += MAX_RBUFF_SZ;
187 188 189
	}

	/* Set the Wrap bit on the last descriptor */
190
	lp->rx_ring[MAX_RX_DESCR - 1].addr |= MACB_BIT(RX_WRAP);
191 192

	/* Reset buffer index */
193
	lp->rx_tail = 0;
194 195

	/* Program address of descriptor list in Rx Buffer Queue register */
196
	macb_writel(lp, RBQP, lp->rx_ring_dma);
197 198

	/* Enable Receive and Transmit */
199 200
	ctl = macb_readl(lp, NCR);
	macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
201 202

	return 0;
203 204 205 206 207 208 209
}

/*
 * Open the ethernet interface
 */
static int at91ether_open(struct net_device *dev)
{
210
	struct macb *lp = netdev_priv(dev);
211
	unsigned long ctl;
212
	int ret;
213

214 215
	if (!is_valid_ether_addr(dev->dev_addr))
		return -EADDRNOTAVAIL;
216 217

	/* Clear internal statistics */
218 219
	ctl = macb_readl(lp, NCR);
	macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
220 221 222 223

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

224 225 226 227
	ret = at91ether_start(dev);
	if (ret)
		return ret;

228
	/* Enable MAC interrupts */
229
	macb_writel(lp, IER, MACB_BIT(RCOMP) | MACB_BIT(RXUBR)
230 231
				| MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE) | MACB_BIT(TCOMP)
				| MACB_BIT(ISR_ROVR) | MACB_BIT(HRESP));
232

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

236
	netif_start_queue(dev);
237

238 239 240 241 242 243 244 245
	return 0;
}

/*
 * Close the interface
 */
static int at91ether_close(struct net_device *dev)
{
246
	struct macb *lp = netdev_priv(dev);
247 248 249
	unsigned long ctl;

	/* Disable Receiver and Transmitter */
250 251
	ctl = macb_readl(lp, NCR);
	macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
252 253

	/* Disable MAC interrupts */
254
	macb_writel(lp, IDR, MACB_BIT(RCOMP) | MACB_BIT(RXUBR)
255 256 257
				| MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)
				| MACB_BIT(TCOMP) | MACB_BIT(ISR_ROVR)
				| MACB_BIT(HRESP));
258 259 260

	netif_stop_queue(dev);

261
	dma_free_coherent(&lp->pdev->dev,
262
				MAX_RX_DESCR * sizeof(struct macb_dma_desc),
263 264 265 266 267 268 269 270
				lp->rx_ring, lp->rx_ring_dma);
	lp->rx_ring = NULL;

	dma_free_coherent(&lp->pdev->dev,
				MAX_RX_DESCR * MAX_RBUFF_SZ,
				lp->rx_buffers, lp->rx_buffers_dma);
	lp->rx_buffers = NULL;

271 272 273 274 275 276
	return 0;
}

/*
 * Transmit packet.
 */
277
static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
278
{
279
	struct macb *lp = netdev_priv(dev);
280

281
	if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
282 283 284 285 286 287
		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);
288
		dev->stats.tx_bytes += skb->len;
289 290

		/* Set address of the data in the Transmit Address register */
291
		macb_writel(lp, TAR, lp->skb_physaddr);
292
		/* Set length of the packet in the Transmit Control register */
293
		macb_writel(lp, TCR, skb->len);
294 295

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

302
	return NETDEV_TX_OK;
303 304 305 306 307 308 309
}

/*
 * Update the current statistics from the internal statistics registers.
 */
static struct net_device_stats *at91ether_stats(struct net_device *dev)
{
310
	struct macb *lp = netdev_priv(dev);
311 312 313
	int ale, lenerr, seqe, lcol, ecol;

	if (netif_running(dev)) {
314 315
		dev->stats.rx_packets += macb_readl(lp, FRO);	/* Good frames received */
		ale = macb_readl(lp, ALE);
316
		dev->stats.rx_frame_errors += ale;				/* Alignment errors */
317
		lenerr = macb_readl(lp, ELE) + macb_readl(lp, USF);
318
		dev->stats.rx_length_errors += lenerr;				/* Excessive Length or Undersize Frame error */
319
		seqe = macb_readl(lp, FCSE);
320
		dev->stats.rx_crc_errors += seqe;				/* CRC error */
321
		dev->stats.rx_fifo_errors += macb_readl(lp, RRE);/* Receive buffer not available */
322
		dev->stats.rx_errors += (ale + lenerr + seqe
323
			+ macb_readl(lp, RSE) + macb_readl(lp, RJA));
324

325 326 327 328
		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 */
329

330 331
		lcol = macb_readl(lp, LCOL);
		ecol = macb_readl(lp, EXCOL);
332 333
		dev->stats.tx_window_errors += lcol;			/* Late collisions */
		dev->stats.tx_aborted_errors += ecol;			/* 16 collisions */
334

335
		dev->stats.collisions += (macb_readl(lp, SCF) + macb_readl(lp, MCF) + lcol + ecol);
336
	}
337
	return &dev->stats;
338 339 340 341 342 343 344 345
}

/*
 * Extract received frame from buffer descriptors and sent to upper layers.
 * (Called from interrupt context)
 */
static void at91ether_rx(struct net_device *dev)
{
346
	struct macb *lp = netdev_priv(dev);
347 348 349 350
	unsigned char *p_recv;
	struct sk_buff *skb;
	unsigned int pktlen;

351 352 353
	while (lp->rx_ring[lp->rx_tail].addr & MACB_BIT(RX_USED)) {
		p_recv = lp->rx_buffers + lp->rx_tail * MAX_RBUFF_SZ;
		pktlen = MACB_BF(RX_FRMLEN, lp->rx_ring[lp->rx_tail].ctrl);
354
		skb = netdev_alloc_skb(dev, pktlen + 2);
355
		if (skb) {
356 357 358 359
			skb_reserve(skb, 2);
			memcpy(skb_put(skb, pktlen), p_recv, pktlen);

			skb->protocol = eth_type_trans(skb, dev);
360
			dev->stats.rx_bytes += pktlen;
361
			netif_rx(skb);
362
		} else {
363
			dev->stats.rx_dropped += 1;
364
			netdev_notice(dev, "Memory squeeze, dropping packet.\n");
365 366
		}

367
		if (lp->rx_ring[lp->rx_tail].ctrl & MACB_BIT(RX_MHASH_MATCH))
368
			dev->stats.multicast++;
369

370 371 372 373 374 375
		/* reset ownership bit */
		lp->rx_ring[lp->rx_tail].addr &= ~MACB_BIT(RX_USED);

		/* wrap after last buffer */
		if (lp->rx_tail == MAX_RX_DESCR - 1)
			lp->rx_tail = 0;
376
		else
377
			lp->rx_tail++;
378 379 380 381 382 383
	}
}

/*
 * MAC interrupt handler
 */
384
static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
385 386
{
	struct net_device *dev = (struct net_device *) dev_id;
387
	struct macb *lp = netdev_priv(dev);
388 389 390 391
	unsigned long intstatus, ctl;

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

394
	if (intstatus & MACB_BIT(RCOMP))		/* Receive complete */
395 396
		at91ether_rx(dev);

397
	if (intstatus & MACB_BIT(TCOMP)) {	/* Transmit complete */
398
		/* The TCOM bit is set even if the transmission failed. */
399
		if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
400
			dev->stats.tx_errors += 1;
401 402 403 404 405 406 407 408 409 410

		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 */
411
	if (intstatus & MACB_BIT(RXUBR)) {
412 413 414
		ctl = macb_readl(lp, NCR);
		macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
		macb_writel(lp, NCR, ctl | MACB_BIT(RE));
415 416
	}

417
	if (intstatus & MACB_BIT(ISR_ROVR))
418 419 420 421 422
		printk("%s: ROVR error\n", dev->name);

	return IRQ_HANDLED;
}

423 424 425 426 427 428 429 430 431 432 433
#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

434 435 436 437 438
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,
439
	.ndo_set_rx_mode	= macb_set_rx_mode,
440
	.ndo_set_mac_address	= set_mac_address,
441
	.ndo_do_ioctl		= macb_ioctl,
442 443 444 445 446 447 448
	.ndo_validate_addr	= eth_validate_addr,
	.ndo_change_mtu		= eth_change_mtu,
#ifdef CONFIG_NET_POLL_CONTROLLER
	.ndo_poll_controller	= at91ether_poll_controller,
#endif
};

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 479 480 481 482 483 484 485 486 487 488 489 490 491 492
#if defined(CONFIG_OF)
static const struct of_device_id at91ether_dt_ids[] = {
	{ .compatible = "cdns,at91rm9200-emac" },
	{ .compatible = "cdns,emac" },
	{ /* sentinel */ }
};

MODULE_DEVICE_TABLE(of, at91ether_dt_ids);

static int at91ether_get_phy_mode_dt(struct platform_device *pdev)
{
	struct device_node *np = pdev->dev.of_node;

	if (np)
		return of_get_phy_mode(np);

	return -ENODEV;
}

static int at91ether_get_hwaddr_dt(struct macb *bp)
{
	struct device_node *np = bp->pdev->dev.of_node;

	if (np) {
		const char *mac = of_get_mac_address(np);
		if (mac) {
			memcpy(bp->dev->dev_addr, mac, ETH_ALEN);
			return 0;
		}
	}

	return -ENODEV;
}
#else
static int at91ether_get_phy_mode_dt(struct platform_device *pdev)
{
	return -ENODEV;
}
static int at91ether_get_hwaddr_dt(struct macb *bp)
{
	return -ENODEV;
}
#endif

493 494 495 496
/*
 * Detect MAC & PHY and perform ethernet interface initialization
 */
static int __init at91ether_probe(struct platform_device *pdev)
497
{
498
	struct macb_platform_data *board_data = pdev->dev.platform_data;
499
	struct resource *regs;
500
	struct net_device *dev;
501
	struct phy_device *phydev;
502
	struct macb *lp;
503 504
	int res;

505 506 507 508
	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!regs)
		return -ENOENT;

509
	dev = alloc_etherdev(sizeof(struct macb));
510 511 512
	if (!dev)
		return -ENOMEM;

513
	lp = netdev_priv(dev);
514 515
	lp->pdev = pdev;
	lp->dev = dev;
516 517
	if (board_data)
		lp->board_data = *board_data;
518 519 520
	spin_lock_init(&lp->lock);

	dev->base_addr = regs->start;		/* physical base address */
521
	lp->regs = devm_ioremap(&pdev->dev, regs->start, resource_size(regs));
522
	if (!lp->regs) {
523 524 525 526 527
		res = -ENOMEM;
		goto err_free_dev;
	}

	/* Clock */
528
	lp->pclk = devm_clk_get(&pdev->dev, "ether_clk");
529 530
	if (IS_ERR(lp->pclk)) {
		res = PTR_ERR(lp->pclk);
531
		goto err_free_dev;
532
	}
533
	clk_enable(lp->pclk);
534 535

	/* Install the interrupt handler */
536
	dev->irq = platform_get_irq(pdev, 0);
537 538
	res = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt, 0, dev->name, dev);
	if (res)
539
		goto err_disable_clock;
540 541

	ether_setup(dev);
542
	dev->netdev_ops = &at91ether_netdev_ops;
543
	dev->ethtool_ops = &macb_ethtool_ops;
544
	platform_set_drvdata(pdev, dev);
545 546
	SET_NETDEV_DEV(dev, &pdev->dev);

547 548 549 550
	res = at91ether_get_hwaddr_dt(lp);
	if (res < 0)
		get_mac_address(dev);		/* Get ethernet address and store it in dev->dev_addr */

551 552
	update_mac_address(dev);	/* Program ethernet address into MAC */

553 554 555 556 557 558 559 560 561 562
	res = at91ether_get_phy_mode_dt(pdev);
	if (res < 0) {
		if (board_data && board_data->is_rmii)
			lp->phy_interface = PHY_INTERFACE_MODE_RMII;
		else
			lp->phy_interface = PHY_INTERFACE_MODE_MII;
	} else {
		lp->phy_interface = res;
	}

563
	macb_writel(lp, NCR, 0);
564

565
	if (lp->phy_interface == PHY_INTERFACE_MODE_RMII)
566
		macb_writel(lp, NCFGR, MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG) | MACB_BIT(RM9200_RMII));
567
	else
568
		macb_writel(lp, NCFGR, MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG));
569

570 571
	/* Register the network interface */
	res = register_netdev(dev);
572
	if (res)
573
		goto err_disable_clock;
574

575 576 577
	if (macb_mii_init(lp) != 0)
		goto err_out_unregister_netdev;

578 579
	netif_carrier_off(dev);		/* will be enabled in open() */

580 581 582
	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);
583

584
	/* Display ethernet banner */
J
Johannes Berg 已提交
585
	printk(KERN_INFO "%s: AT91 ethernet at 0x%08x int=%d %s%s (%pM)\n",
586
	       dev->name, (uint) dev->base_addr, dev->irq,
587 588
	       macb_readl(lp, NCFGR) & MACB_BIT(SPD) ? "100-" : "10-",
	       macb_readl(lp, NCFGR) & MACB_BIT(FD) ? "FullDuplex" : "HalfDuplex",
J
Johannes Berg 已提交
589
	       dev->dev_addr);
590

591
	return 0;
592

593 594
err_out_unregister_netdev:
	unregister_netdev(dev);
595
err_disable_clock:
596
	clk_disable(lp->pclk);
597 598 599
err_free_dev:
	free_netdev(dev);
	return res;
600 601 602 603
}

static int __devexit at91ether_remove(struct platform_device *pdev)
{
604
	struct net_device *dev = platform_get_drvdata(pdev);
605
	struct macb *lp = netdev_priv(dev);
606

607 608
	if (lp->phy_dev)
		phy_disconnect(lp->phy_dev);
609

610 611 612
	mdiobus_unregister(lp->mii_bus);
	kfree(lp->mii_bus->irq);
	mdiobus_free(lp->mii_bus);
613
	unregister_netdev(dev);
614
	clk_disable(lp->pclk);
615
	free_netdev(dev);
616 617
	platform_set_drvdata(pdev, NULL);

618 619 620
	return 0;
}

621 622 623 624 625
#ifdef CONFIG_PM

static int at91ether_suspend(struct platform_device *pdev, pm_message_t mesg)
{
	struct net_device *net_dev = platform_get_drvdata(pdev);
626
	struct macb *lp = netdev_priv(net_dev);
627 628 629 630 631

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

632
		clk_disable(lp->pclk);
633 634 635 636 637 638 639
	}
	return 0;
}

static int at91ether_resume(struct platform_device *pdev)
{
	struct net_device *net_dev = platform_get_drvdata(pdev);
640
	struct macb *lp = netdev_priv(net_dev);
641 642

	if (netif_running(net_dev)) {
643
		clk_enable(lp->pclk);
644 645 646 647 648 649 650 651 652 653 654 655

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

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

656 657
static struct platform_driver at91ether_driver = {
	.remove		= __devexit_p(at91ether_remove),
658 659
	.suspend	= at91ether_suspend,
	.resume		= at91ether_resume,
660 661 662
	.driver		= {
		.name	= DRV_NAME,
		.owner	= THIS_MODULE,
663
		.of_match_table	= of_match_ptr(at91ether_dt_ids),
664 665 666 667 668
	},
};

static int __init at91ether_init(void)
{
669
	return platform_driver_probe(&at91ether_driver, at91ether_probe);
670 671 672 673 674 675 676 677 678 679 680 681 682
}

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