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/config.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/delay.h>
17 #include <linux/interrupt.h>
18 #include <linux/time.h>
19 #include <linux/signal.h>
20 #include <linux/errno.h>
21 #include <linux/sched.h>
23 #include <asm/system.h>
24 #include <asm/hardware.h>
28 #include <asm/mach/irq.h>
29 #include <asm/mach/time.h>
30 #include <asm/arch/pxa-regs.h>
33 static inline unsigned long pxa_get_rtc_time(void)
38 static int pxa_set_rtc(void)
40 unsigned long current_time = xtime.tv_sec;
42 if (RTSR & RTSR_ALE) {
43 /* make sure not to forward the clock over an alarm */
44 unsigned long alarm = RTAR;
45 if (current_time >= alarm && alarm >= RCNR)
52 /* IRQs are disabled before entering here from do_gettimeofday() */
53 static unsigned long pxa_gettimeoffset (void)
55 long ticks_to_match, elapsed, usec;
57 /* Get ticks before next timer match */
58 ticks_to_match = OSMR0 - OSCR;
60 /* We need elapsed ticks since last match */
61 elapsed = LATCH - ticks_to_match;
63 /* don't get fooled by the workaround in pxa_timer_interrupt() */
67 /* Now convert them to usec */
68 usec = (unsigned long)(elapsed * (tick_nsec / 1000))/LATCH;
73 #ifdef CONFIG_NO_IDLE_HZ
74 static unsigned long initial_match;
75 static int match_posponed;
79 pxa_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
83 write_seqlock(&xtime_lock);
85 #ifdef CONFIG_NO_IDLE_HZ
88 OSMR0 = initial_match;
92 /* Loop until we get ahead of the free running timer.
93 * This ensures an exact clock tick count and time accuracy.
94 * Since IRQs are disabled at this point, coherence between
95 * lost_ticks(updated in do_timer()) and the match reg value is
96 * ensured, hence we can use do_gettimeofday() from interrupt
99 * HACK ALERT: it seems that the PXA timer regs aren't updated right
100 * away in all cases when a write occurs. We therefore compare with
101 * 8 instead of 0 in the while() condition below to avoid missing a
102 * match if OSCR has already reached the next OSMR value.
103 * Experience has shown that up to 6 ticks are needed to work around
104 * this problem, but let's use 8 to be conservative. Note that this
105 * affect things only when the timer IRQ has been delayed by nearly
106 * exactly one tick period which should be a pretty rare event.
110 OSSR = OSSR_M0; /* Clear match on timer 0 */
111 next_match = (OSMR0 += LATCH);
112 } while( (signed long)(next_match - OSCR) <= 8 );
114 write_sequnlock(&xtime_lock);
119 static struct irqaction pxa_timer_irq = {
120 .name = "PXA Timer Tick",
121 .flags = SA_INTERRUPT | SA_TIMER,
122 .handler = pxa_timer_interrupt,
125 static void __init pxa_timer_init(void)
129 set_rtc = pxa_set_rtc;
132 tv.tv_sec = pxa_get_rtc_time();
133 do_settimeofday(&tv);
135 OSMR0 = 0; /* set initial match at 0 */
136 OSSR = 0xf; /* clear status on all timers */
137 setup_irq(IRQ_OST0, &pxa_timer_irq);
138 OIER |= OIER_E0; /* enable match on timer 0 to cause interrupts */
139 OSCR = 0; /* initialize free-running timer, force first match */
142 #ifdef CONFIG_NO_IDLE_HZ
143 static int pxa_dyn_tick_enable_disable(void)
149 static void pxa_dyn_tick_reprogram(unsigned long ticks)
152 initial_match = OSMR0;
153 OSMR0 = initial_match + ticks * LATCH;
159 pxa_dyn_tick_handler(int irq, void *dev_id, struct pt_regs *regs)
161 if (match_posponed) {
163 OSMR0 = initial_match;
164 if ( (signed long)(initial_match - OSCR) <= 8 )
165 return pxa_timer_interrupt(irq, dev_id, regs);
170 static struct dyn_tick_timer pxa_dyn_tick = {
171 .enable = pxa_dyn_tick_enable_disable,
172 .disable = pxa_dyn_tick_enable_disable,
173 .reprogram = pxa_dyn_tick_reprogram,
174 .handler = pxa_dyn_tick_handler,
179 static unsigned long osmr[4], oier;
181 static void pxa_timer_suspend(void)
190 static void pxa_timer_resume(void)
199 * OSMR0 is the system timer: make sure OSCR is sufficiently behind
201 OSCR = OSMR0 - LATCH;
204 #define pxa_timer_suspend NULL
205 #define pxa_timer_resume NULL
208 struct sys_timer pxa_timer = {
209 .init = pxa_timer_init,
210 .suspend = pxa_timer_suspend,
211 .resume = pxa_timer_resume,
212 .offset = pxa_gettimeoffset,
213 #ifdef CONFIG_NO_IDLE_HZ
214 .dyn_tick = &pxa_dyn_tick,