1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * PTP 1588 clock support - User space test program
5 * Copyright (C) 2010 OMICRON electronics GmbH
8 #define __SANE_USERSPACE_TYPES__ /* For PPC64, to get LL64 types */
17 #include <sys/ioctl.h>
21 #include <sys/timex.h>
22 #include <sys/types.h>
26 #include <linux/ptp_clock.h>
28 #define DEVICE "/dev/ptp0"
31 #define ADJ_SETOFFSET 0x0100
35 #define CLOCK_INVALID -1
38 /* clock_adjtime is not available in GLIBC < 2.14 */
39 #if !__GLIBC_PREREQ(2, 14)
40 #include <sys/syscall.h>
41 static int clock_adjtime(clockid_t id, struct timex *tx)
43 return syscall(__NR_clock_adjtime, id, tx);
47 static void show_flag_test(int rq_index, unsigned int flags, int err)
49 printf("PTP_EXTTS_REQUEST%c flags 0x%08x : (%d) %s\n",
50 rq_index ? '1' + rq_index : ' ',
51 flags, err, strerror(errno));
52 /* sigh, uClibc ... */
56 static void do_flag_test(int fd, unsigned int index)
58 struct ptp_extts_request extts_request;
59 unsigned long request[2] = {
63 unsigned int enable_flags[5] = {
65 PTP_ENABLE_FEATURE | PTP_RISING_EDGE,
66 PTP_ENABLE_FEATURE | PTP_FALLING_EDGE,
67 PTP_ENABLE_FEATURE | PTP_RISING_EDGE | PTP_FALLING_EDGE,
68 PTP_ENABLE_FEATURE | (PTP_EXTTS_VALID_FLAGS + 1),
72 memset(&extts_request, 0, sizeof(extts_request));
73 extts_request.index = index;
75 for (i = 0; i < 2; i++) {
76 for (j = 0; j < 5; j++) {
77 extts_request.flags = enable_flags[j];
78 err = ioctl(fd, request[i], &extts_request);
79 show_flag_test(i, extts_request.flags, err);
81 extts_request.flags = 0;
82 err = ioctl(fd, request[i], &extts_request);
87 static clockid_t get_clockid(int fd)
90 return (((unsigned int) ~fd) << 3) | CLOCKFD;
93 static long ppb_to_scaled_ppm(int ppb)
96 * The 'freq' field in the 'struct timex' is in parts per
97 * million, but with a 16 bit binary fractional field.
98 * Instead of calculating either one of
100 * scaled_ppm = (ppb / 1000) << 16 [1]
101 * scaled_ppm = (ppb << 16) / 1000 [2]
103 * we simply use double precision math, in order to avoid the
104 * truncation in [1] and the possible overflow in [2].
106 return (long) (ppb * 65.536);
109 static int64_t pctns(struct ptp_clock_time *t)
111 return t->sec * 1000000000LL + t->nsec;
114 static void usage(char *progname)
117 "usage: %s [options]\n"
118 " -c query the ptp clock's capabilities\n"
119 " -d name device to open\n"
120 " -e val read 'val' external time stamp events\n"
121 " -f val adjust the ptp clock frequency by 'val' ppb\n"
122 " -g get the ptp clock time\n"
123 " -h prints this message\n"
124 " -i val index for event/trigger\n"
125 " -k val measure the time offset between system and phc clock\n"
126 " for 'val' times (Maximum 25)\n"
127 " -l list the current pin configuration\n"
128 " -L pin,val configure pin index 'pin' with function 'val'\n"
129 " the channel index is taken from the '-i' option\n"
130 " 'val' specifies the auxiliary function:\n"
132 " 1 - external time stamp\n"
133 " 2 - periodic output\n"
134 " -p val enable output with a period of 'val' nanoseconds\n"
135 " -P val enable or disable (val=1|0) the system clock PPS\n"
136 " -s set the ptp clock time from the system time\n"
137 " -S set the system time from the ptp clock time\n"
138 " -t val shift the ptp clock time by 'val' seconds\n"
139 " -T val set the ptp clock time to 'val' seconds\n"
140 " -z test combinations of rising/falling external time stamp flags\n",
144 int main(int argc, char *argv[])
146 struct ptp_clock_caps caps;
147 struct ptp_extts_event event;
148 struct ptp_extts_request extts_request;
149 struct ptp_perout_request perout_request;
150 struct ptp_pin_desc desc;
153 struct ptp_clock_time *pct;
154 struct ptp_sys_offset *sysoff;
160 char *device = DEVICE;
162 int adjfreq = 0x7fffffff;
164 int capabilities = 0;
173 int pin_index = -1, pin_func;
179 int64_t interval, offset;
181 progname = strrchr(argv[0], '/');
182 progname = progname ? 1+progname : argv[0];
183 while (EOF != (c = getopt(argc, argv, "cd:e:f:ghi:k:lL:p:P:sSt:T:z"))) {
192 extts = atoi(optarg);
195 adjfreq = atoi(optarg);
201 index = atoi(optarg);
205 n_samples = atoi(optarg);
211 cnt = sscanf(optarg, "%d,%d", &pin_index, &pin_func);
218 perout = atoi(optarg);
230 adjtime = atoi(optarg);
234 seconds = atoi(optarg);
249 fd = open(device, O_RDWR);
251 fprintf(stderr, "opening %s: %s\n", device, strerror(errno));
255 clkid = get_clockid(fd);
256 if (CLOCK_INVALID == clkid) {
257 fprintf(stderr, "failed to read clock id\n");
262 if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) {
263 perror("PTP_CLOCK_GETCAPS");
265 printf("capabilities:\n"
266 " %d maximum frequency adjustment (ppb)\n"
267 " %d programmable alarms\n"
268 " %d external time stamp channels\n"
269 " %d programmable periodic signals\n"
270 " %d pulse per second\n"
271 " %d programmable pins\n"
272 " %d cross timestamping\n"
273 " %d adjust_phase\n",
280 caps.cross_timestamping,
285 if (0x7fffffff != adjfreq) {
286 memset(&tx, 0, sizeof(tx));
287 tx.modes = ADJ_FREQUENCY;
288 tx.freq = ppb_to_scaled_ppm(adjfreq);
289 if (clock_adjtime(clkid, &tx)) {
290 perror("clock_adjtime");
292 puts("frequency adjustment okay");
297 memset(&tx, 0, sizeof(tx));
298 tx.modes = ADJ_SETOFFSET;
299 tx.time.tv_sec = adjtime;
301 if (clock_adjtime(clkid, &tx) < 0) {
302 perror("clock_adjtime");
304 puts("time shift okay");
309 if (clock_gettime(clkid, &ts)) {
310 perror("clock_gettime");
312 printf("clock time: %ld.%09ld or %s",
313 ts.tv_sec, ts.tv_nsec, ctime(&ts.tv_sec));
318 clock_gettime(CLOCK_REALTIME, &ts);
319 if (clock_settime(clkid, &ts)) {
320 perror("clock_settime");
322 puts("set time okay");
327 clock_gettime(clkid, &ts);
328 if (clock_settime(CLOCK_REALTIME, &ts)) {
329 perror("clock_settime");
331 puts("set time okay");
338 if (clock_settime(clkid, &ts)) {
339 perror("clock_settime");
341 puts("set time okay");
346 memset(&extts_request, 0, sizeof(extts_request));
347 extts_request.index = index;
348 extts_request.flags = PTP_ENABLE_FEATURE;
349 if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) {
350 perror("PTP_EXTTS_REQUEST");
353 puts("external time stamp request okay");
355 for (; extts; extts--) {
356 cnt = read(fd, &event, sizeof(event));
357 if (cnt != sizeof(event)) {
361 printf("event index %u at %lld.%09u\n", event.index,
362 event.t.sec, event.t.nsec);
365 /* Disable the feature again. */
366 extts_request.flags = 0;
367 if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) {
368 perror("PTP_EXTTS_REQUEST");
373 do_flag_test(fd, index);
378 if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) {
379 perror("PTP_CLOCK_GETCAPS");
381 n_pins = caps.n_pins;
383 for (i = 0; i < n_pins; i++) {
385 if (ioctl(fd, PTP_PIN_GETFUNC, &desc)) {
386 perror("PTP_PIN_GETFUNC");
389 printf("name %s index %u func %u chan %u\n",
390 desc.name, desc.index, desc.func, desc.chan);
395 if (clock_gettime(clkid, &ts)) {
396 perror("clock_gettime");
399 memset(&perout_request, 0, sizeof(perout_request));
400 perout_request.index = index;
401 perout_request.start.sec = ts.tv_sec + 2;
402 perout_request.start.nsec = 0;
403 perout_request.period.sec = 0;
404 perout_request.period.nsec = perout;
405 if (ioctl(fd, PTP_PEROUT_REQUEST, &perout_request)) {
406 perror("PTP_PEROUT_REQUEST");
408 puts("periodic output request okay");
412 if (pin_index >= 0) {
413 memset(&desc, 0, sizeof(desc));
414 desc.index = pin_index;
415 desc.func = pin_func;
417 if (ioctl(fd, PTP_PIN_SETFUNC, &desc)) {
418 perror("PTP_PIN_SETFUNC");
420 puts("set pin function okay");
425 int enable = pps ? 1 : 0;
426 if (ioctl(fd, PTP_ENABLE_PPS, enable)) {
427 perror("PTP_ENABLE_PPS");
429 puts("pps for system time request okay");
434 if (n_samples <= 0 || n_samples > 25) {
435 puts("n_samples should be between 1 and 25");
440 sysoff = calloc(1, sizeof(*sysoff));
445 sysoff->n_samples = n_samples;
447 if (ioctl(fd, PTP_SYS_OFFSET, sysoff))
448 perror("PTP_SYS_OFFSET");
450 puts("system and phc clock time offset request okay");
452 pct = &sysoff->ts[0];
453 for (i = 0; i < sysoff->n_samples; i++) {
455 tp = pctns(pct+2*i+1);
456 t2 = pctns(pct+2*i+2);
458 offset = (t2 + t1) / 2 - tp;
460 printf("system time: %lld.%u\n",
461 (pct+2*i)->sec, (pct+2*i)->nsec);
462 printf("phc time: %lld.%u\n",
463 (pct+2*i+1)->sec, (pct+2*i+1)->nsec);
464 printf("system time: %lld.%u\n",
465 (pct+2*i+2)->sec, (pct+2*i+2)->nsec);
466 printf("system/phc clock time offset is %" PRId64 " ns\n"
467 "system clock time delay is %" PRId64 " ns\n",