Skip to content

Commit 25c6a5a

Browse files
Wei Fangkuba-moo
authored andcommitted
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 9853294 ("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 99ac4cb ("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. Fixes: 9853294 ("net: phy: micrel: use devm_clk_get_optional_enabled for the rmii-ref clock") Fixes: 99ac4cb ("net: phy: micrel: allow usage of generic ethernet-phy clock") Signed-off-by: Wei Fang <[email protected]> Reviewed-by: Andrew Lunn <[email protected]> Reviewed-by: Florian Fainelli <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 8faabc0 commit 25c6a5a

File tree

1 file changed

+101
-13
lines changed

1 file changed

+101
-13
lines changed

drivers/net/phy/micrel.c

Lines changed: 101 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,12 @@ struct kszphy_ptp_priv {
432432
struct kszphy_priv {
433433
struct kszphy_ptp_priv ptp_priv;
434434
const struct kszphy_type *type;
435+
struct clk *clk;
435436
int led_mode;
436437
u16 vct_ctrl1000;
437438
bool rmii_ref_clk_sel;
438439
bool rmii_ref_clk_sel_val;
440+
bool clk_enable;
439441
u64 stats[ARRAY_SIZE(kszphy_hw_stats)];
440442
};
441443

@@ -2050,6 +2052,46 @@ static void kszphy_get_stats(struct phy_device *phydev,
20502052
data[i] = kszphy_get_stat(phydev, i);
20512053
}
20522054

2055+
static void kszphy_enable_clk(struct phy_device *phydev)
2056+
{
2057+
struct kszphy_priv *priv = phydev->priv;
2058+
2059+
if (!priv->clk_enable && priv->clk) {
2060+
clk_prepare_enable(priv->clk);
2061+
priv->clk_enable = true;
2062+
}
2063+
}
2064+
2065+
static void kszphy_disable_clk(struct phy_device *phydev)
2066+
{
2067+
struct kszphy_priv *priv = phydev->priv;
2068+
2069+
if (priv->clk_enable && priv->clk) {
2070+
clk_disable_unprepare(priv->clk);
2071+
priv->clk_enable = false;
2072+
}
2073+
}
2074+
2075+
static int kszphy_generic_resume(struct phy_device *phydev)
2076+
{
2077+
kszphy_enable_clk(phydev);
2078+
2079+
return genphy_resume(phydev);
2080+
}
2081+
2082+
static int kszphy_generic_suspend(struct phy_device *phydev)
2083+
{
2084+
int ret;
2085+
2086+
ret = genphy_suspend(phydev);
2087+
if (ret)
2088+
return ret;
2089+
2090+
kszphy_disable_clk(phydev);
2091+
2092+
return 0;
2093+
}
2094+
20532095
static int kszphy_suspend(struct phy_device *phydev)
20542096
{
20552097
/* Disable PHY Interrupts */
@@ -2059,7 +2101,7 @@ static int kszphy_suspend(struct phy_device *phydev)
20592101
phydev->drv->config_intr(phydev);
20602102
}
20612103

2062-
return genphy_suspend(phydev);
2104+
return kszphy_generic_suspend(phydev);
20632105
}
20642106

20652107
static void kszphy_parse_led_mode(struct phy_device *phydev)
@@ -2090,7 +2132,9 @@ static int kszphy_resume(struct phy_device *phydev)
20902132
{
20912133
int ret;
20922134

2093-
genphy_resume(phydev);
2135+
ret = kszphy_generic_resume(phydev);
2136+
if (ret)
2137+
return ret;
20942138

20952139
/* After switching from power-down to normal mode, an internal global
20962140
* reset is automatically generated. Wait a minimum of 1 ms before
@@ -2112,6 +2156,24 @@ static int kszphy_resume(struct phy_device *phydev)
21122156
return 0;
21132157
}
21142158

2159+
/* Because of errata DS80000700A, receiver error following software
2160+
* power down. Suspend and resume callbacks only disable and enable
2161+
* external rmii reference clock.
2162+
*/
2163+
static int ksz8041_resume(struct phy_device *phydev)
2164+
{
2165+
kszphy_enable_clk(phydev);
2166+
2167+
return 0;
2168+
}
2169+
2170+
static int ksz8041_suspend(struct phy_device *phydev)
2171+
{
2172+
kszphy_disable_clk(phydev);
2173+
2174+
return 0;
2175+
}
2176+
21152177
static int ksz9477_resume(struct phy_device *phydev)
21162178
{
21172179
int ret;
@@ -2159,7 +2221,10 @@ static int ksz8061_resume(struct phy_device *phydev)
21592221
if (!(ret & BMCR_PDOWN))
21602222
return 0;
21612223

2162-
genphy_resume(phydev);
2224+
ret = kszphy_generic_resume(phydev);
2225+
if (ret)
2226+
return ret;
2227+
21632228
usleep_range(1000, 2000);
21642229

21652230
/* Re-program the value after chip is reset. */
@@ -2177,6 +2242,11 @@ static int ksz8061_resume(struct phy_device *phydev)
21772242
return 0;
21782243
}
21792244

2245+
static int ksz8061_suspend(struct phy_device *phydev)
2246+
{
2247+
return kszphy_suspend(phydev);
2248+
}
2249+
21802250
static int kszphy_probe(struct phy_device *phydev)
21812251
{
21822252
const struct kszphy_type *type = phydev->drv->driver_data;
@@ -2217,10 +2287,14 @@ static int kszphy_probe(struct phy_device *phydev)
22172287
} else if (!clk) {
22182288
/* unnamed clock from the generic ethernet-phy binding */
22192289
clk = devm_clk_get_optional_enabled(&phydev->mdio.dev, NULL);
2220-
if (IS_ERR(clk))
2221-
return PTR_ERR(clk);
22222290
}
22232291

2292+
if (IS_ERR(clk))
2293+
return PTR_ERR(clk);
2294+
2295+
clk_disable_unprepare(clk);
2296+
priv->clk = clk;
2297+
22242298
if (ksz8041_fiber_mode(phydev))
22252299
phydev->port = PORT_FIBRE;
22262300

@@ -5290,6 +5364,21 @@ static int lan8841_probe(struct phy_device *phydev)
52905364
return 0;
52915365
}
52925366

5367+
static int lan8804_resume(struct phy_device *phydev)
5368+
{
5369+
return kszphy_resume(phydev);
5370+
}
5371+
5372+
static int lan8804_suspend(struct phy_device *phydev)
5373+
{
5374+
return kszphy_generic_suspend(phydev);
5375+
}
5376+
5377+
static int lan8841_resume(struct phy_device *phydev)
5378+
{
5379+
return kszphy_generic_resume(phydev);
5380+
}
5381+
52935382
static int lan8841_suspend(struct phy_device *phydev)
52945383
{
52955384
struct kszphy_priv *priv = phydev->priv;
@@ -5298,7 +5387,7 @@ static int lan8841_suspend(struct phy_device *phydev)
52985387
if (ptp_priv->ptp_clock)
52995388
ptp_cancel_worker_sync(ptp_priv->ptp_clock);
53005389

5301-
return genphy_suspend(phydev);
5390+
return kszphy_generic_suspend(phydev);
53025391
}
53035392

53045393
static struct phy_driver ksphy_driver[] = {
@@ -5358,9 +5447,8 @@ static struct phy_driver ksphy_driver[] = {
53585447
.get_sset_count = kszphy_get_sset_count,
53595448
.get_strings = kszphy_get_strings,
53605449
.get_stats = kszphy_get_stats,
5361-
/* No suspend/resume callbacks because of errata DS80000700A,
5362-
* receiver error following software power down.
5363-
*/
5450+
.suspend = ksz8041_suspend,
5451+
.resume = ksz8041_resume,
53645452
}, {
53655453
.phy_id = PHY_ID_KSZ8041RNLI,
53665454
.phy_id_mask = MICREL_PHY_ID_MASK,
@@ -5436,7 +5524,7 @@ static struct phy_driver ksphy_driver[] = {
54365524
.soft_reset = genphy_soft_reset,
54375525
.config_intr = kszphy_config_intr,
54385526
.handle_interrupt = kszphy_handle_interrupt,
5439-
.suspend = kszphy_suspend,
5527+
.suspend = ksz8061_suspend,
54405528
.resume = ksz8061_resume,
54415529
}, {
54425530
.phy_id = PHY_ID_KSZ9021,
@@ -5507,8 +5595,8 @@ static struct phy_driver ksphy_driver[] = {
55075595
.get_sset_count = kszphy_get_sset_count,
55085596
.get_strings = kszphy_get_strings,
55095597
.get_stats = kszphy_get_stats,
5510-
.suspend = genphy_suspend,
5511-
.resume = kszphy_resume,
5598+
.suspend = lan8804_suspend,
5599+
.resume = lan8804_resume,
55125600
.config_intr = lan8804_config_intr,
55135601
.handle_interrupt = lan8804_handle_interrupt,
55145602
}, {
@@ -5526,7 +5614,7 @@ static struct phy_driver ksphy_driver[] = {
55265614
.get_strings = kszphy_get_strings,
55275615
.get_stats = kszphy_get_stats,
55285616
.suspend = lan8841_suspend,
5529-
.resume = genphy_resume,
5617+
.resume = lan8841_resume,
55305618
.cable_test_start = lan8814_cable_test_start,
55315619
.cable_test_get_status = ksz886x_cable_test_get_status,
55325620
}, {

0 commit comments

Comments
 (0)