diff options
Diffstat (limited to 'arch/arm/mach-tegra/timer.c')
-rw-r--r-- | arch/arm/mach-tegra/timer.c | 147 |
1 files changed, 91 insertions, 56 deletions
diff --git a/arch/arm/mach-tegra/timer.c b/arch/arm/mach-tegra/timer.c index 90350420c4e..83d0e17b50c 100644 --- a/arch/arm/mach-tegra/timer.c +++ b/arch/arm/mach-tegra/timer.c | |||
@@ -2,10 +2,13 @@ | |||
2 | * arch/arch/mach-tegra/timer.c | 2 | * arch/arch/mach-tegra/timer.c |
3 | * | 3 | * |
4 | * Copyright (C) 2010 Google, Inc. | 4 | * Copyright (C) 2010 Google, Inc. |
5 | * Copyright (C) 2011 NVIDIA Corporation. | ||
5 | * | 6 | * |
6 | * Author: | 7 | * Author: |
7 | * Colin Cross <ccross@google.com> | 8 | * Colin Cross <ccross@google.com> |
8 | * | 9 | * |
10 | * Copyright (C) 2010-2011 NVIDIA Corporation. | ||
11 | * | ||
9 | * This software is licensed under the terms of the GNU General Public | 12 | * This software is licensed under the terms of the GNU General Public |
10 | * License version 2, as published by the Free Software Foundation, and | 13 | * License version 2, as published by the Free Software Foundation, and |
11 | * may be copied, distributed, and modified under those terms. | 14 | * may be copied, distributed, and modified under those terms. |
@@ -27,39 +30,30 @@ | |||
27 | #include <linux/clocksource.h> | 30 | #include <linux/clocksource.h> |
28 | #include <linux/clk.h> | 31 | #include <linux/clk.h> |
29 | #include <linux/io.h> | 32 | #include <linux/io.h> |
33 | #include <linux/syscore_ops.h> | ||
30 | 34 | ||
31 | #include <asm/mach/time.h> | 35 | #include <asm/mach/time.h> |
32 | #include <asm/localtimer.h> | 36 | #include <asm/localtimer.h> |
37 | #include <asm/smp_twd.h> | ||
33 | #include <asm/sched_clock.h> | 38 | #include <asm/sched_clock.h> |
34 | 39 | ||
35 | #include <mach/iomap.h> | 40 | #include <mach/iomap.h> |
36 | #include <mach/irqs.h> | 41 | #include <mach/irqs.h> |
37 | #include <mach/suspend.h> | ||
38 | 42 | ||
39 | #include "board.h" | 43 | #include "board.h" |
40 | #include "clock.h" | 44 | #include "clock.h" |
41 | 45 | #include "timer.h" | |
42 | #define RTC_SECONDS 0x08 | ||
43 | #define RTC_SHADOW_SECONDS 0x0c | ||
44 | #define RTC_MILLISECONDS 0x10 | ||
45 | |||
46 | #define TIMERUS_CNTR_1US 0x10 | ||
47 | #define TIMERUS_USEC_CFG 0x14 | ||
48 | #define TIMERUS_CNTR_FREEZE 0x4c | ||
49 | |||
50 | #define TIMER1_BASE 0x0 | ||
51 | #define TIMER2_BASE 0x8 | ||
52 | #define TIMER3_BASE 0x50 | ||
53 | #define TIMER4_BASE 0x58 | ||
54 | |||
55 | #define TIMER_PTV 0x0 | ||
56 | #define TIMER_PCR 0x4 | ||
57 | 46 | ||
58 | static void __iomem *timer_reg_base = IO_ADDRESS(TEGRA_TMR1_BASE); | 47 | static void __iomem *timer_reg_base = IO_ADDRESS(TEGRA_TMR1_BASE); |
59 | static void __iomem *rtc_base = IO_ADDRESS(TEGRA_RTC_BASE); | 48 | static void __iomem *rtc_base = IO_ADDRESS(TEGRA_RTC_BASE); |
60 | 49 | ||
61 | static struct timespec persistent_ts; | 50 | static struct timespec persistent_ts; |
62 | static u64 persistent_ms, last_persistent_ms; | 51 | static u64 persistent_ms, last_persistent_ms; |
52 | static u32 usec_config; | ||
53 | static u32 usec_offset; | ||
54 | static bool usec_suspended; | ||
55 | |||
56 | static u32 system_timer; | ||
63 | 57 | ||
64 | #define timer_writel(value, reg) \ | 58 | #define timer_writel(value, reg) \ |
65 | __raw_writel(value, (u32)timer_reg_base + (reg)) | 59 | __raw_writel(value, (u32)timer_reg_base + (reg)) |
@@ -72,7 +66,7 @@ static int tegra_timer_set_next_event(unsigned long cycles, | |||
72 | u32 reg; | 66 | u32 reg; |
73 | 67 | ||
74 | reg = 0x80000000 | ((cycles > 1) ? (cycles-1) : 0); | 68 | reg = 0x80000000 | ((cycles > 1) ? (cycles-1) : 0); |
75 | timer_writel(reg, TIMER3_BASE + TIMER_PTV); | 69 | timer_writel(reg, system_timer + TIMER_PTV); |
76 | 70 | ||
77 | return 0; | 71 | return 0; |
78 | } | 72 | } |
@@ -82,12 +76,12 @@ static void tegra_timer_set_mode(enum clock_event_mode mode, | |||
82 | { | 76 | { |
83 | u32 reg; | 77 | u32 reg; |
84 | 78 | ||
85 | timer_writel(0, TIMER3_BASE + TIMER_PTV); | 79 | timer_writel(0, system_timer + TIMER_PTV); |
86 | 80 | ||
87 | switch (mode) { | 81 | switch (mode) { |
88 | case CLOCK_EVT_MODE_PERIODIC: | 82 | case CLOCK_EVT_MODE_PERIODIC: |
89 | reg = 0xC0000000 | ((1000000/HZ)-1); | 83 | reg = 0xC0000000 | ((1000000/HZ)-1); |
90 | timer_writel(reg, TIMER3_BASE + TIMER_PTV); | 84 | timer_writel(reg, system_timer + TIMER_PTV); |
91 | break; | 85 | break; |
92 | case CLOCK_EVT_MODE_ONESHOT: | 86 | case CLOCK_EVT_MODE_ONESHOT: |
93 | break; | 87 | break; |
@@ -115,15 +109,23 @@ static DEFINE_CLOCK_DATA(cd); | |||
115 | #define SC_MULT 4194304000u | 109 | #define SC_MULT 4194304000u |
116 | #define SC_SHIFT 22 | 110 | #define SC_SHIFT 22 |
117 | 111 | ||
112 | static u32 notrace tegra_read_usec(void) | ||
113 | { | ||
114 | u32 cyc = usec_offset; | ||
115 | if (!usec_suspended) | ||
116 | cyc += timer_readl(TIMERUS_CNTR_1US); | ||
117 | return cyc; | ||
118 | } | ||
119 | |||
118 | unsigned long long notrace sched_clock(void) | 120 | unsigned long long notrace sched_clock(void) |
119 | { | 121 | { |
120 | u32 cyc = timer_readl(TIMERUS_CNTR_1US); | 122 | u32 cyc = tegra_read_usec(); |
121 | return cyc_to_fixed_sched_clock(&cd, cyc, (u32)~0, SC_MULT, SC_SHIFT); | 123 | return cyc_to_fixed_sched_clock(&cd, cyc, (u32)~0, SC_MULT, SC_SHIFT); |
122 | } | 124 | } |
123 | 125 | ||
124 | static void notrace tegra_update_sched_clock(void) | 126 | static void notrace tegra_update_sched_clock(void) |
125 | { | 127 | { |
126 | u32 cyc = timer_readl(TIMERUS_CNTR_1US); | 128 | u32 cyc = tegra_read_usec(); |
127 | update_sched_clock(&cd, cyc, (u32)~0); | 129 | update_sched_clock(&cd, cyc, (u32)~0); |
128 | } | 130 | } |
129 | 131 | ||
@@ -133,7 +135,7 @@ static void notrace tegra_update_sched_clock(void) | |||
133 | * tegra_rtc driver could be executing to avoid race conditions | 135 | * tegra_rtc driver could be executing to avoid race conditions |
134 | * on the RTC shadow register | 136 | * on the RTC shadow register |
135 | */ | 137 | */ |
136 | u64 tegra_rtc_read_ms(void) | 138 | static u64 tegra_rtc_read_ms(void) |
137 | { | 139 | { |
138 | u32 ms = readl(rtc_base + RTC_MILLISECONDS); | 140 | u32 ms = readl(rtc_base + RTC_MILLISECONDS); |
139 | u32 s = readl(rtc_base + RTC_SHADOW_SECONDS); | 141 | u32 s = readl(rtc_base + RTC_SHADOW_SECONDS); |
@@ -166,7 +168,7 @@ void read_persistent_clock(struct timespec *ts) | |||
166 | static irqreturn_t tegra_timer_interrupt(int irq, void *dev_id) | 168 | static irqreturn_t tegra_timer_interrupt(int irq, void *dev_id) |
167 | { | 169 | { |
168 | struct clock_event_device *evt = (struct clock_event_device *)dev_id; | 170 | struct clock_event_device *evt = (struct clock_event_device *)dev_id; |
169 | timer_writel(1<<30, TIMER3_BASE + TIMER_PCR); | 171 | timer_writel(1<<30, system_timer + TIMER_PCR); |
170 | evt->event_handler(evt); | 172 | evt->event_handler(evt); |
171 | return IRQ_HANDLED; | 173 | return IRQ_HANDLED; |
172 | } | 174 | } |
@@ -176,13 +178,69 @@ static struct irqaction tegra_timer_irq = { | |||
176 | .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_TRIGGER_HIGH, | 178 | .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_TRIGGER_HIGH, |
177 | .handler = tegra_timer_interrupt, | 179 | .handler = tegra_timer_interrupt, |
178 | .dev_id = &tegra_clockevent, | 180 | .dev_id = &tegra_clockevent, |
179 | .irq = INT_TMR3, | ||
180 | }; | 181 | }; |
181 | 182 | ||
183 | static int tegra_timer_suspend(void) | ||
184 | { | ||
185 | usec_config = timer_readl(TIMERUS_USEC_CFG); | ||
186 | |||
187 | usec_offset += timer_readl(TIMERUS_CNTR_1US); | ||
188 | usec_suspended = true; | ||
189 | |||
190 | return 0; | ||
191 | } | ||
192 | |||
193 | static void tegra_timer_resume(void) | ||
194 | { | ||
195 | timer_writel(usec_config, TIMERUS_USEC_CFG); | ||
196 | |||
197 | usec_offset -= timer_readl(TIMERUS_CNTR_1US); | ||
198 | usec_suspended = false; | ||
199 | } | ||
200 | |||
201 | static struct syscore_ops tegra_timer_syscore_ops = { | ||
202 | .suspend = tegra_timer_suspend, | ||
203 | .resume = tegra_timer_resume, | ||
204 | }; | ||
205 | |||
206 | #ifdef CONFIG_HAVE_ARM_TWD | ||
207 | int tegra_twd_get_state(struct tegra_twd_context *context) | ||
208 | { | ||
209 | context->twd_ctrl = readl(twd_base + TWD_TIMER_CONTROL); | ||
210 | context->twd_load = readl(twd_base + TWD_TIMER_LOAD); | ||
211 | context->twd_cnt = readl(twd_base + TWD_TIMER_COUNTER); | ||
212 | |||
213 | return 0; | ||
214 | } | ||
215 | |||
216 | void tegra_twd_suspend(struct tegra_twd_context *context) | ||
217 | { | ||
218 | context->twd_ctrl = readl(twd_base + TWD_TIMER_CONTROL); | ||
219 | context->twd_load = readl(twd_base + TWD_TIMER_LOAD); | ||
220 | if ((context->twd_load == 0) && | ||
221 | (context->twd_ctrl & TWD_TIMER_CONTROL_PERIODIC) && | ||
222 | (context->twd_ctrl & (TWD_TIMER_CONTROL_ENABLE | | ||
223 | TWD_TIMER_CONTROL_IT_ENABLE))) { | ||
224 | WARN("%s: TWD enabled but counter was 0\n", __func__); | ||
225 | context->twd_load = 1; | ||
226 | } | ||
227 | __raw_writel(0, twd_base + TWD_TIMER_CONTROL); | ||
228 | } | ||
229 | |||
230 | void tegra_twd_resume(struct tegra_twd_context *context) | ||
231 | { | ||
232 | BUG_ON((context->twd_load == 0) && | ||
233 | (context->twd_ctrl & TWD_TIMER_CONTROL_PERIODIC) && | ||
234 | (context->twd_ctrl & (TWD_TIMER_CONTROL_ENABLE | | ||
235 | TWD_TIMER_CONTROL_IT_ENABLE))); | ||
236 | writel(context->twd_load, twd_base + TWD_TIMER_LOAD); | ||
237 | writel(context->twd_ctrl, twd_base + TWD_TIMER_CONTROL); | ||
238 | } | ||
239 | #endif | ||
240 | |||
182 | static void __init tegra_init_timer(void) | 241 | static void __init tegra_init_timer(void) |
183 | { | 242 | { |
184 | struct clk *clk; | 243 | struct clk *clk; |
185 | unsigned long rate = clk_measure_input_freq(); | ||
186 | int ret; | 244 | int ret; |
187 | 245 | ||
188 | clk = clk_get_sys("timer", NULL); | 246 | clk = clk_get_sys("timer", NULL); |
@@ -201,22 +259,11 @@ static void __init tegra_init_timer(void) | |||
201 | twd_base = IO_ADDRESS(TEGRA_ARM_PERIF_BASE + 0x600); | 259 | twd_base = IO_ADDRESS(TEGRA_ARM_PERIF_BASE + 0x600); |
202 | #endif | 260 | #endif |
203 | 261 | ||
204 | switch (rate) { | 262 | #ifdef CONFIG_ARCH_TEGRA_2x_SOC |
205 | case 12000000: | 263 | tegra2_init_timer(&system_timer, &tegra_timer_irq.irq); |
206 | timer_writel(0x000b, TIMERUS_USEC_CFG); | 264 | #else |
207 | break; | 265 | tegra3_init_timer(&system_timer, &tegra_timer_irq.irq); |
208 | case 13000000: | 266 | #endif |
209 | timer_writel(0x000c, TIMERUS_USEC_CFG); | ||
210 | break; | ||
211 | case 19200000: | ||
212 | timer_writel(0x045f, TIMERUS_USEC_CFG); | ||
213 | break; | ||
214 | case 26000000: | ||
215 | timer_writel(0x0019, TIMERUS_USEC_CFG); | ||
216 | break; | ||
217 | default: | ||
218 | WARN(1, "Unknown clock rate"); | ||
219 | } | ||
220 | 267 | ||
221 | init_fixed_sched_clock(&cd, tegra_update_sched_clock, 32, | 268 | init_fixed_sched_clock(&cd, tegra_update_sched_clock, 32, |
222 | 1000000, SC_MULT, SC_SHIFT); | 269 | 1000000, SC_MULT, SC_SHIFT); |
@@ -241,22 +288,10 @@ static void __init tegra_init_timer(void) | |||
241 | tegra_clockevent.cpumask = cpu_all_mask; | 288 | tegra_clockevent.cpumask = cpu_all_mask; |
242 | tegra_clockevent.irq = tegra_timer_irq.irq; | 289 | tegra_clockevent.irq = tegra_timer_irq.irq; |
243 | clockevents_register_device(&tegra_clockevent); | 290 | clockevents_register_device(&tegra_clockevent); |
291 | |||
292 | register_syscore_ops(&tegra_timer_syscore_ops); | ||
244 | } | 293 | } |
245 | 294 | ||
246 | struct sys_timer tegra_timer = { | 295 | struct sys_timer tegra_timer = { |
247 | .init = tegra_init_timer, | 296 | .init = tegra_init_timer, |
248 | }; | 297 | }; |
249 | |||
250 | #ifdef CONFIG_PM | ||
251 | static u32 usec_config; | ||
252 | |||
253 | void tegra_timer_suspend(void) | ||
254 | { | ||
255 | usec_config = timer_readl(TIMERUS_USEC_CFG); | ||
256 | } | ||
257 | |||
258 | void tegra_timer_resume(void) | ||
259 | { | ||
260 | timer_writel(usec_config, TIMERUS_USEC_CFG); | ||
261 | } | ||
262 | #endif | ||