blob: 212ad77a1bb0b8a94de0ed512edce3d46f808f76 [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>
Greg Reidb1d240a2012-10-12 12:20:31 -040026#include <linux/mm.h>
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -080027
Steve Mucklef132c6c2012-06-06 18:30:57 -070028#include <asm/localtimer.h>
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -080029#include <asm/mach/time.h>
Stephen Boydebf30dc2011-05-31 16:10:00 -070030#include <asm/hardware/gic.h>
Stephen Boydf8e56c42012-02-22 01:39:37 +000031#include <asm/sched_clock.h>
Taniya Das36057be2011-10-28 13:02:17 +053032#include <asm/smp_plat.h>
Greg Reidb1d240a2012-10-12 12:20:31 -040033#include <asm/user_accessible_timer.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010034#include <mach/msm_iomap.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035#include <mach/irqs.h>
36#include <mach/socinfo.h>
37
38#if defined(CONFIG_MSM_SMD)
39#include "smd_private.h"
40#endif
41#include "timer.h"
42
43enum {
44 MSM_TIMER_DEBUG_SYNC = 1U << 0,
45};
46static int msm_timer_debug_mask;
47module_param_named(debug_mask, msm_timer_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP);
48
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049#ifdef CONFIG_MSM7X00A_USE_GP_TIMER
50 #define DG_TIMER_RATING 100
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070051#else
52 #define DG_TIMER_RATING 300
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070053#endif
54
Jeff Ohlstein7e538f02011-11-01 17:36:22 -070055#ifndef MSM_TMR0_BASE
56#define MSM_TMR0_BASE MSM_TMR_BASE
57#endif
58
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070059#define MSM_DGT_SHIFT (5)
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -080060
61#define TIMER_MATCH_VAL 0x0000
62#define TIMER_COUNT_VAL 0x0004
63#define TIMER_ENABLE 0x0008
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -080064#define TIMER_CLEAR 0x000C
Jeff Ohlstein672039f2010-10-05 15:23:57 -070065#define DGT_CLK_CTL 0x0034
66enum {
67 DGT_CLK_CTL_DIV_1 = 0,
68 DGT_CLK_CTL_DIV_2 = 1,
69 DGT_CLK_CTL_DIV_3 = 2,
70 DGT_CLK_CTL_DIV_4 = 3,
71};
Jeff Ohlstein6c47a272012-02-24 14:48:55 -080072#define TIMER_STATUS 0x0088
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070073#define TIMER_ENABLE_EN 1
74#define TIMER_ENABLE_CLR_ON_MATCH_EN 2
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -080075
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070076#define LOCAL_TIMER 0
77#define GLOBAL_TIMER 1
Jeff Ohlstein672039f2010-10-05 15:23:57 -070078
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070079/*
Jeff Ohlsteine1a7e402011-09-07 12:52:36 -070080 * global_timer_offset is added to the regbase of a timer to force the memory
81 * access to come from the CPU0 region.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070082 */
Jeff Ohlsteine1a7e402011-09-07 12:52:36 -070083static int global_timer_offset;
Jeff Ohlstein7a018322011-09-28 12:44:06 -070084static int msm_global_timer;
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -080085
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070086#define NR_TIMERS ARRAY_SIZE(msm_clocks)
87
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -070088unsigned int gpt_hz = 32768;
89unsigned int sclk_hz = 32768;
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -080090
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070091static struct msm_clock *clockevent_to_clock(struct clock_event_device *evt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070092static irqreturn_t msm_timer_interrupt(int irq, void *dev_id);
93static cycle_t msm_gpt_read(struct clocksource *cs);
94static cycle_t msm_dgt_read(struct clocksource *cs);
95static void msm_timer_set_mode(enum clock_event_mode mode,
96 struct clock_event_device *evt);
97static int msm_timer_set_next_event(unsigned long cycles,
98 struct clock_event_device *evt);
99
100enum {
101 MSM_CLOCK_FLAGS_UNSTABLE_COUNT = 1U << 0,
102 MSM_CLOCK_FLAGS_ODD_MATCH_WRITE = 1U << 1,
103 MSM_CLOCK_FLAGS_DELAYED_WRITE_POST = 1U << 2,
104};
105
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800106struct msm_clock {
107 struct clock_event_device clockevent;
108 struct clocksource clocksource;
Trilok Sonieecb28c2011-07-20 16:24:14 +0100109 unsigned int irq;
Brian Swetlandbcc0f6a2008-09-10 14:00:53 -0700110 void __iomem *regbase;
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800111 uint32_t freq;
112 uint32_t shift;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700113 uint32_t flags;
114 uint32_t write_delay;
115 uint32_t rollover_offset;
116 uint32_t index;
Trilok Sonieecb28c2011-07-20 16:24:14 +0100117 void __iomem *global_counter;
118 void __iomem *local_counter;
Jeff Ohlstein6c47a272012-02-24 14:48:55 -0800119 uint32_t status_mask;
Trilok Sonieecb28c2011-07-20 16:24:14 +0100120 union {
121 struct clock_event_device *evt;
122 struct clock_event_device __percpu **percpu_evt;
123 };
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800124};
125
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800126enum {
127 MSM_CLOCK_GPT,
128 MSM_CLOCK_DGT,
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800129};
130
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700131struct msm_clock_percpu_data {
132 uint32_t last_set;
133 uint32_t sleep_offset;
134 uint32_t alarm_vtime;
135 uint32_t alarm;
136 uint32_t non_sleep_offset;
137 uint32_t in_sync;
138 cycle_t stopped_tick;
139 int stopped;
140 uint32_t last_sync_gpt;
141 u64 last_sync_jiffies;
142};
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800143
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700144struct msm_timer_sync_data_t {
145 struct msm_clock *clock;
146 uint32_t timeout;
147 int exit_sleep;
148};
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800149
150static struct msm_clock msm_clocks[] = {
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800151 [MSM_CLOCK_GPT] = {
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800152 .clockevent = {
153 .name = "gp_timer",
154 .features = CLOCK_EVT_FEAT_ONESHOT,
155 .shift = 32,
156 .rating = 200,
157 .set_next_event = msm_timer_set_next_event,
158 .set_mode = msm_timer_set_mode,
159 },
160 .clocksource = {
161 .name = "gp_timer",
162 .rating = 200,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700163 .read = msm_gpt_read,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800164 .mask = CLOCKSOURCE_MASK(32),
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800165 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
166 },
Trilok Sonieecb28c2011-07-20 16:24:14 +0100167 .irq = INT_GP_TIMER_EXP,
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700168 .regbase = MSM_TMR_BASE + 0x4,
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700169 .freq = 32768,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700170 .index = MSM_CLOCK_GPT,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700171 .write_delay = 9,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800172 },
Jeff Ohlstein94790ec2010-12-02 12:05:12 -0800173 [MSM_CLOCK_DGT] = {
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800174 .clockevent = {
175 .name = "dg_timer",
176 .features = CLOCK_EVT_FEAT_ONESHOT,
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700177 .shift = 32,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700178 .rating = DG_TIMER_RATING,
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800179 .set_next_event = msm_timer_set_next_event,
180 .set_mode = msm_timer_set_mode,
181 },
182 .clocksource = {
183 .name = "dg_timer",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700184 .rating = DG_TIMER_RATING,
185 .read = msm_dgt_read,
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700186 .mask = CLOCKSOURCE_MASK(32),
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;
Steve Muckled599fda2012-05-20 21:38:02 -0700327 struct msm_clock **cur_clock;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700328 struct msm_clock_percpu_data *clock_state, *gpt_state;
329 unsigned long irq_flags;
Jin Hongeecb1e02011-10-21 14:36:32 -0700330 struct irq_chip *chip;
Stephen Boyda850c3f2011-11-08 10:34:06 -0800331
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700332 clock = clockevent_to_clock(evt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700333 clock_state = &__get_cpu_var(msm_clocks_percpu)[clock->index];
334 gpt_state = &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT];
335
336 local_irq_save(irq_flags);
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800337
338 switch (mode) {
339 case CLOCK_EVT_MODE_RESUME:
340 case CLOCK_EVT_MODE_PERIODIC:
341 break;
342 case CLOCK_EVT_MODE_ONESHOT:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700343 clock_state->stopped = 0;
344 clock_state->sleep_offset =
345 -msm_read_timer_count(clock, LOCAL_TIMER) +
346 clock_state->stopped_tick;
347 get_cpu_var(msm_active_clock) = clock;
348 put_cpu_var(msm_active_clock);
349 __raw_writel(TIMER_ENABLE_EN, clock->regbase + TIMER_ENABLE);
Trilok Sonieecb28c2011-07-20 16:24:14 +0100350 chip = irq_get_chip(clock->irq);
Jin Hongeecb1e02011-10-21 14:36:32 -0700351 if (chip && chip->irq_unmask)
Trilok Sonieecb28c2011-07-20 16:24:14 +0100352 chip->irq_unmask(irq_get_irq_data(clock->irq));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700353 if (clock != &msm_clocks[MSM_CLOCK_GPT])
354 __raw_writel(TIMER_ENABLE_EN,
355 msm_clocks[MSM_CLOCK_GPT].regbase +
356 TIMER_ENABLE);
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800357 break;
358 case CLOCK_EVT_MODE_UNUSED:
359 case CLOCK_EVT_MODE_SHUTDOWN:
Steve Muckled599fda2012-05-20 21:38:02 -0700360 cur_clock = &get_cpu_var(msm_active_clock);
361 if (*cur_clock == clock)
362 *cur_clock = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700363 put_cpu_var(msm_active_clock);
364 clock_state->in_sync = 0;
365 clock_state->stopped = 1;
366 clock_state->stopped_tick =
367 msm_read_timer_count(clock, LOCAL_TIMER) +
368 clock_state->sleep_offset;
369 __raw_writel(0, clock->regbase + TIMER_MATCH_VAL);
Trilok Sonieecb28c2011-07-20 16:24:14 +0100370 chip = irq_get_chip(clock->irq);
Jin Hongeecb1e02011-10-21 14:36:32 -0700371 if (chip && chip->irq_mask)
Trilok Sonieecb28c2011-07-20 16:24:14 +0100372 chip->irq_mask(irq_get_irq_data(clock->irq));
Taniya Das36057be2011-10-28 13:02:17 +0530373
374 if (!is_smp() || clock != &msm_clocks[MSM_CLOCK_DGT]
375 || smp_processor_id())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700376 __raw_writel(0, clock->regbase + TIMER_ENABLE);
Taniya Das36057be2011-10-28 13:02:17 +0530377
Steve Mucklef132c6c2012-06-06 18:30:57 -0700378 if (msm_global_timer == MSM_CLOCK_DGT &&
379 clock != &msm_clocks[MSM_CLOCK_GPT]) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700380 gpt_state->in_sync = 0;
381 __raw_writel(0, msm_clocks[MSM_CLOCK_GPT].regbase +
382 TIMER_ENABLE);
383 }
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800384 break;
385 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700386 wmb();
387 local_irq_restore(irq_flags);
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800388}
389
Jeff Ohlstein973871d2011-09-28 11:46:26 -0700390void __iomem *msm_timer_get_timer0_base(void)
Stephen Boyd2a00c102011-11-08 10:34:07 -0800391{
Jeff Ohlstein973871d2011-09-28 11:46:26 -0700392 return MSM_TMR_BASE + global_timer_offset;
Stephen Boyd2081a6b2011-11-08 10:34:08 -0800393}
394
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700395#define MPM_SCLK_COUNT_VAL 0x0024
396
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700397#ifdef CONFIG_PM
398/*
399 * Retrieve the cycle count from sclk and optionally synchronize local clock
400 * with the sclk value.
401 *
402 * time_start and time_expired are callbacks that must be specified. The
403 * protocol uses them to detect timeout. The update callback is optional.
404 * If not NULL, update will be called so that it can update local clock.
405 *
406 * The function does not use the argument data directly; it passes data to
407 * the callbacks.
408 *
409 * Return value:
410 * 0: the operation failed
411 * >0: the slow clock value after time-sync
412 */
413static void (*msm_timer_sync_timeout)(void);
414#if defined(CONFIG_MSM_DIRECT_SCLK_ACCESS)
Jeff Ohlsteinecefdc02012-01-13 12:37:44 -0800415uint32_t msm_timer_get_sclk_ticks(void)
Stephen Boyd2081a6b2011-11-08 10:34:08 -0800416{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700417 uint32_t t1, t2;
418 int loop_count = 10;
419 int loop_zero_count = 3;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700420 int tmp = USEC_PER_SEC;
421 do_div(tmp, sclk_hz);
422 tmp /= (loop_zero_count-1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700423
424 while (loop_zero_count--) {
Laura Abbott1d506042012-01-23 13:21:34 -0800425 t1 = __raw_readl_no_log(MSM_RPM_MPM_BASE + MPM_SCLK_COUNT_VAL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700426 do {
427 udelay(1);
428 t2 = t1;
Laura Abbott1d506042012-01-23 13:21:34 -0800429 t1 = __raw_readl_no_log(
430 MSM_RPM_MPM_BASE + MPM_SCLK_COUNT_VAL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700431 } while ((t2 != t1) && --loop_count);
432
433 if (!loop_count) {
434 printk(KERN_EMERG "SCLK did not stabilize\n");
435 return 0;
436 }
437
438 if (t1)
439 break;
440
441 udelay(tmp);
442 }
443
444 if (!loop_zero_count) {
445 printk(KERN_EMERG "SCLK reads zero\n");
446 return 0;
447 }
448
Jeff Ohlsteinecefdc02012-01-13 12:37:44 -0800449 return t1;
Stephen Boyd2a00c102011-11-08 10:34:07 -0800450}
451
Jeff Ohlsteinecefdc02012-01-13 12:37:44 -0800452static uint32_t msm_timer_do_sync_to_sclk(
453 void (*time_start)(struct msm_timer_sync_data_t *data),
454 bool (*time_expired)(struct msm_timer_sync_data_t *data),
455 void (*update)(struct msm_timer_sync_data_t *, uint32_t, uint32_t),
456 struct msm_timer_sync_data_t *data)
457{
458 unsigned t1 = msm_timer_get_sclk_ticks();
459
460 if (t1 && update != NULL)
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700461 update(data, t1, sclk_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700462 return t1;
463}
464#elif defined(CONFIG_MSM_N_WAY_SMSM)
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700465
466/* Time Master State Bits */
467#define MASTER_BITS_PER_CPU 1
468#define MASTER_TIME_PENDING \
469 (0x01UL << (MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
470
471/* Time Slave State Bits */
472#define SLAVE_TIME_REQUEST 0x0400
473#define SLAVE_TIME_POLL 0x0800
474#define SLAVE_TIME_INIT 0x1000
475
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700476static uint32_t msm_timer_do_sync_to_sclk(
477 void (*time_start)(struct msm_timer_sync_data_t *data),
478 bool (*time_expired)(struct msm_timer_sync_data_t *data),
479 void (*update)(struct msm_timer_sync_data_t *, uint32_t, uint32_t),
480 struct msm_timer_sync_data_t *data)
481{
482 uint32_t *smem_clock;
483 uint32_t smem_clock_val;
484 uint32_t state;
485
486 smem_clock = smem_alloc(SMEM_SMEM_SLOW_CLOCK_VALUE, sizeof(uint32_t));
487 if (smem_clock == NULL) {
488 printk(KERN_ERR "no smem clock\n");
489 return 0;
490 }
491
492 state = smsm_get_state(SMSM_MODEM_STATE);
493 if ((state & SMSM_INIT) == 0) {
494 printk(KERN_ERR "smsm not initialized\n");
495 return 0;
496 }
497
498 time_start(data);
499 while ((state = smsm_get_state(SMSM_TIME_MASTER_DEM)) &
500 MASTER_TIME_PENDING) {
501 if (time_expired(data)) {
502 printk(KERN_EMERG "get_smem_clock: timeout 1 still "
503 "invalid state %x\n", state);
504 msm_timer_sync_timeout();
505 }
506 }
507
508 smsm_change_state(SMSM_APPS_DEM, SLAVE_TIME_POLL | SLAVE_TIME_INIT,
509 SLAVE_TIME_REQUEST);
510
511 time_start(data);
512 while (!((state = smsm_get_state(SMSM_TIME_MASTER_DEM)) &
513 MASTER_TIME_PENDING)) {
514 if (time_expired(data)) {
515 printk(KERN_EMERG "get_smem_clock: timeout 2 still "
516 "invalid state %x\n", state);
517 msm_timer_sync_timeout();
518 }
519 }
520
521 smsm_change_state(SMSM_APPS_DEM, SLAVE_TIME_REQUEST, SLAVE_TIME_POLL);
522
523 time_start(data);
524 do {
525 smem_clock_val = *smem_clock;
526 } while (smem_clock_val == 0 && !time_expired(data));
527
528 state = smsm_get_state(SMSM_TIME_MASTER_DEM);
529
530 if (smem_clock_val) {
531 if (update != NULL)
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700532 update(data, smem_clock_val, sclk_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700533
534 if (msm_timer_debug_mask & MSM_TIMER_DEBUG_SYNC)
535 printk(KERN_INFO
536 "get_smem_clock: state %x clock %u\n",
537 state, smem_clock_val);
538 } else {
539 printk(KERN_EMERG
540 "get_smem_clock: timeout state %x clock %u\n",
541 state, smem_clock_val);
542 msm_timer_sync_timeout();
543 }
544
545 smsm_change_state(SMSM_APPS_DEM, SLAVE_TIME_REQUEST | SLAVE_TIME_POLL,
546 SLAVE_TIME_INIT);
547 return smem_clock_val;
548}
549#else /* CONFIG_MSM_N_WAY_SMSM */
550static uint32_t msm_timer_do_sync_to_sclk(
551 void (*time_start)(struct msm_timer_sync_data_t *data),
552 bool (*time_expired)(struct msm_timer_sync_data_t *data),
553 void (*update)(struct msm_timer_sync_data_t *, uint32_t, uint32_t),
554 struct msm_timer_sync_data_t *data)
555{
556 uint32_t *smem_clock;
557 uint32_t smem_clock_val;
558 uint32_t last_state;
559 uint32_t state;
560
561 smem_clock = smem_alloc(SMEM_SMEM_SLOW_CLOCK_VALUE,
562 sizeof(uint32_t));
563
564 if (smem_clock == NULL) {
565 printk(KERN_ERR "no smem clock\n");
566 return 0;
567 }
568
569 last_state = state = smsm_get_state(SMSM_MODEM_STATE);
570 smem_clock_val = *smem_clock;
571 if (smem_clock_val) {
572 printk(KERN_INFO "get_smem_clock: invalid start state %x "
573 "clock %u\n", state, smem_clock_val);
574 smsm_change_state(SMSM_APPS_STATE,
575 SMSM_TIMEWAIT, SMSM_TIMEINIT);
576
577 time_start(data);
578 while (*smem_clock != 0 && !time_expired(data))
579 ;
580
581 smem_clock_val = *smem_clock;
582 if (smem_clock_val) {
583 printk(KERN_EMERG "get_smem_clock: timeout still "
584 "invalid state %x clock %u\n",
585 state, smem_clock_val);
586 msm_timer_sync_timeout();
587 }
588 }
589
590 time_start(data);
591 smsm_change_state(SMSM_APPS_STATE, SMSM_TIMEINIT, SMSM_TIMEWAIT);
592 do {
593 smem_clock_val = *smem_clock;
594 state = smsm_get_state(SMSM_MODEM_STATE);
595 if (state != last_state) {
596 last_state = state;
597 if (msm_timer_debug_mask & MSM_TIMER_DEBUG_SYNC)
598 printk(KERN_INFO
599 "get_smem_clock: state %x clock %u\n",
600 state, smem_clock_val);
601 }
602 } while (smem_clock_val == 0 && !time_expired(data));
603
604 if (smem_clock_val) {
605 if (update != NULL)
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700606 update(data, smem_clock_val, sclk_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700607 } else {
608 printk(KERN_EMERG
609 "get_smem_clock: timeout state %x clock %u\n",
610 state, smem_clock_val);
611 msm_timer_sync_timeout();
612 }
613
614 smsm_change_state(SMSM_APPS_STATE, SMSM_TIMEWAIT, SMSM_TIMEINIT);
615 return smem_clock_val;
616}
617#endif /* CONFIG_MSM_N_WAY_SMSM */
618
619/*
620 * Callback function that initializes the timeout value.
621 */
622static void msm_timer_sync_to_sclk_time_start(
623 struct msm_timer_sync_data_t *data)
624{
625 /* approx 2 seconds */
626 uint32_t delta = data->clock->freq << data->clock->shift << 1;
627 data->timeout = msm_read_timer_count(data->clock, LOCAL_TIMER) + delta;
628}
629
630/*
631 * Callback function that checks the timeout.
632 */
633static bool msm_timer_sync_to_sclk_time_expired(
634 struct msm_timer_sync_data_t *data)
635{
636 uint32_t delta = msm_read_timer_count(data->clock, LOCAL_TIMER) -
637 data->timeout;
638 return ((int32_t) delta) > 0;
639}
640
641/*
642 * Callback function that updates local clock from the specified source clock
643 * value and frequency.
644 */
645static void msm_timer_sync_update(struct msm_timer_sync_data_t *data,
646 uint32_t src_clk_val, uint32_t src_clk_freq)
647{
648 struct msm_clock *dst_clk = data->clock;
649 struct msm_clock_percpu_data *dst_clk_state =
650 &__get_cpu_var(msm_clocks_percpu)[dst_clk->index];
651 uint32_t dst_clk_val = msm_read_timer_count(dst_clk, LOCAL_TIMER);
652 uint32_t new_offset;
653
654 if ((dst_clk->freq << dst_clk->shift) == src_clk_freq) {
655 new_offset = src_clk_val - dst_clk_val;
656 } else {
657 uint64_t temp;
658
659 /* separate multiplication and division steps to reduce
660 rounding error */
661 temp = src_clk_val;
662 temp *= dst_clk->freq << dst_clk->shift;
663 do_div(temp, src_clk_freq);
664
665 new_offset = (uint32_t)(temp) - dst_clk_val;
666 }
667
668 if (dst_clk_state->sleep_offset + dst_clk_state->non_sleep_offset !=
669 new_offset) {
670 if (data->exit_sleep)
671 dst_clk_state->sleep_offset =
672 new_offset - dst_clk_state->non_sleep_offset;
673 else
674 dst_clk_state->non_sleep_offset =
675 new_offset - dst_clk_state->sleep_offset;
676
677 if (msm_timer_debug_mask & MSM_TIMER_DEBUG_SYNC)
678 printk(KERN_INFO "sync clock %s: "
679 "src %u, new offset %u + %u\n",
680 dst_clk->clocksource.name, src_clk_val,
681 dst_clk_state->sleep_offset,
682 dst_clk_state->non_sleep_offset);
683 }
684}
685
686/*
687 * Synchronize GPT clock with sclk.
688 */
689static void msm_timer_sync_gpt_to_sclk(int exit_sleep)
690{
691 struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT];
692 struct msm_clock_percpu_data *gpt_clk_state =
693 &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT];
694 struct msm_timer_sync_data_t data;
695 uint32_t ret;
696
697 if (gpt_clk_state->in_sync)
698 return;
699
700 data.clock = gpt_clk;
701 data.timeout = 0;
702 data.exit_sleep = exit_sleep;
703
704 ret = msm_timer_do_sync_to_sclk(
705 msm_timer_sync_to_sclk_time_start,
706 msm_timer_sync_to_sclk_time_expired,
707 msm_timer_sync_update,
708 &data);
709
710 if (ret)
711 gpt_clk_state->in_sync = 1;
712}
713
714/*
715 * Synchronize clock with GPT clock.
716 */
717static void msm_timer_sync_to_gpt(struct msm_clock *clock, int exit_sleep)
718{
719 struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT];
720 struct msm_clock_percpu_data *gpt_clk_state =
721 &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT];
722 struct msm_clock_percpu_data *clock_state =
723 &__get_cpu_var(msm_clocks_percpu)[clock->index];
724 struct msm_timer_sync_data_t data;
725 uint32_t gpt_clk_val;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700726 u64 gpt_period = (1ULL << 32) * HZ;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700727 u64 now = get_jiffies_64();
728
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700729 do_div(gpt_period, gpt_hz);
730
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700731 BUG_ON(clock == gpt_clk);
732
733 if (clock_state->in_sync &&
734 (now - clock_state->last_sync_jiffies < (gpt_period >> 1)))
735 return;
736
737 gpt_clk_val = msm_read_timer_count(gpt_clk, LOCAL_TIMER)
738 + gpt_clk_state->sleep_offset + gpt_clk_state->non_sleep_offset;
739
740 if (exit_sleep && gpt_clk_val < clock_state->last_sync_gpt)
741 clock_state->non_sleep_offset -= clock->rollover_offset;
742
743 data.clock = clock;
744 data.timeout = 0;
745 data.exit_sleep = exit_sleep;
746
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700747 msm_timer_sync_update(&data, gpt_clk_val, gpt_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700748
749 clock_state->in_sync = 1;
750 clock_state->last_sync_gpt = gpt_clk_val;
751 clock_state->last_sync_jiffies = now;
752}
753
754static void msm_timer_reactivate_alarm(struct msm_clock *clock)
755{
756 struct msm_clock_percpu_data *clock_state =
757 &__get_cpu_var(msm_clocks_percpu)[clock->index];
758 long alarm_delta = clock_state->alarm_vtime -
759 clock_state->sleep_offset -
760 msm_read_timer_count(clock, LOCAL_TIMER);
761 alarm_delta >>= clock->shift;
762 if (alarm_delta < (long)clock->write_delay + 4)
763 alarm_delta = clock->write_delay + 4;
764 while (msm_timer_set_next_event(alarm_delta, &clock->clockevent))
765 ;
766}
767
768int64_t msm_timer_enter_idle(void)
769{
770 struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT];
771 struct msm_clock *clock = __get_cpu_var(msm_active_clock);
772 struct msm_clock_percpu_data *clock_state =
773 &__get_cpu_var(msm_clocks_percpu)[clock->index];
774 uint32_t alarm;
775 uint32_t count;
776 int32_t delta;
777
778 BUG_ON(clock != &msm_clocks[MSM_CLOCK_GPT] &&
779 clock != &msm_clocks[MSM_CLOCK_DGT]);
780
781 msm_timer_sync_gpt_to_sclk(0);
782 if (clock != gpt_clk)
783 msm_timer_sync_to_gpt(clock, 0);
784
785 count = msm_read_timer_count(clock, LOCAL_TIMER);
786 if (clock_state->stopped++ == 0)
787 clock_state->stopped_tick = count + clock_state->sleep_offset;
788 alarm = clock_state->alarm;
789 delta = alarm - count;
790 if (delta <= -(int32_t)((clock->freq << clock->shift) >> 10)) {
791 /* timer should have triggered 1ms ago */
792 printk(KERN_ERR "msm_timer_enter_idle: timer late %d, "
793 "reprogram it\n", delta);
794 msm_timer_reactivate_alarm(clock);
795 }
796 if (delta <= 0)
797 return 0;
798 return clocksource_cyc2ns((alarm - count) >> clock->shift,
799 clock->clocksource.mult,
800 clock->clocksource.shift);
801}
802
803void msm_timer_exit_idle(int low_power)
804{
805 struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT];
806 struct msm_clock *clock = __get_cpu_var(msm_active_clock);
807 struct msm_clock_percpu_data *gpt_clk_state =
808 &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT];
809 struct msm_clock_percpu_data *clock_state =
810 &__get_cpu_var(msm_clocks_percpu)[clock->index];
811 uint32_t enabled;
812
813 BUG_ON(clock != &msm_clocks[MSM_CLOCK_GPT] &&
814 clock != &msm_clocks[MSM_CLOCK_DGT]);
815
816 if (!low_power)
817 goto exit_idle_exit;
818
819 enabled = __raw_readl(gpt_clk->regbase + TIMER_ENABLE) &
820 TIMER_ENABLE_EN;
821 if (!enabled)
822 __raw_writel(TIMER_ENABLE_EN, gpt_clk->regbase + TIMER_ENABLE);
823
824#if defined(CONFIG_ARCH_MSM_SCORPION) || defined(CONFIG_ARCH_MSM_KRAIT)
825 gpt_clk_state->in_sync = 0;
826#else
827 gpt_clk_state->in_sync = gpt_clk_state->in_sync && enabled;
828#endif
829 /* Make sure timer is actually enabled before we sync it */
830 wmb();
831 msm_timer_sync_gpt_to_sclk(1);
832
833 if (clock == gpt_clk)
834 goto exit_idle_alarm;
835
836 enabled = __raw_readl(clock->regbase + TIMER_ENABLE) & TIMER_ENABLE_EN;
837 if (!enabled)
838 __raw_writel(TIMER_ENABLE_EN, clock->regbase + TIMER_ENABLE);
839
840#if defined(CONFIG_ARCH_MSM_SCORPION) || defined(CONFIG_ARCH_MSM_KRAIT)
841 clock_state->in_sync = 0;
842#else
843 clock_state->in_sync = clock_state->in_sync && enabled;
844#endif
845 /* Make sure timer is actually enabled before we sync it */
846 wmb();
847 msm_timer_sync_to_gpt(clock, 1);
848
849exit_idle_alarm:
850 msm_timer_reactivate_alarm(clock);
851
852exit_idle_exit:
853 clock_state->stopped--;
854}
855
856/*
857 * Callback function that initializes the timeout value.
858 */
859static void msm_timer_get_sclk_time_start(
860 struct msm_timer_sync_data_t *data)
861{
862 data->timeout = 200000;
863}
864
865/*
866 * Callback function that checks the timeout.
867 */
868static bool msm_timer_get_sclk_time_expired(
869 struct msm_timer_sync_data_t *data)
870{
871 udelay(10);
872 return --data->timeout <= 0;
873}
874
875/*
876 * Retrieve the cycle count from the sclk and convert it into
877 * nanoseconds.
878 *
879 * On exit, if period is not NULL, it contains the period of the
880 * sclk in nanoseconds, i.e. how long the cycle count wraps around.
881 *
882 * Return value:
883 * 0: the operation failed; period is not set either
884 * >0: time in nanoseconds
885 */
886int64_t msm_timer_get_sclk_time(int64_t *period)
887{
888 struct msm_timer_sync_data_t data;
889 uint32_t clock_value;
890 int64_t tmp;
891
892 memset(&data, 0, sizeof(data));
893 clock_value = msm_timer_do_sync_to_sclk(
894 msm_timer_get_sclk_time_start,
895 msm_timer_get_sclk_time_expired,
896 NULL,
897 &data);
898
899 if (!clock_value)
900 return 0;
901
902 if (period) {
903 tmp = 1LL << 32;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700904 tmp *= NSEC_PER_SEC;
905 do_div(tmp, sclk_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700906 *period = tmp;
907 }
908
909 tmp = (int64_t)clock_value;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -0700910 tmp *= NSEC_PER_SEC;
911 do_div(tmp, sclk_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700912 return tmp;
913}
914
915int __init msm_timer_init_time_sync(void (*timeout)(void))
916{
917#if defined(CONFIG_MSM_N_WAY_SMSM) && !defined(CONFIG_MSM_DIRECT_SCLK_ACCESS)
918 int ret = smsm_change_intr_mask(SMSM_TIME_MASTER_DEM, 0xFFFFFFFF, 0);
919
920 if (ret) {
921 printk(KERN_ERR "%s: failed to clear interrupt mask, %d\n",
922 __func__, ret);
923 return ret;
924 }
925
926 smsm_change_state(SMSM_APPS_DEM,
927 SLAVE_TIME_REQUEST | SLAVE_TIME_POLL, SLAVE_TIME_INIT);
928#endif
929
930 BUG_ON(timeout == NULL);
931 msm_timer_sync_timeout = timeout;
932
933 return 0;
934}
935
936#endif
937
Steve Mucklef132c6c2012-06-06 18:30:57 -0700938static u32 notrace msm_read_sched_clock(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700939{
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700940 struct msm_clock *clock = &msm_clocks[msm_global_timer];
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700941 struct clocksource *cs = &clock->clocksource;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700942 return cs->read(NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700943}
944
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700945int read_current_timer(unsigned long *timer_val)
946{
947 struct msm_clock *dgt = &msm_clocks[MSM_CLOCK_DGT];
948 *timer_val = msm_read_timer_count(dgt, GLOBAL_TIMER);
949 return 0;
950}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700951
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700952static void __init msm_sched_clock_init(void)
953{
Jeff Ohlstein7a018322011-09-28 12:44:06 -0700954 struct msm_clock *clock = &msm_clocks[msm_global_timer];
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700955
Steve Mucklef132c6c2012-06-06 18:30:57 -0700956 setup_sched_clock(msm_read_sched_clock, 32 - clock->shift, clock->freq);
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -0700957}
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -0800958
Marc Zyngier5ca709c2012-01-10 19:44:19 +0000959#ifdef CONFIG_LOCAL_TIMERS
Steve Mucklef132c6c2012-06-06 18:30:57 -0700960int __cpuinit local_timer_setup(struct clock_event_device *evt)
Marc Zyngier5ca709c2012-01-10 19:44:19 +0000961{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700962 static DEFINE_PER_CPU(bool, first_boot) = true;
963 struct msm_clock *clock = &msm_clocks[msm_global_timer];
964
Marc Zyngier5ca709c2012-01-10 19:44:19 +0000965 /* Use existing clock_event for cpu 0 */
966 if (!smp_processor_id())
967 return 0;
968
Stepan Moskovchenko5b9e7762012-09-21 20:32:17 -0700969 if (cpu_is_msm8x60() || soc_class_is_msm8960() ||
970 soc_class_is_apq8064() || soc_class_is_msm8930())
Steve Mucklef132c6c2012-06-06 18:30:57 -0700971 __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
972
973 if (__get_cpu_var(first_boot)) {
974 __raw_writel(0, clock->regbase + TIMER_ENABLE);
975 __raw_writel(0, clock->regbase + TIMER_CLEAR);
976 __raw_writel(~0, clock->regbase + TIMER_MATCH_VAL);
977 __get_cpu_var(first_boot) = false;
978 if (clock->status_mask)
979 while (__raw_readl(MSM_TMR_BASE + TIMER_STATUS) &
980 clock->status_mask)
981 ;
982 }
983 evt->irq = clock->irq;
Marc Zyngier5ca709c2012-01-10 19:44:19 +0000984 evt->name = "local_timer";
Steve Mucklef132c6c2012-06-06 18:30:57 -0700985 evt->features = CLOCK_EVT_FEAT_ONESHOT;
986 evt->rating = clock->clockevent.rating;
Marc Zyngier5ca709c2012-01-10 19:44:19 +0000987 evt->set_mode = msm_timer_set_mode;
988 evt->set_next_event = msm_timer_set_next_event;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700989 evt->shift = clock->clockevent.shift;
990 evt->mult = div_sc(clock->freq, NSEC_PER_SEC, evt->shift);
991 evt->max_delta_ns =
992 clockevent_delta2ns(0xf0000000 >> clock->shift, evt);
Marc Zyngier5ca709c2012-01-10 19:44:19 +0000993 evt->min_delta_ns = clockevent_delta2ns(4, evt);
994
Steve Mucklef132c6c2012-06-06 18:30:57 -0700995 *__this_cpu_ptr(clock->percpu_evt) = evt;
996
Marc Zyngier5ca709c2012-01-10 19:44:19 +0000997 clockevents_register_device(evt);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700998 enable_percpu_irq(evt->irq, IRQ_TYPE_EDGE_RISING);
999
Marc Zyngier5ca709c2012-01-10 19:44:19 +00001000 return 0;
1001}
1002
Steve Mucklef132c6c2012-06-06 18:30:57 -07001003void local_timer_stop(struct clock_event_device *evt)
Marc Zyngier5ca709c2012-01-10 19:44:19 +00001004{
1005 evt->set_mode(CLOCK_EVT_MODE_UNUSED, evt);
1006 disable_percpu_irq(evt->irq);
1007}
1008
Steve Mucklef132c6c2012-06-06 18:30:57 -07001009static struct local_timer_ops msm_lt_ops = {
1010 local_timer_setup,
1011 local_timer_stop,
Marc Zyngier5ca709c2012-01-10 19:44:19 +00001012};
1013#endif /* CONFIG_LOCAL_TIMERS */
1014
Stepan Moskovchenko7543aaa2012-11-20 15:31:22 -08001015#ifdef CONFIG_ARCH_MSM8625
1016static void fixup_msm8625_timer(void)
1017{
1018 struct msm_clock *dgt = &msm_clocks[MSM_CLOCK_DGT];
1019 struct msm_clock *gpt = &msm_clocks[MSM_CLOCK_GPT];
1020 dgt->irq = MSM8625_INT_DEBUG_TIMER_EXP;
1021 gpt->irq = MSM8625_INT_GP_TIMER_EXP;
1022 global_timer_offset = MSM_TMR0_BASE - MSM_TMR_BASE;
1023}
1024#else
1025static inline void fixup_msm8625_timer(void) { };
1026#endif
1027
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001028static void __init msm_timer_init(void)
1029{
1030 int i;
1031 int res;
Jin Hongeecb1e02011-10-21 14:36:32 -07001032 struct irq_chip *chip;
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001033 struct msm_clock *dgt = &msm_clocks[MSM_CLOCK_DGT];
1034 struct msm_clock *gpt = &msm_clocks[MSM_CLOCK_GPT];
Stephen Boyddd15ab82011-11-08 10:34:05 -08001035
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001036 if (cpu_is_msm7x01() || cpu_is_msm7x25() || cpu_is_msm7x27() ||
1037 cpu_is_msm7x25a() || cpu_is_msm7x27a() || cpu_is_msm7x25aa() ||
Pankaj Kumarfee56a82012-04-17 14:26:49 +05301038 cpu_is_msm7x27aa() || cpu_is_msm8625() || cpu_is_msm7x25ab()) {
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001039 dgt->shift = MSM_DGT_SHIFT;
1040 dgt->freq = 19200000 >> MSM_DGT_SHIFT;
1041 dgt->clockevent.shift = 32 + MSM_DGT_SHIFT;
1042 dgt->clocksource.mask = CLOCKSOURCE_MASK(32 - MSM_DGT_SHIFT);
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001043 gpt->regbase = MSM_TMR_BASE;
1044 dgt->regbase = MSM_TMR_BASE + 0x10;
Jeff Ohlstein5d90e252011-11-04 19:00:50 -07001045 gpt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT
1046 | MSM_CLOCK_FLAGS_ODD_MATCH_WRITE
1047 | MSM_CLOCK_FLAGS_DELAYED_WRITE_POST;
Stepan Moskovchenko7543aaa2012-11-20 15:31:22 -08001048 if (cpu_is_msm8625())
1049 fixup_msm8625_timer();
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001050 } else if (cpu_is_qsd8x50()) {
1051 dgt->freq = 4800000;
1052 gpt->regbase = MSM_TMR_BASE;
1053 dgt->regbase = MSM_TMR_BASE + 0x10;
1054 } else if (cpu_is_fsm9xxx())
1055 dgt->freq = 4800000;
Jeff Ohlstein6c47a272012-02-24 14:48:55 -08001056 else if (cpu_is_msm7x30() || cpu_is_msm8x55()) {
1057 gpt->status_mask = BIT(10);
1058 dgt->status_mask = BIT(2);
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001059 dgt->freq = 6144000;
Jeff Ohlstein6c47a272012-02-24 14:48:55 -08001060 } else if (cpu_is_msm8x60()) {
Jeff Ohlstein7e538f02011-11-01 17:36:22 -07001061 global_timer_offset = MSM_TMR0_BASE - MSM_TMR_BASE;
Jeff Ohlstein6c47a272012-02-24 14:48:55 -08001062 gpt->status_mask = BIT(10);
1063 dgt->status_mask = BIT(2);
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001064 dgt->freq = 6750000;
1065 __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
Jeff Ohlstein7e538f02011-11-01 17:36:22 -07001066 } else if (cpu_is_msm9615()) {
1067 dgt->freq = 6750000;
1068 __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
Jeff Ohlstein6c47a272012-02-24 14:48:55 -08001069 gpt->status_mask = BIT(10);
1070 dgt->status_mask = BIT(2);
Jeff Ohlstein7e538f02011-11-01 17:36:22 -07001071 gpt->freq = 32765;
1072 gpt_hz = 32765;
1073 sclk_hz = 32765;
Jeff Ohlsteind47f96a2011-11-04 19:00:50 -07001074 gpt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT;
1075 dgt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT;
Stepan Moskovchenko5b9e7762012-09-21 20:32:17 -07001076 } else if (soc_class_is_msm8960() || soc_class_is_apq8064() ||
1077 soc_class_is_msm8930()) {
Jeff Ohlstein7e538f02011-11-01 17:36:22 -07001078 global_timer_offset = MSM_TMR0_BASE - MSM_TMR_BASE;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -07001079 dgt->freq = 6750000;
1080 __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL);
Jeff Ohlstein6c47a272012-02-24 14:48:55 -08001081 gpt->status_mask = BIT(10);
1082 dgt->status_mask = BIT(2);
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -07001083 gpt->freq = 32765;
1084 gpt_hz = 32765;
1085 sclk_hz = 32765;
Stepan Moskovchenko5b9e7762012-09-21 20:32:17 -07001086 if (!soc_class_is_msm8930() && !cpu_is_msm8960ab()) {
Jeff Ohlstein391a3ee2011-12-01 16:44:45 -08001087 gpt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT;
1088 dgt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT;
Marc Zyngier5ca709c2012-01-10 19:44:19 +00001089 }
Stephen Boyddd15ab82011-11-08 10:34:05 -08001090 } else {
Jeff Ohlsteinf0a31e42012-01-06 19:03:05 -08001091 WARN(1, "Timer running on unknown hardware. Configure this! "
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001092 "Assuming default configuration.\n");
1093 dgt->freq = 6750000;
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001094 }
Stephen Boyddd15ab82011-11-08 10:34:05 -08001095
Jeff Ohlstein7a018322011-09-28 12:44:06 -07001096 if (msm_clocks[MSM_CLOCK_GPT].clocksource.rating > DG_TIMER_RATING)
1097 msm_global_timer = MSM_CLOCK_GPT;
1098 else
1099 msm_global_timer = MSM_CLOCK_DGT;
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001100
1101 for (i = 0; i < ARRAY_SIZE(msm_clocks); i++) {
1102 struct msm_clock *clock = &msm_clocks[i];
1103 struct clock_event_device *ce = &clock->clockevent;
1104 struct clocksource *cs = &clock->clocksource;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001105 __raw_writel(0, clock->regbase + TIMER_ENABLE);
Jeff Ohlstein6c47a272012-02-24 14:48:55 -08001106 __raw_writel(0, clock->regbase + TIMER_CLEAR);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001107 __raw_writel(~0, clock->regbase + TIMER_MATCH_VAL);
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001108
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -07001109 if ((clock->freq << clock->shift) == gpt_hz) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001110 clock->rollover_offset = 0;
1111 } else {
1112 uint64_t temp;
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001113
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001114 temp = clock->freq << clock->shift;
1115 temp <<= 32;
Jeff Ohlsteinc83811b2011-10-21 14:24:04 -07001116 do_div(temp, gpt_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001117
1118 clock->rollover_offset = (uint32_t) temp;
1119 }
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001120
1121 ce->mult = div_sc(clock->freq, NSEC_PER_SEC, ce->shift);
1122 /* allow at least 10 seconds to notice that the timer wrapped */
1123 ce->max_delta_ns =
1124 clockevent_delta2ns(0xf0000000 >> clock->shift, ce);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001125 /* ticks gets rounded down by one */
1126 ce->min_delta_ns =
1127 clockevent_delta2ns(clock->write_delay + 4, ce);
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001128 ce->cpumask = cpumask_of(0);
1129
Jeff Ohlstein711a7142012-05-23 11:57:33 -07001130 res = clocksource_register_hz(cs, clock->freq);
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001131 if (res)
1132 printk(KERN_ERR "msm_timer_init: clocksource_register "
1133 "failed for %s\n", cs->name);
1134
Trilok Sonieecb28c2011-07-20 16:24:14 +01001135 ce->irq = clock->irq;
Stepan Moskovchenko5b9e7762012-09-21 20:32:17 -07001136 if (cpu_is_msm8x60() || cpu_is_msm9615() || cpu_is_msm8625() ||
1137 soc_class_is_msm8960() || soc_class_is_apq8064() ||
1138 soc_class_is_msm8930()) {
Trilok Sonieecb28c2011-07-20 16:24:14 +01001139 clock->percpu_evt = alloc_percpu(struct clock_event_device *);
1140 if (!clock->percpu_evt) {
1141 pr_err("msm_timer_init: memory allocation "
1142 "failed for %s\n", ce->name);
1143 continue;
1144 }
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001145
Trilok Sonieecb28c2011-07-20 16:24:14 +01001146 *__this_cpu_ptr(clock->percpu_evt) = ce;
1147 res = request_percpu_irq(ce->irq, msm_timer_interrupt,
1148 ce->name, clock->percpu_evt);
1149 if (!res)
Trilok Soni1e52e432012-01-13 18:06:14 +05301150 enable_percpu_irq(ce->irq,
1151 IRQ_TYPE_EDGE_RISING);
Trilok Sonieecb28c2011-07-20 16:24:14 +01001152 } else {
1153 clock->evt = ce;
1154 res = request_irq(ce->irq, msm_timer_interrupt,
1155 IRQF_TIMER | IRQF_NOBALANCING | IRQF_TRIGGER_RISING,
1156 ce->name, &clock->evt);
1157 }
1158
1159 if (res)
1160 pr_err("msm_timer_init: request_irq failed for %s\n",
1161 ce->name);
1162
1163 chip = irq_get_chip(clock->irq);
Jin Hongeecb1e02011-10-21 14:36:32 -07001164 if (chip && chip->irq_mask)
Trilok Sonieecb28c2011-07-20 16:24:14 +01001165 chip->irq_mask(irq_get_irq_data(clock->irq));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001166
Jeff Ohlstein6c47a272012-02-24 14:48:55 -08001167 if (clock->status_mask)
1168 while (__raw_readl(MSM_TMR_BASE + TIMER_STATUS) &
1169 clock->status_mask)
1170 ;
1171
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001172 clockevents_register_device(ce);
1173 }
Jeff Ohlstein4e93ae12011-09-26 18:22:26 -07001174 msm_sched_clock_init();
Taniya Das36057be2011-10-28 13:02:17 +05301175
Greg Reidb1d240a2012-10-12 12:20:31 -04001176 if (use_user_accessible_timers()) {
1177 if (cpu_is_msm8960() || cpu_is_msm8930() || cpu_is_apq8064()) {
1178 struct msm_clock *gtclock = &msm_clocks[MSM_CLOCK_GPT];
1179 void __iomem *addr = gtclock->regbase +
1180 TIMER_COUNT_VAL + global_timer_offset;
1181 setup_user_timer_offset(virt_to_phys(addr)&0xfff);
1182 set_user_accessible_timer_flag(true);
1183 }
1184 }
1185
Taniya Dasc43e6872012-03-21 16:41:14 +05301186#ifdef ARCH_HAS_READ_CURRENT_TIMER
1187 if (is_smp()) {
Taniya Dasbb0b6db2012-03-19 14:09:55 +05301188 __raw_writel(1,
1189 msm_clocks[MSM_CLOCK_DGT].regbase + TIMER_ENABLE);
1190 set_delay_fn(read_current_timer_delay_loop);
1191 }
Taniya Dasc43e6872012-03-21 16:41:14 +05301192#endif
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001193
Steve Mucklef132c6c2012-06-06 18:30:57 -07001194#ifdef CONFIG_LOCAL_TIMERS
1195 local_timer_register(&msm_lt_ops);
Jeff Ohlstein94790ec2010-12-02 12:05:12 -08001196#endif
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001197}
1198
Arve Hjønnevåg3e4ea372007-11-26 04:11:58 -08001199struct sys_timer msm_timer = {
1200 .init = msm_timer_init
1201};