blob: 6f91fd2d061ab0b9dc5062d5db3c551e03f6198e [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070033static int pxa_set_rtc(void)
34{
35 unsigned long current_time = xtime.tv_sec;
36
37 if (RTSR & RTSR_ALE) {
38 /* make sure not to forward the clock over an alarm */
39 unsigned long alarm = RTAR;
40 if (current_time >= alarm && alarm >= RCNR)
41 return -ERESTARTSYS;
42 }
43 RCNR = current_time;
44 return 0;
45}
46
Nicolas Pitre5c53ff02005-09-01 12:48:40 +010047#ifdef CONFIG_NO_IDLE_HZ
48static unsigned long initial_match;
49static int match_posponed;
50#endif
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static irqreturn_t
Linus Torvalds0cd61b62006-10-06 10:53:39 -070053pxa_timer_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 int next_match;
56
57 write_seqlock(&xtime_lock);
58
Nicolas Pitre5c53ff02005-09-01 12:48:40 +010059#ifdef CONFIG_NO_IDLE_HZ
60 if (match_posponed) {
61 match_posponed = 0;
62 OSMR0 = initial_match;
63 }
64#endif
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 /* Loop until we get ahead of the free running timer.
67 * This ensures an exact clock tick count and time accuracy.
Nicolas Pitre20e91262005-09-01 12:48:47 +010068 * Since IRQs are disabled at this point, coherence between
69 * lost_ticks(updated in do_timer()) and the match reg value is
70 * ensured, hence we can use do_gettimeofday() from interrupt
71 * handlers.
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 *
73 * HACK ALERT: it seems that the PXA timer regs aren't updated right
74 * away in all cases when a write occurs. We therefore compare with
75 * 8 instead of 0 in the while() condition below to avoid missing a
76 * match if OSCR has already reached the next OSMR value.
77 * Experience has shown that up to 6 ticks are needed to work around
78 * this problem, but let's use 8 to be conservative. Note that this
79 * affect things only when the timer IRQ has been delayed by nearly
80 * exactly one tick period which should be a pretty rare event.
81 */
82 do {
Linus Torvalds0cd61b62006-10-06 10:53:39 -070083 timer_tick();
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 OSSR = OSSR_M0; /* Clear match on timer 0 */
85 next_match = (OSMR0 += LATCH);
86 } while( (signed long)(next_match - OSCR) <= 8 );
87
88 write_sequnlock(&xtime_lock);
89
90 return IRQ_HANDLED;
91}
92
93static struct irqaction pxa_timer_irq = {
94 .name = "PXA Timer Tick",
Bernhard Walleb30faba2007-05-08 00:35:39 -070095 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
Russell King09b8b5f2005-06-26 17:06:36 +010096 .handler = pxa_timer_interrupt,
Linus Torvalds1da177e2005-04-16 15:20:36 -070097};
98
Nicolas Pitre35108fb2006-12-22 18:36:30 +010099static cycle_t pxa_get_cycles(void)
Sascha Hauerc80204e2006-12-12 09:21:50 +0100100{
101 return OSCR;
102}
103
104static struct clocksource clocksource_pxa = {
105 .name = "pxa_timer",
106 .rating = 200,
107 .read = pxa_get_cycles,
108 .mask = CLOCKSOURCE_MASK(32),
109 .shift = 20,
Thomas Gleixnerc66699a2007-02-16 01:27:37 -0800110 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Sascha Hauerc80204e2006-12-12 09:21:50 +0100111};
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static void __init pxa_timer_init(void)
114{
115 struct timespec tv;
Nicolas Pitre46ec0ce2006-11-20 22:19:29 +0100116 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 set_rtc = pxa_set_rtc;
119
Nicolas Pitre5285eb572005-11-08 22:43:06 +0000120 OIER = 0; /* disable any timer interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 OSSR = 0xf; /* clear status on all timers */
122 setup_irq(IRQ_OST0, &pxa_timer_irq);
Nicolas Pitre46ec0ce2006-11-20 22:19:29 +0100123 local_irq_save(flags);
Nicolas Pitre5285eb572005-11-08 22:43:06 +0000124 OIER = OIER_E0; /* enable match on timer 0 to cause interrupts */
Nicolas Pitre46ec0ce2006-11-20 22:19:29 +0100125 OSMR0 = OSCR + LATCH; /* set initial match */
126 local_irq_restore(flags);
Sascha Hauerc80204e2006-12-12 09:21:50 +0100127
Nicolas Pitre35108fb2006-12-22 18:36:30 +0100128 /*
129 * OSCR runs continuously on PXA and is not written to,
130 * so we can use it as clock source directly.
Sascha Hauerc80204e2006-12-12 09:21:50 +0100131 */
132 clocksource_pxa.mult =
133 clocksource_hz2mult(CLOCK_TICK_RATE, clocksource_pxa.shift);
134 clocksource_register(&clocksource_pxa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Nicolas Pitre5c53ff02005-09-01 12:48:40 +0100137#ifdef CONFIG_NO_IDLE_HZ
138static int pxa_dyn_tick_enable_disable(void)
139{
140 /* nothing to do */
141 return 0;
142}
143
144static void pxa_dyn_tick_reprogram(unsigned long ticks)
145{
146 if (ticks > 1) {
147 initial_match = OSMR0;
148 OSMR0 = initial_match + ticks * LATCH;
149 match_posponed = 1;
150 }
151}
152
153static irqreturn_t
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700154pxa_dyn_tick_handler(int irq, void *dev_id)
Nicolas Pitre5c53ff02005-09-01 12:48:40 +0100155{
156 if (match_posponed) {
157 match_posponed = 0;
158 OSMR0 = initial_match;
159 if ( (signed long)(initial_match - OSCR) <= 8 )
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700160 return pxa_timer_interrupt(irq, dev_id);
Nicolas Pitre5c53ff02005-09-01 12:48:40 +0100161 }
162 return IRQ_NONE;
163}
164
165static struct dyn_tick_timer pxa_dyn_tick = {
166 .enable = pxa_dyn_tick_enable_disable,
167 .disable = pxa_dyn_tick_enable_disable,
168 .reprogram = pxa_dyn_tick_reprogram,
169 .handler = pxa_dyn_tick_handler,
170};
171#endif
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173#ifdef CONFIG_PM
174static unsigned long osmr[4], oier;
175
176static void pxa_timer_suspend(void)
177{
178 osmr[0] = OSMR0;
179 osmr[1] = OSMR1;
180 osmr[2] = OSMR2;
181 osmr[3] = OSMR3;
182 oier = OIER;
183}
184
185static void pxa_timer_resume(void)
186{
187 OSMR0 = osmr[0];
188 OSMR1 = osmr[1];
189 OSMR2 = osmr[2];
190 OSMR3 = osmr[3];
191 OIER = oier;
192
193 /*
194 * OSMR0 is the system timer: make sure OSCR is sufficiently behind
195 */
196 OSCR = OSMR0 - LATCH;
197}
198#else
199#define pxa_timer_suspend NULL
200#define pxa_timer_resume NULL
201#endif
202
203struct sys_timer pxa_timer = {
204 .init = pxa_timer_init,
205 .suspend = pxa_timer_suspend,
206 .resume = pxa_timer_resume,
Nicolas Pitre5c53ff02005-09-01 12:48:40 +0100207#ifdef CONFIG_NO_IDLE_HZ
208 .dyn_tick = &pxa_dyn_tick,
209#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210};