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