dpll: zl3073x: add PTP clock support
Subsystems (5)
Ivan Vecera
maintainerv6 cover dpll: zl3073x: add PTP clock support
Add PTP hardware clock support to the zl3073x DPLL driver.
Patch 1 scales the poll sleep interval in zl3073x_poll_zero_u8() proportionally to the timeout to avoid excessive bus traffic for the longer PTP-related timeouts.
Patch 2 adds low-level channel operations for ToD read/write/adjust, output phase step, delta frequency offset write and TIE write as building blocks for PTP callbacks.
Patch 3 registers a PTP clock device for each DPLL channel with gettimex64, settime64, adjtime, adjfine, adjphase and getmaxphase callbacks. Callback availability adapts to the current channel state - adjfine requires NCO pin connected, adjphase uses TIE write when tracking a reference, and adjtime selects the appropriate mechanism automatically. Periodic output support will be added in a follow-up series.
Changes:
v6:
- Fix kernel-doc warning: move ZL_TOD_MAX_RETRIES define inside the function body to avoid confusing the kernel-doc parser
v5:
- Add prep patch to scale poll interval with timeout
- Enumerate all ToD commands in tod_ready_wait switch, return -EOPNOTSUPP with WARN_ON in default case (Vadim)
- Handle CMD=0 (power-on default) in tod_ready_wait
- Rewrite tod_adjust rollover guard as a retry loop that re-reads until the margin is sufficient, replacing manual fixup and fsleep
- Fix "lowest-ID synth" comments to "first enabled synth"
- Include dev_name() in PTP clock name for multi-device systems
- Update kernel-doc for adjfine/adjphase to document zero no-op
v4:
- Move out_step_time_mask to struct zl3073x_dev (device-global)
- Fix tod_adjust rollover guard for tick-crossing case
- Drop trailing tod_ready_wait from tod_adjust
- Fix TOD_CTRL register item count
- Move phase step registers into existing page 9 section in regs.h
- Align ToD command define values
- Update kernel-doc for chan_state_fetch
- Drop perout support (will rework as follow-up)
- Replace abs(delta) with explicit range check to avoid S64_MIN UB
- Add locking context doc to zl3073x_chan_df_offset_set
- Export zl3073x_chan_tod_ready_wait and wait for ToD completion in adjtime before phase step/TIE write to prevent WR_NEXT_1HZ from overwriting the sub-second adjustment
v3:
- Use PTP_1588_CLOCK instead of PTP_1588_CLOCK_OPTIONAL (Jakub)
- Factor out output pin frequency helper to a separate commit (Jakub)
- Remove spurious #include <linux/mutex.h> from dpll.h (Jakub)
v2:
- Squash low-level ops patches (1+3) into patch 1.
- Squash PTP callback patches (2+4) into patch 2, register PTP clock regardless of initial channel state.
Ivan Vecera (3):
dpll: zl3073x: scale poll interval proportionally to timeout
dpll: zl3073x: add channel ToD, phase step and TIE operations
dpll: zl3073x: add PTP clock support
drivers/dpll/zl3073x/Kconfig | 6 +- drivers/dpll/zl3073x/chan.c | 322 +++++++++++++++++++++++++- drivers/dpll/zl3073x/chan.h | 32 +++ drivers/dpll/zl3073x/core.c | 17 +- drivers/dpll/zl3073x/core.h | 23 ++ drivers/dpll/zl3073x/dpll.c | 431 +++++++++++++++++++++++++++++++++-- drivers/dpll/zl3073x/dpll.h | 5 + drivers/dpll/zl3073x/regs.h | 52 +++++ 8 files changed, 856 insertions(+), 32 deletions(-)
base-commit: 4fa4977a0d900f936bcae5cd2c510be5554e8dd6
Signature · 1 line
2.54.0
Ivan Vecera
maintainerPATCH v6 1/3 dpll: zl3073x: scale poll interval proportionally to timeout
Replace the fixed 10 us poll sleep in zl3073x_poll_zero_u8() with timeout_us / 50. This scales the sleep interval proportionally to the timeout, targeting approximately 50 iterations regardless of the timeout magnitude.
The fixed 10 us interval was adequate for the existing 25-50 ms timeouts but wasteful for the longer PTP-related timeouts (up to 3000 ms for phase step) added in the following patches, where it would generate on the order of 10^5 bus transactions per wait.
--- drivers/dpll/zl3073x/core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/dpll/zl3073x/core.c +2 -2
@@ -322,7 +322,7 @@ int zl3073x_write_u48(struct zl3073x_dev *zldev, unsigned int reg, u64 val)int zl3073x_poll_zero_u8 (struct zl3073x_dev *zldev,unsigned int reg,u8 mask,unsigned int timeout_us) { -#define ZL_POLL_SLEEP_US 10 +unsigned int sleep_us = timeout_us /50 ;unsigned int val;/* Check the register is 8bit */
@@ -336,7 +336,7 @@ int zl3073x_poll_zero_u8(struct zl3073x_dev *zldev, unsigned int reg, reg =ZL_REG_ADDR (reg) +ZL_RANGE_OFFSET ;return regmap_read_poll_timeout (zldev->regmap, reg, val, !(val & mask), -ZL_POLL_SLEEP_US , timeout_us); + sleep_us, timeout_us); } int zl3073x_mb_op(struct zl3073x_dev *zldev, unsigned int op_reg, u8 op_val,
Signature · 1 line
2.54.0
Ivan Vecera
maintainerRe: [PATCH net-next v6 1/3] dpll: zl3073x: scale poll interval proportionally to timeout
Sashiko findings with comments:
Quoted 3 lines
Since
zl3073x_poll_zero_u8()is the single shared polling helper, does this also change the poll granularity for all the pre-existing callers [...]sleep_usbecomes 500 us, 1000 us, 1000 us, 600 us and 1000 us respectively
Yes, the change affects all callers. Testing showed that the existing callers typically completed in low hundreds of sleep cycles with the fixed 10 us interval. With the scaled interval, the cycle count dropped to single digits — a much better fit for the actual hardware response times.
Quoted 3 lines
Does the flash path also get slower? [...]
over the tens of thousands of words in a firmware or utility image could add a noticeable amount of time to a devlink flash.
No. Firmware flash operations were tested with no observable regression. The overall flash time remained the same because the reduced number of sleep cycles compensates for the longer individual sleep intervals.
Quoted 2 lines
Would clamping the derived interval [...] reach the stated goal [...] without changing the existing 25-50 ms call sites?
Given the test results above, clamping is not necessary.
Quoted 3 lines
Reading this, one would conclude the existing 25-50 ms call sites keep their old behaviour [...] Could the changelog mention the effect on the existing [...] polls?
Fair point. Will update the changelog to mention that all callers are affected.
Quoted 1 line
Is "approximately 50 iterations" accurate?
Will reword to avoid the specific iteration count claim.
Thanks,
Ivan
Ivan Vecera
maintainerRe: [PATCH net-next v6 1/3] dpll: zl3073x: scale poll interval proportionally to timeout
In reply to Ivan VeceraSashiko findings with comments:
Quoted 3 lines
Will this introduce a latency regression for fast bulk hardware operations? [...]
zl3073x_flash_download()loops over data [...] This multiplies the total firmware flashing time
Firmware flash operations were tested with no observable regression. Testing showed that the existing callers typically completed in low hundreds of sleep cycles with the fixed 10 us interval. With the scaled interval, the cycle count dropped to single digits — the reduced iteration count compensates for the longer per-iteration sleep.
Thanks,
Ivan
Ivan Vecera
maintainerPATCH v6 2/3 dpll: zl3073x: add channel ToD, phase step and TIE operations
In reply to Ivan VeceraAdd low-level DPLL channel operations for ToD read/write/adjust, output phase step, delta frequency offset write and TIE (Time Interval Error) write. These serve as building blocks for the PTP clock callbacks added in the next patch.
ToD operations use a wait-before-write pattern to avoid blocking after each operation.
The tod_ready_wait helper selects the poll timeout based on the current ToD command - write operations use a longer timeout (1000 ms) than reads (30 ms).
The ToD read captures system timestamps (ptp_system_timestamp) around the HW command and completion poll to support cross-timestamping.
The TIE write operation provides sub-picosecond resolution phase adjustment for modes where the DPLL is tracking a reference (AUTO and REFLOCK).
Add output step-time mask to struct zl3073x_dev and zl3073x_dev_out_is_stepped() helper to check if an output participates in step-time operations.
--- drivers/dpll/zl3073x/chan.c | 322 +++++++++++++++++++++++++++++++++++- drivers/dpll/zl3073x/chan.h | 32 ++++ drivers/dpll/zl3073x/core.c | 13 ++ drivers/dpll/zl3073x/core.h | 23 +++ drivers/dpll/zl3073x/regs.h | 52 ++++++ 5 files changed, 440 insertions(+), 2 deletions(-)
drivers/dpll/zl3073x/chan.c +320 -2
@@ -3,6 +3,7 @@#include <linux/cleanup.h> #include <linux/delay.h> #include <linux/dev_printk.h> +#include <linux/ptp_clock_kernel.h> #include <linux/string.h> #include <linux/types.h>
@@ -162,8 +163,8 @@ int zl3073x_chan_nco_mode_set(struct zl3073x_dev *zldev, u8 index)* @zldev: pointer to zl3073x_dev structure * @index: DPLL channel index to fetch state for * -* Reads the mode_refsel register and reference priority registers for -* the given DPLL channel and stores the raw values for later use. +* Reads the mode_refsel, status and reference priority registers for +* the given DPLL channel and stores the values for later use. * * Return: 0 on success, <0 on error */
@@ -234,6 +235,323 @@ const struct zl3073x_chan *zl3073x_chan_state_get(struct zl3073x_dev *zldev,return &zldev->chan[index]; } +/** +* zl3073x_chan_tod_ready_wait - wait for ToD semaphore to clear +* @zldev: pointer to zl3073x device +* @ch: DPLL channel index +* +* Polls the ToD control register until the semaphore bit is cleared, +* indicating the device has completed the previous ToD operation. +* +* Return: 0 on success, -EBUSY if semaphore not cleared, <0 on error +*/ +int zl3073x_chan_tod_ready_wait (struct zl3073x_dev *zldev,u8 ch) +{ +unsigned int timeout; +u8 tod_ctrl; +int rc; + + rc =zl3073x_read_u8 (zldev,ZL_REG_DPLL_TOD_CTRL (ch), &tod_ctrl); +if (rc) +return rc; + +switch (FIELD_GET (ZL_DPLL_TOD_CTRL_CMD , tod_ctrl)) { +case 0 : +/* No operation pending (power-on/reset default) */ +return 0 ; +case ZL_DPLL_TOD_CTRL_CMD_WR_NEXT_1HZ : + timeout =ZL_POLL_TOD_WR_TIMEOUT_US ; +break ; +case ZL_DPLL_TOD_CTRL_CMD_RD_CURRENT : +case ZL_DPLL_TOD_CTRL_CMD_RD_NEXT_1HZ : + timeout =ZL_POLL_TOD_RD_TIMEOUT_US ; +break ; +default : +/* FW never writes cmd bits; only the driver sets them */ +WARN_ON (1 ); +return -EOPNOTSUPP ; + } + + rc =zl3073x_poll_zero_u8 (zldev,ZL_REG_DPLL_TOD_CTRL (ch), +ZL_DPLL_TOD_CTRL_SEM , timeout); + +return rc == -ETIMEDOUT ? -EBUSY : rc; +} + +/** +* zl3073x_chan_tod_ctrl - issue ToD command +* @zldev: pointer to zl3073x device +* @ch: DPLL channel index +* @cmd: ToD command to execute +* +* Writes the semaphore and command to dpll_tod_ctrl. The caller must +* ensure the device is ready (semaphore clear) before calling and +* must wait for completion if needed. +* +* Return: 0 on success, <0 on error +*/ +static int zl3073x_chan_tod_ctrl (struct zl3073x_dev *zldev,u8 ch,u8 cmd) +{ +return zl3073x_write_u8 (zldev,ZL_REG_DPLL_TOD_CTRL (ch), +ZL_DPLL_TOD_CTRL_SEM | cmd); +} + +/** +* zl3073x_chan_tod_read - read ToD registers after issuing a command +* @zldev: pointer to zl3073x device +* @ch: DPLL channel index +* @next_hz: if true, read predicted ToD at next 1 Hz; otherwise read current +* @ts: timespec to store the result +* @sts: optional system timestamp pair for cross-timestamping +* +* Context: Caller must serialize all zl3073x_chan_tod_* calls externally. +* Return: 0 on success, <0 on error +*/ +int zl3073x_chan_tod_read (struct zl3073x_dev *zldev,u8 ch, +bool next_hz,struct timespec64 *ts, +struct ptp_system_timestamp *sts) +{ +u32 nsec; +u64 sec; +u8 cmd; +int rc; + +if (next_hz) + cmd =ZL_DPLL_TOD_CTRL_CMD_RD_NEXT_1HZ ; +else + cmd =ZL_DPLL_TOD_CTRL_CMD_RD_CURRENT ; + +/* Wait for any previous ToD operation to complete */ + rc =zl3073x_chan_tod_ready_wait (zldev, ch); +if (rc) +return rc; + +ptp_read_system_prets (sts); + rc =zl3073x_chan_tod_ctrl (zldev, ch, cmd); +if (rc) +return rc; + + rc =zl3073x_chan_tod_ready_wait (zldev, ch); +if (rc) +return rc; +ptp_read_system_postts (sts); + + rc =zl3073x_read_u48 (zldev,ZL_REG_DPLL_TOD_SEC (ch), &sec); +if (rc) +return rc; + +/* HW nanoseconds are always in [0, NSEC_PER_SEC) range */ + rc =zl3073x_read_u32 (zldev,ZL_REG_DPLL_TOD_NS (ch), &nsec); +if (rc) +return rc; + + ts->tv_sec = sec; + ts->tv_nsec = nsec; + +return 0 ; +} + +/** +* zl3073x_chan_tod_write - write ToD registers and trigger 1 Hz update +* @zldev: pointer to zl3073x device +* @ch: DPLL channel index +* @ts: time to set +* +* Context: Caller must serialize all zl3073x_chan_tod_* calls externally. +* Return: 0 on success, <0 on error +*/ +int zl3073x_chan_tod_write (struct zl3073x_dev *zldev,u8 ch, +struct timespec64 ts) +{ +int rc; + +/* Wait for any previous ToD operation to complete */ + rc =zl3073x_chan_tod_ready_wait (zldev, ch); +if (rc) +return rc; + + rc =zl3073x_write_u48 (zldev,ZL_REG_DPLL_TOD_SEC (ch), ts.tv_sec); +if (rc) +return rc; + + rc =zl3073x_write_u32 (zldev,ZL_REG_DPLL_TOD_NS (ch), ts.tv_nsec); +if (rc) +return rc; + +return zl3073x_chan_tod_ctrl (zldev, ch, +ZL_DPLL_TOD_CTRL_CMD_WR_NEXT_1HZ ); +} + +/** +* zl3073x_chan_tod_adjust - atomic ToD read-modify-write with rollover guard +* @zldev: pointer to zl3073x device +* @ch: DPLL channel index +* @delta: time adjustment to apply +* +* Reads the next-Hz ToD and current ToD, then checks whether enough time +* remains before the next 1 Hz rollover to safely complete the write. +* Re-reads if the 1 Hz tick crossed between the two reads or if less +* than 20 ms remains before the next rollover. Applies @delta and writes +* the result back. +* +* Context: Caller must serialize all zl3073x_chan_tod_* calls externally. +* Return: 0 on success, <0 on error +*/ +int zl3073x_chan_tod_adjust (struct zl3073x_dev *zldev,u8 ch, +struct timespec64 delta) +{ +#define ZL_TOD_MAX_RETRIES 20 +static const long threshold_ns =20 *NSEC_PER_MSEC ; +struct timespec64 ts_next, ts_cur, diff; +int rc, i; + +for (i =0 ; i <ZL_TOD_MAX_RETRIES ; i++) { + rc =zl3073x_chan_tod_read (zldev, ch, true, &ts_next,NULL ); +if (rc) +return rc; + + rc =zl3073x_chan_tod_read (zldev, ch, false, &ts_cur,NULL ); +if (rc) +return rc; + +/* Ensure the 1 Hz tick did not cross between the two reads +* and that enough margin remains to complete the write. +*/ + diff =timespec64_sub (ts_next, ts_cur); +if (diff.tv_sec >0 || + (!diff.tv_sec && diff.tv_nsec >= threshold_ns)) +break ; + } +if (i ==ZL_TOD_MAX_RETRIES ) { +dev_warn (zldev->dev, +"DPLL%u ToD adjust failed to get stable margin\n" , + ch); +return -EBUSY ; + } + +/* Apply delta to the next-Hz ToD */ + ts_next =timespec64_add (ts_next, delta); +if (!timespec64_valid (&ts_next)) +return -EINVAL ; + +return zl3073x_chan_tod_write (zldev, ch, ts_next); +#undef ZL_TOD_MAX_RETRIES +} + +/** +* zl3073x_chan_df_offset_set - write delta frequency offset to hardware +* @zldev: pointer to zl3073x device +* @ch: DPLL channel index +* @offset: frequency offset in 2^-48 steps +* +* Context: Caller must hold the per-DPLL lock. +* Return: 0 on success, <0 on error +*/ +int zl3073x_chan_df_offset_set (struct zl3073x_dev *zldev,u8 ch,s64 offset) +{ +int rc; + + rc =zl3073x_write_u48 (zldev,ZL_REG_DPLL_DF_OFFSET (ch), offset); +if (!rc) + zldev->chan[ch].df_offset = offset; + +return rc; +} + +/** +* zl3073x_chan_tie_write - adjust DPLL phase using TIE write +* @zldev: pointer to zl3073x device +* @ch: DPLL channel index +* @delta_ns: phase adjustment in nanoseconds (must be within +-1s) +* +* Converts nanoseconds to TIE units (0.01 ps) and writes TIE data +* to the specified channel. +* +* Return: 0 on success, <0 on error +*/ +int zl3073x_chan_tie_write (struct zl3073x_dev *zldev,u8 ch,s64 delta_ns) +{ +s64 tie_data; +int rc; + +if (WARN_ON (delta_ns <= -NSEC_PER_SEC || delta_ns >=NSEC_PER_SEC )) +return -ERANGE ; + +guard (mutex)(&zldev->tie_lock); + +/* Wait for any previous TIE operation to complete */ + rc =zl3073x_poll_zero_u8 (zldev,ZL_REG_DPLL_TIE_CTRL , +ZL_DPLL_TIE_CTRL_OP , +ZL_POLL_TIE_WR_TIMEOUT_US ); +if (rc) +return rc; + +/* Convert ns to TIE units (0.01 ps = 10^-14 s) */ + tie_data = delta_ns *100000LL ; + + rc =zl3073x_write_u48 (zldev,ZL_REG_DPLL_TIE_DATA (ch), tie_data); +if (rc) +return rc; + + rc =zl3073x_write_u8 (zldev,ZL_REG_DPLL_TIE_CTRL_MASK ,BIT (ch)); +if (rc) +return rc; + +return zl3073x_write_u8 (zldev,ZL_REG_DPLL_TIE_CTRL , +ZL_DPLL_TIE_CTRL_OP_WR ); +} + +/** +* zl3073x_chan_phase_step - execute one output phase step operation +* @zldev: pointer to zl3073x device +* @ch: DPLL channel index +* @out_mask: bitmask of outputs to step +* @step_cycles: phase step in synthesizer clock cycles +* @tod_step: also step the ToD counter +* +* All masked outputs must use synthesizers of the same frequency since +* the step value is in synthesizer clock cycles. +* +* Return: 0 on success, <0 on error +*/ +int zl3073x_chan_phase_step (struct zl3073x_dev *zldev,u8 ch, +u16 out_mask,s32 step_cycles, +bool tod_step) +{ +u8 ctrl; +int rc; + +guard (mutex)(&zldev->phase_step_lock); + +/* Wait for any previous phase step operation to complete */ + rc =zl3073x_poll_zero_u8 (zldev,ZL_REG_OUTPUT_PHASE_STEP_CTRL , +ZL_OUTPUT_PHASE_STEP_CTRL_OP , +ZL_POLL_PHASE_STEP_TIMEOUT_US ); +if (rc) +return rc; + + rc =zl3073x_write_u32 (zldev,ZL_REG_OUTPUT_PHASE_STEP_DATA , + step_cycles); +if (rc) +return rc; + + rc =zl3073x_write_u16 (zldev,ZL_REG_OUTPUT_PHASE_STEP_MASK , out_mask); +if (rc) +return rc; + + rc =zl3073x_write_u8 (zldev,ZL_REG_OUTPUT_PHASE_STEP_NUMBER ,1 ); +if (rc) +return rc; + + ctrl =FIELD_PREP (ZL_OUTPUT_PHASE_STEP_CTRL_DPLL , ch) | +FIELD_PREP (ZL_OUTPUT_PHASE_STEP_CTRL_OP , +ZL_OUTPUT_PHASE_STEP_CTRL_OP_WRITE ); +if (tod_step) + ctrl |=ZL_OUTPUT_PHASE_STEP_CTRL_TOD_STEP ; + +return zl3073x_write_u8 (zldev,ZL_REG_OUTPUT_PHASE_STEP_CTRL , ctrl); +} +/** * zl3073x_chan_state_set - commit DPLL channel state changes to hardware * @zldev: pointer to zl3073x_dev structure
drivers/dpll/zl3073x/chan.h +32 -0
@@ -5,10 +5,12 @@#include <linux/bitfield.h> #include <linux/stddef.h> +#include <linux/time64.h> #include <linux/types.h> #include "regs.h" +struct ptp_system_timestamp ;struct zl3073x_dev ;/**
@@ -42,6 +44,21 @@ int zl3073x_chan_state_set(struct zl3073x_dev *zldev, u8 index,int zl3073x_chan_state_update (struct zl3073x_dev *zldev,u8 index);int zl3073x_chan_nco_mode_set (struct zl3073x_dev *zldev,u8 index); +int zl3073x_chan_tod_ready_wait (struct zl3073x_dev *zldev,u8 ch); +int zl3073x_chan_tod_read (struct zl3073x_dev *zldev,u8 ch, +bool next_hz,struct timespec64 *ts, +struct ptp_system_timestamp *sts); +int zl3073x_chan_tod_write (struct zl3073x_dev *zldev,u8 ch, +struct timespec64 ts); +int zl3073x_chan_tod_adjust (struct zl3073x_dev *zldev,u8 ch, +struct timespec64 delta); +int zl3073x_chan_phase_step (struct zl3073x_dev *zldev,u8 ch, +u16 out_mask,s32 step_cycles,bool tod_step); + +int zl3073x_chan_df_offset_set (struct zl3073x_dev *zldev,u8 ch,s64 offset); + +int zl3073x_chan_tie_write (struct zl3073x_dev *zldev,u8 ch,s64 delta_ns); +/** * zl3073x_chan_df_offset_get - get cached df_offset vs tracked reference * @chan: pointer to channel state
@@ -200,6 +217,21 @@ static inline bool zl3073x_chan_mode_is_reflock(const struct zl3073x_chan *chan)return zl3073x_chan_mode_get (chan) ==ZL_DPLL_MODE_REFSEL_MODE_REFLOCK ; } +/** +* zl3073x_chan_mode_supports_tie - check if channel mode supports TIE write +* @chan: pointer to channel state +* +* TIE write is supported in AUTO and REFLOCK modes regardless of lock state. +* +* Return: true if TIE write is supported, false otherwise +*/ +static inline bool +zl3073x_chan_mode_supports_tie (const struct zl3073x_chan *chan) +{ +return zl3073x_chan_mode_is_auto (chan) || +zl3073x_chan_mode_is_reflock (chan); +} +/** * zl3073x_chan_is_ho_ready - check if holdover is ready * @chan: pointer to channel state
drivers/dpll/zl3073x/core.c +13 -0
@@ -511,6 +511,11 @@ zl3073x_dev_state_fetch(struct zl3073x_dev *zldev)int rc;u8 i; + rc =zl3073x_read_u16 (zldev,ZL_REG_OUTPUT_STEP_TIME_MASK , + &zldev->out_step_time_mask); +if (rc) +return rc; +for (i =0 ; i <ZL3073X_NUM_REFS ; i++) { rc =zl3073x_ref_state_fetch (zldev, i);if (rc) {
@@ -1034,6 +1039,14 @@ int zl3073x_dev_probe(struct zl3073x_dev *zldev)* and/or polls are required to be done atomically. */ rc =devm_mutex_init (zldev->dev, &zldev->multiop_lock); +if (rc) +return dev_err_probe (zldev->dev, rc, +"Failed to initialize mutex\n" ); + rc =devm_mutex_init (zldev->dev, &zldev->phase_step_lock); +if (rc) +return dev_err_probe (zldev->dev, rc, +"Failed to initialize mutex\n" ); + rc =devm_mutex_init (zldev->dev, &zldev->tie_lock);if (rc)return dev_err_probe (zldev->dev, rc,"Failed to initialize mutex\n" );
drivers/dpll/zl3073x/core.h +23 -0
@@ -26,6 +26,10 @@ struct zl3073x_dpll;#define ZL_POLL_HWREG_TIMEOUT_US (50 * USEC_PER_MSEC)#define ZL_POLL_MB_TIMEOUT_US (30 * USEC_PER_MSEC)#define ZL_POLL_PHASE_ERR_TIMEOUT_US (50 * USEC_PER_MSEC) +#define ZL_POLL_PHASE_STEP_TIMEOUT_US (3000 * USEC_PER_MSEC) +#define ZL_POLL_TIE_WR_TIMEOUT_US (1000 * USEC_PER_MSEC) +#define ZL_POLL_TOD_RD_TIMEOUT_US (30 * USEC_PER_MSEC) +#define ZL_POLL_TOD_WR_TIMEOUT_US (1000 * USEC_PER_MSEC)enum zl3073x_flags {ZL3073X_FLAG_REF_PHASE_COMP_32_BIT ,
@@ -55,6 +59,8 @@ struct zl3073x_chip_info {* @regmap: regmap to access device registers * @info: detected chip info * @multiop_lock: to serialize multiple register operations +* @tie_lock: to serialize TIE write operations +* @phase_step_lock: to serialize output phase step operations * @ref: array of input references' invariants * @out: array of outs' invariants * @synth: array of synths' invariants
@@ -63,6 +69,7 @@ struct zl3073x_chip_info {* @kworker: thread for periodic work * @work: periodic work * @clock_id: clock id of the device +* @out_step_time_mask: output step-time mask (device-global) * @phase_avg_factor: phase offset measurement averaging factor * @freq_monitor: is frequency monitor enabled */
@@ -71,6 +78,8 @@ struct zl3073x_dev {struct regmap *regmap;const struct zl3073x_chip_info *info;struct mutex multiop_lock; +struct mutex tie_lock; +struct mutex phase_step_lock;/* Invariants */ struct zl3073x_ref ref[ZL3073X_NUM_REFS ];
@@ -87,6 +96,7 @@ struct zl3073x_dev {/* Per-chip parameters */ u64 clock_id; +u16 out_step_time_mask;u8 phase_avg_factor;bool freq_monitor; };
@@ -308,6 +318,19 @@ zl3073x_dev_out_is_enabled(struct zl3073x_dev *zldev, u8 index)return zl3073x_synth_is_enabled (synth) &&zl3073x_out_is_enabled (out); } +/** +* zl3073x_dev_out_is_stepped - check if output is in step-time mask +* @zldev: pointer to zl3073x device +* @index: output index +* +* Return: true if output is affected by step-time operations +*/ +static inline bool +zl3073x_dev_out_is_stepped (struct zl3073x_dev *zldev,u8 index) +{ +return !!(zldev->out_step_time_mask &BIT (index)); +} +/** * zl3073x_dev_out_dpll_get - get DPLL ID the output is driven by * @zldev: pointer to zl3073x device
drivers/dpll/zl3073x/regs.h +52 -0
76 lines not on this page · raw message
Signature · 1 line
2.54.0
Ivan Vecera
maintainerRe: [PATCH net-next v6 2/3] dpll: zl3073x: add channel ToD, phase step and TIE operations
Sashiko findings with replies:
Quoted 2 lines
Should the kernel-doc match what the function actually does here? [case 0 returns without testing SEM]
Will restructure tod_ready_wait to check the SEM bit first. If SEM is clear, no operation is pending and we can return 0 immediately regardless of the CMD field value. This also handles the unknown-CMD case from a prior boot stage gracefully — if the semaphore is clear, whatever CMD bits remain are stale and irrelevant.
Quoted 1 line
The documented return list [...] does not mention
-EOPNOTSUPP.
Will update the return documentation.
Quoted 2 lines
Can this loop exhaust its 20 retries on healthy hardware and return
-EBUSY?
Testing shows that a single iteration of the loop body (two ToD reads) takes approximately 17-19 ms. With 20 retries that gives a budget of 340-380 ms, which is more than enough to outlast the 20 ms margin window near a 1 Hz edge. After the edge crosses, the next read returns ~980 ms of margin and the loop breaks immediately.
Quoted 3 lines
How is the measured margin guaranteed to still hold when the
WR_NEXT_1HZcommand finally reaches the device? [...] Would a post-write confirmation [...] be worth adding?
The 20 ms threshold provides sufficient margin for the write sequence (ready wait + three register writes + command). A post-write confirmation would add complexity for a scenario that requires ~20 ms of preemption under a mutex, which is unreachable in practice.
Quoted 2 lines
The kernel-doc says
delta_ns"must be within +-1s", which reads as an inclusive bound, while the guard accepts only the open interval
Will reword to clarify the exclusive bound.
Quoted 2 lines
The ToD ready wait logic fails permanently if the hardware control register inherits an unknown command from a prior boot stage.
Addressed by the SEM-first restructuring described above. If SEM is clear, the function returns 0 regardless of the CMD bits.
Thanks,
Ivan
Ivan Vecera
maintainerRe: [PATCH net-next v6 2/3] dpll: zl3073x: add channel ToD, phase step and TIE operations
In reply to Ivan VeceraSashiko findings and replies:
Quoted 4 lines
Does capturing the post-timestamp here degrade cross-timestamping precision? [...]
Should the post-timestamp be captured immediately afterzl3073x_chan_tod_ctrl()issues the command?
The post-timestamp must be taken after the semaphore clears, not after the command write. The hardware latches the ToD value as part of processing the command, which completes when the semaphore is cleared. Taking the post-timestamp before the wait would risk the window not containing the actual latch event, which would be worse than a wider but correct window.
Quoted 1 line
Could this retry loop exhaust its budget and fail spuriously [...]?
Testing shows that a single iteration of the loop body (two ToD reads) takes approximately 17-19 ms. With 20 retries that gives a budget of 340-380 ms, which is more than enough to outlast the 20 ms margin window. After the 1 Hz edge crosses, the next read returns ~980 ms of margin and the loop breaks.
Quoted 1 line
Is it possible for preemption to cause a 1-second clock shift here?
The 20 ms threshold provides sufficient margin for the write sequence. This requires ~20 ms of preemption under a held mutex to cause an issue, which is not reachable in practice.
Thanks,
Ivan
Ivan Vecera
maintainerPATCH v6 3/3 dpll: zl3073x: add PTP clock support
In reply to Ivan VeceraAdd PTP clock support for the ZL3073x DPLL driver. A PTP clock device is registered for each DPLL channel regardless of the initial channel state, providing gettimex64, settime64, adjtime, adjfine, adjphase and getmaxphase callbacks.
Callback availability depends on the current channel state:
- adjfine: when NCO pin is connected (returns -EOPNOTSUPP otherwise)
- adjphase: available when tracking a reference, uses TIE write
- adjtime: always available and uses
* phase step for sub-second deltas when NCO pin is connected
* TIE write when tracking a reference
* plain ToD read-modify-write otherwise
- gettime/settime: always available
The adjtime callback splits multi-second adjustments into a ToD read-modify-write for the seconds part and a sub-second mechanism (phase step or TIE write) for the remainder. On partial failure where seconds were already committed, success is returned to prevent the PTP servo from retrying and applying seconds again.
All PTP callbacks are serialized by the existing per-DPLL zldpll->lock mutex, which is also used by DPLL pin and device callbacks.
--- drivers/dpll/zl3073x/Kconfig | 6 +- drivers/dpll/zl3073x/dpll.c | 431 +++++++++++++++++++++++++++++++++-- drivers/dpll/zl3073x/dpll.h | 5 + 3 files changed, 414 insertions(+), 28 deletions(-)
drivers/dpll/zl3073x/Kconfig +3 -3
@@ -2,7 +2,7 @@ config ZL3073X tristate "Microchip Azurite DPLL/PTP/SyncE devices" if COMPILE_TEST - depends on NET + depends on NET && PTP_1588_CLOCK select DPLL select NET_DEVLINK select REGMAP
@@ -16,7 +16,7 @@ config ZL3073X config ZL3073X_I2C tristate "I2C bus implementation for Microchip Azurite devices" - depends on I2C && NET + depends on I2C && NET && PTP_1588_CLOCK select REGMAP_I2C select ZL3073X help
@@ -28,7 +28,7 @@ config ZL3073X_I2C config ZL3073X_SPI tristate "SPI bus implementation for Microchip Azurite devices" - depends on NET && SPI + depends on NET && SPI && PTP_1588_CLOCK select REGMAP_SPI select ZL3073X help
drivers/dpll/zl3073x/dpll.c +406 -25
482 lines not on this page · raw message
drivers/dpll/zl3073x/dpll.h +5 -0
@@ -5,6 +5,7 @@#include <linux/dpll.h> #include <linux/list.h> +#include <linux/ptp_clock_kernel.h> #include "core.h"
@@ -22,6 +23,8 @@* @type: DPLL type (PPS or EEC) * @lock_status: last saved DPLL lock status * @pins: list of pins +* @ptp_info: PTP clock info +* @ptp_clock: registered PTP clock (or NULL) */ struct zl3073x_dpll {struct list_head list;
@@ -36,6 +39,8 @@ struct zl3073x_dpll {enum dpll_type type;enum dpll_lock_status lock_status;struct list_head pins; +struct ptp_clock_info ptp_info; +struct ptp_clock *ptp_clock; };struct zl3073x_dpll *zl3073x_dpll_alloc (struct zl3073x_dev *zldev,u8 ch);
Signature · 1 line
2.54.0
Ivan Vecera
maintainerSashiko findings and my replies:
Quoted 1 line
Is the hard dependency on
PTP_1588_CLOCKintended here?
Yes. This was explicitly requested by Jakub in the v2 review [1].
[1] https://lore.kernel.org/netdev/[email protected]/
Quoted 3 lines
Does the requested timestamp need compensation for the 1 Hz latch here? [...]
gettimex64()keeps returning the old time until that edge
gettimex64() does not return the old value. It calls zl3073x_chan_tod_read() which starts with zl3073x_chan_tod_ready_wait(). If a WR_NEXT_1HZ from a previous settime64() is still pending, the wait blocks until the 1 Hz edge applies it, and the subsequent read returns the new value. So the written timestamp is applied accurately at the edge with no drift to compensate for.
Quoted 2 lines
What state is the hardware left in on this path? [partial phase step failure returns 0]
This is intentional. At this point the seconds and the first synth group (with ToD) have been committed. Propagating the error would cause the PTP servo to retry the full delta, applying seconds a second time — a much worse outcome than a sub-second skew on some outputs. A regmap/bus error at this stage indicates a serious hardware problem that dev_warn() surfaces appropriately.
Quoted 5 lines
zl3073x_dpll_ptp_adjtime()documents "Return: 0 on success, <0 on error", yet it returns 0 with rc != 0 when the seconds were already committed [...]
Could these blocks spell out that 0 does not imply the requested adjustment was fully applied?
Good point. Will update the kernel-doc to document the partial success semantics.
Quoted 6 lines
Should
sec_adjustedbe set right after the successfulzl3073x_chan_tod_adjust()rather than after the wait?Once
zl3073x_chan_tod_adjust()returns 0, theWR_NEXT_1HZcommand has been accepted and the firmware will latch the seconds at the next 1 Hz edge; the driver cannot cancel it.
If tod_ready_wait fails, we cannot be certain that WR_NEXT_1HZ was actually applied — the semaphore timeout means we do not know whether the hardware committed the seconds or not. Returning the error and letting the caller handle the retry is the safer choice.
Quoted 6 lines
Does the template need to be copied here [in alloc], or in
zl3073x_dpll_ptp_register()just beforeptp_clock_register()?
ptp_clock_register()writes into the driver-owned structptp_clock_info[...] On the second registration the leftover getcycles64/getcyclesx64 pointers [...]
Good catch. Will move the template assignment from zl3073x_dpll_alloc() to zl3073x_dpll_ptp_register() so that ptp_info is always fresh before each registration.
Quoted 2 lines
Sub-second PTP adjustments can be overwritten by pending hardware ToD writes from previous API calls.
The PTP callbacks are serialized by zldpll->lock. A pending WR_NEXT_1HZ from a previous settime64 or adjtime call is consumed at the next 1 Hz edge. Any subsequent ToD operation goes through tod_ready_wait which waits for the pending write to complete. For phase step and TIE write: these operate on independent hardware blocks and do not conflict with a pending ToD write.
Thanks,
Ivan
Ivan Vecera
maintainerSashiko findings and my replies:
Quoted 4 lines
By returning rc directly here, does this bypass the
sec_adjustedcheck at the end of the function? [...]
Should this path handle the timeout failure similarly to the fallback path?
If tod_ready_wait fails, we cannot be certain that WR_NEXT_1HZ was actually applied. Returning the error and letting the caller handle the retry is the safer choice, because masking the error would silently report success when the adjustment may not have been applied at all.
Quoted 5 lines
If a previous API call (like
zl3073x_dpll_ptp_settime64) issued a ToD write and returned without waiting for the 1 Hz edge, could a subsequent sub-second adjustment clobber it? [...]
Does the driver need to wait for pending ToD writes before issuing sub-second adjustments regardless of the delta size?
All PTP callbacks are serialized by zldpll->lock. A subsequent sub-second adjtime goes through zl3073x_chan_tod_adjust() which calls zl3073x_chan_tod_read() which starts with zl3073x_chan_tod_ready_wait() — this waits for any pending WR_NEXT_1HZ to complete. Phase step and TIE write operate on independent hardware blocks and do not conflict with pending ToD writes.
Quoted 3 lines
Similarly,
zl3073x_dpll_ptp_adjphase()issues a TIE write without checking for pending ToD writes. Could this phase adjustment also be overwritten?
TIE write adjusts the DPLL input-to-output phase through a separate hardware block. It does not modify the ToD counter and is not affected by a pending WR_NEXT_1HZ.
Thanks,
Ivan