This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.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/config.h>
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/time.h>
17
18 #include <asm/hardware.h>
19 #include <asm/io.h>
20 #include <asm/leds.h>
21 #include <asm/irq.h>
22 #include <asm/mach/time.h>
23
24 /* Use timer 1 as system timer */
25 #define TIMER_BASE IMX_TIM1_BASE
26
27 /*
28  * Returns number of us since last clock interrupt.  Note that interrupts
29  * will have been disabled by do_gettimeoffset()
30  */
31 static unsigned long
32 imx_gettimeoffset(void)
33 {
34         unsigned long ticks;
35
36         /*
37          * Get the current number of ticks.  Note that there is a race
38          * condition between us reading the timer and checking for
39          * an interrupt.  We get around this by ensuring that the
40          * counter has not reloaded between our two reads.
41          */
42         ticks = IMX_TCN(TIMER_BASE);
43
44         /*
45          * Interrupt pending?  If so, we've reloaded once already.
46          */
47         if (IMX_TSTAT(TIMER_BASE) & TSTAT_COMP)
48                 ticks += LATCH;
49
50         /*
51          * Convert the ticks to usecs
52          */
53         return (1000000 / CLK32) * ticks;
54 }
55
56 /*
57  * IRQ handler for the timer
58  */
59 static irqreturn_t
60 imx_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
61 {
62         /* clear the interrupt */
63         if (IMX_TSTAT(TIMER_BASE))
64                 IMX_TSTAT(TIMER_BASE) = 0;
65
66         timer_tick(regs);
67         return IRQ_HANDLED;
68 }
69
70 static struct irqaction imx_timer_irq = {
71         .name           = "i.MX Timer Tick",
72         .flags          = SA_INTERRUPT,
73         .handler        = imx_timer_interrupt
74 };
75
76 /*
77  * Set up timer interrupt, and return the current time in seconds.
78  */
79 void __init
80 imx_init_time(void)
81 {
82         /*
83          * Initialise to a known state (all timers off, and timing reset)
84          */
85         IMX_TCTL(TIMER_BASE) = 0;
86         IMX_TPRER(TIMER_BASE) = 0;
87         IMX_TCMP(TIMER_BASE) = LATCH - 1;
88         IMX_TCTL(TIMER_BASE) = TCTL_CLK_32 | TCTL_IRQEN | TCTL_TEN;
89
90         /*
91          * Make irqs happen for the system timer
92          */
93         setup_irq(TIM1_INT, &imx_timer_irq);
94         gettimeoffset = imx_gettimeoffset;
95 }