Merge tag 'u-boot-stm32-20211012' of https://source.denx.de/u-boot/custodians/u-boot-stm
[platform/kernel/u-boot.git] / drivers / net / smc911x.c
index 2d1a9e0..5d9a73f 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,10 +22,13 @@ 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];
+       bool                    use_32_bit_io;
 };
 
 static const struct chip_id chip_ids[] =  {
@@ -45,36 +49,24 @@ static const struct chip_id chip_ids[] =  {
 
 #define DRIVERNAME "smc911x"
 
-#if defined (CONFIG_SMC911X_32_BIT) && \
-       defined (CONFIG_SMC911X_16_BIT)
-#error "SMC911X: Only one of CONFIG_SMC911X_32_BIT and \
-       CONFIG_SMC911X_16_BIT shall be set"
-#endif
-
-#if defined (CONFIG_SMC911X_32_BIT)
 static u32 smc911x_reg_read(struct smc911x_priv *priv, u32 offset)
 {
-       return readl(priv->iobase + offset);
-}
+       if (priv->use_32_bit_io)
+               return readl(priv->iobase + offset);
 
-static void smc911x_reg_write(struct smc911x_priv *priv, u32 offset, u32 val)
-{
-       writel(val, priv->iobase + offset);
-}
-#elif defined (CONFIG_SMC911X_16_BIT)
-static u32 smc911x_reg_read(struct smc911x_priv *priv, u32 offset)
-{
        return (readw(priv->iobase + offset) & 0xffff) |
               (readw(priv->iobase + offset + 2) << 16);
 }
+
 static void smc911x_reg_write(struct smc911x_priv *priv, u32 offset, u32 val)
 {
-       writew(val & 0xffff, priv->iobase + offset);
-       writew(val >> 16, priv->iobase + offset + 2);
+       if (priv->use_32_bit_io) {
+               writel(val, priv->iobase + offset);
+       } else {
+               writew(val & 0xffff, priv->iobase + offset);
+               writew(val >> 16, priv->iobase + offset + 2);
+       }
 }
-#else
-#error "SMC911X: undefined bus width"
-#endif /* CONFIG_SMC911X_16_BIT */
 
 static u32 smc911x_get_mac_csr(struct smc911x_priv *priv, u8 reg)
 {
@@ -184,6 +176,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 +289,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 +308,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 +348,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 +372,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,
@@ -416,7 +425,7 @@ static int smc911x_initialize_mii(struct smc911x_priv *priv)
        if (!mdiodev)
                return -ENOMEM;
 
-       strncpy(mdiodev->name, priv->dev.name, MDIO_NAME_LEN);
+       strlcpy(mdiodev->name, priv->dev.name, MDIO_NAME_LEN);
        mdiodev->read = smc911x_miiphy_read;
        mdiodev->write = smc911x_miiphy_write;
 
@@ -435,9 +444,42 @@ static int smc911x_initialize_mii(struct smc911x_priv *priv)
 }
 #endif
 
-int smc911x_initialize(u8 dev_num, int base_addr)
+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, phys_addr_t base_addr)
 {
-       unsigned long addrl, addrh;
        struct smc911x_priv *priv;
        int ret;
 
@@ -448,6 +490,8 @@ int smc911x_initialize(u8 dev_num, int base_addr)
        priv->iobase = base_addr;
        priv->dev.iobase = base_addr;
 
+       priv->use_32_bit_io = CONFIG_IS_ENABLED(SMC911X_32_BIT);
+
        /* Try to detect chip. Will fail if not present. */
        ret = smc911x_detect_chip(priv);
        if (ret) {
@@ -455,17 +499,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 +522,124 @@ err_detect:
        free(priv);
        return ret;
 }
+
+#else  /* ifdef CONFIG_DM_ETH */
+
+static int smc911x_start(struct udevice *dev)
+{
+       struct eth_pdata *plat = dev_get_plat(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_plat(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_of_to_plat(struct udevice *dev)
+{
+       struct smc911x_priv *priv = dev_get_priv(dev);
+       struct eth_pdata *pdata = dev_get_plat(dev);
+       u32 io_width;
+       int ret;
+
+       pdata->iobase = dev_read_addr(dev);
+       priv->iobase = pdata->iobase;
+
+       ret = dev_read_u32(dev, "reg-io-width", &io_width);
+       if (!ret)
+               priv->use_32_bit_io = (io_width == 4);
+       else
+               priv->use_32_bit_io = CONFIG_IS_ENABLED(SMC911X_32_BIT);
+
+       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,
+       .of_to_plat = smc911x_of_to_plat,
+       .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