Merge branch 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee13...
[pandora-kernel.git] / arch / ppc / kernel / time.c
1 /*
2  * Common time routines among all ppc machines.
3  *
4  * Written by Cort Dougan (cort@cs.nmt.edu) to merge
5  * Paul Mackerras' version and mine for PReP and Pmac.
6  * MPC8xx/MBX changes by Dan Malek (dmalek@jlc.net).
7  *
8  * First round of bugfixes by Gabriel Paubert (paubert@iram.es)
9  * to make clock more stable (2.4.0-test5). The only thing
10  * that this code assumes is that the timebases have been synchronized
11  * by firmware on SMP and are never stopped (never do sleep
12  * on SMP then, nap and doze are OK).
13  *
14  * TODO (not necessarily in this file):
15  * - improve precision and reproducibility of timebase frequency
16  * measurement at boot time.
17  * - get rid of xtime_lock for gettimeofday (generic kernel problem
18  * to be implemented on all architectures for SMP scalability and
19  * eventually implementing gettimeofday without entering the kernel).
20  * - put all time/clock related variables in a single structure
21  * to minimize number of cache lines touched by gettimeofday()
22  * - for astronomical applications: add a new function to get
23  * non ambiguous timestamps even around leap seconds. This needs
24  * a new timestamp format and a good name.
25  *
26  *
27  * The following comment is partially obsolete (at least the long wait
28  * is no more a valid reason):
29  * Since the MPC8xx has a programmable interrupt timer, I decided to
30  * use that rather than the decrementer.  Two reasons: 1.) the clock
31  * frequency is low, causing 2.) a long wait in the timer interrupt
32  *              while ((d = get_dec()) == dval)
33  * loop.  The MPC8xx can be driven from a variety of input clocks,
34  * so a number of assumptions have been made here because the kernel
35  * parameter HZ is a constant.  We assume (correctly, today :-) that
36  * the MPC8xx on the MBX board is driven from a 32.768 kHz crystal.
37  * This is then divided by 4, providing a 8192 Hz clock into the PIT.
38  * Since it is not possible to get a nice 100 Hz clock out of this, without
39  * creating a software PLL, I have set HZ to 128.  -- Dan
40  *
41  * 1997-09-10  Updated NTP code according to technical memorandum Jan '96
42  *             "A Kernel Model for Precision Timekeeping" by Dave Mills
43  */
44
45 #include <linux/errno.h>
46 #include <linux/sched.h>
47 #include <linux/kernel.h>
48 #include <linux/param.h>
49 #include <linux/string.h>
50 #include <linux/mm.h>
51 #include <linux/module.h>
52 #include <linux/interrupt.h>
53 #include <linux/timex.h>
54 #include <linux/kernel_stat.h>
55 #include <linux/mc146818rtc.h>
56 #include <linux/time.h>
57 #include <linux/init.h>
58 #include <linux/profile.h>
59
60 #include <asm/io.h>
61 #include <asm/nvram.h>
62 #include <asm/cache.h>
63 #include <asm/8xx_immap.h>
64 #include <asm/machdep.h>
65
66 #include <asm/time.h>
67
68 unsigned long disarm_decr[NR_CPUS];
69
70 extern struct timezone sys_tz;
71
72 /* keep track of when we need to update the rtc */
73 time_t last_rtc_update;
74
75 /* The decrementer counts down by 128 every 128ns on a 601. */
76 #define DECREMENTER_COUNT_601   (1000000000 / HZ)
77
78 unsigned tb_ticks_per_jiffy;
79 unsigned tb_to_us;
80 unsigned tb_last_stamp;
81 unsigned long tb_to_ns_scale;
82
83 /* used for timezone offset */
84 static long timezone_offset;
85
86 DEFINE_SPINLOCK(rtc_lock);
87
88 EXPORT_SYMBOL(rtc_lock);
89
90 /* Timer interrupt helper function */
91 static inline int tb_delta(unsigned *jiffy_stamp) {
92         int delta;
93         if (__USE_RTC()) {
94                 delta = get_rtcl();
95                 if (delta < *jiffy_stamp) *jiffy_stamp -= 1000000000;
96                 delta -= *jiffy_stamp;
97         } else {
98                 delta = get_tbl() - *jiffy_stamp;
99         }
100         return delta;
101 }
102
103 #ifdef CONFIG_SMP
104 unsigned long profile_pc(struct pt_regs *regs)
105 {
106         unsigned long pc = instruction_pointer(regs);
107
108         if (in_lock_functions(pc))
109                 return regs->link;
110
111         return pc;
112 }
113 EXPORT_SYMBOL(profile_pc);
114 #endif
115
116 void wakeup_decrementer(void)
117 {
118         set_dec(tb_ticks_per_jiffy);
119         /* No currently-supported powerbook has a 601,
120          * so use get_tbl, not native
121          */
122         last_jiffy_stamp(0) = tb_last_stamp = get_tbl();
123 }
124
125 /*
126  * timer_interrupt - gets called when the decrementer overflows,
127  * with interrupts disabled.
128  * We set it up to overflow again in 1/HZ seconds.
129  */
130 void timer_interrupt(struct pt_regs * regs)
131 {
132         int next_dec;
133         unsigned long cpu = smp_processor_id();
134         unsigned jiffy_stamp = last_jiffy_stamp(cpu);
135         extern void do_IRQ(struct pt_regs *);
136
137         if (atomic_read(&ppc_n_lost_interrupts) != 0)
138                 do_IRQ(regs);
139
140         irq_enter();
141
142         while ((next_dec = tb_ticks_per_jiffy - tb_delta(&jiffy_stamp)) <= 0) {
143                 jiffy_stamp += tb_ticks_per_jiffy;
144                 
145                 profile_tick(CPU_PROFILING);
146                 update_process_times(user_mode(regs));
147
148                 if (smp_processor_id())
149                         continue;
150
151                 /* We are in an interrupt, no need to save/restore flags */
152                 write_seqlock(&xtime_lock);
153                 tb_last_stamp = jiffy_stamp;
154                 do_timer(1);
155
156                 /*
157                  * update the rtc when needed, this should be performed on the
158                  * right fraction of a second. Half or full second ?
159                  * Full second works on mk48t59 clocks, others need testing.
160                  * Note that this update is basically only used through
161                  * the adjtimex system calls. Setting the HW clock in
162                  * any other way is a /dev/rtc and userland business.
163                  * This is still wrong by -0.5/+1.5 jiffies because of the
164                  * timer interrupt resolution and possible delay, but here we
165                  * hit a quantization limit which can only be solved by higher
166                  * resolution timers and decoupling time management from timer
167                  * interrupts. This is also wrong on the clocks
168                  * which require being written at the half second boundary.
169                  * We should have an rtc call that only sets the minutes and
170                  * seconds like on Intel to avoid problems with non UTC clocks.
171                  */
172                 if ( ppc_md.set_rtc_time && ntp_synced() &&
173                      xtime.tv_sec - last_rtc_update >= 659 &&
174                      abs((xtime.tv_nsec / 1000) - (1000000-1000000/HZ)) < 500000/HZ) {
175                         if (ppc_md.set_rtc_time(xtime.tv_sec+1 + timezone_offset) == 0)
176                                 last_rtc_update = xtime.tv_sec+1;
177                         else
178                                 /* Try again one minute later */
179                                 last_rtc_update += 60;
180                 }
181                 write_sequnlock(&xtime_lock);
182         }
183         if ( !disarm_decr[smp_processor_id()] )
184                 set_dec(next_dec);
185         last_jiffy_stamp(cpu) = jiffy_stamp;
186
187         if (ppc_md.heartbeat && !ppc_md.heartbeat_count--)
188                 ppc_md.heartbeat();
189
190         irq_exit();
191 }
192
193 /*
194  * This version of gettimeofday has microsecond resolution.
195  */
196 void do_gettimeofday(struct timeval *tv)
197 {
198         unsigned long flags;
199         unsigned long seq;
200         unsigned delta, usec, sec;
201
202         do {
203                 seq = read_seqbegin_irqsave(&xtime_lock, flags);
204                 sec = xtime.tv_sec;
205                 usec = (xtime.tv_nsec / 1000);
206                 delta = tb_ticks_since(tb_last_stamp);
207 #ifdef CONFIG_SMP
208                 /* As long as timebases are not in sync, gettimeofday can only
209                  * have jiffy resolution on SMP.
210                  */
211                 if (!smp_tb_synchronized)
212                         delta = 0;
213 #endif /* CONFIG_SMP */
214         } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
215
216         usec += mulhwu(tb_to_us, delta);
217         while (usec >= 1000000) {
218                 sec++;
219                 usec -= 1000000;
220         }
221         tv->tv_sec = sec;
222         tv->tv_usec = usec;
223 }
224
225 EXPORT_SYMBOL(do_gettimeofday);
226
227 int do_settimeofday(struct timespec *tv)
228 {
229         time_t wtm_sec, new_sec = tv->tv_sec;
230         long wtm_nsec, new_nsec = tv->tv_nsec;
231         unsigned long flags;
232         int tb_delta;
233
234         if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
235                 return -EINVAL;
236
237         write_seqlock_irqsave(&xtime_lock, flags);
238         /* Updating the RTC is not the job of this code. If the time is
239          * stepped under NTP, the RTC will be update after STA_UNSYNC
240          * is cleared. Tool like clock/hwclock either copy the RTC
241          * to the system time, in which case there is no point in writing
242          * to the RTC again, or write to the RTC but then they don't call
243          * settimeofday to perform this operation. Note also that
244          * we don't touch the decrementer since:
245          * a) it would lose timer interrupt synchronization on SMP
246          * (if it is working one day)
247          * b) it could make one jiffy spuriously shorter or longer
248          * which would introduce another source of uncertainty potentially
249          * harmful to relatively short timers.
250          */
251
252         /* This works perfectly on SMP only if the tb are in sync but
253          * guarantees an error < 1 jiffy even if they are off by eons,
254          * still reasonable when gettimeofday resolution is 1 jiffy.
255          */
256         tb_delta = tb_ticks_since(last_jiffy_stamp(smp_processor_id()));
257
258         new_nsec -= 1000 * mulhwu(tb_to_us, tb_delta);
259
260         wtm_sec  = wall_to_monotonic.tv_sec + (xtime.tv_sec - new_sec);
261         wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - new_nsec);
262
263         set_normalized_timespec(&xtime, new_sec, new_nsec);
264         set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
265
266         /* In case of a large backwards jump in time with NTP, we want the
267          * clock to be updated as soon as the PLL is again in lock.
268          */
269         last_rtc_update = new_sec - 658;
270
271         ntp_clear();
272         write_sequnlock_irqrestore(&xtime_lock, flags);
273         clock_was_set();
274         return 0;
275 }
276
277 EXPORT_SYMBOL(do_settimeofday);
278
279 /* This function is only called on the boot processor */
280 void __init time_init(void)
281 {
282         time_t sec, old_sec;
283         unsigned old_stamp, stamp, elapsed;
284
285         if (ppc_md.time_init != NULL)
286                 timezone_offset = ppc_md.time_init();
287
288         if (__USE_RTC()) {
289                 /* 601 processor: dec counts down by 128 every 128ns */
290                 tb_ticks_per_jiffy = DECREMENTER_COUNT_601;
291                 /* mulhwu_scale_factor(1000000000, 1000000) is 0x418937 */
292                 tb_to_us = 0x418937;
293         } else {
294                 ppc_md.calibrate_decr();
295                 tb_to_ns_scale = mulhwu(tb_to_us, 1000 << 10);
296         }
297
298         /* Now that the decrementer is calibrated, it can be used in case the
299          * clock is stuck, but the fact that we have to handle the 601
300          * makes things more complex. Repeatedly read the RTC until the
301          * next second boundary to try to achieve some precision.  If there
302          * is no RTC, we still need to set tb_last_stamp and
303          * last_jiffy_stamp(cpu 0) to the current stamp.
304          */
305         stamp = get_native_tbl();
306         if (ppc_md.get_rtc_time) {
307                 sec = ppc_md.get_rtc_time();
308                 elapsed = 0;
309                 do {
310                         old_stamp = stamp;
311                         old_sec = sec;
312                         stamp = get_native_tbl();
313                         if (__USE_RTC() && stamp < old_stamp)
314                                 old_stamp -= 1000000000;
315                         elapsed += stamp - old_stamp;
316                         sec = ppc_md.get_rtc_time();
317                 } while ( sec == old_sec && elapsed < 2*HZ*tb_ticks_per_jiffy);
318                 if (sec==old_sec)
319                         printk("Warning: real time clock seems stuck!\n");
320                 xtime.tv_sec = sec;
321                 xtime.tv_nsec = 0;
322                 /* No update now, we just read the time from the RTC ! */
323                 last_rtc_update = xtime.tv_sec;
324         }
325         last_jiffy_stamp(0) = tb_last_stamp = stamp;
326
327         /* Not exact, but the timer interrupt takes care of this */
328         set_dec(tb_ticks_per_jiffy);
329
330         /* If platform provided a timezone (pmac), we correct the time */
331         if (timezone_offset) {
332                 sys_tz.tz_minuteswest = -timezone_offset / 60;
333                 sys_tz.tz_dsttime = 0;
334                 xtime.tv_sec -= timezone_offset;
335         }
336         set_normalized_timespec(&wall_to_monotonic,
337                                 -xtime.tv_sec, -xtime.tv_nsec);
338 }
339
340 #define FEBRUARY                2
341 #define STARTOFTIME             1970
342 #define SECDAY                  86400L
343 #define SECYR                   (SECDAY * 365)
344
345 /*
346  * Note: this is wrong for 2100, but our signed 32-bit time_t will
347  * have overflowed long before that, so who cares.  -- paulus
348  */
349 #define leapyear(year)          ((year) % 4 == 0)
350 #define days_in_year(a)         (leapyear(a) ? 366 : 365)
351 #define days_in_month(a)        (month_days[(a) - 1])
352
353 static int month_days[12] = {
354         31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
355 };
356
357 void to_tm(int tim, struct rtc_time * tm)
358 {
359         register int i;
360         register long hms, day, gday;
361
362         gday = day = tim / SECDAY;
363         hms = tim % SECDAY;
364
365         /* Hours, minutes, seconds are easy */
366         tm->tm_hour = hms / 3600;
367         tm->tm_min = (hms % 3600) / 60;
368         tm->tm_sec = (hms % 3600) % 60;
369
370         /* Number of years in days */
371         for (i = STARTOFTIME; day >= days_in_year(i); i++)
372                 day -= days_in_year(i);
373         tm->tm_year = i;
374
375         /* Number of months in days left */
376         if (leapyear(tm->tm_year))
377                 days_in_month(FEBRUARY) = 29;
378         for (i = 1; day >= days_in_month(i); i++)
379                 day -= days_in_month(i);
380         days_in_month(FEBRUARY) = 28;
381         tm->tm_mon = i;
382
383         /* Days are what is left over (+1) from all that. */
384         tm->tm_mday = day + 1;
385
386         /*
387          * Determine the day of week. Jan. 1, 1970 was a Thursday.
388          */
389         tm->tm_wday = (gday + 4) % 7;
390 }
391
392 /* Auxiliary function to compute scaling factors */
393 /* Actually the choice of a timebase running at 1/4 the of the bus
394  * frequency giving resolution of a few tens of nanoseconds is quite nice.
395  * It makes this computation very precise (27-28 bits typically) which
396  * is optimistic considering the stability of most processor clock
397  * oscillators and the precision with which the timebase frequency
398  * is measured but does not harm.
399  */
400 unsigned mulhwu_scale_factor(unsigned inscale, unsigned outscale) {
401         unsigned mlt=0, tmp, err;
402         /* No concern for performance, it's done once: use a stupid
403          * but safe and compact method to find the multiplier.
404          */
405         for (tmp = 1U<<31; tmp != 0; tmp >>= 1) {
406                 if (mulhwu(inscale, mlt|tmp) < outscale) mlt|=tmp;
407         }
408         /* We might still be off by 1 for the best approximation.
409          * A side effect of this is that if outscale is too large
410          * the returned value will be zero.
411          * Many corner cases have been checked and seem to work,
412          * some might have been forgotten in the test however.
413          */
414         err = inscale*(mlt+1);
415         if (err <= inscale/2) mlt++;
416         return mlt;
417 }
418
419 unsigned long long sched_clock(void)
420 {
421         unsigned long lo, hi, hi2;
422         unsigned long long tb;
423
424         if (!__USE_RTC()) {
425                 do {
426                         hi = get_tbu();
427                         lo = get_tbl();
428                         hi2 = get_tbu();
429                 } while (hi2 != hi);
430                 tb = ((unsigned long long) hi << 32) | lo;
431                 tb = (tb * tb_to_ns_scale) >> 10;
432         } else {
433                 do {
434                         hi = get_rtcu();
435                         lo = get_rtcl();
436                         hi2 = get_rtcu();
437                 } while (hi2 != hi);
438                 tb = ((unsigned long long) hi) * 1000000000 + lo;
439         }
440         return tb;
441 }