2 * Copyright 2001 MontaVista Software Inc.
3 * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
5 * Common time service routines for MIPS machines. See
6 * Documents/mips/README.txt.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
13 #include <linux/config.h>
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/sched.h>
18 #include <linux/param.h>
19 #include <linux/time.h>
20 #include <linux/smp.h>
21 #include <linux/kernel_stat.h>
22 #include <linux/spinlock.h>
23 #include <linux/interrupt.h>
24 #include <linux/module.h>
26 #include <asm/bootinfo.h>
29 #include <asm/hardirq.h>
30 #include <asm/div64.h>
32 /* This is for machines which generate the exact clock. */
33 #define USECS_PER_JIFFY (1000000/HZ)
34 #define USECS_PER_JIFFY_FRAC ((u32)((1000000ULL << 32) / HZ))
36 #define TICK_SIZE (tick_nsec / 1000)
43 extern rwlock_t xtime_lock
;
44 extern volatile unsigned long wall_jiffies
;
46 spinlock_t rtc_lock
= SPIN_LOCK_UNLOCKED
;
49 * whether we emulate local_timer_interrupts for SMP machines.
51 int emulate_local_timer_interrupt
;
54 * By default we provide the null RTC ops
56 static unsigned long null_rtc_get_time(void)
58 return mktime(2000, 1, 1, 0, 0, 0);
61 static int null_rtc_set_time(unsigned long sec
)
66 unsigned long (*rtc_get_time
)(void) = null_rtc_get_time
;
67 int (*rtc_set_time
)(unsigned long) = null_rtc_set_time
;
71 * This version of gettimeofday has microsecond resolution and better than
72 * microsecond precision on fast machines with cycle counter.
74 void do_gettimeofday(struct timeval
*tv
)
77 unsigned long usec
, sec
;
79 read_lock_irqsave(&xtime_lock
, flags
);
80 usec
= do_gettimeoffset();
82 unsigned long lost
= jiffies
- wall_jiffies
;
84 usec
+= lost
* (1000000 / HZ
);
87 usec
+= (xtime
.tv_nsec
/ 1000);
88 read_unlock_irqrestore(&xtime_lock
, flags
);
90 while (usec
>= 1000000) {
99 void do_settimeofday(struct timeval
*tv
)
101 write_lock_irq(&xtime_lock
);
103 * This is revolting. We need to set "xtime" correctly. However, the
104 * value in this location is the value at the most recent update of
105 * wall time. Discover what correction gettimeofday() would have
106 * made, and then undo it!
108 tv
->tv_usec
-= do_gettimeoffset();
109 tv
->tv_usec
-= (jiffies
- wall_jiffies
) * (1000000 / HZ
);
111 while (tv
->tv_usec
< 0) {
112 tv
->tv_usec
+= 1000000;
116 xtime
.tv_sec
= tv
->tv_sec
;
117 xtime
.tv_nsec
= (tv
->tv_usec
* 1000);
118 time_adjust
= 0; /* stop active adjtime() */
119 time_status
|= STA_UNSYNC
;
120 time_maxerror
= NTP_PHASE_LIMIT
;
121 time_esterror
= NTP_PHASE_LIMIT
;
122 write_unlock_irq(&xtime_lock
);
127 * Gettimeoffset routines. These routines returns the time duration
128 * since last timer interrupt in usecs.
130 * If the exact CPU counter frequency is known, use fixed_rate_gettimeoffset.
131 * Otherwise use calibrate_gettimeoffset()
133 * If the CPU does not have counter register all, you can either supply
134 * your own gettimeoffset() routine, or use null_gettimeoffset() routines,
135 * which gives the same resolution as HZ.
139 /* This is for machines which generate the exact clock. */
140 #define USECS_PER_JIFFY (1000000/HZ)
142 /* usecs per counter cycle, shifted to left by 32 bits */
143 static unsigned int sll32_usecs_per_cycle
=0;
145 /* how many counter cycles in a jiffy */
146 static unsigned long cycles_per_jiffy
=0;
148 /* Cycle counter value at the previous timer interrupt.. */
149 static unsigned int timerhi
, timerlo
;
151 /* last time when xtime and rtc are sync'ed up */
152 static long last_rtc_update
;
154 /* the function pointer to one of the gettimeoffset funcs*/
155 unsigned long (*do_gettimeoffset
)(void) = null_gettimeoffset
;
157 unsigned long null_gettimeoffset(void)
162 unsigned long fixed_rate_gettimeoffset(void)
167 /* Get last timer tick in absolute kernel time */
168 count
= read_c0_count();
170 /* .. relative to previous jiffy (32 bits is enough) */
173 __asm__("multu\t%1,%2\n\t"
177 "r" (sll32_usecs_per_cycle
));
180 * Due to possible jiffies inconsistencies, we need to check
181 * the result so that we'll get a timer that is monotonic.
183 if (res
>= USECS_PER_JIFFY
)
184 res
= USECS_PER_JIFFY
-1;
190 * Cached "1/(clocks per usec)*2^32" value.
191 * It has to be recalculated once each jiffy.
193 static unsigned long cached_quotient
;
195 /* Last jiffy when calibrate_divXX_gettimeoffset() was called. */
196 static unsigned long last_jiffies
= 0;
200 * This is copied from dec/time.c:do_ioasic_gettimeoffset() by Mercij.
202 unsigned long calibrate_div32_gettimeoffset(void)
205 unsigned long res
, tmp
;
206 unsigned long quotient
;
210 quotient
= cached_quotient
;
212 if (last_jiffies
!= tmp
) {
214 if (last_jiffies
!= 0) {
216 do_div64_32(r0
, timerhi
, timerlo
, tmp
);
217 do_div64_32(quotient
, USECS_PER_JIFFY
,
218 USECS_PER_JIFFY_FRAC
, r0
);
219 cached_quotient
= quotient
;
223 /* Get last timer tick in absolute kernel time */
224 count
= read_c0_count();
226 /* .. relative to previous jiffy (32 bits is enough) */
229 __asm__("multu %2,%3"
230 : "=l" (tmp
), "=h" (res
)
231 : "r" (count
), "r" (quotient
));
234 * Due to possible jiffies inconsistencies, we need to check
235 * the result so that we'll get a timer that is monotonic.
237 if (res
>= USECS_PER_JIFFY
)
238 res
= USECS_PER_JIFFY
- 1;
243 unsigned long calibrate_div64_gettimeoffset(void)
246 unsigned long res
, tmp
;
247 unsigned long quotient
;
251 quotient
= cached_quotient
;
253 if (tmp
&& last_jiffies
!= tmp
) {
255 __asm__(".set\tnoreorder\n\t"
259 "dsll32\t$1,%1,0\n\t"
261 "ddivu\t$0,$1,%3\n\t"
263 "dsll32\t%0,%4,0\n\t"
265 "ddivu\t$0,%0,$1\n\t"
274 "r" (USECS_PER_JIFFY
));
275 cached_quotient
= quotient
;
278 /* Get last timer tick in absolute kernel time */
279 count
= read_c0_count();
281 /* .. relative to previous jiffy (32 bits is enough) */
284 __asm__("multu\t%1,%2\n\t"
291 * Due to possible jiffies inconsistencies, we need to check
292 * the result so that we'll get a timer that is monotonic.
294 if (res
>= USECS_PER_JIFFY
)
295 res
= USECS_PER_JIFFY
-1;
302 * local_timer_interrupt() does profiling and process accounting
303 * on a per-CPU basis.
305 * In UP mode, it is invoked from the (global) timer_interrupt.
307 * In SMP mode, it might invoked by per-CPU timer interrupt, or
308 * a broadcasted inter-processor interrupt which itself is triggered
309 * by the global timer interrupt.
311 void local_timer_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
313 if (!user_mode(regs
)) {
314 if (prof_buffer
&& current
->pid
) {
316 unsigned long pc
= regs
->cp0_epc
;
318 pc
-= (unsigned long) &_stext
;
321 * Dont ignore out-of-bounds pc values silently,
322 * put them into the last histogram slot, so if
323 * present, they will show up as a sharp peak.
327 atomic_inc((atomic_t
*)&prof_buffer
[pc
]);
332 /* in UP mode, update_process_times() is invoked by do_timer() */
333 update_process_times(user_mode(regs
));
338 * high-level timer interrupt service routines. This function
339 * is set as irqaction->handler and is invoked through do_IRQ.
341 void timer_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
343 if (current_cpu_data
.options
& MIPS_CPU_COUNTER
) {
347 * The cycle counter is only 32 bit which is good for about
348 * a minute at current count rates of upto 150MHz or so.
350 count
= read_c0_count();
351 timerhi
+= (count
< timerlo
); /* Wrap around */
355 * set up for next timer interrupt - no harm if the machine
356 * is using another timer interrupt source.
357 * Note that writing to COMPARE register clears the interrupt
360 count
+ cycles_per_jiffy
);
365 * call the generic timer interrupt handling
370 * If we have an externally synchronized Linux clock, then update
371 * CMOS clock accordingly every ~11 minutes. rtc_set_time() has to be
372 * called as close as possible to 500 ms before the new second starts.
374 read_lock (&xtime_lock
);
375 if ((time_status
& STA_UNSYNC
) == 0 &&
376 xtime
.tv_sec
> last_rtc_update
+ 660 &&
377 (xtime
.tv_nsec
/ 1000) >= 500000 - ((unsigned) TICK_SIZE
) / 2 &&
378 (xtime
.tv_nsec
/ 1000) <= 500000 + ((unsigned) TICK_SIZE
) / 2) {
379 if (rtc_set_time(xtime
.tv_sec
) == 0) {
380 last_rtc_update
= xtime
.tv_sec
;
382 last_rtc_update
= xtime
.tv_sec
- 600;
383 /* do it again in 60 s */
386 read_unlock (&xtime_lock
);
389 * If jiffies has overflowed in this timer_interrupt we must
390 * update the timer[hi]/[lo] to make fast gettimeoffset funcs
391 * quotient calc still valid. -arca
394 timerhi
= timerlo
= 0;
397 #if !defined(CONFIG_SMP)
399 * In UP mode, we call local_timer_interrupt() to do profiling
400 * and process accouting.
402 * In SMP mode, local_timer_interrupt() is invoked by appropriate
403 * low-level local timer interrupt handler.
405 local_timer_interrupt(0, NULL
, regs
);
407 #else /* CONFIG_SMP */
409 if (emulate_local_timer_interrupt
) {
411 * this is the place where we send out inter-process
412 * interrupts and let each CPU do its own profiling
413 * and process accouting.
415 * Obviously we need to call local_timer_interrupt() for
416 * the current CPU too.
418 panic("Not implemented yet!!!");
420 #endif /* CONFIG_SMP */
423 asmlinkage
void ll_timer_interrupt(int irq
, struct pt_regs
*regs
)
425 int cpu
= smp_processor_id();
428 kstat_cpu(cpu
).irqs
[irq
]++;
430 /* we keep interrupt disabled all the time */
431 timer_interrupt(irq
, NULL
, regs
);
435 if (softirq_pending(cpu
))
439 asmlinkage
void ll_local_timer_interrupt(int irq
, struct pt_regs
*regs
)
441 int cpu
= smp_processor_id();
444 kstat_cpu(cpu
).irqs
[irq
]++;
446 /* we keep interrupt disabled all the time */
447 local_timer_interrupt(irq
, NULL
, regs
);
451 if (softirq_pending(cpu
))
456 * time_init() - it does the following things.
458 * 1) board_time_init() -
459 * a) (optional) set up RTC routines,
460 * b) (optional) calibrate and set the mips_counter_frequency
461 * (only needed if you intended to use fixed_rate_gettimeoffset
462 * or use cpu counter as timer interrupt source)
463 * 2) setup xtime based on rtc_get_time().
464 * 3) choose a appropriate gettimeoffset routine.
465 * 4) calculate a couple of cached variables for later usage
466 * 5) board_timer_setup() -
467 * a) (optional) over-write any choices made above by time_init().
468 * b) machine specific code should setup the timer irqaction.
469 * c) enable the timer interrupt
472 void (*board_time_init
)(void) = NULL
;
473 void (*board_timer_setup
)(struct irqaction
*irq
) = NULL
;
475 unsigned int mips_counter_frequency
= 0;
477 static struct irqaction timer_irqaction
= {
486 void __init
time_init(void)
491 xtime
.tv_sec
= rtc_get_time();
494 /* choose appropriate gettimeoffset routine */
495 if (!(current_cpu_data
.options
& MIPS_CPU_COUNTER
)) {
496 /* no cpu counter - sorry */
497 do_gettimeoffset
= null_gettimeoffset
;
498 } else if (mips_counter_frequency
!= 0) {
499 /* we have cpu counter and know counter frequency! */
500 do_gettimeoffset
= fixed_rate_gettimeoffset
;
501 } else if ((current_cpu_data
.isa_level
== MIPS_CPU_ISA_M32
) ||
502 (current_cpu_data
.isa_level
== MIPS_CPU_ISA_I
) ||
503 (current_cpu_data
.isa_level
== MIPS_CPU_ISA_II
) ) {
504 /* we need to calibrate the counter but we don't have
505 * 64-bit division. */
506 do_gettimeoffset
= calibrate_div32_gettimeoffset
;
508 /* we need to calibrate the counter but we *do* have
509 * 64-bit division. */
510 do_gettimeoffset
= calibrate_div64_gettimeoffset
;
513 /* caclulate cache parameters */
514 if (mips_counter_frequency
) {
517 cycles_per_jiffy
= mips_counter_frequency
/ HZ
;
519 /* sll32_usecs_per_cycle = 10^6 * 2^32 / mips_counter_freq */
520 /* any better way to do this? */
521 sll32_usecs_per_cycle
= mips_counter_frequency
/ 100000;
522 sll32_usecs_per_cycle
= 0xffffffff / sll32_usecs_per_cycle
;
523 sll32_usecs_per_cycle
*= 10;
526 * For those using cpu counter as timer, this sets up the
529 count
= read_c0_count();
531 count
+ cycles_per_jiffy
);
535 * Call board specific timer interrupt setup.
537 * this pointer must be setup in machine setup routine.
539 * Even if the machine choose to use low-level timer interrupt,
540 * it still needs to setup the timer_irqaction.
541 * In that case, it might be better to set timer_irqaction.handler
542 * to be NULL function so that we are sure the high-level code
543 * is not invoked accidentally.
545 board_timer_setup(&timer_irqaction
);
549 #define STARTOFTIME 1970
550 #define SECDAY 86400L
551 #define SECYR (SECDAY * 365)
552 #define leapyear(year) ((year) % 4 == 0)
553 #define days_in_year(a) (leapyear(a) ? 366 : 365)
554 #define days_in_month(a) (month_days[(a) - 1])
556 static int month_days
[12] = {
557 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
560 void to_tm(unsigned long tim
, struct rtc_time
* tm
)
565 gday
= day
= tim
/ SECDAY
;
568 /* Hours, minutes, seconds are easy */
569 tm
->tm_hour
= hms
/ 3600;
570 tm
->tm_min
= (hms
% 3600) / 60;
571 tm
->tm_sec
= (hms
% 3600) % 60;
573 /* Number of years in days */
574 for (i
= STARTOFTIME
; day
>= days_in_year(i
); i
++)
575 day
-= days_in_year(i
);
578 /* Number of months in days left */
579 if (leapyear(tm
->tm_year
))
580 days_in_month(FEBRUARY
) = 29;
581 for (i
= 1; day
>= days_in_month(i
); i
++)
582 day
-= days_in_month(i
);
583 days_in_month(FEBRUARY
) = 28;
584 tm
->tm_mon
= i
-1; /* tm_mon starts from 0 to 11 */
586 /* Days are what is left over (+1) from all that. */
587 tm
->tm_mday
= day
+ 1;
590 * Determine the day of week
592 tm
->tm_wday
= (gday
+ 4) % 7; /* 1970/1/1 was Thursday */
595 EXPORT_SYMBOL(rtc_lock
);