ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / asm-arm / arch-pxa / time.h
1 /*
2  * linux/include/asm-arm/arch-pxa/time.h
3  *
4  * Author:      Nicolas Pitre
5  * Created:     Jun 15, 2001
6  * Copyright:   MontaVista Software Inc.
7  *
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.
11  */
12
13
14 static inline unsigned long pxa_get_rtc_time(void)
15 {
16         return RCNR;
17 }
18
19 static int pxa_set_rtc(void)
20 {
21         unsigned long current_time = xtime.tv_sec;
22
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)
27                         return -ERESTARTSYS;
28         }
29         RCNR = current_time;
30         return 0;
31 }
32
33 /* IRQs are disabled before entering here from do_gettimeofday() */
34 static unsigned long pxa_gettimeoffset (void)
35 {
36         long ticks_to_match, elapsed, usec;
37
38         /* Get ticks before next timer match */
39         ticks_to_match = OSMR0 - OSCR;
40
41         /* We need elapsed ticks since last match */
42         elapsed = LATCH - ticks_to_match;
43
44         /* don't get fooled by the workaround in pxa_timer_interrupt() */
45         if (elapsed <= 0)
46                 return 0;
47
48         /* Now convert them to usec */
49         usec = (unsigned long)(elapsed * (tick_nsec / 1000))/LATCH;
50
51         return usec;
52 }
53
54 static irqreturn_t
55 pxa_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
56 {
57         int next_match;
58
59         do_profile(regs);
60
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.
66          *
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.
75          */
76         do {
77                 do_leds();
78                 do_set_rtc();
79                 do_timer(regs);
80                 OSSR = OSSR_M0;  /* Clear match on timer 0 */
81                 next_match = (OSMR0 += LATCH);
82         } while( (signed long)(next_match - OSCR) <= 8 );
83
84         return IRQ_HANDLED;
85 }
86
87 void __init time_init(void)
88 {
89         struct timespec tv;
90
91         gettimeoffset = pxa_gettimeoffset;
92         set_rtc = pxa_set_rtc;
93
94         tv.tv_nsec = 0;
95         tv.tv_sec = pxa_get_rtc_time();
96         do_settimeofday(&tv);
97
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 */
104 }
105