http://downloads.netgear.com/files/GPL/GPL_Source_V361j_DM111PSP_series_consumer_rele...
[bcm963xx.git] / kernel / linux / arch / arm / mach-pxa / time.c
1 /*
2  * arch/arm/mach-pxa/time.c
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 #include <linux/config.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/delay.h>
17 #include <linux/interrupt.h>
18 #include <linux/time.h>
19 #include <linux/signal.h>
20 #include <linux/errno.h>
21 #include <linux/sched.h>
22
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
31
32 static inline unsigned long pxa_get_rtc_time(void)
33 {
34         return RCNR;
35 }
36
37 static int pxa_set_rtc(void)
38 {
39         unsigned long current_time = xtime.tv_sec;
40
41         if (RTSR & RTSR_ALE) {
42                 /* make sure not to forward the clock over an alarm */
43                 unsigned long alarm = RTAR;
44                 if (current_time >= alarm && alarm >= RCNR)
45                         return -ERESTARTSYS;
46         }
47         RCNR = current_time;
48         return 0;
49 }
50
51 /* IRQs are disabled before entering here from do_gettimeofday() */
52 static unsigned long pxa_gettimeoffset (void)
53 {
54         long ticks_to_match, elapsed, usec;
55
56         /* Get ticks before next timer match */
57         ticks_to_match = OSMR0 - OSCR;
58
59         /* We need elapsed ticks since last match */
60         elapsed = LATCH - ticks_to_match;
61
62         /* don't get fooled by the workaround in pxa_timer_interrupt() */
63         if (elapsed <= 0)
64                 return 0;
65
66         /* Now convert them to usec */
67         usec = (unsigned long)(elapsed * (tick_nsec / 1000))/LATCH;
68
69         return usec;
70 }
71
72 static irqreturn_t
73 pxa_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
74 {
75         int next_match;
76
77         /* Loop until we get ahead of the free running timer.
78          * This ensures an exact clock tick count and time accuracy.
79          * IRQs are disabled inside the loop to ensure coherence between
80          * lost_ticks (updated in do_timer()) and the match reg value, so we
81          * can use do_gettimeofday() from interrupt handlers.
82          *
83          * HACK ALERT: it seems that the PXA timer regs aren't updated right
84          * away in all cases when a write occurs.  We therefore compare with
85          * 8 instead of 0 in the while() condition below to avoid missing a
86          * match if OSCR has already reached the next OSMR value.
87          * Experience has shown that up to 6 ticks are needed to work around
88          * this problem, but let's use 8 to be conservative.  Note that this
89          * affect things only when the timer IRQ has been delayed by nearly
90          * exactly one tick period which should be a pretty rare event.
91          */
92         do {
93                 timer_tick(regs);
94                 OSSR = OSSR_M0;  /* Clear match on timer 0 */
95                 next_match = (OSMR0 += LATCH);
96         } while( (signed long)(next_match - OSCR) <= 8 );
97
98         return IRQ_HANDLED;
99 }
100
101 static struct irqaction pxa_timer_irq = {
102         .name           = "PXA Timer Tick",
103         .flags          = SA_INTERRUPT,
104         .handler        = pxa_timer_interrupt
105 };
106
107 void __init pxa_init_time(void)
108 {
109         struct timespec tv;
110
111         gettimeoffset = pxa_gettimeoffset;
112         set_rtc = pxa_set_rtc;
113
114         tv.tv_nsec = 0;
115         tv.tv_sec = pxa_get_rtc_time();
116         do_settimeofday(&tv);
117
118         OSMR0 = 0;              /* set initial match at 0 */
119         OSSR = 0xf;             /* clear status on all timers */
120         setup_irq(IRQ_OST0, &pxa_timer_irq);
121         OIER |= OIER_E0;        /* enable match on timer 0 to cause interrupts */
122         OSCR = 0;               /* initialize free-running timer, force first match */
123 }
124