net: phy: ensure Gigabit features are masked off if requested
[firefly-linux-kernel-4.4.55.git] / drivers / net / phy / phy_device.c
index 5d81c89ee52be02040508b6a1685a4ec93088121..82514e72b3d8b47538cc8c75a8e52f361d1361c8 100644 (file)
@@ -30,6 +30,7 @@
 #include <linux/mii.h>
 #include <linux/ethtool.h>
 #include <linux/phy.h>
+#include <linux/mdio.h>
 #include <linux/io.h>
 #include <linux/uaccess.h>
 
@@ -50,14 +51,17 @@ static void phy_device_release(struct device *dev)
        kfree(to_phy_device(dev));
 }
 
-static struct phy_driver genphy_driver;
+enum genphy_driver {
+       GENPHY_DRV_1G,
+       GENPHY_DRV_10G,
+       GENPHY_DRV_MAX
+};
+
+static struct phy_driver genphy_driver[GENPHY_DRV_MAX];
 
 static LIST_HEAD(phy_fixup_list);
 static DEFINE_MUTEX(phy_fixup_lock);
 
-static int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
-                            u32 flags, phy_interface_t interface);
-
 /**
  * phy_register_fixup - creates a new phy_fixup and adds it to the list
  * @bus_id: A string which matches phydev->dev.bus_id (or PHY_ANY_ID)
@@ -560,13 +564,13 @@ EXPORT_SYMBOL(phy_init_hw);
  *
  * Description: Called by drivers to attach to a particular PHY
  *     device. The phy_device is found, and properly hooked up
- *     to the phy_driver.  If no driver is attached, then the
- *     genphy_driver is used.  The phy_device is given a ptr to
+ *     to the phy_driver.  If no driver is attached, then a
+ *     generic driver is used.  The phy_device is given a ptr to
  *     the attaching device, and given a callback for link status
  *     change.  The phy_device is returned to the attaching driver.
  */
-static int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
-                            u32 flags, phy_interface_t interface)
+int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
+                     u32 flags, phy_interface_t interface)
 {
        struct device *d = &phydev->dev;
        int err;
@@ -575,12 +579,10 @@ static int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
         * exist, and we should use the genphy driver.
         */
        if (NULL == d->driver) {
-               if (phydev->is_c45) {
-                       pr_err("No driver for phy %x\n", phydev->phy_id);
-                       return -ENODEV;
-               }
-
-               d->driver = &genphy_driver.driver;
+               if (phydev->is_c45)
+                       d->driver = &genphy_driver[GENPHY_DRV_10G].driver;
+               else
+                       d->driver = &genphy_driver[GENPHY_DRV_1G].driver;
 
                err = d->driver->probe(d);
                if (err >= 0)
@@ -616,6 +618,7 @@ static int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
 
        return err;
 }
+EXPORT_SYMBOL(phy_attach_direct);
 
 /**
  * phy_attach - attach a network device to a particular PHY device
@@ -658,6 +661,7 @@ EXPORT_SYMBOL(phy_attach);
  */
 void phy_detach(struct phy_device *phydev)
 {
+       int i;
        phydev->attached_dev->phydev = NULL;
        phydev->attached_dev = NULL;
        phy_suspend(phydev);
@@ -667,8 +671,12 @@ void phy_detach(struct phy_device *phydev)
         * from the generic driver so that there's a chance a
         * real driver could be loaded
         */
-       if (phydev->dev.driver == &genphy_driver.driver)
-               device_release_driver(&phydev->dev);
+       for (i = 0; i < ARRAY_SIZE(genphy_driver); i++) {
+               if (phydev->dev.driver == &genphy_driver[i].driver) {
+                       device_release_driver(&phydev->dev);
+                       break;
+               }
+       }
 }
 EXPORT_SYMBOL(phy_detach);
 
@@ -711,7 +719,7 @@ int phy_resume(struct phy_device *phydev)
 static int genphy_config_advert(struct phy_device *phydev)
 {
        u32 advertise;
-       int oldadv, adv;
+       int oldadv, adv, bmsr;
        int err, changed = 0;
 
        /* Only allow advertising what this PHY supports */
@@ -736,26 +744,36 @@ static int genphy_config_advert(struct phy_device *phydev)
                changed = 1;
        }
 
+       bmsr = phy_read(phydev, MII_BMSR);
+       if (bmsr < 0)
+               return bmsr;
+
+       /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
+        * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
+        * logical 1.
+        */
+       if (!(bmsr & BMSR_ESTATEN))
+               return changed;
+
        /* Configure gigabit if it's supported */
+       adv = phy_read(phydev, MII_CTRL1000);
+       if (adv < 0)
+               return adv;
+
+       oldadv = adv;
+       adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
+
        if (phydev->supported & (SUPPORTED_1000baseT_Half |
                                 SUPPORTED_1000baseT_Full)) {
-               adv = phy_read(phydev, MII_CTRL1000);
-               if (adv < 0)
-                       return adv;
-
-               oldadv = adv;
-               adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
                adv |= ethtool_adv_to_mii_ctrl1000_t(advertise);
-
-               if (adv != oldadv) {
-                       err = phy_write(phydev, MII_CTRL1000, adv);
-
-                       if (err < 0)
-                               return err;
+               if (adv != oldadv)
                        changed = 1;
-               }
        }
 
+       err = phy_write(phydev, MII_CTRL1000, adv);
+       if (err < 0)
+               return err;
+
        return changed;
 }
 
@@ -847,6 +865,11 @@ int genphy_config_aneg(struct phy_device *phydev)
 }
 EXPORT_SYMBOL(genphy_config_aneg);
 
+static int gen10g_config_aneg(struct phy_device *phydev)
+{
+       return 0;
+}
+
 /**
  * genphy_update_link - update link status in @phydev
  * @phydev: target phy_device struct
@@ -978,6 +1001,33 @@ int genphy_read_status(struct phy_device *phydev)
 }
 EXPORT_SYMBOL(genphy_read_status);
 
+static int gen10g_read_status(struct phy_device *phydev)
+{
+       int devad, reg;
+       u32 mmd_mask = phydev->c45_ids.devices_in_package;
+
+       phydev->link = 1;
+
+       /* For now just lie and say it's 10G all the time */
+       phydev->speed = SPEED_10000;
+       phydev->duplex = DUPLEX_FULL;
+
+       for (devad = 0; mmd_mask; devad++, mmd_mask = mmd_mask >> 1) {
+               if (!(mmd_mask & 1))
+                       continue;
+
+               /* Read twice because link state is latched and a
+                * read moves the current state into the register
+                */
+               phy_read_mmd(phydev, devad, MDIO_STAT1);
+               reg = phy_read_mmd(phydev, devad, MDIO_STAT1);
+               if (reg < 0 || !(reg & MDIO_STAT1_LSTATUS))
+                       phydev->link = 0;
+       }
+
+       return 0;
+}
+
 static int genphy_config_init(struct phy_device *phydev)
 {
        int val;
@@ -1023,6 +1073,16 @@ static int genphy_config_init(struct phy_device *phydev)
 
        return 0;
 }
+
+static int gen10g_config_init(struct phy_device *phydev)
+{
+       /* Temporarily just say we support everything */
+       phydev->supported = SUPPORTED_10000baseT_Full;
+       phydev->advertising = SUPPORTED_10000baseT_Full;
+
+       return 0;
+}
+
 int genphy_suspend(struct phy_device *phydev)
 {
        int value;
@@ -1038,6 +1098,11 @@ int genphy_suspend(struct phy_device *phydev)
 }
 EXPORT_SYMBOL(genphy_suspend);
 
+static int gen10g_suspend(struct phy_device *phydev)
+{
+       return 0;
+}
+
 int genphy_resume(struct phy_device *phydev)
 {
        int value;
@@ -1053,6 +1118,11 @@ int genphy_resume(struct phy_device *phydev)
 }
 EXPORT_SYMBOL(genphy_resume);
 
+static int gen10g_resume(struct phy_device *phydev)
+{
+       return 0;
+}
+
 /**
  * phy_probe - probe and init a PHY device
  * @dev: device to probe and init
@@ -1173,7 +1243,8 @@ void phy_drivers_unregister(struct phy_driver *drv, int n)
 }
 EXPORT_SYMBOL(phy_drivers_unregister);
 
-static struct phy_driver genphy_driver = {
+static struct phy_driver genphy_driver[] = {
+{
        .phy_id         = 0xffffffff,
        .phy_id_mask    = 0xffffffff,
        .name           = "Generic PHY",
@@ -1184,7 +1255,18 @@ static struct phy_driver genphy_driver = {
        .suspend        = genphy_suspend,
        .resume         = genphy_resume,
        .driver         = { .owner = THIS_MODULE, },
-};
+}, {
+       .phy_id         = 0xffffffff,
+       .phy_id_mask    = 0xffffffff,
+       .name           = "Generic 10G PHY",
+       .config_init    = gen10g_config_init,
+       .features       = 0,
+       .config_aneg    = gen10g_config_aneg,
+       .read_status    = gen10g_read_status,
+       .suspend        = gen10g_suspend,
+       .resume         = gen10g_resume,
+       .driver         = {.owner = THIS_MODULE, },
+} };
 
 static int __init phy_init(void)
 {
@@ -1194,7 +1276,8 @@ static int __init phy_init(void)
        if (rc)
                return rc;
 
-       rc = phy_driver_register(&genphy_driver);
+       rc = phy_drivers_register(genphy_driver,
+                                 ARRAY_SIZE(genphy_driver));
        if (rc)
                mdio_bus_exit();
 
@@ -1203,7 +1286,8 @@ static int __init phy_init(void)
 
 static void __exit phy_exit(void)
 {
-       phy_driver_unregister(&genphy_driver);
+       phy_drivers_unregister(genphy_driver,
+                              ARRAY_SIZE(genphy_driver));
        mdio_bus_exit();
 }