blob: 118c90b0522bfbe39e4f502d6b37ea52611edba0 [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
45#if defined(CONFIG_ARCH_MSM7X30) || defined(CONFIG_ARCH_MSM8X60) || \
46 defined(CONFIG_ARCH_MSM8960) || defined(CONFIG_ARCH_FSM9XXX) || \
Rohit Vaswani2a473b22011-08-16 15:35:34 -070047 defined(CONFIG_ARCH_APQ8064) || defined(CONFIG_ARCH_MSM9615)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070048#define MSM_GPT_BASE (MSM_TMR_BASE + 0x4)
49#define MSM_DGT_BASE (MSM_TMR_BASE + 0x24)
50#else
51#define MSM_GPT_BASE MSM_TMR_BASE
52#define MSM_DGT_BASE (MSM_TMR_BASE + 0x10)
53#endif
54
55#ifdef CONFIG_MSM7X00A_USE_GP_TIMER
56 #define DG_TIMER_RATING 100
57 #define MSM_GLOBAL_TIMER MSM_CLOCK_GPT
58#else
59 #define DG_TIMER_RATING 300
60 #define MSM_GLOBAL_TIMER MSM_CLOCK_DGT
61#endif
62
Rohit Vaswani2a473b22011-08-16 15:35:34 -070063#if defined(CONFIG_CPU_V6) || defined(CONFIG_ARCH_MSM7X27A)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070064#define MSM_DGT_SHIFT (5)
65#else
66#define MSM_DGT_SHIFT (0)
67#endif
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -080068
69#define TIMER_MATCH_VAL 0x0000
70#define TIMER_COUNT_VAL 0x0004
71#define TIMER_ENABLE 0x0008
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -080072#define TIMER_CLEAR 0x000C
Jeff Ohlstein672039f2010-10-05 15:23:57 -070073#define DGT_CLK_CTL 0x0034
74enum {
75 DGT_CLK_CTL_DIV_1 = 0,
76 DGT_CLK_CTL_DIV_2 = 1,
77 DGT_CLK_CTL_DIV_3 = 2,
78 DGT_CLK_CTL_DIV_4 = 3,
79};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070080#define TIMER_ENABLE_EN 1
81#define TIMER_ENABLE_CLR_ON_MATCH_EN 2
82
83#define LOCAL_TIMER 0
84#define GLOBAL_TIMER 1
85
86/*
Jeff Ohlsteine1a7e402011-09-07 12:52:36 -070087 * global_timer_offset is added to the regbase of a timer to force the memory
88 * access to come from the CPU0 region.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070089 */
Jeff Ohlsteine1a7e402011-09-07 12:52:36 -070090static int global_timer_offset;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070091
92#if defined(CONFIG_MSM_DIRECT_SCLK_ACCESS)
93#define MPM_SCLK_COUNT_VAL 0x0024
94#endif
95
96#define NR_TIMERS ARRAY_SIZE(msm_clocks)
97
Rohit Vaswaniffc76e92011-08-15 13:44:16 -070098#if defined(CONFIG_ARCH_QSD8X50) || defined(CONFIG_ARCH_FSM9XXX)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070099#define DGT_HZ 4800000 /* Uses TCXO/4 (19.2 MHz / 4) */
100#elif defined(CONFIG_ARCH_MSM7X30)
101#define DGT_HZ 6144000 /* Uses LPXO/4 (24.576 MHz / 4) */
102#elif defined(CONFIG_ARCH_MSM8X60) || defined(CONFIG_ARCH_MSM8960) || \
Rohit Vaswani2a473b22011-08-16 15:35:34 -0700103 defined(CONFIG_ARCH_APQ8064) || defined(CONFIG_ARCH_MSM9615)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700104/* Uses PXO/4 (24.576 MHz / 4) on V1, (27 MHz / 4) on V2 */
105#define DGT_HZ 6750000
106#else
107#define DGT_HZ 19200000 /* Uses TCXO (19.2 MHz) */
108#endif
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800109
110#define GPT_HZ 32768
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700111#define SCLK_HZ 32768
Jeff Ohlstein672039f2010-10-05 15:23:57 -0700112
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700113#if defined(CONFIG_MSM_N_WAY_SMSM)
114/* Time Master State Bits */
115#define MASTER_BITS_PER_CPU 1
116#define MASTER_TIME_PENDING \
117 (0x01UL << (MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800118
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700119/* Time Slave State Bits */
120#define SLAVE_TIME_REQUEST 0x0400
121#define SLAVE_TIME_POLL 0x0800
122#define SLAVE_TIME_INIT 0x1000
Jeff Ohlstein672039f2010-10-05 15:23:57 -0700123#endif
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800124
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700125#ifdef CONFIG_SMP
126static struct msm_clock *clockevent_to_clock(struct clock_event_device *evt);
127#endif
128static irqreturn_t msm_timer_interrupt(int irq, void *dev_id);
129static cycle_t msm_gpt_read(struct clocksource *cs);
130static cycle_t msm_dgt_read(struct clocksource *cs);
131static void msm_timer_set_mode(enum clock_event_mode mode,
132 struct clock_event_device *evt);
133static int msm_timer_set_next_event(unsigned long cycles,
134 struct clock_event_device *evt);
135
136enum {
137 MSM_CLOCK_FLAGS_UNSTABLE_COUNT = 1U << 0,
138 MSM_CLOCK_FLAGS_ODD_MATCH_WRITE = 1U << 1,
139 MSM_CLOCK_FLAGS_DELAYED_WRITE_POST = 1U << 2,
140};
141
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800142struct msm_clock {
143 struct clock_event_device clockevent;
144 struct clocksource clocksource;
145 struct irqaction irq;
Brian Swetlandbcc0f6a2008-09-10 14:00:53 -0700146 void __iomem *regbase;
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800147 uint32_t freq;
148 uint32_t shift;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700149 uint32_t flags;
150 uint32_t write_delay;
151 uint32_t rollover_offset;
152 uint32_t index;
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800153};
154
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800155enum {
156 MSM_CLOCK_GPT,
157 MSM_CLOCK_DGT,
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800158};
159
160
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700161struct msm_clock_percpu_data {
162 uint32_t last_set;
163 uint32_t sleep_offset;
164 uint32_t alarm_vtime;
165 uint32_t alarm;
166 uint32_t non_sleep_offset;
167 uint32_t in_sync;
168 cycle_t stopped_tick;
169 int stopped;
170 uint32_t last_sync_gpt;
171 u64 last_sync_jiffies;
172};
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800173
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700174struct msm_timer_sync_data_t {
175 struct msm_clock *clock;
176 uint32_t timeout;
177 int exit_sleep;
178};
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800179
180static struct msm_clock msm_clocks[] = {
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800181 [MSM_CLOCK_GPT] = {
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800182 .clockevent = {
183 .name = "gp_timer",
184 .features = CLOCK_EVT_FEAT_ONESHOT,
185 .shift = 32,
186 .rating = 200,
187 .set_next_event = msm_timer_set_next_event,
188 .set_mode = msm_timer_set_mode,
189 },
190 .clocksource = {
191 .name = "gp_timer",
192 .rating = 200,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700193 .read = msm_gpt_read,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800194 .mask = CLOCKSOURCE_MASK(32),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700195 .shift = 17,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800196 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
197 },
198 .irq = {
199 .name = "gp_timer",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700200 .flags = IRQF_DISABLED | IRQF_TIMER |
201 IRQF_TRIGGER_RISING,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800202 .handler = msm_timer_interrupt,
203 .dev_id = &msm_clocks[0].clockevent,
204 .irq = INT_GP_TIMER_EXP
205 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700206 .regbase = MSM_GPT_BASE,
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800207 .freq = GPT_HZ,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700208 .index = MSM_CLOCK_GPT,
209 .flags =
Rohit Vaswani2a473b22011-08-16 15:35:34 -0700210#if defined(CONFIG_CPU_V6) || defined(CONFIG_ARCH_MSM7X27A)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700211 MSM_CLOCK_FLAGS_UNSTABLE_COUNT |
212 MSM_CLOCK_FLAGS_ODD_MATCH_WRITE |
213 MSM_CLOCK_FLAGS_DELAYED_WRITE_POST |
214#endif
215 0,
216 .write_delay = 9,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800217 },
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800218 [MSM_CLOCK_DGT] = {
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800219 .clockevent = {
220 .name = "dg_timer",
221 .features = CLOCK_EVT_FEAT_ONESHOT,
222 .shift = 32 + MSM_DGT_SHIFT,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700223 .rating = DG_TIMER_RATING,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800224 .set_next_event = msm_timer_set_next_event,
225 .set_mode = msm_timer_set_mode,
226 },
227 .clocksource = {
228 .name = "dg_timer",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700229 .rating = DG_TIMER_RATING,
230 .read = msm_dgt_read,
231 .mask = CLOCKSOURCE_MASK((32-MSM_DGT_SHIFT)),
232 .shift = 24 - MSM_DGT_SHIFT,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800233 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
234 },
235 .irq = {
236 .name = "dg_timer",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700237 .flags = IRQF_DISABLED | IRQF_TIMER |
238 IRQF_TRIGGER_RISING,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800239 .handler = msm_timer_interrupt,
240 .dev_id = &msm_clocks[1].clockevent,
241 .irq = INT_DEBUG_TIMER_EXP
242 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700243 .regbase = MSM_DGT_BASE,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800244 .freq = DGT_HZ >> MSM_DGT_SHIFT,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700245 .index = MSM_CLOCK_DGT,
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800246 .shift = MSM_DGT_SHIFT,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700247 .write_delay = 9,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800248 }
249};
250
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700251static DEFINE_PER_CPU(struct clock_event_device*, local_clock_event);
252
253static DEFINE_PER_CPU(struct msm_clock_percpu_data[NR_TIMERS],
254 msm_clocks_percpu);
255
256static DEFINE_PER_CPU(struct msm_clock *, msm_active_clock);
257
258static irqreturn_t msm_timer_interrupt(int irq, void *dev_id)
259{
260 struct clock_event_device *evt = dev_id;
261 if (smp_processor_id() != 0)
262 evt = __get_cpu_var(local_clock_event);
263 if (evt->event_handler == NULL)
264 return IRQ_HANDLED;
265 evt->event_handler(evt);
266 return IRQ_HANDLED;
267}
268
269static uint32_t msm_read_timer_count(struct msm_clock *clock, int global)
270{
271 uint32_t t1, t2;
272 int loop_count = 0;
273
274 if (global)
275 t1 = __raw_readl(clock->regbase + TIMER_COUNT_VAL +
Jeff Ohlsteine1a7e402011-09-07 12:52:36 -0700276 global_timer_offset);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700277 else
278 t1 = __raw_readl(clock->regbase + TIMER_COUNT_VAL);
279
280 if (!(clock->flags & MSM_CLOCK_FLAGS_UNSTABLE_COUNT))
281 return t1;
282 while (1) {
283 if (global)
284 t2 = __raw_readl(clock->regbase + TIMER_COUNT_VAL +
Jeff Ohlsteine1a7e402011-09-07 12:52:36 -0700285 global_timer_offset);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700286 else
287 t2 = __raw_readl(clock->regbase + TIMER_COUNT_VAL);
288 if (t1 == t2)
289 return t1;
290 if (loop_count++ > 10) {
291 printk(KERN_ERR "msm_read_timer_count timer %s did not"
292 "stabilize %u != %u\n", clock->clockevent.name,
293 t2, t1);
294 return t2;
295 }
296 t1 = t2;
297 }
298}
299
300static cycle_t msm_gpt_read(struct clocksource *cs)
301{
302 struct msm_clock *clock = &msm_clocks[MSM_CLOCK_GPT];
303 struct msm_clock_percpu_data *clock_state =
304 &per_cpu(msm_clocks_percpu, 0)[MSM_CLOCK_GPT];
305
306 if (clock_state->stopped)
307 return clock_state->stopped_tick;
308
309 return msm_read_timer_count(clock, GLOBAL_TIMER) +
310 clock_state->sleep_offset;
311}
312
313static cycle_t msm_dgt_read(struct clocksource *cs)
314{
315 struct msm_clock *clock = &msm_clocks[MSM_CLOCK_DGT];
316 struct msm_clock_percpu_data *clock_state =
317 &per_cpu(msm_clocks_percpu, 0)[MSM_CLOCK_DGT];
318
319 if (clock_state->stopped)
320 return clock_state->stopped_tick >> MSM_DGT_SHIFT;
321
322 return (msm_read_timer_count(clock, GLOBAL_TIMER) +
323 clock_state->sleep_offset) >> MSM_DGT_SHIFT;
324}
325
326#ifdef CONFIG_SMP
327static struct msm_clock *clockevent_to_clock(struct clock_event_device *evt)
328{
329 int i;
330 for (i = 0; i < NR_TIMERS; i++)
331 if (evt == &(msm_clocks[i].clockevent))
332 return &msm_clocks[i];
333 return &msm_clocks[MSM_GLOBAL_TIMER];
334}
335#endif
336
337static int msm_timer_set_next_event(unsigned long cycles,
338 struct clock_event_device *evt)
339{
340 int i;
341 struct msm_clock *clock;
342 struct msm_clock_percpu_data *clock_state;
343 uint32_t now;
344 uint32_t alarm;
345 int late;
346
347#ifdef CONFIG_SMP
348 clock = clockevent_to_clock(evt);
349#else
350 clock = container_of(evt, struct msm_clock, clockevent);
351#endif
352 clock_state = &__get_cpu_var(msm_clocks_percpu)[clock->index];
353 if (clock_state->stopped)
354 return 0;
355 now = msm_read_timer_count(clock, LOCAL_TIMER);
356 alarm = now + (cycles << clock->shift);
357 if (clock->flags & MSM_CLOCK_FLAGS_ODD_MATCH_WRITE)
358 while (now == clock_state->last_set)
359 now = msm_read_timer_count(clock, LOCAL_TIMER);
360
361 clock_state->alarm = alarm;
362 __raw_writel(alarm, clock->regbase + TIMER_MATCH_VAL);
363
364 if (clock->flags & MSM_CLOCK_FLAGS_DELAYED_WRITE_POST) {
365 /* read the counter four extra times to make sure write posts
366 before reading the time */
367 for (i = 0; i < 4; i++)
368 __raw_readl(clock->regbase + TIMER_COUNT_VAL);
369 }
370 now = msm_read_timer_count(clock, LOCAL_TIMER);
371 clock_state->last_set = now;
372 clock_state->alarm_vtime = alarm + clock_state->sleep_offset;
373 late = now - alarm;
374 if (late >= (int)(-clock->write_delay << clock->shift) &&
375 late < clock->freq*5)
376 return -ETIME;
377
378 return 0;
379}
380
381static void msm_timer_set_mode(enum clock_event_mode mode,
382 struct clock_event_device *evt)
383{
384 struct msm_clock *clock;
385 struct msm_clock_percpu_data *clock_state, *gpt_state;
386 unsigned long irq_flags;
387
388#ifdef CONFIG_SMP
389 clock = clockevent_to_clock(evt);
390#else
391 clock = container_of(evt, struct msm_clock, clockevent);
392#endif
393 clock_state = &__get_cpu_var(msm_clocks_percpu)[clock->index];
394 gpt_state = &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT];
395
396 local_irq_save(irq_flags);
397
398 switch (mode) {
399 case CLOCK_EVT_MODE_RESUME:
400 case CLOCK_EVT_MODE_PERIODIC:
401 break;
402 case CLOCK_EVT_MODE_ONESHOT:
403 clock_state->stopped = 0;
404 clock_state->sleep_offset =
405 -msm_read_timer_count(clock, LOCAL_TIMER) +
406 clock_state->stopped_tick;
407 get_cpu_var(msm_active_clock) = clock;
408 put_cpu_var(msm_active_clock);
409 __raw_writel(TIMER_ENABLE_EN, clock->regbase + TIMER_ENABLE);
410 if (irq_get_chip(clock->irq.irq) &&
411 irq_get_chip(clock->irq.irq)->irq_unmask) {
412 irq_get_chip(clock->irq.irq)->irq_unmask(
413 irq_get_irq_data(clock->irq.irq));
414 }
415 if (clock != &msm_clocks[MSM_CLOCK_GPT])
416 __raw_writel(TIMER_ENABLE_EN,
417 msm_clocks[MSM_CLOCK_GPT].regbase +
418 TIMER_ENABLE);
419 break;
420 case CLOCK_EVT_MODE_UNUSED:
421 case CLOCK_EVT_MODE_SHUTDOWN:
422 get_cpu_var(msm_active_clock) = NULL;
423 put_cpu_var(msm_active_clock);
424 clock_state->in_sync = 0;
425 clock_state->stopped = 1;
426 clock_state->stopped_tick =
427 msm_read_timer_count(clock, LOCAL_TIMER) +
428 clock_state->sleep_offset;
429 __raw_writel(0, clock->regbase + TIMER_MATCH_VAL);
430 if (irq_get_chip(clock->irq.irq) &&
431 irq_get_chip(clock->irq.irq)->irq_mask) {
432 irq_get_chip(clock->irq.irq)->irq_mask(
433 irq_get_irq_data(clock->irq.irq));
434 }
435#ifdef CONFIG_MSM_SMP
436 if (clock != &msm_clocks[MSM_CLOCK_DGT] || smp_processor_id())
437#endif
438 __raw_writel(0, clock->regbase + TIMER_ENABLE);
439 if (clock != &msm_clocks[MSM_CLOCK_GPT]) {
440 gpt_state->in_sync = 0;
441 __raw_writel(0, msm_clocks[MSM_CLOCK_GPT].regbase +
442 TIMER_ENABLE);
443 }
444 break;
445 }
446 wmb();
447 local_irq_restore(irq_flags);
448}
449
Jeff Ohlstein973871d2011-09-28 11:46:26 -0700450/* Call this after SMP init */
451void __iomem *msm_timer_get_timer0_base(void)
452{
453 return MSM_TMR_BASE + global_timer_offset;
454}
455
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700456#ifdef CONFIG_PM
457/*
458 * Retrieve the cycle count from sclk and optionally synchronize local clock
459 * with the sclk value.
460 *
461 * time_start and time_expired are callbacks that must be specified. The
462 * protocol uses them to detect timeout. The update callback is optional.
463 * If not NULL, update will be called so that it can update local clock.
464 *
465 * The function does not use the argument data directly; it passes data to
466 * the callbacks.
467 *
468 * Return value:
469 * 0: the operation failed
470 * >0: the slow clock value after time-sync
471 */
472static void (*msm_timer_sync_timeout)(void);
473#if defined(CONFIG_MSM_DIRECT_SCLK_ACCESS)
474static 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 t1, t2;
481 int loop_count = 10;
482 int loop_zero_count = 3;
483 int tmp = USEC_PER_SEC/SCLK_HZ/(loop_zero_count-1);
484
485 while (loop_zero_count--) {
486 t1 = __raw_readl(MSM_RPM_MPM_BASE + MPM_SCLK_COUNT_VAL);
487 do {
488 udelay(1);
489 t2 = t1;
490 t1 = __raw_readl(MSM_RPM_MPM_BASE + MPM_SCLK_COUNT_VAL);
491 } while ((t2 != t1) && --loop_count);
492
493 if (!loop_count) {
494 printk(KERN_EMERG "SCLK did not stabilize\n");
495 return 0;
496 }
497
498 if (t1)
499 break;
500
501 udelay(tmp);
502 }
503
504 if (!loop_zero_count) {
505 printk(KERN_EMERG "SCLK reads zero\n");
506 return 0;
507 }
508
509 if (update != NULL)
510 update(data, t1, SCLK_HZ);
511 return t1;
512}
513#elif defined(CONFIG_MSM_N_WAY_SMSM)
514static uint32_t msm_timer_do_sync_to_sclk(
515 void (*time_start)(struct msm_timer_sync_data_t *data),
516 bool (*time_expired)(struct msm_timer_sync_data_t *data),
517 void (*update)(struct msm_timer_sync_data_t *, uint32_t, uint32_t),
518 struct msm_timer_sync_data_t *data)
519{
520 uint32_t *smem_clock;
521 uint32_t smem_clock_val;
522 uint32_t state;
523
524 smem_clock = smem_alloc(SMEM_SMEM_SLOW_CLOCK_VALUE, sizeof(uint32_t));
525 if (smem_clock == NULL) {
526 printk(KERN_ERR "no smem clock\n");
527 return 0;
528 }
529
530 state = smsm_get_state(SMSM_MODEM_STATE);
531 if ((state & SMSM_INIT) == 0) {
532 printk(KERN_ERR "smsm not initialized\n");
533 return 0;
534 }
535
536 time_start(data);
537 while ((state = smsm_get_state(SMSM_TIME_MASTER_DEM)) &
538 MASTER_TIME_PENDING) {
539 if (time_expired(data)) {
540 printk(KERN_EMERG "get_smem_clock: timeout 1 still "
541 "invalid state %x\n", state);
542 msm_timer_sync_timeout();
543 }
544 }
545
546 smsm_change_state(SMSM_APPS_DEM, SLAVE_TIME_POLL | SLAVE_TIME_INIT,
547 SLAVE_TIME_REQUEST);
548
549 time_start(data);
550 while (!((state = smsm_get_state(SMSM_TIME_MASTER_DEM)) &
551 MASTER_TIME_PENDING)) {
552 if (time_expired(data)) {
553 printk(KERN_EMERG "get_smem_clock: timeout 2 still "
554 "invalid state %x\n", state);
555 msm_timer_sync_timeout();
556 }
557 }
558
559 smsm_change_state(SMSM_APPS_DEM, SLAVE_TIME_REQUEST, SLAVE_TIME_POLL);
560
561 time_start(data);
562 do {
563 smem_clock_val = *smem_clock;
564 } while (smem_clock_val == 0 && !time_expired(data));
565
566 state = smsm_get_state(SMSM_TIME_MASTER_DEM);
567
568 if (smem_clock_val) {
569 if (update != NULL)
570 update(data, smem_clock_val, SCLK_HZ);
571
572 if (msm_timer_debug_mask & MSM_TIMER_DEBUG_SYNC)
573 printk(KERN_INFO
574 "get_smem_clock: state %x clock %u\n",
575 state, smem_clock_val);
576 } else {
577 printk(KERN_EMERG
578 "get_smem_clock: timeout state %x clock %u\n",
579 state, smem_clock_val);
580 msm_timer_sync_timeout();
581 }
582
583 smsm_change_state(SMSM_APPS_DEM, SLAVE_TIME_REQUEST | SLAVE_TIME_POLL,
584 SLAVE_TIME_INIT);
585 return smem_clock_val;
586}
587#else /* CONFIG_MSM_N_WAY_SMSM */
588static uint32_t msm_timer_do_sync_to_sclk(
589 void (*time_start)(struct msm_timer_sync_data_t *data),
590 bool (*time_expired)(struct msm_timer_sync_data_t *data),
591 void (*update)(struct msm_timer_sync_data_t *, uint32_t, uint32_t),
592 struct msm_timer_sync_data_t *data)
593{
594 uint32_t *smem_clock;
595 uint32_t smem_clock_val;
596 uint32_t last_state;
597 uint32_t state;
598
599 smem_clock = smem_alloc(SMEM_SMEM_SLOW_CLOCK_VALUE,
600 sizeof(uint32_t));
601
602 if (smem_clock == NULL) {
603 printk(KERN_ERR "no smem clock\n");
604 return 0;
605 }
606
607 last_state = state = smsm_get_state(SMSM_MODEM_STATE);
608 smem_clock_val = *smem_clock;
609 if (smem_clock_val) {
610 printk(KERN_INFO "get_smem_clock: invalid start state %x "
611 "clock %u\n", state, smem_clock_val);
612 smsm_change_state(SMSM_APPS_STATE,
613 SMSM_TIMEWAIT, SMSM_TIMEINIT);
614
615 time_start(data);
616 while (*smem_clock != 0 && !time_expired(data))
617 ;
618
619 smem_clock_val = *smem_clock;
620 if (smem_clock_val) {
621 printk(KERN_EMERG "get_smem_clock: timeout still "
622 "invalid state %x clock %u\n",
623 state, smem_clock_val);
624 msm_timer_sync_timeout();
625 }
626 }
627
628 time_start(data);
629 smsm_change_state(SMSM_APPS_STATE, SMSM_TIMEINIT, SMSM_TIMEWAIT);
630 do {
631 smem_clock_val = *smem_clock;
632 state = smsm_get_state(SMSM_MODEM_STATE);
633 if (state != last_state) {
634 last_state = state;
635 if (msm_timer_debug_mask & MSM_TIMER_DEBUG_SYNC)
636 printk(KERN_INFO
637 "get_smem_clock: state %x clock %u\n",
638 state, smem_clock_val);
639 }
640 } while (smem_clock_val == 0 && !time_expired(data));
641
642 if (smem_clock_val) {
643 if (update != NULL)
644 update(data, smem_clock_val, SCLK_HZ);
645 } else {
646 printk(KERN_EMERG
647 "get_smem_clock: timeout state %x clock %u\n",
648 state, smem_clock_val);
649 msm_timer_sync_timeout();
650 }
651
652 smsm_change_state(SMSM_APPS_STATE, SMSM_TIMEWAIT, SMSM_TIMEINIT);
653 return smem_clock_val;
654}
655#endif /* CONFIG_MSM_N_WAY_SMSM */
656
657/*
658 * Callback function that initializes the timeout value.
659 */
660static void msm_timer_sync_to_sclk_time_start(
661 struct msm_timer_sync_data_t *data)
662{
663 /* approx 2 seconds */
664 uint32_t delta = data->clock->freq << data->clock->shift << 1;
665 data->timeout = msm_read_timer_count(data->clock, LOCAL_TIMER) + delta;
666}
667
668/*
669 * Callback function that checks the timeout.
670 */
671static bool msm_timer_sync_to_sclk_time_expired(
672 struct msm_timer_sync_data_t *data)
673{
674 uint32_t delta = msm_read_timer_count(data->clock, LOCAL_TIMER) -
675 data->timeout;
676 return ((int32_t) delta) > 0;
677}
678
679/*
680 * Callback function that updates local clock from the specified source clock
681 * value and frequency.
682 */
683static void msm_timer_sync_update(struct msm_timer_sync_data_t *data,
684 uint32_t src_clk_val, uint32_t src_clk_freq)
685{
686 struct msm_clock *dst_clk = data->clock;
687 struct msm_clock_percpu_data *dst_clk_state =
688 &__get_cpu_var(msm_clocks_percpu)[dst_clk->index];
689 uint32_t dst_clk_val = msm_read_timer_count(dst_clk, LOCAL_TIMER);
690 uint32_t new_offset;
691
692 if ((dst_clk->freq << dst_clk->shift) == src_clk_freq) {
693 new_offset = src_clk_val - dst_clk_val;
694 } else {
695 uint64_t temp;
696
697 /* separate multiplication and division steps to reduce
698 rounding error */
699 temp = src_clk_val;
700 temp *= dst_clk->freq << dst_clk->shift;
701 do_div(temp, src_clk_freq);
702
703 new_offset = (uint32_t)(temp) - dst_clk_val;
704 }
705
706 if (dst_clk_state->sleep_offset + dst_clk_state->non_sleep_offset !=
707 new_offset) {
708 if (data->exit_sleep)
709 dst_clk_state->sleep_offset =
710 new_offset - dst_clk_state->non_sleep_offset;
711 else
712 dst_clk_state->non_sleep_offset =
713 new_offset - dst_clk_state->sleep_offset;
714
715 if (msm_timer_debug_mask & MSM_TIMER_DEBUG_SYNC)
716 printk(KERN_INFO "sync clock %s: "
717 "src %u, new offset %u + %u\n",
718 dst_clk->clocksource.name, src_clk_val,
719 dst_clk_state->sleep_offset,
720 dst_clk_state->non_sleep_offset);
721 }
722}
723
724/*
725 * Synchronize GPT clock with sclk.
726 */
727static void msm_timer_sync_gpt_to_sclk(int exit_sleep)
728{
729 struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT];
730 struct msm_clock_percpu_data *gpt_clk_state =
731 &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT];
732 struct msm_timer_sync_data_t data;
733 uint32_t ret;
734
735 if (gpt_clk_state->in_sync)
736 return;
737
738 data.clock = gpt_clk;
739 data.timeout = 0;
740 data.exit_sleep = exit_sleep;
741
742 ret = msm_timer_do_sync_to_sclk(
743 msm_timer_sync_to_sclk_time_start,
744 msm_timer_sync_to_sclk_time_expired,
745 msm_timer_sync_update,
746 &data);
747
748 if (ret)
749 gpt_clk_state->in_sync = 1;
750}
751
752/*
753 * Synchronize clock with GPT clock.
754 */
755static void msm_timer_sync_to_gpt(struct msm_clock *clock, int exit_sleep)
756{
757 struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT];
758 struct msm_clock_percpu_data *gpt_clk_state =
759 &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT];
760 struct msm_clock_percpu_data *clock_state =
761 &__get_cpu_var(msm_clocks_percpu)[clock->index];
762 struct msm_timer_sync_data_t data;
763 uint32_t gpt_clk_val;
764 u64 gpt_period = (1ULL << 32) * HZ / GPT_HZ;
765 u64 now = get_jiffies_64();
766
767 BUG_ON(clock == gpt_clk);
768
769 if (clock_state->in_sync &&
770 (now - clock_state->last_sync_jiffies < (gpt_period >> 1)))
771 return;
772
773 gpt_clk_val = msm_read_timer_count(gpt_clk, LOCAL_TIMER)
774 + gpt_clk_state->sleep_offset + gpt_clk_state->non_sleep_offset;
775
776 if (exit_sleep && gpt_clk_val < clock_state->last_sync_gpt)
777 clock_state->non_sleep_offset -= clock->rollover_offset;
778
779 data.clock = clock;
780 data.timeout = 0;
781 data.exit_sleep = exit_sleep;
782
783 msm_timer_sync_update(&data, gpt_clk_val, GPT_HZ);
784
785 clock_state->in_sync = 1;
786 clock_state->last_sync_gpt = gpt_clk_val;
787 clock_state->last_sync_jiffies = now;
788}
789
790static void msm_timer_reactivate_alarm(struct msm_clock *clock)
791{
792 struct msm_clock_percpu_data *clock_state =
793 &__get_cpu_var(msm_clocks_percpu)[clock->index];
794 long alarm_delta = clock_state->alarm_vtime -
795 clock_state->sleep_offset -
796 msm_read_timer_count(clock, LOCAL_TIMER);
797 alarm_delta >>= clock->shift;
798 if (alarm_delta < (long)clock->write_delay + 4)
799 alarm_delta = clock->write_delay + 4;
800 while (msm_timer_set_next_event(alarm_delta, &clock->clockevent))
801 ;
802}
803
804int64_t msm_timer_enter_idle(void)
805{
806 struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT];
807 struct msm_clock *clock = __get_cpu_var(msm_active_clock);
808 struct msm_clock_percpu_data *clock_state =
809 &__get_cpu_var(msm_clocks_percpu)[clock->index];
810 uint32_t alarm;
811 uint32_t count;
812 int32_t delta;
813
814 BUG_ON(clock != &msm_clocks[MSM_CLOCK_GPT] &&
815 clock != &msm_clocks[MSM_CLOCK_DGT]);
816
817 msm_timer_sync_gpt_to_sclk(0);
818 if (clock != gpt_clk)
819 msm_timer_sync_to_gpt(clock, 0);
820
821 count = msm_read_timer_count(clock, LOCAL_TIMER);
822 if (clock_state->stopped++ == 0)
823 clock_state->stopped_tick = count + clock_state->sleep_offset;
824 alarm = clock_state->alarm;
825 delta = alarm - count;
826 if (delta <= -(int32_t)((clock->freq << clock->shift) >> 10)) {
827 /* timer should have triggered 1ms ago */
828 printk(KERN_ERR "msm_timer_enter_idle: timer late %d, "
829 "reprogram it\n", delta);
830 msm_timer_reactivate_alarm(clock);
831 }
832 if (delta <= 0)
833 return 0;
834 return clocksource_cyc2ns((alarm - count) >> clock->shift,
835 clock->clocksource.mult,
836 clock->clocksource.shift);
837}
838
839void msm_timer_exit_idle(int low_power)
840{
841 struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT];
842 struct msm_clock *clock = __get_cpu_var(msm_active_clock);
843 struct msm_clock_percpu_data *gpt_clk_state =
844 &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT];
845 struct msm_clock_percpu_data *clock_state =
846 &__get_cpu_var(msm_clocks_percpu)[clock->index];
847 uint32_t enabled;
848
849 BUG_ON(clock != &msm_clocks[MSM_CLOCK_GPT] &&
850 clock != &msm_clocks[MSM_CLOCK_DGT]);
851
852 if (!low_power)
853 goto exit_idle_exit;
854
855 enabled = __raw_readl(gpt_clk->regbase + TIMER_ENABLE) &
856 TIMER_ENABLE_EN;
857 if (!enabled)
858 __raw_writel(TIMER_ENABLE_EN, gpt_clk->regbase + TIMER_ENABLE);
859
860#if defined(CONFIG_ARCH_MSM_SCORPION) || defined(CONFIG_ARCH_MSM_KRAIT)
861 gpt_clk_state->in_sync = 0;
862#else
863 gpt_clk_state->in_sync = gpt_clk_state->in_sync && enabled;
864#endif
865 /* Make sure timer is actually enabled before we sync it */
866 wmb();
867 msm_timer_sync_gpt_to_sclk(1);
868
869 if (clock == gpt_clk)
870 goto exit_idle_alarm;
871
872 enabled = __raw_readl(clock->regbase + TIMER_ENABLE) & TIMER_ENABLE_EN;
873 if (!enabled)
874 __raw_writel(TIMER_ENABLE_EN, clock->regbase + TIMER_ENABLE);
875
876#if defined(CONFIG_ARCH_MSM_SCORPION) || defined(CONFIG_ARCH_MSM_KRAIT)
877 clock_state->in_sync = 0;
878#else
879 clock_state->in_sync = clock_state->in_sync && enabled;
880#endif
881 /* Make sure timer is actually enabled before we sync it */
882 wmb();
883 msm_timer_sync_to_gpt(clock, 1);
884
885exit_idle_alarm:
886 msm_timer_reactivate_alarm(clock);
887
888exit_idle_exit:
889 clock_state->stopped--;
890}
891
892/*
893 * Callback function that initializes the timeout value.
894 */
895static void msm_timer_get_sclk_time_start(
896 struct msm_timer_sync_data_t *data)
897{
898 data->timeout = 200000;
899}
900
901/*
902 * Callback function that checks the timeout.
903 */
904static bool msm_timer_get_sclk_time_expired(
905 struct msm_timer_sync_data_t *data)
906{
907 udelay(10);
908 return --data->timeout <= 0;
909}
910
911/*
912 * Retrieve the cycle count from the sclk and convert it into
913 * nanoseconds.
914 *
915 * On exit, if period is not NULL, it contains the period of the
916 * sclk in nanoseconds, i.e. how long the cycle count wraps around.
917 *
918 * Return value:
919 * 0: the operation failed; period is not set either
920 * >0: time in nanoseconds
921 */
922int64_t msm_timer_get_sclk_time(int64_t *period)
923{
924 struct msm_timer_sync_data_t data;
925 uint32_t clock_value;
926 int64_t tmp;
927
928 memset(&data, 0, sizeof(data));
929 clock_value = msm_timer_do_sync_to_sclk(
930 msm_timer_get_sclk_time_start,
931 msm_timer_get_sclk_time_expired,
932 NULL,
933 &data);
934
935 if (!clock_value)
936 return 0;
937
938 if (period) {
939 tmp = 1LL << 32;
940 tmp = tmp * NSEC_PER_SEC / SCLK_HZ;
941 *period = tmp;
942 }
943
944 tmp = (int64_t)clock_value;
945 tmp = tmp * NSEC_PER_SEC / SCLK_HZ;
946 return tmp;
947}
948
949int __init msm_timer_init_time_sync(void (*timeout)(void))
950{
951#if defined(CONFIG_MSM_N_WAY_SMSM) && !defined(CONFIG_MSM_DIRECT_SCLK_ACCESS)
952 int ret = smsm_change_intr_mask(SMSM_TIME_MASTER_DEM, 0xFFFFFFFF, 0);
953
954 if (ret) {
955 printk(KERN_ERR "%s: failed to clear interrupt mask, %d\n",
956 __func__, ret);
957 return ret;
958 }
959
960 smsm_change_state(SMSM_APPS_DEM,
961 SLAVE_TIME_REQUEST | SLAVE_TIME_POLL, SLAVE_TIME_INIT);
962#endif
963
964 BUG_ON(timeout == NULL);
965 msm_timer_sync_timeout = timeout;
966
967 return 0;
968}
969
970#endif
971
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700972static DEFINE_CLOCK_DATA(cd);
973
974unsigned long long notrace sched_clock(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700975{
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700976 struct msm_clock *clock = &msm_clocks[MSM_GLOBAL_TIMER];
977 struct clocksource *cs = &clock->clocksource;
978 u32 cyc = cs->read(cs);
979 return cyc_to_sched_clock(&cd, cyc, ((u32)~0 >> clock->shift));
980}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700981
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700982static void notrace msm_update_sched_clock(void)
983{
984 struct msm_clock *clock = &msm_clocks[MSM_GLOBAL_TIMER];
985 struct clocksource *cs = &clock->clocksource;
986 u32 cyc = cs->read(cs);
987 update_sched_clock(&cd, cyc, ((u32)~0) >> clock->shift);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700988}
989
990#ifdef CONFIG_MSM_SMP
991int read_current_timer(unsigned long *timer_val)
992{
993 struct msm_clock *dgt = &msm_clocks[MSM_CLOCK_DGT];
994 *timer_val = msm_read_timer_count(dgt, GLOBAL_TIMER);
995 return 0;
996}
997#endif
998
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700999static void __init msm_sched_clock_init(void)
1000{
1001 struct msm_clock *clock = &msm_clocks[MSM_GLOBAL_TIMER];
1002
1003 init_sched_clock(&cd, msm_update_sched_clock, 32 - clock->shift,
1004 clock->freq);
1005}
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001006static void __init msm_timer_init(void)
1007{
1008 int i;
1009 int res;
David Brown8c27e6f2011-01-07 10:20:49 -08001010
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001011#if defined(CONFIG_ARCH_MSM8X60) || defined(CONFIG_ARCH_MSM8960) || \
Rohit Vaswani2a473b22011-08-16 15:35:34 -07001012 defined(CONFIG_ARCH_APQ8064) || defined(CONFIG_ARCH_MSM9615)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001013 __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
Jeff Ohlstein672039f2010-10-05 15:23:57 -07001014#endif
1015
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001016 for (i = 0; i < ARRAY_SIZE(msm_clocks); i++) {
1017 struct msm_clock *clock = &msm_clocks[i];
1018 struct clock_event_device *ce = &clock->clockevent;
1019 struct clocksource *cs = &clock->clocksource;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001020 __raw_writel(0, clock->regbase + TIMER_ENABLE);
1021 __raw_writel(1, clock->regbase + TIMER_CLEAR);
1022 __raw_writel(0, clock->regbase + TIMER_COUNT_VAL);
1023 __raw_writel(~0, clock->regbase + TIMER_MATCH_VAL);
David Brown8c27e6f2011-01-07 10:20:49 -08001024
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001025 if ((clock->freq << clock->shift) == GPT_HZ) {
1026 clock->rollover_offset = 0;
1027 } else {
1028 uint64_t temp;
David Brown8c27e6f2011-01-07 10:20:49 -08001029
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001030 temp = clock->freq << clock->shift;
1031 temp <<= 32;
1032 temp /= GPT_HZ;
1033
1034 clock->rollover_offset = (uint32_t) temp;
1035 }
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001036
1037 ce->mult = div_sc(clock->freq, NSEC_PER_SEC, ce->shift);
1038 /* allow at least 10 seconds to notice that the timer wrapped */
1039 ce->max_delta_ns =
1040 clockevent_delta2ns(0xf0000000 >> clock->shift, ce);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001041 /* ticks gets rounded down by one */
1042 ce->min_delta_ns =
1043 clockevent_delta2ns(clock->write_delay + 4, ce);
Rusty Russell320ab2b2008-12-13 21:20:26 +10301044 ce->cpumask = cpumask_of(0);
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001045
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001046 cs->mult = clocksource_hz2mult(clock->freq, cs->shift);
1047 res = clocksource_register(cs);
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001048 if (res)
1049 printk(KERN_ERR "msm_timer_init: clocksource_register "
1050 "failed for %s\n", cs->name);
1051
1052 res = setup_irq(clock->irq.irq, &clock->irq);
1053 if (res)
1054 printk(KERN_ERR "msm_timer_init: setup_irq "
1055 "failed for %s\n", cs->name);
1056
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001057 irq_get_chip(clock->irq.irq)->irq_mask(irq_get_irq_data(
1058 clock->irq.irq));
1059
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001060 clockevents_register_device(ce);
1061 }
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -07001062 msm_sched_clock_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001063#ifdef CONFIG_MSM_SMP
1064 __raw_writel(1, msm_clocks[MSM_CLOCK_DGT].regbase + TIMER_ENABLE);
1065 set_delay_fn(read_current_timer_delay_loop);
1066#endif
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001067}
1068
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001069#ifdef CONFIG_SMP
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001070
Santosh Shilimkaraf90f102011-02-23 18:53:15 +01001071int __cpuinit local_timer_setup(struct clock_event_device *evt)
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001072{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001073 unsigned long flags;
1074 static bool first_boot = true;
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001075 struct msm_clock *clock = &msm_clocks[MSM_GLOBAL_TIMER];
1076
1077 /* Use existing clock_event for cpu 0 */
1078 if (!smp_processor_id())
David Brown893b66c2011-03-30 11:26:57 -07001079 return 0;
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001080
Jeff Ohlsteine1a7e402011-09-07 12:52:36 -07001081 global_timer_offset = MSM_TMR0_BASE - MSM_TMR_BASE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001082 __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001083
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001084 if (first_boot) {
1085 __raw_writel(0, clock->regbase + TIMER_ENABLE);
1086 __raw_writel(0, clock->regbase + TIMER_CLEAR);
1087 __raw_writel(~0, clock->regbase + TIMER_MATCH_VAL);
1088 first_boot = false;
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001089 }
1090 evt->irq = clock->irq.irq;
1091 evt->name = "local_timer";
1092 evt->features = CLOCK_EVT_FEAT_ONESHOT;
1093 evt->rating = clock->clockevent.rating;
1094 evt->set_mode = msm_timer_set_mode;
1095 evt->set_next_event = msm_timer_set_next_event;
1096 evt->shift = clock->clockevent.shift;
1097 evt->mult = div_sc(clock->freq, NSEC_PER_SEC, evt->shift);
1098 evt->max_delta_ns =
1099 clockevent_delta2ns(0xf0000000 >> clock->shift, evt);
1100 evt->min_delta_ns = clockevent_delta2ns(4, evt);
1101
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001102 __get_cpu_var(local_clock_event) = evt;
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001103
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001104 local_irq_save(flags);
1105 gic_clear_spi_pending(clock->irq.irq);
1106 local_irq_restore(flags);
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001107 gic_enable_ppi(clock->irq.irq);
1108
1109 clockevents_register_device(evt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001110
Santosh Shilimkaraf90f102011-02-23 18:53:15 +01001111 return 0;
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001112}
1113
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001114int local_timer_ack(void)
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001115{
1116 return 1;
1117}
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001118#endif
1119
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001120struct sys_timer msm_timer = {
1121 .init = msm_timer_init
1122};