Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 1 | /* linux/arch/arm/mach-msm/timer.c |
| 2 | * |
| 3 | * Copyright (C) 2007 Google, Inc. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 4 | * Copyright (c) 2009-2011, Code Aurora Forum. All rights reserved. |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 5 | * |
| 6 | * This software is licensed under the terms of the GNU General Public |
| 7 | * License version 2, as published by the Free Software Foundation, and |
| 8 | * may be copied, distributed, and modified under those terms. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/time.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/irq.h> |
| 21 | #include <linux/clk.h> |
| 22 | #include <linux/clockchips.h> |
| 23 | #include <linux/delay.h> |
Russell King | fced80c | 2008-09-06 12:10:45 +0100 | [diff] [blame] | 24 | #include <linux/io.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 25 | #include <linux/percpu.h> |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 26 | |
| 27 | #include <asm/mach/time.h> |
Stephen Boyd | ebf30dc | 2011-05-31 16:10:00 -0700 | [diff] [blame] | 28 | #include <asm/hardware/gic.h> |
Jeff Ohlstein | 4e93ae1 | 2011-09-26 18:22:26 -0700 | [diff] [blame] | 29 | #include <asm/sched_clock.h> |
Taniya Das | 36057be | 2011-10-28 13:02:17 +0530 | [diff] [blame] | 30 | #include <asm/smp_plat.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 31 | #include <mach/msm_iomap.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 32 | #include <mach/irqs.h> |
| 33 | #include <mach/socinfo.h> |
| 34 | |
| 35 | #if defined(CONFIG_MSM_SMD) |
| 36 | #include "smd_private.h" |
| 37 | #endif |
| 38 | #include "timer.h" |
| 39 | |
| 40 | enum { |
| 41 | MSM_TIMER_DEBUG_SYNC = 1U << 0, |
| 42 | }; |
| 43 | static int msm_timer_debug_mask; |
| 44 | module_param_named(debug_mask, msm_timer_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP); |
| 45 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 46 | #ifdef CONFIG_MSM7X00A_USE_GP_TIMER |
| 47 | #define DG_TIMER_RATING 100 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 48 | #else |
| 49 | #define DG_TIMER_RATING 300 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 50 | #endif |
| 51 | |
Jeff Ohlstein | 7e538f0 | 2011-11-01 17:36:22 -0700 | [diff] [blame] | 52 | #ifndef MSM_TMR0_BASE |
| 53 | #define MSM_TMR0_BASE MSM_TMR_BASE |
| 54 | #endif |
| 55 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 56 | #define MSM_DGT_SHIFT (5) |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 57 | |
| 58 | #define TIMER_MATCH_VAL 0x0000 |
| 59 | #define TIMER_COUNT_VAL 0x0004 |
| 60 | #define TIMER_ENABLE 0x0008 |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 61 | #define TIMER_CLEAR 0x000C |
Jeff Ohlstein | 672039f | 2010-10-05 15:23:57 -0700 | [diff] [blame] | 62 | #define DGT_CLK_CTL 0x0034 |
| 63 | enum { |
| 64 | DGT_CLK_CTL_DIV_1 = 0, |
| 65 | DGT_CLK_CTL_DIV_2 = 1, |
| 66 | DGT_CLK_CTL_DIV_3 = 2, |
| 67 | DGT_CLK_CTL_DIV_4 = 3, |
| 68 | }; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 69 | #define TIMER_ENABLE_EN 1 |
| 70 | #define TIMER_ENABLE_CLR_ON_MATCH_EN 2 |
| 71 | |
| 72 | #define LOCAL_TIMER 0 |
| 73 | #define GLOBAL_TIMER 1 |
| 74 | |
| 75 | /* |
Jeff Ohlstein | e1a7e40 | 2011-09-07 12:52:36 -0700 | [diff] [blame] | 76 | * global_timer_offset is added to the regbase of a timer to force the memory |
| 77 | * access to come from the CPU0 region. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 78 | */ |
Jeff Ohlstein | e1a7e40 | 2011-09-07 12:52:36 -0700 | [diff] [blame] | 79 | static int global_timer_offset; |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 80 | static int msm_global_timer; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 81 | |
| 82 | #define NR_TIMERS ARRAY_SIZE(msm_clocks) |
| 83 | |
Jeff Ohlstein | c83811b | 2011-10-21 14:24:04 -0700 | [diff] [blame] | 84 | unsigned int gpt_hz = 32768; |
| 85 | unsigned int sclk_hz = 32768; |
Jeff Ohlstein | 94790ec | 2010-12-02 12:05:12 -0800 | [diff] [blame] | 86 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 87 | static struct msm_clock *clockevent_to_clock(struct clock_event_device *evt); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 88 | static irqreturn_t msm_timer_interrupt(int irq, void *dev_id); |
| 89 | static cycle_t msm_gpt_read(struct clocksource *cs); |
| 90 | static cycle_t msm_dgt_read(struct clocksource *cs); |
| 91 | static void msm_timer_set_mode(enum clock_event_mode mode, |
| 92 | struct clock_event_device *evt); |
| 93 | static int msm_timer_set_next_event(unsigned long cycles, |
| 94 | struct clock_event_device *evt); |
| 95 | |
| 96 | enum { |
| 97 | MSM_CLOCK_FLAGS_UNSTABLE_COUNT = 1U << 0, |
| 98 | MSM_CLOCK_FLAGS_ODD_MATCH_WRITE = 1U << 1, |
| 99 | MSM_CLOCK_FLAGS_DELAYED_WRITE_POST = 1U << 2, |
| 100 | }; |
| 101 | |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 102 | struct msm_clock { |
| 103 | struct clock_event_device clockevent; |
| 104 | struct clocksource clocksource; |
Trilok Soni | eecb28c | 2011-07-20 16:24:14 +0100 | [diff] [blame^] | 105 | unsigned int irq; |
Brian Swetland | bcc0f6a | 2008-09-10 14:00:53 -0700 | [diff] [blame] | 106 | void __iomem *regbase; |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 107 | uint32_t freq; |
| 108 | uint32_t shift; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 109 | uint32_t flags; |
| 110 | uint32_t write_delay; |
| 111 | uint32_t rollover_offset; |
| 112 | uint32_t index; |
Trilok Soni | eecb28c | 2011-07-20 16:24:14 +0100 | [diff] [blame^] | 113 | void __iomem *global_counter; |
| 114 | void __iomem *local_counter; |
| 115 | union { |
| 116 | struct clock_event_device *evt; |
| 117 | struct clock_event_device __percpu **percpu_evt; |
| 118 | }; |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 119 | }; |
| 120 | |
Jeff Ohlstein | 94790ec | 2010-12-02 12:05:12 -0800 | [diff] [blame] | 121 | enum { |
| 122 | MSM_CLOCK_GPT, |
| 123 | MSM_CLOCK_DGT, |
Jeff Ohlstein | 94790ec | 2010-12-02 12:05:12 -0800 | [diff] [blame] | 124 | }; |
| 125 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 126 | struct msm_clock_percpu_data { |
| 127 | uint32_t last_set; |
| 128 | uint32_t sleep_offset; |
| 129 | uint32_t alarm_vtime; |
| 130 | uint32_t alarm; |
| 131 | uint32_t non_sleep_offset; |
| 132 | uint32_t in_sync; |
| 133 | cycle_t stopped_tick; |
| 134 | int stopped; |
| 135 | uint32_t last_sync_gpt; |
| 136 | u64 last_sync_jiffies; |
| 137 | }; |
Jeff Ohlstein | 94790ec | 2010-12-02 12:05:12 -0800 | [diff] [blame] | 138 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 139 | struct msm_timer_sync_data_t { |
| 140 | struct msm_clock *clock; |
| 141 | uint32_t timeout; |
| 142 | int exit_sleep; |
| 143 | }; |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 144 | |
| 145 | static struct msm_clock msm_clocks[] = { |
Jeff Ohlstein | 94790ec | 2010-12-02 12:05:12 -0800 | [diff] [blame] | 146 | [MSM_CLOCK_GPT] = { |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 147 | .clockevent = { |
| 148 | .name = "gp_timer", |
| 149 | .features = CLOCK_EVT_FEAT_ONESHOT, |
| 150 | .shift = 32, |
| 151 | .rating = 200, |
| 152 | .set_next_event = msm_timer_set_next_event, |
| 153 | .set_mode = msm_timer_set_mode, |
| 154 | }, |
| 155 | .clocksource = { |
| 156 | .name = "gp_timer", |
| 157 | .rating = 200, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 158 | .read = msm_gpt_read, |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 159 | .mask = CLOCKSOURCE_MASK(32), |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 160 | .shift = 17, |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 161 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 162 | }, |
Trilok Soni | eecb28c | 2011-07-20 16:24:14 +0100 | [diff] [blame^] | 163 | .irq = INT_GP_TIMER_EXP, |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 164 | .regbase = MSM_TMR_BASE + 0x4, |
Jeff Ohlstein | c83811b | 2011-10-21 14:24:04 -0700 | [diff] [blame] | 165 | .freq = 32768, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 166 | .index = MSM_CLOCK_GPT, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 167 | .write_delay = 9, |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 168 | }, |
Jeff Ohlstein | 94790ec | 2010-12-02 12:05:12 -0800 | [diff] [blame] | 169 | [MSM_CLOCK_DGT] = { |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 170 | .clockevent = { |
| 171 | .name = "dg_timer", |
| 172 | .features = CLOCK_EVT_FEAT_ONESHOT, |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 173 | .shift = 32, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 174 | .rating = DG_TIMER_RATING, |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 175 | .set_next_event = msm_timer_set_next_event, |
| 176 | .set_mode = msm_timer_set_mode, |
| 177 | }, |
| 178 | .clocksource = { |
| 179 | .name = "dg_timer", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 180 | .rating = DG_TIMER_RATING, |
| 181 | .read = msm_dgt_read, |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 182 | .mask = CLOCKSOURCE_MASK(32), |
| 183 | .shift = 24, |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 184 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 185 | }, |
Trilok Soni | eecb28c | 2011-07-20 16:24:14 +0100 | [diff] [blame^] | 186 | .irq = INT_DEBUG_TIMER_EXP, |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 187 | .regbase = MSM_TMR_BASE + 0x24, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 188 | .index = MSM_CLOCK_DGT, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 189 | .write_delay = 9, |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 190 | } |
| 191 | }; |
| 192 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 193 | static DEFINE_PER_CPU(struct msm_clock_percpu_data[NR_TIMERS], |
| 194 | msm_clocks_percpu); |
| 195 | |
| 196 | static DEFINE_PER_CPU(struct msm_clock *, msm_active_clock); |
| 197 | |
| 198 | static irqreturn_t msm_timer_interrupt(int irq, void *dev_id) |
| 199 | { |
Trilok Soni | eecb28c | 2011-07-20 16:24:14 +0100 | [diff] [blame^] | 200 | struct clock_event_device *evt = *(struct clock_event_device **)dev_id; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 201 | if (evt->event_handler == NULL) |
| 202 | return IRQ_HANDLED; |
| 203 | evt->event_handler(evt); |
| 204 | return IRQ_HANDLED; |
| 205 | } |
| 206 | |
| 207 | static uint32_t msm_read_timer_count(struct msm_clock *clock, int global) |
| 208 | { |
Jeff Ohlstein | 5d90e25 | 2011-11-04 19:00:50 -0700 | [diff] [blame] | 209 | uint32_t t1, t2, t3; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 210 | int loop_count = 0; |
Jeff Ohlstein | 5d90e25 | 2011-11-04 19:00:50 -0700 | [diff] [blame] | 211 | void __iomem *addr = clock->regbase + TIMER_COUNT_VAL + |
| 212 | global*global_timer_offset; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 213 | |
| 214 | if (!(clock->flags & MSM_CLOCK_FLAGS_UNSTABLE_COUNT)) |
Jeff Ohlstein | 5d90e25 | 2011-11-04 19:00:50 -0700 | [diff] [blame] | 215 | return __raw_readl(addr); |
| 216 | |
| 217 | t1 = __raw_readl(addr); |
| 218 | t2 = __raw_readl(addr); |
| 219 | if ((t2-t1) <= 1) |
| 220 | return t2; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 221 | while (1) { |
Jeff Ohlstein | 5d90e25 | 2011-11-04 19:00:50 -0700 | [diff] [blame] | 222 | t1 = __raw_readl(addr); |
| 223 | t2 = __raw_readl(addr); |
| 224 | t3 = __raw_readl(addr); |
Jeff Ohlstein | 10206eb | 2011-11-30 19:18:49 -0800 | [diff] [blame] | 225 | cpu_relax(); |
Jeff Ohlstein | 5d90e25 | 2011-11-04 19:00:50 -0700 | [diff] [blame] | 226 | if ((t3-t2) <= 1) |
| 227 | return t3; |
| 228 | if ((t2-t1) <= 1) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 229 | return t2; |
Jeff Ohlstein | fdd8708 | 2011-12-09 13:40:08 -0800 | [diff] [blame] | 230 | if ((t2 >= t1) && (t3 >= t2)) |
| 231 | return t2; |
Jeff Ohlstein | 10206eb | 2011-11-30 19:18:49 -0800 | [diff] [blame] | 232 | if (++loop_count == 5) { |
Jeff Ohlstein | 5d90e25 | 2011-11-04 19:00:50 -0700 | [diff] [blame] | 233 | pr_err("msm_read_timer_count timer %s did not " |
| 234 | "stabilize: %u -> %u -> %u\n", |
| 235 | clock->clockevent.name, t1, t2, t3); |
| 236 | return t3; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 237 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
| 241 | static cycle_t msm_gpt_read(struct clocksource *cs) |
| 242 | { |
| 243 | struct msm_clock *clock = &msm_clocks[MSM_CLOCK_GPT]; |
| 244 | struct msm_clock_percpu_data *clock_state = |
| 245 | &per_cpu(msm_clocks_percpu, 0)[MSM_CLOCK_GPT]; |
| 246 | |
| 247 | if (clock_state->stopped) |
| 248 | return clock_state->stopped_tick; |
| 249 | |
| 250 | return msm_read_timer_count(clock, GLOBAL_TIMER) + |
| 251 | clock_state->sleep_offset; |
| 252 | } |
| 253 | |
| 254 | static cycle_t msm_dgt_read(struct clocksource *cs) |
| 255 | { |
| 256 | struct msm_clock *clock = &msm_clocks[MSM_CLOCK_DGT]; |
| 257 | struct msm_clock_percpu_data *clock_state = |
| 258 | &per_cpu(msm_clocks_percpu, 0)[MSM_CLOCK_DGT]; |
| 259 | |
| 260 | if (clock_state->stopped) |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 261 | return clock_state->stopped_tick >> clock->shift; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 262 | |
| 263 | return (msm_read_timer_count(clock, GLOBAL_TIMER) + |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 264 | clock_state->sleep_offset) >> clock->shift; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 267 | static struct msm_clock *clockevent_to_clock(struct clock_event_device *evt) |
| 268 | { |
| 269 | int i; |
Taniya Das | 36057be | 2011-10-28 13:02:17 +0530 | [diff] [blame] | 270 | |
| 271 | if (!is_smp()) |
| 272 | return container_of(evt, struct msm_clock, clockevent); |
| 273 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 274 | for (i = 0; i < NR_TIMERS; i++) |
| 275 | if (evt == &(msm_clocks[i].clockevent)) |
| 276 | return &msm_clocks[i]; |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 277 | return &msm_clocks[msm_global_timer]; |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 278 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 279 | |
| 280 | static int msm_timer_set_next_event(unsigned long cycles, |
| 281 | struct clock_event_device *evt) |
| 282 | { |
| 283 | int i; |
| 284 | struct msm_clock *clock; |
| 285 | struct msm_clock_percpu_data *clock_state; |
| 286 | uint32_t now; |
| 287 | uint32_t alarm; |
| 288 | int late; |
| 289 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 290 | clock = clockevent_to_clock(evt); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 291 | clock_state = &__get_cpu_var(msm_clocks_percpu)[clock->index]; |
| 292 | if (clock_state->stopped) |
| 293 | return 0; |
| 294 | now = msm_read_timer_count(clock, LOCAL_TIMER); |
| 295 | alarm = now + (cycles << clock->shift); |
| 296 | if (clock->flags & MSM_CLOCK_FLAGS_ODD_MATCH_WRITE) |
| 297 | while (now == clock_state->last_set) |
| 298 | now = msm_read_timer_count(clock, LOCAL_TIMER); |
| 299 | |
| 300 | clock_state->alarm = alarm; |
| 301 | __raw_writel(alarm, clock->regbase + TIMER_MATCH_VAL); |
| 302 | |
| 303 | if (clock->flags & MSM_CLOCK_FLAGS_DELAYED_WRITE_POST) { |
| 304 | /* read the counter four extra times to make sure write posts |
| 305 | before reading the time */ |
| 306 | for (i = 0; i < 4; i++) |
| 307 | __raw_readl(clock->regbase + TIMER_COUNT_VAL); |
| 308 | } |
| 309 | now = msm_read_timer_count(clock, LOCAL_TIMER); |
| 310 | clock_state->last_set = now; |
| 311 | clock_state->alarm_vtime = alarm + clock_state->sleep_offset; |
| 312 | late = now - alarm; |
| 313 | if (late >= (int)(-clock->write_delay << clock->shift) && |
| 314 | late < clock->freq*5) |
| 315 | return -ETIME; |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static void msm_timer_set_mode(enum clock_event_mode mode, |
| 321 | struct clock_event_device *evt) |
| 322 | { |
| 323 | struct msm_clock *clock; |
| 324 | struct msm_clock_percpu_data *clock_state, *gpt_state; |
| 325 | unsigned long irq_flags; |
Jin Hong | eecb1e0 | 2011-10-21 14:36:32 -0700 | [diff] [blame] | 326 | struct irq_chip *chip; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 327 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 328 | clock = clockevent_to_clock(evt); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 329 | clock_state = &__get_cpu_var(msm_clocks_percpu)[clock->index]; |
| 330 | gpt_state = &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT]; |
| 331 | |
| 332 | local_irq_save(irq_flags); |
| 333 | |
| 334 | switch (mode) { |
| 335 | case CLOCK_EVT_MODE_RESUME: |
| 336 | case CLOCK_EVT_MODE_PERIODIC: |
| 337 | break; |
| 338 | case CLOCK_EVT_MODE_ONESHOT: |
| 339 | clock_state->stopped = 0; |
| 340 | clock_state->sleep_offset = |
| 341 | -msm_read_timer_count(clock, LOCAL_TIMER) + |
| 342 | clock_state->stopped_tick; |
| 343 | get_cpu_var(msm_active_clock) = clock; |
| 344 | put_cpu_var(msm_active_clock); |
| 345 | __raw_writel(TIMER_ENABLE_EN, clock->regbase + TIMER_ENABLE); |
Trilok Soni | eecb28c | 2011-07-20 16:24:14 +0100 | [diff] [blame^] | 346 | chip = irq_get_chip(clock->irq); |
Jin Hong | eecb1e0 | 2011-10-21 14:36:32 -0700 | [diff] [blame] | 347 | if (chip && chip->irq_unmask) |
Trilok Soni | eecb28c | 2011-07-20 16:24:14 +0100 | [diff] [blame^] | 348 | chip->irq_unmask(irq_get_irq_data(clock->irq)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 349 | if (clock != &msm_clocks[MSM_CLOCK_GPT]) |
| 350 | __raw_writel(TIMER_ENABLE_EN, |
| 351 | msm_clocks[MSM_CLOCK_GPT].regbase + |
| 352 | TIMER_ENABLE); |
| 353 | break; |
| 354 | case CLOCK_EVT_MODE_UNUSED: |
| 355 | case CLOCK_EVT_MODE_SHUTDOWN: |
| 356 | get_cpu_var(msm_active_clock) = NULL; |
| 357 | put_cpu_var(msm_active_clock); |
| 358 | clock_state->in_sync = 0; |
| 359 | clock_state->stopped = 1; |
| 360 | clock_state->stopped_tick = |
| 361 | msm_read_timer_count(clock, LOCAL_TIMER) + |
| 362 | clock_state->sleep_offset; |
| 363 | __raw_writel(0, clock->regbase + TIMER_MATCH_VAL); |
Trilok Soni | eecb28c | 2011-07-20 16:24:14 +0100 | [diff] [blame^] | 364 | chip = irq_get_chip(clock->irq); |
Jin Hong | eecb1e0 | 2011-10-21 14:36:32 -0700 | [diff] [blame] | 365 | if (chip && chip->irq_mask) |
Trilok Soni | eecb28c | 2011-07-20 16:24:14 +0100 | [diff] [blame^] | 366 | chip->irq_mask(irq_get_irq_data(clock->irq)); |
Taniya Das | 36057be | 2011-10-28 13:02:17 +0530 | [diff] [blame] | 367 | |
| 368 | if (!is_smp() || clock != &msm_clocks[MSM_CLOCK_DGT] |
| 369 | || smp_processor_id()) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 370 | __raw_writel(0, clock->regbase + TIMER_ENABLE); |
Taniya Das | 36057be | 2011-10-28 13:02:17 +0530 | [diff] [blame] | 371 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 372 | if (clock != &msm_clocks[MSM_CLOCK_GPT]) { |
| 373 | gpt_state->in_sync = 0; |
| 374 | __raw_writel(0, msm_clocks[MSM_CLOCK_GPT].regbase + |
| 375 | TIMER_ENABLE); |
| 376 | } |
| 377 | break; |
| 378 | } |
| 379 | wmb(); |
| 380 | local_irq_restore(irq_flags); |
| 381 | } |
| 382 | |
Jeff Ohlstein | 973871d | 2011-09-28 11:46:26 -0700 | [diff] [blame] | 383 | void __iomem *msm_timer_get_timer0_base(void) |
| 384 | { |
| 385 | return MSM_TMR_BASE + global_timer_offset; |
| 386 | } |
| 387 | |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 388 | #define MPM_SCLK_COUNT_VAL 0x0024 |
| 389 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 390 | #ifdef CONFIG_PM |
| 391 | /* |
| 392 | * Retrieve the cycle count from sclk and optionally synchronize local clock |
| 393 | * with the sclk value. |
| 394 | * |
| 395 | * time_start and time_expired are callbacks that must be specified. The |
| 396 | * protocol uses them to detect timeout. The update callback is optional. |
| 397 | * If not NULL, update will be called so that it can update local clock. |
| 398 | * |
| 399 | * The function does not use the argument data directly; it passes data to |
| 400 | * the callbacks. |
| 401 | * |
| 402 | * Return value: |
| 403 | * 0: the operation failed |
| 404 | * >0: the slow clock value after time-sync |
| 405 | */ |
| 406 | static void (*msm_timer_sync_timeout)(void); |
| 407 | #if defined(CONFIG_MSM_DIRECT_SCLK_ACCESS) |
| 408 | static uint32_t msm_timer_do_sync_to_sclk( |
| 409 | void (*time_start)(struct msm_timer_sync_data_t *data), |
| 410 | bool (*time_expired)(struct msm_timer_sync_data_t *data), |
| 411 | void (*update)(struct msm_timer_sync_data_t *, uint32_t, uint32_t), |
| 412 | struct msm_timer_sync_data_t *data) |
| 413 | { |
| 414 | uint32_t t1, t2; |
| 415 | int loop_count = 10; |
| 416 | int loop_zero_count = 3; |
Jeff Ohlstein | c83811b | 2011-10-21 14:24:04 -0700 | [diff] [blame] | 417 | int tmp = USEC_PER_SEC; |
| 418 | do_div(tmp, sclk_hz); |
| 419 | tmp /= (loop_zero_count-1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 420 | |
| 421 | while (loop_zero_count--) { |
| 422 | t1 = __raw_readl(MSM_RPM_MPM_BASE + MPM_SCLK_COUNT_VAL); |
| 423 | do { |
| 424 | udelay(1); |
| 425 | t2 = t1; |
| 426 | t1 = __raw_readl(MSM_RPM_MPM_BASE + MPM_SCLK_COUNT_VAL); |
| 427 | } while ((t2 != t1) && --loop_count); |
| 428 | |
| 429 | if (!loop_count) { |
| 430 | printk(KERN_EMERG "SCLK did not stabilize\n"); |
| 431 | return 0; |
| 432 | } |
| 433 | |
| 434 | if (t1) |
| 435 | break; |
| 436 | |
| 437 | udelay(tmp); |
| 438 | } |
| 439 | |
| 440 | if (!loop_zero_count) { |
| 441 | printk(KERN_EMERG "SCLK reads zero\n"); |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | if (update != NULL) |
Jeff Ohlstein | c83811b | 2011-10-21 14:24:04 -0700 | [diff] [blame] | 446 | update(data, t1, sclk_hz); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 447 | return t1; |
| 448 | } |
| 449 | #elif defined(CONFIG_MSM_N_WAY_SMSM) |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 450 | |
| 451 | /* Time Master State Bits */ |
| 452 | #define MASTER_BITS_PER_CPU 1 |
| 453 | #define MASTER_TIME_PENDING \ |
| 454 | (0x01UL << (MASTER_BITS_PER_CPU * SMSM_APPS_STATE)) |
| 455 | |
| 456 | /* Time Slave State Bits */ |
| 457 | #define SLAVE_TIME_REQUEST 0x0400 |
| 458 | #define SLAVE_TIME_POLL 0x0800 |
| 459 | #define SLAVE_TIME_INIT 0x1000 |
| 460 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 461 | static uint32_t msm_timer_do_sync_to_sclk( |
| 462 | void (*time_start)(struct msm_timer_sync_data_t *data), |
| 463 | bool (*time_expired)(struct msm_timer_sync_data_t *data), |
| 464 | void (*update)(struct msm_timer_sync_data_t *, uint32_t, uint32_t), |
| 465 | struct msm_timer_sync_data_t *data) |
| 466 | { |
| 467 | uint32_t *smem_clock; |
| 468 | uint32_t smem_clock_val; |
| 469 | uint32_t state; |
| 470 | |
| 471 | smem_clock = smem_alloc(SMEM_SMEM_SLOW_CLOCK_VALUE, sizeof(uint32_t)); |
| 472 | if (smem_clock == NULL) { |
| 473 | printk(KERN_ERR "no smem clock\n"); |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | state = smsm_get_state(SMSM_MODEM_STATE); |
| 478 | if ((state & SMSM_INIT) == 0) { |
| 479 | printk(KERN_ERR "smsm not initialized\n"); |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | time_start(data); |
| 484 | while ((state = smsm_get_state(SMSM_TIME_MASTER_DEM)) & |
| 485 | MASTER_TIME_PENDING) { |
| 486 | if (time_expired(data)) { |
| 487 | printk(KERN_EMERG "get_smem_clock: timeout 1 still " |
| 488 | "invalid state %x\n", state); |
| 489 | msm_timer_sync_timeout(); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | smsm_change_state(SMSM_APPS_DEM, SLAVE_TIME_POLL | SLAVE_TIME_INIT, |
| 494 | SLAVE_TIME_REQUEST); |
| 495 | |
| 496 | time_start(data); |
| 497 | while (!((state = smsm_get_state(SMSM_TIME_MASTER_DEM)) & |
| 498 | MASTER_TIME_PENDING)) { |
| 499 | if (time_expired(data)) { |
| 500 | printk(KERN_EMERG "get_smem_clock: timeout 2 still " |
| 501 | "invalid state %x\n", state); |
| 502 | msm_timer_sync_timeout(); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | smsm_change_state(SMSM_APPS_DEM, SLAVE_TIME_REQUEST, SLAVE_TIME_POLL); |
| 507 | |
| 508 | time_start(data); |
| 509 | do { |
| 510 | smem_clock_val = *smem_clock; |
| 511 | } while (smem_clock_val == 0 && !time_expired(data)); |
| 512 | |
| 513 | state = smsm_get_state(SMSM_TIME_MASTER_DEM); |
| 514 | |
| 515 | if (smem_clock_val) { |
| 516 | if (update != NULL) |
Jeff Ohlstein | c83811b | 2011-10-21 14:24:04 -0700 | [diff] [blame] | 517 | update(data, smem_clock_val, sclk_hz); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 518 | |
| 519 | if (msm_timer_debug_mask & MSM_TIMER_DEBUG_SYNC) |
| 520 | printk(KERN_INFO |
| 521 | "get_smem_clock: state %x clock %u\n", |
| 522 | state, smem_clock_val); |
| 523 | } else { |
| 524 | printk(KERN_EMERG |
| 525 | "get_smem_clock: timeout state %x clock %u\n", |
| 526 | state, smem_clock_val); |
| 527 | msm_timer_sync_timeout(); |
| 528 | } |
| 529 | |
| 530 | smsm_change_state(SMSM_APPS_DEM, SLAVE_TIME_REQUEST | SLAVE_TIME_POLL, |
| 531 | SLAVE_TIME_INIT); |
| 532 | return smem_clock_val; |
| 533 | } |
| 534 | #else /* CONFIG_MSM_N_WAY_SMSM */ |
| 535 | static uint32_t msm_timer_do_sync_to_sclk( |
| 536 | void (*time_start)(struct msm_timer_sync_data_t *data), |
| 537 | bool (*time_expired)(struct msm_timer_sync_data_t *data), |
| 538 | void (*update)(struct msm_timer_sync_data_t *, uint32_t, uint32_t), |
| 539 | struct msm_timer_sync_data_t *data) |
| 540 | { |
| 541 | uint32_t *smem_clock; |
| 542 | uint32_t smem_clock_val; |
| 543 | uint32_t last_state; |
| 544 | uint32_t state; |
| 545 | |
| 546 | smem_clock = smem_alloc(SMEM_SMEM_SLOW_CLOCK_VALUE, |
| 547 | sizeof(uint32_t)); |
| 548 | |
| 549 | if (smem_clock == NULL) { |
| 550 | printk(KERN_ERR "no smem clock\n"); |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | last_state = state = smsm_get_state(SMSM_MODEM_STATE); |
| 555 | smem_clock_val = *smem_clock; |
| 556 | if (smem_clock_val) { |
| 557 | printk(KERN_INFO "get_smem_clock: invalid start state %x " |
| 558 | "clock %u\n", state, smem_clock_val); |
| 559 | smsm_change_state(SMSM_APPS_STATE, |
| 560 | SMSM_TIMEWAIT, SMSM_TIMEINIT); |
| 561 | |
| 562 | time_start(data); |
| 563 | while (*smem_clock != 0 && !time_expired(data)) |
| 564 | ; |
| 565 | |
| 566 | smem_clock_val = *smem_clock; |
| 567 | if (smem_clock_val) { |
| 568 | printk(KERN_EMERG "get_smem_clock: timeout still " |
| 569 | "invalid state %x clock %u\n", |
| 570 | state, smem_clock_val); |
| 571 | msm_timer_sync_timeout(); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | time_start(data); |
| 576 | smsm_change_state(SMSM_APPS_STATE, SMSM_TIMEINIT, SMSM_TIMEWAIT); |
| 577 | do { |
| 578 | smem_clock_val = *smem_clock; |
| 579 | state = smsm_get_state(SMSM_MODEM_STATE); |
| 580 | if (state != last_state) { |
| 581 | last_state = state; |
| 582 | if (msm_timer_debug_mask & MSM_TIMER_DEBUG_SYNC) |
| 583 | printk(KERN_INFO |
| 584 | "get_smem_clock: state %x clock %u\n", |
| 585 | state, smem_clock_val); |
| 586 | } |
| 587 | } while (smem_clock_val == 0 && !time_expired(data)); |
| 588 | |
| 589 | if (smem_clock_val) { |
| 590 | if (update != NULL) |
Jeff Ohlstein | c83811b | 2011-10-21 14:24:04 -0700 | [diff] [blame] | 591 | update(data, smem_clock_val, sclk_hz); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 592 | } else { |
| 593 | printk(KERN_EMERG |
| 594 | "get_smem_clock: timeout state %x clock %u\n", |
| 595 | state, smem_clock_val); |
| 596 | msm_timer_sync_timeout(); |
| 597 | } |
| 598 | |
| 599 | smsm_change_state(SMSM_APPS_STATE, SMSM_TIMEWAIT, SMSM_TIMEINIT); |
| 600 | return smem_clock_val; |
| 601 | } |
| 602 | #endif /* CONFIG_MSM_N_WAY_SMSM */ |
| 603 | |
| 604 | /* |
| 605 | * Callback function that initializes the timeout value. |
| 606 | */ |
| 607 | static void msm_timer_sync_to_sclk_time_start( |
| 608 | struct msm_timer_sync_data_t *data) |
| 609 | { |
| 610 | /* approx 2 seconds */ |
| 611 | uint32_t delta = data->clock->freq << data->clock->shift << 1; |
| 612 | data->timeout = msm_read_timer_count(data->clock, LOCAL_TIMER) + delta; |
| 613 | } |
| 614 | |
| 615 | /* |
| 616 | * Callback function that checks the timeout. |
| 617 | */ |
| 618 | static bool msm_timer_sync_to_sclk_time_expired( |
| 619 | struct msm_timer_sync_data_t *data) |
| 620 | { |
| 621 | uint32_t delta = msm_read_timer_count(data->clock, LOCAL_TIMER) - |
| 622 | data->timeout; |
| 623 | return ((int32_t) delta) > 0; |
| 624 | } |
| 625 | |
| 626 | /* |
| 627 | * Callback function that updates local clock from the specified source clock |
| 628 | * value and frequency. |
| 629 | */ |
| 630 | static void msm_timer_sync_update(struct msm_timer_sync_data_t *data, |
| 631 | uint32_t src_clk_val, uint32_t src_clk_freq) |
| 632 | { |
| 633 | struct msm_clock *dst_clk = data->clock; |
| 634 | struct msm_clock_percpu_data *dst_clk_state = |
| 635 | &__get_cpu_var(msm_clocks_percpu)[dst_clk->index]; |
| 636 | uint32_t dst_clk_val = msm_read_timer_count(dst_clk, LOCAL_TIMER); |
| 637 | uint32_t new_offset; |
| 638 | |
| 639 | if ((dst_clk->freq << dst_clk->shift) == src_clk_freq) { |
| 640 | new_offset = src_clk_val - dst_clk_val; |
| 641 | } else { |
| 642 | uint64_t temp; |
| 643 | |
| 644 | /* separate multiplication and division steps to reduce |
| 645 | rounding error */ |
| 646 | temp = src_clk_val; |
| 647 | temp *= dst_clk->freq << dst_clk->shift; |
| 648 | do_div(temp, src_clk_freq); |
| 649 | |
| 650 | new_offset = (uint32_t)(temp) - dst_clk_val; |
| 651 | } |
| 652 | |
| 653 | if (dst_clk_state->sleep_offset + dst_clk_state->non_sleep_offset != |
| 654 | new_offset) { |
| 655 | if (data->exit_sleep) |
| 656 | dst_clk_state->sleep_offset = |
| 657 | new_offset - dst_clk_state->non_sleep_offset; |
| 658 | else |
| 659 | dst_clk_state->non_sleep_offset = |
| 660 | new_offset - dst_clk_state->sleep_offset; |
| 661 | |
| 662 | if (msm_timer_debug_mask & MSM_TIMER_DEBUG_SYNC) |
| 663 | printk(KERN_INFO "sync clock %s: " |
| 664 | "src %u, new offset %u + %u\n", |
| 665 | dst_clk->clocksource.name, src_clk_val, |
| 666 | dst_clk_state->sleep_offset, |
| 667 | dst_clk_state->non_sleep_offset); |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | /* |
| 672 | * Synchronize GPT clock with sclk. |
| 673 | */ |
| 674 | static void msm_timer_sync_gpt_to_sclk(int exit_sleep) |
| 675 | { |
| 676 | struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT]; |
| 677 | struct msm_clock_percpu_data *gpt_clk_state = |
| 678 | &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT]; |
| 679 | struct msm_timer_sync_data_t data; |
| 680 | uint32_t ret; |
| 681 | |
| 682 | if (gpt_clk_state->in_sync) |
| 683 | return; |
| 684 | |
| 685 | data.clock = gpt_clk; |
| 686 | data.timeout = 0; |
| 687 | data.exit_sleep = exit_sleep; |
| 688 | |
| 689 | ret = msm_timer_do_sync_to_sclk( |
| 690 | msm_timer_sync_to_sclk_time_start, |
| 691 | msm_timer_sync_to_sclk_time_expired, |
| 692 | msm_timer_sync_update, |
| 693 | &data); |
| 694 | |
| 695 | if (ret) |
| 696 | gpt_clk_state->in_sync = 1; |
| 697 | } |
| 698 | |
| 699 | /* |
| 700 | * Synchronize clock with GPT clock. |
| 701 | */ |
| 702 | static void msm_timer_sync_to_gpt(struct msm_clock *clock, int exit_sleep) |
| 703 | { |
| 704 | struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT]; |
| 705 | struct msm_clock_percpu_data *gpt_clk_state = |
| 706 | &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT]; |
| 707 | struct msm_clock_percpu_data *clock_state = |
| 708 | &__get_cpu_var(msm_clocks_percpu)[clock->index]; |
| 709 | struct msm_timer_sync_data_t data; |
| 710 | uint32_t gpt_clk_val; |
Jeff Ohlstein | c83811b | 2011-10-21 14:24:04 -0700 | [diff] [blame] | 711 | u64 gpt_period = (1ULL << 32) * HZ; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 712 | u64 now = get_jiffies_64(); |
| 713 | |
Jeff Ohlstein | c83811b | 2011-10-21 14:24:04 -0700 | [diff] [blame] | 714 | do_div(gpt_period, gpt_hz); |
| 715 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 716 | BUG_ON(clock == gpt_clk); |
| 717 | |
| 718 | if (clock_state->in_sync && |
| 719 | (now - clock_state->last_sync_jiffies < (gpt_period >> 1))) |
| 720 | return; |
| 721 | |
| 722 | gpt_clk_val = msm_read_timer_count(gpt_clk, LOCAL_TIMER) |
| 723 | + gpt_clk_state->sleep_offset + gpt_clk_state->non_sleep_offset; |
| 724 | |
| 725 | if (exit_sleep && gpt_clk_val < clock_state->last_sync_gpt) |
| 726 | clock_state->non_sleep_offset -= clock->rollover_offset; |
| 727 | |
| 728 | data.clock = clock; |
| 729 | data.timeout = 0; |
| 730 | data.exit_sleep = exit_sleep; |
| 731 | |
Jeff Ohlstein | c83811b | 2011-10-21 14:24:04 -0700 | [diff] [blame] | 732 | msm_timer_sync_update(&data, gpt_clk_val, gpt_hz); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 733 | |
| 734 | clock_state->in_sync = 1; |
| 735 | clock_state->last_sync_gpt = gpt_clk_val; |
| 736 | clock_state->last_sync_jiffies = now; |
| 737 | } |
| 738 | |
| 739 | static void msm_timer_reactivate_alarm(struct msm_clock *clock) |
| 740 | { |
| 741 | struct msm_clock_percpu_data *clock_state = |
| 742 | &__get_cpu_var(msm_clocks_percpu)[clock->index]; |
| 743 | long alarm_delta = clock_state->alarm_vtime - |
| 744 | clock_state->sleep_offset - |
| 745 | msm_read_timer_count(clock, LOCAL_TIMER); |
| 746 | alarm_delta >>= clock->shift; |
| 747 | if (alarm_delta < (long)clock->write_delay + 4) |
| 748 | alarm_delta = clock->write_delay + 4; |
| 749 | while (msm_timer_set_next_event(alarm_delta, &clock->clockevent)) |
| 750 | ; |
| 751 | } |
| 752 | |
| 753 | int64_t msm_timer_enter_idle(void) |
| 754 | { |
| 755 | struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT]; |
| 756 | struct msm_clock *clock = __get_cpu_var(msm_active_clock); |
| 757 | struct msm_clock_percpu_data *clock_state = |
| 758 | &__get_cpu_var(msm_clocks_percpu)[clock->index]; |
| 759 | uint32_t alarm; |
| 760 | uint32_t count; |
| 761 | int32_t delta; |
| 762 | |
| 763 | BUG_ON(clock != &msm_clocks[MSM_CLOCK_GPT] && |
| 764 | clock != &msm_clocks[MSM_CLOCK_DGT]); |
| 765 | |
| 766 | msm_timer_sync_gpt_to_sclk(0); |
| 767 | if (clock != gpt_clk) |
| 768 | msm_timer_sync_to_gpt(clock, 0); |
| 769 | |
| 770 | count = msm_read_timer_count(clock, LOCAL_TIMER); |
| 771 | if (clock_state->stopped++ == 0) |
| 772 | clock_state->stopped_tick = count + clock_state->sleep_offset; |
| 773 | alarm = clock_state->alarm; |
| 774 | delta = alarm - count; |
| 775 | if (delta <= -(int32_t)((clock->freq << clock->shift) >> 10)) { |
| 776 | /* timer should have triggered 1ms ago */ |
| 777 | printk(KERN_ERR "msm_timer_enter_idle: timer late %d, " |
| 778 | "reprogram it\n", delta); |
| 779 | msm_timer_reactivate_alarm(clock); |
| 780 | } |
| 781 | if (delta <= 0) |
| 782 | return 0; |
| 783 | return clocksource_cyc2ns((alarm - count) >> clock->shift, |
| 784 | clock->clocksource.mult, |
| 785 | clock->clocksource.shift); |
| 786 | } |
| 787 | |
| 788 | void msm_timer_exit_idle(int low_power) |
| 789 | { |
| 790 | struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT]; |
| 791 | struct msm_clock *clock = __get_cpu_var(msm_active_clock); |
| 792 | struct msm_clock_percpu_data *gpt_clk_state = |
| 793 | &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT]; |
| 794 | struct msm_clock_percpu_data *clock_state = |
| 795 | &__get_cpu_var(msm_clocks_percpu)[clock->index]; |
| 796 | uint32_t enabled; |
| 797 | |
| 798 | BUG_ON(clock != &msm_clocks[MSM_CLOCK_GPT] && |
| 799 | clock != &msm_clocks[MSM_CLOCK_DGT]); |
| 800 | |
| 801 | if (!low_power) |
| 802 | goto exit_idle_exit; |
| 803 | |
| 804 | enabled = __raw_readl(gpt_clk->regbase + TIMER_ENABLE) & |
| 805 | TIMER_ENABLE_EN; |
| 806 | if (!enabled) |
| 807 | __raw_writel(TIMER_ENABLE_EN, gpt_clk->regbase + TIMER_ENABLE); |
| 808 | |
| 809 | #if defined(CONFIG_ARCH_MSM_SCORPION) || defined(CONFIG_ARCH_MSM_KRAIT) |
| 810 | gpt_clk_state->in_sync = 0; |
| 811 | #else |
| 812 | gpt_clk_state->in_sync = gpt_clk_state->in_sync && enabled; |
| 813 | #endif |
| 814 | /* Make sure timer is actually enabled before we sync it */ |
| 815 | wmb(); |
| 816 | msm_timer_sync_gpt_to_sclk(1); |
| 817 | |
| 818 | if (clock == gpt_clk) |
| 819 | goto exit_idle_alarm; |
| 820 | |
| 821 | enabled = __raw_readl(clock->regbase + TIMER_ENABLE) & TIMER_ENABLE_EN; |
| 822 | if (!enabled) |
| 823 | __raw_writel(TIMER_ENABLE_EN, clock->regbase + TIMER_ENABLE); |
| 824 | |
| 825 | #if defined(CONFIG_ARCH_MSM_SCORPION) || defined(CONFIG_ARCH_MSM_KRAIT) |
| 826 | clock_state->in_sync = 0; |
| 827 | #else |
| 828 | clock_state->in_sync = clock_state->in_sync && enabled; |
| 829 | #endif |
| 830 | /* Make sure timer is actually enabled before we sync it */ |
| 831 | wmb(); |
| 832 | msm_timer_sync_to_gpt(clock, 1); |
| 833 | |
| 834 | exit_idle_alarm: |
| 835 | msm_timer_reactivate_alarm(clock); |
| 836 | |
| 837 | exit_idle_exit: |
| 838 | clock_state->stopped--; |
| 839 | } |
| 840 | |
| 841 | /* |
| 842 | * Callback function that initializes the timeout value. |
| 843 | */ |
| 844 | static void msm_timer_get_sclk_time_start( |
| 845 | struct msm_timer_sync_data_t *data) |
| 846 | { |
| 847 | data->timeout = 200000; |
| 848 | } |
| 849 | |
| 850 | /* |
| 851 | * Callback function that checks the timeout. |
| 852 | */ |
| 853 | static bool msm_timer_get_sclk_time_expired( |
| 854 | struct msm_timer_sync_data_t *data) |
| 855 | { |
| 856 | udelay(10); |
| 857 | return --data->timeout <= 0; |
| 858 | } |
| 859 | |
| 860 | /* |
| 861 | * Retrieve the cycle count from the sclk and convert it into |
| 862 | * nanoseconds. |
| 863 | * |
| 864 | * On exit, if period is not NULL, it contains the period of the |
| 865 | * sclk in nanoseconds, i.e. how long the cycle count wraps around. |
| 866 | * |
| 867 | * Return value: |
| 868 | * 0: the operation failed; period is not set either |
| 869 | * >0: time in nanoseconds |
| 870 | */ |
| 871 | int64_t msm_timer_get_sclk_time(int64_t *period) |
| 872 | { |
| 873 | struct msm_timer_sync_data_t data; |
| 874 | uint32_t clock_value; |
| 875 | int64_t tmp; |
| 876 | |
| 877 | memset(&data, 0, sizeof(data)); |
| 878 | clock_value = msm_timer_do_sync_to_sclk( |
| 879 | msm_timer_get_sclk_time_start, |
| 880 | msm_timer_get_sclk_time_expired, |
| 881 | NULL, |
| 882 | &data); |
| 883 | |
| 884 | if (!clock_value) |
| 885 | return 0; |
| 886 | |
| 887 | if (period) { |
| 888 | tmp = 1LL << 32; |
Jeff Ohlstein | c83811b | 2011-10-21 14:24:04 -0700 | [diff] [blame] | 889 | tmp *= NSEC_PER_SEC; |
| 890 | do_div(tmp, sclk_hz); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 891 | *period = tmp; |
| 892 | } |
| 893 | |
| 894 | tmp = (int64_t)clock_value; |
Jeff Ohlstein | c83811b | 2011-10-21 14:24:04 -0700 | [diff] [blame] | 895 | tmp *= NSEC_PER_SEC; |
| 896 | do_div(tmp, sclk_hz); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 897 | return tmp; |
| 898 | } |
| 899 | |
| 900 | int __init msm_timer_init_time_sync(void (*timeout)(void)) |
| 901 | { |
| 902 | #if defined(CONFIG_MSM_N_WAY_SMSM) && !defined(CONFIG_MSM_DIRECT_SCLK_ACCESS) |
| 903 | int ret = smsm_change_intr_mask(SMSM_TIME_MASTER_DEM, 0xFFFFFFFF, 0); |
| 904 | |
| 905 | if (ret) { |
| 906 | printk(KERN_ERR "%s: failed to clear interrupt mask, %d\n", |
| 907 | __func__, ret); |
| 908 | return ret; |
| 909 | } |
| 910 | |
| 911 | smsm_change_state(SMSM_APPS_DEM, |
| 912 | SLAVE_TIME_REQUEST | SLAVE_TIME_POLL, SLAVE_TIME_INIT); |
| 913 | #endif |
| 914 | |
| 915 | BUG_ON(timeout == NULL); |
| 916 | msm_timer_sync_timeout = timeout; |
| 917 | |
| 918 | return 0; |
| 919 | } |
| 920 | |
| 921 | #endif |
| 922 | |
Jeff Ohlstein | 4e93ae1 | 2011-09-26 18:22:26 -0700 | [diff] [blame] | 923 | static DEFINE_CLOCK_DATA(cd); |
| 924 | |
Vikram Mulukutla | a41f3a1 | 2011-10-31 14:20:50 -0700 | [diff] [blame] | 925 | /* |
| 926 | * Store the most recent timestamp read from hardware |
| 927 | * in last_ns. This is useful for debugging crashes. |
| 928 | */ |
Jeff Ohlstein | 06658f7 | 2011-11-09 13:51:11 -0800 | [diff] [blame] | 929 | static atomic64_t last_ns; |
Vikram Mulukutla | a41f3a1 | 2011-10-31 14:20:50 -0700 | [diff] [blame] | 930 | |
Jeff Ohlstein | 4e93ae1 | 2011-09-26 18:22:26 -0700 | [diff] [blame] | 931 | unsigned long long notrace sched_clock(void) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 932 | { |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 933 | struct msm_clock *clock = &msm_clocks[msm_global_timer]; |
Jeff Ohlstein | 4e93ae1 | 2011-09-26 18:22:26 -0700 | [diff] [blame] | 934 | struct clocksource *cs = &clock->clocksource; |
Jeff Ohlstein | 06658f7 | 2011-11-09 13:51:11 -0800 | [diff] [blame] | 935 | u64 cyc = cs->read(cs); |
| 936 | u64 last_ns_local; |
| 937 | last_ns_local = cyc_to_sched_clock(&cd, cyc, ((u32)~0 >> clock->shift)); |
| 938 | atomic64_set(&last_ns, last_ns_local); |
| 939 | return last_ns_local; |
Jeff Ohlstein | 4e93ae1 | 2011-09-26 18:22:26 -0700 | [diff] [blame] | 940 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 941 | |
Jeff Ohlstein | 4e93ae1 | 2011-09-26 18:22:26 -0700 | [diff] [blame] | 942 | static void notrace msm_update_sched_clock(void) |
| 943 | { |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 944 | struct msm_clock *clock = &msm_clocks[msm_global_timer]; |
Jeff Ohlstein | 4e93ae1 | 2011-09-26 18:22:26 -0700 | [diff] [blame] | 945 | struct clocksource *cs = &clock->clocksource; |
| 946 | u32 cyc = cs->read(cs); |
| 947 | update_sched_clock(&cd, cyc, ((u32)~0) >> clock->shift); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 948 | } |
| 949 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 950 | int read_current_timer(unsigned long *timer_val) |
| 951 | { |
| 952 | struct msm_clock *dgt = &msm_clocks[MSM_CLOCK_DGT]; |
| 953 | *timer_val = msm_read_timer_count(dgt, GLOBAL_TIMER); |
| 954 | return 0; |
| 955 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 956 | |
Jeff Ohlstein | 4e93ae1 | 2011-09-26 18:22:26 -0700 | [diff] [blame] | 957 | static void __init msm_sched_clock_init(void) |
| 958 | { |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 959 | struct msm_clock *clock = &msm_clocks[msm_global_timer]; |
Jeff Ohlstein | 4e93ae1 | 2011-09-26 18:22:26 -0700 | [diff] [blame] | 960 | |
| 961 | init_sched_clock(&cd, msm_update_sched_clock, 32 - clock->shift, |
| 962 | clock->freq); |
| 963 | } |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 964 | static void __init msm_timer_init(void) |
| 965 | { |
| 966 | int i; |
| 967 | int res; |
Jin Hong | eecb1e0 | 2011-10-21 14:36:32 -0700 | [diff] [blame] | 968 | struct irq_chip *chip; |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 969 | struct msm_clock *dgt = &msm_clocks[MSM_CLOCK_DGT]; |
| 970 | struct msm_clock *gpt = &msm_clocks[MSM_CLOCK_GPT]; |
David Brown | 8c27e6f | 2011-01-07 10:20:49 -0800 | [diff] [blame] | 971 | |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 972 | if (cpu_is_msm7x01() || cpu_is_msm7x25() || cpu_is_msm7x27() || |
| 973 | cpu_is_msm7x25a() || cpu_is_msm7x27a() || cpu_is_msm7x25aa() || |
| 974 | cpu_is_msm7x27aa()) { |
| 975 | dgt->shift = MSM_DGT_SHIFT; |
| 976 | dgt->freq = 19200000 >> MSM_DGT_SHIFT; |
| 977 | dgt->clockevent.shift = 32 + MSM_DGT_SHIFT; |
| 978 | dgt->clocksource.mask = CLOCKSOURCE_MASK(32 - MSM_DGT_SHIFT); |
| 979 | dgt->clocksource.shift = 24 - MSM_DGT_SHIFT; |
| 980 | gpt->regbase = MSM_TMR_BASE; |
| 981 | dgt->regbase = MSM_TMR_BASE + 0x10; |
Jeff Ohlstein | 5d90e25 | 2011-11-04 19:00:50 -0700 | [diff] [blame] | 982 | gpt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT |
| 983 | | MSM_CLOCK_FLAGS_ODD_MATCH_WRITE |
| 984 | | MSM_CLOCK_FLAGS_DELAYED_WRITE_POST; |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 985 | } else if (cpu_is_qsd8x50()) { |
| 986 | dgt->freq = 4800000; |
| 987 | gpt->regbase = MSM_TMR_BASE; |
| 988 | dgt->regbase = MSM_TMR_BASE + 0x10; |
| 989 | } else if (cpu_is_fsm9xxx()) |
| 990 | dgt->freq = 4800000; |
| 991 | else if (cpu_is_msm7x30() || cpu_is_msm8x55()) |
| 992 | dgt->freq = 6144000; |
Jeff Ohlstein | c83811b | 2011-10-21 14:24:04 -0700 | [diff] [blame] | 993 | else if (cpu_is_msm8x60()) { |
Jeff Ohlstein | 7e538f0 | 2011-11-01 17:36:22 -0700 | [diff] [blame] | 994 | global_timer_offset = MSM_TMR0_BASE - MSM_TMR_BASE; |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 995 | dgt->freq = 6750000; |
| 996 | __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL); |
Jeff Ohlstein | 7e538f0 | 2011-11-01 17:36:22 -0700 | [diff] [blame] | 997 | } else if (cpu_is_msm9615()) { |
| 998 | dgt->freq = 6750000; |
| 999 | __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL); |
| 1000 | gpt->freq = 32765; |
| 1001 | gpt_hz = 32765; |
| 1002 | sclk_hz = 32765; |
Jeff Ohlstein | d47f96a | 2011-11-04 19:00:50 -0700 | [diff] [blame] | 1003 | gpt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT; |
| 1004 | dgt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT; |
Jeff Ohlstein | 7e538f0 | 2011-11-01 17:36:22 -0700 | [diff] [blame] | 1005 | } else if (cpu_is_msm8960() || cpu_is_apq8064() || cpu_is_msm8930()) { |
| 1006 | global_timer_offset = MSM_TMR0_BASE - MSM_TMR_BASE; |
Jeff Ohlstein | c83811b | 2011-10-21 14:24:04 -0700 | [diff] [blame] | 1007 | dgt->freq = 6750000; |
| 1008 | __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL); |
| 1009 | gpt->freq = 32765; |
| 1010 | gpt_hz = 32765; |
| 1011 | sclk_hz = 32765; |
Jeff Ohlstein | 391a3ee | 2011-12-01 16:44:45 -0800 | [diff] [blame] | 1012 | if (!machine_is_apq8064_rumi3()) { |
| 1013 | gpt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT; |
| 1014 | dgt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT; |
| 1015 | } |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 1016 | } else { |
| 1017 | WARN_ON("Timer running on unknown hardware. Configure this! " |
| 1018 | "Assuming default configuration.\n"); |
| 1019 | dgt->freq = 6750000; |
| 1020 | } |
| 1021 | |
| 1022 | if (msm_clocks[MSM_CLOCK_GPT].clocksource.rating > DG_TIMER_RATING) |
| 1023 | msm_global_timer = MSM_CLOCK_GPT; |
| 1024 | else |
| 1025 | msm_global_timer = MSM_CLOCK_DGT; |
Jeff Ohlstein | 672039f | 2010-10-05 15:23:57 -0700 | [diff] [blame] | 1026 | |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 1027 | for (i = 0; i < ARRAY_SIZE(msm_clocks); i++) { |
| 1028 | struct msm_clock *clock = &msm_clocks[i]; |
| 1029 | struct clock_event_device *ce = &clock->clockevent; |
| 1030 | struct clocksource *cs = &clock->clocksource; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1031 | __raw_writel(0, clock->regbase + TIMER_ENABLE); |
| 1032 | __raw_writel(1, clock->regbase + TIMER_CLEAR); |
| 1033 | __raw_writel(0, clock->regbase + TIMER_COUNT_VAL); |
| 1034 | __raw_writel(~0, clock->regbase + TIMER_MATCH_VAL); |
David Brown | 8c27e6f | 2011-01-07 10:20:49 -0800 | [diff] [blame] | 1035 | |
Jeff Ohlstein | c83811b | 2011-10-21 14:24:04 -0700 | [diff] [blame] | 1036 | if ((clock->freq << clock->shift) == gpt_hz) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1037 | clock->rollover_offset = 0; |
| 1038 | } else { |
| 1039 | uint64_t temp; |
David Brown | 8c27e6f | 2011-01-07 10:20:49 -0800 | [diff] [blame] | 1040 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1041 | temp = clock->freq << clock->shift; |
| 1042 | temp <<= 32; |
Jeff Ohlstein | c83811b | 2011-10-21 14:24:04 -0700 | [diff] [blame] | 1043 | do_div(temp, gpt_hz); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1044 | |
| 1045 | clock->rollover_offset = (uint32_t) temp; |
| 1046 | } |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 1047 | |
| 1048 | ce->mult = div_sc(clock->freq, NSEC_PER_SEC, ce->shift); |
| 1049 | /* allow at least 10 seconds to notice that the timer wrapped */ |
| 1050 | ce->max_delta_ns = |
| 1051 | clockevent_delta2ns(0xf0000000 >> clock->shift, ce); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1052 | /* ticks gets rounded down by one */ |
| 1053 | ce->min_delta_ns = |
| 1054 | clockevent_delta2ns(clock->write_delay + 4, ce); |
Rusty Russell | 320ab2b | 2008-12-13 21:20:26 +1030 | [diff] [blame] | 1055 | ce->cpumask = cpumask_of(0); |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 1056 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1057 | cs->mult = clocksource_hz2mult(clock->freq, cs->shift); |
| 1058 | res = clocksource_register(cs); |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 1059 | if (res) |
| 1060 | printk(KERN_ERR "msm_timer_init: clocksource_register " |
| 1061 | "failed for %s\n", cs->name); |
| 1062 | |
Trilok Soni | eecb28c | 2011-07-20 16:24:14 +0100 | [diff] [blame^] | 1063 | ce->irq = clock->irq; |
| 1064 | if (cpu_is_msm8x60() || cpu_is_msm8960() || cpu_is_apq8064() || |
| 1065 | cpu_is_msm8930() || cpu_is_msm9615()) { |
| 1066 | clock->percpu_evt = alloc_percpu(struct clock_event_device *); |
| 1067 | if (!clock->percpu_evt) { |
| 1068 | pr_err("msm_timer_init: memory allocation " |
| 1069 | "failed for %s\n", ce->name); |
| 1070 | continue; |
| 1071 | } |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 1072 | |
Trilok Soni | eecb28c | 2011-07-20 16:24:14 +0100 | [diff] [blame^] | 1073 | *__this_cpu_ptr(clock->percpu_evt) = ce; |
| 1074 | res = request_percpu_irq(ce->irq, msm_timer_interrupt, |
| 1075 | ce->name, clock->percpu_evt); |
| 1076 | if (!res) |
| 1077 | enable_percpu_irq(ce->irq, 0); |
| 1078 | } else { |
| 1079 | clock->evt = ce; |
| 1080 | res = request_irq(ce->irq, msm_timer_interrupt, |
| 1081 | IRQF_TIMER | IRQF_NOBALANCING | IRQF_TRIGGER_RISING, |
| 1082 | ce->name, &clock->evt); |
| 1083 | } |
| 1084 | |
| 1085 | if (res) |
| 1086 | pr_err("msm_timer_init: request_irq failed for %s\n", |
| 1087 | ce->name); |
| 1088 | |
| 1089 | chip = irq_get_chip(clock->irq); |
Jin Hong | eecb1e0 | 2011-10-21 14:36:32 -0700 | [diff] [blame] | 1090 | if (chip && chip->irq_mask) |
Trilok Soni | eecb28c | 2011-07-20 16:24:14 +0100 | [diff] [blame^] | 1091 | chip->irq_mask(irq_get_irq_data(clock->irq)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1092 | |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 1093 | clockevents_register_device(ce); |
| 1094 | } |
Jeff Ohlstein | 4e93ae1 | 2011-09-26 18:22:26 -0700 | [diff] [blame] | 1095 | msm_sched_clock_init(); |
Taniya Das | 36057be | 2011-10-28 13:02:17 +0530 | [diff] [blame] | 1096 | |
| 1097 | if (is_smp()) { |
| 1098 | __raw_writel(1, |
| 1099 | msm_clocks[MSM_CLOCK_DGT].regbase + TIMER_ENABLE); |
| 1100 | set_delay_fn(read_current_timer_delay_loop); |
| 1101 | } |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 1102 | } |
| 1103 | |
Jeff Ohlstein | 94790ec | 2010-12-02 12:05:12 -0800 | [diff] [blame] | 1104 | #ifdef CONFIG_SMP |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1105 | |
Santosh Shilimkar | af90f10 | 2011-02-23 18:53:15 +0100 | [diff] [blame] | 1106 | int __cpuinit local_timer_setup(struct clock_event_device *evt) |
Jeff Ohlstein | 94790ec | 2010-12-02 12:05:12 -0800 | [diff] [blame] | 1107 | { |
Jeff Ohlstein | d17ee76 | 2011-09-26 19:44:22 -0700 | [diff] [blame] | 1108 | static DEFINE_PER_CPU(bool, first_boot) = true; |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 1109 | struct msm_clock *clock = &msm_clocks[msm_global_timer]; |
Jeff Ohlstein | 94790ec | 2010-12-02 12:05:12 -0800 | [diff] [blame] | 1110 | |
| 1111 | /* Use existing clock_event for cpu 0 */ |
| 1112 | if (!smp_processor_id()) |
David Brown | 893b66c | 2011-03-30 11:26:57 -0700 | [diff] [blame] | 1113 | return 0; |
Jeff Ohlstein | 94790ec | 2010-12-02 12:05:12 -0800 | [diff] [blame] | 1114 | |
Taniya Das | 36057be | 2011-10-28 13:02:17 +0530 | [diff] [blame] | 1115 | if (cpu_is_msm8x60() || cpu_is_msm8960() || cpu_is_apq8064() |
| 1116 | || cpu_is_msm8930()) |
| 1117 | __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL); |
Jeff Ohlstein | 94790ec | 2010-12-02 12:05:12 -0800 | [diff] [blame] | 1118 | |
Jeff Ohlstein | d17ee76 | 2011-09-26 19:44:22 -0700 | [diff] [blame] | 1119 | if (__get_cpu_var(first_boot)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1120 | __raw_writel(0, clock->regbase + TIMER_ENABLE); |
| 1121 | __raw_writel(0, clock->regbase + TIMER_CLEAR); |
| 1122 | __raw_writel(~0, clock->regbase + TIMER_MATCH_VAL); |
Jeff Ohlstein | d17ee76 | 2011-09-26 19:44:22 -0700 | [diff] [blame] | 1123 | __get_cpu_var(first_boot) = false; |
Jeff Ohlstein | 94790ec | 2010-12-02 12:05:12 -0800 | [diff] [blame] | 1124 | } |
Trilok Soni | eecb28c | 2011-07-20 16:24:14 +0100 | [diff] [blame^] | 1125 | evt->irq = clock->irq; |
Jeff Ohlstein | 94790ec | 2010-12-02 12:05:12 -0800 | [diff] [blame] | 1126 | evt->name = "local_timer"; |
| 1127 | evt->features = CLOCK_EVT_FEAT_ONESHOT; |
| 1128 | evt->rating = clock->clockevent.rating; |
| 1129 | evt->set_mode = msm_timer_set_mode; |
| 1130 | evt->set_next_event = msm_timer_set_next_event; |
| 1131 | evt->shift = clock->clockevent.shift; |
| 1132 | evt->mult = div_sc(clock->freq, NSEC_PER_SEC, evt->shift); |
| 1133 | evt->max_delta_ns = |
| 1134 | clockevent_delta2ns(0xf0000000 >> clock->shift, evt); |
| 1135 | evt->min_delta_ns = clockevent_delta2ns(4, evt); |
| 1136 | |
Trilok Soni | eecb28c | 2011-07-20 16:24:14 +0100 | [diff] [blame^] | 1137 | *__this_cpu_ptr(clock->percpu_evt) = evt; |
Jeff Ohlstein | 94790ec | 2010-12-02 12:05:12 -0800 | [diff] [blame] | 1138 | |
| 1139 | clockevents_register_device(evt); |
Trilok Soni | eecb28c | 2011-07-20 16:24:14 +0100 | [diff] [blame^] | 1140 | enable_percpu_irq(evt->irq, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1141 | |
Santosh Shilimkar | af90f10 | 2011-02-23 18:53:15 +0100 | [diff] [blame] | 1142 | return 0; |
Jeff Ohlstein | 94790ec | 2010-12-02 12:05:12 -0800 | [diff] [blame] | 1143 | } |
| 1144 | |
Trilok Soni | eecb28c | 2011-07-20 16:24:14 +0100 | [diff] [blame^] | 1145 | void local_timer_stop(struct clock_event_device *evt) |
Jeff Ohlstein | 94790ec | 2010-12-02 12:05:12 -0800 | [diff] [blame] | 1146 | { |
Trilok Soni | eecb28c | 2011-07-20 16:24:14 +0100 | [diff] [blame^] | 1147 | evt->set_mode(CLOCK_EVT_MODE_UNUSED, evt); |
| 1148 | disable_percpu_irq(evt->irq); |
Jeff Ohlstein | 94790ec | 2010-12-02 12:05:12 -0800 | [diff] [blame] | 1149 | } |
Jeff Ohlstein | 94790ec | 2010-12-02 12:05:12 -0800 | [diff] [blame] | 1150 | #endif |
| 1151 | |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 1152 | struct sys_timer msm_timer = { |
| 1153 | .init = msm_timer_init |
| 1154 | }; |