2 * linux/arch/arm/mach-sa1100/time.c
4 * Copyright (C) 1998 Deborah Wallach.
5 * Twiddles (C) 1999 Hugo Fiennes <hugo@empeg.com>
7 * 2000/03/29 (C) Nicolas Pitre <nico@cam.org>
8 * Rewritten: big cleanup, much simpler, better HZ accuracy.
11 #include <linux/init.h>
12 #include <linux/errno.h>
13 #include <linux/interrupt.h>
14 #include <linux/irq.h>
15 #include <linux/timex.h>
16 #include <linux/signal.h>
18 #include <asm/mach/time.h>
19 #include <asm/hardware.h>
21 #define RTC_DEF_DIVIDER (32768 - 1)
22 #define RTC_DEF_TRIM 0
24 static int sa1100_set_rtc(void)
26 unsigned long current_time = xtime.tv_sec;
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)
38 /* IRQs are disabled before entering here from do_gettimeofday() */
39 static unsigned long sa1100_gettimeoffset (void)
41 unsigned long ticks_to_match, elapsed, usec;
43 /* Get ticks before next timer match */
44 ticks_to_match = OSMR0 - OSCR;
46 /* We need elapsed ticks since last match */
47 elapsed = LATCH - ticks_to_match;
49 /* Now convert them to usec */
50 usec = (unsigned long)(elapsed * (tick_nsec / 1000))/LATCH;
55 #ifdef CONFIG_NO_IDLE_HZ
56 static unsigned long initial_match;
57 static int match_posponed;
61 sa1100_timer_interrupt(int irq, void *dev_id)
63 unsigned int next_match;
65 write_seqlock(&xtime_lock);
67 #ifdef CONFIG_NO_IDLE_HZ
70 OSMR0 = initial_match;
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
84 OSSR = OSSR_M0; /* Clear match on timer 0 */
85 next_match = (OSMR0 += LATCH);
86 } while ((signed long)(next_match - OSCR) <= 0);
88 write_sequnlock(&xtime_lock);
93 static struct irqaction sa1100_timer_irq = {
94 .name = "SA11xx Timer Tick",
95 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
96 .handler = sa1100_timer_interrupt,
99 static void __init sa1100_timer_init(void)
103 set_rtc = sa1100_set_rtc;
105 OIER = 0; /* disable any timer interrupts */
106 OSSR = 0xf; /* clear status on all timers */
107 setup_irq(IRQ_OST0, &sa1100_timer_irq);
108 local_irq_save(flags);
109 OIER = OIER_E0; /* enable match on timer 0 to cause interrupts */
110 OSMR0 = OSCR + LATCH; /* set initial match */
111 local_irq_restore(flags);
114 #ifdef CONFIG_NO_IDLE_HZ
115 static int sa1100_dyn_tick_enable_disable(void)
121 static void sa1100_dyn_tick_reprogram(unsigned long ticks)
124 initial_match = OSMR0;
125 OSMR0 = initial_match + ticks * LATCH;
131 sa1100_dyn_tick_handler(int irq, void *dev_id)
133 if (match_posponed) {
135 OSMR0 = initial_match;
136 if ((signed long)(initial_match - OSCR) <= 0)
137 return sa1100_timer_interrupt(irq, dev_id);
142 static 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,
151 unsigned long osmr[4], oier;
153 static void sa1100_timer_suspend(void)
162 static void sa1100_timer_resume(void)
172 * OSMR0 is the system timer: make sure OSCR is sufficiently behind
174 OSCR = OSMR0 - LATCH;
177 #define sa1100_timer_suspend NULL
178 #define sa1100_timer_resume NULL
181 struct sys_timer sa1100_timer = {
182 .init = sa1100_timer_init,
183 .suspend = sa1100_timer_suspend,
184 .resume = sa1100_timer_resume,
185 .offset = sa1100_gettimeoffset,
186 #ifdef CONFIG_NO_IDLE_HZ
187 .dyn_tick = &sa1100_dyn_tick,