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 volatile unsigned long wall_jiffies;
45 spinlock_t rtc_lock = SPIN_LOCK_UNLOCKED;
48 * whether we emulate local_timer_interrupts for SMP machines.
50 int emulate_local_timer_interrupt;
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_get_time)(void) = null_rtc_get_time;
66 int (*rtc_set_time)(unsigned long) = null_rtc_set_time;
70 * This version of gettimeofday has microsecond resolution and better than
71 * microsecond precision on fast machines with cycle counter.
73 void do_gettimeofday(struct timeval *tv)
76 unsigned long usec, sec;
79 seq = read_seqbegin(&xtime_lock);
80 usec = do_gettimeoffset();
82 unsigned long lost = jiffies - wall_jiffies;
84 usec += lost * (1000000 / HZ);
87 usec += (xtime.tv_nsec / 1000);
88 } while (read_seqretry(&xtime_lock, seq));
90 while (usec >= 1000000) {
99 int do_settimeofday(struct timespec *tv)
101 time_t wtm_sec, sec = tv->tv_sec;
102 long wtm_nsec, nsec = tv->tv_nsec;
104 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
107 write_seqlock_irq(&xtime_lock);
109 * This is revolting. We need to set "xtime" correctly. However, the
110 * value in this location is the value at the most recent update of
111 * wall time. Discover what correction gettimeofday() would have
112 * made, and then undo it!
114 nsec -= do_gettimeoffset() * NSEC_PER_USEC;
115 nsec -= (jiffies - wall_jiffies) * TICK_NSEC;
117 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - tsec);
118 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
120 set_normalized_timespec(&xtime, sec, nsec);
121 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
123 time_adjust = 0; /* stop active adjtime() */
124 time_status |= STA_UNSYNC;
125 time_maxerror = NTP_PHASE_LIMIT;
126 time_esterror = NTP_PHASE_LIMIT;
127 write_sequnlock_irq(&xtime_lock);
134 * Gettimeoffset routines. These routines returns the time duration
135 * since last timer interrupt in usecs.
137 * If the exact CPU counter frequency is known, use fixed_rate_gettimeoffset.
138 * Otherwise use calibrate_gettimeoffset()
140 * If the CPU does not have counter register all, you can either supply
141 * your own gettimeoffset() routine, or use null_gettimeoffset() routines,
142 * which gives the same resolution as HZ.
146 /* This is for machines which generate the exact clock. */
147 #define USECS_PER_JIFFY (1000000/HZ)
149 /* usecs per counter cycle, shifted to left by 32 bits */
150 static unsigned int sll32_usecs_per_cycle=0;
152 /* how many counter cycles in a jiffy */
153 static unsigned long cycles_per_jiffy=0;
155 /* Cycle counter value at the previous timer interrupt.. */
156 static unsigned int timerhi, timerlo;
158 /* expirelo is the count value for next CPU timer interrupt */
159 static unsigned int expirelo;
161 /* last time when xtime and rtc are sync'ed up */
162 static long last_rtc_update;
164 /* the function pointer to one of the gettimeoffset funcs*/
165 unsigned long (*do_gettimeoffset)(void) = null_gettimeoffset;
167 unsigned long null_gettimeoffset(void)
172 unsigned long fixed_rate_gettimeoffset(void)
177 /* Get last timer tick in absolute kernel time */
178 count = read_c0_count();
180 /* .. relative to previous jiffy (32 bits is enough) */
183 __asm__("multu\t%1,%2\n\t"
187 "r" (sll32_usecs_per_cycle));
190 * Due to possible jiffies inconsistencies, we need to check
191 * the result so that we'll get a timer that is monotonic.
193 if (res >= USECS_PER_JIFFY)
194 res = USECS_PER_JIFFY-1;
200 * Cached "1/(clocks per usec)*2^32" value.
201 * It has to be recalculated once each jiffy.
203 static unsigned long cached_quotient;
205 /* Last jiffy when calibrate_divXX_gettimeoffset() was called. */
206 static unsigned long last_jiffies = 0;
210 * This is copied from dec/time.c:do_ioasic_gettimeoffset() by Mercij.
212 unsigned long calibrate_div32_gettimeoffset(void)
215 unsigned long res, tmp;
216 unsigned long quotient;
220 quotient = cached_quotient;
222 if (last_jiffies != tmp) {
224 if (last_jiffies != 0) {
226 do_div64_32(r0, timerhi, timerlo, tmp);
227 do_div64_32(quotient, USECS_PER_JIFFY,
228 USECS_PER_JIFFY_FRAC, r0);
229 cached_quotient = quotient;
233 /* Get last timer tick in absolute kernel time */
234 count = read_c0_count();
236 /* .. relative to previous jiffy (32 bits is enough) */
239 __asm__("multu %2,%3"
240 : "=l" (tmp), "=h" (res)
241 : "r" (count), "r" (quotient));
244 * Due to possible jiffies inconsistencies, we need to check
245 * the result so that we'll get a timer that is monotonic.
247 if (res >= USECS_PER_JIFFY)
248 res = USECS_PER_JIFFY - 1;
253 unsigned long calibrate_div64_gettimeoffset(void)
256 unsigned long res, tmp;
257 unsigned long quotient;
261 quotient = cached_quotient;
263 if (tmp && last_jiffies != tmp) {
265 __asm__(".set\tnoreorder\n\t"
269 "dsll32\t$1,%1,0\n\t"
271 "ddivu\t$0,$1,%3\n\t"
273 "dsll32\t%0,%4,0\n\t"
275 "ddivu\t$0,%0,$1\n\t"
284 "r" (USECS_PER_JIFFY));
285 cached_quotient = quotient;
288 /* Get last timer tick in absolute kernel time */
289 count = read_c0_count();
291 /* .. relative to previous jiffy (32 bits is enough) */
294 __asm__("multu\t%1,%2\n\t"
301 * Due to possible jiffies inconsistencies, we need to check
302 * the result so that we'll get a timer that is monotonic.
304 if (res >= USECS_PER_JIFFY)
305 res = USECS_PER_JIFFY-1;
312 * local_timer_interrupt() does profiling and process accounting
313 * on a per-CPU basis.
315 * In UP mode, it is invoked from the (global) timer_interrupt.
317 * In SMP mode, it might invoked by per-CPU timer interrupt, or
318 * a broadcasted inter-processor interrupt which itself is triggered
319 * by the global timer interrupt.
321 void local_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
323 if (!user_mode(regs)) {
324 if (prof_buffer && current->pid) {
326 unsigned long pc = regs->cp0_epc;
328 pc -= (unsigned long) &_stext;
331 * Dont ignore out-of-bounds pc values silently,
332 * put them into the last histogram slot, so if
333 * present, they will show up as a sharp peak.
337 atomic_inc((atomic_t *)&prof_buffer[pc]);
342 /* in UP mode, update_process_times() is invoked by do_timer() */
343 update_process_times(user_mode(regs));
348 * high-level timer interrupt service routines. This function
349 * is set as irqaction->handler and is invoked through do_IRQ.
351 irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
353 if (cpu_has_counter) {
356 /* ack timer interrupt, and try to set next interrupt */
357 expirelo += cycles_per_jiffy;
358 write_c0_compare(expirelo);
359 count = read_c0_count();
361 /* check to see if we have missed any timer interrupts */
362 if ((count - expirelo) < 0x7fffffff) {
363 /* missed_timer_count ++; */
364 expirelo = count + cycles_per_jiffy;
365 write_c0_compare(expirelo);
368 /* Update timerhi/timerlo for intra-jiffy calibration. */
369 timerhi += count < timerlo; /* Wrap around */
374 * call the generic timer interrupt handling
379 * If we have an externally synchronized Linux clock, then update
380 * CMOS clock accordingly every ~11 minutes. rtc_set_time() has to be
381 * called as close as possible to 500 ms before the new second starts.
383 write_seqlock(&xtime_lock);
384 if ((time_status & STA_UNSYNC) == 0 &&
385 xtime.tv_sec > last_rtc_update + 660 &&
386 (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
387 (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
388 if (rtc_set_time(xtime.tv_sec) == 0) {
389 last_rtc_update = xtime.tv_sec;
391 last_rtc_update = xtime.tv_sec - 600;
392 /* do it again in 60 s */
395 write_sequnlock(&xtime_lock);
398 * If jiffies has overflowed in this timer_interrupt we must
399 * update the timer[hi]/[lo] to make fast gettimeoffset funcs
400 * quotient calc still valid. -arca
403 timerhi = timerlo = 0;
406 #if !defined(CONFIG_SMP)
408 * In UP mode, we call local_timer_interrupt() to do profiling
409 * and process accouting.
411 * In SMP mode, local_timer_interrupt() is invoked by appropriate
412 * low-level local timer interrupt handler.
414 local_timer_interrupt(0, NULL, regs);
416 #else /* CONFIG_SMP */
418 if (emulate_local_timer_interrupt) {
420 * this is the place where we send out inter-process
421 * interrupts and let each CPU do its own profiling
422 * and process accouting.
424 * Obviously we need to call local_timer_interrupt() for
425 * the current CPU too.
427 panic("Not implemented yet!!!");
429 #endif /* CONFIG_SMP */
434 asmlinkage void ll_timer_interrupt(int irq, struct pt_regs *regs)
436 int cpu = smp_processor_id();
439 kstat_cpu(cpu).irqs[irq]++;
441 /* we keep interrupt disabled all the time */
442 timer_interrupt(irq, NULL, regs);
446 if (softirq_pending(cpu))
450 asmlinkage void ll_local_timer_interrupt(int irq, struct pt_regs *regs)
452 int cpu = smp_processor_id();
455 kstat_cpu(cpu).irqs[irq]++;
457 /* we keep interrupt disabled all the time */
458 local_timer_interrupt(irq, NULL, regs);
462 if (softirq_pending(cpu))
467 * time_init() - it does the following things.
469 * 1) board_time_init() -
470 * a) (optional) set up RTC routines,
471 * b) (optional) calibrate and set the mips_counter_frequency
472 * (only needed if you intended to use fixed_rate_gettimeoffset
473 * or use cpu counter as timer interrupt source)
474 * 2) setup xtime based on rtc_get_time().
475 * 3) choose a appropriate gettimeoffset routine.
476 * 4) calculate a couple of cached variables for later usage
477 * 5) board_timer_setup() -
478 * a) (optional) over-write any choices made above by time_init().
479 * b) machine specific code should setup the timer irqaction.
480 * c) enable the timer interrupt
483 void (*board_time_init)(void) = NULL;
484 void (*board_timer_setup)(struct irqaction *irq) = NULL;
486 unsigned int mips_counter_frequency = 0;
488 static struct irqaction timer_irqaction = {
497 void __init time_init(void)
502 xtime.tv_sec = rtc_get_time();
504 set_normalized_timespec(&wall_to_monotonic,
505 -xtime.tv_sec, -xtime.tv_nsec);
507 /* choose appropriate gettimeoffset routine */
508 if (!cpu_has_counter) {
509 /* no cpu counter - sorry */
510 do_gettimeoffset = null_gettimeoffset;
511 } else if (mips_counter_frequency != 0) {
512 /* we have cpu counter and know counter frequency! */
513 do_gettimeoffset = fixed_rate_gettimeoffset;
514 } else if ((current_cpu_data.isa_level == MIPS_CPU_ISA_M32) ||
515 (current_cpu_data.isa_level == MIPS_CPU_ISA_I) ||
516 (current_cpu_data.isa_level == MIPS_CPU_ISA_II) ) {
517 /* we need to calibrate the counter but we don't have
518 * 64-bit division. */
519 do_gettimeoffset = calibrate_div32_gettimeoffset;
521 /* we need to calibrate the counter but we *do* have
522 * 64-bit division. */
523 do_gettimeoffset = calibrate_div64_gettimeoffset;
526 /* caclulate cache parameters */
527 if (mips_counter_frequency) {
528 cycles_per_jiffy = mips_counter_frequency / HZ;
530 /* sll32_usecs_per_cycle = 10^6 * 2^32 / mips_counter_freq */
531 /* any better way to do this? */
532 sll32_usecs_per_cycle = mips_counter_frequency / 100000;
533 sll32_usecs_per_cycle = 0xffffffff / sll32_usecs_per_cycle;
534 sll32_usecs_per_cycle *= 10;
537 * For those using cpu counter as timer, this sets up the
540 write_c0_compare(cycles_per_jiffy);
542 expirelo = cycles_per_jiffy;
546 * Call board specific timer interrupt setup.
548 * this pointer must be setup in machine setup routine.
550 * Even if the machine choose to use low-level timer interrupt,
551 * it still needs to setup the timer_irqaction.
552 * In that case, it might be better to set timer_irqaction.handler
553 * to be NULL function so that we are sure the high-level code
554 * is not invoked accidentally.
556 board_timer_setup(&timer_irqaction);
560 #define STARTOFTIME 1970
561 #define SECDAY 86400L
562 #define SECYR (SECDAY * 365)
563 #define leapyear(year) ((year) % 4 == 0)
564 #define days_in_year(a) (leapyear(a) ? 366 : 365)
565 #define days_in_month(a) (month_days[(a) - 1])
567 static int month_days[12] = {
568 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
571 void to_tm(unsigned long tim, struct rtc_time * tm)
576 gday = day = tim / SECDAY;
579 /* Hours, minutes, seconds are easy */
580 tm->tm_hour = hms / 3600;
581 tm->tm_min = (hms % 3600) / 60;
582 tm->tm_sec = (hms % 3600) % 60;
584 /* Number of years in days */
585 for (i = STARTOFTIME; day >= days_in_year(i); i++)
586 day -= days_in_year(i);
589 /* Number of months in days left */
590 if (leapyear(tm->tm_year))
591 days_in_month(FEBRUARY) = 29;
592 for (i = 1; day >= days_in_month(i); i++)
593 day -= days_in_month(i);
594 days_in_month(FEBRUARY) = 28;
595 tm->tm_mon = i-1; /* tm_mon starts from 0 to 11 */
597 /* Days are what is left over (+1) from all that. */
598 tm->tm_mday = day + 1;
601 * Determine the day of week
603 tm->tm_wday = (gday + 4) % 7; /* 1970/1/1 was Thursday */
606 EXPORT_SYMBOL(rtc_lock);