blob: c2677368d6af9568d97616b6d0d6becb1a3a5385 [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>
Russell Kingd142b6e2007-11-12 21:55:12 +000017#include <linux/clocksource.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <asm/mach/time.h>
20#include <asm/hardware.h>
21
22#define RTC_DEF_DIVIDER (32768 - 1)
23#define RTC_DEF_TRIM 0
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025static int sa1100_set_rtc(void)
26{
27 unsigned long current_time = xtime.tv_sec;
28
29 if (RTSR & RTSR_ALE) {
30 /* make sure not to forward the clock over an alarm */
31 unsigned long alarm = RTAR;
32 if (current_time >= alarm && alarm >= RCNR)
33 return -ERESTARTSYS;
34 }
35 RCNR = current_time;
36 return 0;
37}
38
Nicolas Pitre569d2c32005-09-01 12:48:48 +010039#ifdef CONFIG_NO_IDLE_HZ
40static unsigned long initial_match;
41static int match_posponed;
42#endif
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static irqreturn_t
Linus Torvalds0cd61b62006-10-06 10:53:39 -070045sa1100_timer_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 unsigned int next_match;
48
Nicolas Pitre569d2c32005-09-01 12:48:48 +010049#ifdef CONFIG_NO_IDLE_HZ
50 if (match_posponed) {
51 match_posponed = 0;
52 OSMR0 = initial_match;
53 }
54#endif
55
Nicolas Pitre20e91262005-09-01 12:48:47 +010056 /*
57 * Loop until we get ahead of the free running timer.
58 * This ensures an exact clock tick count and time accuracy.
59 * Since IRQs are disabled at this point, coherence between
60 * lost_ticks(updated in do_timer()) and the match reg value is
61 * ensured, hence we can use do_gettimeofday() from interrupt
62 * handlers.
63 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 do {
Linus Torvalds0cd61b62006-10-06 10:53:39 -070065 timer_tick();
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 OSSR = OSSR_M0; /* Clear match on timer 0 */
67 next_match = (OSMR0 += LATCH);
68 } while ((signed long)(next_match - OSCR) <= 0);
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 return IRQ_HANDLED;
71}
72
73static struct irqaction sa1100_timer_irq = {
74 .name = "SA11xx Timer Tick",
Bernhard Walleb30faba2007-05-08 00:35:39 -070075 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
Russell King09b8b5f2005-06-26 17:06:36 +010076 .handler = sa1100_timer_interrupt,
Linus Torvalds1da177e2005-04-16 15:20:36 -070077};
78
Russell Kingd142b6e2007-11-12 21:55:12 +000079static cycle_t sa1100_read_oscr(void)
80{
81 return OSCR;
82}
83
84static struct clocksource cksrc_sa1100_oscr = {
85 .name = "oscr",
86 .rating = 200,
87 .read = sa1100_read_oscr,
88 .mask = CLOCKSOURCE_MASK(32),
89 .shift = 20,
90 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
91};
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static void __init sa1100_timer_init(void)
94{
Nicolas Pitrebf468782006-11-20 22:17:09 +010095 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 set_rtc = sa1100_set_rtc;
98
Nicolas Pitre5285eb572005-11-08 22:43:06 +000099 OIER = 0; /* disable any timer interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 OSSR = 0xf; /* clear status on all timers */
101 setup_irq(IRQ_OST0, &sa1100_timer_irq);
Nicolas Pitrebf468782006-11-20 22:17:09 +0100102 local_irq_save(flags);
Nicolas Pitre5285eb572005-11-08 22:43:06 +0000103 OIER = OIER_E0; /* enable match on timer 0 to cause interrupts */
Nicolas Pitrebf468782006-11-20 22:17:09 +0100104 OSMR0 = OSCR + LATCH; /* set initial match */
105 local_irq_restore(flags);
Russell Kingd142b6e2007-11-12 21:55:12 +0000106
107 cksrc_sa1100_oscr.mult =
108 clocksource_hz2mult(CLOCK_TICK_RATE, cksrc_sa1100_oscr.shift);
109
110 clocksource_register(&cksrc_sa1100_oscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Nicolas Pitre569d2c32005-09-01 12:48:48 +0100113#ifdef CONFIG_NO_IDLE_HZ
114static int sa1100_dyn_tick_enable_disable(void)
115{
116 /* nothing to do */
117 return 0;
118}
119
120static void sa1100_dyn_tick_reprogram(unsigned long ticks)
121{
122 if (ticks > 1) {
123 initial_match = OSMR0;
124 OSMR0 = initial_match + ticks * LATCH;
125 match_posponed = 1;
126 }
127}
128
129static irqreturn_t
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700130sa1100_dyn_tick_handler(int irq, void *dev_id)
Nicolas Pitre569d2c32005-09-01 12:48:48 +0100131{
132 if (match_posponed) {
133 match_posponed = 0;
134 OSMR0 = initial_match;
135 if ((signed long)(initial_match - OSCR) <= 0)
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700136 return sa1100_timer_interrupt(irq, dev_id);
Nicolas Pitre569d2c32005-09-01 12:48:48 +0100137 }
138 return IRQ_NONE;
139}
140
141static struct dyn_tick_timer sa1100_dyn_tick = {
142 .enable = sa1100_dyn_tick_enable_disable,
143 .disable = sa1100_dyn_tick_enable_disable,
144 .reprogram = sa1100_dyn_tick_reprogram,
145 .handler = sa1100_dyn_tick_handler,
146};
147#endif
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#ifdef CONFIG_PM
150unsigned long osmr[4], oier;
151
152static void sa1100_timer_suspend(void)
153{
154 osmr[0] = OSMR0;
155 osmr[1] = OSMR1;
156 osmr[2] = OSMR2;
157 osmr[3] = OSMR3;
158 oier = OIER;
159}
160
161static void sa1100_timer_resume(void)
162{
163 OSSR = 0x0f;
164 OSMR0 = osmr[0];
165 OSMR1 = osmr[1];
166 OSMR2 = osmr[2];
167 OSMR3 = osmr[3];
168 OIER = oier;
169
170 /*
171 * OSMR0 is the system timer: make sure OSCR is sufficiently behind
172 */
173 OSCR = OSMR0 - LATCH;
174}
175#else
176#define sa1100_timer_suspend NULL
177#define sa1100_timer_resume NULL
178#endif
179
180struct sys_timer sa1100_timer = {
181 .init = sa1100_timer_init,
182 .suspend = sa1100_timer_suspend,
183 .resume = sa1100_timer_resume,
Nicolas Pitre569d2c32005-09-01 12:48:48 +0100184#ifdef CONFIG_NO_IDLE_HZ
185 .dyn_tick = &sa1100_dyn_tick,
186#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187};