From: wei.fang@oss.nxp.com
To: xiaoning.wang@nxp.com, andrew@lunn.ch, olteanv@gmail.com,
andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@google.com,
kuba@kernel.org, pabeni@redhat.com, horms@kernel.org,
richardcochran@gmail.com
Cc: wei.fang@nxp.com, imx@lists.linux.dev, netdev@vger.kernel.org,
linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org,
linux-arm-kernel@lists.infradead.org
Subject: [PATCH v2 net-next 3/7] ptp: netc: export netc_timer_get_current_time() for cross-driver use
Date: Sat, 8 Aug 2026 11:21:42 +0800 [thread overview]
Message-ID: <20260808032146.2335723-5-wei.fang@oss.nxp.com> (raw)
In-Reply-To: <20260808032146.2335723-1-wei.fang@oss.nxp.com>
From: Wei Fang <wei.fang@nxp.com>
The NETC Switch does not have its own time registers and must obtain
the current PTP time from the NETC Timer bound to it. Since the two
are separate PCIe functions with independent drivers, add
netc_timer_get_current_time() to the Timer driver and export it via
EXPORT_SYMBOL_GPL().
The function takes the Timer's pci_dev pointer, acquires the per-device
spinlock to protect against concurrent register access, and reads
TMR_CUR_TIME via netc_timer_cur_time_read(). It returns 0 if the Timer
driver has not yet probed or has already been removed, allowing the
caller to handle the unavailable case gracefully.
Hold the device lock around pci_get_drvdata() and the register read to
serialize against concurrent driver unbind. The remove() callback runs
under the same device lock, so this guarantees that priv and the MMIO
mapping remain valid for the entire duration of the read.
Move spin_lock_init() from netc_timer_probe() into netc_timer_pci_probe(),
before pci_set_drvdata(), so that the spinlock is fully initialized before
the driver data becomes visible to other drivers.
Signed-off-by: Wei Fang <wei.fang@nxp.com>
---
drivers/ptp/ptp_netc.c | 43 ++++++++++++++++++++++++++++++++-
include/linux/fsl/netc_global.h | 10 ++++++++
2 files changed, 52 insertions(+), 1 deletion(-)
diff --git a/drivers/ptp/ptp_netc.c b/drivers/ptp/ptp_netc.c
index d33c49c86ac4..27f85818d6a8 100644
--- a/drivers/ptp/ptp_netc.c
+++ b/drivers/ptp/ptp_netc.c
@@ -165,6 +165,47 @@ static u64 netc_timer_cur_time_read(struct netc_timer *priv)
return netc_timer_rd64(priv, NETC_TMR_CUR_TIME_L);
}
+/**
+ * netc_timer_get_current_time - read the current PTP time from the NETC Timer
+ * @pdev: PCI device of the NETC Timer
+ *
+ * Reads the 64-bit current time register (TMR_CUR_TIME) from the NETC Timer
+ * device associated with @pdev. Returns 0 if the Timer driver has not yet
+ * probed or has already been removed.
+ *
+ * Context: Process context only. Acquires the device mutex via device_lock(),
+ * which may sleep. Must not be called from atomic context, softirq,
+ * BH, or while holding a spinlock.
+ *
+ * Return: Current PTP time in nanoseconds, or 0 if the timer is unavailable.
+ */
+u64 netc_timer_get_current_time(struct pci_dev *pdev)
+{
+ struct netc_timer *priv;
+ unsigned long flags;
+ u64 cur_time = 0;
+
+ /* Serialize against driver unbind: the remove() callback runs under
+ * the device lock, so holding it here ensures that priv remains valid
+ * for the entire duration of the register read.
+ */
+ device_lock(&pdev->dev);
+
+ priv = pci_get_drvdata(pdev);
+ if (!priv)
+ goto unlock_device;
+
+ spin_lock_irqsave(&priv->lock, flags);
+ cur_time = netc_timer_cur_time_read(priv);
+ spin_unlock_irqrestore(&priv->lock, flags);
+
+unlock_device:
+ device_unlock(&pdev->dev);
+
+ return cur_time;
+}
+EXPORT_SYMBOL_GPL(netc_timer_get_current_time);
+
static void netc_timer_alarm_write(struct netc_timer *priv,
u64 alarm, int index)
{
@@ -795,6 +836,7 @@ static int netc_timer_pci_probe(struct pci_dev *pdev)
goto release_mem_regions;
}
+ spin_lock_init(&priv->lock);
pci_set_drvdata(pdev, priv);
return 0;
@@ -965,7 +1007,6 @@ static int netc_timer_probe(struct pci_dev *pdev,
priv->caps = netc_timer_ptp_caps;
priv->oclk_prsc = NETC_TMR_DEFAULT_PRSC;
priv->pps_channel = NETC_TMR_INVALID_CHANNEL;
- spin_lock_init(&priv->lock);
snprintf(priv->irq_name, sizeof(priv->irq_name), "ptp-netc %s",
pci_name(pdev));
diff --git a/include/linux/fsl/netc_global.h b/include/linux/fsl/netc_global.h
index 5b8ff528d369..ace0bcde1e2c 100644
--- a/include/linux/fsl/netc_global.h
+++ b/include/linux/fsl/netc_global.h
@@ -6,6 +6,7 @@
#include <linux/io.h>
#include <linux/io-64-nonatomic-lo-hi.h>
+#include <linux/pci.h>
static inline u32 netc_read(void __iomem *reg)
{
@@ -22,4 +23,13 @@ static inline u64 netc_read64(void __iomem *reg)
return ioread64(reg);
}
+#if IS_REACHABLE(CONFIG_PTP_NETC_V4_TIMER)
+u64 netc_timer_get_current_time(struct pci_dev *timer_dev);
+#else
+static inline u64 netc_timer_get_current_time(struct pci_dev *timer_dev)
+{
+ return 0;
+}
+#endif
+
#endif
--
2.34.1
next prev parent reply other threads:[~2026-08-08 3:18 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-08 3:21 [PATCH v2 net-next 0/7] net: dsa: netc: add PTP support for NETC switch wei.fang
2026-08-08 3:21 ` [PATCH] net: dsa: netc: add PTP one-step timestamping support wei.fang
2026-08-08 3:26 ` Wei Fang
2026-08-08 3:21 ` [PATCH v2 net-next 1/7] ptp: netc: use ioread64_lo_hi/iowrite64_lo_hi for 64-bit register access wei.fang
2026-08-08 3:21 ` [PATCH v2 net-next 2/7] ptp: netc: remove unnecessary pcie_flr() call in probe wei.fang
2026-08-08 3:21 ` wei.fang [this message]
2026-08-08 3:21 ` [PATCH v2 net-next 4/7] net: dsa: netc: use entry ID instead of pointer to track host flood rule wei.fang
2026-08-08 3:21 ` [PATCH v2 net-next 5/7] net: dsa: netc: enable ingress port filtering lookup by default wei.fang
2026-08-08 3:21 ` [PATCH v2 net-next 6/7] net: dsa: netc: add PTP two-step timestamping support wei.fang
2026-08-08 3:21 ` [PATCH v2 net-next 7/7] net: dsa: netc: add PTP one-step " wei.fang
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=20260808032146.2335723-5-wei.fang@oss.nxp.com \
--to=wei.fang@oss.nxp.com \
--cc=andrew+netdev@lunn.ch \
--cc=andrew@lunn.ch \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=horms@kernel.org \
--cc=imx@lists.linux.dev \
--cc=kuba@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=netdev@vger.kernel.org \
--cc=olteanv@gmail.com \
--cc=pabeni@redhat.com \
--cc=richardcochran@gmail.com \
--cc=wei.fang@nxp.com \
--cc=xiaoning.wang@nxp.com \
/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