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