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