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>
21 #include <linux/clocksource.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 int pxa_set_rtc(void)
35 unsigned long current_time = xtime.tv_sec;
37 if (RTSR & RTSR_ALE) {
38 /* make sure not to forward the clock over an alarm */
39 unsigned long alarm = RTAR;
40 if (current_time >= alarm && alarm >= RCNR)
47 #ifdef CONFIG_NO_IDLE_HZ
48 static unsigned long initial_match;
49 static int match_posponed;
53 pxa_timer_interrupt(int irq, void *dev_id)
57 write_seqlock(&xtime_lock);
59 #ifdef CONFIG_NO_IDLE_HZ
62 OSMR0 = initial_match;
66 /* Loop until we get ahead of the free running timer.
67 * This ensures an exact clock tick count and time accuracy.
68 * Since IRQs are disabled at this point, coherence between
69 * lost_ticks(updated in do_timer()) and the match reg value is
70 * ensured, hence we can use do_gettimeofday() from interrupt
73 * HACK ALERT: it seems that the PXA timer regs aren't updated right
74 * away in all cases when a write occurs. We therefore compare with
75 * 8 instead of 0 in the while() condition below to avoid missing a
76 * match if OSCR has already reached the next OSMR value.
77 * Experience has shown that up to 6 ticks are needed to work around
78 * this problem, but let's use 8 to be conservative. Note that this
79 * affect things only when the timer IRQ has been delayed by nearly
80 * exactly one tick period which should be a pretty rare event.
84 OSSR = OSSR_M0; /* Clear match on timer 0 */
85 next_match = (OSMR0 += LATCH);
86 } while( (signed long)(next_match - OSCR) <= 8 );
88 write_sequnlock(&xtime_lock);
93 static struct irqaction pxa_timer_irq = {
94 .name = "PXA Timer Tick",
95 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
96 .handler = pxa_timer_interrupt,
99 static cycle_t pxa_get_cycles(void)
104 static struct clocksource clocksource_pxa = {
107 .read = pxa_get_cycles,
108 .mask = CLOCKSOURCE_MASK(32),
110 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
113 static void __init pxa_timer_init(void)
118 set_rtc = pxa_set_rtc;
120 OIER = 0; /* disable any timer interrupts */
121 OSSR = 0xf; /* clear status on all timers */
122 setup_irq(IRQ_OST0, &pxa_timer_irq);
123 local_irq_save(flags);
124 OIER = OIER_E0; /* enable match on timer 0 to cause interrupts */
125 OSMR0 = OSCR + LATCH; /* set initial match */
126 local_irq_restore(flags);
129 * OSCR runs continuously on PXA and is not written to,
130 * so we can use it as clock source directly.
132 clocksource_pxa.mult =
133 clocksource_hz2mult(CLOCK_TICK_RATE, clocksource_pxa.shift);
134 clocksource_register(&clocksource_pxa);
137 #ifdef CONFIG_NO_IDLE_HZ
138 static int pxa_dyn_tick_enable_disable(void)
144 static void pxa_dyn_tick_reprogram(unsigned long ticks)
147 initial_match = OSMR0;
148 OSMR0 = initial_match + ticks * LATCH;
154 pxa_dyn_tick_handler(int irq, void *dev_id)
156 if (match_posponed) {
158 OSMR0 = initial_match;
159 if ( (signed long)(initial_match - OSCR) <= 8 )
160 return pxa_timer_interrupt(irq, dev_id);
165 static struct dyn_tick_timer pxa_dyn_tick = {
166 .enable = pxa_dyn_tick_enable_disable,
167 .disable = pxa_dyn_tick_enable_disable,
168 .reprogram = pxa_dyn_tick_reprogram,
169 .handler = pxa_dyn_tick_handler,
174 static unsigned long osmr[4], oier;
176 static void pxa_timer_suspend(void)
185 static void pxa_timer_resume(void)
194 * OSMR0 is the system timer: make sure OSCR is sufficiently behind
196 OSCR = OSMR0 - LATCH;
199 #define pxa_timer_suspend NULL
200 #define pxa_timer_resume NULL
203 struct sys_timer pxa_timer = {
204 .init = pxa_timer_init,
205 .suspend = pxa_timer_suspend,
206 .resume = pxa_timer_resume,
207 #ifdef CONFIG_NO_IDLE_HZ
208 .dyn_tick = &pxa_dyn_tick,