netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Olivier Dautricourt <olivier.dautricourt@orolia.com>
To: Giuseppe Cavallaro <peppe.cavallaro@st.com>,
	Alexandre Torgue <alexandre.torgue@st.com>,
	Jose Abreu <joabreu@synopsys.com>,
	"David S . Miller" <davem@davemloft.net>
Cc: Richard Cochran <richardcochran@gmail.com>,
	netdev@vger.kernel.org,
	Olivier Dautricourt <olivier.dautricourt@orolia.com>
Subject: [PATCH 0/3] Patch series for a PTP Grandmaster use case using stmmac/gmac3 ptp clock
Date: Thu, 14 May 2020 12:28:05 +0200	[thread overview]
Message-ID: <20200514102808.31163-1-olivier.dautricourt@orolia.com> (raw)

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


             reply	other threads:[~2020-05-14 10:29 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-14 10:28 Olivier Dautricourt [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200514102808.31163-1-olivier.dautricourt@orolia.com \
    --to=olivier.dautricourt@orolia.com \
    --cc=alexandre.torgue@st.com \
    --cc=davem@davemloft.net \
    --cc=joabreu@synopsys.com \
    --cc=netdev@vger.kernel.org \
    --cc=peppe.cavallaro@st.com \
    --cc=richardcochran@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).