Skip to contentSkip to conversation

dpll: zl3073x: add PTP clock support

PATCH Superseded Ivan Vecera 2 participants 9 messages Aug 3 – Aug 6

Subsystems (4)

v4 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Ivan Vecera

author maintainer
#0 lore raw

v4 cover dpll: zl3073x: add PTP clock support

Add PTP hardware clock support to the zl3073x DPLL driver.

Patch 1 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 2 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:
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 (2):
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  | 310 ++++++++++++++++++++++++-
 drivers/dpll/zl3073x/chan.h  |  32 +++
 drivers/dpll/zl3073x/core.c  |  13 ++
 drivers/dpll/zl3073x/core.h  |  23 ++
 drivers/dpll/zl3073x/dpll.c  | 425 ++++++++++++++++++++++++++++++++---
 drivers/dpll/zl3073x/dpll.h  |   5 +
 drivers/dpll/zl3073x/regs.h  |  52 +++++
 8 files changed, 836 insertions(+), 30 deletions(-)

base-commit: a5c6ae8de11ef0c3aedbe5e123354aad87d0d925

Signature · 1 line
2.54.0
author maintainer
#1 lore raw

PATCH v4 1/2 dpll: zl3073x: add channel ToD, phase step and TIE operations

Add 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 | 310 +++++++++++++++++++++++++++++++++++-
 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, 428 insertions(+), 2 deletions(-)
drivers/dpll/zl3073x/chan.c +308 -2
index 4ec2cf53dad468..79874a9fdb4962 100644
@@ -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,311 @@ 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 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. If the tick crossed between the two reads,
+ * normalizes by the number of elapsed ticks. 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 the next rollover, wait it out. If the 1 Hz tick
+	 * crossed between the two reads, normalize by the elapsed ticks.
+	 */
+	diff = timespec64_sub(ts_next, ts_cur);
+	if (!diff.tv_sec && diff.tv_nsec < threshold_ns) {
+		fsleep((unsigned long)diff.tv_nsec / NSEC_PER_USEC + 1);
+		ts_next.tv_sec++;
+	} else if (diff.tv_sec < 0) {
+		ts_next.tv_sec -= diff.tv_sec;
+	}
+
+	/* 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);
+}
+
+/**
+ * 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
index dc9c6d95bdee75..99c3b7088f678f 100644
@@ -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
index 7f5afaaae6342f..8a8c27a06ac9de 100644
@@ -510,6 +510,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) {
@@ -1033,6 +1038,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
index 78dc208f3eea2a..67c10e2595118c 100644
@@ -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
maintainer
#2 lore raw

Re: [PATCH net-next v4 1/2] dpll: zl3073x: add channel ToD, phase step and TIE operations

Quoted 79 lines · Ivan Vecera

Add 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 | 310 +++++++++++++++++++++++++++++++++++-
  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, 428 insertions(+), 2 deletions(-)
drivers/dpll/zl3073x/chan.c +21 -2
index 4ec2cf53dad468..79874a9fdb4962 100644
@@ -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,311 @@ 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)

drivers/dpll/zl3073x/chan.c const struct zl3073x_chan *zl3073x_chan_state_get(struct zl3073x_dev *zldev, · line 265

Quoted 10 lines
drivers/dpll/zl3073x/chan.c +10 -0
+		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;
+	}

there are 3 cmds defined, but FIELD_GET(ZL_DPLL_TOD_CTRL_CMD) can return up to 16 possible values. I would explicitly put defined commands in cases and make default to ENOTSUPP..

Quoted 6 lines
drivers/dpll/zl3073x/chan.c +6 -0
+
+	rc = zl3073x_poll_zero_u8(zldev, ZL_REG_DPLL_TOD_CTRL(ch),
+				  ZL_DPLL_TOD_CTRL_SEM, timeout);
+
+	return rc == -ETIMEDOUT ? -EBUSY : rc;
+}

[...]

Quoted 5 lines
drivers/dpll/zl3073x/chan.c +5 -0
+#define ZL_DPLL_TOD_CTRL_SEM			BIT(4)
+#define ZL_DPLL_TOD_CTRL_CMD			GENMASK(3, 0)
+#define ZL_DPLL_TOD_CTRL_CMD_WR_NEXT_1HZ	1
+#define ZL_DPLL_TOD_CTRL_CMD_RD_CURRENT		8
+#define ZL_DPLL_TOD_CTRL_CMD_RD_NEXT_1HZ	9
author maintainer
#3 lore raw

Re: [PATCH net-next v4 1/2] dpll: zl3073x: add channel ToD, phase step and TIE operations

Quoted 86 lines · Vadim Fedorenko
Quoted 85 lines · Ivan Vecera

Add 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 | 310 +++++++++++++++++++++++++++++++++++-
  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, 428 insertions(+), 2 deletions(-)
drivers/dpll/zl3073x/chan.c +3 -2
index 4ec2cf53dad468..79874a9fdb4962 100644
@@ -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

   */

drivers/dpll/zl3073x/chan.c +24 -0
@@ -234,6 +235,311 @@ 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 ZL_DPLL_TOD_CTRL_CMD_WR_NEXT_1HZ:
+        timeout = ZL_POLL_TOD_WR_TIMEOUT_US;
+        break;
Quoted 8 lines

drivers/dpll/zl3073x/chan.c const struct zl3073x_chan *zl3073x_chan_state_get(struct zl3073x_dev *zldev, · line 265

Quoted 4 lines
drivers/dpll/zl3073x/chan.c +4 -0
+    default:
+        timeout = ZL_POLL_TOD_RD_TIMEOUT_US;
+        break;
+    }

there are 3 cmds defined, but FIELD_GET(ZL_DPLL_TOD_CTRL_CMD) can return up to 16 possible values. I would explicitly put defined commands in cases and make default to ENOTSUPP..

Hi Vadim,
the ZL_DPLL_TOD_CTRL_CMD bits are never filled by firmware (only the semaphore bit) so the driver knows what it writes. So such check is not necessary but if you want it I can add something like:

...

    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:
        WARN_ON(1); /* this is really unexpected */
        return -ENOTSUPP;

...

Let me know.

Thanks,
Ivan

maintainer
#4 lore raw

Re: [PATCH net-next v4 1/2] dpll: zl3073x: add channel ToD, phase step and TIE operations

Quoted 101 lines · Ivan Vecera
Quoted 86 lines · Vadim Fedorenko
Quoted 85 lines · Ivan Vecera

Add 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 | 310 +++++++++++++++++++++++++++++++++++-
  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, 428 insertions(+), 2 deletions(-)
drivers/dpll/zl3073x/chan.c +3 -2
index 4ec2cf53dad468..79874a9fdb4962 100644
@@ -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

   */

drivers/dpll/zl3073x/chan.c +24 -0
@@ -234,6 +235,311 @@ 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 ZL_DPLL_TOD_CTRL_CMD_WR_NEXT_1HZ:
+        timeout = ZL_POLL_TOD_WR_TIMEOUT_US;
+        break;
Quoted 8 lines

drivers/dpll/zl3073x/chan.c const struct zl3073x_chan *zl3073x_chan_state_get(struct zl3073x_dev *zldev, · line 265

Quoted 4 lines
drivers/dpll/zl3073x/chan.c +4 -0
+    default:
+        timeout = ZL_POLL_TOD_RD_TIMEOUT_US;
+        break;
+    }

there are 3 cmds defined, but FIELD_GET(ZL_DPLL_TOD_CTRL_CMD) can return up to 16 possible values. I would explicitly put defined commands in cases and make default to ENOTSUPP..

Hi Vadim,
the ZL_DPLL_TOD_CTRL_CMD bits are never filled by firmware (only the semaphore bit) so the driver knows what it writes. So such check is not necessary but if you want it I can add something like:

Quoted 9 lines

...
    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:
        WARN_ON(1); /* this is really unexpected */ return -ENOTSUPP;
...

This looks a bit more safe, especially now while the driver is in active development. Let's add this part.

Thanks

Quoted 6 lines

Let me know.

Thanks,
Ivan

author maintainer
#5 lore raw

Re: [PATCH net-next v4 1/2] dpll: zl3073x: add channel ToD, phase step and TIE operations

Quoted 113 lines · Vadim Fedorenko
Quoted 102 lines · Ivan Vecera
Quoted 87 lines · Vadim Fedorenko
Quoted 86 lines · Ivan Vecera

Add 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 | 310 ++++++++++++++++++++++++++++++++
+++-

  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, 428 insertions(+), 2 deletions(-)
drivers/dpll/zl3073x/chan.c +3 -2
index 4ec2cf53dad468..79874a9fdb4962 100644
@@ -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

   */

drivers/dpll/zl3073x/chan.c +24 -0
@@ -234,6 +235,311 @@ 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 ZL_DPLL_TOD_CTRL_CMD_WR_NEXT_1HZ:
+        timeout = ZL_POLL_TOD_WR_TIMEOUT_US;
+        break;
Quoted 8 lines

drivers/dpll/zl3073x/chan.c const struct zl3073x_chan *zl3073x_chan_state_get(struct zl3073x_dev *zldev, · line 265

Quoted 4 lines
drivers/dpll/zl3073x/chan.c +4 -0
+    default:
+        timeout = ZL_POLL_TOD_RD_TIMEOUT_US;
+        break;
+    }

there are 3 cmds defined, but FIELD_GET(ZL_DPLL_TOD_CTRL_CMD) can return up to 16 possible values. I would explicitly put defined commands in cases and make default to ENOTSUPP..

Hi Vadim,
the ZL_DPLL_TOD_CTRL_CMD bits are never filled by firmware (only the semaphore bit) so the driver knows what it writes. So such check is not necessary but if you want it I can add something like:

Quoted 9 lines

...
     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:
         WARN_ON(1); /* this is really unexpected */ return -ENOTSUPP;
...

Quoted 4 lines

This looks a bit more safe, especially now while the driver is in active development. Let's add this part.

Thanks

OK, will update in v5.

Thanks,
Ivan

author maintainer
#6 lore raw

Re: [PATCH net-next v4 1/2] dpll: zl3073x: add channel ToD, phase step and TIE operations

In reply to Ivan Vecera

Sashiko findings with replies...

Quoted 10 lines

zl3073x_poll_zero_u8() hard-codes the interval:

#define ZL_POLL_SLEEP_US 10

so the new timeouts [...] all wait at a 10 us cadence. [...] giving on the order of 10^5 bus reads while waiting out a WR_NEXT_1HZ command [...]
Would a millisecond-scale interval variant be preferable for these three cases, so the shared bus and the driver's monitor kworker are not starved for up to a second?

Good catch. I did some testing and found that even the existing registers typically clear within low hundreds of iterations, so the fixed 10 us sleep is already unnecessarily aggressive for all callers.

I will add a prep patch that replaces the fixed ZL_POLL_SLEEP_US with timeout_us / 50 in zl3073x_poll_zero_u8(). This scales the sleep interval proportionally to the timeout, targeting ~50 iterations regardless of timeout magnitude:
- existing 30-50 ms timeouts: 600-1000 us sleep
- new 1000-3000 ms PTP timeouts: 20-60 ms sleep

Quoted 6 lines

Can the tick-crossed branch skip the 20 ms rollover guard and shift the applied time by one second?

The margin test is gated on !diff.tv_sec, so when the 1 Hz tick crossed between the two zl3073x_chan_tod_read() calls only the seconds get fixed up and the remaining margin is never compared against threshold_ns [...]

Good point. Although this requires ~1 s delay between two consecutive register reads (practically unreachable), the fix is clean and costs nothing. I will restructure the guard to separate the tick-crossing fixup from the margin check so both apply independently:

    diff = timespec64_sub(ts_next, ts_cur);

    if (diff.tv_sec < 0)
        ts_next.tv_sec -= diff.tv_sec;

    if (diff.tv_sec <= 0 && diff.tv_nsec < threshold_ns) {
        fsleep((unsigned long)diff.tv_nsec / NSEC_PER_USEC + 1);
        ts_next.tv_sec++;
    }

Thanks,
Ivan

author maintainer
#7 lore raw

PATCH v4 2/2 dpll: zl3073x: add PTP clock support

In reply to Ivan Vecera

Add 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  | 425 ++++++++++++++++++++++++++++++++---
 drivers/dpll/zl3073x/dpll.h  |   5 +
 3 files changed, 408 insertions(+), 28 deletions(-)
drivers/dpll/zl3073x/Kconfig +3 -3
index 5bbca140058131..da1731ace15f02 100644
@@ -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 +400 -25

476 lines not on this page · raw message

drivers/dpll/zl3073x/dpll.h +5 -0
index faebc402ba1b70..993221dc63249d 100644
@@ -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
author maintainer
#8 lore raw

Sashiko findings with replies...

Quoted 4 lines

Do these three depends on lines remove the driver from configurations that have CONFIG_PTP_1588_CLOCK=n? [...]
Is the hard dependency needed? [...] Would [PTP_1588_CLOCK_OPTIONAL] be a better fit here?

This was explicitly requested by Jakub in his v2 review [1]. The driver's PTP support is a core feature, not optional functionality.

[1] https://lore.kernel.org/netdev/[email protected]/

Quoted 6 lines

div_s64() truncates toward zero [...] does a delta smaller than one synth period end up as a 0-cycle step?

For a 100 MHz synth, a 5 ns request gives step_cycles == 0 [...] Would it make sense to route the truncated residual through one of the finer mechanisms [...]?

The synthesizer frequency range is 187.5-750 MHz, giving a step resolution of 1.3-5.3 ns. The maximum truncation loss is therefore one synthesizer period (~5 ns worst case), which is well below the noise floor of any PTP servo. The servo will compensate for this residual in the next correction cycle. Adding a secondary adjustment path for a sub-5 ns residual would complicate the code for no practical benefit.

Quoted 6 lines

When the phase step for the second or a later synth group fails, this returns 0 after the warning. Does that leave the outputs of the remaining synthesizers skewed from the ToD?

[...] Would propagating the error, or retrying the remaining groups, be preferable to reporting success here?

This is intentional. At this point the seconds have already been committed via ToD and the first synth group has been stepped with tod_step=true. Propagating the error would cause the PTP servo to retry the full delta, applying the seconds component a second time - a much worse outcome than leaving some outputs with a sub-second skew. A regmap/bus error at this stage indicates a serious hardware problem that dev_warn() surfaces appropriately. Retrying would likely hit the same bus error.

Quoted 5 lines

Which synth period does the firmware really use for this ToD-only step?

The doc block says "the FW uses the lowest-ID synth's period for the conversion", but first_synth_freq is latched from the first synth that is both enabled and assigned to this channel [...]

The comment is wrong, the code is right. first_synth_freq is the first enabled synth assigned to this DPLL channel, not the globally lowest-ID synth. I will fix the comment.

Quoted 5 lines

Is it intended that zldpll->lock is held across the multi-second hardware waits that follow?

[...] So one clock_adjtime() can park dpll_lock behind zldpll->lock for up to seconds, which blocks dpll netlink operations [...]

This is intentional and follows the standard PTP driver pattern. The lock serializes all DPLL and PTP operations for a given channel. The timeouts are worst-case values - normal operations complete in milliseconds (ToD reads ~1 ms, phase step completes within one synth period). The tod_ready_wait for WR_NEXT_1HZ is the only one that genuinely waits up to ~500 ms on average, but this only occurs for multi-second adjtime calls (>= 1 s delta), which are rare one-shot corrections. Splitting the lock would require re-validating channel state between operations and introduce race conditions with mode changes.

Quoted 5 lines

Should sec_adjusted be set before this tod_ready_wait() error return?

[...] ptp_clock_adjtime() passes the error to userspace unchanged, so a servo or tool that retries the same offset applies the whole-seconds component a second time and the clock jumps by twice the seconds part.

The current ordering is correct. If tod_ready_wait() fails, we cannot be certain that the 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 servo handle the retry is the right behavior, because setting sec_adjusted = true before confirmation would mask a genuine hardware failure and silently report success when the adjustment may not have been applied at all.

Quoted 5 lines

Both mechanisms are asynchronous here, so can a gettimex64() right after adjtime() observe a ToD that has not been stepped yet?

[...] Should the phase step and TIE write get an equivalent wait before success is returned to the PTP core?

In practice this is not a problem. PTP servos wait for the next sync interval (typically 1-8 s) before reading the clock again. The phase step completes within one synthesizer period (1.3-5.3 ns) and TIE write settles within the DPLL loop bandwidth. A completion wait would add up to 3 seconds of latency to every adjtime call for no practical benefit - the sub-nanosecond residual from an in-flight phase step is orders of magnitude below the servo's measurement noise.

Quoted 4 lines

Can this multiplication overflow s64 for a value the core lets through?

[...] Since 125 is invertible modulo 2^64, a tx->freq around 10^17 can be chosen so the wrapped ppb passes the max_adj test.

This is a pre-existing issue in the PTP core's scaled_ppm_to_ppb() helper, not in this driver. For in-range values (bounded by max_adj = 10^7 ppb, |scaled_ppm| up to ~6.6e8), the product is ~4.4e16 which is well within s64 range. Exploiting the wrap in scaled_ppm_to_ppb() requires deliberately constructed tx->freq values around 10^17 and CAP_SYS_TIME privilege. A driver-side guard would be papering over a core bug.

Quoted 5 lines

The kernel-doc just above says "Return: 0 on success, -EOPNOTSUPP if NCO pin is not connected", but this returns 0 for a non-NCO channel whenever scaled_ppm == 0. [...]

zl3073x_dpll_ptp_adjphase() has the same mismatch [...]

This is intentional - a zero adjustment is a no-op and succeeds regardless of mode. Will update the kernel-doc to reflect this behavior.

Quoted 3 lines

The name is built only from the per-device channel index, so two ZL3073x chips in one system expose PTP clocks with identical ptp_clock_info.name [...] Should dev_name(zldev->dev) be part of the PTP clock name too?

Good catch. Will include dev_name() in the PTP clock name, consistent with the kworker naming pattern.

Thanks,
Ivan