blob: fdf7b016e7adeadfa5652ae9779803209eba3d91 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Thomas Gleixner119c6412006-07-01 22:32:38 +010014#include <linux/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070024static 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() */
39static 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
Nicolas Pitre569d2c32005-09-01 12:48:48 +010055#ifdef CONFIG_NO_IDLE_HZ
56static unsigned long initial_match;
57static int match_posponed;
58#endif
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static irqreturn_t
Linus Torvalds0cd61b62006-10-06 10:53:39 -070061sa1100_timer_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 unsigned int next_match;
64
65 write_seqlock(&xtime_lock);
66
Nicolas Pitre569d2c32005-09-01 12:48:48 +010067#ifdef CONFIG_NO_IDLE_HZ
68 if (match_posponed) {
69 match_posponed = 0;
70 OSMR0 = initial_match;
71 }
72#endif
73
Nicolas Pitre20e91262005-09-01 12:48:47 +010074 /*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 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) <= 0);
87
88 write_sequnlock(&xtime_lock);
89
90 return IRQ_HANDLED;
91}
92
93static struct irqaction sa1100_timer_irq = {
94 .name = "SA11xx 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 = sa1100_timer_interrupt,
Linus Torvalds1da177e2005-04-16 15:20:36 -070097};
98
99static void __init sa1100_timer_init(void)
100{
Nicolas Pitrebf468782006-11-20 22:17:09 +0100101 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 set_rtc = sa1100_set_rtc;
104
Nicolas Pitre5285eb52005-11-08 22:43:06 +0000105 OIER = 0; /* disable any timer interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 OSSR = 0xf; /* clear status on all timers */
107 setup_irq(IRQ_OST0, &sa1100_timer_irq);
Nicolas Pitrebf468782006-11-20 22:17:09 +0100108 local_irq_save(flags);
Nicolas Pitre5285eb52005-11-08 22:43:06 +0000109 OIER = OIER_E0; /* enable match on timer 0 to cause interrupts */
Nicolas Pitrebf468782006-11-20 22:17:09 +0100110 OSMR0 = OSCR + LATCH; /* set initial match */
111 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Nicolas Pitre569d2c32005-09-01 12:48:48 +0100114#ifdef CONFIG_NO_IDLE_HZ
115static int sa1100_dyn_tick_enable_disable(void)
116{
117 /* nothing to do */
118 return 0;
119}
120
121static 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
130static irqreturn_t
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700131sa1100_dyn_tick_handler(int irq, void *dev_id)
Nicolas Pitre569d2c32005-09-01 12:48:48 +0100132{
133 if (match_posponed) {
134 match_posponed = 0;
135 OSMR0 = initial_match;
136 if ((signed long)(initial_match - OSCR) <= 0)
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700137 return sa1100_timer_interrupt(irq, dev_id);
Nicolas Pitre569d2c32005-09-01 12:48:48 +0100138 }
139 return IRQ_NONE;
140}
141
142static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#ifdef CONFIG_PM
151unsigned long osmr[4], oier;
152
153static 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
162static 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
181struct sys_timer sa1100_timer = {
182 .init = sa1100_timer_init,
183 .suspend = sa1100_timer_suspend,
184 .resume = sa1100_timer_resume,
185 .offset = sa1100_gettimeoffset,
Nicolas Pitre569d2c32005-09-01 12:48:48 +0100186#ifdef CONFIG_NO_IDLE_HZ
187 .dyn_tick = &sa1100_dyn_tick,
188#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189};