blob: 0fc550e7e4825a04093a4e1980b1bd169f8cd893 [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>
Tony Lindgren1dbae812005-11-10 14:26:51 +000043
Manjunath Kondaiah G04aeae72010-10-08 09:58:35 -070044#include "timer-gp.h"
45
Paul Walmsleyd8328f32011-01-15 21:32:01 -070046#include <plat/common.h>
47
Paul Walmsleyf2480762009-04-23 21:11:10 -060048/* MAX_GPTIMER_ID: number of GPTIMERs on the chip */
49#define MAX_GPTIMER_ID 12
50
Timo Teras77900a22006-06-26 16:16:12 -070051static struct omap_dm_timer *gptimer;
Kevin Hilman5a3a3882007-11-12 23:24:02 -080052static struct clock_event_device clockevent_gpt;
Paul Walmsleyf2480762009-04-23 21:11:10 -060053static u8 __initdata gptimer_id = 1;
54static u8 __initdata inited;
Kevin Hilmand7814e42009-10-06 14:30:23 -070055struct omap_dm_timer *gptimer_wakeup;
Tony Lindgren1dbae812005-11-10 14:26:51 +000056
Linus Torvalds0cd61b62006-10-06 10:53:39 -070057static irqreturn_t omap2_gp_timer_interrupt(int irq, void *dev_id)
Tony Lindgren1dbae812005-11-10 14:26:51 +000058{
Kevin Hilman5a3a3882007-11-12 23:24:02 -080059 struct omap_dm_timer *gpt = (struct omap_dm_timer *)dev_id;
60 struct clock_event_device *evt = &clockevent_gpt;
Tony Lindgren1dbae812005-11-10 14:26:51 +000061
Kevin Hilman5a3a3882007-11-12 23:24:02 -080062 omap_dm_timer_write_status(gpt, OMAP_TIMER_INT_OVERFLOW);
63
64 evt->event_handler(evt);
Tony Lindgren1dbae812005-11-10 14:26:51 +000065 return IRQ_HANDLED;
66}
67
68static struct irqaction omap2_gp_timer_irq = {
69 .name = "gp timer",
Bernhard Walleb30faba2007-05-08 00:35:39 -070070 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
Tony Lindgren1dbae812005-11-10 14:26:51 +000071 .handler = omap2_gp_timer_interrupt,
72};
73
Kevin Hilman5a3a3882007-11-12 23:24:02 -080074static int omap2_gp_timer_set_next_event(unsigned long cycles,
75 struct clock_event_device *evt)
Tony Lindgren1dbae812005-11-10 14:26:51 +000076{
Richard Woodruff3fddd092008-07-03 12:24:30 +030077 omap_dm_timer_set_load_start(gptimer, 0, 0xffffffff - cycles);
Tony Lindgren1dbae812005-11-10 14:26:51 +000078
Kevin Hilman5a3a3882007-11-12 23:24:02 -080079 return 0;
80}
81
82static void omap2_gp_timer_set_mode(enum clock_event_mode mode,
83 struct clock_event_device *evt)
84{
85 u32 period;
86
87 omap_dm_timer_stop(gptimer);
88
89 switch (mode) {
90 case CLOCK_EVT_MODE_PERIODIC:
91 period = clk_get_rate(omap_dm_timer_get_fclk(gptimer)) / HZ;
92 period -= 1;
Richard Woodruff3fddd092008-07-03 12:24:30 +030093 omap_dm_timer_set_load_start(gptimer, 1, 0xffffffff - period);
Kevin Hilman5a3a3882007-11-12 23:24:02 -080094 break;
95 case CLOCK_EVT_MODE_ONESHOT:
96 break;
97 case CLOCK_EVT_MODE_UNUSED:
98 case CLOCK_EVT_MODE_SHUTDOWN:
99 case CLOCK_EVT_MODE_RESUME:
100 break;
101 }
102}
103
104static struct clock_event_device clockevent_gpt = {
105 .name = "gp timer",
106 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
107 .shift = 32,
108 .set_next_event = omap2_gp_timer_set_next_event,
109 .set_mode = omap2_gp_timer_set_mode,
110};
111
Paul Walmsleyf2480762009-04-23 21:11:10 -0600112/**
113 * omap2_gp_clockevent_set_gptimer - set which GPTIMER is used for clockevents
114 * @id: GPTIMER to use (1..MAX_GPTIMER_ID)
115 *
116 * Define the GPTIMER that the system should use for the tick timer.
117 * Meant to be called from board-*.c files in the event that GPTIMER1, the
118 * default, is unsuitable. Returns -EINVAL on error or 0 on success.
119 */
120int __init omap2_gp_clockevent_set_gptimer(u8 id)
121{
122 if (id < 1 || id > MAX_GPTIMER_ID)
123 return -EINVAL;
124
125 BUG_ON(inited);
126
127 gptimer_id = id;
128
129 return 0;
130}
131
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800132static void __init omap2_gp_clockevent_init(void)
133{
134 u32 tick_rate;
Paul Walmsleyf2480762009-04-23 21:11:10 -0600135 int src;
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800136
Paul Walmsleyf2480762009-04-23 21:11:10 -0600137 inited = 1;
138
139 gptimer = omap_dm_timer_request_specific(gptimer_id);
Timo Teras77900a22006-06-26 16:16:12 -0700140 BUG_ON(gptimer == NULL);
Kevin Hilmand7814e42009-10-06 14:30:23 -0700141 gptimer_wakeup = gptimer;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000142
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800143#if defined(CONFIG_OMAP_32K_TIMER)
Paul Walmsleyf2480762009-04-23 21:11:10 -0600144 src = OMAP_TIMER_SRC_32_KHZ;
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800145#else
Paul Walmsleyf2480762009-04-23 21:11:10 -0600146 src = OMAP_TIMER_SRC_SYS_CLK;
147 WARN(gptimer_id == 12, "WARNING: GPTIMER12 can only use the "
148 "secure 32KiHz clock source\n");
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800149#endif
Paul Walmsleyf2480762009-04-23 21:11:10 -0600150
151 if (gptimer_id != 12)
152 WARN(IS_ERR_VALUE(omap_dm_timer_set_source(gptimer, src)),
153 "timer-gp: omap_dm_timer_set_source() failed\n");
154
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800155 tick_rate = clk_get_rate(omap_dm_timer_get_fclk(gptimer));
Tony Lindgren1dbae812005-11-10 14:26:51 +0000156
Paul Walmsleyf2480762009-04-23 21:11:10 -0600157 pr_info("OMAP clockevent source: GPTIMER%d at %u Hz\n",
158 gptimer_id, tick_rate);
159
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800160 omap2_gp_timer_irq.dev_id = (void *)gptimer;
Timo Teras77900a22006-06-26 16:16:12 -0700161 setup_irq(omap_dm_timer_get_irq(gptimer), &omap2_gp_timer_irq);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800162 omap_dm_timer_set_int_enable(gptimer, OMAP_TIMER_INT_OVERFLOW);
163
164 clockevent_gpt.mult = div_sc(tick_rate, NSEC_PER_SEC,
165 clockevent_gpt.shift);
166 clockevent_gpt.max_delta_ns =
167 clockevent_delta2ns(0xffffffff, &clockevent_gpt);
168 clockevent_gpt.min_delta_ns =
Aaro Koskinendf88acb2009-01-29 08:57:17 -0800169 clockevent_delta2ns(3, &clockevent_gpt);
170 /* Timer internal resynch latency. */
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800171
Rusty Russell320ab2b2008-12-13 21:20:26 +1030172 clockevent_gpt.cpumask = cpumask_of(0);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800173 clockevents_register_device(&clockevent_gpt);
174}
175
Paul Walmsleyf2480762009-04-23 21:11:10 -0600176/* Clocksource code */
177
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800178#ifdef CONFIG_OMAP_32K_TIMER
179/*
180 * When 32k-timer is enabled, don't use GPTimer for clocksource
181 * instead, just leave default clocksource which uses the 32k
Paul Walmsleyd8328f32011-01-15 21:32:01 -0700182 * sync counter. See clocksource setup in plat-omap/counter_32k.c
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800183 */
184
Paul Walmsleyd8328f32011-01-15 21:32:01 -0700185static void __init omap2_gp_clocksource_init(void)
186{
187 omap_init_clocksource_32k();
188}
189
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800190#else
191/*
192 * clocksource
193 */
Paul Walmsleycbc94382011-02-22 19:59:49 -0700194static DEFINE_CLOCK_DATA(cd);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800195static struct omap_dm_timer *gpt_clocksource;
Magnus Damm8e196082009-04-21 12:24:00 -0700196static cycle_t clocksource_read_cycles(struct clocksource *cs)
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800197{
198 return (cycle_t)omap_dm_timer_read_counter(gpt_clocksource);
199}
200
201static struct clocksource clocksource_gpt = {
202 .name = "gp timer",
203 .rating = 300,
204 .read = clocksource_read_cycles,
205 .mask = CLOCKSOURCE_MASK(32),
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800206 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
207};
208
Paul Walmsleycbc94382011-02-22 19:59:49 -0700209static void notrace dmtimer_update_sched_clock(void)
210{
211 u32 cyc;
212
213 cyc = omap_dm_timer_read_counter(gpt_clocksource);
214
215 update_sched_clock(&cd, cyc, (u32)~0);
216}
217
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800218/* Setup free-running counter for clocksource */
219static void __init omap2_gp_clocksource_init(void)
220{
221 static struct omap_dm_timer *gpt;
Aaro Koskinen28629452010-11-18 19:59:51 +0200222 u32 tick_rate;
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800223 static char err1[] __initdata = KERN_ERR
224 "%s: failed to request dm-timer\n";
225 static char err2[] __initdata = KERN_ERR
226 "%s: can't register clocksource!\n";
227
228 gpt = omap_dm_timer_request();
229 if (!gpt)
230 printk(err1, clocksource_gpt.name);
231 gpt_clocksource = gpt;
232
233 omap_dm_timer_set_source(gpt, OMAP_TIMER_SRC_SYS_CLK);
234 tick_rate = clk_get_rate(omap_dm_timer_get_fclk(gpt));
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800235
Richard Woodruff3fddd092008-07-03 12:24:30 +0300236 omap_dm_timer_set_load_start(gpt, 1, 0);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800237
Paul Walmsleycbc94382011-02-22 19:59:49 -0700238 init_sched_clock(&cd, dmtimer_update_sched_clock, 32, tick_rate);
239
Russell King8437c252010-12-13 13:18:44 +0000240 if (clocksource_register_hz(&clocksource_gpt, tick_rate))
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800241 printk(err2, clocksource_gpt.name);
242}
243#endif
244
245static void __init omap2_gp_timer_init(void)
246{
Santosh Shilimkar39e1d4c2009-04-28 20:52:00 +0530247#ifdef CONFIG_LOCAL_TIMERS
Tony Lindgrenc3083412010-09-20 14:53:15 -0700248 if (cpu_is_omap44xx()) {
249 twd_base = ioremap(OMAP44XX_LOCAL_TWD_BASE, SZ_256);
250 BUG_ON(!twd_base);
251 }
Santosh Shilimkar39e1d4c2009-04-28 20:52:00 +0530252#endif
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800253 omap_dm_timer_init();
254
255 omap2_gp_clockevent_init();
256 omap2_gp_clocksource_init();
Tony Lindgren1dbae812005-11-10 14:26:51 +0000257}
258
259struct sys_timer omap_timer = {
260 .init = omap2_gp_timer_init,
261};