ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / m68k / kernel / time.c
1 /*
2  *  linux/arch/m68k/kernel/time.c
3  *
4  *  Copyright (C) 1991, 1992, 1995  Linus Torvalds
5  *
6  * This file contains the m68k-specific time handling details.
7  * Most of the stuff is located in the machine specific files.
8  *
9  * 1997-09-10   Updated NTP code according to technical memorandum Jan '96
10  *              "A Kernel Model for Precision Timekeeping" by Dave Mills
11  */
12
13 #include <linux/config.h> /* CONFIG_HEARTBEAT */
14 #include <linux/errno.h>
15 #include <linux/module.h>
16 #include <linux/sched.h>
17 #include <linux/kernel.h>
18 #include <linux/param.h>
19 #include <linux/string.h>
20 #include <linux/mm.h>
21 #include <linux/rtc.h>
22
23 #include <asm/machdep.h>
24 #include <asm/io.h>
25
26 #include <linux/time.h>
27 #include <linux/timex.h>
28 #include <linux/profile.h>
29
30 u64 jiffies_64 = INITIAL_JIFFIES;
31
32 EXPORT_SYMBOL(jiffies_64);
33
34 static inline int set_rtc_mmss(unsigned long nowtime)
35 {
36   if (mach_set_clock_mmss)
37     return mach_set_clock_mmss (nowtime);
38   return -1;
39 }
40
41 static inline void do_profile (unsigned long pc)
42 {
43         if (prof_buffer && current->pid) {
44                 extern int _stext;
45                 pc -= (unsigned long) &_stext;
46                 pc >>= prof_shift;
47                 if (pc < prof_len)
48                         ++prof_buffer[pc];
49                 else
50                 /*
51                  * Don't ignore out-of-bounds PC values silently,
52                  * put them into the last histogram slot, so if
53                  * present, they will show up as a sharp peak.
54                  */
55                         ++prof_buffer[prof_len-1];
56         }
57 }
58
59 /*
60  * timer_interrupt() needs to keep up the real-time clock,
61  * as well as call the "do_timer()" routine every clocktick
62  */
63 static irqreturn_t timer_interrupt(int irq, void *dummy, struct pt_regs * regs)
64 {
65         do_timer(regs);
66
67         if (!user_mode(regs))
68                 do_profile(regs->pc);
69
70 #ifdef CONFIG_HEARTBEAT
71         /* use power LED as a heartbeat instead -- much more useful
72            for debugging -- based on the version for PReP by Cort */
73         /* acts like an actual heart beat -- ie thump-thump-pause... */
74         if (mach_heartbeat) {
75             static unsigned cnt = 0, period = 0, dist = 0;
76
77             if (cnt == 0 || cnt == dist)
78                 mach_heartbeat( 1 );
79             else if (cnt == 7 || cnt == dist+7)
80                 mach_heartbeat( 0 );
81
82             if (++cnt > period) {
83                 cnt = 0;
84                 /* The hyperbolic function below modifies the heartbeat period
85                  * length in dependency of the current (5min) load. It goes
86                  * through the points f(0)=126, f(1)=86, f(5)=51,
87                  * f(inf)->30. */
88                 period = ((672<<FSHIFT)/(5*avenrun[0]+(7<<FSHIFT))) + 30;
89                 dist = period / 4;
90             }
91         }
92 #endif /* CONFIG_HEARTBEAT */
93         return IRQ_HANDLED;
94 }
95
96 void time_init(void)
97 {
98         struct rtc_time time;
99
100         if (mach_hwclk) {
101                 mach_hwclk(0, &time);
102
103                 if ((time.tm_year += 1900) < 1970)
104                         time.tm_year += 100;
105                 xtime.tv_sec = mktime(time.tm_year, time.tm_mon, time.tm_mday,
106                                       time.tm_hour, time.tm_min, time.tm_sec);
107                 xtime.tv_nsec = 0;
108         }
109         wall_to_monotonic.tv_sec = -xtime.tv_sec;
110
111         mach_sched_init(timer_interrupt);
112 }
113
114 /*
115  * This version of gettimeofday has near microsecond resolution.
116  */
117 void do_gettimeofday(struct timeval *tv)
118 {
119         unsigned long flags;
120         extern unsigned long wall_jiffies;
121         unsigned long seq;
122         unsigned long usec, sec, lost;
123         unsigned long max_ntp_tick = tick_usec - tickadj;
124
125         do {
126                 seq = read_seqbegin_irqsave(&xtime_lock, flags);
127
128                 usec = mach_gettimeoffset();
129                 lost = jiffies - wall_jiffies;
130
131                 /*
132                  * If time_adjust is negative then NTP is slowing the clock
133                  * so make sure not to go into next possible interval.
134                  * Better to lose some accuracy than have time go backwards..
135                  */
136                 if (unlikely(time_adjust < 0)) {
137                         usec = min(usec, max_ntp_tick);
138
139                         if (lost)
140                                 usec += lost * max_ntp_tick;
141                 }
142                 else if (unlikely(lost))
143                         usec += lost * tick_usec;
144
145                 sec = xtime.tv_sec;
146                 usec += xtime.tv_nsec/1000;
147         } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
148
149
150         while (usec >= 1000000) {
151                 usec -= 1000000;
152                 sec++;
153         }
154
155         tv->tv_sec = sec;
156         tv->tv_usec = usec;
157 }
158
159 EXPORT_SYMBOL(do_gettimeofday);
160
161 int do_settimeofday(struct timespec *tv)
162 {
163         time_t wtm_sec, sec = tv->tv_sec;
164         long wtm_nsec, nsec = tv->tv_nsec;
165         extern unsigned long wall_jiffies;
166
167         if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
168                 return -EINVAL;
169
170         write_seqlock_irq(&xtime_lock);
171         /* This is revolting. We need to set the xtime.tv_nsec
172          * correctly. However, the value in this location is
173          * is value at the last tick.
174          * Discover what correction gettimeofday
175          * would have done, and then undo it!
176          */
177         nsec -= 1000 * (mach_gettimeoffset() +
178                         (jiffies - wall_jiffies) * (1000000 / HZ));
179
180         wtm_sec  = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
181         wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
182
183         set_normalized_timespec(&xtime, sec, nsec);
184         set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
185
186         time_adjust = 0;                /* stop active adjtime() */
187         time_status |= STA_UNSYNC;
188         time_maxerror = NTP_PHASE_LIMIT;
189         time_esterror = NTP_PHASE_LIMIT;
190         write_sequnlock_irq(&xtime_lock);
191         clock_was_set();
192         return 0;
193 }
194
195 EXPORT_SYMBOL(do_settimeofday);
196
197 /*
198  * Scheduler clock - returns current time in ns units.
199  */
200 unsigned long long sched_clock(void)
201 {
202        return (unsigned long long)jiffies*(1000000000/HZ);
203 }
204