ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / h8300 / kernel / time.c
1 /*
2  *  linux/arch/h8300/kernel/time.c
3  *
4  *  Yoshinori Sato <ysato@users.sourceforge.jp>
5  *
6  *  Copied/hacked from:
7  *
8  *  linux/arch/m68k/kernel/time.c
9  *
10  *  Copyright (C) 1991, 1992, 1995  Linus Torvalds
11  *
12  * This file contains the m68k-specific time handling details.
13  * Most of the stuff is located in the machine specific files.
14  *
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
19 #include <linux/config.h> /* CONFIG_HEARTBEAT */
20 #include <linux/errno.h>
21 #include <linux/module.h>
22 #include <linux/sched.h>
23 #include <linux/kernel.h>
24 #include <linux/param.h>
25 #include <linux/string.h>
26 #include <linux/mm.h>
27 #include <linux/timex.h>
28 #include <linux/profile.h>
29
30 #include <asm/io.h>
31 #include <asm/target_time.h>
32
33 #define TICK_SIZE (tick_nsec / 1000)
34
35 u64 jiffies_64;
36
37 EXPORT_SYMBOL(jiffies_64);
38
39 static inline void do_profile (unsigned long pc)
40 {
41         if (prof_buffer && current->pid) {
42                 extern int _stext;
43                 pc -= (unsigned long) &_stext;
44                 pc >>= prof_shift;
45                 if (pc < prof_len)
46                         ++prof_buffer[pc];
47                 else
48                 /*
49                  * Don't ignore out-of-bounds PC values silently,
50                  * put them into the last histogram slot, so if
51                  * present, they will show up as a sharp peak.
52                  */
53                         ++prof_buffer[prof_len-1];
54         }
55 }
56
57 /*
58  * timer_interrupt() needs to keep up the real-time clock,
59  * as well as call the "do_timer()" routine every clocktick
60  */
61 static void timer_interrupt(int irq, void *dummy, struct pt_regs * regs)
62 {
63         /* may need to kick the hardware timer */
64         platform_timer_eoi();
65
66         do_timer(regs);
67
68         if (!user_mode(regs))
69                 do_profile(regs->pc);
70
71 }
72
73 void time_init(void)
74 {
75         unsigned int year, mon, day, hour, min, sec;
76
77         /* FIX by dqg : Set to zero for platforms that don't have tod */
78         /* without this time is undefined and can overflow time_t, causing  */
79         /* very stange errors */
80         year = 1980;
81         mon = day = 1;
82         hour = min = sec = 0;
83         platform_gettod (&year, &mon, &day, &hour, &min, &sec);
84
85         if ((year += 1900) < 1970)
86                 year += 100;
87         xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
88         xtime.tv_nsec = 0;
89
90         platform_timer_setup(timer_interrupt);
91 }
92
93 /*
94  * This version of gettimeofday has near microsecond resolution.
95  */
96 void do_gettimeofday(struct timeval *tv)
97 {
98         unsigned long flags;
99         unsigned long usec, sec;
100
101         read_lock_irqsave(&xtime_lock, flags);
102         usec = 0;
103         sec = xtime.tv_sec;
104         usec += (xtime.tv_nsec / 1000);
105         read_unlock_irqrestore(&xtime_lock, flags);
106
107         while (usec >= 1000000) {
108                 usec -= 1000000;
109                 sec++;
110         }
111
112         tv->tv_sec = sec;
113         tv->tv_usec = usec;
114 }
115
116 EXPORT_SYMBOL(do_gettimeofday);
117
118 int do_settimeofday(struct timespec *tv)
119 {
120         if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
121                 return -EINVAL;
122
123         write_lock_irq(&xtime_lock);
124         /* This is revolting. We need to set the xtime.tv_usec
125          * correctly. However, the value in this location is
126          * is value at the last tick.
127          * Discover what correction gettimeofday
128          * would have done, and then undo it!
129          */
130         while (tv->tv_nsec < 0) {
131                 tv->tv_nsec += NSEC_PER_SEC;
132                 tv->tv_sec--;
133         }
134
135         xtime.tv_sec = tv->tv_sec;
136         xtime.tv_nsec = tv->tv_nsec;
137         time_adjust = 0;                /* stop active adjtime() */
138         time_status |= STA_UNSYNC;
139         time_maxerror = NTP_PHASE_LIMIT;
140         time_esterror = NTP_PHASE_LIMIT;
141         write_sequnlock_irq(&xtime_lock);
142         clock_was_set();
143         return 0;
144 }
145
146 EXPORT_SYMBOL(do_settimeofday);
147
148 unsigned long long sched_clock(void)
149 {
150         return (unsigned long long)jiffies * (1000000000 / HZ);
151
152 }