2 * linux/arch/arm/mach-omap2/timer-gp.c
4 * OMAP2 GP timer support.
6 * Update to use new clocksource/clockevent layers
7 * Author: Kevin Hilman, MontaVista Software, Inc. <source@mvista.com>
8 * Copyright (C) 2007 MontaVista Software, Inc.
11 * Copyright (C) 2005 Nokia Corporation
12 * Author: Paul Mundt <paul.mundt@nokia.com>
13 * Juha Yrjölä <juha.yrjola@nokia.com>
14 * OMAP Dual-mode timer framework support by Timo Teras
16 * Some parts based off of TI's 24xx code:
18 * Copyright (C) 2004 Texas Instruments, Inc.
20 * Roughly modelled after the OMAP1 MPU timer code.
22 * This file is subject to the terms and conditions of the GNU General Public
23 * License. See the file "COPYING" in the main directory of this archive
26 #include <linux/init.h>
27 #include <linux/time.h>
28 #include <linux/interrupt.h>
29 #include <linux/err.h>
30 #include <linux/clk.h>
31 #include <linux/delay.h>
32 #include <linux/irq.h>
33 #include <linux/clocksource.h>
34 #include <linux/clockchips.h>
36 #include <asm/mach/time.h>
37 #include <mach/dmtimer.h>
39 static struct omap_dm_timer *gptimer;
40 static struct clock_event_device clockevent_gpt;
42 static irqreturn_t omap2_gp_timer_interrupt(int irq, void *dev_id)
44 struct omap_dm_timer *gpt = (struct omap_dm_timer *)dev_id;
45 struct clock_event_device *evt = &clockevent_gpt;
47 omap_dm_timer_write_status(gpt, OMAP_TIMER_INT_OVERFLOW);
49 evt->event_handler(evt);
53 static struct irqaction omap2_gp_timer_irq = {
55 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
56 .handler = omap2_gp_timer_interrupt,
59 static int omap2_gp_timer_set_next_event(unsigned long cycles,
60 struct clock_event_device *evt)
62 omap_dm_timer_set_load_start(gptimer, 0, 0xffffffff - cycles);
67 static void omap2_gp_timer_set_mode(enum clock_event_mode mode,
68 struct clock_event_device *evt)
72 omap_dm_timer_stop(gptimer);
75 case CLOCK_EVT_MODE_PERIODIC:
76 period = clk_get_rate(omap_dm_timer_get_fclk(gptimer)) / HZ;
79 omap_dm_timer_set_load_start(gptimer, 1, 0xffffffff - period);
81 case CLOCK_EVT_MODE_ONESHOT:
83 case CLOCK_EVT_MODE_UNUSED:
84 case CLOCK_EVT_MODE_SHUTDOWN:
85 case CLOCK_EVT_MODE_RESUME:
90 static struct clock_event_device clockevent_gpt = {
92 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
94 .set_next_event = omap2_gp_timer_set_next_event,
95 .set_mode = omap2_gp_timer_set_mode,
98 static void __init omap2_gp_clockevent_init(void)
102 gptimer = omap_dm_timer_request_specific(1);
103 BUG_ON(gptimer == NULL);
105 #if defined(CONFIG_OMAP_32K_TIMER)
106 omap_dm_timer_set_source(gptimer, OMAP_TIMER_SRC_32_KHZ);
108 omap_dm_timer_set_source(gptimer, OMAP_TIMER_SRC_SYS_CLK);
110 tick_rate = clk_get_rate(omap_dm_timer_get_fclk(gptimer));
112 omap2_gp_timer_irq.dev_id = (void *)gptimer;
113 setup_irq(omap_dm_timer_get_irq(gptimer), &omap2_gp_timer_irq);
114 omap_dm_timer_set_int_enable(gptimer, OMAP_TIMER_INT_OVERFLOW);
116 clockevent_gpt.mult = div_sc(tick_rate, NSEC_PER_SEC,
117 clockevent_gpt.shift);
118 clockevent_gpt.max_delta_ns =
119 clockevent_delta2ns(0xffffffff, &clockevent_gpt);
120 clockevent_gpt.min_delta_ns =
121 clockevent_delta2ns(1, &clockevent_gpt);
123 clockevent_gpt.cpumask = cpumask_of_cpu(0);
124 clockevents_register_device(&clockevent_gpt);
127 #ifdef CONFIG_OMAP_32K_TIMER
129 * When 32k-timer is enabled, don't use GPTimer for clocksource
130 * instead, just leave default clocksource which uses the 32k
131 * sync counter. See clocksource setup in see plat-omap/common.c.
134 static inline void __init omap2_gp_clocksource_init(void) {}
139 static struct omap_dm_timer *gpt_clocksource;
140 static cycle_t clocksource_read_cycles(void)
142 return (cycle_t)omap_dm_timer_read_counter(gpt_clocksource);
145 static struct clocksource clocksource_gpt = {
148 .read = clocksource_read_cycles,
149 .mask = CLOCKSOURCE_MASK(32),
151 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
154 /* Setup free-running counter for clocksource */
155 static void __init omap2_gp_clocksource_init(void)
157 static struct omap_dm_timer *gpt;
158 u32 tick_rate, tick_period;
159 static char err1[] __initdata = KERN_ERR
160 "%s: failed to request dm-timer\n";
161 static char err2[] __initdata = KERN_ERR
162 "%s: can't register clocksource!\n";
164 gpt = omap_dm_timer_request();
166 printk(err1, clocksource_gpt.name);
167 gpt_clocksource = gpt;
169 omap_dm_timer_set_source(gpt, OMAP_TIMER_SRC_SYS_CLK);
170 tick_rate = clk_get_rate(omap_dm_timer_get_fclk(gpt));
171 tick_period = (tick_rate / HZ) - 1;
173 omap_dm_timer_set_load_start(gpt, 1, 0);
175 clocksource_gpt.mult =
176 clocksource_khz2mult(tick_rate/1000, clocksource_gpt.shift);
177 if (clocksource_register(&clocksource_gpt))
178 printk(err2, clocksource_gpt.name);
182 static void __init omap2_gp_timer_init(void)
184 omap_dm_timer_init();
186 omap2_gp_clockevent_init();
187 omap2_gp_clocksource_init();
190 struct sys_timer omap_timer = {
191 .init = omap2_gp_timer_init,