[PATCH] ppc64: Add missing exports
[firewire-audio.git] / arch / ppc64 / kernel / time.c
blob2a532db9138a6176f891fed3fb1c4e5209ffa344
1 /*
2 *
3 * Common time routines among all ppc machines.
5 * Written by Cort Dougan (cort@cs.nmt.edu) to merge
6 * Paul Mackerras' version and mine for PReP and Pmac.
7 * MPC8xx/MBX changes by Dan Malek (dmalek@jlc.net).
8 * Converted for 64-bit by Mike Corrigan (mikejc@us.ibm.com)
10 * First round of bugfixes by Gabriel Paubert (paubert@iram.es)
11 * to make clock more stable (2.4.0-test5). The only thing
12 * that this code assumes is that the timebases have been synchronized
13 * by firmware on SMP and are never stopped (never do sleep
14 * on SMP then, nap and doze are OK).
16 * Speeded up do_gettimeofday by getting rid of references to
17 * xtime (which required locks for consistency). (mikejc@us.ibm.com)
19 * TODO (not necessarily in this file):
20 * - improve precision and reproducibility of timebase frequency
21 * measurement at boot time. (for iSeries, we calibrate the timebase
22 * against the Titan chip's clock.)
23 * - for astronomical applications: add a new function to get
24 * non ambiguous timestamps even around leap seconds. This needs
25 * a new timestamp format and a good name.
27 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
28 * "A Kernel Model for Precision Timekeeping" by Dave Mills
30 * This program is free software; you can redistribute it and/or
31 * modify it under the terms of the GNU General Public License
32 * as published by the Free Software Foundation; either version
33 * 2 of the License, or (at your option) any later version.
36 #include <linux/config.h>
37 #include <linux/errno.h>
38 #include <linux/module.h>
39 #include <linux/sched.h>
40 #include <linux/kernel.h>
41 #include <linux/param.h>
42 #include <linux/string.h>
43 #include <linux/mm.h>
44 #include <linux/interrupt.h>
45 #include <linux/timex.h>
46 #include <linux/kernel_stat.h>
47 #include <linux/mc146818rtc.h>
48 #include <linux/time.h>
49 #include <linux/init.h>
50 #include <linux/profile.h>
51 #include <linux/cpu.h>
52 #include <linux/security.h>
54 #include <asm/segment.h>
55 #include <asm/io.h>
56 #include <asm/processor.h>
57 #include <asm/nvram.h>
58 #include <asm/cache.h>
59 #include <asm/machdep.h>
60 #ifdef CONFIG_PPC_ISERIES
61 #include <asm/iSeries/ItLpQueue.h>
62 #include <asm/iSeries/HvCallXm.h>
63 #endif
64 #include <asm/uaccess.h>
65 #include <asm/time.h>
66 #include <asm/ppcdebug.h>
67 #include <asm/prom.h>
68 #include <asm/sections.h>
69 #include <asm/systemcfg.h>
71 u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
73 EXPORT_SYMBOL(jiffies_64);
75 /* keep track of when we need to update the rtc */
76 time_t last_rtc_update;
77 extern int piranha_simulator;
78 #ifdef CONFIG_PPC_ISERIES
79 unsigned long iSeries_recal_titan = 0;
80 unsigned long iSeries_recal_tb = 0;
81 static unsigned long first_settimeofday = 1;
82 #endif
84 #define XSEC_PER_SEC (1024*1024)
86 unsigned long tb_ticks_per_jiffy;
87 unsigned long tb_ticks_per_usec = 100; /* sane default */
88 EXPORT_SYMBOL(tb_ticks_per_usec);
89 unsigned long tb_ticks_per_sec;
90 unsigned long tb_to_xs;
91 unsigned tb_to_us;
92 unsigned long processor_freq;
93 DEFINE_SPINLOCK(rtc_lock);
94 EXPORT_SYMBOL_GPL(rtc_lock);
96 unsigned long tb_to_ns_scale;
97 unsigned long tb_to_ns_shift;
99 struct gettimeofday_struct do_gtod;
101 extern unsigned long wall_jiffies;
102 extern unsigned long lpevent_count;
103 extern int smp_tb_synchronized;
105 extern struct timezone sys_tz;
107 void ppc_adjtimex(void);
109 static unsigned adjusting_time = 0;
111 unsigned long ppc_proc_freq;
112 unsigned long ppc_tb_freq;
114 static __inline__ void timer_check_rtc(void)
117 * update the rtc when needed, this should be performed on the
118 * right fraction of a second. Half or full second ?
119 * Full second works on mk48t59 clocks, others need testing.
120 * Note that this update is basically only used through
121 * the adjtimex system calls. Setting the HW clock in
122 * any other way is a /dev/rtc and userland business.
123 * This is still wrong by -0.5/+1.5 jiffies because of the
124 * timer interrupt resolution and possible delay, but here we
125 * hit a quantization limit which can only be solved by higher
126 * resolution timers and decoupling time management from timer
127 * interrupts. This is also wrong on the clocks
128 * which require being written at the half second boundary.
129 * We should have an rtc call that only sets the minutes and
130 * seconds like on Intel to avoid problems with non UTC clocks.
132 if ( (time_status & STA_UNSYNC) == 0 &&
133 xtime.tv_sec - last_rtc_update >= 659 &&
134 abs((xtime.tv_nsec/1000) - (1000000-1000000/HZ)) < 500000/HZ &&
135 jiffies - wall_jiffies == 1) {
136 struct rtc_time tm;
137 to_tm(xtime.tv_sec+1, &tm);
138 tm.tm_year -= 1900;
139 tm.tm_mon -= 1;
140 if (ppc_md.set_rtc_time(&tm) == 0)
141 last_rtc_update = xtime.tv_sec+1;
142 else
143 /* Try again one minute later */
144 last_rtc_update += 60;
149 * This version of gettimeofday has microsecond resolution.
151 static inline void __do_gettimeofday(struct timeval *tv, unsigned long tb_val)
153 unsigned long sec, usec, tb_ticks;
154 unsigned long xsec, tb_xsec;
155 struct gettimeofday_vars * temp_varp;
156 unsigned long temp_tb_to_xs, temp_stamp_xsec;
159 * These calculations are faster (gets rid of divides)
160 * if done in units of 1/2^20 rather than microseconds.
161 * The conversion to microseconds at the end is done
162 * without a divide (and in fact, without a multiply)
164 temp_varp = do_gtod.varp;
165 tb_ticks = tb_val - temp_varp->tb_orig_stamp;
166 temp_tb_to_xs = temp_varp->tb_to_xs;
167 temp_stamp_xsec = temp_varp->stamp_xsec;
168 tb_xsec = mulhdu( tb_ticks, temp_tb_to_xs );
169 xsec = temp_stamp_xsec + tb_xsec;
170 sec = xsec / XSEC_PER_SEC;
171 xsec -= sec * XSEC_PER_SEC;
172 usec = (xsec * USEC_PER_SEC)/XSEC_PER_SEC;
174 tv->tv_sec = sec;
175 tv->tv_usec = usec;
178 void do_gettimeofday(struct timeval *tv)
180 __do_gettimeofday(tv, get_tb());
183 EXPORT_SYMBOL(do_gettimeofday);
185 /* Synchronize xtime with do_gettimeofday */
187 static inline void timer_sync_xtime(unsigned long cur_tb)
189 struct timeval my_tv;
191 __do_gettimeofday(&my_tv, cur_tb);
193 if (xtime.tv_sec <= my_tv.tv_sec) {
194 xtime.tv_sec = my_tv.tv_sec;
195 xtime.tv_nsec = my_tv.tv_usec * 1000;
200 * When the timebase - tb_orig_stamp gets too big, we do a manipulation
201 * between tb_orig_stamp and stamp_xsec. The goal here is to keep the
202 * difference tb - tb_orig_stamp small enough to always fit inside a
203 * 32 bits number. This is a requirement of our fast 32 bits userland
204 * implementation in the vdso. If we "miss" a call to this function
205 * (interrupt latency, CPU locked in a spinlock, ...) and we end up
206 * with a too big difference, then the vdso will fallback to calling
207 * the syscall
209 static __inline__ void timer_recalc_offset(unsigned long cur_tb)
211 struct gettimeofday_vars * temp_varp;
212 unsigned temp_idx;
213 unsigned long offset, new_stamp_xsec, new_tb_orig_stamp;
215 if (((cur_tb - do_gtod.varp->tb_orig_stamp) & 0x80000000u) == 0)
216 return;
218 temp_idx = (do_gtod.var_idx == 0);
219 temp_varp = &do_gtod.vars[temp_idx];
221 new_tb_orig_stamp = cur_tb;
222 offset = new_tb_orig_stamp - do_gtod.varp->tb_orig_stamp;
223 new_stamp_xsec = do_gtod.varp->stamp_xsec + mulhdu(offset, do_gtod.varp->tb_to_xs);
225 temp_varp->tb_to_xs = do_gtod.varp->tb_to_xs;
226 temp_varp->tb_orig_stamp = new_tb_orig_stamp;
227 temp_varp->stamp_xsec = new_stamp_xsec;
228 smp_mb();
229 do_gtod.varp = temp_varp;
230 do_gtod.var_idx = temp_idx;
232 ++(systemcfg->tb_update_count);
233 smp_wmb();
234 systemcfg->tb_orig_stamp = new_tb_orig_stamp;
235 systemcfg->stamp_xsec = new_stamp_xsec;
236 smp_wmb();
237 ++(systemcfg->tb_update_count);
240 #ifdef CONFIG_SMP
241 unsigned long profile_pc(struct pt_regs *regs)
243 unsigned long pc = instruction_pointer(regs);
245 if (in_lock_functions(pc))
246 return regs->link;
248 return pc;
250 EXPORT_SYMBOL(profile_pc);
251 #endif
253 #ifdef CONFIG_PPC_ISERIES
256 * This function recalibrates the timebase based on the 49-bit time-of-day
257 * value in the Titan chip. The Titan is much more accurate than the value
258 * returned by the service processor for the timebase frequency.
261 static void iSeries_tb_recal(void)
263 struct div_result divres;
264 unsigned long titan, tb;
265 tb = get_tb();
266 titan = HvCallXm_loadTod();
267 if ( iSeries_recal_titan ) {
268 unsigned long tb_ticks = tb - iSeries_recal_tb;
269 unsigned long titan_usec = (titan - iSeries_recal_titan) >> 12;
270 unsigned long new_tb_ticks_per_sec = (tb_ticks * USEC_PER_SEC)/titan_usec;
271 unsigned long new_tb_ticks_per_jiffy = (new_tb_ticks_per_sec+(HZ/2))/HZ;
272 long tick_diff = new_tb_ticks_per_jiffy - tb_ticks_per_jiffy;
273 char sign = '+';
274 /* make sure tb_ticks_per_sec and tb_ticks_per_jiffy are consistent */
275 new_tb_ticks_per_sec = new_tb_ticks_per_jiffy * HZ;
277 if ( tick_diff < 0 ) {
278 tick_diff = -tick_diff;
279 sign = '-';
281 if ( tick_diff ) {
282 if ( tick_diff < tb_ticks_per_jiffy/25 ) {
283 printk( "Titan recalibrate: new tb_ticks_per_jiffy = %lu (%c%ld)\n",
284 new_tb_ticks_per_jiffy, sign, tick_diff );
285 tb_ticks_per_jiffy = new_tb_ticks_per_jiffy;
286 tb_ticks_per_sec = new_tb_ticks_per_sec;
287 div128_by_32( XSEC_PER_SEC, 0, tb_ticks_per_sec, &divres );
288 do_gtod.tb_ticks_per_sec = tb_ticks_per_sec;
289 tb_to_xs = divres.result_low;
290 do_gtod.varp->tb_to_xs = tb_to_xs;
291 systemcfg->tb_ticks_per_sec = tb_ticks_per_sec;
292 systemcfg->tb_to_xs = tb_to_xs;
294 else {
295 printk( "Titan recalibrate: FAILED (difference > 4 percent)\n"
296 " new tb_ticks_per_jiffy = %lu\n"
297 " old tb_ticks_per_jiffy = %lu\n",
298 new_tb_ticks_per_jiffy, tb_ticks_per_jiffy );
302 iSeries_recal_titan = titan;
303 iSeries_recal_tb = tb;
305 #endif
308 * For iSeries shared processors, we have to let the hypervisor
309 * set the hardware decrementer. We set a virtual decrementer
310 * in the lppaca and call the hypervisor if the virtual
311 * decrementer is less than the current value in the hardware
312 * decrementer. (almost always the new decrementer value will
313 * be greater than the current hardware decementer so the hypervisor
314 * call will not be needed)
317 unsigned long tb_last_stamp __cacheline_aligned_in_smp;
320 * timer_interrupt - gets called when the decrementer overflows,
321 * with interrupts disabled.
323 int timer_interrupt(struct pt_regs * regs)
325 int next_dec;
326 unsigned long cur_tb;
327 struct paca_struct *lpaca = get_paca();
328 unsigned long cpu = smp_processor_id();
330 irq_enter();
332 profile_tick(CPU_PROFILING, regs);
334 lpaca->lppaca.int_dword.fields.decr_int = 0;
336 while (lpaca->next_jiffy_update_tb <= (cur_tb = get_tb())) {
338 * We cannot disable the decrementer, so in the period
339 * between this cpu's being marked offline in cpu_online_map
340 * and calling stop-self, it is taking timer interrupts.
341 * Avoid calling into the scheduler rebalancing code if this
342 * is the case.
344 if (!cpu_is_offline(cpu))
345 update_process_times(user_mode(regs));
347 * No need to check whether cpu is offline here; boot_cpuid
348 * should have been fixed up by now.
350 if (cpu == boot_cpuid) {
351 write_seqlock(&xtime_lock);
352 tb_last_stamp = lpaca->next_jiffy_update_tb;
353 timer_recalc_offset(lpaca->next_jiffy_update_tb);
354 do_timer(regs);
355 timer_sync_xtime(lpaca->next_jiffy_update_tb);
356 timer_check_rtc();
357 write_sequnlock(&xtime_lock);
358 if ( adjusting_time && (time_adjust == 0) )
359 ppc_adjtimex();
361 lpaca->next_jiffy_update_tb += tb_ticks_per_jiffy;
364 next_dec = lpaca->next_jiffy_update_tb - cur_tb;
365 if (next_dec > lpaca->default_decr)
366 next_dec = lpaca->default_decr;
367 set_dec(next_dec);
369 #ifdef CONFIG_PPC_ISERIES
371 struct ItLpQueue *lpq = lpaca->lpqueue_ptr;
372 if (lpq && ItLpQueue_isLpIntPending(lpq))
373 lpevent_count += ItLpQueue_process(lpq, regs);
375 #endif
377 /* collect purr register values often, for accurate calculations */
378 #if defined(CONFIG_PPC_PSERIES)
379 if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR) {
380 struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array);
381 cu->current_tb = mfspr(SPRN_PURR);
383 #endif
385 irq_exit();
387 return 1;
391 * Scheduler clock - returns current time in nanosec units.
393 * Note: mulhdu(a, b) (multiply high double unsigned) returns
394 * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b
395 * are 64-bit unsigned numbers.
397 unsigned long long sched_clock(void)
399 return mulhdu(get_tb(), tb_to_ns_scale) << tb_to_ns_shift;
402 int do_settimeofday(struct timespec *tv)
404 time_t wtm_sec, new_sec = tv->tv_sec;
405 long wtm_nsec, new_nsec = tv->tv_nsec;
406 unsigned long flags;
407 unsigned long delta_xsec;
408 long int tb_delta;
409 unsigned long new_xsec;
411 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
412 return -EINVAL;
414 write_seqlock_irqsave(&xtime_lock, flags);
415 /* Updating the RTC is not the job of this code. If the time is
416 * stepped under NTP, the RTC will be update after STA_UNSYNC
417 * is cleared. Tool like clock/hwclock either copy the RTC
418 * to the system time, in which case there is no point in writing
419 * to the RTC again, or write to the RTC but then they don't call
420 * settimeofday to perform this operation.
422 #ifdef CONFIG_PPC_ISERIES
423 if ( first_settimeofday ) {
424 iSeries_tb_recal();
425 first_settimeofday = 0;
427 #endif
428 tb_delta = tb_ticks_since(tb_last_stamp);
429 tb_delta += (jiffies - wall_jiffies) * tb_ticks_per_jiffy;
431 new_nsec -= tb_delta / tb_ticks_per_usec / 1000;
433 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - new_sec);
434 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - new_nsec);
436 set_normalized_timespec(&xtime, new_sec, new_nsec);
437 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
439 /* In case of a large backwards jump in time with NTP, we want the
440 * clock to be updated as soon as the PLL is again in lock.
442 last_rtc_update = new_sec - 658;
444 time_adjust = 0; /* stop active adjtime() */
445 time_status |= STA_UNSYNC;
446 time_maxerror = NTP_PHASE_LIMIT;
447 time_esterror = NTP_PHASE_LIMIT;
449 delta_xsec = mulhdu( (tb_last_stamp-do_gtod.varp->tb_orig_stamp),
450 do_gtod.varp->tb_to_xs );
452 new_xsec = (new_nsec * XSEC_PER_SEC) / NSEC_PER_SEC;
453 new_xsec += new_sec * XSEC_PER_SEC;
454 if ( new_xsec > delta_xsec ) {
455 do_gtod.varp->stamp_xsec = new_xsec - delta_xsec;
456 systemcfg->stamp_xsec = new_xsec - delta_xsec;
458 else {
459 /* This is only for the case where the user is setting the time
460 * way back to a time such that the boot time would have been
461 * before 1970 ... eg. we booted ten days ago, and we are setting
462 * the time to Jan 5, 1970 */
463 do_gtod.varp->stamp_xsec = new_xsec;
464 do_gtod.varp->tb_orig_stamp = tb_last_stamp;
465 systemcfg->stamp_xsec = new_xsec;
466 systemcfg->tb_orig_stamp = tb_last_stamp;
469 systemcfg->tz_minuteswest = sys_tz.tz_minuteswest;
470 systemcfg->tz_dsttime = sys_tz.tz_dsttime;
472 write_sequnlock_irqrestore(&xtime_lock, flags);
473 clock_was_set();
474 return 0;
477 EXPORT_SYMBOL(do_settimeofday);
479 #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_MAPLE) || defined(CONFIG_PPC_BPA)
480 void __init generic_calibrate_decr(void)
482 struct device_node *cpu;
483 struct div_result divres;
484 unsigned int *fp;
485 int node_found;
488 * The cpu node should have a timebase-frequency property
489 * to tell us the rate at which the decrementer counts.
491 cpu = of_find_node_by_type(NULL, "cpu");
493 ppc_tb_freq = DEFAULT_TB_FREQ; /* hardcoded default */
494 node_found = 0;
495 if (cpu != 0) {
496 fp = (unsigned int *)get_property(cpu, "timebase-frequency",
497 NULL);
498 if (fp != 0) {
499 node_found = 1;
500 ppc_tb_freq = *fp;
503 if (!node_found)
504 printk(KERN_ERR "WARNING: Estimating decrementer frequency "
505 "(not found)\n");
507 ppc_proc_freq = DEFAULT_PROC_FREQ;
508 node_found = 0;
509 if (cpu != 0) {
510 fp = (unsigned int *)get_property(cpu, "clock-frequency",
511 NULL);
512 if (fp != 0) {
513 node_found = 1;
514 ppc_proc_freq = *fp;
517 if (!node_found)
518 printk(KERN_ERR "WARNING: Estimating processor frequency "
519 "(not found)\n");
521 of_node_put(cpu);
523 printk(KERN_INFO "time_init: decrementer frequency = %lu.%.6lu MHz\n",
524 ppc_tb_freq/1000000, ppc_tb_freq%1000000);
525 printk(KERN_INFO "time_init: processor frequency = %lu.%.6lu MHz\n",
526 ppc_proc_freq/1000000, ppc_proc_freq%1000000);
528 tb_ticks_per_jiffy = ppc_tb_freq / HZ;
529 tb_ticks_per_sec = tb_ticks_per_jiffy * HZ;
530 tb_ticks_per_usec = ppc_tb_freq / 1000000;
531 tb_to_us = mulhwu_scale_factor(ppc_tb_freq, 1000000);
532 div128_by_32(1024*1024, 0, tb_ticks_per_sec, &divres);
533 tb_to_xs = divres.result_low;
535 setup_default_decr();
537 #endif
539 void __init time_init(void)
541 /* This function is only called on the boot processor */
542 unsigned long flags;
543 struct rtc_time tm;
544 struct div_result res;
545 unsigned long scale, shift;
547 ppc_md.calibrate_decr();
550 * Compute scale factor for sched_clock.
551 * The calibrate_decr() function has set tb_ticks_per_sec,
552 * which is the timebase frequency.
553 * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret
554 * the 128-bit result as a 64.64 fixed-point number.
555 * We then shift that number right until it is less than 1.0,
556 * giving us the scale factor and shift count to use in
557 * sched_clock().
559 div128_by_32(1000000000, 0, tb_ticks_per_sec, &res);
560 scale = res.result_low;
561 for (shift = 0; res.result_high != 0; ++shift) {
562 scale = (scale >> 1) | (res.result_high << 63);
563 res.result_high >>= 1;
565 tb_to_ns_scale = scale;
566 tb_to_ns_shift = shift;
568 #ifdef CONFIG_PPC_ISERIES
569 if (!piranha_simulator)
570 #endif
571 ppc_md.get_boot_time(&tm);
573 write_seqlock_irqsave(&xtime_lock, flags);
574 xtime.tv_sec = mktime(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
575 tm.tm_hour, tm.tm_min, tm.tm_sec);
576 tb_last_stamp = get_tb();
577 do_gtod.varp = &do_gtod.vars[0];
578 do_gtod.var_idx = 0;
579 do_gtod.varp->tb_orig_stamp = tb_last_stamp;
580 get_paca()->next_jiffy_update_tb = tb_last_stamp + tb_ticks_per_jiffy;
581 do_gtod.varp->stamp_xsec = xtime.tv_sec * XSEC_PER_SEC;
582 do_gtod.tb_ticks_per_sec = tb_ticks_per_sec;
583 do_gtod.varp->tb_to_xs = tb_to_xs;
584 do_gtod.tb_to_us = tb_to_us;
585 systemcfg->tb_orig_stamp = tb_last_stamp;
586 systemcfg->tb_update_count = 0;
587 systemcfg->tb_ticks_per_sec = tb_ticks_per_sec;
588 systemcfg->stamp_xsec = xtime.tv_sec * XSEC_PER_SEC;
589 systemcfg->tb_to_xs = tb_to_xs;
591 time_freq = 0;
593 xtime.tv_nsec = 0;
594 last_rtc_update = xtime.tv_sec;
595 set_normalized_timespec(&wall_to_monotonic,
596 -xtime.tv_sec, -xtime.tv_nsec);
597 write_sequnlock_irqrestore(&xtime_lock, flags);
599 /* Not exact, but the timer interrupt takes care of this */
600 set_dec(tb_ticks_per_jiffy);
604 * After adjtimex is called, adjust the conversion of tb ticks
605 * to microseconds to keep do_gettimeofday synchronized
606 * with ntpd.
608 * Use the time_adjust, time_freq and time_offset computed by adjtimex to
609 * adjust the frequency.
612 /* #define DEBUG_PPC_ADJTIMEX 1 */
614 void ppc_adjtimex(void)
616 unsigned long den, new_tb_ticks_per_sec, tb_ticks, old_xsec, new_tb_to_xs, new_xsec, new_stamp_xsec;
617 unsigned long tb_ticks_per_sec_delta;
618 long delta_freq, ltemp;
619 struct div_result divres;
620 unsigned long flags;
621 struct gettimeofday_vars * temp_varp;
622 unsigned temp_idx;
623 long singleshot_ppm = 0;
625 /* Compute parts per million frequency adjustment to accomplish the time adjustment
626 implied by time_offset to be applied over the elapsed time indicated by time_constant.
627 Use SHIFT_USEC to get it into the same units as time_freq. */
628 if ( time_offset < 0 ) {
629 ltemp = -time_offset;
630 ltemp <<= SHIFT_USEC - SHIFT_UPDATE;
631 ltemp >>= SHIFT_KG + time_constant;
632 ltemp = -ltemp;
634 else {
635 ltemp = time_offset;
636 ltemp <<= SHIFT_USEC - SHIFT_UPDATE;
637 ltemp >>= SHIFT_KG + time_constant;
640 /* If there is a single shot time adjustment in progress */
641 if ( time_adjust ) {
642 #ifdef DEBUG_PPC_ADJTIMEX
643 printk("ppc_adjtimex: ");
644 if ( adjusting_time == 0 )
645 printk("starting ");
646 printk("single shot time_adjust = %ld\n", time_adjust);
647 #endif
649 adjusting_time = 1;
651 /* Compute parts per million frequency adjustment to match time_adjust */
652 singleshot_ppm = tickadj * HZ;
654 * The adjustment should be tickadj*HZ to match the code in
655 * linux/kernel/timer.c, but experiments show that this is too
656 * large. 3/4 of tickadj*HZ seems about right
658 singleshot_ppm -= singleshot_ppm / 4;
659 /* Use SHIFT_USEC to get it into the same units as time_freq */
660 singleshot_ppm <<= SHIFT_USEC;
661 if ( time_adjust < 0 )
662 singleshot_ppm = -singleshot_ppm;
664 else {
665 #ifdef DEBUG_PPC_ADJTIMEX
666 if ( adjusting_time )
667 printk("ppc_adjtimex: ending single shot time_adjust\n");
668 #endif
669 adjusting_time = 0;
672 /* Add up all of the frequency adjustments */
673 delta_freq = time_freq + ltemp + singleshot_ppm;
675 /* Compute a new value for tb_ticks_per_sec based on the frequency adjustment */
676 den = 1000000 * (1 << (SHIFT_USEC - 8));
677 if ( delta_freq < 0 ) {
678 tb_ticks_per_sec_delta = ( tb_ticks_per_sec * ( (-delta_freq) >> (SHIFT_USEC - 8))) / den;
679 new_tb_ticks_per_sec = tb_ticks_per_sec + tb_ticks_per_sec_delta;
681 else {
682 tb_ticks_per_sec_delta = ( tb_ticks_per_sec * ( delta_freq >> (SHIFT_USEC - 8))) / den;
683 new_tb_ticks_per_sec = tb_ticks_per_sec - tb_ticks_per_sec_delta;
686 #ifdef DEBUG_PPC_ADJTIMEX
687 printk("ppc_adjtimex: ltemp = %ld, time_freq = %ld, singleshot_ppm = %ld\n", ltemp, time_freq, singleshot_ppm);
688 printk("ppc_adjtimex: tb_ticks_per_sec - base = %ld new = %ld\n", tb_ticks_per_sec, new_tb_ticks_per_sec);
689 #endif
691 /* Compute a new value of tb_to_xs (used to convert tb to microseconds and a new value of
692 stamp_xsec which is the time (in 1/2^20 second units) corresponding to tb_orig_stamp. This
693 new value of stamp_xsec compensates for the change in frequency (implied by the new tb_to_xs)
694 which guarantees that the current time remains the same */
695 write_seqlock_irqsave( &xtime_lock, flags );
696 tb_ticks = get_tb() - do_gtod.varp->tb_orig_stamp;
697 div128_by_32( 1024*1024, 0, new_tb_ticks_per_sec, &divres );
698 new_tb_to_xs = divres.result_low;
699 new_xsec = mulhdu( tb_ticks, new_tb_to_xs );
701 old_xsec = mulhdu( tb_ticks, do_gtod.varp->tb_to_xs );
702 new_stamp_xsec = do_gtod.varp->stamp_xsec + old_xsec - new_xsec;
704 /* There are two copies of tb_to_xs and stamp_xsec so that no lock is needed to access and use these
705 values in do_gettimeofday. We alternate the copies and as long as a reasonable time elapses between
706 changes, there will never be inconsistent values. ntpd has a minimum of one minute between updates */
708 temp_idx = (do_gtod.var_idx == 0);
709 temp_varp = &do_gtod.vars[temp_idx];
711 temp_varp->tb_to_xs = new_tb_to_xs;
712 temp_varp->stamp_xsec = new_stamp_xsec;
713 temp_varp->tb_orig_stamp = do_gtod.varp->tb_orig_stamp;
714 smp_mb();
715 do_gtod.varp = temp_varp;
716 do_gtod.var_idx = temp_idx;
719 * tb_update_count is used to allow the problem state gettimeofday code
720 * to assure itself that it sees a consistent view of the tb_to_xs and
721 * stamp_xsec variables. It reads the tb_update_count, then reads
722 * tb_to_xs and stamp_xsec and then reads tb_update_count again. If
723 * the two values of tb_update_count match and are even then the
724 * tb_to_xs and stamp_xsec values are consistent. If not, then it
725 * loops back and reads them again until this criteria is met.
727 ++(systemcfg->tb_update_count);
728 smp_wmb();
729 systemcfg->tb_to_xs = new_tb_to_xs;
730 systemcfg->stamp_xsec = new_stamp_xsec;
731 smp_wmb();
732 ++(systemcfg->tb_update_count);
734 write_sequnlock_irqrestore( &xtime_lock, flags );
739 #define TICK_SIZE tick
740 #define FEBRUARY 2
741 #define STARTOFTIME 1970
742 #define SECDAY 86400L
743 #define SECYR (SECDAY * 365)
744 #define leapyear(year) ((year) % 4 == 0)
745 #define days_in_year(a) (leapyear(a) ? 366 : 365)
746 #define days_in_month(a) (month_days[(a) - 1])
748 static int month_days[12] = {
749 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
753 * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
755 void GregorianDay(struct rtc_time * tm)
757 int leapsToDate;
758 int lastYear;
759 int day;
760 int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
762 lastYear=tm->tm_year-1;
765 * Number of leap corrections to apply up to end of last year
767 leapsToDate = lastYear/4 - lastYear/100 + lastYear/400;
770 * This year is a leap year if it is divisible by 4 except when it is
771 * divisible by 100 unless it is divisible by 400
773 * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 will be
775 if((tm->tm_year%4==0) &&
776 ((tm->tm_year%100!=0) || (tm->tm_year%400==0)) &&
777 (tm->tm_mon>2))
780 * We are past Feb. 29 in a leap year
782 day=1;
784 else
786 day=0;
789 day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
790 tm->tm_mday;
792 tm->tm_wday=day%7;
795 void to_tm(int tim, struct rtc_time * tm)
797 register int i;
798 register long hms, day;
800 day = tim / SECDAY;
801 hms = tim % SECDAY;
803 /* Hours, minutes, seconds are easy */
804 tm->tm_hour = hms / 3600;
805 tm->tm_min = (hms % 3600) / 60;
806 tm->tm_sec = (hms % 3600) % 60;
808 /* Number of years in days */
809 for (i = STARTOFTIME; day >= days_in_year(i); i++)
810 day -= days_in_year(i);
811 tm->tm_year = i;
813 /* Number of months in days left */
814 if (leapyear(tm->tm_year))
815 days_in_month(FEBRUARY) = 29;
816 for (i = 1; day >= days_in_month(i); i++)
817 day -= days_in_month(i);
818 days_in_month(FEBRUARY) = 28;
819 tm->tm_mon = i;
821 /* Days are what is left over (+1) from all that. */
822 tm->tm_mday = day + 1;
825 * Determine the day of week
827 GregorianDay(tm);
830 /* Auxiliary function to compute scaling factors */
831 /* Actually the choice of a timebase running at 1/4 the of the bus
832 * frequency giving resolution of a few tens of nanoseconds is quite nice.
833 * It makes this computation very precise (27-28 bits typically) which
834 * is optimistic considering the stability of most processor clock
835 * oscillators and the precision with which the timebase frequency
836 * is measured but does not harm.
838 unsigned mulhwu_scale_factor(unsigned inscale, unsigned outscale) {
839 unsigned mlt=0, tmp, err;
840 /* No concern for performance, it's done once: use a stupid
841 * but safe and compact method to find the multiplier.
844 for (tmp = 1U<<31; tmp != 0; tmp >>= 1) {
845 if (mulhwu(inscale, mlt|tmp) < outscale) mlt|=tmp;
848 /* We might still be off by 1 for the best approximation.
849 * A side effect of this is that if outscale is too large
850 * the returned value will be zero.
851 * Many corner cases have been checked and seem to work,
852 * some might have been forgotten in the test however.
855 err = inscale*(mlt+1);
856 if (err <= inscale/2) mlt++;
857 return mlt;
861 * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
862 * result.
865 void div128_by_32( unsigned long dividend_high, unsigned long dividend_low,
866 unsigned divisor, struct div_result *dr )
868 unsigned long a,b,c,d, w,x,y,z, ra,rb,rc;
870 a = dividend_high >> 32;
871 b = dividend_high & 0xffffffff;
872 c = dividend_low >> 32;
873 d = dividend_low & 0xffffffff;
875 w = a/divisor;
876 ra = (a - (w * divisor)) << 32;
878 x = (ra + b)/divisor;
879 rb = ((ra + b) - (x * divisor)) << 32;
881 y = (rb + c)/divisor;
882 rc = ((rb + b) - (y * divisor)) << 32;
884 z = (rc + d)/divisor;
886 dr->result_high = (w << 32) + x;
887 dr->result_low = (y << 32) + z;