blob: 3ac268fa419b68ce81c04af8e5b369d7f1aae1ca [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#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
22#include <asm/system.h>
23#include <asm/hardware.h>
24#include <asm/io.h>
25#include <asm/leds.h>
26#include <asm/irq.h>
27#include <asm/mach/irq.h>
28#include <asm/mach/time.h>
29#include <asm/arch/pxa-regs.h>
30
31
32static inline unsigned long pxa_get_rtc_time(void)
33{
34 return RCNR;
35}
36
37static 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() */
52static 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
Nicolas Pitre5c53ff02005-09-01 12:48:40 +010072#ifdef CONFIG_NO_IDLE_HZ
73static unsigned long initial_match;
74static int match_posponed;
75#endif
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static irqreturn_t
Linus Torvalds0cd61b62006-10-06 10:53:39 -070078pxa_timer_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 int next_match;
81
82 write_seqlock(&xtime_lock);
83
Nicolas Pitre5c53ff02005-09-01 12:48:40 +010084#ifdef CONFIG_NO_IDLE_HZ
85 if (match_posponed) {
86 match_posponed = 0;
87 OSMR0 = initial_match;
88 }
89#endif
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 /* Loop until we get ahead of the free running timer.
92 * This ensures an exact clock tick count and time accuracy.
Nicolas Pitre20e91262005-09-01 12:48:47 +010093 * Since IRQs are disabled at this point, coherence between
94 * lost_ticks(updated in do_timer()) and the match reg value is
95 * ensured, hence we can use do_gettimeofday() from interrupt
96 * handlers.
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 *
98 * HACK ALERT: it seems that the PXA timer regs aren't updated right
99 * away in all cases when a write occurs. We therefore compare with
100 * 8 instead of 0 in the while() condition below to avoid missing a
101 * match if OSCR has already reached the next OSMR value.
102 * Experience has shown that up to 6 ticks are needed to work around
103 * this problem, but let's use 8 to be conservative. Note that this
104 * affect things only when the timer IRQ has been delayed by nearly
105 * exactly one tick period which should be a pretty rare event.
106 */
107 do {
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700108 timer_tick();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 OSSR = OSSR_M0; /* Clear match on timer 0 */
110 next_match = (OSMR0 += LATCH);
111 } while( (signed long)(next_match - OSCR) <= 8 );
112
113 write_sequnlock(&xtime_lock);
114
115 return IRQ_HANDLED;
116}
117
118static struct irqaction pxa_timer_irq = {
119 .name = "PXA Timer Tick",
Thomas Gleixner52e405e2006-07-03 02:20:05 +0200120 .flags = IRQF_DISABLED | IRQF_TIMER,
Russell King09b8b5f2005-06-26 17:06:36 +0100121 .handler = pxa_timer_interrupt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122};
123
124static void __init pxa_timer_init(void)
125{
126 struct timespec tv;
127
128 set_rtc = pxa_set_rtc;
129
130 tv.tv_nsec = 0;
131 tv.tv_sec = pxa_get_rtc_time();
132 do_settimeofday(&tv);
133
Nicolas Pitre5285eb52005-11-08 22:43:06 +0000134 OIER = 0; /* disable any timer interrupts */
135 OSCR = LATCH*2; /* push OSCR out of the way */
136 OSMR0 = LATCH; /* set initial match */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 OSSR = 0xf; /* clear status on all timers */
138 setup_irq(IRQ_OST0, &pxa_timer_irq);
Nicolas Pitre5285eb52005-11-08 22:43:06 +0000139 OIER = OIER_E0; /* enable match on timer 0 to cause interrupts */
140 OSCR = 0; /* initialize free-running timer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Nicolas Pitre5c53ff02005-09-01 12:48:40 +0100143#ifdef CONFIG_NO_IDLE_HZ
144static int pxa_dyn_tick_enable_disable(void)
145{
146 /* nothing to do */
147 return 0;
148}
149
150static void pxa_dyn_tick_reprogram(unsigned long ticks)
151{
152 if (ticks > 1) {
153 initial_match = OSMR0;
154 OSMR0 = initial_match + ticks * LATCH;
155 match_posponed = 1;
156 }
157}
158
159static irqreturn_t
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700160pxa_dyn_tick_handler(int irq, void *dev_id)
Nicolas Pitre5c53ff02005-09-01 12:48:40 +0100161{
162 if (match_posponed) {
163 match_posponed = 0;
164 OSMR0 = initial_match;
165 if ( (signed long)(initial_match - OSCR) <= 8 )
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700166 return pxa_timer_interrupt(irq, dev_id);
Nicolas Pitre5c53ff02005-09-01 12:48:40 +0100167 }
168 return IRQ_NONE;
169}
170
171static struct dyn_tick_timer pxa_dyn_tick = {
172 .enable = pxa_dyn_tick_enable_disable,
173 .disable = pxa_dyn_tick_enable_disable,
174 .reprogram = pxa_dyn_tick_reprogram,
175 .handler = pxa_dyn_tick_handler,
176};
177#endif
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179#ifdef CONFIG_PM
180static unsigned long osmr[4], oier;
181
182static void pxa_timer_suspend(void)
183{
184 osmr[0] = OSMR0;
185 osmr[1] = OSMR1;
186 osmr[2] = OSMR2;
187 osmr[3] = OSMR3;
188 oier = OIER;
189}
190
191static void pxa_timer_resume(void)
192{
193 OSMR0 = osmr[0];
194 OSMR1 = osmr[1];
195 OSMR2 = osmr[2];
196 OSMR3 = osmr[3];
197 OIER = oier;
198
199 /*
200 * OSMR0 is the system timer: make sure OSCR is sufficiently behind
201 */
202 OSCR = OSMR0 - LATCH;
203}
204#else
205#define pxa_timer_suspend NULL
206#define pxa_timer_resume NULL
207#endif
208
209struct sys_timer pxa_timer = {
210 .init = pxa_timer_init,
211 .suspend = pxa_timer_suspend,
212 .resume = pxa_timer_resume,
213 .offset = pxa_gettimeoffset,
Nicolas Pitre5c53ff02005-09-01 12:48:40 +0100214#ifdef CONFIG_NO_IDLE_HZ
215 .dyn_tick = &pxa_dyn_tick,
216#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217};