ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / asm-arm / arch-integrator / time.h
1 /*
2  *  linux/include/asm-arm/arch-integrator/time.h
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18 #include <asm/system.h>
19 #include <asm/leds.h>
20 #include <asm/mach-types.h>
21
22 /*
23  * Where is the timer (VA)?
24  */
25 #define TIMER0_VA_BASE (IO_ADDRESS(INTEGRATOR_CT_BASE)+0x00000000)
26 #define TIMER1_VA_BASE (IO_ADDRESS(INTEGRATOR_CT_BASE)+0x00000100)
27 #define TIMER2_VA_BASE (IO_ADDRESS(INTEGRATOR_CT_BASE)+0x00000200)
28 #define VA_IC_BASE     IO_ADDRESS(INTEGRATOR_IC_BASE) 
29
30 /*
31  * How long is the timer interval?
32  */
33 #define TIMER_INTERVAL  (TICKS_PER_uSEC * mSEC_10)
34 #if TIMER_INTERVAL >= 0x100000
35 #define TICKS2USECS(x)  (256 * (x) / TICKS_PER_uSEC)
36 #elif TIMER_INTERVAL >= 0x10000
37 #define TICKS2USECS(x)  (16 * (x) / TICKS_PER_uSEC)
38 #else
39 #define TICKS2USECS(x)  ((x) / TICKS_PER_uSEC)
40 #endif
41
42 #define TIMER_CTRL_IE   (1 << 5)                        /* Interrupt Enable */
43
44 /*
45  * What does it look like?
46  */
47 typedef struct TimerStruct {
48         unsigned long TimerLoad;
49         unsigned long TimerValue;
50         unsigned long TimerControl;
51         unsigned long TimerClear;
52 } TimerStruct_t;
53
54 extern unsigned long (*gettimeoffset)(void);
55
56 static unsigned long timer_reload;
57
58 /*
59  * Returns number of ms since last clock interrupt.  Note that interrupts
60  * will have been disabled by do_gettimeoffset()
61  */
62 static unsigned long integrator_gettimeoffset(void)
63 {
64         volatile TimerStruct_t *timer1 = (TimerStruct_t *)TIMER1_VA_BASE;
65         unsigned long ticks1, ticks2, status;
66
67         /*
68          * Get the current number of ticks.  Note that there is a race
69          * condition between us reading the timer and checking for
70          * an interrupt.  We get around this by ensuring that the
71          * counter has not reloaded between our two reads.
72          */
73         ticks2 = timer1->TimerValue & 0xffff;
74         do {
75                 ticks1 = ticks2;
76                 status = __raw_readl(VA_IC_BASE + IRQ_RAW_STATUS);
77                 ticks2 = timer1->TimerValue & 0xffff;
78         } while (ticks2 > ticks1);
79
80         /*
81          * Number of ticks since last interrupt.
82          */
83         ticks1 = timer_reload - ticks2;
84
85         /*
86          * Interrupt pending?  If so, we've reloaded once already.
87          */
88         if (status & (1 << IRQ_TIMERINT1))
89                 ticks1 += timer_reload;
90
91         /*
92          * Convert the ticks to usecs
93          */
94         return TICKS2USECS(ticks1);
95 }
96
97 /*
98  * IRQ handler for the timer
99  */
100 static irqreturn_t
101 integrator_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
102 {
103         volatile TimerStruct_t *timer1 = (volatile TimerStruct_t *)TIMER1_VA_BASE;
104
105         // ...clear the interrupt
106         timer1->TimerClear = 1;
107
108         do_leds();
109         do_timer(regs);
110         do_profile(regs);
111
112         return IRQ_HANDLED;
113 }
114
115 /*
116  * Set up timer interrupt, and return the current time in seconds.
117  */
118 void __init time_init(void)
119 {
120         volatile TimerStruct_t *timer0 = (volatile TimerStruct_t *)TIMER0_VA_BASE;
121         volatile TimerStruct_t *timer1 = (volatile TimerStruct_t *)TIMER1_VA_BASE;
122         volatile TimerStruct_t *timer2 = (volatile TimerStruct_t *)TIMER2_VA_BASE;
123         unsigned int timer_ctrl = 0x80 | 0x40;  /* periodic */
124
125         if (machine_is_integrator()) {
126                 timer_reload = 1000000 * TICKS_PER_uSEC / HZ;
127         } else if (machine_is_cintegrator()) {
128                 timer_reload = 1000000 / HZ;
129                 timer_ctrl |= TIMER_CTRL_IE;
130         }
131         if (timer_reload > 0x100000) {
132                 timer_reload >>= 8;
133                 timer_ctrl |= 0x08; /* /256 */
134         } else if (timer_reload > 0x010000) {
135                 timer_reload >>= 4;
136                 timer_ctrl |= 0x04; /* /16 */
137         }
138
139         /*
140          * Initialise to a known state (all timers off)
141          */
142         timer0->TimerControl = 0;
143         timer1->TimerControl = 0;
144         timer2->TimerControl = 0;
145
146         timer1->TimerLoad    = timer_reload;
147         timer1->TimerValue   = timer_reload;
148         timer1->TimerControl = timer_ctrl;
149
150         /* 
151          * Make irqs happen for the system timer
152          */
153         timer_irq.handler = integrator_timer_interrupt;
154         setup_irq(IRQ_TIMERINT1, &timer_irq);
155         gettimeoffset = integrator_gettimeoffset;
156 }