blob: 795e1d69bdabf81edde9b3ee92b77607674a3c81 [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.
Jay Chokshi7935fae2013-03-12 11:37:18 -07003 * Copyright (c) 2009-2013, The Linux Foundation. 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),
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800163 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
164 },
Trilok Sonieecb28c2011-07-20 16:24:14 +0100165 .irq = INT_GP_TIMER_EXP,
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700166 .regbase = MSM_TMR_BASE + 0x4,
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700167 .freq = 32768,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700168 .index = MSM_CLOCK_GPT,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700169 .write_delay = 9,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800170 },
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800171 [MSM_CLOCK_DGT] = {
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800172 .clockevent = {
173 .name = "dg_timer",
174 .features = CLOCK_EVT_FEAT_ONESHOT,
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700175 .shift = 32,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700176 .rating = DG_TIMER_RATING,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800177 .set_next_event = msm_timer_set_next_event,
178 .set_mode = msm_timer_set_mode,
179 },
180 .clocksource = {
181 .name = "dg_timer",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700182 .rating = DG_TIMER_RATING,
183 .read = msm_dgt_read,
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700184 .mask = CLOCKSOURCE_MASK(32),
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800185 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
186 },
Trilok Sonieecb28c2011-07-20 16:24:14 +0100187 .irq = INT_DEBUG_TIMER_EXP,
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700188 .regbase = MSM_TMR_BASE + 0x24,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700189 .index = MSM_CLOCK_DGT,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700190 .write_delay = 9,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800191 }
192};
193
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700194static DEFINE_PER_CPU(struct msm_clock_percpu_data[NR_TIMERS],
195 msm_clocks_percpu);
196
197static DEFINE_PER_CPU(struct msm_clock *, msm_active_clock);
Stephen Boyda850c3f2011-11-08 10:34:06 -0800198
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800199static irqreturn_t msm_timer_interrupt(int irq, void *dev_id)
200{
Marc Zyngier28af6902011-07-22 12:52:37 +0100201 struct clock_event_device *evt = *(struct clock_event_device **)dev_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700202 if (evt->event_handler == NULL)
203 return IRQ_HANDLED;
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800204 evt->event_handler(evt);
205 return IRQ_HANDLED;
206}
207
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700208static uint32_t msm_read_timer_count(struct msm_clock *clock, int global)
209{
Jeff Ohlstein5d90e252011-11-04 19:00:50 -0700210 uint32_t t1, t2, t3;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700211 int loop_count = 0;
Jeff Ohlstein5d90e252011-11-04 19:00:50 -0700212 void __iomem *addr = clock->regbase + TIMER_COUNT_VAL +
213 global*global_timer_offset;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700214
215 if (!(clock->flags & MSM_CLOCK_FLAGS_UNSTABLE_COUNT))
Jeff Ohlstein60b68702012-03-30 16:35:25 -0700216 return __raw_readl_no_log(addr);
Jeff Ohlstein5d90e252011-11-04 19:00:50 -0700217
Jeff Ohlstein60b68702012-03-30 16:35:25 -0700218 t1 = __raw_readl_no_log(addr);
Laura Abbott1d506042012-01-23 13:21:34 -0800219 t2 = __raw_readl_no_log(addr);
Jeff Ohlstein5d90e252011-11-04 19:00:50 -0700220 if ((t2-t1) <= 1)
221 return t2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700222 while (1) {
Laura Abbott1d506042012-01-23 13:21:34 -0800223 t1 = __raw_readl_no_log(addr);
224 t2 = __raw_readl_no_log(addr);
225 t3 = __raw_readl_no_log(addr);
Jeff Ohlstein10206eb2011-11-30 19:18:49 -0800226 cpu_relax();
Jeff Ohlstein5d90e252011-11-04 19:00:50 -0700227 if ((t3-t2) <= 1)
228 return t3;
229 if ((t2-t1) <= 1)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700230 return t2;
Jeff Ohlsteinfdd87082011-12-09 13:40:08 -0800231 if ((t2 >= t1) && (t3 >= t2))
232 return t2;
Jeff Ohlstein10206eb2011-11-30 19:18:49 -0800233 if (++loop_count == 5) {
Jeff Ohlstein5d90e252011-11-04 19:00:50 -0700234 pr_err("msm_read_timer_count timer %s did not "
235 "stabilize: %u -> %u -> %u\n",
236 clock->clockevent.name, t1, t2, t3);
237 return t3;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700238 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700239 }
240}
241
242static cycle_t msm_gpt_read(struct clocksource *cs)
243{
244 struct msm_clock *clock = &msm_clocks[MSM_CLOCK_GPT];
245 struct msm_clock_percpu_data *clock_state =
246 &per_cpu(msm_clocks_percpu, 0)[MSM_CLOCK_GPT];
247
248 if (clock_state->stopped)
249 return clock_state->stopped_tick;
250
251 return msm_read_timer_count(clock, GLOBAL_TIMER) +
252 clock_state->sleep_offset;
253}
254
255static cycle_t msm_dgt_read(struct clocksource *cs)
256{
257 struct msm_clock *clock = &msm_clocks[MSM_CLOCK_DGT];
258 struct msm_clock_percpu_data *clock_state =
259 &per_cpu(msm_clocks_percpu, 0)[MSM_CLOCK_DGT];
260
261 if (clock_state->stopped)
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700262 return clock_state->stopped_tick >> clock->shift;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700263
264 return (msm_read_timer_count(clock, GLOBAL_TIMER) +
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700265 clock_state->sleep_offset) >> clock->shift;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700266}
267
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700268static struct msm_clock *clockevent_to_clock(struct clock_event_device *evt)
269{
270 int i;
Taniya Das36057be2011-10-28 13:02:17 +0530271
272 if (!is_smp())
273 return container_of(evt, struct msm_clock, clockevent);
274
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700275 for (i = 0; i < NR_TIMERS; i++)
276 if (evt == &(msm_clocks[i].clockevent))
277 return &msm_clocks[i];
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700278 return &msm_clocks[msm_global_timer];
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700279}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700280
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800281static int msm_timer_set_next_event(unsigned long cycles,
282 struct clock_event_device *evt)
283{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700284 int i;
285 struct msm_clock *clock;
286 struct msm_clock_percpu_data *clock_state;
287 uint32_t now;
288 uint32_t alarm;
289 int late;
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800290
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700291 clock = clockevent_to_clock(evt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700292 clock_state = &__get_cpu_var(msm_clocks_percpu)[clock->index];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700293 now = msm_read_timer_count(clock, LOCAL_TIMER);
294 alarm = now + (cycles << clock->shift);
295 if (clock->flags & MSM_CLOCK_FLAGS_ODD_MATCH_WRITE)
296 while (now == clock_state->last_set)
297 now = msm_read_timer_count(clock, LOCAL_TIMER);
298
299 clock_state->alarm = alarm;
300 __raw_writel(alarm, clock->regbase + TIMER_MATCH_VAL);
301
302 if (clock->flags & MSM_CLOCK_FLAGS_DELAYED_WRITE_POST) {
303 /* read the counter four extra times to make sure write posts
304 before reading the time */
305 for (i = 0; i < 4; i++)
Laura Abbott1d506042012-01-23 13:21:34 -0800306 __raw_readl_no_log(clock->regbase + TIMER_COUNT_VAL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700307 }
308 now = msm_read_timer_count(clock, LOCAL_TIMER);
309 clock_state->last_set = now;
310 clock_state->alarm_vtime = alarm + clock_state->sleep_offset;
311 late = now - alarm;
312 if (late >= (int)(-clock->write_delay << clock->shift) &&
313 late < clock->freq*5)
314 return -ETIME;
315
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800316 return 0;
317}
318
319static void msm_timer_set_mode(enum clock_event_mode mode,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700320 struct clock_event_device *evt)
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800321{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700322 struct msm_clock *clock;
Steve Muckled599fda2012-05-20 21:38:02 -0700323 struct msm_clock **cur_clock;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700324 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;
Stephen Boyda850c3f2011-11-08 10:34:06 -0800327
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);
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800333
334 switch (mode) {
335 case CLOCK_EVT_MODE_RESUME:
336 case CLOCK_EVT_MODE_PERIODIC:
337 break;
338 case CLOCK_EVT_MODE_ONESHOT:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700339 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);
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800353 break;
354 case CLOCK_EVT_MODE_UNUSED:
355 case CLOCK_EVT_MODE_SHUTDOWN:
Steve Muckled599fda2012-05-20 21:38:02 -0700356 cur_clock = &get_cpu_var(msm_active_clock);
357 if (*cur_clock == clock)
358 *cur_clock = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700359 put_cpu_var(msm_active_clock);
360 clock_state->in_sync = 0;
361 clock_state->stopped = 1;
362 clock_state->stopped_tick =
363 msm_read_timer_count(clock, LOCAL_TIMER) +
364 clock_state->sleep_offset;
365 __raw_writel(0, clock->regbase + TIMER_MATCH_VAL);
Trilok Sonieecb28c2011-07-20 16:24:14 +0100366 chip = irq_get_chip(clock->irq);
Jin Hongeecb1e02011-10-21 14:36:32 -0700367 if (chip && chip->irq_mask)
Trilok Sonieecb28c2011-07-20 16:24:14 +0100368 chip->irq_mask(irq_get_irq_data(clock->irq));
Taniya Das36057be2011-10-28 13:02:17 +0530369
370 if (!is_smp() || clock != &msm_clocks[MSM_CLOCK_DGT]
371 || smp_processor_id())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700372 __raw_writel(0, clock->regbase + TIMER_ENABLE);
Taniya Das36057be2011-10-28 13:02:17 +0530373
Steve Mucklef132c6c2012-06-06 18:30:57 -0700374 if (msm_global_timer == MSM_CLOCK_DGT &&
375 clock != &msm_clocks[MSM_CLOCK_GPT]) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700376 gpt_state->in_sync = 0;
377 __raw_writel(0, msm_clocks[MSM_CLOCK_GPT].regbase +
378 TIMER_ENABLE);
379 }
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800380 break;
381 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700382 wmb();
383 local_irq_restore(irq_flags);
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800384}
385
Jeff Ohlstein973871d2011-09-28 11:46:26 -0700386void __iomem *msm_timer_get_timer0_base(void)
Stephen Boyd2a00c102011-11-08 10:34:07 -0800387{
Jeff Ohlstein973871d2011-09-28 11:46:26 -0700388 return MSM_TMR_BASE + global_timer_offset;
Stephen Boyd2081a6b2011-11-08 10:34:08 -0800389}
390
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700391#define MPM_SCLK_COUNT_VAL 0x0024
392
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700393#ifdef CONFIG_PM
394/*
395 * Retrieve the cycle count from sclk and optionally synchronize local clock
396 * with the sclk value.
397 *
398 * time_start and time_expired are callbacks that must be specified. The
399 * protocol uses them to detect timeout. The update callback is optional.
400 * If not NULL, update will be called so that it can update local clock.
401 *
402 * The function does not use the argument data directly; it passes data to
403 * the callbacks.
404 *
405 * Return value:
406 * 0: the operation failed
407 * >0: the slow clock value after time-sync
408 */
409static void (*msm_timer_sync_timeout)(void);
410#if defined(CONFIG_MSM_DIRECT_SCLK_ACCESS)
Jeff Ohlsteinecefdc02012-01-13 12:37:44 -0800411uint32_t msm_timer_get_sclk_ticks(void)
Stephen Boyd2081a6b2011-11-08 10:34:08 -0800412{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700413 uint32_t t1, t2;
414 int loop_count = 10;
415 int loop_zero_count = 3;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700416 int tmp = USEC_PER_SEC;
417 do_div(tmp, sclk_hz);
418 tmp /= (loop_zero_count-1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700419
420 while (loop_zero_count--) {
Laura Abbott1d506042012-01-23 13:21:34 -0800421 t1 = __raw_readl_no_log(MSM_RPM_MPM_BASE + MPM_SCLK_COUNT_VAL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700422 do {
423 udelay(1);
424 t2 = t1;
Laura Abbott1d506042012-01-23 13:21:34 -0800425 t1 = __raw_readl_no_log(
426 MSM_RPM_MPM_BASE + MPM_SCLK_COUNT_VAL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700427 } 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
Jeff Ohlsteinecefdc02012-01-13 12:37:44 -0800445 return t1;
Stephen Boyd2a00c102011-11-08 10:34:07 -0800446}
447
Jeff Ohlsteinecefdc02012-01-13 12:37:44 -0800448static uint32_t msm_timer_do_sync_to_sclk(
449 void (*time_start)(struct msm_timer_sync_data_t *data),
450 bool (*time_expired)(struct msm_timer_sync_data_t *data),
451 void (*update)(struct msm_timer_sync_data_t *, uint32_t, uint32_t),
452 struct msm_timer_sync_data_t *data)
453{
454 unsigned t1 = msm_timer_get_sclk_ticks();
455
456 if (t1 && update != NULL)
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700457 update(data, t1, sclk_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700458 return t1;
459}
460#elif defined(CONFIG_MSM_N_WAY_SMSM)
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700461
462/* Time Master State Bits */
463#define MASTER_BITS_PER_CPU 1
464#define MASTER_TIME_PENDING \
465 (0x01UL << (MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
466
467/* Time Slave State Bits */
468#define SLAVE_TIME_REQUEST 0x0400
469#define SLAVE_TIME_POLL 0x0800
470#define SLAVE_TIME_INIT 0x1000
471
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700472static uint32_t msm_timer_do_sync_to_sclk(
473 void (*time_start)(struct msm_timer_sync_data_t *data),
474 bool (*time_expired)(struct msm_timer_sync_data_t *data),
475 void (*update)(struct msm_timer_sync_data_t *, uint32_t, uint32_t),
476 struct msm_timer_sync_data_t *data)
477{
478 uint32_t *smem_clock;
479 uint32_t smem_clock_val;
480 uint32_t state;
481
482 smem_clock = smem_alloc(SMEM_SMEM_SLOW_CLOCK_VALUE, sizeof(uint32_t));
483 if (smem_clock == NULL) {
484 printk(KERN_ERR "no smem clock\n");
485 return 0;
486 }
487
488 state = smsm_get_state(SMSM_MODEM_STATE);
489 if ((state & SMSM_INIT) == 0) {
490 printk(KERN_ERR "smsm not initialized\n");
491 return 0;
492 }
493
494 time_start(data);
495 while ((state = smsm_get_state(SMSM_TIME_MASTER_DEM)) &
496 MASTER_TIME_PENDING) {
497 if (time_expired(data)) {
498 printk(KERN_EMERG "get_smem_clock: timeout 1 still "
499 "invalid state %x\n", state);
500 msm_timer_sync_timeout();
501 }
502 }
503
504 smsm_change_state(SMSM_APPS_DEM, SLAVE_TIME_POLL | SLAVE_TIME_INIT,
505 SLAVE_TIME_REQUEST);
506
507 time_start(data);
508 while (!((state = smsm_get_state(SMSM_TIME_MASTER_DEM)) &
509 MASTER_TIME_PENDING)) {
510 if (time_expired(data)) {
511 printk(KERN_EMERG "get_smem_clock: timeout 2 still "
512 "invalid state %x\n", state);
513 msm_timer_sync_timeout();
514 }
515 }
516
517 smsm_change_state(SMSM_APPS_DEM, SLAVE_TIME_REQUEST, SLAVE_TIME_POLL);
518
519 time_start(data);
520 do {
521 smem_clock_val = *smem_clock;
522 } while (smem_clock_val == 0 && !time_expired(data));
523
524 state = smsm_get_state(SMSM_TIME_MASTER_DEM);
525
526 if (smem_clock_val) {
527 if (update != NULL)
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700528 update(data, smem_clock_val, sclk_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700529
530 if (msm_timer_debug_mask & MSM_TIMER_DEBUG_SYNC)
531 printk(KERN_INFO
532 "get_smem_clock: state %x clock %u\n",
533 state, smem_clock_val);
534 } else {
535 printk(KERN_EMERG
536 "get_smem_clock: timeout state %x clock %u\n",
537 state, smem_clock_val);
538 msm_timer_sync_timeout();
539 }
540
541 smsm_change_state(SMSM_APPS_DEM, SLAVE_TIME_REQUEST | SLAVE_TIME_POLL,
542 SLAVE_TIME_INIT);
543 return smem_clock_val;
544}
545#else /* CONFIG_MSM_N_WAY_SMSM */
546static uint32_t msm_timer_do_sync_to_sclk(
547 void (*time_start)(struct msm_timer_sync_data_t *data),
548 bool (*time_expired)(struct msm_timer_sync_data_t *data),
549 void (*update)(struct msm_timer_sync_data_t *, uint32_t, uint32_t),
550 struct msm_timer_sync_data_t *data)
551{
552 uint32_t *smem_clock;
553 uint32_t smem_clock_val;
554 uint32_t last_state;
555 uint32_t state;
556
557 smem_clock = smem_alloc(SMEM_SMEM_SLOW_CLOCK_VALUE,
558 sizeof(uint32_t));
559
560 if (smem_clock == NULL) {
561 printk(KERN_ERR "no smem clock\n");
562 return 0;
563 }
564
565 last_state = state = smsm_get_state(SMSM_MODEM_STATE);
566 smem_clock_val = *smem_clock;
567 if (smem_clock_val) {
568 printk(KERN_INFO "get_smem_clock: invalid start state %x "
569 "clock %u\n", state, smem_clock_val);
570 smsm_change_state(SMSM_APPS_STATE,
571 SMSM_TIMEWAIT, SMSM_TIMEINIT);
572
573 time_start(data);
574 while (*smem_clock != 0 && !time_expired(data))
575 ;
576
577 smem_clock_val = *smem_clock;
578 if (smem_clock_val) {
579 printk(KERN_EMERG "get_smem_clock: timeout still "
580 "invalid state %x clock %u\n",
581 state, smem_clock_val);
582 msm_timer_sync_timeout();
583 }
584 }
585
586 time_start(data);
587 smsm_change_state(SMSM_APPS_STATE, SMSM_TIMEINIT, SMSM_TIMEWAIT);
588 do {
589 smem_clock_val = *smem_clock;
590 state = smsm_get_state(SMSM_MODEM_STATE);
591 if (state != last_state) {
592 last_state = state;
593 if (msm_timer_debug_mask & MSM_TIMER_DEBUG_SYNC)
594 printk(KERN_INFO
595 "get_smem_clock: state %x clock %u\n",
596 state, smem_clock_val);
597 }
598 } while (smem_clock_val == 0 && !time_expired(data));
599
600 if (smem_clock_val) {
601 if (update != NULL)
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700602 update(data, smem_clock_val, sclk_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700603 } else {
604 printk(KERN_EMERG
605 "get_smem_clock: timeout state %x clock %u\n",
606 state, smem_clock_val);
607 msm_timer_sync_timeout();
608 }
609
610 smsm_change_state(SMSM_APPS_STATE, SMSM_TIMEWAIT, SMSM_TIMEINIT);
611 return smem_clock_val;
612}
613#endif /* CONFIG_MSM_N_WAY_SMSM */
614
615/*
616 * Callback function that initializes the timeout value.
617 */
618static void msm_timer_sync_to_sclk_time_start(
619 struct msm_timer_sync_data_t *data)
620{
621 /* approx 2 seconds */
622 uint32_t delta = data->clock->freq << data->clock->shift << 1;
623 data->timeout = msm_read_timer_count(data->clock, LOCAL_TIMER) + delta;
624}
625
626/*
627 * Callback function that checks the timeout.
628 */
629static bool msm_timer_sync_to_sclk_time_expired(
630 struct msm_timer_sync_data_t *data)
631{
632 uint32_t delta = msm_read_timer_count(data->clock, LOCAL_TIMER) -
633 data->timeout;
634 return ((int32_t) delta) > 0;
635}
636
637/*
638 * Callback function that updates local clock from the specified source clock
639 * value and frequency.
640 */
641static void msm_timer_sync_update(struct msm_timer_sync_data_t *data,
642 uint32_t src_clk_val, uint32_t src_clk_freq)
643{
644 struct msm_clock *dst_clk = data->clock;
645 struct msm_clock_percpu_data *dst_clk_state =
646 &__get_cpu_var(msm_clocks_percpu)[dst_clk->index];
647 uint32_t dst_clk_val = msm_read_timer_count(dst_clk, LOCAL_TIMER);
648 uint32_t new_offset;
649
650 if ((dst_clk->freq << dst_clk->shift) == src_clk_freq) {
651 new_offset = src_clk_val - dst_clk_val;
652 } else {
653 uint64_t temp;
654
655 /* separate multiplication and division steps to reduce
656 rounding error */
657 temp = src_clk_val;
658 temp *= dst_clk->freq << dst_clk->shift;
659 do_div(temp, src_clk_freq);
660
661 new_offset = (uint32_t)(temp) - dst_clk_val;
662 }
663
664 if (dst_clk_state->sleep_offset + dst_clk_state->non_sleep_offset !=
665 new_offset) {
666 if (data->exit_sleep)
667 dst_clk_state->sleep_offset =
668 new_offset - dst_clk_state->non_sleep_offset;
669 else
670 dst_clk_state->non_sleep_offset =
671 new_offset - dst_clk_state->sleep_offset;
672
673 if (msm_timer_debug_mask & MSM_TIMER_DEBUG_SYNC)
674 printk(KERN_INFO "sync clock %s: "
675 "src %u, new offset %u + %u\n",
676 dst_clk->clocksource.name, src_clk_val,
677 dst_clk_state->sleep_offset,
678 dst_clk_state->non_sleep_offset);
679 }
680}
681
682/*
683 * Synchronize GPT clock with sclk.
684 */
685static void msm_timer_sync_gpt_to_sclk(int exit_sleep)
686{
687 struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT];
688 struct msm_clock_percpu_data *gpt_clk_state =
689 &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT];
690 struct msm_timer_sync_data_t data;
691 uint32_t ret;
692
693 if (gpt_clk_state->in_sync)
694 return;
695
696 data.clock = gpt_clk;
697 data.timeout = 0;
698 data.exit_sleep = exit_sleep;
699
700 ret = msm_timer_do_sync_to_sclk(
701 msm_timer_sync_to_sclk_time_start,
702 msm_timer_sync_to_sclk_time_expired,
703 msm_timer_sync_update,
704 &data);
705
706 if (ret)
707 gpt_clk_state->in_sync = 1;
708}
709
710/*
711 * Synchronize clock with GPT clock.
712 */
713static void msm_timer_sync_to_gpt(struct msm_clock *clock, int exit_sleep)
714{
715 struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT];
716 struct msm_clock_percpu_data *gpt_clk_state =
717 &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT];
718 struct msm_clock_percpu_data *clock_state =
719 &__get_cpu_var(msm_clocks_percpu)[clock->index];
720 struct msm_timer_sync_data_t data;
721 uint32_t gpt_clk_val;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700722 u64 gpt_period = (1ULL << 32) * HZ;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700723 u64 now = get_jiffies_64();
724
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700725 do_div(gpt_period, gpt_hz);
726
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700727 BUG_ON(clock == gpt_clk);
728
729 if (clock_state->in_sync &&
730 (now - clock_state->last_sync_jiffies < (gpt_period >> 1)))
731 return;
732
733 gpt_clk_val = msm_read_timer_count(gpt_clk, LOCAL_TIMER)
734 + gpt_clk_state->sleep_offset + gpt_clk_state->non_sleep_offset;
735
736 if (exit_sleep && gpt_clk_val < clock_state->last_sync_gpt)
737 clock_state->non_sleep_offset -= clock->rollover_offset;
738
739 data.clock = clock;
740 data.timeout = 0;
741 data.exit_sleep = exit_sleep;
742
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700743 msm_timer_sync_update(&data, gpt_clk_val, gpt_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700744
745 clock_state->in_sync = 1;
746 clock_state->last_sync_gpt = gpt_clk_val;
747 clock_state->last_sync_jiffies = now;
748}
749
750static void msm_timer_reactivate_alarm(struct msm_clock *clock)
751{
752 struct msm_clock_percpu_data *clock_state =
753 &__get_cpu_var(msm_clocks_percpu)[clock->index];
754 long alarm_delta = clock_state->alarm_vtime -
755 clock_state->sleep_offset -
756 msm_read_timer_count(clock, LOCAL_TIMER);
757 alarm_delta >>= clock->shift;
758 if (alarm_delta < (long)clock->write_delay + 4)
759 alarm_delta = clock->write_delay + 4;
760 while (msm_timer_set_next_event(alarm_delta, &clock->clockevent))
761 ;
762}
763
764int64_t msm_timer_enter_idle(void)
765{
766 struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT];
767 struct msm_clock *clock = __get_cpu_var(msm_active_clock);
768 struct msm_clock_percpu_data *clock_state =
769 &__get_cpu_var(msm_clocks_percpu)[clock->index];
770 uint32_t alarm;
771 uint32_t count;
772 int32_t delta;
773
774 BUG_ON(clock != &msm_clocks[MSM_CLOCK_GPT] &&
775 clock != &msm_clocks[MSM_CLOCK_DGT]);
776
777 msm_timer_sync_gpt_to_sclk(0);
778 if (clock != gpt_clk)
779 msm_timer_sync_to_gpt(clock, 0);
780
781 count = msm_read_timer_count(clock, LOCAL_TIMER);
782 if (clock_state->stopped++ == 0)
783 clock_state->stopped_tick = count + clock_state->sleep_offset;
784 alarm = clock_state->alarm;
785 delta = alarm - count;
786 if (delta <= -(int32_t)((clock->freq << clock->shift) >> 10)) {
787 /* timer should have triggered 1ms ago */
788 printk(KERN_ERR "msm_timer_enter_idle: timer late %d, "
789 "reprogram it\n", delta);
790 msm_timer_reactivate_alarm(clock);
791 }
792 if (delta <= 0)
793 return 0;
794 return clocksource_cyc2ns((alarm - count) >> clock->shift,
795 clock->clocksource.mult,
796 clock->clocksource.shift);
797}
798
799void msm_timer_exit_idle(int low_power)
800{
801 struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT];
802 struct msm_clock *clock = __get_cpu_var(msm_active_clock);
803 struct msm_clock_percpu_data *gpt_clk_state =
804 &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT];
805 struct msm_clock_percpu_data *clock_state =
806 &__get_cpu_var(msm_clocks_percpu)[clock->index];
807 uint32_t enabled;
808
809 BUG_ON(clock != &msm_clocks[MSM_CLOCK_GPT] &&
810 clock != &msm_clocks[MSM_CLOCK_DGT]);
811
812 if (!low_power)
813 goto exit_idle_exit;
814
815 enabled = __raw_readl(gpt_clk->regbase + TIMER_ENABLE) &
816 TIMER_ENABLE_EN;
817 if (!enabled)
818 __raw_writel(TIMER_ENABLE_EN, gpt_clk->regbase + TIMER_ENABLE);
819
820#if defined(CONFIG_ARCH_MSM_SCORPION) || defined(CONFIG_ARCH_MSM_KRAIT)
821 gpt_clk_state->in_sync = 0;
822#else
823 gpt_clk_state->in_sync = gpt_clk_state->in_sync && enabled;
824#endif
825 /* Make sure timer is actually enabled before we sync it */
826 wmb();
827 msm_timer_sync_gpt_to_sclk(1);
828
829 if (clock == gpt_clk)
830 goto exit_idle_alarm;
831
832 enabled = __raw_readl(clock->regbase + TIMER_ENABLE) & TIMER_ENABLE_EN;
833 if (!enabled)
834 __raw_writel(TIMER_ENABLE_EN, clock->regbase + TIMER_ENABLE);
835
836#if defined(CONFIG_ARCH_MSM_SCORPION) || defined(CONFIG_ARCH_MSM_KRAIT)
837 clock_state->in_sync = 0;
838#else
839 clock_state->in_sync = clock_state->in_sync && enabled;
840#endif
841 /* Make sure timer is actually enabled before we sync it */
842 wmb();
843 msm_timer_sync_to_gpt(clock, 1);
844
845exit_idle_alarm:
846 msm_timer_reactivate_alarm(clock);
847
848exit_idle_exit:
849 clock_state->stopped--;
850}
851
852/*
853 * Callback function that initializes the timeout value.
854 */
855static void msm_timer_get_sclk_time_start(
856 struct msm_timer_sync_data_t *data)
857{
858 data->timeout = 200000;
859}
860
861/*
862 * Callback function that checks the timeout.
863 */
864static bool msm_timer_get_sclk_time_expired(
865 struct msm_timer_sync_data_t *data)
866{
867 udelay(10);
868 return --data->timeout <= 0;
869}
870
871/*
872 * Retrieve the cycle count from the sclk and convert it into
873 * nanoseconds.
874 *
875 * On exit, if period is not NULL, it contains the period of the
876 * sclk in nanoseconds, i.e. how long the cycle count wraps around.
877 *
878 * Return value:
879 * 0: the operation failed; period is not set either
880 * >0: time in nanoseconds
881 */
882int64_t msm_timer_get_sclk_time(int64_t *period)
883{
884 struct msm_timer_sync_data_t data;
885 uint32_t clock_value;
886 int64_t tmp;
887
888 memset(&data, 0, sizeof(data));
889 clock_value = msm_timer_do_sync_to_sclk(
890 msm_timer_get_sclk_time_start,
891 msm_timer_get_sclk_time_expired,
892 NULL,
893 &data);
894
895 if (!clock_value)
896 return 0;
897
898 if (period) {
899 tmp = 1LL << 32;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700900 tmp *= NSEC_PER_SEC;
901 do_div(tmp, sclk_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700902 *period = tmp;
903 }
904
905 tmp = (int64_t)clock_value;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700906 tmp *= NSEC_PER_SEC;
907 do_div(tmp, sclk_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700908 return tmp;
909}
910
911int __init msm_timer_init_time_sync(void (*timeout)(void))
912{
913#if defined(CONFIG_MSM_N_WAY_SMSM) && !defined(CONFIG_MSM_DIRECT_SCLK_ACCESS)
914 int ret = smsm_change_intr_mask(SMSM_TIME_MASTER_DEM, 0xFFFFFFFF, 0);
915
916 if (ret) {
917 printk(KERN_ERR "%s: failed to clear interrupt mask, %d\n",
918 __func__, ret);
919 return ret;
920 }
921
922 smsm_change_state(SMSM_APPS_DEM,
923 SLAVE_TIME_REQUEST | SLAVE_TIME_POLL, SLAVE_TIME_INIT);
924#endif
925
926 BUG_ON(timeout == NULL);
927 msm_timer_sync_timeout = timeout;
928
929 return 0;
930}
931
932#endif
933
Steve Mucklef132c6c2012-06-06 18:30:57 -0700934static u32 notrace msm_read_sched_clock(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700935{
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700936 struct msm_clock *clock = &msm_clocks[msm_global_timer];
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700937 struct clocksource *cs = &clock->clocksource;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700938 return cs->read(NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700939}
940
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700941int read_current_timer(unsigned long *timer_val)
942{
943 struct msm_clock *dgt = &msm_clocks[MSM_CLOCK_DGT];
944 *timer_val = msm_read_timer_count(dgt, GLOBAL_TIMER);
945 return 0;
946}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700947
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700948static void __init msm_sched_clock_init(void)
949{
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700950 struct msm_clock *clock = &msm_clocks[msm_global_timer];
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700951
Steve Mucklef132c6c2012-06-06 18:30:57 -0700952 setup_sched_clock(msm_read_sched_clock, 32 - clock->shift, clock->freq);
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700953}
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800954
Marc Zyngier5ca709c2012-01-10 19:44:19 +0000955#ifdef CONFIG_LOCAL_TIMERS
Steve Mucklef132c6c2012-06-06 18:30:57 -0700956int __cpuinit local_timer_setup(struct clock_event_device *evt)
Marc Zyngier5ca709c2012-01-10 19:44:19 +0000957{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700958 static DEFINE_PER_CPU(bool, first_boot) = true;
959 struct msm_clock *clock = &msm_clocks[msm_global_timer];
960
Marc Zyngier5ca709c2012-01-10 19:44:19 +0000961 /* Use existing clock_event for cpu 0 */
962 if (!smp_processor_id())
963 return 0;
964
Stepan Moskovchenkoc6a603a2012-09-21 20:32:17 -0700965 if (cpu_is_msm8x60() || soc_class_is_msm8960() ||
966 soc_class_is_apq8064() || soc_class_is_msm8930())
Steve Mucklef132c6c2012-06-06 18:30:57 -0700967 __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
968
969 if (__get_cpu_var(first_boot)) {
970 __raw_writel(0, clock->regbase + TIMER_ENABLE);
971 __raw_writel(0, clock->regbase + TIMER_CLEAR);
972 __raw_writel(~0, clock->regbase + TIMER_MATCH_VAL);
973 __get_cpu_var(first_boot) = false;
974 if (clock->status_mask)
975 while (__raw_readl(MSM_TMR_BASE + TIMER_STATUS) &
976 clock->status_mask)
977 ;
978 }
979 evt->irq = clock->irq;
Marc Zyngier5ca709c2012-01-10 19:44:19 +0000980 evt->name = "local_timer";
Steve Mucklef132c6c2012-06-06 18:30:57 -0700981 evt->features = CLOCK_EVT_FEAT_ONESHOT;
982 evt->rating = clock->clockevent.rating;
Marc Zyngier5ca709c2012-01-10 19:44:19 +0000983 evt->set_mode = msm_timer_set_mode;
984 evt->set_next_event = msm_timer_set_next_event;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700985 evt->shift = clock->clockevent.shift;
986 evt->mult = div_sc(clock->freq, NSEC_PER_SEC, evt->shift);
987 evt->max_delta_ns =
988 clockevent_delta2ns(0xf0000000 >> clock->shift, evt);
Marc Zyngier5ca709c2012-01-10 19:44:19 +0000989 evt->min_delta_ns = clockevent_delta2ns(4, evt);
990
Steve Mucklef132c6c2012-06-06 18:30:57 -0700991 *__this_cpu_ptr(clock->percpu_evt) = evt;
992
Marc Zyngier5ca709c2012-01-10 19:44:19 +0000993 clockevents_register_device(evt);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700994 enable_percpu_irq(evt->irq, IRQ_TYPE_EDGE_RISING);
995
Marc Zyngier5ca709c2012-01-10 19:44:19 +0000996 return 0;
997}
998
Steve Mucklef132c6c2012-06-06 18:30:57 -0700999void local_timer_stop(struct clock_event_device *evt)
Marc Zyngier5ca709c2012-01-10 19:44:19 +00001000{
1001 evt->set_mode(CLOCK_EVT_MODE_UNUSED, evt);
1002 disable_percpu_irq(evt->irq);
1003}
1004
Steve Mucklef132c6c2012-06-06 18:30:57 -07001005static struct local_timer_ops msm_lt_ops = {
1006 local_timer_setup,
1007 local_timer_stop,
Marc Zyngier5ca709c2012-01-10 19:44:19 +00001008};
1009#endif /* CONFIG_LOCAL_TIMERS */
1010
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001011static void __init msm_timer_init(void)
1012{
1013 int i;
1014 int res;
Jin Hongeecb1e02011-10-21 14:36:32 -07001015 struct irq_chip *chip;
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001016 struct msm_clock *dgt = &msm_clocks[MSM_CLOCK_DGT];
1017 struct msm_clock *gpt = &msm_clocks[MSM_CLOCK_GPT];
Stephen Boyddd15ab82011-11-08 10:34:05 -08001018
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001019 if (cpu_is_msm7x01() || cpu_is_msm7x25() || cpu_is_msm7x27() ||
1020 cpu_is_msm7x25a() || cpu_is_msm7x27a() || cpu_is_msm7x25aa() ||
Pankaj Kumarfee56a82012-04-17 14:26:49 +05301021 cpu_is_msm7x27aa() || cpu_is_msm8625() || cpu_is_msm7x25ab()) {
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001022 dgt->shift = MSM_DGT_SHIFT;
1023 dgt->freq = 19200000 >> MSM_DGT_SHIFT;
1024 dgt->clockevent.shift = 32 + MSM_DGT_SHIFT;
1025 dgt->clocksource.mask = CLOCKSOURCE_MASK(32 - MSM_DGT_SHIFT);
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001026 gpt->regbase = MSM_TMR_BASE;
1027 dgt->regbase = MSM_TMR_BASE + 0x10;
Jeff Ohlstein5d90e252011-11-04 19:00:50 -07001028 gpt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT
1029 | MSM_CLOCK_FLAGS_ODD_MATCH_WRITE
1030 | MSM_CLOCK_FLAGS_DELAYED_WRITE_POST;
Taniya Das5eb25142011-11-17 21:53:34 +05301031 if (cpu_is_msm8625()) {
1032 dgt->irq = MSM8625_INT_DEBUG_TIMER_EXP;
1033 gpt->irq = MSM8625_INT_GP_TIMER_EXP;
1034 global_timer_offset = MSM_TMR0_BASE - MSM_TMR_BASE;
Marc Zyngier28af6902011-07-22 12:52:37 +01001035 }
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001036 } else if (cpu_is_qsd8x50()) {
1037 dgt->freq = 4800000;
1038 gpt->regbase = MSM_TMR_BASE;
1039 dgt->regbase = MSM_TMR_BASE + 0x10;
1040 } else if (cpu_is_fsm9xxx())
1041 dgt->freq = 4800000;
Jeff Ohlstein6c47a272012-02-24 14:48:55 -08001042 else if (cpu_is_msm7x30() || cpu_is_msm8x55()) {
1043 gpt->status_mask = BIT(10);
1044 dgt->status_mask = BIT(2);
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001045 dgt->freq = 6144000;
Jeff Ohlstein6c47a272012-02-24 14:48:55 -08001046 } else if (cpu_is_msm8x60()) {
Jeff Ohlstein7e538f02011-11-01 17:36:22 -07001047 global_timer_offset = MSM_TMR0_BASE - MSM_TMR_BASE;
Jeff Ohlstein6c47a272012-02-24 14:48:55 -08001048 gpt->status_mask = BIT(10);
1049 dgt->status_mask = BIT(2);
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001050 dgt->freq = 6750000;
1051 __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
Jeff Ohlstein7e538f02011-11-01 17:36:22 -07001052 } else if (cpu_is_msm9615()) {
1053 dgt->freq = 6750000;
1054 __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
Jeff Ohlstein6c47a272012-02-24 14:48:55 -08001055 gpt->status_mask = BIT(10);
1056 dgt->status_mask = BIT(2);
Jeff Ohlstein7e538f02011-11-01 17:36:22 -07001057 gpt->freq = 32765;
1058 gpt_hz = 32765;
1059 sclk_hz = 32765;
Jeff Ohlsteind47f96a2011-11-04 19:00:50 -07001060 gpt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT;
1061 dgt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT;
Stepan Moskovchenkoc6a603a2012-09-21 20:32:17 -07001062 } else if (soc_class_is_msm8960() || soc_class_is_apq8064() ||
1063 soc_class_is_msm8930()) {
Jeff Ohlstein7e538f02011-11-01 17:36:22 -07001064 global_timer_offset = MSM_TMR0_BASE - MSM_TMR_BASE;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -07001065 dgt->freq = 6750000;
1066 __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
Jeff Ohlstein6c47a272012-02-24 14:48:55 -08001067 gpt->status_mask = BIT(10);
1068 dgt->status_mask = BIT(2);
Jay Chokshi7935fae2013-03-12 11:37:18 -07001069 if (!soc_class_is_apq8064()) {
1070 gpt->freq = 32765;
1071 gpt_hz = 32765;
1072 sclk_hz = 32765;
1073 }
Stepan Moskovchenkoc6a603a2012-09-21 20:32:17 -07001074 if (!soc_class_is_msm8930() && !cpu_is_msm8960ab()) {
Jeff Ohlstein391a3ee2011-12-01 16:44:45 -08001075 gpt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT;
1076 dgt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT;
Marc Zyngier5ca709c2012-01-10 19:44:19 +00001077 }
Stephen Boyddd15ab82011-11-08 10:34:05 -08001078 } else {
Jeff Ohlsteinf0a31e42012-01-06 19:03:05 -08001079 WARN(1, "Timer running on unknown hardware. Configure this! "
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001080 "Assuming default configuration.\n");
1081 dgt->freq = 6750000;
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001082 }
Stephen Boyddd15ab82011-11-08 10:34:05 -08001083
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001084 if (msm_clocks[MSM_CLOCK_GPT].clocksource.rating > DG_TIMER_RATING)
1085 msm_global_timer = MSM_CLOCK_GPT;
1086 else
1087 msm_global_timer = MSM_CLOCK_DGT;
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001088
1089 for (i = 0; i < ARRAY_SIZE(msm_clocks); i++) {
1090 struct msm_clock *clock = &msm_clocks[i];
1091 struct clock_event_device *ce = &clock->clockevent;
1092 struct clocksource *cs = &clock->clocksource;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001093 __raw_writel(0, clock->regbase + TIMER_ENABLE);
Jeff Ohlstein6c47a272012-02-24 14:48:55 -08001094 __raw_writel(0, clock->regbase + TIMER_CLEAR);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001095 __raw_writel(~0, clock->regbase + TIMER_MATCH_VAL);
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001096
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -07001097 if ((clock->freq << clock->shift) == gpt_hz) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001098 clock->rollover_offset = 0;
1099 } else {
1100 uint64_t temp;
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001101
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001102 temp = clock->freq << clock->shift;
1103 temp <<= 32;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -07001104 do_div(temp, gpt_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001105
1106 clock->rollover_offset = (uint32_t) temp;
1107 }
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001108
1109 ce->mult = div_sc(clock->freq, NSEC_PER_SEC, ce->shift);
1110 /* allow at least 10 seconds to notice that the timer wrapped */
1111 ce->max_delta_ns =
1112 clockevent_delta2ns(0xf0000000 >> clock->shift, ce);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001113 /* ticks gets rounded down by one */
1114 ce->min_delta_ns =
1115 clockevent_delta2ns(clock->write_delay + 4, ce);
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001116 ce->cpumask = cpumask_of(0);
1117
Jeff Ohlstein711a7142012-05-23 11:57:33 -07001118 res = clocksource_register_hz(cs, clock->freq);
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;
Stepan Moskovchenkoc6a603a2012-09-21 20:32:17 -07001124 if (cpu_is_msm8x60() || cpu_is_msm9615() || cpu_is_msm8625() ||
1125 soc_class_is_msm8960() || soc_class_is_apq8064() ||
1126 soc_class_is_msm8930()) {
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};