]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - drivers/net/phy/phy.c
net: phy: don't try autonegotiation if it is not enabled in the PHY
[karo-tx-uboot.git] / drivers / net / phy / phy.c
index d0ed7666ed98cbeccee364b38dc31f77cd6ce0d5..7ad1343ab14692fe7219454407c97587f6fd300c 100644 (file)
@@ -1,21 +1,7 @@
 /*
  * Generic PHY Management code
  *
- * 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+
  *
  * Copyright 2011 Freescale Semiconductor, Inc.
  * author Andy Fleming
@@ -75,6 +61,10 @@ static int genphy_config_advert(struct phy_device *phydev)
                adv |= ADVERTISE_PAUSE_CAP;
        if (advertise & ADVERTISED_Asym_Pause)
                adv |= ADVERTISE_PAUSE_ASYM;
+       if (advertise & ADVERTISED_1000baseX_Half)
+               adv |= ADVERTISE_1000XHALF;
+       if (advertise & ADVERTISED_1000baseX_Full)
+               adv |= ADVERTISE_1000XFULL;
 
        if (adv != oldadv) {
                err = phy_write(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE, adv);
@@ -121,7 +111,6 @@ static int genphy_config_advert(struct phy_device *phydev)
  */
 static int genphy_setup_forced(struct phy_device *phydev)
 {
-       int err;
        int ctl = 0;
 
        phydev->pause = phydev->asym_pause = 0;
@@ -134,9 +123,7 @@ static int genphy_setup_forced(struct phy_device *phydev)
        if (DUPLEX_FULL == phydev->duplex)
                ctl |= BMCR_FULLDPLX;
 
-       err = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
-
-       return err;
+       return phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
 }
 
 
@@ -158,9 +145,7 @@ int genphy_restart_aneg(struct phy_device *phydev)
        /* Don't isolate the PHY if we're negotiating */
        ctl &= ~(BMCR_ISOLATE);
 
-       ctl = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
-
-       return ctl;
+       return phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
 }
 
 
@@ -214,13 +199,16 @@ int genphy_config_aneg(struct phy_device *phydev)
  */
 int genphy_update_link(struct phy_device *phydev)
 {
-       unsigned int mii_reg;
+       int mii_reg;
+       int bmcr;
 
        /*
         * Wait if the link is up, and autonegotiation is in progress
         * (ie - we're capable and it's not done)
         */
        mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
+       if (mii_reg < 0)
+               return mii_reg;
 
        /*
         * If we already saw the link up, and it hasn't gone down, then
@@ -229,6 +217,13 @@ int genphy_update_link(struct phy_device *phydev)
        if (phydev->link && mii_reg & BMSR_LSTATUS)
                return 0;
 
+       bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
+       if (bmcr < 0)
+               return bmcr;
+
+       if (!(bmcr & BMCR_ANENABLE))
+               return 0;
+
        if ((mii_reg & BMSR_ANEGCAPABLE) && !(mii_reg & BMSR_ANEGCOMPLETE)) {
                int i = 0;
 
@@ -255,12 +250,16 @@ int genphy_update_link(struct phy_device *phydev)
 
                        udelay(1000);   /* 1 ms */
                        mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
+                       if (mii_reg < 0)
+                               return mii_reg;
                }
                printf(" done\n");
                phydev->link = 1;
        } else {
                /* Read the link a second time to clear the latched state */
                mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
+               if (mii_reg < 0)
+                       return mii_reg;
 
                if (mii_reg & BMSR_LSTATUS)
                        phydev->link = 1;
@@ -280,23 +279,34 @@ int genphy_update_link(struct phy_device *phydev)
  *
  * Stolen from Linux's mii.c and phy_device.c
  */
-static int genphy_parse_link(struct phy_device *phydev)
+int genphy_parse_link(struct phy_device *phydev)
 {
        int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
 
+       if (mii_reg < 0)
+               return mii_reg;
+
        /* We're using autonegotiation */
        if (mii_reg & BMSR_ANEGCAPABLE) {
-               u32 lpa = 0;
-               u32 gblpa = 0;
+               int ret;
+               u16 lpa;
+               u16 gblpa = 0;
+               int estatus = 0;
 
                /* Check for gigabit capability */
                if (mii_reg & BMSR_ERCAP) {
                        /* We want a list of states supported by
                         * both PHYs in the link
                         */
-                       gblpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000);
-                       gblpa &= phy_read(phydev,
-                                       MDIO_DEVAD_NONE, MII_CTRL1000) << 2;
+                       ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000);
+                       if (ret < 0)
+                               return ret;
+                       gblpa = ret;
+
+                       ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
+                       if (ret < 0)
+                               return ret;
+                       gblpa &= ret << 2;
                }
 
                /* Set the baseline so we only have to set them
@@ -316,8 +326,15 @@ static int genphy_parse_link(struct phy_device *phydev)
                        return 0;
                }
 
-               lpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
-               lpa &= phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA);
+               ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
+               if (ret < 0)
+                       return ret;
+               lpa = ret;
+
+               ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA);
+               if (ret < 0)
+                       return ret;
+               lpa &= ret;
 
                if (lpa & (LPA_100FULL | LPA_100HALF)) {
                        phydev->speed = SPEED_100;
@@ -327,8 +344,33 @@ static int genphy_parse_link(struct phy_device *phydev)
 
                } else if (lpa & LPA_10FULL)
                        phydev->duplex = DUPLEX_FULL;
+
+               /*
+                * Extended status may indicate that the PHY supports
+                * 1000BASE-T/X even though the 1000BASE-T registers
+                * are missing. In this case we can't tell whether the
+                * peer also supports it, so we only check extended
+                * status if the 1000BASE-T registers are actually
+                * missing.
+                */
+               if ((mii_reg & BMSR_ESTATEN) && !(mii_reg & BMSR_ERCAP))
+                       estatus = phy_read(phydev, MDIO_DEVAD_NONE,
+                                          MII_ESTATUS);
+               if (estatus < 0)
+                       return estatus;
+
+               if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_XHALF |
+                               ESTATUS_1000_TFULL | ESTATUS_1000_THALF)) {
+                       phydev->speed = SPEED_1000;
+                       if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_TFULL))
+                               phydev->duplex = DUPLEX_FULL;
+               }
+
        } else {
-               u32 bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
+               int bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
+
+               if (bmcr < 0)
+                       return bmcr;
 
                phydev->speed = SPEED_10;
                phydev->duplex = DUPLEX_HALF;
@@ -384,6 +426,10 @@ int genphy_config(struct phy_device *phydev)
                        features |= SUPPORTED_1000baseT_Full;
                if (val & ESTATUS_1000_THALF)
                        features |= SUPPORTED_1000baseT_Half;
+               if (val & ESTATUS_1000_XFULL)
+                       features |= SUPPORTED_1000baseX_Full;
+               if (val & ESTATUS_1000_XHALF)
+                       features |= SUPPORTED_1000baseX_Half;
        }
 
        phydev->supported = features;
@@ -430,6 +476,12 @@ int phy_init(void)
 #ifdef CONFIG_PHY_DAVICOM
        phy_davicom_init();
 #endif
+#ifdef CONFIG_PHY_ET1011C
+       phy_et1011c_init();
+#endif
+#ifdef CONFIG_PHY_ICPLUS
+       phy_icplus_init();
+#endif
 #ifdef CONFIG_PHY_LXT
        phy_lxt_init();
 #endif
@@ -646,6 +698,7 @@ static struct phy_device *get_phy_device(struct mii_dev *bus, int addr,
 
 int phy_reset(struct phy_device *phydev)
 {
+       int err;
        int reg;
        int timeout = 500;
        int devad = MDIO_DEVAD_NONE;
@@ -668,9 +721,10 @@ int phy_reset(struct phy_device *phydev)
 
        reg |= BMCR_RESET;
 
-       if (phy_write(phydev, devad, MII_BMCR, reg) < 0) {
+       err = phy_write(phydev, devad, MII_BMCR, reg);
+       if (err < 0) {
                debug("PHY reset failed\n");
-               return -1;
+               return err;
        }
 
 #ifdef CONFIG_PHY_RESET_DELAY
@@ -681,19 +735,19 @@ int phy_reset(struct phy_device *phydev)
         * auto-clearing).  This should happen within 0.5 seconds per the
         * IEEE spec.
         */
-       while ((reg & BMCR_RESET) && timeout--) {
+       while ((reg & BMCR_RESET) && timeout-- >= 0) {
                reg = phy_read(phydev, devad, MII_BMCR);
 
                if (reg < 0) {
                        debug("PHY status read failed\n");
-                       return -1;
+                       return reg;
                }
                udelay(1000);
        }
 
        if (reg & BMCR_RESET) {
                puts("PHY reset timed out\n");
-               return -1;
+               return -ETIMEDOUT;
        }
 
        return 0;