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