blob: 3d44075cd93bf47bdd89ebdcfdf766fd3d00ccf1 [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.
Jeff Ohlsteinf0a31e42012-01-06 19:03:05 -08004 * Copyright (c) 2009-2012, 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>
Taniya Das36057be2011-10-28 13:02:17 +053030#include <asm/smp_plat.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010031#include <mach/msm_iomap.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070032#include <mach/irqs.h>
33#include <mach/socinfo.h>
34
35#if defined(CONFIG_MSM_SMD)
36#include "smd_private.h"
37#endif
38#include "timer.h"
39
40enum {
41 MSM_TIMER_DEBUG_SYNC = 1U << 0,
42};
43static int msm_timer_debug_mask;
44module_param_named(debug_mask, msm_timer_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP);
45
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070046#ifdef CONFIG_MSM7X00A_USE_GP_TIMER
47 #define DG_TIMER_RATING 100
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070048#else
49 #define DG_TIMER_RATING 300
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070050#endif
51
Jeff Ohlstein7e538f02011-11-01 17:36:22 -070052#ifndef MSM_TMR0_BASE
53#define MSM_TMR0_BASE MSM_TMR_BASE
54#endif
55
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070056#define MSM_DGT_SHIFT (5)
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -080057
58#define TIMER_MATCH_VAL 0x0000
59#define TIMER_COUNT_VAL 0x0004
60#define TIMER_ENABLE 0x0008
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -080061#define TIMER_CLEAR 0x000C
Jeff Ohlstein672039f2010-10-05 15:23:57 -070062#define DGT_CLK_CTL 0x0034
63enum {
64 DGT_CLK_CTL_DIV_1 = 0,
65 DGT_CLK_CTL_DIV_2 = 1,
66 DGT_CLK_CTL_DIV_3 = 2,
67 DGT_CLK_CTL_DIV_4 = 3,
68};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070069#define TIMER_ENABLE_EN 1
70#define TIMER_ENABLE_CLR_ON_MATCH_EN 2
71
72#define LOCAL_TIMER 0
73#define GLOBAL_TIMER 1
74
75/*
Jeff Ohlsteine1a7e402011-09-07 12:52:36 -070076 * global_timer_offset is added to the regbase of a timer to force the memory
77 * access to come from the CPU0 region.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070078 */
Jeff Ohlsteine1a7e402011-09-07 12:52:36 -070079static int global_timer_offset;
Jeff Ohlstein7a018322011-09-28 12:44:06 -070080static int msm_global_timer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070081
82#define NR_TIMERS ARRAY_SIZE(msm_clocks)
83
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -070084unsigned int gpt_hz = 32768;
85unsigned int sclk_hz = 32768;
Jeff Ohlstein94790ec2010-12-02 12:05:12 -080086
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070087static struct msm_clock *clockevent_to_clock(struct clock_event_device *evt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070088static irqreturn_t msm_timer_interrupt(int irq, void *dev_id);
89static cycle_t msm_gpt_read(struct clocksource *cs);
90static cycle_t msm_dgt_read(struct clocksource *cs);
91static void msm_timer_set_mode(enum clock_event_mode mode,
92 struct clock_event_device *evt);
93static int msm_timer_set_next_event(unsigned long cycles,
94 struct clock_event_device *evt);
95
96enum {
97 MSM_CLOCK_FLAGS_UNSTABLE_COUNT = 1U << 0,
98 MSM_CLOCK_FLAGS_ODD_MATCH_WRITE = 1U << 1,
99 MSM_CLOCK_FLAGS_DELAYED_WRITE_POST = 1U << 2,
100};
101
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800102struct msm_clock {
103 struct clock_event_device clockevent;
104 struct clocksource clocksource;
Trilok Sonieecb28c2011-07-20 16:24:14 +0100105 unsigned int irq;
Brian Swetlandbcc0f6a2008-09-10 14:00:53 -0700106 void __iomem *regbase;
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800107 uint32_t freq;
108 uint32_t shift;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700109 uint32_t flags;
110 uint32_t write_delay;
111 uint32_t rollover_offset;
112 uint32_t index;
Trilok Sonieecb28c2011-07-20 16:24:14 +0100113 void __iomem *global_counter;
114 void __iomem *local_counter;
115 union {
116 struct clock_event_device *evt;
117 struct clock_event_device __percpu **percpu_evt;
118 };
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800119};
120
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800121enum {
122 MSM_CLOCK_GPT,
123 MSM_CLOCK_DGT,
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800124};
125
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700126struct msm_clock_percpu_data {
127 uint32_t last_set;
128 uint32_t sleep_offset;
129 uint32_t alarm_vtime;
130 uint32_t alarm;
131 uint32_t non_sleep_offset;
132 uint32_t in_sync;
133 cycle_t stopped_tick;
134 int stopped;
135 uint32_t last_sync_gpt;
136 u64 last_sync_jiffies;
137};
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800138
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700139struct msm_timer_sync_data_t {
140 struct msm_clock *clock;
141 uint32_t timeout;
142 int exit_sleep;
143};
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800144
145static struct msm_clock msm_clocks[] = {
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800146 [MSM_CLOCK_GPT] = {
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800147 .clockevent = {
148 .name = "gp_timer",
149 .features = CLOCK_EVT_FEAT_ONESHOT,
150 .shift = 32,
151 .rating = 200,
152 .set_next_event = msm_timer_set_next_event,
153 .set_mode = msm_timer_set_mode,
154 },
155 .clocksource = {
156 .name = "gp_timer",
157 .rating = 200,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700158 .read = msm_gpt_read,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800159 .mask = CLOCKSOURCE_MASK(32),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700160 .shift = 17,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800161 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
162 },
Trilok Sonieecb28c2011-07-20 16:24:14 +0100163 .irq = INT_GP_TIMER_EXP,
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700164 .regbase = MSM_TMR_BASE + 0x4,
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700165 .freq = 32768,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700166 .index = MSM_CLOCK_GPT,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700167 .write_delay = 9,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800168 },
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800169 [MSM_CLOCK_DGT] = {
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800170 .clockevent = {
171 .name = "dg_timer",
172 .features = CLOCK_EVT_FEAT_ONESHOT,
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700173 .shift = 32,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700174 .rating = DG_TIMER_RATING,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800175 .set_next_event = msm_timer_set_next_event,
176 .set_mode = msm_timer_set_mode,
177 },
178 .clocksource = {
179 .name = "dg_timer",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700180 .rating = DG_TIMER_RATING,
181 .read = msm_dgt_read,
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700182 .mask = CLOCKSOURCE_MASK(32),
183 .shift = 24,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800184 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
185 },
Trilok Sonieecb28c2011-07-20 16:24:14 +0100186 .irq = INT_DEBUG_TIMER_EXP,
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700187 .regbase = MSM_TMR_BASE + 0x24,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700188 .index = MSM_CLOCK_DGT,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700189 .write_delay = 9,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800190 }
191};
192
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700193static DEFINE_PER_CPU(struct msm_clock_percpu_data[NR_TIMERS],
194 msm_clocks_percpu);
195
196static DEFINE_PER_CPU(struct msm_clock *, msm_active_clock);
197
198static irqreturn_t msm_timer_interrupt(int irq, void *dev_id)
199{
Trilok Sonieecb28c2011-07-20 16:24:14 +0100200 struct clock_event_device *evt = *(struct clock_event_device **)dev_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700201 if (evt->event_handler == NULL)
202 return IRQ_HANDLED;
203 evt->event_handler(evt);
204 return IRQ_HANDLED;
205}
206
207static uint32_t msm_read_timer_count(struct msm_clock *clock, int global)
208{
Jeff Ohlstein5d90e252011-11-04 19:00:50 -0700209 uint32_t t1, t2, t3;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700210 int loop_count = 0;
Jeff Ohlstein5d90e252011-11-04 19:00:50 -0700211 void __iomem *addr = clock->regbase + TIMER_COUNT_VAL +
212 global*global_timer_offset;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700213
214 if (!(clock->flags & MSM_CLOCK_FLAGS_UNSTABLE_COUNT))
Jeff Ohlstein5d90e252011-11-04 19:00:50 -0700215 return __raw_readl(addr);
216
217 t1 = __raw_readl(addr);
218 t2 = __raw_readl(addr);
219 if ((t2-t1) <= 1)
220 return t2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700221 while (1) {
Jeff Ohlstein5d90e252011-11-04 19:00:50 -0700222 t1 = __raw_readl(addr);
223 t2 = __raw_readl(addr);
224 t3 = __raw_readl(addr);
Jeff Ohlstein10206eb2011-11-30 19:18:49 -0800225 cpu_relax();
Jeff Ohlstein5d90e252011-11-04 19:00:50 -0700226 if ((t3-t2) <= 1)
227 return t3;
228 if ((t2-t1) <= 1)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700229 return t2;
Jeff Ohlsteinfdd87082011-12-09 13:40:08 -0800230 if ((t2 >= t1) && (t3 >= t2))
231 return t2;
Jeff Ohlstein10206eb2011-11-30 19:18:49 -0800232 if (++loop_count == 5) {
Jeff Ohlstein5d90e252011-11-04 19:00:50 -0700233 pr_err("msm_read_timer_count timer %s did not "
234 "stabilize: %u -> %u -> %u\n",
235 clock->clockevent.name, t1, t2, t3);
236 return t3;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700237 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700238 }
239}
240
241static cycle_t msm_gpt_read(struct clocksource *cs)
242{
243 struct msm_clock *clock = &msm_clocks[MSM_CLOCK_GPT];
244 struct msm_clock_percpu_data *clock_state =
245 &per_cpu(msm_clocks_percpu, 0)[MSM_CLOCK_GPT];
246
247 if (clock_state->stopped)
248 return clock_state->stopped_tick;
249
250 return msm_read_timer_count(clock, GLOBAL_TIMER) +
251 clock_state->sleep_offset;
252}
253
254static cycle_t msm_dgt_read(struct clocksource *cs)
255{
256 struct msm_clock *clock = &msm_clocks[MSM_CLOCK_DGT];
257 struct msm_clock_percpu_data *clock_state =
258 &per_cpu(msm_clocks_percpu, 0)[MSM_CLOCK_DGT];
259
260 if (clock_state->stopped)
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700261 return clock_state->stopped_tick >> clock->shift;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700262
263 return (msm_read_timer_count(clock, GLOBAL_TIMER) +
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700264 clock_state->sleep_offset) >> clock->shift;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700265}
266
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700267static struct msm_clock *clockevent_to_clock(struct clock_event_device *evt)
268{
269 int i;
Taniya Das36057be2011-10-28 13:02:17 +0530270
271 if (!is_smp())
272 return container_of(evt, struct msm_clock, clockevent);
273
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700274 for (i = 0; i < NR_TIMERS; i++)
275 if (evt == &(msm_clocks[i].clockevent))
276 return &msm_clocks[i];
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700277 return &msm_clocks[msm_global_timer];
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700278}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700279
280static int msm_timer_set_next_event(unsigned long cycles,
281 struct clock_event_device *evt)
282{
283 int i;
284 struct msm_clock *clock;
285 struct msm_clock_percpu_data *clock_state;
286 uint32_t now;
287 uint32_t alarm;
288 int late;
289
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700290 clock = clockevent_to_clock(evt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700291 clock_state = &__get_cpu_var(msm_clocks_percpu)[clock->index];
292 if (clock_state->stopped)
293 return 0;
294 now = msm_read_timer_count(clock, LOCAL_TIMER);
295 alarm = now + (cycles << clock->shift);
296 if (clock->flags & MSM_CLOCK_FLAGS_ODD_MATCH_WRITE)
297 while (now == clock_state->last_set)
298 now = msm_read_timer_count(clock, LOCAL_TIMER);
299
300 clock_state->alarm = alarm;
301 __raw_writel(alarm, clock->regbase + TIMER_MATCH_VAL);
302
303 if (clock->flags & MSM_CLOCK_FLAGS_DELAYED_WRITE_POST) {
304 /* read the counter four extra times to make sure write posts
305 before reading the time */
306 for (i = 0; i < 4; i++)
307 __raw_readl(clock->regbase + TIMER_COUNT_VAL);
308 }
309 now = msm_read_timer_count(clock, LOCAL_TIMER);
310 clock_state->last_set = now;
311 clock_state->alarm_vtime = alarm + clock_state->sleep_offset;
312 late = now - alarm;
313 if (late >= (int)(-clock->write_delay << clock->shift) &&
314 late < clock->freq*5)
315 return -ETIME;
316
317 return 0;
318}
319
320static void msm_timer_set_mode(enum clock_event_mode mode,
321 struct clock_event_device *evt)
322{
323 struct msm_clock *clock;
324 struct msm_clock_percpu_data *clock_state, *gpt_state;
325 unsigned long irq_flags;
Jin Hongeecb1e02011-10-21 14:36:32 -0700326 struct irq_chip *chip;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700327
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700328 clock = clockevent_to_clock(evt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700329 clock_state = &__get_cpu_var(msm_clocks_percpu)[clock->index];
330 gpt_state = &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT];
331
332 local_irq_save(irq_flags);
333
334 switch (mode) {
335 case CLOCK_EVT_MODE_RESUME:
336 case CLOCK_EVT_MODE_PERIODIC:
337 break;
338 case CLOCK_EVT_MODE_ONESHOT:
339 clock_state->stopped = 0;
340 clock_state->sleep_offset =
341 -msm_read_timer_count(clock, LOCAL_TIMER) +
342 clock_state->stopped_tick;
343 get_cpu_var(msm_active_clock) = clock;
344 put_cpu_var(msm_active_clock);
345 __raw_writel(TIMER_ENABLE_EN, clock->regbase + TIMER_ENABLE);
Trilok Sonieecb28c2011-07-20 16:24:14 +0100346 chip = irq_get_chip(clock->irq);
Jin Hongeecb1e02011-10-21 14:36:32 -0700347 if (chip && chip->irq_unmask)
Trilok Sonieecb28c2011-07-20 16:24:14 +0100348 chip->irq_unmask(irq_get_irq_data(clock->irq));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700349 if (clock != &msm_clocks[MSM_CLOCK_GPT])
350 __raw_writel(TIMER_ENABLE_EN,
351 msm_clocks[MSM_CLOCK_GPT].regbase +
352 TIMER_ENABLE);
353 break;
354 case CLOCK_EVT_MODE_UNUSED:
355 case CLOCK_EVT_MODE_SHUTDOWN:
356 get_cpu_var(msm_active_clock) = NULL;
357 put_cpu_var(msm_active_clock);
358 clock_state->in_sync = 0;
359 clock_state->stopped = 1;
360 clock_state->stopped_tick =
361 msm_read_timer_count(clock, LOCAL_TIMER) +
362 clock_state->sleep_offset;
363 __raw_writel(0, clock->regbase + TIMER_MATCH_VAL);
Trilok Sonieecb28c2011-07-20 16:24:14 +0100364 chip = irq_get_chip(clock->irq);
Jin Hongeecb1e02011-10-21 14:36:32 -0700365 if (chip && chip->irq_mask)
Trilok Sonieecb28c2011-07-20 16:24:14 +0100366 chip->irq_mask(irq_get_irq_data(clock->irq));
Taniya Das36057be2011-10-28 13:02:17 +0530367
368 if (!is_smp() || clock != &msm_clocks[MSM_CLOCK_DGT]
369 || smp_processor_id())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700370 __raw_writel(0, clock->regbase + TIMER_ENABLE);
Taniya Das36057be2011-10-28 13:02:17 +0530371
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700372 if (clock != &msm_clocks[MSM_CLOCK_GPT]) {
373 gpt_state->in_sync = 0;
374 __raw_writel(0, msm_clocks[MSM_CLOCK_GPT].regbase +
375 TIMER_ENABLE);
376 }
377 break;
378 }
379 wmb();
380 local_irq_restore(irq_flags);
381}
382
Jeff Ohlstein973871d2011-09-28 11:46:26 -0700383void __iomem *msm_timer_get_timer0_base(void)
384{
385 return MSM_TMR_BASE + global_timer_offset;
386}
387
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700388#define MPM_SCLK_COUNT_VAL 0x0024
389
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700390#ifdef CONFIG_PM
391/*
392 * Retrieve the cycle count from sclk and optionally synchronize local clock
393 * with the sclk value.
394 *
395 * time_start and time_expired are callbacks that must be specified. The
396 * protocol uses them to detect timeout. The update callback is optional.
397 * If not NULL, update will be called so that it can update local clock.
398 *
399 * The function does not use the argument data directly; it passes data to
400 * the callbacks.
401 *
402 * Return value:
403 * 0: the operation failed
404 * >0: the slow clock value after time-sync
405 */
406static void (*msm_timer_sync_timeout)(void);
407#if defined(CONFIG_MSM_DIRECT_SCLK_ACCESS)
Jeff Ohlsteinecefdc02012-01-13 12:37:44 -0800408uint32_t msm_timer_get_sclk_ticks(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700409{
410 uint32_t t1, t2;
411 int loop_count = 10;
412 int loop_zero_count = 3;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700413 int tmp = USEC_PER_SEC;
414 do_div(tmp, sclk_hz);
415 tmp /= (loop_zero_count-1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700416
417 while (loop_zero_count--) {
418 t1 = __raw_readl(MSM_RPM_MPM_BASE + MPM_SCLK_COUNT_VAL);
419 do {
420 udelay(1);
421 t2 = t1;
422 t1 = __raw_readl(MSM_RPM_MPM_BASE + MPM_SCLK_COUNT_VAL);
423 } while ((t2 != t1) && --loop_count);
424
425 if (!loop_count) {
426 printk(KERN_EMERG "SCLK did not stabilize\n");
427 return 0;
428 }
429
430 if (t1)
431 break;
432
433 udelay(tmp);
434 }
435
436 if (!loop_zero_count) {
437 printk(KERN_EMERG "SCLK reads zero\n");
438 return 0;
439 }
440
Jeff Ohlsteinecefdc02012-01-13 12:37:44 -0800441 return t1;
442}
443
444static uint32_t msm_timer_do_sync_to_sclk(
445 void (*time_start)(struct msm_timer_sync_data_t *data),
446 bool (*time_expired)(struct msm_timer_sync_data_t *data),
447 void (*update)(struct msm_timer_sync_data_t *, uint32_t, uint32_t),
448 struct msm_timer_sync_data_t *data)
449{
450 unsigned t1 = msm_timer_get_sclk_ticks();
451
452 if (t1 && update != NULL)
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700453 update(data, t1, sclk_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700454 return t1;
455}
456#elif defined(CONFIG_MSM_N_WAY_SMSM)
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700457
458/* Time Master State Bits */
459#define MASTER_BITS_PER_CPU 1
460#define MASTER_TIME_PENDING \
461 (0x01UL << (MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
462
463/* Time Slave State Bits */
464#define SLAVE_TIME_REQUEST 0x0400
465#define SLAVE_TIME_POLL 0x0800
466#define SLAVE_TIME_INIT 0x1000
467
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700468static uint32_t msm_timer_do_sync_to_sclk(
469 void (*time_start)(struct msm_timer_sync_data_t *data),
470 bool (*time_expired)(struct msm_timer_sync_data_t *data),
471 void (*update)(struct msm_timer_sync_data_t *, uint32_t, uint32_t),
472 struct msm_timer_sync_data_t *data)
473{
474 uint32_t *smem_clock;
475 uint32_t smem_clock_val;
476 uint32_t state;
477
478 smem_clock = smem_alloc(SMEM_SMEM_SLOW_CLOCK_VALUE, sizeof(uint32_t));
479 if (smem_clock == NULL) {
480 printk(KERN_ERR "no smem clock\n");
481 return 0;
482 }
483
484 state = smsm_get_state(SMSM_MODEM_STATE);
485 if ((state & SMSM_INIT) == 0) {
486 printk(KERN_ERR "smsm not initialized\n");
487 return 0;
488 }
489
490 time_start(data);
491 while ((state = smsm_get_state(SMSM_TIME_MASTER_DEM)) &
492 MASTER_TIME_PENDING) {
493 if (time_expired(data)) {
494 printk(KERN_EMERG "get_smem_clock: timeout 1 still "
495 "invalid state %x\n", state);
496 msm_timer_sync_timeout();
497 }
498 }
499
500 smsm_change_state(SMSM_APPS_DEM, SLAVE_TIME_POLL | SLAVE_TIME_INIT,
501 SLAVE_TIME_REQUEST);
502
503 time_start(data);
504 while (!((state = smsm_get_state(SMSM_TIME_MASTER_DEM)) &
505 MASTER_TIME_PENDING)) {
506 if (time_expired(data)) {
507 printk(KERN_EMERG "get_smem_clock: timeout 2 still "
508 "invalid state %x\n", state);
509 msm_timer_sync_timeout();
510 }
511 }
512
513 smsm_change_state(SMSM_APPS_DEM, SLAVE_TIME_REQUEST, SLAVE_TIME_POLL);
514
515 time_start(data);
516 do {
517 smem_clock_val = *smem_clock;
518 } while (smem_clock_val == 0 && !time_expired(data));
519
520 state = smsm_get_state(SMSM_TIME_MASTER_DEM);
521
522 if (smem_clock_val) {
523 if (update != NULL)
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700524 update(data, smem_clock_val, sclk_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700525
526 if (msm_timer_debug_mask & MSM_TIMER_DEBUG_SYNC)
527 printk(KERN_INFO
528 "get_smem_clock: state %x clock %u\n",
529 state, smem_clock_val);
530 } else {
531 printk(KERN_EMERG
532 "get_smem_clock: timeout state %x clock %u\n",
533 state, smem_clock_val);
534 msm_timer_sync_timeout();
535 }
536
537 smsm_change_state(SMSM_APPS_DEM, SLAVE_TIME_REQUEST | SLAVE_TIME_POLL,
538 SLAVE_TIME_INIT);
539 return smem_clock_val;
540}
541#else /* CONFIG_MSM_N_WAY_SMSM */
542static uint32_t msm_timer_do_sync_to_sclk(
543 void (*time_start)(struct msm_timer_sync_data_t *data),
544 bool (*time_expired)(struct msm_timer_sync_data_t *data),
545 void (*update)(struct msm_timer_sync_data_t *, uint32_t, uint32_t),
546 struct msm_timer_sync_data_t *data)
547{
548 uint32_t *smem_clock;
549 uint32_t smem_clock_val;
550 uint32_t last_state;
551 uint32_t state;
552
553 smem_clock = smem_alloc(SMEM_SMEM_SLOW_CLOCK_VALUE,
554 sizeof(uint32_t));
555
556 if (smem_clock == NULL) {
557 printk(KERN_ERR "no smem clock\n");
558 return 0;
559 }
560
561 last_state = state = smsm_get_state(SMSM_MODEM_STATE);
562 smem_clock_val = *smem_clock;
563 if (smem_clock_val) {
564 printk(KERN_INFO "get_smem_clock: invalid start state %x "
565 "clock %u\n", state, smem_clock_val);
566 smsm_change_state(SMSM_APPS_STATE,
567 SMSM_TIMEWAIT, SMSM_TIMEINIT);
568
569 time_start(data);
570 while (*smem_clock != 0 && !time_expired(data))
571 ;
572
573 smem_clock_val = *smem_clock;
574 if (smem_clock_val) {
575 printk(KERN_EMERG "get_smem_clock: timeout still "
576 "invalid state %x clock %u\n",
577 state, smem_clock_val);
578 msm_timer_sync_timeout();
579 }
580 }
581
582 time_start(data);
583 smsm_change_state(SMSM_APPS_STATE, SMSM_TIMEINIT, SMSM_TIMEWAIT);
584 do {
585 smem_clock_val = *smem_clock;
586 state = smsm_get_state(SMSM_MODEM_STATE);
587 if (state != last_state) {
588 last_state = state;
589 if (msm_timer_debug_mask & MSM_TIMER_DEBUG_SYNC)
590 printk(KERN_INFO
591 "get_smem_clock: state %x clock %u\n",
592 state, smem_clock_val);
593 }
594 } while (smem_clock_val == 0 && !time_expired(data));
595
596 if (smem_clock_val) {
597 if (update != NULL)
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700598 update(data, smem_clock_val, sclk_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700599 } else {
600 printk(KERN_EMERG
601 "get_smem_clock: timeout state %x clock %u\n",
602 state, smem_clock_val);
603 msm_timer_sync_timeout();
604 }
605
606 smsm_change_state(SMSM_APPS_STATE, SMSM_TIMEWAIT, SMSM_TIMEINIT);
607 return smem_clock_val;
608}
609#endif /* CONFIG_MSM_N_WAY_SMSM */
610
611/*
612 * Callback function that initializes the timeout value.
613 */
614static void msm_timer_sync_to_sclk_time_start(
615 struct msm_timer_sync_data_t *data)
616{
617 /* approx 2 seconds */
618 uint32_t delta = data->clock->freq << data->clock->shift << 1;
619 data->timeout = msm_read_timer_count(data->clock, LOCAL_TIMER) + delta;
620}
621
622/*
623 * Callback function that checks the timeout.
624 */
625static bool msm_timer_sync_to_sclk_time_expired(
626 struct msm_timer_sync_data_t *data)
627{
628 uint32_t delta = msm_read_timer_count(data->clock, LOCAL_TIMER) -
629 data->timeout;
630 return ((int32_t) delta) > 0;
631}
632
633/*
634 * Callback function that updates local clock from the specified source clock
635 * value and frequency.
636 */
637static void msm_timer_sync_update(struct msm_timer_sync_data_t *data,
638 uint32_t src_clk_val, uint32_t src_clk_freq)
639{
640 struct msm_clock *dst_clk = data->clock;
641 struct msm_clock_percpu_data *dst_clk_state =
642 &__get_cpu_var(msm_clocks_percpu)[dst_clk->index];
643 uint32_t dst_clk_val = msm_read_timer_count(dst_clk, LOCAL_TIMER);
644 uint32_t new_offset;
645
646 if ((dst_clk->freq << dst_clk->shift) == src_clk_freq) {
647 new_offset = src_clk_val - dst_clk_val;
648 } else {
649 uint64_t temp;
650
651 /* separate multiplication and division steps to reduce
652 rounding error */
653 temp = src_clk_val;
654 temp *= dst_clk->freq << dst_clk->shift;
655 do_div(temp, src_clk_freq);
656
657 new_offset = (uint32_t)(temp) - dst_clk_val;
658 }
659
660 if (dst_clk_state->sleep_offset + dst_clk_state->non_sleep_offset !=
661 new_offset) {
662 if (data->exit_sleep)
663 dst_clk_state->sleep_offset =
664 new_offset - dst_clk_state->non_sleep_offset;
665 else
666 dst_clk_state->non_sleep_offset =
667 new_offset - dst_clk_state->sleep_offset;
668
669 if (msm_timer_debug_mask & MSM_TIMER_DEBUG_SYNC)
670 printk(KERN_INFO "sync clock %s: "
671 "src %u, new offset %u + %u\n",
672 dst_clk->clocksource.name, src_clk_val,
673 dst_clk_state->sleep_offset,
674 dst_clk_state->non_sleep_offset);
675 }
676}
677
678/*
679 * Synchronize GPT clock with sclk.
680 */
681static void msm_timer_sync_gpt_to_sclk(int exit_sleep)
682{
683 struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT];
684 struct msm_clock_percpu_data *gpt_clk_state =
685 &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT];
686 struct msm_timer_sync_data_t data;
687 uint32_t ret;
688
689 if (gpt_clk_state->in_sync)
690 return;
691
692 data.clock = gpt_clk;
693 data.timeout = 0;
694 data.exit_sleep = exit_sleep;
695
696 ret = msm_timer_do_sync_to_sclk(
697 msm_timer_sync_to_sclk_time_start,
698 msm_timer_sync_to_sclk_time_expired,
699 msm_timer_sync_update,
700 &data);
701
702 if (ret)
703 gpt_clk_state->in_sync = 1;
704}
705
706/*
707 * Synchronize clock with GPT clock.
708 */
709static void msm_timer_sync_to_gpt(struct msm_clock *clock, int exit_sleep)
710{
711 struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT];
712 struct msm_clock_percpu_data *gpt_clk_state =
713 &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT];
714 struct msm_clock_percpu_data *clock_state =
715 &__get_cpu_var(msm_clocks_percpu)[clock->index];
716 struct msm_timer_sync_data_t data;
717 uint32_t gpt_clk_val;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700718 u64 gpt_period = (1ULL << 32) * HZ;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700719 u64 now = get_jiffies_64();
720
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700721 do_div(gpt_period, gpt_hz);
722
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700723 BUG_ON(clock == gpt_clk);
724
725 if (clock_state->in_sync &&
726 (now - clock_state->last_sync_jiffies < (gpt_period >> 1)))
727 return;
728
729 gpt_clk_val = msm_read_timer_count(gpt_clk, LOCAL_TIMER)
730 + gpt_clk_state->sleep_offset + gpt_clk_state->non_sleep_offset;
731
732 if (exit_sleep && gpt_clk_val < clock_state->last_sync_gpt)
733 clock_state->non_sleep_offset -= clock->rollover_offset;
734
735 data.clock = clock;
736 data.timeout = 0;
737 data.exit_sleep = exit_sleep;
738
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700739 msm_timer_sync_update(&data, gpt_clk_val, gpt_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700740
741 clock_state->in_sync = 1;
742 clock_state->last_sync_gpt = gpt_clk_val;
743 clock_state->last_sync_jiffies = now;
744}
745
746static void msm_timer_reactivate_alarm(struct msm_clock *clock)
747{
748 struct msm_clock_percpu_data *clock_state =
749 &__get_cpu_var(msm_clocks_percpu)[clock->index];
750 long alarm_delta = clock_state->alarm_vtime -
751 clock_state->sleep_offset -
752 msm_read_timer_count(clock, LOCAL_TIMER);
753 alarm_delta >>= clock->shift;
754 if (alarm_delta < (long)clock->write_delay + 4)
755 alarm_delta = clock->write_delay + 4;
756 while (msm_timer_set_next_event(alarm_delta, &clock->clockevent))
757 ;
758}
759
760int64_t msm_timer_enter_idle(void)
761{
762 struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT];
763 struct msm_clock *clock = __get_cpu_var(msm_active_clock);
764 struct msm_clock_percpu_data *clock_state =
765 &__get_cpu_var(msm_clocks_percpu)[clock->index];
766 uint32_t alarm;
767 uint32_t count;
768 int32_t delta;
769
770 BUG_ON(clock != &msm_clocks[MSM_CLOCK_GPT] &&
771 clock != &msm_clocks[MSM_CLOCK_DGT]);
772
773 msm_timer_sync_gpt_to_sclk(0);
774 if (clock != gpt_clk)
775 msm_timer_sync_to_gpt(clock, 0);
776
777 count = msm_read_timer_count(clock, LOCAL_TIMER);
778 if (clock_state->stopped++ == 0)
779 clock_state->stopped_tick = count + clock_state->sleep_offset;
780 alarm = clock_state->alarm;
781 delta = alarm - count;
782 if (delta <= -(int32_t)((clock->freq << clock->shift) >> 10)) {
783 /* timer should have triggered 1ms ago */
784 printk(KERN_ERR "msm_timer_enter_idle: timer late %d, "
785 "reprogram it\n", delta);
786 msm_timer_reactivate_alarm(clock);
787 }
788 if (delta <= 0)
789 return 0;
790 return clocksource_cyc2ns((alarm - count) >> clock->shift,
791 clock->clocksource.mult,
792 clock->clocksource.shift);
793}
794
795void msm_timer_exit_idle(int low_power)
796{
797 struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT];
798 struct msm_clock *clock = __get_cpu_var(msm_active_clock);
799 struct msm_clock_percpu_data *gpt_clk_state =
800 &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT];
801 struct msm_clock_percpu_data *clock_state =
802 &__get_cpu_var(msm_clocks_percpu)[clock->index];
803 uint32_t enabled;
804
805 BUG_ON(clock != &msm_clocks[MSM_CLOCK_GPT] &&
806 clock != &msm_clocks[MSM_CLOCK_DGT]);
807
808 if (!low_power)
809 goto exit_idle_exit;
810
811 enabled = __raw_readl(gpt_clk->regbase + TIMER_ENABLE) &
812 TIMER_ENABLE_EN;
813 if (!enabled)
814 __raw_writel(TIMER_ENABLE_EN, gpt_clk->regbase + TIMER_ENABLE);
815
816#if defined(CONFIG_ARCH_MSM_SCORPION) || defined(CONFIG_ARCH_MSM_KRAIT)
817 gpt_clk_state->in_sync = 0;
818#else
819 gpt_clk_state->in_sync = gpt_clk_state->in_sync && enabled;
820#endif
821 /* Make sure timer is actually enabled before we sync it */
822 wmb();
823 msm_timer_sync_gpt_to_sclk(1);
824
825 if (clock == gpt_clk)
826 goto exit_idle_alarm;
827
828 enabled = __raw_readl(clock->regbase + TIMER_ENABLE) & TIMER_ENABLE_EN;
829 if (!enabled)
830 __raw_writel(TIMER_ENABLE_EN, clock->regbase + TIMER_ENABLE);
831
832#if defined(CONFIG_ARCH_MSM_SCORPION) || defined(CONFIG_ARCH_MSM_KRAIT)
833 clock_state->in_sync = 0;
834#else
835 clock_state->in_sync = clock_state->in_sync && enabled;
836#endif
837 /* Make sure timer is actually enabled before we sync it */
838 wmb();
839 msm_timer_sync_to_gpt(clock, 1);
840
841exit_idle_alarm:
842 msm_timer_reactivate_alarm(clock);
843
844exit_idle_exit:
845 clock_state->stopped--;
846}
847
848/*
849 * Callback function that initializes the timeout value.
850 */
851static void msm_timer_get_sclk_time_start(
852 struct msm_timer_sync_data_t *data)
853{
854 data->timeout = 200000;
855}
856
857/*
858 * Callback function that checks the timeout.
859 */
860static bool msm_timer_get_sclk_time_expired(
861 struct msm_timer_sync_data_t *data)
862{
863 udelay(10);
864 return --data->timeout <= 0;
865}
866
867/*
868 * Retrieve the cycle count from the sclk and convert it into
869 * nanoseconds.
870 *
871 * On exit, if period is not NULL, it contains the period of the
872 * sclk in nanoseconds, i.e. how long the cycle count wraps around.
873 *
874 * Return value:
875 * 0: the operation failed; period is not set either
876 * >0: time in nanoseconds
877 */
878int64_t msm_timer_get_sclk_time(int64_t *period)
879{
880 struct msm_timer_sync_data_t data;
881 uint32_t clock_value;
882 int64_t tmp;
883
884 memset(&data, 0, sizeof(data));
885 clock_value = msm_timer_do_sync_to_sclk(
886 msm_timer_get_sclk_time_start,
887 msm_timer_get_sclk_time_expired,
888 NULL,
889 &data);
890
891 if (!clock_value)
892 return 0;
893
894 if (period) {
895 tmp = 1LL << 32;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700896 tmp *= NSEC_PER_SEC;
897 do_div(tmp, sclk_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700898 *period = tmp;
899 }
900
901 tmp = (int64_t)clock_value;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700902 tmp *= NSEC_PER_SEC;
903 do_div(tmp, sclk_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700904 return tmp;
905}
906
907int __init msm_timer_init_time_sync(void (*timeout)(void))
908{
909#if defined(CONFIG_MSM_N_WAY_SMSM) && !defined(CONFIG_MSM_DIRECT_SCLK_ACCESS)
910 int ret = smsm_change_intr_mask(SMSM_TIME_MASTER_DEM, 0xFFFFFFFF, 0);
911
912 if (ret) {
913 printk(KERN_ERR "%s: failed to clear interrupt mask, %d\n",
914 __func__, ret);
915 return ret;
916 }
917
918 smsm_change_state(SMSM_APPS_DEM,
919 SLAVE_TIME_REQUEST | SLAVE_TIME_POLL, SLAVE_TIME_INIT);
920#endif
921
922 BUG_ON(timeout == NULL);
923 msm_timer_sync_timeout = timeout;
924
925 return 0;
926}
927
928#endif
929
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700930static DEFINE_CLOCK_DATA(cd);
931
Vikram Mulukutlaa41f3a12011-10-31 14:20:50 -0700932/*
933 * Store the most recent timestamp read from hardware
934 * in last_ns. This is useful for debugging crashes.
935 */
Jeff Ohlstein06658f72011-11-09 13:51:11 -0800936static atomic64_t last_ns;
Vikram Mulukutlaa41f3a12011-10-31 14:20:50 -0700937
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700938unsigned long long notrace sched_clock(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700939{
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700940 struct msm_clock *clock = &msm_clocks[msm_global_timer];
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700941 struct clocksource *cs = &clock->clocksource;
Jeff Ohlstein06658f72011-11-09 13:51:11 -0800942 u64 cyc = cs->read(cs);
943 u64 last_ns_local;
944 last_ns_local = cyc_to_sched_clock(&cd, cyc, ((u32)~0 >> clock->shift));
945 atomic64_set(&last_ns, last_ns_local);
946 return last_ns_local;
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700947}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700948
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700949static void notrace msm_update_sched_clock(void)
950{
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700951 struct msm_clock *clock = &msm_clocks[msm_global_timer];
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700952 struct clocksource *cs = &clock->clocksource;
953 u32 cyc = cs->read(cs);
954 update_sched_clock(&cd, cyc, ((u32)~0) >> clock->shift);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700955}
956
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700957int read_current_timer(unsigned long *timer_val)
958{
959 struct msm_clock *dgt = &msm_clocks[MSM_CLOCK_DGT];
960 *timer_val = msm_read_timer_count(dgt, GLOBAL_TIMER);
961 return 0;
962}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700963
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700964static void __init msm_sched_clock_init(void)
965{
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700966 struct msm_clock *clock = &msm_clocks[msm_global_timer];
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700967
968 init_sched_clock(&cd, msm_update_sched_clock, 32 - clock->shift,
969 clock->freq);
970}
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800971static void __init msm_timer_init(void)
972{
973 int i;
974 int res;
Jin Hongeecb1e02011-10-21 14:36:32 -0700975 struct irq_chip *chip;
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700976 struct msm_clock *dgt = &msm_clocks[MSM_CLOCK_DGT];
977 struct msm_clock *gpt = &msm_clocks[MSM_CLOCK_GPT];
David Brown8c27e6f2011-01-07 10:20:49 -0800978
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700979 if (cpu_is_msm7x01() || cpu_is_msm7x25() || cpu_is_msm7x27() ||
980 cpu_is_msm7x25a() || cpu_is_msm7x27a() || cpu_is_msm7x25aa() ||
Taniya Das5eb25142011-11-17 21:53:34 +0530981 cpu_is_msm7x27aa() || cpu_is_msm8625()) {
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700982 dgt->shift = MSM_DGT_SHIFT;
983 dgt->freq = 19200000 >> MSM_DGT_SHIFT;
984 dgt->clockevent.shift = 32 + MSM_DGT_SHIFT;
985 dgt->clocksource.mask = CLOCKSOURCE_MASK(32 - MSM_DGT_SHIFT);
986 dgt->clocksource.shift = 24 - MSM_DGT_SHIFT;
987 gpt->regbase = MSM_TMR_BASE;
988 dgt->regbase = MSM_TMR_BASE + 0x10;
Jeff Ohlstein5d90e252011-11-04 19:00:50 -0700989 gpt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT
990 | MSM_CLOCK_FLAGS_ODD_MATCH_WRITE
991 | MSM_CLOCK_FLAGS_DELAYED_WRITE_POST;
Taniya Das5eb25142011-11-17 21:53:34 +0530992 if (cpu_is_msm8625()) {
993 dgt->irq = MSM8625_INT_DEBUG_TIMER_EXP;
994 gpt->irq = MSM8625_INT_GP_TIMER_EXP;
995 global_timer_offset = MSM_TMR0_BASE - MSM_TMR_BASE;
996 }
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700997 } else if (cpu_is_qsd8x50()) {
998 dgt->freq = 4800000;
999 gpt->regbase = MSM_TMR_BASE;
1000 dgt->regbase = MSM_TMR_BASE + 0x10;
1001 } else if (cpu_is_fsm9xxx())
1002 dgt->freq = 4800000;
1003 else if (cpu_is_msm7x30() || cpu_is_msm8x55())
1004 dgt->freq = 6144000;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -07001005 else if (cpu_is_msm8x60()) {
Jeff Ohlstein7e538f02011-11-01 17:36:22 -07001006 global_timer_offset = MSM_TMR0_BASE - MSM_TMR_BASE;
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001007 dgt->freq = 6750000;
1008 __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
Jeff Ohlstein7e538f02011-11-01 17:36:22 -07001009 } else if (cpu_is_msm9615()) {
1010 dgt->freq = 6750000;
1011 __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
1012 gpt->freq = 32765;
1013 gpt_hz = 32765;
1014 sclk_hz = 32765;
Jeff Ohlsteind47f96a2011-11-04 19:00:50 -07001015 gpt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT;
1016 dgt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT;
Jeff Ohlstein7e538f02011-11-01 17:36:22 -07001017 } else if (cpu_is_msm8960() || cpu_is_apq8064() || cpu_is_msm8930()) {
1018 global_timer_offset = MSM_TMR0_BASE - MSM_TMR_BASE;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -07001019 dgt->freq = 6750000;
1020 __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
1021 gpt->freq = 32765;
1022 gpt_hz = 32765;
1023 sclk_hz = 32765;
Jeff Ohlstein391a3ee2011-12-01 16:44:45 -08001024 if (!machine_is_apq8064_rumi3()) {
1025 gpt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT;
1026 dgt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT;
1027 }
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001028 } else {
Jeff Ohlsteinf0a31e42012-01-06 19:03:05 -08001029 WARN(1, "Timer running on unknown hardware. Configure this! "
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001030 "Assuming default configuration.\n");
1031 dgt->freq = 6750000;
1032 }
1033
1034 if (msm_clocks[MSM_CLOCK_GPT].clocksource.rating > DG_TIMER_RATING)
1035 msm_global_timer = MSM_CLOCK_GPT;
1036 else
1037 msm_global_timer = MSM_CLOCK_DGT;
Jeff Ohlstein672039f2010-10-05 15:23:57 -07001038
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001039 for (i = 0; i < ARRAY_SIZE(msm_clocks); i++) {
1040 struct msm_clock *clock = &msm_clocks[i];
1041 struct clock_event_device *ce = &clock->clockevent;
1042 struct clocksource *cs = &clock->clocksource;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001043 __raw_writel(0, clock->regbase + TIMER_ENABLE);
1044 __raw_writel(1, clock->regbase + TIMER_CLEAR);
1045 __raw_writel(0, clock->regbase + TIMER_COUNT_VAL);
1046 __raw_writel(~0, clock->regbase + TIMER_MATCH_VAL);
David Brown8c27e6f2011-01-07 10:20:49 -08001047
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -07001048 if ((clock->freq << clock->shift) == gpt_hz) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001049 clock->rollover_offset = 0;
1050 } else {
1051 uint64_t temp;
David Brown8c27e6f2011-01-07 10:20:49 -08001052
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001053 temp = clock->freq << clock->shift;
1054 temp <<= 32;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -07001055 do_div(temp, gpt_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001056
1057 clock->rollover_offset = (uint32_t) temp;
1058 }
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001059
1060 ce->mult = div_sc(clock->freq, NSEC_PER_SEC, ce->shift);
1061 /* allow at least 10 seconds to notice that the timer wrapped */
1062 ce->max_delta_ns =
1063 clockevent_delta2ns(0xf0000000 >> clock->shift, ce);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001064 /* ticks gets rounded down by one */
1065 ce->min_delta_ns =
1066 clockevent_delta2ns(clock->write_delay + 4, ce);
Rusty Russell320ab2b2008-12-13 21:20:26 +10301067 ce->cpumask = cpumask_of(0);
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001068
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001069 cs->mult = clocksource_hz2mult(clock->freq, cs->shift);
1070 res = clocksource_register(cs);
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001071 if (res)
1072 printk(KERN_ERR "msm_timer_init: clocksource_register "
1073 "failed for %s\n", cs->name);
1074
Trilok Sonieecb28c2011-07-20 16:24:14 +01001075 ce->irq = clock->irq;
1076 if (cpu_is_msm8x60() || cpu_is_msm8960() || cpu_is_apq8064() ||
Taniya Das5eb25142011-11-17 21:53:34 +05301077 cpu_is_msm8930() || cpu_is_msm9615() ||
1078 cpu_is_msm8625()) {
Trilok Sonieecb28c2011-07-20 16:24:14 +01001079 clock->percpu_evt = alloc_percpu(struct clock_event_device *);
1080 if (!clock->percpu_evt) {
1081 pr_err("msm_timer_init: memory allocation "
1082 "failed for %s\n", ce->name);
1083 continue;
1084 }
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001085
Trilok Sonieecb28c2011-07-20 16:24:14 +01001086 *__this_cpu_ptr(clock->percpu_evt) = ce;
1087 res = request_percpu_irq(ce->irq, msm_timer_interrupt,
1088 ce->name, clock->percpu_evt);
1089 if (!res)
1090 enable_percpu_irq(ce->irq, 0);
1091 } else {
1092 clock->evt = ce;
1093 res = request_irq(ce->irq, msm_timer_interrupt,
1094 IRQF_TIMER | IRQF_NOBALANCING | IRQF_TRIGGER_RISING,
1095 ce->name, &clock->evt);
1096 }
1097
1098 if (res)
1099 pr_err("msm_timer_init: request_irq failed for %s\n",
1100 ce->name);
1101
1102 chip = irq_get_chip(clock->irq);
Jin Hongeecb1e02011-10-21 14:36:32 -07001103 if (chip && chip->irq_mask)
Trilok Sonieecb28c2011-07-20 16:24:14 +01001104 chip->irq_mask(irq_get_irq_data(clock->irq));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001105
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001106 clockevents_register_device(ce);
1107 }
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -07001108 msm_sched_clock_init();
Taniya Das36057be2011-10-28 13:02:17 +05301109
1110 if (is_smp()) {
1111 __raw_writel(1,
1112 msm_clocks[MSM_CLOCK_DGT].regbase + TIMER_ENABLE);
1113 set_delay_fn(read_current_timer_delay_loop);
1114 }
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001115}
1116
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001117#ifdef CONFIG_SMP
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001118
Santosh Shilimkaraf90f102011-02-23 18:53:15 +01001119int __cpuinit local_timer_setup(struct clock_event_device *evt)
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001120{
Jeff Ohlsteind17ee762011-09-26 19:44:22 -07001121 static DEFINE_PER_CPU(bool, first_boot) = true;
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001122 struct msm_clock *clock = &msm_clocks[msm_global_timer];
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001123
1124 /* Use existing clock_event for cpu 0 */
1125 if (!smp_processor_id())
David Brown893b66c2011-03-30 11:26:57 -07001126 return 0;
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001127
Taniya Das36057be2011-10-28 13:02:17 +05301128 if (cpu_is_msm8x60() || cpu_is_msm8960() || cpu_is_apq8064()
1129 || cpu_is_msm8930())
1130 __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001131
Jeff Ohlsteind17ee762011-09-26 19:44:22 -07001132 if (__get_cpu_var(first_boot)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001133 __raw_writel(0, clock->regbase + TIMER_ENABLE);
1134 __raw_writel(0, clock->regbase + TIMER_CLEAR);
1135 __raw_writel(~0, clock->regbase + TIMER_MATCH_VAL);
Jeff Ohlsteind17ee762011-09-26 19:44:22 -07001136 __get_cpu_var(first_boot) = false;
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001137 }
Trilok Sonieecb28c2011-07-20 16:24:14 +01001138 evt->irq = clock->irq;
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001139 evt->name = "local_timer";
1140 evt->features = CLOCK_EVT_FEAT_ONESHOT;
1141 evt->rating = clock->clockevent.rating;
1142 evt->set_mode = msm_timer_set_mode;
1143 evt->set_next_event = msm_timer_set_next_event;
1144 evt->shift = clock->clockevent.shift;
1145 evt->mult = div_sc(clock->freq, NSEC_PER_SEC, evt->shift);
1146 evt->max_delta_ns =
1147 clockevent_delta2ns(0xf0000000 >> clock->shift, evt);
1148 evt->min_delta_ns = clockevent_delta2ns(4, evt);
1149
Trilok Sonieecb28c2011-07-20 16:24:14 +01001150 *__this_cpu_ptr(clock->percpu_evt) = evt;
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001151
1152 clockevents_register_device(evt);
Trilok Sonieecb28c2011-07-20 16:24:14 +01001153 enable_percpu_irq(evt->irq, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001154
Santosh Shilimkaraf90f102011-02-23 18:53:15 +01001155 return 0;
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001156}
1157
Trilok Sonieecb28c2011-07-20 16:24:14 +01001158void local_timer_stop(struct clock_event_device *evt)
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001159{
Trilok Sonieecb28c2011-07-20 16:24:14 +01001160 evt->set_mode(CLOCK_EVT_MODE_UNUSED, evt);
1161 disable_percpu_irq(evt->irq);
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001162}
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001163#endif
1164
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001165struct sys_timer msm_timer = {
1166 .init = msm_timer_init
1167};