blob: cf2ec85b95fa47b40f55ddc96c2718c6060d6576 [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 Lindgren11a01862011-03-29 15:54:49 -070072u32 sys_timer_reserved;
73
Tony Lindgrenaa561882011-03-29 15:54:48 -070074/* Clockevent code */
75
76static struct omap_dm_timer clkev;
Kevin Hilman5a3a3882007-11-12 23:24:02 -080077static struct clock_event_device clockevent_gpt;
Paul Walmsleyf2480762009-04-23 21:11:10 -060078static u8 __initdata gptimer_id = 1;
79static u8 __initdata inited;
Tony Lindgren1dbae812005-11-10 14:26:51 +000080
Linus Torvalds0cd61b62006-10-06 10:53:39 -070081static irqreturn_t omap2_gp_timer_interrupt(int irq, void *dev_id)
Tony Lindgren1dbae812005-11-10 14:26:51 +000082{
Kevin Hilman5a3a3882007-11-12 23:24:02 -080083 struct clock_event_device *evt = &clockevent_gpt;
Tony Lindgren1dbae812005-11-10 14:26:51 +000084
Tony Lindgrenaa561882011-03-29 15:54:48 -070085 __omap_dm_timer_write_status(clkev.io_base, OMAP_TIMER_INT_OVERFLOW);
Kevin Hilman5a3a3882007-11-12 23:24:02 -080086
87 evt->event_handler(evt);
Tony Lindgren1dbae812005-11-10 14:26:51 +000088 return IRQ_HANDLED;
89}
90
91static struct irqaction omap2_gp_timer_irq = {
92 .name = "gp timer",
Bernhard Walleb30faba2007-05-08 00:35:39 -070093 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
Tony Lindgren1dbae812005-11-10 14:26:51 +000094 .handler = omap2_gp_timer_interrupt,
95};
96
Kevin Hilman5a3a3882007-11-12 23:24:02 -080097static int omap2_gp_timer_set_next_event(unsigned long cycles,
98 struct clock_event_device *evt)
Tony Lindgren1dbae812005-11-10 14:26:51 +000099{
Tony Lindgrenaa561882011-03-29 15:54:48 -0700100 __omap_dm_timer_load_start(clkev.io_base, OMAP_TIMER_CTRL_ST,
101 0xffffffff - cycles, 1);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000102
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800103 return 0;
104}
105
106static void omap2_gp_timer_set_mode(enum clock_event_mode mode,
107 struct clock_event_device *evt)
108{
109 u32 period;
110
Tony Lindgrenaa561882011-03-29 15:54:48 -0700111 __omap_dm_timer_stop(clkev.io_base, 1, clkev.rate);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800112
113 switch (mode) {
114 case CLOCK_EVT_MODE_PERIODIC:
Tony Lindgrenaa561882011-03-29 15:54:48 -0700115 period = clkev.rate / HZ;
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800116 period -= 1;
Tony Lindgrenaa561882011-03-29 15:54:48 -0700117 /* Looks like we need to first set the load value separately */
118 __omap_dm_timer_write(clkev.io_base, OMAP_TIMER_LOAD_REG,
119 0xffffffff - period, 1);
120 __omap_dm_timer_load_start(clkev.io_base,
121 OMAP_TIMER_CTRL_AR | OMAP_TIMER_CTRL_ST,
122 0xffffffff - period, 1);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800123 break;
124 case CLOCK_EVT_MODE_ONESHOT:
125 break;
126 case CLOCK_EVT_MODE_UNUSED:
127 case CLOCK_EVT_MODE_SHUTDOWN:
128 case CLOCK_EVT_MODE_RESUME:
129 break;
130 }
131}
132
133static struct clock_event_device clockevent_gpt = {
134 .name = "gp timer",
135 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
136 .shift = 32,
137 .set_next_event = omap2_gp_timer_set_next_event,
138 .set_mode = omap2_gp_timer_set_mode,
139};
140
Paul Walmsleyf2480762009-04-23 21:11:10 -0600141/**
142 * omap2_gp_clockevent_set_gptimer - set which GPTIMER is used for clockevents
143 * @id: GPTIMER to use (1..MAX_GPTIMER_ID)
144 *
145 * Define the GPTIMER that the system should use for the tick timer.
146 * Meant to be called from board-*.c files in the event that GPTIMER1, the
147 * default, is unsuitable. Returns -EINVAL on error or 0 on success.
148 */
149int __init omap2_gp_clockevent_set_gptimer(u8 id)
150{
151 if (id < 1 || id > MAX_GPTIMER_ID)
152 return -EINVAL;
153
154 BUG_ON(inited);
155
156 gptimer_id = id;
157
158 return 0;
159}
160
Tony Lindgrenaa561882011-03-29 15:54:48 -0700161static int __init omap_dm_timer_init_one(struct omap_dm_timer *timer,
162 int gptimer_id,
163 const char *fck_source)
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800164{
Tony Lindgrenaa561882011-03-29 15:54:48 -0700165 char name[10]; /* 10 = sizeof("gptXX_Xck0") */
166 struct omap_hwmod *oh;
167 size_t size;
168 int res = 0;
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800169
Tony Lindgrenaa561882011-03-29 15:54:48 -0700170 sprintf(name, "timer%d", gptimer_id);
171 omap_hwmod_setup_one(name);
172 oh = omap_hwmod_lookup(name);
173 if (!oh)
174 return -ENODEV;
Paul Walmsleyf2480762009-04-23 21:11:10 -0600175
Tony Lindgrenaa561882011-03-29 15:54:48 -0700176 timer->irq = oh->mpu_irqs[0].irq;
177 timer->phys_base = oh->slaves[0]->addr->pa_start;
178 size = oh->slaves[0]->addr->pa_end - timer->phys_base;
179
180 /* Static mapping, never released */
181 timer->io_base = ioremap(timer->phys_base, size);
182 if (!timer->io_base)
183 return -ENXIO;
184
185 /* After the dmtimer is using hwmod these clocks won't be needed */
186 sprintf(name, "gpt%d_fck", gptimer_id);
187 timer->fclk = clk_get(NULL, name);
188 if (IS_ERR(timer->fclk))
189 return -ENODEV;
190
191 sprintf(name, "gpt%d_ick", gptimer_id);
192 timer->iclk = clk_get(NULL, name);
193 if (IS_ERR(timer->iclk)) {
194 clk_put(timer->fclk);
195 return -ENODEV;
196 }
197
198 omap_hwmod_enable(oh);
199
Tony Lindgren11a01862011-03-29 15:54:49 -0700200 sys_timer_reserved |= (1 << (gptimer_id - 1));
201
Tony Lindgrenaa561882011-03-29 15:54:48 -0700202 if (gptimer_id != 12) {
203 struct clk *src;
204
205 src = clk_get(NULL, fck_source);
206 if (IS_ERR(src)) {
207 res = -EINVAL;
208 } else {
209 res = __omap_dm_timer_set_source(timer->fclk, src);
210 if (IS_ERR_VALUE(res))
211 pr_warning("%s: timer%i cannot set source\n",
212 __func__, gptimer_id);
213 clk_put(src);
214 }
215 }
216 __omap_dm_timer_reset(timer->io_base, 1, 1);
217 timer->posted = 1;
218
219 timer->rate = clk_get_rate(timer->fclk);
220
221 timer->reserved = 1;
Paul Walmsley38698be2011-02-23 00:14:08 -0700222
Tony Lindgrenaa561882011-03-29 15:54:48 -0700223 return res;
224}
Paul Walmsleyf2480762009-04-23 21:11:10 -0600225
Tony Lindgrenaa561882011-03-29 15:54:48 -0700226static void __init omap2_gp_clockevent_init(int gptimer_id,
227 const char *fck_source)
228{
229 int res;
Paul Walmsleyf2480762009-04-23 21:11:10 -0600230
Tony Lindgrenaa561882011-03-29 15:54:48 -0700231 inited = 1;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000232
Tony Lindgrenaa561882011-03-29 15:54:48 -0700233 res = omap_dm_timer_init_one(&clkev, gptimer_id, fck_source);
234 BUG_ON(res);
Paul Walmsleyf2480762009-04-23 21:11:10 -0600235
Tony Lindgren98e182a2011-03-29 15:54:49 -0700236 omap2_gp_timer_irq.dev_id = (void *)&clkev;
Tony Lindgrenaa561882011-03-29 15:54:48 -0700237 setup_irq(clkev.irq, &omap2_gp_timer_irq);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800238
Tony Lindgrenaa561882011-03-29 15:54:48 -0700239 __omap_dm_timer_int_enable(clkev.io_base, OMAP_TIMER_INT_OVERFLOW);
240
241 clockevent_gpt.mult = div_sc(clkev.rate, NSEC_PER_SEC,
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800242 clockevent_gpt.shift);
243 clockevent_gpt.max_delta_ns =
244 clockevent_delta2ns(0xffffffff, &clockevent_gpt);
245 clockevent_gpt.min_delta_ns =
Aaro Koskinendf88acb2009-01-29 08:57:17 -0800246 clockevent_delta2ns(3, &clockevent_gpt);
247 /* Timer internal resynch latency. */
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800248
Rusty Russell320ab2b2008-12-13 21:20:26 +1030249 clockevent_gpt.cpumask = cpumask_of(0);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800250 clockevents_register_device(&clockevent_gpt);
Tony Lindgrenaa561882011-03-29 15:54:48 -0700251
252 pr_info("OMAP clockevent source: GPTIMER%d at %lu Hz\n",
253 gptimer_id, clkev.rate);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800254}
255
Paul Walmsleyf2480762009-04-23 21:11:10 -0600256/* Clocksource code */
257
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800258#ifdef CONFIG_OMAP_32K_TIMER
259/*
260 * When 32k-timer is enabled, don't use GPTimer for clocksource
261 * instead, just leave default clocksource which uses the 32k
Paul Walmsleyd8328f32011-01-15 21:32:01 -0700262 * sync counter. See clocksource setup in plat-omap/counter_32k.c
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800263 */
264
Paul Walmsleyd8328f32011-01-15 21:32:01 -0700265static void __init omap2_gp_clocksource_init(void)
266{
267 omap_init_clocksource_32k();
268}
269
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800270#else
271/*
272 * clocksource
273 */
Paul Walmsleycbc94382011-02-22 19:59:49 -0700274static DEFINE_CLOCK_DATA(cd);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800275static struct omap_dm_timer *gpt_clocksource;
Magnus Damm8e196082009-04-21 12:24:00 -0700276static cycle_t clocksource_read_cycles(struct clocksource *cs)
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800277{
278 return (cycle_t)omap_dm_timer_read_counter(gpt_clocksource);
279}
280
281static struct clocksource clocksource_gpt = {
282 .name = "gp timer",
283 .rating = 300,
284 .read = clocksource_read_cycles,
285 .mask = CLOCKSOURCE_MASK(32),
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800286 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
287};
288
Paul Walmsleycbc94382011-02-22 19:59:49 -0700289static void notrace dmtimer_update_sched_clock(void)
290{
291 u32 cyc;
292
293 cyc = omap_dm_timer_read_counter(gpt_clocksource);
294
295 update_sched_clock(&cd, cyc, (u32)~0);
296}
297
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800298/* Setup free-running counter for clocksource */
299static void __init omap2_gp_clocksource_init(void)
300{
301 static struct omap_dm_timer *gpt;
Aaro Koskinen28629452010-11-18 19:59:51 +0200302 u32 tick_rate;
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800303 static char err1[] __initdata = KERN_ERR
304 "%s: failed to request dm-timer\n";
305 static char err2[] __initdata = KERN_ERR
306 "%s: can't register clocksource!\n";
307
308 gpt = omap_dm_timer_request();
309 if (!gpt)
310 printk(err1, clocksource_gpt.name);
311 gpt_clocksource = gpt;
312
313 omap_dm_timer_set_source(gpt, OMAP_TIMER_SRC_SYS_CLK);
314 tick_rate = clk_get_rate(omap_dm_timer_get_fclk(gpt));
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800315
Richard Woodruff3fddd092008-07-03 12:24:30 +0300316 omap_dm_timer_set_load_start(gpt, 1, 0);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800317
Paul Walmsleycbc94382011-02-22 19:59:49 -0700318 init_sched_clock(&cd, dmtimer_update_sched_clock, 32, tick_rate);
319
Russell King8437c252010-12-13 13:18:44 +0000320 if (clocksource_register_hz(&clocksource_gpt, tick_rate))
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800321 printk(err2, clocksource_gpt.name);
322}
323#endif
324
Tony Lindgrenaa561882011-03-29 15:54:48 -0700325#define OMAP_SYS_TIMER_INIT(name, clkev_nr, clkev_src) \
Tony Lindgrene74984e2011-03-29 15:54:48 -0700326static void __init omap##name##_timer_init(void) \
327{ \
Tony Lindgrenaa561882011-03-29 15:54:48 -0700328 omap2_gp_clockevent_init((clkev_nr), clkev_src); \
Tony Lindgrene74984e2011-03-29 15:54:48 -0700329 omap2_gp_clocksource_init(); \
330}
331
332#define OMAP_SYS_TIMER(name) \
333struct sys_timer omap##name##_timer = { \
334 .init = omap##name##_timer_init, \
335};
336
337#ifdef CONFIG_ARCH_OMAP2
Tony Lindgrenaa561882011-03-29 15:54:48 -0700338OMAP_SYS_TIMER_INIT(2, 1, OMAP2_CLKEV_SOURCE)
Tony Lindgrene74984e2011-03-29 15:54:48 -0700339OMAP_SYS_TIMER(2)
340#endif
341
342#ifdef CONFIG_ARCH_OMAP3
Tony Lindgrenaa561882011-03-29 15:54:48 -0700343OMAP_SYS_TIMER_INIT(3, 1, OMAP3_CLKEV_SOURCE)
Tony Lindgrene74984e2011-03-29 15:54:48 -0700344OMAP_SYS_TIMER(3)
Tony Lindgrenaa561882011-03-29 15:54:48 -0700345OMAP_SYS_TIMER_INIT(3_secure, OMAP3_SECURE_TIMER, OMAP3_CLKEV_SOURCE)
Tony Lindgrene74984e2011-03-29 15:54:48 -0700346OMAP_SYS_TIMER(3_secure)
347#endif
348
349#ifdef CONFIG_ARCH_OMAP4
350static void __init omap4_timer_init(void)
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800351{
Santosh Shilimkar39e1d4c2009-04-28 20:52:00 +0530352#ifdef CONFIG_LOCAL_TIMERS
Tony Lindgrene74984e2011-03-29 15:54:48 -0700353 twd_base = ioremap(OMAP44XX_LOCAL_TWD_BASE, SZ_256);
354 BUG_ON(!twd_base);
Santosh Shilimkar39e1d4c2009-04-28 20:52:00 +0530355#endif
Tony Lindgrenaa561882011-03-29 15:54:48 -0700356 omap2_gp_clockevent_init(1, OMAP4_CLKEV_SOURCE);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800357 omap2_gp_clocksource_init();
Tony Lindgren1dbae812005-11-10 14:26:51 +0000358}
Tony Lindgrene74984e2011-03-29 15:54:48 -0700359OMAP_SYS_TIMER(4)
360#endif