2 * linux/include/asm-arm/arch-pxa/time.h
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.
14 static inline unsigned long pxa_get_rtc_time(void)
19 static int pxa_set_rtc(void)
21 unsigned long current_time = xtime.tv_sec;
23 if (RTSR & RTSR_ALE) {
24 /* make sure not to forward the clock over an alarm */
25 unsigned long alarm = RTAR;
26 if (current_time >= alarm && alarm >= RCNR)
33 /* IRQs are disabled before entering here from do_gettimeofday() */
34 static unsigned long pxa_gettimeoffset (void)
36 long ticks_to_match, elapsed, usec;
38 /* Get ticks before next timer match */
39 ticks_to_match = OSMR0 - OSCR;
41 /* We need elapsed ticks since last match */
42 elapsed = LATCH - ticks_to_match;
44 /* don't get fooled by the workaround in pxa_timer_interrupt() */
48 /* Now convert them to usec */
49 usec = (unsigned long)(elapsed * (tick_nsec / 1000))/LATCH;
55 pxa_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
61 /* Loop until we get ahead of the free running timer.
62 * This ensures an exact clock tick count and time accuracy.
63 * IRQs are disabled inside the loop to ensure coherence between
64 * lost_ticks (updated in do_timer()) and the match reg value, so we
65 * can use do_gettimeofday() from interrupt handlers.
67 * HACK ALERT: it seems that the PXA timer regs aren't updated right
68 * away in all cases when a write occurs. We therefore compare with
69 * 8 instead of 0 in the while() condition below to avoid missing a
70 * match if OSCR has already reached the next OSMR value.
71 * Experience has shown that up to 6 ticks are needed to work around
72 * this problem, but let's use 8 to be conservative. Note that this
73 * affect things only when the timer IRQ has been delayed by nearly
74 * exactly one tick period which should be a pretty rare event.
80 OSSR = OSSR_M0; /* Clear match on timer 0 */
81 next_match = (OSMR0 += LATCH);
82 } while( (signed long)(next_match - OSCR) <= 8 );
87 void __init time_init(void)
91 gettimeoffset = pxa_gettimeoffset;
92 set_rtc = pxa_set_rtc;
95 tv.tv_sec = pxa_get_rtc_time();
98 timer_irq.handler = pxa_timer_interrupt;
99 OSMR0 = 0; /* set initial match at 0 */
100 OSSR = 0xf; /* clear status on all timers */
101 setup_irq(IRQ_OST0, &timer_irq);
102 OIER |= OIER_E0; /* enable match on timer 0 to cause interrupts */
103 OSCR = 0; /* initialize free-running timer, force first match */