blob: b39916ad31c2f8f8ff57ac675b32ec40346a58bc [file] [log] [blame]
Russell Kingf32f4ce2009-05-16 12:14:21 +01001/*
2 * linux/arch/arm/kernel/smp_twd.c
3 *
4 * Copyright (C) 2002 ARM Ltd.
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/init.h>
12#include <linux/kernel.h>
Linus Walleij5def51b2011-12-13 12:47:31 +010013#include <linux/clk.h>
Linus Walleij4fd7f9b2011-12-13 12:48:18 +010014#include <linux/cpufreq.h>
Russell Kingf32f4ce2009-05-16 12:14:21 +010015#include <linux/delay.h>
16#include <linux/device.h>
Linus Walleij5def51b2011-12-13 12:47:31 +010017#include <linux/err.h>
Russell Kingf32f4ce2009-05-16 12:14:21 +010018#include <linux/smp.h>
19#include <linux/jiffies.h>
20#include <linux/clockchips.h>
21#include <linux/irq.h>
22#include <linux/io.h>
23
24#include <asm/smp_twd.h>
Marc Zyngier28af6902011-07-22 12:52:37 +010025#include <asm/localtimer.h>
Russell Kingf32f4ce2009-05-16 12:14:21 +010026#include <asm/hardware/gic.h>
27
Russell Kingf32f4ce2009-05-16 12:14:21 +010028/* set up by the platform code */
29void __iomem *twd_base;
30
Linus Walleij5def51b2011-12-13 12:47:31 +010031static struct clk *twd_clk;
Russell Kingf32f4ce2009-05-16 12:14:21 +010032static unsigned long twd_timer_rate;
33
Marc Zyngier28af6902011-07-22 12:52:37 +010034static struct clock_event_device __percpu **twd_evt;
35
Russell Kingf32f4ce2009-05-16 12:14:21 +010036static void twd_set_mode(enum clock_event_mode mode,
37 struct clock_event_device *clk)
38{
39 unsigned long ctrl;
40
Russell King4c5158d2009-05-17 10:58:54 +010041 switch (mode) {
Russell Kingf32f4ce2009-05-16 12:14:21 +010042 case CLOCK_EVT_MODE_PERIODIC:
43 /* timer load already set up */
44 ctrl = TWD_TIMER_CONTROL_ENABLE | TWD_TIMER_CONTROL_IT_ENABLE
45 | TWD_TIMER_CONTROL_PERIODIC;
Russell King03399c12011-01-25 10:35:36 +000046 __raw_writel(twd_timer_rate / HZ, twd_base + TWD_TIMER_LOAD);
Russell Kingf32f4ce2009-05-16 12:14:21 +010047 break;
48 case CLOCK_EVT_MODE_ONESHOT:
49 /* period set, and timer enabled in 'next_event' hook */
50 ctrl = TWD_TIMER_CONTROL_IT_ENABLE | TWD_TIMER_CONTROL_ONESHOT;
51 break;
52 case CLOCK_EVT_MODE_UNUSED:
53 case CLOCK_EVT_MODE_SHUTDOWN:
54 default:
55 ctrl = 0;
56 }
57
58 __raw_writel(ctrl, twd_base + TWD_TIMER_CONTROL);
59}
60
61static int twd_set_next_event(unsigned long evt,
62 struct clock_event_device *unused)
63{
64 unsigned long ctrl = __raw_readl(twd_base + TWD_TIMER_CONTROL);
65
Russell King4c5158d2009-05-17 10:58:54 +010066 ctrl |= TWD_TIMER_CONTROL_ENABLE;
67
Russell Kingf32f4ce2009-05-16 12:14:21 +010068 __raw_writel(evt, twd_base + TWD_TIMER_COUNTER);
Russell King4c5158d2009-05-17 10:58:54 +010069 __raw_writel(ctrl, twd_base + TWD_TIMER_CONTROL);
Russell Kingf32f4ce2009-05-16 12:14:21 +010070
71 return 0;
72}
73
74/*
75 * local_timer_ack: checks for a local timer interrupt.
76 *
77 * If a local timer interrupt has occurred, acknowledge and return 1.
78 * Otherwise, return 0.
79 */
80int twd_timer_ack(void)
81{
82 if (__raw_readl(twd_base + TWD_TIMER_INTSTAT)) {
83 __raw_writel(1, twd_base + TWD_TIMER_INTSTAT);
84 return 1;
85 }
86
87 return 0;
88}
89
Marc Zyngierabde7102012-01-10 19:07:28 +000090static void twd_timer_stop(struct clock_event_device *clk)
Marc Zyngier28af6902011-07-22 12:52:37 +010091{
92 twd_set_mode(CLOCK_EVT_MODE_UNUSED, clk);
93 disable_percpu_irq(clk->irq);
94}
95
Marc Zyngierabde7102012-01-10 19:07:28 +000096/* Temporary hack to be removed when all TWD users are converted to
97 the new registration interface */
98void local_timer_stop(struct clock_event_device *clk)
99 __attribute__ ((alias ("twd_timer_stop")));
100
Linus Walleij4fd7f9b2011-12-13 12:48:18 +0100101#ifdef CONFIG_CPU_FREQ
102
103/*
104 * Updates clockevent frequency when the cpu frequency changes.
105 * Called on the cpu that is changing frequency with interrupts disabled.
106 */
107static void twd_update_frequency(void *data)
108{
109 twd_timer_rate = clk_get_rate(twd_clk);
110
111 clockevents_update_freq(*__this_cpu_ptr(twd_evt), twd_timer_rate);
112}
113
114static int twd_cpufreq_transition(struct notifier_block *nb,
115 unsigned long state, void *data)
116{
117 struct cpufreq_freqs *freqs = data;
118
119 /*
120 * The twd clock events must be reprogrammed to account for the new
121 * frequency. The timer is local to a cpu, so cross-call to the
122 * changing cpu.
123 */
124 if (state == CPUFREQ_POSTCHANGE || state == CPUFREQ_RESUMECHANGE)
125 smp_call_function_single(freqs->cpu, twd_update_frequency,
126 NULL, 1);
127
128 return NOTIFY_OK;
129}
130
131static struct notifier_block twd_cpufreq_nb = {
132 .notifier_call = twd_cpufreq_transition,
133};
134
135static int twd_cpufreq_init(void)
136{
137 if (!IS_ERR(twd_clk))
138 return cpufreq_register_notifier(&twd_cpufreq_nb,
139 CPUFREQ_TRANSITION_NOTIFIER);
140
141 return 0;
142}
143core_initcall(twd_cpufreq_init);
144
145#endif
146
Russell Kingf32f4ce2009-05-16 12:14:21 +0100147static void __cpuinit twd_calibrate_rate(void)
148{
Russell King03399c12011-01-25 10:35:36 +0000149 unsigned long count;
Russell Kingf32f4ce2009-05-16 12:14:21 +0100150 u64 waitjiffies;
151
152 /*
153 * If this is the first time round, we need to work out how fast
154 * the timer ticks
155 */
156 if (twd_timer_rate == 0) {
Russell King4c5158d2009-05-17 10:58:54 +0100157 printk(KERN_INFO "Calibrating local timer... ");
Russell Kingf32f4ce2009-05-16 12:14:21 +0100158
159 /* Wait for a tick to start */
160 waitjiffies = get_jiffies_64() + 1;
161
162 while (get_jiffies_64() < waitjiffies)
163 udelay(10);
164
165 /* OK, now the tick has started, let's get the timer going */
166 waitjiffies += 5;
167
168 /* enable, no interrupt or reload */
169 __raw_writel(0x1, twd_base + TWD_TIMER_CONTROL);
170
171 /* maximum value */
172 __raw_writel(0xFFFFFFFFU, twd_base + TWD_TIMER_COUNTER);
173
174 while (get_jiffies_64() < waitjiffies)
175 udelay(10);
176
177 count = __raw_readl(twd_base + TWD_TIMER_COUNTER);
178
179 twd_timer_rate = (0xFFFFFFFFU - count) * (HZ / 5);
180
181 printk("%lu.%02luMHz.\n", twd_timer_rate / 1000000,
Vitaly Kuzmichev90c5ffe2011-07-07 14:56:05 +0100182 (twd_timer_rate / 10000) % 100);
Russell Kingf32f4ce2009-05-16 12:14:21 +0100183 }
Russell Kingf32f4ce2009-05-16 12:14:21 +0100184}
185
Marc Zyngier28af6902011-07-22 12:52:37 +0100186static irqreturn_t twd_handler(int irq, void *dev_id)
187{
188 struct clock_event_device *evt = *(struct clock_event_device **)dev_id;
189
190 if (twd_timer_ack()) {
191 evt->event_handler(evt);
192 return IRQ_HANDLED;
193 }
194
195 return IRQ_NONE;
196}
197
Linus Walleij5def51b2011-12-13 12:47:31 +0100198static struct clk *twd_get_clock(void)
199{
200 struct clk *clk;
201 int err;
202
203 clk = clk_get_sys("smp_twd", NULL);
204 if (IS_ERR(clk)) {
205 pr_err("smp_twd: clock not found: %d\n", (int)PTR_ERR(clk));
206 return clk;
207 }
208
209 err = clk_prepare(clk);
210 if (err) {
211 pr_err("smp_twd: clock failed to prepare: %d\n", err);
212 clk_put(clk);
213 return ERR_PTR(err);
214 }
215
216 err = clk_enable(clk);
217 if (err) {
218 pr_err("smp_twd: clock failed to enable: %d\n", err);
219 clk_unprepare(clk);
220 clk_put(clk);
221 return ERR_PTR(err);
222 }
223
224 return clk;
225}
226
Russell Kingf32f4ce2009-05-16 12:14:21 +0100227/*
228 * Setup the local clock events for a CPU.
229 */
230void __cpuinit twd_timer_setup(struct clock_event_device *clk)
231{
Marc Zyngier28af6902011-07-22 12:52:37 +0100232 struct clock_event_device **this_cpu_clk;
233
234 if (!twd_evt) {
235 int err;
236
237 twd_evt = alloc_percpu(struct clock_event_device *);
238 if (!twd_evt) {
239 pr_err("twd: can't allocate memory\n");
240 return;
241 }
242
243 err = request_percpu_irq(clk->irq, twd_handler,
244 "twd", twd_evt);
245 if (err) {
246 pr_err("twd: can't register interrupt %d (%d)\n",
247 clk->irq, err);
248 return;
249 }
250 }
251
Linus Walleij5def51b2011-12-13 12:47:31 +0100252 if (!twd_clk)
253 twd_clk = twd_get_clock();
254
255 if (!IS_ERR_OR_NULL(twd_clk))
256 twd_timer_rate = clk_get_rate(twd_clk);
257 else
258 twd_calibrate_rate();
Russell Kingf32f4ce2009-05-16 12:14:21 +0100259
Marc Zyngierc2144552012-01-20 12:24:47 +0100260 __raw_writel(0, twd_base + TWD_TIMER_CONTROL);
261
Russell King4c5158d2009-05-17 10:58:54 +0100262 clk->name = "local_timer";
Russell King5388a6b2010-07-26 13:19:43 +0100263 clk->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT |
264 CLOCK_EVT_FEAT_C3STOP;
Russell King4c5158d2009-05-17 10:58:54 +0100265 clk->rating = 350;
266 clk->set_mode = twd_set_mode;
267 clk->set_next_event = twd_set_next_event;
Russell Kingf32f4ce2009-05-16 12:14:21 +0100268
Marc Zyngier28af6902011-07-22 12:52:37 +0100269 this_cpu_clk = __this_cpu_ptr(twd_evt);
270 *this_cpu_clk = clk;
271
Linus Walleij54d15b12011-12-13 12:46:43 +0100272 clockevents_config_and_register(clk, twd_timer_rate,
273 0xf, 0xffffffff);
Marc Zyngier28af6902011-07-22 12:52:37 +0100274 enable_percpu_irq(clk->irq, 0);
Russell Kingf32f4ce2009-05-16 12:14:21 +0100275}