net: smc911x: Invert the logic in smc911x_miiphy_{read,write}()
[platform/kernel/u-boot.git] / drivers / net / smc911x.c
index cac08d0..ceb4f81 100644 (file)
@@ -1,25 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * SMSC LAN9[12]1[567] Network driver
  *
  * (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
- *
- * See file CREDITS for list of people who contributed to this
- * project.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- * MA 02111-1307 USA
  */
 
 #include <common.h>
 
 #include "smc911x.h"
 
-u32 pkt_data_pull(struct eth_device *dev, u32 addr) \
-       __attribute__ ((weak, alias ("smc911x_reg_read")));
-void pkt_data_push(struct eth_device *dev, u32 addr, u32 val) \
-       __attribute__ ((weak, alias ("smc911x_reg_write")));
-
-#define mdelay(n)       udelay((n)*1000)
-
 static void smc911x_handle_mac_address(struct eth_device *dev)
 {
        unsigned long addrh, addrl;
@@ -50,7 +26,7 @@ static void smc911x_handle_mac_address(struct eth_device *dev)
        printf(DRIVERNAME ": MAC %pM\n", m);
 }
 
-static int smc911x_miiphy_read(struct eth_device *dev,
+static int smc911x_eth_phy_read(struct eth_device *dev,
                                u8 phy, u8 reg, u16 *val)
 {
        while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
@@ -67,7 +43,7 @@ static int smc911x_miiphy_read(struct eth_device *dev,
        return 0;
 }
 
-static int smc911x_miiphy_write(struct eth_device *dev,
+static int smc911x_eth_phy_write(struct eth_device *dev,
                                u8 phy, u8 reg, u16  val)
 {
        while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
@@ -103,11 +79,11 @@ static void smc911x_phy_configure(struct eth_device *dev)
 
        smc911x_phy_reset(dev);
 
-       smc911x_miiphy_write(dev, 1, PHY_BMCR, PHY_BMCR_RESET);
+       smc911x_eth_phy_write(dev, 1, MII_BMCR, BMCR_RESET);
        mdelay(1);
-       smc911x_miiphy_write(dev, 1, PHY_ANAR, 0x01e1);
-       smc911x_miiphy_write(dev, 1, PHY_BMCR, PHY_BMCR_AUTON |
-                               PHY_BMCR_RST_NEG);
+       smc911x_eth_phy_write(dev, 1, MII_ADVERTISE, 0x01e1);
+       smc911x_eth_phy_write(dev, 1, MII_BMCR, BMCR_ANENABLE |
+                               BMCR_ANRESTART);
 
        timeout = 5000;
        do {
@@ -115,9 +91,9 @@ static void smc911x_phy_configure(struct eth_device *dev)
                if ((timeout--) == 0)
                        goto err_out;
 
-               if (smc911x_miiphy_read(dev, 1, PHY_BMSR, &status) != 0)
+               if (smc911x_eth_phy_read(dev, 1, MII_BMSR, &status) != 0)
                        goto err_out;
-       } while (!(status & PHY_BMSR_LS));
+       } while (!(status & BMSR_LSTATUS));
 
        printf(DRIVERNAME ": phy initialized\n");
 
@@ -163,8 +139,7 @@ static int smc911x_init(struct eth_device *dev, bd_t * bd)
        return 0;
 }
 
-static int smc911x_send(struct eth_device *dev,
-                       volatile void *packet, int length)
+static int smc911x_send(struct eth_device *dev, void *packet, int length)
 {
        u32 *data = (u32*)packet;
        u32 tmplen;
@@ -177,7 +152,7 @@ static int smc911x_send(struct eth_device *dev,
        tmplen = (length + 3) / 4;
 
        while (tmplen--)
-               pkt_data_push(dev, TX_DATA_FIFO, *data++);
+               smc911x_reg_write(dev, TX_DATA_FIFO, *data++);
 
        /* wait for transmission */
        while (!((smc911x_reg_read(dev, TX_FIFO_INF) &
@@ -206,11 +181,12 @@ static int smc911x_send(struct eth_device *dev,
 static void smc911x_halt(struct eth_device *dev)
 {
        smc911x_reset(dev);
+       smc911x_handle_mac_address(dev);
 }
 
-static int smc911x_rx(struct eth_device *dev)
+static int smc911x_recv(struct eth_device *dev)
 {
-       u32 *data = (u32 *)NetRxPackets[0];
+       u32 *data = (u32 *)net_rx_packets[0];
        u32 pktlen, tmplen;
        u32 status;
 
@@ -220,32 +196,60 @@ static int smc911x_rx(struct eth_device *dev)
 
                smc911x_reg_write(dev, RX_CFG, 0);
 
-               tmplen = (pktlen + 2+ 3) / 4;
+               tmplen = (pktlen + 3) / 4;
                while (tmplen--)
-                       *data++ = pkt_data_pull(dev, RX_DATA_FIFO);
+                       *data++ = smc911x_reg_read(dev, RX_DATA_FIFO);
 
                if (status & RX_STS_ES)
                        printf(DRIVERNAME
                                ": dropped bad packet. Status: 0x%08x\n",
                                status);
                else
-                       NetReceive(NetRxPackets[0], pktlen);
+                       net_process_received_packet(net_rx_packets[0], pktlen);
        }
 
        return 0;
 }
 
+#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,
+                              int reg)
+{
+       struct eth_device *dev = eth_get_dev_by_name(bus->name);
+       u16 val = 0;
+       int ret;
+
+       if (!dev)
+               return -ENODEV;
+
+       ret = smc911x_eth_phy_read(dev, phy, reg, &val);
+       if (ret < 0)
+               return ret;
+
+       return val;
+}
+/* wrapper for smc911x_eth_phy_write */
+static int smc911x_miiphy_write(struct mii_dev *bus, int phy, int devad,
+                               int reg, u16 val)
+{
+       struct eth_device *dev = eth_get_dev_by_name(bus->name);
+
+       if (!dev)
+               return -ENODEV;
+
+       return smc911x_eth_phy_write(dev, phy, reg, val);
+}
+#endif
+
 int smc911x_initialize(u8 dev_num, int base_addr)
 {
        unsigned long addrl, addrh;
        struct eth_device *dev;
 
-       dev = malloc(sizeof(*dev));
-       if (!dev) {
-               free(dev);
-               return -1;
-       }
-       memset(dev, 0, sizeof(*dev));
+       dev = calloc(1, sizeof(*dev));
+       if (!dev)
+               return -ENOMEM;
 
        dev->iobase = base_addr;
 
@@ -270,9 +274,24 @@ int smc911x_initialize(u8 dev_num, int base_addr)
        dev->init = smc911x_init;
        dev->halt = smc911x_halt;
        dev->send = smc911x_send;
-       dev->recv = smc911x_rx;
+       dev->recv = smc911x_recv;
        sprintf(dev->name, "%s-%hu", DRIVERNAME, dev_num);
 
        eth_register(dev);
+
+#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
+       int retval;
+       struct mii_dev *mdiodev = mdio_alloc();
+       if (!mdiodev)
+               return -ENOMEM;
+       strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
+       mdiodev->read = smc911x_miiphy_read;
+       mdiodev->write = smc911x_miiphy_write;
+
+       retval = mdio_register(mdiodev);
+       if (retval < 0)
+               return retval;
+#endif
+
        return 1;
 }