net: phy: micrel: Dynamically control external clock of KSZ PHY
Subsystems (4)
PATCH v5 1/1 net: phy: micrel: Dynamically control external clock of KSZ PHY
On the i.MX6ULL-14x14-EVK board, enet1_ref and enet2_ref are used as the clock sources for two external KSZ PHYs. However, after closing the two FEC ports, the clk_enable_count of the enet1_ref and enet2_ref clocks is not 0. The root cause is that since the commit 985329462723 ("net: phy:
micrel: use devm_clk_get_optional_enabled for the rmii-ref clock"), the external clock of KSZ PHY has been enabled when the PHY driver probes, and it can only be disabled when the PHY driver is removed. This causes the clock to continue working when the system is suspended or the network port is down.
Although Heiko explained in the commit message that the patch was because some clock suppliers need to enable the clock to get the valid clock rate , it seems that the simple fix is to disable the clock after getting the clock rate to solve the current problem. This is indeed true, but we need to admit that Heiko's patch has been applied for more than a year, and we cannot guarantee whether there are platforms that only enable rmii-ref in the KSZ PHY driver during this period. If this is the case, disabling rmii-ref will cause RMII on these platforms to not work.
Secondly, commit 99ac4cbcc2a5 ("net: phy: micrel: allow usage of generic ethernet-phy clock") just simply enables the generic clock permanently, which seems like the generic clock may only be enabled in the PHY driver. If we simply disable the generic clock, RMII may not work. If we keep it as it is, the platform using the generic clock will have the same problem as the i.MX6ULL platform.
To solve this problem, the clock is enabled when phy_driver::resume() is called, and the clock is disabled when phy_driver::suspend() is called. Since phy_driver::resume() and phy_driver::suspend() are not called in pairs, an additional clk_enable flag is added. When phy_driver::suspend() is called, the clock is disabled only if clk_enable is true. Conversely, when phy_driver::resume() is called, the clock is enabled if clk_enable is false.
The changes that introduced the problem were only a few lines, while the current fix is about a hundred lines, which seems out of proportion, but it is necessary because kszphy_probe() is used by multiple KSZ PHYs and we need to fix all of them.
---
v1 link: https://lore.kernel.org/imx/[email protected]/
v2 changes: only refine the commit message.
v2 link: https://lore.kernel.org/imx/[email protected]/
v3 changes: disable clock after getting the clock rate in kszphy_probe() v3 link: https://lore.kernel.org/imx/[email protected]/
v4 changes: add more detailed explanation to the commit message. v4 link: https://lore.kernel.org/imx/[email protected]/
v5 changes:
1. Add generic functions kszphy_generic_resume() and kszphy_generic_suspend()
2. Move the comments of ksz8041_resume()/ksz8041_suspend() to definition
3. Add ksz8061_suspend() and lan8804_resume()
4. Return failure if the clk is an error pointer in kszphy_probe()
--- drivers/net/phy/micrel.c | 114 ++++++++++++++++++++++++++++++++++----- 1 file changed, 101 insertions(+), 13 deletions(-)
drivers/net/phy/micrel.c +101 -13
@@ -432,10 +432,12 @@ struct kszphy_ptp_priv {struct kszphy_priv {struct kszphy_ptp_priv ptp_priv;const struct kszphy_type *type; +struct clk *clk;int led_mode;u16 vct_ctrl1000;bool rmii_ref_clk_sel;bool rmii_ref_clk_sel_val; +bool clk_enable;u64 stats[ARRAY_SIZE (kszphy_hw_stats)]; };
@@ -2050,6 +2052,46 @@ static void kszphy_get_stats(struct phy_device *phydev, data[i] =kszphy_get_stat (phydev, i); } +static void kszphy_enable_clk (struct phy_device *phydev) +{ +struct kszphy_priv *priv = phydev->priv; + +if (!priv->clk_enable && priv->clk) { +clk_prepare_enable (priv->clk); + priv->clk_enable = true; + } +} + +static void kszphy_disable_clk (struct phy_device *phydev) +{ +struct kszphy_priv *priv = phydev->priv; + +if (priv->clk_enable && priv->clk) { +clk_disable_unprepare (priv->clk); + priv->clk_enable = false; + } +} + +static int kszphy_generic_resume (struct phy_device *phydev) +{ +kszphy_enable_clk (phydev); + +return genphy_resume (phydev); +} + +static int kszphy_generic_suspend (struct phy_device *phydev) +{ +int ret; + + ret =genphy_suspend (phydev); +if (ret) +return ret; + +kszphy_disable_clk (phydev); + +return 0 ; +} +static int kszphy_suspend (struct phy_device *phydev) {/* Disable PHY Interrupts */
@@ -2059,7 +2101,7 @@ static int kszphy_suspend(struct phy_device *phydev) phydev->drv->config_intr (phydev); } -return genphy_suspend (phydev); +return kszphy_generic_suspend (phydev); }static void kszphy_parse_led_mode (struct phy_device *phydev)
@@ -2090,7 +2132,9 @@ static int kszphy_resume(struct phy_device *phydev) {int ret; -genphy_resume (phydev); + ret =kszphy_generic_resume (phydev); +if (ret) +return ret;/* After switching from power-down to normal mode, an internal global * reset is automatically generated. Wait a minimum of 1 ms before
@@ -2112,6 +2156,24 @@ static int kszphy_resume(struct phy_device *phydev)return 0 ; } +/* Because of errata DS80000700A, receiver error following software +* power down. Suspend and resume callbacks only disable and enable +* external rmii reference clock. +*/ +static int ksz8041_resume (struct phy_device *phydev) +{ +kszphy_enable_clk (phydev); + +return 0 ; +} + +static int ksz8041_suspend (struct phy_device *phydev) +{ +kszphy_disable_clk (phydev); + +return 0 ; +} +static int ksz9477_resume (struct phy_device *phydev) {int ret;
@@ -2159,7 +2221,10 @@ static int ksz8061_resume(struct phy_device *phydev)if (!(ret &BMCR_PDOWN ))return 0 ; -genphy_resume (phydev); + ret =kszphy_generic_resume (phydev); +if (ret) +return ret; +usleep_range (1000 ,2000 );/* Re-program the value after chip is reset. */
@@ -2177,6 +2242,11 @@ static int ksz8061_resume(struct phy_device *phydev)return 0 ; } +static int ksz8061_suspend (struct phy_device *phydev) +{ +return kszphy_suspend (phydev); +} +static int kszphy_probe (struct phy_device *phydev) {const struct kszphy_type *type = phydev->drv->driver_data;
@@ -2217,10 +2287,14 @@ static int kszphy_probe(struct phy_device *phydev) }else if (!clk) {/* unnamed clock from the generic ethernet-phy binding */ clk =devm_clk_get_optional_enabled (&phydev->mdio.dev,NULL ); -if (IS_ERR (clk)) -return PTR_ERR (clk); } +if (IS_ERR (clk)) +return PTR_ERR (clk); + +clk_disable_unprepare (clk); + priv->clk = clk; +if (ksz8041_fiber_mode (phydev)) phydev->port =PORT_FIBRE ;
@@ -5290,6 +5364,21 @@ static int lan8841_probe(struct phy_device *phydev)return 0 ; } +static int lan8804_resume (struct phy_device *phydev) +{ +return kszphy_resume (phydev); +} + +static int lan8804_suspend (struct phy_device *phydev) +{ +return kszphy_generic_suspend (phydev); +} + +static int lan8841_resume (struct phy_device *phydev) +{ +return kszphy_generic_resume (phydev); +} +static int lan8841_suspend (struct phy_device *phydev) {struct kszphy_priv *priv = phydev->priv;
@@ -5298,7 +5387,7 @@ static int lan8841_suspend(struct phy_device *phydev)if (ptp_priv->ptp_clock)ptp_cancel_worker_sync (ptp_priv->ptp_clock); -return genphy_suspend (phydev); +return kszphy_generic_suspend (phydev); }static struct phy_driver ksphy_driver[] = {
@@ -5358,9 +5447,8 @@ static struct phy_driver ksphy_driver[] = { .get_sset_count = kszphy_get_sset_count, .get_strings = kszphy_get_strings, .get_stats = kszphy_get_stats, -/* No suspend/resume callbacks because of errata DS80000700A, -* receiver error following software power down. -*/ + .suspend = ksz8041_suspend, + .resume = ksz8041_resume, }, { .phy_id = PHY_ID_KSZ8041RNLI, .phy_id_mask = MICREL_PHY_ID_MASK,
@@ -5436,7 +5524,7 @@ static struct phy_driver ksphy_driver[] = { .soft_reset = genphy_soft_reset, .config_intr = kszphy_config_intr, .handle_interrupt = kszphy_handle_interrupt, - .suspend = kszphy_suspend, + .suspend = ksz8061_suspend, .resume = ksz8061_resume, }, { .phy_id = PHY_ID_KSZ9021,
@@ -5507,8 +5595,8 @@ static struct phy_driver ksphy_driver[] = { .get_sset_count = kszphy_get_sset_count, .get_strings = kszphy_get_strings, .get_stats = kszphy_get_stats, - .suspend = genphy_suspend, - .resume = kszphy_resume, + .suspend = lan8804_suspend, + .resume = lan8804_resume, .config_intr = lan8804_config_intr, .handle_interrupt = lan8804_handle_interrupt, }, {
@@ -5526,7 +5614,7 @@ static struct phy_driver ksphy_driver[] = { .get_strings = kszphy_get_strings, .get_stats = kszphy_get_stats, .suspend = lan8841_suspend, - .resume = genphy_resume, + .resume = lan8841_resume, .cable_test_start = lan8814_cable_test_start, .cable_test_get_status = ksz886x_cable_test_get_status, }, {
Signature · 1 line
2.34.1
Quoted 34 lines · Wei Fang
On the i.MX6ULL-14x14-EVK board,
enet1_refandenet2_refare used as the clock sources for two external KSZ PHYs. However, after closing the two FEC ports, theclk_enable_countof theenet1_refandenet2_refclocks is not 0. The root cause is that since the commit 985329462723 ("net: phy:
micrel: usedevm_clk_get_optional_enabledfor the rmii-ref clock"), the external clock of KSZ PHY has been enabled when the PHY driver probes, and it can only be disabled when the PHY driver is removed. This causes the clock to continue working when the system is suspended or the network port is down.Although Heiko explained in the commit message that the patch was because some clock suppliers need to enable the clock to get the valid clock rate , it seems that the simple fix is to disable the clock after getting the clock rate to solve the current problem. This is indeed true, but we need to admit that Heiko's patch has been applied for more than a year, and we cannot guarantee whether there are platforms that only enable rmii-ref in the KSZ PHY driver during this period. If this is the case, disabling rmii-ref will cause RMII on these platforms to not work.
Secondly, commit
99ac4cbcc2a5("net: phy: micrel: allow usage of generic ethernet-phy clock") just simply enables the generic clock permanently, which seems like the generic clock may only be enabled in the PHY driver. If we simply disable the generic clock, RMII may not work. If we keep it as it is, the platform using the generic clock will have the same problem as the i.MX6ULL platform.To solve this problem, the clock is enabled when
phy_driver::resume()is called, and the clock is disabled whenphy_driver::suspend()is called. Sincephy_driver::resume()andphy_driver::suspend()are not called in pairs, an additionalclk_enableflag is added. Whenphy_driver::suspend()is called, the clock is disabled only ifclk_enableis true. Conversely, whenphy_driver::resume()is called, the clock is enabled ifclk_enableis false.
Quoted 8 lines
The changes that introduced the problem were only a few lines, while the current fix is about a hundred lines, which seems out of proportion, but it is necessary because
kszphy_probe()is used by multiple KSZ PHYs and we need to fix all of them.Fixes 985329462723 ("net: phy: micrel: use devm_clk_get_optional_enabled for the rmii-ref clock")Fixes 99ac4cbcc2a5 ("net: phy: micrel: allow usage of generic ethernet-phy clock")Signed-off-by Wei Fang <[email protected]>
Andrew
Quoted 34 lines · Wei Fang
On the i.MX6ULL-14x14-EVK board,
enet1_refandenet2_refare used as the clock sources for two external KSZ PHYs. However, after closing the two FEC ports, theclk_enable_countof theenet1_refandenet2_refclocks is not 0. The root cause is that since the commit 985329462723 ("net: phy:
micrel: usedevm_clk_get_optional_enabledfor the rmii-ref clock"), the external clock of KSZ PHY has been enabled when the PHY driver probes, and it can only be disabled when the PHY driver is removed. This causes the clock to continue working when the system is suspended or the network port is down.Although Heiko explained in the commit message that the patch was because some clock suppliers need to enable the clock to get the valid clock rate , it seems that the simple fix is to disable the clock after getting the clock rate to solve the current problem. This is indeed true, but we need to admit that Heiko's patch has been applied for more than a year, and we cannot guarantee whether there are platforms that only enable rmii-ref in the KSZ PHY driver during this period. If this is the case, disabling rmii-ref will cause RMII on these platforms to not work.
Secondly, commit
99ac4cbcc2a5("net: phy: micrel: allow usage of generic ethernet-phy clock") just simply enables the generic clock permanently, which seems like the generic clock may only be enabled in the PHY driver. If we simply disable the generic clock, RMII may not work. If we keep it as it is, the platform using the generic clock will have the same problem as the i.MX6ULL platform.To solve this problem, the clock is enabled when
phy_driver::resume()is called, and the clock is disabled whenphy_driver::suspend()is called. Sincephy_driver::resume()andphy_driver::suspend()are not called in pairs, an additionalclk_enableflag is added. Whenphy_driver::suspend()is called, the clock is disabled only ifclk_enableis true. Conversely, whenphy_driver::resume()is called, the clock is enabled ifclk_enableis false.
Quoted 8 lines
The changes that introduced the problem were only a few lines, while the current fix is about a hundred lines, which seems out of proportion, but it is necessary because
kszphy_probe()is used by multiple KSZ PHYs and we need to fix all of them.Fixes 985329462723 ("net: phy: micrel: use devm_clk_get_optional_enabled for the rmii-ref clock")Fixes 99ac4cbcc2a5 ("net: phy: micrel: allow usage of generic ethernet-phy clock")Signed-off-by Wei Fang <[email protected]>
Signature · 1 line
Florian
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 11 lines · On Tue, 17 Dec 2024 14:35:00 +0800 you wrote:
On the i.MX6ULL-14x14-EVK board,
enet1_refandenet2_refare used as the clock sources for two external KSZ PHYs. However, after closing the two FEC ports, theclk_enable_countof theenet1_refandenet2_refclocks is not 0. The root cause is that since the commit 985329462723 ("net: phy:
micrel: usedevm_clk_get_optional_enabledfor the rmii-ref clock"), the external clock of KSZ PHY has been enabled when the PHY driver probes, and it can only be disabled when the PHY driver is removed. This causes the clock to continue working when the system is suspended or the network port is down.[...]
Here is the summary with links:
- [v5,net] net: phy: micrel: Dynamically control external clock of KSZ PHY
https://git.kernel.org/netdev/net/c/25c6a5ab151f
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html