Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-drm-fsl-dcu.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 #include <linux/sched.h>        /* just for sched_clock() - funny that */
35
36 int have_rtc;  /* used to remember if we have an RTC or not */;
37
38 #define TICK_SIZE tick
39
40 extern unsigned long loops_per_jiffy; /* init/main.c */
41 unsigned long loops_per_usec;
42
43 extern unsigned long do_slow_gettimeoffset(void);
44 static unsigned long (*do_gettimeoffset)(void) = do_slow_gettimeoffset;
45
46 /*
47  * This version of gettimeofday has near microsecond resolution.
48  *
49  * Note: Division is quite slow on CRIS and do_gettimeofday is called
50  *       rather often. Maybe we should do some kind of approximation here
51  *       (a naive approximation would be to divide by 1024).
52  */
53 void do_gettimeofday(struct timeval *tv)
54 {
55         unsigned long flags;
56         signed long usec, sec;
57         local_irq_save(flags);
58         usec = do_gettimeoffset();
59
60         /*
61          * If time_adjust is negative then NTP is slowing the clock
62          * so make sure not to go into next possible interval.
63          * Better to lose some accuracy than have time go backwards..
64          */
65         if (unlikely(time_adjust < 0) && usec > tickadj)
66                 usec = tickadj;
67
68         sec = xtime.tv_sec;
69         usec += xtime.tv_nsec / 1000;
70         local_irq_restore(flags);
71
72         while (usec >= 1000000) {
73                 usec -= 1000000;
74                 sec++;
75         }
76
77         tv->tv_sec = sec;
78         tv->tv_usec = usec;
79 }
80
81 EXPORT_SYMBOL(do_gettimeofday);
82
83 int do_settimeofday(struct timespec *tv)
84 {
85         time_t wtm_sec, sec = tv->tv_sec;
86         long wtm_nsec, nsec = tv->tv_nsec;
87
88         if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
89                 return -EINVAL;
90
91         write_seqlock_irq(&xtime_lock);
92         /*
93          * This is revolting. We need to set "xtime" correctly. However, the
94          * value in this location is the value at the most recent update of
95          * wall time.  Discover what correction gettimeofday() would have
96          * made, and then undo it!
97          */
98         nsec -= do_gettimeoffset() * NSEC_PER_USEC;
99
100         wtm_sec  = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
101         wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
102
103         set_normalized_timespec(&xtime, sec, nsec);
104         set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
105
106         ntp_clear();
107         write_sequnlock_irq(&xtime_lock);
108         clock_was_set();
109         return 0;
110 }
111
112 EXPORT_SYMBOL(do_settimeofday);
113
114
115 /*
116  * BUG: This routine does not handle hour overflow properly; it just
117  *      sets the minutes. Usually you'll only notice that after reboot!
118  */
119
120 int set_rtc_mmss(unsigned long nowtime)
121 {
122         int retval = 0;
123         int real_seconds, real_minutes, cmos_minutes;
124
125         printk(KERN_DEBUG "set_rtc_mmss(%lu)\n", nowtime);
126
127         if(!have_rtc)
128                 return 0;
129
130         cmos_minutes = CMOS_READ(RTC_MINUTES);
131         BCD_TO_BIN(cmos_minutes);
132
133         /*
134          * since we're only adjusting minutes and seconds,
135          * don't interfere with hour overflow. This avoids
136          * messing with unknown time zones but requires your
137          * RTC not to be off by more than 15 minutes
138          */
139         real_seconds = nowtime % 60;
140         real_minutes = nowtime / 60;
141         if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
142                 real_minutes += 30;             /* correct for half hour time zone */
143         real_minutes %= 60;
144
145         if (abs(real_minutes - cmos_minutes) < 30) {
146                 BIN_TO_BCD(real_seconds);
147                 BIN_TO_BCD(real_minutes);
148                 CMOS_WRITE(real_seconds,RTC_SECONDS);
149                 CMOS_WRITE(real_minutes,RTC_MINUTES);
150         } else {
151                 printk(KERN_WARNING
152                        "set_rtc_mmss: can't update from %d to %d\n",
153                        cmos_minutes, real_minutes);
154                 retval = -1;
155         }
156
157         return retval;
158 }
159
160 /* grab the time from the RTC chip */
161
162 unsigned long
163 get_cmos_time(void)
164 {
165         unsigned int year, mon, day, hour, min, sec;
166
167         sec = CMOS_READ(RTC_SECONDS);
168         min = CMOS_READ(RTC_MINUTES);
169         hour = CMOS_READ(RTC_HOURS);
170         day = CMOS_READ(RTC_DAY_OF_MONTH);
171         mon = CMOS_READ(RTC_MONTH);
172         year = CMOS_READ(RTC_YEAR);
173
174         printk(KERN_DEBUG
175                "rtc: sec 0x%x min 0x%x hour 0x%x day 0x%x mon 0x%x year 0x%x\n",
176                sec, min, hour, day, mon, year);
177
178         BCD_TO_BIN(sec);
179         BCD_TO_BIN(min);
180         BCD_TO_BIN(hour);
181         BCD_TO_BIN(day);
182         BCD_TO_BIN(mon);
183         BCD_TO_BIN(year);
184
185         if ((year += 1900) < 1970)
186                 year += 100;
187
188         return mktime(year, mon, day, hour, min, sec);
189 }
190
191 /* update xtime from the CMOS settings. used when /dev/rtc gets a SET_TIME.
192  * TODO: this doesn't reset the fancy NTP phase stuff as do_settimeofday does.
193  */
194
195 void
196 update_xtime_from_cmos(void)
197 {
198         if(have_rtc) {
199                 xtime.tv_sec = get_cmos_time();
200                 xtime.tv_nsec = 0;
201         }
202 }
203
204 extern void cris_profile_sample(struct pt_regs* regs);
205
206 void
207 cris_do_profile(struct pt_regs* regs)
208 {
209
210 #if CONFIG_SYSTEM_PROFILER
211         cris_profile_sample(regs);
212 #endif
213
214 #if CONFIG_PROFILING
215         profile_tick(CPU_PROFILING, regs);
216 #endif
217 }
218
219 static int
220 __init init_udelay(void)
221 {
222         loops_per_usec = (loops_per_jiffy * HZ) / 1000000;
223         return 0;
224 }
225
226 __initcall(init_udelay);