dpll: zl3073x: add PTP clock support
Subsystems (4)
Ivan Vecera
maintainercover dpll: zl3073x: add PTP clock support
Add PTP hardware clock support to the zl3073x DPLL driver.
Patch 1 adds channel ToD read/write, phase step and delta frequency offset operations as low-level building blocks for PTP callbacks.
Patch 2 registers a PTP clock device for each DPLL channel with gettimex64, settime64, adjtime, adjfine and perout callbacks. The adjtime callback splits multi-second adjustments via ToD read-modify-write and sub-second remainders via output phase step.
Patch 3 adds a channel TIE (Time Interval Error) write operation for sub-picosecond resolution phase adjustment.
Patch 4 adds PTP adjphase and getmaxphase callbacks using TIE write for phase adjustment in AUTO and REFLOCK modes, extends adjtime to handle non-NCO modes via TIE write, and removes the NCO-only gate from PTP clock registration.
Ivan Vecera (4):
dpll: zl3073x: add channel ToD and phase step operations
dpll: zl3073x: add PTP clock support
dpll: zl3073x: add channel TIE write operation
dpll: zl3073x: add PTP clock adjphase and TIE support
drivers/dpll/zl3073x/Kconfig | 6 +- drivers/dpll/zl3073x/chan.c | 307 +++++++++++++++++++ drivers/dpll/zl3073x/chan.h | 48 +++ drivers/dpll/zl3073x/core.c | 8 + drivers/dpll/zl3073x/core.h | 8 + drivers/dpll/zl3073x/dpll.c | 575 +++++++++++++++++++++++++++++++++-- drivers/dpll/zl3073x/dpll.h | 6 + drivers/dpll/zl3073x/regs.h | 56 ++++ 8 files changed, 978 insertions(+), 36 deletions(-)
base-commit: 4a13f31a92f35161b499bf29638336885259da78
Signature · 1 line
2.53.0
Ivan Vecera
maintainerPATCH 1/4 dpll: zl3073x: add channel ToD and phase step operations
Add low-level DPLL channel operations for ToD read/write/adjust, delta frequency offset write and output phase step. ToD operations use a wait-before-write pattern to avoid blocking after each operation. tod_adjust additionally waits for completion since callers may follow with phase step operations.
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.
Add output step-time mask invariant to zl3073x_chan and zl3073x_chan_is_out_stepped() helper to check if an output participates in step-time operations.
--- drivers/dpll/zl3073x/chan.c | 267 ++++++++++++++++++++++++++++++++++++ drivers/dpll/zl3073x/chan.h | 31 +++++ drivers/dpll/zl3073x/core.c | 4 + drivers/dpll/zl3073x/core.h | 5 + drivers/dpll/zl3073x/regs.h | 44 ++++++ 5 files changed, 351 insertions(+)
drivers/dpll/zl3073x/chan.c +267 -0
@@ -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>
@@ -202,6 +203,11 @@ int zl3073x_chan_state_fetch(struct zl3073x_dev *zldev, u8 index)zl3073x_chan_refsel_state_get (chan), zl3073x_chan_refsel_ref_get(chan)); + rc =zl3073x_read_u16 (zldev,ZL_REG_OUTPUT_STEP_TIME_MASK , + &chan->out_step_time_mask); +if (rc) +return rc; +guard (mutex)(&zldev->multiop_lock);/* Read DPLL configuration from mailbox */
@@ -234,6 +240,267 @@ 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 +*/ +static 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 ZL_DPLL_TOD_CTRL_CMD_WR_NEXT_1HZ : + timeout =ZL_POLL_TOD_WR_TIMEOUT_US ; +break ; +default : + timeout =ZL_POLL_TOD_RD_TIMEOUT_US ; +break ; + } + + 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. +* If less than 20 ms remains, waits for the rollover and increments the +* next-Hz seconds by one. 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) +{ +static const long threshold_ns =20 *NSEC_PER_MSEC ; +struct timespec64 ts_next, ts_cur, diff; +int rc; + +/* Read predicted ToD at next 1 Hz tick */ + rc =zl3073x_chan_tod_read (zldev, ch, true, &ts_next,NULL ); +if (rc) +return rc; + +/* Read current ToD to determine remaining margin */ + rc =zl3073x_chan_tod_read (zldev, ch, false, &ts_cur,NULL ); +if (rc) +return rc; + +/* If too close to (or past) the next rollover, wait it out */ + diff =timespec64_sub (ts_next, ts_cur); +if (diff.tv_sec <0 || (!diff.tv_sec && diff.tv_nsec < threshold_ns)) { +if (!diff.tv_sec && diff.tv_nsec) +fsleep ((unsigned long )diff.tv_nsec /NSEC_PER_USEC +1 ); + ts_next.tv_sec++; + } + +/* Apply delta to the next-Hz ToD */ + ts_next =timespec64_add (ts_next, delta); +if (!timespec64_valid (&ts_next)) +return -EINVAL ; + +/* Write adjusted ToD back and wait for completion */ + rc =zl3073x_chan_tod_write (zldev, ch, ts_next); +if (rc) +return rc; + +return zl3073x_chan_tod_ready_wait (zldev, ch); +} + +/** +* 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 +* +* 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_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 +31 -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 ;/**
@@ -16,6 +18,7 @@ struct zl3073x_dev;* @ctrl: DPLL control register value * @mode_refsel: mode and reference selection register value * @ref_prio: reference priority registers (4 bits per ref, P/N packed) +* @out_step_time_mask: output step-time mask * @mon_status: monitor status register value * @refsel_status: reference selection status register value * @df_offset: frequency offset vs tracked reference in 2^-48 steps
@@ -26,6 +29,9 @@ struct zl3073x_chan {u8 mode_refsel;u8 ref_prio[ZL3073X_NUM_REFS /2 ]; ); +struct_group (inv,/* Invariants */ + u16 out_step_time_mask; + ); struct_group(stat, u8 mon_status; u8 refsel_status;
@@ -42,6 +48,18 @@ 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_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); +/** * zl3073x_chan_df_offset_get - get cached df_offset vs tracked reference * @chan: pointer to channel state
@@ -233,4 +251,17 @@ static inline u8 zl3073x_chan_refsel_ref_get(const struct zl3073x_chan *chan) return FIELD_GET(ZL_DPLL_REFSEL_STATUS_REFSEL, chan->refsel_status); } +/** +* zl3073x_chan_is_out_stepped - check if output is in step-time mask +* @chan: pointer to channel state +* @out: output index +* +* Return: true if output is affected by step-time operations +*/ +static inline bool +zl3073x_chan_is_out_stepped (const struct zl3073x_chan *chan,u8 out) +{ +return !!(chan->out_step_time_mask &BIT (out)); +} +#endif /* _ZL3073X_CHAN_H */
drivers/dpll/zl3073x/core.c +4 -0
@@ -1033,6 +1033,10 @@ 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" );
drivers/dpll/zl3073x/core.h +5 -0
@@ -26,6 +26,9 @@ 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_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 +58,7 @@ struct zl3073x_chip_info {* @regmap: regmap to access device registers * @info: detected chip info * @multiop_lock: to serialize multiple register 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
@@ -71,6 +75,7 @@ struct zl3073x_dev {struct regmap *regmap;const struct zl3073x_chip_info *info;struct mutex multiop_lock; +struct mutex phase_step_lock;/* Invariants */ struct zl3073x_ref ref[ZL3073X_NUM_REFS ];
drivers/dpll/zl3073x/regs.h +44 -0
74 lines not on this page · raw message
Signature · 1 line
2.53.0
Vadim Fedorenko
maintainerRe: [PATCH net-next 1/4] dpll: zl3073x: add channel ToD and phase step operations
Quoted 17 lines · Ivan Vecera
Add low-level DPLL channel operations for ToD read/write/adjust, delta frequency offset write and output phase step. ToD operations use a wait-before-write pattern to avoid blocking after each operation.
tod_adjustadditionally waits for completion since callers may follow with phase step operations.The
tod_ready_waithelper 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.Add output step-time mask invariant to
zl3073x_chanandzl3073x_chan_is_out_stepped()helper to check if an output participates in step-time operations.
[...]
Quoted 29 lines
drivers/dpll/zl3073x/chan.c +29
-0+/** +* 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;
drivers/dpll/zl3073x/chan.c const struct zl3073x_chan *zl3073x_chan_state_get(struct zl3073x_dev *zldev, · line 334
Quoted 10 lines
drivers/dpll/zl3073x/chan.c +10
-0+ +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);
AFAIU, this code means that the ToD value was somewhere between tod_ctrl command and tod_ready read value 0 of the register. How does it work with "predicted ToD at next 1 Hz"?
Quoted 15 lines
drivers/dpll/zl3073x/chan.c +15
-0+ + 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 ; +}
[...]
Ivan Vecera
maintainerRe: [PATCH net-next 1/4] dpll: zl3073x: add channel ToD and phase step operations
Quoted 58 lines · Vadim Fedorenko
Quoted 17 lines · Ivan Vecera
Add low-level DPLL channel operations for ToD read/write/adjust, delta frequency offset write and output phase step. ToD operations use a wait-before-write pattern to avoid blocking after each operation.
tod_adjustadditionally waits for completion since callers may follow with phase step operations.The
tod_ready_waithelper 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.Add output step-time mask invariant to
zl3073x_chanandzl3073x_chan_is_out_stepped()helper to check if an output participates in step-time operations.[...]
Quoted 37 lines
+/**
+ *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
+ */drivers/dpll/zl3073x/chan.c +24
-0+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; +
Quoted 8 lines
Quoted 4 lines
+ rc =
zl3073x_chan_tod_ready_wait(zldev, ch);
+ if (rc)
+ return rc;
+ptp_read_system_postts(sts);AFAIU, this code means that the ToD value was somewhere between
tod_ctrlcommand andtod_readyread value 0 of the register. How does it work with "predicted ToD at next 1 Hz"?
The ptp_system_timestamp is only used by the gettimex64 callback which always calls tod_read with next_hz=false (current ToD). The next_hz=true path is only used internally by tod_adjust() which passes sts=NULL.
So in practice, system timestamps are never captured around a predicted ToD read. The function accepts both options through the same interface for simplicity, but the sts parameter is only meaningful with next_hz=false.
Thanks,
Ivan
Ivan Vecera
maintainerPATCH 2/4 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 that is in NCO mode, providing gettimex64, settime64, adjtime, adjfine and perout callbacks.
The adjtime callback handles multi-second adjustments via ToD read-modify-write at 1 Hz boundaries, with sub-second remainders handled through the output phase step mechanism. If phase step is unavailable, the full adjustment falls back to ToD read-modify-write.
Output pins eligible for periodic output (single-ended, marked in output_step_time_mask, supporting 1 Hz) are registered as PTP perout channels. Enabling perout sets the output frequency to 1 Hz via the existing DPLL frequency_set path.
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 | 521 ++++++++++++++++++++++++++++++++--- drivers/dpll/zl3073x/dpll.h | 6 + 3 files changed, 497 insertions(+), 36 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_OPTIONAL 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_OPTIONAL 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_OPTIONAL select REGMAP_SPI select ZL3073X help
drivers/dpll/zl3073x/dpll.c +488 -33
627 lines not on this page · raw message
drivers/dpll/zl3073x/dpll.h +6 -0
@@ -5,6 +5,8 @@#include <linux/dpll.h> #include <linux/list.h> +#include <linux/mutex.h> +#include <linux/ptp_clock_kernel.h> #include "core.h"
@@ -22,6 +24,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 +40,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.53.0
Vadim Fedorenko
maintainerQuoted 3 lines · Ivan Vecera
Add PTP clock support for the ZL3073x DPLL driver. A PTP clock device is registered for each DPLL channel that is in NCO mode, providing gettimex64, settime64, adjtime, adjfine and perout callbacks.
Here we come back to the discussion of NCO mode. Is DPLL channel actually a pin (virtual or physical)? If it's not, then it means that channel is exposed as independent DPLL device, that shouldn't affect other devices, but AFAIU, it can in the current implementation. If it is a pin, why do you check device's mode rather then pins?
And another question - what happens when channel changes its mode to non-NCO? Do we have ptp device disappearing from the system?
Ivan Vecera
maintainerQuoted 5 lines · Vadim Fedorenko
Quoted 3 lines · Ivan Vecera
Add PTP clock support for the ZL3073x DPLL driver. A PTP clock device is registered for each DPLL channel that is in NCO mode, providing gettimex64, settime64, adjtime, adjfine and perout callbacks.
Quoted 8 lines
Here we come back to the discussion of NCO mode. Is DPLL channel actually a pin (virtual or physical)? If it's not, then it means that channel is exposed as independent DPLL device, that shouldn't affect other devices, but AFAIU, it can in the current implementation. If it is a pin, why do you check device's mode rather then pins?
And another question - what happens when channel changes its mode to non-NCO? Do we have ptp device disappearing from the system?
Hi Vadim,
I understand your point. The PTP code was originally developed before the NCO pin switching was implemented, when it made sense to split PTP support into two patches - basic PTP for NCO-only channels (patch 2) and then extending it to all modes via TIE write (patch 4).
Now that NCO mode can be entered and left dynamically via the NCO pin, this split no longer makes sense. Having patch 2 register PTP only for NCO-mode channels creates an awkward intermediate state where a dynamic NCO switch would require dynamic PTP clock registration/unregistration.
I'll squash patches 1&3 and 2&4 in v2, so the PTP clock is always registered regardless of the initial channel mode, with per-callback mode checks.
Thanks,
Ivan
Ivan Vecera
maintainerPATCH 3/4 dpll: zl3073x: add channel TIE write operation
In reply to Ivan VeceraAdd TIE phase adjustment operation zl3073x_chan_tie_write() that converts nanoseconds to TIE units (0.01 ps) and writes TIE data to the specified DPLL channel. A tie_lock mutex serializes TIE operations.
Add convenience helper zl3073x_chan_mode_supports_tie() to chan.h.
--- drivers/dpll/zl3073x/chan.c | 40 +++++++++++++++++++++++++++++++++++++ drivers/dpll/zl3073x/chan.h | 17 ++++++++++++++++ drivers/dpll/zl3073x/core.c | 4 ++++ drivers/dpll/zl3073x/core.h | 3 +++ drivers/dpll/zl3073x/regs.h | 12 +++++++++++ 5 files changed, 76 insertions(+)
drivers/dpll/zl3073x/chan.c +40 -0
@@ -450,6 +450,46 @@ int zl3073x_chan_df_offset_set(struct zl3073x_dev *zldev, u8 ch, s64 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; + +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
drivers/dpll/zl3073x/chan.h +17 -0
@@ -60,6 +60,8 @@ int zl3073x_chan_phase_step(struct zl3073x_dev *zldev, u8 ch,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
@@ -218,6 +220,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 +4 -0
@@ -1037,6 +1037,10 @@ int zl3073x_dev_probe(struct zl3073x_dev *zldev)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 +3 -0
@@ -27,6 +27,7 @@ struct zl3073x_dpll;#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)
@@ -58,6 +59,7 @@ 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
@@ -75,6 +77,7 @@ 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 */
drivers/dpll/zl3073x/regs.h +12 -0
@@ -179,6 +179,12 @@#define ZL_DPLL_DF_READ_CMD GENMASK(2, 0)#define ZL_DPLL_DF_READ_CMD_ACC_I 4 +#define ZL_REG_DPLL_TIE_CTRL ZL_REG(5, 0x30, 1) +#define ZL_DPLL_TIE_CTRL_OP GENMASK(2, 0) +#define ZL_DPLL_TIE_CTRL_OP_WR 4 + +#define ZL_REG_DPLL_TIE_CTRL_MASK ZL_REG(5, 0x31, 1) +#define ZL_REG_DPLL_TOD_CTRL (_idx) \ ZL_REG_IDX(_idx, 5, 0x38, 1, 8, 1)#define ZL_DPLL_TOD_CTRL_SEM BIT(4)
@@ -213,6 +219,12 @@ ((_idx) < 4 ? ZL_REG_DPLL_DF_OFFSET_03(_idx) : ZL_REG_DPLL_DF_OFFSET_4)#define ZL_DPLL_DF_OFFSET_UNKNOWN S64_MIN +#define ZL_REG_DPLL_TIE_DATA_03 (_idx) \ + ZL_REG_IDX(_idx, 6, 0x0C, 6, 4, 0x20) +#define ZL_REG_DPLL_TIE_DATA_4 ZL_REG(7, 0x0C, 6) +#define ZL_REG_DPLL_TIE_DATA (_idx) \ + ((_idx) < 4 ? ZL_REG_DPLL_TIE_DATA_03(_idx) : ZL_REG_DPLL_TIE_DATA_4) +#define ZL_REG_DPLL_TOD_SEC_03 (_idx) \ ZL_REG_IDX(_idx, 6, 0x12, 6, 4, 0x20)#define ZL_REG_DPLL_TOD_SEC_4 ZL_REG(7, 0x12, 6)
Signature · 1 line
2.53.0
Ivan Vecera
maintainerPATCH 4/4 dpll: zl3073x: add PTP clock adjphase and TIE support
In reply to Ivan VeceraAdd adjphase and getmaxphase PTP callbacks using TIE write for sub-picosecond phase adjustment in AUTO and REFLOCK modes.
Extend the adjtime callback to handle non-NCO modes: in AUTO/REFLOCK modes the sub-second remainder is applied via TIE write instead of phase step. In modes that support neither, the full adjustment falls back to ToD read-modify-write.
Remove the NCO-only gate from PTP clock registration since the PTP clock now provides useful operations in all modes. Add a mode check to adjfine so it returns -EOPNOTSUPP when not in NCO mode.
--- drivers/dpll/zl3073x/dpll.c | 102 +++++++++++++++++++++++++++--------- 1 file changed, 78 insertions(+), 24 deletions(-)
drivers/dpll/zl3073x/dpll.c +78 -24
@@ -2489,10 +2489,13 @@ static int zl3073x_dpll_ptp_adjtime_phase_step(struct zl3073x_dpll *zldpll,* @info: PTP clock info * @delta: time adjustment in nanoseconds * -* For large deltas (>= 1 second), the seconds part is adjusted via ToD -* read-modify-write at 1 Hz boundaries and the sub-second remainder via -* output phase step. For sub-second deltas, uses phase step directly. -* Falls back to full ToD read-modify-write if phase step is unavailable. +* In NCO mode, large deltas (>= 1 second) are split into a ToD +* read-modify-write for the seconds part and an output phase step for +* the sub-second remainder. Sub-second deltas use phase step directly, +* falling back to ToD read-modify-write if phase step is unavailable. +* In AUTO/REFLOCK modes, large deltas are split into ToD +* read-modify-write for seconds and TIE write for the sub-second +* remainder. Sub-second deltas use TIE write directly. * * Return: 0 on success, <0 on error */
@@ -2501,6 +2504,7 @@ static int zl3073x_dpll_ptp_adjtime(struct ptp_clock_info *info, s64 delta) struct zl3073x_dpll *zldpll = container_of(info, struct zl3073x_dpll, ptp_info);struct zl3073x_dev *zldev = zldpll->dev; +const struct zl3073x_chan *chan;bool sec_adjusted = false;struct timespec64 ts;int rc;
@@ -2510,8 +2514,16 @@ static int zl3073x_dpll_ptp_adjtime(struct ptp_clock_info *info, s64 delta)guard (mutex)(&zldpll->lock); +/* Modes without phase step or TIE use plain ToD adjust */ + chan =zl3073x_chan_state_get (zldev, zldpll->id); +if (!zl3073x_chan_mode_is_nco (chan) && + !zl3073x_chan_mode_supports_tie (chan)) +return zl3073x_chan_tod_adjust (zldev, zldpll->id, +ns_to_timespec64 (delta)); +/* Split off seconds via ToD read-modify-write so the sub-second -* remainder can be applied through the output-coherent phase step. +* remainder can be applied through the output-coherent mechanism +* (phase step or TIE write). */ if (abs (delta) >=NSEC_PER_SEC ) {s32 remainder;
@@ -2531,19 +2543,26 @@ static int zl3073x_dpll_ptp_adjtime(struct ptp_clock_info *info, s64 delta)return 0 ; } - rc =zl3073x_dpll_ptp_adjtime_phase_step (zldpll, delta); -if (!rc) -return 0 ; +/* Apply sub-second delta via phase step (NCO) or TIE write */ +if (zl3073x_chan_mode_is_nco (chan)) { + rc =zl3073x_dpll_ptp_adjtime_phase_step (zldpll, delta); +if (!rc) +return 0 ; + }else { + rc =zl3073x_chan_tie_write (zldev, zldpll->id, delta); +if (!rc) +return 0 ; + } -/* Phase step failed or unavailable, fall back to ToD adjust */ +/* Phase step or TIE write failed, fall back to ToD adjust */ rc =zl3073x_chan_tod_adjust (zldev, zldpll->id,ns_to_timespec64 (delta)); -/* In the unlikely event that both phase step and fallback ToD adjust -* fail after seconds were already committed, return success to prevent -* the PTP servo from retrying the full delta and applying seconds -* again. The sub-second residual will self-correct in the next servo -* cycle. +/* In the unlikely event that both phase step/TIE write and fallback +* ToD adjust fail after seconds were already committed, return +* success to prevent the PTP servo from retrying the full delta and +* applying seconds again. The sub-second residual will self-correct +* in the next servo cycle. */ if (rc && sec_adjusted) { dev_warn(zldev->dev,
@@ -2559,9 +2578,9 @@ static int zl3073x_dpll_ptp_adjtime(struct ptp_clock_info *info, s64 delta)* @info: PTP clock info * @scaled_ppm: frequency adjustment in scaled ppm (ppm * 2^16) * -* Writes the delta frequency offset register. +* Only supported in NCO mode. Writes the delta frequency offset register. * -* Return: 0 on success, <0 on error +* Return: 0 on success, -EOPNOTSUPP if not in NCO mode, <0 on error */ static int zl3073x_dpll_ptp_adjfine (struct ptp_clock_info *info,long scaled_ppm)
@@ -2583,12 +2602,53 @@ zl3073x_dpll_ptp_adjfine(struct ptp_clock_info *info, long scaled_ppm)guard (mutex)(&zldpll->lock); chan =zl3073x_chan_state_get (zldpll->dev, zldpll->id); +if (!zl3073x_chan_mode_is_nco (chan)) +return scaled_ppm ? -EOPNOTSUPP :0 ;if (offset == chan->df_offset)return 0 ;return zl3073x_chan_df_offset_set (zldpll->dev, zldpll->id, offset); } +/** +* zl3073x_dpll_ptp_adjphase - adjust PTP clock phase +* @info: PTP clock info +* @delta: phase adjustment in nanoseconds +* +* Only supported in AUTO and REFLOCK modes. Uses TIE write for +* nanosecond resolution phase adjustment. +* +* Return: 0 on success, -EOPNOTSUPP if mode doesn't support TIE, <0 on error +*/ +static int zl3073x_dpll_ptp_adjphase (struct ptp_clock_info *info,s32 delta) +{ + struct zl3073x_dpll *zldpll = container_of(info, struct zl3073x_dpll, + ptp_info); +struct zl3073x_dev *zldev = zldpll->dev; +const struct zl3073x_chan *chan; + +if (!delta) +return 0 ; + +guard (mutex)(&zldpll->lock); + + chan =zl3073x_chan_state_get (zldev, zldpll->id); + +if (!zl3073x_chan_mode_supports_tie (chan)) +return -EOPNOTSUPP ; + +return zl3073x_chan_tie_write (zldev, zldpll->id, delta); +} + +static s32 +zl3073x_dpll_ptp_getmaxphase (struct ptp_clock_info *info__always_unused ) +{ +/* HW limits TIE write to +-1 second. Return the constant HW +* limit and let adjphase handle mode-specific checks. +*/ +return NSEC_PER_SEC -1 ; +} +/** * zl3073x_dpll_ptp_perout_find_pin - find pin by perout channel index * @zldpll: DPLL channel
@@ -2678,6 +2738,8 @@ static const struct ptp_clock_info zl3073x_dpll_ptp_clock_info = { .settime64 = zl3073x_dpll_ptp_settime64, .adjtime = zl3073x_dpll_ptp_adjtime, .adjfine = zl3073x_dpll_ptp_adjfine, + .adjphase = zl3073x_dpll_ptp_adjphase, + .getmaxphase = zl3073x_dpll_ptp_getmaxphase, .enable = zl3073x_dpll_ptp_enable, };
@@ -2685,21 +2747,13 @@ static const struct ptp_clock_info zl3073x_dpll_ptp_clock_info = {* zl3073x_dpll_ptp_register - register PTP clock for a DPLL channel * @zldpll: DPLL channel to register PTP clock for * -* PTP clock is only registered when the channel is in NCO mode. -* If the channel is not in NCO mode, this is a no-op. -* * Return: 0 on success, <0 on error */ static int zl3073x_dpll_ptp_register (struct zl3073x_dpll *zldpll) {struct zl3073x_dev *zldev = zldpll->dev; -const struct zl3073x_chan *chan;struct ptp_clock *ptp_clock; - chan =zl3073x_chan_state_get (zldev, zldpll->id); -if (!zl3073x_chan_mode_is_nco (chan)) -return 0 ; -snprintf (zldpll->ptp_info.name,sizeof (zldpll->ptp_info.name),"zl3073x-dpll%u" , zldpll->id);
Signature · 1 line
2.53.0