mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Oleksij Rempel <o.rempel@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Oleksij Rempel <o.rempel@pengutronix.de>
Subject: [PATCH v2] net: phy: Add support for PHY-specific link polling interval and randomization
Date: Wed, 12 Jun 2024 18:28:30 +0200	[thread overview]
Message-ID: <20240612162830.1023574-1-o.rempel@pengutronix.de> (raw)

Introduce the ability to use PHY-specific link polling intervals. Some
PHYs, such as the TI DP83TG720, require different polling intervals for
link status checks. This is particularly necessary for PHYs that do not
support autonegotiation and need frequent assistance to establish a
link.

For instance, the TI DP83TG720 PHY may need to be reset if a link is not
established within 100 milliseconds. If two identical PHYs are connected
to each other and require the same software assistance, they might reset
at the same frequency and fail to establish a link. To mitigate this, a
randomization mechanism is added to the polling interval.

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 drivers/net/phy/dp83tg720.c | 17 +++++++++++++++++
 drivers/net/phy/phy.c       |  2 ++
 include/linux/phy.h         |  3 +++
 net/eth.c                   |  2 +-
 4 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/drivers/net/phy/dp83tg720.c b/drivers/net/phy/dp83tg720.c
index d57985539d..7e807314ce 100644
--- a/drivers/net/phy/dp83tg720.c
+++ b/drivers/net/phy/dp83tg720.c
@@ -5,6 +5,7 @@
 #include <common.h>
 #include <linux/mdio.h>
 #include <linux/phy.h>
+#include <stdlib.h>
 
 #define DP83TG720S_PHY_ID			0x2000a284
 
@@ -26,6 +27,8 @@
 /* Power Mode 0 is Normal mode */
 #define DP83TG720S_LPS_CFG3_PWR_MODE_0		BIT(0)
 
+#define DP83TG720S_POLL_TIMEOUT_MS		100
+
 static int dp83tg720_config_rgmii_delay(struct phy_device *phydev)
 {
 	u16 rgmii_delay_mask;
@@ -92,6 +95,12 @@ static int dp83tg720_read_status(struct phy_device *phydev)
 {
 	u16 phy_sts;
 
+	/* Randomize the polling interval to avoid reset synchronization with
+	 * the link partner.  The polling interval is set to 150ms +/- 50ms.
+	 */
+	phydev->polling_interval = (DP83TG720S_POLL_TIMEOUT_MS +
+				    (rand() % 10) * 10) * MSECOND;
+
 	phy_sts = phy_read(phydev, DP83TG720S_MII_REG_10);
 	phydev->link = !!(phy_sts & DP83TG720S_LINK_STATUS);
 	if (!phydev->link) {
@@ -112,12 +121,20 @@ static int dp83tg720_read_status(struct phy_device *phydev)
 	return 0;
 }
 
+static int dp83tg720_probe(struct phy_device *phydev)
+{
+	phydev->polling_interval = DP83TG720S_POLL_TIMEOUT_MS * MSECOND;
+
+	return 0;
+}
+
 static struct phy_driver dp83tg720_driver[] = {
 	{
 		PHY_ID_MATCH_MODEL(DP83TG720S_PHY_ID),
 		.drv.name	= "TI DP83TG720S",
 		.read_status	= dp83tg720_read_status,
 		.config_init	= dp83tg720_phy_init,
+		.probe		= dp83tg720_probe,
 	}
 };
 device_phy_drivers(dp83tg720_driver);
diff --git a/drivers/net/phy/phy.c b/drivers/net/phy/phy.c
index c4d9844af5..2795d09918 100644
--- a/drivers/net/phy/phy.c
+++ b/drivers/net/phy/phy.c
@@ -174,6 +174,8 @@ struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id)
 	phydev->pause = phydev->asym_pause = 0;
 	phydev->autoneg = AUTONEG_ENABLE;
 
+	phydev->polling_interval = PHY_POLL_INTERVAL;
+
 	phydev->addr = addr;
 	phydev->phy_id = phy_id;
 
diff --git a/include/linux/phy.h b/include/linux/phy.h
index ef25dec033..d8e53d446e 100644
--- a/include/linux/phy.h
+++ b/include/linux/phy.h
@@ -118,6 +118,7 @@ typedef enum {
 #define PHY_INIT_TIMEOUT	100000
 #define PHY_FORCE_TIMEOUT	10
 #define PHY_AN_TIMEOUT		10
+#define PHY_POLL_INTERVAL	(5 * SECOND)
 
 #define PHY_MAX_ADDR	32
 
@@ -247,6 +248,8 @@ struct phy_device {
 	void (*adjust_link)(struct eth_device *dev);
 
 	struct cdev cdev;
+
+	uint64_t polling_interval;
 };
 #define to_phy_device(d) container_of(d, struct phy_device, dev)
 
diff --git a/net/eth.c b/net/eth.c
index 98567d8d3f..fbe8fc95f8 100644
--- a/net/eth.c
+++ b/net/eth.c
@@ -204,7 +204,7 @@ static int eth_carrier_check(struct eth_device *edev, bool may_wait)
 		return 0;
 
 	if (!edev->last_link_check ||
-	    is_timeout(edev->last_link_check, 5 * SECOND))
+	    is_timeout(edev->last_link_check, edev->phydev->polling_interval))
 		eth_carrier_poll_once(edev);
 
 	if (may_wait && !edev->phydev->link) {
-- 
2.39.2




             reply	other threads:[~2024-06-12 16:29 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-12 16:28 Oleksij Rempel [this message]
2024-06-13  6:54 ` Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240612162830.1023574-1-o.rempel@pengutronix.de \
    --to=o.rempel@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox