blob: 19085ff0484a27df21142c14a3fcc5384b4497a6 [file] [log] [blame]
Chris Zankel5a0015d2005-06-23 22:01:16 -07001/*
2 * arch/xtensa/kernel/time.c
3 *
4 * Timer and clock support.
5 *
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
8 * for more details.
9 *
10 * Copyright (C) 2005 Tensilica Inc.
11 *
12 * Chris Zankel <chris@zankel.net>
13 */
14
Chris Zankel5a0015d2005-06-23 22:01:16 -070015#include <linux/errno.h>
16#include <linux/time.h>
Johannes Weinerfcc8f0f2009-03-04 21:39:12 +010017#include <linux/clocksource.h>
Chris Zankel5a0015d2005-06-23 22:01:16 -070018#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>
24
25#include <asm/timex.h>
26#include <asm/platform.h>
27
Chris Zankel5a0015d2005-06-23 22:01:16 -070028#ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
29unsigned long ccount_per_jiffy; /* per 1/HZ */
Chris Zankel2b8aea72007-08-05 10:26:30 -070030unsigned long nsec_per_ccount; /* nsec per ccount increment */
Chris Zankel5a0015d2005-06-23 22:01:16 -070031#endif
32
Johannes Weinerfcc8f0f2009-03-04 21:39:12 +010033static cycle_t ccount_read(void)
34{
35 return (cycle_t)get_ccount();
36}
37
38static struct clocksource ccount_clocksource = {
39 .name = "ccount",
40 .rating = 200,
41 .read = ccount_read,
42 .mask = CLOCKSOURCE_MASK(32),
43 /*
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.
49 */
50 .shift = 22,
51};
52
Chris Zankelfd43fe12006-12-10 02:18:47 -080053static irqreturn_t timer_interrupt(int irq, void *dev_id);
Chris Zankel5a0015d2005-06-23 22:01:16 -070054static struct irqaction timer_irqaction = {
55 .handler = timer_interrupt,
Thomas Gleixner85ac3ab2006-07-01 19:29:31 -070056 .flags = IRQF_DISABLED,
Chris Zankel5a0015d2005-06-23 22:01:16 -070057 .name = "timer",
58};
59
60void __init time_init(void)
61{
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +020062 /* FIXME: xtime&wall_to_monotonic are set in timekeeping_init. */
63 read_persistent_clock(&xtime);
Johannes Weinerfcc8f0f2009-03-04 21:39:12 +010064 set_normalized_timespec(&wall_to_monotonic,
65 -xtime.tv_sec, -xtime.tv_nsec);
Chris Zankel5a0015d2005-06-23 22:01:16 -070066
Chris Zankel288a60c2005-09-22 21:44:23 -070067#ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
Chris Zankel5a0015d2005-06-23 22:01:16 -070068 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);
72#endif
Johannes Weinerfcc8f0f2009-03-04 21:39:12 +010073 ccount_clocksource.mult =
74 clocksource_hz2mult(CCOUNT_PER_JIFFY * HZ,
75 ccount_clocksource.shift);
76 clocksource_register(&ccount_clocksource);
Chris Zankel5a0015d2005-06-23 22:01:16 -070077
78 /* Initialize the linux timer interrupt. */
79
80 setup_irq(LINUX_TIMER_INT, &timer_irqaction);
81 set_linux_timer(get_ccount() + CCOUNT_PER_JIFFY);
82}
83
Chris Zankel5a0015d2005-06-23 22:01:16 -070084/*
85 * The timer interrupt is called HZ times per second.
86 */
87
Chris Zankelfd43fe12006-12-10 02:18:47 -080088irqreturn_t timer_interrupt (int irq, void *dev_id)
Chris Zankel5a0015d2005-06-23 22:01:16 -070089{
90
91 unsigned long next;
92
93 next = get_linux_timer();
94
95again:
96 while ((signed long)(get_ccount() - next) > 0) {
97
Chris Zankelfd43fe12006-12-10 02:18:47 -080098 profile_tick(CPU_PROFILING);
Chris Zankel5a0015d2005-06-23 22:01:16 -070099#ifndef CONFIG_SMP
Chris Zankelfd43fe12006-12-10 02:18:47 -0800100 update_process_times(user_mode(get_irq_regs()));
Chris Zankel5a0015d2005-06-23 22:01:16 -0700101#endif
102
103 write_seqlock(&xtime_lock);
104
Chris Zankel2b8aea72007-08-05 10:26:30 -0700105 do_timer(1); /* Linux handler in kernel/timer.c */
106
107 /* Note that writing CCOMPARE clears the interrupt. */
108
Chris Zankel5a0015d2005-06-23 22:01:16 -0700109 next += CCOUNT_PER_JIFFY;
Chris Zankel2b8aea72007-08-05 10:26:30 -0700110 set_linux_timer(next);
Chris Zankel5a0015d2005-06-23 22:01:16 -0700111
Chris Zankel5a0015d2005-06-23 22:01:16 -0700112 write_sequnlock(&xtime_lock);
113 }
114
Chris Zankel2b8aea72007-08-05 10:26:30 -0700115 /* Allow platform to do something useful (Wdog). */
Chris Zankel5a0015d2005-06-23 22:01:16 -0700116
Chris Zankel2b8aea72007-08-05 10:26:30 -0700117 platform_heartbeat();
Chris Zankel5a0015d2005-06-23 22:01:16 -0700118
119 /* Make sure we didn't miss any tick... */
120
121 if ((signed long)(get_ccount() - next) > 0)
122 goto again;
123
Chris Zankel5a0015d2005-06-23 22:01:16 -0700124 return IRQ_HANDLED;
125}
126
127#ifndef CONFIG_GENERIC_CALIBRATE_DELAY
Adrian Bunk6c81c322008-02-06 01:37:51 -0800128void __cpuinit calibrate_delay(void)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700129{
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);
135}
136#endif