[TG3]: 5784 / 5764 DMA engine lockup fix
[safe/jmp/linux-2.6] / drivers / net / fs_enet / fs_enet-main.c
index 196298f..f2a4d39 100644 (file)
@@ -1,32 +1,29 @@
 /*
  * Combined Ethernet driver for Motorola MPC8xx and MPC82xx.
  *
- * Copyright (c) 2003 Intracom S.A. 
+ * Copyright (c) 2003 Intracom S.A.
  *  by Pantelis Antoniou <panto@intracom.gr>
- * 
- * 2005 (c) MontaVista Software, Inc. 
+ *
+ * 2005 (c) MontaVista Software, Inc.
  * Vitaly Bordug <vbordug@ru.mvista.com>
  *
  * Heavily based on original FEC driver by Dan Malek <dan@embeddededge.com>
  * and modifications by Joakim Tjernlund <joakim.tjernlund@lumentis.se>
  *
- * This file is licensed under the terms of the GNU General Public License 
- * version 2. This program is licensed "as is" without any warranty of any 
+ * This file is licensed under the terms of the GNU General Public License
+ * version 2. This program is licensed "as is" without any warranty of any
  * kind, whether express or implied.
  */
 
-#include <linux/config.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/types.h>
-#include <linux/sched.h>
 #include <linux/string.h>
 #include <linux/ptrace.h>
 #include <linux/errno.h>
 #include <linux/ioport.h>
 #include <linux/slab.h>
 #include <linux/interrupt.h>
-#include <linux/pci.h>
 #include <linux/init.h>
 #include <linux/delay.h>
 #include <linux/netdevice.h>
 #include <linux/bitops.h>
 #include <linux/fs.h>
 #include <linux/platform_device.h>
+#include <linux/phy.h>
 
 #include <linux/vmalloc.h>
 #include <asm/pgtable.h>
-
-#include <asm/pgtable.h>
 #include <asm/irq.h>
 #include <asm/uaccess.h>
 
+#ifdef CONFIG_PPC_CPM_NEW_BINDING
+#include <asm/of_platform.h>
+#endif
+
 #include "fs_enet.h"
 
 /*************************************************/
 
+#ifndef CONFIG_PPC_CPM_NEW_BINDING
 static char version[] __devinitdata =
     DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")" "\n";
+#endif
 
 MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>");
 MODULE_DESCRIPTION("Freescale Ethernet Driver");
 MODULE_LICENSE("GPL");
 MODULE_VERSION(DRV_MODULE_VERSION);
 
-int fs_enet_debug = -1;                /* -1 == use FS_ENET_DEF_MSG_ENABLE as value */
+static int fs_enet_debug = -1; /* -1 == use FS_ENET_DEF_MSG_ENABLE as value */
 module_param(fs_enet_debug, int, 0);
 MODULE_PARM_DESC(fs_enet_debug,
                 "Freescale bitmapped debugging message enable value");
 
+#ifdef CONFIG_NET_POLL_CONTROLLER
+static void fs_enet_netpoll(struct net_device *dev);
+#endif
 
 static void fs_set_multicast_list(struct net_device *dev)
 {
@@ -71,19 +76,25 @@ static void fs_set_multicast_list(struct net_device *dev)
        (*fep->ops->set_multicast_list)(dev);
 }
 
+static void skb_align(struct sk_buff *skb, int align)
+{
+       int off = ((unsigned long)skb->data) & (align - 1);
+
+       if (off)
+               skb_reserve(skb, align - off);
+}
+
 /* NAPI receive function */
-static int fs_enet_rx_napi(struct net_device *dev, int *budget)
+static int fs_enet_rx_napi(struct napi_struct *napi, int budget)
 {
-       struct fs_enet_private *fep = netdev_priv(dev);
+       struct fs_enet_private *fep = container_of(napi, struct fs_enet_private, napi);
+       struct net_device *dev = fep->ndev;
        const struct fs_platform_info *fpi = fep->fpi;
-       cbd_t *bdp;
+       cbd_t __iomem *bdp;
        struct sk_buff *skb, *skbn, *skbt;
        int received = 0;
        u16 pkt_len, sc;
        int curidx;
-       int rx_work_limit = 0;  /* pacify gcc */
-
-       rx_work_limit = min(dev->quota, *budget);
 
        if (!netif_running(dev))
                return 0;
@@ -98,7 +109,6 @@ static int fs_enet_rx_napi(struct net_device *dev, int *budget)
        (*fep->ops->napi_clear_rx_event)(dev);
 
        while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
-
                curidx = bdp - fep->rx_bd_base;
 
                /*
@@ -111,7 +121,7 @@ static int fs_enet_rx_napi(struct net_device *dev, int *budget)
                               dev->name);
 
                /*
-                * Check for errors. 
+                * Check for errors.
                 */
                if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
                          BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
@@ -138,11 +148,6 @@ static int fs_enet_rx_napi(struct net_device *dev, int *budget)
                        skbn = skb;
 
                } else {
-
-                       /* napi, got packet but no quota */
-                       if (--rx_work_limit < 0)
-                               break;
-
                        skb = fep->rx_skbuff[curidx];
 
                        dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
@@ -161,17 +166,21 @@ static int fs_enet_rx_napi(struct net_device *dev, int *budget)
                                skbn = dev_alloc_skb(pkt_len + 2);
                                if (skbn != NULL) {
                                        skb_reserve(skbn, 2);   /* align IP header */
-                                       memcpy(skbn->data, skb->data, pkt_len);
+                                       skb_copy_from_linear_data(skb,
+                                                     skbn->data, pkt_len);
                                        /* swap */
                                        skbt = skb;
                                        skb = skbn;
                                        skbn = skbt;
                                }
-                       } else
+                       } else {
                                skbn = dev_alloc_skb(ENET_RX_FRSIZE);
 
+                               if (skbn)
+                                       skb_align(skbn, ENET_RX_ALIGN);
+                       }
+
                        if (skbn != NULL) {
-                               skb->dev = dev;
                                skb_put(skb, pkt_len);  /* Make room */
                                skb->protocol = eth_type_trans(skb, dev);
                                received++;
@@ -193,7 +202,7 @@ static int fs_enet_rx_napi(struct net_device *dev, int *budget)
                CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
 
                /*
-                * Update BD pointer to next entry. 
+                * Update BD pointer to next entry.
                 */
                if ((sc & BD_ENET_RX_WRAP) == 0)
                        bdp++;
@@ -201,22 +210,19 @@ static int fs_enet_rx_napi(struct net_device *dev, int *budget)
                        bdp = fep->rx_bd_base;
 
                (*fep->ops->rx_bd_done)(dev);
+
+               if (received >= budget)
+                       break;
        }
 
        fep->cur_rx = bdp;
 
-       dev->quota -= received;
-       *budget -= received;
-
-       if (rx_work_limit < 0)
-               return 1;       /* not done */
-
-       /* done */
-       netif_rx_complete(dev);
-
-       (*fep->ops->napi_enable_rx)(dev);
-
-       return 0;
+       if (received < budget) {
+               /* done */
+               netif_rx_complete(dev, napi);
+               (*fep->ops->napi_enable_rx)(dev);
+       }
+       return received;
 }
 
 /* non NAPI receive function */
@@ -224,7 +230,7 @@ static int fs_enet_rx_non_napi(struct net_device *dev)
 {
        struct fs_enet_private *fep = netdev_priv(dev);
        const struct fs_platform_info *fpi = fep->fpi;
-       cbd_t *bdp;
+       cbd_t __iomem *bdp;
        struct sk_buff *skb, *skbn, *skbt;
        int received = 0;
        u16 pkt_len, sc;
@@ -249,7 +255,7 @@ static int fs_enet_rx_non_napi(struct net_device *dev)
                               dev->name);
 
                /*
-                * Check for errors. 
+                * Check for errors.
                 */
                if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
                          BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
@@ -295,17 +301,21 @@ static int fs_enet_rx_non_napi(struct net_device *dev)
                                skbn = dev_alloc_skb(pkt_len + 2);
                                if (skbn != NULL) {
                                        skb_reserve(skbn, 2);   /* align IP header */
-                                       memcpy(skbn->data, skb->data, pkt_len);
+                                       skb_copy_from_linear_data(skb,
+                                                     skbn->data, pkt_len);
                                        /* swap */
                                        skbt = skb;
                                        skb = skbn;
                                        skbn = skbt;
                                }
-                       } else
+                       } else {
                                skbn = dev_alloc_skb(ENET_RX_FRSIZE);
 
+                               if (skbn)
+                                       skb_align(skbn, ENET_RX_ALIGN);
+                       }
+
                        if (skbn != NULL) {
-                               skb->dev = dev;
                                skb_put(skb, pkt_len);  /* Make room */
                                skb->protocol = eth_type_trans(skb, dev);
                                received++;
@@ -327,7 +337,7 @@ static int fs_enet_rx_non_napi(struct net_device *dev)
                CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
 
                /*
-                * Update BD pointer to next entry. 
+                * Update BD pointer to next entry.
                 */
                if ((sc & BD_ENET_RX_WRAP) == 0)
                        bdp++;
@@ -345,17 +355,16 @@ static int fs_enet_rx_non_napi(struct net_device *dev)
 static void fs_enet_tx(struct net_device *dev)
 {
        struct fs_enet_private *fep = netdev_priv(dev);
-       cbd_t *bdp;
+       cbd_t __iomem *bdp;
        struct sk_buff *skb;
        int dirtyidx, do_wake, do_restart;
        u16 sc;
 
-       spin_lock(&fep->lock);
+       spin_lock(&fep->tx_lock);
        bdp = fep->dirty_tx;
 
        do_wake = do_restart = 0;
        while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) {
-
                dirtyidx = bdp - fep->tx_bd_base;
 
                if (fep->tx_free == fep->tx_ring)
@@ -364,7 +373,7 @@ static void fs_enet_tx(struct net_device *dev)
                skb = fep->tx_skbuff[dirtyidx];
 
                /*
-                * Check for errors. 
+                * Check for errors.
                 */
                if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
                          BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) {
@@ -404,13 +413,13 @@ static void fs_enet_tx(struct net_device *dev)
                                skb->len, DMA_TO_DEVICE);
 
                /*
-                * Free the sk buffer associated with this last transmit. 
+                * Free the sk buffer associated with this last transmit.
                 */
                dev_kfree_skb_irq(skb);
                fep->tx_skbuff[dirtyidx] = NULL;
 
                /*
-                * Update pointer to next buffer descriptor to be transmitted. 
+                * Update pointer to next buffer descriptor to be transmitted.
                 */
                if ((sc & BD_ENET_TX_WRAP) == 0)
                        bdp++;
@@ -430,7 +439,7 @@ static void fs_enet_tx(struct net_device *dev)
        if (do_restart)
                (*fep->ops->tx_restart)(dev);
 
-       spin_unlock(&fep->lock);
+       spin_unlock(&fep->tx_lock);
 
        if (do_wake)
                netif_wake_queue(dev);
@@ -441,7 +450,7 @@ static void fs_enet_tx(struct net_device *dev)
  * This is called from the MPC core interrupt.
  */
 static irqreturn_t
-fs_enet_interrupt(int irq, void *dev_id, struct pt_regs *regs)
+fs_enet_interrupt(int irq, void *dev_id)
 {
        struct net_device *dev = dev_id;
        struct fs_enet_private *fep;
@@ -456,7 +465,6 @@ fs_enet_interrupt(int irq, void *dev_id, struct pt_regs *regs)
 
        nr = 0;
        while ((int_events = (*fep->ops->get_int_events)(dev)) != 0) {
-
                nr++;
 
                int_clr_events = int_events;
@@ -472,7 +480,7 @@ fs_enet_interrupt(int irq, void *dev_id, struct pt_regs *regs)
                        if (!fpi->use_napi)
                                fs_enet_rx_non_napi(dev);
                        else {
-                               napi_ok = netif_rx_schedule_prep(dev);
+                               napi_ok = napi_schedule_prep(&fep->napi);
 
                                (*fep->ops->napi_disable_rx)(dev);
                                (*fep->ops->clear_int_events)(dev, fep->ev_napi_rx);
@@ -480,7 +488,7 @@ fs_enet_interrupt(int irq, void *dev_id, struct pt_regs *regs)
                                /* NOTE: it is possible for FCCs in NAPI mode    */
                                /* to submit a spurious interrupt while in poll  */
                                if (napi_ok)
-                                       __netif_rx_schedule(dev);
+                                       __netif_rx_schedule(dev, &fep->napi);
                        }
                }
 
@@ -495,7 +503,7 @@ fs_enet_interrupt(int irq, void *dev_id, struct pt_regs *regs)
 void fs_init_bds(struct net_device *dev)
 {
        struct fs_enet_private *fep = netdev_priv(dev);
-       cbd_t *bdp;
+       cbd_t __iomem *bdp;
        struct sk_buff *skb;
        int i;
 
@@ -506,7 +514,7 @@ void fs_init_bds(struct net_device *dev)
        fep->cur_rx = fep->rx_bd_base;
 
        /*
-        * Initialize the receive buffer descriptors. 
+        * Initialize the receive buffer descriptors.
         */
        for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
                skb = dev_alloc_skb(ENET_RX_FRSIZE);
@@ -516,8 +524,8 @@ void fs_init_bds(struct net_device *dev)
                               dev->name);
                        break;
                }
+               skb_align(skb, ENET_RX_ALIGN);
                fep->rx_skbuff[i] = skb;
-               skb->dev = dev;
                CBDW_BUFADDR(bdp,
                        dma_map_single(fep->dev, skb->data,
                                L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
@@ -527,7 +535,7 @@ void fs_init_bds(struct net_device *dev)
                        ((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP));
        }
        /*
-        * if we failed, fillup remainder 
+        * if we failed, fillup remainder
         */
        for (; i < fep->rx_ring; i++, bdp++) {
                fep->rx_skbuff[i] = NULL;
@@ -535,7 +543,7 @@ void fs_init_bds(struct net_device *dev)
        }
 
        /*
-        * ...and the same for transmit.  
+        * ...and the same for transmit.
         */
        for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
                fep->tx_skbuff[i] = NULL;
@@ -549,11 +557,11 @@ void fs_cleanup_bds(struct net_device *dev)
 {
        struct fs_enet_private *fep = netdev_priv(dev);
        struct sk_buff *skb;
-       cbd_t *bdp;
+       cbd_t __iomem *bdp;
        int i;
 
        /*
-        * Reset SKB transmit buffers.  
+        * Reset SKB transmit buffers.
         */
        for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
                if ((skb = fep->tx_skbuff[i]) == NULL)
@@ -568,7 +576,7 @@ void fs_cleanup_bds(struct net_device *dev)
        }
 
        /*
-        * Reset SKB receive buffers 
+        * Reset SKB receive buffers
         */
        for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
                if ((skb = fep->rx_skbuff[i]) == NULL)
@@ -590,7 +598,7 @@ void fs_cleanup_bds(struct net_device *dev)
 static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
 {
        struct fs_enet_private *fep = netdev_priv(dev);
-       cbd_t *bdp;
+       cbd_t __iomem *bdp;
        int curidx;
        u16 sc;
        unsigned long flags;
@@ -598,7 +606,7 @@ static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
        spin_lock_irqsave(&fep->tx_lock, flags);
 
        /*
-        * Fill in a Tx ring entry 
+        * Fill in a Tx ring entry
         */
        bdp = fep->cur_tx;
 
@@ -617,19 +625,19 @@ static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
 
        curidx = bdp - fep->tx_bd_base;
        /*
-        * Clear all of the status flags. 
+        * Clear all of the status flags.
         */
        CBDC_SC(bdp, BD_ENET_TX_STATS);
 
        /*
-        * Save skb pointer. 
+        * Save skb pointer.
         */
        fep->tx_skbuff[curidx] = skb;
 
        fep->stats.tx_bytes += skb->len;
 
        /*
-        * Push the data cache so the CPM does not get stale memory data. 
+        * Push the data cache so the CPM does not get stale memory data.
         */
        CBDW_BUFADDR(bdp, dma_map_single(fep->dev,
                                skb->data, skb->len, DMA_TO_DEVICE));
@@ -638,7 +646,7 @@ static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
        dev->trans_start = jiffies;
 
        /*
-        * If this was the last BD in the ring, start at the beginning again. 
+        * If this was the last BD in the ring, start at the beginning again.
         */
        if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
                fep->cur_tx++;
@@ -667,12 +675,12 @@ static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
 }
 
 static int fs_request_irq(struct net_device *dev, int irq, const char *name,
-               irqreturn_t (*irqf)(int irq, void *dev_id, struct pt_regs *regs))
+               irq_handler_t irqf)
 {
        struct fs_enet_private *fep = netdev_priv(dev);
 
        (*fep->ops->pre_request_irq)(dev, irq);
-       return request_irq(irq, irqf, SA_SHIRQ, name, dev);
+       return request_irq(irq, irqf, IRQF_SHARED, name, dev);
 }
 
 static void fs_free_irq(struct net_device *dev, int irq)
@@ -683,35 +691,6 @@ static void fs_free_irq(struct net_device *dev, int irq)
        (*fep->ops->post_free_irq)(dev, irq);
 }
 
-/**********************************************************************************/
-
-/* This interrupt occurs when the PHY detects a link change. */
-static irqreturn_t
-fs_mii_link_interrupt(int irq, void *dev_id, struct pt_regs *regs)
-{
-       struct net_device *dev = dev_id;
-       struct fs_enet_private *fep;
-       const struct fs_platform_info *fpi;
-
-       fep = netdev_priv(dev);
-       fpi = fep->fpi;
-
-       /*
-        * Acknowledge the interrupt if possible. If we have not
-        * found the PHY yet we can't process or acknowledge the
-        * interrupt now. Instead we ignore this interrupt for now,
-        * which we can do since it is edge triggered. It will be
-        * acknowledged later by fs_enet_open().
-        */
-       if (!fep->phy)
-               return IRQ_NONE;
-
-       fs_mii_ack_int(dev);
-       fs_mii_link_status_change_check(dev, 0);
-
-       return IRQ_HANDLED;
-}
-
 static void fs_timeout(struct net_device *dev)
 {
        struct fs_enet_private *fep = netdev_priv(dev);
@@ -723,10 +702,13 @@ static void fs_timeout(struct net_device *dev)
        spin_lock_irqsave(&fep->lock, flags);
 
        if (dev->flags & IFF_UP) {
+               phy_stop(fep->phydev);
                (*fep->ops->stop)(dev);
                (*fep->ops->restart)(dev);
+               phy_start(fep->phydev);
        }
 
+       phy_start(fep->phydev);
        wake = fep->tx_free && !(CBDR_SC(fep->cur_tx) & BD_ENET_TX_READY);
        spin_unlock_irqrestore(&fep->lock, flags);
 
@@ -734,35 +716,117 @@ static void fs_timeout(struct net_device *dev)
                netif_wake_queue(dev);
 }
 
+/*-----------------------------------------------------------------------------
+ *  generic link-change handler - should be sufficient for most cases
+ *-----------------------------------------------------------------------------*/
+static void generic_adjust_link(struct  net_device *dev)
+{
+       struct fs_enet_private *fep = netdev_priv(dev);
+       struct phy_device *phydev = fep->phydev;
+       int new_state = 0;
+
+       if (phydev->link) {
+               /* adjust to duplex mode */
+               if (phydev->duplex != fep->oldduplex) {
+                       new_state = 1;
+                       fep->oldduplex = phydev->duplex;
+               }
+
+               if (phydev->speed != fep->oldspeed) {
+                       new_state = 1;
+                       fep->oldspeed = phydev->speed;
+               }
+
+               if (!fep->oldlink) {
+                       new_state = 1;
+                       fep->oldlink = 1;
+                       netif_schedule(dev);
+                       netif_carrier_on(dev);
+                       netif_start_queue(dev);
+               }
+
+               if (new_state)
+                       fep->ops->restart(dev);
+       } else if (fep->oldlink) {
+               new_state = 1;
+               fep->oldlink = 0;
+               fep->oldspeed = 0;
+               fep->oldduplex = -1;
+               netif_carrier_off(dev);
+               netif_stop_queue(dev);
+       }
+
+       if (new_state && netif_msg_link(fep))
+               phy_print_status(phydev);
+}
+
+
+static void fs_adjust_link(struct net_device *dev)
+{
+       struct fs_enet_private *fep = netdev_priv(dev);
+       unsigned long flags;
+
+       spin_lock_irqsave(&fep->lock, flags);
+
+       if(fep->ops->adjust_link)
+               fep->ops->adjust_link(dev);
+       else
+               generic_adjust_link(dev);
+
+       spin_unlock_irqrestore(&fep->lock, flags);
+}
+
+static int fs_init_phy(struct net_device *dev)
+{
+       struct fs_enet_private *fep = netdev_priv(dev);
+       struct phy_device *phydev;
+
+       fep->oldlink = 0;
+       fep->oldspeed = 0;
+       fep->oldduplex = -1;
+       if(fep->fpi->bus_id)
+               phydev = phy_connect(dev, fep->fpi->bus_id, &fs_adjust_link, 0,
+                               PHY_INTERFACE_MODE_MII);
+       else {
+               printk("No phy bus ID specified in BSP code\n");
+               return -EINVAL;
+       }
+       if (IS_ERR(phydev)) {
+               printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
+               return PTR_ERR(phydev);
+       }
+
+       fep->phydev = phydev;
+
+       return 0;
+}
+
 static int fs_enet_open(struct net_device *dev)
 {
        struct fs_enet_private *fep = netdev_priv(dev);
-       const struct fs_platform_info *fpi = fep->fpi;
        int r;
+       int err;
+
+       if (fep->fpi->use_napi)
+               napi_enable(&fep->napi);
 
        /* Install our interrupt handler. */
        r = fs_request_irq(dev, fep->interrupt, "fs_enet-mac", fs_enet_interrupt);
        if (r != 0) {
                printk(KERN_ERR DRV_MODULE_NAME
-                      ": %s Could not allocate FEC IRQ!", dev->name);
+                      ": %s Could not allocate FS_ENET IRQ!", dev->name);
+               if (fep->fpi->use_napi)
+                       napi_disable(&fep->napi);
                return -EINVAL;
        }
 
-       /* Install our phy interrupt handler */
-       if (fpi->phy_irq != -1) {
-
-               r = fs_request_irq(dev, fpi->phy_irq, "fs_enet-phy", fs_mii_link_interrupt);
-               if (r != 0) {
-                       printk(KERN_ERR DRV_MODULE_NAME
-                              ": %s Could not allocate PHY IRQ!", dev->name);
-                       fs_free_irq(dev, fep->interrupt);
-                       return -EINVAL;
-               }
+       err = fs_init_phy(dev);
+       if (err) {
+               if (fep->fpi->use_napi)
+                       napi_disable(&fep->napi);
+               return err;
        }
-
-       fs_mii_startup(dev);
-       netif_carrier_off(dev);
-       fs_mii_link_status_change_check(dev, 1);
+       phy_start(fep->phydev);
 
        return 0;
 }
@@ -770,20 +834,22 @@ static int fs_enet_open(struct net_device *dev)
 static int fs_enet_close(struct net_device *dev)
 {
        struct fs_enet_private *fep = netdev_priv(dev);
-       const struct fs_platform_info *fpi = fep->fpi;
        unsigned long flags;
 
        netif_stop_queue(dev);
        netif_carrier_off(dev);
-       fs_mii_shutdown(dev);
+       napi_disable(&fep->napi);
+       phy_stop(fep->phydev);
 
        spin_lock_irqsave(&fep->lock, flags);
+       spin_lock(&fep->tx_lock);
        (*fep->ops->stop)(dev);
+       spin_unlock(&fep->tx_lock);
        spin_unlock_irqrestore(&fep->lock, flags);
 
        /* release any irqs */
-       if (fpi->phy_irq != -1)
-               fs_free_irq(dev, fpi->phy_irq);
+       phy_disconnect(fep->phydev);
+       fep->phydev = NULL;
        fs_free_irq(dev, fep->interrupt);
 
        return 0;
@@ -831,33 +897,19 @@ static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs,
 static int fs_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
 {
        struct fs_enet_private *fep = netdev_priv(dev);
-       unsigned long flags;
-       int rc;
-
-       spin_lock_irqsave(&fep->lock, flags);
-       rc = mii_ethtool_gset(&fep->mii_if, cmd);
-       spin_unlock_irqrestore(&fep->lock, flags);
-
-       return rc;
+       return phy_ethtool_gset(fep->phydev, cmd);
 }
 
 static int fs_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
 {
        struct fs_enet_private *fep = netdev_priv(dev);
-       unsigned long flags;
-       int rc;
-
-       spin_lock_irqsave(&fep->lock, flags);
-       rc = mii_ethtool_sset(&fep->mii_if, cmd);
-       spin_unlock_irqrestore(&fep->lock, flags);
-
-       return rc;
+       phy_ethtool_sset(fep->phydev, cmd);
+       return 0;
 }
 
 static int fs_nway_reset(struct net_device *dev)
 {
-       struct fs_enet_private *fep = netdev_priv(dev);
-       return mii_nway_restart(&fep->mii_if);
+       return 0;
 }
 
 static u32 fs_get_msglevel(struct net_device *dev)
@@ -872,7 +924,7 @@ static void fs_set_msglevel(struct net_device *dev, u32 value)
        fep->msg_enable = value;
 }
 
-static struct ethtool_ops fs_ethtool_ops = {
+static const struct ethtool_ops fs_ethtool_ops = {
        .get_drvinfo = fs_get_drvinfo,
        .get_regs_len = fs_get_regs_len,
        .get_settings = fs_get_settings,
@@ -881,9 +933,7 @@ static struct ethtool_ops fs_ethtool_ops = {
        .get_link = ethtool_op_get_link,
        .get_msglevel = fs_get_msglevel,
        .set_msglevel = fs_set_msglevel,
-       .get_tx_csum = ethtool_op_get_tx_csum,
        .set_tx_csum = ethtool_op_set_tx_csum,  /* local! */
-       .get_sg = ethtool_op_get_sg,
        .set_sg = ethtool_op_set_sg,
        .get_regs = fs_get_regs,
 };
@@ -899,7 +949,7 @@ static int fs_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
                return -EINVAL;
 
        spin_lock_irqsave(&fep->lock, flags);
-       rc = generic_mii_ioctl(&fep->mii_if, mii, cmd, NULL);
+       rc = phy_mii_ioctl(fep->phydev, mii, cmd);
        spin_unlock_irqrestore(&fep->lock, flags);
        return rc;
 }
@@ -907,13 +957,15 @@ static int fs_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
 extern int fs_mii_connect(struct net_device *dev);
 extern void fs_mii_disconnect(struct net_device *dev);
 
+#ifndef CONFIG_PPC_CPM_NEW_BINDING
 static struct net_device *fs_init_instance(struct device *dev,
-               const struct fs_platform_info *fpi)
+               struct fs_platform_info *fpi)
 {
        struct net_device *ndev = NULL;
        struct fs_enet_private *fep = NULL;
        int privsize, i, r, err = 0, registered = 0;
 
+       fpi->fs_no = fs_get_id(fpi);
        /* guard */
        if ((unsigned int)fpi->fs_no >= FS_MAX_INDEX)
                return ERR_PTR(-EINVAL);
@@ -926,16 +978,14 @@ static struct net_device *fs_init_instance(struct device *dev,
                err = -ENOMEM;
                goto err;
        }
-       SET_MODULE_OWNER(ndev);
 
        fep = netdev_priv(ndev);
-       memset(fep, 0, privsize);       /* clear everything */
 
        fep->dev = dev;
        dev_set_drvdata(dev, ndev);
        fep->fpi = fpi;
        if (fpi->init_ioports)
-               fpi->init_ioports();
+               fpi->init_ioports((struct fs_platform_info *)fpi);
 
 #ifdef CONFIG_FS_ENET_HAS_FEC
        if (fs_get_fec_index(fpi->fs_no) >= 0)
@@ -943,7 +993,7 @@ static struct net_device *fs_init_instance(struct device *dev,
 #endif
 
 #ifdef CONFIG_FS_ENET_HAS_SCC
-       if (fs_get_scc_index(fpi->fs_no) >=0 )
+       if (fs_get_scc_index(fpi->fs_no) >=0)
                fep->ops = &fs_scc_ops;
 #endif
 
@@ -978,13 +1028,13 @@ static struct net_device *fs_init_instance(struct device *dev,
        spin_lock_init(&fep->tx_lock);
 
        /*
-        * Set the Ethernet address. 
+        * Set the Ethernet address.
         */
        for (i = 0; i < 6; i++)
                ndev->dev_addr[i] = fpi->macaddr[i];
-       
+
        r = (*fep->ops->allocate_bd)(ndev);
-       
+
        if (fep->ring_base == NULL) {
                printk(KERN_ERR DRV_MODULE_NAME
                       ": %s buffer descriptor alloc failed (%d).\n", ndev->name, r);
@@ -1003,7 +1053,7 @@ static struct net_device *fs_init_instance(struct device *dev,
        fep->rx_ring = fpi->rx_ring;
 
        /*
-        * The FEC Ethernet specific entries in the device structure. 
+        * The FEC Ethernet specific entries in the device structure.
         */
        ndev->open = fs_enet_open;
        ndev->hard_start_xmit = fs_enet_start_xmit;
@@ -1012,10 +1062,14 @@ static struct net_device *fs_init_instance(struct device *dev,
        ndev->stop = fs_enet_close;
        ndev->get_stats = fs_enet_get_stats;
        ndev->set_multicast_list = fs_set_multicast_list;
-       if (fpi->use_napi) {
-               ndev->poll = fs_enet_rx_napi;
-               ndev->weight = fpi->napi_weight;
-       }
+
+#ifdef CONFIG_NET_POLL_CONTROLLER
+       ndev->poll_controller = fs_enet_netpoll;
+#endif
+
+       netif_napi_add(ndev, &fep->napi,
+                      fs_enet_rx_napi, fpi->napi_weight);
+
        ndev->ethtool_ops = &fs_ethtool_ops;
        ndev->do_ioctl = fs_ioctl;
 
@@ -1031,18 +1085,11 @@ static struct net_device *fs_init_instance(struct device *dev,
        }
        registered = 1;
 
-       err = fs_mii_connect(ndev);
-       if (err != 0) {
-               printk(KERN_ERR DRV_MODULE_NAME
-                      ": %s fs_mii_connect failed.\n", ndev->name);
-               goto err;
-       }
 
        return ndev;
 
-      err:
+err:
        if (ndev != NULL) {
-
                if (registered)
                        unregister_netdev(ndev);
 
@@ -1074,12 +1121,10 @@ static int fs_cleanup_instance(struct net_device *ndev)
 
        fpi = fep->fpi;
 
-       fs_mii_disconnect(ndev);
-
        unregister_netdev(ndev);
 
        dma_free_coherent(fep->dev, (fpi->tx_ring + fpi->rx_ring) * sizeof(cbd_t),
-                         fep->ring_base, fep->ring_mem_addr);
+                         (void __force *)fep->ring_base, fep->ring_mem_addr);
 
        /* reset it */
        (*fep->ops->cleanup_data)(ndev);
@@ -1094,43 +1139,260 @@ static int fs_cleanup_instance(struct net_device *ndev)
 
        return 0;
 }
+#endif
 
 /**************************************************************************************/
 
 /* handy pointer to the immap */
-void *fs_enet_immap = NULL;
+void __iomem *fs_enet_immap = NULL;
 
 static int setup_immap(void)
 {
-       phys_addr_t paddr = 0;
-       unsigned long size = 0;
-
 #ifdef CONFIG_CPM1
-       paddr = IMAP_ADDR;
-       size = 0x10000; /* map 64K */
-#endif
-
-#ifdef CONFIG_CPM2
-       paddr = CPM_MAP_ADDR;
-       size = 0x40000; /* map 256 K */
+       fs_enet_immap = ioremap(IMAP_ADDR, 0x4000);
+       WARN_ON(!fs_enet_immap);
+#elif defined(CONFIG_CPM2)
+       fs_enet_immap = cpm2_immr;
 #endif
-       fs_enet_immap = ioremap(paddr, size);
-       if (fs_enet_immap == NULL)
-               return -EBADF;  /* XXX ahem; maybe just BUG_ON? */
 
        return 0;
 }
 
 static void cleanup_immap(void)
 {
-       if (fs_enet_immap != NULL) {
-               iounmap(fs_enet_immap);
-               fs_enet_immap = NULL;
-       }
+#if defined(CONFIG_CPM1)
+       iounmap(fs_enet_immap);
+#endif
 }
 
 /**************************************************************************************/
 
+#ifdef CONFIG_PPC_CPM_NEW_BINDING
+static int __devinit find_phy(struct device_node *np,
+                              struct fs_platform_info *fpi)
+{
+       struct device_node *phynode, *mdionode;
+       struct resource res;
+       int ret = 0, len;
+
+       const u32 *data = of_get_property(np, "phy-handle", &len);
+       if (!data || len != 4)
+               return -EINVAL;
+
+       phynode = of_find_node_by_phandle(*data);
+       if (!phynode)
+               return -EINVAL;
+
+       mdionode = of_get_parent(phynode);
+       if (!mdionode)
+               goto out_put_phy;
+
+       ret = of_address_to_resource(mdionode, 0, &res);
+       if (ret)
+               goto out_put_mdio;
+
+       data = of_get_property(phynode, "reg", &len);
+       if (!data || len != 4)
+               goto out_put_mdio;
+
+       snprintf(fpi->bus_id, 16, PHY_ID_FMT, res.start, *data);
+
+out_put_mdio:
+       of_node_put(mdionode);
+out_put_phy:
+       of_node_put(phynode);
+       return ret;
+}
+
+#ifdef CONFIG_FS_ENET_HAS_FEC
+#define IS_FEC(match) ((match)->data == &fs_fec_ops)
+#else
+#define IS_FEC(match) 0
+#endif
+
+static int __devinit fs_enet_probe(struct of_device *ofdev,
+                                   const struct of_device_id *match)
+{
+       struct net_device *ndev;
+       struct fs_enet_private *fep;
+       struct fs_platform_info *fpi;
+       const u32 *data;
+       const u8 *mac_addr;
+       int privsize, len, ret = -ENODEV;
+
+       fpi = kzalloc(sizeof(*fpi), GFP_KERNEL);
+       if (!fpi)
+               return -ENOMEM;
+
+       if (!IS_FEC(match)) {
+               data = of_get_property(ofdev->node, "fsl,cpm-command", &len);
+               if (!data || len != 4)
+                       goto out_free_fpi;
+
+               fpi->cp_command = *data;
+       }
+
+       fpi->rx_ring = 32;
+       fpi->tx_ring = 32;
+       fpi->rx_copybreak = 240;
+       fpi->use_napi = 1;
+       fpi->napi_weight = 17;
+
+       ret = find_phy(ofdev->node, fpi);
+       if (ret)
+               goto out_free_fpi;
+
+       privsize = sizeof(*fep) +
+                  sizeof(struct sk_buff **) *
+                  (fpi->rx_ring + fpi->tx_ring);
+
+       ndev = alloc_etherdev(privsize);
+       if (!ndev) {
+               ret = -ENOMEM;
+               goto out_free_fpi;
+       }
+
+       dev_set_drvdata(&ofdev->dev, ndev);
+
+       fep = netdev_priv(ndev);
+       fep->dev = &ofdev->dev;
+       fep->ndev = ndev;
+       fep->fpi = fpi;
+       fep->ops = match->data;
+
+       ret = fep->ops->setup_data(ndev);
+       if (ret)
+               goto out_free_dev;
+
+       fep->rx_skbuff = (struct sk_buff **)&fep[1];
+       fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
+
+       spin_lock_init(&fep->lock);
+       spin_lock_init(&fep->tx_lock);
+
+       mac_addr = of_get_mac_address(ofdev->node);
+       if (mac_addr)
+               memcpy(ndev->dev_addr, mac_addr, 6);
+
+       ret = fep->ops->allocate_bd(ndev);
+       if (ret)
+               goto out_cleanup_data;
+
+       fep->rx_bd_base = fep->ring_base;
+       fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
+
+       fep->tx_ring = fpi->tx_ring;
+       fep->rx_ring = fpi->rx_ring;
+
+       ndev->open = fs_enet_open;
+       ndev->hard_start_xmit = fs_enet_start_xmit;
+       ndev->tx_timeout = fs_timeout;
+       ndev->watchdog_timeo = 2 * HZ;
+       ndev->stop = fs_enet_close;
+       ndev->get_stats = fs_enet_get_stats;
+       ndev->set_multicast_list = fs_set_multicast_list;
+
+       if (fpi->use_napi)
+               netif_napi_add(ndev, &fep->napi, fs_enet_rx_napi,
+                              fpi->napi_weight);
+
+       ndev->ethtool_ops = &fs_ethtool_ops;
+       ndev->do_ioctl = fs_ioctl;
+
+       init_timer(&fep->phy_timer_list);
+
+       netif_carrier_off(ndev);
+
+       ret = register_netdev(ndev);
+       if (ret)
+               goto out_free_bd;
+
+       printk(KERN_INFO "%s: fs_enet: %02x:%02x:%02x:%02x:%02x:%02x\n",
+              ndev->name,
+              ndev->dev_addr[0], ndev->dev_addr[1], ndev->dev_addr[2],
+              ndev->dev_addr[3], ndev->dev_addr[4], ndev->dev_addr[5]);
+
+       return 0;
+
+out_free_bd:
+       fep->ops->free_bd(ndev);
+out_cleanup_data:
+       fep->ops->cleanup_data(ndev);
+out_free_dev:
+       free_netdev(ndev);
+       dev_set_drvdata(&ofdev->dev, NULL);
+out_free_fpi:
+       kfree(fpi);
+       return ret;
+}
+
+static int fs_enet_remove(struct of_device *ofdev)
+{
+       struct net_device *ndev = dev_get_drvdata(&ofdev->dev);
+       struct fs_enet_private *fep = netdev_priv(ndev);
+
+       unregister_netdev(ndev);
+
+       fep->ops->free_bd(ndev);
+       fep->ops->cleanup_data(ndev);
+       dev_set_drvdata(fep->dev, NULL);
+
+       free_netdev(ndev);
+       return 0;
+}
+
+static struct of_device_id fs_enet_match[] = {
+#ifdef CONFIG_FS_ENET_HAS_SCC
+       {
+               .compatible = "fsl,cpm1-scc-enet",
+               .data = (void *)&fs_scc_ops,
+       },
+#endif
+#ifdef CONFIG_FS_ENET_HAS_FCC
+       {
+               .compatible = "fsl,cpm2-fcc-enet",
+               .data = (void *)&fs_fcc_ops,
+       },
+#endif
+#ifdef CONFIG_FS_ENET_HAS_FEC
+       {
+               .compatible = "fsl,pq1-fec-enet",
+               .data = (void *)&fs_fec_ops,
+       },
+#endif
+       {}
+};
+
+static struct of_platform_driver fs_enet_driver = {
+       .name   = "fs_enet",
+       .match_table = fs_enet_match,
+       .probe = fs_enet_probe,
+       .remove = fs_enet_remove,
+};
+
+static int __init fs_init(void)
+{
+       int r = setup_immap();
+       if (r != 0)
+               return r;
+
+       r = of_register_platform_driver(&fs_enet_driver);
+       if (r != 0)
+               goto out;
+
+       return 0;
+
+out:
+       cleanup_immap();
+       return r;
+}
+
+static void __exit fs_cleanup(void)
+{
+       of_unregister_platform_driver(&fs_enet_driver);
+       cleanup_immap();
+}
+#else
 static int __devinit fs_enet_probe(struct device *dev)
 {
        struct net_device *ndev;
@@ -1197,23 +1459,44 @@ static int __init fs_init(void)
        r = setup_immap();
        if (r != 0)
                return r;
-       r = driver_register(&fs_enet_fec_driver);
+
+#ifdef CONFIG_FS_ENET_HAS_FCC
+       /* let's insert mii stuff */
+       r = fs_enet_mdio_bb_init();
+
+       if (r != 0) {
+               printk(KERN_ERR DRV_MODULE_NAME
+                       "BB PHY init failed.\n");
+               return r;
+       }
+       r = driver_register(&fs_enet_fcc_driver);
        if (r != 0)
                goto err;
+#endif
 
-       r = driver_register(&fs_enet_fcc_driver);
+#ifdef CONFIG_FS_ENET_HAS_FEC
+       r =  fs_enet_mdio_fec_init();
+       if (r != 0) {
+               printk(KERN_ERR DRV_MODULE_NAME
+                       "FEC PHY init failed.\n");
+               return r;
+       }
+
+       r = driver_register(&fs_enet_fec_driver);
        if (r != 0)
                goto err;
+#endif
 
+#ifdef CONFIG_FS_ENET_HAS_SCC
        r = driver_register(&fs_enet_scc_driver);
        if (r != 0)
                goto err;
+#endif
 
        return 0;
 err:
        cleanup_immap();
        return r;
-       
 }
 
 static void __exit fs_cleanup(void)
@@ -1223,6 +1506,16 @@ static void __exit fs_cleanup(void)
        driver_unregister(&fs_enet_scc_driver);
        cleanup_immap();
 }
+#endif
+
+#ifdef CONFIG_NET_POLL_CONTROLLER
+static void fs_enet_netpoll(struct net_device *dev)
+{
+       disable_irq(dev->irq);
+       fs_enet_interrupt(dev->irq, dev, NULL);
+       enable_irq(dev->irq);
+}
+#endif
 
 /**************************************************************************************/