Pull altix-mmr into release branch
[deliverable/linux.git] / arch / cris / kernel / time.c
1 /* $Id: time.c,v 1.18 2005/03/04 08:16:17 starvik Exp $
2 *
3 * linux/arch/cris/kernel/time.c
4 *
5 * Copyright (C) 1991, 1992, 1995 Linus Torvalds
6 * Copyright (C) 1999, 2000, 2001 Axis Communications AB
7 *
8 * 1994-07-02 Alan Modra
9 * fixed set_rtc_mmss, fixed time.year for >= 2000, new mktime
10 * 1995-03-26 Markus Kuhn
11 * fixed 500 ms bug at call to set_rtc_mmss, fixed DS12887
12 * precision CMOS clock update
13 * 1996-05-03 Ingo Molnar
14 * fixed time warps in do_[slow|fast]_gettimeoffset()
15 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
16 * "A Kernel Model for Precision Timekeeping" by Dave Mills
17 *
18 * Linux/CRIS specific code:
19 *
20 * Authors: Bjorn Wesen
21 * Johan Adolfsson
22 *
23 */
24
25 #include <asm/rtc.h>
26 #include <linux/errno.h>
27 #include <linux/module.h>
28 #include <linux/param.h>
29 #include <linux/jiffies.h>
30 #include <linux/bcd.h>
31 #include <linux/timex.h>
32 #include <linux/init.h>
33 #include <linux/profile.h>
34
35 u64 jiffies_64 = INITIAL_JIFFIES;
36
37 EXPORT_SYMBOL(jiffies_64);
38
39 int have_rtc; /* used to remember if we have an RTC or not */;
40
41 #define TICK_SIZE tick
42
43 extern unsigned long wall_jiffies;
44 extern unsigned long loops_per_jiffy; /* init/main.c */
45 unsigned long loops_per_usec;
46
47 extern unsigned long do_slow_gettimeoffset(void);
48 static unsigned long (*do_gettimeoffset)(void) = do_slow_gettimeoffset;
49
50 /*
51 * This version of gettimeofday has near microsecond resolution.
52 *
53 * Note: Division is quite slow on CRIS and do_gettimeofday is called
54 * rather often. Maybe we should do some kind of approximation here
55 * (a naive approximation would be to divide by 1024).
56 */
57 void do_gettimeofday(struct timeval *tv)
58 {
59 unsigned long flags;
60 signed long usec, sec;
61 local_irq_save(flags);
62 local_irq_disable();
63 usec = do_gettimeoffset();
64 {
65 unsigned long lost = jiffies - wall_jiffies;
66 if (lost)
67 usec += lost * (1000000 / HZ);
68 }
69
70 /*
71 * If time_adjust is negative then NTP is slowing the clock
72 * so make sure not to go into next possible interval.
73 * Better to lose some accuracy than have time go backwards..
74 */
75 if (unlikely(time_adjust < 0) && usec > tickadj)
76 usec = tickadj;
77
78 sec = xtime.tv_sec;
79 usec += xtime.tv_nsec / 1000;
80 local_irq_restore(flags);
81
82 while (usec >= 1000000) {
83 usec -= 1000000;
84 sec++;
85 }
86
87 tv->tv_sec = sec;
88 tv->tv_usec = usec;
89 }
90
91 EXPORT_SYMBOL(do_gettimeofday);
92
93 int do_settimeofday(struct timespec *tv)
94 {
95 time_t wtm_sec, sec = tv->tv_sec;
96 long wtm_nsec, nsec = tv->tv_nsec;
97
98 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
99 return -EINVAL;
100
101 write_seqlock_irq(&xtime_lock);
102 /*
103 * This is revolting. We need to set "xtime" correctly. However, the
104 * value in this location is the value at the most recent update of
105 * wall time. Discover what correction gettimeofday() would have
106 * made, and then undo it!
107 */
108 nsec -= do_gettimeoffset() * NSEC_PER_USEC;
109 nsec -= (jiffies - wall_jiffies) * TICK_NSEC;
110
111 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
112 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
113
114 set_normalized_timespec(&xtime, sec, nsec);
115 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
116
117 ntp_clear();
118 write_sequnlock_irq(&xtime_lock);
119 clock_was_set();
120 return 0;
121 }
122
123 EXPORT_SYMBOL(do_settimeofday);
124
125
126 /*
127 * BUG: This routine does not handle hour overflow properly; it just
128 * sets the minutes. Usually you'll only notice that after reboot!
129 */
130
131 int set_rtc_mmss(unsigned long nowtime)
132 {
133 int retval = 0;
134 int real_seconds, real_minutes, cmos_minutes;
135
136 printk(KERN_DEBUG "set_rtc_mmss(%lu)\n", nowtime);
137
138 if(!have_rtc)
139 return 0;
140
141 cmos_minutes = CMOS_READ(RTC_MINUTES);
142 BCD_TO_BIN(cmos_minutes);
143
144 /*
145 * since we're only adjusting minutes and seconds,
146 * don't interfere with hour overflow. This avoids
147 * messing with unknown time zones but requires your
148 * RTC not to be off by more than 15 minutes
149 */
150 real_seconds = nowtime % 60;
151 real_minutes = nowtime / 60;
152 if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
153 real_minutes += 30; /* correct for half hour time zone */
154 real_minutes %= 60;
155
156 if (abs(real_minutes - cmos_minutes) < 30) {
157 BIN_TO_BCD(real_seconds);
158 BIN_TO_BCD(real_minutes);
159 CMOS_WRITE(real_seconds,RTC_SECONDS);
160 CMOS_WRITE(real_minutes,RTC_MINUTES);
161 } else {
162 printk(KERN_WARNING
163 "set_rtc_mmss: can't update from %d to %d\n",
164 cmos_minutes, real_minutes);
165 retval = -1;
166 }
167
168 return retval;
169 }
170
171 /* grab the time from the RTC chip */
172
173 unsigned long
174 get_cmos_time(void)
175 {
176 unsigned int year, mon, day, hour, min, sec;
177
178 sec = CMOS_READ(RTC_SECONDS);
179 min = CMOS_READ(RTC_MINUTES);
180 hour = CMOS_READ(RTC_HOURS);
181 day = CMOS_READ(RTC_DAY_OF_MONTH);
182 mon = CMOS_READ(RTC_MONTH);
183 year = CMOS_READ(RTC_YEAR);
184
185 printk(KERN_DEBUG
186 "rtc: sec 0x%x min 0x%x hour 0x%x day 0x%x mon 0x%x year 0x%x\n",
187 sec, min, hour, day, mon, year);
188
189 BCD_TO_BIN(sec);
190 BCD_TO_BIN(min);
191 BCD_TO_BIN(hour);
192 BCD_TO_BIN(day);
193 BCD_TO_BIN(mon);
194 BCD_TO_BIN(year);
195
196 if ((year += 1900) < 1970)
197 year += 100;
198
199 return mktime(year, mon, day, hour, min, sec);
200 }
201
202 /* update xtime from the CMOS settings. used when /dev/rtc gets a SET_TIME.
203 * TODO: this doesn't reset the fancy NTP phase stuff as do_settimeofday does.
204 */
205
206 void
207 update_xtime_from_cmos(void)
208 {
209 if(have_rtc) {
210 xtime.tv_sec = get_cmos_time();
211 xtime.tv_nsec = 0;
212 }
213 }
214
215 extern void cris_profile_sample(struct pt_regs* regs);
216
217 void
218 cris_do_profile(struct pt_regs* regs)
219 {
220
221 #if CONFIG_SYSTEM_PROFILER
222 cris_profile_sample(regs);
223 #endif
224
225 #if CONFIG_PROFILING
226 profile_tick(CPU_PROFILING, regs);
227 #endif
228 }
229
230 /*
231 * Scheduler clock - returns current time in nanosec units.
232 */
233 unsigned long long sched_clock(void)
234 {
235 return (unsigned long long)jiffies * (1000000000 / HZ);
236 }
237
238 static int
239 __init init_udelay(void)
240 {
241 loops_per_usec = (loops_per_jiffy * HZ) / 1000000;
242 return 0;
243 }
244
245 __initcall(init_udelay);
This page took 0.035149 seconds and 6 git commands to generate.