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 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 #ifdef CONFIG_NO_IDLE_HZ
53 static unsigned long initial_match
;
54 static int match_posponed
;
58 pxa_timer_interrupt(int irq
, void *dev_id
)
62 write_seqlock(&xtime_lock
);
64 #ifdef CONFIG_NO_IDLE_HZ
67 OSMR0
= initial_match
;
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
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.
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
);
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)
109 static struct clocksource clocksource_pxa
= {
112 .read
= pxa_get_cycles
,
113 .mask
= CLOCKSOURCE_MASK(32),
115 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
118 static void __init
pxa_timer_init(void)
123 set_rtc
= pxa_set_rtc
;
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)
153 static void pxa_dyn_tick_reprogram(unsigned long ticks
)
156 initial_match
= OSMR0
;
157 OSMR0
= initial_match
+ ticks
* LATCH
;
163 pxa_dyn_tick_handler(int irq
, void *dev_id
)
165 if (match_posponed
) {
167 OSMR0
= initial_match
;
168 if ( (signed long)(initial_match
- OSCR
) <= 8 )
169 return pxa_timer_interrupt(irq
, dev_id
);
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
,
183 static unsigned long osmr
[4], oier
;
185 static void pxa_timer_suspend(void)
194 static void pxa_timer_resume(void)
203 * OSMR0 is the system timer: make sure OSCR is sufficiently behind
205 OSCR
= OSMR0
- LATCH
;
208 #define pxa_timer_suspend NULL
209 #define pxa_timer_resume NULL
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
,