blob: 6f044708601884867602a90d3962dd0c14a4649f [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>
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)
408static uint32_t msm_timer_do_sync_to_sclk(
409 void (*time_start)(struct msm_timer_sync_data_t *data),
410 bool (*time_expired)(struct msm_timer_sync_data_t *data),
411 void (*update)(struct msm_timer_sync_data_t *, uint32_t, uint32_t),
412 struct msm_timer_sync_data_t *data)
413{
414 uint32_t t1, t2;
415 int loop_count = 10;
416 int loop_zero_count = 3;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700417 int tmp = USEC_PER_SEC;
418 do_div(tmp, sclk_hz);
419 tmp /= (loop_zero_count-1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700420
421 while (loop_zero_count--) {
422 t1 = __raw_readl(MSM_RPM_MPM_BASE + MPM_SCLK_COUNT_VAL);
423 do {
424 udelay(1);
425 t2 = t1;
426 t1 = __raw_readl(MSM_RPM_MPM_BASE + MPM_SCLK_COUNT_VAL);
427 } while ((t2 != t1) && --loop_count);
428
429 if (!loop_count) {
430 printk(KERN_EMERG "SCLK did not stabilize\n");
431 return 0;
432 }
433
434 if (t1)
435 break;
436
437 udelay(tmp);
438 }
439
440 if (!loop_zero_count) {
441 printk(KERN_EMERG "SCLK reads zero\n");
442 return 0;
443 }
444
445 if (update != NULL)
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700446 update(data, t1, sclk_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700447 return t1;
448}
449#elif defined(CONFIG_MSM_N_WAY_SMSM)
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700450
451/* Time Master State Bits */
452#define MASTER_BITS_PER_CPU 1
453#define MASTER_TIME_PENDING \
454 (0x01UL << (MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
455
456/* Time Slave State Bits */
457#define SLAVE_TIME_REQUEST 0x0400
458#define SLAVE_TIME_POLL 0x0800
459#define SLAVE_TIME_INIT 0x1000
460
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700461static uint32_t msm_timer_do_sync_to_sclk(
462 void (*time_start)(struct msm_timer_sync_data_t *data),
463 bool (*time_expired)(struct msm_timer_sync_data_t *data),
464 void (*update)(struct msm_timer_sync_data_t *, uint32_t, uint32_t),
465 struct msm_timer_sync_data_t *data)
466{
467 uint32_t *smem_clock;
468 uint32_t smem_clock_val;
469 uint32_t state;
470
471 smem_clock = smem_alloc(SMEM_SMEM_SLOW_CLOCK_VALUE, sizeof(uint32_t));
472 if (smem_clock == NULL) {
473 printk(KERN_ERR "no smem clock\n");
474 return 0;
475 }
476
477 state = smsm_get_state(SMSM_MODEM_STATE);
478 if ((state & SMSM_INIT) == 0) {
479 printk(KERN_ERR "smsm not initialized\n");
480 return 0;
481 }
482
483 time_start(data);
484 while ((state = smsm_get_state(SMSM_TIME_MASTER_DEM)) &
485 MASTER_TIME_PENDING) {
486 if (time_expired(data)) {
487 printk(KERN_EMERG "get_smem_clock: timeout 1 still "
488 "invalid state %x\n", state);
489 msm_timer_sync_timeout();
490 }
491 }
492
493 smsm_change_state(SMSM_APPS_DEM, SLAVE_TIME_POLL | SLAVE_TIME_INIT,
494 SLAVE_TIME_REQUEST);
495
496 time_start(data);
497 while (!((state = smsm_get_state(SMSM_TIME_MASTER_DEM)) &
498 MASTER_TIME_PENDING)) {
499 if (time_expired(data)) {
500 printk(KERN_EMERG "get_smem_clock: timeout 2 still "
501 "invalid state %x\n", state);
502 msm_timer_sync_timeout();
503 }
504 }
505
506 smsm_change_state(SMSM_APPS_DEM, SLAVE_TIME_REQUEST, SLAVE_TIME_POLL);
507
508 time_start(data);
509 do {
510 smem_clock_val = *smem_clock;
511 } while (smem_clock_val == 0 && !time_expired(data));
512
513 state = smsm_get_state(SMSM_TIME_MASTER_DEM);
514
515 if (smem_clock_val) {
516 if (update != NULL)
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700517 update(data, smem_clock_val, sclk_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700518
519 if (msm_timer_debug_mask & MSM_TIMER_DEBUG_SYNC)
520 printk(KERN_INFO
521 "get_smem_clock: state %x clock %u\n",
522 state, smem_clock_val);
523 } else {
524 printk(KERN_EMERG
525 "get_smem_clock: timeout state %x clock %u\n",
526 state, smem_clock_val);
527 msm_timer_sync_timeout();
528 }
529
530 smsm_change_state(SMSM_APPS_DEM, SLAVE_TIME_REQUEST | SLAVE_TIME_POLL,
531 SLAVE_TIME_INIT);
532 return smem_clock_val;
533}
534#else /* CONFIG_MSM_N_WAY_SMSM */
535static uint32_t msm_timer_do_sync_to_sclk(
536 void (*time_start)(struct msm_timer_sync_data_t *data),
537 bool (*time_expired)(struct msm_timer_sync_data_t *data),
538 void (*update)(struct msm_timer_sync_data_t *, uint32_t, uint32_t),
539 struct msm_timer_sync_data_t *data)
540{
541 uint32_t *smem_clock;
542 uint32_t smem_clock_val;
543 uint32_t last_state;
544 uint32_t state;
545
546 smem_clock = smem_alloc(SMEM_SMEM_SLOW_CLOCK_VALUE,
547 sizeof(uint32_t));
548
549 if (smem_clock == NULL) {
550 printk(KERN_ERR "no smem clock\n");
551 return 0;
552 }
553
554 last_state = state = smsm_get_state(SMSM_MODEM_STATE);
555 smem_clock_val = *smem_clock;
556 if (smem_clock_val) {
557 printk(KERN_INFO "get_smem_clock: invalid start state %x "
558 "clock %u\n", state, smem_clock_val);
559 smsm_change_state(SMSM_APPS_STATE,
560 SMSM_TIMEWAIT, SMSM_TIMEINIT);
561
562 time_start(data);
563 while (*smem_clock != 0 && !time_expired(data))
564 ;
565
566 smem_clock_val = *smem_clock;
567 if (smem_clock_val) {
568 printk(KERN_EMERG "get_smem_clock: timeout still "
569 "invalid state %x clock %u\n",
570 state, smem_clock_val);
571 msm_timer_sync_timeout();
572 }
573 }
574
575 time_start(data);
576 smsm_change_state(SMSM_APPS_STATE, SMSM_TIMEINIT, SMSM_TIMEWAIT);
577 do {
578 smem_clock_val = *smem_clock;
579 state = smsm_get_state(SMSM_MODEM_STATE);
580 if (state != last_state) {
581 last_state = state;
582 if (msm_timer_debug_mask & MSM_TIMER_DEBUG_SYNC)
583 printk(KERN_INFO
584 "get_smem_clock: state %x clock %u\n",
585 state, smem_clock_val);
586 }
587 } while (smem_clock_val == 0 && !time_expired(data));
588
589 if (smem_clock_val) {
590 if (update != NULL)
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700591 update(data, smem_clock_val, sclk_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700592 } else {
593 printk(KERN_EMERG
594 "get_smem_clock: timeout state %x clock %u\n",
595 state, smem_clock_val);
596 msm_timer_sync_timeout();
597 }
598
599 smsm_change_state(SMSM_APPS_STATE, SMSM_TIMEWAIT, SMSM_TIMEINIT);
600 return smem_clock_val;
601}
602#endif /* CONFIG_MSM_N_WAY_SMSM */
603
604/*
605 * Callback function that initializes the timeout value.
606 */
607static void msm_timer_sync_to_sclk_time_start(
608 struct msm_timer_sync_data_t *data)
609{
610 /* approx 2 seconds */
611 uint32_t delta = data->clock->freq << data->clock->shift << 1;
612 data->timeout = msm_read_timer_count(data->clock, LOCAL_TIMER) + delta;
613}
614
615/*
616 * Callback function that checks the timeout.
617 */
618static bool msm_timer_sync_to_sclk_time_expired(
619 struct msm_timer_sync_data_t *data)
620{
621 uint32_t delta = msm_read_timer_count(data->clock, LOCAL_TIMER) -
622 data->timeout;
623 return ((int32_t) delta) > 0;
624}
625
626/*
627 * Callback function that updates local clock from the specified source clock
628 * value and frequency.
629 */
630static void msm_timer_sync_update(struct msm_timer_sync_data_t *data,
631 uint32_t src_clk_val, uint32_t src_clk_freq)
632{
633 struct msm_clock *dst_clk = data->clock;
634 struct msm_clock_percpu_data *dst_clk_state =
635 &__get_cpu_var(msm_clocks_percpu)[dst_clk->index];
636 uint32_t dst_clk_val = msm_read_timer_count(dst_clk, LOCAL_TIMER);
637 uint32_t new_offset;
638
639 if ((dst_clk->freq << dst_clk->shift) == src_clk_freq) {
640 new_offset = src_clk_val - dst_clk_val;
641 } else {
642 uint64_t temp;
643
644 /* separate multiplication and division steps to reduce
645 rounding error */
646 temp = src_clk_val;
647 temp *= dst_clk->freq << dst_clk->shift;
648 do_div(temp, src_clk_freq);
649
650 new_offset = (uint32_t)(temp) - dst_clk_val;
651 }
652
653 if (dst_clk_state->sleep_offset + dst_clk_state->non_sleep_offset !=
654 new_offset) {
655 if (data->exit_sleep)
656 dst_clk_state->sleep_offset =
657 new_offset - dst_clk_state->non_sleep_offset;
658 else
659 dst_clk_state->non_sleep_offset =
660 new_offset - dst_clk_state->sleep_offset;
661
662 if (msm_timer_debug_mask & MSM_TIMER_DEBUG_SYNC)
663 printk(KERN_INFO "sync clock %s: "
664 "src %u, new offset %u + %u\n",
665 dst_clk->clocksource.name, src_clk_val,
666 dst_clk_state->sleep_offset,
667 dst_clk_state->non_sleep_offset);
668 }
669}
670
671/*
672 * Synchronize GPT clock with sclk.
673 */
674static void msm_timer_sync_gpt_to_sclk(int exit_sleep)
675{
676 struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT];
677 struct msm_clock_percpu_data *gpt_clk_state =
678 &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT];
679 struct msm_timer_sync_data_t data;
680 uint32_t ret;
681
682 if (gpt_clk_state->in_sync)
683 return;
684
685 data.clock = gpt_clk;
686 data.timeout = 0;
687 data.exit_sleep = exit_sleep;
688
689 ret = msm_timer_do_sync_to_sclk(
690 msm_timer_sync_to_sclk_time_start,
691 msm_timer_sync_to_sclk_time_expired,
692 msm_timer_sync_update,
693 &data);
694
695 if (ret)
696 gpt_clk_state->in_sync = 1;
697}
698
699/*
700 * Synchronize clock with GPT clock.
701 */
702static void msm_timer_sync_to_gpt(struct msm_clock *clock, int exit_sleep)
703{
704 struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT];
705 struct msm_clock_percpu_data *gpt_clk_state =
706 &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT];
707 struct msm_clock_percpu_data *clock_state =
708 &__get_cpu_var(msm_clocks_percpu)[clock->index];
709 struct msm_timer_sync_data_t data;
710 uint32_t gpt_clk_val;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700711 u64 gpt_period = (1ULL << 32) * HZ;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700712 u64 now = get_jiffies_64();
713
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700714 do_div(gpt_period, gpt_hz);
715
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700716 BUG_ON(clock == gpt_clk);
717
718 if (clock_state->in_sync &&
719 (now - clock_state->last_sync_jiffies < (gpt_period >> 1)))
720 return;
721
722 gpt_clk_val = msm_read_timer_count(gpt_clk, LOCAL_TIMER)
723 + gpt_clk_state->sleep_offset + gpt_clk_state->non_sleep_offset;
724
725 if (exit_sleep && gpt_clk_val < clock_state->last_sync_gpt)
726 clock_state->non_sleep_offset -= clock->rollover_offset;
727
728 data.clock = clock;
729 data.timeout = 0;
730 data.exit_sleep = exit_sleep;
731
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700732 msm_timer_sync_update(&data, gpt_clk_val, gpt_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700733
734 clock_state->in_sync = 1;
735 clock_state->last_sync_gpt = gpt_clk_val;
736 clock_state->last_sync_jiffies = now;
737}
738
739static void msm_timer_reactivate_alarm(struct msm_clock *clock)
740{
741 struct msm_clock_percpu_data *clock_state =
742 &__get_cpu_var(msm_clocks_percpu)[clock->index];
743 long alarm_delta = clock_state->alarm_vtime -
744 clock_state->sleep_offset -
745 msm_read_timer_count(clock, LOCAL_TIMER);
746 alarm_delta >>= clock->shift;
747 if (alarm_delta < (long)clock->write_delay + 4)
748 alarm_delta = clock->write_delay + 4;
749 while (msm_timer_set_next_event(alarm_delta, &clock->clockevent))
750 ;
751}
752
753int64_t msm_timer_enter_idle(void)
754{
755 struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT];
756 struct msm_clock *clock = __get_cpu_var(msm_active_clock);
757 struct msm_clock_percpu_data *clock_state =
758 &__get_cpu_var(msm_clocks_percpu)[clock->index];
759 uint32_t alarm;
760 uint32_t count;
761 int32_t delta;
762
763 BUG_ON(clock != &msm_clocks[MSM_CLOCK_GPT] &&
764 clock != &msm_clocks[MSM_CLOCK_DGT]);
765
766 msm_timer_sync_gpt_to_sclk(0);
767 if (clock != gpt_clk)
768 msm_timer_sync_to_gpt(clock, 0);
769
770 count = msm_read_timer_count(clock, LOCAL_TIMER);
771 if (clock_state->stopped++ == 0)
772 clock_state->stopped_tick = count + clock_state->sleep_offset;
773 alarm = clock_state->alarm;
774 delta = alarm - count;
775 if (delta <= -(int32_t)((clock->freq << clock->shift) >> 10)) {
776 /* timer should have triggered 1ms ago */
777 printk(KERN_ERR "msm_timer_enter_idle: timer late %d, "
778 "reprogram it\n", delta);
779 msm_timer_reactivate_alarm(clock);
780 }
781 if (delta <= 0)
782 return 0;
783 return clocksource_cyc2ns((alarm - count) >> clock->shift,
784 clock->clocksource.mult,
785 clock->clocksource.shift);
786}
787
788void msm_timer_exit_idle(int low_power)
789{
790 struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT];
791 struct msm_clock *clock = __get_cpu_var(msm_active_clock);
792 struct msm_clock_percpu_data *gpt_clk_state =
793 &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT];
794 struct msm_clock_percpu_data *clock_state =
795 &__get_cpu_var(msm_clocks_percpu)[clock->index];
796 uint32_t enabled;
797
798 BUG_ON(clock != &msm_clocks[MSM_CLOCK_GPT] &&
799 clock != &msm_clocks[MSM_CLOCK_DGT]);
800
801 if (!low_power)
802 goto exit_idle_exit;
803
804 enabled = __raw_readl(gpt_clk->regbase + TIMER_ENABLE) &
805 TIMER_ENABLE_EN;
806 if (!enabled)
807 __raw_writel(TIMER_ENABLE_EN, gpt_clk->regbase + TIMER_ENABLE);
808
809#if defined(CONFIG_ARCH_MSM_SCORPION) || defined(CONFIG_ARCH_MSM_KRAIT)
810 gpt_clk_state->in_sync = 0;
811#else
812 gpt_clk_state->in_sync = gpt_clk_state->in_sync && enabled;
813#endif
814 /* Make sure timer is actually enabled before we sync it */
815 wmb();
816 msm_timer_sync_gpt_to_sclk(1);
817
818 if (clock == gpt_clk)
819 goto exit_idle_alarm;
820
821 enabled = __raw_readl(clock->regbase + TIMER_ENABLE) & TIMER_ENABLE_EN;
822 if (!enabled)
823 __raw_writel(TIMER_ENABLE_EN, clock->regbase + TIMER_ENABLE);
824
825#if defined(CONFIG_ARCH_MSM_SCORPION) || defined(CONFIG_ARCH_MSM_KRAIT)
826 clock_state->in_sync = 0;
827#else
828 clock_state->in_sync = clock_state->in_sync && enabled;
829#endif
830 /* Make sure timer is actually enabled before we sync it */
831 wmb();
832 msm_timer_sync_to_gpt(clock, 1);
833
834exit_idle_alarm:
835 msm_timer_reactivate_alarm(clock);
836
837exit_idle_exit:
838 clock_state->stopped--;
839}
840
841/*
842 * Callback function that initializes the timeout value.
843 */
844static void msm_timer_get_sclk_time_start(
845 struct msm_timer_sync_data_t *data)
846{
847 data->timeout = 200000;
848}
849
850/*
851 * Callback function that checks the timeout.
852 */
853static bool msm_timer_get_sclk_time_expired(
854 struct msm_timer_sync_data_t *data)
855{
856 udelay(10);
857 return --data->timeout <= 0;
858}
859
860/*
861 * Retrieve the cycle count from the sclk and convert it into
862 * nanoseconds.
863 *
864 * On exit, if period is not NULL, it contains the period of the
865 * sclk in nanoseconds, i.e. how long the cycle count wraps around.
866 *
867 * Return value:
868 * 0: the operation failed; period is not set either
869 * >0: time in nanoseconds
870 */
871int64_t msm_timer_get_sclk_time(int64_t *period)
872{
873 struct msm_timer_sync_data_t data;
874 uint32_t clock_value;
875 int64_t tmp;
876
877 memset(&data, 0, sizeof(data));
878 clock_value = msm_timer_do_sync_to_sclk(
879 msm_timer_get_sclk_time_start,
880 msm_timer_get_sclk_time_expired,
881 NULL,
882 &data);
883
884 if (!clock_value)
885 return 0;
886
887 if (period) {
888 tmp = 1LL << 32;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700889 tmp *= NSEC_PER_SEC;
890 do_div(tmp, sclk_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700891 *period = tmp;
892 }
893
894 tmp = (int64_t)clock_value;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700895 tmp *= NSEC_PER_SEC;
896 do_div(tmp, sclk_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700897 return tmp;
898}
899
900int __init msm_timer_init_time_sync(void (*timeout)(void))
901{
902#if defined(CONFIG_MSM_N_WAY_SMSM) && !defined(CONFIG_MSM_DIRECT_SCLK_ACCESS)
903 int ret = smsm_change_intr_mask(SMSM_TIME_MASTER_DEM, 0xFFFFFFFF, 0);
904
905 if (ret) {
906 printk(KERN_ERR "%s: failed to clear interrupt mask, %d\n",
907 __func__, ret);
908 return ret;
909 }
910
911 smsm_change_state(SMSM_APPS_DEM,
912 SLAVE_TIME_REQUEST | SLAVE_TIME_POLL, SLAVE_TIME_INIT);
913#endif
914
915 BUG_ON(timeout == NULL);
916 msm_timer_sync_timeout = timeout;
917
918 return 0;
919}
920
921#endif
922
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700923static DEFINE_CLOCK_DATA(cd);
924
Vikram Mulukutlaa41f3a12011-10-31 14:20:50 -0700925/*
926 * Store the most recent timestamp read from hardware
927 * in last_ns. This is useful for debugging crashes.
928 */
Jeff Ohlstein06658f72011-11-09 13:51:11 -0800929static atomic64_t last_ns;
Vikram Mulukutlaa41f3a12011-10-31 14:20:50 -0700930
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700931unsigned long long notrace sched_clock(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700932{
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700933 struct msm_clock *clock = &msm_clocks[msm_global_timer];
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700934 struct clocksource *cs = &clock->clocksource;
Jeff Ohlstein06658f72011-11-09 13:51:11 -0800935 u64 cyc = cs->read(cs);
936 u64 last_ns_local;
937 last_ns_local = cyc_to_sched_clock(&cd, cyc, ((u32)~0 >> clock->shift));
938 atomic64_set(&last_ns, last_ns_local);
939 return last_ns_local;
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700940}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700941
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700942static void notrace msm_update_sched_clock(void)
943{
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700944 struct msm_clock *clock = &msm_clocks[msm_global_timer];
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700945 struct clocksource *cs = &clock->clocksource;
946 u32 cyc = cs->read(cs);
947 update_sched_clock(&cd, cyc, ((u32)~0) >> clock->shift);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700948}
949
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700950int read_current_timer(unsigned long *timer_val)
951{
952 struct msm_clock *dgt = &msm_clocks[MSM_CLOCK_DGT];
953 *timer_val = msm_read_timer_count(dgt, GLOBAL_TIMER);
954 return 0;
955}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700956
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700957static void __init msm_sched_clock_init(void)
958{
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700959 struct msm_clock *clock = &msm_clocks[msm_global_timer];
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700960
961 init_sched_clock(&cd, msm_update_sched_clock, 32 - clock->shift,
962 clock->freq);
963}
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800964static void __init msm_timer_init(void)
965{
966 int i;
967 int res;
Jin Hongeecb1e02011-10-21 14:36:32 -0700968 struct irq_chip *chip;
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700969 struct msm_clock *dgt = &msm_clocks[MSM_CLOCK_DGT];
970 struct msm_clock *gpt = &msm_clocks[MSM_CLOCK_GPT];
David Brown8c27e6f2011-01-07 10:20:49 -0800971
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700972 if (cpu_is_msm7x01() || cpu_is_msm7x25() || cpu_is_msm7x27() ||
973 cpu_is_msm7x25a() || cpu_is_msm7x27a() || cpu_is_msm7x25aa() ||
974 cpu_is_msm7x27aa()) {
975 dgt->shift = MSM_DGT_SHIFT;
976 dgt->freq = 19200000 >> MSM_DGT_SHIFT;
977 dgt->clockevent.shift = 32 + MSM_DGT_SHIFT;
978 dgt->clocksource.mask = CLOCKSOURCE_MASK(32 - MSM_DGT_SHIFT);
979 dgt->clocksource.shift = 24 - MSM_DGT_SHIFT;
980 gpt->regbase = MSM_TMR_BASE;
981 dgt->regbase = MSM_TMR_BASE + 0x10;
Jeff Ohlstein5d90e252011-11-04 19:00:50 -0700982 gpt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT
983 | MSM_CLOCK_FLAGS_ODD_MATCH_WRITE
984 | MSM_CLOCK_FLAGS_DELAYED_WRITE_POST;
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700985 } else if (cpu_is_qsd8x50()) {
986 dgt->freq = 4800000;
987 gpt->regbase = MSM_TMR_BASE;
988 dgt->regbase = MSM_TMR_BASE + 0x10;
989 } else if (cpu_is_fsm9xxx())
990 dgt->freq = 4800000;
991 else if (cpu_is_msm7x30() || cpu_is_msm8x55())
992 dgt->freq = 6144000;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700993 else if (cpu_is_msm8x60()) {
Jeff Ohlstein7e538f02011-11-01 17:36:22 -0700994 global_timer_offset = MSM_TMR0_BASE - MSM_TMR_BASE;
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700995 dgt->freq = 6750000;
996 __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
Jeff Ohlstein7e538f02011-11-01 17:36:22 -0700997 } else if (cpu_is_msm9615()) {
998 dgt->freq = 6750000;
999 __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
1000 gpt->freq = 32765;
1001 gpt_hz = 32765;
1002 sclk_hz = 32765;
Jeff Ohlsteind47f96a2011-11-04 19:00:50 -07001003 gpt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT;
1004 dgt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT;
Jeff Ohlstein7e538f02011-11-01 17:36:22 -07001005 } else if (cpu_is_msm8960() || cpu_is_apq8064() || cpu_is_msm8930()) {
1006 global_timer_offset = MSM_TMR0_BASE - MSM_TMR_BASE;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -07001007 dgt->freq = 6750000;
1008 __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
1009 gpt->freq = 32765;
1010 gpt_hz = 32765;
1011 sclk_hz = 32765;
Jeff Ohlstein391a3ee2011-12-01 16:44:45 -08001012 if (!machine_is_apq8064_rumi3()) {
1013 gpt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT;
1014 dgt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT;
1015 }
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001016 } else {
1017 WARN_ON("Timer running on unknown hardware. Configure this! "
1018 "Assuming default configuration.\n");
1019 dgt->freq = 6750000;
1020 }
1021
1022 if (msm_clocks[MSM_CLOCK_GPT].clocksource.rating > DG_TIMER_RATING)
1023 msm_global_timer = MSM_CLOCK_GPT;
1024 else
1025 msm_global_timer = MSM_CLOCK_DGT;
Jeff Ohlstein672039f2010-10-05 15:23:57 -07001026
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001027 for (i = 0; i < ARRAY_SIZE(msm_clocks); i++) {
1028 struct msm_clock *clock = &msm_clocks[i];
1029 struct clock_event_device *ce = &clock->clockevent;
1030 struct clocksource *cs = &clock->clocksource;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001031 __raw_writel(0, clock->regbase + TIMER_ENABLE);
1032 __raw_writel(1, clock->regbase + TIMER_CLEAR);
1033 __raw_writel(0, clock->regbase + TIMER_COUNT_VAL);
1034 __raw_writel(~0, clock->regbase + TIMER_MATCH_VAL);
David Brown8c27e6f2011-01-07 10:20:49 -08001035
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -07001036 if ((clock->freq << clock->shift) == gpt_hz) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001037 clock->rollover_offset = 0;
1038 } else {
1039 uint64_t temp;
David Brown8c27e6f2011-01-07 10:20:49 -08001040
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001041 temp = clock->freq << clock->shift;
1042 temp <<= 32;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -07001043 do_div(temp, gpt_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001044
1045 clock->rollover_offset = (uint32_t) temp;
1046 }
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001047
1048 ce->mult = div_sc(clock->freq, NSEC_PER_SEC, ce->shift);
1049 /* allow at least 10 seconds to notice that the timer wrapped */
1050 ce->max_delta_ns =
1051 clockevent_delta2ns(0xf0000000 >> clock->shift, ce);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001052 /* ticks gets rounded down by one */
1053 ce->min_delta_ns =
1054 clockevent_delta2ns(clock->write_delay + 4, ce);
Rusty Russell320ab2b2008-12-13 21:20:26 +10301055 ce->cpumask = cpumask_of(0);
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001056
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001057 cs->mult = clocksource_hz2mult(clock->freq, cs->shift);
1058 res = clocksource_register(cs);
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001059 if (res)
1060 printk(KERN_ERR "msm_timer_init: clocksource_register "
1061 "failed for %s\n", cs->name);
1062
Trilok Sonieecb28c2011-07-20 16:24:14 +01001063 ce->irq = clock->irq;
1064 if (cpu_is_msm8x60() || cpu_is_msm8960() || cpu_is_apq8064() ||
1065 cpu_is_msm8930() || cpu_is_msm9615()) {
1066 clock->percpu_evt = alloc_percpu(struct clock_event_device *);
1067 if (!clock->percpu_evt) {
1068 pr_err("msm_timer_init: memory allocation "
1069 "failed for %s\n", ce->name);
1070 continue;
1071 }
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001072
Trilok Sonieecb28c2011-07-20 16:24:14 +01001073 *__this_cpu_ptr(clock->percpu_evt) = ce;
1074 res = request_percpu_irq(ce->irq, msm_timer_interrupt,
1075 ce->name, clock->percpu_evt);
1076 if (!res)
1077 enable_percpu_irq(ce->irq, 0);
1078 } else {
1079 clock->evt = ce;
1080 res = request_irq(ce->irq, msm_timer_interrupt,
1081 IRQF_TIMER | IRQF_NOBALANCING | IRQF_TRIGGER_RISING,
1082 ce->name, &clock->evt);
1083 }
1084
1085 if (res)
1086 pr_err("msm_timer_init: request_irq failed for %s\n",
1087 ce->name);
1088
1089 chip = irq_get_chip(clock->irq);
Jin Hongeecb1e02011-10-21 14:36:32 -07001090 if (chip && chip->irq_mask)
Trilok Sonieecb28c2011-07-20 16:24:14 +01001091 chip->irq_mask(irq_get_irq_data(clock->irq));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001092
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001093 clockevents_register_device(ce);
1094 }
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -07001095 msm_sched_clock_init();
Taniya Das36057be2011-10-28 13:02:17 +05301096
1097 if (is_smp()) {
1098 __raw_writel(1,
1099 msm_clocks[MSM_CLOCK_DGT].regbase + TIMER_ENABLE);
1100 set_delay_fn(read_current_timer_delay_loop);
1101 }
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001102}
1103
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001104#ifdef CONFIG_SMP
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001105
Santosh Shilimkaraf90f102011-02-23 18:53:15 +01001106int __cpuinit local_timer_setup(struct clock_event_device *evt)
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001107{
Jeff Ohlsteind17ee762011-09-26 19:44:22 -07001108 static DEFINE_PER_CPU(bool, first_boot) = true;
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001109 struct msm_clock *clock = &msm_clocks[msm_global_timer];
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001110
1111 /* Use existing clock_event for cpu 0 */
1112 if (!smp_processor_id())
David Brown893b66c2011-03-30 11:26:57 -07001113 return 0;
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001114
Taniya Das36057be2011-10-28 13:02:17 +05301115 if (cpu_is_msm8x60() || cpu_is_msm8960() || cpu_is_apq8064()
1116 || cpu_is_msm8930())
1117 __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001118
Jeff Ohlsteind17ee762011-09-26 19:44:22 -07001119 if (__get_cpu_var(first_boot)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001120 __raw_writel(0, clock->regbase + TIMER_ENABLE);
1121 __raw_writel(0, clock->regbase + TIMER_CLEAR);
1122 __raw_writel(~0, clock->regbase + TIMER_MATCH_VAL);
Jeff Ohlsteind17ee762011-09-26 19:44:22 -07001123 __get_cpu_var(first_boot) = false;
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001124 }
Trilok Sonieecb28c2011-07-20 16:24:14 +01001125 evt->irq = clock->irq;
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001126 evt->name = "local_timer";
1127 evt->features = CLOCK_EVT_FEAT_ONESHOT;
1128 evt->rating = clock->clockevent.rating;
1129 evt->set_mode = msm_timer_set_mode;
1130 evt->set_next_event = msm_timer_set_next_event;
1131 evt->shift = clock->clockevent.shift;
1132 evt->mult = div_sc(clock->freq, NSEC_PER_SEC, evt->shift);
1133 evt->max_delta_ns =
1134 clockevent_delta2ns(0xf0000000 >> clock->shift, evt);
1135 evt->min_delta_ns = clockevent_delta2ns(4, evt);
1136
Trilok Sonieecb28c2011-07-20 16:24:14 +01001137 *__this_cpu_ptr(clock->percpu_evt) = evt;
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001138
1139 clockevents_register_device(evt);
Trilok Sonieecb28c2011-07-20 16:24:14 +01001140 enable_percpu_irq(evt->irq, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001141
Santosh Shilimkaraf90f102011-02-23 18:53:15 +01001142 return 0;
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001143}
1144
Trilok Sonieecb28c2011-07-20 16:24:14 +01001145void local_timer_stop(struct clock_event_device *evt)
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001146{
Trilok Sonieecb28c2011-07-20 16:24:14 +01001147 evt->set_mode(CLOCK_EVT_MODE_UNUSED, evt);
1148 disable_percpu_irq(evt->irq);
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001149}
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001150#endif
1151
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001152struct sys_timer msm_timer = {
1153 .init = msm_timer_init
1154};