2 * linux/include/asm-arm/arch-integrator/time.h
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.
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.
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
18 #include <asm/system.h>
20 #include <asm/mach-types.h>
23 * Where is the timer (VA)?
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)
31 * How long is the timer interval?
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)
39 #define TICKS2USECS(x) ((x) / TICKS_PER_uSEC)
42 #define TIMER_CTRL_IE (1 << 5) /* Interrupt Enable */
45 * What does it look like?
47 typedef struct TimerStruct {
48 unsigned long TimerLoad;
49 unsigned long TimerValue;
50 unsigned long TimerControl;
51 unsigned long TimerClear;
54 extern unsigned long (*gettimeoffset)(void);
56 static unsigned long timer_reload;
59 * Returns number of ms since last clock interrupt. Note that interrupts
60 * will have been disabled by do_gettimeoffset()
62 static unsigned long integrator_gettimeoffset(void)
64 volatile TimerStruct_t *timer1 = (TimerStruct_t *)TIMER1_VA_BASE;
65 unsigned long ticks1, ticks2, status;
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.
73 ticks2 = timer1->TimerValue & 0xffff;
76 status = __raw_readl(VA_IC_BASE + IRQ_RAW_STATUS);
77 ticks2 = timer1->TimerValue & 0xffff;
78 } while (ticks2 > ticks1);
81 * Number of ticks since last interrupt.
83 ticks1 = timer_reload - ticks2;
86 * Interrupt pending? If so, we've reloaded once already.
88 if (status & (1 << IRQ_TIMERINT1))
89 ticks1 += timer_reload;
92 * Convert the ticks to usecs
94 return TICKS2USECS(ticks1);
98 * IRQ handler for the timer
101 integrator_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
103 volatile TimerStruct_t *timer1 = (volatile TimerStruct_t *)TIMER1_VA_BASE;
105 // ...clear the interrupt
106 timer1->TimerClear = 1;
116 * Set up timer interrupt, and return the current time in seconds.
118 void __init time_init(void)
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 */
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;
131 if (timer_reload > 0x100000) {
133 timer_ctrl |= 0x08; /* /256 */
134 } else if (timer_reload > 0x010000) {
136 timer_ctrl |= 0x04; /* /16 */
140 * Initialise to a known state (all timers off)
142 timer0->TimerControl = 0;
143 timer1->TimerControl = 0;
144 timer2->TimerControl = 0;
146 timer1->TimerLoad = timer_reload;
147 timer1->TimerValue = timer_reload;
148 timer1->TimerControl = timer_ctrl;
151 * Make irqs happen for the system timer
153 timer_irq.handler = integrator_timer_interrupt;
154 setup_irq(IRQ_TIMERINT1, &timer_irq);
155 gettimeoffset = integrator_gettimeoffset;