2 * linux/include/asm-arm/arch-sa1100/time.h
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.
13 #define RTC_DEF_DIVIDER (32768 - 1)
14 #define RTC_DEF_TRIM 0
16 static unsigned long __init sa1100_get_rtc_time(void)
19 * According to the manual we should be able to let RTTR be zero
20 * and then a default diviser for a 32.768KHz clock is used.
21 * Apparently this doesn't work, at least for my SA1110 rev 5.
22 * If the clock divider is uninitialized then reset it to the
23 * default value to get the 1Hz clock.
26 RTTR = RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16);
27 printk(KERN_WARNING "Warning: uninitialized Real Time Clock\n");
28 /* The current RTC value probably doesn't make sense either */
35 static int sa1100_set_rtc(void)
37 unsigned long current_time = xtime.tv_sec;
39 if (RTSR & RTSR_ALE) {
40 /* make sure not to forward the clock over an alarm */
41 unsigned long alarm = RTAR;
42 if (current_time >= alarm && alarm >= RCNR)
49 /* IRQs are disabled before entering here from do_gettimeofday() */
50 static unsigned long sa1100_gettimeoffset (void)
52 unsigned long ticks_to_match, elapsed, usec;
54 /* Get ticks before next timer match */
55 ticks_to_match = OSMR0 - OSCR;
57 /* We need elapsed ticks since last match */
58 elapsed = LATCH - ticks_to_match;
60 /* Now convert them to usec */
61 usec = (unsigned long)(elapsed * (tick_nsec / 1000))/LATCH;
67 * We will be entered with IRQs enabled.
69 * Loop until we get ahead of the free running timer.
70 * This ensures an exact clock tick count and time accuracy.
71 * IRQs are disabled inside the loop to ensure coherence between
72 * lost_ticks (updated in do_timer()) and the match reg value, so we
73 * can use do_gettimeofday() from interrupt handlers.
76 sa1100_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
78 unsigned int next_match;
83 OSSR = OSSR_M0; /* Clear match on timer 0 */
84 next_match = (OSMR0 += LATCH);
86 } while ((signed long)(next_match - OSCR) <= 0);
93 void __init time_init(void)
97 gettimeoffset = sa1100_gettimeoffset;
98 set_rtc = sa1100_set_rtc;
101 tv.tv_sec = sa1100_get_rtc_time();
102 do_settimeofday(&tv);
104 timer_irq.handler = sa1100_timer_interrupt;
105 OSMR0 = 0; /* set initial match at 0 */
106 OSSR = 0xf; /* clear status on all timers */
107 setup_irq(IRQ_OST0, &timer_irq);
108 OIER |= OIER_E0; /* enable match on timer 0 to cause interrupts */
109 OSCR = 0; /* initialize free-running timer, force first match */