1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * PTP 1588 clock support - User space test program
4  *
5  * Copyright (C) 2010 OMICRON electronics GmbH
6  */
7 #define _GNU_SOURCE
8 #define __SANE_USERSPACE_TYPES__        /* For PPC64, to get LL64 types */
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <inttypes.h>
12 #include <math.h>
13 #include <signal.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/ioctl.h>
18 #include <sys/mman.h>
19 #include <sys/stat.h>
20 #include <sys/time.h>
21 #include <sys/timex.h>
22 #include <sys/types.h>
23 #include <time.h>
24 #include <unistd.h>
25 
26 #include <linux/ptp_clock.h>
27 
28 #define DEVICE "/dev/ptp0"
29 
30 #ifndef ADJ_SETOFFSET
31 #define ADJ_SETOFFSET 0x0100
32 #endif
33 
34 #ifndef CLOCK_INVALID
35 #define CLOCK_INVALID -1
36 #endif
37 
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)
42 {
43 	return syscall(__NR_clock_adjtime, id, tx);
44 }
45 #endif
46 
47 static void show_flag_test(int rq_index, unsigned int flags, int err)
48 {
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 ... */
53 	errno = 0;
54 }
55 
56 static void do_flag_test(int fd, unsigned int index)
57 {
58 	struct ptp_extts_request extts_request;
59 	unsigned long request[2] = {
60 		PTP_EXTTS_REQUEST,
61 		PTP_EXTTS_REQUEST2,
62 	};
63 	unsigned int enable_flags[5] = {
64 		PTP_ENABLE_FEATURE,
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),
69 	};
70 	int err, i, j;
71 
72 	memset(&extts_request, 0, sizeof(extts_request));
73 	extts_request.index = index;
74 
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);
80 
81 			extts_request.flags = 0;
82 			err = ioctl(fd, request[i], &extts_request);
83 		}
84 	}
85 }
86 
87 static clockid_t get_clockid(int fd)
88 {
89 #define CLOCKFD 3
90 	return (((unsigned int) ~fd) << 3) | CLOCKFD;
91 }
92 
93 static long ppb_to_scaled_ppm(int ppb)
94 {
95 	/*
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
99 	 *
100 	 *    scaled_ppm = (ppb / 1000) << 16  [1]
101 	 *    scaled_ppm = (ppb << 16) / 1000  [2]
102 	 *
103 	 * we simply use double precision math, in order to avoid the
104 	 * truncation in [1] and the possible overflow in [2].
105 	 */
106 	return (long) (ppb * 65.536);
107 }
108 
109 static int64_t pctns(struct ptp_clock_time *t)
110 {
111 	return t->sec * 1000000000LL + t->nsec;
112 }
113 
114 static void usage(char *progname)
115 {
116 	fprintf(stderr,
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"
131 		"            0 - none\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",
141 		progname);
142 }
143 
144 int main(int argc, char *argv[])
145 {
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;
151 	struct timespec ts;
152 	struct timex tx;
153 	struct ptp_clock_time *pct;
154 	struct ptp_sys_offset *sysoff;
155 
156 	char *progname;
157 	unsigned int i;
158 	int c, cnt, fd;
159 
160 	char *device = DEVICE;
161 	clockid_t clkid;
162 	int adjfreq = 0x7fffffff;
163 	int adjtime = 0;
164 	int capabilities = 0;
165 	int extts = 0;
166 	int flagtest = 0;
167 	int gettime = 0;
168 	int index = 0;
169 	int list_pins = 0;
170 	int pct_offset = 0;
171 	int n_samples = 0;
172 	int perout = -1;
173 	int pin_index = -1, pin_func;
174 	int pps = -1;
175 	int seconds = 0;
176 	int settime = 0;
177 
178 	int64_t t1, t2, tp;
179 	int64_t interval, offset;
180 
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"))) {
184 		switch (c) {
185 		case 'c':
186 			capabilities = 1;
187 			break;
188 		case 'd':
189 			device = optarg;
190 			break;
191 		case 'e':
192 			extts = atoi(optarg);
193 			break;
194 		case 'f':
195 			adjfreq = atoi(optarg);
196 			break;
197 		case 'g':
198 			gettime = 1;
199 			break;
200 		case 'i':
201 			index = atoi(optarg);
202 			break;
203 		case 'k':
204 			pct_offset = 1;
205 			n_samples = atoi(optarg);
206 			break;
207 		case 'l':
208 			list_pins = 1;
209 			break;
210 		case 'L':
211 			cnt = sscanf(optarg, "%d,%d", &pin_index, &pin_func);
212 			if (cnt != 2) {
213 				usage(progname);
214 				return -1;
215 			}
216 			break;
217 		case 'p':
218 			perout = atoi(optarg);
219 			break;
220 		case 'P':
221 			pps = atoi(optarg);
222 			break;
223 		case 's':
224 			settime = 1;
225 			break;
226 		case 'S':
227 			settime = 2;
228 			break;
229 		case 't':
230 			adjtime = atoi(optarg);
231 			break;
232 		case 'T':
233 			settime = 3;
234 			seconds = atoi(optarg);
235 			break;
236 		case 'z':
237 			flagtest = 1;
238 			break;
239 		case 'h':
240 			usage(progname);
241 			return 0;
242 		case '?':
243 		default:
244 			usage(progname);
245 			return -1;
246 		}
247 	}
248 
249 	fd = open(device, O_RDWR);
250 	if (fd < 0) {
251 		fprintf(stderr, "opening %s: %s\n", device, strerror(errno));
252 		return -1;
253 	}
254 
255 	clkid = get_clockid(fd);
256 	if (CLOCK_INVALID == clkid) {
257 		fprintf(stderr, "failed to read clock id\n");
258 		return -1;
259 	}
260 
261 	if (capabilities) {
262 		if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) {
263 			perror("PTP_CLOCK_GETCAPS");
264 		} else {
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",
274 			       caps.max_adj,
275 			       caps.n_alarm,
276 			       caps.n_ext_ts,
277 			       caps.n_per_out,
278 			       caps.pps,
279 			       caps.n_pins,
280 			       caps.cross_timestamping,
281 			       caps.adjust_phase);
282 		}
283 	}
284 
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");
291 		} else {
292 			puts("frequency adjustment okay");
293 		}
294 	}
295 
296 	if (adjtime) {
297 		memset(&tx, 0, sizeof(tx));
298 		tx.modes = ADJ_SETOFFSET;
299 		tx.time.tv_sec = adjtime;
300 		tx.time.tv_usec = 0;
301 		if (clock_adjtime(clkid, &tx) < 0) {
302 			perror("clock_adjtime");
303 		} else {
304 			puts("time shift okay");
305 		}
306 	}
307 
308 	if (gettime) {
309 		if (clock_gettime(clkid, &ts)) {
310 			perror("clock_gettime");
311 		} else {
312 			printf("clock time: %ld.%09ld or %s",
313 			       ts.tv_sec, ts.tv_nsec, ctime(&ts.tv_sec));
314 		}
315 	}
316 
317 	if (settime == 1) {
318 		clock_gettime(CLOCK_REALTIME, &ts);
319 		if (clock_settime(clkid, &ts)) {
320 			perror("clock_settime");
321 		} else {
322 			puts("set time okay");
323 		}
324 	}
325 
326 	if (settime == 2) {
327 		clock_gettime(clkid, &ts);
328 		if (clock_settime(CLOCK_REALTIME, &ts)) {
329 			perror("clock_settime");
330 		} else {
331 			puts("set time okay");
332 		}
333 	}
334 
335 	if (settime == 3) {
336 		ts.tv_sec = seconds;
337 		ts.tv_nsec = 0;
338 		if (clock_settime(clkid, &ts)) {
339 			perror("clock_settime");
340 		} else {
341 			puts("set time okay");
342 		}
343 	}
344 
345 	if (extts) {
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");
351 			extts = 0;
352 		} else {
353 			puts("external time stamp request okay");
354 		}
355 		for (; extts; extts--) {
356 			cnt = read(fd, &event, sizeof(event));
357 			if (cnt != sizeof(event)) {
358 				perror("read");
359 				break;
360 			}
361 			printf("event index %u at %lld.%09u\n", event.index,
362 			       event.t.sec, event.t.nsec);
363 			fflush(stdout);
364 		}
365 		/* Disable the feature again. */
366 		extts_request.flags = 0;
367 		if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) {
368 			perror("PTP_EXTTS_REQUEST");
369 		}
370 	}
371 
372 	if (flagtest) {
373 		do_flag_test(fd, index);
374 	}
375 
376 	if (list_pins) {
377 		int n_pins = 0;
378 		if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) {
379 			perror("PTP_CLOCK_GETCAPS");
380 		} else {
381 			n_pins = caps.n_pins;
382 		}
383 		for (i = 0; i < n_pins; i++) {
384 			desc.index = i;
385 			if (ioctl(fd, PTP_PIN_GETFUNC, &desc)) {
386 				perror("PTP_PIN_GETFUNC");
387 				break;
388 			}
389 			printf("name %s index %u func %u chan %u\n",
390 			       desc.name, desc.index, desc.func, desc.chan);
391 		}
392 	}
393 
394 	if (perout >= 0) {
395 		if (clock_gettime(clkid, &ts)) {
396 			perror("clock_gettime");
397 			return -1;
398 		}
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");
407 		} else {
408 			puts("periodic output request okay");
409 		}
410 	}
411 
412 	if (pin_index >= 0) {
413 		memset(&desc, 0, sizeof(desc));
414 		desc.index = pin_index;
415 		desc.func = pin_func;
416 		desc.chan = index;
417 		if (ioctl(fd, PTP_PIN_SETFUNC, &desc)) {
418 			perror("PTP_PIN_SETFUNC");
419 		} else {
420 			puts("set pin function okay");
421 		}
422 	}
423 
424 	if (pps != -1) {
425 		int enable = pps ? 1 : 0;
426 		if (ioctl(fd, PTP_ENABLE_PPS, enable)) {
427 			perror("PTP_ENABLE_PPS");
428 		} else {
429 			puts("pps for system time request okay");
430 		}
431 	}
432 
433 	if (pct_offset) {
434 		if (n_samples <= 0 || n_samples > 25) {
435 			puts("n_samples should be between 1 and 25");
436 			usage(progname);
437 			return -1;
438 		}
439 
440 		sysoff = calloc(1, sizeof(*sysoff));
441 		if (!sysoff) {
442 			perror("calloc");
443 			return -1;
444 		}
445 		sysoff->n_samples = n_samples;
446 
447 		if (ioctl(fd, PTP_SYS_OFFSET, sysoff))
448 			perror("PTP_SYS_OFFSET");
449 		else
450 			puts("system and phc clock time offset request okay");
451 
452 		pct = &sysoff->ts[0];
453 		for (i = 0; i < sysoff->n_samples; i++) {
454 			t1 = pctns(pct+2*i);
455 			tp = pctns(pct+2*i+1);
456 			t2 = pctns(pct+2*i+2);
457 			interval = t2 - t1;
458 			offset = (t2 + t1) / 2 - tp;
459 
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",
468 				offset, interval);
469 		}
470 
471 		free(sysoff);
472 	}
473 
474 	close(fd);
475 	return 0;
476 }
477