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