dm: treewide: Rename 'platdata' variables to just 'plat'
[platform/kernel/u-boot.git] / drivers / net / smc911x.c
index 2d1a9e0..648932d 100644 (file)
@@ -11,6 +11,7 @@
 #include <net.h>
 #include <miiphy.h>
 #include <linux/io.h>
+#include <linux/delay.h>
 #include <linux/types.h>
 
 #include "smc911x.h"
@@ -21,7 +22,9 @@ struct chip_id {
 };
 
 struct smc911x_priv {
+#ifndef CONFIG_DM_ETH
        struct eth_device       dev;
+#endif
        phys_addr_t             iobase;
        const struct chip_id    *chipid;
        unsigned char           enetaddr[6];
@@ -184,6 +187,26 @@ static void smc911x_handle_mac_address(struct smc911x_priv *priv)
        printf(DRIVERNAME ": MAC %pM\n", m);
 }
 
+static bool smc911x_read_mac_address(struct smc911x_priv *priv)
+{
+       u32 addrh, addrl;
+
+       /* address is obtained from optional eeprom */
+       addrh = smc911x_get_mac_csr(priv, ADDRH);
+       addrl = smc911x_get_mac_csr(priv, ADDRL);
+       if (addrl == 0xffffffff && addrh == 0x0000ffff)
+               return false;
+
+       priv->enetaddr[0] = addrl;
+       priv->enetaddr[1] = addrl >>  8;
+       priv->enetaddr[2] = addrl >> 16;
+       priv->enetaddr[3] = addrl >> 24;
+       priv->enetaddr[4] = addrh;
+       priv->enetaddr[5] = addrh >> 8;
+
+       return true;
+}
+
 static int smc911x_eth_phy_read(struct smc911x_priv *priv,
                                u8 phy, u8 reg, u16 *val)
 {
@@ -277,9 +300,8 @@ static void smc911x_enable(struct smc911x_priv *priv)
                                MAC_CR_HBDIS);
 }
 
-static int smc911x_init(struct eth_device *dev, bd_t * bd)
+static int smc911x_init_common(struct smc911x_priv *priv)
 {
-       struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
        const struct chip_id *id = priv->chipid;
 
        printf(DRIVERNAME ": detected %s controller\n", id->name);
@@ -297,9 +319,9 @@ static int smc911x_init(struct eth_device *dev, bd_t * bd)
        return 0;
 }
 
-static int smc911x_send(struct eth_device *dev, void *packet, int length)
+static int smc911x_send_common(struct smc911x_priv *priv,
+                              void *packet, int length)
 {
-       struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
        u32 *data = (u32*)packet;
        u32 tmplen;
        u32 status;
@@ -337,18 +359,14 @@ static int smc911x_send(struct eth_device *dev, void *packet, int length)
        return -1;
 }
 
-static void smc911x_halt(struct eth_device *dev)
+static void smc911x_halt_common(struct smc911x_priv *priv)
 {
-       struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
-
        smc911x_reset(priv);
        smc911x_handle_mac_address(priv);
 }
 
-static int smc911x_recv(struct eth_device *dev)
+static int smc911x_recv_common(struct smc911x_priv *priv, u32 *data)
 {
-       struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
-       u32 *data = (u32 *)net_rx_packets[0];
        u32 pktlen, tmplen;
        u32 status;
 
@@ -365,16 +383,18 @@ static int smc911x_recv(struct eth_device *dev)
        while (tmplen--)
                *data++ = smc911x_reg_read(priv, RX_DATA_FIFO);
 
-       if (status & RX_STS_ES)
+       if (status & RX_STS_ES) {
                printf(DRIVERNAME
                        ": dropped bad packet. Status: 0x%08x\n",
                        status);
-       else
-               net_process_received_packet(net_rx_packets[0], pktlen);
+               return 0;
+       }
 
-       return 0;
+       return pktlen;
 }
 
+#ifndef CONFIG_DM_ETH
+
 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
 /* wrapper for smc911x_eth_phy_read */
 static int smc911x_miiphy_read(struct mii_dev *bus, int phy, int devad,
@@ -435,9 +455,42 @@ static int smc911x_initialize_mii(struct smc911x_priv *priv)
 }
 #endif
 
+static int smc911x_init(struct eth_device *dev, struct bd_info *bd)
+{
+       struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
+
+       return smc911x_init_common(priv);
+}
+
+static void smc911x_halt(struct eth_device *dev)
+{
+       struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
+
+       smc911x_halt_common(priv);
+}
+
+static int smc911x_send(struct eth_device *dev, void *packet, int length)
+{
+       struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
+
+       return smc911x_send_common(priv, packet, length);
+}
+
+static int smc911x_recv(struct eth_device *dev)
+{
+       struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
+       u32 *data = (u32 *)net_rx_packets[0];
+       int ret;
+
+       ret = smc911x_recv_common(priv, data);
+       if (ret)
+               net_process_received_packet(net_rx_packets[0], ret);
+
+       return ret;
+}
+
 int smc911x_initialize(u8 dev_num, int base_addr)
 {
-       unsigned long addrl, addrh;
        struct smc911x_priv *priv;
        int ret;
 
@@ -455,17 +508,8 @@ int smc911x_initialize(u8 dev_num, int base_addr)
                goto err_detect;
        }
 
-       addrh = smc911x_get_mac_csr(priv, ADDRH);
-       addrl = smc911x_get_mac_csr(priv, ADDRL);
-       if (!(addrl == 0xffffffff && addrh == 0x0000ffff)) {
-               /* address is obtained from optional eeprom */
-               priv->enetaddr[0] = addrl;
-               priv->enetaddr[1] = addrl >>  8;
-               priv->enetaddr[2] = addrl >> 16;
-               priv->enetaddr[3] = addrl >> 24;
-               priv->enetaddr[4] = addrh;
-               priv->enetaddr[5] = addrh >> 8;
-       }
+       if (smc911x_read_mac_address(priv))
+               memcpy(priv->dev.enetaddr, priv->enetaddr, 6);
 
        priv->dev.init = smc911x_init;
        priv->dev.halt = smc911x_halt;
@@ -487,3 +531,116 @@ err_detect:
        free(priv);
        return ret;
 }
+
+#else  /* ifdef CONFIG_DM_ETH */
+
+static int smc911x_start(struct udevice *dev)
+{
+       struct eth_pdata *plat = dev_get_platdata(dev);
+       struct smc911x_priv *priv = dev_get_priv(dev);
+
+       memcpy(priv->enetaddr, plat->enetaddr, sizeof(plat->enetaddr));
+
+       return smc911x_init_common(priv);
+}
+
+static void smc911x_stop(struct udevice *dev)
+{
+       struct smc911x_priv *priv = dev_get_priv(dev);
+
+       smc911x_halt_common(priv);
+}
+
+static int smc911x_send(struct udevice *dev, void *packet, int length)
+{
+       struct smc911x_priv *priv = dev_get_priv(dev);
+       int ret;
+
+       ret = smc911x_send_common(priv, packet, length);
+
+       return ret ? 0 : -ETIMEDOUT;
+}
+
+static int smc911x_recv(struct udevice *dev, int flags, uchar **packetp)
+{
+       struct smc911x_priv *priv = dev_get_priv(dev);
+       u32 *data = (u32 *)net_rx_packets[0];
+       int ret;
+
+       ret = smc911x_recv_common(priv, data);
+       if (ret)
+               *packetp = (void *)data;
+
+       return ret ? ret : -EAGAIN;
+}
+
+static int smc911x_read_rom_hwaddr(struct udevice *dev)
+{
+       struct smc911x_priv *priv = dev_get_priv(dev);
+       struct eth_pdata *pdata = dev_get_platdata(dev);
+
+       if (!smc911x_read_mac_address(priv))
+               return -ENODEV;
+
+       memcpy(pdata->enetaddr, priv->enetaddr, sizeof(pdata->enetaddr));
+
+       return 0;
+}
+
+static int smc911x_bind(struct udevice *dev)
+{
+       return device_set_name(dev, dev->name);
+}
+
+static int smc911x_probe(struct udevice *dev)
+{
+       struct smc911x_priv *priv = dev_get_priv(dev);
+       int ret;
+
+       /* Try to detect chip. Will fail if not present. */
+       ret = smc911x_detect_chip(priv);
+       if (ret)
+               return ret;
+
+       smc911x_read_rom_hwaddr(dev);
+
+       return 0;
+}
+
+static int smc911x_ofdata_to_platdata(struct udevice *dev)
+{
+       struct smc911x_priv *priv = dev_get_priv(dev);
+       struct eth_pdata *pdata = dev_get_platdata(dev);
+
+       pdata->iobase = dev_read_addr(dev);
+       priv->iobase = pdata->iobase;
+
+       return 0;
+}
+
+static const struct eth_ops smc911x_ops = {
+       .start  = smc911x_start,
+       .send   = smc911x_send,
+       .recv   = smc911x_recv,
+       .stop   = smc911x_stop,
+       .read_rom_hwaddr = smc911x_read_rom_hwaddr,
+};
+
+static const struct udevice_id smc911x_ids[] = {
+       { .compatible = "smsc,lan9115" },
+       { }
+};
+
+U_BOOT_DRIVER(smc911x) = {
+       .name           = "eth_smc911x",
+       .id             = UCLASS_ETH,
+       .of_match       = smc911x_ids,
+       .bind           = smc911x_bind,
+       .ofdata_to_platdata = smc911x_ofdata_to_platdata,
+       .probe          = smc911x_probe,
+       .ops            = &smc911x_ops,
+       .priv_auto      = sizeof(struct smc911x_priv),
+       .plat_auto      = sizeof(struct eth_pdata),
+       .flags          = DM_FLAG_ALLOC_PRIV_DMA,
+};
+#endif