common/board_r: Drop initr_bedbug wrapper
[oweals/u-boot.git] / common / miiphyutil.c
index 35ad357b95072384139679d1f3bd34a3f5ec6bc1..2db067876e4b8f58263ddf2ceca8209f0f5e207d 100644 (file)
@@ -1,24 +1,7 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * (C) Copyright 2001
  * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
- *
- * 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
  */
 
 /*
@@ -27,6 +10,7 @@
  */
 
 #include <common.h>
+#include <dm.h>
 #include <miiphy.h>
 #include <phy.h>
 
@@ -80,76 +64,6 @@ void miiphy_init(void)
        current_mii = NULL;
 }
 
-static int legacy_miiphy_read(struct mii_dev *bus, int addr, int devad, int reg)
-{
-       unsigned short val;
-       int ret;
-       struct legacy_mii_dev *ldev = bus->priv;
-
-       ret = ldev->read(bus->name, addr, reg, &val);
-
-       return ret ? -1 : (int)val;
-}
-
-static int legacy_miiphy_write(struct mii_dev *bus, int addr, int devad,
-                               int reg, u16 val)
-{
-       struct legacy_mii_dev *ldev = bus->priv;
-
-       return ldev->write(bus->name, addr, reg, val);
-}
-
-/*****************************************************************************
- *
- * Register read and write MII access routines for the device <name>.
- */
-void miiphy_register(const char *name,
-                     int (*read)(const char *devname, unsigned char addr,
-                                  unsigned char reg, unsigned short *value),
-                     int (*write)(const char *devname, unsigned char addr,
-                                   unsigned char reg, unsigned short value))
-{
-       struct mii_dev *new_dev;
-       struct legacy_mii_dev *ldev;
-
-       BUG_ON(strlen(name) >= MDIO_NAME_LEN);
-
-       /* check if we have unique name */
-       new_dev = miiphy_get_dev_by_name(name);
-       if (new_dev) {
-               printf("miiphy_register: non unique device name '%s'\n", name);
-               return;
-       }
-
-       /* allocate memory */
-       new_dev = mdio_alloc();
-       ldev = malloc(sizeof(*ldev));
-
-       if (new_dev == NULL || ldev == NULL) {
-               printf("miiphy_register: cannot allocate memory for '%s'\n",
-                       name);
-               return;
-       }
-
-       /* initalize mii_dev struct fields */
-       new_dev->read = legacy_miiphy_read;
-       new_dev->write = legacy_miiphy_write;
-       strncpy(new_dev->name, name, MDIO_NAME_LEN);
-       new_dev->name[MDIO_NAME_LEN - 1] = 0;
-       ldev->read = read;
-       ldev->write = write;
-       new_dev->priv = ldev;
-
-       debug("miiphy_register: added '%s', read=0x%08lx, write=0x%08lx\n",
-              new_dev->name, ldev->read, ldev->write);
-
-       /* add it to the list */
-       list_add_tail(&new_dev->link, &mii_devs);
-
-       if (!current_mii)
-               current_mii = new_dev;
-}
-
 struct mii_dev *mdio_alloc(void)
 {
        struct mii_dev *bus;
@@ -166,9 +80,14 @@ struct mii_dev *mdio_alloc(void)
        return bus;
 }
 
+void mdio_free(struct mii_dev *bus)
+{
+       free(bus);
+}
+
 int mdio_register(struct mii_dev *bus)
 {
-       if (!bus || !bus->name || !bus->read || !bus->write)
+       if (!bus || !bus->read || !bus->write)
                return -1;
 
        /* check if we have unique name */
@@ -187,6 +106,32 @@ int mdio_register(struct mii_dev *bus)
        return 0;
 }
 
+int mdio_register_seq(struct mii_dev *bus, int seq)
+{
+       int ret;
+
+       /* Setup a unique name for each mdio bus */
+       ret = snprintf(bus->name, MDIO_NAME_LEN, "eth%d", seq);
+       if (ret < 0)
+               return ret;
+
+       return mdio_register(bus);
+}
+
+int mdio_unregister(struct mii_dev *bus)
+{
+       if (!bus)
+               return 0;
+
+       /* delete it from the list */
+       list_del(&bus->link);
+
+       if (current_mii == bus)
+               current_mii = NULL;
+
+       return 0;
+}
+
 void mdio_list_devices(void)
 {
        struct list_head *entry;
@@ -201,7 +146,7 @@ void mdio_list_devices(void)
                        struct phy_device *phydev = bus->phymap[i];
 
                        if (phydev) {
-                               printf("%d - %s", i, phydev->drv->name);
+                               printf("%x - %s", i, phydev->drv->name);
 
                                if (phydev->dev)
                                        printf(" <--> %s\n", phydev->dev->name);
@@ -232,6 +177,11 @@ struct mii_dev *mdio_get_current_dev(void)
        return current_mii;
 }
 
+struct list_head *mdio_get_list_head(void)
+{
+       return &mii_devs;
+}
+
 struct phy_device *mdio_phydev_for_ethname(const char *ethname)
 {
        struct list_head *entry;
@@ -281,6 +231,8 @@ static struct mii_dev *miiphy_get_active_dev(const char *devname)
  * Read to variable <value> from the PHY attached to device <devname>,
  * use PHY address <addr> and register <reg>.
  *
+ * This API is deprecated. Use phy_read on a phy_device found via phy_connect
+ *
  * Returns:
  *   0 on success
  */
@@ -307,6 +259,8 @@ int miiphy_read(const char *devname, unsigned char addr, unsigned char reg,
  * Write <value> to the PHY attached to device <devname>,
  * use PHY address <addr> and register <reg>.
  *
+ * This API is deprecated. Use phy_write on a phy_device found by phy_connect
+ *
  * Returns:
  *   0 on success
  */
@@ -350,6 +304,8 @@ void miiphy_listdev(void)
  * Model:    6 bits (unsigned char)
  * Revision: 4 bits (unsigned char)
  *
+ * This API is deprecated.
+ *
  * Returns:
  *   0 on success
  */
@@ -389,6 +345,9 @@ int miiphy_info(const char *devname, unsigned char addr, unsigned int *oui,
 /*****************************************************************************
  *
  * Reset the PHY.
+ *
+ * This API is deprecated. Use PHYLIB.
+ *
  * Returns:
  *   0 on success
  */
@@ -437,7 +396,7 @@ int miiphy_reset(const char *devname, unsigned char addr)
  */
 int miiphy_speed(const char *devname, unsigned char addr)
 {
-       u16 bmcr, anlpar;
+       u16 bmcr, anlpar, adv;
 
 #if defined(CONFIG_PHY_GIGE)
        u16 btsr;
@@ -474,7 +433,12 @@ int miiphy_speed(const char *devname, unsigned char addr)
                        printf("PHY AN speed");
                        goto miiphy_read_failed;
                }
-               return (anlpar & LPA_100) ? _100BASET : _10BASET;
+
+               if (miiphy_read(devname, addr, MII_ADVERTISE, &adv)) {
+                       puts("PHY AN adv speed");
+                       goto miiphy_read_failed;
+               }
+               return ((anlpar & adv) & LPA_100) ? _100BASET : _10BASET;
        }
        /* Get speed from basic control settings. */
        return (bmcr & BMCR_SPEED100) ? _100BASET : _10BASET;
@@ -490,7 +454,7 @@ miiphy_read_failed:
  */
 int miiphy_duplex(const char *devname, unsigned char addr)
 {
-       u16 bmcr, anlpar;
+       u16 bmcr, anlpar, adv;
 
 #if defined(CONFIG_PHY_GIGE)
        u16 btsr;
@@ -532,7 +496,12 @@ int miiphy_duplex(const char *devname, unsigned char addr)
                        puts("PHY AN duplex");
                        goto miiphy_read_failed;
                }
-               return (anlpar & (LPA_10FULL | LPA_100FULL)) ?
+
+               if (miiphy_read(devname, addr, MII_ADVERTISE, &adv)) {
+                       puts("PHY AN adv duplex");
+                       goto miiphy_read_failed;
+               }
+               return ((anlpar & adv) & (LPA_10FULL | LPA_100FULL)) ?
                    FULL : HALF;
        }
        /* Get speed from basic control settings. */