2 * arch/arm/mach-pxa/time.c
4 * Author: Nicolas Pitre
5 * Created: Jun 15, 2001
6 * Copyright: MontaVista Software Inc.
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.
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/delay.h>
16 #include <linux/interrupt.h>
17 #include <linux/time.h>
18 #include <linux/signal.h>
19 #include <linux/errno.h>
20 #include <linux/sched.h>
22 #include <asm/system.h>
23 #include <asm/hardware.h>
27 #include <asm/mach/irq.h>
28 #include <asm/mach/time.h>
29 #include <asm/arch/pxa-regs.h>
32 static inline unsigned long pxa_get_rtc_time(void)
37 static int pxa_set_rtc(void)
39 unsigned long current_time = xtime.tv_sec;
41 if (RTSR & RTSR_ALE) {
42 /* make sure not to forward the clock over an alarm */
43 unsigned long alarm = RTAR;
44 if (current_time >= alarm && alarm >= RCNR)
51 /* IRQs are disabled before entering here from do_gettimeofday() */
52 static unsigned long pxa_gettimeoffset (void)
54 long ticks_to_match, elapsed, usec;
56 /* Get ticks before next timer match */
57 ticks_to_match = OSMR0 - OSCR;
59 /* We need elapsed ticks since last match */
60 elapsed = LATCH - ticks_to_match;
62 /* don't get fooled by the workaround in pxa_timer_interrupt() */
66 /* Now convert them to usec */
67 usec = (unsigned long)(elapsed * (tick_nsec / 1000))/LATCH;
72 #ifdef CONFIG_NO_IDLE_HZ
73 static unsigned long initial_match;
74 static int match_posponed;
78 pxa_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
82 write_seqlock(&xtime_lock);
84 #ifdef CONFIG_NO_IDLE_HZ
87 OSMR0 = initial_match;
91 /* Loop until we get ahead of the free running timer.
92 * This ensures an exact clock tick count and time accuracy.
93 * Since IRQs are disabled at this point, coherence between
94 * lost_ticks(updated in do_timer()) and the match reg value is
95 * ensured, hence we can use do_gettimeofday() from interrupt
98 * HACK ALERT: it seems that the PXA timer regs aren't updated right
99 * away in all cases when a write occurs. We therefore compare with
100 * 8 instead of 0 in the while() condition below to avoid missing a
101 * match if OSCR has already reached the next OSMR value.
102 * Experience has shown that up to 6 ticks are needed to work around
103 * this problem, but let's use 8 to be conservative. Note that this
104 * affect things only when the timer IRQ has been delayed by nearly
105 * exactly one tick period which should be a pretty rare event.
109 OSSR = OSSR_M0; /* Clear match on timer 0 */
110 next_match = (OSMR0 += LATCH);
111 } while( (signed long)(next_match - OSCR) <= 8 );
113 write_sequnlock(&xtime_lock);
118 static struct irqaction pxa_timer_irq = {
119 .name = "PXA Timer Tick",
120 .flags = SA_INTERRUPT | SA_TIMER,
121 .handler = pxa_timer_interrupt,
124 static void __init pxa_timer_init(void)
128 set_rtc = pxa_set_rtc;
131 tv.tv_sec = pxa_get_rtc_time();
132 do_settimeofday(&tv);
134 OIER = 0; /* disable any timer interrupts */
135 OSCR = LATCH*2; /* push OSCR out of the way */
136 OSMR0 = LATCH; /* set initial match */
137 OSSR = 0xf; /* clear status on all timers */
138 setup_irq(IRQ_OST0, &pxa_timer_irq);
139 OIER = OIER_E0; /* enable match on timer 0 to cause interrupts */
140 OSCR = 0; /* initialize free-running timer */
143 #ifdef CONFIG_NO_IDLE_HZ
144 static int pxa_dyn_tick_enable_disable(void)
150 static void pxa_dyn_tick_reprogram(unsigned long ticks)
153 initial_match = OSMR0;
154 OSMR0 = initial_match + ticks * LATCH;
160 pxa_dyn_tick_handler(int irq, void *dev_id, struct pt_regs *regs)
162 if (match_posponed) {
164 OSMR0 = initial_match;
165 if ( (signed long)(initial_match - OSCR) <= 8 )
166 return pxa_timer_interrupt(irq, dev_id, regs);
171 static struct dyn_tick_timer pxa_dyn_tick = {
172 .enable = pxa_dyn_tick_enable_disable,
173 .disable = pxa_dyn_tick_enable_disable,
174 .reprogram = pxa_dyn_tick_reprogram,
175 .handler = pxa_dyn_tick_handler,
180 static unsigned long osmr[4], oier;
182 static void pxa_timer_suspend(void)
191 static void pxa_timer_resume(void)
200 * OSMR0 is the system timer: make sure OSCR is sufficiently behind
202 OSCR = OSMR0 - LATCH;
205 #define pxa_timer_suspend NULL
206 #define pxa_timer_resume NULL
209 struct sys_timer pxa_timer = {
210 .init = pxa_timer_init,
211 .suspend = pxa_timer_suspend,
212 .resume = pxa_timer_resume,
213 .offset = pxa_gettimeoffset,
214 #ifdef CONFIG_NO_IDLE_HZ
215 .dyn_tick = &pxa_dyn_tick,