blob: 3775b8f3842942c209a4238b7745faaaa24dcd68 [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>
Sascha Hauerc80204e2006-12-12 09:21:50 +010021#include <linux/clocksource.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
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#include <asm/arch/pxa-regs.h>
31
32
33static inline unsigned long pxa_get_rtc_time(void)
34{
35 return RCNR;
36}
37
38static int pxa_set_rtc(void)
39{
40 unsigned long current_time = xtime.tv_sec;
41
42 if (RTSR & RTSR_ALE) {
43 /* make sure not to forward the clock over an alarm */
44 unsigned long alarm = RTAR;
45 if (current_time >= alarm && alarm >= RCNR)
46 return -ERESTARTSYS;
47 }
48 RCNR = current_time;
49 return 0;
50}
51
Nicolas Pitre5c53ff02005-09-01 12:48:40 +010052#ifdef CONFIG_NO_IDLE_HZ
53static unsigned long initial_match;
54static int match_posponed;
55#endif
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static irqreturn_t
Linus Torvalds0cd61b62006-10-06 10:53:39 -070058pxa_timer_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 int next_match;
61
62 write_seqlock(&xtime_lock);
63
Nicolas Pitre5c53ff02005-09-01 12:48:40 +010064#ifdef CONFIG_NO_IDLE_HZ
65 if (match_posponed) {
66 match_posponed = 0;
67 OSMR0 = initial_match;
68 }
69#endif
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 /* Loop until we get ahead of the free running timer.
72 * This ensures an exact clock tick count and time accuracy.
Nicolas Pitre20e91262005-09-01 12:48:47 +010073 * Since IRQs are disabled at this point, coherence between
74 * lost_ticks(updated in do_timer()) and the match reg value is
75 * ensured, hence we can use do_gettimeofday() from interrupt
76 * handlers.
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 *
78 * HACK ALERT: it seems that the PXA timer regs aren't updated right
79 * away in all cases when a write occurs. We therefore compare with
80 * 8 instead of 0 in the while() condition below to avoid missing a
81 * match if OSCR has already reached the next OSMR value.
82 * Experience has shown that up to 6 ticks are needed to work around
83 * this problem, but let's use 8 to be conservative. Note that this
84 * affect things only when the timer IRQ has been delayed by nearly
85 * exactly one tick period which should be a pretty rare event.
86 */
87 do {
Linus Torvalds0cd61b62006-10-06 10:53:39 -070088 timer_tick();
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 OSSR = OSSR_M0; /* Clear match on timer 0 */
90 next_match = (OSMR0 += LATCH);
91 } while( (signed long)(next_match - OSCR) <= 8 );
92
93 write_sequnlock(&xtime_lock);
94
95 return IRQ_HANDLED;
96}
97
98static struct irqaction pxa_timer_irq = {
99 .name = "PXA Timer Tick",
Thomas Gleixner52e405e2006-07-03 02:20:05 +0200100 .flags = IRQF_DISABLED | IRQF_TIMER,
Russell King09b8b5f2005-06-26 17:06:36 +0100101 .handler = pxa_timer_interrupt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
Sascha Hauerc80204e2006-12-12 09:21:50 +0100104cycle_t pxa_get_cycles(void)
105{
106 return OSCR;
107}
108
109static struct clocksource clocksource_pxa = {
110 .name = "pxa_timer",
111 .rating = 200,
112 .read = pxa_get_cycles,
113 .mask = CLOCKSOURCE_MASK(32),
114 .shift = 20,
115 .is_continuous = 1,
116};
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static void __init pxa_timer_init(void)
119{
120 struct timespec tv;
Nicolas Pitre46ec0ce2006-11-20 22:19:29 +0100121 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 set_rtc = pxa_set_rtc;
124
125 tv.tv_nsec = 0;
126 tv.tv_sec = pxa_get_rtc_time();
127 do_settimeofday(&tv);
128
Nicolas Pitre5285eb572005-11-08 22:43:06 +0000129 OIER = 0; /* disable any timer interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 OSSR = 0xf; /* clear status on all timers */
131 setup_irq(IRQ_OST0, &pxa_timer_irq);
Nicolas Pitre46ec0ce2006-11-20 22:19:29 +0100132 local_irq_save(flags);
Nicolas Pitre5285eb572005-11-08 22:43:06 +0000133 OIER = OIER_E0; /* enable match on timer 0 to cause interrupts */
Nicolas Pitre46ec0ce2006-11-20 22:19:29 +0100134 OSMR0 = OSCR + LATCH; /* set initial match */
135 local_irq_restore(flags);
Sascha Hauerc80204e2006-12-12 09:21:50 +0100136
137 /* on PXA OSCR runs continiously and is not written to, so we can use it
138 * as clock source directly.
139 */
140 clocksource_pxa.mult =
141 clocksource_hz2mult(CLOCK_TICK_RATE, clocksource_pxa.shift);
142 clocksource_register(&clocksource_pxa);
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Nicolas Pitre5c53ff02005-09-01 12:48:40 +0100146#ifdef CONFIG_NO_IDLE_HZ
147static int pxa_dyn_tick_enable_disable(void)
148{
149 /* nothing to do */
150 return 0;
151}
152
153static void pxa_dyn_tick_reprogram(unsigned long ticks)
154{
155 if (ticks > 1) {
156 initial_match = OSMR0;
157 OSMR0 = initial_match + ticks * LATCH;
158 match_posponed = 1;
159 }
160}
161
162static irqreturn_t
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700163pxa_dyn_tick_handler(int irq, void *dev_id)
Nicolas Pitre5c53ff02005-09-01 12:48:40 +0100164{
165 if (match_posponed) {
166 match_posponed = 0;
167 OSMR0 = initial_match;
168 if ( (signed long)(initial_match - OSCR) <= 8 )
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700169 return pxa_timer_interrupt(irq, dev_id);
Nicolas Pitre5c53ff02005-09-01 12:48:40 +0100170 }
171 return IRQ_NONE;
172}
173
174static struct dyn_tick_timer pxa_dyn_tick = {
175 .enable = pxa_dyn_tick_enable_disable,
176 .disable = pxa_dyn_tick_enable_disable,
177 .reprogram = pxa_dyn_tick_reprogram,
178 .handler = pxa_dyn_tick_handler,
179};
180#endif
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182#ifdef CONFIG_PM
183static unsigned long osmr[4], oier;
184
185static void pxa_timer_suspend(void)
186{
187 osmr[0] = OSMR0;
188 osmr[1] = OSMR1;
189 osmr[2] = OSMR2;
190 osmr[3] = OSMR3;
191 oier = OIER;
192}
193
194static void pxa_timer_resume(void)
195{
196 OSMR0 = osmr[0];
197 OSMR1 = osmr[1];
198 OSMR2 = osmr[2];
199 OSMR3 = osmr[3];
200 OIER = oier;
201
202 /*
203 * OSMR0 is the system timer: make sure OSCR is sufficiently behind
204 */
205 OSCR = OSMR0 - LATCH;
206}
207#else
208#define pxa_timer_suspend NULL
209#define pxa_timer_resume NULL
210#endif
211
212struct sys_timer pxa_timer = {
213 .init = pxa_timer_init,
214 .suspend = pxa_timer_suspend,
215 .resume = pxa_timer_resume,
Nicolas Pitre5c53ff02005-09-01 12:48:40 +0100216#ifdef CONFIG_NO_IDLE_HZ
217 .dyn_tick = &pxa_dyn_tick,
218#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219};