1 /* $Id: time.c,v 1.19 2004/02/27 00:40:48 lethal Exp $
3 * linux/arch/sh/kernel/time.c
5 * Copyright (C) 1999 Tetsuya Okada & Niibe Yutaka
6 * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
7 * Copyright (C) 2002, 2003 Paul Mundt
8 * Copyright (C) 2002 M. R. Brown <mrbrown@linux-sh.org>
10 * Some code taken from i386 version.
11 * Copyright (C) 1991, 1992, 1995 Linus Torvalds
14 #include <linux/config.h>
15 #include <linux/errno.h>
16 #include <linux/module.h>
17 #include <linux/sched.h>
18 #include <linux/kernel.h>
19 #include <linux/param.h>
20 #include <linux/string.h>
22 #include <linux/interrupt.h>
23 #include <linux/time.h>
24 #include <linux/delay.h>
25 #include <linux/init.h>
26 #include <linux/smp.h>
28 #include <asm/processor.h>
29 #include <asm/uaccess.h>
32 #include <asm/delay.h>
33 #include <asm/machvec.h>
40 #include <linux/timex.h>
41 #include <linux/irq.h>
43 #define TMU_TOCR_INIT 0x00
44 #define TMU0_TCR_INIT 0x0020
45 #define TMU_TSTR_INIT 1
47 #define TMU0_TCR_CALIB 0x0000
49 #if defined(CONFIG_CPU_SH3)
50 #define TMU_TOCR 0xfffffe90 /* Byte access */
51 #define TMU_TSTR 0xfffffe92 /* Byte access */
53 #define TMU0_TCOR 0xfffffe94 /* Long access */
54 #define TMU0_TCNT 0xfffffe98 /* Long access */
55 #define TMU0_TCR 0xfffffe9c /* Word access */
56 #elif defined(CONFIG_CPU_SH4)
57 #define TMU_TOCR 0xffd80000 /* Byte access */
58 #define TMU_TSTR 0xffd80004 /* Byte access */
60 #define TMU0_TCOR 0xffd80008 /* Long access */
61 #define TMU0_TCNT 0xffd8000c /* Long access */
62 #define TMU0_TCR 0xffd80010 /* Word access */
64 #ifdef CONFIG_CPU_SUBTYPE_ST40STB1
65 #define CLOCKGEN_MEMCLKCR 0xbb040038
66 #define MEMCLKCR_RATIO_MASK 0x7
67 #endif /* CONFIG_CPU_SUBTYPE_ST40STB1 */
68 #endif /* CONFIG_CPU_SH3 or CONFIG_CPU_SH4 */
70 extern unsigned long wall_jiffies;
71 #define TICK_SIZE (tick_nsec / 1000)
72 spinlock_t tmu0_lock = SPIN_LOCK_UNLOCKED;
74 u64 jiffies_64 = INITIAL_JIFFIES;
76 EXPORT_SYMBOL(jiffies_64);
78 /* XXX: Can we initialize this in a routine somewhere? Dreamcast doesn't want
79 * these routines anywhere... */
81 void (*rtc_get_time)(struct timespec *) = sh_rtc_gettimeofday;
82 int (*rtc_set_time)(const time_t) = sh_rtc_settimeofday;
84 void (*rtc_get_time)(struct timespec *) = 0;
85 int (*rtc_set_time)(const time_t) = 0;
88 #if defined(CONFIG_CPU_SH3)
89 static int stc_multipliers[] = { 1, 2, 3, 4, 6, 1, 1, 1 };
90 static int stc_values[] = { 0, 1, 4, 2, 5, 0, 0, 0 };
91 #define bfc_divisors stc_multipliers
92 #define bfc_values stc_values
93 static int ifc_divisors[] = { 1, 2, 3, 4, 1, 1, 1, 1 };
94 static int ifc_values[] = { 0, 1, 4, 2, 0, 0, 0, 0 };
95 static int pfc_divisors[] = { 1, 2, 3, 4, 6, 1, 1, 1 };
96 static int pfc_values[] = { 0, 1, 4, 2, 5, 0, 0, 0 };
97 #elif defined(CONFIG_CPU_SH4)
98 static int ifc_divisors[] = { 1, 2, 3, 4, 6, 8, 1, 1 };
99 static int ifc_values[] = { 0, 1, 2, 3, 0, 4, 0, 5 };
100 #define bfc_divisors ifc_divisors /* Same */
101 #define bfc_values ifc_values
102 static int pfc_divisors[] = { 2, 3, 4, 6, 8, 2, 2, 2 };
103 static int pfc_values[] = { 0, 0, 1, 2, 0, 3, 0, 4 };
105 #error "Unknown ifc/bfc/pfc/stc values for this processor"
109 * Scheduler clock - returns current time in nanosec units.
111 unsigned long long sched_clock(void)
113 return (unsigned long long)jiffies * (1000000000 / HZ);
116 static unsigned long do_gettimeoffset(void)
121 static int count_p = 0x7fffffff; /* for the first call after boot */
122 static unsigned long jiffies_p = 0;
125 * cache volatile jiffies temporarily; we have IRQs turned off.
127 unsigned long jiffies_t;
129 spin_lock_irqsave(&tmu0_lock, flags);
130 /* timer count may underflow right here */
131 count = ctrl_inl(TMU0_TCNT); /* read the latched count */
136 * avoiding timer inconsistencies (they are rare, but they happen)...
137 * there is one kind of problem that must be avoided here:
138 * 1. the timer counter underflows
141 if( jiffies_t == jiffies_p ) {
142 if( count > count_p ) {
145 if(ctrl_inw(TMU0_TCR) & 0x100) { /* Check UNF bit */
147 * We cannot detect lost timer interrupts ...
148 * well, that's why we call them lost, don't we? :)
149 * [hmm, on the Pentium and Alpha we can ... sort of]
153 printk("do_slow_gettimeoffset(): hardware timer problem?\n");
157 jiffies_p = jiffies_t;
160 spin_unlock_irqrestore(&tmu0_lock, flags);
162 count = ((LATCH-1) - count) * TICK_SIZE;
163 count = (count + LATCH/2) / LATCH;
168 void do_gettimeofday(struct timeval *tv)
171 unsigned long usec, sec;
175 seq = read_seqbegin(&xtime_lock);
176 usec = do_gettimeoffset();
178 lost = jiffies - wall_jiffies;
180 usec += lost * (1000000 / HZ);
183 usec += xtime.tv_nsec / 1000;
184 } while (read_seqretry(&xtime_lock, seq));
186 while (usec >= 1000000) {
195 EXPORT_SYMBOL(do_gettimeofday);
197 int do_settimeofday(struct timespec *tv)
199 time_t wtm_sec, sec = tv->tv_sec;
200 long wtm_nsec, nsec = tv->tv_nsec;
202 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
205 write_seqlock_irq(&xtime_lock);
207 * This is revolting. We need to set "xtime" correctly. However, the
208 * value in this location is the value at the most recent update of
209 * wall time. Discover what correction gettimeofday() would have
210 * made, and then undo it!
212 nsec -= 1000 * (do_gettimeoffset() +
213 (jiffies - wall_jiffies) * (1000000 / HZ));
215 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
216 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
218 set_normalized_timespec(&xtime, sec, nsec);
219 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
221 time_adjust = 0; /* stop active adjtime() */
222 time_status |= STA_UNSYNC;
223 time_maxerror = NTP_PHASE_LIMIT;
224 time_esterror = NTP_PHASE_LIMIT;
225 write_sequnlock_irq(&xtime_lock);
231 EXPORT_SYMBOL(do_settimeofday);
233 /* last time the RTC clock got updated */
234 static long last_rtc_update;
236 /* Profiling definitions */
237 extern unsigned long prof_cpu_mask;
238 extern unsigned int * prof_buffer;
239 extern unsigned long prof_len;
240 extern unsigned long prof_shift;
243 static inline void sh_do_profile(unsigned long pc)
245 /* Don't profile cpu_idle.. */
246 if (!prof_buffer || !current->pid)
249 if (pc >= 0xa0000000UL && pc < 0xc0000000UL)
252 pc -= (unsigned long)&_stext;
256 * Don't ignore out-of-bounds PC values silently,
257 * put them into the last histogram slot, so if
258 * present, they will show up as a sharp peak.
260 if (pc > prof_len - 1)
263 atomic_inc((atomic_t *)&prof_buffer[pc]);
267 * timer_interrupt() needs to keep up the real-time clock,
268 * as well as call the "do_timer()" routine every clocktick
270 static inline void do_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
274 if (!user_mode(regs))
275 sh_do_profile(regs->pc);
277 #ifdef CONFIG_HEARTBEAT
278 if (sh_mv.mv_heartbeat != NULL)
279 sh_mv.mv_heartbeat();
283 * If we have an externally synchronized Linux clock, then update
284 * RTC clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
285 * called as close as possible to 500 ms before the new second starts.
287 if ((time_status & STA_UNSYNC) == 0 &&
288 xtime.tv_sec > last_rtc_update + 660 &&
289 (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
290 (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
291 if (rtc_set_time(xtime.tv_sec) == 0)
292 last_rtc_update = xtime.tv_sec;
294 last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
299 * This is the same as the above, except we _also_ save the current
300 * Time Stamp Counter value at the time of the timer interrupt, so that
301 * we later on can estimate the time of day more exactly.
303 static irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
305 unsigned long timer_status;
308 timer_status = ctrl_inw(TMU0_TCR);
309 timer_status &= ~0x100;
310 ctrl_outw(timer_status, TMU0_TCR);
313 * Here we are in the timer irq handler. We just have irqs locally
314 * disabled but we don't know if the timer_bh is running on the other
315 * CPU. We need to avoid to SMP race with it. NOTE: we don' t need
316 * the irq version of write_lock because as just said we have irq
317 * locally disabled. -arca
319 write_seqlock(&xtime_lock);
320 do_timer_interrupt(irq, NULL, regs);
321 write_sequnlock(&xtime_lock);
327 * Hah! We'll see if this works (switching from usecs to nsecs).
329 static unsigned int __init get_timer_frequency(void)
332 struct timespec ts1, ts2;
333 unsigned long diff_nsec;
334 unsigned long factor;
336 /* Setup the timer: We don't want to generate interrupts, just
337 * have it count down at its natural rate.
339 ctrl_outb(0, TMU_TSTR);
340 ctrl_outb(TMU_TOCR_INIT, TMU_TOCR);
341 ctrl_outw(TMU0_TCR_CALIB, TMU0_TCR);
342 ctrl_outl(0xffffffff, TMU0_TCOR);
343 ctrl_outl(0xffffffff, TMU0_TCNT);
349 } while (ts1.tv_nsec == ts2.tv_nsec && ts1.tv_sec == ts2.tv_sec);
351 /* actually start the timer */
352 ctrl_outb(TMU_TSTR_INIT, TMU_TSTR);
356 } while (ts1.tv_nsec == ts2.tv_nsec && ts1.tv_sec == ts2.tv_sec);
358 freq = 0xffffffff - ctrl_inl(TMU0_TCNT);
359 if (ts2.tv_nsec < ts1.tv_nsec) {
360 ts2.tv_nsec += 1000000000;
364 diff_nsec = (ts2.tv_sec - ts1.tv_sec) * 1000000000 + (ts2.tv_nsec - ts1.tv_nsec);
366 /* this should work well if the RTC has a precision of n Hz, where
367 * n is an integer. I don't think we have to worry about the other
369 factor = (1000000000 + diff_nsec/2) / diff_nsec;
371 if (factor * diff_nsec > 1100000000 ||
372 factor * diff_nsec < 900000000)
373 panic("weird RTC (diff_nsec %ld)", diff_nsec);
375 return freq * factor;
378 void (*board_time_init)(void) = 0;
379 void (*board_timer_setup)(struct irqaction *irq) = 0;
381 static unsigned int sh_pclk_freq __initdata = CONFIG_SH_PCLK_FREQ;
383 static int __init sh_pclk_setup(char *str)
387 if (get_option(&str, &freq))
392 __setup("sh_pclk=", sh_pclk_setup);
394 static struct irqaction irq0 = { timer_interrupt, SA_INTERRUPT, 0, "timer", NULL, NULL};
396 void get_current_frequency_divisors(unsigned int *ifc, unsigned int *bfc, unsigned int *pfc)
398 unsigned int frqcr = ctrl_inw(FRQCR);
400 #if defined(CONFIG_CPU_SH3)
403 tmp = (frqcr & 0x8000) >> 13;
404 tmp |= (frqcr & 0x0030) >> 4;
405 *bfc = stc_multipliers[tmp];
406 tmp = (frqcr & 0x4000) >> 12;
407 tmp |= (frqcr & 0x000c) >> 2;
408 *ifc = ifc_divisors[tmp];
409 tmp = (frqcr & 0x2000) >> 11;
410 tmp |= frqcr & 0x0003;
411 *pfc = pfc_divisors[tmp];
412 #elif defined(CONFIG_CPU_SH4)
413 *ifc = ifc_divisors[(frqcr >> 6) & 0x0007];
414 *bfc = bfc_divisors[(frqcr >> 3) & 0x0007];
415 *pfc = pfc_divisors[frqcr & 0x0007];
420 * This bit of ugliness builds up accessor routines to get at both
421 * the divisors and the physical values.
423 #define _FREQ_TABLE(x) \
424 unsigned int get_##x##_divisor(unsigned int value) \
425 { return x##_divisors[value]; } \
427 unsigned int get_##x##_value(unsigned int divisor) \
428 { return x##_values[(divisor - 1)]; }
434 void __init time_init(void)
436 unsigned int timer_freq = 0;
437 unsigned int ifc, pfc, bfc;
438 unsigned long interval;
443 get_current_frequency_divisors(&ifc, &bfc, &pfc);
446 * If we don't have an RTC (such as with the SH7300), don't attempt to
447 * probe the timer frequency. Rely on an either hardcoded peripheral
448 * clock value, or on the sh_pclk command line option.
450 current_cpu_data.module_clock = sh_pclk_freq;
452 /* XXX: Switch this over to a more generic test. */
453 if (current_cpu_data.type != CPU_SH7300) {
457 * If we've specified a peripheral clock frequency, and we have
458 * an RTC, compare it against the autodetected value. Complain
459 * if there's a mismatch.
461 * Note: We should allow for some high and low watermarks for
462 * the frequency here (compensating for potential drift), as
463 * otherwise we'll likely end up triggering this essentially
466 timer_freq = get_timer_frequency();
467 freq = timer_freq * 4;
469 if (sh_pclk_freq && sh_pclk_freq != freq) {
470 printk(KERN_NOTICE "Calculated peripheral clock value "
471 "%d differs from sh_pclk value %d, fixing..\n",
473 current_cpu_data.module_clock = freq;
477 rtc_get_time(&xtime);
479 set_normalized_timespec(&wall_to_monotonic,
480 -xtime.tv_sec, -xtime.tv_nsec);
482 if (board_timer_setup) {
483 board_timer_setup(&irq0);
485 setup_irq(TIMER_IRQ, &irq0);
488 if (!current_cpu_data.master_clock)
489 current_cpu_data.master_clock = current_cpu_data.module_clock * pfc;
490 if (!current_cpu_data.bus_clock)
491 current_cpu_data.bus_clock = current_cpu_data.master_clock / bfc;
492 if (!current_cpu_data.cpu_clock)
493 current_cpu_data.cpu_clock = current_cpu_data.master_clock / ifc;
495 printk("CPU clock: %d.%02dMHz\n",
496 (current_cpu_data.cpu_clock / 1000000),
497 (current_cpu_data.cpu_clock % 1000000)/10000);
498 printk("Bus clock: %d.%02dMHz\n",
499 (current_cpu_data.bus_clock / 1000000),
500 (current_cpu_data.bus_clock % 1000000)/10000);
501 #ifdef CONFIG_CPU_SUBTYPE_ST40STB1
502 printk("Memory clock: %d.%02dMHz\n",
503 (current_cpu_data.memory_clock / 1000000),
504 (current_cpu_data.memory_clock % 1000000)/10000);
506 printk("Module clock: %d.%02dMHz\n",
507 (current_cpu_data.module_clock / 1000000),
508 (current_cpu_data.module_clock % 1000000)/10000);
509 interval = (current_cpu_data.module_clock/4 + HZ/2) / HZ;
511 printk("Interval = %ld\n", interval);
514 ctrl_outb(0, TMU_TSTR);
515 ctrl_outb(TMU_TOCR_INIT, TMU_TOCR);
516 ctrl_outw(TMU0_TCR_INIT, TMU0_TCR);
517 ctrl_outl(interval, TMU0_TCOR);
518 ctrl_outl(interval, TMU0_TCNT);
519 ctrl_outb(TMU_TSTR_INIT, TMU_TSTR);
521 #if defined(CONFIG_SH_KGDB)
523 * Set up kgdb as requested. We do it here because the serial
524 * init uses the timer vars we just set up for figuring baud.