Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[cascardo/linux.git] / drivers / net / ethernet / marvell / pxa168_eth.c
index 8f5aa7c..af829c5 100644 (file)
  * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <linux/dma-mapping.h>
-#include <linux/in.h>
-#include <linux/ip.h>
-#include <linux/tcp.h>
-#include <linux/udp.h>
-#include <linux/etherdevice.h>
 #include <linux/bitops.h>
+#include <linux/clk.h>
 #include <linux/delay.h>
+#include <linux/dma-mapping.h>
+#include <linux/etherdevice.h>
 #include <linux/ethtool.h>
-#include <linux/platform_device.h>
-#include <linux/module.h>
+#include <linux/in.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/ip.h>
 #include <linux/kernel.h>
-#include <linux/workqueue.h>
-#include <linux/clk.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_net.h>
 #include <linux/phy.h>
-#include <linux/io.h>
-#include <linux/interrupt.h>
+#include <linux/platform_device.h>
+#include <linux/pxa168_eth.h>
+#include <linux/tcp.h>
 #include <linux/types.h>
+#include <linux/udp.h>
+#include <linux/workqueue.h>
+
 #include <asm/pgtable.h>
 #include <asm/cacheflush.h>
-#include <linux/pxa168_eth.h>
 
 #define DRIVER_NAME    "pxa168-eth"
 #define DRIVER_VERSION "0.3"
@@ -58,6 +61,8 @@
 #define PORT_COMMAND           0x0410
 #define PORT_STATUS            0x0418
 #define HTPR                   0x0428
+#define MAC_ADDR_LOW           0x0430
+#define MAC_ADDR_HIGH          0x0438
 #define SDMA_CONFIG            0x0440
 #define SDMA_CMD               0x0448
 #define INT_CAUSE              0x0450
 #define SDMA_CMD_ERD           (1 << 7)
 
 /* Bit definitions of the Port Config Reg */
+#define PCR_DUPLEX_FULL                (1 << 15)
 #define PCR_HS                 (1 << 12)
 #define PCR_EN                 (1 << 7)
 #define PCR_PM                 (1 << 0)
 /* Bit definitions of the Port Config Extend Reg */
 #define PCXR_2BSM              (1 << 28)
 #define PCXR_DSCP_EN           (1 << 21)
+#define PCXR_RMII_EN           (1 << 20)
+#define PCXR_AN_SPEED_DIS      (1 << 19)
+#define PCXR_SPEED_100         (1 << 18)
 #define PCXR_MFL_1518          (0 << 14)
 #define PCXR_MFL_1536          (1 << 14)
 #define PCXR_MFL_2048          (2 << 14)
 #define PCXR_MFL_64K           (3 << 14)
+#define PCXR_FLOWCTL_DIS       (1 << 12)
 #define PCXR_FLP               (1 << 11)
+#define PCXR_AN_FLOWCTL_DIS    (1 << 10)
+#define PCXR_AN_DUPLEX_DIS     (1 << 9)
 #define PCXR_PRIO_TX_OFF       3
 #define PCXR_TX_HIGH_PRI       (7 << PCXR_PRIO_TX_OFF)
 
 /* Bit definitions for Port status */
 #define PORT_SPEED_100         (1 << 0)
 #define FULL_DUPLEX            (1 << 1)
-#define FLOW_CONTROL_ENABLED   (1 << 2)
+#define FLOW_CONTROL_DISABLED  (1 << 2)
 #define LINK_UP                        (1 << 3)
 
 /* Bit definitions for work to be done */
-#define WORK_LINK              (1 << 0)
 #define WORK_TX_DONE           (1 << 1)
 
 /*
@@ -191,6 +202,10 @@ struct tx_desc {
 
 struct pxa168_eth_private {
        int port_num;           /* User Ethernet port number    */
+       int phy_addr;
+       int phy_speed;
+       int phy_duplex;
+       phy_interface_t phy_intf;
 
        int rx_resource_err;    /* Rx ring resource error flag */
 
@@ -263,11 +278,11 @@ enum hash_table_entry {
 static int pxa168_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
 static int pxa168_set_settings(struct net_device *dev, struct ethtool_cmd *cmd);
 static int pxa168_init_hw(struct pxa168_eth_private *pep);
+static int pxa168_init_phy(struct net_device *dev);
 static void eth_port_reset(struct net_device *dev);
 static void eth_port_start(struct net_device *dev);
 static int pxa168_eth_open(struct net_device *dev);
 static int pxa168_eth_stop(struct net_device *dev);
-static int ethernet_phy_setup(struct net_device *dev);
 
 static inline u32 rdl(struct pxa168_eth_private *pep, int offset)
 {
@@ -296,27 +311,7 @@ static void abort_dma(struct pxa168_eth_private *pep)
        } while (max_retries-- > 0 && delay <= 0);
 
        if (max_retries <= 0)
-               printk(KERN_ERR "%s : DMA Stuck\n", __func__);
-}
-
-static int ethernet_phy_get(struct pxa168_eth_private *pep)
-{
-       unsigned int reg_data;
-
-       reg_data = rdl(pep, PHY_ADDRESS);
-
-       return (reg_data >> (5 * pep->port_num)) & 0x1f;
-}
-
-static void ethernet_phy_set_addr(struct pxa168_eth_private *pep, int phy_addr)
-{
-       u32 reg_data;
-       int addr_shift = 5 * pep->port_num;
-
-       reg_data = rdl(pep, PHY_ADDRESS);
-       reg_data &= ~(0x1f << addr_shift);
-       reg_data |= (phy_addr & 0x1f) << addr_shift;
-       wrl(pep, PHY_ADDRESS, reg_data);
+               netdev_err(pep->dev, "%s : DMA Stuck\n", __func__);
 }
 
 static void rxq_refill(struct net_device *dev)
@@ -507,9 +502,10 @@ static int add_del_hash_entry(struct pxa168_eth_private *pep,
 
        if (i == HOP_NUMBER) {
                if (!del) {
-                       printk(KERN_INFO "%s: table section is full, need to "
-                                       "move to 16kB implementation?\n",
-                                        __FILE__);
+                       netdev_info(pep->dev,
+                                   "%s: table section is full, need to "
+                                   "move to 16kB implementation?\n",
+                                   __FILE__);
                        return -ENOSPC;
                } else
                        return 0;
@@ -600,16 +596,42 @@ static void pxa168_eth_set_rx_mode(struct net_device *dev)
                update_hash_table_mac_address(pep, NULL, ha->addr);
 }
 
+static void pxa168_eth_get_mac_address(struct net_device *dev,
+                                      unsigned char *addr)
+{
+       struct pxa168_eth_private *pep = netdev_priv(dev);
+       unsigned int mac_h = rdl(pep, MAC_ADDR_HIGH);
+       unsigned int mac_l = rdl(pep, MAC_ADDR_LOW);
+
+       addr[0] = (mac_h >> 24) & 0xff;
+       addr[1] = (mac_h >> 16) & 0xff;
+       addr[2] = (mac_h >> 8) & 0xff;
+       addr[3] = mac_h & 0xff;
+       addr[4] = (mac_l >> 8) & 0xff;
+       addr[5] = mac_l & 0xff;
+}
+
 static int pxa168_eth_set_mac_address(struct net_device *dev, void *addr)
 {
        struct sockaddr *sa = addr;
        struct pxa168_eth_private *pep = netdev_priv(dev);
        unsigned char oldMac[ETH_ALEN];
+       u32 mac_h, mac_l;
 
        if (!is_valid_ether_addr(sa->sa_data))
                return -EADDRNOTAVAIL;
        memcpy(oldMac, dev->dev_addr, ETH_ALEN);
        memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
+
+       mac_h = dev->dev_addr[0] << 24;
+       mac_h |= dev->dev_addr[1] << 16;
+       mac_h |= dev->dev_addr[2] << 8;
+       mac_h |= dev->dev_addr[3];
+       mac_l = dev->dev_addr[4] << 8;
+       mac_l |= dev->dev_addr[5];
+       wrl(pep, MAC_ADDR_HIGH, mac_h);
+       wrl(pep, MAC_ADDR_LOW, mac_l);
+
        netif_addr_lock_bh(dev);
        update_hash_table_mac_address(pep, oldMac, dev->dev_addr);
        netif_addr_unlock_bh(dev);
@@ -622,14 +644,7 @@ static void eth_port_start(struct net_device *dev)
        struct pxa168_eth_private *pep = netdev_priv(dev);
        int tx_curr_desc, rx_curr_desc;
 
-       /* Perform PHY reset, if there is a PHY. */
-       if (pep->phy != NULL) {
-               struct ethtool_cmd cmd;
-
-               pxa168_get_settings(pep->dev, &cmd);
-               phy_init_hw(pep->phy);
-               pxa168_set_settings(pep->dev, &cmd);
-       }
+       phy_start(pep->phy);
 
        /* Assignment of Tx CTRP of given queue */
        tx_curr_desc = pep->tx_curr_desc_q;
@@ -684,6 +699,8 @@ static void eth_port_reset(struct net_device *dev)
        val = rdl(pep, PORT_CONFIG);
        val &= ~PCR_EN;
        wrl(pep, PORT_CONFIG, val);
+
+       phy_stop(pep->phy);
 }
 
 /*
@@ -726,7 +743,7 @@ static int txq_reclaim(struct net_device *dev, int force)
 
                if (cmd_sts & TX_ERROR) {
                        if (net_ratelimit())
-                               printk(KERN_ERR "%s: Error in TX\n", dev->name);
+                               netdev_err(dev, "Error in TX\n");
                        dev->stats.tx_errors++;
                }
                dma_unmap_single(NULL, addr, count, DMA_TO_DEVICE);
@@ -743,8 +760,7 @@ static void pxa168_eth_tx_timeout(struct net_device *dev)
 {
        struct pxa168_eth_private *pep = netdev_priv(dev);
 
-       printk(KERN_INFO "%s: TX timeout  desc_count %d\n",
-              dev->name, pep->tx_desc_count);
+       netdev_info(dev, "TX timeout  desc_count %d\n", pep->tx_desc_count);
 
        schedule_work(&pep->tx_timeout_task);
 }
@@ -814,9 +830,8 @@ static int rxq_process(struct net_device *dev, int budget)
                        if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
                            (RX_FIRST_DESC | RX_LAST_DESC)) {
                                if (net_ratelimit())
-                                       printk(KERN_ERR
-                                              "%s: Rx pkt on multiple desc\n",
-                                              dev->name);
+                                       netdev_err(dev,
+                                                  "Rx pkt on multiple desc\n");
                        }
                        if (cmd_sts & RX_ERROR)
                                stats->rx_errors++;
@@ -853,44 +868,9 @@ static int pxa168_eth_collect_events(struct pxa168_eth_private *pep,
        }
        if (icr & ICR_RXBUF)
                ret = 1;
-       if (icr & ICR_MII_CH) {
-               pep->work_todo |= WORK_LINK;
-               ret = 1;
-       }
        return ret;
 }
 
-static void handle_link_event(struct pxa168_eth_private *pep)
-{
-       struct net_device *dev = pep->dev;
-       u32 port_status;
-       int speed;
-       int duplex;
-       int fc;
-
-       port_status = rdl(pep, PORT_STATUS);
-       if (!(port_status & LINK_UP)) {
-               if (netif_carrier_ok(dev)) {
-                       printk(KERN_INFO "%s: link down\n", dev->name);
-                       netif_carrier_off(dev);
-                       txq_reclaim(dev, 1);
-               }
-               return;
-       }
-       if (port_status & PORT_SPEED_100)
-               speed = 100;
-       else
-               speed = 10;
-
-       duplex = (port_status & FULL_DUPLEX) ? 1 : 0;
-       fc = (port_status & FLOW_CONTROL_ENABLED) ? 1 : 0;
-       printk(KERN_INFO "%s: link up, %d Mb/s, %s duplex, "
-              "flow control %sabled\n", dev->name,
-              speed, duplex ? "full" : "half", fc ? "en" : "dis");
-       if (!netif_carrier_ok(dev))
-               netif_carrier_on(dev);
-}
-
 static irqreturn_t pxa168_eth_int_handler(int irq, void *dev_id)
 {
        struct net_device *dev = (struct net_device *)dev_id;
@@ -948,8 +928,11 @@ static int set_port_config_ext(struct pxa168_eth_private *pep)
                skb_size = PCXR_MFL_64K;
 
        /* Extended Port Configuration */
-       wrl(pep,
-           PORT_CONFIG_EXT, PCXR_2BSM | /* Two byte prefix aligns IP hdr */
+       wrl(pep, PORT_CONFIG_EXT,
+           PCXR_AN_SPEED_DIS |          /* Disable HW AN */
+           PCXR_AN_DUPLEX_DIS |
+           PCXR_AN_FLOWCTL_DIS |
+           PCXR_2BSM |                  /* Two byte prefix aligns IP hdr */
            PCXR_DSCP_EN |               /* Enable DSCP in IP */
            skb_size | PCXR_FLP |        /* do not force link pass */
            PCXR_TX_HIGH_PRI);           /* Transmit - high priority queue */
@@ -957,6 +940,69 @@ static int set_port_config_ext(struct pxa168_eth_private *pep)
        return 0;
 }
 
+static void pxa168_eth_adjust_link(struct net_device *dev)
+{
+       struct pxa168_eth_private *pep = netdev_priv(dev);
+       struct phy_device *phy = pep->phy;
+       u32 cfg, cfg_o = rdl(pep, PORT_CONFIG);
+       u32 cfgext, cfgext_o = rdl(pep, PORT_CONFIG_EXT);
+
+       cfg = cfg_o & ~PCR_DUPLEX_FULL;
+       cfgext = cfgext_o & ~(PCXR_SPEED_100 | PCXR_FLOWCTL_DIS | PCXR_RMII_EN);
+
+       if (phy->interface == PHY_INTERFACE_MODE_RMII)
+               cfgext |= PCXR_RMII_EN;
+       if (phy->speed == SPEED_100)
+               cfgext |= PCXR_SPEED_100;
+       if (phy->duplex)
+               cfg |= PCR_DUPLEX_FULL;
+       if (!phy->pause)
+               cfgext |= PCXR_FLOWCTL_DIS;
+
+       /* Bail out if there has nothing changed */
+       if (cfg == cfg_o && cfgext == cfgext_o)
+               return;
+
+       wrl(pep, PORT_CONFIG, cfg);
+       wrl(pep, PORT_CONFIG_EXT, cfgext);
+
+       phy_print_status(phy);
+}
+
+static int pxa168_init_phy(struct net_device *dev)
+{
+       struct pxa168_eth_private *pep = netdev_priv(dev);
+       struct ethtool_cmd cmd;
+       int err;
+
+       if (pep->phy)
+               return 0;
+
+       pep->phy = mdiobus_scan(pep->smi_bus, pep->phy_addr);
+       if (!pep->phy)
+               return -ENODEV;
+
+       err = phy_connect_direct(dev, pep->phy, pxa168_eth_adjust_link,
+                                pep->phy_intf);
+       if (err)
+               return err;
+
+       err = pxa168_get_settings(dev, &cmd);
+       if (err)
+               return err;
+
+       cmd.phy_address = pep->phy_addr;
+       cmd.speed = pep->phy_speed;
+       cmd.duplex = pep->phy_duplex;
+       cmd.advertising = PHY_BASIC_FEATURES;
+       cmd.autoneg = AUTONEG_ENABLE;
+
+       if (cmd.speed != 0)
+               cmd.autoneg = AUTONEG_DISABLE;
+
+       return pxa168_set_settings(dev, &cmd);
+}
+
 static int pxa168_init_hw(struct pxa168_eth_private *pep)
 {
        int err = 0;
@@ -1039,9 +1085,8 @@ static void rxq_deinit(struct net_device *dev)
                }
        }
        if (pep->rx_desc_count)
-               printk(KERN_ERR
-                      "Error in freeing Rx Ring. %d skb's still\n",
-                      pep->rx_desc_count);
+               netdev_err(dev, "Error in freeing Rx Ring. %d skb's still\n",
+                          pep->rx_desc_count);
        /* Free RX ring */
        if (pep->p_rx_desc_area)
                dma_free_coherent(pep->dev->dev.parent, pep->rx_desc_area_size,
@@ -1104,6 +1149,10 @@ static int pxa168_eth_open(struct net_device *dev)
        struct pxa168_eth_private *pep = netdev_priv(dev);
        int err;
 
+       err = pxa168_init_phy(dev);
+       if (err)
+               return err;
+
        err = request_irq(dev->irq, pxa168_eth_int_handler, 0, dev->name, dev);
        if (err) {
                dev_err(&dev->dev, "can't assign irq\n");
@@ -1124,8 +1173,8 @@ static int pxa168_eth_open(struct net_device *dev)
        pep->rx_used_desc_q = 0;
        pep->rx_curr_desc_q = 0;
        netif_carrier_off(dev);
-       eth_port_start(dev);
        napi_enable(&pep->napi);
+       eth_port_start(dev);
        return 0;
 out_free_rx_skb:
        rxq_deinit(dev);
@@ -1202,10 +1251,6 @@ static int pxa168_rx_poll(struct napi_struct *napi, int budget)
        struct net_device *dev = pep->dev;
        int work_done = 0;
 
-       if (unlikely(pep->work_todo & WORK_LINK)) {
-               pep->work_todo &= ~(WORK_LINK);
-               handle_link_event(pep);
-       }
        /*
         * We call txq_reclaim every time since in NAPI interupts are disabled
         * and due to this we miss the TX_DONE interrupt,which is not updated in
@@ -1280,15 +1325,15 @@ static int pxa168_smi_read(struct mii_bus *bus, int phy_addr, int regnum)
        int val;
 
        if (smi_wait_ready(pep)) {
-               printk(KERN_WARNING "pxa168_eth: SMI bus busy timeout\n");
+               netdev_warn(pep->dev, "pxa168_eth: SMI bus busy timeout\n");
                return -ETIMEDOUT;
        }
        wrl(pep, SMI, (phy_addr << 16) | (regnum << 21) | SMI_OP_R);
        /* now wait for the data to be valid */
        for (i = 0; !((val = rdl(pep, SMI)) & SMI_R_VALID); i++) {
                if (i == PHY_WAIT_ITERATIONS) {
-                       printk(KERN_WARNING
-                               "pxa168_eth: SMI bus read not valid\n");
+                       netdev_warn(pep->dev,
+                                   "pxa168_eth: SMI bus read not valid\n");
                        return -ENODEV;
                }
                msleep(10);
@@ -1303,7 +1348,7 @@ static int pxa168_smi_write(struct mii_bus *bus, int phy_addr, int regnum,
        struct pxa168_eth_private *pep = bus->priv;
 
        if (smi_wait_ready(pep)) {
-               printk(KERN_WARNING "pxa168_eth: SMI bus busy timeout\n");
+               netdev_warn(pep->dev, "pxa168_eth: SMI bus busy timeout\n");
                return -ETIMEDOUT;
        }
 
@@ -1311,7 +1356,7 @@ static int pxa168_smi_write(struct mii_bus *bus, int phy_addr, int regnum,
            SMI_OP_W | (value & 0xffff));
 
        if (smi_wait_ready(pep)) {
-               printk(KERN_ERR "pxa168_eth: SMI bus busy timeout\n");
+               netdev_err(pep->dev, "pxa168_eth: SMI bus busy timeout\n");
                return -ETIMEDOUT;
        }
 
@@ -1328,74 +1373,6 @@ static int pxa168_eth_do_ioctl(struct net_device *dev, struct ifreq *ifr,
        return -EOPNOTSUPP;
 }
 
-static struct phy_device *phy_scan(struct pxa168_eth_private *pep, int phy_addr)
-{
-       struct mii_bus *bus = pep->smi_bus;
-       struct phy_device *phydev;
-       int start;
-       int num;
-       int i;
-
-       if (phy_addr == PXA168_ETH_PHY_ADDR_DEFAULT) {
-               /* Scan entire range */
-               start = ethernet_phy_get(pep);
-               num = 32;
-       } else {
-               /* Use phy addr specific to platform */
-               start = phy_addr & 0x1f;
-               num = 1;
-       }
-       phydev = NULL;
-       for (i = 0; i < num; i++) {
-               int addr = (start + i) & 0x1f;
-               if (bus->phy_map[addr] == NULL)
-                       mdiobus_scan(bus, addr);
-
-               if (phydev == NULL) {
-                       phydev = bus->phy_map[addr];
-                       if (phydev != NULL)
-                               ethernet_phy_set_addr(pep, addr);
-               }
-       }
-
-       return phydev;
-}
-
-static void phy_init(struct pxa168_eth_private *pep, int speed, int duplex)
-{
-       struct phy_device *phy = pep->phy;
-
-       phy_attach(pep->dev, dev_name(&phy->dev), PHY_INTERFACE_MODE_MII);
-
-       if (speed == 0) {
-               phy->autoneg = AUTONEG_ENABLE;
-               phy->speed = 0;
-               phy->duplex = 0;
-               phy->supported &= PHY_BASIC_FEATURES;
-               phy->advertising = phy->supported | ADVERTISED_Autoneg;
-       } else {
-               phy->autoneg = AUTONEG_DISABLE;
-               phy->advertising = 0;
-               phy->speed = speed;
-               phy->duplex = duplex;
-       }
-       phy_start_aneg(phy);
-}
-
-static int ethernet_phy_setup(struct net_device *dev)
-{
-       struct pxa168_eth_private *pep = netdev_priv(dev);
-
-       if (pep->pd->init)
-               pep->pd->init();
-       pep->phy = phy_scan(pep, pep->pd->phy_addr & 0x1f);
-       if (pep->phy != NULL)
-               phy_init(pep, pep->pd->speed, pep->pd->duplex);
-       update_hash_table_mac_address(pep, NULL, dev->dev_addr);
-
-       return 0;
-}
-
 static int pxa168_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
 {
        struct pxa168_eth_private *pep = netdev_priv(dev);
@@ -1425,23 +1402,23 @@ static void pxa168_get_drvinfo(struct net_device *dev,
 }
 
 static const struct ethtool_ops pxa168_ethtool_ops = {
-       .get_settings = pxa168_get_settings,
-       .set_settings = pxa168_set_settings,
-       .get_drvinfo = pxa168_get_drvinfo,
-       .get_link = ethtool_op_get_link,
-       .get_ts_info = ethtool_op_get_ts_info,
+       .get_settings   = pxa168_get_settings,
+       .set_settings   = pxa168_set_settings,
+       .get_drvinfo    = pxa168_get_drvinfo,
+       .get_link       = ethtool_op_get_link,
+       .get_ts_info    = ethtool_op_get_ts_info,
 };
 
 static const struct net_device_ops pxa168_eth_netdev_ops = {
-       .ndo_open = pxa168_eth_open,
-       .ndo_stop = pxa168_eth_stop,
-       .ndo_start_xmit = pxa168_eth_start_xmit,
-       .ndo_set_rx_mode = pxa168_eth_set_rx_mode,
-       .ndo_set_mac_address = pxa168_eth_set_mac_address,
-       .ndo_validate_addr = eth_validate_addr,
-       .ndo_do_ioctl = pxa168_eth_do_ioctl,
-       .ndo_change_mtu = pxa168_eth_change_mtu,
-       .ndo_tx_timeout = pxa168_eth_tx_timeout,
+       .ndo_open               = pxa168_eth_open,
+       .ndo_stop               = pxa168_eth_stop,
+       .ndo_start_xmit         = pxa168_eth_start_xmit,
+       .ndo_set_rx_mode        = pxa168_eth_set_rx_mode,
+       .ndo_set_mac_address    = pxa168_eth_set_mac_address,
+       .ndo_validate_addr      = eth_validate_addr,
+       .ndo_do_ioctl           = pxa168_eth_do_ioctl,
+       .ndo_change_mtu         = pxa168_eth_change_mtu,
+       .ndo_tx_timeout         = pxa168_eth_tx_timeout,
 };
 
 static int pxa168_eth_probe(struct platform_device *pdev)
@@ -1450,17 +1427,18 @@ static int pxa168_eth_probe(struct platform_device *pdev)
        struct net_device *dev = NULL;
        struct resource *res;
        struct clk *clk;
+       struct device_node *np;
+       const unsigned char *mac_addr = NULL;
        int err;
 
        printk(KERN_NOTICE "PXA168 10/100 Ethernet Driver\n");
 
-       clk = clk_get(&pdev->dev, "MFUCLK");
+       clk = devm_clk_get(&pdev->dev, NULL);
        if (IS_ERR(clk)) {
-               printk(KERN_ERR "%s: Fast Ethernet failed to get clock\n",
-                       DRIVER_NAME);
+               dev_err(&pdev->dev, "Fast Ethernet failed to get clock\n");
                return -ENODEV;
        }
-       clk_enable(clk);
+       clk_prepare_enable(clk);
 
        dev = alloc_etherdev(sizeof(struct pxa168_eth_private));
        if (!dev) {
@@ -1472,16 +1450,14 @@ static int pxa168_eth_probe(struct platform_device *pdev)
        pep = netdev_priv(dev);
        pep->dev = dev;
        pep->clk = clk;
+
        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
-       if (res == NULL) {
-               err = -ENODEV;
-               goto err_netdev;
-       }
-       pep->base = ioremap(res->start, resource_size(res));
-       if (pep->base == NULL) {
+       pep->base = devm_ioremap_resource(&pdev->dev, res);
+       if (IS_ERR(pep->base)) {
                err = -ENOMEM;
                goto err_netdev;
        }
+
        res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
        BUG_ON(!res);
        dev->irq = res->start;
@@ -1492,19 +1468,52 @@ static int pxa168_eth_probe(struct platform_device *pdev)
 
        INIT_WORK(&pep->tx_timeout_task, pxa168_eth_tx_timeout_task);
 
-       printk(KERN_INFO "%s:Using random mac address\n", DRIVER_NAME);
-       eth_hw_addr_random(dev);
+       if (pdev->dev.of_node)
+               mac_addr = of_get_mac_address(pdev->dev.of_node);
 
-       pep->pd = dev_get_platdata(&pdev->dev);
-       pep->rx_ring_size = NUM_RX_DESCS;
-       if (pep->pd->rx_queue_size)
-               pep->rx_ring_size = pep->pd->rx_queue_size;
+       if (mac_addr && is_valid_ether_addr(mac_addr)) {
+               ether_addr_copy(dev->dev_addr, mac_addr);
+       } else {
+               /* try reading the mac address, if set by the bootloader */
+               pxa168_eth_get_mac_address(dev, dev->dev_addr);
+               if (!is_valid_ether_addr(dev->dev_addr)) {
+                       dev_info(&pdev->dev, "Using random mac address\n");
+                       eth_hw_addr_random(dev);
+               }
+       }
 
+       pep->rx_ring_size = NUM_RX_DESCS;
        pep->tx_ring_size = NUM_TX_DESCS;
-       if (pep->pd->tx_queue_size)
-               pep->tx_ring_size = pep->pd->tx_queue_size;
 
-       pep->port_num = pep->pd->port_number;
+       pep->pd = dev_get_platdata(&pdev->dev);
+       if (pep->pd) {
+               if (pep->pd->rx_queue_size)
+                       pep->rx_ring_size = pep->pd->rx_queue_size;
+
+               if (pep->pd->tx_queue_size)
+                       pep->tx_ring_size = pep->pd->tx_queue_size;
+
+               pep->port_num = pep->pd->port_number;
+               pep->phy_addr = pep->pd->phy_addr;
+               pep->phy_speed = pep->pd->speed;
+               pep->phy_duplex = pep->pd->duplex;
+               pep->phy_intf = pep->pd->intf;
+
+               if (pep->pd->init)
+                       pep->pd->init();
+       } else if (pdev->dev.of_node) {
+               of_property_read_u32(pdev->dev.of_node, "port-id",
+                                    &pep->port_num);
+
+               np = of_parse_phandle(pdev->dev.of_node, "phy-handle", 0);
+               if (!np) {
+                       dev_err(&pdev->dev, "missing phy-handle\n");
+                       return -EINVAL;
+               }
+               of_property_read_u32(np, "reg", &pep->phy_addr);
+               pep->phy_intf = of_get_phy_mode(pdev->dev.of_node);
+       }
+
        /* Hardware supports only 3 ports */
        BUG_ON(pep->port_num > 2);
        netif_napi_add(dev, &pep->napi, pxa168_rx_poll, pep->rx_ring_size);
@@ -1531,11 +1540,8 @@ static int pxa168_eth_probe(struct platform_device *pdev)
        if (err)
                goto err_free_mdio;
 
-       pxa168_init_hw(pep);
-       err = ethernet_phy_setup(dev);
-       if (err)
-               goto err_mdiobus;
        SET_NETDEV_DEV(dev, &pdev->dev);
+       pxa168_init_hw(pep);
        err = register_netdev(dev);
        if (err)
                goto err_mdiobus;
@@ -1565,13 +1571,13 @@ static int pxa168_eth_remove(struct platform_device *pdev)
                                  pep->htpr, pep->htpr_dma);
                pep->htpr = NULL;
        }
+       if (pep->phy)
+               phy_disconnect(pep->phy);
        if (pep->clk) {
                clk_disable(pep->clk);
                clk_put(pep->clk);
                pep->clk = NULL;
        }
-       if (pep->phy != NULL)
-               phy_detach(pep->phy);
 
        iounmap(pep->base);
        pep->base = NULL;
@@ -1605,6 +1611,12 @@ static int pxa168_eth_suspend(struct platform_device *pdev, pm_message_t state)
 #define pxa168_eth_suspend NULL
 #endif
 
+static const struct of_device_id pxa168_eth_of_match[] = {
+       { .compatible = "marvell,pxa168-eth" },
+       { },
+};
+MODULE_DEVICE_TABLE(of, pxa168_eth_of_match);
+
 static struct platform_driver pxa168_eth_driver = {
        .probe = pxa168_eth_probe,
        .remove = pxa168_eth_remove,
@@ -1612,8 +1624,9 @@ static struct platform_driver pxa168_eth_driver = {
        .resume = pxa168_eth_resume,
        .suspend = pxa168_eth_suspend,
        .driver = {
-                  .name = DRIVER_NAME,
-                  },
+               .name           = DRIVER_NAME,
+               .of_match_table = of_match_ptr(pxa168_eth_of_match),
+       },
 };
 
 module_platform_driver(pxa168_eth_driver);