From: Frank Li <Frank.li@nxp.com>
To: Wei Fang <wei.fang@nxp.com>
Cc: robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org,
richardcochran@gmail.com, claudiu.manoil@nxp.com,
vladimir.oltean@nxp.com, xiaoning.wang@nxp.com,
andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@google.com,
kuba@kernel.org, pabeni@redhat.com, vadim.fedorenko@linux.dev,
shawnguo@kernel.org, s.hauer@pengutronix.de, festevam@gmail.com,
fushi.peng@nxp.com, devicetree@vger.kernel.org,
netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
imx@lists.linux.dev, kernel@pengutronix.de
Subject: Re: [PATCH v2 net-next 04/14] ptp: netc: add PTP_CLK_REQ_PPS support
Date: Wed, 16 Jul 2025 16:05:59 -0400 [thread overview]
Message-ID: <aHgGJ6sia5Xe7AA9@lizhi-Precision-Tower-5810> (raw)
In-Reply-To: <20250716073111.367382-5-wei.fang@nxp.com>
On Wed, Jul 16, 2025 at 03:31:01PM +0800, Wei Fang wrote:
> The NETC Times is able to generate the PPS event, so add PTP_CLK_REQ_PPS
> support. In addition, if there is a time drift when PPS is enabled, the
> PPS event will not be generated at an integral second of PHC. Based on
> the suggestion from IP team, FIPER should be disabled before adjusting
> the hardware time and then rearm ALARM after the time adjustment to make
> the next PPS event be generated at an integral second of PHC. Finally,
> re-enable FIPER.
Add PTP_CLK_REQ_PPS supports.
The suggested steps by IP team if time drift happen:
1: Disable FIPER before adjusting the hardware time
2: rearm ALARM after the time adjustment to make ...
3: re-enable FIPER.
>
> Signed-off-by: Wei Fang <wei.fang@nxp.com>
>
> ---
> v2 changes:
> 1. Refine the subject and the commit message
> 2. Add a comment to netc_timer_enable_pps()
> 3. Remove the "nxp,pps-channel" logic from the driver
> ---
> drivers/ptp/ptp_netc.c | 176 ++++++++++++++++++++++++++++++++++++++++-
> 1 file changed, 175 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/ptp/ptp_netc.c b/drivers/ptp/ptp_netc.c
> index 82cb1e6a0fe9..e39605c5b73b 100644
> --- a/drivers/ptp/ptp_netc.c
> +++ b/drivers/ptp/ptp_netc.c
> @@ -24,6 +24,8 @@
> #define TMR_ALARM1P BIT(31)
>
> #define NETC_TMR_TEVENT 0x0084
> +#define TMR_TEVNET_PPEN(i) BIT(7 - (i))
> +#define TMR_TEVENT_PPEN_ALL GENMASK(7, 5)
> #define TMR_TEVENT_ALM1EN BIT(16)
> #define TMR_TEVENT_ALM2EN BIT(17)
>
> @@ -39,9 +41,15 @@
> #define NETC_TMR_ALARM_L(i) (0x00b8 + (i) * 8)
> #define NETC_TMR_ALARM_H(i) (0x00bc + (i) * 8)
>
> +/* i = 0, 1, 2. i indicates the index of TMR_FIPER. */
> +#define NETC_TMR_FIPER(i) (0x00d0 + (i) * 4)
> +
> #define NETC_TMR_FIPER_CTRL 0x00dc
> #define FIPER_CTRL_DIS(i) (BIT(7) << (i) * 8)
> #define FIPER_CTRL_PG(i) (BIT(6) << (i) * 8)
> +#define FIPER_CTRL_FS_ALARM(i) (BIT(5) << (i) * 8)
> +#define FIPER_CTRL_PW(i) (GENMASK(4, 0) << (i) * 8)
> +#define FIPER_CTRL_SET_PW(i, v) (((v) & GENMASK(4, 0)) << 8 * (i))
>
> #define NETC_TMR_CUR_TIME_L 0x00f0
> #define NETC_TMR_CUR_TIME_H 0x00f4
> @@ -51,6 +59,9 @@
> #define NETC_TMR_FIPER_NUM 3
> #define NETC_TMR_DEFAULT_PRSC 2
> #define NETC_TMR_DEFAULT_ALARM GENMASK_ULL(63, 0)
> +#define NETC_TMR_DEFAULT_PPS_CHANNEL 0
> +#define NETC_TMR_DEFAULT_FIPER GENMASK(31, 0)
> +#define NETC_TMR_FIPER_MAX_PW GENMASK(4, 0)
>
> /* 1588 timer reference clock source select */
> #define NETC_TMR_CCM_TIMER1 0 /* enet_timer1_clk_root, from CCM */
> @@ -75,6 +86,8 @@ struct netc_timer {
> u64 period;
>
> int irq;
> + u8 pps_channel;
> + bool pps_enabled;
> };
>
> #define netc_timer_rd(p, o) netc_read((p)->base + (o))
> @@ -152,6 +165,147 @@ static void netc_timer_alarm_write(struct netc_timer *priv,
> netc_timer_wr(priv, NETC_TMR_ALARM_H(index), alarm_h);
> }
>
> +static u32 netc_timer_get_integral_period(struct netc_timer *priv)
> +{
> + u32 tmr_ctrl, integral_period;
> +
> + tmr_ctrl = netc_timer_rd(priv, NETC_TMR_CTRL);
> + integral_period = FIELD_GET(TMR_CTRL_TCLK_PERIOD, tmr_ctrl);
> +
> + return integral_period;
> +}
> +
> +static u32 netc_timer_calculate_fiper_pw(struct netc_timer *priv,
> + u32 fiper)
> +{
> + u64 divisor, pulse_width;
> +
> + /* Set the FIPER pulse width to half FIPER interval by default.
> + * pulse_width = (fiper / 2) / TMR_GCLK_period,
> + * TMR_GCLK_period = NSEC_PER_SEC / TMR_GCLK_freq,
> + * TMR_GCLK_freq = (clk_freq / oclk_prsc) Hz,
> + * so pulse_width = fiper * clk_freq / (2 * NSEC_PER_SEC * oclk_prsc).
> + */
> + divisor = mul_u32_u32(2000000000U, priv->oclk_prsc);
is it 2*PSEC_PER_SEC ?
Frank
> + pulse_width = div64_u64(mul_u32_u32(fiper, priv->clk_freq), divisor);
> +
> + /* The FIPER_PW field only has 5 bits, need to update oclk_prsc */
> + if (pulse_width > NETC_TMR_FIPER_MAX_PW)
> + pulse_width = NETC_TMR_FIPER_MAX_PW;
> +
> + return pulse_width;
> +}
> +
> +static void netc_timer_set_pps_alarm(struct netc_timer *priv, int channel,
> + u32 integral_period)
> +{
> + u64 alarm;
> +
> + /* Get the alarm value */
> + alarm = netc_timer_cur_time_read(priv) + NSEC_PER_MSEC;
> + alarm = roundup_u64(alarm, NSEC_PER_SEC);
> + alarm = roundup_u64(alarm, integral_period);
> +
> + netc_timer_alarm_write(priv, alarm, 0);
> +}
> +
> +/* Note that users should not use this API to output PPS signal on
> + * external pins, because PTP_CLK_REQ_PPS trigger internal PPS event
> + * for input into kernel PPS subsystem. See:
> + * https://lore.kernel.org/r/20201117213826.18235-1-a.fatoum@pengutronix.de
> + */
> +static int netc_timer_enable_pps(struct netc_timer *priv,
> + struct ptp_clock_request *rq, int on)
> +{
> + u32 tmr_emask, fiper, fiper_ctrl;
> + u8 channel = priv->pps_channel;
> + unsigned long flags;
> +
> + spin_lock_irqsave(&priv->lock, flags);
> +
> + tmr_emask = netc_timer_rd(priv, NETC_TMR_TEMASK);
> + fiper_ctrl = netc_timer_rd(priv, NETC_TMR_FIPER_CTRL);
> +
> + if (on) {
> + u32 integral_period, fiper_pw;
> +
> + if (priv->pps_enabled)
> + goto unlock_spinlock;
> +
> + integral_period = netc_timer_get_integral_period(priv);
> + fiper = NSEC_PER_SEC - integral_period;
> + fiper_pw = netc_timer_calculate_fiper_pw(priv, fiper);
> + fiper_ctrl &= ~(FIPER_CTRL_DIS(channel) | FIPER_CTRL_PW(channel) |
> + FIPER_CTRL_FS_ALARM(channel));
> + fiper_ctrl |= FIPER_CTRL_SET_PW(channel, fiper_pw);
> + tmr_emask |= TMR_TEVNET_PPEN(channel);
> + priv->pps_enabled = true;
> + netc_timer_set_pps_alarm(priv, channel, integral_period);
> + } else {
> + if (!priv->pps_enabled)
> + goto unlock_spinlock;
> +
> + fiper = NETC_TMR_DEFAULT_FIPER;
> + tmr_emask &= ~TMR_TEVNET_PPEN(channel);
> + fiper_ctrl |= FIPER_CTRL_DIS(channel);
> + priv->pps_enabled = false;
> + }
> +
> + netc_timer_wr(priv, NETC_TMR_TEMASK, tmr_emask);
> + netc_timer_wr(priv, NETC_TMR_FIPER(channel), fiper);
> + netc_timer_wr(priv, NETC_TMR_FIPER_CTRL, fiper_ctrl);
> +
> +unlock_spinlock:
> + spin_unlock_irqrestore(&priv->lock, flags);
> +
> + return 0;
> +}
> +
> +static void netc_timer_disable_pps_fiper(struct netc_timer *priv)
> +{
> + u32 fiper = NETC_TMR_DEFAULT_FIPER;
> + u8 channel = priv->pps_channel;
> + u32 fiper_ctrl;
> +
> + if (!priv->pps_enabled)
> + return;
> +
> + fiper_ctrl = netc_timer_rd(priv, NETC_TMR_FIPER_CTRL);
> + fiper_ctrl |= FIPER_CTRL_DIS(channel);
> + netc_timer_wr(priv, NETC_TMR_FIPER(channel), fiper);
> + netc_timer_wr(priv, NETC_TMR_FIPER_CTRL, fiper_ctrl);
> +}
> +
> +static void netc_timer_enable_pps_fiper(struct netc_timer *priv)
> +{
> + u32 fiper_ctrl, integral_period, fiper;
> + u8 channel = priv->pps_channel;
> +
> + if (!priv->pps_enabled)
> + return;
> +
> + integral_period = netc_timer_get_integral_period(priv);
> + fiper_ctrl = netc_timer_rd(priv, NETC_TMR_FIPER_CTRL);
> + fiper_ctrl &= ~FIPER_CTRL_DIS(channel);
> + fiper = NSEC_PER_SEC - integral_period;
> + netc_timer_set_pps_alarm(priv, channel, integral_period);
> + netc_timer_wr(priv, NETC_TMR_FIPER(channel), fiper);
> + netc_timer_wr(priv, NETC_TMR_FIPER_CTRL, fiper_ctrl);
> +}
> +
> +static int netc_timer_enable(struct ptp_clock_info *ptp,
> + struct ptp_clock_request *rq, int on)
> +{
> + struct netc_timer *priv = ptp_to_netc_timer(ptp);
> +
> + switch (rq->type) {
> + case PTP_CLK_REQ_PPS:
> + return netc_timer_enable_pps(priv, rq, on);
> + default:
> + return -EOPNOTSUPP;
> + }
> +}
> +
> static void netc_timer_adjust_period(struct netc_timer *priv, u64 period)
> {
> u32 fractional_period = lower_32_bits(period);
> @@ -164,8 +318,11 @@ static void netc_timer_adjust_period(struct netc_timer *priv, u64 period)
> old_tmr_ctrl = netc_timer_rd(priv, NETC_TMR_CTRL);
> tmr_ctrl = u32_replace_bits(old_tmr_ctrl, integral_period,
> TMR_CTRL_TCLK_PERIOD);
> - if (tmr_ctrl != old_tmr_ctrl)
> + if (tmr_ctrl != old_tmr_ctrl) {
> + netc_timer_disable_pps_fiper(priv);
> netc_timer_wr(priv, NETC_TMR_CTRL, tmr_ctrl);
> + netc_timer_enable_pps_fiper(priv);
> + }
>
> netc_timer_wr(priv, NETC_TMR_ADD, fractional_period);
>
> @@ -191,6 +348,8 @@ static int netc_timer_adjtime(struct ptp_clock_info *ptp, s64 delta)
>
> spin_lock_irqsave(&priv->lock, flags);
>
> + netc_timer_disable_pps_fiper(priv);
> +
> tmr_off = netc_timer_offset_read(priv);
> if (delta < 0 && tmr_off < abs(delta)) {
> delta += tmr_off;
> @@ -205,6 +364,8 @@ static int netc_timer_adjtime(struct ptp_clock_info *ptp, s64 delta)
> netc_timer_offset_write(priv, tmr_off);
> }
>
> + netc_timer_enable_pps_fiper(priv);
> +
> spin_unlock_irqrestore(&priv->lock, flags);
>
> return 0;
> @@ -239,8 +400,12 @@ static int netc_timer_settime64(struct ptp_clock_info *ptp,
> unsigned long flags;
>
> spin_lock_irqsave(&priv->lock, flags);
> +
> + netc_timer_disable_pps_fiper(priv);
> netc_timer_offset_write(priv, 0);
> netc_timer_cnt_write(priv, ns);
> + netc_timer_enable_pps_fiper(priv);
> +
> spin_unlock_irqrestore(&priv->lock, flags);
>
> return 0;
> @@ -267,10 +432,12 @@ static const struct ptp_clock_info netc_timer_ptp_caps = {
> .max_adj = 500000000,
> .n_alarm = 2,
> .n_pins = 0,
> + .pps = 1,
> .adjfine = netc_timer_adjfine,
> .adjtime = netc_timer_adjtime,
> .gettimex64 = netc_timer_gettimex64,
> .settime64 = netc_timer_settime64,
> + .enable = netc_timer_enable,
> };
>
> static void netc_timer_init(struct netc_timer *priv)
> @@ -429,6 +596,7 @@ static int netc_timer_parse_dt(struct netc_timer *priv)
> static irqreturn_t netc_timer_isr(int irq, void *data)
> {
> struct netc_timer *priv = data;
> + struct ptp_clock_event event;
> u32 tmr_event, tmr_emask;
> unsigned long flags;
>
> @@ -444,6 +612,11 @@ static irqreturn_t netc_timer_isr(int irq, void *data)
> if (tmr_event & TMR_TEVENT_ALM2EN)
> netc_timer_alarm_write(priv, NETC_TMR_DEFAULT_ALARM, 1);
>
> + if (tmr_event & TMR_TEVENT_PPEN_ALL) {
> + event.type = PTP_CLOCK_PPS;
> + ptp_clock_event(priv->clock, &event);
> + }
> +
> /* Clear interrupts status */
> netc_timer_wr(priv, NETC_TMR_TEVENT, tmr_event);
>
> @@ -506,6 +679,7 @@ 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_DEFAULT_PPS_CHANNEL;
> priv->phc_index = -1; /* initialize it as an invalid index */
> spin_lock_init(&priv->lock);
>
> --
> 2.34.1
>
next prev parent reply other threads:[~2025-07-16 20:06 UTC|newest]
Thread overview: 64+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-16 7:30 [PATCH v2 net-next 00/14] Add NETC Timer PTP driver and add PTP support for i.MX95 Wei Fang
2025-07-16 7:30 ` [PATCH v2 net-next 01/14] dt-bindings: ptp: add NETC Timer PTP clock Wei Fang
2025-07-16 19:19 ` Frank Li
2025-07-17 7:40 ` Krzysztof Kozlowski
2025-07-17 8:30 ` Wei Fang
2025-07-17 9:05 ` Vladimir Oltean
2025-07-17 9:55 ` Wei Fang
2025-07-17 12:42 ` Vladimir Oltean
2025-07-17 15:06 ` Frank Li
2025-07-22 14:36 ` Vladimir Oltean
2025-07-22 18:25 ` Frank Li
2025-07-17 10:04 ` Krzysztof Kozlowski
2025-07-17 10:28 ` Wei Fang
2025-07-16 7:30 ` [PATCH v2 net-next 02/14] dt-bindings: net: add nxp,netc-timer property Wei Fang
2025-07-16 19:28 ` Frank Li
2025-07-17 3:23 ` Wei Fang
2025-07-17 7:42 ` Krzysztof Kozlowski
2025-07-17 8:32 ` Wei Fang
2025-07-17 9:12 ` Krzysztof Kozlowski
2025-07-17 9:49 ` Wei Fang
2025-07-17 10:06 ` Krzysztof Kozlowski
2025-07-17 10:26 ` Wei Fang
2025-07-18 7:46 ` Krzysztof Kozlowski
2025-07-18 7:50 ` Krzysztof Kozlowski
2025-07-18 12:01 ` Vladimir Oltean
2025-07-21 6:00 ` Wei Fang
2025-07-21 12:23 ` Krzysztof Kozlowski
2025-07-16 7:31 ` [PATCH v2 net-next 03/14] ptp: netc: add NETC Timer PTP driver support Wei Fang
2025-07-16 19:58 ` Frank Li
2025-07-17 8:42 ` Wei Fang
2025-07-23 16:09 ` Vladimir Oltean
2025-07-24 2:36 ` Wei Fang
2025-07-16 7:31 ` [PATCH v2 net-next 04/14] ptp: netc: add PTP_CLK_REQ_PPS support Wei Fang
2025-07-16 20:05 ` Frank Li [this message]
2025-07-17 11:59 ` Wei Fang
2025-07-17 15:15 ` Frank Li
2025-07-18 2:08 ` Wei Fang
2025-07-16 7:31 ` [PATCH v2 net-next 05/14] ptp: netc: add periodic pulse output support Wei Fang
2025-07-16 20:26 ` Frank Li
2025-07-17 12:11 ` Wei Fang
2025-07-16 7:31 ` [PATCH v2 net-next 06/14] ptp: netc: add external trigger stamp support Wei Fang
2025-07-16 20:30 ` Frank Li
2025-07-16 7:31 ` [PATCH v2 net-next 07/14] ptp: netc: add debugfs support to loop back pulse signal Wei Fang
2025-07-16 20:32 ` Frank Li
2025-07-16 7:31 ` [PATCH v2 net-next 08/14] MAINTAINERS: add NETC Timer PTP clock driver section Wei Fang
2025-07-16 20:33 ` Frank Li
2025-07-16 7:31 ` [PATCH v2 net-next 09/14] net: enetc: save the parsed information of PTP packet to skb->cb Wei Fang
2025-07-16 20:46 ` Frank Li
2025-07-17 12:20 ` Wei Fang
2025-07-16 7:31 ` [PATCH v2 net-next 10/14] net: enetc: Add enetc_update_ptp_sync_msg() to process PTP sync packet Wei Fang
2025-07-16 20:49 ` Frank Li
2025-07-16 7:31 ` [PATCH v2 net-next 11/14] net: enetc: remove unnecessary CONFIG_FSL_ENETC_PTP_CLOCK check Wei Fang
2025-07-16 20:50 ` Frank Li
2025-07-16 7:31 ` [PATCH v2 net-next 12/14] net: enetc: add PTP synchronization support for ENETC v4 Wei Fang
2025-07-16 21:01 ` Frank Li
2025-07-17 12:35 ` Wei Fang
2025-07-17 22:07 ` Frank Li
2025-07-18 2:08 ` Wei Fang
2025-07-22 12:57 ` Vladimir Oltean
2025-07-22 13:41 ` Wei Fang
2025-07-16 7:31 ` [PATCH v2 net-next 13/14] net: enetc: don't update sync packet checksum if checksum offload is used Wei Fang
2025-07-16 21:03 ` Frank Li
2025-07-16 7:31 ` [PATCH v2 14/14] arm64: dts: imx95: Add NETC Timer support Wei Fang
2025-07-16 21:04 ` Frank Li
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=aHgGJ6sia5Xe7AA9@lizhi-Precision-Tower-5810 \
--to=frank.li@nxp.com \
--cc=andrew+netdev@lunn.ch \
--cc=claudiu.manoil@nxp.com \
--cc=conor+dt@kernel.org \
--cc=davem@davemloft.net \
--cc=devicetree@vger.kernel.org \
--cc=edumazet@google.com \
--cc=festevam@gmail.com \
--cc=fushi.peng@nxp.com \
--cc=imx@lists.linux.dev \
--cc=kernel@pengutronix.de \
--cc=krzk+dt@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=richardcochran@gmail.com \
--cc=robh@kernel.org \
--cc=s.hauer@pengutronix.de \
--cc=shawnguo@kernel.org \
--cc=vadim.fedorenko@linux.dev \
--cc=vladimir.oltean@nxp.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;
as well as URLs for NNTP newsgroup(s).