fdf7b016e7adeadfa5652ae9779803209eba3d91
[safe/jmp/linux-2.6] / arch / arm / mach-sa1100 / time.c
1 /*
2  * linux/arch/arm/mach-sa1100/time.c
3  *
4  * Copyright (C) 1998 Deborah Wallach.
5  * Twiddles  (C) 1999   Hugo Fiennes <hugo@empeg.com>
6  * 
7  * 2000/03/29 (C) Nicolas Pitre <nico@cam.org>
8  *      Rewritten: big cleanup, much simpler, better HZ accuracy.
9  *
10  */
11 #include <linux/init.h>
12 #include <linux/errno.h>
13 #include <linux/interrupt.h>
14 #include <linux/irq.h>
15 #include <linux/timex.h>
16 #include <linux/signal.h>
17
18 #include <asm/mach/time.h>
19 #include <asm/hardware.h>
20
21 #define RTC_DEF_DIVIDER         (32768 - 1)
22 #define RTC_DEF_TRIM            0
23
24 static int sa1100_set_rtc(void)
25 {
26         unsigned long current_time = xtime.tv_sec;
27
28         if (RTSR & RTSR_ALE) {
29                 /* make sure not to forward the clock over an alarm */
30                 unsigned long alarm = RTAR;
31                 if (current_time >= alarm && alarm >= RCNR)
32                         return -ERESTARTSYS;
33         }
34         RCNR = current_time;
35         return 0;
36 }
37
38 /* IRQs are disabled before entering here from do_gettimeofday() */
39 static unsigned long sa1100_gettimeoffset (void)
40 {
41         unsigned long ticks_to_match, elapsed, usec;
42
43         /* Get ticks before next timer match */
44         ticks_to_match = OSMR0 - OSCR;
45
46         /* We need elapsed ticks since last match */
47         elapsed = LATCH - ticks_to_match;
48
49         /* Now convert them to usec */
50         usec = (unsigned long)(elapsed * (tick_nsec / 1000))/LATCH;
51
52         return usec;
53 }
54
55 #ifdef CONFIG_NO_IDLE_HZ
56 static unsigned long initial_match;
57 static int match_posponed;
58 #endif
59
60 static irqreturn_t
61 sa1100_timer_interrupt(int irq, void *dev_id)
62 {
63         unsigned int next_match;
64
65         write_seqlock(&xtime_lock);
66
67 #ifdef CONFIG_NO_IDLE_HZ
68         if (match_posponed) {
69                 match_posponed = 0;
70                 OSMR0 = initial_match;
71         }
72 #endif
73
74         /*
75          * Loop until we get ahead of the free running timer.
76          * This ensures an exact clock tick count and time accuracy.
77          * Since IRQs are disabled at this point, coherence between
78          * lost_ticks(updated in do_timer()) and the match reg value is
79          * ensured, hence we can use do_gettimeofday() from interrupt
80          * handlers.
81          */
82         do {
83                 timer_tick();
84                 OSSR = OSSR_M0;  /* Clear match on timer 0 */
85                 next_match = (OSMR0 += LATCH);
86         } while ((signed long)(next_match - OSCR) <= 0);
87
88         write_sequnlock(&xtime_lock);
89
90         return IRQ_HANDLED;
91 }
92
93 static struct irqaction sa1100_timer_irq = {
94         .name           = "SA11xx Timer Tick",
95         .flags          = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
96         .handler        = sa1100_timer_interrupt,
97 };
98
99 static void __init sa1100_timer_init(void)
100 {
101         unsigned long flags;
102
103         set_rtc = sa1100_set_rtc;
104
105         OIER = 0;               /* disable any timer interrupts */
106         OSSR = 0xf;             /* clear status on all timers */
107         setup_irq(IRQ_OST0, &sa1100_timer_irq);
108         local_irq_save(flags);
109         OIER = OIER_E0;         /* enable match on timer 0 to cause interrupts */
110         OSMR0 = OSCR + LATCH;   /* set initial match */
111         local_irq_restore(flags);
112 }
113
114 #ifdef CONFIG_NO_IDLE_HZ
115 static int sa1100_dyn_tick_enable_disable(void)
116 {
117         /* nothing to do */
118         return 0;
119 }
120
121 static void sa1100_dyn_tick_reprogram(unsigned long ticks)
122 {
123         if (ticks > 1) {
124                 initial_match = OSMR0;
125                 OSMR0 = initial_match + ticks * LATCH;
126                 match_posponed = 1;
127         }
128 }
129
130 static irqreturn_t
131 sa1100_dyn_tick_handler(int irq, void *dev_id)
132 {
133         if (match_posponed) {
134                 match_posponed = 0;
135                 OSMR0 = initial_match;
136                 if ((signed long)(initial_match - OSCR) <= 0)
137                         return sa1100_timer_interrupt(irq, dev_id);
138         }
139         return IRQ_NONE;
140 }
141
142 static struct dyn_tick_timer sa1100_dyn_tick = {
143         .enable         = sa1100_dyn_tick_enable_disable,
144         .disable        = sa1100_dyn_tick_enable_disable,
145         .reprogram      = sa1100_dyn_tick_reprogram,
146         .handler        = sa1100_dyn_tick_handler,
147 };
148 #endif
149
150 #ifdef CONFIG_PM
151 unsigned long osmr[4], oier;
152
153 static void sa1100_timer_suspend(void)
154 {
155         osmr[0] = OSMR0;
156         osmr[1] = OSMR1;
157         osmr[2] = OSMR2;
158         osmr[3] = OSMR3;
159         oier = OIER;
160 }
161
162 static void sa1100_timer_resume(void)
163 {
164         OSSR = 0x0f;
165         OSMR0 = osmr[0];
166         OSMR1 = osmr[1];
167         OSMR2 = osmr[2];
168         OSMR3 = osmr[3];
169         OIER = oier;
170
171         /*
172          * OSMR0 is the system timer: make sure OSCR is sufficiently behind
173          */
174         OSCR = OSMR0 - LATCH;
175 }
176 #else
177 #define sa1100_timer_suspend NULL
178 #define sa1100_timer_resume NULL
179 #endif
180
181 struct sys_timer sa1100_timer = {
182         .init           = sa1100_timer_init,
183         .suspend        = sa1100_timer_suspend,
184         .resume         = sa1100_timer_resume,
185         .offset         = sa1100_gettimeoffset,
186 #ifdef CONFIG_NO_IDLE_HZ
187         .dyn_tick       = &sa1100_dyn_tick,
188 #endif
189 };