blob: 62c0d5cb25f7446c3d712605c2af098a32130c5c [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;
Timo Teras77900a22006-06-26 16:16:12 -070075static struct omap_dm_timer *gptimer;
Kevin Hilman5a3a3882007-11-12 23:24:02 -080076static struct clock_event_device clockevent_gpt;
Paul Walmsleyf2480762009-04-23 21:11:10 -060077static u8 __initdata gptimer_id = 1;
78static u8 __initdata inited;
Kevin Hilmand7814e42009-10-06 14:30:23 -070079struct omap_dm_timer *gptimer_wakeup;
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
200 if (gptimer_id != 12) {
201 struct clk *src;
202
203 src = clk_get(NULL, fck_source);
204 if (IS_ERR(src)) {
205 res = -EINVAL;
206 } else {
207 res = __omap_dm_timer_set_source(timer->fclk, src);
208 if (IS_ERR_VALUE(res))
209 pr_warning("%s: timer%i cannot set source\n",
210 __func__, gptimer_id);
211 clk_put(src);
212 }
213 }
214 __omap_dm_timer_reset(timer->io_base, 1, 1);
215 timer->posted = 1;
216
217 timer->rate = clk_get_rate(timer->fclk);
218
219 timer->reserved = 1;
Paul Walmsley38698be2011-02-23 00:14:08 -0700220
Paul Walmsleyf2480762009-04-23 21:11:10 -0600221 gptimer = omap_dm_timer_request_specific(gptimer_id);
Timo Teras77900a22006-06-26 16:16:12 -0700222 BUG_ON(gptimer == NULL);
Kevin Hilmand7814e42009-10-06 14:30:23 -0700223 gptimer_wakeup = gptimer;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000224
Tony Lindgrenaa561882011-03-29 15:54:48 -0700225 return res;
226}
Paul Walmsleyf2480762009-04-23 21:11:10 -0600227
Tony Lindgrenaa561882011-03-29 15:54:48 -0700228static void __init omap2_gp_clockevent_init(int gptimer_id,
229 const char *fck_source)
230{
231 int res;
Paul Walmsleyf2480762009-04-23 21:11:10 -0600232
Tony Lindgrenaa561882011-03-29 15:54:48 -0700233 inited = 1;
Tony Lindgren1dbae812005-11-10 14:26:51 +0000234
Tony Lindgrenaa561882011-03-29 15:54:48 -0700235 res = omap_dm_timer_init_one(&clkev, gptimer_id, fck_source);
236 BUG_ON(res);
Paul Walmsleyf2480762009-04-23 21:11:10 -0600237
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800238 omap2_gp_timer_irq.dev_id = (void *)gptimer;
Tony Lindgrenaa561882011-03-29 15:54:48 -0700239 setup_irq(clkev.irq, &omap2_gp_timer_irq);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800240
Tony Lindgrenaa561882011-03-29 15:54:48 -0700241 __omap_dm_timer_int_enable(clkev.io_base, OMAP_TIMER_INT_OVERFLOW);
242
243 clockevent_gpt.mult = div_sc(clkev.rate, NSEC_PER_SEC,
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800244 clockevent_gpt.shift);
245 clockevent_gpt.max_delta_ns =
246 clockevent_delta2ns(0xffffffff, &clockevent_gpt);
247 clockevent_gpt.min_delta_ns =
Aaro Koskinendf88acb2009-01-29 08:57:17 -0800248 clockevent_delta2ns(3, &clockevent_gpt);
249 /* Timer internal resynch latency. */
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800250
Rusty Russell320ab2b2008-12-13 21:20:26 +1030251 clockevent_gpt.cpumask = cpumask_of(0);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800252 clockevents_register_device(&clockevent_gpt);
Tony Lindgrenaa561882011-03-29 15:54:48 -0700253
254 pr_info("OMAP clockevent source: GPTIMER%d at %lu Hz\n",
255 gptimer_id, clkev.rate);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800256}
257
Paul Walmsleyf2480762009-04-23 21:11:10 -0600258/* Clocksource code */
259
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800260#ifdef CONFIG_OMAP_32K_TIMER
261/*
262 * When 32k-timer is enabled, don't use GPTimer for clocksource
263 * instead, just leave default clocksource which uses the 32k
Paul Walmsleyd8328f32011-01-15 21:32:01 -0700264 * sync counter. See clocksource setup in plat-omap/counter_32k.c
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800265 */
266
Paul Walmsleyd8328f32011-01-15 21:32:01 -0700267static void __init omap2_gp_clocksource_init(void)
268{
269 omap_init_clocksource_32k();
270}
271
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800272#else
273/*
274 * clocksource
275 */
Paul Walmsleycbc94382011-02-22 19:59:49 -0700276static DEFINE_CLOCK_DATA(cd);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800277static struct omap_dm_timer *gpt_clocksource;
Magnus Damm8e196082009-04-21 12:24:00 -0700278static cycle_t clocksource_read_cycles(struct clocksource *cs)
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800279{
280 return (cycle_t)omap_dm_timer_read_counter(gpt_clocksource);
281}
282
283static struct clocksource clocksource_gpt = {
284 .name = "gp timer",
285 .rating = 300,
286 .read = clocksource_read_cycles,
287 .mask = CLOCKSOURCE_MASK(32),
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800288 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
289};
290
Paul Walmsleycbc94382011-02-22 19:59:49 -0700291static void notrace dmtimer_update_sched_clock(void)
292{
293 u32 cyc;
294
295 cyc = omap_dm_timer_read_counter(gpt_clocksource);
296
297 update_sched_clock(&cd, cyc, (u32)~0);
298}
299
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800300/* Setup free-running counter for clocksource */
301static void __init omap2_gp_clocksource_init(void)
302{
303 static struct omap_dm_timer *gpt;
Aaro Koskinen28629452010-11-18 19:59:51 +0200304 u32 tick_rate;
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800305 static char err1[] __initdata = KERN_ERR
306 "%s: failed to request dm-timer\n";
307 static char err2[] __initdata = KERN_ERR
308 "%s: can't register clocksource!\n";
309
310 gpt = omap_dm_timer_request();
311 if (!gpt)
312 printk(err1, clocksource_gpt.name);
313 gpt_clocksource = gpt;
314
315 omap_dm_timer_set_source(gpt, OMAP_TIMER_SRC_SYS_CLK);
316 tick_rate = clk_get_rate(omap_dm_timer_get_fclk(gpt));
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800317
Richard Woodruff3fddd092008-07-03 12:24:30 +0300318 omap_dm_timer_set_load_start(gpt, 1, 0);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800319
Paul Walmsleycbc94382011-02-22 19:59:49 -0700320 init_sched_clock(&cd, dmtimer_update_sched_clock, 32, tick_rate);
321
Russell King8437c252010-12-13 13:18:44 +0000322 if (clocksource_register_hz(&clocksource_gpt, tick_rate))
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800323 printk(err2, clocksource_gpt.name);
324}
325#endif
326
Tony Lindgrenaa561882011-03-29 15:54:48 -0700327#define OMAP_SYS_TIMER_INIT(name, clkev_nr, clkev_src) \
Tony Lindgrene74984e2011-03-29 15:54:48 -0700328static void __init omap##name##_timer_init(void) \
329{ \
330 omap_dm_timer_init(); \
Tony Lindgrenaa561882011-03-29 15:54:48 -0700331 omap2_gp_clockevent_init((clkev_nr), clkev_src); \
Tony Lindgrene74984e2011-03-29 15:54:48 -0700332 omap2_gp_clocksource_init(); \
333}
334
335#define OMAP_SYS_TIMER(name) \
336struct sys_timer omap##name##_timer = { \
337 .init = omap##name##_timer_init, \
338};
339
340#ifdef CONFIG_ARCH_OMAP2
Tony Lindgrenaa561882011-03-29 15:54:48 -0700341OMAP_SYS_TIMER_INIT(2, 1, OMAP2_CLKEV_SOURCE)
Tony Lindgrene74984e2011-03-29 15:54:48 -0700342OMAP_SYS_TIMER(2)
343#endif
344
345#ifdef CONFIG_ARCH_OMAP3
Tony Lindgrenaa561882011-03-29 15:54:48 -0700346OMAP_SYS_TIMER_INIT(3, 1, OMAP3_CLKEV_SOURCE)
Tony Lindgrene74984e2011-03-29 15:54:48 -0700347OMAP_SYS_TIMER(3)
Tony Lindgrenaa561882011-03-29 15:54:48 -0700348OMAP_SYS_TIMER_INIT(3_secure, OMAP3_SECURE_TIMER, OMAP3_CLKEV_SOURCE)
Tony Lindgrene74984e2011-03-29 15:54:48 -0700349OMAP_SYS_TIMER(3_secure)
350#endif
351
352#ifdef CONFIG_ARCH_OMAP4
353static void __init omap4_timer_init(void)
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800354{
Santosh Shilimkar39e1d4c2009-04-28 20:52:00 +0530355#ifdef CONFIG_LOCAL_TIMERS
Tony Lindgrene74984e2011-03-29 15:54:48 -0700356 twd_base = ioremap(OMAP44XX_LOCAL_TWD_BASE, SZ_256);
357 BUG_ON(!twd_base);
Santosh Shilimkar39e1d4c2009-04-28 20:52:00 +0530358#endif
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800359 omap_dm_timer_init();
Tony Lindgrenaa561882011-03-29 15:54:48 -0700360 omap2_gp_clockevent_init(1, OMAP4_CLKEV_SOURCE);
Kevin Hilman5a3a3882007-11-12 23:24:02 -0800361 omap2_gp_clocksource_init();
Tony Lindgren1dbae812005-11-10 14:26:51 +0000362}
Tony Lindgrene74984e2011-03-29 15:54:48 -0700363OMAP_SYS_TIMER(4)
364#endif