2 * linux/arch/arm/mach-imx/time.c
4 * Copyright (C) 2000-2001 Deep Blue Solutions
5 * Copyright (C) 2002 Shane Nay (shane@minirl.com)
6 * Copyright (C) 2006-2007 Pavel Pisa (ppisa@pikron.com)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/time.h>
18 #include <linux/clocksource.h>
19 #include <linux/clockchips.h>
20 #include <linux/clk.h>
23 #include <mach/hardware.h>
26 #include <asm/mach/time.h>
28 /* Use timer 1 as system timer */
29 #define TIMER_BASE IMX_TIM1_BASE
31 static struct clock_event_device clockevent_imx;
32 static enum clock_event_mode clockevent_mode = CLOCK_EVT_MODE_UNUSED;
35 * IRQ handler for the timer
38 imx_timer_interrupt(int irq, void *dev_id)
40 struct clock_event_device *evt = &clockevent_imx;
42 irqreturn_t ret = IRQ_NONE;
44 /* clear the interrupt */
45 tstat = IMX_TSTAT(TIMER_BASE);
46 IMX_TSTAT(TIMER_BASE) = 0;
48 if (tstat & TSTAT_COMP) {
49 evt->event_handler(evt);
56 static struct irqaction imx_timer_irq = {
57 .name = "i.MX Timer Tick",
58 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
59 .handler = imx_timer_interrupt,
63 * Set up timer hardware into expected mode and state.
65 static void __init imx_timer_hardware_init(void)
68 * Initialise to a known state (all timers off, and timing reset)
70 IMX_TCTL(TIMER_BASE) = 0;
71 IMX_TPRER(TIMER_BASE) = 0;
73 IMX_TCTL(TIMER_BASE) = TCTL_FRR | TCTL_CLK_PCLK1 | TCTL_TEN;
76 cycle_t imx_get_cycles(void)
78 return IMX_TCN(TIMER_BASE);
81 static struct clocksource clocksource_imx = {
84 .read = imx_get_cycles,
87 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
90 static int __init imx_clocksource_init(unsigned long rate)
92 clocksource_imx.mult =
93 clocksource_hz2mult(rate, clocksource_imx.shift);
94 clocksource_register(&clocksource_imx);
99 static int imx_set_next_event(unsigned long evt,
100 struct clock_event_device *unused)
104 tcmp = IMX_TCN(TIMER_BASE) + evt;
105 IMX_TCMP(TIMER_BASE) = tcmp;
107 return (int32_t)(tcmp - IMX_TCN(TIMER_BASE)) < 0 ? -ETIME : 0;
111 static const char *clock_event_mode_label[]={
112 [CLOCK_EVT_MODE_PERIODIC] = "CLOCK_EVT_MODE_PERIODIC",
113 [CLOCK_EVT_MODE_ONESHOT] = "CLOCK_EVT_MODE_ONESHOT",
114 [CLOCK_EVT_MODE_SHUTDOWN] = "CLOCK_EVT_MODE_SHUTDOWN",
115 [CLOCK_EVT_MODE_UNUSED] = "CLOCK_EVT_MODE_UNUSED"
119 static void imx_set_mode(enum clock_event_mode mode, struct clock_event_device *evt)
124 * The timer interrupt generation is disabled at least
125 * for enough time to call imx_set_next_event()
127 local_irq_save(flags);
128 /* Disable interrupt in GPT module */
129 IMX_TCTL(TIMER_BASE) &= ~TCTL_IRQEN;
130 if (mode != clockevent_mode) {
131 /* Set event time into far-far future */
132 IMX_TCMP(TIMER_BASE) = IMX_TCN(TIMER_BASE) - 3;
133 /* Clear pending interrupt */
134 IMX_TSTAT(TIMER_BASE) &= ~TSTAT_COMP;
138 printk(KERN_INFO "imx_set_mode: changing mode from %s to %s\n",
139 clock_event_mode_label[clockevent_mode], clock_event_mode_label[mode]);
142 /* Remember timer mode */
143 clockevent_mode = mode;
144 local_irq_restore(flags);
147 case CLOCK_EVT_MODE_PERIODIC:
148 printk(KERN_ERR "imx_set_mode: Periodic mode is not supported for i.MX\n");
150 case CLOCK_EVT_MODE_ONESHOT:
152 * Do not put overhead of interrupt enable/disable into
153 * imx_set_next_event(), the core has about 4 minutes
154 * to call imx_set_next_event() or shutdown clock after
157 local_irq_save(flags);
158 IMX_TCTL(TIMER_BASE) |= TCTL_IRQEN;
159 local_irq_restore(flags);
161 case CLOCK_EVT_MODE_SHUTDOWN:
162 case CLOCK_EVT_MODE_UNUSED:
163 case CLOCK_EVT_MODE_RESUME:
164 /* Left event sources disabled, no more interrupts appears */
169 static struct clock_event_device clockevent_imx = {
170 .name = "imx_timer1",
171 .features = CLOCK_EVT_FEAT_ONESHOT,
173 .set_mode = imx_set_mode,
174 .set_next_event = imx_set_next_event,
178 static int __init imx_clockevent_init(unsigned long rate)
180 clockevent_imx.mult = div_sc(rate, NSEC_PER_SEC,
181 clockevent_imx.shift);
182 clockevent_imx.max_delta_ns =
183 clockevent_delta2ns(0xfffffffe, &clockevent_imx);
184 clockevent_imx.min_delta_ns =
185 clockevent_delta2ns(0xf, &clockevent_imx);
187 clockevent_imx.cpumask = cpumask_of(0);
189 clockevents_register_device(&clockevent_imx);
194 extern int imx_clocks_init(void);
196 static void __init imx_timer_init(void)
203 clk = clk_get(NULL, "perclk1");
205 rate = clk_get_rate(clk);
207 imx_timer_hardware_init();
208 imx_clocksource_init(rate);
210 imx_clockevent_init(rate);
213 * Make irqs happen for the system timer
215 setup_irq(TIM1_INT, &imx_timer_irq);
218 struct sys_timer imx_timer = {
219 .init = imx_timer_init,