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