blob: 026bad4ac57acb8d3a794f779004166842b34eac [file] [log] [blame]
Colin Cross2d5cd9a2010-01-28 16:41:42 -08001/*
2 * arch/arch/mach-tegra/timer.c
3 *
4 * Copyright (C) 2010 Google, Inc.
5 *
6 * Author:
7 * Colin Cross <ccross@google.com>
8 *
9 * This software is licensed under the terms of the GNU General Public
10 * License version 2, as published by the Free Software Foundation, and
11 * may be copied, distributed, and modified under those terms.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 */
19
20#include <linux/init.h>
Colin Cross62248ae2011-02-21 17:04:37 -080021#include <linux/err.h>
Colin Cross2d5cd9a2010-01-28 16:41:42 -080022#include <linux/time.h>
23#include <linux/interrupt.h>
24#include <linux/irq.h>
25#include <linux/clockchips.h>
26#include <linux/clocksource.h>
27#include <linux/clk.h>
28#include <linux/io.h>
Stephen Warren56415482012-09-19 13:13:33 -060029#include <linux/of_irq.h>
Colin Cross2d5cd9a2010-01-28 16:41:42 -080030
31#include <asm/mach/time.h>
Marc Zyngier1fcf3a62012-01-10 19:44:19 +000032#include <asm/smp_twd.h>
Russell Kinge3f4c0a2010-12-15 21:49:42 +000033#include <asm/sched_clock.h>
Colin Cross2d5cd9a2010-01-28 16:41:42 -080034
Colin Cross2d5cd9a2010-01-28 16:41:42 -080035#include "board.h"
36#include "clock.h"
Stephen Warren2be39c02012-10-04 14:24:09 -060037#include "iomap.h"
Colin Cross2d5cd9a2010-01-28 16:41:42 -080038
Colin Cross09361782010-11-28 16:26:19 -080039#define RTC_SECONDS 0x08
40#define RTC_SHADOW_SECONDS 0x0c
41#define RTC_MILLISECONDS 0x10
42
Colin Cross2d5cd9a2010-01-28 16:41:42 -080043#define TIMERUS_CNTR_1US 0x10
44#define TIMERUS_USEC_CFG 0x14
45#define TIMERUS_CNTR_FREEZE 0x4c
46
47#define TIMER1_BASE 0x0
48#define TIMER2_BASE 0x8
49#define TIMER3_BASE 0x50
50#define TIMER4_BASE 0x58
51
52#define TIMER_PTV 0x0
53#define TIMER_PCR 0x4
54
Colin Cross2d5cd9a2010-01-28 16:41:42 -080055static void __iomem *timer_reg_base = IO_ADDRESS(TEGRA_TMR1_BASE);
Colin Cross09361782010-11-28 16:26:19 -080056static void __iomem *rtc_base = IO_ADDRESS(TEGRA_RTC_BASE);
57
58static struct timespec persistent_ts;
59static u64 persistent_ms, last_persistent_ms;
Colin Cross2d5cd9a2010-01-28 16:41:42 -080060
61#define timer_writel(value, reg) \
Olof Johansson75d71162011-09-08 17:49:13 -070062 __raw_writel(value, timer_reg_base + (reg))
Colin Cross2d5cd9a2010-01-28 16:41:42 -080063#define timer_readl(reg) \
Olof Johansson75d71162011-09-08 17:49:13 -070064 __raw_readl(timer_reg_base + (reg))
Colin Cross2d5cd9a2010-01-28 16:41:42 -080065
66static int tegra_timer_set_next_event(unsigned long cycles,
67 struct clock_event_device *evt)
68{
69 u32 reg;
70
71 reg = 0x80000000 | ((cycles > 1) ? (cycles-1) : 0);
72 timer_writel(reg, TIMER3_BASE + TIMER_PTV);
73
74 return 0;
75}
76
77static void tegra_timer_set_mode(enum clock_event_mode mode,
78 struct clock_event_device *evt)
79{
80 u32 reg;
81
82 timer_writel(0, TIMER3_BASE + TIMER_PTV);
83
84 switch (mode) {
85 case CLOCK_EVT_MODE_PERIODIC:
86 reg = 0xC0000000 | ((1000000/HZ)-1);
87 timer_writel(reg, TIMER3_BASE + TIMER_PTV);
88 break;
89 case CLOCK_EVT_MODE_ONESHOT:
90 break;
91 case CLOCK_EVT_MODE_UNUSED:
92 case CLOCK_EVT_MODE_SHUTDOWN:
93 case CLOCK_EVT_MODE_RESUME:
94 break;
95 }
96}
97
Colin Cross2d5cd9a2010-01-28 16:41:42 -080098static struct clock_event_device tegra_clockevent = {
99 .name = "timer0",
100 .rating = 300,
101 .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
102 .set_next_event = tegra_timer_set_next_event,
103 .set_mode = tegra_timer_set_mode,
104};
105
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100106static u32 notrace tegra_read_sched_clock(void)
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800107{
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100108 return timer_readl(TIMERUS_CNTR_1US);
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800109}
110
Colin Cross09361782010-11-28 16:26:19 -0800111/*
112 * tegra_rtc_read - Reads the Tegra RTC registers
113 * Care must be taken that this funciton is not called while the
114 * tegra_rtc driver could be executing to avoid race conditions
115 * on the RTC shadow register
116 */
Olof Johanssonb28fba22011-09-08 17:50:03 -0700117static u64 tegra_rtc_read_ms(void)
Colin Cross09361782010-11-28 16:26:19 -0800118{
119 u32 ms = readl(rtc_base + RTC_MILLISECONDS);
120 u32 s = readl(rtc_base + RTC_SHADOW_SECONDS);
121 return (u64)s * MSEC_PER_SEC + ms;
122}
123
124/*
Marc Zyngierbd0493e2012-05-05 19:28:44 +0100125 * tegra_read_persistent_clock - Return time from a persistent clock.
Colin Cross09361782010-11-28 16:26:19 -0800126 *
127 * Reads the time from a source which isn't disabled during PM, the
128 * 32k sync timer. Convert the cycles elapsed since last read into
129 * nsecs and adds to a monotonically increasing timespec.
130 * Care must be taken that this funciton is not called while the
131 * tegra_rtc driver could be executing to avoid race conditions
132 * on the RTC shadow register
133 */
Marc Zyngierbd0493e2012-05-05 19:28:44 +0100134static void tegra_read_persistent_clock(struct timespec *ts)
Colin Cross09361782010-11-28 16:26:19 -0800135{
136 u64 delta;
137 struct timespec *tsp = &persistent_ts;
138
139 last_persistent_ms = persistent_ms;
140 persistent_ms = tegra_rtc_read_ms();
141 delta = persistent_ms - last_persistent_ms;
142
143 timespec_add_ns(tsp, delta * NSEC_PER_MSEC);
144 *ts = *tsp;
145}
146
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800147static irqreturn_t tegra_timer_interrupt(int irq, void *dev_id)
148{
149 struct clock_event_device *evt = (struct clock_event_device *)dev_id;
150 timer_writel(1<<30, TIMER3_BASE + TIMER_PCR);
151 evt->event_handler(evt);
152 return IRQ_HANDLED;
153}
154
155static struct irqaction tegra_timer_irq = {
156 .name = "timer0",
157 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_TRIGGER_HIGH,
158 .handler = tegra_timer_interrupt,
159 .dev_id = &tegra_clockevent,
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800160};
161
Stephen Warren56415482012-09-19 13:13:33 -0600162static const struct of_device_id timer_match[] __initconst = {
163 { .compatible = "nvidia,tegra20-timer" },
164 {}
165};
Marc Zyngier1fcf3a62012-01-10 19:44:19 +0000166
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800167static void __init tegra_init_timer(void)
168{
Stephen Warren56415482012-09-19 13:13:33 -0600169 struct device_node *np;
Colin Cross62248ae2011-02-21 17:04:37 -0800170 struct clk *clk;
Peter De Schrijver8e4fab22011-12-14 17:03:16 +0200171 unsigned long rate;
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800172 int ret;
173
Stephen Warren56415482012-09-19 13:13:33 -0600174 np = of_find_matching_node(NULL, timer_match);
175 if (!np) {
176 pr_err("Failed to find timer DT node\n");
177 BUG();
178 }
179
180 tegra_timer_irq.irq = irq_of_parse_and_map(np, 2);
181 if (tegra_timer_irq.irq <= 0) {
182 pr_err("Failed to map timer IRQ\n");
183 BUG();
184 }
185
Colin Cross62248ae2011-02-21 17:04:37 -0800186 clk = clk_get_sys("timer", NULL);
Peter De Schrijver8e4fab22011-12-14 17:03:16 +0200187 if (IS_ERR(clk)) {
Stephen Warren58664f92012-10-23 12:21:39 -0600188 pr_warn("Unable to get timer clock. Assuming 12Mhz input clock.\n");
Peter De Schrijver8e4fab22011-12-14 17:03:16 +0200189 rate = 12000000;
190 } else {
Prashant Gaikwad6a5278d2012-06-05 09:59:35 +0530191 clk_prepare_enable(clk);
Peter De Schrijver8e4fab22011-12-14 17:03:16 +0200192 rate = clk_get_rate(clk);
193 }
Colin Cross62248ae2011-02-21 17:04:37 -0800194
195 /*
196 * rtc registers are used by read_persistent_clock, keep the rtc clock
197 * enabled
198 */
199 clk = clk_get_sys("rtc-tegra", NULL);
Peter De Schrijver2d85b5d2011-10-26 11:41:41 +0300200 if (IS_ERR(clk))
201 pr_warn("Unable to get rtc-tegra clock\n");
202 else
Prashant Gaikwad6a5278d2012-06-05 09:59:35 +0530203 clk_prepare_enable(clk);
Colin Cross62248ae2011-02-21 17:04:37 -0800204
Stephen Warren56415482012-09-19 13:13:33 -0600205 of_node_put(np);
206
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800207 switch (rate) {
208 case 12000000:
209 timer_writel(0x000b, TIMERUS_USEC_CFG);
210 break;
211 case 13000000:
212 timer_writel(0x000c, TIMERUS_USEC_CFG);
213 break;
214 case 19200000:
215 timer_writel(0x045f, TIMERUS_USEC_CFG);
216 break;
217 case 26000000:
218 timer_writel(0x0019, TIMERUS_USEC_CFG);
219 break;
220 default:
221 WARN(1, "Unknown clock rate");
222 }
223
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100224 setup_sched_clock(tegra_read_sched_clock, 32, 1000000);
Russell Kinge3f4c0a2010-12-15 21:49:42 +0000225
Russell King234b6ced2011-05-08 14:09:47 +0100226 if (clocksource_mmio_init(timer_reg_base + TIMERUS_CNTR_1US,
227 "timer_us", 1000000, 300, 32, clocksource_mmio_readl_up)) {
Stephen Warren58664f92012-10-23 12:21:39 -0600228 pr_err("Failed to register clocksource\n");
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800229 BUG();
230 }
231
232 ret = setup_irq(tegra_timer_irq.irq, &tegra_timer_irq);
233 if (ret) {
Stephen Warren58664f92012-10-23 12:21:39 -0600234 pr_err("Failed to register timer IRQ: %d\n", ret);
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800235 BUG();
236 }
237
238 clockevents_calc_mult_shift(&tegra_clockevent, 1000000, 5);
239 tegra_clockevent.max_delta_ns =
240 clockevent_delta2ns(0x1fffffff, &tegra_clockevent);
241 tegra_clockevent.min_delta_ns =
242 clockevent_delta2ns(0x1, &tegra_clockevent);
243 tegra_clockevent.cpumask = cpu_all_mask;
244 tegra_clockevent.irq = tegra_timer_irq.irq;
245 clockevents_register_device(&tegra_clockevent);
Stephen Warren56415482012-09-19 13:13:33 -0600246#ifdef CONFIG_HAVE_ARM_TWD
247 twd_local_timer_of_register();
248#endif
Marc Zyngierbd0493e2012-05-05 19:28:44 +0100249 register_persistent_clock(NULL, tegra_read_persistent_clock);
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800250}
251
Sivaram Nairf2ef4122012-10-16 13:08:35 +0300252struct sys_timer tegra_sys_timer = {
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800253 .init = tegra_init_timer,
254};
Colin Cross09361782010-11-28 16:26:19 -0800255
256#ifdef CONFIG_PM
257static u32 usec_config;
258
259void tegra_timer_suspend(void)
260{
261 usec_config = timer_readl(TIMERUS_USEC_CFG);
262}
263
264void tegra_timer_resume(void)
265{
266 timer_writel(usec_config, TIMERUS_USEC_CFG);
267}
268#endif