fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / net / arm / at91_ether.c
index 5503dc8..fada15d 100644 (file)
@@ -19,7 +19,6 @@
 
 #include <linux/module.h>
 #include <linux/init.h>
-#include <linux/config.h>
 #include <linux/mii.h>
 #include <linux/netdevice.h>
 #include <linux/etherdevice.h>
@@ -42,8 +41,7 @@
 #define DRV_NAME       "at91_ether"
 #define DRV_VERSION    "1.0"
 
-static struct net_device *at91_dev;
-static struct clk *ether_clk;
+#define LINK_POLL_INTERVAL     (HZ)
 
 /* ..................................................................... */
 
@@ -143,15 +141,16 @@ static void read_phy(unsigned char phy_addr, unsigned char address, unsigned int
  * MAC accordingly.
  * If no link or auto-negotiation is busy, then no changes are made.
  */
-static void update_linkspeed(struct net_device *dev)
+static void update_linkspeed(struct net_device *dev, int silent)
 {
-       struct at91_private *lp = (struct at91_private *) dev->priv;
+       struct at91_private *lp = netdev_priv(dev);
        unsigned int bmsr, bmcr, lpa, mac_cfg;
        unsigned int speed, duplex;
 
        if (!mii_link_ok(&lp->mii)) {           /* no link */
                netif_carrier_off(dev);
-               printk(KERN_INFO "%s: Link down.\n", dev->name);
+               if (!silent)
+                       printk(KERN_INFO "%s: Link down.\n", dev->name);
                return;
        }
 
@@ -186,17 +185,18 @@ static void update_linkspeed(struct net_device *dev)
        }
        at91_emac_write(AT91_EMAC_CFG, mac_cfg);
 
-       printk(KERN_INFO "%s: Link now %i-%s\n", dev->name, speed, (duplex == DUPLEX_FULL) ? "FullDuplex" : "HalfDuplex");
+       if (!silent)
+               printk(KERN_INFO "%s: Link now %i-%s\n", dev->name, speed, (duplex == DUPLEX_FULL) ? "FullDuplex" : "HalfDuplex");
        netif_carrier_on(dev);
 }
 
 /*
  * Handle interrupts from the PHY
  */
-static irqreturn_t at91ether_phy_interrupt(int irq, void *dev_id, struct pt_regs *regs)
+static irqreturn_t at91ether_phy_interrupt(int irq, void *dev_id)
 {
        struct net_device *dev = (struct net_device *) dev_id;
-       struct at91_private *lp = (struct at91_private *) dev->priv;
+       struct at91_private *lp = netdev_priv(dev);
        unsigned int phy;
 
        /*
@@ -226,7 +226,7 @@ static irqreturn_t at91ether_phy_interrupt(int irq, void *dev_id, struct pt_regs
                        goto done;
        }
 
-       update_linkspeed(dev);
+       update_linkspeed(dev, 0);
 
 done:
        disable_mdi();
@@ -239,18 +239,20 @@ done:
  */
 static void enable_phyirq(struct net_device *dev)
 {
-       struct at91_private *lp = (struct at91_private *) dev->priv;
+       struct at91_private *lp = netdev_priv(dev);
        unsigned int dsintr, irq_number;
        int status;
 
-       if (lp->phy_type == MII_RTL8201_ID)     /* RTL8201 does not have an interrupt */
-               return;
-       if (lp->phy_type == MII_DP83847_ID)     /* DP83847 does not have an interrupt */
-               return;
-       if (lp->phy_type == MII_AC101L_ID)      /* AC101L interrupt not supported yet */
+       irq_number = lp->board_data.phy_irq_pin;
+       if (!irq_number) {
+               /*
+                * PHY doesn't have an IRQ pin (RTL8201, DP83847, AC101L),
+                * or board does not have it connected.
+                */
+               mod_timer(&lp->check_timer, jiffies + LINK_POLL_INTERVAL);
                return;
+       }
 
-       irq_number = lp->board_data.phy_irq_pin;
        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);
@@ -288,16 +290,15 @@ static void enable_phyirq(struct net_device *dev)
  */
 static void disable_phyirq(struct net_device *dev)
 {
-       struct at91_private *lp = (struct at91_private *) dev->priv;
+       struct at91_private *lp = netdev_priv(dev);
        unsigned int dsintr;
        unsigned int irq_number;
 
-       if (lp->phy_type == MII_RTL8201_ID)     /* RTL8201 does not have an interrupt */
-               return;
-       if (lp->phy_type == MII_DP83847_ID)     /* DP83847 does not have an interrupt */
-               return;
-       if (lp->phy_type == MII_AC101L_ID)      /* AC101L interrupt not supported yet */
+       irq_number = lp->board_data.phy_irq_pin;
+       if (!irq_number) {
+               del_timer_sync(&lp->check_timer);
                return;
+       }
 
        spin_lock_irq(&lp->lock);
        enable_mdi();
@@ -326,7 +327,6 @@ static void disable_phyirq(struct net_device *dev)
        disable_mdi();
        spin_unlock_irq(&lp->lock);
 
-       irq_number = lp->board_data.phy_irq_pin;
        free_irq(irq_number, dev);                      /* Free interrupt handler */
 }
 
@@ -336,7 +336,7 @@ static void disable_phyirq(struct net_device *dev)
 #if 0
 static void reset_phy(struct net_device *dev)
 {
-       struct at91_private *lp = (struct at91_private *) dev->priv;
+       struct at91_private *lp = netdev_priv(dev);
        unsigned int bmcr;
 
        spin_lock_irq(&lp->lock);
@@ -355,6 +355,18 @@ static void reset_phy(struct net_device *dev)
 }
 #endif
 
+static void at91ether_check_link(unsigned long dev_id)
+{
+       struct net_device *dev = (struct net_device *) dev_id;
+       struct at91_private *lp = netdev_priv(dev);
+
+       enable_mdi();
+       update_linkspeed(dev, 1);
+       disable_mdi();
+
+       mod_timer(&lp->check_timer, jiffies + LINK_POLL_INTERVAL);
+}
+
 /* ......................... ADDRESS MANAGEMENT ........................ */
 
 /*
@@ -501,7 +513,7 @@ static int hash_get_index(__u8 *addr)
                hash_index |= (bitval << j);
        }
 
-        return hash_index;
+       return hash_index;
 }
 
 /*
@@ -557,10 +569,8 @@ static void at91ether_set_rx_mode(struct net_device *dev)
        at91_emac_write(AT91_EMAC_CFG, cfg);
 }
 
-
 /* ......................... ETHTOOL SUPPORT ........................... */
 
-
 static int mdio_read(struct net_device *dev, int phy_id, int location)
 {
        unsigned int value;
@@ -576,7 +586,7 @@ static void mdio_write(struct net_device *dev, int phy_id, int location, int val
 
 static int at91ether_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
 {
-       struct at91_private *lp = (struct at91_private *) dev->priv;
+       struct at91_private *lp = netdev_priv(dev);
        int ret;
 
        spin_lock_irq(&lp->lock);
@@ -597,7 +607,7 @@ static int at91ether_get_settings(struct net_device *dev, struct ethtool_cmd *cm
 
 static int at91ether_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
 {
-       struct at91_private *lp = (struct at91_private *) dev->priv;
+       struct at91_private *lp = netdev_priv(dev);
        int ret;
 
        spin_lock_irq(&lp->lock);
@@ -613,7 +623,7 @@ static int at91ether_set_settings(struct net_device *dev, struct ethtool_cmd *cm
 
 static int at91ether_nwayreset(struct net_device *dev)
 {
-       struct at91_private *lp = (struct at91_private *) dev->priv;
+       struct at91_private *lp = netdev_priv(dev);
        int ret;
 
        spin_lock_irq(&lp->lock);
@@ -634,7 +644,7 @@ static void at91ether_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo
        strlcpy(info->bus_info, dev->class_dev.dev->bus_id, sizeof(info->bus_info));
 }
 
-static struct ethtool_ops at91ether_ethtool_ops = {
+static const struct ethtool_ops at91ether_ethtool_ops = {
        .get_settings   = at91ether_get_settings,
        .set_settings   = at91ether_set_settings,
        .get_drvinfo    = at91ether_get_drvinfo,
@@ -642,6 +652,22 @@ static struct ethtool_ops at91ether_ethtool_ops = {
        .get_link       = ethtool_op_get_link,
 };
 
+static int at91ether_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
+{
+       struct at91_private *lp = netdev_priv(dev);
+       int res;
+
+       if (!netif_running(dev))
+               return -EINVAL;
+
+       spin_lock_irq(&lp->lock);
+       enable_mdi();
+       res = generic_mii_ioctl(&lp->mii, if_mii(rq), cmd, NULL);
+       disable_mdi();
+       spin_unlock_irq(&lp->lock);
+
+       return res;
+}
 
 /* ................................ MAC ................................ */
 
@@ -650,7 +676,7 @@ static struct ethtool_ops at91ether_ethtool_ops = {
  */
 static void at91ether_start(struct net_device *dev)
 {
-       struct at91_private *lp = (struct at91_private *) dev->priv;
+       struct at91_private *lp = netdev_priv(dev);
        struct recv_desc_bufs *dlist, *dlist_phys;
        int i;
        unsigned long ctl;
@@ -682,13 +708,13 @@ static void at91ether_start(struct net_device *dev)
  */
 static int at91ether_open(struct net_device *dev)
 {
-       struct at91_private *lp = (struct at91_private *) dev->priv;
+       struct at91_private *lp = netdev_priv(dev);
        unsigned long ctl;
 
-        if (!is_valid_ether_addr(dev->dev_addr))
-               return -EADDRNOTAVAIL;
+       if (!is_valid_ether_addr(dev->dev_addr))
+               return -EADDRNOTAVAIL;
 
-       clk_enable(ether_clk);                  /* Re-enable Peripheral clock */
+       clk_enable(lp->ether_clk);              /* Re-enable Peripheral clock */
 
        /* Clear internal statistics */
        ctl = at91_emac_read(AT91_EMAC_CTL);
@@ -708,7 +734,7 @@ static int at91ether_open(struct net_device *dev)
        /* Determine current link speed */
        spin_lock_irq(&lp->lock);
        enable_mdi();
-       update_linkspeed(dev);
+       update_linkspeed(dev, 0);
        disable_mdi();
        spin_unlock_irq(&lp->lock);
 
@@ -722,6 +748,7 @@ static int at91ether_open(struct net_device *dev)
  */
 static int at91ether_close(struct net_device *dev)
 {
+       struct at91_private *lp = netdev_priv(dev);
        unsigned long ctl;
 
        /* Disable Receiver and Transmitter */
@@ -738,7 +765,7 @@ static int at91ether_close(struct net_device *dev)
 
        netif_stop_queue(dev);
 
-       clk_disable(ether_clk);                 /* Disable Peripheral clock */
+       clk_disable(lp->ether_clk);             /* Disable Peripheral clock */
 
        return 0;
 }
@@ -748,7 +775,7 @@ static int at91ether_close(struct net_device *dev)
  */
 static int at91ether_tx(struct sk_buff *skb, struct net_device *dev)
 {
-       struct at91_private *lp = (struct at91_private *) dev->priv;
+       struct at91_private *lp = netdev_priv(dev);
 
        if (at91_emac_read(AT91_EMAC_TSR) & AT91_EMAC_TSR_BNQ) {
                netif_stop_queue(dev);
@@ -780,7 +807,7 @@ static int at91ether_tx(struct sk_buff *skb, struct net_device *dev)
  */
 static struct net_device_stats *at91ether_stats(struct net_device *dev)
 {
-       struct at91_private *lp = (struct at91_private *) dev->priv;
+       struct at91_private *lp = netdev_priv(dev);
        int ale, lenerr, seqe, lcol, ecol;
 
        if (netif_running(dev)) {
@@ -816,7 +843,7 @@ static struct net_device_stats *at91ether_stats(struct net_device *dev)
  */
 static void at91ether_rx(struct net_device *dev)
 {
-       struct at91_private *lp = (struct at91_private *) dev->priv;
+       struct at91_private *lp = netdev_priv(dev);
        struct recv_desc_bufs *dlist;
        unsigned char *p_recv;
        struct sk_buff *skb;
@@ -826,14 +853,13 @@ static void at91ether_rx(struct net_device *dev)
        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 */
-               skb = alloc_skb(pktlen + 2, GFP_ATOMIC);
+               skb = dev_alloc_skb(pktlen + 2);
                if (skb != NULL) {
                        skb_reserve(skb, 2);
                        memcpy(skb_put(skb, pktlen), p_recv, pktlen);
 
                        skb->dev = dev;
                        skb->protocol = eth_type_trans(skb, dev);
-                       skb->len = pktlen;
                        dev->last_rx = jiffies;
                        lp->stats.rx_bytes += pktlen;
                        netif_rx(skb);
@@ -857,10 +883,10 @@ static void at91ether_rx(struct net_device *dev)
 /*
  * MAC interrupt handler
  */
-static irqreturn_t at91ether_interrupt(int irq, void *dev_id, struct pt_regs *regs)
+static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
 {
        struct net_device *dev = (struct net_device *) dev_id;
-       struct at91_private *lp = (struct at91_private *) dev->priv;
+       struct at91_private *lp = netdev_priv(dev);
        unsigned long intstatus, ctl;
 
        /* MAC Interrupt Status register indicates what interrupts are pending.
@@ -870,7 +896,7 @@ static irqreturn_t at91ether_interrupt(int irq, void *dev_id, struct pt_regs *re
        if (intstatus & AT91_EMAC_RCOM)         /* Receive complete */
                at91ether_rx(dev);
 
-       if (intstatus & AT91_EMAC_TCOM) {               /* Transmit complete */
+       if (intstatus & AT91_EMAC_TCOM) {       /* Transmit complete */
                /* The TCOM bit is set even if the transmission failed. */
                if (intstatus & (AT91_EMAC_TUND | AT91_EMAC_RTRY))
                        lp->stats.tx_errors += 1;
@@ -896,10 +922,22 @@ static irqreturn_t at91ether_interrupt(int irq, void *dev_id, struct pt_regs *re
        return IRQ_HANDLED;
 }
 
+#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
+
 /*
  * Initialize the ethernet interface
  */
-static int __init at91ether_setup(unsigned long phy_type, unsigned short phy_address, struct platform_device *pdev)
+static int __init at91ether_setup(unsigned long phy_type, unsigned short phy_address,
+                       struct platform_device *pdev, struct clk *ether_clk)
 {
        struct at91_eth_data *board_data = pdev->dev.platform_data;
        struct net_device *dev;
@@ -907,15 +945,12 @@ static int __init at91ether_setup(unsigned long phy_type, unsigned short phy_add
        unsigned int val;
        int res;
 
-       if (at91_dev)                   /* already initialized */
-               return 0;
-
        dev = alloc_etherdev(sizeof(struct at91_private));
        if (!dev)
                return -ENOMEM;
 
        dev->base_addr = AT91_VA_BASE_EMAC;
-       dev->irq = AT91_ID_EMAC;
+       dev->irq = AT91RM9200_ID_EMAC;
        SET_MODULE_OWNER(dev);
 
        /* Install the interrupt handler */
@@ -925,7 +960,7 @@ static int __init at91ether_setup(unsigned long phy_type, unsigned short phy_add
        }
 
        /* Allocate memory for DMA Receive descriptors */
-       lp = (struct at91_private *)dev->priv;
+       lp = netdev_priv(dev);
        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) {
                free_irq(dev->irq, dev);
@@ -933,6 +968,7 @@ static int __init at91ether_setup(unsigned long phy_type, unsigned short phy_add
                return -ENOMEM;
        }
        lp->board_data = *board_data;
+       lp->ether_clk = ether_clk;
        platform_set_drvdata(pdev, dev);
 
        spin_lock_init(&lp->lock);
@@ -945,6 +981,10 @@ static int __init at91ether_setup(unsigned long phy_type, unsigned short phy_add
        dev->set_multicast_list = at91ether_set_rx_mode;
        dev->set_mac_address = set_mac_address;
        dev->ethtool_ops = &at91ether_ethtool_ops;
+       dev->do_ioctl = at91ether_ioctl;
+#ifdef CONFIG_NET_POLL_CONTROLLER
+       dev->poll_controller = at91ether_poll_controller;
+#endif
 
        SET_NETDEV_DEV(dev, &pdev->dev);
 
@@ -975,6 +1015,9 @@ static int __init at91ether_setup(unsigned long phy_type, unsigned short phy_add
        lp->mii.dev = dev;              /* Support for ethtool */
        lp->mii.mdio_read = mdio_read;
        lp->mii.mdio_write = mdio_write;
+       lp->mii.phy_id = phy_address;
+       lp->mii.phy_id_mask = 0x1f;
+       lp->mii.reg_num_mask = 0x1f;
 
        lp->phy_type = phy_type;        /* Type of PHY connected */
        lp->phy_address = phy_address;  /* MDI address of PHY */
@@ -987,16 +1030,22 @@ static int __init at91ether_setup(unsigned long phy_type, unsigned short phy_add
                dma_free_coherent(NULL, sizeof(struct recv_desc_bufs), lp->dlist, (dma_addr_t)lp->dlist_phys);
                return res;
        }
-       at91_dev = dev;
 
        /* Determine current link speed */
        spin_lock_irq(&lp->lock);
        enable_mdi();
-       update_linkspeed(dev);
+       update_linkspeed(dev, 0);
        disable_mdi();
        spin_unlock_irq(&lp->lock);
        netif_carrier_off(dev);         /* will be enabled in open() */
 
+       /* If board has no PHY IRQ, use a timer to poll the PHY */
+       if (!lp->board_data.phy_irq_pin) {
+               init_timer(&lp->check_timer);
+               lp->check_timer.data = (unsigned long)dev;
+               lp->check_timer.function = at91ether_check_link;
+       }
+
        /* Display ethernet banner */
        printk(KERN_INFO "%s: AT91 ethernet at 0x%08x int=%d %s%s (%02x:%02x:%02x:%02x:%02x:%02x)\n",
                dev->name, (uint) dev->base_addr, dev->irq,
@@ -1005,7 +1054,7 @@ static int __init at91ether_setup(unsigned long phy_type, unsigned short phy_add
                dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
                dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
        if ((phy_type == MII_DM9161_ID) || (lp->phy_type == MII_DM9161A_ID))
-               printk(KERN_INFO "%s: Davicom 9196 PHY %s\n", dev->name, (lp->phy_media == PORT_FIBRE) ? "(Fiber)" : "(Copper)");
+               printk(KERN_INFO "%s: Davicom 9161 PHY %s\n", dev->name, (lp->phy_media == PORT_FIBRE) ? "(Fiber)" : "(Copper)");
        else if (phy_type == MII_LXT971A_ID)
                printk(KERN_INFO "%s: Intel LXT971A PHY\n", dev->name);
        else if (phy_type == MII_RTL8201_ID)
@@ -1031,9 +1080,10 @@ static int __init at91ether_probe(struct platform_device *pdev)
        int detected = -1;
        unsigned long phy_id;
        unsigned short phy_address = 0;
+       struct clk *ether_clk;
 
        ether_clk = clk_get(&pdev->dev, "ether_clk");
-       if (!ether_clk) {
+       if (IS_ERR(ether_clk)) {
                printk(KERN_ERR "at91_ether: no clock defined\n");
                return -ENODEV;
        }
@@ -1056,7 +1106,7 @@ static int __init at91ether_probe(struct platform_device *pdev)
                        case MII_DP83847_ID:            /* National Semiconductor DP83847:  */
                        case MII_AC101L_ID:             /* Altima AC101L: PHY_ID1 = 0x22, PHY_ID2 = 0x5520 */
                        case MII_KS8721_ID:             /* Micrel KS8721: PHY_ID1 = 0x22, PHY_ID2 = 0x1610 */
-                               detected = at91ether_setup(phy_id, phy_address, pdev);
+                               detected = at91ether_setup(phy_id, phy_address, pdev, ether_clk);
                                break;
                }
 
@@ -1070,22 +1120,67 @@ static int __init at91ether_probe(struct platform_device *pdev)
 
 static int __devexit at91ether_remove(struct platform_device *pdev)
 {
-       struct at91_private *lp = (struct at91_private *) at91_dev->priv;
+       struct net_device *dev = platform_get_drvdata(pdev);
+       struct at91_private *lp = netdev_priv(dev);
 
-       unregister_netdev(at91_dev);
-       free_irq(at91_dev->irq, at91_dev);
+       unregister_netdev(dev);
+       free_irq(dev->irq, dev);
        dma_free_coherent(NULL, sizeof(struct recv_desc_bufs), lp->dlist, (dma_addr_t)lp->dlist_phys);
-       clk_put(ether_clk);
+       clk_put(lp->ether_clk);
 
-       free_netdev(at91_dev);
-       at91_dev = NULL;
+       platform_set_drvdata(pdev, NULL);
+       free_netdev(dev);
        return 0;
 }
 
+#ifdef CONFIG_PM
+
+static int at91ether_suspend(struct platform_device *pdev, pm_message_t mesg)
+{
+       struct net_device *net_dev = platform_get_drvdata(pdev);
+       struct at91_private *lp = netdev_priv(net_dev);
+       int phy_irq = lp->board_data.phy_irq_pin;
+
+       if (netif_running(net_dev)) {
+               if (phy_irq)
+                       disable_irq(phy_irq);
+
+               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);
+       struct at91_private *lp = netdev_priv(net_dev);
+       int phy_irq = lp->board_data.phy_irq_pin;
+
+       if (netif_running(net_dev)) {
+               clk_enable(lp->ether_clk);
+
+               netif_device_attach(net_dev);
+               netif_start_queue(net_dev);
+
+               if (phy_irq)
+                       enable_irq(phy_irq);
+       }
+       return 0;
+}
+
+#else
+#define at91ether_suspend      NULL
+#define at91ether_resume       NULL
+#endif
+
 static struct platform_driver at91ether_driver = {
        .probe          = at91ether_probe,
        .remove         = __devexit_p(at91ether_remove),
-       /* FIXME:  support suspend and resume */
+       .suspend        = at91ether_suspend,
+       .resume         = at91ether_resume,
        .driver         = {
                .name   = DRV_NAME,
                .owner  = THIS_MODULE,