initial commit with v2.6.9
[linux-2.6.9-moxart.git] / arch / arm / mach-sa1100 / time.c
blob1c3d082f8d491561c1a2e8b3c81f2eb673d6e229
1 /*
2 * linux/arch/arm/mach-sa1100/time.c
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.
11 #include <linux/init.h>
12 #include <linux/errno.h>
13 #include <linux/interrupt.h>
14 #include <linux/timex.h>
15 #include <linux/signal.h>
17 #include <asm/mach/time.h>
18 #include <asm/hardware.h>
20 #define RTC_DEF_DIVIDER (32768 - 1)
21 #define RTC_DEF_TRIM 0
23 static unsigned long __init sa1100_get_rtc_time(void)
26 * According to the manual we should be able to let RTTR be zero
27 * and then a default diviser for a 32.768KHz clock is used.
28 * Apparently this doesn't work, at least for my SA1110 rev 5.
29 * If the clock divider is uninitialized then reset it to the
30 * default value to get the 1Hz clock.
32 if (RTTR == 0) {
33 RTTR = RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16);
34 printk(KERN_WARNING "Warning: uninitialized Real Time Clock\n");
35 /* The current RTC value probably doesn't make sense either */
36 RCNR = 0;
37 return 0;
39 return RCNR;
42 static int sa1100_set_rtc(void)
44 unsigned long current_time = xtime.tv_sec;
46 if (RTSR & RTSR_ALE) {
47 /* make sure not to forward the clock over an alarm */
48 unsigned long alarm = RTAR;
49 if (current_time >= alarm && alarm >= RCNR)
50 return -ERESTARTSYS;
52 RCNR = current_time;
53 return 0;
56 /* IRQs are disabled before entering here from do_gettimeofday() */
57 static unsigned long sa1100_gettimeoffset (void)
59 unsigned long ticks_to_match, elapsed, usec;
61 /* Get ticks before next timer match */
62 ticks_to_match = OSMR0 - OSCR;
64 /* We need elapsed ticks since last match */
65 elapsed = LATCH - ticks_to_match;
67 /* Now convert them to usec */
68 usec = (unsigned long)(elapsed * (tick_nsec / 1000))/LATCH;
70 return usec;
74 * We will be entered with IRQs enabled.
76 * Loop until we get ahead of the free running timer.
77 * This ensures an exact clock tick count and time accuracy.
78 * IRQs are disabled inside the loop to ensure coherence between
79 * lost_ticks (updated in do_timer()) and the match reg value, so we
80 * can use do_gettimeofday() from interrupt handlers.
82 static irqreturn_t
83 sa1100_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
85 unsigned int next_match;
87 do {
88 timer_tick(regs);
89 OSSR = OSSR_M0; /* Clear match on timer 0 */
90 next_match = (OSMR0 += LATCH);
91 } while ((signed long)(next_match - OSCR) <= 0);
93 return IRQ_HANDLED;
96 static struct irqaction sa1100_timer_irq = {
97 .name = "SA11xx Timer Tick",
98 .flags = SA_INTERRUPT,
99 .handler = sa1100_timer_interrupt
102 void __init sa1100_init_time(void)
104 struct timespec tv;
106 gettimeoffset = sa1100_gettimeoffset;
107 set_rtc = sa1100_set_rtc;
109 tv.tv_nsec = 0;
110 tv.tv_sec = sa1100_get_rtc_time();
111 do_settimeofday(&tv);
113 OSMR0 = 0; /* set initial match at 0 */
114 OSSR = 0xf; /* clear status on all timers */
115 setup_irq(IRQ_OST0, &sa1100_timer_irq);
116 OIER |= OIER_E0; /* enable match on timer 0 to cause interrupts */
117 OSCR = 0; /* initialize free-running timer, force first match */