3 * Alchemy Au1x00 ethernet driver
5 * Copyright 2001-2003, 2006 MontaVista Software Inc.
6 * Copyright 2002 TimeSys Corp.
7 * Added ethtool/mii-tool support,
8 * Copyright 2004 Matt Porter <mporter@kernel.crashing.org>
9 * Update: 2004 Bjoern Riemer, riemer@fokus.fraunhofer.de
10 * or riemer@riemer-nt.de: fixed the link beat detection with
11 * ioctls (SIOCGMIIPHY)
12 * Copyright 2006 Herbert Valerio Riedel <hvr@gnu.org>
13 * converted to use linux-2.6.x's PHY framework
15 * Author: MontaVista Software, Inc.
16 * ppopov@mvista.com or source@mvista.com
18 * ########################################################################
20 * This program is free software; you can distribute it and/or modify it
21 * under the terms of the GNU General Public License (Version 2) as
22 * published by the Free Software Foundation.
24 * This program is distributed in the hope it will be useful, but WITHOUT
25 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
26 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29 * You should have received a copy of the GNU General Public License along
30 * with this program; if not, write to the Free Software Foundation, Inc.,
31 * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
33 * ########################################################################
37 #include <linux/capability.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/module.h>
40 #include <linux/kernel.h>
41 #include <linux/string.h>
42 #include <linux/timer.h>
43 #include <linux/errno.h>
45 #include <linux/ioport.h>
46 #include <linux/bitops.h>
47 #include <linux/slab.h>
48 #include <linux/interrupt.h>
49 #include <linux/init.h>
50 #include <linux/netdevice.h>
51 #include <linux/etherdevice.h>
52 #include <linux/ethtool.h>
53 #include <linux/mii.h>
54 #include <linux/skbuff.h>
55 #include <linux/delay.h>
56 #include <linux/crc32.h>
57 #include <linux/phy.h>
58 #include <linux/platform_device.h>
61 #include <asm/mipsregs.h>
64 #include <asm/processor.h>
67 #include <au1xxx_eth.h>
70 #include "au1000_eth.h"
72 #ifdef AU1000_ETH_DEBUG
73 static int au1000_debug = 5;
75 static int au1000_debug = 3;
78 #define AU1000_DEF_MSG_ENABLE (NETIF_MSG_DRV | \
82 #define DRV_NAME "au1000_eth"
83 #define DRV_VERSION "1.6"
84 #define DRV_AUTHOR "Pete Popov <ppopov@embeddedalley.com>"
85 #define DRV_DESC "Au1xxx on-chip Ethernet driver"
87 MODULE_AUTHOR(DRV_AUTHOR);
88 MODULE_DESCRIPTION(DRV_DESC);
89 MODULE_LICENSE("GPL");
90 MODULE_VERSION(DRV_VERSION);
95 * The Au1000 MACs use a simple rx and tx descriptor ring scheme.
96 * There are four receive and four transmit descriptors. These
97 * descriptors are not in memory; rather, they are just a set of
100 * Since the Au1000 has a coherent data cache, the receive and
101 * transmit buffers are allocated from the KSEG0 segment. The
102 * hardware registers, however, are still mapped at KSEG1 to
103 * make sure there's no out-of-order writes, and that all writes
104 * complete immediately.
107 /* These addresses are only used if yamon doesn't tell us what
108 * the mac address is, and the mac address is not passed on the
111 static unsigned char au1000_mac_addr[6] __devinitdata = {
112 0x00, 0x50, 0xc2, 0x0c, 0x30, 0x00
115 struct au1000_private *au_macs[NUM_ETH_INTERFACES];
118 * board-specific configurations
120 * PHY detection algorithm
122 * If phy_static_config is undefined, the PHY setup is
125 * mii_probe() first searches the current MAC's MII bus for a PHY,
126 * selecting the first (or last, if phy_search_highest_addr is
127 * defined) PHY address not already claimed by another netdev.
129 * If nothing was found that way when searching for the 2nd ethernet
130 * controller's PHY and phy1_search_mac0 is defined, then
131 * the first MII bus is searched as well for an unclaimed PHY; this is
132 * needed in case of a dual-PHY accessible only through the MAC0's MII
135 * Finally, if no PHY is found, then the corresponding ethernet
136 * controller is not registered to the network subsystem.
139 /* autodetection defaults: phy1_search_mac0 */
143 * most boards PHY setup should be detectable properly with the
144 * autodetection algorithm in mii_probe(), but in some cases (e.g. if
145 * you have a switch attached, or want to use the PHY's interrupt
146 * notification capabilities) you can provide a static PHY
149 * IRQs may only be set, if a PHY address was configured
150 * If a PHY address is given, also a bus id is required to be set
152 * ps: make sure the used irqs are configured properly in the board
156 static void au1000_enable_mac(struct net_device *dev, int force_reset)
159 struct au1000_private *aup = netdev_priv(dev);
161 spin_lock_irqsave(&aup->lock, flags);
163 if(force_reset || (!aup->mac_enabled)) {
164 *aup->enable = MAC_EN_CLOCK_ENABLE;
166 *aup->enable = (MAC_EN_RESET0 | MAC_EN_RESET1 | MAC_EN_RESET2
167 | MAC_EN_CLOCK_ENABLE);
170 aup->mac_enabled = 1;
173 spin_unlock_irqrestore(&aup->lock, flags);
179 static int au1000_mdio_read(struct net_device *dev, int phy_addr, int reg)
181 struct au1000_private *aup = netdev_priv(dev);
182 volatile u32 *const mii_control_reg = &aup->mac->mii_control;
183 volatile u32 *const mii_data_reg = &aup->mac->mii_data;
187 while (*mii_control_reg & MAC_MII_BUSY) {
189 if (--timedout == 0) {
190 printk(KERN_ERR "%s: read_MII busy timeout!!\n",
196 mii_control = MAC_SET_MII_SELECT_REG(reg) |
197 MAC_SET_MII_SELECT_PHY(phy_addr) | MAC_MII_READ;
199 *mii_control_reg = mii_control;
202 while (*mii_control_reg & MAC_MII_BUSY) {
204 if (--timedout == 0) {
205 printk(KERN_ERR "%s: mdio_read busy timeout!!\n",
210 return (int)*mii_data_reg;
213 static void au1000_mdio_write(struct net_device *dev, int phy_addr,
216 struct au1000_private *aup = netdev_priv(dev);
217 volatile u32 *const mii_control_reg = &aup->mac->mii_control;
218 volatile u32 *const mii_data_reg = &aup->mac->mii_data;
222 while (*mii_control_reg & MAC_MII_BUSY) {
224 if (--timedout == 0) {
225 printk(KERN_ERR "%s: mdio_write busy timeout!!\n",
231 mii_control = MAC_SET_MII_SELECT_REG(reg) |
232 MAC_SET_MII_SELECT_PHY(phy_addr) | MAC_MII_WRITE;
234 *mii_data_reg = value;
235 *mii_control_reg = mii_control;
238 static int au1000_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
240 /* WARNING: bus->phy_map[phy_addr].attached_dev == dev does
241 * _NOT_ hold (e.g. when PHY is accessed through other MAC's MII bus) */
242 struct net_device *const dev = bus->priv;
244 au1000_enable_mac(dev, 0); /* make sure the MAC associated with this
245 * mii_bus is enabled */
246 return au1000_mdio_read(dev, phy_addr, regnum);
249 static int au1000_mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum,
252 struct net_device *const dev = bus->priv;
254 au1000_enable_mac(dev, 0); /* make sure the MAC associated with this
255 * mii_bus is enabled */
256 au1000_mdio_write(dev, phy_addr, regnum, value);
260 static int au1000_mdiobus_reset(struct mii_bus *bus)
262 struct net_device *const dev = bus->priv;
264 au1000_enable_mac(dev, 0); /* make sure the MAC associated with this
265 * mii_bus is enabled */
269 static void au1000_hard_stop(struct net_device *dev)
271 struct au1000_private *aup = netdev_priv(dev);
273 if (au1000_debug > 4)
274 printk(KERN_INFO "%s: hard stop\n", dev->name);
276 aup->mac->control &= ~(MAC_RX_ENABLE | MAC_TX_ENABLE);
280 static void au1000_enable_rx_tx(struct net_device *dev)
282 struct au1000_private *aup = netdev_priv(dev);
284 if (au1000_debug > 4)
285 printk(KERN_INFO "%s: enable_rx_tx\n", dev->name);
287 aup->mac->control |= (MAC_RX_ENABLE | MAC_TX_ENABLE);
292 au1000_adjust_link(struct net_device *dev)
294 struct au1000_private *aup = netdev_priv(dev);
295 struct phy_device *phydev = aup->phy_dev;
298 int status_change = 0;
300 BUG_ON(!aup->phy_dev);
302 spin_lock_irqsave(&aup->lock, flags);
304 if (phydev->link && (aup->old_speed != phydev->speed)) {
307 switch (phydev->speed) {
313 "%s: Speed (%d) is not 10/100 ???\n",
314 dev->name, phydev->speed);
318 aup->old_speed = phydev->speed;
323 if (phydev->link && (aup->old_duplex != phydev->duplex)) {
324 /* duplex mode changed */
326 /* switching duplex mode requires to disable rx and tx! */
327 au1000_hard_stop(dev);
329 if (DUPLEX_FULL == phydev->duplex)
330 aup->mac->control = ((aup->mac->control
332 & ~MAC_DISABLE_RX_OWN);
334 aup->mac->control = ((aup->mac->control
336 | MAC_DISABLE_RX_OWN);
339 au1000_enable_rx_tx(dev);
340 aup->old_duplex = phydev->duplex;
345 if (phydev->link != aup->old_link) {
346 /* link state changed */
351 aup->old_duplex = -1;
354 aup->old_link = phydev->link;
358 spin_unlock_irqrestore(&aup->lock, flags);
362 printk(KERN_INFO "%s: link up (%d/%s)\n",
363 dev->name, phydev->speed,
364 DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
366 printk(KERN_INFO "%s: link down\n", dev->name);
370 static int au1000_mii_probe (struct net_device *dev)
372 struct au1000_private *const aup = netdev_priv(dev);
373 struct phy_device *phydev = NULL;
375 if (aup->phy_static_config) {
376 BUG_ON(aup->mac_id < 0 || aup->mac_id > 1);
379 phydev = aup->mii_bus->phy_map[aup->phy_addr];
381 printk (KERN_INFO DRV_NAME ":%s: using PHY-less setup\n",
387 /* find the first (lowest address) PHY on the current MAC's MII bus */
388 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++)
389 if (aup->mii_bus->phy_map[phy_addr]) {
390 phydev = aup->mii_bus->phy_map[phy_addr];
391 if (!aup->phy_search_highest_addr)
392 break; /* break out with first one found */
395 if (aup->phy1_search_mac0) {
396 /* try harder to find a PHY */
397 if (!phydev && (aup->mac_id == 1)) {
398 /* no PHY found, maybe we have a dual PHY? */
399 printk (KERN_INFO DRV_NAME ": no PHY found on MAC1, "
400 "let's see if it's attached to MAC0...\n");
402 /* find the first (lowest address) non-attached PHY on
403 * the MAC0 MII bus */
404 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
405 struct phy_device *const tmp_phydev =
406 aup->mii_bus->phy_map[phy_addr];
408 if (aup->mac_id == 1)
412 continue; /* no PHY here... */
414 if (tmp_phydev->attached_dev)
415 continue; /* already claimed by MAC0 */
418 break; /* found it */
425 printk (KERN_ERR DRV_NAME ":%s: no PHY found\n", dev->name);
429 /* now we are supposed to have a proper phydev, to attach to... */
430 BUG_ON(phydev->attached_dev);
432 phydev = phy_connect(dev, dev_name(&phydev->dev), &au1000_adjust_link,
433 0, PHY_INTERFACE_MODE_MII);
435 if (IS_ERR(phydev)) {
436 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
437 return PTR_ERR(phydev);
440 /* mask with MAC supported features */
441 phydev->supported &= (SUPPORTED_10baseT_Half
442 | SUPPORTED_10baseT_Full
443 | SUPPORTED_100baseT_Half
444 | SUPPORTED_100baseT_Full
446 /* | SUPPORTED_Pause | SUPPORTED_Asym_Pause */
450 phydev->advertising = phydev->supported;
454 aup->old_duplex = -1;
455 aup->phy_dev = phydev;
457 printk(KERN_INFO "%s: attached PHY driver [%s] "
458 "(mii_bus:phy_addr=%s, irq=%d)\n", dev->name,
459 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
466 * Buffer allocation/deallocation routines. The buffer descriptor returned
467 * has the virtual and dma address of a buffer suitable for
468 * both, receive and transmit operations.
470 static db_dest_t *au1000_GetFreeDB(struct au1000_private *aup)
476 aup->pDBfree = pDB->pnext;
481 void au1000_ReleaseDB(struct au1000_private *aup, db_dest_t *pDB)
483 db_dest_t *pDBfree = aup->pDBfree;
485 pDBfree->pnext = pDB;
489 static void au1000_reset_mac_unlocked(struct net_device *dev)
491 struct au1000_private *const aup = netdev_priv(dev);
494 au1000_hard_stop(dev);
496 *aup->enable = MAC_EN_CLOCK_ENABLE;
502 for (i = 0; i < NUM_RX_DMA; i++) {
503 /* reset control bits */
504 aup->rx_dma_ring[i]->buff_stat &= ~0xf;
506 for (i = 0; i < NUM_TX_DMA; i++) {
507 /* reset control bits */
508 aup->tx_dma_ring[i]->buff_stat &= ~0xf;
511 aup->mac_enabled = 0;
515 static void au1000_reset_mac(struct net_device *dev)
517 struct au1000_private *const aup = netdev_priv(dev);
520 if (au1000_debug > 4)
521 printk(KERN_INFO "%s: reset mac, aup %x\n",
522 dev->name, (unsigned)aup);
524 spin_lock_irqsave(&aup->lock, flags);
526 au1000_reset_mac_unlocked (dev);
528 spin_unlock_irqrestore(&aup->lock, flags);
532 * Setup the receive and transmit "rings". These pointers are the addresses
533 * of the rx and tx MAC DMA registers so they are fixed by the hardware --
534 * these are not descriptors sitting in memory.
537 au1000_setup_hw_rings(struct au1000_private *aup, u32 rx_base, u32 tx_base)
541 for (i = 0; i < NUM_RX_DMA; i++) {
542 aup->rx_dma_ring[i] =
543 (volatile rx_dma_t *) (rx_base + sizeof(rx_dma_t)*i);
545 for (i = 0; i < NUM_TX_DMA; i++) {
546 aup->tx_dma_ring[i] =
547 (volatile tx_dma_t *) (tx_base + sizeof(tx_dma_t)*i);
555 static int au1000_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
557 struct au1000_private *aup = netdev_priv(dev);
560 return phy_ethtool_gset(aup->phy_dev, cmd);
565 static int au1000_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
567 struct au1000_private *aup = netdev_priv(dev);
569 if (!capable(CAP_NET_ADMIN))
573 return phy_ethtool_sset(aup->phy_dev, cmd);
579 au1000_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
581 struct au1000_private *aup = netdev_priv(dev);
583 strcpy(info->driver, DRV_NAME);
584 strcpy(info->version, DRV_VERSION);
585 info->fw_version[0] = '\0';
586 sprintf(info->bus_info, "%s %d", DRV_NAME, aup->mac_id);
587 info->regdump_len = 0;
590 static void au1000_set_msglevel(struct net_device *dev, u32 value)
592 struct au1000_private *aup = netdev_priv(dev);
593 aup->msg_enable = value;
596 static u32 au1000_get_msglevel(struct net_device *dev)
598 struct au1000_private *aup = netdev_priv(dev);
599 return aup->msg_enable;
602 static const struct ethtool_ops au1000_ethtool_ops = {
603 .get_settings = au1000_get_settings,
604 .set_settings = au1000_set_settings,
605 .get_drvinfo = au1000_get_drvinfo,
606 .get_link = ethtool_op_get_link,
607 .get_msglevel = au1000_get_msglevel,
608 .set_msglevel = au1000_set_msglevel,
613 * Initialize the interface.
615 * When the device powers up, the clocks are disabled and the
616 * mac is in reset state. When the interface is closed, we
617 * do the same -- reset the device and disable the clocks to
618 * conserve power. Thus, whenever au1000_init() is called,
619 * the device should already be in reset state.
621 static int au1000_init(struct net_device *dev)
623 struct au1000_private *aup = netdev_priv(dev);
628 if (au1000_debug > 4)
629 printk("%s: au1000_init\n", dev->name);
631 /* bring the device out of reset */
632 au1000_enable_mac(dev, 1);
634 spin_lock_irqsave(&aup->lock, flags);
636 aup->mac->control = 0;
637 aup->tx_head = (aup->tx_dma_ring[0]->buff_stat & 0xC) >> 2;
638 aup->tx_tail = aup->tx_head;
639 aup->rx_head = (aup->rx_dma_ring[0]->buff_stat & 0xC) >> 2;
641 aup->mac->mac_addr_high = dev->dev_addr[5]<<8 | dev->dev_addr[4];
642 aup->mac->mac_addr_low = dev->dev_addr[3]<<24 | dev->dev_addr[2]<<16 |
643 dev->dev_addr[1]<<8 | dev->dev_addr[0];
645 for (i = 0; i < NUM_RX_DMA; i++) {
646 aup->rx_dma_ring[i]->buff_stat |= RX_DMA_ENABLE;
650 control = MAC_RX_ENABLE | MAC_TX_ENABLE;
651 #ifndef CONFIG_CPU_LITTLE_ENDIAN
652 control |= MAC_BIG_ENDIAN;
655 if (aup->phy_dev->link && (DUPLEX_FULL == aup->phy_dev->duplex))
656 control |= MAC_FULL_DUPLEX;
658 control |= MAC_DISABLE_RX_OWN;
659 } else { /* PHY-less op, assume full-duplex */
660 control |= MAC_FULL_DUPLEX;
663 aup->mac->control = control;
664 aup->mac->vlan1_tag = 0x8100; /* activate vlan support */
667 spin_unlock_irqrestore(&aup->lock, flags);
671 static inline void au1000_update_rx_stats(struct net_device *dev, u32 status)
673 struct net_device_stats *ps = &dev->stats;
676 if (status & RX_MCAST_FRAME)
679 if (status & RX_ERROR) {
681 if (status & RX_MISSED_FRAME)
682 ps->rx_missed_errors++;
683 if (status & (RX_OVERLEN | RX_RUNT | RX_LEN_ERROR))
684 ps->rx_length_errors++;
685 if (status & RX_CRC_ERROR)
687 if (status & RX_COLL)
690 ps->rx_bytes += status & RX_FRAME_LEN_MASK;
695 * Au1000 receive routine.
697 static int au1000_rx(struct net_device *dev)
699 struct au1000_private *aup = netdev_priv(dev);
701 volatile rx_dma_t *prxd;
702 u32 buff_stat, status;
706 if (au1000_debug > 5)
707 printk("%s: au1000_rx head %d\n", dev->name, aup->rx_head);
709 prxd = aup->rx_dma_ring[aup->rx_head];
710 buff_stat = prxd->buff_stat;
711 while (buff_stat & RX_T_DONE) {
712 status = prxd->status;
713 pDB = aup->rx_db_inuse[aup->rx_head];
714 au1000_update_rx_stats(dev, status);
715 if (!(status & RX_ERROR)) {
718 frmlen = (status & RX_FRAME_LEN_MASK);
719 frmlen -= 4; /* Remove FCS */
720 skb = dev_alloc_skb(frmlen + 2);
723 "%s: Memory squeeze, dropping packet.\n",
725 dev->stats.rx_dropped++;
728 skb_reserve(skb, 2); /* 16 byte IP header align */
729 skb_copy_to_linear_data(skb,
730 (unsigned char *)pDB->vaddr, frmlen);
731 skb_put(skb, frmlen);
732 skb->protocol = eth_type_trans(skb, dev);
733 netif_rx(skb); /* pass the packet to upper layers */
735 if (au1000_debug > 4) {
736 if (status & RX_MISSED_FRAME)
738 if (status & RX_WDOG_TIMER)
740 if (status & RX_RUNT)
742 if (status & RX_OVERLEN)
743 printk("rx overlen\n");
744 if (status & RX_COLL)
746 if (status & RX_MII_ERROR)
747 printk("rx mii error\n");
748 if (status & RX_CRC_ERROR)
749 printk("rx crc error\n");
750 if (status & RX_LEN_ERROR)
751 printk("rx len error\n");
752 if (status & RX_U_CNTRL_FRAME)
753 printk("rx u control frame\n");
756 prxd->buff_stat = (u32)(pDB->dma_addr | RX_DMA_ENABLE);
757 aup->rx_head = (aup->rx_head + 1) & (NUM_RX_DMA - 1);
760 /* next descriptor */
761 prxd = aup->rx_dma_ring[aup->rx_head];
762 buff_stat = prxd->buff_stat;
767 static void au1000_update_tx_stats(struct net_device *dev, u32 status)
769 struct au1000_private *aup = netdev_priv(dev);
770 struct net_device_stats *ps = &dev->stats;
772 if (status & TX_FRAME_ABORTED) {
773 if (!aup->phy_dev || (DUPLEX_FULL == aup->phy_dev->duplex)) {
774 if (status & (TX_JAB_TIMEOUT | TX_UNDERRUN)) {
775 /* any other tx errors are only valid
776 * in half duplex mode */
778 ps->tx_aborted_errors++;
782 ps->tx_aborted_errors++;
783 if (status & (TX_NO_CARRIER | TX_LOSS_CARRIER))
784 ps->tx_carrier_errors++;
790 * Called from the interrupt service routine to acknowledge
791 * the TX DONE bits. This is a must if the irq is setup as
794 static void au1000_tx_ack(struct net_device *dev)
796 struct au1000_private *aup = netdev_priv(dev);
797 volatile tx_dma_t *ptxd;
799 ptxd = aup->tx_dma_ring[aup->tx_tail];
801 while (ptxd->buff_stat & TX_T_DONE) {
802 au1000_update_tx_stats(dev, ptxd->status);
803 ptxd->buff_stat &= ~TX_T_DONE;
807 aup->tx_tail = (aup->tx_tail + 1) & (NUM_TX_DMA - 1);
808 ptxd = aup->tx_dma_ring[aup->tx_tail];
812 netif_wake_queue(dev);
818 * Au1000 interrupt service routine.
820 static irqreturn_t au1000_interrupt(int irq, void *dev_id)
822 struct net_device *dev = dev_id;
824 /* Handle RX interrupts first to minimize chance of overrun */
828 return IRQ_RETVAL(1);
831 static int au1000_open(struct net_device *dev)
834 struct au1000_private *aup = netdev_priv(dev);
836 if (au1000_debug > 4)
837 printk("%s: open: dev=%p\n", dev->name, dev);
839 retval = request_irq(dev->irq, au1000_interrupt, 0,
842 printk(KERN_ERR "%s: unable to get IRQ %d\n",
843 dev->name, dev->irq);
847 retval = au1000_init(dev);
849 printk(KERN_ERR "%s: error in au1000_init\n", dev->name);
850 free_irq(dev->irq, dev);
855 /* cause the PHY state machine to schedule a link state check */
856 aup->phy_dev->state = PHY_CHANGELINK;
857 phy_start(aup->phy_dev);
860 netif_start_queue(dev);
862 if (au1000_debug > 4)
863 printk("%s: open: Initialization done.\n", dev->name);
868 static int au1000_close(struct net_device *dev)
871 struct au1000_private *const aup = netdev_priv(dev);
873 if (au1000_debug > 4)
874 printk("%s: close: dev=%p\n", dev->name, dev);
877 phy_stop(aup->phy_dev);
879 spin_lock_irqsave(&aup->lock, flags);
881 au1000_reset_mac_unlocked (dev);
883 /* stop the device */
884 netif_stop_queue(dev);
886 /* disable the interrupt */
887 free_irq(dev->irq, dev);
888 spin_unlock_irqrestore(&aup->lock, flags);
894 * Au1000 transmit routine.
896 static netdev_tx_t au1000_tx(struct sk_buff *skb, struct net_device *dev)
898 struct au1000_private *aup = netdev_priv(dev);
899 struct net_device_stats *ps = &dev->stats;
900 volatile tx_dma_t *ptxd;
905 if (au1000_debug > 5)
906 printk("%s: tx: aup %x len=%d, data=%p, head %d\n",
907 dev->name, (unsigned)aup, skb->len,
908 skb->data, aup->tx_head);
910 ptxd = aup->tx_dma_ring[aup->tx_head];
911 buff_stat = ptxd->buff_stat;
912 if (buff_stat & TX_DMA_ENABLE) {
913 /* We've wrapped around and the transmitter is still busy */
914 netif_stop_queue(dev);
916 return NETDEV_TX_BUSY;
917 } else if (buff_stat & TX_T_DONE) {
918 au1000_update_tx_stats(dev, ptxd->status);
924 netif_wake_queue(dev);
927 pDB = aup->tx_db_inuse[aup->tx_head];
928 skb_copy_from_linear_data(skb, (void *)pDB->vaddr, skb->len);
929 if (skb->len < ETH_ZLEN) {
930 for (i = skb->len; i < ETH_ZLEN; i++) {
931 ((char *)pDB->vaddr)[i] = 0;
933 ptxd->len = ETH_ZLEN;
935 ptxd->len = skb->len;
938 ps->tx_bytes += ptxd->len;
940 ptxd->buff_stat = pDB->dma_addr | TX_DMA_ENABLE;
943 aup->tx_head = (aup->tx_head + 1) & (NUM_TX_DMA - 1);
944 dev->trans_start = jiffies;
949 * The Tx ring has been full longer than the watchdog timeout
950 * value. The transmitter must be hung?
952 static void au1000_tx_timeout(struct net_device *dev)
954 printk(KERN_ERR "%s: au1000_tx_timeout: dev=%p\n", dev->name, dev);
955 au1000_reset_mac(dev);
957 dev->trans_start = jiffies;
958 netif_wake_queue(dev);
961 static void au1000_multicast_list(struct net_device *dev)
963 struct au1000_private *aup = netdev_priv(dev);
965 if (au1000_debug > 4)
966 printk("%s: au1000_multicast_list: flags=%x\n", dev->name, dev->flags);
968 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
969 aup->mac->control |= MAC_PROMISCUOUS;
970 } else if ((dev->flags & IFF_ALLMULTI) ||
971 netdev_mc_count(dev) > MULTICAST_FILTER_LIMIT) {
972 aup->mac->control |= MAC_PASS_ALL_MULTI;
973 aup->mac->control &= ~MAC_PROMISCUOUS;
974 printk(KERN_INFO "%s: Pass all multicast\n", dev->name);
976 struct netdev_hw_addr *ha;
977 u32 mc_filter[2]; /* Multicast hash filter */
979 mc_filter[1] = mc_filter[0] = 0;
980 netdev_for_each_mc_addr(ha, dev)
981 set_bit(ether_crc(ETH_ALEN, ha->addr)>>26,
983 aup->mac->multi_hash_high = mc_filter[1];
984 aup->mac->multi_hash_low = mc_filter[0];
985 aup->mac->control &= ~MAC_PROMISCUOUS;
986 aup->mac->control |= MAC_HASH_MODE;
990 static int au1000_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
992 struct au1000_private *aup = netdev_priv(dev);
994 if (!netif_running(dev))
998 return -EINVAL; /* PHY not controllable */
1000 return phy_mii_ioctl(aup->phy_dev, if_mii(rq), cmd);
1003 static const struct net_device_ops au1000_netdev_ops = {
1004 .ndo_open = au1000_open,
1005 .ndo_stop = au1000_close,
1006 .ndo_start_xmit = au1000_tx,
1007 .ndo_set_multicast_list = au1000_multicast_list,
1008 .ndo_do_ioctl = au1000_ioctl,
1009 .ndo_tx_timeout = au1000_tx_timeout,
1010 .ndo_set_mac_address = eth_mac_addr,
1011 .ndo_validate_addr = eth_validate_addr,
1012 .ndo_change_mtu = eth_change_mtu,
1015 static int __devinit au1000_probe(struct platform_device *pdev)
1017 static unsigned version_printed;
1018 struct au1000_private *aup = NULL;
1019 struct au1000_eth_platform_data *pd;
1020 struct net_device *dev = NULL;
1021 db_dest_t *pDB, *pDBfree;
1022 int irq, i, err = 0;
1023 struct resource *base, *macen;
1026 base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1028 printk(KERN_ERR DRV_NAME ": failed to retrieve base register\n");
1033 macen = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1035 printk(KERN_ERR DRV_NAME ": failed to retrieve MAC Enable register\n");
1040 irq = platform_get_irq(pdev, 0);
1042 printk(KERN_ERR DRV_NAME ": failed to retrieve IRQ\n");
1047 if (!request_mem_region(base->start, resource_size(base), pdev->name)) {
1048 printk(KERN_ERR DRV_NAME ": failed to request memory region for base registers\n");
1053 if (!request_mem_region(macen->start, resource_size(macen), pdev->name)) {
1054 printk(KERN_ERR DRV_NAME ": failed to request memory region for MAC enable register\n");
1059 dev = alloc_etherdev(sizeof(struct au1000_private));
1061 printk(KERN_ERR "%s: alloc_etherdev failed\n", DRV_NAME);
1066 SET_NETDEV_DEV(dev, &pdev->dev);
1067 platform_set_drvdata(pdev, dev);
1068 aup = netdev_priv(dev);
1070 spin_lock_init(&aup->lock);
1071 aup->msg_enable = (au1000_debug < 4 ? AU1000_DEF_MSG_ENABLE : au1000_debug);
1073 /* Allocate the data buffers */
1074 /* Snooping works fine with eth on all au1xxx */
1075 aup->vaddr = (u32)dma_alloc_noncoherent(NULL, MAX_BUF_SIZE *
1076 (NUM_TX_BUFFS + NUM_RX_BUFFS),
1079 printk(KERN_ERR DRV_NAME ": failed to allocate data buffers\n");
1084 /* aup->mac is the base address of the MAC's registers */
1085 aup->mac = (volatile mac_reg_t *)ioremap_nocache(base->start, resource_size(base));
1087 printk(KERN_ERR DRV_NAME ": failed to ioremap MAC registers\n");
1092 /* Setup some variables for quick register address access */
1093 aup->enable = (volatile u32 *)ioremap_nocache(macen->start, resource_size(macen));
1095 printk(KERN_ERR DRV_NAME ": failed to ioremap MAC enable register\n");
1099 aup->mac_id = pdev->id;
1101 if (pdev->id == 0) {
1102 if (prom_get_ethernet_addr(ethaddr) == 0)
1103 memcpy(au1000_mac_addr, ethaddr, sizeof(au1000_mac_addr));
1105 printk(KERN_INFO "%s: No MAC address found\n",
1107 /* Use the hard coded MAC addresses */
1110 au1000_setup_hw_rings(aup, MAC0_RX_DMA_ADDR, MAC0_TX_DMA_ADDR);
1111 } else if (pdev->id == 1)
1112 au1000_setup_hw_rings(aup, MAC1_RX_DMA_ADDR, MAC1_TX_DMA_ADDR);
1115 * Assign to the Ethernet ports two consecutive MAC addresses
1116 * to match those that are printed on their stickers
1118 memcpy(dev->dev_addr, au1000_mac_addr, sizeof(au1000_mac_addr));
1119 dev->dev_addr[5] += pdev->id;
1122 aup->mac_enabled = 0;
1124 pd = pdev->dev.platform_data;
1126 printk(KERN_INFO DRV_NAME ": no platform_data passed, PHY search on MAC0\n");
1127 aup->phy1_search_mac0 = 1;
1129 aup->phy_static_config = pd->phy_static_config;
1130 aup->phy_search_highest_addr = pd->phy_search_highest_addr;
1131 aup->phy1_search_mac0 = pd->phy1_search_mac0;
1132 aup->phy_addr = pd->phy_addr;
1133 aup->phy_busid = pd->phy_busid;
1134 aup->phy_irq = pd->phy_irq;
1137 if (aup->phy_busid && aup->phy_busid > 0) {
1138 printk(KERN_ERR DRV_NAME ": MAC0-associated PHY attached 2nd MACs MII"
1139 "bus not supported yet\n");
1141 goto err_mdiobus_alloc;
1144 aup->mii_bus = mdiobus_alloc();
1145 if (aup->mii_bus == NULL) {
1146 printk(KERN_ERR DRV_NAME ": failed to allocate mdiobus structure\n");
1148 goto err_mdiobus_alloc;
1151 aup->mii_bus->priv = dev;
1152 aup->mii_bus->read = au1000_mdiobus_read;
1153 aup->mii_bus->write = au1000_mdiobus_write;
1154 aup->mii_bus->reset = au1000_mdiobus_reset;
1155 aup->mii_bus->name = "au1000_eth_mii";
1156 snprintf(aup->mii_bus->id, MII_BUS_ID_SIZE, "%x", aup->mac_id);
1157 aup->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
1158 if (aup->mii_bus->irq == NULL)
1161 for (i = 0; i < PHY_MAX_ADDR; ++i)
1162 aup->mii_bus->irq[i] = PHY_POLL;
1163 /* if known, set corresponding PHY IRQs */
1164 if (aup->phy_static_config)
1165 if (aup->phy_irq && aup->phy_busid == aup->mac_id)
1166 aup->mii_bus->irq[aup->phy_addr] = aup->phy_irq;
1168 err = mdiobus_register(aup->mii_bus);
1170 printk(KERN_ERR DRV_NAME " failed to register MDIO bus\n");
1171 goto err_mdiobus_reg;
1174 if (au1000_mii_probe(dev) != 0)
1178 /* setup the data buffer descriptors and attach a buffer to each one */
1180 for (i = 0; i < (NUM_TX_BUFFS+NUM_RX_BUFFS); i++) {
1181 pDB->pnext = pDBfree;
1183 pDB->vaddr = (u32 *)((unsigned)aup->vaddr + MAX_BUF_SIZE*i);
1184 pDB->dma_addr = (dma_addr_t)virt_to_bus(pDB->vaddr);
1187 aup->pDBfree = pDBfree;
1189 for (i = 0; i < NUM_RX_DMA; i++) {
1190 pDB = au1000_GetFreeDB(aup);
1194 aup->rx_dma_ring[i]->buff_stat = (unsigned)pDB->dma_addr;
1195 aup->rx_db_inuse[i] = pDB;
1197 for (i = 0; i < NUM_TX_DMA; i++) {
1198 pDB = au1000_GetFreeDB(aup);
1202 aup->tx_dma_ring[i]->buff_stat = (unsigned)pDB->dma_addr;
1203 aup->tx_dma_ring[i]->len = 0;
1204 aup->tx_db_inuse[i] = pDB;
1207 dev->base_addr = base->start;
1209 dev->netdev_ops = &au1000_netdev_ops;
1210 SET_ETHTOOL_OPS(dev, &au1000_ethtool_ops);
1211 dev->watchdog_timeo = ETH_TX_TIMEOUT;
1214 * The boot code uses the ethernet controller, so reset it to start
1215 * fresh. au1000_init() expects that the device is in reset state.
1217 au1000_reset_mac(dev);
1219 err = register_netdev(dev);
1221 printk(KERN_ERR DRV_NAME "%s: Cannot register net device, aborting.\n",
1226 printk("%s: Au1xx0 Ethernet found at 0x%lx, irq %d\n",
1227 dev->name, (unsigned long)base->start, irq);
1228 if (version_printed++ == 0)
1229 printk("%s version %s %s\n", DRV_NAME, DRV_VERSION, DRV_AUTHOR);
1234 if (aup->mii_bus != NULL)
1235 mdiobus_unregister(aup->mii_bus);
1237 /* here we should have a valid dev plus aup-> register addresses
1238 * so we can reset the mac properly.*/
1239 au1000_reset_mac(dev);
1241 for (i = 0; i < NUM_RX_DMA; i++) {
1242 if (aup->rx_db_inuse[i])
1243 au1000_ReleaseDB(aup, aup->rx_db_inuse[i]);
1245 for (i = 0; i < NUM_TX_DMA; i++) {
1246 if (aup->tx_db_inuse[i])
1247 au1000_ReleaseDB(aup, aup->tx_db_inuse[i]);
1250 mdiobus_free(aup->mii_bus);
1252 iounmap(aup->enable);
1256 dma_free_noncoherent(NULL, MAX_BUF_SIZE * (NUM_TX_BUFFS + NUM_RX_BUFFS),
1257 (void *)aup->vaddr, aup->dma_addr);
1261 release_mem_region(macen->start, resource_size(macen));
1263 release_mem_region(base->start, resource_size(base));
1268 static int __devexit au1000_remove(struct platform_device *pdev)
1270 struct net_device *dev = platform_get_drvdata(pdev);
1271 struct au1000_private *aup = netdev_priv(dev);
1273 struct resource *base, *macen;
1275 platform_set_drvdata(pdev, NULL);
1277 unregister_netdev(dev);
1278 mdiobus_unregister(aup->mii_bus);
1279 mdiobus_free(aup->mii_bus);
1281 for (i = 0; i < NUM_RX_DMA; i++)
1282 if (aup->rx_db_inuse[i])
1283 au1000_ReleaseDB(aup, aup->rx_db_inuse[i]);
1285 for (i = 0; i < NUM_TX_DMA; i++)
1286 if (aup->tx_db_inuse[i])
1287 au1000_ReleaseDB(aup, aup->tx_db_inuse[i]);
1289 dma_free_noncoherent(NULL, MAX_BUF_SIZE *
1290 (NUM_TX_BUFFS + NUM_RX_BUFFS),
1291 (void *)aup->vaddr, aup->dma_addr);
1294 iounmap(aup->enable);
1296 base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1297 release_mem_region(base->start, resource_size(base));
1299 macen = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1300 release_mem_region(macen->start, resource_size(macen));
1307 static struct platform_driver au1000_eth_driver = {
1308 .probe = au1000_probe,
1309 .remove = __devexit_p(au1000_remove),
1311 .name = "au1000-eth",
1312 .owner = THIS_MODULE,
1315 MODULE_ALIAS("platform:au1000-eth");
1318 static int __init au1000_init_module(void)
1320 return platform_driver_register(&au1000_eth_driver);
1323 static void __exit au1000_exit_module(void)
1325 platform_driver_unregister(&au1000_eth_driver);
1328 module_init(au1000_init_module);
1329 module_exit(au1000_exit_module);