v2.6.22.24-op1
[linux-2.6.22.y-op.git] / arch / arm / mach-pxa / time.c
blob5248abe334d23f6966ea63f2c0a8462f69878261
1 /*
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>
25 #include <asm/io.h>
26 #include <asm/leds.h>
27 #include <asm/irq.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)
35 return RCNR;
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)
46 return -ERESTARTSYS;
48 RCNR = current_time;
49 return 0;
52 #ifdef CONFIG_NO_IDLE_HZ
53 static unsigned long initial_match;
54 static int match_posponed;
55 #endif
57 static irqreturn_t
58 pxa_timer_interrupt(int irq, void *dev_id)
60 int next_match;
62 write_seqlock(&xtime_lock);
64 #ifdef CONFIG_NO_IDLE_HZ
65 if (match_posponed) {
66 match_posponed = 0;
67 OSMR0 = initial_match;
69 #endif
71 /* Loop until we get ahead of the free running timer.
72 * This ensures an exact clock tick count and time accuracy.
73 * Since IRQs are disabled at this point, coherence between
74 * lost_ticks(updated in do_timer()) and the match reg value is
75 * ensured, hence we can use do_gettimeofday() from interrupt
76 * handlers.
78 * HACK ALERT: it seems that the PXA timer regs aren't updated right
79 * away in all cases when a write occurs. We therefore compare with
80 * 8 instead of 0 in the while() condition below to avoid missing a
81 * match if OSCR has already reached the next OSMR value.
82 * Experience has shown that up to 6 ticks are needed to work around
83 * this problem, but let's use 8 to be conservative. Note that this
84 * affect things only when the timer IRQ has been delayed by nearly
85 * exactly one tick period which should be a pretty rare event.
87 do {
88 timer_tick();
89 OSSR = OSSR_M0; /* Clear match on timer 0 */
90 next_match = (OSMR0 += LATCH);
91 } while( (signed long)(next_match - OSCR) <= 8 );
93 write_sequnlock(&xtime_lock);
95 return IRQ_HANDLED;
98 static struct irqaction pxa_timer_irq = {
99 .name = "PXA Timer Tick",
100 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
101 .handler = pxa_timer_interrupt,
104 static cycle_t pxa_get_cycles(void)
106 return OSCR;
109 static struct clocksource clocksource_pxa = {
110 .name = "pxa_timer",
111 .rating = 200,
112 .read = pxa_get_cycles,
113 .mask = CLOCKSOURCE_MASK(32),
114 .shift = 20,
115 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
118 static void __init pxa_timer_init(void)
120 struct timespec tv;
121 unsigned long flags;
123 set_rtc = pxa_set_rtc;
125 tv.tv_nsec = 0;
126 tv.tv_sec = pxa_get_rtc_time();
127 do_settimeofday(&tv);
129 OIER = 0; /* disable any timer interrupts */
130 OSSR = 0xf; /* clear status on all timers */
131 setup_irq(IRQ_OST0, &pxa_timer_irq);
132 local_irq_save(flags);
133 OIER = OIER_E0; /* enable match on timer 0 to cause interrupts */
134 OSMR0 = OSCR + LATCH; /* set initial match */
135 local_irq_restore(flags);
138 * OSCR runs continuously on PXA and is not written to,
139 * so we can use it as clock source directly.
141 clocksource_pxa.mult =
142 clocksource_hz2mult(CLOCK_TICK_RATE, clocksource_pxa.shift);
143 clocksource_register(&clocksource_pxa);
146 #ifdef CONFIG_NO_IDLE_HZ
147 static int pxa_dyn_tick_enable_disable(void)
149 /* nothing to do */
150 return 0;
153 static void pxa_dyn_tick_reprogram(unsigned long ticks)
155 if (ticks > 1) {
156 initial_match = OSMR0;
157 OSMR0 = initial_match + ticks * LATCH;
158 match_posponed = 1;
162 static irqreturn_t
163 pxa_dyn_tick_handler(int irq, void *dev_id)
165 if (match_posponed) {
166 match_posponed = 0;
167 OSMR0 = initial_match;
168 if ( (signed long)(initial_match - OSCR) <= 8 )
169 return pxa_timer_interrupt(irq, dev_id);
171 return IRQ_NONE;
174 static struct dyn_tick_timer pxa_dyn_tick = {
175 .enable = pxa_dyn_tick_enable_disable,
176 .disable = pxa_dyn_tick_enable_disable,
177 .reprogram = pxa_dyn_tick_reprogram,
178 .handler = pxa_dyn_tick_handler,
180 #endif
182 #ifdef CONFIG_PM
183 static unsigned long osmr[4], oier;
185 static void pxa_timer_suspend(void)
187 osmr[0] = OSMR0;
188 osmr[1] = OSMR1;
189 osmr[2] = OSMR2;
190 osmr[3] = OSMR3;
191 oier = OIER;
194 static void pxa_timer_resume(void)
196 OSMR0 = osmr[0];
197 OSMR1 = osmr[1];
198 OSMR2 = osmr[2];
199 OSMR3 = osmr[3];
200 OIER = oier;
203 * OSMR0 is the system timer: make sure OSCR is sufficiently behind
205 OSCR = OSMR0 - LATCH;
207 #else
208 #define pxa_timer_suspend NULL
209 #define pxa_timer_resume NULL
210 #endif
212 struct sys_timer pxa_timer = {
213 .init = pxa_timer_init,
214 .suspend = pxa_timer_suspend,
215 .resume = pxa_timer_resume,
216 #ifdef CONFIG_NO_IDLE_HZ
217 .dyn_tick = &pxa_dyn_tick,
218 #endif