All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/3] Patch series for a PTP Grandmaster use case using stmmac/gmac3 ptp clock
@ 2020-05-14 10:28 Olivier Dautricourt
  2020-05-14 10:28 ` [PATCH 1/3] net: stmmac: gmac3: add auxiliary snapshot support Olivier Dautricourt
                   ` (3 more replies)
  0 siblings, 4 replies; 17+ messages in thread
From: Olivier Dautricourt @ 2020-05-14 10:28 UTC (permalink / raw)
  To: Giuseppe Cavallaro, Alexandre Torgue, Jose Abreu, David S . Miller
  Cc: Richard Cochran, netdev, Olivier Dautricourt

This patch series covers a use case where an embedded system is
disciplining an internal clock to a GNSS signal, which provides a
stable frequency, and wants to act as a PTP Grandmaster by disciplining
a ptp clock to this internal clock.

In our setup a 10Mhz oscillator is frequency adjusted so that a derived
pps from that oscillator is in phase with the pps generated by 
a gnss receiver.

An other derived clock from the same disciplined oscillator is used as
ptp_clock for the ethernet mac.

The internal pps of the system is forwarded to one of the auxiliary inputs
of the MAC.

Initially the mac time registers are considered random.
We want the mac nanosecond field to be 0 on the auxiliary pps input edge.


PATCH 1/3: 
	The stmmac gmac3 version used in the setup is patched to retrieve a
	timestamp at the rising edge of the aux input and to forward
	it to userspace.

* What matters here is that we get the subsecond offset between the aux 
edge and the edge of the PHC's pps. *


PATCH 2,3/3:

	We want the ptp clock to be in time with our aux pps input.
	Since the ptp clock is derived from the system oscillator, we
	don't want to do frequency adjustements.

	The stmmac driver is patched to allow to set the coarse correction
	mode which avoid to adjust the frequency of the mac continuously
	(the default behavior), but instead, have just one
	time adjustment.


We calculate the time difference between the mac and the internal
clock, and adust the ptp clock time with clock_adjtime syscall.


To summarize this in a user-space program:

****
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

#include <arpa/inet.h>
#include <net/if.h>

#include <sys/ioctl.h>
#include <sys/syscall.h>
#include <sys/timex.h>

#include <linux/ptp_clock.h>
#include <linux/net_tstamp.h>
#include <linux/sockios.h>

#define NS_PER_SEC 1000000000LL

#define CLOCKFD 3

#define FD_TO_CLOCKID(fd) \
	((clockid_t) ((((unsigned int) ~fd) << 3) | CLOCKFD))


static inline int clock_adjtime(clockid_t id, struct timex *tx)
{
	return syscall(__NR_clock_adjtime, id, tx);
}

int main(void)
{
	int fd;
	struct timex tx = {0};
	struct ifreq ifreq = {0};
	struct hwtstamp_config cfg = {0};
	struct ptp_extts_event event = {0};
	struct ptp_extts_request extts_request = {
		.index = 0,
		.flags = PTP_RISING_EDGE | PTP_ENABLE_FEATURE
	};

	const char *iface = "eth0";
	const char *ptp_dev = "/dev/ptp2";

	strncpy(ifreq.ifr_name, iface, sizeof(ifreq.ifr_name) - 1);
	ifreq.ifr_data = (void *) &cfg;
	fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);

	if (fd < 0)
		return 1;

	if (ioctl(fd, SIOCGHWTSTAMP, &ifreq) < 0)
		return 1;

	// Activate coarse mode for stmmac
	cfg.flags |= HWTSTAMP_FLAGS_ADJ_COARSE;
	cfg.flags &= ~HWTSTAMP_FLAGS_ADJ_FINE;

	if (ioctl(fd, SIOCSHWTSTAMP, &ifreq) < 0)
		return 1;

	fd = open(ptp_dev, O_RDWR);

	if (fd < 0)
		return 1;

	// Enable extts input index 0
	if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request) < 0)
		return 1;

	// Read extts
	if (read(fd, &event, sizeof(event)) != sizeof(event))
		return 1;

	// Correct phc time subsecond: note that this does not correct the phc
	// second count for concision. The delta is (event.t.nsec - NS_PER_SEC).
	tx.modes = ADJ_SETOFFSET | ADJ_NANO;
	tx.time.tv_sec = -1;
	tx.time.tv_usec = event.t.nsec;

	if (clock_adjtime(FD_TO_CLOCKID(fd), &tx))
		return 1;

	// Disable extts index 0
	extts_request.index = 0;
	extts_request.flags = 0;

	if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request) < 0)
		return 1;

	return 0;
}
****

Artem Panfilov (1):
  net: stmmac: GMAC3: add auxiliary snapshot support

Olivier Dautricourt (2):
  net: uapi: Add HWTSTAMP_FLAGS_ADJ_FINE/ADJ_COARSE
  net: stmmac: Support coarse mode through ioctl

 .../net/ethernet/stmicro/stmmac/dwmac1000.h   |  3 +-
 .../ethernet/stmicro/stmmac/dwmac1000_core.c  | 24 ++++++++++
 drivers/net/ethernet/stmicro/stmmac/hwif.h    |  9 ++--
 .../ethernet/stmicro/stmmac/stmmac_hwtstamp.c | 10 +++-
 .../net/ethernet/stmicro/stmmac/stmmac_main.c | 21 ++++++---
 .../net/ethernet/stmicro/stmmac/stmmac_ptp.c  | 47 +++++++++++++++++++
 .../net/ethernet/stmicro/stmmac/stmmac_ptp.h  | 20 ++++++++
 include/uapi/linux/net_tstamp.h               | 12 +++++
 net/core/dev_ioctl.c                          |  3 --
 9 files changed, 133 insertions(+), 16 deletions(-)

-- 
2.17.1


^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2020-06-03 16:12 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-14 10:28 [PATCH 0/3] Patch series for a PTP Grandmaster use case using stmmac/gmac3 ptp clock Olivier Dautricourt
2020-05-14 10:28 ` [PATCH 1/3] net: stmmac: gmac3: add auxiliary snapshot support Olivier Dautricourt
2020-05-14 10:28 ` [PATCH 2/3] net: uapi: Add HWTSTAMP_FLAGS_ADJ_FINE/ADJ_COARSE Olivier Dautricourt
2020-05-14 13:38   ` Richard Cochran
2020-05-14 15:20     ` Olivier Dautricourt
2020-05-15  0:29       ` Richard Cochran
2020-05-14 10:28 ` [PATCH 3/3] net: stmmac: Support coarse mode through ioctl Olivier Dautricourt
2020-05-27  3:55   ` Richard Cochran
2020-06-03 16:12     ` Olivier Dautricourt
2020-05-14 13:53 ` [PATCH 0/3] Patch series for a PTP Grandmaster use case using stmmac/gmac3 ptp clock Richard Cochran
2020-05-14 15:09   ` Olivier Dautricourt
2020-05-15  0:37     ` Richard Cochran
2020-05-15 13:26       ` Julien Beraud
2020-05-15 23:30         ` Richard Cochran
2020-05-25 10:00           ` Olivier Dautricourt
2020-05-27  4:05         ` Richard Cochran
2020-06-03 15:17           ` Olivier Dautricourt

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.