www.usr.com/support/gpl/USR9113_release1.0.tar.gz
[bcm963xx.git] / kernel / linux / arch / mips / kernel / time.c
1 /*
2  * Copyright 2001 MontaVista Software Inc.
3  * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
4  * Copyright (c) 2003  Maciej W. Rozycki
5  *
6  * Common time service routines for MIPS machines. See
7  * Documentation/mips/time.README.
8  *
9  * This program is free software; you can redistribute  it and/or modify it
10  * under  the terms of  the GNU General  Public License as published by the
11  * Free Software Foundation;  either version 2 of the  License, or (at your
12  * option) any later version.
13  */
14 #include <linux/config.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/sched.h>
19 #include <linux/param.h>
20 #include <linux/time.h>
21 #include <linux/timex.h>
22 #include <linux/smp.h>
23 #include <linux/kernel_stat.h>
24 #include <linux/spinlock.h>
25 #include <linux/interrupt.h>
26 #include <linux/module.h>
27
28 #include <asm/bootinfo.h>
29 #include <asm/cpu.h>
30 #include <asm/cpu-features.h>
31 #include <asm/div64.h>
32 #include <asm/hardirq.h>
33 #include <asm/sections.h>
34 #include <asm/time.h>
35
36 /*
37  * The integer part of the number of usecs per jiffy is taken from tick,
38  * but the fractional part is not recorded, so we calculate it using the
39  * initial value of HZ.  This aids systems where tick isn't really an
40  * integer (e.g. for HZ = 128).
41  */
42 #define USECS_PER_JIFFY         TICK_SIZE
43 #define USECS_PER_JIFFY_FRAC    ((unsigned long)(u32)((1000000ULL << 32) / HZ))
44
45 #define TICK_SIZE       (tick_nsec / 1000)
46
47 u64 jiffies_64 = INITIAL_JIFFIES;
48
49 EXPORT_SYMBOL(jiffies_64);
50
51 /*
52  * forward reference
53  */
54 extern volatile unsigned long wall_jiffies;
55
56 spinlock_t rtc_lock = SPIN_LOCK_UNLOCKED;
57
58 /*
59  * whether we emulate local_timer_interrupts for SMP machines.
60  */
61 int emulate_local_timer_interrupt;
62
63
64 /*
65  * By default we provide the null RTC ops
66  */
67 static unsigned long null_rtc_get_time(void)
68 {
69         return mktime(2000, 1, 1, 0, 0, 0);
70 }
71
72 static int null_rtc_set_time(unsigned long sec)
73 {
74         return 0;
75 }
76
77 unsigned long (*rtc_get_time)(void) = null_rtc_get_time;
78 int (*rtc_set_time)(unsigned long) = null_rtc_set_time;
79 int (*rtc_set_mmss)(unsigned long);
80
81
82 /* usecs per counter cycle, shifted to left by 32 bits */
83 static unsigned int sll32_usecs_per_cycle;
84
85 /* how many counter cycles in a jiffy */
86 static unsigned long cycles_per_jiffy;
87
88 /* Cycle counter value at the previous timer interrupt.. */
89 static unsigned int timerhi, timerlo;
90
91 /* expirelo is the count value for next CPU timer interrupt */
92 static unsigned int expirelo;
93
94
95 /*
96  * Null timer ack for systems not needing one (e.g. i8254).
97  */
98 static void null_timer_ack(void) { /* nothing */ }
99
100 /*
101  * Null high precision timer functions for systems lacking one.
102  */
103 static unsigned int null_hpt_read(void)
104 {
105         return 0;
106 }
107
108 static void null_hpt_init(unsigned int count) { /* nothing */ }
109
110
111 /*
112  * Timer ack for an R4k-compatible timer of a known frequency.
113  */
114 static void c0_timer_ack(void)
115 {
116         unsigned int count;
117
118         /* Ack this timer interrupt and set the next one.  */
119         expirelo += cycles_per_jiffy;
120         write_c0_compare(expirelo);
121
122         /* Check to see if we have missed any timer interrupts.  */
123         count = read_c0_count();
124         if ((count - expirelo) < 0x7fffffff) {
125                 /* missed_timer_count++; */
126                 expirelo = count + cycles_per_jiffy;
127                 write_c0_compare(expirelo);
128         }
129 }
130
131 /*
132  * High precision timer functions for a R4k-compatible timer.
133  */
134 static unsigned int c0_hpt_read(void)
135 {
136         return read_c0_count();
137 }
138
139 /* For use solely as a high precision timer.  */
140 static void c0_hpt_init(unsigned int count)
141 {
142         write_c0_count(read_c0_count() - count);
143 }
144
145 /* For use both as a high precision timer and an interrupt source.  */
146 static void c0_hpt_timer_init(unsigned int count)
147 {
148         count = read_c0_count() - count;
149         expirelo = (count / cycles_per_jiffy + 1) * cycles_per_jiffy;
150         write_c0_count(expirelo - cycles_per_jiffy);
151         write_c0_compare(expirelo);
152         write_c0_count(count);
153 }
154
155 int (*mips_timer_state)(void);
156 void (*mips_timer_ack)(void);
157 unsigned int (*mips_hpt_read)(void);
158 void (*mips_hpt_init)(unsigned int);
159
160
161 /*
162  * This version of gettimeofday has microsecond resolution and better than
163  * microsecond precision on fast machines with cycle counter.
164  */
165 void do_gettimeofday(struct timeval *tv)
166 {
167         unsigned long seq;
168         unsigned long lost;
169         unsigned long usec, sec;
170         unsigned long max_ntp_tick = tick_usec - tickadj;
171
172         do {
173                 seq = read_seqbegin(&xtime_lock);
174
175                 usec = do_gettimeoffset();
176
177                 lost = jiffies - wall_jiffies;
178
179                 /*
180                  * If time_adjust is negative then NTP is slowing the clock
181                  * so make sure not to go into next possible interval.
182                  * Better to lose some accuracy than have time go backwards..
183                  */
184                 if (unlikely(time_adjust < 0)) {
185                         usec = min(usec, max_ntp_tick);
186
187                         if (lost)
188                                 usec += lost * max_ntp_tick;
189                 } else if (unlikely(lost))
190                         usec += lost * tick_usec;
191
192                 sec = xtime.tv_sec;
193                 usec += (xtime.tv_nsec / 1000);
194
195         } while (read_seqretry(&xtime_lock, seq));
196
197         while (usec >= 1000000) {
198                 usec -= 1000000;
199                 sec++;
200         }
201
202         tv->tv_sec = sec;
203         tv->tv_usec = usec;
204 }
205
206 EXPORT_SYMBOL(do_gettimeofday);
207
208 int do_settimeofday(struct timespec *tv)
209 {
210         time_t wtm_sec, sec = tv->tv_sec;
211         long wtm_nsec, nsec = tv->tv_nsec;
212
213         if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
214                 return -EINVAL;
215
216         write_seqlock_irq(&xtime_lock);
217
218         /*
219          * This is revolting.  We need to set "xtime" correctly.  However,
220          * the value in this location is the value at the most recent update
221          * of wall time.  Discover what correction gettimeofday() would have
222          * made, and then undo it!
223          */
224         nsec -= do_gettimeoffset() * NSEC_PER_USEC;
225         nsec -= (jiffies - wall_jiffies) * tick_nsec;
226
227         wtm_sec  = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
228         wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
229
230         set_normalized_timespec(&xtime, sec, nsec);
231         set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
232
233         time_adjust = 0;                        /* stop active adjtime() */
234         time_status |= STA_UNSYNC;
235         time_maxerror = NTP_PHASE_LIMIT;
236         time_esterror = NTP_PHASE_LIMIT;
237
238         write_sequnlock_irq(&xtime_lock);
239         clock_was_set();
240         return 0;
241 }
242
243 EXPORT_SYMBOL(do_settimeofday);
244
245 /*
246  * Gettimeoffset routines.  These routines returns the time duration
247  * since last timer interrupt in usecs.
248  *
249  * If the exact CPU counter frequency is known, use fixed_rate_gettimeoffset.
250  * Otherwise use calibrate_gettimeoffset()
251  *
252  * If the CPU does not have the counter register, you can either supply
253  * your own gettimeoffset() routine, or use null_gettimeoffset(), which
254  * gives the same resolution as HZ.
255  */
256
257 static unsigned long null_gettimeoffset(void)
258 {
259         return 0;
260 }
261
262
263 /* The function pointer to one of the gettimeoffset funcs.  */
264 unsigned long (*do_gettimeoffset)(void) = null_gettimeoffset;
265
266
267 static unsigned long fixed_rate_gettimeoffset(void)
268 {
269         u32 count;
270         unsigned long res;
271
272         /* Get last timer tick in absolute kernel time */
273         count = mips_hpt_read();
274
275         /* .. relative to previous jiffy (32 bits is enough) */
276         count -= timerlo;
277         
278         __asm__("multu  %1,%2"
279                 : "=h" (res)
280                 : "r" (count), "r" (sll32_usecs_per_cycle)
281 #if defined(CONFIG_MIPS_BRCM)           
282                 : "lo");
283 #else
284                 : "lo", "accum");
285 #endif          
286
287         /*
288          * Due to possible jiffies inconsistencies, we need to check
289          * the result so that we'll get a timer that is monotonic.
290          */
291         if (res >= USECS_PER_JIFFY)
292                 res = USECS_PER_JIFFY - 1;
293
294         return res;
295 }
296
297
298 /*
299  * Cached "1/(clocks per usec) * 2^32" value.
300  * It has to be recalculated once each jiffy.
301  */
302 static unsigned long cached_quotient;
303
304 /* Last jiffy when calibrate_divXX_gettimeoffset() was called. */
305 static unsigned long last_jiffies;
306
307 /*
308  * This is moved from dec/time.c:do_ioasic_gettimeoffset() by Maciej.
309  */
310 static unsigned long calibrate_div32_gettimeoffset(void)
311 {
312         u32 count;
313         unsigned long res, tmp;
314         unsigned long quotient;
315
316         tmp = jiffies;
317
318         quotient = cached_quotient;
319
320         if (last_jiffies != tmp) {
321                 last_jiffies = tmp;
322                 if (last_jiffies != 0) {
323                         unsigned long r0;
324                         do_div64_32(r0, timerhi, timerlo, tmp);
325                         do_div64_32(quotient, USECS_PER_JIFFY,
326                                     USECS_PER_JIFFY_FRAC, r0);
327                         cached_quotient = quotient;
328                 }
329         }
330
331         /* Get last timer tick in absolute kernel time */
332         count = mips_hpt_read();
333
334         /* .. relative to previous jiffy (32 bits is enough) */
335         count -= timerlo;
336
337         __asm__("multu  %1,%2"
338                 : "=h" (res)
339                 : "r" (count), "r" (quotient)
340 #if defined(CONFIG_MIPS_BRCM)
341                 : "lo");
342 #else           
343                 : "lo", "accum");
344 #endif          
345
346         /*
347          * Due to possible jiffies inconsistencies, we need to check
348          * the result so that we'll get a timer that is monotonic.
349          */
350         if (res >= USECS_PER_JIFFY)
351                 res = USECS_PER_JIFFY - 1;
352
353         return res;
354 }
355
356 static unsigned long calibrate_div64_gettimeoffset(void)
357 {
358         u32 count;
359         unsigned long res, tmp;
360         unsigned long quotient;
361
362         tmp = jiffies;
363
364         quotient = cached_quotient;
365
366         if (last_jiffies != tmp) {
367                 last_jiffies = tmp;
368                 if (last_jiffies) {
369                         unsigned long r0;
370                         __asm__(".set   push\n\t"
371                                 ".set   mips3\n\t"
372                                 "lwu    %0,%3\n\t"
373                                 "dsll32 %1,%2,0\n\t"
374                                 "or     %1,%1,%0\n\t"
375                                 "ddivu  $0,%1,%4\n\t"
376                                 "mflo   %1\n\t"
377                                 "dsll32 %0,%5,0\n\t"
378                                 "or     %0,%0,%6\n\t"
379                                 "ddivu  $0,%0,%1\n\t"
380                                 "mflo   %0\n\t"
381                                 ".set   pop"
382                                 : "=&r" (quotient), "=&r" (r0)
383                                 : "r" (timerhi), "m" (timerlo),
384                                   "r" (tmp), "r" (USECS_PER_JIFFY),
385                                   "r" (USECS_PER_JIFFY_FRAC)
386 #if defined(CONFIG_MIPS_BRCM)
387                                 : "hi", "lo");
388 #else                             
389                                 : "hi", "lo", "accum");
390 #endif                          
391                         cached_quotient = quotient;
392                 }
393         }
394
395         /* Get last timer tick in absolute kernel time */
396         count = mips_hpt_read();
397
398         /* .. relative to previous jiffy (32 bits is enough) */
399         count -= timerlo;
400
401         __asm__("multu  %1,%2"
402                 : "=h" (res)
403                 : "r" (count), "r" (quotient)
404 #if defined(CONFIG_MIPS_BRCM)
405                 : "lo");
406 #else           
407                 : "lo", "accum");
408 #endif          
409
410         /*
411          * Due to possible jiffies inconsistencies, we need to check
412          * the result so that we'll get a timer that is monotonic.
413          */
414         if (res >= USECS_PER_JIFFY)
415                 res = USECS_PER_JIFFY - 1;
416
417         return res;
418 }
419
420
421 /* last time when xtime and rtc are sync'ed up */
422 static long last_rtc_update;
423
424 /*
425  * local_timer_interrupt() does profiling and process accounting
426  * on a per-CPU basis.
427  *
428  * In UP mode, it is invoked from the (global) timer_interrupt.
429  *
430  * In SMP mode, it might invoked by per-CPU timer interrupt, or
431  * a broadcasted inter-processor interrupt which itself is triggered
432  * by the global timer interrupt.
433  */
434 void local_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
435 {
436         if (!user_mode(regs)) {
437                 if (prof_buffer && current->pid) {
438                         unsigned long pc = regs->cp0_epc;
439
440                         pc -= (unsigned long) _stext;
441                         pc >>= prof_shift;
442                         /*
443                          * Dont ignore out-of-bounds pc values silently,
444                          * put them into the last histogram slot, so if
445                          * present, they will show up as a sharp peak.
446                          */
447                         if (pc > prof_len - 1)
448                                 pc = prof_len - 1;
449                         atomic_inc((atomic_t *)&prof_buffer[pc]);
450                 }
451         }
452
453 #ifdef CONFIG_SMP
454         /* in UP mode, update_process_times() is invoked by do_timer() */
455         update_process_times(user_mode(regs));
456 #endif
457 }
458
459 /*
460  * High-level timer interrupt service routines.  This function
461  * is set as irqaction->handler and is invoked through do_IRQ.
462  */
463 irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
464 {
465         unsigned long j;
466         unsigned int count;
467
468         count = mips_hpt_read();
469         mips_timer_ack();
470
471         /* Update timerhi/timerlo for intra-jiffy calibration. */
472         timerhi += count < timerlo;                     /* Wrap around */
473         timerlo = count;
474
475         /*
476          * call the generic timer interrupt handling
477          */
478         do_timer(regs);
479
480         /*
481          * If we have an externally synchronized Linux clock, then update
482          * CMOS clock accordingly every ~11 minutes. rtc_set_time() has to be
483          * called as close as possible to 500 ms before the new second starts.
484          */
485         write_seqlock(&xtime_lock);
486         if ((time_status & STA_UNSYNC) == 0 &&
487             xtime.tv_sec > last_rtc_update + 660 &&
488             (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
489             (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
490                 if (rtc_set_mmss(xtime.tv_sec) == 0) {
491                         last_rtc_update = xtime.tv_sec;
492                 } else {
493                         /* do it again in 60 s */
494                         last_rtc_update = xtime.tv_sec - 600;
495                 }
496         }
497         write_sequnlock(&xtime_lock);
498
499         /*
500          * If jiffies has overflown in this timer_interrupt, we must
501          * update the timer[hi]/[lo] to make fast gettimeoffset funcs
502          * quotient calc still valid. -arca
503          *
504          * The first timer interrupt comes late as interrupts are
505          * enabled long after timers are initialized.  Therefore the
506          * high precision timer is fast, leading to wrong gettimeoffset()
507          * calculations.  We deal with it by setting it based on the
508          * number of its ticks between the second and the third interrupt.
509          * That is still somewhat imprecise, but it's a good estimate.
510          * --macro
511          */
512         j = jiffies;
513         if (j < 4) {
514                 static unsigned int prev_count;
515                 static int hpt_initialized;
516
517                 switch (j) {
518                 case 0:
519                         timerhi = timerlo = 0;
520                         mips_hpt_init(count);
521                         break;
522                 case 2:
523                         prev_count = count;
524                         break;
525                 case 3:
526                         if (!hpt_initialized) {
527                                 unsigned int c3 = 3 * (count - prev_count);
528
529                                 timerhi = 0;
530                                 timerlo = c3;
531                                 mips_hpt_init(count - c3);
532                                 hpt_initialized = 1;
533                         }
534                         break;
535                 default:
536                         break;
537                 }
538         }
539
540 #if !defined(CONFIG_SMP)
541         /*
542          * In UP mode, we call local_timer_interrupt() to do profiling
543          * and process accouting.
544          *
545          * In SMP mode, local_timer_interrupt() is invoked by appropriate
546          * low-level local timer interrupt handler.
547          */
548         local_timer_interrupt(irq, dev_id, regs);
549
550 #else   /* CONFIG_SMP */
551
552         if (emulate_local_timer_interrupt) {
553                 /*
554                  * this is the place where we send out inter-process
555                  * interrupts and let each CPU do its own profiling
556                  * and process accouting.
557                  *
558                  * Obviously we need to call local_timer_interrupt() for
559                  * the current CPU too.
560                  */
561                 panic("Not implemented yet!!!");
562         }
563 #endif  /* CONFIG_SMP */
564
565         return IRQ_HANDLED;
566 }
567
568 asmlinkage void ll_timer_interrupt(int irq, struct pt_regs *regs)
569 {
570         irq_enter();
571         kstat_this_cpu.irqs[irq]++;
572
573         /* we keep interrupt disabled all the time */
574         timer_interrupt(irq, NULL, regs);
575
576         irq_exit();
577 }
578
579 asmlinkage void ll_local_timer_interrupt(int irq, struct pt_regs *regs)
580 {
581         irq_enter();
582         if (smp_processor_id() != 0)
583                 kstat_this_cpu.irqs[irq]++;
584
585         /* we keep interrupt disabled all the time */
586         local_timer_interrupt(irq, NULL, regs);
587
588         irq_exit();
589 }
590
591 /*
592  * time_init() - it does the following things.
593  *
594  * 1) board_time_init() -
595  *      a) (optional) set up RTC routines,
596  *      b) (optional) calibrate and set the mips_hpt_frequency
597  *          (only needed if you intended to use fixed_rate_gettimeoffset
598  *           or use cpu counter as timer interrupt source)
599  * 2) setup xtime based on rtc_get_time().
600  * 3) choose a appropriate gettimeoffset routine.
601  * 4) calculate a couple of cached variables for later usage
602  * 5) board_timer_setup() -
603  *      a) (optional) over-write any choices made above by time_init().
604  *      b) machine specific code should setup the timer irqaction.
605  *      c) enable the timer interrupt
606  */
607
608 void (*board_time_init)(void);
609 void (*board_timer_setup)(struct irqaction *irq);
610
611 unsigned int mips_hpt_frequency;
612
613 static struct irqaction timer_irqaction = {
614         .handler = timer_interrupt,
615         .flags = SA_INTERRUPT,
616         .name = "timer",
617 };
618
619 static unsigned int __init calibrate_hpt(void)
620 {
621         u64 frequency;
622         u32 hpt_start, hpt_end, hpt_count, hz;
623
624         const int loops = HZ / 10;
625         int log_2_loops = 0;
626         int i;
627
628         /*
629          * We want to calibrate for 0.1s, but to avoid a 64-bit
630          * division we round the number of loops up to the nearest
631          * power of 2.
632          */
633         while (loops > 1 << log_2_loops)
634                 log_2_loops++;
635         i = 1 << log_2_loops;
636
637         /*
638          * Wait for a rising edge of the timer interrupt.
639          */
640         while (mips_timer_state());
641         while (!mips_timer_state());
642
643         /*
644          * Now see how many high precision timer ticks happen
645          * during the calculated number of periods between timer
646          * interrupts.
647          */
648         hpt_start = mips_hpt_read();
649         do {
650                 while (mips_timer_state());
651                 while (!mips_timer_state());
652         } while (--i);
653         hpt_end = mips_hpt_read();
654
655         hpt_count = hpt_end - hpt_start;
656         hz = HZ;
657         frequency = (u64)hpt_count * (u64)hz;
658
659         return frequency >> log_2_loops;
660 }
661
662 void __init time_init(void)
663 {
664         if (board_time_init)
665                 board_time_init();
666
667         if (!rtc_set_mmss)
668                 rtc_set_mmss = rtc_set_time;
669
670         xtime.tv_sec = rtc_get_time();
671         xtime.tv_nsec = 0;
672
673         set_normalized_timespec(&wall_to_monotonic,
674                                 -xtime.tv_sec, -xtime.tv_nsec);
675
676         /* Choose appropriate high precision timer routines.  */
677         if (!cpu_has_counter && !mips_hpt_read) {
678                 /* No high precision timer -- sorry.  */
679                 mips_hpt_read = null_hpt_read;
680                 mips_hpt_init = null_hpt_init;
681         } else if (!mips_hpt_frequency && !mips_timer_state) {
682                 /* A high precision timer of unknown frequency.  */
683                 if (!mips_hpt_read) {
684                         /* No external high precision timer -- use R4k.  */
685                         mips_hpt_read = c0_hpt_read;
686                         mips_hpt_init = c0_hpt_init;
687                 }
688
689                 if ((current_cpu_data.isa_level == MIPS_CPU_ISA_M32) ||
690                          (current_cpu_data.isa_level == MIPS_CPU_ISA_I) ||
691                          (current_cpu_data.isa_level == MIPS_CPU_ISA_II))
692                         /*
693                          * We need to calibrate the counter but we don't have
694                          * 64-bit division.
695                          */
696                         do_gettimeoffset = calibrate_div32_gettimeoffset;
697                 else
698                         /*
699                          * We need to calibrate the counter but we *do* have
700                          * 64-bit division.
701                          */
702                         do_gettimeoffset = calibrate_div64_gettimeoffset;
703         } else {
704                 /* We know counter frequency.  Or we can get it.  */
705                 if (!mips_hpt_read) {
706                         /* No external high precision timer -- use R4k.  */
707                         mips_hpt_read = c0_hpt_read;
708
709                         if (mips_timer_state)
710                                 mips_hpt_init = c0_hpt_init;
711                         else {
712                                 /* No external timer interrupt -- use R4k.  */
713                                 mips_hpt_init = c0_hpt_timer_init;
714                                 mips_timer_ack = c0_timer_ack;
715                         }
716                 }
717                 if (!mips_hpt_frequency)
718                         mips_hpt_frequency = calibrate_hpt();
719
720                 do_gettimeoffset = fixed_rate_gettimeoffset;
721
722                 /* Calculate cache parameters.  */
723                 cycles_per_jiffy = (mips_hpt_frequency + HZ / 2) / HZ;
724
725                 /* sll32_usecs_per_cycle = 10^6 * 2^32 / mips_counter_freq  */
726                 do_div64_32(sll32_usecs_per_cycle,
727                             1000000, mips_hpt_frequency / 2,
728                             mips_hpt_frequency);
729
730                 /* Report the high precision timer rate for a reference.  */
731                 printk("Using %u.%03u MHz high precision timer.\n",
732                        ((mips_hpt_frequency + 500) / 1000) / 1000,
733                        ((mips_hpt_frequency + 500) / 1000) % 1000);
734         }
735
736         if (!mips_timer_ack)
737                 /* No timer interrupt ack (e.g. i8254).  */
738                 mips_timer_ack = null_timer_ack;
739
740         /* This sets up the high precision timer for the first interrupt.  */
741         mips_hpt_init(mips_hpt_read());
742
743         /*
744          * Call board specific timer interrupt setup.
745          *
746          * this pointer must be setup in machine setup routine.
747          *
748          * Even if a machine chooses to use a low-level timer interrupt,
749          * it still needs to setup the timer_irqaction.
750          * In that case, it might be better to set timer_irqaction.handler
751          * to be NULL function so that we are sure the high-level code
752          * is not invoked accidentally.
753          */
754         board_timer_setup(&timer_irqaction);
755 }
756
757
758 #define FEBRUARY                2
759 #define STARTOFTIME             1970
760 #define SECDAY                  86400L
761 #define SECYR                   (SECDAY * 365)
762 #define leapyear(y)             ((!((y) % 4) && ((y) % 100)) || !((y) % 400))
763 #define days_in_year(y)         (leapyear(y) ? 366 : 365)
764 #define days_in_month(m)        (month_days[(m) - 1])
765
766 static int month_days[12] = {
767         31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
768 };
769
770 void to_tm(unsigned long tim, struct rtc_time *tm)
771 {
772         long hms, day, gday;
773         int i;
774
775         gday = day = tim / SECDAY;
776         hms = tim % SECDAY;
777
778         /* Hours, minutes, seconds are easy */
779         tm->tm_hour = hms / 3600;
780         tm->tm_min = (hms % 3600) / 60;
781         tm->tm_sec = (hms % 3600) % 60;
782
783         /* Number of years in days */
784         for (i = STARTOFTIME; day >= days_in_year(i); i++)
785                 day -= days_in_year(i);
786         tm->tm_year = i;
787
788         /* Number of months in days left */
789         if (leapyear(tm->tm_year))
790                 days_in_month(FEBRUARY) = 29;
791         for (i = 1; day >= days_in_month(i); i++)
792                 day -= days_in_month(i);
793         days_in_month(FEBRUARY) = 28;
794         tm->tm_mon = i - 1;             /* tm_mon starts from 0 to 11 */
795
796         /* Days are what is left over (+1) from all that. */
797         tm->tm_mday = day + 1;
798
799         /*
800          * Determine the day of week
801          */
802         tm->tm_wday = (gday + 4) % 7;   /* 1970/1/1 was Thursday */
803 }
804
805 EXPORT_SYMBOL(rtc_lock);
806 EXPORT_SYMBOL(to_tm);
807 EXPORT_SYMBOL(rtc_set_time);
808 EXPORT_SYMBOL(rtc_get_time);
809
810 unsigned long long sched_clock(void)
811 {
812         return (unsigned long long)jiffies*(1000000000/HZ);
813 }