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