Merge git://git.kernel.org/pub/scm/linux/kernel/git/sam/kbuild
[pandora-kernel.git] / arch / arm / mach-imx / time.c
1 /*
2  *  linux/arch/arm/mach-imx/time.c
3  *
4  *  Copyright (C) 2000-2001 Deep Blue Solutions
5  *  Copyright (C) 2002 Shane Nay (shane@minirl.com)
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/time.h>
16
17 #include <asm/hardware.h>
18 #include <asm/io.h>
19 #include <asm/leds.h>
20 #include <asm/irq.h>
21 #include <asm/mach/time.h>
22
23 /* Use timer 1 as system timer */
24 #define TIMER_BASE IMX_TIM1_BASE
25
26 /*
27  * Returns number of us since last clock interrupt.  Note that interrupts
28  * will have been disabled by do_gettimeoffset()
29  */
30 static unsigned long imx_gettimeoffset(void)
31 {
32         unsigned long ticks;
33
34         /*
35          * Get the current number of ticks.  Note that there is a race
36          * condition between us reading the timer and checking for
37          * an interrupt.  We get around this by ensuring that the
38          * counter has not reloaded between our two reads.
39          */
40         ticks = IMX_TCN(TIMER_BASE);
41
42         /*
43          * Interrupt pending?  If so, we've reloaded once already.
44          */
45         if (IMX_TSTAT(TIMER_BASE) & TSTAT_COMP)
46                 ticks += LATCH;
47
48         /*
49          * Convert the ticks to usecs
50          */
51         return (1000000 / CLK32) * ticks;
52 }
53
54 /*
55  * IRQ handler for the timer
56  */
57 static irqreturn_t
58 imx_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
59 {
60         write_seqlock(&xtime_lock);
61
62         /* clear the interrupt */
63         if (IMX_TSTAT(TIMER_BASE))
64                 IMX_TSTAT(TIMER_BASE) = 0;
65
66         timer_tick(regs);
67         write_sequnlock(&xtime_lock);
68
69         return IRQ_HANDLED;
70 }
71
72 static struct irqaction imx_timer_irq = {
73         .name           = "i.MX Timer Tick",
74         .flags          = SA_INTERRUPT | SA_TIMER,
75         .handler        = imx_timer_interrupt,
76 };
77
78 /*
79  * Set up timer interrupt, and return the current time in seconds.
80  */
81 static void __init imx_timer_init(void)
82 {
83         /*
84          * Initialise to a known state (all timers off, and timing reset)
85          */
86         IMX_TCTL(TIMER_BASE) = 0;
87         IMX_TPRER(TIMER_BASE) = 0;
88         IMX_TCMP(TIMER_BASE) = LATCH - 1;
89         IMX_TCTL(TIMER_BASE) = TCTL_CLK_32 | TCTL_IRQEN | TCTL_TEN;
90
91         /*
92          * Make irqs happen for the system timer
93          */
94         setup_irq(TIM1_INT, &imx_timer_irq);
95 }
96
97 struct sys_timer imx_timer = {
98         .init           = imx_timer_init,
99         .offset         = imx_gettimeoffset,
100 };