blob: 69466f3884179a1fba1cd0b0a1f0986d9bddf8ba [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 Lindgrenee17f112011-09-16 15:44:20 -070081 __omap_dm_timer_write_status(&clkev, 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 Lindgrenee17f112011-09-16 15:44:20 -070096 __omap_dm_timer_load_start(&clkev, OMAP_TIMER_CTRL_ST,
Tony Lindgrenaa561882011-03-29 15:54:48 -070097 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 Lindgrenee17f112011-09-16 15:44:20 -0700107 __omap_dm_timer_stop(&clkev, 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 */
Tony Lindgrenee17f112011-09-16 15:44:20 -0700114 __omap_dm_timer_write(&clkev, OMAP_TIMER_LOAD_REG,
Tony Lindgrenaa561882011-03-29 15:54:48 -0700115 0xffffffff - period, 1);
Tony Lindgrenee17f112011-09-16 15:44:20 -0700116 __omap_dm_timer_load_start(&clkev,
Tony Lindgrenaa561882011-03-29 15:54:48 -0700117 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 }
Tony Lindgrenee17f112011-09-16 15:44:20 -0700192 __omap_dm_timer_init_regs(timer);
193 __omap_dm_timer_reset(timer, 1, 1);
Tony Lindgrenaa561882011-03-29 15:54:48 -0700194 timer->posted = 1;
195
196 timer->rate = clk_get_rate(timer->fclk);
197
198 timer->reserved = 1;
Paul Walmsley38698be2011-02-23 00:14:08 -0700199
Tony Lindgrenaa561882011-03-29 15:54:48 -0700200 return res;
201}
Paul Walmsleyf2480762009-04-23 21:11:10 -0600202
Tony Lindgrenaa561882011-03-29 15:54:48 -0700203static void __init omap2_gp_clockevent_init(int gptimer_id,
204 const char *fck_source)
205{
206 int res;
Paul Walmsleyf2480762009-04-23 21:11:10 -0600207
Tony Lindgrenaa561882011-03-29 15:54:48 -0700208 res = omap_dm_timer_init_one(&clkev, gptimer_id, fck_source);
209 BUG_ON(res);
Paul Walmsleyf2480762009-04-23 21:11:10 -0600210
Tony Lindgren98e182a2011-03-29 15:54:49 -0700211 omap2_gp_timer_irq.dev_id = (void *)&clkev;
Tony Lindgrenaa561882011-03-29 15:54:48 -0700212 setup_irq(clkev.irq, &omap2_gp_timer_irq);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800213
Tony Lindgrenee17f112011-09-16 15:44:20 -0700214 __omap_dm_timer_int_enable(&clkev, OMAP_TIMER_INT_OVERFLOW);
Tony Lindgrenaa561882011-03-29 15:54:48 -0700215
216 clockevent_gpt.mult = div_sc(clkev.rate, NSEC_PER_SEC,
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800217 clockevent_gpt.shift);
218 clockevent_gpt.max_delta_ns =
219 clockevent_delta2ns(0xffffffff, &clockevent_gpt);
220 clockevent_gpt.min_delta_ns =
Aaro Koskinendf88acb2009-01-29 08:57:17 -0800221 clockevent_delta2ns(3, &clockevent_gpt);
222 /* Timer internal resynch latency. */
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800223
Rusty Russell320ab2b2008-12-13 21:20:26 +1030224 clockevent_gpt.cpumask = cpumask_of(0);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800225 clockevents_register_device(&clockevent_gpt);
Tony Lindgrenaa561882011-03-29 15:54:48 -0700226
227 pr_info("OMAP clockevent source: GPTIMER%d at %lu Hz\n",
228 gptimer_id, clkev.rate);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800229}
230
Paul Walmsleyf2480762009-04-23 21:11:10 -0600231/* Clocksource code */
232
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800233#ifdef CONFIG_OMAP_32K_TIMER
Tony Lindgren0f622e82011-03-29 15:54:50 -0700234/*
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800235 * When 32k-timer is enabled, don't use GPTimer for clocksource
236 * instead, just leave default clocksource which uses the 32k
Paul Walmsleyd8328f32011-01-15 21:32:01 -0700237 * sync counter. See clocksource setup in plat-omap/counter_32k.c
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800238 */
239
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700240static void __init omap2_gp_clocksource_init(int unused, const char *dummy)
Paul Walmsleyd8328f32011-01-15 21:32:01 -0700241{
242 omap_init_clocksource_32k();
243}
244
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800245#else
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700246
247static struct omap_dm_timer clksrc;
248
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800249/*
250 * clocksource
251 */
Paul Walmsleycbc94382011-02-22 19:59:49 -0700252static DEFINE_CLOCK_DATA(cd);
Magnus Damm8e196082009-04-21 12:24:00 -0700253static cycle_t clocksource_read_cycles(struct clocksource *cs)
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800254{
Tony Lindgrenee17f112011-09-16 15:44:20 -0700255 return (cycle_t)__omap_dm_timer_read_counter(&clksrc, 1);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800256}
257
258static struct clocksource clocksource_gpt = {
259 .name = "gp timer",
260 .rating = 300,
261 .read = clocksource_read_cycles,
262 .mask = CLOCKSOURCE_MASK(32),
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800263 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
264};
265
Paul Walmsleycbc94382011-02-22 19:59:49 -0700266static void notrace dmtimer_update_sched_clock(void)
267{
268 u32 cyc;
269
Tony Lindgrenee17f112011-09-16 15:44:20 -0700270 cyc = __omap_dm_timer_read_counter(&clksrc, 1);
Paul Walmsleycbc94382011-02-22 19:59:49 -0700271
272 update_sched_clock(&cd, cyc, (u32)~0);
273}
274
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700275unsigned long long notrace sched_clock(void)
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800276{
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700277 u32 cyc = 0;
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800278
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700279 if (clksrc.reserved)
Tony Lindgrenee17f112011-09-16 15:44:20 -0700280 cyc = __omap_dm_timer_read_counter(&clksrc, 1);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800281
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700282 return cyc_to_sched_clock(&cd, cyc, (u32)~0);
283}
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800284
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700285/* Setup free-running counter for clocksource */
286static void __init omap2_gp_clocksource_init(int gptimer_id,
287 const char *fck_source)
288{
289 int res;
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800290
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700291 res = omap_dm_timer_init_one(&clksrc, gptimer_id, fck_source);
292 BUG_ON(res);
Paul Walmsleycbc94382011-02-22 19:59:49 -0700293
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700294 pr_info("OMAP clocksource: GPTIMER%d at %lu Hz\n",
295 gptimer_id, clksrc.rate);
296
Tony Lindgrenee17f112011-09-16 15:44:20 -0700297 __omap_dm_timer_load_start(&clksrc,
Hemant Pedanekare9d0b972011-08-10 13:19:35 +0000298 OMAP_TIMER_CTRL_ST | OMAP_TIMER_CTRL_AR, 0, 1);
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700299 init_sched_clock(&cd, dmtimer_update_sched_clock, 32, clksrc.rate);
300
301 if (clocksource_register_hz(&clocksource_gpt, clksrc.rate))
302 pr_err("Could not register clocksource %s\n",
303 clocksource_gpt.name);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800304}
305#endif
306
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700307#define OMAP_SYS_TIMER_INIT(name, clkev_nr, clkev_src, \
308 clksrc_nr, clksrc_src) \
Tony Lindgrene74984e2011-03-29 15:54:48 -0700309static void __init omap##name##_timer_init(void) \
310{ \
Tony Lindgrenaa561882011-03-29 15:54:48 -0700311 omap2_gp_clockevent_init((clkev_nr), clkev_src); \
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700312 omap2_gp_clocksource_init((clksrc_nr), clksrc_src); \
Tony Lindgrene74984e2011-03-29 15:54:48 -0700313}
314
315#define OMAP_SYS_TIMER(name) \
316struct sys_timer omap##name##_timer = { \
317 .init = omap##name##_timer_init, \
318};
319
320#ifdef CONFIG_ARCH_OMAP2
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700321OMAP_SYS_TIMER_INIT(2, 1, OMAP2_CLKEV_SOURCE, 2, OMAP2_MPU_SOURCE)
Tony Lindgrene74984e2011-03-29 15:54:48 -0700322OMAP_SYS_TIMER(2)
323#endif
324
325#ifdef CONFIG_ARCH_OMAP3
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700326OMAP_SYS_TIMER_INIT(3, 1, OMAP3_CLKEV_SOURCE, 2, OMAP3_MPU_SOURCE)
Tony Lindgrene74984e2011-03-29 15:54:48 -0700327OMAP_SYS_TIMER(3)
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700328OMAP_SYS_TIMER_INIT(3_secure, OMAP3_SECURE_TIMER, OMAP3_CLKEV_SOURCE,
329 2, OMAP3_MPU_SOURCE)
Tony Lindgrene74984e2011-03-29 15:54:48 -0700330OMAP_SYS_TIMER(3_secure)
331#endif
332
333#ifdef CONFIG_ARCH_OMAP4
334static void __init omap4_timer_init(void)
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800335{
Santosh Shilimkar39e1d4c2009-04-28 20:52:00 +0530336#ifdef CONFIG_LOCAL_TIMERS
Tony Lindgrene74984e2011-03-29 15:54:48 -0700337 twd_base = ioremap(OMAP44XX_LOCAL_TWD_BASE, SZ_256);
338 BUG_ON(!twd_base);
Santosh Shilimkar39e1d4c2009-04-28 20:52:00 +0530339#endif
Tony Lindgrenaa561882011-03-29 15:54:48 -0700340 omap2_gp_clockevent_init(1, OMAP4_CLKEV_SOURCE);
Tony Lindgren3d05a3e2011-03-29 15:54:49 -0700341 omap2_gp_clocksource_init(2, OMAP4_MPU_SOURCE);
Tony Lindgren1dbae812005-11-10 14:26:51 +0000342}
Tony Lindgrene74984e2011-03-29 15:54:48 -0700343OMAP_SYS_TIMER(4)
344#endif