blob: ffb4741043116dc0bb76a5ccdb11f161257ac9cd [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>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040016#include <linux/sched.h>
Chris Zankel5a0015d2005-06-23 22:01:16 -070017#include <linux/time.h>
Johannes Weinerfcc8f0f2009-03-04 21:39:12 +010018#include <linux/clocksource.h>
Chris Zankel5a0015d2005-06-23 22:01:16 -070019#include <linux/interrupt.h>
20#include <linux/module.h>
21#include <linux/init.h>
22#include <linux/irq.h>
23#include <linux/profile.h>
24#include <linux/delay.h>
Max Filippov2206d5d2012-11-04 00:29:12 +040025#include <linux/irqdomain.h>
Chris Zankel5a0015d2005-06-23 22:01:16 -070026
27#include <asm/timex.h>
28#include <asm/platform.h>
29
Chris Zankel5a0015d2005-06-23 22:01:16 -070030#ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
31unsigned long ccount_per_jiffy; /* per 1/HZ */
Chris Zankel2b8aea72007-08-05 10:26:30 -070032unsigned long nsec_per_ccount; /* nsec per ccount increment */
Chris Zankel5a0015d2005-06-23 22:01:16 -070033#endif
34
Wanlong Gao09378d72011-06-01 22:37:43 +080035static cycle_t ccount_read(struct clocksource *cs)
Johannes Weinerfcc8f0f2009-03-04 21:39:12 +010036{
37 return (cycle_t)get_ccount();
38}
39
40static struct clocksource ccount_clocksource = {
41 .name = "ccount",
42 .rating = 200,
43 .read = ccount_read,
44 .mask = CLOCKSOURCE_MASK(32),
Johannes Weinerfcc8f0f2009-03-04 21:39:12 +010045};
46
Chris Zankelfd43fe12006-12-10 02:18:47 -080047static irqreturn_t timer_interrupt(int irq, void *dev_id);
Chris Zankel5a0015d2005-06-23 22:01:16 -070048static struct irqaction timer_irqaction = {
49 .handler = timer_interrupt,
Thomas Gleixner85ac3ab2006-07-01 19:29:31 -070050 .flags = IRQF_DISABLED,
Chris Zankel5a0015d2005-06-23 22:01:16 -070051 .name = "timer",
52};
53
54void __init time_init(void)
55{
Max Filippov2206d5d2012-11-04 00:29:12 +040056 unsigned int irq;
Chris Zankel288a60c2005-09-22 21:44:23 -070057#ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
Chris Zankel5a0015d2005-06-23 22:01:16 -070058 printk("Calibrating CPU frequency ");
59 platform_calibrate_ccount();
60 printk("%d.%02d MHz\n", (int)ccount_per_jiffy/(1000000/HZ),
61 (int)(ccount_per_jiffy/(10000/HZ))%100);
62#endif
John Stultza1397232010-04-26 20:25:56 -070063 clocksource_register_hz(&ccount_clocksource, CCOUNT_PER_JIFFY * HZ);
Chris Zankel5a0015d2005-06-23 22:01:16 -070064
65 /* Initialize the linux timer interrupt. */
66
Max Filippov2206d5d2012-11-04 00:29:12 +040067 irq = irq_create_mapping(NULL, LINUX_TIMER_INT);
68 setup_irq(irq, &timer_irqaction);
Chris Zankel5a0015d2005-06-23 22:01:16 -070069 set_linux_timer(get_ccount() + CCOUNT_PER_JIFFY);
70}
71
Chris Zankel5a0015d2005-06-23 22:01:16 -070072/*
73 * The timer interrupt is called HZ times per second.
74 */
75
Chris Zankelfd43fe12006-12-10 02:18:47 -080076irqreturn_t timer_interrupt (int irq, void *dev_id)
Chris Zankel5a0015d2005-06-23 22:01:16 -070077{
78
79 unsigned long next;
80
81 next = get_linux_timer();
82
83again:
84 while ((signed long)(get_ccount() - next) > 0) {
85
Chris Zankelfd43fe12006-12-10 02:18:47 -080086 profile_tick(CPU_PROFILING);
Chris Zankel5a0015d2005-06-23 22:01:16 -070087#ifndef CONFIG_SMP
Chris Zankelfd43fe12006-12-10 02:18:47 -080088 update_process_times(user_mode(get_irq_regs()));
Chris Zankel5a0015d2005-06-23 22:01:16 -070089#endif
90
Torben Hohnd12b0e22011-01-27 16:00:27 +010091 xtime_update(1); /* Linux handler in kernel/time/timekeeping */
Chris Zankel2b8aea72007-08-05 10:26:30 -070092
93 /* Note that writing CCOMPARE clears the interrupt. */
94
Chris Zankel5a0015d2005-06-23 22:01:16 -070095 next += CCOUNT_PER_JIFFY;
Chris Zankel2b8aea72007-08-05 10:26:30 -070096 set_linux_timer(next);
Chris Zankel5a0015d2005-06-23 22:01:16 -070097 }
98
Chris Zankel2b8aea72007-08-05 10:26:30 -070099 /* Allow platform to do something useful (Wdog). */
Chris Zankel5a0015d2005-06-23 22:01:16 -0700100
Chris Zankel2b8aea72007-08-05 10:26:30 -0700101 platform_heartbeat();
Chris Zankel5a0015d2005-06-23 22:01:16 -0700102
103 /* Make sure we didn't miss any tick... */
104
105 if ((signed long)(get_ccount() - next) > 0)
106 goto again;
107
Chris Zankel5a0015d2005-06-23 22:01:16 -0700108 return IRQ_HANDLED;
109}
110
111#ifndef CONFIG_GENERIC_CALIBRATE_DELAY
Adrian Bunk6c81c322008-02-06 01:37:51 -0800112void __cpuinit calibrate_delay(void)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700113{
114 loops_per_jiffy = CCOUNT_PER_JIFFY;
115 printk("Calibrating delay loop (skipped)... "
116 "%lu.%02lu BogoMIPS preset\n",
117 loops_per_jiffy/(1000000/HZ),
118 (loops_per_jiffy/(10000/HZ)) % 100);
119}
120#endif