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/clk.h>
16 #include <linux/clk-provider.h>
17 #include <linux/errno.h>
18 #include <linux/sched.h>
19 #include <linux/time.h>
20 #include <linux/clocksource.h>
21 #include <linux/clockchips.h>
22 #include <linux/interrupt.h>
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/irq.h>
26 #include <linux/profile.h>
27 #include <linux/delay.h>
28 #include <linux/irqdomain.h>
29 #include <linux/sched_clock.h>
31 #include <asm/timex.h>
32 #include <asm/platform.h>
34 unsigned long ccount_freq
; /* ccount Hz */
35 EXPORT_SYMBOL(ccount_freq
);
37 static u64
ccount_read(struct clocksource
*cs
)
39 return (u64
)get_ccount();
42 static u64 notrace
ccount_sched_clock_read(void)
47 static struct clocksource ccount_clocksource
= {
51 .mask
= CLOCKSOURCE_MASK(32),
52 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
55 static int ccount_timer_set_next_event(unsigned long delta
,
56 struct clock_event_device
*dev
);
58 struct clock_event_device evt
;
62 static DEFINE_PER_CPU(struct ccount_timer
, ccount_timer
);
64 static int ccount_timer_set_next_event(unsigned long delta
,
65 struct clock_event_device
*dev
)
67 unsigned long flags
, next
;
70 local_irq_save(flags
);
71 next
= get_ccount() + delta
;
72 set_linux_timer(next
);
73 if (next
- get_ccount() > delta
)
75 local_irq_restore(flags
);
81 * There is no way to disable the timer interrupt at the device level,
82 * only at the intenable register itself. Since enable_irq/disable_irq
83 * calls are nested, we need to make sure that these calls are
86 static int ccount_timer_shutdown(struct clock_event_device
*evt
)
88 struct ccount_timer
*timer
=
89 container_of(evt
, struct ccount_timer
, evt
);
91 if (timer
->irq_enabled
) {
92 disable_irq_nosync(evt
->irq
);
93 timer
->irq_enabled
= 0;
98 static int ccount_timer_set_oneshot(struct clock_event_device
*evt
)
100 struct ccount_timer
*timer
=
101 container_of(evt
, struct ccount_timer
, evt
);
103 if (!timer
->irq_enabled
) {
104 enable_irq(evt
->irq
);
105 timer
->irq_enabled
= 1;
110 static irqreturn_t
timer_interrupt(int irq
, void *dev_id
);
111 static struct irqaction timer_irqaction
= {
112 .handler
= timer_interrupt
,
117 void local_timer_setup(unsigned cpu
)
119 struct ccount_timer
*timer
= &per_cpu(ccount_timer
, cpu
);
120 struct clock_event_device
*clockevent
= &timer
->evt
;
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_state_shutdown
= ccount_timer_shutdown
;
129 clockevent
->set_state_oneshot
= ccount_timer_set_oneshot
;
130 clockevent
->tick_resume
= ccount_timer_set_oneshot
;
131 clockevent
->cpumask
= cpumask_of(cpu
);
132 clockevent
->irq
= irq_create_mapping(NULL
, LINUX_TIMER_INT
);
133 if (WARN(!clockevent
->irq
, "error: can't map timer irq"))
135 clockevents_config_and_register(clockevent
, ccount_freq
,
139 #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
141 static void __init
calibrate_ccount(void)
143 struct device_node
*cpu
;
146 cpu
= of_find_compatible_node(NULL
, NULL
, "cdns,xtensa-cpu");
148 clk
= of_clk_get(cpu
, 0);
150 ccount_freq
= clk_get_rate(clk
);
153 pr_warn("%s: CPU input clock not found\n",
157 pr_warn("%s: CPU node not found in the device tree\n",
161 platform_calibrate_ccount();
164 static inline void calibrate_ccount(void)
166 platform_calibrate_ccount();
171 void __init
time_init(void)
174 #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
175 pr_info("Calibrating CPU frequency ");
177 pr_cont("%d.%02d MHz\n",
178 (int)ccount_freq
/ 1000000,
179 (int)(ccount_freq
/ 10000) % 100);
181 ccount_freq
= CONFIG_XTENSA_CPU_CLOCK
*1000000UL;
184 "%s: CPU clock frequency is not set up correctly\n",
186 clocksource_register_hz(&ccount_clocksource
, ccount_freq
);
187 local_timer_setup(0);
188 setup_irq(this_cpu_ptr(&ccount_timer
)->evt
.irq
, &timer_irqaction
);
189 sched_clock_register(ccount_sched_clock_read
, 32, ccount_freq
);
194 * The timer interrupt is called HZ times per second.
197 irqreturn_t
timer_interrupt(int irq
, void *dev_id
)
199 struct clock_event_device
*evt
= &this_cpu_ptr(&ccount_timer
)->evt
;
201 set_linux_timer(get_linux_timer());
202 evt
->event_handler(evt
);
204 /* Allow platform to do something useful (Wdog). */
205 platform_heartbeat();
210 #ifndef CONFIG_GENERIC_CALIBRATE_DELAY
211 void calibrate_delay(void)
213 loops_per_jiffy
= ccount_freq
/ HZ
;
214 pr_info("Calibrating delay loop (skipped)... %lu.%02lu BogoMIPS preset\n",
215 loops_per_jiffy
/ (1000000 / HZ
),
216 (loops_per_jiffy
/ (10000 / HZ
)) % 100);