blob: 67b1f7baef954da7662a00d1a1a5568c42993232 [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() ||
981 cpu_is_msm7x27aa()) {
982 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;
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700992 } else if (cpu_is_qsd8x50()) {
993 dgt->freq = 4800000;
994 gpt->regbase = MSM_TMR_BASE;
995 dgt->regbase = MSM_TMR_BASE + 0x10;
996 } else if (cpu_is_fsm9xxx())
997 dgt->freq = 4800000;
998 else if (cpu_is_msm7x30() || cpu_is_msm8x55())
999 dgt->freq = 6144000;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -07001000 else if (cpu_is_msm8x60()) {
Jeff Ohlstein7e538f02011-11-01 17:36:22 -07001001 global_timer_offset = MSM_TMR0_BASE - MSM_TMR_BASE;
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001002 dgt->freq = 6750000;
1003 __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
Jeff Ohlstein7e538f02011-11-01 17:36:22 -07001004 } else if (cpu_is_msm9615()) {
1005 dgt->freq = 6750000;
1006 __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
1007 gpt->freq = 32765;
1008 gpt_hz = 32765;
1009 sclk_hz = 32765;
Jeff Ohlsteind47f96a2011-11-04 19:00:50 -07001010 gpt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT;
1011 dgt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT;
Jeff Ohlstein7e538f02011-11-01 17:36:22 -07001012 } else if (cpu_is_msm8960() || cpu_is_apq8064() || cpu_is_msm8930()) {
1013 global_timer_offset = MSM_TMR0_BASE - MSM_TMR_BASE;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -07001014 dgt->freq = 6750000;
1015 __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
1016 gpt->freq = 32765;
1017 gpt_hz = 32765;
1018 sclk_hz = 32765;
Jeff Ohlstein391a3ee2011-12-01 16:44:45 -08001019 if (!machine_is_apq8064_rumi3()) {
1020 gpt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT;
1021 dgt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT;
1022 }
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001023 } else {
Jeff Ohlsteinf0a31e42012-01-06 19:03:05 -08001024 WARN(1, "Timer running on unknown hardware. Configure this! "
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001025 "Assuming default configuration.\n");
1026 dgt->freq = 6750000;
1027 }
1028
1029 if (msm_clocks[MSM_CLOCK_GPT].clocksource.rating > DG_TIMER_RATING)
1030 msm_global_timer = MSM_CLOCK_GPT;
1031 else
1032 msm_global_timer = MSM_CLOCK_DGT;
Jeff Ohlstein672039f2010-10-05 15:23:57 -07001033
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001034 for (i = 0; i < ARRAY_SIZE(msm_clocks); i++) {
1035 struct msm_clock *clock = &msm_clocks[i];
1036 struct clock_event_device *ce = &clock->clockevent;
1037 struct clocksource *cs = &clock->clocksource;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001038 __raw_writel(0, clock->regbase + TIMER_ENABLE);
1039 __raw_writel(1, clock->regbase + TIMER_CLEAR);
1040 __raw_writel(0, clock->regbase + TIMER_COUNT_VAL);
1041 __raw_writel(~0, clock->regbase + TIMER_MATCH_VAL);
David Brown8c27e6f2011-01-07 10:20:49 -08001042
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -07001043 if ((clock->freq << clock->shift) == gpt_hz) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001044 clock->rollover_offset = 0;
1045 } else {
1046 uint64_t temp;
David Brown8c27e6f2011-01-07 10:20:49 -08001047
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001048 temp = clock->freq << clock->shift;
1049 temp <<= 32;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -07001050 do_div(temp, gpt_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001051
1052 clock->rollover_offset = (uint32_t) temp;
1053 }
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001054
1055 ce->mult = div_sc(clock->freq, NSEC_PER_SEC, ce->shift);
1056 /* allow at least 10 seconds to notice that the timer wrapped */
1057 ce->max_delta_ns =
1058 clockevent_delta2ns(0xf0000000 >> clock->shift, ce);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001059 /* ticks gets rounded down by one */
1060 ce->min_delta_ns =
1061 clockevent_delta2ns(clock->write_delay + 4, ce);
Rusty Russell320ab2b2008-12-13 21:20:26 +10301062 ce->cpumask = cpumask_of(0);
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001063
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001064 cs->mult = clocksource_hz2mult(clock->freq, cs->shift);
1065 res = clocksource_register(cs);
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001066 if (res)
1067 printk(KERN_ERR "msm_timer_init: clocksource_register "
1068 "failed for %s\n", cs->name);
1069
Trilok Sonieecb28c2011-07-20 16:24:14 +01001070 ce->irq = clock->irq;
1071 if (cpu_is_msm8x60() || cpu_is_msm8960() || cpu_is_apq8064() ||
1072 cpu_is_msm8930() || cpu_is_msm9615()) {
1073 clock->percpu_evt = alloc_percpu(struct clock_event_device *);
1074 if (!clock->percpu_evt) {
1075 pr_err("msm_timer_init: memory allocation "
1076 "failed for %s\n", ce->name);
1077 continue;
1078 }
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001079
Trilok Sonieecb28c2011-07-20 16:24:14 +01001080 *__this_cpu_ptr(clock->percpu_evt) = ce;
1081 res = request_percpu_irq(ce->irq, msm_timer_interrupt,
1082 ce->name, clock->percpu_evt);
1083 if (!res)
1084 enable_percpu_irq(ce->irq, 0);
1085 } else {
1086 clock->evt = ce;
1087 res = request_irq(ce->irq, msm_timer_interrupt,
1088 IRQF_TIMER | IRQF_NOBALANCING | IRQF_TRIGGER_RISING,
1089 ce->name, &clock->evt);
1090 }
1091
1092 if (res)
1093 pr_err("msm_timer_init: request_irq failed for %s\n",
1094 ce->name);
1095
1096 chip = irq_get_chip(clock->irq);
Jin Hongeecb1e02011-10-21 14:36:32 -07001097 if (chip && chip->irq_mask)
Trilok Sonieecb28c2011-07-20 16:24:14 +01001098 chip->irq_mask(irq_get_irq_data(clock->irq));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001099
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001100 clockevents_register_device(ce);
1101 }
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -07001102 msm_sched_clock_init();
Taniya Das36057be2011-10-28 13:02:17 +05301103
1104 if (is_smp()) {
1105 __raw_writel(1,
1106 msm_clocks[MSM_CLOCK_DGT].regbase + TIMER_ENABLE);
1107 set_delay_fn(read_current_timer_delay_loop);
1108 }
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001109}
1110
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001111#ifdef CONFIG_SMP
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001112
Santosh Shilimkaraf90f102011-02-23 18:53:15 +01001113int __cpuinit local_timer_setup(struct clock_event_device *evt)
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001114{
Jeff Ohlsteind17ee762011-09-26 19:44:22 -07001115 static DEFINE_PER_CPU(bool, first_boot) = true;
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001116 struct msm_clock *clock = &msm_clocks[msm_global_timer];
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001117
1118 /* Use existing clock_event for cpu 0 */
1119 if (!smp_processor_id())
David Brown893b66c2011-03-30 11:26:57 -07001120 return 0;
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001121
Taniya Das36057be2011-10-28 13:02:17 +05301122 if (cpu_is_msm8x60() || cpu_is_msm8960() || cpu_is_apq8064()
1123 || cpu_is_msm8930())
1124 __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001125
Jeff Ohlsteind17ee762011-09-26 19:44:22 -07001126 if (__get_cpu_var(first_boot)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001127 __raw_writel(0, clock->regbase + TIMER_ENABLE);
1128 __raw_writel(0, clock->regbase + TIMER_CLEAR);
1129 __raw_writel(~0, clock->regbase + TIMER_MATCH_VAL);
Jeff Ohlsteind17ee762011-09-26 19:44:22 -07001130 __get_cpu_var(first_boot) = false;
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001131 }
Trilok Sonieecb28c2011-07-20 16:24:14 +01001132 evt->irq = clock->irq;
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001133 evt->name = "local_timer";
1134 evt->features = CLOCK_EVT_FEAT_ONESHOT;
1135 evt->rating = clock->clockevent.rating;
1136 evt->set_mode = msm_timer_set_mode;
1137 evt->set_next_event = msm_timer_set_next_event;
1138 evt->shift = clock->clockevent.shift;
1139 evt->mult = div_sc(clock->freq, NSEC_PER_SEC, evt->shift);
1140 evt->max_delta_ns =
1141 clockevent_delta2ns(0xf0000000 >> clock->shift, evt);
1142 evt->min_delta_ns = clockevent_delta2ns(4, evt);
1143
Trilok Sonieecb28c2011-07-20 16:24:14 +01001144 *__this_cpu_ptr(clock->percpu_evt) = evt;
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001145
1146 clockevents_register_device(evt);
Trilok Sonieecb28c2011-07-20 16:24:14 +01001147 enable_percpu_irq(evt->irq, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001148
Santosh Shilimkaraf90f102011-02-23 18:53:15 +01001149 return 0;
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001150}
1151
Trilok Sonieecb28c2011-07-20 16:24:14 +01001152void local_timer_stop(struct clock_event_device *evt)
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001153{
Trilok Sonieecb28c2011-07-20 16:24:14 +01001154 evt->set_mode(CLOCK_EVT_MODE_UNUSED, evt);
1155 disable_percpu_irq(evt->irq);
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001156}
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001157#endif
1158
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001159struct sys_timer msm_timer = {
1160 .init = msm_timer_init
1161};