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
6 * Common time service routines for MIPS machines. See
7 * Documentation/mips/time.README.
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.
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/timex.h>
21 #include <linux/smp.h>
22 #include <linux/kernel_stat.h>
23 #include <linux/spinlock.h>
24 #include <linux/interrupt.h>
25 #include <linux/module.h>
27 #include <asm/bootinfo.h>
28 #include <asm/cache.h>
29 #include <asm/compiler.h>
31 #include <asm/cpu-features.h>
32 #include <asm/div64.h>
33 #include <asm/sections.h>
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).
42 #define USECS_PER_JIFFY TICK_SIZE
43 #define USECS_PER_JIFFY_FRAC ((unsigned long)(u32)((1000000ULL << 32) / HZ))
45 #define TICK_SIZE (tick_nsec / 1000)
50 DEFINE_SPINLOCK(rtc_lock);
53 * By default we provide the null RTC ops
55 static unsigned long null_rtc_get_time(void)
57 return mktime(2000, 1, 1, 0, 0, 0);
60 static int null_rtc_set_time(unsigned long sec)
65 unsigned long (*rtc_mips_get_time)(void) = null_rtc_get_time;
66 int (*rtc_mips_set_time)(unsigned long) = null_rtc_set_time;
67 int (*rtc_mips_set_mmss)(unsigned long);
70 /* how many counter cycles in a jiffy */
71 static unsigned long cycles_per_jiffy __read_mostly;
73 /* expirelo is the count value for next CPU timer interrupt */
74 static unsigned int expirelo;
78 * Null timer ack for systems not needing one (e.g. i8254).
80 static void null_timer_ack(void) { /* nothing */ }
83 * Null high precision timer functions for systems lacking one.
85 static cycle_t null_hpt_read(void)
91 * Timer ack for an R4k-compatible timer of a known frequency.
93 static void c0_timer_ack(void)
97 /* Ack this timer interrupt and set the next one. */
98 expirelo += cycles_per_jiffy;
99 write_c0_compare(expirelo);
101 /* Check to see if we have missed any timer interrupts. */
102 while (((count = read_c0_count()) - expirelo) < 0x7fffffff) {
103 /* missed_timer_count++; */
104 expirelo = count + cycles_per_jiffy;
105 write_c0_compare(expirelo);
110 * High precision timer functions for a R4k-compatible timer.
112 static cycle_t c0_hpt_read(void)
114 return read_c0_count();
117 /* For use both as a high precision timer and an interrupt source. */
118 static void __init c0_hpt_timer_init(void)
120 expirelo = read_c0_count() + cycles_per_jiffy;
121 write_c0_compare(expirelo);
124 int (*mips_timer_state)(void);
125 void (*mips_timer_ack)(void);
127 /* last time when xtime and rtc are sync'ed up */
128 static long last_rtc_update;
131 * local_timer_interrupt() does profiling and process accounting
132 * on a per-CPU basis.
134 * In UP mode, it is invoked from the (global) timer_interrupt.
136 * In SMP mode, it might invoked by per-CPU timer interrupt, or
137 * a broadcasted inter-processor interrupt which itself is triggered
138 * by the global timer interrupt.
140 void local_timer_interrupt(int irq, void *dev_id)
142 profile_tick(CPU_PROFILING);
143 update_process_times(user_mode(get_irq_regs()));
147 * High-level timer interrupt service routines. This function
148 * is set as irqaction->handler and is invoked through do_IRQ.
150 irqreturn_t timer_interrupt(int irq, void *dev_id)
152 write_seqlock(&xtime_lock);
157 * call the generic timer interrupt handling
162 * If we have an externally synchronized Linux clock, then update
163 * CMOS clock accordingly every ~11 minutes. rtc_mips_set_time() has to be
164 * called as close as possible to 500 ms before the new second starts.
167 xtime.tv_sec > last_rtc_update + 660 &&
168 (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
169 (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
170 if (rtc_mips_set_mmss(xtime.tv_sec) == 0) {
171 last_rtc_update = xtime.tv_sec;
173 /* do it again in 60 s */
174 last_rtc_update = xtime.tv_sec - 600;
178 write_sequnlock(&xtime_lock);
181 * In UP mode, we call local_timer_interrupt() to do profiling
182 * and process accouting.
184 * In SMP mode, local_timer_interrupt() is invoked by appropriate
185 * low-level local timer interrupt handler.
187 local_timer_interrupt(irq, dev_id);
192 int null_perf_irq(void)
197 int (*perf_irq)(void) = null_perf_irq;
199 EXPORT_SYMBOL(null_perf_irq);
200 EXPORT_SYMBOL(perf_irq);
208 * Performance counter IRQ or -1 if shared with timer
210 int cp0_perfcount_irq;
211 EXPORT_SYMBOL_GPL(cp0_perfcount_irq);
214 * Possibly handle a performance counter interrupt.
215 * Return true if the timer interrupt should not be checked
217 static inline int handle_perf_irq (int r2)
220 * The performance counter overflow interrupt may be shared with the
221 * timer interrupt (cp0_perfcount_irq < 0). If it is and a
222 * performance counter has overflowed (perf_irq() == IRQ_HANDLED)
223 * and we can't reliably determine if a counter interrupt has also
224 * happened (!r2) then don't check for a timer interrupt.
226 return (cp0_perfcount_irq < 0) &&
227 perf_irq() == IRQ_HANDLED &&
231 asmlinkage void ll_timer_interrupt(int irq)
233 int r2 = cpu_has_mips_r2;
236 kstat_this_cpu.irqs[irq]++;
238 if (handle_perf_irq(r2))
241 if (r2 && ((read_c0_cause() & (1 << 30)) == 0))
244 timer_interrupt(irq, NULL);
250 asmlinkage void ll_local_timer_interrupt(int irq)
253 if (smp_processor_id() != 0)
254 kstat_this_cpu.irqs[irq]++;
256 /* we keep interrupt disabled all the time */
257 local_timer_interrupt(irq, NULL);
263 * time_init() - it does the following things.
265 * 1) board_time_init() -
266 * a) (optional) set up RTC routines,
267 * b) (optional) calibrate and set the mips_hpt_frequency
268 * (only needed if you intended to use cpu counter as timer interrupt
270 * 2) setup xtime based on rtc_mips_get_time().
271 * 3) calculate a couple of cached variables for later usage
272 * 4) plat_timer_setup() -
273 * a) (optional) over-write any choices made above by time_init().
274 * b) machine specific code should setup the timer irqaction.
275 * c) enable the timer interrupt
278 void (*board_time_init)(void);
280 unsigned int mips_hpt_frequency;
282 static struct irqaction timer_irqaction = {
283 .handler = timer_interrupt,
284 .flags = IRQF_DISABLED | IRQF_PERCPU,
288 static unsigned int __init calibrate_hpt(void)
290 cycle_t frequency, hpt_start, hpt_end, hpt_count, hz;
292 const int loops = HZ / 10;
297 * We want to calibrate for 0.1s, but to avoid a 64-bit
298 * division we round the number of loops up to the nearest
301 while (loops > 1 << log_2_loops)
303 i = 1 << log_2_loops;
306 * Wait for a rising edge of the timer interrupt.
308 while (mips_timer_state());
309 while (!mips_timer_state());
312 * Now see how many high precision timer ticks happen
313 * during the calculated number of periods between timer
316 hpt_start = clocksource_mips.read();
318 while (mips_timer_state());
319 while (!mips_timer_state());
321 hpt_end = clocksource_mips.read();
323 hpt_count = (hpt_end - hpt_start) & clocksource_mips.mask;
325 frequency = hpt_count * hz;
327 return frequency >> log_2_loops;
330 struct clocksource clocksource_mips = {
332 .mask = CLOCKSOURCE_MASK(32),
333 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
336 static void __init init_mips_clocksource(void)
341 if (!mips_hpt_frequency || clocksource_mips.read == null_hpt_read)
344 /* Calclate a somewhat reasonable rating value */
345 clocksource_mips.rating = 200 + mips_hpt_frequency / 10000000;
346 /* Find a shift value */
347 for (shift = 32; shift > 0; shift--) {
348 temp = (u64) NSEC_PER_SEC << shift;
349 do_div(temp, mips_hpt_frequency);
350 if ((temp >> 32) == 0)
353 clocksource_mips.shift = shift;
354 clocksource_mips.mult = (u32)temp;
356 clocksource_register(&clocksource_mips);
359 void __init time_init(void)
364 if (!rtc_mips_set_mmss)
365 rtc_mips_set_mmss = rtc_mips_set_time;
367 xtime.tv_sec = rtc_mips_get_time();
370 set_normalized_timespec(&wall_to_monotonic,
371 -xtime.tv_sec, -xtime.tv_nsec);
373 /* Choose appropriate high precision timer routines. */
374 if (!cpu_has_counter && !clocksource_mips.read)
375 /* No high precision timer -- sorry. */
376 clocksource_mips.read = null_hpt_read;
377 else if (!mips_hpt_frequency && !mips_timer_state) {
378 /* A high precision timer of unknown frequency. */
379 if (!clocksource_mips.read)
380 /* No external high precision timer -- use R4k. */
381 clocksource_mips.read = c0_hpt_read;
383 /* We know counter frequency. Or we can get it. */
384 if (!clocksource_mips.read) {
385 /* No external high precision timer -- use R4k. */
386 clocksource_mips.read = c0_hpt_read;
388 if (!mips_timer_state) {
389 /* No external timer interrupt -- use R4k. */
390 mips_timer_ack = c0_timer_ack;
391 /* Calculate cache parameters. */
393 (mips_hpt_frequency + HZ / 2) / HZ;
395 * This sets up the high precision
396 * timer for the first interrupt.
401 if (!mips_hpt_frequency)
402 mips_hpt_frequency = calibrate_hpt();
404 /* Report the high precision timer rate for a reference. */
405 printk("Using %u.%03u MHz high precision timer.\n",
406 ((mips_hpt_frequency + 500) / 1000) / 1000,
407 ((mips_hpt_frequency + 500) / 1000) % 1000);
411 /* No timer interrupt ack (e.g. i8254). */
412 mips_timer_ack = null_timer_ack;
415 * Call board specific timer interrupt setup.
417 * this pointer must be setup in machine setup routine.
419 * Even if a machine chooses to use a low-level timer interrupt,
420 * it still needs to setup the timer_irqaction.
421 * In that case, it might be better to set timer_irqaction.handler
422 * to be NULL function so that we are sure the high-level code
423 * is not invoked accidentally.
425 plat_timer_setup(&timer_irqaction);
427 init_mips_clocksource();
431 #define STARTOFTIME 1970
432 #define SECDAY 86400L
433 #define SECYR (SECDAY * 365)
434 #define leapyear(y) ((!((y) % 4) && ((y) % 100)) || !((y) % 400))
435 #define days_in_year(y) (leapyear(y) ? 366 : 365)
436 #define days_in_month(m) (month_days[(m) - 1])
438 static int month_days[12] = {
439 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
442 void to_tm(unsigned long tim, struct rtc_time *tm)
447 gday = day = tim / SECDAY;
450 /* Hours, minutes, seconds are easy */
451 tm->tm_hour = hms / 3600;
452 tm->tm_min = (hms % 3600) / 60;
453 tm->tm_sec = (hms % 3600) % 60;
455 /* Number of years in days */
456 for (i = STARTOFTIME; day >= days_in_year(i); i++)
457 day -= days_in_year(i);
460 /* Number of months in days left */
461 if (leapyear(tm->tm_year))
462 days_in_month(FEBRUARY) = 29;
463 for (i = 1; day >= days_in_month(i); i++)
464 day -= days_in_month(i);
465 days_in_month(FEBRUARY) = 28;
466 tm->tm_mon = i - 1; /* tm_mon starts from 0 to 11 */
468 /* Days are what is left over (+1) from all that. */
469 tm->tm_mday = day + 1;
472 * Determine the day of week
474 tm->tm_wday = (gday + 4) % 7; /* 1970/1/1 was Thursday */
477 EXPORT_SYMBOL(rtc_lock);
478 EXPORT_SYMBOL(to_tm);
479 EXPORT_SYMBOL(rtc_mips_set_time);
480 EXPORT_SYMBOL(rtc_mips_get_time);