initial commit with v2.6.9
[linux-2.6.9-moxart.git] / arch / arm26 / kernel / time.c
blobdbbc21404727e9befbfd219cbc7b4364560f93a6
1 /*
2 * linux/arch/arm26/kernel/time.c
4 * Copyright (C) 1991, 1992, 1995 Linus Torvalds
5 * Modifications for ARM (C) 1994-2001 Russell King
6 * Mods for ARM26 (C) 2003 Ian Molton
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This file contains the ARM-specific time handling details:
13 * reading the RTC at bootup, etc...
15 * 1994-07-02 Alan Modra
16 * fixed set_rtc_mmss, fixed time.year for >= 2000, new mktime
17 * 1998-12-20 Updated NTP code according to technical memorandum Jan '96
18 * "A Kernel Model for Precision Timekeeping" by Dave Mills
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/interrupt.h>
25 #include <linux/time.h>
26 #include <linux/init.h>
27 #include <linux/smp.h>
28 #include <linux/timex.h>
29 #include <linux/errno.h>
30 #include <linux/profile.h>
32 #include <asm/hardware.h>
33 #include <asm/io.h>
34 #include <asm/irq.h>
35 #include <asm/leds.h>
37 u64 jiffies_64 = INITIAL_JIFFIES;
39 EXPORT_SYMBOL(jiffies_64);
41 extern unsigned long wall_jiffies;
43 /* this needs a better home */
44 spinlock_t rtc_lock = SPIN_LOCK_UNLOCKED;
46 /* change this if you have some constant time drift */
47 #define USECS_PER_JIFFY (1000000/HZ)
49 static int dummy_set_rtc(void)
51 return 0;
55 * hook for setting the RTC's idea of the current time.
57 int (*set_rtc)(void) = dummy_set_rtc;
59 static unsigned long dummy_gettimeoffset(void)
61 return 0;
65 * hook for getting the time offset. Note that it is
66 * always called with interrupts disabled.
68 unsigned long (*gettimeoffset)(void) = dummy_gettimeoffset;
70 static unsigned long next_rtc_update;
73 * If we have an externally synchronized linux clock, then update
74 * CMOS clock accordingly every ~11 minutes. set_rtc() has to be
75 * called as close as possible to 500 ms before the new second
76 * starts.
78 static inline void do_set_rtc(void)
80 if (time_status & STA_UNSYNC || set_rtc == NULL)
81 return;
83 //FIXME - timespec.tv_sec is a time_t not unsigned long
84 if (next_rtc_update &&
85 time_before((unsigned long)xtime.tv_sec, next_rtc_update))
86 return;
88 if (xtime.tv_nsec < 500000000 - ((unsigned) tick_nsec >> 1) &&
89 xtime.tv_nsec >= 500000000 + ((unsigned) tick_nsec >> 1))
90 return;
92 if (set_rtc())
94 * rtc update failed. Try again in 60s
96 next_rtc_update = xtime.tv_sec + 60;
97 else
98 next_rtc_update = xtime.tv_sec + 660;
101 #define do_leds()
103 void do_gettimeofday(struct timeval *tv)
105 unsigned long flags;
106 unsigned long seq;
107 unsigned long usec, sec, lost;
109 do {
110 seq = read_seqbegin_irqsave(&xtime_lock, flags);
111 usec = gettimeoffset();
113 lost = jiffies - wall_jiffies;
114 if (lost)
115 usec += lost * USECS_PER_JIFFY;
117 sec = xtime.tv_sec;
118 usec += xtime.tv_nsec / 1000;
119 } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
121 /* usec may have gone up a lot: be safe */
122 while (usec >= 1000000) {
123 usec -= 1000000;
124 sec++;
127 tv->tv_sec = sec;
128 tv->tv_usec = usec;
131 EXPORT_SYMBOL(do_gettimeofday);
133 int do_settimeofday(struct timespec *tv)
135 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
136 return -EINVAL;
138 write_seqlock_irq(&xtime_lock);
140 * This is revolting. We need to set "xtime" correctly. However, the
141 * value in this location is the value at the most recent update of
142 * wall time. Discover what correction gettimeofday() would have
143 * done, and then undo it!
145 tv->tv_nsec -= 1000 * (gettimeoffset() +
146 (jiffies - wall_jiffies) * USECS_PER_JIFFY);
148 while (tv->tv_nsec < 0) {
149 tv->tv_nsec += NSEC_PER_SEC;
150 tv->tv_sec--;
153 xtime.tv_sec = tv->tv_sec;
154 xtime.tv_nsec = tv->tv_nsec;
155 time_adjust = 0; /* stop active adjtime() */
156 time_status |= STA_UNSYNC;
157 time_maxerror = NTP_PHASE_LIMIT;
158 time_esterror = NTP_PHASE_LIMIT;
159 write_sequnlock_irq(&xtime_lock);
160 clock_was_set();
161 return 0;
164 EXPORT_SYMBOL(do_settimeofday);
166 static irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
168 do_timer(regs);
169 do_set_rtc(); //FIME - EVERY timer IRQ?
170 profile_tick(CPU_PROFILING, regs);
171 return IRQ_HANDLED; //FIXME - is this right?
174 static struct irqaction timer_irq = {
175 .name = "timer",
176 .flags = SA_INTERRUPT,
177 .handler = timer_interrupt,
180 extern void ioctime_init(void);
183 * Set up timer interrupt.
185 void __init time_init(void)
187 ioctime_init();
189 setup_irq(IRQ_TIMER, &timer_irq);