blob: 578e9df4fdea5c5faa412635c1ed2040acdd1072 [file] [log] [blame]
Tony Lindgren1dbae812005-11-10 14:26:51 +00001/*
2 * linux/arch/arm/mach-omap2/timer-gp.c
3 *
4 * OMAP2 GP timer support.
5 *
Paul Walmsleyf2480762009-04-23 21:11:10 -06006 * Copyright (C) 2009 Nokia Corporation
7 *
Kevin Hilman5a3a3882007-11-12 23:24:02 -08008 * Update to use new clocksource/clockevent layers
9 * Author: Kevin Hilman, MontaVista Software, Inc. <source@mvista.com>
10 * Copyright (C) 2007 MontaVista Software, Inc.
11 *
12 * Original driver:
Tony Lindgren1dbae812005-11-10 14:26:51 +000013 * Copyright (C) 2005 Nokia Corporation
14 * Author: Paul Mundt <paul.mundt@nokia.com>
Jan Engelhardt96de0e22007-10-19 23:21:04 +020015 * Juha Yrjölä <juha.yrjola@nokia.com>
Timo Teras77900a22006-06-26 16:16:12 -070016 * OMAP Dual-mode timer framework support by Timo Teras
Tony Lindgren1dbae812005-11-10 14:26:51 +000017 *
18 * Some parts based off of TI's 24xx code:
19 *
Santosh Shilimkar44169072009-05-28 14:16:04 -070020 * Copyright (C) 2004-2009 Texas Instruments, Inc.
Tony Lindgren1dbae812005-11-10 14:26:51 +000021 *
22 * Roughly modelled after the OMAP1 MPU timer code.
Santosh Shilimkar44169072009-05-28 14:16:04 -070023 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
Tony Lindgren1dbae812005-11-10 14:26:51 +000024 *
25 * This file is subject to the terms and conditions of the GNU General Public
26 * License. See the file "COPYING" in the main directory of this archive
27 * for more details.
28 */
29#include <linux/init.h>
30#include <linux/time.h>
31#include <linux/interrupt.h>
32#include <linux/err.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000033#include <linux/clk.h>
Timo Teras77900a22006-06-26 16:16:12 -070034#include <linux/delay.h>
Dirk Behmee6687292006-12-06 17:14:00 -080035#include <linux/irq.h>
Kevin Hilman5a3a3882007-11-12 23:24:02 -080036#include <linux/clocksource.h>
37#include <linux/clockchips.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000038
Tony Lindgren1dbae812005-11-10 14:26:51 +000039#include <asm/mach/time.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070040#include <plat/dmtimer.h>
Santosh Shilimkar39e1d4c2009-04-28 20:52:00 +053041#include <asm/localtimer.h>
Paul Walmsleycbc94382011-02-22 19:59:49 -070042#include <asm/sched_clock.h>
Paul Walmsley38698be2011-02-23 00:14:08 -070043#include <plat/common.h>
44#include <plat/omap_hwmod.h>
Tony Lindgren1dbae812005-11-10 14:26:51 +000045
Manjunath Kondaiah G04aeae72010-10-08 09:58:35 -070046#include "timer-gp.h"
47
Tony Lindgrenaa561882011-03-29 15:54:48 -070048/* Parent clocks, eventually these will come from the clock framework */
49
50#define OMAP2_MPU_SOURCE "sys_ck"
51#define OMAP3_MPU_SOURCE OMAP2_MPU_SOURCE
52#define OMAP4_MPU_SOURCE "sys_clkin_ck"
53#define OMAP2_32K_SOURCE "func_32k_ck"
54#define OMAP3_32K_SOURCE "omap_32k_fck"
55#define OMAP4_32K_SOURCE "sys_32k_ck"
56
57#ifdef CONFIG_OMAP_32K_TIMER
58#define OMAP2_CLKEV_SOURCE OMAP2_32K_SOURCE
59#define OMAP3_CLKEV_SOURCE OMAP3_32K_SOURCE
60#define OMAP4_CLKEV_SOURCE OMAP4_32K_SOURCE
61#define OMAP3_SECURE_TIMER 12
62#else
63#define OMAP2_CLKEV_SOURCE OMAP2_MPU_SOURCE
64#define OMAP3_CLKEV_SOURCE OMAP3_MPU_SOURCE
65#define OMAP4_CLKEV_SOURCE OMAP4_MPU_SOURCE
66#define OMAP3_SECURE_TIMER 1
67#endif
Paul Walmsleyd8328f32011-01-15 21:32:01 -070068
Paul Walmsleyf2480762009-04-23 21:11:10 -060069/* MAX_GPTIMER_ID: number of GPTIMERs on the chip */
70#define MAX_GPTIMER_ID 12
71
Tony Lindgrenaa561882011-03-29 15:54:48 -070072/* Clockevent code */
73
74static struct omap_dm_timer clkev;
Kevin Hilman5a3a3882007-11-12 23:24:02 -080075static struct clock_event_device clockevent_gpt;
Paul Walmsleyf2480762009-04-23 21:11:10 -060076static u8 __initdata gptimer_id = 1;
77static u8 __initdata inited;
Tony Lindgren1dbae812005-11-10 14:26:51 +000078
Linus Torvalds0cd61b62006-10-06 10:53:39 -070079static irqreturn_t omap2_gp_timer_interrupt(int irq, void *dev_id)
Tony Lindgren1dbae812005-11-10 14:26:51 +000080{
Kevin Hilman5a3a3882007-11-12 23:24:02 -080081 struct clock_event_device *evt = &clockevent_gpt;
Tony Lindgren1dbae812005-11-10 14:26:51 +000082
Tony Lindgrenaa561882011-03-29 15:54:48 -070083 __omap_dm_timer_write_status(clkev.io_base, OMAP_TIMER_INT_OVERFLOW);
Kevin Hilman5a3a3882007-11-12 23:24:02 -080084
85 evt->event_handler(evt);
Tony Lindgren1dbae812005-11-10 14:26:51 +000086 return IRQ_HANDLED;
87}
88
89static struct irqaction omap2_gp_timer_irq = {
90 .name = "gp timer",
Bernhard Walleb30faba2007-05-08 00:35:39 -070091 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
Tony Lindgren1dbae812005-11-10 14:26:51 +000092 .handler = omap2_gp_timer_interrupt,
93};
94
Kevin Hilman5a3a3882007-11-12 23:24:02 -080095static int omap2_gp_timer_set_next_event(unsigned long cycles,
96 struct clock_event_device *evt)
Tony Lindgren1dbae812005-11-10 14:26:51 +000097{
Tony Lindgrenaa561882011-03-29 15:54:48 -070098 __omap_dm_timer_load_start(clkev.io_base, OMAP_TIMER_CTRL_ST,
99 0xffffffff - cycles, 1);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000100
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800101 return 0;
102}
103
104static void omap2_gp_timer_set_mode(enum clock_event_mode mode,
105 struct clock_event_device *evt)
106{
107 u32 period;
108
Tony Lindgrenaa561882011-03-29 15:54:48 -0700109 __omap_dm_timer_stop(clkev.io_base, 1, clkev.rate);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800110
111 switch (mode) {
112 case CLOCK_EVT_MODE_PERIODIC:
Tony Lindgrenaa561882011-03-29 15:54:48 -0700113 period = clkev.rate / HZ;
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800114 period -= 1;
Tony Lindgrenaa561882011-03-29 15:54:48 -0700115 /* Looks like we need to first set the load value separately */
116 __omap_dm_timer_write(clkev.io_base, OMAP_TIMER_LOAD_REG,
117 0xffffffff - period, 1);
118 __omap_dm_timer_load_start(clkev.io_base,
119 OMAP_TIMER_CTRL_AR | OMAP_TIMER_CTRL_ST,
120 0xffffffff - period, 1);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800121 break;
122 case CLOCK_EVT_MODE_ONESHOT:
123 break;
124 case CLOCK_EVT_MODE_UNUSED:
125 case CLOCK_EVT_MODE_SHUTDOWN:
126 case CLOCK_EVT_MODE_RESUME:
127 break;
128 }
129}
130
131static struct clock_event_device clockevent_gpt = {
132 .name = "gp timer",
133 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
134 .shift = 32,
135 .set_next_event = omap2_gp_timer_set_next_event,
136 .set_mode = omap2_gp_timer_set_mode,
137};
138
Paul Walmsleyf2480762009-04-23 21:11:10 -0600139/**
140 * omap2_gp_clockevent_set_gptimer - set which GPTIMER is used for clockevents
141 * @id: GPTIMER to use (1..MAX_GPTIMER_ID)
142 *
143 * Define the GPTIMER that the system should use for the tick timer.
144 * Meant to be called from board-*.c files in the event that GPTIMER1, the
145 * default, is unsuitable. Returns -EINVAL on error or 0 on success.
146 */
147int __init omap2_gp_clockevent_set_gptimer(u8 id)
148{
149 if (id < 1 || id > MAX_GPTIMER_ID)
150 return -EINVAL;
151
152 BUG_ON(inited);
153
154 gptimer_id = id;
155
156 return 0;
157}
158
Tony Lindgrenaa561882011-03-29 15:54:48 -0700159static int __init omap_dm_timer_init_one(struct omap_dm_timer *timer,
160 int gptimer_id,
161 const char *fck_source)
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800162{
Tony Lindgrenaa561882011-03-29 15:54:48 -0700163 char name[10]; /* 10 = sizeof("gptXX_Xck0") */
164 struct omap_hwmod *oh;
165 size_t size;
166 int res = 0;
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800167
Tony Lindgrenaa561882011-03-29 15:54:48 -0700168 sprintf(name, "timer%d", gptimer_id);
169 omap_hwmod_setup_one(name);
170 oh = omap_hwmod_lookup(name);
171 if (!oh)
172 return -ENODEV;
Paul Walmsleyf2480762009-04-23 21:11:10 -0600173
Tony Lindgrenaa561882011-03-29 15:54:48 -0700174 timer->irq = oh->mpu_irqs[0].irq;
175 timer->phys_base = oh->slaves[0]->addr->pa_start;
176 size = oh->slaves[0]->addr->pa_end - timer->phys_base;
177
178 /* Static mapping, never released */
179 timer->io_base = ioremap(timer->phys_base, size);
180 if (!timer->io_base)
181 return -ENXIO;
182
183 /* After the dmtimer is using hwmod these clocks won't be needed */
184 sprintf(name, "gpt%d_fck", gptimer_id);
185 timer->fclk = clk_get(NULL, name);
186 if (IS_ERR(timer->fclk))
187 return -ENODEV;
188
189 sprintf(name, "gpt%d_ick", gptimer_id);
190 timer->iclk = clk_get(NULL, name);
191 if (IS_ERR(timer->iclk)) {
192 clk_put(timer->fclk);
193 return -ENODEV;
194 }
195
196 omap_hwmod_enable(oh);
197
198 if (gptimer_id != 12) {
199 struct clk *src;
200
201 src = clk_get(NULL, fck_source);
202 if (IS_ERR(src)) {
203 res = -EINVAL;
204 } else {
205 res = __omap_dm_timer_set_source(timer->fclk, src);
206 if (IS_ERR_VALUE(res))
207 pr_warning("%s: timer%i cannot set source\n",
208 __func__, gptimer_id);
209 clk_put(src);
210 }
211 }
212 __omap_dm_timer_reset(timer->io_base, 1, 1);
213 timer->posted = 1;
214
215 timer->rate = clk_get_rate(timer->fclk);
216
217 timer->reserved = 1;
Paul Walmsley38698be2011-02-23 00:14:08 -0700218
Tony Lindgrenaa561882011-03-29 15:54:48 -0700219 return res;
220}
Paul Walmsleyf2480762009-04-23 21:11:10 -0600221
Tony Lindgrenaa561882011-03-29 15:54:48 -0700222static void __init omap2_gp_clockevent_init(int gptimer_id,
223 const char *fck_source)
224{
225 int res;
Paul Walmsleyf2480762009-04-23 21:11:10 -0600226
Tony Lindgrenaa561882011-03-29 15:54:48 -0700227 inited = 1;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000228
Tony Lindgrenaa561882011-03-29 15:54:48 -0700229 res = omap_dm_timer_init_one(&clkev, gptimer_id, fck_source);
230 BUG_ON(res);
Paul Walmsleyf2480762009-04-23 21:11:10 -0600231
Tony Lindgren98e182a2011-03-29 15:54:49 -0700232 omap2_gp_timer_irq.dev_id = (void *)&clkev;
Tony Lindgrenaa561882011-03-29 15:54:48 -0700233 setup_irq(clkev.irq, &omap2_gp_timer_irq);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800234
Tony Lindgrenaa561882011-03-29 15:54:48 -0700235 __omap_dm_timer_int_enable(clkev.io_base, OMAP_TIMER_INT_OVERFLOW);
236
237 clockevent_gpt.mult = div_sc(clkev.rate, NSEC_PER_SEC,
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800238 clockevent_gpt.shift);
239 clockevent_gpt.max_delta_ns =
240 clockevent_delta2ns(0xffffffff, &clockevent_gpt);
241 clockevent_gpt.min_delta_ns =
Aaro Koskinendf88acb2009-01-29 08:57:17 -0800242 clockevent_delta2ns(3, &clockevent_gpt);
243 /* Timer internal resynch latency. */
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800244
Rusty Russell320ab2b2008-12-13 21:20:26 +1030245 clockevent_gpt.cpumask = cpumask_of(0);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800246 clockevents_register_device(&clockevent_gpt);
Tony Lindgrenaa561882011-03-29 15:54:48 -0700247
248 pr_info("OMAP clockevent source: GPTIMER%d at %lu Hz\n",
249 gptimer_id, clkev.rate);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800250}
251
Paul Walmsleyf2480762009-04-23 21:11:10 -0600252/* Clocksource code */
253
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800254#ifdef CONFIG_OMAP_32K_TIMER
255/*
256 * When 32k-timer is enabled, don't use GPTimer for clocksource
257 * instead, just leave default clocksource which uses the 32k
Paul Walmsleyd8328f32011-01-15 21:32:01 -0700258 * sync counter. See clocksource setup in plat-omap/counter_32k.c
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800259 */
260
Paul Walmsleyd8328f32011-01-15 21:32:01 -0700261static void __init omap2_gp_clocksource_init(void)
262{
263 omap_init_clocksource_32k();
264}
265
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800266#else
267/*
268 * clocksource
269 */
Paul Walmsleycbc94382011-02-22 19:59:49 -0700270static DEFINE_CLOCK_DATA(cd);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800271static struct omap_dm_timer *gpt_clocksource;
Magnus Damm8e196082009-04-21 12:24:00 -0700272static cycle_t clocksource_read_cycles(struct clocksource *cs)
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800273{
274 return (cycle_t)omap_dm_timer_read_counter(gpt_clocksource);
275}
276
277static struct clocksource clocksource_gpt = {
278 .name = "gp timer",
279 .rating = 300,
280 .read = clocksource_read_cycles,
281 .mask = CLOCKSOURCE_MASK(32),
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800282 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
283};
284
Paul Walmsleycbc94382011-02-22 19:59:49 -0700285static void notrace dmtimer_update_sched_clock(void)
286{
287 u32 cyc;
288
289 cyc = omap_dm_timer_read_counter(gpt_clocksource);
290
291 update_sched_clock(&cd, cyc, (u32)~0);
292}
293
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800294/* Setup free-running counter for clocksource */
295static void __init omap2_gp_clocksource_init(void)
296{
297 static struct omap_dm_timer *gpt;
Aaro Koskinen28629452010-11-18 19:59:51 +0200298 u32 tick_rate;
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800299 static char err1[] __initdata = KERN_ERR
300 "%s: failed to request dm-timer\n";
301 static char err2[] __initdata = KERN_ERR
302 "%s: can't register clocksource!\n";
303
304 gpt = omap_dm_timer_request();
305 if (!gpt)
306 printk(err1, clocksource_gpt.name);
307 gpt_clocksource = gpt;
308
309 omap_dm_timer_set_source(gpt, OMAP_TIMER_SRC_SYS_CLK);
310 tick_rate = clk_get_rate(omap_dm_timer_get_fclk(gpt));
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800311
Richard Woodruff3fddd092008-07-03 12:24:30 +0300312 omap_dm_timer_set_load_start(gpt, 1, 0);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800313
Paul Walmsleycbc94382011-02-22 19:59:49 -0700314 init_sched_clock(&cd, dmtimer_update_sched_clock, 32, tick_rate);
315
Russell King8437c252010-12-13 13:18:44 +0000316 if (clocksource_register_hz(&clocksource_gpt, tick_rate))
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800317 printk(err2, clocksource_gpt.name);
318}
319#endif
320
Tony Lindgrenaa561882011-03-29 15:54:48 -0700321#define OMAP_SYS_TIMER_INIT(name, clkev_nr, clkev_src) \
Tony Lindgrene74984e2011-03-29 15:54:48 -0700322static void __init omap##name##_timer_init(void) \
323{ \
324 omap_dm_timer_init(); \
Tony Lindgrenaa561882011-03-29 15:54:48 -0700325 omap2_gp_clockevent_init((clkev_nr), clkev_src); \
Tony Lindgrene74984e2011-03-29 15:54:48 -0700326 omap2_gp_clocksource_init(); \
327}
328
329#define OMAP_SYS_TIMER(name) \
330struct sys_timer omap##name##_timer = { \
331 .init = omap##name##_timer_init, \
332};
333
334#ifdef CONFIG_ARCH_OMAP2
Tony Lindgrenaa561882011-03-29 15:54:48 -0700335OMAP_SYS_TIMER_INIT(2, 1, OMAP2_CLKEV_SOURCE)
Tony Lindgrene74984e2011-03-29 15:54:48 -0700336OMAP_SYS_TIMER(2)
337#endif
338
339#ifdef CONFIG_ARCH_OMAP3
Tony Lindgrenaa561882011-03-29 15:54:48 -0700340OMAP_SYS_TIMER_INIT(3, 1, OMAP3_CLKEV_SOURCE)
Tony Lindgrene74984e2011-03-29 15:54:48 -0700341OMAP_SYS_TIMER(3)
Tony Lindgrenaa561882011-03-29 15:54:48 -0700342OMAP_SYS_TIMER_INIT(3_secure, OMAP3_SECURE_TIMER, OMAP3_CLKEV_SOURCE)
Tony Lindgrene74984e2011-03-29 15:54:48 -0700343OMAP_SYS_TIMER(3_secure)
344#endif
345
346#ifdef CONFIG_ARCH_OMAP4
347static void __init omap4_timer_init(void)
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800348{
Santosh Shilimkar39e1d4c2009-04-28 20:52:00 +0530349#ifdef CONFIG_LOCAL_TIMERS
Tony Lindgrene74984e2011-03-29 15:54:48 -0700350 twd_base = ioremap(OMAP44XX_LOCAL_TWD_BASE, SZ_256);
351 BUG_ON(!twd_base);
Santosh Shilimkar39e1d4c2009-04-28 20:52:00 +0530352#endif
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800353 omap_dm_timer_init();
Tony Lindgrenaa561882011-03-29 15:54:48 -0700354 omap2_gp_clockevent_init(1, OMAP4_CLKEV_SOURCE);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800355 omap2_gp_clocksource_init();
Tony Lindgren1dbae812005-11-10 14:26:51 +0000356}
Tony Lindgrene74984e2011-03-29 15:54:48 -0700357OMAP_SYS_TIMER(4)
358#endif