174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 28dbbf854SShuah Khan /* 38dbbf854SShuah Khan * PTP 1588 clock support - User space test program 48dbbf854SShuah Khan * 58dbbf854SShuah Khan * Copyright (C) 2010 OMICRON electronics GmbH 68dbbf854SShuah Khan */ 78dbbf854SShuah Khan #define _GNU_SOURCE 88dbbf854SShuah Khan #define __SANE_USERSPACE_TYPES__ /* For PPC64, to get LL64 types */ 98dbbf854SShuah Khan #include <errno.h> 108dbbf854SShuah Khan #include <fcntl.h> 118dbbf854SShuah Khan #include <inttypes.h> 128dbbf854SShuah Khan #include <math.h> 138dbbf854SShuah Khan #include <signal.h> 148dbbf854SShuah Khan #include <stdio.h> 158dbbf854SShuah Khan #include <stdlib.h> 168dbbf854SShuah Khan #include <string.h> 178dbbf854SShuah Khan #include <sys/ioctl.h> 188dbbf854SShuah Khan #include <sys/mman.h> 198dbbf854SShuah Khan #include <sys/stat.h> 208dbbf854SShuah Khan #include <sys/time.h> 218dbbf854SShuah Khan #include <sys/timex.h> 228dbbf854SShuah Khan #include <sys/types.h> 238dbbf854SShuah Khan #include <time.h> 248dbbf854SShuah Khan #include <unistd.h> 258dbbf854SShuah Khan 268dbbf854SShuah Khan #include <linux/ptp_clock.h> 278dbbf854SShuah Khan 288dbbf854SShuah Khan #define DEVICE "/dev/ptp0" 298dbbf854SShuah Khan 308dbbf854SShuah Khan #ifndef ADJ_SETOFFSET 318dbbf854SShuah Khan #define ADJ_SETOFFSET 0x0100 328dbbf854SShuah Khan #endif 338dbbf854SShuah Khan 348dbbf854SShuah Khan #ifndef CLOCK_INVALID 358dbbf854SShuah Khan #define CLOCK_INVALID -1 368dbbf854SShuah Khan #endif 378dbbf854SShuah Khan 384a09a981SVladimir Oltean #define NSEC_PER_SEC 1000000000LL 394a09a981SVladimir Oltean 408dbbf854SShuah Khan /* clock_adjtime is not available in GLIBC < 2.14 */ 418dbbf854SShuah Khan #if !__GLIBC_PREREQ(2, 14) 428dbbf854SShuah Khan #include <sys/syscall.h> 438dbbf854SShuah Khan static int clock_adjtime(clockid_t id, struct timex *tx) 448dbbf854SShuah Khan { 458dbbf854SShuah Khan return syscall(__NR_clock_adjtime, id, tx); 468dbbf854SShuah Khan } 478dbbf854SShuah Khan #endif 488dbbf854SShuah Khan 496eb54cbbSRichard Cochran static void show_flag_test(int rq_index, unsigned int flags, int err) 506eb54cbbSRichard Cochran { 516eb54cbbSRichard Cochran printf("PTP_EXTTS_REQUEST%c flags 0x%08x : (%d) %s\n", 526eb54cbbSRichard Cochran rq_index ? '1' + rq_index : ' ', 536eb54cbbSRichard Cochran flags, err, strerror(errno)); 546eb54cbbSRichard Cochran /* sigh, uClibc ... */ 556eb54cbbSRichard Cochran errno = 0; 566eb54cbbSRichard Cochran } 576eb54cbbSRichard Cochran 586eb54cbbSRichard Cochran static void do_flag_test(int fd, unsigned int index) 596eb54cbbSRichard Cochran { 606eb54cbbSRichard Cochran struct ptp_extts_request extts_request; 616eb54cbbSRichard Cochran unsigned long request[2] = { 626eb54cbbSRichard Cochran PTP_EXTTS_REQUEST, 636eb54cbbSRichard Cochran PTP_EXTTS_REQUEST2, 646eb54cbbSRichard Cochran }; 656eb54cbbSRichard Cochran unsigned int enable_flags[5] = { 666eb54cbbSRichard Cochran PTP_ENABLE_FEATURE, 676eb54cbbSRichard Cochran PTP_ENABLE_FEATURE | PTP_RISING_EDGE, 686eb54cbbSRichard Cochran PTP_ENABLE_FEATURE | PTP_FALLING_EDGE, 696eb54cbbSRichard Cochran PTP_ENABLE_FEATURE | PTP_RISING_EDGE | PTP_FALLING_EDGE, 706eb54cbbSRichard Cochran PTP_ENABLE_FEATURE | (PTP_EXTTS_VALID_FLAGS + 1), 716eb54cbbSRichard Cochran }; 726eb54cbbSRichard Cochran int err, i, j; 736eb54cbbSRichard Cochran 746eb54cbbSRichard Cochran memset(&extts_request, 0, sizeof(extts_request)); 756eb54cbbSRichard Cochran extts_request.index = index; 766eb54cbbSRichard Cochran 776eb54cbbSRichard Cochran for (i = 0; i < 2; i++) { 786eb54cbbSRichard Cochran for (j = 0; j < 5; j++) { 796eb54cbbSRichard Cochran extts_request.flags = enable_flags[j]; 806eb54cbbSRichard Cochran err = ioctl(fd, request[i], &extts_request); 816eb54cbbSRichard Cochran show_flag_test(i, extts_request.flags, err); 826eb54cbbSRichard Cochran 836eb54cbbSRichard Cochran extts_request.flags = 0; 846eb54cbbSRichard Cochran err = ioctl(fd, request[i], &extts_request); 856eb54cbbSRichard Cochran } 866eb54cbbSRichard Cochran } 876eb54cbbSRichard Cochran } 886eb54cbbSRichard Cochran 898dbbf854SShuah Khan static clockid_t get_clockid(int fd) 908dbbf854SShuah Khan { 918dbbf854SShuah Khan #define CLOCKFD 3 9229f1b2b0SNick Desaulniers return (((unsigned int) ~fd) << 3) | CLOCKFD; 938dbbf854SShuah Khan } 948dbbf854SShuah Khan 958dbbf854SShuah Khan static long ppb_to_scaled_ppm(int ppb) 968dbbf854SShuah Khan { 978dbbf854SShuah Khan /* 988dbbf854SShuah Khan * The 'freq' field in the 'struct timex' is in parts per 998dbbf854SShuah Khan * million, but with a 16 bit binary fractional field. 1008dbbf854SShuah Khan * Instead of calculating either one of 1018dbbf854SShuah Khan * 1028dbbf854SShuah Khan * scaled_ppm = (ppb / 1000) << 16 [1] 1038dbbf854SShuah Khan * scaled_ppm = (ppb << 16) / 1000 [2] 1048dbbf854SShuah Khan * 1058dbbf854SShuah Khan * we simply use double precision math, in order to avoid the 1068dbbf854SShuah Khan * truncation in [1] and the possible overflow in [2]. 1078dbbf854SShuah Khan */ 1088dbbf854SShuah Khan return (long) (ppb * 65.536); 1098dbbf854SShuah Khan } 1108dbbf854SShuah Khan 1118dbbf854SShuah Khan static int64_t pctns(struct ptp_clock_time *t) 1128dbbf854SShuah Khan { 1138dbbf854SShuah Khan return t->sec * 1000000000LL + t->nsec; 1148dbbf854SShuah Khan } 1158dbbf854SShuah Khan 1168dbbf854SShuah Khan static void usage(char *progname) 1178dbbf854SShuah Khan { 1188dbbf854SShuah Khan fprintf(stderr, 1198dbbf854SShuah Khan "usage: %s [options]\n" 1208dbbf854SShuah Khan " -c query the ptp clock's capabilities\n" 1218dbbf854SShuah Khan " -d name device to open\n" 1228dbbf854SShuah Khan " -e val read 'val' external time stamp events\n" 1238dbbf854SShuah Khan " -f val adjust the ptp clock frequency by 'val' ppb\n" 1248dbbf854SShuah Khan " -g get the ptp clock time\n" 1258dbbf854SShuah Khan " -h prints this message\n" 1268dbbf854SShuah Khan " -i val index for event/trigger\n" 1278dbbf854SShuah Khan " -k val measure the time offset between system and phc clock\n" 1288dbbf854SShuah Khan " for 'val' times (Maximum 25)\n" 1298dbbf854SShuah Khan " -l list the current pin configuration\n" 1308dbbf854SShuah Khan " -L pin,val configure pin index 'pin' with function 'val'\n" 1318dbbf854SShuah Khan " the channel index is taken from the '-i' option\n" 1328dbbf854SShuah Khan " 'val' specifies the auxiliary function:\n" 1338dbbf854SShuah Khan " 0 - none\n" 1348dbbf854SShuah Khan " 1 - external time stamp\n" 1358dbbf854SShuah Khan " 2 - periodic output\n" 1368dbbf854SShuah Khan " -p val enable output with a period of 'val' nanoseconds\n" 137*7570ebe0SVladimir Oltean " -H val set output phase to 'val' nanoseconds (requires -p)\n" 138*7570ebe0SVladimir Oltean " -w val set output pulse width to 'val' nanoseconds (requires -p)\n" 1398dbbf854SShuah Khan " -P val enable or disable (val=1|0) the system clock PPS\n" 1408dbbf854SShuah Khan " -s set the ptp clock time from the system time\n" 1418dbbf854SShuah Khan " -S set the system time from the ptp clock time\n" 1428dbbf854SShuah Khan " -t val shift the ptp clock time by 'val' seconds\n" 1436eb54cbbSRichard Cochran " -T val set the ptp clock time to 'val' seconds\n" 1446eb54cbbSRichard Cochran " -z test combinations of rising/falling external time stamp flags\n", 1458dbbf854SShuah Khan progname); 1468dbbf854SShuah Khan } 1478dbbf854SShuah Khan 1488dbbf854SShuah Khan int main(int argc, char *argv[]) 1498dbbf854SShuah Khan { 1508dbbf854SShuah Khan struct ptp_clock_caps caps; 1518dbbf854SShuah Khan struct ptp_extts_event event; 1528dbbf854SShuah Khan struct ptp_extts_request extts_request; 1538dbbf854SShuah Khan struct ptp_perout_request perout_request; 1548dbbf854SShuah Khan struct ptp_pin_desc desc; 1558dbbf854SShuah Khan struct timespec ts; 1568dbbf854SShuah Khan struct timex tx; 1578dbbf854SShuah Khan struct ptp_clock_time *pct; 1588dbbf854SShuah Khan struct ptp_sys_offset *sysoff; 1598dbbf854SShuah Khan 1608dbbf854SShuah Khan char *progname; 1618dbbf854SShuah Khan unsigned int i; 1628dbbf854SShuah Khan int c, cnt, fd; 1638dbbf854SShuah Khan 1648dbbf854SShuah Khan char *device = DEVICE; 1658dbbf854SShuah Khan clockid_t clkid; 1668dbbf854SShuah Khan int adjfreq = 0x7fffffff; 1678dbbf854SShuah Khan int adjtime = 0; 1688dbbf854SShuah Khan int capabilities = 0; 1698dbbf854SShuah Khan int extts = 0; 1706eb54cbbSRichard Cochran int flagtest = 0; 1718dbbf854SShuah Khan int gettime = 0; 1728dbbf854SShuah Khan int index = 0; 1738dbbf854SShuah Khan int list_pins = 0; 1748dbbf854SShuah Khan int pct_offset = 0; 1758dbbf854SShuah Khan int n_samples = 0; 1768dbbf854SShuah Khan int pin_index = -1, pin_func; 1778dbbf854SShuah Khan int pps = -1; 1788dbbf854SShuah Khan int seconds = 0; 1798dbbf854SShuah Khan int settime = 0; 1808dbbf854SShuah Khan 1818dbbf854SShuah Khan int64_t t1, t2, tp; 1828dbbf854SShuah Khan int64_t interval, offset; 183*7570ebe0SVladimir Oltean int64_t perout_phase = -1; 184*7570ebe0SVladimir Oltean int64_t pulsewidth = -1; 1854a09a981SVladimir Oltean int64_t perout = -1; 1868dbbf854SShuah Khan 1878dbbf854SShuah Khan progname = strrchr(argv[0], '/'); 1888dbbf854SShuah Khan progname = progname ? 1+progname : argv[0]; 189*7570ebe0SVladimir Oltean while (EOF != (c = getopt(argc, argv, "cd:e:f:ghH:i:k:lL:p:P:sSt:T:w:z"))) { 1908dbbf854SShuah Khan switch (c) { 1918dbbf854SShuah Khan case 'c': 1928dbbf854SShuah Khan capabilities = 1; 1938dbbf854SShuah Khan break; 1948dbbf854SShuah Khan case 'd': 1958dbbf854SShuah Khan device = optarg; 1968dbbf854SShuah Khan break; 1978dbbf854SShuah Khan case 'e': 1988dbbf854SShuah Khan extts = atoi(optarg); 1998dbbf854SShuah Khan break; 2008dbbf854SShuah Khan case 'f': 2018dbbf854SShuah Khan adjfreq = atoi(optarg); 2028dbbf854SShuah Khan break; 2038dbbf854SShuah Khan case 'g': 2048dbbf854SShuah Khan gettime = 1; 2058dbbf854SShuah Khan break; 206*7570ebe0SVladimir Oltean case 'H': 207*7570ebe0SVladimir Oltean perout_phase = atoll(optarg); 208*7570ebe0SVladimir Oltean break; 2098dbbf854SShuah Khan case 'i': 2108dbbf854SShuah Khan index = atoi(optarg); 2118dbbf854SShuah Khan break; 2128dbbf854SShuah Khan case 'k': 2138dbbf854SShuah Khan pct_offset = 1; 2148dbbf854SShuah Khan n_samples = atoi(optarg); 2158dbbf854SShuah Khan break; 2168dbbf854SShuah Khan case 'l': 2178dbbf854SShuah Khan list_pins = 1; 2188dbbf854SShuah Khan break; 2198dbbf854SShuah Khan case 'L': 2208dbbf854SShuah Khan cnt = sscanf(optarg, "%d,%d", &pin_index, &pin_func); 2218dbbf854SShuah Khan if (cnt != 2) { 2228dbbf854SShuah Khan usage(progname); 2238dbbf854SShuah Khan return -1; 2248dbbf854SShuah Khan } 2258dbbf854SShuah Khan break; 2268dbbf854SShuah Khan case 'p': 2274a09a981SVladimir Oltean perout = atoll(optarg); 2288dbbf854SShuah Khan break; 2298dbbf854SShuah Khan case 'P': 2308dbbf854SShuah Khan pps = atoi(optarg); 2318dbbf854SShuah Khan break; 2328dbbf854SShuah Khan case 's': 2338dbbf854SShuah Khan settime = 1; 2348dbbf854SShuah Khan break; 2358dbbf854SShuah Khan case 'S': 2368dbbf854SShuah Khan settime = 2; 2378dbbf854SShuah Khan break; 2388dbbf854SShuah Khan case 't': 2398dbbf854SShuah Khan adjtime = atoi(optarg); 2408dbbf854SShuah Khan break; 2418dbbf854SShuah Khan case 'T': 2428dbbf854SShuah Khan settime = 3; 2438dbbf854SShuah Khan seconds = atoi(optarg); 2448dbbf854SShuah Khan break; 245*7570ebe0SVladimir Oltean case 'w': 246*7570ebe0SVladimir Oltean pulsewidth = atoi(optarg); 247*7570ebe0SVladimir Oltean break; 2486eb54cbbSRichard Cochran case 'z': 2496eb54cbbSRichard Cochran flagtest = 1; 2506eb54cbbSRichard Cochran break; 2518dbbf854SShuah Khan case 'h': 2528dbbf854SShuah Khan usage(progname); 2538dbbf854SShuah Khan return 0; 2548dbbf854SShuah Khan case '?': 2558dbbf854SShuah Khan default: 2568dbbf854SShuah Khan usage(progname); 2578dbbf854SShuah Khan return -1; 2588dbbf854SShuah Khan } 2598dbbf854SShuah Khan } 2608dbbf854SShuah Khan 2618dbbf854SShuah Khan fd = open(device, O_RDWR); 2628dbbf854SShuah Khan if (fd < 0) { 2638dbbf854SShuah Khan fprintf(stderr, "opening %s: %s\n", device, strerror(errno)); 2648dbbf854SShuah Khan return -1; 2658dbbf854SShuah Khan } 2668dbbf854SShuah Khan 2678dbbf854SShuah Khan clkid = get_clockid(fd); 2688dbbf854SShuah Khan if (CLOCK_INVALID == clkid) { 2698dbbf854SShuah Khan fprintf(stderr, "failed to read clock id\n"); 2708dbbf854SShuah Khan return -1; 2718dbbf854SShuah Khan } 2728dbbf854SShuah Khan 2738dbbf854SShuah Khan if (capabilities) { 2748dbbf854SShuah Khan if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) { 2758dbbf854SShuah Khan perror("PTP_CLOCK_GETCAPS"); 2768dbbf854SShuah Khan } else { 2778dbbf854SShuah Khan printf("capabilities:\n" 2788dbbf854SShuah Khan " %d maximum frequency adjustment (ppb)\n" 2798dbbf854SShuah Khan " %d programmable alarms\n" 2808dbbf854SShuah Khan " %d external time stamp channels\n" 2818dbbf854SShuah Khan " %d programmable periodic signals\n" 2828dbbf854SShuah Khan " %d pulse per second\n" 2838dbbf854SShuah Khan " %d programmable pins\n" 284d3f1cbd2SVincent Cheng " %d cross timestamping\n" 285d3f1cbd2SVincent Cheng " %d adjust_phase\n", 2868dbbf854SShuah Khan caps.max_adj, 2878dbbf854SShuah Khan caps.n_alarm, 2888dbbf854SShuah Khan caps.n_ext_ts, 2898dbbf854SShuah Khan caps.n_per_out, 2908dbbf854SShuah Khan caps.pps, 2918dbbf854SShuah Khan caps.n_pins, 292d3f1cbd2SVincent Cheng caps.cross_timestamping, 293d3f1cbd2SVincent Cheng caps.adjust_phase); 2948dbbf854SShuah Khan } 2958dbbf854SShuah Khan } 2968dbbf854SShuah Khan 2978dbbf854SShuah Khan if (0x7fffffff != adjfreq) { 2988dbbf854SShuah Khan memset(&tx, 0, sizeof(tx)); 2998dbbf854SShuah Khan tx.modes = ADJ_FREQUENCY; 3008dbbf854SShuah Khan tx.freq = ppb_to_scaled_ppm(adjfreq); 3018dbbf854SShuah Khan if (clock_adjtime(clkid, &tx)) { 3028dbbf854SShuah Khan perror("clock_adjtime"); 3038dbbf854SShuah Khan } else { 3048dbbf854SShuah Khan puts("frequency adjustment okay"); 3058dbbf854SShuah Khan } 3068dbbf854SShuah Khan } 3078dbbf854SShuah Khan 3088dbbf854SShuah Khan if (adjtime) { 3098dbbf854SShuah Khan memset(&tx, 0, sizeof(tx)); 3108dbbf854SShuah Khan tx.modes = ADJ_SETOFFSET; 3118dbbf854SShuah Khan tx.time.tv_sec = adjtime; 3128dbbf854SShuah Khan tx.time.tv_usec = 0; 3138dbbf854SShuah Khan if (clock_adjtime(clkid, &tx) < 0) { 3148dbbf854SShuah Khan perror("clock_adjtime"); 3158dbbf854SShuah Khan } else { 3168dbbf854SShuah Khan puts("time shift okay"); 3178dbbf854SShuah Khan } 3188dbbf854SShuah Khan } 3198dbbf854SShuah Khan 3208dbbf854SShuah Khan if (gettime) { 3218dbbf854SShuah Khan if (clock_gettime(clkid, &ts)) { 3228dbbf854SShuah Khan perror("clock_gettime"); 3238dbbf854SShuah Khan } else { 3248dbbf854SShuah Khan printf("clock time: %ld.%09ld or %s", 3258dbbf854SShuah Khan ts.tv_sec, ts.tv_nsec, ctime(&ts.tv_sec)); 3268dbbf854SShuah Khan } 3278dbbf854SShuah Khan } 3288dbbf854SShuah Khan 3298dbbf854SShuah Khan if (settime == 1) { 3308dbbf854SShuah Khan clock_gettime(CLOCK_REALTIME, &ts); 3318dbbf854SShuah Khan if (clock_settime(clkid, &ts)) { 3328dbbf854SShuah Khan perror("clock_settime"); 3338dbbf854SShuah Khan } else { 3348dbbf854SShuah Khan puts("set time okay"); 3358dbbf854SShuah Khan } 3368dbbf854SShuah Khan } 3378dbbf854SShuah Khan 3388dbbf854SShuah Khan if (settime == 2) { 3398dbbf854SShuah Khan clock_gettime(clkid, &ts); 3408dbbf854SShuah Khan if (clock_settime(CLOCK_REALTIME, &ts)) { 3418dbbf854SShuah Khan perror("clock_settime"); 3428dbbf854SShuah Khan } else { 3438dbbf854SShuah Khan puts("set time okay"); 3448dbbf854SShuah Khan } 3458dbbf854SShuah Khan } 3468dbbf854SShuah Khan 3478dbbf854SShuah Khan if (settime == 3) { 3488dbbf854SShuah Khan ts.tv_sec = seconds; 3498dbbf854SShuah Khan ts.tv_nsec = 0; 3508dbbf854SShuah Khan if (clock_settime(clkid, &ts)) { 3518dbbf854SShuah Khan perror("clock_settime"); 3528dbbf854SShuah Khan } else { 3538dbbf854SShuah Khan puts("set time okay"); 3548dbbf854SShuah Khan } 3558dbbf854SShuah Khan } 3568dbbf854SShuah Khan 3578dbbf854SShuah Khan if (extts) { 3588dbbf854SShuah Khan memset(&extts_request, 0, sizeof(extts_request)); 3598dbbf854SShuah Khan extts_request.index = index; 3608dbbf854SShuah Khan extts_request.flags = PTP_ENABLE_FEATURE; 3618dbbf854SShuah Khan if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) { 3628dbbf854SShuah Khan perror("PTP_EXTTS_REQUEST"); 3638dbbf854SShuah Khan extts = 0; 3648dbbf854SShuah Khan } else { 3658dbbf854SShuah Khan puts("external time stamp request okay"); 3668dbbf854SShuah Khan } 3678dbbf854SShuah Khan for (; extts; extts--) { 3688dbbf854SShuah Khan cnt = read(fd, &event, sizeof(event)); 3698dbbf854SShuah Khan if (cnt != sizeof(event)) { 3708dbbf854SShuah Khan perror("read"); 3718dbbf854SShuah Khan break; 3728dbbf854SShuah Khan } 3738dbbf854SShuah Khan printf("event index %u at %lld.%09u\n", event.index, 3748dbbf854SShuah Khan event.t.sec, event.t.nsec); 3758dbbf854SShuah Khan fflush(stdout); 3768dbbf854SShuah Khan } 3778dbbf854SShuah Khan /* Disable the feature again. */ 3788dbbf854SShuah Khan extts_request.flags = 0; 3798dbbf854SShuah Khan if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) { 3808dbbf854SShuah Khan perror("PTP_EXTTS_REQUEST"); 3818dbbf854SShuah Khan } 3828dbbf854SShuah Khan } 3838dbbf854SShuah Khan 3846eb54cbbSRichard Cochran if (flagtest) { 3856eb54cbbSRichard Cochran do_flag_test(fd, index); 3866eb54cbbSRichard Cochran } 3876eb54cbbSRichard Cochran 3888dbbf854SShuah Khan if (list_pins) { 3898dbbf854SShuah Khan int n_pins = 0; 3908dbbf854SShuah Khan if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) { 3918dbbf854SShuah Khan perror("PTP_CLOCK_GETCAPS"); 3928dbbf854SShuah Khan } else { 3938dbbf854SShuah Khan n_pins = caps.n_pins; 3948dbbf854SShuah Khan } 3958dbbf854SShuah Khan for (i = 0; i < n_pins; i++) { 3968dbbf854SShuah Khan desc.index = i; 3978dbbf854SShuah Khan if (ioctl(fd, PTP_PIN_GETFUNC, &desc)) { 3988dbbf854SShuah Khan perror("PTP_PIN_GETFUNC"); 3998dbbf854SShuah Khan break; 4008dbbf854SShuah Khan } 4018dbbf854SShuah Khan printf("name %s index %u func %u chan %u\n", 4028dbbf854SShuah Khan desc.name, desc.index, desc.func, desc.chan); 4038dbbf854SShuah Khan } 4048dbbf854SShuah Khan } 4058dbbf854SShuah Khan 406*7570ebe0SVladimir Oltean if (pulsewidth >= 0 && perout < 0) { 407*7570ebe0SVladimir Oltean puts("-w can only be specified together with -p"); 408*7570ebe0SVladimir Oltean return -1; 409*7570ebe0SVladimir Oltean } 410*7570ebe0SVladimir Oltean 411*7570ebe0SVladimir Oltean if (perout_phase >= 0 && perout < 0) { 412*7570ebe0SVladimir Oltean puts("-H can only be specified together with -p"); 413*7570ebe0SVladimir Oltean return -1; 414*7570ebe0SVladimir Oltean } 415*7570ebe0SVladimir Oltean 4168dbbf854SShuah Khan if (perout >= 0) { 4178dbbf854SShuah Khan if (clock_gettime(clkid, &ts)) { 4188dbbf854SShuah Khan perror("clock_gettime"); 4198dbbf854SShuah Khan return -1; 4208dbbf854SShuah Khan } 4218dbbf854SShuah Khan memset(&perout_request, 0, sizeof(perout_request)); 4228dbbf854SShuah Khan perout_request.index = index; 4234a09a981SVladimir Oltean perout_request.period.sec = perout / NSEC_PER_SEC; 4244a09a981SVladimir Oltean perout_request.period.nsec = perout % NSEC_PER_SEC; 425*7570ebe0SVladimir Oltean perout_request.flags = 0; 426*7570ebe0SVladimir Oltean if (pulsewidth >= 0) { 427*7570ebe0SVladimir Oltean perout_request.flags |= PTP_PEROUT_DUTY_CYCLE; 428*7570ebe0SVladimir Oltean perout_request.on.sec = pulsewidth / NSEC_PER_SEC; 429*7570ebe0SVladimir Oltean perout_request.on.nsec = pulsewidth % NSEC_PER_SEC; 430*7570ebe0SVladimir Oltean } 431*7570ebe0SVladimir Oltean if (perout_phase >= 0) { 432*7570ebe0SVladimir Oltean perout_request.flags |= PTP_PEROUT_PHASE; 433*7570ebe0SVladimir Oltean perout_request.phase.sec = perout_phase / NSEC_PER_SEC; 434*7570ebe0SVladimir Oltean perout_request.phase.nsec = perout_phase % NSEC_PER_SEC; 435*7570ebe0SVladimir Oltean } else { 436*7570ebe0SVladimir Oltean perout_request.start.sec = ts.tv_sec + 2; 437*7570ebe0SVladimir Oltean perout_request.start.nsec = 0; 438*7570ebe0SVladimir Oltean } 439*7570ebe0SVladimir Oltean 440*7570ebe0SVladimir Oltean if (ioctl(fd, PTP_PEROUT_REQUEST2, &perout_request)) { 4418dbbf854SShuah Khan perror("PTP_PEROUT_REQUEST"); 4428dbbf854SShuah Khan } else { 4438dbbf854SShuah Khan puts("periodic output request okay"); 4448dbbf854SShuah Khan } 4458dbbf854SShuah Khan } 4468dbbf854SShuah Khan 4478dbbf854SShuah Khan if (pin_index >= 0) { 4488dbbf854SShuah Khan memset(&desc, 0, sizeof(desc)); 4498dbbf854SShuah Khan desc.index = pin_index; 4508dbbf854SShuah Khan desc.func = pin_func; 4518dbbf854SShuah Khan desc.chan = index; 4528dbbf854SShuah Khan if (ioctl(fd, PTP_PIN_SETFUNC, &desc)) { 4538dbbf854SShuah Khan perror("PTP_PIN_SETFUNC"); 4548dbbf854SShuah Khan } else { 4558dbbf854SShuah Khan puts("set pin function okay"); 4568dbbf854SShuah Khan } 4578dbbf854SShuah Khan } 4588dbbf854SShuah Khan 4598dbbf854SShuah Khan if (pps != -1) { 4608dbbf854SShuah Khan int enable = pps ? 1 : 0; 4618dbbf854SShuah Khan if (ioctl(fd, PTP_ENABLE_PPS, enable)) { 4628dbbf854SShuah Khan perror("PTP_ENABLE_PPS"); 4638dbbf854SShuah Khan } else { 4648dbbf854SShuah Khan puts("pps for system time request okay"); 4658dbbf854SShuah Khan } 4668dbbf854SShuah Khan } 4678dbbf854SShuah Khan 4688dbbf854SShuah Khan if (pct_offset) { 4698dbbf854SShuah Khan if (n_samples <= 0 || n_samples > 25) { 4708dbbf854SShuah Khan puts("n_samples should be between 1 and 25"); 4718dbbf854SShuah Khan usage(progname); 4728dbbf854SShuah Khan return -1; 4738dbbf854SShuah Khan } 4748dbbf854SShuah Khan 4758dbbf854SShuah Khan sysoff = calloc(1, sizeof(*sysoff)); 4768dbbf854SShuah Khan if (!sysoff) { 4778dbbf854SShuah Khan perror("calloc"); 4788dbbf854SShuah Khan return -1; 4798dbbf854SShuah Khan } 4808dbbf854SShuah Khan sysoff->n_samples = n_samples; 4818dbbf854SShuah Khan 4828dbbf854SShuah Khan if (ioctl(fd, PTP_SYS_OFFSET, sysoff)) 4838dbbf854SShuah Khan perror("PTP_SYS_OFFSET"); 4848dbbf854SShuah Khan else 4858dbbf854SShuah Khan puts("system and phc clock time offset request okay"); 4868dbbf854SShuah Khan 4878dbbf854SShuah Khan pct = &sysoff->ts[0]; 4888dbbf854SShuah Khan for (i = 0; i < sysoff->n_samples; i++) { 4898dbbf854SShuah Khan t1 = pctns(pct+2*i); 4908dbbf854SShuah Khan tp = pctns(pct+2*i+1); 4918dbbf854SShuah Khan t2 = pctns(pct+2*i+2); 4928dbbf854SShuah Khan interval = t2 - t1; 4938dbbf854SShuah Khan offset = (t2 + t1) / 2 - tp; 4948dbbf854SShuah Khan 4958dbbf854SShuah Khan printf("system time: %lld.%u\n", 4968dbbf854SShuah Khan (pct+2*i)->sec, (pct+2*i)->nsec); 4978dbbf854SShuah Khan printf("phc time: %lld.%u\n", 4988dbbf854SShuah Khan (pct+2*i+1)->sec, (pct+2*i+1)->nsec); 4998dbbf854SShuah Khan printf("system time: %lld.%u\n", 5008dbbf854SShuah Khan (pct+2*i+2)->sec, (pct+2*i+2)->nsec); 5018dbbf854SShuah Khan printf("system/phc clock time offset is %" PRId64 " ns\n" 5028dbbf854SShuah Khan "system clock time delay is %" PRId64 " ns\n", 5038dbbf854SShuah Khan offset, interval); 5048dbbf854SShuah Khan } 5058dbbf854SShuah Khan 5068dbbf854SShuah Khan free(sysoff); 5078dbbf854SShuah Khan } 5088dbbf854SShuah Khan 5098dbbf854SShuah Khan close(fd); 5108dbbf854SShuah Khan return 0; 5118dbbf854SShuah Khan } 512