2 * Ethernet driver for the Atmel AT91RM9200 (Thunder)
4 * Copyright (C) 2003 SAN People (Pty) Ltd
6 * Based on an earlier Atmel EMAC macrocell driver by Atmel and Lineo Inc.
7 * Initial version by Rick Bronson 01/11/2003
9 * Intel LXT971A PHY support by Christopher Bahns & David Knickerbocker
10 * (Polaroid Corporation)
12 * Realtek RTL8201(B)L PHY support by Roman Avramenko <roman@imsystems.ru>
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/interrupt.h>
23 #include <linux/netdevice.h>
24 #include <linux/etherdevice.h>
25 #include <linux/skbuff.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/ethtool.h>
28 #include <linux/platform_data/macb.h>
29 #include <linux/platform_device.h>
30 #include <linux/clk.h>
31 #include <linux/gfp.h>
32 #include <linux/phy.h>
37 #define DRV_NAME "at91_ether"
38 #define DRV_VERSION "1.0"
41 #define MAX_RBUFF_SZ 0x600
42 /* max number of receive buffers */
43 #define MAX_RX_DESCR 9
45 /* ......................... ADDRESS MANAGEMENT ........................ */
48 * NOTE: Your bootloader must always set the MAC address correctly before
51 * - It must always set the MAC address after reset, even if it doesn't
52 * happen to access the Ethernet while it's booting. Some versions of
53 * U-Boot on the AT91RM9200-DK do not do this.
55 * - Likewise it must store the addresses in the correct byte order.
56 * MicroMonitor (uMon) on the CSB337 does this incorrectly (and
57 * continues to do so, for bug-compatibility).
60 static short __init unpack_mac_address(struct net_device *dev, unsigned int hi, unsigned int lo)
62 struct macb *lp = netdev_priv(dev);
65 if (lp->board_data.rev_eth_addr) {
66 addr[5] = (lo & 0xff); /* The CSB337 bootloader stores the MAC the wrong-way around */
67 addr[4] = (lo & 0xff00) >> 8;
68 addr[3] = (lo & 0xff0000) >> 16;
69 addr[2] = (lo & 0xff000000) >> 24;
70 addr[1] = (hi & 0xff);
71 addr[0] = (hi & 0xff00) >> 8;
74 addr[0] = (lo & 0xff);
75 addr[1] = (lo & 0xff00) >> 8;
76 addr[2] = (lo & 0xff0000) >> 16;
77 addr[3] = (lo & 0xff000000) >> 24;
78 addr[4] = (hi & 0xff);
79 addr[5] = (hi & 0xff00) >> 8;
82 if (is_valid_ether_addr(addr)) {
83 memcpy(dev->dev_addr, &addr, 6);
90 * Set the ethernet MAC address in dev->dev_addr
92 static void __init get_mac_address(struct net_device *dev)
94 struct macb *lp = netdev_priv(dev);
96 /* Check Specific-Address 1 */
97 if (unpack_mac_address(dev, macb_readl(lp, SA1T), macb_readl(lp, SA1B)))
99 /* Check Specific-Address 2 */
100 if (unpack_mac_address(dev, macb_readl(lp, SA2T), macb_readl(lp, SA2B)))
102 /* Check Specific-Address 3 */
103 if (unpack_mac_address(dev, macb_readl(lp, SA3T), macb_readl(lp, SA3B)))
105 /* Check Specific-Address 4 */
106 if (unpack_mac_address(dev, macb_readl(lp, SA4T), macb_readl(lp, SA4B)))
109 printk(KERN_ERR "at91_ether: Your bootloader did not configure a MAC address.\n");
113 * Program the hardware MAC address from dev->dev_addr.
115 static void update_mac_address(struct net_device *dev)
117 struct macb *lp = netdev_priv(dev);
119 macb_writel(lp, SA1B, (dev->dev_addr[3] << 24) | (dev->dev_addr[2] << 16)
120 | (dev->dev_addr[1] << 8) | (dev->dev_addr[0]));
121 macb_writel(lp, SA1T, (dev->dev_addr[5] << 8) | (dev->dev_addr[4]));
123 macb_writel(lp, SA2B, 0);
124 macb_writel(lp, SA2T, 0);
128 * Store the new hardware address in dev->dev_addr, and update the MAC.
130 static int set_mac_address(struct net_device *dev, void* addr)
132 struct sockaddr *address = addr;
134 if (!is_valid_ether_addr(address->sa_data))
135 return -EADDRNOTAVAIL;
137 memcpy(dev->dev_addr, address->sa_data, dev->addr_len);
138 update_mac_address(dev);
140 printk("%s: Setting MAC address to %pM\n", dev->name,
146 /* ................................ MAC ................................ */
149 * Initialize and start the Receiver and Transmit subsystems
151 static int at91ether_start(struct net_device *dev)
153 struct macb *lp = netdev_priv(dev);
158 lp->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
159 MAX_RX_DESCR * sizeof(struct dma_desc),
160 &lp->rx_ring_dma, GFP_KERNEL);
162 netdev_err(lp->dev, "unable to alloc rx ring DMA buffer\n");
166 lp->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
167 MAX_RX_DESCR * MAX_RBUFF_SZ,
168 &lp->rx_buffers_dma, GFP_KERNEL);
169 if (!lp->rx_buffers) {
170 netdev_err(lp->dev, "unable to alloc rx data DMA buffer\n");
172 dma_free_coherent(&lp->pdev->dev,
173 MAX_RX_DESCR * sizeof(struct dma_desc),
174 lp->rx_ring, lp->rx_ring_dma);
179 addr = lp->rx_buffers_dma;
180 for (i = 0; i < MAX_RX_DESCR; i++) {
181 lp->rx_ring[i].addr = addr;
182 lp->rx_ring[i].ctrl = 0;
183 addr += MAX_RBUFF_SZ;
186 /* Set the Wrap bit on the last descriptor */
187 lp->rx_ring[MAX_RX_DESCR - 1].addr |= MACB_BIT(RX_WRAP);
189 /* Reset buffer index */
192 /* Program address of descriptor list in Rx Buffer Queue register */
193 macb_writel(lp, RBQP, lp->rx_ring_dma);
195 /* Enable Receive and Transmit */
196 ctl = macb_readl(lp, NCR);
197 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
203 * Open the ethernet interface
205 static int at91ether_open(struct net_device *dev)
207 struct macb *lp = netdev_priv(dev);
211 if (!is_valid_ether_addr(dev->dev_addr))
212 return -EADDRNOTAVAIL;
214 /* Clear internal statistics */
215 ctl = macb_readl(lp, NCR);
216 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
218 /* Update the MAC address (incase user has changed it) */
219 update_mac_address(dev);
221 ret = at91ether_start(dev);
225 /* Enable MAC interrupts */
226 macb_writel(lp, IER, MACB_BIT(RCOMP) | MACB_BIT(RXUBR)
227 | MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE) | MACB_BIT(TCOMP)
228 | MACB_BIT(ISR_ROVR) | MACB_BIT(HRESP));
230 /* schedule a link state check */
231 phy_start(lp->phy_dev);
233 netif_start_queue(dev);
239 * Close the interface
241 static int at91ether_close(struct net_device *dev)
243 struct macb *lp = netdev_priv(dev);
246 /* Disable Receiver and Transmitter */
247 ctl = macb_readl(lp, NCR);
248 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
250 /* Disable MAC interrupts */
251 macb_writel(lp, IDR, MACB_BIT(RCOMP) | MACB_BIT(RXUBR)
252 | MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)
253 | MACB_BIT(TCOMP) | MACB_BIT(ISR_ROVR)
256 netif_stop_queue(dev);
258 dma_free_coherent(&lp->pdev->dev,
259 MAX_RX_DESCR * sizeof(struct dma_desc),
260 lp->rx_ring, lp->rx_ring_dma);
263 dma_free_coherent(&lp->pdev->dev,
264 MAX_RX_DESCR * MAX_RBUFF_SZ,
265 lp->rx_buffers, lp->rx_buffers_dma);
266 lp->rx_buffers = NULL;
274 static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
276 struct macb *lp = netdev_priv(dev);
278 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
279 netif_stop_queue(dev);
281 /* Store packet information (to free when Tx completed) */
283 lp->skb_length = skb->len;
284 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len, DMA_TO_DEVICE);
285 dev->stats.tx_bytes += skb->len;
287 /* Set address of the data in the Transmit Address register */
288 macb_writel(lp, TAR, lp->skb_physaddr);
289 /* Set length of the packet in the Transmit Control register */
290 macb_writel(lp, TCR, skb->len);
293 printk(KERN_ERR "at91_ether.c: at91ether_start_xmit() called, but device is busy!\n");
294 return NETDEV_TX_BUSY; /* if we return anything but zero, dev.c:1055 calls kfree_skb(skb)
295 on this skb, he also reports -ENETDOWN and printk's, so either
296 we free and return(0) or don't free and return 1 */
303 * Update the current statistics from the internal statistics registers.
305 static struct net_device_stats *at91ether_stats(struct net_device *dev)
307 struct macb *lp = netdev_priv(dev);
308 int ale, lenerr, seqe, lcol, ecol;
310 if (netif_running(dev)) {
311 dev->stats.rx_packets += macb_readl(lp, FRO); /* Good frames received */
312 ale = macb_readl(lp, ALE);
313 dev->stats.rx_frame_errors += ale; /* Alignment errors */
314 lenerr = macb_readl(lp, ELE) + macb_readl(lp, USF);
315 dev->stats.rx_length_errors += lenerr; /* Excessive Length or Undersize Frame error */
316 seqe = macb_readl(lp, FCSE);
317 dev->stats.rx_crc_errors += seqe; /* CRC error */
318 dev->stats.rx_fifo_errors += macb_readl(lp, RRE);/* Receive buffer not available */
319 dev->stats.rx_errors += (ale + lenerr + seqe
320 + macb_readl(lp, RSE) + macb_readl(lp, RJA));
322 dev->stats.tx_packets += macb_readl(lp, FTO); /* Frames successfully transmitted */
323 dev->stats.tx_fifo_errors += macb_readl(lp, TUND); /* Transmit FIFO underruns */
324 dev->stats.tx_carrier_errors += macb_readl(lp, CSE); /* Carrier Sense errors */
325 dev->stats.tx_heartbeat_errors += macb_readl(lp, STE);/* Heartbeat error */
327 lcol = macb_readl(lp, LCOL);
328 ecol = macb_readl(lp, EXCOL);
329 dev->stats.tx_window_errors += lcol; /* Late collisions */
330 dev->stats.tx_aborted_errors += ecol; /* 16 collisions */
332 dev->stats.collisions += (macb_readl(lp, SCF) + macb_readl(lp, MCF) + lcol + ecol);
338 * Extract received frame from buffer descriptors and sent to upper layers.
339 * (Called from interrupt context)
341 static void at91ether_rx(struct net_device *dev)
343 struct macb *lp = netdev_priv(dev);
344 unsigned char *p_recv;
348 while (lp->rx_ring[lp->rx_tail].addr & MACB_BIT(RX_USED)) {
349 p_recv = lp->rx_buffers + lp->rx_tail * MAX_RBUFF_SZ;
350 pktlen = MACB_BF(RX_FRMLEN, lp->rx_ring[lp->rx_tail].ctrl);
351 skb = netdev_alloc_skb(dev, pktlen + 2);
354 memcpy(skb_put(skb, pktlen), p_recv, pktlen);
356 skb->protocol = eth_type_trans(skb, dev);
357 dev->stats.rx_bytes += pktlen;
360 dev->stats.rx_dropped += 1;
361 netdev_notice(dev, "Memory squeeze, dropping packet.\n");
364 if (lp->rx_ring[lp->rx_tail].ctrl & MACB_BIT(RX_MHASH_MATCH))
365 dev->stats.multicast++;
367 /* reset ownership bit */
368 lp->rx_ring[lp->rx_tail].addr &= ~MACB_BIT(RX_USED);
370 /* wrap after last buffer */
371 if (lp->rx_tail == MAX_RX_DESCR - 1)
379 * MAC interrupt handler
381 static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
383 struct net_device *dev = (struct net_device *) dev_id;
384 struct macb *lp = netdev_priv(dev);
385 unsigned long intstatus, ctl;
387 /* MAC Interrupt Status register indicates what interrupts are pending.
388 It is automatically cleared once read. */
389 intstatus = macb_readl(lp, ISR);
391 if (intstatus & MACB_BIT(RCOMP)) /* Receive complete */
394 if (intstatus & MACB_BIT(TCOMP)) { /* Transmit complete */
395 /* The TCOM bit is set even if the transmission failed. */
396 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
397 dev->stats.tx_errors += 1;
400 dev_kfree_skb_irq(lp->skb);
402 dma_unmap_single(NULL, lp->skb_physaddr, lp->skb_length, DMA_TO_DEVICE);
404 netif_wake_queue(dev);
407 /* Work-around for Errata #11 */
408 if (intstatus & MACB_BIT(RXUBR)) {
409 ctl = macb_readl(lp, NCR);
410 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
411 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
414 if (intstatus & MACB_BIT(ISR_ROVR))
415 printk("%s: ROVR error\n", dev->name);
420 #ifdef CONFIG_NET_POLL_CONTROLLER
421 static void at91ether_poll_controller(struct net_device *dev)
425 local_irq_save(flags);
426 at91ether_interrupt(dev->irq, dev);
427 local_irq_restore(flags);
431 static const struct net_device_ops at91ether_netdev_ops = {
432 .ndo_open = at91ether_open,
433 .ndo_stop = at91ether_close,
434 .ndo_start_xmit = at91ether_start_xmit,
435 .ndo_get_stats = at91ether_stats,
436 .ndo_set_rx_mode = macb_set_rx_mode,
437 .ndo_set_mac_address = set_mac_address,
438 .ndo_do_ioctl = macb_ioctl,
439 .ndo_validate_addr = eth_validate_addr,
440 .ndo_change_mtu = eth_change_mtu,
441 #ifdef CONFIG_NET_POLL_CONTROLLER
442 .ndo_poll_controller = at91ether_poll_controller,
447 * Detect MAC & PHY and perform ethernet interface initialization
449 static int __init at91ether_probe(struct platform_device *pdev)
451 struct macb_platform_data *board_data = pdev->dev.platform_data;
452 struct resource *regs;
453 struct net_device *dev;
454 struct phy_device *phydev;
458 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
462 dev = alloc_etherdev(sizeof(struct macb));
466 lp = netdev_priv(dev);
469 lp->board_data = *board_data;
470 spin_lock_init(&lp->lock);
472 dev->base_addr = regs->start; /* physical base address */
473 lp->regs = devm_ioremap(&pdev->dev, regs->start, resource_size(regs));
480 lp->pclk = devm_clk_get(&pdev->dev, "ether_clk");
481 if (IS_ERR(lp->pclk)) {
482 res = PTR_ERR(lp->pclk);
485 clk_enable(lp->pclk);
487 /* Install the interrupt handler */
488 dev->irq = platform_get_irq(pdev, 0);
489 res = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt, 0, dev->name, dev);
491 goto err_disable_clock;
494 dev->netdev_ops = &at91ether_netdev_ops;
495 dev->ethtool_ops = &macb_ethtool_ops;
496 platform_set_drvdata(pdev, dev);
497 SET_NETDEV_DEV(dev, &pdev->dev);
499 get_mac_address(dev); /* Get ethernet address and store it in dev->dev_addr */
500 update_mac_address(dev); /* Program ethernet address into MAC */
502 macb_writel(lp, NCR, 0);
504 if (board_data->is_rmii) {
505 macb_writel(lp, NCFGR, MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG) | MACB_BIT(RM9200_RMII));
506 lp->phy_interface = PHY_INTERFACE_MODE_RMII;
508 macb_writel(lp, NCFGR, MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG));
509 lp->phy_interface = PHY_INTERFACE_MODE_MII;
512 /* Register the network interface */
513 res = register_netdev(dev);
515 goto err_disable_clock;
517 if (macb_mii_init(lp) != 0)
518 goto err_out_unregister_netdev;
520 netif_carrier_off(dev); /* will be enabled in open() */
522 phydev = lp->phy_dev;
523 netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
524 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
526 /* Display ethernet banner */
527 printk(KERN_INFO "%s: AT91 ethernet at 0x%08x int=%d %s%s (%pM)\n",
528 dev->name, (uint) dev->base_addr, dev->irq,
529 macb_readl(lp, NCFGR) & MACB_BIT(SPD) ? "100-" : "10-",
530 macb_readl(lp, NCFGR) & MACB_BIT(FD) ? "FullDuplex" : "HalfDuplex",
535 err_out_unregister_netdev:
536 unregister_netdev(dev);
538 clk_disable(lp->pclk);
544 static int __devexit at91ether_remove(struct platform_device *pdev)
546 struct net_device *dev = platform_get_drvdata(pdev);
547 struct macb *lp = netdev_priv(dev);
550 phy_disconnect(lp->phy_dev);
552 mdiobus_unregister(lp->mii_bus);
553 kfree(lp->mii_bus->irq);
554 mdiobus_free(lp->mii_bus);
555 unregister_netdev(dev);
556 clk_disable(lp->pclk);
558 platform_set_drvdata(pdev, NULL);
565 static int at91ether_suspend(struct platform_device *pdev, pm_message_t mesg)
567 struct net_device *net_dev = platform_get_drvdata(pdev);
568 struct macb *lp = netdev_priv(net_dev);
570 if (netif_running(net_dev)) {
571 netif_stop_queue(net_dev);
572 netif_device_detach(net_dev);
574 clk_disable(lp->pclk);
579 static int at91ether_resume(struct platform_device *pdev)
581 struct net_device *net_dev = platform_get_drvdata(pdev);
582 struct macb *lp = netdev_priv(net_dev);
584 if (netif_running(net_dev)) {
585 clk_enable(lp->pclk);
587 netif_device_attach(net_dev);
588 netif_start_queue(net_dev);
594 #define at91ether_suspend NULL
595 #define at91ether_resume NULL
598 static struct platform_driver at91ether_driver = {
599 .remove = __devexit_p(at91ether_remove),
600 .suspend = at91ether_suspend,
601 .resume = at91ether_resume,
604 .owner = THIS_MODULE,
608 static int __init at91ether_init(void)
610 return platform_driver_probe(&at91ether_driver, at91ether_probe);
613 static void __exit at91ether_exit(void)
615 platform_driver_unregister(&at91ether_driver);
618 module_init(at91ether_init)
619 module_exit(at91ether_exit)
621 MODULE_LICENSE("GPL");
622 MODULE_DESCRIPTION("AT91RM9200 EMAC Ethernet driver");
623 MODULE_AUTHOR("Andrew Victor");
624 MODULE_ALIAS("platform:" DRV_NAME);