]> git.ipfire.org Git - people/ms/u-boot.git/blobdiff - common/miiphyutil.c
imx6q: engicam: Use SPL_LOAD_FIT for MMC boards
[people/ms/u-boot.git] / common / miiphyutil.c
index 6944ea773031bf5927c6f3676aa89bed0e53ae18..8eb0f761bb0164de32d4a47f69a8b19dd9f98756 100644 (file)
@@ -2,23 +2,7 @@
  * (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
+ * SPDX-License-Identifier:    GPL-2.0+
  */
 
 /*
@@ -27,7 +11,9 @@
  */
 
 #include <common.h>
+#include <dm.h>
 #include <miiphy.h>
+#include <phy.h>
 
 #include <asm/types.h>
 #include <linux/list.h>
 #define debug(fmt, args...)
 #endif /* MII_DEBUG */
 
-struct mii_dev {
-       struct list_head link;
-       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);
-};
-
 static struct list_head mii_devs;
 static struct mii_dev *current_mii;
 
 /*
  * Lookup the mii_dev struct by the registered device name.
  */
-static struct mii_dev *miiphy_get_dev_by_name(const char *devname, int quiet)
+struct mii_dev *miiphy_get_dev_by_name(const char *devname)
 {
        struct list_head *entry;
        struct mii_dev *dev;
@@ -75,8 +52,6 @@ static struct mii_dev *miiphy_get_dev_by_name(const char *devname, int quiet)
                        return dev;
        }
 
-       if (!quiet)
-               printf("No such device: %s\n", devname);
        return NULL;
 }
 
@@ -90,70 +65,141 @@ void miiphy_init(void)
        current_mii = NULL;
 }
 
-/*****************************************************************************
- *
- * 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 *mdio_alloc(void)
 {
-       struct mii_dev *new_dev;
-       unsigned int name_len;
-       char *new_name;
+       struct mii_dev *bus;
 
-       /* check if we have unique name */
-       new_dev = miiphy_get_dev_by_name(name, 1);
-       if (new_dev) {
-               printf("miiphy_register: non unique device name '%s'\n", name);
-               return;
-       }
+       bus = malloc(sizeof(*bus));
+       if (!bus)
+               return bus;
 
-       /* allocate memory */
-       name_len = strlen(name);
-       new_dev =
-               (struct mii_dev *)malloc(sizeof(struct mii_dev) + name_len + 1);
-
-       if (new_dev == NULL) {
-               printf("miiphy_register: cannot allocate memory for '%s'\n",
-                       name);
-               return;
-       }
-       memset(new_dev, 0, sizeof(struct mii_dev) + name_len);
+       memset(bus, 0, sizeof(*bus));
 
        /* initalize mii_dev struct fields */
-       INIT_LIST_HEAD(&new_dev->link);
-       new_dev->read = read;
-       new_dev->write = write;
-       new_dev->name = new_name = (char *)(new_dev + 1);
-       strncpy(new_name, name, name_len);
-       new_name[name_len] = '\0';
+       INIT_LIST_HEAD(&bus->link);
 
-       debug("miiphy_register: added '%s', read=0x%08lx, write=0x%08lx\n",
-              new_dev->name, new_dev->read, new_dev->write);
+       return bus;
+}
+
+void mdio_free(struct mii_dev *bus)
+{
+       free(bus);
+}
+
+int mdio_register(struct mii_dev *bus)
+{
+       if (!bus || !bus->read || !bus->write)
+               return -1;
+
+       /* check if we have unique name */
+       if (miiphy_get_dev_by_name(bus->name)) {
+               printf("mdio_register: non unique device name '%s'\n",
+                       bus->name);
+               return -1;
+       }
 
        /* add it to the list */
-       list_add_tail(&new_dev->link, &mii_devs);
+       list_add_tail(&bus->link, &mii_devs);
 
        if (!current_mii)
-               current_mii = new_dev;
+               current_mii = 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;
+
+       list_for_each(entry, &mii_devs) {
+               int i;
+               struct mii_dev *bus = list_entry(entry, struct mii_dev, link);
+
+               printf("%s:\n", bus->name);
+
+               for (i = 0; i < PHY_MAX_ADDR; i++) {
+                       struct phy_device *phydev = bus->phymap[i];
+
+                       if (phydev) {
+                               printf("%x - %s", i, phydev->drv->name);
+
+                               if (phydev->dev)
+                                       printf(" <--> %s\n", phydev->dev->name);
+                               else
+                                       printf("\n");
+                       }
+               }
+       }
 }
 
 int miiphy_set_current_dev(const char *devname)
 {
        struct mii_dev *dev;
 
-       dev = miiphy_get_dev_by_name(devname, 0);
+       dev = miiphy_get_dev_by_name(devname);
        if (dev) {
                current_mii = dev;
                return 0;
        }
 
+       printf("No such device: %s\n", devname);
+
        return 1;
 }
 
+struct mii_dev *mdio_get_current_dev(void)
+{
+       return current_mii;
+}
+
+struct phy_device *mdio_phydev_for_ethname(const char *ethname)
+{
+       struct list_head *entry;
+       struct mii_dev *bus;
+
+       list_for_each(entry, &mii_devs) {
+               int i;
+               bus = list_entry(entry, struct mii_dev, link);
+
+               for (i = 0; i < PHY_MAX_ADDR; i++) {
+                       if (!bus->phymap[i] || !bus->phymap[i]->dev)
+                               continue;
+
+                       if (strcmp(bus->phymap[i]->dev->name, ethname) == 0)
+                               return bus->phymap[i];
+               }
+       }
+
+       printf("%s is not a known ethernet\n", ethname);
+       return NULL;
+}
+
 const char *miiphy_get_current_dev(void)
 {
        if (current_mii)
@@ -181,19 +227,27 @@ 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
  */
 int miiphy_read(const char *devname, unsigned char addr, unsigned char reg,
                 unsigned short *value)
 {
-       struct mii_dev *dev;
+       struct mii_dev *bus;
+       int ret;
 
-       dev = miiphy_get_active_dev(devname);
-       if (dev)
-               return dev->read(devname, addr, reg, value);
+       bus = miiphy_get_active_dev(devname);
+       if (!bus)
+               return 1;
 
-       return 1;
+       ret = bus->read(bus, addr, MDIO_DEVAD_NONE, reg);
+       if (ret < 0)
+               return 1;
+
+       *value = (unsigned short)ret;
+       return 0;
 }
 
 /*****************************************************************************
@@ -201,17 +255,19 @@ 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
  */
 int miiphy_write(const char *devname, unsigned char addr, unsigned char reg,
                  unsigned short value)
 {
-       struct mii_dev *dev;
+       struct mii_dev *bus;
 
-       dev = miiphy_get_active_dev(devname);
-       if (dev)
-               return dev->write(devname, addr, reg, value);
+       bus = miiphy_get_active_dev(devname);
+       if (bus)
+               return bus->write(bus, addr, MDIO_DEVAD_NONE, reg, value);
 
        return 1;
 }
@@ -244,6 +300,8 @@ void miiphy_listdev(void)
  * Model:    6 bits (unsigned char)
  * Revision: 4 bits (unsigned char)
  *
+ * This API is deprecated.
+ *
  * Returns:
  *   0 on success
  */
@@ -279,9 +337,13 @@ int miiphy_info(const char *devname, unsigned char addr, unsigned int *oui,
        return 0;
 }
 
+#ifndef CONFIG_PHYLIB
 /*****************************************************************************
  *
  * Reset the PHY.
+ *
+ * This API is deprecated. Use PHYLIB.
+ *
  * Returns:
  *   0 on success
  */
@@ -322,6 +384,7 @@ int miiphy_reset(const char *devname, unsigned char addr)
        }
        return 0;
 }
+#endif /* !PHYLIB */
 
 /*****************************************************************************
  *
@@ -329,7 +392,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;
@@ -352,7 +415,6 @@ int miiphy_speed(const char *devname, unsigned char addr)
        if (btsr != 0xFFFF &&
                        (btsr & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)))
                return _1000BASET;
-
 #endif /* CONFIG_PHY_GIGE */
 
        /* Check Basic Management Control Register first. */
@@ -367,7 +429,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;
@@ -383,7 +450,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;
@@ -425,7 +492,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. */