🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
2 messages
2 total messages Started by javen Wed, 25 Nov 2026 09:02
[PATCH net-next v4 2/3] net: phy: realtek: add PTP clock support for RTL8127a
#22404
Author: javen
Date: Wed, 25 Nov 2026 09:02
587 lines
25485 bytes
From: Javen Xu <javen_xu@realsil.com.cn>

The internal PHY of RTL8127A (RTL8261C, PHY ID 0x001cc890) contains a
PTP block with a free running clock that can be read, set, stepped and
frequency adjusted through vendor MMD registers.

Register a PTP hardware clock for it. All register accesses go through
the normal blocking MDIO path, multi register sequences are serialized
by a driver mutex. The frequency adjustment is cleared at probe. The
clock is set from the system time when the link comes up for the first
time after a PHY init or link loss.

Signed-off-by: Javen Xu <javen_xu@realsil.com.cn>
---
Changes in v3:
 - Keep the clock running across PHY re-initializations, only start it
   from the system time if the counter has been reset
 - Only bracket the latching EXEC write with the system timestamps in
   gettimex64()
 - Clear phydev->priv in remove()
 - Add defines for the PHY reset register
 - Depend on NETWORK_PHY_TIMESTAMPING from this patch on

Changes in v4:
 - Set the clock from the system time on link up, after a PHY init or
   link loss
 - Don't fail the PHY probe if PTP setup fails
---
 drivers/net/phy/realtek/Kconfig        |  11 +
 drivers/net/phy/realtek/Makefile       |   1 +
 drivers/net/phy/realtek/realtek.h      |  25 ++
 drivers/net/phy/realtek/realtek_main.c |  25 +-
 drivers/net/phy/realtek/realtek_ptp.c  | 428 +++++++++++++++++++++++++
 5 files changed, 489 insertions(+), 1 deletion(-)
 create mode 100644 drivers/net/phy/realtek/realtek_ptp.c

diff --git a/drivers/net/phy/realtek/Kconfig b/drivers/net/phy/realtek/Kconfig
index a741b34d193e..feb9a7fd1582 100644
--- a/drivers/net/phy/realtek/Kconfig
+++ b/drivers/net/phy/realtek/Kconfig
@@ -13,4 +13,15 @@ config REALTEK_PHY_HWMON
 	help
 	  Optional hwmon support for the temperature sensor
 
+config REALTEK_PHY_PTP
+	bool "PTP support for Realtek PHYs"
+	default y
+	depends on PTP_1588_CLOCK && NETWORK_PHY_TIMESTAMPING
+	depends on !(REALTEK_PHY=y && PTP_1588_CLOCK=m)
+	help
+	  Optional PTP hardware clock support for the internal PHY of
+	  RTL8127A. The clock is provided by the PHY itself and can be
+	  read and adjusted through the PTP clock device, no support from
+	  the MAC driver is needed.
+
 endif # REALTEK_PHY
diff --git a/drivers/net/phy/realtek/Makefile b/drivers/net/phy/realtek/Makefile
index dd21cf87f2f1..450bd05b7ef9 100644
--- a/drivers/net/phy/realtek/Makefile
+++ b/drivers/net/phy/realtek/Makefile
@@ -1,4 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
 realtek-y			+= realtek_main.o
 realtek-$(CONFIG_REALTEK_PHY_HWMON) += realtek_hwmon.o
+realtek-$(CONFIG_REALTEK_PHY_PTP) += realtek_ptp.o
 obj-$(CONFIG_REALTEK_PHY)	+= realtek.o
diff --git a/drivers/net/phy/realtek/realtek.h b/drivers/net/phy/realtek/realtek.h
index a39b44fa18a0..1841c87e23fc 100644
--- a/drivers/net/phy/realtek/realtek.h
+++ b/drivers/net/phy/realtek/realtek.h
@@ -7,4 +7,29 @@
 
 int rtl822x_hwmon_init(struct phy_device *phydev);
 
+#if IS_ENABLED(CONFIG_REALTEK_PHY_PTP)
+int rtl8127a_ptp_probe(struct phy_device *phydev);
+void rtl8127a_ptp_remove(struct phy_device *phydev);
+int rtl8127a_ptp_config_init(struct phy_device *phydev);
+void rtl8127a_ptp_link_change_notify(struct phy_device *phydev);
+#else
+static inline int rtl8127a_ptp_probe(struct phy_device *phydev)
+{
+	return 0;
+}
+
+static inline void rtl8127a_ptp_remove(struct phy_device *phydev)
+{
+}
+
+static inline int rtl8127a_ptp_config_init(struct phy_device *phydev)
+{
+	return 0;
+}
+
+static inline void rtl8127a_ptp_link_change_notify(struct phy_device *phydev)
+{
+}
+#endif
+
 #endif /* REALTEK_H */
diff --git a/drivers/net/phy/realtek/realtek_main.c b/drivers/net/phy/realtek/realtek_main.c
index ca2d515b77b3..395222f97e0d 100644
--- a/drivers/net/phy/realtek/realtek_main.c
+++ b/drivers/net/phy/realtek/realtek_main.c
@@ -1839,6 +1839,26 @@ static int rtl822x_probe(struct phy_device *phydev)
 	return 0;
 }
 
+static int rtl_internal_nbaset_probe(struct phy_device *phydev)
+{
+	int ret;
+
+	ret = rtl822x_probe(phydev);
+	if (ret)
+		return ret;
+
+	/* Only the internal PHY of RTL8127A has a PTP block. PTP is
+	 * optional, keep the PHY usable without it.
+	 */
+	if (phydev->phy_id == RTL_8261C) {
+		ret = rtl8127a_ptp_probe(phydev);
+		if (ret)
+			phydev_warn(phydev, "PTP support disabled: %d\n", ret);
+	}
+
+	return 0;
+}
+
 /* RTL822x cannot access MDIO_MMD_VEND2 via MII_MMD_CTRL/MII_MMD_DATA.
  * A mapping to use paged access needs to be used instead.
  * All other MMD devices can be accessed as usual.
@@ -3413,7 +3433,9 @@ static struct phy_driver realtek_drvs[] = {
 		.match_phy_device = rtl_internal_nbaset_match_phy_device,
 		.name		= "Realtek Internal NBASE-T PHY",
 		.flags		= PHY_IS_INTERNAL,
-		.probe		= rtl822x_probe,
+		.probe		= rtl_internal_nbaset_probe,
+		.remove		= rtl8127a_ptp_remove,
+		.config_init	= rtl8127a_ptp_config_init,
 		.get_features	= rtl822x_get_features,
 		.config_aneg	= rtl822x_config_aneg,
 		.read_status	= rtl822x_read_status,
@@ -3423,6 +3445,7 @@ static struct phy_driver realtek_drvs[] = {
 		.write_page	= rtl821x_write_page,
 		.read_mmd	= rtl822x_read_mmd,
 		.write_mmd	= rtl822x_write_mmd,
+		.link_change_notify = rtl8127a_ptp_link_change_notify,
 	}, {
 		PHY_ID_MATCH_EXACT(0x001ccad0),
 		.name		= "RTL8224 2.5Gbps PHY",
diff --git a/drivers/net/phy/realtek/realtek_ptp.c b/drivers/net/phy/realtek/realtek_ptp.c
new file mode 100644
index 000000000000..83ee125517b0
--- /dev/null
+++ b/drivers/net/phy/realtek/realtek_ptp.c
@@ -0,0 +1,428 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * PTP hardware clock support for the internal PHY of RTL8127A
+ */
+
+#include <linux/bitfield.h>
+#include <linux/bits.h>
+#include <linux/iopoll.h>
+#include <linux/math64.h>
+#include <linux/netdevice.h>
+#include <linux/phy.h>
+#include <linux/ptp_clock_kernel.h>
+#include <linux/timekeeping.h>
+
+#include "realtek.h"
+
+#define RTL8127A_PTP_CTL		0xe400
+#define	RTL8127A_PTP_CTL_EN		BIT(0)
+#define	RTL8127A_PTP_CTL_TYPE_3		0x003f
+#define RTL8127A_PTP_INER		0xe402
+#define RTL8127A_PTP_INSR		0xe404
+#define	RTL8127A_PTP_INT_RX_TS		BIT(2)
+#define	RTL8127A_PTP_INT_TX_TS		BIT(3)
+#define RTL8127A_PTP_SYNCE_CTL		0xe406
+#define	RTL8127A_PTP_SYNCE_EN		BIT(0)
+#define RTL8127A_PTP_GEN_CFG		0xe408
+#define	RTL8127A_PTP_INT_CLR_MODE	BIT(0)
+#define RTL8127A_PTP_CLK_CFG		0xe410
+#define	RTL8127A_PTP_CLK_CFG_EXEC	BIT(0)
+#define	RTL8127A_PTP_CLK_CFG_MODE	GENMASK(3, 1)
+#define RTL8127A_PHY_RST_CTL		0xa640
+#define RTL8127A_PHY_RST		BIT(15)
+/* Time registers */
+#define RTL8127A_PTP_CFG_TIME		0xe412
+#define	RTL8127A_PTP_NS_LO		0x0
+#define	RTL8127A_PTP_NS_HI		0x2
+#define	RTL8127A_PTP_S_LO		0x4
+#define	RTL8127A_PTP_S_MI		0x6
+#define	RTL8127A_PTP_S_HI		0x8
+#define	RTL8127A_PTP_NS_HI_MASK		GENMASK(13, 0)
+#define	RTL8127A_PTP_NSEC_MASK		GENMASK(29, 0)
+#define	RTL8127A_PTP_SEC_MASK		GENMASK_ULL(47, 0)
+#define	RTL8127A_PTP_RATE_HI_MASK	GENMASK(21, 16)
+
+#define RTL8127A_PTP_MAX_ADJ		488281
+
+/* RTL8127A_PTP_CLK_CFG clock adjust modes */
+enum rtl8127a_ptp_clkadj_mode {
+	RTL8127A_PTP_DIRECT_READ	= 4,
+	RTL8127A_PTP_DIRECT_WRITE	= 6,
+	RTL8127A_PTP_INC_STEP		= 8,
+	RTL8127A_PTP_DEC_STEP		= 10,
+	RTL8127A_PTP_RATE_WRITE		= 14,
+};
+
+struct rtl8127a_ptp {
+	struct phy_device *phydev;
+	struct ptp_clock_info info;
+	struct ptp_clock *clock;
+	struct mutex lock;	/* serializes PTP register access sequences */
+	bool time_set;
+};
+
+static int rtl8127a_ptp_read(struct rtl8127a_ptp *ptp, u16 reg)
+{
+	return phy_read_mmd(ptp->phydev, MDIO_MMD_VEND2, reg);
+}
+
+static int rtl8127a_ptp_write(struct rtl8127a_ptp *ptp, u16 reg, u16 val)
+{
+	return phy_write_mmd(ptp->phydev, MDIO_MMD_VEND2, reg, val);
+}
+
+/* Unlike phy_modify_mmd() always write the register back, some of the
+ * bits are commands to the PTP block.
+ */
+static int rtl8127a_ptp_modify(struct rtl8127a_ptp *ptp, u16 reg, u16 mask,
+			       u16 set)
+{
+	int val = rtl8127a_ptp_read(ptp, reg);
+
+	if (val < 0)
+		return val;
+
+	return rtl8127a_ptp_write(ptp, reg, (val & ~mask) | set);
+}
+
+/* Poll until (reg & mask) is non-zero (set == true) or zero (set == false).
+ * Returns the last register value read, or a negative error code.
+ */
+static int rtl8127a_ptp_poll(struct rtl8127a_ptp *ptp, u16 reg, u16 mask,
+			     bool set)
+{
+	int ret, val;
+
+	ret = read_poll_timeout(rtl8127a_ptp_read, val,
+				val < 0 || !!(val & mask) == set, 1, 1000,
+				false, ptp, reg);
+	if (val < 0)
+		ret = val;
+	if (ret) {
+		if (net_ratelimit())
+			phydev_err(ptp->phydev,
+				   "PTP reg 0x%04x poll failed: %d\n", reg, ret);
+		return ret;
+	}
+
+	return val;
+}
+
+static int rtl8127a_ptp_clkadj_sts(struct rtl8127a_ptp *ptp,
+				   enum rtl8127a_ptp_clkadj_mode mode,
+				   struct ptp_system_timestamp *sts)
+{
+	int val, ret;
+
+	val = rtl8127a_ptp_read(ptp, RTL8127A_PTP_CLK_CFG);
+	if (val < 0)
+		return val;
+
+	val &= ~RTL8127A_PTP_CLK_CFG_MODE;
+	val |= RTL8127A_PTP_CLK_CFG_EXEC | mode;
+
+	/* Only the exec write latches the counter. */
+	ptp_read_system_prets(sts);
+	ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_CLK_CFG, val);
+	ptp_read_system_postts(sts);
+	if (ret)
+		return ret;
+
+	ret = rtl8127a_ptp_poll(ptp, RTL8127A_PTP_CLK_CFG,
+				RTL8127A_PTP_CLK_CFG_EXEC, false);
+
+	return ret < 0 ? ret : 0;
+}
+
+static int rtl8127a_ptp_clkadj(struct rtl8127a_ptp *ptp,
+			       enum rtl8127a_ptp_clkadj_mode mode)
+{
+	return rtl8127a_ptp_clkadj_sts(ptp, mode, NULL);
+}
+
+static int rtl8127a_ptp_read_time(struct rtl8127a_ptp *ptp, u16 base,
+				  struct timespec64 *ts)
+{
+	static const u16 regs[] = {
+		RTL8127A_PTP_NS_HI, RTL8127A_PTP_NS_LO,
+		RTL8127A_PTP_S_HI, RTL8127A_PTP_S_MI, RTL8127A_PTP_S_LO,
+	};
+	int val[ARRAY_SIZE(regs)];
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(regs); i++) {
+		val[i] = rtl8127a_ptp_read(ptp, base + regs[i]);
+		if (val[i] < 0)
+			return val[i];
+	}
+
+	ts->tv_nsec = (val[0] & RTL8127A_PTP_NS_HI_MASK) << 16 | val[1];
+	ts->tv_sec = (u64)val[2] << 32 | (u64)val[3] << 16 | val[4];
+
+	return 0;
+}
+
+static int rtl8127a_ptp_write_time(struct rtl8127a_ptp *ptp,
+				   const struct timespec64 *ts)
+{
+	u64 sec = ts->tv_sec & RTL8127A_PTP_SEC_MASK;
+	u32 nsec = ts->tv_nsec & RTL8127A_PTP_NSEC_MASK;
+	const struct {
+		u16 reg;
+		u16 val;
+	} regs[] = {
+		{ RTL8127A_PTP_NS_LO, nsec },
+		{ RTL8127A_PTP_NS_HI, nsec >> 16 },
+		{ RTL8127A_PTP_S_LO, sec },
+		{ RTL8127A_PTP_S_MI, sec >> 16 },
+		{ RTL8127A_PTP_S_HI, sec >> 32 },
+	};
+	int i, ret;
+
+	for (i = 0; i < ARRAY_SIZE(regs); i++) {
+		ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_CFG_TIME + regs[i].reg,
+					 regs[i].val);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
+static int rtl8127a_ptp_hwtstamp_enable(struct rtl8127a_ptp *ptp, bool enable)
+{
+	int ret;
+
+	mutex_lock(&ptp->lock);
+
+	ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_SYNCE_CTL,
+				  RTL8127A_PTP_SYNCE_EN, 0);
+	if (ret)
+		goto out;
+
+	if (enable) {
+		ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_INER, 0,
+					  RTL8127A_PTP_INT_RX_TS |
+					  RTL8127A_PTP_INT_TX_TS);
+		if (ret)
+			goto out;
+
+		ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_GEN_CFG, 0,
+					  RTL8127A_PTP_INT_CLR_MODE);
+		if (ret)
+			goto out;
+
+		ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_INSR, 0xffff);
+		if (ret)
+			goto out;
+
+		ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_CTL,
+					 RTL8127A_PTP_CTL_TYPE_3 | BIT(12));
+	} else {
+		ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_INER,
+					  RTL8127A_PTP_INT_RX_TS |
+					  RTL8127A_PTP_INT_TX_TS, 0);
+		if (ret)
+			goto out;
+
+		ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_CTL,
+					  RTL8127A_PTP_CTL_EN, 0);
+	}
+	if (ret)
+		goto out;
+
+	ret = rtl8127a_ptp_modify(ptp, RTL8127A_PHY_RST_CTL, 0,
+				  RTL8127A_PHY_RST);
+out:
+	mutex_unlock(&ptp->lock);
+
+	return ret;
+}
+
+static int rtl8127a_ptp_settime(struct rtl8127a_ptp *ptp,
+				const struct timespec64 *ts)
+{
+	int ret;
+
+	mutex_lock(&ptp->lock);
+	ret = rtl8127a_ptp_write_time(ptp, ts);
+	if (!ret)
+		ret = rtl8127a_ptp_clkadj(ptp, RTL8127A_PTP_DIRECT_WRITE);
+	mutex_unlock(&ptp->lock);
+
+	return ret;
+}
+
+static int rtl8127a_ptp_adjfine(struct ptp_clock_info *info, long scaled_ppm)
+{
+	struct rtl8127a_ptp *ptp = container_of(info, struct rtl8127a_ptp, info);
+	s32 ppb = scaled_ppm_to_ppb(scaled_ppm);
+	u32 rate;
+	int ret;
+
+	rate = div_u64((u64)abs(ppb) << 32, NSEC_PER_SEC);
+	if (ppb < 0)
+		rate = -rate;
+
+	mutex_lock(&ptp->lock);
+	ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_CFG_TIME + RTL8127A_PTP_NS_LO,
+				 rate);
+	if (ret)
+		goto out;
+
+	ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_CFG_TIME + RTL8127A_PTP_NS_HI,
+				 FIELD_GET(RTL8127A_PTP_RATE_HI_MASK, rate));
+	if (ret)
+		goto out;
+
+	ret = rtl8127a_ptp_clkadj(ptp, RTL8127A_PTP_RATE_WRITE);
+out:
+	mutex_unlock(&ptp->lock);
+
+	return ret;
+}
+
+static int rtl8127a_ptp_adjtime(struct ptp_clock_info *info, s64 delta)
+{
+	struct rtl8127a_ptp *ptp = container_of(info, struct rtl8127a_ptp, info);
+	struct timespec64 d = ns_to_timespec64(abs(delta));
+	int ret;
+
+	mutex_lock(&ptp->lock);
+	ret = rtl8127a_ptp_write_time(ptp, &d);
+	if (!ret)
+		ret = rtl8127a_ptp_clkadj(ptp, delta < 0 ?
+						RTL8127A_PTP_DEC_STEP :
+						RTL8127A_PTP_INC_STEP);
+	mutex_unlock(&ptp->lock);
+
+	return ret;
+}
+
+static int rtl8127a_ptp_gettimex64(struct ptp_clock_info *info,
+				   struct timespec64 *ts,
+				   struct ptp_system_timestamp *sts)
+{
+	struct rtl8127a_ptp *ptp = container_of(info, struct rtl8127a_ptp, info);
+	int ret;
+
+	mutex_lock(&ptp->lock);
+	ret = rtl8127a_ptp_clkadj_sts(ptp, RTL8127A_PTP_DIRECT_READ, sts);
+	if (!ret)
+		ret = rtl8127a_ptp_read_time(ptp, RTL8127A_PTP_CFG_TIME, ts);
+	mutex_unlock(&ptp->lock);
+
+	return ret;
+}
+
+static int rtl8127a_ptp_settime64(struct ptp_clock_info *info,
+				  const struct timespec64 *ts)
+{
+	struct rtl8127a_ptp *ptp = container_of(info, struct rtl8127a_ptp, info);
+
+	return rtl8127a_ptp_settime(ptp, ts);
+}
+
+static const struct ptp_clock_info rtl8127a_ptp_info = {
+	.owner		= THIS_MODULE,
+	.name		= "RTL8127A PHC",
+	.max_adj	= RTL8127A_PTP_MAX_ADJ,
+	.adjfine	= rtl8127a_ptp_adjfine,
+	.adjtime	= rtl8127a_ptp_adjtime,
+	.gettimex64	= rtl8127a_ptp_gettimex64,
+	.settime64	= rtl8127a_ptp_settime64,
+};
+
+int rtl8127a_ptp_config_init(struct phy_device *phydev)
+{
+	struct rtl8127a_ptp *ptp = phydev->priv;
+
+	if (!ptp)
+		return 0;
+
+	mutex_lock(&ptp->lock);
+	ptp->time_set = false;
+	mutex_unlock(&ptp->lock);
+
+	return 0;
+}
+
+void rtl8127a_ptp_link_change_notify(struct phy_device *phydev)
+{
+	struct rtl8127a_ptp *ptp = phydev->priv;
+	struct timespec64 ts;
+	int ret = 0;
+
+	if (!ptp)
+		return;
+
+	if (!phydev->link) {
+		mutex_lock(&ptp->lock);
+		ptp->time_set = false;
+		mutex_unlock(&ptp->lock);
+		return;
+	}
+
+	mutex_lock(&ptp->lock);
+
+	if (!ptp->time_set) {
+		ktime_get_real_ts64(&ts);
+		ret = rtl8127a_ptp_write_time(ptp, &ts);
+		if (!ret)
+			ret = rtl8127a_ptp_clkadj(ptp, RTL8127A_PTP_DIRECT_WRITE);
+		if (ret)
+			goto out;
+		ptp->time_set = true;
+	}
+out:
+	mutex_unlock(&ptp->lock);
+
+	if (ret < 0)
+		phydev_warn(phydev, "failed to set PTP time: %d\n", ret);
+}
+
+int rtl8127a_ptp_probe(struct phy_device *phydev)
+{
+	struct device *dev = &phydev->mdio.dev;
+	struct rtl8127a_ptp *ptp;
+	int ret;
+
+	ptp = devm_kzalloc(dev, sizeof(*ptp), GFP_KERNEL);
+	if (!ptp)
+		return -ENOMEM;
+
+	ret = devm_mutex_init(dev, &ptp->lock);
+	if (ret)
+		return ret;
+
+	ptp->phydev = phydev;
+	ptp->info = rtl8127a_ptp_info;
+
+	ret = rtl8127a_ptp_hwtstamp_enable(ptp, false);
+	if (ret)
+		return ret;
+
+	ret = rtl8127a_ptp_adjfine(&ptp->info, 0);
+	if (ret)
+		return ret;
+
+	ptp->clock = ptp_clock_register(&ptp->info, dev);
+	if (IS_ERR(ptp->clock))
+		return PTR_ERR(ptp->clock);
+
+	phydev->priv = ptp;
+
+	return 0;
+}
+
+void rtl8127a_ptp_remove(struct phy_device *phydev)
+{
+	struct rtl8127a_ptp *ptp = phydev->priv;
+
+	if (!ptp)
+		return;
+
+	rtl8127a_ptp_hwtstamp_enable(ptp, false);
+	ptp_clock_unregister(ptp->clock);
+	phydev->priv = NULL;
+}
-- 
2.43.0
Re: [PATCH net-next v4 2/3] net: phy: realtek: add PTP clock support for RTL8127a
#24110
Author: netdev-bot+sashiko
Date: Sun, 29 Nov 2026 09:50
166 lines
13267 bytes
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 2 · Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] The driver re-seeds the PHC from CLOCK_REALTIME on the next
  link-up whenever `ptp->time_set` is false.
- [Medium] `rtl8127a_ptp_hwtstamp_enable(ptp, false)` ends by setting
  `RTL8127A_PHY_RST` (BIT(15) of PHY OCP 0xa640) at…

--

net: phy: realtek: add PTP clock support for RTL8127a

This adds a PTP hardware clock for the internal PHY of RTL8127A
(RTL8261C). The clock can be read, set, stepped and frequency adjusted
through vendor MMD registers. The frequency adjustment is cleared at
probe, and the clock is seeded from the system time on the first link up
after a PHY init or link loss.

> diff --git a/drivers/net/phy/realtek/realtek_ptp.c b/drivers/net/phy/realtek/realtek_ptp.c
> new file mode 100644
> index 0000000000000..83ee125517b00
> --- /dev/null
> +++ b/drivers/net/phy/realtek/realtek_ptp.c

[ ... ]

> +int rtl8127a_ptp_config_init(struct phy_device *phydev)
> +{
> +	struct rtl8127a_ptp *ptp = phydev->priv;
> +
> +	if (!ptp)
> +		return 0;
> +
> +	mutex_lock(&ptp->lock);
> +	ptp->time_set = false;
> +	mutex_unlock(&ptp->lock);
> +
> +	return 0;
> +}
> +
> +void rtl8127a_ptp_link_change_notify(struct phy_device *phydev)
> +{
> +	struct rtl8127a_ptp *ptp = phydev->priv;
> +	struct timespec64 ts;
> +	int ret = 0;
> +
> +	if (!ptp)
> +		return;
> +
> +	if (!phydev->link) {
> +		mutex_lock(&ptp->lock);
> +		ptp->time_set = false;
> +		mutex_unlock(&ptp->lock);
> +		return;
> +	}
> +
> +	mutex_lock(&ptp->lock);
> +
> +	if (!ptp->time_set) {
> +		ktime_get_real_ts64(&ts);
> +		ret = rtl8127a_ptp_write_time(ptp, &ts);
> +		if (!ret)
> +			ret = rtl8127a_ptp_clkadj(ptp, RTL8127A_PTP_DIRECT_WRITE);

[Severity: Medium]
Won't this overwrite a PHC time that userspace has set or disciplined?

time_set gets cleared, or is never set, in these cases:

- rtl8127a_ptp_config_init() clears it on every phy_init_hw(). In r8169,
  rtl8169_init_phy() calls phy_init_hw() from rtl8169_up(), and
  rtl8169_up() runs from both rtl_open() and rtl8169_runtime_resume().

- rtl8127a_ptp_link_change_notify() clears it on any state change with
  phydev->link == 0. That covers cable flaps and phy_stop() on ifdown.

- rtl8127a_ptp_settime64()->rtl8127a_ptp_settime() and
  rtl8127a_ptp_adjtime() never set time_set to true.

So on the first link up after any of these, the PHC is stepped to
CLOCK_REALTIME, even if the counter was never reset. For example, a
TAI based PHC maintained by ptp4l or phc2sys would be stepped back to
UTC, roughly 37 seconds off.

The v3 changelog says the clock is kept running across PHY
re-initializations, but v4 now re-seeds it after every phy_init_hw().
Is that intended?

If the counter really is reset by these events, wouldn't the rate
programmed with RTL8127A_PTP_RATE_WRITE be lost too? It is never
re-applied here.

The Kconfig help text says the clock "can be read and adjusted through
the PTP clock device". Here those adjustments are replaced without any
notice.

The same logic is still present at the end of the series. The later
commit "net: phy: realtek: add hardware timestamping for RTL8127a" only
adds re-enabling of timestamping in link_change_notify. It leaves the
time_set handling unchanged.

> +		if (ret)
> +			goto out;
> +		ptp->time_set = true;
> +	}

[ ... ]

> +	ptp->phydev = phydev;
> +	ptp->info = rtl8127a_ptp_info;
> +
> +	ret = rtl8127a_ptp_hwtstamp_enable(ptp, false);
> +	if (ret)
> +		return ret;
> +
> +	ret = rtl8127a_ptp_adjfine(&ptp->info, 0);

[Severity: Medium]
Can the PHY still be in reset when adjfine runs here?

rtl8127a_ptp_hwtstamp_enable(ptp, false) ends by setting the bit that
this patch defines as the PHY reset:

	ret = rtl8127a_ptp_modify(ptp, RTL8127A_PHY_RST_CTL, 0,
				  RTL8127A_PHY_RST);

Nothing waits for the reset to finish or for the bit to self-clear.
Right after that, rtl8127a_ptp_adjfine() writes the CFG_TIME rate
staging registers and issues a RATE_WRITE EXEC on CLK_CFG. It then
polls EXEC with only a 1 ms timeout.

If the reset is still in progress, could the staging writes be lost, or
could EXEC fail to clear? In that case probe would drop PTP with "PTP
support disabled".

rtl8127a_ptp_remove() issues the same reset while the PHC is still
registered:

	rtl8127a_ptp_hwtstamp_enable(ptp, false);
	ptp_clock_unregister(ptp->clock);

Could a concurrent clock_gettime() or clock_adjtime() then program
CLK_CFG and CFG_TIME on a PHY that is in the middle of a reset?

Nothing else in drivers/net/ touches 0xa640, so I couldn't confirm
whether this reset affects the PTP block, or how long it takes. For
comparison, genphy_soft_reset() polls BMCR_RESET before it continues.

This sequence is also unchanged at the end of the series.

> +	if (ret)
> +		return ret;

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261125090217.1198-1-javen_xu%40realsil.com.cn
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads