* [PATCH net-next v5 0/4] net: phy: realtek: add PTP support for RTL8127a
@ 2026-12-01 9:44 javen
2026-12-01 9:44 ` [PATCH net-next v5 1/4] net: ethtool: tsconfig: allow selecting a provider javen
` (3 more replies)
0 siblings, 4 replies; 8+ messages in thread
From: javen @ 2026-12-01 9:44 UTC (permalink / raw)
To: andrew, hkallweit1, linux, davem, edumazet, kuba, pabeni, nb,
maxime.chevallier
Cc: netdev, linux-kernel, daniel, vladimir.oltean, Javen
From: Javen <javen_xu@realsil.com.cn>
The PTP block of the RTL8127A resides in its internal PHY (RTL8261C).
Therefore, this series adds PTP support to the Realtek PHY driver
instead of the r8169 MAC driver.
Javen (1):
net: ethtool: tsconfig: allow selecting a provider
Javen Xu (3):
r8169: hold MDIO bus lock while loading firmware
net: phy: realtek: add PTP clock support for RTL8127a
net: phy: realtek: add hardware timestamping for RTL8127a
drivers/net/ethernet/realtek/r8169_main.c | 5 +
drivers/net/phy/realtek/Kconfig | 13 +
drivers/net/phy/realtek/Makefile | 1 +
drivers/net/phy/realtek/realtek.h | 19 +
drivers/net/phy/realtek/realtek_main.c | 24 +-
drivers/net/phy/realtek/realtek_ptp.c | 807 ++++++++++++++++++++++
net/ethtool/tsconfig.c | 17 +-
7 files changed, 879 insertions(+), 7 deletions(-)
create mode 100644 drivers/net/phy/realtek/realtek_ptp.c
--
2.43.0
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH net-next v5 1/4] net: ethtool: tsconfig: allow selecting a provider
2026-12-01 9:44 [PATCH net-next v5 0/4] net: phy: realtek: add PTP support for RTL8127a javen
@ 2026-12-01 9:44 ` javen
2026-12-01 10:06 ` Nicolai Buchwitz
2026-12-01 9:44 ` [PATCH net-next v5 2/4] r8169: hold MDIO bus lock while loading firmware javen
` (2 subsequent siblings)
3 siblings, 1 reply; 8+ messages in thread
From: javen @ 2026-12-01 9:44 UTC (permalink / raw)
To: andrew, hkallweit1, linux, davem, edumazet, kuba, pabeni, nb,
maxime.chevallier
Cc: netdev, linux-kernel, daniel, vladimir.oltean, Javen
From: Javen <javen_xu@realsil.com.cn>
A hwtstamp provider can obly ne selected with ETHTOOL_MSG_TSCONFIG_SET
when the device already has an active time stamping source: a MAC
implementing ndo_hwtstamp_set(), a PHY flagged as default_timestamp, or
a previously selected dev->hwprov.
This patch makes it possible to select a PHY time stamper on a MAC
without time stamping support. New phy drivers are expected to reply on
dev->hwprov rather than default_timestamp.
Signed-off-by: Javen <javen_xu@realsil.com.cn>
---
Changes in v5:
- new file.
---
net/ethtool/tsconfig.c | 17 +++++++++++------
1 file changed, 11 insertions(+), 6 deletions(-)
diff --git a/net/ethtool/tsconfig.c b/net/ethtool/tsconfig.c
index 2db0e7ba8b9f..9c983666a1b0 100644
--- a/net/ethtool/tsconfig.c
+++ b/net/ethtool/tsconfig.c
@@ -300,14 +300,16 @@ static int ethnl_set_tsconfig(struct ethnl_req_info *req_base,
struct hwtstamp_provider *hwprov = NULL;
struct net_device *dev = req_base->dev;
struct nlattr **tb = info->attrs;
+ bool has_hwts;
int ret;
if (!netif_device_present(dev))
return -ENODEV;
- if (!dev->netdev_ops->ndo_hwtstamp_set &&
- !phy_is_default_hwtstamp(dev->phydev) &&
- !netdev_ops_lock_dereference(dev->hwprov, dev))
+ has_hwts = dev->netdev_ops->ndo_hwtstamp_set ||
+ phy_is_default_hwtstamp(dev->phydev) ||
+ netdev_ops_lock_dereference(dev->hwprov, dev);
+ if (!has_hwts && !tb[ETHTOOL_A_TSCONFIG_HWTSTAMP_PROVIDER])
return -EOPNOTSUPP;
if (tb[ETHTOOL_A_TSCONFIG_HWTSTAMP_PROVIDER]) {
@@ -411,9 +413,12 @@ static int ethnl_set_tsconfig(struct ethnl_req_info *req_base,
/* Disable current time stamping if we try to enable
* another one
*/
- ret = dev_set_hwtstamp_phylib(dev, &zero_config, info->extack);
- if (ret < 0)
- goto err_free_hwprov;
+ if (has_hwts) {
+ ret = dev_set_hwtstamp_phylib(dev, &zero_config,
+ info->extack);
+ if (ret < 0)
+ goto err_free_hwprov;
+ }
/* Change the selected hwtstamp source */
__hwprov = rcu_replace_pointer(dev->hwprov, hwprov,
--
2.43.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH net-next v5 2/4] r8169: hold MDIO bus lock while loading firmware
2026-12-01 9:44 [PATCH net-next v5 0/4] net: phy: realtek: add PTP support for RTL8127a javen
2026-12-01 9:44 ` [PATCH net-next v5 1/4] net: ethtool: tsconfig: allow selecting a provider javen
@ 2026-12-01 9:44 ` javen
2026-12-01 10:18 ` Nicolai Buchwitz
2026-12-01 9:44 ` [PATCH net-next v5 3/4] net: phy: realtek: add PTP clock support for RTL8127a javen
2026-12-01 9:44 ` [PATCH net-next v5 4/4] net: phy: realtek: add hardware timestamping " javen
3 siblings, 1 reply; 8+ messages in thread
From: javen @ 2026-12-01 9:44 UTC (permalink / raw)
To: andrew, hkallweit1, linux, davem, edumazet, kuba, pabeni, nb,
maxime.chevallier
Cc: netdev, linux-kernel, daniel, vladimir.oltean, Javen Xu
From: Javen Xu <javen_xu@realsil.com.cn>
rtl_fw_write_firmware() accesses PHY registers directly, bypassing
phylib. Hold MDIO bus lock meanwhile, so other phylib users cannot
interleave their accesses with the firmware sequence.
Signed-off-by: Javen Xu <javen_xu@realsil.com.cn>
---
Changes in v3:
- New patch
Changes in v4:
- Skip the bus lock on PHY-less RTL8116af
- Restore tp->ocp_base before releasing the bus lock
Changes in v5:
- no changes
---
drivers/net/ethernet/realtek/r8169_main.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c
index f151ab681393..b289ff6c3064 100644
--- a/drivers/net/ethernet/realtek/r8169_main.c
+++ b/drivers/net/ethernet/realtek/r8169_main.c
@@ -2686,9 +2686,14 @@ void r8169_apply_firmware(struct rtl8169_private *tp)
/* TODO: release firmware if rtl_fw_write_firmware signals failure. */
if (tp->rtl_fw) {
+ /* Keep phylib users (hwmon, PTP) off the bus meanwhile. */
+ if (tp->phydev)
+ phy_lock_mdio_bus(tp->phydev);
rtl_fw_write_firmware(tp, tp->rtl_fw);
/* At least one firmware doesn't reset tp->ocp_base. */
tp->ocp_base = OCP_STD_PHY_BASE;
+ if (tp->phydev)
+ phy_unlock_mdio_bus(tp->phydev);
/* PHY soft reset may still be in progress */
if (tp->phydev)
--
2.43.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH net-next v5 3/4] net: phy: realtek: add PTP clock support for RTL8127a
2026-12-01 9:44 [PATCH net-next v5 0/4] net: phy: realtek: add PTP support for RTL8127a javen
2026-12-01 9:44 ` [PATCH net-next v5 1/4] net: ethtool: tsconfig: allow selecting a provider javen
2026-12-01 9:44 ` [PATCH net-next v5 2/4] r8169: hold MDIO bus lock while loading firmware javen
@ 2026-12-01 9:44 ` javen
2026-12-01 10:23 ` Nicolai Buchwitz
2026-12-01 9:44 ` [PATCH net-next v5 4/4] net: phy: realtek: add hardware timestamping " javen
3 siblings, 1 reply; 8+ messages in thread
From: javen @ 2026-12-01 9:44 UTC (permalink / raw)
To: andrew, hkallweit1, linux, davem, edumazet, kuba, pabeni, nb,
maxime.chevallier
Cc: netdev, linux-kernel, daniel, vladimir.oltean, Javen Xu
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
Changes in v5:
- Don't set the clock from the system time on link up, leave the time
to userspace.
---
drivers/net/phy/realtek/Kconfig | 11 +
drivers/net/phy/realtek/Makefile | 1 +
drivers/net/phy/realtek/realtek.h | 14 +
drivers/net/phy/realtek/realtek_main.c | 23 +-
drivers/net/phy/realtek/realtek_ptp.c | 380 +++++++++++++++++++++++++
5 files changed, 428 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..d1dc79c396e5 100644
--- a/drivers/net/phy/realtek/realtek.h
+++ b/drivers/net/phy/realtek/realtek.h
@@ -7,4 +7,18 @@
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);
+#else
+static inline int rtl8127a_ptp_probe(struct phy_device *phydev)
+{
+ return 0;
+}
+
+static inline void rtl8127a_ptp_remove(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 a39ad18382f2..483eaf6fb6b1 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,8 @@ 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,
.get_features = rtl822x_get_features,
.config_aneg = rtl822x_config_aneg,
.read_status = rtl822x_read_status,
diff --git a/drivers/net/phy/realtek/realtek_ptp.c b/drivers/net/phy/realtek/realtek_ptp.c
new file mode 100644
index 000000000000..72d0663a90c2
--- /dev/null
+++ b/drivers/net/phy/realtek/realtek_ptp.c
@@ -0,0 +1,380 @@
+// 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_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
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH net-next v5 4/4] net: phy: realtek: add hardware timestamping for RTL8127a
2026-12-01 9:44 [PATCH net-next v5 0/4] net: phy: realtek: add PTP support for RTL8127a javen
` (2 preceding siblings ...)
2026-12-01 9:44 ` [PATCH net-next v5 3/4] net: phy: realtek: add PTP clock support for RTL8127a javen
@ 2026-12-01 9:44 ` javen
3 siblings, 0 replies; 8+ messages in thread
From: javen @ 2026-12-01 9:44 UTC (permalink / raw)
To: andrew, hkallweit1, linux, davem, edumazet, kuba, pabeni, nb,
maxime.chevallier
Cc: netdev, linux-kernel, daniel, vladimir.oltean, Javen Xu
From: Javen Xu <javen_xu@realsil.com.cn>
The PTP block of the RTL8127a internal PHY also timestamps PTPv2 event
messages over UDP in both directions. Register a mii_timestamper for it,
so the PHY can provide hardware timestamps without any support from the
MAC driver.
The timestamps are read out over MDIO, which may sleep, so this is done
from the PTP aux worker. Received packets are queued until their
timestamp has been read, transmitted packets are completed once the
hardware reports their timestamp.
The hardware only keeps the most recent timestamp per message type and
direction and does not record the sequenceId. Hence only one TX
timestamp can be pending at a time, it is dropped if nothing arrives
within 15 seconds, and an RX timestamp may be overwritten by a later
message of the same type before it has been read.
Signed-off-by: Javen Xu <javen_xu@realsil.com.cn>
---
Changes in v3:
- Only take TX skbs which requested a hardware timestamp and only
PTPv2 event messages
- Latch the TX timestamp of the recorded message type, fail on timeout
instead of falling back to Sync
- Bound the RX queue, ignore PTPv1
- Only update the cached config after programming the hardware, only
reprogram the hardware on an on/off change
- Drop a pending TX skb when TX timestamping is turned off
- Back off in the aux worker instead of busy polling
- Restore timestamping in config_init()
- Document the per message type limitation
- Move the NETWORK_PHY_TIMESTAMPING dependency to patch 1
- Only support PTPv2 over UDP, drop the L2 and any other layer filters
Changes in v4:
- Wait for in-flight rxtstamp()/txtstamp() in remove()
- clear mii_ts after synchronize_net()
Changes in v5:
- Drop default_timestamp
- Drop synchronize_net() from remove()
- Splice the RX queue in the aux worker so TX is not starved
- Drop a pending TX skb in hwtstamp_set() when TX is turned off
- Try to trun the hardware off if enabling it fails
- Restore timestamping only link_change_notify()
---
drivers/net/phy/realtek/Kconfig | 10 +-
drivers/net/phy/realtek/realtek.h | 5 +
drivers/net/phy/realtek/realtek_main.c | 1 +
drivers/net/phy/realtek/realtek_ptp.c | 453 ++++++++++++++++++++++++-
4 files changed, 452 insertions(+), 17 deletions(-)
diff --git a/drivers/net/phy/realtek/Kconfig b/drivers/net/phy/realtek/Kconfig
index feb9a7fd1582..53ccea53fd5e 100644
--- a/drivers/net/phy/realtek/Kconfig
+++ b/drivers/net/phy/realtek/Kconfig
@@ -19,9 +19,11 @@ config REALTEK_PHY_PTP
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.
+ Optional PTP hardware clock and hardware timestamping support
+ for the internal PHY of RTL8127A. The PHY registers a PTP clock
+ and timestamps PTPv2 event messages over UDP on behalf of the
+ MAC, so no support from the MAC driver is needed.
+ The hardware keeps only one timestamp per message type and
+ direction, so a single PTP application on the link is assumed.
endif # REALTEK_PHY
diff --git a/drivers/net/phy/realtek/realtek.h b/drivers/net/phy/realtek/realtek.h
index d1dc79c396e5..47f84d911bc1 100644
--- a/drivers/net/phy/realtek/realtek.h
+++ b/drivers/net/phy/realtek/realtek.h
@@ -10,6 +10,7 @@ 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);
+void rtl8127a_ptp_link_change_notify(struct phy_device *phydev);
#else
static inline int rtl8127a_ptp_probe(struct phy_device *phydev)
{
@@ -19,6 +20,10 @@ static inline int rtl8127a_ptp_probe(struct phy_device *phydev)
static inline void rtl8127a_ptp_remove(struct phy_device *phydev)
{
}
+
+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 483eaf6fb6b1..704401117c3e 100644
--- a/drivers/net/phy/realtek/realtek_main.c
+++ b/drivers/net/phy/realtek/realtek_main.c
@@ -3444,6 +3444,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
index 72d0663a90c2..eff057e14c10 100644
--- a/drivers/net/phy/realtek/realtek_ptp.c
+++ b/drivers/net/phy/realtek/realtek_ptp.c
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0+
/*
- * PTP hardware clock support for the internal PHY of RTL8127A
+ * PTP hardware clock and timestamping support for the internal PHY of
+ * RTL8127A
*/
#include <linux/bitfield.h>
@@ -9,7 +10,9 @@
#include <linux/math64.h>
#include <linux/netdevice.h>
#include <linux/phy.h>
+#include <linux/ptp_classify.h>
#include <linux/ptp_clock_kernel.h>
+#include <linux/skbuff.h>
#include <linux/timekeeping.h>
#include "realtek.h"
@@ -30,8 +33,18 @@
#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_TRX_TS_STA 0xe430
+#define RTL8127A_PTP_TRX_TS_RD BIT(0)
+#define RTL8127A_PTP_TRX_TS_SEL_RX BIT(1)
+#define RTL8127A_PTP_TRX_TS_MSGTYPE GENMASK(3, 2)
+#define RTL8127A_PTP_TRX_TS_EXEC BIT(4)
+#define RTL8127A_PTP_TX_PDLYRSP_RDY BIT(12)
+#define RTL8127A_PTP_TX_PDLYREQ_RDY BIT(13)
+#define RTL8127A_PTP_TX_DLYREQ_RDY BIT(14)
+#define RTL8127A_PTP_TX_SYNC_RDY BIT(15)
+/* Time registers, CFG_TIME for the clock, TRX_TS_TIME for packets */
#define RTL8127A_PTP_CFG_TIME 0xe412
+#define RTL8127A_PTP_TRX_TS_TIME 0xe446
#define RTL8127A_PTP_NS_LO 0x0
#define RTL8127A_PTP_NS_HI 0x2
#define RTL8127A_PTP_S_LO 0x4
@@ -43,6 +56,9 @@
#define RTL8127A_PTP_RATE_HI_MASK GENMASK(21, 16)
#define RTL8127A_PTP_MAX_ADJ 488281
+#define RTL8127A_PTP_TX_TIMEOUT (15 * HZ)
+#define RTL8127A_PTP_TX_POLL_FAST msecs_to_jiffies(2)
+#define RTL8127A_PTP_RX_QUEUE_MAX 64
/* RTL8127A_PTP_CLK_CFG clock adjust modes */
enum rtl8127a_ptp_clkadj_mode {
@@ -53,11 +69,33 @@ enum rtl8127a_ptp_clkadj_mode {
RTL8127A_PTP_RATE_WRITE = 14,
};
+/* ptp->state bits */
+enum rtl8127a_ptp_state {
+ RTL8127A_PTP_TX_IN_PROGRESS,
+};
+
+struct rtl8127a_ptp_skb_cb {
+ u8 msgtype;
+};
+
+#define RTL8127A_PTP_SKB_CB(skb) \
+ ((struct rtl8127a_ptp_skb_cb *)(skb)->cb)
+
struct rtl8127a_ptp {
struct phy_device *phydev;
+ struct mii_timestamper mii_ts;
struct ptp_clock_info info;
struct ptp_clock *clock;
struct mutex lock; /* serializes PTP register access sequences */
+ enum hwtstamp_tx_types tx_type;
+ enum hwtstamp_rx_filters rx_filter;
+ bool rx_enabled;
+ bool hw_enabled;
+ struct sk_buff_head rx_queue;
+ struct sk_buff *tx_skb;
+ unsigned long tx_start;
+ u8 tx_msgtype;
+ unsigned long state;
bool time_set;
};
@@ -189,32 +227,80 @@ static int rtl8127a_ptp_write_time(struct rtl8127a_ptp *ptp,
return 0;
}
-static int rtl8127a_ptp_hwtstamp_enable(struct rtl8127a_ptp *ptp, bool enable)
+/* Latch the RX or TX timestamp of the given message type into the
+ * RTL8127A_PTP_TRX_TS_TIME registers.
+ */
+static int rtl8127a_ptp_trx_ts_cmd(struct rtl8127a_ptp *ptp, u16 cmd)
{
int ret;
- mutex_lock(&ptp->lock);
+ ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_TRX_TS_STA,
+ RTL8127A_PTP_TRX_TS_SEL_RX |
+ RTL8127A_PTP_TRX_TS_MSGTYPE,
+ RTL8127A_PTP_TRX_TS_RD | cmd);
+ if (ret)
+ return ret;
+
+ ret = rtl8127a_ptp_poll(ptp, RTL8127A_PTP_TRX_TS_STA,
+ RTL8127A_PTP_TRX_TS_RD, false);
+
+ return ret < 0 ? ret : 0;
+}
+
+static const u16 rtl8127a_ptp_tx_rdy[] = {
+ [PTP_MSGTYPE_SYNC] = RTL8127A_PTP_TX_SYNC_RDY,
+ [PTP_MSGTYPE_DELAY_REQ] = RTL8127A_PTP_TX_DLYREQ_RDY,
+ [PTP_MSGTYPE_PDELAY_REQ] = RTL8127A_PTP_TX_PDLYREQ_RDY,
+ [PTP_MSGTYPE_PDELAY_RESP] = RTL8127A_PTP_TX_PDLYRSP_RDY,
+};
+
+static int rtl8127a_ptp_egress_time(struct rtl8127a_ptp *ptp, u8 msgtype,
+ struct timespec64 *ts)
+{
+ int ret;
+
+ ret = rtl8127a_ptp_poll(ptp, RTL8127A_PTP_TRX_TS_STA,
+ rtl8127a_ptp_tx_rdy[msgtype], true);
+ if (ret < 0)
+ return ret;
+
+ ret = rtl8127a_ptp_trx_ts_cmd(ptp,
+ FIELD_PREP(RTL8127A_PTP_TRX_TS_MSGTYPE,
+ msgtype) |
+ RTL8127A_PTP_TRX_TS_EXEC);
+ if (ret)
+ return ret;
+
+ return rtl8127a_ptp_read_time(ptp, RTL8127A_PTP_TRX_TS_TIME, ts);
+}
+
+static int __rtl8127a_ptp_hwtstamp_enable(struct rtl8127a_ptp *ptp,
+ bool enable)
+{
+ int ret;
+
+ lockdep_assert_held(&ptp->lock);
ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_SYNCE_CTL,
RTL8127A_PTP_SYNCE_EN, 0);
if (ret)
- goto out;
+ return ret;
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;
+ return ret;
ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_GEN_CFG, 0,
RTL8127A_PTP_INT_CLR_MODE);
if (ret)
- goto out;
+ return ret;
ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_INSR, 0xffff);
if (ret)
- goto out;
+ return ret;
ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_CTL,
RTL8127A_PTP_CTL_TYPE_3 | BIT(12));
@@ -223,17 +309,24 @@ static int rtl8127a_ptp_hwtstamp_enable(struct rtl8127a_ptp *ptp, bool enable)
RTL8127A_PTP_INT_RX_TS |
RTL8127A_PTP_INT_TX_TS, 0);
if (ret)
- goto out;
+ return ret;
ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_CTL,
RTL8127A_PTP_CTL_EN, 0);
}
if (ret)
- goto out;
+ return ret;
- ret = rtl8127a_ptp_modify(ptp, RTL8127A_PHY_RST_CTL, 0,
- RTL8127A_PHY_RST);
-out:
+ return rtl8127a_ptp_modify(ptp, RTL8127A_PHY_RST_CTL, 0,
+ RTL8127A_PHY_RST);
+}
+
+static int rtl8127a_ptp_hwtstamp_enable(struct rtl8127a_ptp *ptp, bool enable)
+{
+ int ret;
+
+ mutex_lock(&ptp->lock);
+ ret = __rtl8127a_ptp_hwtstamp_enable(ptp, enable);
mutex_unlock(&ptp->lock);
return ret;
@@ -323,6 +416,124 @@ static int rtl8127a_ptp_settime64(struct ptp_clock_info *info,
return rtl8127a_ptp_settime(ptp, ts);
}
+static void rtl8127a_ptp_tx_complete(struct rtl8127a_ptp *ptp,
+ struct skb_shared_hwtstamps *hwts)
+{
+ struct sk_buff *skb = ptp->tx_skb;
+
+ ptp->tx_skb = NULL;
+ clear_bit_unlock(RTL8127A_PTP_TX_IN_PROGRESS, &ptp->state);
+
+ if (hwts)
+ skb_complete_tx_timestamp(skb, hwts);
+ else
+ kfree_skb(skb);
+}
+
+/* Returns true if no TX timestamp is pending anymore */
+static bool rtl8127a_ptp_tx_tstamp(struct rtl8127a_ptp *ptp)
+{
+ struct skb_shared_hwtstamps hwts = {};
+ struct timespec64 ts;
+ int ret;
+
+ /* Pairs with smp_store_release() in rtl8127a_ptp_txtstamp() */
+ if (!smp_load_acquire(&ptp->tx_skb))
+ return true;
+
+ mutex_lock(&ptp->lock);
+
+ /* rtl8127a_ptp_hwtstamp_set() may have dropped it meanwhile */
+ if (!ptp->tx_skb) {
+ mutex_unlock(&ptp->lock);
+ return true;
+ }
+
+ /* Give up if TX timestamping was turned off or nothing arrived */
+ if (READ_ONCE(ptp->tx_type) != HWTSTAMP_TX_ON ||
+ time_is_before_jiffies(ptp->tx_start + RTL8127A_PTP_TX_TIMEOUT)) {
+ rtl8127a_ptp_write(ptp, RTL8127A_PTP_INSR,
+ RTL8127A_PTP_INT_TX_TS);
+
+ rtl8127a_ptp_tx_complete(ptp, NULL);
+ mutex_unlock(&ptp->lock);
+ return true;
+ }
+
+ ret = rtl8127a_ptp_read(ptp, RTL8127A_PTP_INSR);
+ if (ret < 0 || !(ret & RTL8127A_PTP_INT_TX_TS)) {
+ mutex_unlock(&ptp->lock);
+ return false;
+ }
+
+ ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_INSR,
+ RTL8127A_PTP_INT_TX_TS);
+ if (!ret)
+ ret = rtl8127a_ptp_egress_time(ptp, ptp->tx_msgtype, &ts);
+
+ if (ret) {
+ rtl8127a_ptp_tx_complete(ptp, NULL);
+ } else {
+ hwts.hwtstamp = timespec64_to_ktime(ts);
+ rtl8127a_ptp_tx_complete(ptp, &hwts);
+ }
+
+ mutex_unlock(&ptp->lock);
+
+ return true;
+}
+
+/* The hardware keeps only the latest timestamp per message type and
+ * direction and does not record the sequenceID. A message of the same
+ * type received before the latch is read replaces the timestamp.
+ */
+static void rtl8127a_ptp_rx_tstamp(struct rtl8127a_ptp *ptp,
+ struct sk_buff *skb)
+{
+ u8 msgtype = RTL8127A_PTP_SKB_CB(skb)->msgtype;
+ struct timespec64 ts;
+ int ret;
+
+ mutex_lock(&ptp->lock);
+ ret = rtl8127a_ptp_trx_ts_cmd(ptp, RTL8127A_PTP_TRX_TS_SEL_RX |
+ FIELD_PREP(RTL8127A_PTP_TRX_TS_MSGTYPE,
+ msgtype) |
+ RTL8127A_PTP_TRX_TS_EXEC);
+ if (!ret)
+ ret = rtl8127a_ptp_read_time(ptp, RTL8127A_PTP_TRX_TS_TIME,
+ &ts);
+ mutex_unlock(&ptp->lock);
+
+ if (!ret)
+ skb_hwtstamps(skb)->hwtstamp = timespec64_to_ktime(ts);
+
+ netif_rx(skb);
+}
+
+static long rtl8127a_ptp_do_aux_work(struct ptp_clock_info *info)
+{
+ struct rtl8127a_ptp *ptp = container_of(info, struct rtl8127a_ptp, info);
+ struct sk_buff_head received;
+ struct sk_buff *skb;
+ unsigned long flags;
+
+ /* Only handle the RX frames queued so far, so TX is not starved */
+ __skb_queue_head_init(&received);
+ spin_lock_irqsave(&ptp->rx_queue.lock, flags);
+ skb_queue_splice_tail_init(&ptp->rx_queue, &received);
+ spin_unlock_irqrestore(&ptp->rx_queue.lock, flags);
+
+ while ((skb = __skb_dequeue(&received)))
+ rtl8127a_ptp_rx_tstamp(ptp, skb);
+
+ if (rtl8127a_ptp_tx_tstamp(ptp))
+ return -1;
+
+ /* The TX timestamp is normally ready right away, back off after that */
+ return time_before(jiffies, ptp->tx_start + RTL8127A_PTP_TX_POLL_FAST) ?
+ 0 : 1;
+}
+
static const struct ptp_clock_info rtl8127a_ptp_info = {
.owner = THIS_MODULE,
.name = "RTL8127A PHC",
@@ -331,8 +542,207 @@ static const struct ptp_clock_info rtl8127a_ptp_info = {
.adjtime = rtl8127a_ptp_adjtime,
.gettimex64 = rtl8127a_ptp_gettimex64,
.settime64 = rtl8127a_ptp_settime64,
+ .do_aux_work = rtl8127a_ptp_do_aux_work,
};
+void rtl8127a_ptp_link_change_notify(struct phy_device *phydev)
+{
+ struct rtl8127a_ptp *ptp = phydev->priv;
+ int ret = 0;
+
+ if (!ptp || !phydev->link)
+ return;
+
+ mutex_lock(&ptp->lock);
+ /* Restore timestamping in case the PTP block was reset. */
+ if (ptp->hw_enabled) {
+ ret = rtl8127a_ptp_read(ptp, RTL8127A_PTP_CTL);
+ if (ret >= 0 && !(ret & RTL8127A_PTP_CTL_EN))
+ ret = __rtl8127a_ptp_hwtstamp_enable(ptp, true);
+ }
+ mutex_unlock(&ptp->lock);
+
+ if (ret < 0)
+ phydev_warn(phydev, "failed to restore timestamping: %d\n", ret);
+}
+
+static void rtl8127a_ptp_txtstamp(struct mii_timestamper *mii_ts,
+ struct sk_buff *skb, int type)
+{
+ struct rtl8127a_ptp *ptp = container_of(mii_ts, struct rtl8127a_ptp,
+ mii_ts);
+ struct ptp_header *hdr;
+ u8 msgtype;
+
+ if (READ_ONCE(ptp->tx_type) != HWTSTAMP_TX_ON ||
+ !(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) ||
+ (type & PTP_CLASS_VMASK) != PTP_CLASS_V2 ||
+ (type & PTP_CLASS_PMASK) == PTP_CLASS_L2)
+ goto drop;
+
+ hdr = ptp_parse_header(skb, type);
+ if (!hdr)
+ goto drop;
+
+ /* Only event messages are timestamped by the hardware */
+ msgtype = ptp_get_msgtype(hdr, type);
+ if (msgtype > PTP_MSGTYPE_PDELAY_RESP)
+ goto drop;
+
+ /* Only one TX timestamp can be pending at a time */
+ if (test_and_set_bit_lock(RTL8127A_PTP_TX_IN_PROGRESS, &ptp->state))
+ goto drop;
+
+ skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
+ ptp->tx_msgtype = msgtype;
+ ptp->tx_start = jiffies;
+ /* Publish tx_msgtype and tx_start before tx_skb to the aux worker */
+ smp_store_release(&ptp->tx_skb, skb);
+ ptp_schedule_worker(ptp->clock, 0);
+ return;
+
+drop:
+ kfree_skb(skb);
+}
+
+static bool rtl8127a_ptp_rxtstamp(struct mii_timestamper *mii_ts,
+ struct sk_buff *skb, int type)
+{
+ struct rtl8127a_ptp *ptp = container_of(mii_ts, struct rtl8127a_ptp,
+ mii_ts);
+ struct ptp_header *hdr;
+ u8 msgtype;
+
+ if (!READ_ONCE(ptp->rx_enabled) ||
+ (type & PTP_CLASS_VMASK) != PTP_CLASS_V2 ||
+ (type & PTP_CLASS_PMASK) == PTP_CLASS_L2 ||
+ skb_queue_len_lockless(&ptp->rx_queue) >= RTL8127A_PTP_RX_QUEUE_MAX)
+ return false;
+
+ hdr = ptp_parse_header(skb, type);
+ if (!hdr)
+ return false;
+
+ /* Only event messages are timestamped by the hardware */
+ msgtype = ptp_get_msgtype(hdr, type);
+ if (msgtype > PTP_MSGTYPE_PDELAY_RESP)
+ return false;
+
+ RTL8127A_PTP_SKB_CB(skb)->msgtype = msgtype;
+ skb_queue_tail(&ptp->rx_queue, skb);
+ ptp_schedule_worker(ptp->clock, 0);
+
+ return true;
+}
+
+static int rtl8127a_ptp_hwtstamp_get(struct mii_timestamper *mii_ts,
+ struct kernel_hwtstamp_config *config)
+{
+ struct rtl8127a_ptp *ptp = container_of(mii_ts, struct rtl8127a_ptp,
+ mii_ts);
+
+ config->tx_type = ptp->tx_type;
+ config->rx_filter = ptp->rx_filter;
+
+ return 0;
+}
+
+static int rtl8127a_ptp_hwtstamp_set(struct mii_timestamper *mii_ts,
+ struct kernel_hwtstamp_config *config,
+ struct netlink_ext_ack *extack)
+{
+ struct rtl8127a_ptp *ptp = container_of(mii_ts, struct rtl8127a_ptp,
+ mii_ts);
+ bool enable = false;
+ bool rx_enabled;
+ int ret = 0;
+
+ if (config->flags)
+ return -EINVAL;
+
+ switch (config->tx_type) {
+ case HWTSTAMP_TX_ON:
+ enable = true;
+ break;
+ case HWTSTAMP_TX_OFF:
+ break;
+ default:
+ return -ERANGE;
+ }
+
+ switch (config->rx_filter) {
+ case HWTSTAMP_FILTER_NONE:
+ rx_enabled = false;
+ break;
+ case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
+ case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
+ case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
+ config->rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
+ rx_enabled = true;
+ enable = true;
+ break;
+ default:
+ return -ERANGE;
+ }
+
+ /* The hardware is either on or off, only reprogram it on a change */
+ mutex_lock(&ptp->lock);
+ if (enable != ptp->hw_enabled) {
+ /* Stop timestamping before turning the hardware off */
+ if (!enable) {
+ WRITE_ONCE(ptp->tx_type, HWTSTAMP_TX_OFF);
+ WRITE_ONCE(ptp->rx_enabled, false);
+ }
+
+ ret = __rtl8127a_ptp_hwtstamp_enable(ptp, enable);
+ if (ret) {
+ /* The hardware state is unknown, turn timestamping
+ * off and have the next request reprogram it.
+ */
+ if (enable)
+ __rtl8127a_ptp_hwtstamp_enable(ptp, false);
+ ptp->hw_enabled = false;
+ ptp->rx_filter = HWTSTAMP_FILTER_NONE;
+ WRITE_ONCE(ptp->rx_enabled, false);
+ WRITE_ONCE(ptp->tx_type, HWTSTAMP_TX_OFF);
+ goto out;
+ }
+
+ ptp->hw_enabled = enable;
+ }
+
+ ptp->rx_filter = config->rx_filter;
+ WRITE_ONCE(ptp->rx_enabled, rx_enabled);
+ WRITE_ONCE(ptp->tx_type, config->tx_type);
+out:
+ /* Drop a pending TX timestamp that is no longer wanted */
+ if (ptp->tx_type != HWTSTAMP_TX_ON && READ_ONCE(ptp->tx_skb))
+ rtl8127a_ptp_tx_complete(ptp, NULL);
+ mutex_unlock(&ptp->lock);
+
+ return ret;
+}
+
+static int rtl8127a_ptp_ts_info(struct mii_timestamper *mii_ts,
+ struct kernel_ethtool_ts_info *info)
+{
+ struct rtl8127a_ptp *ptp = container_of(mii_ts, struct rtl8127a_ptp,
+ mii_ts);
+
+ info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
+ SOF_TIMESTAMPING_TX_HARDWARE |
+ SOF_TIMESTAMPING_RX_HARDWARE |
+ SOF_TIMESTAMPING_RAW_HARDWARE;
+ info->phc_index = ptp_clock_index(ptp->clock);
+ info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON);
+ info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
+ BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
+ BIT(HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
+ BIT(HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ);
+
+ return 0;
+}
+
int rtl8127a_ptp_probe(struct phy_device *phydev)
{
struct device *dev = &phydev->mdio.dev;
@@ -349,6 +759,9 @@ int rtl8127a_ptp_probe(struct phy_device *phydev)
ptp->phydev = phydev;
ptp->info = rtl8127a_ptp_info;
+ ptp->tx_type = HWTSTAMP_TX_OFF;
+ ptp->rx_filter = HWTSTAMP_FILTER_NONE;
+ skb_queue_head_init(&ptp->rx_queue);
ret = rtl8127a_ptp_hwtstamp_enable(ptp, false);
if (ret)
@@ -362,7 +775,14 @@ int rtl8127a_ptp_probe(struct phy_device *phydev)
if (IS_ERR(ptp->clock))
return PTR_ERR(ptp->clock);
+ ptp->mii_ts.rxtstamp = rtl8127a_ptp_rxtstamp;
+ ptp->mii_ts.txtstamp = rtl8127a_ptp_txtstamp;
+ ptp->mii_ts.hwtstamp_set = rtl8127a_ptp_hwtstamp_set;
+ ptp->mii_ts.hwtstamp_get = rtl8127a_ptp_hwtstamp_get;
+ ptp->mii_ts.ts_info = rtl8127a_ptp_ts_info;
+
phydev->priv = ptp;
+ phydev->mii_ts = &ptp->mii_ts;
return 0;
}
@@ -374,7 +794,14 @@ void rtl8127a_ptp_remove(struct phy_device *phydev)
if (!ptp)
return;
+ phydev->mii_ts = NULL;
+
rtl8127a_ptp_hwtstamp_enable(ptp, false);
+ /* Also stops the aux worker */
ptp_clock_unregister(ptp->clock);
+
+ if (ptp->tx_skb)
+ rtl8127a_ptp_tx_complete(ptp, NULL);
+ skb_queue_purge(&ptp->rx_queue);
phydev->priv = NULL;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH net-next v5 1/4] net: ethtool: tsconfig: allow selecting a provider
2026-12-01 9:44 ` [PATCH net-next v5 1/4] net: ethtool: tsconfig: allow selecting a provider javen
@ 2026-12-01 10:06 ` Nicolai Buchwitz
0 siblings, 0 replies; 8+ messages in thread
From: Nicolai Buchwitz @ 2026-12-01 10:06 UTC (permalink / raw)
To: javen
Cc: andrew, hkallweit1, linux, davem, edumazet, kuba, pabeni,
maxime.chevallier, netdev, linux-kernel, daniel, vladimir.oltean
Hi Javen
On 1.12.2026 10:44, javen wrote:
> From: Javen <javen_xu@realsil.com.cn>
>
> A hwtstamp provider can obly ne selected with ETHTOOL_MSG_TSCONFIG_SET
nit: obly ne -> only be
> when the device already has an active time stamping source: a MAC
> implementing ndo_hwtstamp_set(), a PHY flagged as default_timestamp, or
> a previously selected dev->hwprov.
>
> This patch makes it possible to select a PHY time stamper on a MAC
> without time stamping support. New phy drivers are expected to reply on
nit: reply on -> rely on
> dev->hwprov rather than default_timestamp.
>
> Signed-off-by: Javen <javen_xu@realsil.com.cn>
Please use your full name in the trailer and From (as in the other
patches).
> [...]
With the above fixed:
Reviewed-by: Nicolai Buchwitz <nb@tipi-net.de>
Thanks,
Nicolai
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH net-next v5 2/4] r8169: hold MDIO bus lock while loading firmware
2026-12-01 9:44 ` [PATCH net-next v5 2/4] r8169: hold MDIO bus lock while loading firmware javen
@ 2026-12-01 10:18 ` Nicolai Buchwitz
0 siblings, 0 replies; 8+ messages in thread
From: Nicolai Buchwitz @ 2026-12-01 10:18 UTC (permalink / raw)
To: javen
Cc: andrew, hkallweit1, linux, davem, edumazet, kuba, pabeni,
maxime.chevallier, netdev, linux-kernel, daniel, vladimir.oltean
Hi Javen
On 1.12.2026 10:44, javen wrote:
> [...]
> diff --git a/drivers/net/ethernet/realtek/r8169_main.c
> b/drivers/net/ethernet/realtek/r8169_main.c
> index f151ab681393..b289ff6c3064 100644
> --- a/drivers/net/ethernet/realtek/r8169_main.c
> +++ b/drivers/net/ethernet/realtek/r8169_main.c
> @@ -2686,9 +2686,14 @@ void r8169_apply_firmware(struct rtl8169_private
> *tp)
>
> /* TODO: release firmware if rtl_fw_write_firmware signals failure.
> */
> if (tp->rtl_fw) {
> + /* Keep phylib users (hwmon, PTP) off the bus meanwhile. */
nit: PTP doesn't exist yet at this point, maybe just drop it.
> [...]
Reviewed-by: Nicolai Buchwitz <nb@tipi-net.de>
Thanks,
Nicolai
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH net-next v5 3/4] net: phy: realtek: add PTP clock support for RTL8127a
2026-12-01 9:44 ` [PATCH net-next v5 3/4] net: phy: realtek: add PTP clock support for RTL8127a javen
@ 2026-12-01 10:23 ` Nicolai Buchwitz
0 siblings, 0 replies; 8+ messages in thread
From: Nicolai Buchwitz @ 2026-12-01 10:23 UTC (permalink / raw)
To: javen
Cc: andrew, hkallweit1, linux, davem, edumazet, kuba, pabeni,
maxime.chevallier, netdev, linux-kernel, daniel, vladimir.oltean
Hi Javen
On 1.12.2026 10:44, javen wrote:
> 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.
The code doesn't do this anymore, and time_set is unused. Please drop
both.
>
> 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
>
> Changes in v5:
> - Don't set the clock from the system time on link up, leave the time
> to userspace.
> ---
> [...]
> +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);
Could use phy_read_mmd_poll_timeout() ?
> [...]
> +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) {
Unused until 4/4, maybe move it there ?
> [...]
> +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);
diff_by_scaled_ppm() would keep the full resolution.
> [...]
> +static int rtl8127a_ptp_gettimex64(struct ptp_clock_info *info,
> + struct timespec64 *ts,
> + struct ptp_system_timestamp *sts)
> +{
What happens when r8169 is runtime suspended? Nothing on this path
resumes it.
> [...]
Thanks,
Nicolai
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-12-01 10:23 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 9:44 [PATCH net-next v5 0/4] net: phy: realtek: add PTP support for RTL8127a javen
2026-12-01 9:44 ` [PATCH net-next v5 1/4] net: ethtool: tsconfig: allow selecting a provider javen
2026-12-01 10:06 ` Nicolai Buchwitz
2026-12-01 9:44 ` [PATCH net-next v5 2/4] r8169: hold MDIO bus lock while loading firmware javen
2026-12-01 10:18 ` Nicolai Buchwitz
2026-12-01 9:44 ` [PATCH net-next v5 3/4] net: phy: realtek: add PTP clock support for RTL8127a javen
2026-12-01 10:23 ` Nicolai Buchwitz
2026-12-01 9:44 ` [PATCH net-next v5 4/4] net: phy: realtek: add hardware timestamping " javen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox