blob: b49f2f5025f288d0fb120e3a246722e054f06ead [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>
21#include <linux/time.h>
22#include <linux/interrupt.h>
23#include <linux/irq.h>
24#include <linux/clockchips.h>
25#include <linux/clocksource.h>
26#include <linux/clk.h>
27#include <linux/io.h>
28#include <linux/cnt32_to_63.h>
29
30#include <asm/mach/time.h>
Colin Cross2d5cd9a2010-01-28 16:41:42 -080031#include <asm/localtimer.h>
32
33#include <mach/iomap.h>
34#include <mach/irqs.h>
35
36#include "board.h"
37#include "clock.h"
38
39#define TIMERUS_CNTR_1US 0x10
40#define TIMERUS_USEC_CFG 0x14
41#define TIMERUS_CNTR_FREEZE 0x4c
42
43#define TIMER1_BASE 0x0
44#define TIMER2_BASE 0x8
45#define TIMER3_BASE 0x50
46#define TIMER4_BASE 0x58
47
48#define TIMER_PTV 0x0
49#define TIMER_PCR 0x4
50
51struct tegra_timer;
52
53static void __iomem *timer_reg_base = IO_ADDRESS(TEGRA_TMR1_BASE);
54
55#define timer_writel(value, reg) \
56 __raw_writel(value, (u32)timer_reg_base + (reg))
57#define timer_readl(reg) \
58 __raw_readl((u32)timer_reg_base + (reg))
59
60static int tegra_timer_set_next_event(unsigned long cycles,
61 struct clock_event_device *evt)
62{
63 u32 reg;
64
65 reg = 0x80000000 | ((cycles > 1) ? (cycles-1) : 0);
66 timer_writel(reg, TIMER3_BASE + TIMER_PTV);
67
68 return 0;
69}
70
71static void tegra_timer_set_mode(enum clock_event_mode mode,
72 struct clock_event_device *evt)
73{
74 u32 reg;
75
76 timer_writel(0, TIMER3_BASE + TIMER_PTV);
77
78 switch (mode) {
79 case CLOCK_EVT_MODE_PERIODIC:
80 reg = 0xC0000000 | ((1000000/HZ)-1);
81 timer_writel(reg, TIMER3_BASE + TIMER_PTV);
82 break;
83 case CLOCK_EVT_MODE_ONESHOT:
84 break;
85 case CLOCK_EVT_MODE_UNUSED:
86 case CLOCK_EVT_MODE_SHUTDOWN:
87 case CLOCK_EVT_MODE_RESUME:
88 break;
89 }
90}
91
92static cycle_t tegra_clocksource_read(struct clocksource *cs)
93{
Colin Cross684e94c2010-11-17 16:20:15 -080094 return timer_readl(TIMERUS_CNTR_1US);
Colin Cross2d5cd9a2010-01-28 16:41:42 -080095}
96
97static struct clock_event_device tegra_clockevent = {
98 .name = "timer0",
99 .rating = 300,
100 .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
101 .set_next_event = tegra_timer_set_next_event,
102 .set_mode = tegra_timer_set_mode,
103};
104
105static struct clocksource tegra_clocksource = {
106 .name = "timer_us",
107 .rating = 300,
108 .read = tegra_clocksource_read,
Colin Cross684e94c2010-11-17 16:20:15 -0800109 .mask = CLOCKSOURCE_MASK(32),
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800110 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
111};
112
113unsigned long long sched_clock(void)
114{
Colin Cross684e94c2010-11-17 16:20:15 -0800115 return cnt32_to_63(timer_readl(TIMERUS_CNTR_1US)) * 1000;
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800116}
117
118static irqreturn_t tegra_timer_interrupt(int irq, void *dev_id)
119{
120 struct clock_event_device *evt = (struct clock_event_device *)dev_id;
121 timer_writel(1<<30, TIMER3_BASE + TIMER_PCR);
122 evt->event_handler(evt);
123 return IRQ_HANDLED;
124}
125
126static struct irqaction tegra_timer_irq = {
127 .name = "timer0",
128 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_TRIGGER_HIGH,
129 .handler = tegra_timer_interrupt,
130 .dev_id = &tegra_clockevent,
131 .irq = INT_TMR3,
132};
133
134static void __init tegra_init_timer(void)
135{
136 unsigned long rate = clk_measure_input_freq();
137 int ret;
138
139#ifdef CONFIG_HAVE_ARM_TWD
140 twd_base = IO_ADDRESS(TEGRA_ARM_PERIF_BASE + 0x600);
141#endif
142
143 switch (rate) {
144 case 12000000:
145 timer_writel(0x000b, TIMERUS_USEC_CFG);
146 break;
147 case 13000000:
148 timer_writel(0x000c, TIMERUS_USEC_CFG);
149 break;
150 case 19200000:
151 timer_writel(0x045f, TIMERUS_USEC_CFG);
152 break;
153 case 26000000:
154 timer_writel(0x0019, TIMERUS_USEC_CFG);
155 break;
156 default:
157 WARN(1, "Unknown clock rate");
158 }
159
160 if (clocksource_register_hz(&tegra_clocksource, 1000000)) {
161 printk(KERN_ERR "Failed to register clocksource\n");
162 BUG();
163 }
164
165 ret = setup_irq(tegra_timer_irq.irq, &tegra_timer_irq);
166 if (ret) {
167 printk(KERN_ERR "Failed to register timer IRQ: %d\n", ret);
168 BUG();
169 }
170
171 clockevents_calc_mult_shift(&tegra_clockevent, 1000000, 5);
172 tegra_clockevent.max_delta_ns =
173 clockevent_delta2ns(0x1fffffff, &tegra_clockevent);
174 tegra_clockevent.min_delta_ns =
175 clockevent_delta2ns(0x1, &tegra_clockevent);
176 tegra_clockevent.cpumask = cpu_all_mask;
177 tegra_clockevent.irq = tegra_timer_irq.irq;
178 clockevents_register_device(&tegra_clockevent);
179
180 return;
181}
182
183struct sys_timer tegra_timer = {
184 .init = tegra_init_timer,
185};