blob: 08b769d3b3a1c48482179eac8c68c562f61321c4 [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>
Baruch Siach925f5532013-06-18 08:48:53 +030019#include <linux/clockchips.h>
Chris Zankel5a0015d2005-06-23 22:01:16 -070020#include <linux/interrupt.h>
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/irq.h>
24#include <linux/profile.h>
25#include <linux/delay.h>
Max Filippov2206d5d2012-11-04 00:29:12 +040026#include <linux/irqdomain.h>
Baruch Siache3f43292013-06-17 11:29:46 +030027#include <linux/sched_clock.h>
Chris Zankel5a0015d2005-06-23 22:01:16 -070028
29#include <asm/timex.h>
30#include <asm/platform.h>
31
Baruch Siache504c4b2013-06-17 11:29:43 +030032unsigned long ccount_freq; /* ccount Hz */
Chris Zankel5a0015d2005-06-23 22:01:16 -070033
Wanlong Gao09378d72011-06-01 22:37:43 +080034static cycle_t ccount_read(struct clocksource *cs)
Johannes Weinerfcc8f0f2009-03-04 21:39:12 +010035{
36 return (cycle_t)get_ccount();
37}
38
Stephen Boyd3ade4f82013-12-13 01:43:58 -080039static u64 notrace ccount_sched_clock_read(void)
Baruch Siache3f43292013-06-17 11:29:46 +030040{
41 return get_ccount();
42}
43
Johannes Weinerfcc8f0f2009-03-04 21:39:12 +010044static struct clocksource ccount_clocksource = {
45 .name = "ccount",
46 .rating = 200,
47 .read = ccount_read,
48 .mask = CLOCKSOURCE_MASK(32),
Baruch Siach0fb40402013-10-17 02:42:18 +040049 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Johannes Weinerfcc8f0f2009-03-04 21:39:12 +010050};
51
Baruch Siach925f5532013-06-18 08:48:53 +030052static int ccount_timer_set_next_event(unsigned long delta,
53 struct clock_event_device *dev);
54static void ccount_timer_set_mode(enum clock_event_mode mode,
55 struct clock_event_device *evt);
Max Filippov62351532013-10-17 02:42:19 +040056struct ccount_timer {
Baruch Siach925f5532013-06-18 08:48:53 +030057 struct clock_event_device evt;
58 int irq_enabled;
Max Filippov62351532013-10-17 02:42:19 +040059 char name[24];
Baruch Siach925f5532013-06-18 08:48:53 +030060};
Max Filippov62351532013-10-17 02:42:19 +040061static DEFINE_PER_CPU(struct ccount_timer, ccount_timer);
Baruch Siach925f5532013-06-18 08:48:53 +030062
63static int ccount_timer_set_next_event(unsigned long delta,
64 struct clock_event_device *dev)
65{
66 unsigned long flags, next;
67 int ret = 0;
68
69 local_irq_save(flags);
70 next = get_ccount() + delta;
71 set_linux_timer(next);
72 if (next - get_ccount() > delta)
73 ret = -ETIME;
74 local_irq_restore(flags);
75
76 return ret;
77}
78
79static void ccount_timer_set_mode(enum clock_event_mode mode,
80 struct clock_event_device *evt)
81{
Max Filippov62351532013-10-17 02:42:19 +040082 struct ccount_timer *timer =
83 container_of(evt, struct ccount_timer, evt);
Baruch Siach925f5532013-06-18 08:48:53 +030084
85 /*
86 * There is no way to disable the timer interrupt at the device level,
87 * only at the intenable register itself. Since enable_irq/disable_irq
88 * calls are nested, we need to make sure that these calls are
89 * balanced.
90 */
91 switch (mode) {
92 case CLOCK_EVT_MODE_SHUTDOWN:
93 case CLOCK_EVT_MODE_UNUSED:
94 if (timer->irq_enabled) {
95 disable_irq(evt->irq);
96 timer->irq_enabled = 0;
97 }
98 break;
99 case CLOCK_EVT_MODE_RESUME:
100 case CLOCK_EVT_MODE_ONESHOT:
101 if (!timer->irq_enabled) {
102 enable_irq(evt->irq);
103 timer->irq_enabled = 1;
104 }
105 default:
106 break;
107 }
108}
109
Chris Zankelfd43fe12006-12-10 02:18:47 -0800110static irqreturn_t timer_interrupt(int irq, void *dev_id);
Chris Zankel5a0015d2005-06-23 22:01:16 -0700111static struct irqaction timer_irqaction = {
112 .handler = timer_interrupt,
Baruch Siach925f5532013-06-18 08:48:53 +0300113 .flags = IRQF_TIMER,
Chris Zankel5a0015d2005-06-23 22:01:16 -0700114 .name = "timer",
115};
116
Max Filippov62351532013-10-17 02:42:19 +0400117void local_timer_setup(unsigned cpu)
118{
119 struct ccount_timer *timer = &per_cpu(ccount_timer, cpu);
120 struct clock_event_device *clockevent = &timer->evt;
121
122 timer->irq_enabled = 1;
123 clockevent->name = timer->name;
124 snprintf(timer->name, sizeof(timer->name), "ccount_clockevent_%u", cpu);
125 clockevent->features = CLOCK_EVT_FEAT_ONESHOT;
126 clockevent->rating = 300;
127 clockevent->set_next_event = ccount_timer_set_next_event;
128 clockevent->set_mode = ccount_timer_set_mode;
129 clockevent->cpumask = cpumask_of(cpu);
130 clockevent->irq = irq_create_mapping(NULL, LINUX_TIMER_INT);
131 if (WARN(!clockevent->irq, "error: can't map timer irq"))
132 return;
133 clockevents_config_and_register(clockevent, ccount_freq,
134 0xf, 0xffffffff);
135}
136
Chris Zankel5a0015d2005-06-23 22:01:16 -0700137void __init time_init(void)
138{
Chris Zankel288a60c2005-09-22 21:44:23 -0700139#ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
Chris Zankel5a0015d2005-06-23 22:01:16 -0700140 printk("Calibrating CPU frequency ");
141 platform_calibrate_ccount();
Baruch Siache504c4b2013-06-17 11:29:43 +0300142 printk("%d.%02d MHz\n", (int)ccount_freq/1000000,
143 (int)(ccount_freq/10000)%100);
Baruch Siachfedc21d2013-07-15 07:03:38 +0300144#else
145 ccount_freq = CONFIG_XTENSA_CPU_CLOCK*1000000UL;
Chris Zankel5a0015d2005-06-23 22:01:16 -0700146#endif
Baruch Siach8d5e1d82013-07-15 08:24:22 +0300147 clocksource_register_hz(&ccount_clocksource, ccount_freq);
Max Filippov62351532013-10-17 02:42:19 +0400148 local_timer_setup(0);
149 setup_irq(this_cpu_ptr(&ccount_timer)->evt.irq, &timer_irqaction);
Stephen Boyd3ade4f82013-12-13 01:43:58 -0800150 sched_clock_register(ccount_sched_clock_read, 32, ccount_freq);
Baruch Siachb087ab72013-12-23 20:49:56 +0200151 clocksource_of_init();
Chris Zankel5a0015d2005-06-23 22:01:16 -0700152}
153
Chris Zankel5a0015d2005-06-23 22:01:16 -0700154/*
155 * The timer interrupt is called HZ times per second.
156 */
157
Max Filippov62351532013-10-17 02:42:19 +0400158irqreturn_t timer_interrupt(int irq, void *dev_id)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700159{
Max Filippov62351532013-10-17 02:42:19 +0400160 struct clock_event_device *evt = &this_cpu_ptr(&ccount_timer)->evt;
Chris Zankel5a0015d2005-06-23 22:01:16 -0700161
Max Filippovbae07f82013-10-17 02:42:24 +0400162 set_linux_timer(get_linux_timer());
Baruch Siach925f5532013-06-18 08:48:53 +0300163 evt->event_handler(evt);
Chris Zankel5a0015d2005-06-23 22:01:16 -0700164
Chris Zankel2b8aea72007-08-05 10:26:30 -0700165 /* Allow platform to do something useful (Wdog). */
Chris Zankel2b8aea72007-08-05 10:26:30 -0700166 platform_heartbeat();
Chris Zankel5a0015d2005-06-23 22:01:16 -0700167
Chris Zankel5a0015d2005-06-23 22:01:16 -0700168 return IRQ_HANDLED;
169}
170
171#ifndef CONFIG_GENERIC_CALIBRATE_DELAY
Paul Gortmaker6cb4c152013-06-18 17:54:49 -0400172void calibrate_delay(void)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700173{
Baruch Siach8d5e1d82013-07-15 08:24:22 +0300174 loops_per_jiffy = ccount_freq / HZ;
Chris Zankel5a0015d2005-06-23 22:01:16 -0700175 printk("Calibrating delay loop (skipped)... "
176 "%lu.%02lu BogoMIPS preset\n",
177 loops_per_jiffy/(1000000/HZ),
178 (loops_per_jiffy/(10000/HZ)) % 100);
179}
180#endif