aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'arch/arm/kernel/smp_twd.c')
-rw-r--r--arch/arm/kernel/smp_twd.c140
1 files changed, 132 insertions, 8 deletions
diff --git a/arch/arm/kernel/smp_twd.c b/arch/arm/kernel/smp_twd.c
index 01c186222f3b..c8e938553d47 100644
--- a/arch/arm/kernel/smp_twd.c
+++ b/arch/arm/kernel/smp_twd.c
@@ -10,8 +10,11 @@
10 */ 10 */
11#include <linux/init.h> 11#include <linux/init.h>
12#include <linux/kernel.h> 12#include <linux/kernel.h>
13#include <linux/clk.h>
14#include <linux/cpufreq.h>
13#include <linux/delay.h> 15#include <linux/delay.h>
14#include <linux/device.h> 16#include <linux/device.h>
17#include <linux/err.h>
15#include <linux/smp.h> 18#include <linux/smp.h>
16#include <linux/jiffies.h> 19#include <linux/jiffies.h>
17#include <linux/clockchips.h> 20#include <linux/clockchips.h>
@@ -19,13 +22,17 @@
19#include <linux/io.h> 22#include <linux/io.h>
20 23
21#include <asm/smp_twd.h> 24#include <asm/smp_twd.h>
25#include <asm/localtimer.h>
22#include <asm/hardware/gic.h> 26#include <asm/hardware/gic.h>
23 27
24/* set up by the platform code */ 28/* set up by the platform code */
25void __iomem *twd_base; 29void __iomem *twd_base;
26 30
31static struct clk *twd_clk;
27static unsigned long twd_timer_rate; 32static unsigned long twd_timer_rate;
28 33
34static struct clock_event_device __percpu **twd_evt;
35
29static void twd_set_mode(enum clock_event_mode mode, 36static void twd_set_mode(enum clock_event_mode mode,
30 struct clock_event_device *clk) 37 struct clock_event_device *clk)
31{ 38{
@@ -80,6 +87,58 @@ int twd_timer_ack(void)
80 return 0; 87 return 0;
81} 88}
82 89
90void twd_timer_stop(struct clock_event_device *clk)
91{
92 twd_set_mode(CLOCK_EVT_MODE_UNUSED, clk);
93 disable_percpu_irq(clk->irq);
94}
95
96#ifdef CONFIG_CPU_FREQ
97
98/*
99 * Updates clockevent frequency when the cpu frequency changes.
100 * Called on the cpu that is changing frequency with interrupts disabled.
101 */
102static void twd_update_frequency(void *data)
103{
104 twd_timer_rate = clk_get_rate(twd_clk);
105
106 clockevents_update_freq(*__this_cpu_ptr(twd_evt), twd_timer_rate);
107}
108
109static int twd_cpufreq_transition(struct notifier_block *nb,
110 unsigned long state, void *data)
111{
112 struct cpufreq_freqs *freqs = data;
113
114 /*
115 * The twd clock events must be reprogrammed to account for the new
116 * frequency. The timer is local to a cpu, so cross-call to the
117 * changing cpu.
118 */
119 if (state == CPUFREQ_POSTCHANGE || state == CPUFREQ_RESUMECHANGE)
120 smp_call_function_single(freqs->cpu, twd_update_frequency,
121 NULL, 1);
122
123 return NOTIFY_OK;
124}
125
126static struct notifier_block twd_cpufreq_nb = {
127 .notifier_call = twd_cpufreq_transition,
128};
129
130static int twd_cpufreq_init(void)
131{
132 if (!IS_ERR(twd_clk))
133 return cpufreq_register_notifier(&twd_cpufreq_nb,
134 CPUFREQ_TRANSITION_NOTIFIER);
135
136 return 0;
137}
138core_initcall(twd_cpufreq_init);
139
140#endif
141
83static void __cpuinit twd_calibrate_rate(void) 142static void __cpuinit twd_calibrate_rate(void)
84{ 143{
85 unsigned long count; 144 unsigned long count;
@@ -119,12 +178,79 @@ static void __cpuinit twd_calibrate_rate(void)
119 } 178 }
120} 179}
121 180
181static irqreturn_t twd_handler(int irq, void *dev_id)
182{
183 struct clock_event_device *evt = *(struct clock_event_device **)dev_id;
184
185 if (twd_timer_ack()) {
186 evt->event_handler(evt);
187 return IRQ_HANDLED;
188 }
189
190 return IRQ_NONE;
191}
192
193static struct clk *twd_get_clock(void)
194{
195 struct clk *clk;
196 int err;
197
198 clk = clk_get_sys("smp_twd", NULL);
199 if (IS_ERR(clk)) {
200 pr_err("smp_twd: clock not found: %d\n", (int)PTR_ERR(clk));
201 return clk;
202 }
203
204 err = clk_prepare(clk);
205 if (err) {
206 pr_err("smp_twd: clock failed to prepare: %d\n", err);
207 clk_put(clk);
208 return ERR_PTR(err);
209 }
210
211 err = clk_enable(clk);
212 if (err) {
213 pr_err("smp_twd: clock failed to enable: %d\n", err);
214 clk_unprepare(clk);
215 clk_put(clk);
216 return ERR_PTR(err);
217 }
218
219 return clk;
220}
221
122/* 222/*
123 * Setup the local clock events for a CPU. 223 * Setup the local clock events for a CPU.
124 */ 224 */
125void __cpuinit twd_timer_setup(struct clock_event_device *clk) 225void __cpuinit twd_timer_setup(struct clock_event_device *clk)
126{ 226{
127 twd_calibrate_rate(); 227 struct clock_event_device **this_cpu_clk;
228
229 if (!twd_evt) {
230 int err;
231
232 twd_evt = alloc_percpu(struct clock_event_device *);
233 if (!twd_evt) {
234 pr_err("twd: can't allocate memory\n");
235 return;
236 }
237
238 err = request_percpu_irq(clk->irq, twd_handler,
239 "twd", twd_evt);
240 if (err) {
241 pr_err("twd: can't register interrupt %d (%d)\n",
242 clk->irq, err);
243 return;
244 }
245 }
246
247 if (!twd_clk)
248 twd_clk = twd_get_clock();
249
250 if (!IS_ERR_OR_NULL(twd_clk))
251 twd_timer_rate = clk_get_rate(twd_clk);
252 else
253 twd_calibrate_rate();
128 254
129 clk->name = "local_timer"; 255 clk->name = "local_timer";
130 clk->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT | 256 clk->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT |
@@ -132,13 +258,11 @@ void __cpuinit twd_timer_setup(struct clock_event_device *clk)
132 clk->rating = 350; 258 clk->rating = 350;
133 clk->set_mode = twd_set_mode; 259 clk->set_mode = twd_set_mode;
134 clk->set_next_event = twd_set_next_event; 260 clk->set_next_event = twd_set_next_event;
135 clk->shift = 20;
136 clk->mult = div_sc(twd_timer_rate, NSEC_PER_SEC, clk->shift);
137 clk->max_delta_ns = clockevent_delta2ns(0xffffffff, clk);
138 clk->min_delta_ns = clockevent_delta2ns(0xf, clk);
139 261
140 clockevents_register_device(clk); 262 this_cpu_clk = __this_cpu_ptr(twd_evt);
263 *this_cpu_clk = clk;
141 264
142 /* Make sure our local interrupt controller has this enabled */ 265 clockevents_config_and_register(clk, twd_timer_rate,
143 gic_enable_ppi(clk->irq); 266 0xf, 0xffffffff);
267 enable_percpu_irq(clk->irq, 0);
144} 268}