net: phy: convert read-modify-write to phy_modify()
authorRussell King <rmk+kernel@armlinux.org.uk>
Tue, 2 Jan 2018 10:58:58 +0000 (10:58 +0000)
committerDavid S. Miller <davem@davemloft.net>
Wed, 3 Jan 2018 16:00:23 +0000 (11:00 -0500)
Convert read-modify-write sequences in at803x, Marvell and core phylib
to use phy_modify() to ensure safety.

Signed-off-by: Russell King <rmk+kernel@armlinux.org.uk>
Reviewed-by: Andrew Lunn <andrew@lunn.ch>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/phy/at803x.c
drivers/net/phy/marvell.c
drivers/net/phy/phy_device.c

index 1e190f3bca63a16486cf897184fb36c95a6b6ccf..c2715908d43ecaae0dd756bcb4d71c054f7e11bd 100644 (file)
@@ -215,34 +215,22 @@ static int at803x_suspend(struct phy_device *phydev)
        int value;
        int wol_enabled;
 
-       mutex_lock(&phydev->lock);
-
        value = phy_read(phydev, AT803X_INTR_ENABLE);
        wol_enabled = value & AT803X_INTR_ENABLE_WOL;
 
-       value = phy_read(phydev, MII_BMCR);
-
        if (wol_enabled)
-               value |= BMCR_ISOLATE;
+               value = BMCR_ISOLATE;
        else
-               value |= BMCR_PDOWN;
+               value = BMCR_PDOWN;
 
-       phy_write(phydev, MII_BMCR, value);
-
-       mutex_unlock(&phydev->lock);
+       phy_modify(phydev, MII_BMCR, 0, value);
 
        return 0;
 }
 
 static int at803x_resume(struct phy_device *phydev)
 {
-       int value;
-
-       value = phy_read(phydev, MII_BMCR);
-       value &= ~(BMCR_PDOWN | BMCR_ISOLATE);
-       phy_write(phydev, MII_BMCR, value);
-
-       return 0;
+       return phy_modify(phydev, MII_BMCR, ~(BMCR_PDOWN | BMCR_ISOLATE), 0);
 }
 
 static int at803x_probe(struct phy_device *phydev)
index ac39d909d07a864372f92e30592794673e0bc79e..2bd38962b5d2eacc9e8e4755972def4b4e903666 100644 (file)
@@ -471,7 +471,7 @@ static int m88e1121_config_aneg(struct phy_device *phydev)
 
        if (phy_interface_is_rgmii(phydev)) {
                err = m88e1121_config_aneg_rgmii_delays(phydev);
-               if (err)
+               if (err < 0)
                        return err;
        }
 
@@ -664,19 +664,14 @@ static int m88e1116r_config_init(struct phy_device *phydev)
 
 static int m88e3016_config_init(struct phy_device *phydev)
 {
-       int reg;
+       int ret;
 
        /* Enable Scrambler and Auto-Crossover */
-       reg = phy_read(phydev, MII_88E3016_PHY_SPEC_CTRL);
-       if (reg < 0)
-               return reg;
-
-       reg &= ~MII_88E3016_DISABLE_SCRAMBLER;
-       reg |= MII_88E3016_AUTO_MDIX_CROSSOVER;
-
-       reg = phy_write(phydev, MII_88E3016_PHY_SPEC_CTRL, reg);
-       if (reg < 0)
-               return reg;
+       ret = phy_modify(phydev, MII_88E3016_PHY_SPEC_CTRL,
+                        ~MII_88E3016_DISABLE_SCRAMBLER,
+                        MII_88E3016_AUTO_MDIX_CROSSOVER);
+       if (ret < 0)
+               return ret;
 
        return marvell_config_init(phydev);
 }
@@ -685,42 +680,34 @@ static int m88e1111_config_init_hwcfg_mode(struct phy_device *phydev,
                                           u16 mode,
                                           int fibre_copper_auto)
 {
-       int temp;
-
-       temp = phy_read(phydev, MII_M1111_PHY_EXT_SR);
-       if (temp < 0)
-               return temp;
-
-       temp &= ~(MII_M1111_HWCFG_MODE_MASK |
-                 MII_M1111_HWCFG_FIBER_COPPER_AUTO |
-                 MII_M1111_HWCFG_FIBER_COPPER_RES);
-       temp |= mode;
-
        if (fibre_copper_auto)
-               temp |= MII_M1111_HWCFG_FIBER_COPPER_AUTO;
+               mode |= MII_M1111_HWCFG_FIBER_COPPER_AUTO;
 
-       return phy_write(phydev, MII_M1111_PHY_EXT_SR, temp);
+       return phy_modify(phydev, MII_M1111_PHY_EXT_SR,
+                         (u16)~(MII_M1111_HWCFG_MODE_MASK |
+                                MII_M1111_HWCFG_FIBER_COPPER_AUTO |
+                                MII_M1111_HWCFG_FIBER_COPPER_RES),
+                         mode);
 }
 
 static int m88e1111_config_init_rgmii_delays(struct phy_device *phydev)
 {
-       int temp;
-
-       temp = phy_read(phydev, MII_M1111_PHY_EXT_CR);
-       if (temp < 0)
-               return temp;
+       int delay;
 
        if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID) {
-               temp |= (MII_M1111_RGMII_RX_DELAY | MII_M1111_RGMII_TX_DELAY);
+               delay = MII_M1111_RGMII_RX_DELAY | MII_M1111_RGMII_TX_DELAY;
        } else if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) {
-               temp &= ~MII_M1111_RGMII_TX_DELAY;
-               temp |= MII_M1111_RGMII_RX_DELAY;
+               delay = MII_M1111_RGMII_RX_DELAY;
        } else if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) {
-               temp &= ~MII_M1111_RGMII_RX_DELAY;
-               temp |= MII_M1111_RGMII_TX_DELAY;
+               delay = MII_M1111_RGMII_TX_DELAY;
+       } else {
+               delay = 0;
        }
 
-       return phy_write(phydev, MII_M1111_PHY_EXT_CR, temp);
+       return phy_modify(phydev, MII_M1111_PHY_EXT_CR,
+                         (u16)~(MII_M1111_RGMII_RX_DELAY |
+                                MII_M1111_RGMII_TX_DELAY),
+                         delay);
 }
 
 static int m88e1111_config_init_rgmii(struct phy_device *phydev)
@@ -766,7 +753,7 @@ static int m88e1111_config_init_rtbi(struct phy_device *phydev)
        int err;
 
        err = m88e1111_config_init_rgmii_delays(phydev);
-       if (err)
+       if (err < 0)
                return err;
 
        err = m88e1111_config_init_hwcfg_mode(
@@ -793,7 +780,7 @@ static int m88e1111_config_init(struct phy_device *phydev)
 
        if (phy_interface_is_rgmii(phydev)) {
                err = m88e1111_config_init_rgmii(phydev);
-               if (err)
+               if (err < 0)
                        return err;
        }
 
@@ -834,7 +821,6 @@ static int m88e1121_config_init(struct phy_device *phydev)
 static int m88e1510_config_init(struct phy_device *phydev)
 {
        int err;
-       int temp;
 
        /* SGMII-to-Copper mode initialization */
        if (phydev->interface == PHY_INTERFACE_MODE_SGMII) {
@@ -846,16 +832,15 @@ static int m88e1510_config_init(struct phy_device *phydev)
                        return err;
 
                /* In reg 20, write MODE[2:0] = 0x1 (SGMII to Copper) */
-               temp = phy_read(phydev, MII_88E1510_GEN_CTRL_REG_1);
-               temp &= ~MII_88E1510_GEN_CTRL_REG_1_MODE_MASK;
-               temp |= MII_88E1510_GEN_CTRL_REG_1_MODE_SGMII;
-               err = phy_write(phydev, MII_88E1510_GEN_CTRL_REG_1, temp);
+               err = phy_modify(phydev, MII_88E1510_GEN_CTRL_REG_1,
+                                ~MII_88E1510_GEN_CTRL_REG_1_MODE_MASK,
+                                MII_88E1510_GEN_CTRL_REG_1_MODE_SGMII);
                if (err < 0)
                        return err;
 
                /* PHY reset is necessary after changing MODE[2:0] */
-               temp |= MII_88E1510_GEN_CTRL_REG_1_RESET;
-               err = phy_write(phydev, MII_88E1510_GEN_CTRL_REG_1, temp);
+               err = phy_modify(phydev, MII_88E1510_GEN_CTRL_REG_1, 0,
+                                MII_88E1510_GEN_CTRL_REG_1_RESET);
                if (err < 0)
                        return err;
 
@@ -961,7 +946,6 @@ static int m88e1149_config_init(struct phy_device *phydev)
 
 static int m88e1145_config_init_rgmii(struct phy_device *phydev)
 {
-       int temp;
        int err;
 
        err = m88e1111_config_init_rgmii_delays(phydev);
@@ -973,15 +957,9 @@ static int m88e1145_config_init_rgmii(struct phy_device *phydev)
                if (err < 0)
                        return err;
 
-               temp = phy_read(phydev, 0x1e);
-               if (temp < 0)
-                       return temp;
-
-               temp &= 0xf03f;
-               temp |= 2 << 9; /* 36 ohm */
-               temp |= 2 << 6; /* 39 ohm */
-
-               err = phy_write(phydev, 0x1e, temp);
+               err = phy_modify(phydev, 0x1e, 0xf03f,
+                                2 << 9 | /* 36 ohm */
+                                2 << 6); /* 39 ohm */
                if (err < 0)
                        return err;
 
index be13b5d6a8bf7e2b4e94fa4d20c4fb53ab9ed1a3..2c5b2e041c0feb4bd0515fc6f2f6a0b0a42ae9d4 100644 (file)
@@ -1368,9 +1368,8 @@ static int genphy_config_eee_advert(struct phy_device *phydev)
  */
 int genphy_setup_forced(struct phy_device *phydev)
 {
-       int ctl = phy_read(phydev, MII_BMCR);
+       u16 ctl = 0;
 
-       ctl &= BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN;
        phydev->pause = 0;
        phydev->asym_pause = 0;
 
@@ -1382,7 +1381,8 @@ int genphy_setup_forced(struct phy_device *phydev)
        if (DUPLEX_FULL == phydev->duplex)
                ctl |= BMCR_FULLDPLX;
 
-       return phy_write(phydev, MII_BMCR, ctl);
+       return phy_modify(phydev, MII_BMCR,
+                         BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN, ctl);
 }
 EXPORT_SYMBOL(genphy_setup_forced);
 
@@ -1392,17 +1392,9 @@ EXPORT_SYMBOL(genphy_setup_forced);
  */
 int genphy_restart_aneg(struct phy_device *phydev)
 {
-       int ctl = phy_read(phydev, MII_BMCR);
-
-       if (ctl < 0)
-               return ctl;
-
-       ctl |= BMCR_ANENABLE | BMCR_ANRESTART;
-
        /* Don't isolate the PHY if we're negotiating */
-       ctl &= ~BMCR_ISOLATE;
-
-       return phy_write(phydev, MII_BMCR, ctl);
+       return phy_modify(phydev, MII_BMCR, ~BMCR_ISOLATE,
+                         BMCR_ANENABLE | BMCR_ANRESTART);
 }
 EXPORT_SYMBOL(genphy_restart_aneg);
 
@@ -1668,44 +1660,20 @@ EXPORT_SYMBOL(genphy_config_init);
 
 int genphy_suspend(struct phy_device *phydev)
 {
-       int value;
-
-       mutex_lock(&phydev->lock);
-
-       value = phy_read(phydev, MII_BMCR);
-       phy_write(phydev, MII_BMCR, value | BMCR_PDOWN);
-
-       mutex_unlock(&phydev->lock);
-
-       return 0;
+       return phy_modify(phydev, MII_BMCR, 0, BMCR_PDOWN);
 }
 EXPORT_SYMBOL(genphy_suspend);
 
 int genphy_resume(struct phy_device *phydev)
 {
-       int value;
-
-       value = phy_read(phydev, MII_BMCR);
-       phy_write(phydev, MII_BMCR, value & ~BMCR_PDOWN);
-
-       return 0;
+       return phy_modify(phydev, MII_BMCR, ~BMCR_PDOWN, 0);
 }
 EXPORT_SYMBOL(genphy_resume);
 
 int genphy_loopback(struct phy_device *phydev, bool enable)
 {
-       int value;
-
-       value = phy_read(phydev, MII_BMCR);
-       if (value < 0)
-               return value;
-
-       if (enable)
-               value |= BMCR_LOOPBACK;
-       else
-               value &= ~BMCR_LOOPBACK;
-
-       return phy_write(phydev, MII_BMCR, value);
+       return phy_modify(phydev, MII_BMCR, ~BMCR_LOOPBACK,
+                         enable ? BMCR_LOOPBACK : 0);
 }
 EXPORT_SYMBOL(genphy_loopback);