blob: 4eef948e77751d47ce73ba82e5762184aad34e01 [file] [log] [blame]
Stephen Boyddd15ab82011-11-08 10:34:05 -08001/*
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08002 * Copyright (C) 2007 Google, Inc.
Jeff Ohlsteinf0a31e42012-01-06 19:03:05 -08003 * Copyright (c) 2009-2012, Code Aurora Forum. All rights reserved.
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08004 *
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 */
15
Steve Mucklef132c6c2012-06-06 18:30:57 -070016#include <linux/module.h>
Stephen Boyd4a184072011-11-08 10:34:04 -080017#include <linux/clocksource.h>
18#include <linux/clockchips.h>
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -080019#include <linux/init.h>
20#include <linux/time.h>
21#include <linux/interrupt.h>
22#include <linux/irq.h>
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -080023#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
Steve Mucklef132c6c2012-06-06 18:30:57 -070027#include <asm/localtimer.h>
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -080028#include <asm/mach/time.h>
Stephen Boydebf30dc2011-05-31 16:10:00 -070029#include <asm/hardware/gic.h>
Stephen Boydf8e56c42012-02-22 01:39:37 +000030#include <asm/sched_clock.h>
Taniya Das36057be2011-10-28 13:02:17 +053031#include <asm/smp_plat.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010032#include <mach/msm_iomap.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033#include <mach/irqs.h>
34#include <mach/socinfo.h>
35
36#if defined(CONFIG_MSM_SMD)
37#include "smd_private.h"
38#endif
39#include "timer.h"
40
41enum {
42 MSM_TIMER_DEBUG_SYNC = 1U << 0,
43};
44static int msm_timer_debug_mask;
45module_param_named(debug_mask, msm_timer_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP);
46
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070047#ifdef CONFIG_MSM7X00A_USE_GP_TIMER
48 #define DG_TIMER_RATING 100
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049#else
50 #define DG_TIMER_RATING 300
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070051#endif
52
Jeff Ohlstein7e538f02011-11-01 17:36:22 -070053#ifndef MSM_TMR0_BASE
54#define MSM_TMR0_BASE MSM_TMR_BASE
55#endif
56
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070057#define MSM_DGT_SHIFT (5)
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -080058
59#define TIMER_MATCH_VAL 0x0000
60#define TIMER_COUNT_VAL 0x0004
61#define TIMER_ENABLE 0x0008
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -080062#define TIMER_CLEAR 0x000C
Jeff Ohlstein672039f2010-10-05 15:23:57 -070063#define DGT_CLK_CTL 0x0034
64enum {
65 DGT_CLK_CTL_DIV_1 = 0,
66 DGT_CLK_CTL_DIV_2 = 1,
67 DGT_CLK_CTL_DIV_3 = 2,
68 DGT_CLK_CTL_DIV_4 = 3,
69};
Jeff Ohlstein6c47a272012-02-24 14:48:55 -080070#define TIMER_STATUS 0x0088
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070071#define TIMER_ENABLE_EN 1
72#define TIMER_ENABLE_CLR_ON_MATCH_EN 2
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -080073
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070074#define LOCAL_TIMER 0
75#define GLOBAL_TIMER 1
Jeff Ohlstein672039f2010-10-05 15:23:57 -070076
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070077/*
Jeff Ohlsteine1a7e402011-09-07 12:52:36 -070078 * global_timer_offset is added to the regbase of a timer to force the memory
79 * access to come from the CPU0 region.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070080 */
Jeff Ohlsteine1a7e402011-09-07 12:52:36 -070081static int global_timer_offset;
Jeff Ohlstein7a018322011-09-28 12:44:06 -070082static int msm_global_timer;
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -080083
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070084#define NR_TIMERS ARRAY_SIZE(msm_clocks)
85
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -070086unsigned int gpt_hz = 32768;
87unsigned int sclk_hz = 32768;
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -080088
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070089static struct msm_clock *clockevent_to_clock(struct clock_event_device *evt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070090static irqreturn_t msm_timer_interrupt(int irq, void *dev_id);
91static cycle_t msm_gpt_read(struct clocksource *cs);
92static cycle_t msm_dgt_read(struct clocksource *cs);
93static void msm_timer_set_mode(enum clock_event_mode mode,
94 struct clock_event_device *evt);
95static int msm_timer_set_next_event(unsigned long cycles,
96 struct clock_event_device *evt);
97
98enum {
99 MSM_CLOCK_FLAGS_UNSTABLE_COUNT = 1U << 0,
100 MSM_CLOCK_FLAGS_ODD_MATCH_WRITE = 1U << 1,
101 MSM_CLOCK_FLAGS_DELAYED_WRITE_POST = 1U << 2,
102};
103
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800104struct msm_clock {
105 struct clock_event_device clockevent;
106 struct clocksource clocksource;
Trilok Sonieecb28c2011-07-20 16:24:14 +0100107 unsigned int irq;
Brian Swetlandbcc0f6a2008-09-10 14:00:53 -0700108 void __iomem *regbase;
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800109 uint32_t freq;
110 uint32_t shift;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700111 uint32_t flags;
112 uint32_t write_delay;
113 uint32_t rollover_offset;
114 uint32_t index;
Trilok Sonieecb28c2011-07-20 16:24:14 +0100115 void __iomem *global_counter;
116 void __iomem *local_counter;
Jeff Ohlstein6c47a272012-02-24 14:48:55 -0800117 uint32_t status_mask;
Trilok Sonieecb28c2011-07-20 16:24:14 +0100118 union {
119 struct clock_event_device *evt;
120 struct clock_event_device __percpu **percpu_evt;
121 };
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800122};
123
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800124enum {
125 MSM_CLOCK_GPT,
126 MSM_CLOCK_DGT,
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800127};
128
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700129struct msm_clock_percpu_data {
130 uint32_t last_set;
131 uint32_t sleep_offset;
132 uint32_t alarm_vtime;
133 uint32_t alarm;
134 uint32_t non_sleep_offset;
135 uint32_t in_sync;
136 cycle_t stopped_tick;
137 int stopped;
138 uint32_t last_sync_gpt;
139 u64 last_sync_jiffies;
140};
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800141
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700142struct msm_timer_sync_data_t {
143 struct msm_clock *clock;
144 uint32_t timeout;
145 int exit_sleep;
146};
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800147
148static struct msm_clock msm_clocks[] = {
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800149 [MSM_CLOCK_GPT] = {
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800150 .clockevent = {
151 .name = "gp_timer",
152 .features = CLOCK_EVT_FEAT_ONESHOT,
153 .shift = 32,
154 .rating = 200,
155 .set_next_event = msm_timer_set_next_event,
156 .set_mode = msm_timer_set_mode,
157 },
158 .clocksource = {
159 .name = "gp_timer",
160 .rating = 200,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700161 .read = msm_gpt_read,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800162 .mask = CLOCKSOURCE_MASK(32),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700163 .shift = 17,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800164 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
165 },
Trilok Sonieecb28c2011-07-20 16:24:14 +0100166 .irq = INT_GP_TIMER_EXP,
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700167 .regbase = MSM_TMR_BASE + 0x4,
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700168 .freq = 32768,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700169 .index = MSM_CLOCK_GPT,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700170 .write_delay = 9,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800171 },
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800172 [MSM_CLOCK_DGT] = {
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800173 .clockevent = {
174 .name = "dg_timer",
175 .features = CLOCK_EVT_FEAT_ONESHOT,
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700176 .shift = 32,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700177 .rating = DG_TIMER_RATING,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800178 .set_next_event = msm_timer_set_next_event,
179 .set_mode = msm_timer_set_mode,
180 },
181 .clocksource = {
182 .name = "dg_timer",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700183 .rating = DG_TIMER_RATING,
184 .read = msm_dgt_read,
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700185 .mask = CLOCKSOURCE_MASK(32),
186 .shift = 24,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800187 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
188 },
Trilok Sonieecb28c2011-07-20 16:24:14 +0100189 .irq = INT_DEBUG_TIMER_EXP,
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700190 .regbase = MSM_TMR_BASE + 0x24,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700191 .index = MSM_CLOCK_DGT,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700192 .write_delay = 9,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800193 }
194};
195
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700196static DEFINE_PER_CPU(struct msm_clock_percpu_data[NR_TIMERS],
197 msm_clocks_percpu);
198
199static DEFINE_PER_CPU(struct msm_clock *, msm_active_clock);
Stephen Boyda850c3f2011-11-08 10:34:06 -0800200
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800201static irqreturn_t msm_timer_interrupt(int irq, void *dev_id)
202{
Marc Zyngier28af6902011-07-22 12:52:37 +0100203 struct clock_event_device *evt = *(struct clock_event_device **)dev_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700204 if (evt->event_handler == NULL)
205 return IRQ_HANDLED;
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800206 evt->event_handler(evt);
207 return IRQ_HANDLED;
208}
209
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700210static uint32_t msm_read_timer_count(struct msm_clock *clock, int global)
211{
Jeff Ohlstein5d90e252011-11-04 19:00:50 -0700212 uint32_t t1, t2, t3;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700213 int loop_count = 0;
Jeff Ohlstein5d90e252011-11-04 19:00:50 -0700214 void __iomem *addr = clock->regbase + TIMER_COUNT_VAL +
215 global*global_timer_offset;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700216
217 if (!(clock->flags & MSM_CLOCK_FLAGS_UNSTABLE_COUNT))
Jeff Ohlstein60b68702012-03-30 16:35:25 -0700218 return __raw_readl_no_log(addr);
Jeff Ohlstein5d90e252011-11-04 19:00:50 -0700219
Jeff Ohlstein60b68702012-03-30 16:35:25 -0700220 t1 = __raw_readl_no_log(addr);
Laura Abbott1d506042012-01-23 13:21:34 -0800221 t2 = __raw_readl_no_log(addr);
Jeff Ohlstein5d90e252011-11-04 19:00:50 -0700222 if ((t2-t1) <= 1)
223 return t2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700224 while (1) {
Laura Abbott1d506042012-01-23 13:21:34 -0800225 t1 = __raw_readl_no_log(addr);
226 t2 = __raw_readl_no_log(addr);
227 t3 = __raw_readl_no_log(addr);
Jeff Ohlstein10206eb2011-11-30 19:18:49 -0800228 cpu_relax();
Jeff Ohlstein5d90e252011-11-04 19:00:50 -0700229 if ((t3-t2) <= 1)
230 return t3;
231 if ((t2-t1) <= 1)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700232 return t2;
Jeff Ohlsteinfdd87082011-12-09 13:40:08 -0800233 if ((t2 >= t1) && (t3 >= t2))
234 return t2;
Jeff Ohlstein10206eb2011-11-30 19:18:49 -0800235 if (++loop_count == 5) {
Jeff Ohlstein5d90e252011-11-04 19:00:50 -0700236 pr_err("msm_read_timer_count timer %s did not "
237 "stabilize: %u -> %u -> %u\n",
238 clock->clockevent.name, t1, t2, t3);
239 return t3;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700240 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700241 }
242}
243
244static cycle_t msm_gpt_read(struct clocksource *cs)
245{
246 struct msm_clock *clock = &msm_clocks[MSM_CLOCK_GPT];
247 struct msm_clock_percpu_data *clock_state =
248 &per_cpu(msm_clocks_percpu, 0)[MSM_CLOCK_GPT];
249
250 if (clock_state->stopped)
251 return clock_state->stopped_tick;
252
253 return msm_read_timer_count(clock, GLOBAL_TIMER) +
254 clock_state->sleep_offset;
255}
256
257static cycle_t msm_dgt_read(struct clocksource *cs)
258{
259 struct msm_clock *clock = &msm_clocks[MSM_CLOCK_DGT];
260 struct msm_clock_percpu_data *clock_state =
261 &per_cpu(msm_clocks_percpu, 0)[MSM_CLOCK_DGT];
262
263 if (clock_state->stopped)
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700264 return clock_state->stopped_tick >> clock->shift;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700265
266 return (msm_read_timer_count(clock, GLOBAL_TIMER) +
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700267 clock_state->sleep_offset) >> clock->shift;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700268}
269
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700270static struct msm_clock *clockevent_to_clock(struct clock_event_device *evt)
271{
272 int i;
Taniya Das36057be2011-10-28 13:02:17 +0530273
274 if (!is_smp())
275 return container_of(evt, struct msm_clock, clockevent);
276
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700277 for (i = 0; i < NR_TIMERS; i++)
278 if (evt == &(msm_clocks[i].clockevent))
279 return &msm_clocks[i];
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700280 return &msm_clocks[msm_global_timer];
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700281}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700282
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800283static int msm_timer_set_next_event(unsigned long cycles,
284 struct clock_event_device *evt)
285{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700286 int i;
287 struct msm_clock *clock;
288 struct msm_clock_percpu_data *clock_state;
289 uint32_t now;
290 uint32_t alarm;
291 int late;
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800292
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700293 clock = clockevent_to_clock(evt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700294 clock_state = &__get_cpu_var(msm_clocks_percpu)[clock->index];
295 if (clock_state->stopped)
296 return 0;
297 now = msm_read_timer_count(clock, LOCAL_TIMER);
298 alarm = now + (cycles << clock->shift);
299 if (clock->flags & MSM_CLOCK_FLAGS_ODD_MATCH_WRITE)
300 while (now == clock_state->last_set)
301 now = msm_read_timer_count(clock, LOCAL_TIMER);
302
303 clock_state->alarm = alarm;
304 __raw_writel(alarm, clock->regbase + TIMER_MATCH_VAL);
305
306 if (clock->flags & MSM_CLOCK_FLAGS_DELAYED_WRITE_POST) {
307 /* read the counter four extra times to make sure write posts
308 before reading the time */
309 for (i = 0; i < 4; i++)
Laura Abbott1d506042012-01-23 13:21:34 -0800310 __raw_readl_no_log(clock->regbase + TIMER_COUNT_VAL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700311 }
312 now = msm_read_timer_count(clock, LOCAL_TIMER);
313 clock_state->last_set = now;
314 clock_state->alarm_vtime = alarm + clock_state->sleep_offset;
315 late = now - alarm;
316 if (late >= (int)(-clock->write_delay << clock->shift) &&
317 late < clock->freq*5)
318 return -ETIME;
319
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800320 return 0;
321}
322
323static void msm_timer_set_mode(enum clock_event_mode mode,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700324 struct clock_event_device *evt)
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800325{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700326 struct msm_clock *clock;
327 struct msm_clock_percpu_data *clock_state, *gpt_state;
328 unsigned long irq_flags;
Jin Hongeecb1e02011-10-21 14:36:32 -0700329 struct irq_chip *chip;
Stephen Boyda850c3f2011-11-08 10:34:06 -0800330
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700331 clock = clockevent_to_clock(evt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700332 clock_state = &__get_cpu_var(msm_clocks_percpu)[clock->index];
333 gpt_state = &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT];
334
335 local_irq_save(irq_flags);
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800336
337 switch (mode) {
338 case CLOCK_EVT_MODE_RESUME:
339 case CLOCK_EVT_MODE_PERIODIC:
340 break;
341 case CLOCK_EVT_MODE_ONESHOT:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700342 clock_state->stopped = 0;
343 clock_state->sleep_offset =
344 -msm_read_timer_count(clock, LOCAL_TIMER) +
345 clock_state->stopped_tick;
346 get_cpu_var(msm_active_clock) = clock;
347 put_cpu_var(msm_active_clock);
348 __raw_writel(TIMER_ENABLE_EN, clock->regbase + TIMER_ENABLE);
Trilok Sonieecb28c2011-07-20 16:24:14 +0100349 chip = irq_get_chip(clock->irq);
Jin Hongeecb1e02011-10-21 14:36:32 -0700350 if (chip && chip->irq_unmask)
Trilok Sonieecb28c2011-07-20 16:24:14 +0100351 chip->irq_unmask(irq_get_irq_data(clock->irq));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700352 if (clock != &msm_clocks[MSM_CLOCK_GPT])
353 __raw_writel(TIMER_ENABLE_EN,
354 msm_clocks[MSM_CLOCK_GPT].regbase +
355 TIMER_ENABLE);
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800356 break;
357 case CLOCK_EVT_MODE_UNUSED:
358 case CLOCK_EVT_MODE_SHUTDOWN:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700359 get_cpu_var(msm_active_clock) = NULL;
360 put_cpu_var(msm_active_clock);
361 clock_state->in_sync = 0;
362 clock_state->stopped = 1;
363 clock_state->stopped_tick =
364 msm_read_timer_count(clock, LOCAL_TIMER) +
365 clock_state->sleep_offset;
366 __raw_writel(0, clock->regbase + TIMER_MATCH_VAL);
Trilok Sonieecb28c2011-07-20 16:24:14 +0100367 chip = irq_get_chip(clock->irq);
Jin Hongeecb1e02011-10-21 14:36:32 -0700368 if (chip && chip->irq_mask)
Trilok Sonieecb28c2011-07-20 16:24:14 +0100369 chip->irq_mask(irq_get_irq_data(clock->irq));
Taniya Das36057be2011-10-28 13:02:17 +0530370
371 if (!is_smp() || clock != &msm_clocks[MSM_CLOCK_DGT]
372 || smp_processor_id())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700373 __raw_writel(0, clock->regbase + TIMER_ENABLE);
Taniya Das36057be2011-10-28 13:02:17 +0530374
Steve Mucklef132c6c2012-06-06 18:30:57 -0700375 if (msm_global_timer == MSM_CLOCK_DGT &&
376 clock != &msm_clocks[MSM_CLOCK_GPT]) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700377 gpt_state->in_sync = 0;
378 __raw_writel(0, msm_clocks[MSM_CLOCK_GPT].regbase +
379 TIMER_ENABLE);
380 }
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800381 break;
382 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700383 wmb();
384 local_irq_restore(irq_flags);
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800385}
386
Jeff Ohlstein973871d2011-09-28 11:46:26 -0700387void __iomem *msm_timer_get_timer0_base(void)
Stephen Boyd2a00c102011-11-08 10:34:07 -0800388{
Jeff Ohlstein973871d2011-09-28 11:46:26 -0700389 return MSM_TMR_BASE + global_timer_offset;
Stephen Boyd2081a6b2011-11-08 10:34:08 -0800390}
391
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700392#define MPM_SCLK_COUNT_VAL 0x0024
393
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700394#ifdef CONFIG_PM
395/*
396 * Retrieve the cycle count from sclk and optionally synchronize local clock
397 * with the sclk value.
398 *
399 * time_start and time_expired are callbacks that must be specified. The
400 * protocol uses them to detect timeout. The update callback is optional.
401 * If not NULL, update will be called so that it can update local clock.
402 *
403 * The function does not use the argument data directly; it passes data to
404 * the callbacks.
405 *
406 * Return value:
407 * 0: the operation failed
408 * >0: the slow clock value after time-sync
409 */
410static void (*msm_timer_sync_timeout)(void);
411#if defined(CONFIG_MSM_DIRECT_SCLK_ACCESS)
Jeff Ohlsteinecefdc02012-01-13 12:37:44 -0800412uint32_t msm_timer_get_sclk_ticks(void)
Stephen Boyd2081a6b2011-11-08 10:34:08 -0800413{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700414 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--) {
Laura Abbott1d506042012-01-23 13:21:34 -0800422 t1 = __raw_readl_no_log(MSM_RPM_MPM_BASE + MPM_SCLK_COUNT_VAL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700423 do {
424 udelay(1);
425 t2 = t1;
Laura Abbott1d506042012-01-23 13:21:34 -0800426 t1 = __raw_readl_no_log(
427 MSM_RPM_MPM_BASE + MPM_SCLK_COUNT_VAL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700428 } while ((t2 != t1) && --loop_count);
429
430 if (!loop_count) {
431 printk(KERN_EMERG "SCLK did not stabilize\n");
432 return 0;
433 }
434
435 if (t1)
436 break;
437
438 udelay(tmp);
439 }
440
441 if (!loop_zero_count) {
442 printk(KERN_EMERG "SCLK reads zero\n");
443 return 0;
444 }
445
Jeff Ohlsteinecefdc02012-01-13 12:37:44 -0800446 return t1;
Stephen Boyd2a00c102011-11-08 10:34:07 -0800447}
448
Jeff Ohlsteinecefdc02012-01-13 12:37:44 -0800449static uint32_t msm_timer_do_sync_to_sclk(
450 void (*time_start)(struct msm_timer_sync_data_t *data),
451 bool (*time_expired)(struct msm_timer_sync_data_t *data),
452 void (*update)(struct msm_timer_sync_data_t *, uint32_t, uint32_t),
453 struct msm_timer_sync_data_t *data)
454{
455 unsigned t1 = msm_timer_get_sclk_ticks();
456
457 if (t1 && update != NULL)
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700458 update(data, t1, sclk_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700459 return t1;
460}
461#elif defined(CONFIG_MSM_N_WAY_SMSM)
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700462
463/* Time Master State Bits */
464#define MASTER_BITS_PER_CPU 1
465#define MASTER_TIME_PENDING \
466 (0x01UL << (MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
467
468/* Time Slave State Bits */
469#define SLAVE_TIME_REQUEST 0x0400
470#define SLAVE_TIME_POLL 0x0800
471#define SLAVE_TIME_INIT 0x1000
472
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700473static uint32_t msm_timer_do_sync_to_sclk(
474 void (*time_start)(struct msm_timer_sync_data_t *data),
475 bool (*time_expired)(struct msm_timer_sync_data_t *data),
476 void (*update)(struct msm_timer_sync_data_t *, uint32_t, uint32_t),
477 struct msm_timer_sync_data_t *data)
478{
479 uint32_t *smem_clock;
480 uint32_t smem_clock_val;
481 uint32_t state;
482
483 smem_clock = smem_alloc(SMEM_SMEM_SLOW_CLOCK_VALUE, sizeof(uint32_t));
484 if (smem_clock == NULL) {
485 printk(KERN_ERR "no smem clock\n");
486 return 0;
487 }
488
489 state = smsm_get_state(SMSM_MODEM_STATE);
490 if ((state & SMSM_INIT) == 0) {
491 printk(KERN_ERR "smsm not initialized\n");
492 return 0;
493 }
494
495 time_start(data);
496 while ((state = smsm_get_state(SMSM_TIME_MASTER_DEM)) &
497 MASTER_TIME_PENDING) {
498 if (time_expired(data)) {
499 printk(KERN_EMERG "get_smem_clock: timeout 1 still "
500 "invalid state %x\n", state);
501 msm_timer_sync_timeout();
502 }
503 }
504
505 smsm_change_state(SMSM_APPS_DEM, SLAVE_TIME_POLL | SLAVE_TIME_INIT,
506 SLAVE_TIME_REQUEST);
507
508 time_start(data);
509 while (!((state = smsm_get_state(SMSM_TIME_MASTER_DEM)) &
510 MASTER_TIME_PENDING)) {
511 if (time_expired(data)) {
512 printk(KERN_EMERG "get_smem_clock: timeout 2 still "
513 "invalid state %x\n", state);
514 msm_timer_sync_timeout();
515 }
516 }
517
518 smsm_change_state(SMSM_APPS_DEM, SLAVE_TIME_REQUEST, SLAVE_TIME_POLL);
519
520 time_start(data);
521 do {
522 smem_clock_val = *smem_clock;
523 } while (smem_clock_val == 0 && !time_expired(data));
524
525 state = smsm_get_state(SMSM_TIME_MASTER_DEM);
526
527 if (smem_clock_val) {
528 if (update != NULL)
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700529 update(data, smem_clock_val, sclk_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700530
531 if (msm_timer_debug_mask & MSM_TIMER_DEBUG_SYNC)
532 printk(KERN_INFO
533 "get_smem_clock: state %x clock %u\n",
534 state, smem_clock_val);
535 } else {
536 printk(KERN_EMERG
537 "get_smem_clock: timeout state %x clock %u\n",
538 state, smem_clock_val);
539 msm_timer_sync_timeout();
540 }
541
542 smsm_change_state(SMSM_APPS_DEM, SLAVE_TIME_REQUEST | SLAVE_TIME_POLL,
543 SLAVE_TIME_INIT);
544 return smem_clock_val;
545}
546#else /* CONFIG_MSM_N_WAY_SMSM */
547static uint32_t msm_timer_do_sync_to_sclk(
548 void (*time_start)(struct msm_timer_sync_data_t *data),
549 bool (*time_expired)(struct msm_timer_sync_data_t *data),
550 void (*update)(struct msm_timer_sync_data_t *, uint32_t, uint32_t),
551 struct msm_timer_sync_data_t *data)
552{
553 uint32_t *smem_clock;
554 uint32_t smem_clock_val;
555 uint32_t last_state;
556 uint32_t state;
557
558 smem_clock = smem_alloc(SMEM_SMEM_SLOW_CLOCK_VALUE,
559 sizeof(uint32_t));
560
561 if (smem_clock == NULL) {
562 printk(KERN_ERR "no smem clock\n");
563 return 0;
564 }
565
566 last_state = state = smsm_get_state(SMSM_MODEM_STATE);
567 smem_clock_val = *smem_clock;
568 if (smem_clock_val) {
569 printk(KERN_INFO "get_smem_clock: invalid start state %x "
570 "clock %u\n", state, smem_clock_val);
571 smsm_change_state(SMSM_APPS_STATE,
572 SMSM_TIMEWAIT, SMSM_TIMEINIT);
573
574 time_start(data);
575 while (*smem_clock != 0 && !time_expired(data))
576 ;
577
578 smem_clock_val = *smem_clock;
579 if (smem_clock_val) {
580 printk(KERN_EMERG "get_smem_clock: timeout still "
581 "invalid state %x clock %u\n",
582 state, smem_clock_val);
583 msm_timer_sync_timeout();
584 }
585 }
586
587 time_start(data);
588 smsm_change_state(SMSM_APPS_STATE, SMSM_TIMEINIT, SMSM_TIMEWAIT);
589 do {
590 smem_clock_val = *smem_clock;
591 state = smsm_get_state(SMSM_MODEM_STATE);
592 if (state != last_state) {
593 last_state = state;
594 if (msm_timer_debug_mask & MSM_TIMER_DEBUG_SYNC)
595 printk(KERN_INFO
596 "get_smem_clock: state %x clock %u\n",
597 state, smem_clock_val);
598 }
599 } while (smem_clock_val == 0 && !time_expired(data));
600
601 if (smem_clock_val) {
602 if (update != NULL)
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700603 update(data, smem_clock_val, sclk_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700604 } else {
605 printk(KERN_EMERG
606 "get_smem_clock: timeout state %x clock %u\n",
607 state, smem_clock_val);
608 msm_timer_sync_timeout();
609 }
610
611 smsm_change_state(SMSM_APPS_STATE, SMSM_TIMEWAIT, SMSM_TIMEINIT);
612 return smem_clock_val;
613}
614#endif /* CONFIG_MSM_N_WAY_SMSM */
615
616/*
617 * Callback function that initializes the timeout value.
618 */
619static void msm_timer_sync_to_sclk_time_start(
620 struct msm_timer_sync_data_t *data)
621{
622 /* approx 2 seconds */
623 uint32_t delta = data->clock->freq << data->clock->shift << 1;
624 data->timeout = msm_read_timer_count(data->clock, LOCAL_TIMER) + delta;
625}
626
627/*
628 * Callback function that checks the timeout.
629 */
630static bool msm_timer_sync_to_sclk_time_expired(
631 struct msm_timer_sync_data_t *data)
632{
633 uint32_t delta = msm_read_timer_count(data->clock, LOCAL_TIMER) -
634 data->timeout;
635 return ((int32_t) delta) > 0;
636}
637
638/*
639 * Callback function that updates local clock from the specified source clock
640 * value and frequency.
641 */
642static void msm_timer_sync_update(struct msm_timer_sync_data_t *data,
643 uint32_t src_clk_val, uint32_t src_clk_freq)
644{
645 struct msm_clock *dst_clk = data->clock;
646 struct msm_clock_percpu_data *dst_clk_state =
647 &__get_cpu_var(msm_clocks_percpu)[dst_clk->index];
648 uint32_t dst_clk_val = msm_read_timer_count(dst_clk, LOCAL_TIMER);
649 uint32_t new_offset;
650
651 if ((dst_clk->freq << dst_clk->shift) == src_clk_freq) {
652 new_offset = src_clk_val - dst_clk_val;
653 } else {
654 uint64_t temp;
655
656 /* separate multiplication and division steps to reduce
657 rounding error */
658 temp = src_clk_val;
659 temp *= dst_clk->freq << dst_clk->shift;
660 do_div(temp, src_clk_freq);
661
662 new_offset = (uint32_t)(temp) - dst_clk_val;
663 }
664
665 if (dst_clk_state->sleep_offset + dst_clk_state->non_sleep_offset !=
666 new_offset) {
667 if (data->exit_sleep)
668 dst_clk_state->sleep_offset =
669 new_offset - dst_clk_state->non_sleep_offset;
670 else
671 dst_clk_state->non_sleep_offset =
672 new_offset - dst_clk_state->sleep_offset;
673
674 if (msm_timer_debug_mask & MSM_TIMER_DEBUG_SYNC)
675 printk(KERN_INFO "sync clock %s: "
676 "src %u, new offset %u + %u\n",
677 dst_clk->clocksource.name, src_clk_val,
678 dst_clk_state->sleep_offset,
679 dst_clk_state->non_sleep_offset);
680 }
681}
682
683/*
684 * Synchronize GPT clock with sclk.
685 */
686static void msm_timer_sync_gpt_to_sclk(int exit_sleep)
687{
688 struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT];
689 struct msm_clock_percpu_data *gpt_clk_state =
690 &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT];
691 struct msm_timer_sync_data_t data;
692 uint32_t ret;
693
694 if (gpt_clk_state->in_sync)
695 return;
696
697 data.clock = gpt_clk;
698 data.timeout = 0;
699 data.exit_sleep = exit_sleep;
700
701 ret = msm_timer_do_sync_to_sclk(
702 msm_timer_sync_to_sclk_time_start,
703 msm_timer_sync_to_sclk_time_expired,
704 msm_timer_sync_update,
705 &data);
706
707 if (ret)
708 gpt_clk_state->in_sync = 1;
709}
710
711/*
712 * Synchronize clock with GPT clock.
713 */
714static void msm_timer_sync_to_gpt(struct msm_clock *clock, int exit_sleep)
715{
716 struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT];
717 struct msm_clock_percpu_data *gpt_clk_state =
718 &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT];
719 struct msm_clock_percpu_data *clock_state =
720 &__get_cpu_var(msm_clocks_percpu)[clock->index];
721 struct msm_timer_sync_data_t data;
722 uint32_t gpt_clk_val;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700723 u64 gpt_period = (1ULL << 32) * HZ;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700724 u64 now = get_jiffies_64();
725
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700726 do_div(gpt_period, gpt_hz);
727
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700728 BUG_ON(clock == gpt_clk);
729
730 if (clock_state->in_sync &&
731 (now - clock_state->last_sync_jiffies < (gpt_period >> 1)))
732 return;
733
734 gpt_clk_val = msm_read_timer_count(gpt_clk, LOCAL_TIMER)
735 + gpt_clk_state->sleep_offset + gpt_clk_state->non_sleep_offset;
736
737 if (exit_sleep && gpt_clk_val < clock_state->last_sync_gpt)
738 clock_state->non_sleep_offset -= clock->rollover_offset;
739
740 data.clock = clock;
741 data.timeout = 0;
742 data.exit_sleep = exit_sleep;
743
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700744 msm_timer_sync_update(&data, gpt_clk_val, gpt_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700745
746 clock_state->in_sync = 1;
747 clock_state->last_sync_gpt = gpt_clk_val;
748 clock_state->last_sync_jiffies = now;
749}
750
751static void msm_timer_reactivate_alarm(struct msm_clock *clock)
752{
753 struct msm_clock_percpu_data *clock_state =
754 &__get_cpu_var(msm_clocks_percpu)[clock->index];
755 long alarm_delta = clock_state->alarm_vtime -
756 clock_state->sleep_offset -
757 msm_read_timer_count(clock, LOCAL_TIMER);
758 alarm_delta >>= clock->shift;
759 if (alarm_delta < (long)clock->write_delay + 4)
760 alarm_delta = clock->write_delay + 4;
761 while (msm_timer_set_next_event(alarm_delta, &clock->clockevent))
762 ;
763}
764
765int64_t msm_timer_enter_idle(void)
766{
767 struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT];
768 struct msm_clock *clock = __get_cpu_var(msm_active_clock);
769 struct msm_clock_percpu_data *clock_state =
770 &__get_cpu_var(msm_clocks_percpu)[clock->index];
771 uint32_t alarm;
772 uint32_t count;
773 int32_t delta;
774
775 BUG_ON(clock != &msm_clocks[MSM_CLOCK_GPT] &&
776 clock != &msm_clocks[MSM_CLOCK_DGT]);
777
778 msm_timer_sync_gpt_to_sclk(0);
779 if (clock != gpt_clk)
780 msm_timer_sync_to_gpt(clock, 0);
781
782 count = msm_read_timer_count(clock, LOCAL_TIMER);
783 if (clock_state->stopped++ == 0)
784 clock_state->stopped_tick = count + clock_state->sleep_offset;
785 alarm = clock_state->alarm;
786 delta = alarm - count;
787 if (delta <= -(int32_t)((clock->freq << clock->shift) >> 10)) {
788 /* timer should have triggered 1ms ago */
789 printk(KERN_ERR "msm_timer_enter_idle: timer late %d, "
790 "reprogram it\n", delta);
791 msm_timer_reactivate_alarm(clock);
792 }
793 if (delta <= 0)
794 return 0;
795 return clocksource_cyc2ns((alarm - count) >> clock->shift,
796 clock->clocksource.mult,
797 clock->clocksource.shift);
798}
799
800void msm_timer_exit_idle(int low_power)
801{
802 struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT];
803 struct msm_clock *clock = __get_cpu_var(msm_active_clock);
804 struct msm_clock_percpu_data *gpt_clk_state =
805 &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT];
806 struct msm_clock_percpu_data *clock_state =
807 &__get_cpu_var(msm_clocks_percpu)[clock->index];
808 uint32_t enabled;
809
810 BUG_ON(clock != &msm_clocks[MSM_CLOCK_GPT] &&
811 clock != &msm_clocks[MSM_CLOCK_DGT]);
812
813 if (!low_power)
814 goto exit_idle_exit;
815
816 enabled = __raw_readl(gpt_clk->regbase + TIMER_ENABLE) &
817 TIMER_ENABLE_EN;
818 if (!enabled)
819 __raw_writel(TIMER_ENABLE_EN, gpt_clk->regbase + TIMER_ENABLE);
820
821#if defined(CONFIG_ARCH_MSM_SCORPION) || defined(CONFIG_ARCH_MSM_KRAIT)
822 gpt_clk_state->in_sync = 0;
823#else
824 gpt_clk_state->in_sync = gpt_clk_state->in_sync && enabled;
825#endif
826 /* Make sure timer is actually enabled before we sync it */
827 wmb();
828 msm_timer_sync_gpt_to_sclk(1);
829
830 if (clock == gpt_clk)
831 goto exit_idle_alarm;
832
833 enabled = __raw_readl(clock->regbase + TIMER_ENABLE) & TIMER_ENABLE_EN;
834 if (!enabled)
835 __raw_writel(TIMER_ENABLE_EN, clock->regbase + TIMER_ENABLE);
836
837#if defined(CONFIG_ARCH_MSM_SCORPION) || defined(CONFIG_ARCH_MSM_KRAIT)
838 clock_state->in_sync = 0;
839#else
840 clock_state->in_sync = clock_state->in_sync && enabled;
841#endif
842 /* Make sure timer is actually enabled before we sync it */
843 wmb();
844 msm_timer_sync_to_gpt(clock, 1);
845
846exit_idle_alarm:
847 msm_timer_reactivate_alarm(clock);
848
849exit_idle_exit:
850 clock_state->stopped--;
851}
852
853/*
854 * Callback function that initializes the timeout value.
855 */
856static void msm_timer_get_sclk_time_start(
857 struct msm_timer_sync_data_t *data)
858{
859 data->timeout = 200000;
860}
861
862/*
863 * Callback function that checks the timeout.
864 */
865static bool msm_timer_get_sclk_time_expired(
866 struct msm_timer_sync_data_t *data)
867{
868 udelay(10);
869 return --data->timeout <= 0;
870}
871
872/*
873 * Retrieve the cycle count from the sclk and convert it into
874 * nanoseconds.
875 *
876 * On exit, if period is not NULL, it contains the period of the
877 * sclk in nanoseconds, i.e. how long the cycle count wraps around.
878 *
879 * Return value:
880 * 0: the operation failed; period is not set either
881 * >0: time in nanoseconds
882 */
883int64_t msm_timer_get_sclk_time(int64_t *period)
884{
885 struct msm_timer_sync_data_t data;
886 uint32_t clock_value;
887 int64_t tmp;
888
889 memset(&data, 0, sizeof(data));
890 clock_value = msm_timer_do_sync_to_sclk(
891 msm_timer_get_sclk_time_start,
892 msm_timer_get_sclk_time_expired,
893 NULL,
894 &data);
895
896 if (!clock_value)
897 return 0;
898
899 if (period) {
900 tmp = 1LL << 32;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700901 tmp *= NSEC_PER_SEC;
902 do_div(tmp, sclk_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700903 *period = tmp;
904 }
905
906 tmp = (int64_t)clock_value;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700907 tmp *= NSEC_PER_SEC;
908 do_div(tmp, sclk_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700909 return tmp;
910}
911
912int __init msm_timer_init_time_sync(void (*timeout)(void))
913{
914#if defined(CONFIG_MSM_N_WAY_SMSM) && !defined(CONFIG_MSM_DIRECT_SCLK_ACCESS)
915 int ret = smsm_change_intr_mask(SMSM_TIME_MASTER_DEM, 0xFFFFFFFF, 0);
916
917 if (ret) {
918 printk(KERN_ERR "%s: failed to clear interrupt mask, %d\n",
919 __func__, ret);
920 return ret;
921 }
922
923 smsm_change_state(SMSM_APPS_DEM,
924 SLAVE_TIME_REQUEST | SLAVE_TIME_POLL, SLAVE_TIME_INIT);
925#endif
926
927 BUG_ON(timeout == NULL);
928 msm_timer_sync_timeout = timeout;
929
930 return 0;
931}
932
933#endif
934
Steve Mucklef132c6c2012-06-06 18:30:57 -0700935static u32 notrace msm_read_sched_clock(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700936{
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700937 struct msm_clock *clock = &msm_clocks[msm_global_timer];
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700938 struct clocksource *cs = &clock->clocksource;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700939 return cs->read(NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700940}
941
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700942int read_current_timer(unsigned long *timer_val)
943{
944 struct msm_clock *dgt = &msm_clocks[MSM_CLOCK_DGT];
945 *timer_val = msm_read_timer_count(dgt, GLOBAL_TIMER);
946 return 0;
947}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700948
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700949static void __init msm_sched_clock_init(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
Steve Mucklef132c6c2012-06-06 18:30:57 -0700953 setup_sched_clock(msm_read_sched_clock, 32 - clock->shift, clock->freq);
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700954}
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800955
Marc Zyngier5ca709c2012-01-10 19:44:19 +0000956#ifdef CONFIG_LOCAL_TIMERS
Steve Mucklef132c6c2012-06-06 18:30:57 -0700957int __cpuinit local_timer_setup(struct clock_event_device *evt)
Marc Zyngier5ca709c2012-01-10 19:44:19 +0000958{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700959 static DEFINE_PER_CPU(bool, first_boot) = true;
960 struct msm_clock *clock = &msm_clocks[msm_global_timer];
961
Marc Zyngier5ca709c2012-01-10 19:44:19 +0000962 /* Use existing clock_event for cpu 0 */
963 if (!smp_processor_id())
964 return 0;
965
Steve Mucklef132c6c2012-06-06 18:30:57 -0700966 if (cpu_is_msm8x60() || cpu_is_msm8960() || cpu_is_apq8064()
967 || cpu_is_msm8930())
968 __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
969
970 if (__get_cpu_var(first_boot)) {
971 __raw_writel(0, clock->regbase + TIMER_ENABLE);
972 __raw_writel(0, clock->regbase + TIMER_CLEAR);
973 __raw_writel(~0, clock->regbase + TIMER_MATCH_VAL);
974 __get_cpu_var(first_boot) = false;
975 if (clock->status_mask)
976 while (__raw_readl(MSM_TMR_BASE + TIMER_STATUS) &
977 clock->status_mask)
978 ;
979 }
980 evt->irq = clock->irq;
Marc Zyngier5ca709c2012-01-10 19:44:19 +0000981 evt->name = "local_timer";
Steve Mucklef132c6c2012-06-06 18:30:57 -0700982 evt->features = CLOCK_EVT_FEAT_ONESHOT;
983 evt->rating = clock->clockevent.rating;
Marc Zyngier5ca709c2012-01-10 19:44:19 +0000984 evt->set_mode = msm_timer_set_mode;
985 evt->set_next_event = msm_timer_set_next_event;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700986 evt->shift = clock->clockevent.shift;
987 evt->mult = div_sc(clock->freq, NSEC_PER_SEC, evt->shift);
988 evt->max_delta_ns =
989 clockevent_delta2ns(0xf0000000 >> clock->shift, evt);
Marc Zyngier5ca709c2012-01-10 19:44:19 +0000990 evt->min_delta_ns = clockevent_delta2ns(4, evt);
991
Steve Mucklef132c6c2012-06-06 18:30:57 -0700992 *__this_cpu_ptr(clock->percpu_evt) = evt;
993
Marc Zyngier5ca709c2012-01-10 19:44:19 +0000994 clockevents_register_device(evt);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700995 enable_percpu_irq(evt->irq, IRQ_TYPE_EDGE_RISING);
996
Marc Zyngier5ca709c2012-01-10 19:44:19 +0000997 return 0;
998}
999
Steve Mucklef132c6c2012-06-06 18:30:57 -07001000void local_timer_stop(struct clock_event_device *evt)
Marc Zyngier5ca709c2012-01-10 19:44:19 +00001001{
1002 evt->set_mode(CLOCK_EVT_MODE_UNUSED, evt);
1003 disable_percpu_irq(evt->irq);
1004}
1005
Steve Mucklef132c6c2012-06-06 18:30:57 -07001006static struct local_timer_ops msm_lt_ops = {
1007 local_timer_setup,
1008 local_timer_stop,
Marc Zyngier5ca709c2012-01-10 19:44:19 +00001009};
1010#endif /* CONFIG_LOCAL_TIMERS */
1011
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001012static void __init msm_timer_init(void)
1013{
1014 int i;
1015 int res;
Jin Hongeecb1e02011-10-21 14:36:32 -07001016 struct irq_chip *chip;
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001017 struct msm_clock *dgt = &msm_clocks[MSM_CLOCK_DGT];
1018 struct msm_clock *gpt = &msm_clocks[MSM_CLOCK_GPT];
Stephen Boyddd15ab82011-11-08 10:34:05 -08001019
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001020 if (cpu_is_msm7x01() || cpu_is_msm7x25() || cpu_is_msm7x27() ||
1021 cpu_is_msm7x25a() || cpu_is_msm7x27a() || cpu_is_msm7x25aa() ||
Pankaj Kumarfee56a82012-04-17 14:26:49 +05301022 cpu_is_msm7x27aa() || cpu_is_msm8625() || cpu_is_msm7x25ab()) {
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001023 dgt->shift = MSM_DGT_SHIFT;
1024 dgt->freq = 19200000 >> MSM_DGT_SHIFT;
1025 dgt->clockevent.shift = 32 + MSM_DGT_SHIFT;
1026 dgt->clocksource.mask = CLOCKSOURCE_MASK(32 - MSM_DGT_SHIFT);
1027 dgt->clocksource.shift = 24 - MSM_DGT_SHIFT;
1028 gpt->regbase = MSM_TMR_BASE;
1029 dgt->regbase = MSM_TMR_BASE + 0x10;
Jeff Ohlstein5d90e252011-11-04 19:00:50 -07001030 gpt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT
1031 | MSM_CLOCK_FLAGS_ODD_MATCH_WRITE
1032 | MSM_CLOCK_FLAGS_DELAYED_WRITE_POST;
Taniya Das5eb25142011-11-17 21:53:34 +05301033 if (cpu_is_msm8625()) {
1034 dgt->irq = MSM8625_INT_DEBUG_TIMER_EXP;
1035 gpt->irq = MSM8625_INT_GP_TIMER_EXP;
1036 global_timer_offset = MSM_TMR0_BASE - MSM_TMR_BASE;
Marc Zyngier28af6902011-07-22 12:52:37 +01001037 }
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001038 } else if (cpu_is_qsd8x50()) {
1039 dgt->freq = 4800000;
1040 gpt->regbase = MSM_TMR_BASE;
1041 dgt->regbase = MSM_TMR_BASE + 0x10;
1042 } else if (cpu_is_fsm9xxx())
1043 dgt->freq = 4800000;
Jeff Ohlstein6c47a272012-02-24 14:48:55 -08001044 else if (cpu_is_msm7x30() || cpu_is_msm8x55()) {
1045 gpt->status_mask = BIT(10);
1046 dgt->status_mask = BIT(2);
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001047 dgt->freq = 6144000;
Jeff Ohlstein6c47a272012-02-24 14:48:55 -08001048 } else if (cpu_is_msm8x60()) {
Jeff Ohlstein7e538f02011-11-01 17:36:22 -07001049 global_timer_offset = MSM_TMR0_BASE - MSM_TMR_BASE;
Jeff Ohlstein6c47a272012-02-24 14:48:55 -08001050 gpt->status_mask = BIT(10);
1051 dgt->status_mask = BIT(2);
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001052 dgt->freq = 6750000;
1053 __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
Jeff Ohlstein7e538f02011-11-01 17:36:22 -07001054 } else if (cpu_is_msm9615()) {
1055 dgt->freq = 6750000;
1056 __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
Jeff Ohlstein6c47a272012-02-24 14:48:55 -08001057 gpt->status_mask = BIT(10);
1058 dgt->status_mask = BIT(2);
Jeff Ohlstein7e538f02011-11-01 17:36:22 -07001059 gpt->freq = 32765;
1060 gpt_hz = 32765;
1061 sclk_hz = 32765;
Jeff Ohlsteind47f96a2011-11-04 19:00:50 -07001062 gpt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT;
1063 dgt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT;
Jeff Ohlstein7e538f02011-11-01 17:36:22 -07001064 } else if (cpu_is_msm8960() || cpu_is_apq8064() || cpu_is_msm8930()) {
1065 global_timer_offset = MSM_TMR0_BASE - MSM_TMR_BASE;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -07001066 dgt->freq = 6750000;
1067 __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
Jeff Ohlstein6c47a272012-02-24 14:48:55 -08001068 gpt->status_mask = BIT(10);
1069 dgt->status_mask = BIT(2);
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -07001070 gpt->freq = 32765;
1071 gpt_hz = 32765;
1072 sclk_hz = 32765;
Jeff Ohlstein42d59202012-03-28 15:39:22 -07001073 if (!cpu_is_msm8930()) {
Jeff Ohlstein391a3ee2011-12-01 16:44:45 -08001074 gpt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT;
1075 dgt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT;
Marc Zyngier5ca709c2012-01-10 19:44:19 +00001076 }
Stephen Boyddd15ab82011-11-08 10:34:05 -08001077 } else {
Jeff Ohlsteinf0a31e42012-01-06 19:03:05 -08001078 WARN(1, "Timer running on unknown hardware. Configure this! "
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001079 "Assuming default configuration.\n");
1080 dgt->freq = 6750000;
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001081 }
Stephen Boyddd15ab82011-11-08 10:34:05 -08001082
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001083 if (msm_clocks[MSM_CLOCK_GPT].clocksource.rating > DG_TIMER_RATING)
1084 msm_global_timer = MSM_CLOCK_GPT;
1085 else
1086 msm_global_timer = MSM_CLOCK_DGT;
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001087
1088 for (i = 0; i < ARRAY_SIZE(msm_clocks); i++) {
1089 struct msm_clock *clock = &msm_clocks[i];
1090 struct clock_event_device *ce = &clock->clockevent;
1091 struct clocksource *cs = &clock->clocksource;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001092 __raw_writel(0, clock->regbase + TIMER_ENABLE);
Jeff Ohlstein6c47a272012-02-24 14:48:55 -08001093 __raw_writel(0, clock->regbase + TIMER_CLEAR);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001094 __raw_writel(~0, clock->regbase + TIMER_MATCH_VAL);
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001095
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -07001096 if ((clock->freq << clock->shift) == gpt_hz) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001097 clock->rollover_offset = 0;
1098 } else {
1099 uint64_t temp;
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001100
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001101 temp = clock->freq << clock->shift;
1102 temp <<= 32;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -07001103 do_div(temp, gpt_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001104
1105 clock->rollover_offset = (uint32_t) temp;
1106 }
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001107
1108 ce->mult = div_sc(clock->freq, NSEC_PER_SEC, ce->shift);
1109 /* allow at least 10 seconds to notice that the timer wrapped */
1110 ce->max_delta_ns =
1111 clockevent_delta2ns(0xf0000000 >> clock->shift, ce);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001112 /* ticks gets rounded down by one */
1113 ce->min_delta_ns =
1114 clockevent_delta2ns(clock->write_delay + 4, ce);
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001115 ce->cpumask = cpumask_of(0);
1116
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001117 cs->mult = clocksource_hz2mult(clock->freq, cs->shift);
1118 res = clocksource_register(cs);
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001119 if (res)
1120 printk(KERN_ERR "msm_timer_init: clocksource_register "
1121 "failed for %s\n", cs->name);
1122
Trilok Sonieecb28c2011-07-20 16:24:14 +01001123 ce->irq = clock->irq;
1124 if (cpu_is_msm8x60() || cpu_is_msm8960() || cpu_is_apq8064() ||
Taniya Das5eb25142011-11-17 21:53:34 +05301125 cpu_is_msm8930() || cpu_is_msm9615() ||
1126 cpu_is_msm8625()) {
Trilok Sonieecb28c2011-07-20 16:24:14 +01001127 clock->percpu_evt = alloc_percpu(struct clock_event_device *);
1128 if (!clock->percpu_evt) {
1129 pr_err("msm_timer_init: memory allocation "
1130 "failed for %s\n", ce->name);
1131 continue;
1132 }
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001133
Trilok Sonieecb28c2011-07-20 16:24:14 +01001134 *__this_cpu_ptr(clock->percpu_evt) = ce;
1135 res = request_percpu_irq(ce->irq, msm_timer_interrupt,
1136 ce->name, clock->percpu_evt);
1137 if (!res)
Trilok Soni1e52e432012-01-13 18:06:14 +05301138 enable_percpu_irq(ce->irq,
1139 IRQ_TYPE_EDGE_RISING);
Trilok Sonieecb28c2011-07-20 16:24:14 +01001140 } else {
1141 clock->evt = ce;
1142 res = request_irq(ce->irq, msm_timer_interrupt,
1143 IRQF_TIMER | IRQF_NOBALANCING | IRQF_TRIGGER_RISING,
1144 ce->name, &clock->evt);
1145 }
1146
1147 if (res)
1148 pr_err("msm_timer_init: request_irq failed for %s\n",
1149 ce->name);
1150
1151 chip = irq_get_chip(clock->irq);
Jin Hongeecb1e02011-10-21 14:36:32 -07001152 if (chip && chip->irq_mask)
Trilok Sonieecb28c2011-07-20 16:24:14 +01001153 chip->irq_mask(irq_get_irq_data(clock->irq));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001154
Jeff Ohlstein6c47a272012-02-24 14:48:55 -08001155 if (clock->status_mask)
1156 while (__raw_readl(MSM_TMR_BASE + TIMER_STATUS) &
1157 clock->status_mask)
1158 ;
1159
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001160 clockevents_register_device(ce);
1161 }
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -07001162 msm_sched_clock_init();
Taniya Das36057be2011-10-28 13:02:17 +05301163
Taniya Dasc43e6872012-03-21 16:41:14 +05301164#ifdef ARCH_HAS_READ_CURRENT_TIMER
1165 if (is_smp()) {
Taniya Dasbb0b6db2012-03-19 14:09:55 +05301166 __raw_writel(1,
1167 msm_clocks[MSM_CLOCK_DGT].regbase + TIMER_ENABLE);
1168 set_delay_fn(read_current_timer_delay_loop);
1169 }
Taniya Dasc43e6872012-03-21 16:41:14 +05301170#endif
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001171
Steve Mucklef132c6c2012-06-06 18:30:57 -07001172#ifdef CONFIG_LOCAL_TIMERS
1173 local_timer_register(&msm_lt_ops);
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001174#endif
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001175}
1176
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001177struct sys_timer msm_timer = {
1178 .init = msm_timer_init
1179};