blob: e1fa3689a90316c4fe9f52424d7a2e326281a505 [file] [log] [blame]
john stultz4c7ee8d2006-09-30 23:28:22 -07001/*
2 * linux/kernel/time/ntp.c
3 *
4 * NTP state machine interfaces and logic.
5 *
6 * This code was mainly moved from kernel/timer.c and kernel/time.c
7 * Please see those files for relevant copyright info and historical
8 * changelogs.
9 */
10
11#include <linux/mm.h>
12#include <linux/time.h>
13#include <linux/timex.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040014#include <linux/jiffies.h>
15#include <linux/hrtimer.h>
Alexey Dobriyanaa0ac362007-07-15 23:40:39 -070016#include <linux/capability.h>
Roman Zippel71abb3a2008-05-01 04:34:26 -070017#include <linux/math64.h>
Roman Zippel7dffa3c2008-05-01 04:34:41 -070018#include <linux/clocksource.h>
Maciej W. Rozyckieb3f9382008-09-22 14:42:40 -070019#include <linux/workqueue.h>
john stultz4c7ee8d2006-09-30 23:28:22 -070020#include <asm/timex.h>
21
Roman Zippelb0ee7552006-09-30 23:28:22 -070022/*
23 * Timekeeping variables
24 */
25unsigned long tick_usec = TICK_USEC; /* USER_HZ period (usec) */
26unsigned long tick_nsec; /* ACTHZ period (nsec) */
Roman Zippel8383c4232008-05-01 04:34:39 -070027u64 tick_length;
28static u64 tick_length_base;
Roman Zippelb0ee7552006-09-30 23:28:22 -070029
Roman Zippel7dffa3c2008-05-01 04:34:41 -070030static struct hrtimer leap_timer;
31
Roman Zippel8f807f82006-09-30 23:28:25 -070032#define MAX_TICKADJ 500 /* microsecs */
33#define MAX_TICKADJ_SCALED (((u64)(MAX_TICKADJ * NSEC_PER_USEC) << \
Roman Zippel7fc5c782008-05-01 04:34:38 -070034 NTP_SCALE_SHIFT) / NTP_INTERVAL_FREQ)
john stultz4c7ee8d2006-09-30 23:28:22 -070035
36/*
37 * phase-lock loop variables
38 */
39/* TIME_ERROR prevents overwriting the CMOS clock */
Adrian Bunk70bc42f2006-09-30 23:28:29 -070040static int time_state = TIME_OK; /* clock synchronization status */
john stultz4c7ee8d2006-09-30 23:28:22 -070041int time_status = STA_UNSYNC; /* clock status bits */
Roman Zippel153b5d02008-05-01 04:34:37 -070042static long time_tai; /* TAI offset (s) */
Roman Zippelee9851b2008-05-01 04:34:32 -070043static s64 time_offset; /* time adjustment (ns) */
Adrian Bunk70bc42f2006-09-30 23:28:29 -070044static long time_constant = 2; /* pll time constant */
john stultz4c7ee8d2006-09-30 23:28:22 -070045long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */
46long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */
Roman Zippel074b3b82008-05-01 04:34:34 -070047static s64 time_freq; /* frequency offset (scaled ns/s)*/
Adrian Bunk70bc42f2006-09-30 23:28:29 -070048static long time_reftime; /* time at last adjustment (s) */
john stultz4c7ee8d2006-09-30 23:28:22 -070049long time_adjust;
Roman Zippel10a398d2008-03-04 15:14:26 -080050static long ntp_tick_adj;
john stultz4c7ee8d2006-09-30 23:28:22 -070051
Adrian Bunk70bc42f2006-09-30 23:28:29 -070052static void ntp_update_frequency(void)
53{
john stultzfdcedf72009-02-18 16:02:22 -080054 u64 old_tick_length_base = tick_length_base;
john stultzf4304ab2007-02-16 01:27:26 -080055 u64 second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ)
Roman Zippel7fc5c782008-05-01 04:34:38 -070056 << NTP_SCALE_SHIFT;
57 second_length += (s64)ntp_tick_adj << NTP_SCALE_SHIFT;
Roman Zippel074b3b82008-05-01 04:34:34 -070058 second_length += time_freq;
Adrian Bunk70bc42f2006-09-30 23:28:29 -070059
john stultzf4304ab2007-02-16 01:27:26 -080060 tick_length_base = second_length;
Adrian Bunk70bc42f2006-09-30 23:28:29 -070061
Roman Zippel7fc5c782008-05-01 04:34:38 -070062 tick_nsec = div_u64(second_length, HZ) >> NTP_SCALE_SHIFT;
Roman Zippel71abb3a2008-05-01 04:34:26 -070063 tick_length_base = div_u64(tick_length_base, NTP_INTERVAL_FREQ);
john stultzfdcedf72009-02-18 16:02:22 -080064
65 /*
66 * Don't wait for the next second_overflow, apply
67 * the change to the tick length immediately
68 */
69 tick_length += tick_length_base - old_tick_length_base;
Adrian Bunk70bc42f2006-09-30 23:28:29 -070070}
71
Roman Zippelee9851b2008-05-01 04:34:32 -070072static void ntp_update_offset(long offset)
73{
74 long mtemp;
75 s64 freq_adj;
76
77 if (!(time_status & STA_PLL))
78 return;
79
Roman Zippeleea83d82008-05-01 04:34:33 -070080 if (!(time_status & STA_NANO))
Roman Zippel9f14f662008-05-01 04:34:36 -070081 offset *= NSEC_PER_USEC;
Roman Zippelee9851b2008-05-01 04:34:32 -070082
83 /*
84 * Scale the phase adjustment and
85 * clamp to the operating range.
86 */
Roman Zippel9f14f662008-05-01 04:34:36 -070087 offset = min(offset, MAXPHASE);
88 offset = max(offset, -MAXPHASE);
Roman Zippelee9851b2008-05-01 04:34:32 -070089
90 /*
91 * Select how the frequency is to be controlled
92 * and in which mode (PLL or FLL).
93 */
94 if (time_status & STA_FREQHOLD || time_reftime == 0)
95 time_reftime = xtime.tv_sec;
96 mtemp = xtime.tv_sec - time_reftime;
97 time_reftime = xtime.tv_sec;
98
Roman Zippel9f14f662008-05-01 04:34:36 -070099 freq_adj = (s64)offset * mtemp;
Roman Zippel7fc5c782008-05-01 04:34:38 -0700100 freq_adj <<= NTP_SCALE_SHIFT - 2 * (SHIFT_PLL + 2 + time_constant);
Roman Zippeleea83d82008-05-01 04:34:33 -0700101 time_status &= ~STA_MODE;
102 if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) {
Roman Zippel7fc5c782008-05-01 04:34:38 -0700103 freq_adj += div_s64((s64)offset << (NTP_SCALE_SHIFT - SHIFT_FLL),
Roman Zippel074b3b82008-05-01 04:34:34 -0700104 mtemp);
Roman Zippeleea83d82008-05-01 04:34:33 -0700105 time_status |= STA_MODE;
106 }
Roman Zippelee9851b2008-05-01 04:34:32 -0700107 freq_adj += time_freq;
Roman Zippel074b3b82008-05-01 04:34:34 -0700108 freq_adj = min(freq_adj, MAXFREQ_SCALED);
109 time_freq = max(freq_adj, -MAXFREQ_SCALED);
Roman Zippel9f14f662008-05-01 04:34:36 -0700110
Roman Zippel7fc5c782008-05-01 04:34:38 -0700111 time_offset = div_s64((s64)offset << NTP_SCALE_SHIFT, NTP_INTERVAL_FREQ);
Roman Zippelee9851b2008-05-01 04:34:32 -0700112}
113
Roman Zippelb0ee7552006-09-30 23:28:22 -0700114/**
115 * ntp_clear - Clears the NTP state variables
116 *
117 * Must be called while holding a write on the xtime_lock
118 */
119void ntp_clear(void)
120{
121 time_adjust = 0; /* stop active adjtime() */
122 time_status |= STA_UNSYNC;
123 time_maxerror = NTP_PHASE_LIMIT;
124 time_esterror = NTP_PHASE_LIMIT;
125
126 ntp_update_frequency();
127
128 tick_length = tick_length_base;
Roman Zippel3d3675c2006-09-30 23:28:25 -0700129 time_offset = 0;
Roman Zippelb0ee7552006-09-30 23:28:22 -0700130}
131
john stultz4c7ee8d2006-09-30 23:28:22 -0700132/*
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700133 * Leap second processing. If in leap-insert state at the end of the
134 * day, the system clock is set back one second; if in leap-delete
135 * state, the system clock is set ahead one second.
136 */
137static enum hrtimer_restart ntp_leap_second(struct hrtimer *timer)
138{
139 enum hrtimer_restart res = HRTIMER_NORESTART;
140
Peter Zijlstraca109492008-11-25 12:43:51 +0100141 write_seqlock(&xtime_lock);
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700142
143 switch (time_state) {
144 case TIME_OK:
145 break;
146 case TIME_INS:
147 xtime.tv_sec--;
148 wall_to_monotonic.tv_sec++;
149 time_state = TIME_OOP;
150 printk(KERN_NOTICE "Clock: "
151 "inserting leap second 23:59:60 UTC\n");
Arjan van de Vencc584b22008-09-01 15:02:30 -0700152 hrtimer_add_expires_ns(&leap_timer, NSEC_PER_SEC);
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700153 res = HRTIMER_RESTART;
154 break;
155 case TIME_DEL:
156 xtime.tv_sec++;
157 time_tai--;
158 wall_to_monotonic.tv_sec--;
159 time_state = TIME_WAIT;
160 printk(KERN_NOTICE "Clock: "
161 "deleting leap second 23:59:59 UTC\n");
162 break;
163 case TIME_OOP:
164 time_tai++;
165 time_state = TIME_WAIT;
166 /* fall through */
167 case TIME_WAIT:
168 if (!(time_status & (STA_INS | STA_DEL)))
169 time_state = TIME_OK;
170 break;
171 }
172 update_vsyscall(&xtime, clock);
173
Peter Zijlstraca109492008-11-25 12:43:51 +0100174 write_sequnlock(&xtime_lock);
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700175
176 return res;
177}
178
179/*
john stultz4c7ee8d2006-09-30 23:28:22 -0700180 * this routine handles the overflow of the microsecond field
181 *
182 * The tricky bits of code to handle the accurate clock support
183 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
184 * They were originally developed for SUN and DEC kernels.
185 * All the kudos should go to Dave for this stuff.
186 */
187void second_overflow(void)
188{
Roman Zippel9f14f662008-05-01 04:34:36 -0700189 s64 time_adj;
john stultz4c7ee8d2006-09-30 23:28:22 -0700190
191 /* Bump the maxerror field */
Roman Zippel074b3b82008-05-01 04:34:34 -0700192 time_maxerror += MAXFREQ / NSEC_PER_USEC;
john stultz4c7ee8d2006-09-30 23:28:22 -0700193 if (time_maxerror > NTP_PHASE_LIMIT) {
194 time_maxerror = NTP_PHASE_LIMIT;
195 time_status |= STA_UNSYNC;
196 }
197
198 /*
Roman Zippelf1992392006-09-30 23:28:28 -0700199 * Compute the phase adjustment for the next second. The offset is
200 * reduced by a fixed factor times the time constant.
john stultz4c7ee8d2006-09-30 23:28:22 -0700201 */
Roman Zippelb0ee7552006-09-30 23:28:22 -0700202 tick_length = tick_length_base;
Roman Zippelf1992392006-09-30 23:28:28 -0700203 time_adj = shift_right(time_offset, SHIFT_PLL + time_constant);
Roman Zippel3d3675c2006-09-30 23:28:25 -0700204 time_offset -= time_adj;
Roman Zippel9f14f662008-05-01 04:34:36 -0700205 tick_length += time_adj;
john stultz4c7ee8d2006-09-30 23:28:22 -0700206
Roman Zippel8f807f82006-09-30 23:28:25 -0700207 if (unlikely(time_adjust)) {
208 if (time_adjust > MAX_TICKADJ) {
209 time_adjust -= MAX_TICKADJ;
210 tick_length += MAX_TICKADJ_SCALED;
211 } else if (time_adjust < -MAX_TICKADJ) {
212 time_adjust += MAX_TICKADJ;
213 tick_length -= MAX_TICKADJ_SCALED;
214 } else {
Roman Zippel8f807f82006-09-30 23:28:25 -0700215 tick_length += (s64)(time_adjust * NSEC_PER_USEC /
Roman Zippel7fc5c782008-05-01 04:34:38 -0700216 NTP_INTERVAL_FREQ) << NTP_SCALE_SHIFT;
Jim Houstonbb1d8602006-10-28 10:38:56 -0700217 time_adjust = 0;
Roman Zippel8f807f82006-09-30 23:28:25 -0700218 }
john stultz4c7ee8d2006-09-30 23:28:22 -0700219 }
220}
221
Thomas Gleixner82644452007-07-21 04:37:37 -0700222#ifdef CONFIG_GENERIC_CMOS_UPDATE
john stultz4c7ee8d2006-09-30 23:28:22 -0700223
Thomas Gleixner82644452007-07-21 04:37:37 -0700224/* Disable the cmos update - used by virtualization and embedded */
225int no_sync_cmos_clock __read_mostly;
226
Maciej W. Rozyckieb3f9382008-09-22 14:42:40 -0700227static void sync_cmos_clock(struct work_struct *work);
Thomas Gleixner82644452007-07-21 04:37:37 -0700228
Maciej W. Rozyckieb3f9382008-09-22 14:42:40 -0700229static DECLARE_DELAYED_WORK(sync_cmos_work, sync_cmos_clock);
Thomas Gleixner82644452007-07-21 04:37:37 -0700230
Maciej W. Rozyckieb3f9382008-09-22 14:42:40 -0700231static void sync_cmos_clock(struct work_struct *work)
john stultz4c7ee8d2006-09-30 23:28:22 -0700232{
Thomas Gleixner82644452007-07-21 04:37:37 -0700233 struct timespec now, next;
234 int fail = 1;
235
236 /*
237 * If we have an externally synchronized Linux clock, then update
238 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
239 * called as close as possible to 500 ms before the new second starts.
240 * This code is run on a timer. If the clock is set, that timer
241 * may not expire at the correct time. Thus, we adjust...
242 */
243 if (!ntp_synced())
244 /*
245 * Not synced, exit, do not restart a timer (if one is
246 * running, let it run out).
247 */
248 return;
249
250 getnstimeofday(&now);
David P. Reedfa6a1a52007-11-14 17:49:21 -0500251 if (abs(now.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec / 2)
Thomas Gleixner82644452007-07-21 04:37:37 -0700252 fail = update_persistent_clock(now);
253
Maciej W. Rozycki4ff4b9e2008-09-05 14:05:31 -0700254 next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec - (TICK_NSEC / 2);
Thomas Gleixner82644452007-07-21 04:37:37 -0700255 if (next.tv_nsec <= 0)
256 next.tv_nsec += NSEC_PER_SEC;
257
258 if (!fail)
259 next.tv_sec = 659;
260 else
261 next.tv_sec = 0;
262
263 if (next.tv_nsec >= NSEC_PER_SEC) {
264 next.tv_sec++;
265 next.tv_nsec -= NSEC_PER_SEC;
266 }
Maciej W. Rozyckieb3f9382008-09-22 14:42:40 -0700267 schedule_delayed_work(&sync_cmos_work, timespec_to_jiffies(&next));
john stultz4c7ee8d2006-09-30 23:28:22 -0700268}
269
Thomas Gleixner82644452007-07-21 04:37:37 -0700270static void notify_cmos_timer(void)
271{
Tony Breeds298a5df2007-09-11 15:24:03 -0700272 if (!no_sync_cmos_clock)
Maciej W. Rozyckieb3f9382008-09-22 14:42:40 -0700273 schedule_delayed_work(&sync_cmos_work, 0);
Thomas Gleixner82644452007-07-21 04:37:37 -0700274}
275
276#else
277static inline void notify_cmos_timer(void) { }
278#endif
279
john stultz4c7ee8d2006-09-30 23:28:22 -0700280/* adjtimex mainly allows reading (and writing, if superuser) of
281 * kernel time-keeping variables. used by xntpd.
282 */
283int do_adjtimex(struct timex *txc)
284{
Roman Zippeleea83d82008-05-01 04:34:33 -0700285 struct timespec ts;
john stultz4c7ee8d2006-09-30 23:28:22 -0700286 int result;
287
Roman Zippel916c7a82008-08-20 16:46:08 -0700288 /* Validate the data before disabling interrupts */
289 if (txc->modes & ADJ_ADJTIME) {
Roman Zippeleea83d82008-05-01 04:34:33 -0700290 /* singleshot must not be used with any other mode bits */
Roman Zippel916c7a82008-08-20 16:46:08 -0700291 if (!(txc->modes & ADJ_OFFSET_SINGLESHOT))
john stultz4c7ee8d2006-09-30 23:28:22 -0700292 return -EINVAL;
Roman Zippel916c7a82008-08-20 16:46:08 -0700293 if (!(txc->modes & ADJ_OFFSET_READONLY) &&
294 !capable(CAP_SYS_TIME))
295 return -EPERM;
296 } else {
297 /* In order to modify anything, you gotta be super-user! */
298 if (txc->modes && !capable(CAP_SYS_TIME))
299 return -EPERM;
300
301 /* if the quartz is off by more than 10% something is VERY wrong! */
302 if (txc->modes & ADJ_TICK &&
303 (txc->tick < 900000/USER_HZ ||
304 txc->tick > 1100000/USER_HZ))
305 return -EINVAL;
306
307 if (txc->modes & ADJ_STATUS && time_state != TIME_OK)
308 hrtimer_cancel(&leap_timer);
John Stultz52bfb362007-11-26 20:42:19 +0100309 }
john stultz4c7ee8d2006-09-30 23:28:22 -0700310
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700311 getnstimeofday(&ts);
312
john stultz4c7ee8d2006-09-30 23:28:22 -0700313 write_seqlock_irq(&xtime_lock);
john stultz4c7ee8d2006-09-30 23:28:22 -0700314
john stultz4c7ee8d2006-09-30 23:28:22 -0700315 /* If there are input parameters, then process them */
Roman Zippel916c7a82008-08-20 16:46:08 -0700316 if (txc->modes & ADJ_ADJTIME) {
317 long save_adjust = time_adjust;
318
319 if (!(txc->modes & ADJ_OFFSET_READONLY)) {
320 /* adjtime() is independent from ntp_adjtime() */
321 time_adjust = txc->offset;
322 ntp_update_frequency();
323 }
324 txc->offset = save_adjust;
325 goto adj_done;
326 }
Roman Zippelee9851b2008-05-01 04:34:32 -0700327 if (txc->modes) {
Roman Zippel916c7a82008-08-20 16:46:08 -0700328 long sec;
329
Roman Zippeleea83d82008-05-01 04:34:33 -0700330 if (txc->modes & ADJ_STATUS) {
331 if ((time_status & STA_PLL) &&
332 !(txc->status & STA_PLL)) {
333 time_state = TIME_OK;
334 time_status = STA_UNSYNC;
335 }
336 /* only set allowed bits */
337 time_status &= STA_RONLY;
338 time_status |= txc->status & ~STA_RONLY;
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700339
340 switch (time_state) {
341 case TIME_OK:
342 start_timer:
343 sec = ts.tv_sec;
344 if (time_status & STA_INS) {
345 time_state = TIME_INS;
346 sec += 86400 - sec % 86400;
347 hrtimer_start(&leap_timer, ktime_set(sec, 0), HRTIMER_MODE_ABS);
348 } else if (time_status & STA_DEL) {
349 time_state = TIME_DEL;
350 sec += 86400 - (sec + 1) % 86400;
351 hrtimer_start(&leap_timer, ktime_set(sec, 0), HRTIMER_MODE_ABS);
352 }
353 break;
354 case TIME_INS:
355 case TIME_DEL:
356 time_state = TIME_OK;
357 goto start_timer;
358 break;
359 case TIME_WAIT:
360 if (!(time_status & (STA_INS | STA_DEL)))
361 time_state = TIME_OK;
362 break;
363 case TIME_OOP:
364 hrtimer_restart(&leap_timer);
365 break;
366 }
Roman Zippeleea83d82008-05-01 04:34:33 -0700367 }
368
369 if (txc->modes & ADJ_NANO)
370 time_status |= STA_NANO;
371 if (txc->modes & ADJ_MICRO)
372 time_status &= ~STA_NANO;
john stultz4c7ee8d2006-09-30 23:28:22 -0700373
Roman Zippelee9851b2008-05-01 04:34:32 -0700374 if (txc->modes & ADJ_FREQUENCY) {
Roman Zippel074b3b82008-05-01 04:34:34 -0700375 time_freq = (s64)txc->freq * PPM_SCALE;
376 time_freq = min(time_freq, MAXFREQ_SCALED);
377 time_freq = max(time_freq, -MAXFREQ_SCALED);
john stultz4c7ee8d2006-09-30 23:28:22 -0700378 }
john stultz4c7ee8d2006-09-30 23:28:22 -0700379
Roman Zippeleea83d82008-05-01 04:34:33 -0700380 if (txc->modes & ADJ_MAXERROR)
Roman Zippelee9851b2008-05-01 04:34:32 -0700381 time_maxerror = txc->maxerror;
Roman Zippeleea83d82008-05-01 04:34:33 -0700382 if (txc->modes & ADJ_ESTERROR)
Roman Zippelee9851b2008-05-01 04:34:32 -0700383 time_esterror = txc->esterror;
john stultz4c7ee8d2006-09-30 23:28:22 -0700384
Roman Zippelee9851b2008-05-01 04:34:32 -0700385 if (txc->modes & ADJ_TIMECONST) {
Roman Zippeleea83d82008-05-01 04:34:33 -0700386 time_constant = txc->constant;
387 if (!(time_status & STA_NANO))
388 time_constant += 4;
389 time_constant = min(time_constant, (long)MAXTC);
390 time_constant = max(time_constant, 0l);
john stultz4c7ee8d2006-09-30 23:28:22 -0700391 }
john stultz4c7ee8d2006-09-30 23:28:22 -0700392
Roman Zippel153b5d02008-05-01 04:34:37 -0700393 if (txc->modes & ADJ_TAI && txc->constant > 0)
394 time_tai = txc->constant;
395
Roman Zippel916c7a82008-08-20 16:46:08 -0700396 if (txc->modes & ADJ_OFFSET)
397 ntp_update_offset(txc->offset);
Roman Zippelee9851b2008-05-01 04:34:32 -0700398 if (txc->modes & ADJ_TICK)
399 tick_usec = txc->tick;
john stultz4c7ee8d2006-09-30 23:28:22 -0700400
Roman Zippelee9851b2008-05-01 04:34:32 -0700401 if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
402 ntp_update_frequency();
403 }
Roman Zippeleea83d82008-05-01 04:34:33 -0700404
Roman Zippel916c7a82008-08-20 16:46:08 -0700405 txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ,
406 NTP_SCALE_SHIFT);
407 if (!(time_status & STA_NANO))
408 txc->offset /= NSEC_PER_USEC;
409
410adj_done:
Roman Zippeleea83d82008-05-01 04:34:33 -0700411 result = time_state; /* mostly `TIME_OK' */
Roman Zippelee9851b2008-05-01 04:34:32 -0700412 if (time_status & (STA_UNSYNC|STA_CLOCKERR))
john stultz4c7ee8d2006-09-30 23:28:22 -0700413 result = TIME_ERROR;
414
Roman Zippeld40e9442008-09-22 14:42:44 -0700415 txc->freq = shift_right((time_freq >> PPM_SCALE_INV_SHIFT) *
416 (s64)PPM_SCALE_INV, NTP_SCALE_SHIFT);
john stultz4c7ee8d2006-09-30 23:28:22 -0700417 txc->maxerror = time_maxerror;
418 txc->esterror = time_esterror;
419 txc->status = time_status;
420 txc->constant = time_constant;
Adrian Bunk70bc42f2006-09-30 23:28:29 -0700421 txc->precision = 1;
Roman Zippel074b3b82008-05-01 04:34:34 -0700422 txc->tolerance = MAXFREQ_SCALED / PPM_SCALE;
john stultz4c7ee8d2006-09-30 23:28:22 -0700423 txc->tick = tick_usec;
Roman Zippel153b5d02008-05-01 04:34:37 -0700424 txc->tai = time_tai;
john stultz4c7ee8d2006-09-30 23:28:22 -0700425
426 /* PPS is not implemented, so these are zero */
427 txc->ppsfreq = 0;
428 txc->jitter = 0;
429 txc->shift = 0;
430 txc->stabil = 0;
431 txc->jitcnt = 0;
432 txc->calcnt = 0;
433 txc->errcnt = 0;
434 txc->stbcnt = 0;
435 write_sequnlock_irq(&xtime_lock);
Roman Zippelee9851b2008-05-01 04:34:32 -0700436
Roman Zippeleea83d82008-05-01 04:34:33 -0700437 txc->time.tv_sec = ts.tv_sec;
438 txc->time.tv_usec = ts.tv_nsec;
439 if (!(time_status & STA_NANO))
440 txc->time.tv_usec /= NSEC_PER_USEC;
Roman Zippelee9851b2008-05-01 04:34:32 -0700441
Thomas Gleixner82644452007-07-21 04:37:37 -0700442 notify_cmos_timer();
Roman Zippelee9851b2008-05-01 04:34:32 -0700443
444 return result;
john stultz4c7ee8d2006-09-30 23:28:22 -0700445}
Roman Zippel10a398d2008-03-04 15:14:26 -0800446
447static int __init ntp_tick_adj_setup(char *str)
448{
449 ntp_tick_adj = simple_strtol(str, NULL, 0);
450 return 1;
451}
452
453__setup("ntp_tick_adj=", ntp_tick_adj_setup);
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700454
455void __init ntp_init(void)
456{
457 ntp_clear();
458 hrtimer_init(&leap_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
459 leap_timer.function = ntp_leap_second;
460}