8 #include <linux/capability.h>
11 #include <linux/hrtimer.h>
14 #include <linux/timex.h>
15 #include <linux/time.h>
17 #include <linux/module.h>
34 static u64 tick_length;
35 static u64 tick_length_base;
37 #define MAX_TICKADJ 500LL
38 #define MAX_TICKADJ_SCALED \
39 (((MAX_TICKADJ * NSEC_PER_USEC) << NTP_SCALE_SHIFT) / NTP_INTERVAL_FREQ)
50 static int time_state =
TIME_OK;
59 static s64 time_offset;
62 static long time_constant = 2;
74 static long time_reftime;
76 static long time_adjust;
79 static s64 ntp_tick_adj;
92 #define PPS_INTCOUNT 4
95 #define PPS_MAXWANDER 100000
98 static long pps_tf[3];
99 static long pps_jitter;
101 static int pps_shift;
102 static int pps_intcnt;
104 static long pps_stabil;
109 static long pps_calcnt;
110 static long pps_jitcnt;
111 static long pps_stbcnt;
112 static long pps_errcnt;
126 static inline void pps_reset_freq_interval(
void)
130 pps_shift = PPS_INTMIN;
139 static inline void pps_clear(
void)
141 pps_reset_freq_interval();
145 pps_fbase.tv_sec = pps_fbase.tv_nsec = 0;
155 static inline void pps_dec_valid(
void)
166 static inline void pps_set_freq(
s64 freq)
171 static inline int is_error_status(
int status)
190 static inline void pps_fill_timex(
struct timex *txc)
197 txc->
shift = pps_shift;
207 static inline s64 ntp_offset_chunk(
s64 offset)
212 static inline void pps_reset_freq_interval(
void) {}
213 static inline void pps_clear(
void) {}
214 static inline void pps_dec_valid(
void) {}
215 static inline void pps_set_freq(
s64 freq) {}
217 static inline int is_error_status(
int status)
222 static inline void pps_fill_timex(
struct timex *txc)
242 static inline int ntp_synced(
void)
256 static void ntp_update_frequency(
void)
264 second_length += ntp_tick_adj;
265 second_length += time_freq;
274 tick_length += new_base - tick_length_base;
275 tick_length_base = new_base;
278 static inline s64 ntp_update_offset_fll(
s64 offset64,
long secs)
293 static void ntp_update_offset(
long offset)
323 freq_adj = ntp_update_offset_fll(offset64, secs);
331 secs = 1 << (
SHIFT_PLL + 1 + time_constant);
333 freq_adj += (offset64 * secs) <<
357 ntp_update_frequency();
359 tick_length = tick_length_base;
364 spin_unlock_irqrestore(&ntp_lock, flags);
376 spin_unlock_irqrestore(&ntp_lock, flags);
404 switch (time_state) {
408 else if (time_status &
STA_DEL)
412 if (!(time_status & STA_INS))
414 else if (secs % 86400 == 0) {
419 "Clock: inserting leap second 23:59:60 UTC\n");
423 if (!(time_status & STA_DEL))
425 else if ((secs + 1) % 86400 == 0) {
430 "Clock: deleting leap second 23:59:59 UTC\n");
438 if (!(time_status & (STA_INS | STA_DEL)))
452 tick_length = tick_length_base;
454 delta = ntp_offset_chunk(time_offset);
455 time_offset -=
delta;
456 tick_length +=
delta;
481 spin_unlock_irqrestore(&ntp_lock, flags);
486 #ifdef CONFIG_GENERIC_CMOS_UPDATE
517 if (
next.tv_nsec <= 0)
532 static void notify_cmos_timer(
void)
538 static inline void notify_cmos_timer(
void) { }
545 static inline void process_adj_status(
struct timex *txc,
struct timespec *
ts)
547 if ((time_status & STA_PLL) && !(txc->
status & STA_PLL)) {
551 pps_reset_freq_interval();
558 if (!(time_status & STA_PLL) && (txc->
status & STA_PLL))
570 static inline void process_adjtimex_modes(
struct timex *txc,
struct timespec *ts)
573 process_adj_status(txc, ts);
579 time_status &= ~STA_NANO;
586 pps_set_freq(time_freq);
597 if (!(time_status & STA_NANO))
599 time_constant =
min(time_constant, (
long)
MAXTC);
600 time_constant =
max(time_constant, 0
l);
607 ntp_update_offset(txc->
offset);
613 ntp_update_frequency();
663 spin_lock_irq(&ntp_lock);
666 long save_adjust = time_adjust;
670 time_adjust = txc->
offset;
671 ntp_update_frequency();
673 txc->
offset = save_adjust;
678 process_adjtimex_modes(txc, &ts);
682 if (!(time_status & STA_NANO))
688 if (is_error_status(time_status))
695 txc->
status = time_status;
705 spin_unlock_irq(&ntp_lock);
709 if (!(time_status & STA_NANO))
717 #ifdef CONFIG_NTP_PPS
723 struct pps_normtime {
730 static inline struct pps_normtime pps_normalize_ts(
struct timespec ts)
732 struct pps_normtime norm = {
746 static inline long pps_phase_filter_get(
long *jitter)
748 *jitter = pps_tf[0] - pps_tf[1];
757 static inline void pps_phase_filter_add(
long err)
759 pps_tf[2] = pps_tf[1];
760 pps_tf[1] = pps_tf[0];
767 static inline void pps_dec_freq_interval(
void)
769 if (--pps_intcnt <= -PPS_INTCOUNT) {
770 pps_intcnt = -PPS_INTCOUNT;
771 if (pps_shift > PPS_INTMIN) {
781 static inline void pps_inc_freq_interval(
void)
783 if (++pps_intcnt >= PPS_INTCOUNT) {
784 pps_intcnt = PPS_INTCOUNT;
785 if (pps_shift < PPS_INTMAX) {
801 static long hardpps_update_freq(
struct pps_normtime freq_norm)
803 long delta, delta_mod;
807 if (freq_norm.sec > (2 << pps_shift)) {
810 pps_dec_freq_interval();
811 pr_err(
"hardpps: PPSERROR: interval too long - %ld s\n",
824 if (delta > PPS_MAXWANDER || delta < -PPS_MAXWANDER) {
825 pr_warning(
"hardpps: PPSWANDER: change=%ld\n", delta);
828 pps_dec_freq_interval();
830 pps_inc_freq_interval();
839 delta_mod = -delta_mod;
840 pps_stabil += (div_s64(((
s64)delta_mod) <<
847 time_freq = pps_freq;
848 ntp_update_frequency();
855 static void hardpps_update_phase(
long error)
857 long correction = -
error;
861 pps_phase_filter_add(correction);
862 correction = pps_phase_filter_get(&jitter);
868 if (jitter > (pps_jitter << PPS_POPCORN)) {
869 pr_warning(
"hardpps: PPSJITTER: jitter=%ld, limit=%ld\n",
870 jitter, (pps_jitter << PPS_POPCORN));
881 pps_jitter += (jitter - pps_jitter) >> PPS_INTMIN;
898 struct pps_normtime pts_norm, freq_norm;
901 pts_norm = pps_normalize_ts(*phase_ts);
910 pps_valid = PPS_VALID;
914 if (
unlikely(pps_fbase.tv_sec == 0)) {
916 spin_unlock_irqrestore(&ntp_lock, flags);
921 freq_norm = pps_normalize_ts(timespec_sub(*raw_ts, pps_fbase));
925 if ((freq_norm.sec == 0) ||
926 (freq_norm.nsec >
MAXFREQ * freq_norm.sec) ||
927 (freq_norm.nsec < -
MAXFREQ * freq_norm.sec)) {
931 spin_unlock_irqrestore(&ntp_lock, flags);
932 pr_err(
"hardpps: PPSJITTER: bad pulse\n");
939 if (freq_norm.sec >= (1 << pps_shift)) {
943 hardpps_update_freq(freq_norm);
946 hardpps_update_phase(pts_norm.nsec);
948 spin_unlock_irqrestore(&ntp_lock, flags);
954 static int __init ntp_tick_adj_setup(
char *
str)
962 __setup(
"ntp_tick_adj=", ntp_tick_adj_setup);