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. |
Jeff Ohlstein | f0a31e4 | 2012-01-06 19:03:05 -0800 | [diff] [blame] | 4 | * Copyright (c) 2009-2012, 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) |
Jeff Ohlstein | ecefdc0 | 2012-01-13 12:37:44 -0800 | [diff] [blame^] | 408 | uint32_t msm_timer_get_sclk_ticks(void) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 409 | { |
| 410 | uint32_t t1, t2; |
| 411 | int loop_count = 10; |
| 412 | int loop_zero_count = 3; |
Jeff Ohlstein | c83811b | 2011-10-21 14:24:04 -0700 | [diff] [blame] | 413 | int tmp = USEC_PER_SEC; |
| 414 | do_div(tmp, sclk_hz); |
| 415 | tmp /= (loop_zero_count-1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 416 | |
| 417 | while (loop_zero_count--) { |
| 418 | t1 = __raw_readl(MSM_RPM_MPM_BASE + MPM_SCLK_COUNT_VAL); |
| 419 | do { |
| 420 | udelay(1); |
| 421 | t2 = t1; |
| 422 | t1 = __raw_readl(MSM_RPM_MPM_BASE + MPM_SCLK_COUNT_VAL); |
| 423 | } while ((t2 != t1) && --loop_count); |
| 424 | |
| 425 | if (!loop_count) { |
| 426 | printk(KERN_EMERG "SCLK did not stabilize\n"); |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | if (t1) |
| 431 | break; |
| 432 | |
| 433 | udelay(tmp); |
| 434 | } |
| 435 | |
| 436 | if (!loop_zero_count) { |
| 437 | printk(KERN_EMERG "SCLK reads zero\n"); |
| 438 | return 0; |
| 439 | } |
| 440 | |
Jeff Ohlstein | ecefdc0 | 2012-01-13 12:37:44 -0800 | [diff] [blame^] | 441 | return t1; |
| 442 | } |
| 443 | |
| 444 | static uint32_t msm_timer_do_sync_to_sclk( |
| 445 | void (*time_start)(struct msm_timer_sync_data_t *data), |
| 446 | bool (*time_expired)(struct msm_timer_sync_data_t *data), |
| 447 | void (*update)(struct msm_timer_sync_data_t *, uint32_t, uint32_t), |
| 448 | struct msm_timer_sync_data_t *data) |
| 449 | { |
| 450 | unsigned t1 = msm_timer_get_sclk_ticks(); |
| 451 | |
| 452 | if (t1 && update != NULL) |
Jeff Ohlstein | c83811b | 2011-10-21 14:24:04 -0700 | [diff] [blame] | 453 | update(data, t1, sclk_hz); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 454 | return t1; |
| 455 | } |
| 456 | #elif defined(CONFIG_MSM_N_WAY_SMSM) |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 457 | |
| 458 | /* Time Master State Bits */ |
| 459 | #define MASTER_BITS_PER_CPU 1 |
| 460 | #define MASTER_TIME_PENDING \ |
| 461 | (0x01UL << (MASTER_BITS_PER_CPU * SMSM_APPS_STATE)) |
| 462 | |
| 463 | /* Time Slave State Bits */ |
| 464 | #define SLAVE_TIME_REQUEST 0x0400 |
| 465 | #define SLAVE_TIME_POLL 0x0800 |
| 466 | #define SLAVE_TIME_INIT 0x1000 |
| 467 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 468 | static uint32_t msm_timer_do_sync_to_sclk( |
| 469 | void (*time_start)(struct msm_timer_sync_data_t *data), |
| 470 | bool (*time_expired)(struct msm_timer_sync_data_t *data), |
| 471 | void (*update)(struct msm_timer_sync_data_t *, uint32_t, uint32_t), |
| 472 | struct msm_timer_sync_data_t *data) |
| 473 | { |
| 474 | uint32_t *smem_clock; |
| 475 | uint32_t smem_clock_val; |
| 476 | uint32_t state; |
| 477 | |
| 478 | smem_clock = smem_alloc(SMEM_SMEM_SLOW_CLOCK_VALUE, sizeof(uint32_t)); |
| 479 | if (smem_clock == NULL) { |
| 480 | printk(KERN_ERR "no smem clock\n"); |
| 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | state = smsm_get_state(SMSM_MODEM_STATE); |
| 485 | if ((state & SMSM_INIT) == 0) { |
| 486 | printk(KERN_ERR "smsm not initialized\n"); |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | time_start(data); |
| 491 | while ((state = smsm_get_state(SMSM_TIME_MASTER_DEM)) & |
| 492 | MASTER_TIME_PENDING) { |
| 493 | if (time_expired(data)) { |
| 494 | printk(KERN_EMERG "get_smem_clock: timeout 1 still " |
| 495 | "invalid state %x\n", state); |
| 496 | msm_timer_sync_timeout(); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | smsm_change_state(SMSM_APPS_DEM, SLAVE_TIME_POLL | SLAVE_TIME_INIT, |
| 501 | SLAVE_TIME_REQUEST); |
| 502 | |
| 503 | time_start(data); |
| 504 | while (!((state = smsm_get_state(SMSM_TIME_MASTER_DEM)) & |
| 505 | MASTER_TIME_PENDING)) { |
| 506 | if (time_expired(data)) { |
| 507 | printk(KERN_EMERG "get_smem_clock: timeout 2 still " |
| 508 | "invalid state %x\n", state); |
| 509 | msm_timer_sync_timeout(); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | smsm_change_state(SMSM_APPS_DEM, SLAVE_TIME_REQUEST, SLAVE_TIME_POLL); |
| 514 | |
| 515 | time_start(data); |
| 516 | do { |
| 517 | smem_clock_val = *smem_clock; |
| 518 | } while (smem_clock_val == 0 && !time_expired(data)); |
| 519 | |
| 520 | state = smsm_get_state(SMSM_TIME_MASTER_DEM); |
| 521 | |
| 522 | if (smem_clock_val) { |
| 523 | if (update != NULL) |
Jeff Ohlstein | c83811b | 2011-10-21 14:24:04 -0700 | [diff] [blame] | 524 | update(data, smem_clock_val, sclk_hz); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 525 | |
| 526 | if (msm_timer_debug_mask & MSM_TIMER_DEBUG_SYNC) |
| 527 | printk(KERN_INFO |
| 528 | "get_smem_clock: state %x clock %u\n", |
| 529 | state, smem_clock_val); |
| 530 | } else { |
| 531 | printk(KERN_EMERG |
| 532 | "get_smem_clock: timeout state %x clock %u\n", |
| 533 | state, smem_clock_val); |
| 534 | msm_timer_sync_timeout(); |
| 535 | } |
| 536 | |
| 537 | smsm_change_state(SMSM_APPS_DEM, SLAVE_TIME_REQUEST | SLAVE_TIME_POLL, |
| 538 | SLAVE_TIME_INIT); |
| 539 | return smem_clock_val; |
| 540 | } |
| 541 | #else /* CONFIG_MSM_N_WAY_SMSM */ |
| 542 | static uint32_t msm_timer_do_sync_to_sclk( |
| 543 | void (*time_start)(struct msm_timer_sync_data_t *data), |
| 544 | bool (*time_expired)(struct msm_timer_sync_data_t *data), |
| 545 | void (*update)(struct msm_timer_sync_data_t *, uint32_t, uint32_t), |
| 546 | struct msm_timer_sync_data_t *data) |
| 547 | { |
| 548 | uint32_t *smem_clock; |
| 549 | uint32_t smem_clock_val; |
| 550 | uint32_t last_state; |
| 551 | uint32_t state; |
| 552 | |
| 553 | smem_clock = smem_alloc(SMEM_SMEM_SLOW_CLOCK_VALUE, |
| 554 | sizeof(uint32_t)); |
| 555 | |
| 556 | if (smem_clock == NULL) { |
| 557 | printk(KERN_ERR "no smem clock\n"); |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | last_state = state = smsm_get_state(SMSM_MODEM_STATE); |
| 562 | smem_clock_val = *smem_clock; |
| 563 | if (smem_clock_val) { |
| 564 | printk(KERN_INFO "get_smem_clock: invalid start state %x " |
| 565 | "clock %u\n", state, smem_clock_val); |
| 566 | smsm_change_state(SMSM_APPS_STATE, |
| 567 | SMSM_TIMEWAIT, SMSM_TIMEINIT); |
| 568 | |
| 569 | time_start(data); |
| 570 | while (*smem_clock != 0 && !time_expired(data)) |
| 571 | ; |
| 572 | |
| 573 | smem_clock_val = *smem_clock; |
| 574 | if (smem_clock_val) { |
| 575 | printk(KERN_EMERG "get_smem_clock: timeout still " |
| 576 | "invalid state %x clock %u\n", |
| 577 | state, smem_clock_val); |
| 578 | msm_timer_sync_timeout(); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | time_start(data); |
| 583 | smsm_change_state(SMSM_APPS_STATE, SMSM_TIMEINIT, SMSM_TIMEWAIT); |
| 584 | do { |
| 585 | smem_clock_val = *smem_clock; |
| 586 | state = smsm_get_state(SMSM_MODEM_STATE); |
| 587 | if (state != last_state) { |
| 588 | last_state = state; |
| 589 | if (msm_timer_debug_mask & MSM_TIMER_DEBUG_SYNC) |
| 590 | printk(KERN_INFO |
| 591 | "get_smem_clock: state %x clock %u\n", |
| 592 | state, smem_clock_val); |
| 593 | } |
| 594 | } while (smem_clock_val == 0 && !time_expired(data)); |
| 595 | |
| 596 | if (smem_clock_val) { |
| 597 | if (update != NULL) |
Jeff Ohlstein | c83811b | 2011-10-21 14:24:04 -0700 | [diff] [blame] | 598 | update(data, smem_clock_val, sclk_hz); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 599 | } else { |
| 600 | printk(KERN_EMERG |
| 601 | "get_smem_clock: timeout state %x clock %u\n", |
| 602 | state, smem_clock_val); |
| 603 | msm_timer_sync_timeout(); |
| 604 | } |
| 605 | |
| 606 | smsm_change_state(SMSM_APPS_STATE, SMSM_TIMEWAIT, SMSM_TIMEINIT); |
| 607 | return smem_clock_val; |
| 608 | } |
| 609 | #endif /* CONFIG_MSM_N_WAY_SMSM */ |
| 610 | |
| 611 | /* |
| 612 | * Callback function that initializes the timeout value. |
| 613 | */ |
| 614 | static void msm_timer_sync_to_sclk_time_start( |
| 615 | struct msm_timer_sync_data_t *data) |
| 616 | { |
| 617 | /* approx 2 seconds */ |
| 618 | uint32_t delta = data->clock->freq << data->clock->shift << 1; |
| 619 | data->timeout = msm_read_timer_count(data->clock, LOCAL_TIMER) + delta; |
| 620 | } |
| 621 | |
| 622 | /* |
| 623 | * Callback function that checks the timeout. |
| 624 | */ |
| 625 | static bool msm_timer_sync_to_sclk_time_expired( |
| 626 | struct msm_timer_sync_data_t *data) |
| 627 | { |
| 628 | uint32_t delta = msm_read_timer_count(data->clock, LOCAL_TIMER) - |
| 629 | data->timeout; |
| 630 | return ((int32_t) delta) > 0; |
| 631 | } |
| 632 | |
| 633 | /* |
| 634 | * Callback function that updates local clock from the specified source clock |
| 635 | * value and frequency. |
| 636 | */ |
| 637 | static void msm_timer_sync_update(struct msm_timer_sync_data_t *data, |
| 638 | uint32_t src_clk_val, uint32_t src_clk_freq) |
| 639 | { |
| 640 | struct msm_clock *dst_clk = data->clock; |
| 641 | struct msm_clock_percpu_data *dst_clk_state = |
| 642 | &__get_cpu_var(msm_clocks_percpu)[dst_clk->index]; |
| 643 | uint32_t dst_clk_val = msm_read_timer_count(dst_clk, LOCAL_TIMER); |
| 644 | uint32_t new_offset; |
| 645 | |
| 646 | if ((dst_clk->freq << dst_clk->shift) == src_clk_freq) { |
| 647 | new_offset = src_clk_val - dst_clk_val; |
| 648 | } else { |
| 649 | uint64_t temp; |
| 650 | |
| 651 | /* separate multiplication and division steps to reduce |
| 652 | rounding error */ |
| 653 | temp = src_clk_val; |
| 654 | temp *= dst_clk->freq << dst_clk->shift; |
| 655 | do_div(temp, src_clk_freq); |
| 656 | |
| 657 | new_offset = (uint32_t)(temp) - dst_clk_val; |
| 658 | } |
| 659 | |
| 660 | if (dst_clk_state->sleep_offset + dst_clk_state->non_sleep_offset != |
| 661 | new_offset) { |
| 662 | if (data->exit_sleep) |
| 663 | dst_clk_state->sleep_offset = |
| 664 | new_offset - dst_clk_state->non_sleep_offset; |
| 665 | else |
| 666 | dst_clk_state->non_sleep_offset = |
| 667 | new_offset - dst_clk_state->sleep_offset; |
| 668 | |
| 669 | if (msm_timer_debug_mask & MSM_TIMER_DEBUG_SYNC) |
| 670 | printk(KERN_INFO "sync clock %s: " |
| 671 | "src %u, new offset %u + %u\n", |
| 672 | dst_clk->clocksource.name, src_clk_val, |
| 673 | dst_clk_state->sleep_offset, |
| 674 | dst_clk_state->non_sleep_offset); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | /* |
| 679 | * Synchronize GPT clock with sclk. |
| 680 | */ |
| 681 | static void msm_timer_sync_gpt_to_sclk(int exit_sleep) |
| 682 | { |
| 683 | struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT]; |
| 684 | struct msm_clock_percpu_data *gpt_clk_state = |
| 685 | &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT]; |
| 686 | struct msm_timer_sync_data_t data; |
| 687 | uint32_t ret; |
| 688 | |
| 689 | if (gpt_clk_state->in_sync) |
| 690 | return; |
| 691 | |
| 692 | data.clock = gpt_clk; |
| 693 | data.timeout = 0; |
| 694 | data.exit_sleep = exit_sleep; |
| 695 | |
| 696 | ret = msm_timer_do_sync_to_sclk( |
| 697 | msm_timer_sync_to_sclk_time_start, |
| 698 | msm_timer_sync_to_sclk_time_expired, |
| 699 | msm_timer_sync_update, |
| 700 | &data); |
| 701 | |
| 702 | if (ret) |
| 703 | gpt_clk_state->in_sync = 1; |
| 704 | } |
| 705 | |
| 706 | /* |
| 707 | * Synchronize clock with GPT clock. |
| 708 | */ |
| 709 | static void msm_timer_sync_to_gpt(struct msm_clock *clock, int exit_sleep) |
| 710 | { |
| 711 | struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT]; |
| 712 | struct msm_clock_percpu_data *gpt_clk_state = |
| 713 | &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT]; |
| 714 | struct msm_clock_percpu_data *clock_state = |
| 715 | &__get_cpu_var(msm_clocks_percpu)[clock->index]; |
| 716 | struct msm_timer_sync_data_t data; |
| 717 | uint32_t gpt_clk_val; |
Jeff Ohlstein | c83811b | 2011-10-21 14:24:04 -0700 | [diff] [blame] | 718 | u64 gpt_period = (1ULL << 32) * HZ; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 719 | u64 now = get_jiffies_64(); |
| 720 | |
Jeff Ohlstein | c83811b | 2011-10-21 14:24:04 -0700 | [diff] [blame] | 721 | do_div(gpt_period, gpt_hz); |
| 722 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 723 | BUG_ON(clock == gpt_clk); |
| 724 | |
| 725 | if (clock_state->in_sync && |
| 726 | (now - clock_state->last_sync_jiffies < (gpt_period >> 1))) |
| 727 | return; |
| 728 | |
| 729 | gpt_clk_val = msm_read_timer_count(gpt_clk, LOCAL_TIMER) |
| 730 | + gpt_clk_state->sleep_offset + gpt_clk_state->non_sleep_offset; |
| 731 | |
| 732 | if (exit_sleep && gpt_clk_val < clock_state->last_sync_gpt) |
| 733 | clock_state->non_sleep_offset -= clock->rollover_offset; |
| 734 | |
| 735 | data.clock = clock; |
| 736 | data.timeout = 0; |
| 737 | data.exit_sleep = exit_sleep; |
| 738 | |
Jeff Ohlstein | c83811b | 2011-10-21 14:24:04 -0700 | [diff] [blame] | 739 | msm_timer_sync_update(&data, gpt_clk_val, gpt_hz); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 740 | |
| 741 | clock_state->in_sync = 1; |
| 742 | clock_state->last_sync_gpt = gpt_clk_val; |
| 743 | clock_state->last_sync_jiffies = now; |
| 744 | } |
| 745 | |
| 746 | static void msm_timer_reactivate_alarm(struct msm_clock *clock) |
| 747 | { |
| 748 | struct msm_clock_percpu_data *clock_state = |
| 749 | &__get_cpu_var(msm_clocks_percpu)[clock->index]; |
| 750 | long alarm_delta = clock_state->alarm_vtime - |
| 751 | clock_state->sleep_offset - |
| 752 | msm_read_timer_count(clock, LOCAL_TIMER); |
| 753 | alarm_delta >>= clock->shift; |
| 754 | if (alarm_delta < (long)clock->write_delay + 4) |
| 755 | alarm_delta = clock->write_delay + 4; |
| 756 | while (msm_timer_set_next_event(alarm_delta, &clock->clockevent)) |
| 757 | ; |
| 758 | } |
| 759 | |
| 760 | int64_t msm_timer_enter_idle(void) |
| 761 | { |
| 762 | struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT]; |
| 763 | struct msm_clock *clock = __get_cpu_var(msm_active_clock); |
| 764 | struct msm_clock_percpu_data *clock_state = |
| 765 | &__get_cpu_var(msm_clocks_percpu)[clock->index]; |
| 766 | uint32_t alarm; |
| 767 | uint32_t count; |
| 768 | int32_t delta; |
| 769 | |
| 770 | BUG_ON(clock != &msm_clocks[MSM_CLOCK_GPT] && |
| 771 | clock != &msm_clocks[MSM_CLOCK_DGT]); |
| 772 | |
| 773 | msm_timer_sync_gpt_to_sclk(0); |
| 774 | if (clock != gpt_clk) |
| 775 | msm_timer_sync_to_gpt(clock, 0); |
| 776 | |
| 777 | count = msm_read_timer_count(clock, LOCAL_TIMER); |
| 778 | if (clock_state->stopped++ == 0) |
| 779 | clock_state->stopped_tick = count + clock_state->sleep_offset; |
| 780 | alarm = clock_state->alarm; |
| 781 | delta = alarm - count; |
| 782 | if (delta <= -(int32_t)((clock->freq << clock->shift) >> 10)) { |
| 783 | /* timer should have triggered 1ms ago */ |
| 784 | printk(KERN_ERR "msm_timer_enter_idle: timer late %d, " |
| 785 | "reprogram it\n", delta); |
| 786 | msm_timer_reactivate_alarm(clock); |
| 787 | } |
| 788 | if (delta <= 0) |
| 789 | return 0; |
| 790 | return clocksource_cyc2ns((alarm - count) >> clock->shift, |
| 791 | clock->clocksource.mult, |
| 792 | clock->clocksource.shift); |
| 793 | } |
| 794 | |
| 795 | void msm_timer_exit_idle(int low_power) |
| 796 | { |
| 797 | struct msm_clock *gpt_clk = &msm_clocks[MSM_CLOCK_GPT]; |
| 798 | struct msm_clock *clock = __get_cpu_var(msm_active_clock); |
| 799 | struct msm_clock_percpu_data *gpt_clk_state = |
| 800 | &__get_cpu_var(msm_clocks_percpu)[MSM_CLOCK_GPT]; |
| 801 | struct msm_clock_percpu_data *clock_state = |
| 802 | &__get_cpu_var(msm_clocks_percpu)[clock->index]; |
| 803 | uint32_t enabled; |
| 804 | |
| 805 | BUG_ON(clock != &msm_clocks[MSM_CLOCK_GPT] && |
| 806 | clock != &msm_clocks[MSM_CLOCK_DGT]); |
| 807 | |
| 808 | if (!low_power) |
| 809 | goto exit_idle_exit; |
| 810 | |
| 811 | enabled = __raw_readl(gpt_clk->regbase + TIMER_ENABLE) & |
| 812 | TIMER_ENABLE_EN; |
| 813 | if (!enabled) |
| 814 | __raw_writel(TIMER_ENABLE_EN, gpt_clk->regbase + TIMER_ENABLE); |
| 815 | |
| 816 | #if defined(CONFIG_ARCH_MSM_SCORPION) || defined(CONFIG_ARCH_MSM_KRAIT) |
| 817 | gpt_clk_state->in_sync = 0; |
| 818 | #else |
| 819 | gpt_clk_state->in_sync = gpt_clk_state->in_sync && enabled; |
| 820 | #endif |
| 821 | /* Make sure timer is actually enabled before we sync it */ |
| 822 | wmb(); |
| 823 | msm_timer_sync_gpt_to_sclk(1); |
| 824 | |
| 825 | if (clock == gpt_clk) |
| 826 | goto exit_idle_alarm; |
| 827 | |
| 828 | enabled = __raw_readl(clock->regbase + TIMER_ENABLE) & TIMER_ENABLE_EN; |
| 829 | if (!enabled) |
| 830 | __raw_writel(TIMER_ENABLE_EN, clock->regbase + TIMER_ENABLE); |
| 831 | |
| 832 | #if defined(CONFIG_ARCH_MSM_SCORPION) || defined(CONFIG_ARCH_MSM_KRAIT) |
| 833 | clock_state->in_sync = 0; |
| 834 | #else |
| 835 | clock_state->in_sync = clock_state->in_sync && enabled; |
| 836 | #endif |
| 837 | /* Make sure timer is actually enabled before we sync it */ |
| 838 | wmb(); |
| 839 | msm_timer_sync_to_gpt(clock, 1); |
| 840 | |
| 841 | exit_idle_alarm: |
| 842 | msm_timer_reactivate_alarm(clock); |
| 843 | |
| 844 | exit_idle_exit: |
| 845 | clock_state->stopped--; |
| 846 | } |
| 847 | |
| 848 | /* |
| 849 | * Callback function that initializes the timeout value. |
| 850 | */ |
| 851 | static void msm_timer_get_sclk_time_start( |
| 852 | struct msm_timer_sync_data_t *data) |
| 853 | { |
| 854 | data->timeout = 200000; |
| 855 | } |
| 856 | |
| 857 | /* |
| 858 | * Callback function that checks the timeout. |
| 859 | */ |
| 860 | static bool msm_timer_get_sclk_time_expired( |
| 861 | struct msm_timer_sync_data_t *data) |
| 862 | { |
| 863 | udelay(10); |
| 864 | return --data->timeout <= 0; |
| 865 | } |
| 866 | |
| 867 | /* |
| 868 | * Retrieve the cycle count from the sclk and convert it into |
| 869 | * nanoseconds. |
| 870 | * |
| 871 | * On exit, if period is not NULL, it contains the period of the |
| 872 | * sclk in nanoseconds, i.e. how long the cycle count wraps around. |
| 873 | * |
| 874 | * Return value: |
| 875 | * 0: the operation failed; period is not set either |
| 876 | * >0: time in nanoseconds |
| 877 | */ |
| 878 | int64_t msm_timer_get_sclk_time(int64_t *period) |
| 879 | { |
| 880 | struct msm_timer_sync_data_t data; |
| 881 | uint32_t clock_value; |
| 882 | int64_t tmp; |
| 883 | |
| 884 | memset(&data, 0, sizeof(data)); |
| 885 | clock_value = msm_timer_do_sync_to_sclk( |
| 886 | msm_timer_get_sclk_time_start, |
| 887 | msm_timer_get_sclk_time_expired, |
| 888 | NULL, |
| 889 | &data); |
| 890 | |
| 891 | if (!clock_value) |
| 892 | return 0; |
| 893 | |
| 894 | if (period) { |
| 895 | tmp = 1LL << 32; |
Jeff Ohlstein | c83811b | 2011-10-21 14:24:04 -0700 | [diff] [blame] | 896 | tmp *= NSEC_PER_SEC; |
| 897 | do_div(tmp, sclk_hz); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 898 | *period = tmp; |
| 899 | } |
| 900 | |
| 901 | tmp = (int64_t)clock_value; |
Jeff Ohlstein | c83811b | 2011-10-21 14:24:04 -0700 | [diff] [blame] | 902 | tmp *= NSEC_PER_SEC; |
| 903 | do_div(tmp, sclk_hz); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 904 | return tmp; |
| 905 | } |
| 906 | |
| 907 | int __init msm_timer_init_time_sync(void (*timeout)(void)) |
| 908 | { |
| 909 | #if defined(CONFIG_MSM_N_WAY_SMSM) && !defined(CONFIG_MSM_DIRECT_SCLK_ACCESS) |
| 910 | int ret = smsm_change_intr_mask(SMSM_TIME_MASTER_DEM, 0xFFFFFFFF, 0); |
| 911 | |
| 912 | if (ret) { |
| 913 | printk(KERN_ERR "%s: failed to clear interrupt mask, %d\n", |
| 914 | __func__, ret); |
| 915 | return ret; |
| 916 | } |
| 917 | |
| 918 | smsm_change_state(SMSM_APPS_DEM, |
| 919 | SLAVE_TIME_REQUEST | SLAVE_TIME_POLL, SLAVE_TIME_INIT); |
| 920 | #endif |
| 921 | |
| 922 | BUG_ON(timeout == NULL); |
| 923 | msm_timer_sync_timeout = timeout; |
| 924 | |
| 925 | return 0; |
| 926 | } |
| 927 | |
| 928 | #endif |
| 929 | |
Jeff Ohlstein | 4e93ae1 | 2011-09-26 18:22:26 -0700 | [diff] [blame] | 930 | static DEFINE_CLOCK_DATA(cd); |
| 931 | |
Vikram Mulukutla | a41f3a1 | 2011-10-31 14:20:50 -0700 | [diff] [blame] | 932 | /* |
| 933 | * Store the most recent timestamp read from hardware |
| 934 | * in last_ns. This is useful for debugging crashes. |
| 935 | */ |
Jeff Ohlstein | 06658f7 | 2011-11-09 13:51:11 -0800 | [diff] [blame] | 936 | static atomic64_t last_ns; |
Vikram Mulukutla | a41f3a1 | 2011-10-31 14:20:50 -0700 | [diff] [blame] | 937 | |
Jeff Ohlstein | 4e93ae1 | 2011-09-26 18:22:26 -0700 | [diff] [blame] | 938 | unsigned long long notrace sched_clock(void) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 939 | { |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 940 | struct msm_clock *clock = &msm_clocks[msm_global_timer]; |
Jeff Ohlstein | 4e93ae1 | 2011-09-26 18:22:26 -0700 | [diff] [blame] | 941 | struct clocksource *cs = &clock->clocksource; |
Jeff Ohlstein | 06658f7 | 2011-11-09 13:51:11 -0800 | [diff] [blame] | 942 | u64 cyc = cs->read(cs); |
| 943 | u64 last_ns_local; |
| 944 | last_ns_local = cyc_to_sched_clock(&cd, cyc, ((u32)~0 >> clock->shift)); |
| 945 | atomic64_set(&last_ns, last_ns_local); |
| 946 | return last_ns_local; |
Jeff Ohlstein | 4e93ae1 | 2011-09-26 18:22:26 -0700 | [diff] [blame] | 947 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 948 | |
Jeff Ohlstein | 4e93ae1 | 2011-09-26 18:22:26 -0700 | [diff] [blame] | 949 | static void notrace msm_update_sched_clock(void) |
| 950 | { |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 951 | struct msm_clock *clock = &msm_clocks[msm_global_timer]; |
Jeff Ohlstein | 4e93ae1 | 2011-09-26 18:22:26 -0700 | [diff] [blame] | 952 | struct clocksource *cs = &clock->clocksource; |
| 953 | u32 cyc = cs->read(cs); |
| 954 | update_sched_clock(&cd, cyc, ((u32)~0) >> clock->shift); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 955 | } |
| 956 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 957 | int read_current_timer(unsigned long *timer_val) |
| 958 | { |
| 959 | struct msm_clock *dgt = &msm_clocks[MSM_CLOCK_DGT]; |
| 960 | *timer_val = msm_read_timer_count(dgt, GLOBAL_TIMER); |
| 961 | return 0; |
| 962 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 963 | |
Jeff Ohlstein | 4e93ae1 | 2011-09-26 18:22:26 -0700 | [diff] [blame] | 964 | static void __init msm_sched_clock_init(void) |
| 965 | { |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 966 | struct msm_clock *clock = &msm_clocks[msm_global_timer]; |
Jeff Ohlstein | 4e93ae1 | 2011-09-26 18:22:26 -0700 | [diff] [blame] | 967 | |
| 968 | init_sched_clock(&cd, msm_update_sched_clock, 32 - clock->shift, |
| 969 | clock->freq); |
| 970 | } |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 971 | static void __init msm_timer_init(void) |
| 972 | { |
| 973 | int i; |
| 974 | int res; |
Jin Hong | eecb1e0 | 2011-10-21 14:36:32 -0700 | [diff] [blame] | 975 | struct irq_chip *chip; |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 976 | struct msm_clock *dgt = &msm_clocks[MSM_CLOCK_DGT]; |
| 977 | struct msm_clock *gpt = &msm_clocks[MSM_CLOCK_GPT]; |
David Brown | 8c27e6f | 2011-01-07 10:20:49 -0800 | [diff] [blame] | 978 | |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 979 | if (cpu_is_msm7x01() || cpu_is_msm7x25() || cpu_is_msm7x27() || |
| 980 | cpu_is_msm7x25a() || cpu_is_msm7x27a() || cpu_is_msm7x25aa() || |
| 981 | cpu_is_msm7x27aa()) { |
| 982 | dgt->shift = MSM_DGT_SHIFT; |
| 983 | dgt->freq = 19200000 >> MSM_DGT_SHIFT; |
| 984 | dgt->clockevent.shift = 32 + MSM_DGT_SHIFT; |
| 985 | dgt->clocksource.mask = CLOCKSOURCE_MASK(32 - MSM_DGT_SHIFT); |
| 986 | dgt->clocksource.shift = 24 - MSM_DGT_SHIFT; |
| 987 | gpt->regbase = MSM_TMR_BASE; |
| 988 | dgt->regbase = MSM_TMR_BASE + 0x10; |
Jeff Ohlstein | 5d90e25 | 2011-11-04 19:00:50 -0700 | [diff] [blame] | 989 | gpt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT |
| 990 | | MSM_CLOCK_FLAGS_ODD_MATCH_WRITE |
| 991 | | MSM_CLOCK_FLAGS_DELAYED_WRITE_POST; |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 992 | } else if (cpu_is_qsd8x50()) { |
| 993 | dgt->freq = 4800000; |
| 994 | gpt->regbase = MSM_TMR_BASE; |
| 995 | dgt->regbase = MSM_TMR_BASE + 0x10; |
| 996 | } else if (cpu_is_fsm9xxx()) |
| 997 | dgt->freq = 4800000; |
| 998 | else if (cpu_is_msm7x30() || cpu_is_msm8x55()) |
| 999 | dgt->freq = 6144000; |
Jeff Ohlstein | c83811b | 2011-10-21 14:24:04 -0700 | [diff] [blame] | 1000 | else if (cpu_is_msm8x60()) { |
Jeff Ohlstein | 7e538f0 | 2011-11-01 17:36:22 -0700 | [diff] [blame] | 1001 | global_timer_offset = MSM_TMR0_BASE - MSM_TMR_BASE; |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 1002 | dgt->freq = 6750000; |
| 1003 | __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] | 1004 | } else if (cpu_is_msm9615()) { |
| 1005 | dgt->freq = 6750000; |
| 1006 | __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL); |
| 1007 | gpt->freq = 32765; |
| 1008 | gpt_hz = 32765; |
| 1009 | sclk_hz = 32765; |
Jeff Ohlstein | d47f96a | 2011-11-04 19:00:50 -0700 | [diff] [blame] | 1010 | gpt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT; |
| 1011 | dgt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT; |
Jeff Ohlstein | 7e538f0 | 2011-11-01 17:36:22 -0700 | [diff] [blame] | 1012 | } else if (cpu_is_msm8960() || cpu_is_apq8064() || cpu_is_msm8930()) { |
| 1013 | global_timer_offset = MSM_TMR0_BASE - MSM_TMR_BASE; |
Jeff Ohlstein | c83811b | 2011-10-21 14:24:04 -0700 | [diff] [blame] | 1014 | dgt->freq = 6750000; |
| 1015 | __raw_writel(DGT_CLK_CTL_DIV_4, MSM_TMR_BASE + DGT_CLK_CTL); |
| 1016 | gpt->freq = 32765; |
| 1017 | gpt_hz = 32765; |
| 1018 | sclk_hz = 32765; |
Jeff Ohlstein | 391a3ee | 2011-12-01 16:44:45 -0800 | [diff] [blame] | 1019 | if (!machine_is_apq8064_rumi3()) { |
| 1020 | gpt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT; |
| 1021 | dgt->flags |= MSM_CLOCK_FLAGS_UNSTABLE_COUNT; |
| 1022 | } |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 1023 | } else { |
Jeff Ohlstein | f0a31e4 | 2012-01-06 19:03:05 -0800 | [diff] [blame] | 1024 | WARN(1, "Timer running on unknown hardware. Configure this! " |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 1025 | "Assuming default configuration.\n"); |
| 1026 | dgt->freq = 6750000; |
| 1027 | } |
| 1028 | |
| 1029 | if (msm_clocks[MSM_CLOCK_GPT].clocksource.rating > DG_TIMER_RATING) |
| 1030 | msm_global_timer = MSM_CLOCK_GPT; |
| 1031 | else |
| 1032 | msm_global_timer = MSM_CLOCK_DGT; |
Jeff Ohlstein | 672039f | 2010-10-05 15:23:57 -0700 | [diff] [blame] | 1033 | |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 1034 | for (i = 0; i < ARRAY_SIZE(msm_clocks); i++) { |
| 1035 | struct msm_clock *clock = &msm_clocks[i]; |
| 1036 | struct clock_event_device *ce = &clock->clockevent; |
| 1037 | struct clocksource *cs = &clock->clocksource; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1038 | __raw_writel(0, clock->regbase + TIMER_ENABLE); |
| 1039 | __raw_writel(1, clock->regbase + TIMER_CLEAR); |
| 1040 | __raw_writel(0, clock->regbase + TIMER_COUNT_VAL); |
| 1041 | __raw_writel(~0, clock->regbase + TIMER_MATCH_VAL); |
David Brown | 8c27e6f | 2011-01-07 10:20:49 -0800 | [diff] [blame] | 1042 | |
Jeff Ohlstein | c83811b | 2011-10-21 14:24:04 -0700 | [diff] [blame] | 1043 | if ((clock->freq << clock->shift) == gpt_hz) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1044 | clock->rollover_offset = 0; |
| 1045 | } else { |
| 1046 | uint64_t temp; |
David Brown | 8c27e6f | 2011-01-07 10:20:49 -0800 | [diff] [blame] | 1047 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1048 | temp = clock->freq << clock->shift; |
| 1049 | temp <<= 32; |
Jeff Ohlstein | c83811b | 2011-10-21 14:24:04 -0700 | [diff] [blame] | 1050 | do_div(temp, gpt_hz); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1051 | |
| 1052 | clock->rollover_offset = (uint32_t) temp; |
| 1053 | } |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 1054 | |
| 1055 | ce->mult = div_sc(clock->freq, NSEC_PER_SEC, ce->shift); |
| 1056 | /* allow at least 10 seconds to notice that the timer wrapped */ |
| 1057 | ce->max_delta_ns = |
| 1058 | clockevent_delta2ns(0xf0000000 >> clock->shift, ce); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1059 | /* ticks gets rounded down by one */ |
| 1060 | ce->min_delta_ns = |
| 1061 | clockevent_delta2ns(clock->write_delay + 4, ce); |
Rusty Russell | 320ab2b | 2008-12-13 21:20:26 +1030 | [diff] [blame] | 1062 | ce->cpumask = cpumask_of(0); |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 1063 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1064 | cs->mult = clocksource_hz2mult(clock->freq, cs->shift); |
| 1065 | res = clocksource_register(cs); |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 1066 | if (res) |
| 1067 | printk(KERN_ERR "msm_timer_init: clocksource_register " |
| 1068 | "failed for %s\n", cs->name); |
| 1069 | |
Trilok Soni | eecb28c | 2011-07-20 16:24:14 +0100 | [diff] [blame] | 1070 | ce->irq = clock->irq; |
| 1071 | if (cpu_is_msm8x60() || cpu_is_msm8960() || cpu_is_apq8064() || |
| 1072 | cpu_is_msm8930() || cpu_is_msm9615()) { |
| 1073 | clock->percpu_evt = alloc_percpu(struct clock_event_device *); |
| 1074 | if (!clock->percpu_evt) { |
| 1075 | pr_err("msm_timer_init: memory allocation " |
| 1076 | "failed for %s\n", ce->name); |
| 1077 | continue; |
| 1078 | } |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 1079 | |
Trilok Soni | eecb28c | 2011-07-20 16:24:14 +0100 | [diff] [blame] | 1080 | *__this_cpu_ptr(clock->percpu_evt) = ce; |
| 1081 | res = request_percpu_irq(ce->irq, msm_timer_interrupt, |
| 1082 | ce->name, clock->percpu_evt); |
| 1083 | if (!res) |
| 1084 | enable_percpu_irq(ce->irq, 0); |
| 1085 | } else { |
| 1086 | clock->evt = ce; |
| 1087 | res = request_irq(ce->irq, msm_timer_interrupt, |
| 1088 | IRQF_TIMER | IRQF_NOBALANCING | IRQF_TRIGGER_RISING, |
| 1089 | ce->name, &clock->evt); |
| 1090 | } |
| 1091 | |
| 1092 | if (res) |
| 1093 | pr_err("msm_timer_init: request_irq failed for %s\n", |
| 1094 | ce->name); |
| 1095 | |
| 1096 | chip = irq_get_chip(clock->irq); |
Jin Hong | eecb1e0 | 2011-10-21 14:36:32 -0700 | [diff] [blame] | 1097 | if (chip && chip->irq_mask) |
Trilok Soni | eecb28c | 2011-07-20 16:24:14 +0100 | [diff] [blame] | 1098 | chip->irq_mask(irq_get_irq_data(clock->irq)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1099 | |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 1100 | clockevents_register_device(ce); |
| 1101 | } |
Jeff Ohlstein | 4e93ae1 | 2011-09-26 18:22:26 -0700 | [diff] [blame] | 1102 | msm_sched_clock_init(); |
Taniya Das | 36057be | 2011-10-28 13:02:17 +0530 | [diff] [blame] | 1103 | |
| 1104 | if (is_smp()) { |
| 1105 | __raw_writel(1, |
| 1106 | msm_clocks[MSM_CLOCK_DGT].regbase + TIMER_ENABLE); |
| 1107 | set_delay_fn(read_current_timer_delay_loop); |
| 1108 | } |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 1109 | } |
| 1110 | |
Jeff Ohlstein | 94790ec | 2010-12-02 12:05:12 -0800 | [diff] [blame] | 1111 | #ifdef CONFIG_SMP |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1112 | |
Santosh Shilimkar | af90f10 | 2011-02-23 18:53:15 +0100 | [diff] [blame] | 1113 | int __cpuinit local_timer_setup(struct clock_event_device *evt) |
Jeff Ohlstein | 94790ec | 2010-12-02 12:05:12 -0800 | [diff] [blame] | 1114 | { |
Jeff Ohlstein | d17ee76 | 2011-09-26 19:44:22 -0700 | [diff] [blame] | 1115 | static DEFINE_PER_CPU(bool, first_boot) = true; |
Jeff Ohlstein | 7a01832 | 2011-09-28 12:44:06 -0700 | [diff] [blame] | 1116 | struct msm_clock *clock = &msm_clocks[msm_global_timer]; |
Jeff Ohlstein | 94790ec | 2010-12-02 12:05:12 -0800 | [diff] [blame] | 1117 | |
| 1118 | /* Use existing clock_event for cpu 0 */ |
| 1119 | if (!smp_processor_id()) |
David Brown | 893b66c | 2011-03-30 11:26:57 -0700 | [diff] [blame] | 1120 | return 0; |
Jeff Ohlstein | 94790ec | 2010-12-02 12:05:12 -0800 | [diff] [blame] | 1121 | |
Taniya Das | 36057be | 2011-10-28 13:02:17 +0530 | [diff] [blame] | 1122 | if (cpu_is_msm8x60() || cpu_is_msm8960() || cpu_is_apq8064() |
| 1123 | || cpu_is_msm8930()) |
| 1124 | __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] | 1125 | |
Jeff Ohlstein | d17ee76 | 2011-09-26 19:44:22 -0700 | [diff] [blame] | 1126 | if (__get_cpu_var(first_boot)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1127 | __raw_writel(0, clock->regbase + TIMER_ENABLE); |
| 1128 | __raw_writel(0, clock->regbase + TIMER_CLEAR); |
| 1129 | __raw_writel(~0, clock->regbase + TIMER_MATCH_VAL); |
Jeff Ohlstein | d17ee76 | 2011-09-26 19:44:22 -0700 | [diff] [blame] | 1130 | __get_cpu_var(first_boot) = false; |
Jeff Ohlstein | 94790ec | 2010-12-02 12:05:12 -0800 | [diff] [blame] | 1131 | } |
Trilok Soni | eecb28c | 2011-07-20 16:24:14 +0100 | [diff] [blame] | 1132 | evt->irq = clock->irq; |
Jeff Ohlstein | 94790ec | 2010-12-02 12:05:12 -0800 | [diff] [blame] | 1133 | evt->name = "local_timer"; |
| 1134 | evt->features = CLOCK_EVT_FEAT_ONESHOT; |
| 1135 | evt->rating = clock->clockevent.rating; |
| 1136 | evt->set_mode = msm_timer_set_mode; |
| 1137 | evt->set_next_event = msm_timer_set_next_event; |
| 1138 | evt->shift = clock->clockevent.shift; |
| 1139 | evt->mult = div_sc(clock->freq, NSEC_PER_SEC, evt->shift); |
| 1140 | evt->max_delta_ns = |
| 1141 | clockevent_delta2ns(0xf0000000 >> clock->shift, evt); |
| 1142 | evt->min_delta_ns = clockevent_delta2ns(4, evt); |
| 1143 | |
Trilok Soni | eecb28c | 2011-07-20 16:24:14 +0100 | [diff] [blame] | 1144 | *__this_cpu_ptr(clock->percpu_evt) = evt; |
Jeff Ohlstein | 94790ec | 2010-12-02 12:05:12 -0800 | [diff] [blame] | 1145 | |
| 1146 | clockevents_register_device(evt); |
Trilok Soni | eecb28c | 2011-07-20 16:24:14 +0100 | [diff] [blame] | 1147 | enable_percpu_irq(evt->irq, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1148 | |
Santosh Shilimkar | af90f10 | 2011-02-23 18:53:15 +0100 | [diff] [blame] | 1149 | return 0; |
Jeff Ohlstein | 94790ec | 2010-12-02 12:05:12 -0800 | [diff] [blame] | 1150 | } |
| 1151 | |
Trilok Soni | eecb28c | 2011-07-20 16:24:14 +0100 | [diff] [blame] | 1152 | void local_timer_stop(struct clock_event_device *evt) |
Jeff Ohlstein | 94790ec | 2010-12-02 12:05:12 -0800 | [diff] [blame] | 1153 | { |
Trilok Soni | eecb28c | 2011-07-20 16:24:14 +0100 | [diff] [blame] | 1154 | evt->set_mode(CLOCK_EVT_MODE_UNUSED, evt); |
| 1155 | disable_percpu_irq(evt->irq); |
Jeff Ohlstein | 94790ec | 2010-12-02 12:05:12 -0800 | [diff] [blame] | 1156 | } |
Jeff Ohlstein | 94790ec | 2010-12-02 12:05:12 -0800 | [diff] [blame] | 1157 | #endif |
| 1158 | |
Arve Hjønnevåg | 3e4ea37 | 2007-11-26 04:11:58 -0800 | [diff] [blame] | 1159 | struct sys_timer msm_timer = { |
| 1160 | .init = msm_timer_init |
| 1161 | }; |