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 #define NSEC_PER_SEC 1000000000LL 39 40 /* clock_adjtime is not available in GLIBC < 2.14 */ 41 #if !__GLIBC_PREREQ(2, 14) 42 #include <sys/syscall.h> 43 static int clock_adjtime(clockid_t id, struct timex *tx) 44 { 45 return syscall(__NR_clock_adjtime, id, tx); 46 } 47 #endif 48 49 static void show_flag_test(int rq_index, unsigned int flags, int err) 50 { 51 printf("PTP_EXTTS_REQUEST%c flags 0x%08x : (%d) %s\n", 52 rq_index ? '1' + rq_index : ' ', 53 flags, err, strerror(errno)); 54 /* sigh, uClibc ... */ 55 errno = 0; 56 } 57 58 static void do_flag_test(int fd, unsigned int index) 59 { 60 struct ptp_extts_request extts_request; 61 unsigned long request[2] = { 62 PTP_EXTTS_REQUEST, 63 PTP_EXTTS_REQUEST2, 64 }; 65 unsigned int enable_flags[5] = { 66 PTP_ENABLE_FEATURE, 67 PTP_ENABLE_FEATURE | PTP_RISING_EDGE, 68 PTP_ENABLE_FEATURE | PTP_FALLING_EDGE, 69 PTP_ENABLE_FEATURE | PTP_RISING_EDGE | PTP_FALLING_EDGE, 70 PTP_ENABLE_FEATURE | (PTP_EXTTS_VALID_FLAGS + 1), 71 }; 72 int err, i, j; 73 74 memset(&extts_request, 0, sizeof(extts_request)); 75 extts_request.index = index; 76 77 for (i = 0; i < 2; i++) { 78 for (j = 0; j < 5; j++) { 79 extts_request.flags = enable_flags[j]; 80 err = ioctl(fd, request[i], &extts_request); 81 show_flag_test(i, extts_request.flags, err); 82 83 extts_request.flags = 0; 84 err = ioctl(fd, request[i], &extts_request); 85 } 86 } 87 } 88 89 static clockid_t get_clockid(int fd) 90 { 91 #define CLOCKFD 3 92 return (((unsigned int) ~fd) << 3) | CLOCKFD; 93 } 94 95 static long ppb_to_scaled_ppm(int ppb) 96 { 97 /* 98 * The 'freq' field in the 'struct timex' is in parts per 99 * million, but with a 16 bit binary fractional field. 100 * Instead of calculating either one of 101 * 102 * scaled_ppm = (ppb / 1000) << 16 [1] 103 * scaled_ppm = (ppb << 16) / 1000 [2] 104 * 105 * we simply use double precision math, in order to avoid the 106 * truncation in [1] and the possible overflow in [2]. 107 */ 108 return (long) (ppb * 65.536); 109 } 110 111 static int64_t pctns(struct ptp_clock_time *t) 112 { 113 return t->sec * NSEC_PER_SEC + t->nsec; 114 } 115 116 static void usage(char *progname) 117 { 118 fprintf(stderr, 119 "usage: %s [options]\n" 120 " -c query the ptp clock's capabilities\n" 121 " -d name device to open\n" 122 " -e val read 'val' external time stamp events\n" 123 " -f val adjust the ptp clock frequency by 'val' ppb\n" 124 " -F chan Enable single channel mask and keep device open for debugfs verification.\n" 125 " -g get the ptp clock time\n" 126 " -h prints this message\n" 127 " -i val index for event/trigger\n" 128 " -k val measure the time offset between system and phc clock\n" 129 " for 'val' times (Maximum 25)\n" 130 " -l list the current pin configuration\n" 131 " -L pin,val configure pin index 'pin' with function 'val'\n" 132 " the channel index is taken from the '-i' option\n" 133 " 'val' specifies the auxiliary function:\n" 134 " 0 - none\n" 135 " 1 - external time stamp\n" 136 " 2 - periodic output\n" 137 " -n val shift the ptp clock time by 'val' nanoseconds\n" 138 " -o val phase offset (in nanoseconds) to be provided to the PHC servo\n" 139 " -p val enable output with a period of 'val' nanoseconds\n" 140 " -H val set output phase to 'val' nanoseconds (requires -p)\n" 141 " -w val set output pulse width to 'val' nanoseconds (requires -p)\n" 142 " -P val enable or disable (val=1|0) the system clock PPS\n" 143 " -r open the ptp clock in readonly mode\n" 144 " -s set the ptp clock time from the system time\n" 145 " -S set the system time from the ptp clock time\n" 146 " -t val shift the ptp clock time by 'val' seconds\n" 147 " -T val set the ptp clock time to 'val' seconds\n" 148 " -x val get an extended ptp clock time with the desired number of samples (up to %d)\n" 149 " -X get a ptp clock cross timestamp\n" 150 " -y val pre/post tstamp timebase to use {realtime|monotonic|monotonic-raw}\n" 151 " -z test combinations of rising/falling external time stamp flags\n", 152 progname, PTP_MAX_SAMPLES); 153 } 154 155 int main(int argc, char *argv[]) 156 { 157 struct ptp_clock_caps caps; 158 struct ptp_extts_event event; 159 struct ptp_extts_request extts_request; 160 struct ptp_perout_request perout_request; 161 struct ptp_pin_desc desc; 162 struct timespec ts; 163 struct timex tx; 164 struct ptp_clock_time *pct; 165 struct ptp_sys_offset *sysoff; 166 struct ptp_sys_offset_extended *soe; 167 struct ptp_sys_offset_precise *xts; 168 169 char *progname; 170 unsigned int i; 171 int c, cnt, fd; 172 173 char *device = DEVICE; 174 clockid_t clkid; 175 int adjfreq = 0x7fffffff; 176 int adjtime = 0; 177 int adjns = 0; 178 int adjphase = 0; 179 int capabilities = 0; 180 int extts = 0; 181 int flagtest = 0; 182 int gettime = 0; 183 int index = 0; 184 int list_pins = 0; 185 int pct_offset = 0; 186 int getextended = 0; 187 int getcross = 0; 188 int n_samples = 0; 189 int pin_index = -1, pin_func; 190 int pps = -1; 191 int seconds = 0; 192 int readonly = 0; 193 int settime = 0; 194 int channel = -1; 195 clockid_t ext_clockid = CLOCK_REALTIME; 196 197 int64_t t1, t2, tp; 198 int64_t interval, offset; 199 int64_t perout_phase = -1; 200 int64_t pulsewidth = -1; 201 int64_t perout = -1; 202 203 progname = strrchr(argv[0], '/'); 204 progname = progname ? 1+progname : argv[0]; 205 while (EOF != (c = getopt(argc, argv, "cd:e:f:F:ghH:i:k:lL:n:o:p:P:rsSt:T:w:x:Xy:z"))) { 206 switch (c) { 207 case 'c': 208 capabilities = 1; 209 break; 210 case 'd': 211 device = optarg; 212 break; 213 case 'e': 214 extts = atoi(optarg); 215 break; 216 case 'f': 217 adjfreq = atoi(optarg); 218 break; 219 case 'F': 220 channel = atoi(optarg); 221 break; 222 case 'g': 223 gettime = 1; 224 break; 225 case 'H': 226 perout_phase = atoll(optarg); 227 break; 228 case 'i': 229 index = atoi(optarg); 230 break; 231 case 'k': 232 pct_offset = 1; 233 n_samples = atoi(optarg); 234 break; 235 case 'l': 236 list_pins = 1; 237 break; 238 case 'L': 239 cnt = sscanf(optarg, "%d,%d", &pin_index, &pin_func); 240 if (cnt != 2) { 241 usage(progname); 242 return -1; 243 } 244 break; 245 case 'n': 246 adjns = atoi(optarg); 247 break; 248 case 'o': 249 adjphase = atoi(optarg); 250 break; 251 case 'p': 252 perout = atoll(optarg); 253 break; 254 case 'P': 255 pps = atoi(optarg); 256 break; 257 case 'r': 258 readonly = 1; 259 break; 260 case 's': 261 settime = 1; 262 break; 263 case 'S': 264 settime = 2; 265 break; 266 case 't': 267 adjtime = atoi(optarg); 268 break; 269 case 'T': 270 settime = 3; 271 seconds = atoi(optarg); 272 break; 273 case 'w': 274 pulsewidth = atoi(optarg); 275 break; 276 case 'x': 277 getextended = atoi(optarg); 278 if (getextended < 1 || getextended > PTP_MAX_SAMPLES) { 279 fprintf(stderr, 280 "number of extended timestamp samples must be between 1 and %d; was asked for %d\n", 281 PTP_MAX_SAMPLES, getextended); 282 return -1; 283 } 284 break; 285 case 'X': 286 getcross = 1; 287 break; 288 case 'y': 289 if (!strcasecmp(optarg, "realtime")) 290 ext_clockid = CLOCK_REALTIME; 291 else if (!strcasecmp(optarg, "monotonic")) 292 ext_clockid = CLOCK_MONOTONIC; 293 else if (!strcasecmp(optarg, "monotonic-raw")) 294 ext_clockid = CLOCK_MONOTONIC_RAW; 295 else { 296 fprintf(stderr, 297 "type needs to be realtime, monotonic or monotonic-raw; was given %s\n", 298 optarg); 299 return -1; 300 } 301 break; 302 303 case 'z': 304 flagtest = 1; 305 break; 306 case 'h': 307 usage(progname); 308 return 0; 309 case '?': 310 default: 311 usage(progname); 312 return -1; 313 } 314 } 315 316 fd = open(device, readonly ? O_RDONLY : O_RDWR); 317 if (fd < 0) { 318 fprintf(stderr, "opening %s: %s\n", device, strerror(errno)); 319 return -1; 320 } 321 322 clkid = get_clockid(fd); 323 if (CLOCK_INVALID == clkid) { 324 fprintf(stderr, "failed to read clock id\n"); 325 return -1; 326 } 327 328 if (capabilities) { 329 if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) { 330 perror("PTP_CLOCK_GETCAPS"); 331 } else { 332 printf("capabilities:\n" 333 " %d maximum frequency adjustment (ppb)\n" 334 " %d programmable alarms\n" 335 " %d external time stamp channels\n" 336 " %d programmable periodic signals\n" 337 " %d pulse per second\n" 338 " %d programmable pins\n" 339 " %d cross timestamping\n" 340 " %d adjust_phase\n" 341 " %d maximum phase adjustment (ns)\n", 342 caps.max_adj, 343 caps.n_alarm, 344 caps.n_ext_ts, 345 caps.n_per_out, 346 caps.pps, 347 caps.n_pins, 348 caps.cross_timestamping, 349 caps.adjust_phase, 350 caps.max_phase_adj); 351 } 352 } 353 354 if (0x7fffffff != adjfreq) { 355 memset(&tx, 0, sizeof(tx)); 356 tx.modes = ADJ_FREQUENCY; 357 tx.freq = ppb_to_scaled_ppm(adjfreq); 358 if (clock_adjtime(clkid, &tx)) { 359 perror("clock_adjtime"); 360 } else { 361 puts("frequency adjustment okay"); 362 } 363 } 364 365 if (adjtime || adjns) { 366 memset(&tx, 0, sizeof(tx)); 367 tx.modes = ADJ_SETOFFSET | ADJ_NANO; 368 tx.time.tv_sec = adjtime; 369 tx.time.tv_usec = adjns; 370 while (tx.time.tv_usec < 0) { 371 tx.time.tv_sec -= 1; 372 tx.time.tv_usec += NSEC_PER_SEC; 373 } 374 375 if (clock_adjtime(clkid, &tx) < 0) { 376 perror("clock_adjtime"); 377 } else { 378 puts("time shift okay"); 379 } 380 } 381 382 if (adjphase) { 383 memset(&tx, 0, sizeof(tx)); 384 tx.modes = ADJ_OFFSET | ADJ_NANO; 385 tx.offset = adjphase; 386 387 if (clock_adjtime(clkid, &tx) < 0) { 388 perror("clock_adjtime"); 389 } else { 390 puts("phase adjustment okay"); 391 } 392 } 393 394 if (gettime) { 395 if (clock_gettime(clkid, &ts)) { 396 perror("clock_gettime"); 397 } else { 398 printf("clock time: %ld.%09ld or %s", 399 ts.tv_sec, ts.tv_nsec, ctime(&ts.tv_sec)); 400 } 401 } 402 403 if (settime == 1) { 404 clock_gettime(CLOCK_REALTIME, &ts); 405 if (clock_settime(clkid, &ts)) { 406 perror("clock_settime"); 407 } else { 408 puts("set time okay"); 409 } 410 } 411 412 if (settime == 2) { 413 clock_gettime(clkid, &ts); 414 if (clock_settime(CLOCK_REALTIME, &ts)) { 415 perror("clock_settime"); 416 } else { 417 puts("set time okay"); 418 } 419 } 420 421 if (settime == 3) { 422 ts.tv_sec = seconds; 423 ts.tv_nsec = 0; 424 if (clock_settime(clkid, &ts)) { 425 perror("clock_settime"); 426 } else { 427 puts("set time okay"); 428 } 429 } 430 431 if (pin_index >= 0) { 432 memset(&desc, 0, sizeof(desc)); 433 desc.index = pin_index; 434 desc.func = pin_func; 435 desc.chan = index; 436 if (ioctl(fd, PTP_PIN_SETFUNC, &desc)) { 437 perror("PTP_PIN_SETFUNC"); 438 } else { 439 puts("set pin function okay"); 440 } 441 } 442 443 if (extts) { 444 if (!readonly) { 445 memset(&extts_request, 0, sizeof(extts_request)); 446 extts_request.index = index; 447 extts_request.flags = PTP_ENABLE_FEATURE; 448 if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) { 449 perror("PTP_EXTTS_REQUEST"); 450 extts = 0; 451 } else { 452 puts("external time stamp request okay"); 453 } 454 } 455 for (; extts; extts--) { 456 cnt = read(fd, &event, sizeof(event)); 457 if (cnt != sizeof(event)) { 458 perror("read"); 459 break; 460 } 461 printf("event index %u at %lld.%09u\n", event.index, 462 event.t.sec, event.t.nsec); 463 fflush(stdout); 464 } 465 if (!readonly) { 466 /* Disable the feature again. */ 467 extts_request.flags = 0; 468 if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) { 469 perror("PTP_EXTTS_REQUEST"); 470 } 471 } 472 } 473 474 if (flagtest) { 475 do_flag_test(fd, index); 476 } 477 478 if (list_pins) { 479 int n_pins = 0; 480 if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) { 481 perror("PTP_CLOCK_GETCAPS"); 482 } else { 483 n_pins = caps.n_pins; 484 } 485 for (i = 0; i < n_pins; i++) { 486 desc.index = i; 487 if (ioctl(fd, PTP_PIN_GETFUNC, &desc)) { 488 perror("PTP_PIN_GETFUNC"); 489 break; 490 } 491 printf("name %s index %u func %u chan %u\n", 492 desc.name, desc.index, desc.func, desc.chan); 493 } 494 } 495 496 if (pulsewidth >= 0 && perout < 0) { 497 puts("-w can only be specified together with -p"); 498 return -1; 499 } 500 501 if (perout_phase >= 0 && perout < 0) { 502 puts("-H can only be specified together with -p"); 503 return -1; 504 } 505 506 if (perout >= 0) { 507 if (clock_gettime(clkid, &ts)) { 508 perror("clock_gettime"); 509 return -1; 510 } 511 memset(&perout_request, 0, sizeof(perout_request)); 512 perout_request.index = index; 513 perout_request.period.sec = perout / NSEC_PER_SEC; 514 perout_request.period.nsec = perout % NSEC_PER_SEC; 515 perout_request.flags = 0; 516 if (pulsewidth >= 0) { 517 perout_request.flags |= PTP_PEROUT_DUTY_CYCLE; 518 perout_request.on.sec = pulsewidth / NSEC_PER_SEC; 519 perout_request.on.nsec = pulsewidth % NSEC_PER_SEC; 520 } 521 if (perout_phase >= 0) { 522 perout_request.flags |= PTP_PEROUT_PHASE; 523 perout_request.phase.sec = perout_phase / NSEC_PER_SEC; 524 perout_request.phase.nsec = perout_phase % NSEC_PER_SEC; 525 } else { 526 perout_request.start.sec = ts.tv_sec + 2; 527 perout_request.start.nsec = 0; 528 } 529 530 if (ioctl(fd, PTP_PEROUT_REQUEST2, &perout_request)) { 531 perror("PTP_PEROUT_REQUEST"); 532 } else { 533 puts("periodic output request okay"); 534 } 535 } 536 537 if (pps != -1) { 538 int enable = pps ? 1 : 0; 539 if (ioctl(fd, PTP_ENABLE_PPS, enable)) { 540 perror("PTP_ENABLE_PPS"); 541 } else { 542 puts("pps for system time request okay"); 543 } 544 } 545 546 if (pct_offset) { 547 if (n_samples <= 0 || n_samples > 25) { 548 puts("n_samples should be between 1 and 25"); 549 usage(progname); 550 return -1; 551 } 552 553 sysoff = calloc(1, sizeof(*sysoff)); 554 if (!sysoff) { 555 perror("calloc"); 556 return -1; 557 } 558 sysoff->n_samples = n_samples; 559 560 if (ioctl(fd, PTP_SYS_OFFSET, sysoff)) 561 perror("PTP_SYS_OFFSET"); 562 else 563 puts("system and phc clock time offset request okay"); 564 565 pct = &sysoff->ts[0]; 566 for (i = 0; i < sysoff->n_samples; i++) { 567 t1 = pctns(pct+2*i); 568 tp = pctns(pct+2*i+1); 569 t2 = pctns(pct+2*i+2); 570 interval = t2 - t1; 571 offset = (t2 + t1) / 2 - tp; 572 573 printf("system time: %lld.%09u\n", 574 (pct+2*i)->sec, (pct+2*i)->nsec); 575 printf("phc time: %lld.%09u\n", 576 (pct+2*i+1)->sec, (pct+2*i+1)->nsec); 577 printf("system time: %lld.%09u\n", 578 (pct+2*i+2)->sec, (pct+2*i+2)->nsec); 579 printf("system/phc clock time offset is %" PRId64 " ns\n" 580 "system clock time delay is %" PRId64 " ns\n", 581 offset, interval); 582 } 583 584 free(sysoff); 585 } 586 587 if (getextended) { 588 soe = calloc(1, sizeof(*soe)); 589 if (!soe) { 590 perror("calloc"); 591 return -1; 592 } 593 594 soe->n_samples = getextended; 595 soe->clockid = ext_clockid; 596 597 if (ioctl(fd, PTP_SYS_OFFSET_EXTENDED, soe)) { 598 perror("PTP_SYS_OFFSET_EXTENDED"); 599 } else { 600 printf("extended timestamp request returned %d samples\n", 601 getextended); 602 603 for (i = 0; i < getextended; i++) { 604 switch (ext_clockid) { 605 case CLOCK_REALTIME: 606 printf("sample #%2d: real time before: %lld.%09u\n", 607 i, soe->ts[i][0].sec, 608 soe->ts[i][0].nsec); 609 break; 610 case CLOCK_MONOTONIC: 611 printf("sample #%2d: monotonic time before: %lld.%09u\n", 612 i, soe->ts[i][0].sec, 613 soe->ts[i][0].nsec); 614 break; 615 case CLOCK_MONOTONIC_RAW: 616 printf("sample #%2d: monotonic-raw time before: %lld.%09u\n", 617 i, soe->ts[i][0].sec, 618 soe->ts[i][0].nsec); 619 break; 620 default: 621 break; 622 } 623 printf(" phc time: %lld.%09u\n", 624 soe->ts[i][1].sec, soe->ts[i][1].nsec); 625 switch (ext_clockid) { 626 case CLOCK_REALTIME: 627 printf(" real time after: %lld.%09u\n", 628 soe->ts[i][2].sec, 629 soe->ts[i][2].nsec); 630 break; 631 case CLOCK_MONOTONIC: 632 printf(" monotonic time after: %lld.%09u\n", 633 soe->ts[i][2].sec, 634 soe->ts[i][2].nsec); 635 break; 636 case CLOCK_MONOTONIC_RAW: 637 printf(" monotonic-raw time after: %lld.%09u\n", 638 soe->ts[i][2].sec, 639 soe->ts[i][2].nsec); 640 break; 641 default: 642 break; 643 } 644 } 645 } 646 647 free(soe); 648 } 649 650 if (getcross) { 651 xts = calloc(1, sizeof(*xts)); 652 if (!xts) { 653 perror("calloc"); 654 return -1; 655 } 656 657 if (ioctl(fd, PTP_SYS_OFFSET_PRECISE, xts)) { 658 perror("PTP_SYS_OFFSET_PRECISE"); 659 } else { 660 puts("system and phc crosstimestamping request okay"); 661 662 printf("device time: %lld.%09u\n", 663 xts->device.sec, xts->device.nsec); 664 printf("system time: %lld.%09u\n", 665 xts->sys_realtime.sec, xts->sys_realtime.nsec); 666 printf("monoraw time: %lld.%09u\n", 667 xts->sys_monoraw.sec, xts->sys_monoraw.nsec); 668 } 669 670 free(xts); 671 } 672 673 if (channel >= 0) { 674 if (ioctl(fd, PTP_MASK_CLEAR_ALL)) { 675 perror("PTP_MASK_CLEAR_ALL"); 676 } else if (ioctl(fd, PTP_MASK_EN_SINGLE, (unsigned int *)&channel)) { 677 perror("PTP_MASK_EN_SINGLE"); 678 } else { 679 printf("Channel %d exclusively enabled. Check on debugfs.\n", channel); 680 printf("Press any key to continue\n."); 681 getchar(); 682 } 683 } 684 685 close(fd); 686 return 0; 687 } 688