blob: cf1de7d2630d69ebfcefee911670bdd94110f305 [file] [log] [blame]
Tony Lindgren1dbae812005-11-10 14:26:51 +00001/*
Tony Lindgren0f622e82011-03-29 15:54:50 -07002 * linux/arch/arm/mach-omap2/timer.c
Tony Lindgren1dbae812005-11-10 14:26:51 +00003 *
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
Tony Lindgrenaa561882011-03-29 15:54:48 -070046/* Parent clocks, eventually these will come from the clock framework */
47
48#define OMAP2_MPU_SOURCE "sys_ck"
49#define OMAP3_MPU_SOURCE OMAP2_MPU_SOURCE
50#define OMAP4_MPU_SOURCE "sys_clkin_ck"
51#define OMAP2_32K_SOURCE "func_32k_ck"
52#define OMAP3_32K_SOURCE "omap_32k_fck"
53#define OMAP4_32K_SOURCE "sys_32k_ck"
54
55#ifdef CONFIG_OMAP_32K_TIMER
56#define OMAP2_CLKEV_SOURCE OMAP2_32K_SOURCE
57#define OMAP3_CLKEV_SOURCE OMAP3_32K_SOURCE
58#define OMAP4_CLKEV_SOURCE OMAP4_32K_SOURCE
59#define OMAP3_SECURE_TIMER 12
60#else
61#define OMAP2_CLKEV_SOURCE OMAP2_MPU_SOURCE
62#define OMAP3_CLKEV_SOURCE OMAP3_MPU_SOURCE
63#define OMAP4_CLKEV_SOURCE OMAP4_MPU_SOURCE
64#define OMAP3_SECURE_TIMER 1
65#endif
Paul Walmsleyd8328f32011-01-15 21:32:01 -070066
Paul Walmsleyf2480762009-04-23 21:11:10 -060067/* MAX_GPTIMER_ID: number of GPTIMERs on the chip */
68#define MAX_GPTIMER_ID 12
69
Tony Lindgren11a01862011-03-29 15:54:49 -070070u32 sys_timer_reserved;
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;
Tony Lindgren1dbae812005-11-10 14:26:51 +000076
Linus Torvalds0cd61b62006-10-06 10:53:39 -070077static irqreturn_t omap2_gp_timer_interrupt(int irq, void *dev_id)
Tony Lindgren1dbae812005-11-10 14:26:51 +000078{
Kevin Hilman5a3a3882007-11-12 23:24:02 -080079 struct clock_event_device *evt = &clockevent_gpt;
Tony Lindgren1dbae812005-11-10 14:26:51 +000080
Tony Lindgrenaa561882011-03-29 15:54:48 -070081 __omap_dm_timer_write_status(clkev.io_base, OMAP_TIMER_INT_OVERFLOW);
Kevin Hilman5a3a3882007-11-12 23:24:02 -080082
83 evt->event_handler(evt);
Tony Lindgren1dbae812005-11-10 14:26:51 +000084 return IRQ_HANDLED;
85}
86
87static struct irqaction omap2_gp_timer_irq = {
88 .name = "gp timer",
Bernhard Walleb30faba2007-05-08 00:35:39 -070089 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
Tony Lindgren1dbae812005-11-10 14:26:51 +000090 .handler = omap2_gp_timer_interrupt,
91};
92
Kevin Hilman5a3a3882007-11-12 23:24:02 -080093static int omap2_gp_timer_set_next_event(unsigned long cycles,
94 struct clock_event_device *evt)
Tony Lindgren1dbae812005-11-10 14:26:51 +000095{
Tony Lindgrenaa561882011-03-29 15:54:48 -070096 __omap_dm_timer_load_start(clkev.io_base, OMAP_TIMER_CTRL_ST,
97 0xffffffff - cycles, 1);
Tony Lindgren1dbae812005-11-10 14:26:51 +000098
Kevin Hilman5a3a3882007-11-12 23:24:02 -080099 return 0;
100}
101
102static void omap2_gp_timer_set_mode(enum clock_event_mode mode,
103 struct clock_event_device *evt)
104{
105 u32 period;
106
Tony Lindgrenaa561882011-03-29 15:54:48 -0700107 __omap_dm_timer_stop(clkev.io_base, 1, clkev.rate);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800108
109 switch (mode) {
110 case CLOCK_EVT_MODE_PERIODIC:
Tony Lindgrenaa561882011-03-29 15:54:48 -0700111 period = clkev.rate / HZ;
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800112 period -= 1;
Tony Lindgrenaa561882011-03-29 15:54:48 -0700113 /* Looks like we need to first set the load value separately */
114 __omap_dm_timer_write(clkev.io_base, OMAP_TIMER_LOAD_REG,
115 0xffffffff - period, 1);
116 __omap_dm_timer_load_start(clkev.io_base,
117 OMAP_TIMER_CTRL_AR | OMAP_TIMER_CTRL_ST,
118 0xffffffff - period, 1);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800119 break;
120 case CLOCK_EVT_MODE_ONESHOT:
121 break;
122 case CLOCK_EVT_MODE_UNUSED:
123 case CLOCK_EVT_MODE_SHUTDOWN:
124 case CLOCK_EVT_MODE_RESUME:
125 break;
126 }
127}
128
129static struct clock_event_device clockevent_gpt = {
130 .name = "gp timer",
131 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
132 .shift = 32,
133 .set_next_event = omap2_gp_timer_set_next_event,
134 .set_mode = omap2_gp_timer_set_mode,
135};
136
Tony Lindgrenaa561882011-03-29 15:54:48 -0700137static int __init omap_dm_timer_init_one(struct omap_dm_timer *timer,
138 int gptimer_id,
139 const char *fck_source)
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800140{
Tony Lindgrenaa561882011-03-29 15:54:48 -0700141 char name[10]; /* 10 = sizeof("gptXX_Xck0") */
142 struct omap_hwmod *oh;
143 size_t size;
144 int res = 0;
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800145
Tony Lindgrenaa561882011-03-29 15:54:48 -0700146 sprintf(name, "timer%d", gptimer_id);
147 omap_hwmod_setup_one(name);
148 oh = omap_hwmod_lookup(name);
149 if (!oh)
150 return -ENODEV;
Paul Walmsleyf2480762009-04-23 21:11:10 -0600151
Tony Lindgrenaa561882011-03-29 15:54:48 -0700152 timer->irq = oh->mpu_irqs[0].irq;
153 timer->phys_base = oh->slaves[0]->addr->pa_start;
154 size = oh->slaves[0]->addr->pa_end - timer->phys_base;
155
156 /* Static mapping, never released */
157 timer->io_base = ioremap(timer->phys_base, size);
158 if (!timer->io_base)
159 return -ENXIO;
160
161 /* After the dmtimer is using hwmod these clocks won't be needed */
162 sprintf(name, "gpt%d_fck", gptimer_id);
163 timer->fclk = clk_get(NULL, name);
164 if (IS_ERR(timer->fclk))
165 return -ENODEV;
166
167 sprintf(name, "gpt%d_ick", gptimer_id);
168 timer->iclk = clk_get(NULL, name);
169 if (IS_ERR(timer->iclk)) {
170 clk_put(timer->fclk);
171 return -ENODEV;
172 }
173
174 omap_hwmod_enable(oh);
175
Tony Lindgren11a01862011-03-29 15:54:49 -0700176 sys_timer_reserved |= (1 << (gptimer_id - 1));
177
Tony Lindgrenaa561882011-03-29 15:54:48 -0700178 if (gptimer_id != 12) {
179 struct clk *src;
180
181 src = clk_get(NULL, fck_source);
182 if (IS_ERR(src)) {
183 res = -EINVAL;
184 } else {
185 res = __omap_dm_timer_set_source(timer->fclk, src);
186 if (IS_ERR_VALUE(res))
187 pr_warning("%s: timer%i cannot set source\n",
188 __func__, gptimer_id);
189 clk_put(src);
190 }
191 }
192 __omap_dm_timer_reset(timer->io_base, 1, 1);
193 timer->posted = 1;
194
195 timer->rate = clk_get_rate(timer->fclk);
196
197 timer->reserved = 1;
Paul Walmsley38698be2011-02-23 00:14:08 -0700198
Tony Lindgrenaa561882011-03-29 15:54:48 -0700199 return res;
200}
Paul Walmsleyf2480762009-04-23 21:11:10 -0600201
Tony Lindgrenaa561882011-03-29 15:54:48 -0700202static void __init omap2_gp_clockevent_init(int gptimer_id,
203 const char *fck_source)
204{
205 int res;
Paul Walmsleyf2480762009-04-23 21:11:10 -0600206
Tony Lindgrenaa561882011-03-29 15:54:48 -0700207 res = omap_dm_timer_init_one(&clkev, gptimer_id, fck_source);
208 BUG_ON(res);
Paul Walmsleyf2480762009-04-23 21:11:10 -0600209
Tony Lindgren98e182a2011-03-29 15:54:49 -0700210 omap2_gp_timer_irq.dev_id = (void *)&clkev;
Tony Lindgrenaa561882011-03-29 15:54:48 -0700211 setup_irq(clkev.irq, &omap2_gp_timer_irq);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800212
Tony Lindgrenaa561882011-03-29 15:54:48 -0700213 __omap_dm_timer_int_enable(clkev.io_base, OMAP_TIMER_INT_OVERFLOW);
214
215 clockevent_gpt.mult = div_sc(clkev.rate, NSEC_PER_SEC,
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800216 clockevent_gpt.shift);
217 clockevent_gpt.max_delta_ns =
218 clockevent_delta2ns(0xffffffff, &clockevent_gpt);
219 clockevent_gpt.min_delta_ns =
Aaro Koskinendf88acb2009-01-29 08:57:17 -0800220 clockevent_delta2ns(3, &clockevent_gpt);
221 /* Timer internal resynch latency. */
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800222
Rusty Russell320ab2b2008-12-13 21:20:26 +1030223 clockevent_gpt.cpumask = cpumask_of(0);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800224 clockevents_register_device(&clockevent_gpt);
Tony Lindgrenaa561882011-03-29 15:54:48 -0700225
226 pr_info("OMAP clockevent source: GPTIMER%d at %lu Hz\n",
227 gptimer_id, clkev.rate);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800228}
229
Paul Walmsleyf2480762009-04-23 21:11:10 -0600230/* Clocksource code */
231
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800232#ifdef CONFIG_OMAP_32K_TIMER
Tony Lindgren0f622e82011-03-29 15:54:50 -0700233/*
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800234 * When 32k-timer is enabled, don't use GPTimer for clocksource
235 * instead, just leave default clocksource which uses the 32k
Paul Walmsleyd8328f32011-01-15 21:32:01 -0700236 * sync counter. See clocksource setup in plat-omap/counter_32k.c
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800237 */
238
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700239static void __init omap2_gp_clocksource_init(int unused, const char *dummy)
Paul Walmsleyd8328f32011-01-15 21:32:01 -0700240{
241 omap_init_clocksource_32k();
242}
243
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800244#else
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700245
246static struct omap_dm_timer clksrc;
247
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800248/*
249 * clocksource
250 */
Paul Walmsleycbc94382011-02-22 19:59:49 -0700251static DEFINE_CLOCK_DATA(cd);
Magnus Damm8e196082009-04-21 12:24:00 -0700252static cycle_t clocksource_read_cycles(struct clocksource *cs)
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800253{
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700254 return (cycle_t)__omap_dm_timer_read_counter(clksrc.io_base, 1);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800255}
256
257static struct clocksource clocksource_gpt = {
258 .name = "gp timer",
259 .rating = 300,
260 .read = clocksource_read_cycles,
261 .mask = CLOCKSOURCE_MASK(32),
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800262 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
263};
264
Paul Walmsleycbc94382011-02-22 19:59:49 -0700265static void notrace dmtimer_update_sched_clock(void)
266{
267 u32 cyc;
268
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700269 cyc = __omap_dm_timer_read_counter(clksrc.io_base, 1);
Paul Walmsleycbc94382011-02-22 19:59:49 -0700270
271 update_sched_clock(&cd, cyc, (u32)~0);
272}
273
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700274unsigned long long notrace sched_clock(void)
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800275{
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700276 u32 cyc = 0;
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800277
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700278 if (clksrc.reserved)
279 cyc = __omap_dm_timer_read_counter(clksrc.io_base, 1);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800280
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700281 return cyc_to_sched_clock(&cd, cyc, (u32)~0);
282}
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800283
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700284/* Setup free-running counter for clocksource */
285static void __init omap2_gp_clocksource_init(int gptimer_id,
286 const char *fck_source)
287{
288 int res;
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800289
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700290 res = omap_dm_timer_init_one(&clksrc, gptimer_id, fck_source);
291 BUG_ON(res);
Paul Walmsleycbc94382011-02-22 19:59:49 -0700292
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700293 pr_info("OMAP clocksource: GPTIMER%d at %lu Hz\n",
294 gptimer_id, clksrc.rate);
295
Hemant Pedanekare9d0b972011-08-10 13:19:35 +0000296 __omap_dm_timer_load_start(clksrc.io_base,
297 OMAP_TIMER_CTRL_ST | OMAP_TIMER_CTRL_AR, 0, 1);
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700298 init_sched_clock(&cd, dmtimer_update_sched_clock, 32, clksrc.rate);
299
300 if (clocksource_register_hz(&clocksource_gpt, clksrc.rate))
301 pr_err("Could not register clocksource %s\n",
302 clocksource_gpt.name);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800303}
304#endif
305
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700306#define OMAP_SYS_TIMER_INIT(name, clkev_nr, clkev_src, \
307 clksrc_nr, clksrc_src) \
Tony Lindgrene74984e2011-03-29 15:54:48 -0700308static void __init omap##name##_timer_init(void) \
309{ \
Tony Lindgrenaa561882011-03-29 15:54:48 -0700310 omap2_gp_clockevent_init((clkev_nr), clkev_src); \
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700311 omap2_gp_clocksource_init((clksrc_nr), clksrc_src); \
Tony Lindgrene74984e2011-03-29 15:54:48 -0700312}
313
314#define OMAP_SYS_TIMER(name) \
315struct sys_timer omap##name##_timer = { \
316 .init = omap##name##_timer_init, \
317};
318
319#ifdef CONFIG_ARCH_OMAP2
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700320OMAP_SYS_TIMER_INIT(2, 1, OMAP2_CLKEV_SOURCE, 2, OMAP2_MPU_SOURCE)
Tony Lindgrene74984e2011-03-29 15:54:48 -0700321OMAP_SYS_TIMER(2)
322#endif
323
324#ifdef CONFIG_ARCH_OMAP3
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700325OMAP_SYS_TIMER_INIT(3, 1, OMAP3_CLKEV_SOURCE, 2, OMAP3_MPU_SOURCE)
Tony Lindgrene74984e2011-03-29 15:54:48 -0700326OMAP_SYS_TIMER(3)
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700327OMAP_SYS_TIMER_INIT(3_secure, OMAP3_SECURE_TIMER, OMAP3_CLKEV_SOURCE,
328 2, OMAP3_MPU_SOURCE)
Tony Lindgrene74984e2011-03-29 15:54:48 -0700329OMAP_SYS_TIMER(3_secure)
330#endif
331
332#ifdef CONFIG_ARCH_OMAP4
333static void __init omap4_timer_init(void)
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800334{
Santosh Shilimkar39e1d4c2009-04-28 20:52:00 +0530335#ifdef CONFIG_LOCAL_TIMERS
Tony Lindgrene74984e2011-03-29 15:54:48 -0700336 twd_base = ioremap(OMAP44XX_LOCAL_TWD_BASE, SZ_256);
337 BUG_ON(!twd_base);
Santosh Shilimkar39e1d4c2009-04-28 20:52:00 +0530338#endif
Tony Lindgrenaa561882011-03-29 15:54:48 -0700339 omap2_gp_clockevent_init(1, OMAP4_CLKEV_SOURCE);
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700340 omap2_gp_clocksource_init(2, OMAP4_MPU_SOURCE);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000341}
Tony Lindgrene74984e2011-03-29 15:54:48 -0700342OMAP_SYS_TIMER(4)
343#endif