Lines Matching +full:crystal +full:- +full:freq

1 /*-
4 * Copyright (c) David L. Mills 1993-2001 *
22 * Poul-Henning Kamp <phk@FreeBSD.org>.
57 * Single-precision macros for 64-bit machines
61 #define L_SUB(v, u) ((v) -= (u))
63 #define L_NEG(v) ((v) = -(v))
67 (v) = -(-(v) >> (n)); \
77 ((v) = -((int64_t)(-(a)) << 32)); \
81 #define L_GINT(v) ((v) < 0 ? -(-(v) >> 32) : (v) >> 32)
104 * architecture-specific module. The interpolation can use either a
111 * Phase/frequency-lock loop (PLL/FLL) definitions
114 * variables and frequency variables. Both types are represented as 64-
115 * bit fixed-point quantities with the decimal point between two 32-bit
116 * halves. On a 32-bit machine, each half is represented as a single
117 * word and mathematical operations are done using multiple-precision
118 * arithmetic. On a 64-bit machine, ordinary computer arithmetic is
121 * A time variable is a signed 64-bit fixed-point number in ns and
124 * 0.5 s and the resolution is about 2.3e-10 ns.
128 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
130 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
132 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
134 * A frequency variable is a signed 64-bit fixed-point number in ns/s
137 * about +-500000 ns/s and the resolution is about 2.3e-10 ns/s.
141 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
143 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
145 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
178 * The following variables are used when a pulse-per-second (PPS) signal
183 #define PPS_FAVG 2 /* min freq avg interval (s) (shift) */
184 #define PPS_FAVGDEF 8 /* default freq avg int (s) (shift) */
185 #define PPS_FAVGMAX 15 /* max freq avg interval (s) (shift) */
211 * End of phase/frequency-lock loop (PLL/FLL) definitions
263 ntvp->time.tv_sec = atv.tv_sec; in ntp_gettime1()
264 ntvp->time.tv_nsec = atv.tv_nsec; in ntp_gettime1()
265 ntvp->maxerror = time_maxerror; in ntp_gettime1()
266 ntvp->esterror = time_esterror; in ntp_gettime1()
267 ntvp->tai = time_tai; in ntp_gettime1()
268 ntvp->time_state = time_state; in ntp_gettime1()
271 ntvp->time_state = TIME_ERROR; in ntp_gettime1()
275 * ntp_gettime() - NTP user application interface
297 td->td_retval[0] = ntv.time_state; in sys_ntp_gettime()
298 return (copyout(&ntv, uap->ntvp, sizeof(ntv))); in sys_ntp_gettime()
338 * ntp_adjtime() - NTP daemon application interface
347 long freq; /* frequency ns/s) */ in kern_ntp_adjtime() local
352 * Update selected clock variables - only the superuser can in kern_ntp_adjtime()
360 modes = ntv->modes; in kern_ntp_adjtime()
368 time_maxerror = ntv->maxerror; in kern_ntp_adjtime()
370 time_esterror = ntv->esterror; in kern_ntp_adjtime()
372 if (time_status & STA_PLL && !(ntv->status & STA_PLL)) { in kern_ntp_adjtime()
380 time_status |= ntv->status & ~STA_RONLY; in kern_ntp_adjtime()
383 if (ntv->constant < 0) in kern_ntp_adjtime()
385 else if (ntv->constant > MAXTC) in kern_ntp_adjtime()
388 time_constant = ntv->constant; in kern_ntp_adjtime()
391 if (ntv->constant > 0) /* XXX zero & negative numbers ? */ in kern_ntp_adjtime()
392 time_tai = ntv->constant; in kern_ntp_adjtime()
396 if (ntv->shift < PPS_FAVG) in kern_ntp_adjtime()
398 else if (ntv->shift > PPS_FAVGMAX) in kern_ntp_adjtime()
401 pps_shiftmax = ntv->shift; in kern_ntp_adjtime()
413 freq = (ntv->freq * 1000LL) >> 16; in kern_ntp_adjtime()
414 if (freq > MAXFREQ) in kern_ntp_adjtime()
416 else if (freq < -MAXFREQ) in kern_ntp_adjtime()
417 L_LINT(time_freq, -MAXFREQ); in kern_ntp_adjtime()
420 * ntv->freq is [PPM * 2^16] = [us/s * 2^16] in kern_ntp_adjtime()
423 time_freq = ntv->freq * 1000LL * 65536LL; in kern_ntp_adjtime()
431 hardupdate(ntv->offset); in kern_ntp_adjtime()
433 hardupdate(ntv->offset * 1000); in kern_ntp_adjtime()
441 ntv->offset = L_GINT(time_offset); in kern_ntp_adjtime()
443 ntv->offset = L_GINT(time_offset) / 1000; /* XXX rounding ? */ in kern_ntp_adjtime()
444 ntv->freq = L_GINT((time_freq / 1000LL) << 16); in kern_ntp_adjtime()
445 ntv->maxerror = time_maxerror; in kern_ntp_adjtime()
446 ntv->esterror = time_esterror; in kern_ntp_adjtime()
447 ntv->status = time_status; in kern_ntp_adjtime()
448 ntv->constant = time_constant; in kern_ntp_adjtime()
450 ntv->precision = time_precision; in kern_ntp_adjtime()
452 ntv->precision = time_precision / 1000; in kern_ntp_adjtime()
453 ntv->tolerance = MAXFREQ * SCALE_PPM; in kern_ntp_adjtime()
455 ntv->shift = pps_shift; in kern_ntp_adjtime()
456 ntv->ppsfreq = L_GINT((pps_freq / 1000LL) << 16); in kern_ntp_adjtime()
458 ntv->jitter = pps_jitter; in kern_ntp_adjtime()
460 ntv->jitter = pps_jitter / 1000; in kern_ntp_adjtime()
461 ntv->stabil = pps_stabil; in kern_ntp_adjtime()
462 ntv->calcnt = pps_calcnt; in kern_ntp_adjtime()
463 ntv->errcnt = pps_errcnt; in kern_ntp_adjtime()
464 ntv->jitcnt = pps_jitcnt; in kern_ntp_adjtime()
465 ntv->stbcnt = pps_stbcnt; in kern_ntp_adjtime()
486 error = copyin(uap->tp, &ntv, sizeof(ntv)); in sys_ntp_adjtime()
490 error = copyout(&ntv, uap->tp, sizeof(ntv)); in sys_ntp_adjtime()
492 td->td_retval[0] = retval; in sys_ntp_adjtime()
499 * second_overflow() - called after ntp_tick_adjust()
510 l_fp ftemp; /* 32/64-bit temporary */ in ntp_update_second()
524 * Leap second processing. If in leap-insert state at in ntp_update_second()
526 * second; if in leap-delete state, the system clock is in ntp_update_second()
550 (*newsec)--; in ntp_update_second()
564 time_tai--; in ntp_update_second()
614 else if (time_adjtime < -1000000) in ntp_update_second()
615 tickrate = -5000; in ntp_update_second()
618 else if (time_adjtime < -500) in ntp_update_second()
619 tickrate = -500; in ntp_update_second()
622 time_adjtime -= tickrate; in ntp_update_second()
630 pps_valid--; in ntp_update_second()
639 * hardupdate() - local clock update
642 * phase and frequency. The implementation is of an adaptive-parameter,
643 * hybrid phase/frequency-lock loop (PLL/FLL). The routine computes new
648 * believes the local clock is valid within some bound (+-128 ms with
652 * For uncompensated quartz crystal oscillators and nominal update
653 * intervals less than 256 s, operation should be in phase-lock mode,
655 * than 1024 s, operation should be in frequency-lock mode, where the
679 else if (offset < -MAXPHASE) in hardupdate()
680 time_monitor = -MAXPHASE; in hardupdate()
698 mtemp = time_uptime - time_reftime; in hardupdate()
714 else if (L_GINT(time_freq) < -MAXFREQ) in hardupdate()
715 L_LINT(time_freq, -MAXFREQ); in hardupdate()
720 * hardpps() - discipline CPU clock oscillator to external PPS signal
724 * first-order feedback loops, one for the phase, the other for the
730 * time and architecture-dependent hardware counter values in
731 * nanoseconds at the on-time PPS signal transition.
739 * tsp - time at current PPS event
740 * delta_nsec - time elapsed between the previous and current PPS event
754 * the range +-500 us. The frequency discriminator rejects input in hardpps()
755 * signals with apparent frequency outside the range 1 +-500 in hardpps()
764 u_sec = tsp->tv_sec; in hardpps()
765 u_nsec = tsp->tv_nsec; in hardpps()
767 u_nsec -= NANOSECOND; in hardpps()
770 v_nsec = u_nsec - pps_tf[0].tv_nsec; in hardpps()
771 if (u_sec == pps_tf[0].tv_sec && v_nsec < NANOSECOND - MAXFREQ) in hardpps()
782 pps_fcount += delta_nsec - NANOSECOND; in hardpps()
783 if (v_nsec > MAXFREQ || v_nsec < -MAXFREQ) in hardpps()
788 * A three-stage median filter is used to help denoise the PPS in hardpps()
796 u_nsec = pps_tf[0].tv_nsec - pps_tf[2].tv_nsec; in hardpps()
799 u_nsec = pps_tf[2].tv_nsec - pps_tf[1].tv_nsec; in hardpps()
802 u_nsec = pps_tf[0].tv_nsec - pps_tf[1].tv_nsec; in hardpps()
807 u_nsec = pps_tf[2].tv_nsec - pps_tf[0].tv_nsec; in hardpps()
810 u_nsec = pps_tf[1].tv_nsec - pps_tf[2].tv_nsec; in hardpps()
813 u_nsec = pps_tf[1].tv_nsec - pps_tf[0].tv_nsec; in hardpps()
835 time_monitor = -v_nsec; in hardpps()
838 pps_jitter += (u_nsec - pps_jitter) >> PPS_FAVG; in hardpps()
839 u_sec = pps_tf[0].tv_sec - pps_lastsec; in hardpps()
854 v_nsec = -pps_fcount; in hardpps()
858 if (v_nsec > u_nsec || v_nsec < -u_nsec || u_sec != (1 << pps_shift)) { in hardpps()
881 pps_intcnt--; in hardpps()
884 } else if (u_nsec < -PPS_MAXWANDER) { in hardpps()
885 L_LINT(ftemp, -PPS_MAXWANDER); in hardpps()
886 pps_intcnt--; in hardpps()
898 } else if (pps_intcnt <= -4 || pps_shift > pps_shiftmax) { in hardpps()
899 pps_intcnt = -4; in hardpps()
901 pps_shift--; in hardpps()
906 u_nsec = -u_nsec; in hardpps()
907 pps_stabil += (u_nsec * SCALE_PPM - pps_stabil) >> PPS_FAVG; in hardpps()
918 else if (u_nsec < -MAXFREQ) in hardpps()
919 L_LINT(pps_freq, -MAXFREQ); in hardpps()
941 if (uap->delta) { in sys_adjtime()
942 error = copyin(uap->delta, &delta, sizeof(delta)); in sys_adjtime()
949 if (uap->olddelta && error == 0) in sys_adjtime()
950 error = copyout(&olddelta, uap->olddelta, sizeof(olddelta)); in sys_adjtime()
965 ltw = (int64_t)delta->tv_sec * 1000000 + delta->tv_usec; in kern_adjtime()
977 atv.tv_sec--; in kern_adjtime()
992 * Read of time_status is lock-less, which is fine since in periodic_resettodr()
1016 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req); in sysctl_resettodr_period()
1017 if (error || !req->newptr) in sysctl_resettodr_period()