1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * arch/xtensa/kernel/time.c
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Timer and clock support.
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * This file is subject to the terms and conditions of the GNU General Public
7*4882a593Smuzhiyun * License. See the file "COPYING" in the main directory of this archive
8*4882a593Smuzhiyun * for more details.
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * Copyright (C) 2005 Tensilica Inc.
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * Chris Zankel <chris@zankel.net>
13*4882a593Smuzhiyun */
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun #include <linux/clk.h>
16*4882a593Smuzhiyun #include <linux/clk-provider.h>
17*4882a593Smuzhiyun #include <linux/errno.h>
18*4882a593Smuzhiyun #include <linux/sched.h>
19*4882a593Smuzhiyun #include <linux/time.h>
20*4882a593Smuzhiyun #include <linux/clocksource.h>
21*4882a593Smuzhiyun #include <linux/clockchips.h>
22*4882a593Smuzhiyun #include <linux/interrupt.h>
23*4882a593Smuzhiyun #include <linux/module.h>
24*4882a593Smuzhiyun #include <linux/init.h>
25*4882a593Smuzhiyun #include <linux/irq.h>
26*4882a593Smuzhiyun #include <linux/profile.h>
27*4882a593Smuzhiyun #include <linux/delay.h>
28*4882a593Smuzhiyun #include <linux/irqdomain.h>
29*4882a593Smuzhiyun #include <linux/sched_clock.h>
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #include <asm/timex.h>
32*4882a593Smuzhiyun #include <asm/platform.h>
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun unsigned long ccount_freq; /* ccount Hz */
35*4882a593Smuzhiyun EXPORT_SYMBOL(ccount_freq);
36*4882a593Smuzhiyun
ccount_read(struct clocksource * cs)37*4882a593Smuzhiyun static u64 ccount_read(struct clocksource *cs)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun return (u64)get_ccount();
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun
ccount_sched_clock_read(void)42*4882a593Smuzhiyun static u64 notrace ccount_sched_clock_read(void)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun return get_ccount();
45*4882a593Smuzhiyun }
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun static struct clocksource ccount_clocksource = {
48*4882a593Smuzhiyun .name = "ccount",
49*4882a593Smuzhiyun .rating = 200,
50*4882a593Smuzhiyun .read = ccount_read,
51*4882a593Smuzhiyun .mask = CLOCKSOURCE_MASK(32),
52*4882a593Smuzhiyun .flags = CLOCK_SOURCE_IS_CONTINUOUS,
53*4882a593Smuzhiyun };
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun struct ccount_timer {
56*4882a593Smuzhiyun struct clock_event_device evt;
57*4882a593Smuzhiyun int irq_enabled;
58*4882a593Smuzhiyun char name[24];
59*4882a593Smuzhiyun };
60*4882a593Smuzhiyun
ccount_timer_set_next_event(unsigned long delta,struct clock_event_device * dev)61*4882a593Smuzhiyun static int ccount_timer_set_next_event(unsigned long delta,
62*4882a593Smuzhiyun struct clock_event_device *dev)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun unsigned long flags, next;
65*4882a593Smuzhiyun int ret = 0;
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun local_irq_save(flags);
68*4882a593Smuzhiyun next = get_ccount() + delta;
69*4882a593Smuzhiyun set_linux_timer(next);
70*4882a593Smuzhiyun if (next - get_ccount() > delta)
71*4882a593Smuzhiyun ret = -ETIME;
72*4882a593Smuzhiyun local_irq_restore(flags);
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun return ret;
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun /*
78*4882a593Smuzhiyun * There is no way to disable the timer interrupt at the device level,
79*4882a593Smuzhiyun * only at the intenable register itself. Since enable_irq/disable_irq
80*4882a593Smuzhiyun * calls are nested, we need to make sure that these calls are
81*4882a593Smuzhiyun * balanced.
82*4882a593Smuzhiyun */
ccount_timer_shutdown(struct clock_event_device * evt)83*4882a593Smuzhiyun static int ccount_timer_shutdown(struct clock_event_device *evt)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun struct ccount_timer *timer =
86*4882a593Smuzhiyun container_of(evt, struct ccount_timer, evt);
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun if (timer->irq_enabled) {
89*4882a593Smuzhiyun disable_irq_nosync(evt->irq);
90*4882a593Smuzhiyun timer->irq_enabled = 0;
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun return 0;
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun
ccount_timer_set_oneshot(struct clock_event_device * evt)95*4882a593Smuzhiyun static int ccount_timer_set_oneshot(struct clock_event_device *evt)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun struct ccount_timer *timer =
98*4882a593Smuzhiyun container_of(evt, struct ccount_timer, evt);
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun if (!timer->irq_enabled) {
101*4882a593Smuzhiyun enable_irq(evt->irq);
102*4882a593Smuzhiyun timer->irq_enabled = 1;
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun return 0;
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun static DEFINE_PER_CPU(struct ccount_timer, ccount_timer) = {
108*4882a593Smuzhiyun .evt = {
109*4882a593Smuzhiyun .features = CLOCK_EVT_FEAT_ONESHOT,
110*4882a593Smuzhiyun .rating = 300,
111*4882a593Smuzhiyun .set_next_event = ccount_timer_set_next_event,
112*4882a593Smuzhiyun .set_state_shutdown = ccount_timer_shutdown,
113*4882a593Smuzhiyun .set_state_oneshot = ccount_timer_set_oneshot,
114*4882a593Smuzhiyun .tick_resume = ccount_timer_set_oneshot,
115*4882a593Smuzhiyun },
116*4882a593Smuzhiyun };
117*4882a593Smuzhiyun
timer_interrupt(int irq,void * dev_id)118*4882a593Smuzhiyun static irqreturn_t timer_interrupt(int irq, void *dev_id)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun struct clock_event_device *evt = &this_cpu_ptr(&ccount_timer)->evt;
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun set_linux_timer(get_linux_timer());
123*4882a593Smuzhiyun evt->event_handler(evt);
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun /* Allow platform to do something useful (Wdog). */
126*4882a593Smuzhiyun platform_heartbeat();
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun return IRQ_HANDLED;
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun
local_timer_setup(unsigned cpu)131*4882a593Smuzhiyun void local_timer_setup(unsigned cpu)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun struct ccount_timer *timer = &per_cpu(ccount_timer, cpu);
134*4882a593Smuzhiyun struct clock_event_device *clockevent = &timer->evt;
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun timer->irq_enabled = 1;
137*4882a593Smuzhiyun snprintf(timer->name, sizeof(timer->name), "ccount_clockevent_%u", cpu);
138*4882a593Smuzhiyun clockevent->name = timer->name;
139*4882a593Smuzhiyun clockevent->cpumask = cpumask_of(cpu);
140*4882a593Smuzhiyun clockevent->irq = irq_create_mapping(NULL, LINUX_TIMER_INT);
141*4882a593Smuzhiyun if (WARN(!clockevent->irq, "error: can't map timer irq"))
142*4882a593Smuzhiyun return;
143*4882a593Smuzhiyun clockevents_config_and_register(clockevent, ccount_freq,
144*4882a593Smuzhiyun 0xf, 0xffffffff);
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
148*4882a593Smuzhiyun #ifdef CONFIG_OF
calibrate_ccount(void)149*4882a593Smuzhiyun static void __init calibrate_ccount(void)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun struct device_node *cpu;
152*4882a593Smuzhiyun struct clk *clk;
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun cpu = of_find_compatible_node(NULL, NULL, "cdns,xtensa-cpu");
155*4882a593Smuzhiyun if (cpu) {
156*4882a593Smuzhiyun clk = of_clk_get(cpu, 0);
157*4882a593Smuzhiyun of_node_put(cpu);
158*4882a593Smuzhiyun if (!IS_ERR(clk)) {
159*4882a593Smuzhiyun ccount_freq = clk_get_rate(clk);
160*4882a593Smuzhiyun return;
161*4882a593Smuzhiyun } else {
162*4882a593Smuzhiyun pr_warn("%s: CPU input clock not found\n",
163*4882a593Smuzhiyun __func__);
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun } else {
166*4882a593Smuzhiyun pr_warn("%s: CPU node not found in the device tree\n",
167*4882a593Smuzhiyun __func__);
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun platform_calibrate_ccount();
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun #else
calibrate_ccount(void)173*4882a593Smuzhiyun static inline void calibrate_ccount(void)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun platform_calibrate_ccount();
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun #endif
178*4882a593Smuzhiyun #endif
179*4882a593Smuzhiyun
time_init(void)180*4882a593Smuzhiyun void __init time_init(void)
181*4882a593Smuzhiyun {
182*4882a593Smuzhiyun int irq;
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun of_clk_init(NULL);
185*4882a593Smuzhiyun #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
186*4882a593Smuzhiyun pr_info("Calibrating CPU frequency ");
187*4882a593Smuzhiyun calibrate_ccount();
188*4882a593Smuzhiyun pr_cont("%d.%02d MHz\n",
189*4882a593Smuzhiyun (int)ccount_freq / 1000000,
190*4882a593Smuzhiyun (int)(ccount_freq / 10000) % 100);
191*4882a593Smuzhiyun #else
192*4882a593Smuzhiyun ccount_freq = CONFIG_XTENSA_CPU_CLOCK*1000000UL;
193*4882a593Smuzhiyun #endif
194*4882a593Smuzhiyun WARN(!ccount_freq,
195*4882a593Smuzhiyun "%s: CPU clock frequency is not set up correctly\n",
196*4882a593Smuzhiyun __func__);
197*4882a593Smuzhiyun clocksource_register_hz(&ccount_clocksource, ccount_freq);
198*4882a593Smuzhiyun local_timer_setup(0);
199*4882a593Smuzhiyun irq = this_cpu_ptr(&ccount_timer)->evt.irq;
200*4882a593Smuzhiyun if (request_irq(irq, timer_interrupt, IRQF_TIMER, "timer", NULL))
201*4882a593Smuzhiyun pr_err("Failed to request irq %d (timer)\n", irq);
202*4882a593Smuzhiyun sched_clock_register(ccount_sched_clock_read, 32, ccount_freq);
203*4882a593Smuzhiyun timer_probe();
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun #ifndef CONFIG_GENERIC_CALIBRATE_DELAY
calibrate_delay(void)207*4882a593Smuzhiyun void calibrate_delay(void)
208*4882a593Smuzhiyun {
209*4882a593Smuzhiyun loops_per_jiffy = ccount_freq / HZ;
210*4882a593Smuzhiyun pr_info("Calibrating delay loop (skipped)... %lu.%02lu BogoMIPS preset\n",
211*4882a593Smuzhiyun loops_per_jiffy / (1000000 / HZ),
212*4882a593Smuzhiyun (loops_per_jiffy / (10000 / HZ)) % 100);
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun #endif
215