blob: a084b38698cfdc6243c4deb4f90e161689daf8bc [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>
14#include <linux/timex.h>
15#include <linux/signal.h>
16
17#include <asm/mach/time.h>
18#include <asm/hardware.h>
19
20#define RTC_DEF_DIVIDER (32768 - 1)
21#define RTC_DEF_TRIM 0
22
23static unsigned long __init sa1100_get_rtc_time(void)
24{
25 /*
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.
31 */
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;
38 }
39 return RCNR;
40}
41
42static int sa1100_set_rtc(void)
43{
44 unsigned long current_time = xtime.tv_sec;
45
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;
51 }
52 RCNR = current_time;
53 return 0;
54}
55
56/* IRQs are disabled before entering here from do_gettimeofday() */
57static unsigned long sa1100_gettimeoffset (void)
58{
59 unsigned long ticks_to_match, elapsed, usec;
60
61 /* Get ticks before next timer match */
62 ticks_to_match = OSMR0 - OSCR;
63
64 /* We need elapsed ticks since last match */
65 elapsed = LATCH - ticks_to_match;
66
67 /* Now convert them to usec */
68 usec = (unsigned long)(elapsed * (tick_nsec / 1000))/LATCH;
69
70 return usec;
71}
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static irqreturn_t
74sa1100_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
75{
76 unsigned int next_match;
77
78 write_seqlock(&xtime_lock);
79
Nicolas Pitre20e91262005-09-01 12:48:47 +010080 /*
81 * Loop until we get ahead of the free running timer.
82 * This ensures an exact clock tick count and time accuracy.
83 * Since IRQs are disabled at this point, coherence between
84 * lost_ticks(updated in do_timer()) and the match reg value is
85 * ensured, hence we can use do_gettimeofday() from interrupt
86 * handlers.
87 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 do {
89 timer_tick(regs);
90 OSSR = OSSR_M0; /* Clear match on timer 0 */
91 next_match = (OSMR0 += LATCH);
92 } while ((signed long)(next_match - OSCR) <= 0);
93
94 write_sequnlock(&xtime_lock);
95
96 return IRQ_HANDLED;
97}
98
99static struct irqaction sa1100_timer_irq = {
100 .name = "SA11xx Timer Tick",
Russell King09b8b5f2005-06-26 17:06:36 +0100101 .flags = SA_INTERRUPT | SA_TIMER,
102 .handler = sa1100_timer_interrupt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103};
104
105static void __init sa1100_timer_init(void)
106{
107 struct timespec tv;
108
109 set_rtc = sa1100_set_rtc;
110
111 tv.tv_nsec = 0;
112 tv.tv_sec = sa1100_get_rtc_time();
113 do_settimeofday(&tv);
114
115 OSMR0 = 0; /* set initial match at 0 */
116 OSSR = 0xf; /* clear status on all timers */
117 setup_irq(IRQ_OST0, &sa1100_timer_irq);
118 OIER |= OIER_E0; /* enable match on timer 0 to cause interrupts */
119 OSCR = 0; /* initialize free-running timer, force first match */
120}
121
122#ifdef CONFIG_PM
123unsigned long osmr[4], oier;
124
125static void sa1100_timer_suspend(void)
126{
127 osmr[0] = OSMR0;
128 osmr[1] = OSMR1;
129 osmr[2] = OSMR2;
130 osmr[3] = OSMR3;
131 oier = OIER;
132}
133
134static void sa1100_timer_resume(void)
135{
136 OSSR = 0x0f;
137 OSMR0 = osmr[0];
138 OSMR1 = osmr[1];
139 OSMR2 = osmr[2];
140 OSMR3 = osmr[3];
141 OIER = oier;
142
143 /*
144 * OSMR0 is the system timer: make sure OSCR is sufficiently behind
145 */
146 OSCR = OSMR0 - LATCH;
147}
148#else
149#define sa1100_timer_suspend NULL
150#define sa1100_timer_resume NULL
151#endif
152
153struct sys_timer sa1100_timer = {
154 .init = sa1100_timer_init,
155 .suspend = sa1100_timer_suspend,
156 .resume = sa1100_timer_resume,
157 .offset = sa1100_gettimeoffset,
158};