2 * arch/xtensa/kernel/time.c
4 * Timer and clock support.
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
10 * Copyright (C) 2005 Tensilica Inc.
12 * Chris Zankel <chris@zankel.net>
15 #include <linux/errno.h>
16 #include <linux/time.h>
17 #include <linux/clocksource.h>
18 #include <linux/interrupt.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/irq.h>
22 #include <linux/profile.h>
23 #include <linux/delay.h>
25 #include <asm/timex.h>
26 #include <asm/platform.h>
28 #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
29 unsigned long ccount_per_jiffy; /* per 1/HZ */
30 unsigned long nsec_per_ccount; /* nsec per ccount increment */
33 static cycle_t ccount_read(void)
35 return (cycle_t)get_ccount();
38 static struct clocksource ccount_clocksource = {
42 .mask = CLOCKSOURCE_MASK(32),
44 * With a shift of 22 the lower limit of the cpu clock is
45 * 1MHz, where NSEC_PER_CCOUNT is 1000 or a bit less than
46 * 2^10: Since we have 32 bits and the multiplicator can
47 * already take up as much as 10 bits, this leaves us with
48 * remaining upper 22 bits.
53 static irqreturn_t timer_interrupt(int irq, void *dev_id);
54 static struct irqaction timer_irqaction = {
55 .handler = timer_interrupt,
56 .flags = IRQF_DISABLED,
60 void __init time_init(void)
62 /* FIXME: xtime&wall_to_monotonic are set in timekeeping_init. */
63 read_persistent_clock(&xtime);
64 set_normalized_timespec(&wall_to_monotonic,
65 -xtime.tv_sec, -xtime.tv_nsec);
67 #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
68 printk("Calibrating CPU frequency ");
69 platform_calibrate_ccount();
70 printk("%d.%02d MHz\n", (int)ccount_per_jiffy/(1000000/HZ),
71 (int)(ccount_per_jiffy/(10000/HZ))%100);
73 ccount_clocksource.mult =
74 clocksource_hz2mult(CCOUNT_PER_JIFFY * HZ,
75 ccount_clocksource.shift);
76 clocksource_register(&ccount_clocksource);
78 /* Initialize the linux timer interrupt. */
80 setup_irq(LINUX_TIMER_INT, &timer_irqaction);
81 set_linux_timer(get_ccount() + CCOUNT_PER_JIFFY);
85 * The timer interrupt is called HZ times per second.
88 irqreturn_t timer_interrupt (int irq, void *dev_id)
93 next = get_linux_timer();
96 while ((signed long)(get_ccount() - next) > 0) {
98 profile_tick(CPU_PROFILING);
100 update_process_times(user_mode(get_irq_regs()));
103 write_seqlock(&xtime_lock);
105 do_timer(1); /* Linux handler in kernel/timer.c */
107 /* Note that writing CCOMPARE clears the interrupt. */
109 next += CCOUNT_PER_JIFFY;
110 set_linux_timer(next);
112 write_sequnlock(&xtime_lock);
115 /* Allow platform to do something useful (Wdog). */
117 platform_heartbeat();
119 /* Make sure we didn't miss any tick... */
121 if ((signed long)(get_ccount() - next) > 0)
127 #ifndef CONFIG_GENERIC_CALIBRATE_DELAY
128 void __cpuinit calibrate_delay(void)
130 loops_per_jiffy = CCOUNT_PER_JIFFY;
131 printk("Calibrating delay loop (skipped)... "
132 "%lu.%02lu BogoMIPS preset\n",
133 loops_per_jiffy/(1000000/HZ),
134 (loops_per_jiffy/(10000/HZ)) % 100);