blob: 7db146c2a3d30011e704ba04dc1e97547e29512a [file] [log] [blame]
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001/* linux/arch/arm/mach-msm/timer.c
2 *
3 * Copyright (C) 2007 Google, Inc.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004 * Copyright (c) 2009-2011, Code Aurora Forum. All rights reserved.
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08005 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
16
17#include <linux/init.h>
18#include <linux/time.h>
19#include <linux/interrupt.h>
20#include <linux/irq.h>
21#include <linux/clk.h>
22#include <linux/clockchips.h>
23#include <linux/delay.h>
Russell Kingfced80c2008-09-06 12:10:45 +010024#include <linux/io.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070025#include <linux/percpu.h>
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -080026
27#include <asm/mach/time.h>
Stephen Boydebf30dc2011-05-31 16:10:00 -070028#include <asm/hardware/gic.h>
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -070029#include <asm/sched_clock.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010030#include <mach/msm_iomap.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070031#include <mach/irqs.h>
32#include <mach/socinfo.h>
33
34#if defined(CONFIG_MSM_SMD)
35#include "smd_private.h"
36#endif
37#include "timer.h"
38
39enum {
40 MSM_TIMER_DEBUG_SYNC = 1U << 0,
41};
42static int msm_timer_debug_mask;
43module_param_named(debug_mask, msm_timer_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP);
44
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070045#ifdef CONFIG_MSM7X00A_USE_GP_TIMER
46 #define DG_TIMER_RATING 100
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070047#else
48 #define DG_TIMER_RATING 300
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049#endif
50
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070051#define MSM_DGT_SHIFT (5)
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -080052
53#define TIMER_MATCH_VAL 0x0000
54#define TIMER_COUNT_VAL 0x0004
55#define TIMER_ENABLE 0x0008
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -080056#define TIMER_CLEAR 0x000C
Jeff Ohlstein672039f2010-10-05 15:23:57 -070057#define DGT_CLK_CTL 0x0034
58enum {
59 DGT_CLK_CTL_DIV_1 = 0,
60 DGT_CLK_CTL_DIV_2 = 1,
61 DGT_CLK_CTL_DIV_3 = 2,
62 DGT_CLK_CTL_DIV_4 = 3,
63};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070064#define TIMER_ENABLE_EN 1
65#define TIMER_ENABLE_CLR_ON_MATCH_EN 2
66
67#define LOCAL_TIMER 0
68#define GLOBAL_TIMER 1
69
70/*
Jeff Ohlsteine1a7e402011-09-07 12:52:36 -070071 * global_timer_offset is added to the regbase of a timer to force the memory
72 * access to come from the CPU0 region.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070073 */
Jeff Ohlsteine1a7e402011-09-07 12:52:36 -070074static int global_timer_offset;
Jeff Ohlstein7a018322011-09-28 12:44:06 -070075static int msm_global_timer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070076
77#define NR_TIMERS ARRAY_SIZE(msm_clocks)
78
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -080079#define GPT_HZ 32768
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070080#define SCLK_HZ 32768
Jeff Ohlstein672039f2010-10-05 15:23:57 -070081
Jeff Ohlstein94790ec2010-12-02 12:05:12 -080082
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070083static struct msm_clock *clockevent_to_clock(struct clock_event_device *evt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070084static irqreturn_t msm_timer_interrupt(int irq, void *dev_id);
85static cycle_t msm_gpt_read(struct clocksource *cs);
86static cycle_t msm_dgt_read(struct clocksource *cs);
87static void msm_timer_set_mode(enum clock_event_mode mode,
88 struct clock_event_device *evt);
89static int msm_timer_set_next_event(unsigned long cycles,
90 struct clock_event_device *evt);
91
92enum {
93 MSM_CLOCK_FLAGS_UNSTABLE_COUNT = 1U << 0,
94 MSM_CLOCK_FLAGS_ODD_MATCH_WRITE = 1U << 1,
95 MSM_CLOCK_FLAGS_DELAYED_WRITE_POST = 1U << 2,
96};
97
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -080098struct msm_clock {
99 struct clock_event_device clockevent;
100 struct clocksource clocksource;
101 struct irqaction irq;
Brian Swetlandbcc0f6a2008-09-10 14:00:53 -0700102 void __iomem *regbase;
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800103 uint32_t freq;
104 uint32_t shift;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700105 uint32_t flags;
106 uint32_t write_delay;
107 uint32_t rollover_offset;
108 uint32_t index;
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800109};
110
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800111enum {
112 MSM_CLOCK_GPT,
113 MSM_CLOCK_DGT,
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800114};
115
116
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700117struct msm_clock_percpu_data {
118 uint32_t last_set;
119 uint32_t sleep_offset;
120 uint32_t alarm_vtime;
121 uint32_t alarm;
122 uint32_t non_sleep_offset;
123 uint32_t in_sync;
124 cycle_t stopped_tick;
125 int stopped;
126 uint32_t last_sync_gpt;
127 u64 last_sync_jiffies;
128};
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800129
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700130struct msm_timer_sync_data_t {
131 struct msm_clock *clock;
132 uint32_t timeout;
133 int exit_sleep;
134};
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800135
136static struct msm_clock msm_clocks[] = {
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800137 [MSM_CLOCK_GPT] = {
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800138 .clockevent = {
139 .name = "gp_timer",
140 .features = CLOCK_EVT_FEAT_ONESHOT,
141 .shift = 32,
142 .rating = 200,
143 .set_next_event = msm_timer_set_next_event,
144 .set_mode = msm_timer_set_mode,
145 },
146 .clocksource = {
147 .name = "gp_timer",
148 .rating = 200,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700149 .read = msm_gpt_read,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800150 .mask = CLOCKSOURCE_MASK(32),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700151 .shift = 17,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800152 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
153 },
154 .irq = {
155 .name = "gp_timer",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700156 .flags = IRQF_DISABLED | IRQF_TIMER |
157 IRQF_TRIGGER_RISING,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800158 .handler = msm_timer_interrupt,
159 .dev_id = &msm_clocks[0].clockevent,
160 .irq = INT_GP_TIMER_EXP
161 },
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700162 .regbase = MSM_TMR_BASE + 0x4,
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800163 .freq = GPT_HZ,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700164 .index = MSM_CLOCK_GPT,
165 .flags =
Rohit Vaswani2a473b22011-08-16 15:35:34 -0700166#if defined(CONFIG_CPU_V6) || defined(CONFIG_ARCH_MSM7X27A)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700167 MSM_CLOCK_FLAGS_UNSTABLE_COUNT |
168 MSM_CLOCK_FLAGS_ODD_MATCH_WRITE |
169 MSM_CLOCK_FLAGS_DELAYED_WRITE_POST |
170#endif
171 0,
172 .write_delay = 9,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800173 },
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800174 [MSM_CLOCK_DGT] = {
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800175 .clockevent = {
176 .name = "dg_timer",
177 .features = CLOCK_EVT_FEAT_ONESHOT,
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700178 .shift = 32,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700179 .rating = DG_TIMER_RATING,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800180 .set_next_event = msm_timer_set_next_event,
181 .set_mode = msm_timer_set_mode,
182 },
183 .clocksource = {
184 .name = "dg_timer",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700185 .rating = DG_TIMER_RATING,
186 .read = msm_dgt_read,
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700187 .mask = CLOCKSOURCE_MASK(32),
188 .shift = 24,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800189 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
190 },
191 .irq = {
192 .name = "dg_timer",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700193 .flags = IRQF_DISABLED | IRQF_TIMER |
194 IRQF_TRIGGER_RISING,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800195 .handler = msm_timer_interrupt,
196 .dev_id = &msm_clocks[1].clockevent,
197 .irq = INT_DEBUG_TIMER_EXP
198 },
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700199 .regbase = MSM_TMR_BASE + 0x24,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700200 .index = MSM_CLOCK_DGT,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700201 .write_delay = 9,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800202 }
203};
204
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700205static DEFINE_PER_CPU(struct clock_event_device*, local_clock_event);
206
207static DEFINE_PER_CPU(struct msm_clock_percpu_data[NR_TIMERS],
208 msm_clocks_percpu);
209
210static DEFINE_PER_CPU(struct msm_clock *, msm_active_clock);
211
212static irqreturn_t msm_timer_interrupt(int irq, void *dev_id)
213{
214 struct clock_event_device *evt = dev_id;
215 if (smp_processor_id() != 0)
216 evt = __get_cpu_var(local_clock_event);
217 if (evt->event_handler == NULL)
218 return IRQ_HANDLED;
219 evt->event_handler(evt);
220 return IRQ_HANDLED;
221}
222
223static uint32_t msm_read_timer_count(struct msm_clock *clock, int global)
224{
225 uint32_t t1, t2;
226 int loop_count = 0;
227
228 if (global)
229 t1 = __raw_readl(clock->regbase + TIMER_COUNT_VAL +
Jeff Ohlsteine1a7e402011-09-07 12:52:36 -0700230 global_timer_offset);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700231 else
232 t1 = __raw_readl(clock->regbase + TIMER_COUNT_VAL);
233
234 if (!(clock->flags & MSM_CLOCK_FLAGS_UNSTABLE_COUNT))
235 return t1;
236 while (1) {
237 if (global)
238 t2 = __raw_readl(clock->regbase + TIMER_COUNT_VAL +
Jeff Ohlsteine1a7e402011-09-07 12:52:36 -0700239 global_timer_offset);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700240 else
241 t2 = __raw_readl(clock->regbase + TIMER_COUNT_VAL);
242 if (t1 == t2)
243 return t1;
244 if (loop_count++ > 10) {
245 printk(KERN_ERR "msm_read_timer_count timer %s did not"
246 "stabilize %u != %u\n", clock->clockevent.name,
247 t2, t1);
248 return t2;
249 }
250 t1 = t2;
251 }
252}
253
254static cycle_t msm_gpt_read(struct clocksource *cs)
255{
256 struct msm_clock *clock = &msm_clocks[MSM_CLOCK_GPT];
257 struct msm_clock_percpu_data *clock_state =
258 &per_cpu(msm_clocks_percpu, 0)[MSM_CLOCK_GPT];
259
260 if (clock_state->stopped)
261 return clock_state->stopped_tick;
262
263 return msm_read_timer_count(clock, GLOBAL_TIMER) +
264 clock_state->sleep_offset;
265}
266
267static cycle_t msm_dgt_read(struct clocksource *cs)
268{
269 struct msm_clock *clock = &msm_clocks[MSM_CLOCK_DGT];
270 struct msm_clock_percpu_data *clock_state =
271 &per_cpu(msm_clocks_percpu, 0)[MSM_CLOCK_DGT];
272
273 if (clock_state->stopped)
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700274 return clock_state->stopped_tick >> clock->shift;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700275
276 return (msm_read_timer_count(clock, GLOBAL_TIMER) +
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700277 clock_state->sleep_offset) >> clock->shift;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700278}
279
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700280static struct msm_clock *clockevent_to_clock(struct clock_event_device *evt)
281{
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700282#ifdef CONFIG_SMP
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700283 int i;
284 for (i = 0; i < NR_TIMERS; i++)
285 if (evt == &(msm_clocks[i].clockevent))
286 return &msm_clocks[i];
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700287 return &msm_clocks[msm_global_timer];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700288#endif
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700289 return container_of(evt, struct msm_clock, clockevent);
290}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700291
292static int msm_timer_set_next_event(unsigned long cycles,
293 struct clock_event_device *evt)
294{
295 int i;
296 struct msm_clock *clock;
297 struct msm_clock_percpu_data *clock_state;
298 uint32_t now;
299 uint32_t alarm;
300 int late;
301
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700302 clock = clockevent_to_clock(evt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700303 clock_state = &__get_cpu_var(msm_clocks_percpu)[clock->index];
304 if (clock_state->stopped)
305 return 0;
306 now = msm_read_timer_count(clock, LOCAL_TIMER);
307 alarm = now + (cycles << clock->shift);
308 if (clock->flags & MSM_CLOCK_FLAGS_ODD_MATCH_WRITE)
309 while (now == clock_state->last_set)
310 now = msm_read_timer_count(clock, LOCAL_TIMER);
311
312 clock_state->alarm = alarm;
313 __raw_writel(alarm, clock->regbase + TIMER_MATCH_VAL);
314
315 if (clock->flags & MSM_CLOCK_FLAGS_DELAYED_WRITE_POST) {
316 /* read the counter four extra times to make sure write posts
317 before reading the time */
318 for (i = 0; i < 4; i++)
319 __raw_readl(clock->regbase + TIMER_COUNT_VAL);
320 }
321 now = msm_read_timer_count(clock, LOCAL_TIMER);
322 clock_state->last_set = now;
323 clock_state->alarm_vtime = alarm + clock_state->sleep_offset;
324 late = now - alarm;
325 if (late >= (int)(-clock->write_delay << clock->shift) &&
326 late < clock->freq*5)
327 return -ETIME;
328
329 return 0;
330}
331
332static void msm_timer_set_mode(enum clock_event_mode mode,
333 struct clock_event_device *evt)
334{
335 struct msm_clock *clock;
336 struct msm_clock_percpu_data *clock_state, *gpt_state;
337 unsigned long irq_flags;
338
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700339 clock = clockevent_to_clock(evt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700340 clock_state = &__get_cpu_var(msm_clocks_percpu)[clock->index];
341 gpt_state = &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT];
342
343 local_irq_save(irq_flags);
344
345 switch (mode) {
346 case CLOCK_EVT_MODE_RESUME:
347 case CLOCK_EVT_MODE_PERIODIC:
348 break;
349 case CLOCK_EVT_MODE_ONESHOT:
350 clock_state->stopped = 0;
351 clock_state->sleep_offset =
352 -msm_read_timer_count(clock, LOCAL_TIMER) +
353 clock_state->stopped_tick;
354 get_cpu_var(msm_active_clock) = clock;
355 put_cpu_var(msm_active_clock);
356 __raw_writel(TIMER_ENABLE_EN, clock->regbase + TIMER_ENABLE);
357 if (irq_get_chip(clock->irq.irq) &&
358 irq_get_chip(clock->irq.irq)->irq_unmask) {
359 irq_get_chip(clock->irq.irq)->irq_unmask(
360 irq_get_irq_data(clock->irq.irq));
361 }
362 if (clock != &msm_clocks[MSM_CLOCK_GPT])
363 __raw_writel(TIMER_ENABLE_EN,
364 msm_clocks[MSM_CLOCK_GPT].regbase +
365 TIMER_ENABLE);
366 break;
367 case CLOCK_EVT_MODE_UNUSED:
368 case CLOCK_EVT_MODE_SHUTDOWN:
369 get_cpu_var(msm_active_clock) = NULL;
370 put_cpu_var(msm_active_clock);
371 clock_state->in_sync = 0;
372 clock_state->stopped = 1;
373 clock_state->stopped_tick =
374 msm_read_timer_count(clock, LOCAL_TIMER) +
375 clock_state->sleep_offset;
376 __raw_writel(0, clock->regbase + TIMER_MATCH_VAL);
377 if (irq_get_chip(clock->irq.irq) &&
378 irq_get_chip(clock->irq.irq)->irq_mask) {
379 irq_get_chip(clock->irq.irq)->irq_mask(
380 irq_get_irq_data(clock->irq.irq));
381 }
382#ifdef CONFIG_MSM_SMP
383 if (clock != &msm_clocks[MSM_CLOCK_DGT] || smp_processor_id())
384#endif
385 __raw_writel(0, clock->regbase + TIMER_ENABLE);
386 if (clock != &msm_clocks[MSM_CLOCK_GPT]) {
387 gpt_state->in_sync = 0;
388 __raw_writel(0, msm_clocks[MSM_CLOCK_GPT].regbase +
389 TIMER_ENABLE);
390 }
391 break;
392 }
393 wmb();
394 local_irq_restore(irq_flags);
395}
396
Jeff Ohlstein973871d2011-09-28 11:46:26 -0700397/* Call this after SMP init */
398void __iomem *msm_timer_get_timer0_base(void)
399{
400 return MSM_TMR_BASE + global_timer_offset;
401}
402
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700403#define MPM_SCLK_COUNT_VAL 0x0024
404
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700405#ifdef CONFIG_PM
406/*
407 * Retrieve the cycle count from sclk and optionally synchronize local clock
408 * with the sclk value.
409 *
410 * time_start and time_expired are callbacks that must be specified. The
411 * protocol uses them to detect timeout. The update callback is optional.
412 * If not NULL, update will be called so that it can update local clock.
413 *
414 * The function does not use the argument data directly; it passes data to
415 * the callbacks.
416 *
417 * Return value:
418 * 0: the operation failed
419 * >0: the slow clock value after time-sync
420 */
421static void (*msm_timer_sync_timeout)(void);
422#if defined(CONFIG_MSM_DIRECT_SCLK_ACCESS)
423static uint32_t msm_timer_do_sync_to_sclk(
424 void (*time_start)(struct msm_timer_sync_data_t *data),
425 bool (*time_expired)(struct msm_timer_sync_data_t *data),
426 void (*update)(struct msm_timer_sync_data_t *, uint32_t, uint32_t),
427 struct msm_timer_sync_data_t *data)
428{
429 uint32_t t1, t2;
430 int loop_count = 10;
431 int loop_zero_count = 3;
432 int tmp = USEC_PER_SEC/SCLK_HZ/(loop_zero_count-1);
433
434 while (loop_zero_count--) {
435 t1 = __raw_readl(MSM_RPM_MPM_BASE + MPM_SCLK_COUNT_VAL);
436 do {
437 udelay(1);
438 t2 = t1;
439 t1 = __raw_readl(MSM_RPM_MPM_BASE + MPM_SCLK_COUNT_VAL);
440 } while ((t2 != t1) && --loop_count);
441
442 if (!loop_count) {
443 printk(KERN_EMERG "SCLK did not stabilize\n");
444 return 0;
445 }
446
447 if (t1)
448 break;
449
450 udelay(tmp);
451 }
452
453 if (!loop_zero_count) {
454 printk(KERN_EMERG "SCLK reads zero\n");
455 return 0;
456 }
457
458 if (update != NULL)
459 update(data, t1, SCLK_HZ);
460 return t1;
461}
462#elif defined(CONFIG_MSM_N_WAY_SMSM)
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700463
464/* Time Master State Bits */
465#define MASTER_BITS_PER_CPU 1
466#define MASTER_TIME_PENDING \
467 (0x01UL << (MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
468
469/* Time Slave State Bits */
470#define SLAVE_TIME_REQUEST 0x0400
471#define SLAVE_TIME_POLL 0x0800
472#define SLAVE_TIME_INIT 0x1000
473
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700474static uint32_t msm_timer_do_sync_to_sclk(
475 void (*time_start)(struct msm_timer_sync_data_t *data),
476 bool (*time_expired)(struct msm_timer_sync_data_t *data),
477 void (*update)(struct msm_timer_sync_data_t *, uint32_t, uint32_t),
478 struct msm_timer_sync_data_t *data)
479{
480 uint32_t *smem_clock;
481 uint32_t smem_clock_val;
482 uint32_t state;
483
484 smem_clock = smem_alloc(SMEM_SMEM_SLOW_CLOCK_VALUE, sizeof(uint32_t));
485 if (smem_clock == NULL) {
486 printk(KERN_ERR "no smem clock\n");
487 return 0;
488 }
489
490 state = smsm_get_state(SMSM_MODEM_STATE);
491 if ((state & SMSM_INIT) == 0) {
492 printk(KERN_ERR "smsm not initialized\n");
493 return 0;
494 }
495
496 time_start(data);
497 while ((state = smsm_get_state(SMSM_TIME_MASTER_DEM)) &
498 MASTER_TIME_PENDING) {
499 if (time_expired(data)) {
500 printk(KERN_EMERG "get_smem_clock: timeout 1 still "
501 "invalid state %x\n", state);
502 msm_timer_sync_timeout();
503 }
504 }
505
506 smsm_change_state(SMSM_APPS_DEM, SLAVE_TIME_POLL | SLAVE_TIME_INIT,
507 SLAVE_TIME_REQUEST);
508
509 time_start(data);
510 while (!((state = smsm_get_state(SMSM_TIME_MASTER_DEM)) &
511 MASTER_TIME_PENDING)) {
512 if (time_expired(data)) {
513 printk(KERN_EMERG "get_smem_clock: timeout 2 still "
514 "invalid state %x\n", state);
515 msm_timer_sync_timeout();
516 }
517 }
518
519 smsm_change_state(SMSM_APPS_DEM, SLAVE_TIME_REQUEST, SLAVE_TIME_POLL);
520
521 time_start(data);
522 do {
523 smem_clock_val = *smem_clock;
524 } while (smem_clock_val == 0 && !time_expired(data));
525
526 state = smsm_get_state(SMSM_TIME_MASTER_DEM);
527
528 if (smem_clock_val) {
529 if (update != NULL)
530 update(data, smem_clock_val, SCLK_HZ);
531
532 if (msm_timer_debug_mask & MSM_TIMER_DEBUG_SYNC)
533 printk(KERN_INFO
534 "get_smem_clock: state %x clock %u\n",
535 state, smem_clock_val);
536 } else {
537 printk(KERN_EMERG
538 "get_smem_clock: timeout state %x clock %u\n",
539 state, smem_clock_val);
540 msm_timer_sync_timeout();
541 }
542
543 smsm_change_state(SMSM_APPS_DEM, SLAVE_TIME_REQUEST | SLAVE_TIME_POLL,
544 SLAVE_TIME_INIT);
545 return smem_clock_val;
546}
547#else /* CONFIG_MSM_N_WAY_SMSM */
548static uint32_t msm_timer_do_sync_to_sclk(
549 void (*time_start)(struct msm_timer_sync_data_t *data),
550 bool (*time_expired)(struct msm_timer_sync_data_t *data),
551 void (*update)(struct msm_timer_sync_data_t *, uint32_t, uint32_t),
552 struct msm_timer_sync_data_t *data)
553{
554 uint32_t *smem_clock;
555 uint32_t smem_clock_val;
556 uint32_t last_state;
557 uint32_t state;
558
559 smem_clock = smem_alloc(SMEM_SMEM_SLOW_CLOCK_VALUE,
560 sizeof(uint32_t));
561
562 if (smem_clock == NULL) {
563 printk(KERN_ERR "no smem clock\n");
564 return 0;
565 }
566
567 last_state = state = smsm_get_state(SMSM_MODEM_STATE);
568 smem_clock_val = *smem_clock;
569 if (smem_clock_val) {
570 printk(KERN_INFO "get_smem_clock: invalid start state %x "
571 "clock %u\n", state, smem_clock_val);
572 smsm_change_state(SMSM_APPS_STATE,
573 SMSM_TIMEWAIT, SMSM_TIMEINIT);
574
575 time_start(data);
576 while (*smem_clock != 0 && !time_expired(data))
577 ;
578
579 smem_clock_val = *smem_clock;
580 if (smem_clock_val) {
581 printk(KERN_EMERG "get_smem_clock: timeout still "
582 "invalid state %x clock %u\n",
583 state, smem_clock_val);
584 msm_timer_sync_timeout();
585 }
586 }
587
588 time_start(data);
589 smsm_change_state(SMSM_APPS_STATE, SMSM_TIMEINIT, SMSM_TIMEWAIT);
590 do {
591 smem_clock_val = *smem_clock;
592 state = smsm_get_state(SMSM_MODEM_STATE);
593 if (state != last_state) {
594 last_state = state;
595 if (msm_timer_debug_mask & MSM_TIMER_DEBUG_SYNC)
596 printk(KERN_INFO
597 "get_smem_clock: state %x clock %u\n",
598 state, smem_clock_val);
599 }
600 } while (smem_clock_val == 0 && !time_expired(data));
601
602 if (smem_clock_val) {
603 if (update != NULL)
604 update(data, smem_clock_val, SCLK_HZ);
605 } else {
606 printk(KERN_EMERG
607 "get_smem_clock: timeout state %x clock %u\n",
608 state, smem_clock_val);
609 msm_timer_sync_timeout();
610 }
611
612 smsm_change_state(SMSM_APPS_STATE, SMSM_TIMEWAIT, SMSM_TIMEINIT);
613 return smem_clock_val;
614}
615#endif /* CONFIG_MSM_N_WAY_SMSM */
616
617/*
618 * Callback function that initializes the timeout value.
619 */
620static void msm_timer_sync_to_sclk_time_start(
621 struct msm_timer_sync_data_t *data)
622{
623 /* approx 2 seconds */
624 uint32_t delta = data->clock->freq << data->clock->shift << 1;
625 data->timeout = msm_read_timer_count(data->clock, LOCAL_TIMER) + delta;
626}
627
628/*
629 * Callback function that checks the timeout.
630 */
631static bool msm_timer_sync_to_sclk_time_expired(
632 struct msm_timer_sync_data_t *data)
633{
634 uint32_t delta = msm_read_timer_count(data->clock, LOCAL_TIMER) -
635 data->timeout;
636 return ((int32_t) delta) > 0;
637}
638
639/*
640 * Callback function that updates local clock from the specified source clock
641 * value and frequency.
642 */
643static void msm_timer_sync_update(struct msm_timer_sync_data_t *data,
644 uint32_t src_clk_val, uint32_t src_clk_freq)
645{
646 struct msm_clock *dst_clk = data->clock;
647 struct msm_clock_percpu_data *dst_clk_state =
648 &__get_cpu_var(msm_clocks_percpu)[dst_clk->index];
649 uint32_t dst_clk_val = msm_read_timer_count(dst_clk, LOCAL_TIMER);
650 uint32_t new_offset;
651
652 if ((dst_clk->freq << dst_clk->shift) == src_clk_freq) {
653 new_offset = src_clk_val - dst_clk_val;
654 } else {
655 uint64_t temp;
656
657 /* separate multiplication and division steps to reduce
658 rounding error */
659 temp = src_clk_val;
660 temp *= dst_clk->freq << dst_clk->shift;
661 do_div(temp, src_clk_freq);
662
663 new_offset = (uint32_t)(temp) - dst_clk_val;
664 }
665
666 if (dst_clk_state->sleep_offset + dst_clk_state->non_sleep_offset !=
667 new_offset) {
668 if (data->exit_sleep)
669 dst_clk_state->sleep_offset =
670 new_offset - dst_clk_state->non_sleep_offset;
671 else
672 dst_clk_state->non_sleep_offset =
673 new_offset - dst_clk_state->sleep_offset;
674
675 if (msm_timer_debug_mask & MSM_TIMER_DEBUG_SYNC)
676 printk(KERN_INFO "sync clock %s: "
677 "src %u, new offset %u + %u\n",
678 dst_clk->clocksource.name, src_clk_val,
679 dst_clk_state->sleep_offset,
680 dst_clk_state->non_sleep_offset);
681 }
682}
683
684/*
685 * Synchronize GPT clock with sclk.
686 */
687static void msm_timer_sync_gpt_to_sclk(int exit_sleep)
688{
689 struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT];
690 struct msm_clock_percpu_data *gpt_clk_state =
691 &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT];
692 struct msm_timer_sync_data_t data;
693 uint32_t ret;
694
695 if (gpt_clk_state->in_sync)
696 return;
697
698 data.clock = gpt_clk;
699 data.timeout = 0;
700 data.exit_sleep = exit_sleep;
701
702 ret = msm_timer_do_sync_to_sclk(
703 msm_timer_sync_to_sclk_time_start,
704 msm_timer_sync_to_sclk_time_expired,
705 msm_timer_sync_update,
706 &data);
707
708 if (ret)
709 gpt_clk_state->in_sync = 1;
710}
711
712/*
713 * Synchronize clock with GPT clock.
714 */
715static void msm_timer_sync_to_gpt(struct msm_clock *clock, int exit_sleep)
716{
717 struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT];
718 struct msm_clock_percpu_data *gpt_clk_state =
719 &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT];
720 struct msm_clock_percpu_data *clock_state =
721 &__get_cpu_var(msm_clocks_percpu)[clock->index];
722 struct msm_timer_sync_data_t data;
723 uint32_t gpt_clk_val;
724 u64 gpt_period = (1ULL << 32) * HZ / GPT_HZ;
725 u64 now = get_jiffies_64();
726
727 BUG_ON(clock == gpt_clk);
728
729 if (clock_state->in_sync &&
730 (now - clock_state->last_sync_jiffies < (gpt_period >> 1)))
731 return;
732
733 gpt_clk_val = msm_read_timer_count(gpt_clk, LOCAL_TIMER)
734 + gpt_clk_state->sleep_offset + gpt_clk_state->non_sleep_offset;
735
736 if (exit_sleep && gpt_clk_val < clock_state->last_sync_gpt)
737 clock_state->non_sleep_offset -= clock->rollover_offset;
738
739 data.clock = clock;
740 data.timeout = 0;
741 data.exit_sleep = exit_sleep;
742
743 msm_timer_sync_update(&data, gpt_clk_val, GPT_HZ);
744
745 clock_state->in_sync = 1;
746 clock_state->last_sync_gpt = gpt_clk_val;
747 clock_state->last_sync_jiffies = now;
748}
749
750static void msm_timer_reactivate_alarm(struct msm_clock *clock)
751{
752 struct msm_clock_percpu_data *clock_state =
753 &__get_cpu_var(msm_clocks_percpu)[clock->index];
754 long alarm_delta = clock_state->alarm_vtime -
755 clock_state->sleep_offset -
756 msm_read_timer_count(clock, LOCAL_TIMER);
757 alarm_delta >>= clock->shift;
758 if (alarm_delta < (long)clock->write_delay + 4)
759 alarm_delta = clock->write_delay + 4;
760 while (msm_timer_set_next_event(alarm_delta, &clock->clockevent))
761 ;
762}
763
764int64_t msm_timer_enter_idle(void)
765{
766 struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT];
767 struct msm_clock *clock = __get_cpu_var(msm_active_clock);
768 struct msm_clock_percpu_data *clock_state =
769 &__get_cpu_var(msm_clocks_percpu)[clock->index];
770 uint32_t alarm;
771 uint32_t count;
772 int32_t delta;
773
774 BUG_ON(clock != &msm_clocks[MSM_CLOCK_GPT] &&
775 clock != &msm_clocks[MSM_CLOCK_DGT]);
776
777 msm_timer_sync_gpt_to_sclk(0);
778 if (clock != gpt_clk)
779 msm_timer_sync_to_gpt(clock, 0);
780
781 count = msm_read_timer_count(clock, LOCAL_TIMER);
782 if (clock_state->stopped++ == 0)
783 clock_state->stopped_tick = count + clock_state->sleep_offset;
784 alarm = clock_state->alarm;
785 delta = alarm - count;
786 if (delta <= -(int32_t)((clock->freq << clock->shift) >> 10)) {
787 /* timer should have triggered 1ms ago */
788 printk(KERN_ERR "msm_timer_enter_idle: timer late %d, "
789 "reprogram it\n", delta);
790 msm_timer_reactivate_alarm(clock);
791 }
792 if (delta <= 0)
793 return 0;
794 return clocksource_cyc2ns((alarm - count) >> clock->shift,
795 clock->clocksource.mult,
796 clock->clocksource.shift);
797}
798
799void msm_timer_exit_idle(int low_power)
800{
801 struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT];
802 struct msm_clock *clock = __get_cpu_var(msm_active_clock);
803 struct msm_clock_percpu_data *gpt_clk_state =
804 &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT];
805 struct msm_clock_percpu_data *clock_state =
806 &__get_cpu_var(msm_clocks_percpu)[clock->index];
807 uint32_t enabled;
808
809 BUG_ON(clock != &msm_clocks[MSM_CLOCK_GPT] &&
810 clock != &msm_clocks[MSM_CLOCK_DGT]);
811
812 if (!low_power)
813 goto exit_idle_exit;
814
815 enabled = __raw_readl(gpt_clk->regbase + TIMER_ENABLE) &
816 TIMER_ENABLE_EN;
817 if (!enabled)
818 __raw_writel(TIMER_ENABLE_EN, gpt_clk->regbase + TIMER_ENABLE);
819
820#if defined(CONFIG_ARCH_MSM_SCORPION) || defined(CONFIG_ARCH_MSM_KRAIT)
821 gpt_clk_state->in_sync = 0;
822#else
823 gpt_clk_state->in_sync = gpt_clk_state->in_sync && enabled;
824#endif
825 /* Make sure timer is actually enabled before we sync it */
826 wmb();
827 msm_timer_sync_gpt_to_sclk(1);
828
829 if (clock == gpt_clk)
830 goto exit_idle_alarm;
831
832 enabled = __raw_readl(clock->regbase + TIMER_ENABLE) & TIMER_ENABLE_EN;
833 if (!enabled)
834 __raw_writel(TIMER_ENABLE_EN, clock->regbase + TIMER_ENABLE);
835
836#if defined(CONFIG_ARCH_MSM_SCORPION) || defined(CONFIG_ARCH_MSM_KRAIT)
837 clock_state->in_sync = 0;
838#else
839 clock_state->in_sync = clock_state->in_sync && enabled;
840#endif
841 /* Make sure timer is actually enabled before we sync it */
842 wmb();
843 msm_timer_sync_to_gpt(clock, 1);
844
845exit_idle_alarm:
846 msm_timer_reactivate_alarm(clock);
847
848exit_idle_exit:
849 clock_state->stopped--;
850}
851
852/*
853 * Callback function that initializes the timeout value.
854 */
855static void msm_timer_get_sclk_time_start(
856 struct msm_timer_sync_data_t *data)
857{
858 data->timeout = 200000;
859}
860
861/*
862 * Callback function that checks the timeout.
863 */
864static bool msm_timer_get_sclk_time_expired(
865 struct msm_timer_sync_data_t *data)
866{
867 udelay(10);
868 return --data->timeout <= 0;
869}
870
871/*
872 * Retrieve the cycle count from the sclk and convert it into
873 * nanoseconds.
874 *
875 * On exit, if period is not NULL, it contains the period of the
876 * sclk in nanoseconds, i.e. how long the cycle count wraps around.
877 *
878 * Return value:
879 * 0: the operation failed; period is not set either
880 * >0: time in nanoseconds
881 */
882int64_t msm_timer_get_sclk_time(int64_t *period)
883{
884 struct msm_timer_sync_data_t data;
885 uint32_t clock_value;
886 int64_t tmp;
887
888 memset(&data, 0, sizeof(data));
889 clock_value = msm_timer_do_sync_to_sclk(
890 msm_timer_get_sclk_time_start,
891 msm_timer_get_sclk_time_expired,
892 NULL,
893 &data);
894
895 if (!clock_value)
896 return 0;
897
898 if (period) {
899 tmp = 1LL << 32;
900 tmp = tmp * NSEC_PER_SEC / SCLK_HZ;
901 *period = tmp;
902 }
903
904 tmp = (int64_t)clock_value;
905 tmp = tmp * NSEC_PER_SEC / SCLK_HZ;
906 return tmp;
907}
908
909int __init msm_timer_init_time_sync(void (*timeout)(void))
910{
911#if defined(CONFIG_MSM_N_WAY_SMSM) && !defined(CONFIG_MSM_DIRECT_SCLK_ACCESS)
912 int ret = smsm_change_intr_mask(SMSM_TIME_MASTER_DEM, 0xFFFFFFFF, 0);
913
914 if (ret) {
915 printk(KERN_ERR "%s: failed to clear interrupt mask, %d\n",
916 __func__, ret);
917 return ret;
918 }
919
920 smsm_change_state(SMSM_APPS_DEM,
921 SLAVE_TIME_REQUEST | SLAVE_TIME_POLL, SLAVE_TIME_INIT);
922#endif
923
924 BUG_ON(timeout == NULL);
925 msm_timer_sync_timeout = timeout;
926
927 return 0;
928}
929
930#endif
931
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700932static DEFINE_CLOCK_DATA(cd);
933
934unsigned long long notrace sched_clock(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700935{
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700936 struct msm_clock *clock = &msm_clocks[msm_global_timer];
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700937 struct clocksource *cs = &clock->clocksource;
938 u32 cyc = cs->read(cs);
939 return cyc_to_sched_clock(&cd, cyc, ((u32)~0 >> clock->shift));
940}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700941
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700942static void notrace msm_update_sched_clock(void)
943{
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700944 struct msm_clock *clock = &msm_clocks[msm_global_timer];
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700945 struct clocksource *cs = &clock->clocksource;
946 u32 cyc = cs->read(cs);
947 update_sched_clock(&cd, cyc, ((u32)~0) >> clock->shift);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700948}
949
950#ifdef CONFIG_MSM_SMP
951int read_current_timer(unsigned long *timer_val)
952{
953 struct msm_clock *dgt = &msm_clocks[MSM_CLOCK_DGT];
954 *timer_val = msm_read_timer_count(dgt, GLOBAL_TIMER);
955 return 0;
956}
957#endif
958
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700959static void __init msm_sched_clock_init(void)
960{
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700961 struct msm_clock *clock = &msm_clocks[msm_global_timer];
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700962
963 init_sched_clock(&cd, msm_update_sched_clock, 32 - clock->shift,
964 clock->freq);
965}
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800966static void __init msm_timer_init(void)
967{
968 int i;
969 int res;
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700970 struct msm_clock *dgt = &msm_clocks[MSM_CLOCK_DGT];
971 struct msm_clock *gpt = &msm_clocks[MSM_CLOCK_GPT];
David Brown8c27e6f2011-01-07 10:20:49 -0800972
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700973 if (cpu_is_msm7x01() || cpu_is_msm7x25() || cpu_is_msm7x27() ||
974 cpu_is_msm7x25a() || cpu_is_msm7x27a() || cpu_is_msm7x25aa() ||
975 cpu_is_msm7x27aa()) {
976 dgt->shift = MSM_DGT_SHIFT;
977 dgt->freq = 19200000 >> MSM_DGT_SHIFT;
978 dgt->clockevent.shift = 32 + MSM_DGT_SHIFT;
979 dgt->clocksource.mask = CLOCKSOURCE_MASK(32 - MSM_DGT_SHIFT);
980 dgt->clocksource.shift = 24 - MSM_DGT_SHIFT;
981 gpt->regbase = MSM_TMR_BASE;
982 dgt->regbase = MSM_TMR_BASE + 0x10;
983 } else if (cpu_is_qsd8x50()) {
984 dgt->freq = 4800000;
985 gpt->regbase = MSM_TMR_BASE;
986 dgt->regbase = MSM_TMR_BASE + 0x10;
987 } else if (cpu_is_fsm9xxx())
988 dgt->freq = 4800000;
989 else if (cpu_is_msm7x30() || cpu_is_msm8x55())
990 dgt->freq = 6144000;
991 else if (cpu_is_msm8x60() || cpu_is_msm8960() || cpu_is_msm9615() ||
992 cpu_is_apq8064() || cpu_is_msm8x30()) {
993 dgt->freq = 6750000;
994 __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
995 } else {
996 WARN_ON("Timer running on unknown hardware. Configure this! "
997 "Assuming default configuration.\n");
998 dgt->freq = 6750000;
999 }
1000
1001 if (msm_clocks[MSM_CLOCK_GPT].clocksource.rating > DG_TIMER_RATING)
1002 msm_global_timer = MSM_CLOCK_GPT;
1003 else
1004 msm_global_timer = MSM_CLOCK_DGT;
Jeff Ohlstein672039f2010-10-05 15:23:57 -07001005
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001006 for (i = 0; i < ARRAY_SIZE(msm_clocks); i++) {
1007 struct msm_clock *clock = &msm_clocks[i];
1008 struct clock_event_device *ce = &clock->clockevent;
1009 struct clocksource *cs = &clock->clocksource;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001010 __raw_writel(0, clock->regbase + TIMER_ENABLE);
1011 __raw_writel(1, clock->regbase + TIMER_CLEAR);
1012 __raw_writel(0, clock->regbase + TIMER_COUNT_VAL);
1013 __raw_writel(~0, clock->regbase + TIMER_MATCH_VAL);
David Brown8c27e6f2011-01-07 10:20:49 -08001014
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001015 if ((clock->freq << clock->shift) == GPT_HZ) {
1016 clock->rollover_offset = 0;
1017 } else {
1018 uint64_t temp;
David Brown8c27e6f2011-01-07 10:20:49 -08001019
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001020 temp = clock->freq << clock->shift;
1021 temp <<= 32;
1022 temp /= GPT_HZ;
1023
1024 clock->rollover_offset = (uint32_t) temp;
1025 }
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001026
1027 ce->mult = div_sc(clock->freq, NSEC_PER_SEC, ce->shift);
1028 /* allow at least 10 seconds to notice that the timer wrapped */
1029 ce->max_delta_ns =
1030 clockevent_delta2ns(0xf0000000 >> clock->shift, ce);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001031 /* ticks gets rounded down by one */
1032 ce->min_delta_ns =
1033 clockevent_delta2ns(clock->write_delay + 4, ce);
Rusty Russell320ab2b2008-12-13 21:20:26 +10301034 ce->cpumask = cpumask_of(0);
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001035
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001036 cs->mult = clocksource_hz2mult(clock->freq, cs->shift);
1037 res = clocksource_register(cs);
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001038 if (res)
1039 printk(KERN_ERR "msm_timer_init: clocksource_register "
1040 "failed for %s\n", cs->name);
1041
1042 res = setup_irq(clock->irq.irq, &clock->irq);
1043 if (res)
1044 printk(KERN_ERR "msm_timer_init: setup_irq "
1045 "failed for %s\n", cs->name);
1046
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001047 irq_get_chip(clock->irq.irq)->irq_mask(irq_get_irq_data(
1048 clock->irq.irq));
1049
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001050 clockevents_register_device(ce);
1051 }
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -07001052 msm_sched_clock_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001053#ifdef CONFIG_MSM_SMP
1054 __raw_writel(1, msm_clocks[MSM_CLOCK_DGT].regbase + TIMER_ENABLE);
1055 set_delay_fn(read_current_timer_delay_loop);
1056#endif
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001057}
1058
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001059#ifdef CONFIG_SMP
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001060
Santosh Shilimkaraf90f102011-02-23 18:53:15 +01001061int __cpuinit local_timer_setup(struct clock_event_device *evt)
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001062{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001063 unsigned long flags;
Jeff Ohlsteind17ee762011-09-26 19:44:22 -07001064 static DEFINE_PER_CPU(bool, first_boot) = true;
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001065 struct msm_clock *clock = &msm_clocks[msm_global_timer];
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001066
1067 /* Use existing clock_event for cpu 0 */
1068 if (!smp_processor_id())
David Brown893b66c2011-03-30 11:26:57 -07001069 return 0;
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001070
Jeff Ohlsteine1a7e402011-09-07 12:52:36 -07001071 global_timer_offset = MSM_TMR0_BASE - MSM_TMR_BASE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001072 __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001073
Jeff Ohlsteind17ee762011-09-26 19:44:22 -07001074 if (__get_cpu_var(first_boot)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001075 __raw_writel(0, clock->regbase + TIMER_ENABLE);
1076 __raw_writel(0, clock->regbase + TIMER_CLEAR);
1077 __raw_writel(~0, clock->regbase + TIMER_MATCH_VAL);
Jeff Ohlsteind17ee762011-09-26 19:44:22 -07001078 __get_cpu_var(first_boot) = false;
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001079 }
1080 evt->irq = clock->irq.irq;
1081 evt->name = "local_timer";
1082 evt->features = CLOCK_EVT_FEAT_ONESHOT;
1083 evt->rating = clock->clockevent.rating;
1084 evt->set_mode = msm_timer_set_mode;
1085 evt->set_next_event = msm_timer_set_next_event;
1086 evt->shift = clock->clockevent.shift;
1087 evt->mult = div_sc(clock->freq, NSEC_PER_SEC, evt->shift);
1088 evt->max_delta_ns =
1089 clockevent_delta2ns(0xf0000000 >> clock->shift, evt);
1090 evt->min_delta_ns = clockevent_delta2ns(4, evt);
1091
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001092 __get_cpu_var(local_clock_event) = evt;
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001093
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001094 local_irq_save(flags);
1095 gic_clear_spi_pending(clock->irq.irq);
1096 local_irq_restore(flags);
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001097 gic_enable_ppi(clock->irq.irq);
1098
1099 clockevents_register_device(evt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001100
Santosh Shilimkaraf90f102011-02-23 18:53:15 +01001101 return 0;
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001102}
1103
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001104int local_timer_ack(void)
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001105{
1106 return 1;
1107}
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001108#endif
1109
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001110struct sys_timer msm_timer = {
1111 .init = msm_timer_init
1112};