drivers/net/skge.c: Use netif_printk macros
[safe/jmp/linux-2.6] / drivers / net / macb.c
index 489c7c3..7a5f897 100644 (file)
@@ -21,8 +21,8 @@
 #include <linux/platform_device.h>
 #include <linux/phy.h>
 
-#include <asm/arch/board.h>
-#include <asm/arch/cpu.h>
+#include <mach/board.h>
+#include <mach/cpu.h>
 
 #include "macb.h"
 
@@ -80,8 +80,12 @@ static void __init macb_get_hwaddr(struct macb *bp)
        addr[4] = top & 0xff;
        addr[5] = (top >> 8) & 0xff;
 
-       if (is_valid_ether_addr(addr))
+       if (is_valid_ether_addr(addr)) {
                memcpy(bp->dev->dev_addr, addr, sizeof(addr));
+       } else {
+               dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
+               random_ether_addr(bp->dev->dev_addr);
+       }
 }
 
 static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
@@ -160,9 +164,7 @@ static void macb_handle_link_change(struct net_device *dev)
        }
 
        if (phydev->link != bp->link) {
-               if (phydev->link)
-                       netif_schedule(dev);
-               else {
+               if (!phydev->link) {
                        bp->speed = 0;
                        bp->duplex = -1;
                }
@@ -187,18 +189,11 @@ static void macb_handle_link_change(struct net_device *dev)
 static int macb_mii_probe(struct net_device *dev)
 {
        struct macb *bp = netdev_priv(dev);
-       struct phy_device *phydev = NULL;
+       struct phy_device *phydev;
        struct eth_platform_data *pdata;
-       int phy_addr;
-
-       /* find the first phy */
-       for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
-               if (bp->mii_bus.phy_map[phy_addr]) {
-                       phydev = bp->mii_bus.phy_map[phy_addr];
-                       break;
-               }
-       }
+       int ret;
 
+       phydev = phy_find_first(bp->mii_bus);
        if (!phydev) {
                printk (KERN_ERR "%s: no PHY found\n", dev->name);
                return -1;
@@ -208,17 +203,13 @@ static int macb_mii_probe(struct net_device *dev)
        /* TODO : add pin_irq */
 
        /* attach the mac to the phy */
-       if (pdata && pdata->is_rmii) {
-               phydev = phy_connect(dev, phydev->dev.bus_id,
-                       &macb_handle_link_change, 0, PHY_INTERFACE_MODE_RMII);
-       } else {
-               phydev = phy_connect(dev, phydev->dev.bus_id,
-                       &macb_handle_link_change, 0, PHY_INTERFACE_MODE_MII);
-       }
-
-       if (IS_ERR(phydev)) {
+       ret = phy_connect_direct(dev, phydev, &macb_handle_link_change, 0,
+                                pdata && pdata->is_rmii ?
+                                PHY_INTERFACE_MODE_RMII :
+                                PHY_INTERFACE_MODE_MII);
+       if (ret) {
                printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
-               return PTR_ERR(phydev);
+               return ret;
        }
 
        /* mask with MAC supported features */
@@ -239,33 +230,39 @@ static int macb_mii_init(struct macb *bp)
        struct eth_platform_data *pdata;
        int err = -ENXIO, i;
 
-       /* Enable managment port */
+       /* Enable management port */
        macb_writel(bp, NCR, MACB_BIT(MPE));
 
-       bp->mii_bus.name = "MACB_mii_bus";
-       bp->mii_bus.read = &macb_mdio_read;
-       bp->mii_bus.write = &macb_mdio_write;
-       bp->mii_bus.reset = &macb_mdio_reset;
-       bp->mii_bus.id = bp->pdev->id;
-       bp->mii_bus.priv = bp;
-       bp->mii_bus.dev = &bp->dev->dev;
+       bp->mii_bus = mdiobus_alloc();
+       if (bp->mii_bus == NULL) {
+               err = -ENOMEM;
+               goto err_out;
+       }
+
+       bp->mii_bus->name = "MACB_mii_bus";
+       bp->mii_bus->read = &macb_mdio_read;
+       bp->mii_bus->write = &macb_mdio_write;
+       bp->mii_bus->reset = &macb_mdio_reset;
+       snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%x", bp->pdev->id);
+       bp->mii_bus->priv = bp;
+       bp->mii_bus->parent = &bp->dev->dev;
        pdata = bp->pdev->dev.platform_data;
 
        if (pdata)
-               bp->mii_bus.phy_mask = pdata->phy_mask;
+               bp->mii_bus->phy_mask = pdata->phy_mask;
 
-       bp->mii_bus.irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
-       if (!bp->mii_bus.irq) {
+       bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
+       if (!bp->mii_bus->irq) {
                err = -ENOMEM;
-               goto err_out;
+               goto err_out_free_mdiobus;
        }
 
        for (i = 0; i < PHY_MAX_ADDR; i++)
-               bp->mii_bus.irq[i] = PHY_POLL;
+               bp->mii_bus->irq[i] = PHY_POLL;
 
-       platform_set_drvdata(bp->dev, &bp->mii_bus);
+       platform_set_drvdata(bp->dev, bp->mii_bus);
 
-       if (mdiobus_register(&bp->mii_bus))
+       if (mdiobus_register(bp->mii_bus))
                goto err_out_free_mdio_irq;
 
        if (macb_mii_probe(bp->dev) != 0) {
@@ -275,9 +272,11 @@ static int macb_mii_init(struct macb *bp)
        return 0;
 
 err_out_unregister_bus:
-       mdiobus_unregister(&bp->mii_bus);
+       mdiobus_unregister(bp->mii_bus);
 err_out_free_mdio_irq:
-       kfree(bp->mii_bus.irq);
+       kfree(bp->mii_bus->irq);
+err_out_free_mdiobus:
+       mdiobus_free(bp->mii_bus);
 err_out:
        return err;
 }
@@ -306,10 +305,15 @@ static void macb_tx(struct macb *bp)
        dev_dbg(&bp->pdev->dev, "macb_tx status = %02lx\n",
                (unsigned long)status);
 
-       if (status & MACB_BIT(UND)) {
+       if (status & (MACB_BIT(UND) | MACB_BIT(TSR_RLE))) {
                int i;
-               printk(KERN_ERR "%s: TX underrun, resetting buffers\n",
-                       bp->dev->name);
+               printk(KERN_ERR "%s: TX %s, resetting buffers\n",
+                       bp->dev->name, status & MACB_BIT(UND) ?
+                       "underrun" : "retry limit exceeded");
+
+               /* Transfer ongoing, disable transmitter, to avoid confusion */
+               if (status & MACB_BIT(TGO))
+                       macb_writel(bp, NCR, macb_readl(bp, NCR) & ~MACB_BIT(TE));
 
                head = bp->tx_head;
 
@@ -333,6 +337,10 @@ static void macb_tx(struct macb *bp)
                }
 
                bp->tx_head = bp->tx_tail = 0;
+
+               /* Enable the transmitter again */
+               if (status & MACB_BIT(TGO))
+                       macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TE));
        }
 
        if (!(status & MACB_BIT(COMP)))
@@ -425,7 +433,6 @@ static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
 
        bp->stats.rx_packets++;
        bp->stats.rx_bytes += len;
-       bp->dev->last_rx = jiffies;
        dev_dbg(&bp->pdev->dev, "received skb of length %u, csum: %08x\n",
                skb->len, skb->csum);
        netif_receive_skb(skb);
@@ -496,7 +503,6 @@ static int macb_rx(struct macb *bp, int budget)
 static int macb_poll(struct napi_struct *napi, int budget)
 {
        struct macb *bp = container_of(napi, struct macb, napi);
-       struct net_device *dev = bp->dev;
        int work_done;
        u32 status;
 
@@ -504,36 +510,18 @@ static int macb_poll(struct napi_struct *napi, int budget)
        macb_writel(bp, RSR, status);
 
        work_done = 0;
-       if (!status) {
-               /*
-                * This may happen if an interrupt was pending before
-                * this function was called last time, and no packets
-                * have been received since.
-                */
-               netif_rx_complete(dev, napi);
-               goto out;
-       }
 
        dev_dbg(&bp->pdev->dev, "poll: status = %08lx, budget = %d\n",
                (unsigned long)status, budget);
 
-       if (!(status & MACB_BIT(REC))) {
-               dev_warn(&bp->pdev->dev,
-                        "No RX buffers complete, status = %02lx\n",
-                        (unsigned long)status);
-               netif_rx_complete(dev, napi);
-               goto out;
-       }
-
        work_done = macb_rx(bp, budget);
        if (work_done < budget)
-               netif_rx_complete(dev, napi);
+               napi_complete(napi);
 
        /*
         * We've done what we can to clean the buffers. Make sure we
         * get notified when new packets arrive.
         */
-out:
        macb_writel(bp, IER, MACB_RX_INT_FLAGS);
 
        /* TODO: Handle errors */
@@ -562,7 +550,7 @@ static irqreturn_t macb_interrupt(int irq, void *dev_id)
                }
 
                if (status & MACB_RX_INT_FLAGS) {
-                       if (netif_rx_schedule_prep(dev, &bp->napi)) {
+                       if (napi_schedule_prep(&bp->napi)) {
                                /*
                                 * There's no point taking any more interrupts
                                 * until we have processed the buffers
@@ -570,11 +558,12 @@ static irqreturn_t macb_interrupt(int irq, void *dev_id)
                                macb_writel(bp, IDR, MACB_RX_INT_FLAGS);
                                dev_dbg(&bp->pdev->dev,
                                        "scheduling RX softirq\n");
-                               __netif_rx_schedule(dev, &bp->napi);
+                               __napi_schedule(&bp->napi);
                        }
                }
 
-               if (status & (MACB_BIT(TCOMP) | MACB_BIT(ISR_TUND)))
+               if (status & (MACB_BIT(TCOMP) | MACB_BIT(ISR_TUND) |
+                           MACB_BIT(ISR_RLE)))
                        macb_tx(bp);
 
                /*
@@ -599,12 +588,28 @@ static irqreturn_t macb_interrupt(int irq, void *dev_id)
        return IRQ_HANDLED;
 }
 
+#ifdef CONFIG_NET_POLL_CONTROLLER
+/*
+ * Polling receive - used by netconsole and other diagnostic tools
+ * to allow network i/o with interrupts disabled.
+ */
+static void macb_poll_controller(struct net_device *dev)
+{
+       unsigned long flags;
+
+       local_irq_save(flags);
+       macb_interrupt(dev->irq, dev);
+       local_irq_restore(flags);
+}
+#endif
+
 static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
 {
        struct macb *bp = netdev_priv(dev);
        dma_addr_t mapping;
        unsigned int len, entry;
        u32 ctrl;
+       unsigned long flags;
 
 #ifdef DEBUG
        int i;
@@ -620,17 +625,17 @@ static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
 #endif
 
        len = skb->len;
-       spin_lock_irq(&bp->lock);
+       spin_lock_irqsave(&bp->lock, flags);
 
        /* This is a hard error, log it. */
        if (TX_BUFFS_AVAIL(bp) < 1) {
                netif_stop_queue(dev);
-               spin_unlock_irq(&bp->lock);
+               spin_unlock_irqrestore(&bp->lock, flags);
                dev_err(&bp->pdev->dev,
                        "BUG! Tx Ring full when queue awake!\n");
                dev_dbg(&bp->pdev->dev, "tx_head = %u, tx_tail = %u\n",
                        bp->tx_head, bp->tx_tail);
-               return 1;
+               return NETDEV_TX_BUSY;
        }
 
        entry = bp->tx_head;
@@ -659,11 +664,11 @@ static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
        if (TX_BUFFS_AVAIL(bp) < 1)
                netif_stop_queue(dev);
 
-       spin_unlock_irq(&bp->lock);
+       spin_unlock_irqrestore(&bp->lock, flags);
 
        dev->trans_start = jiffies;
 
-       return 0;
+       return NETDEV_TX_OK;
 }
 
 static void macb_free_consistent(struct macb *bp)
@@ -885,7 +890,7 @@ static void macb_sethashtable(struct net_device *dev)
        mc_filter[0] = mc_filter[1] = 0;
 
        curr = dev->mc_list;
-       for (i = 0; i < dev->mc_count; i++, curr = curr->next) {
+       for (i = 0; i < netdev_mc_count(dev); i++, curr = curr->next) {
                if (!curr) break;       /* unexpected end of list */
 
                bitnr = hash_get_index(curr->dmi_addr);
@@ -918,7 +923,7 @@ static void macb_set_rx_mode(struct net_device *dev)
                macb_writel(bp, HRB, -1);
                macb_writel(bp, HRT, -1);
                cfg |= MACB_BIT(NCFGR_MTI);
-       } else if (dev->mc_count > 0) {
+       } else if (!netdev_mc_empty(dev)) {
                /* Enable specific multicasts */
                macb_sethashtable(dev);
                cfg |= MACB_BIT(NCFGR_MTI);
@@ -1060,10 +1065,10 @@ static void macb_get_drvinfo(struct net_device *dev,
 
        strcpy(info->driver, bp->pdev->dev.driver->name);
        strcpy(info->version, "$Revision: 1.14 $");
-       strcpy(info->bus_info, bp->pdev->dev.bus_id);
+       strcpy(info->bus_info, dev_name(&bp->pdev->dev));
 }
 
-static struct ethtool_ops macb_ethtool_ops = {
+static const struct ethtool_ops macb_ethtool_ops = {
        .get_settings           = macb_get_settings,
        .set_settings           = macb_set_settings,
        .get_drvinfo            = macb_get_drvinfo,
@@ -1084,6 +1089,21 @@ static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
        return phy_mii_ioctl(phydev, if_mii(rq), cmd);
 }
 
+static const struct net_device_ops macb_netdev_ops = {
+       .ndo_open               = macb_open,
+       .ndo_stop               = macb_close,
+       .ndo_start_xmit         = macb_start_xmit,
+       .ndo_set_multicast_list = macb_set_rx_mode,
+       .ndo_get_stats          = macb_get_stats,
+       .ndo_do_ioctl           = macb_ioctl,
+       .ndo_validate_addr      = eth_validate_addr,
+       .ndo_change_mtu         = eth_change_mtu,
+       .ndo_set_mac_address    = eth_mac_addr,
+#ifdef CONFIG_NET_POLL_CONTROLLER
+       .ndo_poll_controller    = macb_poll_controller,
+#endif
+};
+
 static int __init macb_probe(struct platform_device *pdev)
 {
        struct eth_platform_data *pdata;
@@ -1094,7 +1114,6 @@ static int __init macb_probe(struct platform_device *pdev)
        unsigned long pclk_hz;
        u32 config;
        int err = -ENXIO;
-       DECLARE_MAC_BUF(mac);
 
        regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
        if (!regs) {
@@ -1160,12 +1179,7 @@ static int __init macb_probe(struct platform_device *pdev)
                goto err_out_iounmap;
        }
 
-       dev->open = macb_open;
-       dev->stop = macb_close;
-       dev->hard_start_xmit = macb_start_xmit;
-       dev->get_stats = macb_get_stats;
-       dev->set_multicast_list = macb_set_rx_mode;
-       dev->do_ioctl = macb_ioctl;
+       dev->netdev_ops = &macb_netdev_ops;
        netif_napi_add(dev, &bp->napi, macb_poll, 64);
        dev->ethtool_ops = &macb_ethtool_ops;
 
@@ -1213,15 +1227,13 @@ static int __init macb_probe(struct platform_device *pdev)
 
        platform_set_drvdata(pdev, dev);
 
-       printk(KERN_INFO "%s: Atmel MACB at 0x%08lx irq %d "
-              "(%s)\n",
-              dev->name, dev->base_addr, dev->irq,
-              print_mac(mac, dev->dev_addr));
+       printk(KERN_INFO "%s: Atmel MACB at 0x%08lx irq %d (%pM)\n",
+              dev->name, dev->base_addr, dev->irq, dev->dev_addr);
 
        phydev = bp->phy_dev;
        printk(KERN_INFO "%s: attached PHY driver [%s] "
-               "(mii_bus:phy_addr=%s, irq=%d)\n",
-               dev->name, phydev->drv->name, phydev->dev.bus_id, phydev->irq);
+               "(mii_bus:phy_addr=%s, irq=%d)\n", dev->name,
+               phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
 
        return 0;
 
@@ -1259,8 +1271,9 @@ static int __exit macb_remove(struct platform_device *pdev)
                bp = netdev_priv(dev);
                if (bp->phy_dev)
                        phy_disconnect(bp->phy_dev);
-               mdiobus_unregister(&bp->mii_bus);
-               kfree(bp->mii_bus.irq);
+               mdiobus_unregister(bp->mii_bus);
+               kfree(bp->mii_bus->irq);
+               mdiobus_free(bp->mii_bus);
                unregister_netdev(dev);
                free_irq(dev->irq, dev);
                iounmap(bp->regs);
@@ -1277,10 +1290,48 @@ static int __exit macb_remove(struct platform_device *pdev)
        return 0;
 }
 
+#ifdef CONFIG_PM
+static int macb_suspend(struct platform_device *pdev, pm_message_t state)
+{
+       struct net_device *netdev = platform_get_drvdata(pdev);
+       struct macb *bp = netdev_priv(netdev);
+
+       netif_device_detach(netdev);
+
+#ifndef CONFIG_ARCH_AT91
+       clk_disable(bp->hclk);
+#endif
+       clk_disable(bp->pclk);
+
+       return 0;
+}
+
+static int macb_resume(struct platform_device *pdev)
+{
+       struct net_device *netdev = platform_get_drvdata(pdev);
+       struct macb *bp = netdev_priv(netdev);
+
+       clk_enable(bp->pclk);
+#ifndef CONFIG_ARCH_AT91
+       clk_enable(bp->hclk);
+#endif
+
+       netif_device_attach(netdev);
+
+       return 0;
+}
+#else
+#define macb_suspend   NULL
+#define macb_resume    NULL
+#endif
+
 static struct platform_driver macb_driver = {
        .remove         = __exit_p(macb_remove),
+       .suspend        = macb_suspend,
+       .resume         = macb_resume,
        .driver         = {
                .name           = "macb",
+               .owner  = THIS_MODULE,
        },
 };
 
@@ -1300,3 +1351,4 @@ module_exit(macb_exit);
 MODULE_LICENSE("GPL");
 MODULE_DESCRIPTION("Atmel MACB Ethernet driver");
 MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
+MODULE_ALIAS("platform:macb");