Merge branch 'linus' into cont_syslog
[safe/jmp/linux-2.6] / drivers / net / smsc911x.c
index d615146..cc55974 100644 (file)
 #include <linux/netdevice.h>
 #include <linux/platform_device.h>
 #include <linux/sched.h>
-#include <linux/slab.h>
 #include <linux/timer.h>
-#include <linux/version.h>
 #include <linux/bug.h>
 #include <linux/bitops.h>
 #include <linux/irq.h>
 #include <linux/io.h>
+#include <linux/swab.h>
 #include <linux/phy.h>
 #include <linux/smsc911x.h>
+#include <linux/device.h>
 #include "smsc911x.h"
 
 #define SMSC_CHIPNAME          "smsc911x"
@@ -176,6 +176,12 @@ static inline void
 smsc911x_tx_writefifo(struct smsc911x_data *pdata, unsigned int *buf,
                      unsigned int wordcount)
 {
+       if (pdata->config.flags & SMSC911X_SWAP_FIFO) {
+               while (wordcount--)
+                       smsc911x_reg_write(pdata, TX_DATA_FIFO, swab32(*buf++));
+               return;
+       }
+
        if (pdata->config.flags & SMSC911X_USE_32BIT) {
                writesl(pdata->ioaddr + TX_DATA_FIFO, buf, wordcount);
                return;
@@ -195,6 +201,12 @@ static inline void
 smsc911x_rx_readfifo(struct smsc911x_data *pdata, unsigned int *buf,
                     unsigned int wordcount)
 {
+       if (pdata->config.flags & SMSC911X_SWAP_FIFO) {
+               while (wordcount--)
+                       *buf++ = swab32(smsc911x_reg_read(pdata, RX_DATA_FIFO));
+               return;
+       }
+
        if (pdata->config.flags & SMSC911X_USE_32BIT) {
                readsl(pdata->ioaddr + RX_DATA_FIFO, buf, wordcount);
                return;
@@ -318,7 +330,7 @@ static int smsc911x_mii_read(struct mii_bus *bus, int phyaddr, int regidx)
                        goto out;
                }
 
-       SMSC_WARNING(HW, "Timed out waiting for MII write to finish");
+       SMSC_WARNING(HW, "Timed out waiting for MII read to finish");
        reg = -EIO;
 
 out:
@@ -724,7 +736,7 @@ static void smsc911x_phy_adjust_link(struct net_device *dev)
                        SMSC_TRACE(HW, "configuring for carrier OK");
                        if ((pdata->gpio_orig_setting & GPIO_CFG_LED1_EN_) &&
                            (!pdata->using_extphy)) {
-                               /* Restore orginal GPIO configuration */
+                               /* Restore original GPIO configuration */
                                pdata->gpio_setting = pdata->gpio_orig_setting;
                                smsc911x_reg_write(pdata, GPIO_CFG,
                                        pdata->gpio_setting);
@@ -735,10 +747,10 @@ static void smsc911x_phy_adjust_link(struct net_device *dev)
                         * usage is 10/100 indicator */
                        pdata->gpio_setting = smsc911x_reg_read(pdata,
                                GPIO_CFG);
-                       if ((pdata->gpio_setting & GPIO_CFG_LED1_EN_)
-                           && (!pdata->using_extphy)) {
+                       if ((pdata->gpio_setting & GPIO_CFG_LED1_EN_) &&
+                           (!pdata->using_extphy)) {
                                /* Force 10/100 LED off, after saving
-                                * orginal GPIO configuration */
+                                * original GPIO configuration */
                                pdata->gpio_orig_setting = pdata->gpio_setting;
 
                                pdata->gpio_setting &= ~GPIO_CFG_LED1_EN_;
@@ -757,29 +769,25 @@ static int smsc911x_mii_probe(struct net_device *dev)
 {
        struct smsc911x_data *pdata = netdev_priv(dev);
        struct phy_device *phydev = NULL;
-       int phy_addr;
+       int ret;
 
        /* find the first phy */
-       for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
-               if (pdata->mii_bus->phy_map[phy_addr]) {
-                       phydev = pdata->mii_bus->phy_map[phy_addr];
-                       SMSC_TRACE(PROBE, "PHY %d: addr %d, phy_id 0x%08X",
-                               phy_addr, phydev->addr, phydev->phy_id);
-                       break;
-               }
-       }
-
+       phydev = phy_find_first(pdata->mii_bus);
        if (!phydev) {
                pr_err("%s: no PHY found\n", dev->name);
                return -ENODEV;
        }
 
-       phydev = phy_connect(dev, dev_name(&phydev->dev),
-               &smsc911x_phy_adjust_link, 0, pdata->config.phy_interface);
+       SMSC_TRACE(PROBE, "PHY %d: addr %d, phy_id 0x%08X",
+                       phy_addr, phydev->addr, phydev->phy_id);
 
-       if (IS_ERR(phydev)) {
+       ret = phy_connect_direct(dev, phydev,
+                       &smsc911x_phy_adjust_link, 0,
+                       pdata->config.phy_interface);
+
+       if (ret) {
                pr_err("%s: Could not attach to PHY\n", dev->name);
-               return PTR_ERR(phydev);
+               return ret;
        }
 
        pr_info("%s: attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
@@ -803,7 +811,7 @@ static int smsc911x_mii_probe(struct net_device *dev)
        SMSC_TRACE(HW, "Passed Loop Back Test");
 #endif                         /* USE_PHY_WORK_AROUND */
 
-       SMSC_TRACE(HW, "phy initialised succesfully");
+       SMSC_TRACE(HW, "phy initialised successfully");
        return 0;
 }
 
@@ -973,7 +981,7 @@ static int smsc911x_poll(struct napi_struct *napi, int budget)
        struct net_device *dev = pdata->dev;
        int npackets = 0;
 
-       while (likely(netif_running(dev)) && (npackets < budget)) {
+       while (npackets < budget) {
                unsigned int pktlength;
                unsigned int pktwords;
                struct sk_buff *skb;
@@ -1034,7 +1042,6 @@ static int smsc911x_poll(struct napi_struct *napi, int budget)
                /* Update counters */
                dev->stats.rx_packets++;
                dev->stats.rx_bytes += (pktlength - 4);
-               dev->last_rx = jiffies;
        }
 
        /* Return total received packets */
@@ -1225,6 +1232,10 @@ static int smsc911x_open(struct net_device *dev)
        dev_info(&dev->dev, "SMSC911x/921x identified at %#08lx, IRQ: %d\n",
                 (unsigned long)pdata->ioaddr, dev->irq);
 
+       /* Reset the last known duplex and carrier */
+       pdata->last_duplex = -1;
+       pdata->last_carrier = -1;
+
        /* Bring the PHY up */
        phy_start(pdata->phy_dev);
 
@@ -1324,7 +1335,6 @@ static int smsc911x_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
        smsc911x_tx_writefifo(pdata, (unsigned int *)bufp, wrsz);
        freespace -= (skb->len + 32);
        dev_kfree_skb(skb);
-       dev->trans_start = jiffies;
 
        if (unlikely(smsc911x_tx_get_txstatcount(pdata) >= 30))
                smsc911x_tx_update_txcounters(dev);
@@ -1367,33 +1377,24 @@ static void smsc911x_set_multicast_list(struct net_device *dev)
                pdata->clear_bits_mask = (MAC_CR_PRMS_ | MAC_CR_HPFILT_);
                pdata->hashhi = 0;
                pdata->hashlo = 0;
-       } else if (dev->mc_count > 0) {
+       } else if (!netdev_mc_empty(dev)) {
                /* Enabling specific multicast addresses */
                unsigned int hash_high = 0;
                unsigned int hash_low = 0;
-               unsigned int count = 0;
-               struct dev_mc_list *mc_list = dev->mc_list;
+               struct netdev_hw_addr *ha;
 
                pdata->set_bits_mask = MAC_CR_HPFILT_;
                pdata->clear_bits_mask = (MAC_CR_PRMS_ | MAC_CR_MCPAS_);
 
-               while (mc_list) {
-                       count++;
-                       if ((mc_list->dmi_addrlen) == ETH_ALEN) {
-                               unsigned int bitnum =
-                                   smsc911x_hash(mc_list->dmi_addr);
-                               unsigned int mask = 0x01 << (bitnum & 0x1F);
-                               if (bitnum & 0x20)
-                                       hash_high |= mask;
-                               else
-                                       hash_low |= mask;
-                       } else {
-                               SMSC_WARNING(DRV, "dmi_addrlen != 6");
-                       }
-                       mc_list = mc_list->next;
+               netdev_for_each_mc_addr(ha, dev) {
+                       unsigned int bitnum = smsc911x_hash(ha->addr);
+                       unsigned int mask = 0x01 << (bitnum & 0x1F);
+
+                       if (bitnum & 0x20)
+                               hash_high |= mask;
+                       else
+                               hash_low |= mask;
                }
-               if (count != (unsigned int)dev->mc_count)
-                       SMSC_WARNING(DRV, "mc_count != dev->mc_count");
 
                pdata->hashhi = hash_high;
                pdata->hashlo = hash_low;
@@ -1676,6 +1677,7 @@ static int smsc911x_eeprom_write_location(struct smsc911x_data *pdata,
                                          u8 address, u8 data)
 {
        u32 op = E2P_CMD_EPC_CMD_ERASE_ | address;
+       u32 temp;
        int ret;
 
        SMSC_TRACE(DRV, "address 0x%x, data 0x%x", address, data);
@@ -1684,6 +1686,10 @@ static int smsc911x_eeprom_write_location(struct smsc911x_data *pdata,
        if (!ret) {
                op = E2P_CMD_EPC_CMD_WRITE_ | address;
                smsc911x_reg_write(pdata, E2P_DATA, (u32)data);
+
+               /* Workaround for hardware read-after-write restriction */
+               temp = smsc911x_reg_read(pdata, BYTE_TEST);
+
                ret = smsc911x_eeprom_send_cmd(pdata, op);
        }
 
@@ -1758,6 +1764,7 @@ static const struct net_device_ops smsc911x_netdev_ops = {
        .ndo_get_stats          = smsc911x_get_stats,
        .ndo_set_multicast_list = smsc911x_set_multicast_list,
        .ndo_do_ioctl           = smsc911x_do_ioctl,
+       .ndo_change_mtu         = eth_change_mtu,
        .ndo_validate_addr      = eth_validate_addr,
        .ndo_set_mac_address    = smsc911x_set_mac_address,
 #ifdef CONFIG_NET_POLL_CONTROLLER
@@ -1917,7 +1924,7 @@ static int __devexit smsc911x_drv_remove(struct platform_device *pdev)
        if (!res)
                res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 
-       release_mem_region(res->start, res->end - res->start);
+       release_mem_region(res->start, resource_size(res));
 
        iounmap(pdata->ioaddr);
 
@@ -1955,7 +1962,7 @@ static int __devinit smsc911x_drv_probe(struct platform_device *pdev)
                retval = -ENODEV;
                goto out_0;
        }
-       res_size = res->end - res->start;
+       res_size = resource_size(res);
 
        irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
        if (!irq_res) {
@@ -2049,6 +2056,9 @@ static int __devinit smsc911x_drv_probe(struct platform_device *pdev)
        if (is_valid_ether_addr(dev->dev_addr)) {
                smsc911x_set_hw_mac_address(pdata, dev->dev_addr);
                SMSC_TRACE(PROBE, "MAC Address is specified by configuration");
+       } else if (is_valid_ether_addr(pdata->config.mac)) {
+               memcpy(dev->dev_addr, pdata->config.mac, 6);
+               SMSC_TRACE(PROBE, "MAC Address specified by platform data");
        } else {
                /* Try reading mac address from device. if EEPROM is present
                 * it will already have been set */
@@ -2083,16 +2093,70 @@ out_unmap_io_3:
 out_free_netdev_2:
        free_netdev(dev);
 out_release_io_1:
-       release_mem_region(res->start, res->end - res->start);
+       release_mem_region(res->start, resource_size(res));
 out_0:
        return retval;
 }
 
+#ifdef CONFIG_PM
+/* This implementation assumes the devices remains powered on its VDDVARIO
+ * pins during suspend. */
+
+/* TODO: implement freeze/thaw callbacks for hibernation.*/
+
+static int smsc911x_suspend(struct device *dev)
+{
+       struct net_device *ndev = dev_get_drvdata(dev);
+       struct smsc911x_data *pdata = netdev_priv(ndev);
+
+       /* enable wake on LAN, energy detection and the external PME
+        * signal. */
+       smsc911x_reg_write(pdata, PMT_CTRL,
+               PMT_CTRL_PM_MODE_D1_ | PMT_CTRL_WOL_EN_ |
+               PMT_CTRL_ED_EN_ | PMT_CTRL_PME_EN_);
+
+       return 0;
+}
+
+static int smsc911x_resume(struct device *dev)
+{
+       struct net_device *ndev = dev_get_drvdata(dev);
+       struct smsc911x_data *pdata = netdev_priv(ndev);
+       unsigned int to = 100;
+
+       /* Note 3.11 from the datasheet:
+        *      "When the LAN9220 is in a power saving state, a write of any
+        *       data to the BYTE_TEST register will wake-up the device."
+        */
+       smsc911x_reg_write(pdata, BYTE_TEST, 0);
+
+       /* poll the READY bit in PMT_CTRL. Any other access to the device is
+        * forbidden while this bit isn't set. Try for 100ms and return -EIO
+        * if it failed. */
+       while (!(smsc911x_reg_read(pdata, PMT_CTRL) & PMT_CTRL_READY_) && --to)
+               udelay(1000);
+
+       return (to == 0) ? -EIO : 0;
+}
+
+static const struct dev_pm_ops smsc911x_pm_ops = {
+       .suspend        = smsc911x_suspend,
+       .resume         = smsc911x_resume,
+};
+
+#define SMSC911X_PM_OPS (&smsc911x_pm_ops)
+
+#else
+#define SMSC911X_PM_OPS NULL
+#endif
+
 static struct platform_driver smsc911x_driver = {
        .probe = smsc911x_drv_probe,
-       .remove = smsc911x_drv_remove,
+       .remove = __devexit_p(smsc911x_drv_remove),
        .driver = {
-               .name = SMSC_CHIPNAME,
+               .name   = SMSC_CHIPNAME,
+               .owner  = THIS_MODULE,
+               .pm     = SMSC911X_PM_OPS,
        },
 };