Skip to contentSkip to conversation

ptp: Add clock driver for the OpenCompute TimeCard.

PATCH Superseded Jonathan Lemon 2 participants 4 messages Dec 3, 2020 – Dec 4, 2020

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 ptp: Add clock driver for the OpenCompute TimeCard.

The OpenCompute time card is an atomic clock along with a GPS receiver that provides a Grandmaster clock source for a PTP enabled network.

More information is available at http://www.timingcard.com/

---
 drivers/ptp/Kconfig   |  14 ++
 drivers/ptp/Makefile  |   1 +
 drivers/ptp/ptp_ocp.c | 407 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 422 insertions(+)
 create mode 100644 drivers/ptp/ptp_ocp.c
drivers/ptp/Kconfig +14 -0
index 942f72d8151d..476d7c7fe70a 100644
@@ -151,4 +151,18 @@ config PTP_1588_CLOCK_VMW
 	  To compile this driver as a module, choose M here: the module
 	  will be called ptp_vmw.
 
+config PTP_1588_CLOCK_OCP
+	tristate "OpenCompute TimeCard as PTP clock"
+	depends on PTP_1588_CLOCK
+	depends on HAS_IOMEM && PCI
+	default n
+	help
+	  This driver adds support for an OpenCompute time card.
+
+	  The OpenCompute time card is an atomic clock along with
+	  a GPS receiver that provides a Grandmaster clock source
+	  for a PTP enabled network.
+
+	  More information is available at http://www.timingcard.com/
+
 endmenu
drivers/ptp/Makefile +1 -0
index 7aff75f745dc..db5aef3bddc6 100644
@@ -15,3 +15,4 @@ ptp-qoriq-$(CONFIG_DEBUG_FS)		+= ptp_qoriq_debugfs.o
 obj-$(CONFIG_PTP_1588_CLOCK_IDTCM)	+= ptp_clockmatrix.o
 obj-$(CONFIG_PTP_1588_CLOCK_IDT82P33)	+= ptp_idt82p33.o
 obj-$(CONFIG_PTP_1588_CLOCK_VMW)	+= ptp_vmw.o
+obj-$(CONFIG_PTP_1588_CLOCK_OCP)	+= ptp_ocp.o
drivers/ptp/ptp_ocp.c new file +407 -0

407 lines not on this page · raw message

Signature · 1 line
2.24.1
maintainer
#1 lore raw
Quoted 7 lines · Jonathan Lemon

The OpenCompute time card is an atomic clock along with a GPS receiver that provides a Grandmaster clock source for a PTP enabled network.

More information is available at http://www.timingcard.com/

What changed in v2?

(please include a change log for future patches)

drivers/ptp/ptp_ocp.c line 113

Quoted 4 lines
drivers/ptp/ptp_ocp.c new file +4 -0
+static int
+ptp_ocp_gettimex(struct ptp_clock_info *ptp_info, struct timespec64 *ts,
+		 struct ptp_system_timestamp *sts)
+{

The name here is a bit confusing since "timex" has a special meaning in the NTP/PTP API.

Suggest plain old ptp_ocp_gettime();

Quoted 35 lines
drivers/ptp/ptp_ocp.c new file +35 -0
+	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
+	unsigned long flags;
+	int err;
+
+	spin_lock_irqsave(&bp->lock, flags);
+	err = __ptp_ocp_gettime_locked(bp, ts, sts);
+	spin_unlock_irqrestore(&bp->lock, flags);
+
+	return err;
+}
+
+static void
+__ptp_ocp_settime_locked(struct ptp_ocp *bp, const struct timespec64 *ts)
+{
+	u32 ctrl, time_sec, time_ns;
+	u32 select;
+
+	time_ns = ts->tv_nsec;
+	time_sec = ts->tv_sec;
+
+	select = ioread32(&bp->reg->select);
+	iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
+
+	iowrite32(time_ns, &bp->reg->adjust_ns);
+	iowrite32(time_sec, &bp->reg->adjust_sec);
+
+	ctrl = ioread32(&bp->reg->ctrl);
+	ctrl |= OCP_CTRL_ADJUST_TIME;
+	iowrite32(ctrl, &bp->reg->ctrl);
+
+	/* restore clock selection */
+	iowrite32(select >> 16, &bp->reg->select);
+}
+
+static int

drivers/ptp/ptp_ocp.c line 158

Quoted 10 lines
drivers/ptp/ptp_ocp.c new file +10 -0
+ptp_ocp_settime(struct ptp_clock_info *ptp_info, const struct timespec64 *ts)
+{
+	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
+	unsigned long flags;
+
+	if (ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC)
+		return 0;
+
+	dev_info(&bp->pdev->dev, "settime to: %lld.%ld\n",
+		 ts->tv_sec, ts->tv_nsec);

No need for this dmesg spam. Change to _debug if you really want to
keep it.

Quoted 6 lines
drivers/ptp/ptp_ocp.c new file +6 -0
+	spin_lock_irqsave(&bp->lock, flags);
+	__ptp_ocp_settime_locked(bp, ts);
+	spin_unlock_irqrestore(&bp->lock, flags);
+
+	return 0;
+}

drivers/ptp/ptp_ocp.c line 199

Quoted 10 lines
drivers/ptp/ptp_ocp.c new file +9 -0
 
+static int
+ptp_ocp_null_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm)
+{
+	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
+
+	if (scaled_ppm == 0)
+		return 0;
+
+	dev_info(&bp->pdev->dev, "adjfine, scaled by: %ld\n", scaled_ppm);

No need for this either.

drivers/ptp/ptp_ocp.c line 201

Quoted 1 line
drivers/ptp/ptp_ocp.c new file +1 -0
+	return -EOPNOTSUPP;

You can set the offset but not the frequency adjustment. Makes sense for an atomic clock.

Quoted 1 line

+}

This driver looks fine, but I'm curious how you will use it. Can it provide time stamping for network frames or other IO?

If not, then that seems unfortunate. Still you can regulate the Linux system clock in software, but that of course introduces time error.

Thanks,
Richard

#2 lore raw
Quoted 5 lines · Richard Cochran
Quoted 4 lines · Jonathan Lemon

The OpenCompute time card is an atomic clock along with a GPS receiver that provides a Grandmaster clock source for a PTP enabled network.

Quoted 7 lines
Quoted 3 lines

More information is available at http://www.timingcard.com/

What changed in v2?

(please include a change log for future patches)

test robot complained because it wasn't dependent on CONFIG_PCI. Will add a cover letter for the next v3.

Quoted 7 lines

drivers/ptp/ptp_ocp.c line 113

Quoted 4 lines
drivers/ptp/ptp_ocp.c new file +4 -0
+static int
+ptp_ocp_gettimex(struct ptp_clock_info *ptp_info, struct timespec64 *ts,
+		 struct ptp_system_timestamp *sts)
+{

The name here is a bit confusing since "timex" has a special meaning in the NTP/PTP API.

The .gettimex64 call is used here so the time returned from the clock can be correlated to the system time.

Quoted 6 lines
Quoted 6 lines
drivers/ptp/ptp_ocp.c new file +6 -0
+static int
+ptp_ocp_settime(struct ptp_clock_info *ptp_info, const struct timespec64 *ts)
+{
+	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
+	unsigned long flags;
+
Quoted 8 lines

drivers/ptp/ptp_ocp.c line 158

Quoted 5 lines
drivers/ptp/ptp_ocp.c new file +5 -0
+	if (ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC)
+		return 0;
+
+	dev_info(&bp->pdev->dev, "settime to: %lld.%ld\n",
+		 ts->tv_sec, ts->tv_nsec);

No need for this dmesg spam. Change to _debug if you really want to
keep it.

Will remove.

Quoted 7 lines
Quoted 6 lines
drivers/ptp/ptp_ocp.c new file +6 -0
+	spin_lock_irqsave(&bp->lock, flags);
+	__ptp_ocp_settime_locked(bp, ts);
+	spin_unlock_irqrestore(&bp->lock, flags);
+
+	return 0;
+}
Quoted 11 lines

drivers/ptp/ptp_ocp.c line 199

Quoted 9 lines
drivers/ptp/ptp_ocp.c new file +9 -0
+static int
+ptp_ocp_null_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm)
+{
+	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
+
+	if (scaled_ppm == 0)
+		return 0;
+
+	dev_info(&bp->pdev->dev, "adjfine, scaled by: %ld\n", scaled_ppm);

No need for this either.

Ditto.

Quoted 2 lines

This driver looks fine, but I'm curious how you will use it. Can it provide time stamping for network frames or other IO?

The card does have a PPS pulse output, so it can be wired to a network card which takes an external PPS signal.

Right now, the current model (which certainly can be improved on) is using phc2sys to discipline the system and NIC clocks.

I'll send a v3. I also need to open a discussion on how this should return the leap second changes to the user - there doesn't seem to be anything in the current API for this.

Signature · 1 line
Jonathan
maintainer
#3 lore raw
Quoted 7 lines · Jonathan Lemon
Quoted 2 lines · Richard Cochran

The name here is a bit confusing since "timex" has a special meaning in the NTP/PTP API.

The .gettimex64 call is used here so the time returned from the clock can be correlated to the system time.

[facepalm] man, its in the interface naming. Oh well.

Quoted 5 lines
Quoted 2 lines

This driver looks fine, but I'm curious how you will use it. Can it provide time stamping for network frames or other IO?

The card does have a PPS pulse output, so it can be wired to a network card which takes an external PPS signal.

Cool. So the new ts2phc program can synchronize the NICs for you. All that is missing here is ptp_clock_info.enable(). With that in place, it will work out of the box.

Quoted 2 lines

Right now, the current model (which certainly can be improved on) is using phc2sys to discipline the system and NIC clocks.

Yeah, ts2phc will fill in the gap.

Quoted 3 lines

I'll send a v3. I also need to open a discussion on how this should return the leap second changes to the user - there doesn't seem to be anything in the current API for this.

There is the clock_adjtime() system call, analogue of adjtimex.

Right now there is no PHC driver access to the timex.status field (which carries the leap flags), but that can be changed...

kernel/time/posix-timers.c do_clock_adjtime() calls
kernel/time/posix-clock.c pc_clock_adjtime() calls
drivers/ptp/ptp_clock.c ptp_clock_adjtime()

At this point the PHC layer could invoke a new, optional driver callback that returns the leap second status flags, and then the PHC layer could set timex.status appropriately.

Having said all that, the ts2phc program takes the approach of getting the leap second information from the leap seconds file. Of course, this requires administratively updating the file at least once every six months.

I considered and rejected the idea of trying to get the current leap second status from GPS for two reasons. First, there is no standardized and universally implemented way of querying this from a GPS. Second, and more importantly, the leap second information is only broadcast every 12.5 minutes, and that it is *way* too long to wait after cold boot for applications I am interested in.

So the choice boiled down to either having to keep a file up to date, say every month or so, or waiting 15 minutes in the worst case. I chose the lesser of two evils.

Thanks,
Richard