Re: [PATCH net-next v10 3/5] net: phy: micrel: Add loopback support

Maxime Chevallier
2025-03-13T08:57:45Z

Hello Gerhard,

On Wed, 12 Mar 2025 21:30:08 +0100
Gerhard Engleder <gerhard@engleder-embedded.com> wrote:

> The KSZ9031 PHYs requires full duplex for loopback mode. Add PHY
> specific set_loopback() to ensure this.
>
> Signed-off-by: Gerhard Engleder <gerhard@engleder-embedded.com>
> ---
> drivers/net/phy/micrel.c | 24 ++++++++++++++++++++++++
> 1 file changed, 24 insertions(+)
>
> diff --git a/drivers/net/phy/micrel.c b/drivers/net/phy/micrel.c
> index 289e1d56aa65..24882d30f685 100644
> --- a/drivers/net/phy/micrel.c
> +++ b/drivers/net/phy/micrel.c
> @@ -1032,6 +1032,29 @@ static int ksz9021_config_init(struct phy_device *phydev)
> #define MII_KSZ9031RN_EDPD 0x23
> #define MII_KSZ9031RN_EDPD_ENABLE BIT(0)
>
> +static int ksz9031_set_loopback(struct phy_device *phydev, bool enable,
> + int speed)
> +{
> + u16 ctl = BMCR_LOOPBACK;
> + int val;
> +
> + if (!enable)
> + return genphy_loopback(phydev, enable, 0);
> +
> + if (speed == SPEED_10 || speed == SPEED_100 || speed == SPEED_1000)
> + phydev->speed = speed;
> + else if (speed)
> + return -EINVAL;
> + phydev->duplex = DUPLEX_FULL;
> +
> + ctl |= mii_bmcr_encode_fixed(phydev->speed, phydev->duplex);
> +
> + phy_write(phydev, MII_BMCR, ctl);
> +
> + return phy_read_poll_timeout(phydev, MII_BMSR, val, val & BMSR_LSTATUS,
> + 5000, 500000, true);

Maybe I don't fully get it, but it looks to me that you poll, waiting
for the link to become up. As you are in local loopback mode, how does
that work ? Do you need connection to an active LP for loopback to
work, or does the BMSR_LSTATUS bit behave differently under local
loopback ?

Maxime