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