Skip to contentSkip to conversation

Add Xilinx GMII2RGMII loopback support

PATCH Superseded Gerhard Engleder 2 participants 6 messages Aug 18, 2021

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Add Xilinx GMII2RGMII loopback support

The Xilinx GMII2RGMII driver overrides PHY driver functions in order to configure the device according to the link speed of the PHY attached to it. This is implemented for a normal link but not for loopback.

Andrew told me to use phy_loopback and this changes make phy_loopback work in combination with Xilinx GMII2RGMII.

Gerhard Engleder (2):
net: phy: Support set_loopback override
net: phy: gmii2rgmii: Support PHY loopback

 drivers/net/phy/phy_device.c        |  9 +++---
 drivers/net/phy/xilinx_gmii2rgmii.c | 46 ++++++++++++++++++++++-------
 2 files changed, 39 insertions(+), 16 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/2 net: phy: Support set_loopback override

phy_read_status and various other PHY functions support PHY specific overriding of driver functions by using a PHY specific pointer to the PHY driver. Add support of PHY specific override to phy_loopback too.

---
 drivers/net/phy/phy_device.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/net/phy/phy_device.c +4 -5
index 107aa6d7bc6b..ba5ad86ec826 100644
@@ -1821,11 +1821,10 @@ EXPORT_SYMBOL(phy_resume);
 
 int phy_loopback(struct phy_device *phydev, bool enable)
 {
-	struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
 	int ret = 0;
 
-	if (!phydrv)
-		return -ENODEV;
+	if (!phydev->drv)
+		return -EIO;
 
 	mutex_lock(&phydev->lock);
 
@@ -1839,8 +1838,8 @@ int phy_loopback(struct phy_device *phydev, bool enable)
 		goto out;
 	}
 
-	if (phydrv->set_loopback)
-		ret = phydrv->set_loopback(phydev, enable);
+	if (phydev->drv->set_loopback)
+		ret = phydev->drv->set_loopback(phydev, enable);
 	else
 		ret = genphy_loopback(phydev, enable);
 
Signature · 1 line
2.20.1
maintainer
#2 lore raw

Re: [PATCH net-next 1/2] net: phy: Support set_loopback override

Quoted 14 lines · Gerhard Engleder

phy_read_status and various other PHY functions support PHY specific overriding of driver functions by using a PHY specific pointer to the PHY driver. Add support of PHY specific override to phy_loopback too.

---
 drivers/net/phy/phy_device.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/net/phy/phy_device.c +0 -0
index 107aa6d7bc6b..ba5ad86ec826 100644

drivers/net/phy/phy_device.c EXPORT_SYMBOL(phy_resume); · line 1827

Quoted 10 lines
drivers/net/phy/phy_device.c +2 -3
@@ -1821,11 +1821,10 @@ EXPORT_SYMBOL(phy_resume);
 
 int phy_loopback(struct phy_device *phydev, bool enable)
 {
-	struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
 	int ret = 0;
 
-	if (!phydrv)
-		return -ENODEV;
+	if (!phydev->drv)
+		return -EIO;

Humm, we need to take a closer look at what uses to_phy_driver() and what uses phydev->drv. Do they need to be different? Can we make it uniform?

Andrew

#3 lore raw

Re: [PATCH net-next 1/2] net: phy: Support set_loopback override

Quoted 31 lines · Andrew Lunn
Quoted 14 lines · Gerhard Engleder

phy_read_status and various other PHY functions support PHY specific overriding of driver functions by using a PHY specific pointer to the PHY driver. Add support of PHY specific override to phy_loopback too.

---
 drivers/net/phy/phy_device.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/net/phy/phy_device.c +0 -0
index 107aa6d7bc6b..ba5ad86ec826 100644

drivers/net/phy/phy_device.c EXPORT_SYMBOL(phy_resume); · line 1827

Quoted 10 lines
drivers/net/phy/phy_device.c +2 -3
@@ -1821,11 +1821,10 @@ EXPORT_SYMBOL(phy_resume);
 
 int phy_loopback(struct phy_device *phydev, bool enable)
 {
-     struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
      int ret = 0;
 
-     if (!phydrv)
-             return -ENODEV;
+     if (!phydev->drv)
+             return -EIO;

Humm, we need to take a closer look at what uses to_phy_driver() and what uses phydev->drv. Do they need to be different? Can we make it uniform?

Quoted 1 line

Andrew

I saw only 4 references for to_phy_driver():
- phy_loopback() of course
- phy_probe() which uses it to initialize phydev->drv 3 lines later
- mdio_bus_phy_may_suspend() which checks only for valid suspend function pointer, but later phy_suspend() uses phydev->drv, so this is at
least inconsistent
- phy_bus_match() which casts from struct device_driver to struct phy_driver

phydev->drv is used much more often and seems to be the right way. I suggest to also fix mdio_bus_phy_may_suspend(). phy_probe() and phy_bus_match() are valid uses, because phydev->drv is not available for them.

Do you agree?

Gerhard

maintainer
#4 lore raw

Re: [PATCH net-next 1/2] net: phy: Support set_loopback override

Quoted 3 lines

I saw only 4 references for to_phy_driver():
- phy_loopback() of course
- phy_probe() which uses it to initialize phydev->drv 3 lines later

This is correct. The driver core will set dev.driver to what it thinks is the correct driver to use, before calling probe.

Quoted 3 lines

- mdio_bus_phy_may_suspend() which checks only for valid suspend function pointer, but later phy_suspend() uses phydev->drv, so this is at least inconsistent

I guess the real question here is, can a device be suspended before it is probed? It would seem rather odd. So i expect phydev->drv is safe to use.

Quoted 1 line

- phy_bus_match() which casts from struct device_driver to struct phy_driver

This is used by the driver core when trying to find a matching driver. So it is used before phy_probe(). So this is correct.

Quoted 6 lines

phydev->drv is used much more often and seems to be the right way. I suggest to also fix mdio_bus_phy_may_suspend(). phy_probe() and phy_bus_match() are valid uses, because phydev->drv is not available for them.

Do you agree?

Agreed. Thanks for spending the time to look at this. I was expecting there to be more problems than just loopback.

Andrew

#5 lore raw

PATCH 2/2 net: phy: gmii2rgmii: Support PHY loopback

In reply to Gerhard Engleder

Configure speed if loopback is used. read_status is not called for loopback.

---
 drivers/net/phy/xilinx_gmii2rgmii.c | 46 ++++++++++++++++++++++-------
 1 file changed, 35 insertions(+), 11 deletions(-)
drivers/net/phy/xilinx_gmii2rgmii.c +35 -11
index 151c2a3f0b3a..8dcb49ed1f3d 100644
@@ -27,12 +27,28 @@ struct gmii2rgmii {
 	struct mdio_device *mdio;
 };
 
-static int xgmiitorgmii_read_status(struct phy_device *phydev)
+static void xgmiitorgmii_configure(struct gmii2rgmii *priv, int speed)
 {
-	struct gmii2rgmii *priv = mdiodev_get_drvdata(&phydev->mdio);
 	struct mii_bus *bus = priv->mdio->bus;
 	int addr = priv->mdio->addr;
-	u16 val = 0;
+	u16 val;
+
+	val = mdiobus_read(bus, addr, XILINX_GMII2RGMII_REG);
+	val &= ~XILINX_GMII2RGMII_SPEED_MASK;
+
+	if (speed == SPEED_1000)
+		val |= BMCR_SPEED1000;
+	else if (speed == SPEED_100)
+		val |= BMCR_SPEED100;
+	else
+		val |= BMCR_SPEED10;
+
+	mdiobus_write(bus, addr, XILINX_GMII2RGMII_REG, val);
+}
+
+static int xgmiitorgmii_read_status(struct phy_device *phydev)
+{
+	struct gmii2rgmii *priv = mdiodev_get_drvdata(&phydev->mdio);
 	int err;
 
 	if (priv->phy_drv->read_status)
@@ -42,17 +58,24 @@ static int xgmiitorgmii_read_status(struct phy_device *phydev)
 	if (err < 0)
 		return err;
 
-	val = mdiobus_read(bus, addr, XILINX_GMII2RGMII_REG);
-	val &= ~XILINX_GMII2RGMII_SPEED_MASK;
+	xgmiitorgmii_configure(priv, phydev->speed);
 
-	if (phydev->speed == SPEED_1000)
-		val |= BMCR_SPEED1000;
-	else if (phydev->speed == SPEED_100)
-		val |= BMCR_SPEED100;
+	return 0;
+}
+
+static int xgmiitorgmii_set_loopback(struct phy_device *phydev, bool enable)
+{
+	struct gmii2rgmii *priv = mdiodev_get_drvdata(&phydev->mdio);
+	int err;
+
+	if (priv->phy_drv->set_loopback)
+		err = priv->phy_drv->set_loopback(phydev, enable);
 	else
-		val |= BMCR_SPEED10;
+		err = genphy_loopback(phydev, enable);
+	if (err < 0)
+		return err;
 
-	mdiobus_write(bus, addr, XILINX_GMII2RGMII_REG, val);
+	xgmiitorgmii_configure(priv, phydev->speed);
 
 	return 0;
 }
@@ -90,6 +113,7 @@ static int xgmiitorgmii_probe(struct mdio_device *mdiodev)
 	memcpy(&priv->conv_phy_drv, priv->phy_dev->drv,
 	       sizeof(struct phy_driver));
 	priv->conv_phy_drv.read_status = xgmiitorgmii_read_status;
+	priv->conv_phy_drv.set_loopback = xgmiitorgmii_set_loopback;
 	mdiodev_set_drvdata(&priv->phy_dev->mdio, priv);
 	priv->phy_dev->drv = &priv->conv_phy_drv;
 
Signature · 1 line
2.20.1