sh: rework clocksource and sched_clock
[linux-2.6] / arch / sh / kernel / time_32.c
1 /*
2  *  arch/sh/kernel/time_32.c
3  *
4  *  Copyright (C) 1999  Tetsuya Okada & Niibe Yutaka
5  *  Copyright (C) 2000  Philipp Rumpf <prumpf@tux.org>
6  *  Copyright (C) 2002 - 2008  Paul Mundt
7  *  Copyright (C) 2002  M. R. Brown  <mrbrown@linux-sh.org>
8  *
9  *  Some code taken from i386 version.
10  *    Copyright (C) 1991, 1992, 1995  Linus Torvalds
11  */
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/profile.h>
16 #include <linux/timex.h>
17 #include <linux/sched.h>
18 #include <linux/clockchips.h>
19 #include <linux/mc146818rtc.h>  /* for rtc_lock */
20 #include <linux/smp.h>
21 #include <asm/clock.h>
22 #include <asm/rtc.h>
23 #include <asm/timer.h>
24 #include <asm/kgdb.h>
25
26 struct sys_timer *sys_timer;
27
28 /* Move this somewhere more sensible.. */
29 DEFINE_SPINLOCK(rtc_lock);
30 EXPORT_SYMBOL(rtc_lock);
31
32 /* Dummy RTC ops */
33 static void null_rtc_get_time(struct timespec *tv)
34 {
35         tv->tv_sec = mktime(2000, 1, 1, 0, 0, 0);
36         tv->tv_nsec = 0;
37 }
38
39 static int null_rtc_set_time(const time_t secs)
40 {
41         return 0;
42 }
43
44 void (*rtc_sh_get_time)(struct timespec *) = null_rtc_get_time;
45 int (*rtc_sh_set_time)(const time_t) = null_rtc_set_time;
46
47 #ifndef CONFIG_GENERIC_TIME
48 void do_gettimeofday(struct timeval *tv)
49 {
50         unsigned long flags;
51         unsigned long seq;
52         unsigned long usec, sec;
53
54         do {
55                 /*
56                  * Turn off IRQs when grabbing xtime_lock, so that
57                  * the sys_timer get_offset code doesn't have to handle it.
58                  */
59                 seq = read_seqbegin_irqsave(&xtime_lock, flags);
60                 usec = get_timer_offset();
61                 sec = xtime.tv_sec;
62                 usec += xtime.tv_nsec / NSEC_PER_USEC;
63         } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
64
65         while (usec >= 1000000) {
66                 usec -= 1000000;
67                 sec++;
68         }
69
70         tv->tv_sec = sec;
71         tv->tv_usec = usec;
72 }
73 EXPORT_SYMBOL(do_gettimeofday);
74
75 int do_settimeofday(struct timespec *tv)
76 {
77         time_t wtm_sec, sec = tv->tv_sec;
78         long wtm_nsec, nsec = tv->tv_nsec;
79
80         if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
81                 return -EINVAL;
82
83         write_seqlock_irq(&xtime_lock);
84         /*
85          * This is revolting. We need to set "xtime" correctly. However, the
86          * value in this location is the value at the most recent update of
87          * wall time.  Discover what correction gettimeofday() would have
88          * made, and then undo it!
89          */
90         nsec -= get_timer_offset() * NSEC_PER_USEC;
91
92         wtm_sec  = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
93         wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
94
95         set_normalized_timespec(&xtime, sec, nsec);
96         set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
97
98         ntp_clear();
99         write_sequnlock_irq(&xtime_lock);
100         clock_was_set();
101
102         return 0;
103 }
104 EXPORT_SYMBOL(do_settimeofday);
105 #endif /* !CONFIG_GENERIC_TIME */
106
107 #ifndef CONFIG_GENERIC_CLOCKEVENTS
108 /* last time the RTC clock got updated */
109 static long last_rtc_update;
110
111 /*
112  * handle_timer_tick() needs to keep up the real-time clock,
113  * as well as call the "do_timer()" routine every clocktick
114  */
115 void handle_timer_tick(void)
116 {
117         if (current->pid)
118                 profile_tick(CPU_PROFILING);
119
120         /*
121          * Here we are in the timer irq handler. We just have irqs locally
122          * disabled but we don't know if the timer_bh is running on the other
123          * CPU. We need to avoid to SMP race with it. NOTE: we don' t need
124          * the irq version of write_lock because as just said we have irq
125          * locally disabled. -arca
126          */
127         write_seqlock(&xtime_lock);
128         do_timer(1);
129
130         /*
131          * If we have an externally synchronized Linux clock, then update
132          * RTC clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
133          * called as close as possible to 500 ms before the new second starts.
134          */
135         if (ntp_synced() &&
136             xtime.tv_sec > last_rtc_update + 660 &&
137             (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
138             (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
139                 if (rtc_sh_set_time(xtime.tv_sec) == 0)
140                         last_rtc_update = xtime.tv_sec;
141                 else
142                         /* do it again in 60s */
143                         last_rtc_update = xtime.tv_sec - 600;
144         }
145         write_sequnlock(&xtime_lock);
146
147 #ifndef CONFIG_SMP
148         update_process_times(user_mode(get_irq_regs()));
149 #endif
150 }
151 #endif /* !CONFIG_GENERIC_CLOCKEVENTS */
152
153 #ifdef CONFIG_PM
154 int timer_suspend(struct sys_device *dev, pm_message_t state)
155 {
156         struct sys_timer *sys_timer = container_of(dev, struct sys_timer, dev);
157
158         sys_timer->ops->stop();
159
160         return 0;
161 }
162
163 int timer_resume(struct sys_device *dev)
164 {
165         struct sys_timer *sys_timer = container_of(dev, struct sys_timer, dev);
166
167         sys_timer->ops->start();
168
169         return 0;
170 }
171 #else
172 #define timer_suspend NULL
173 #define timer_resume NULL
174 #endif
175
176 static struct sysdev_class timer_sysclass = {
177         .name    = "timer",
178         .suspend = timer_suspend,
179         .resume  = timer_resume,
180 };
181
182 static int __init timer_init_sysfs(void)
183 {
184         int ret = sysdev_class_register(&timer_sysclass);
185         if (ret != 0)
186                 return ret;
187
188         sys_timer->dev.cls = &timer_sysclass;
189         return sysdev_register(&sys_timer->dev);
190 }
191 device_initcall(timer_init_sysfs);
192
193 void (*board_time_init)(void);
194
195 struct clocksource clocksource_sh = {
196         .name           = "SuperH",
197 };
198
199 #ifdef CONFIG_GENERIC_TIME
200 unsigned long long sched_clock(void)
201 {
202         unsigned long long cycles;
203
204         /* jiffies based sched_clock if no clocksource is installed */
205         if (!clocksource_sh.rating)
206                 return (unsigned long long)jiffies * (NSEC_PER_SEC / HZ);
207
208         cycles = clocksource_sh.read();
209         return cyc2ns(&clocksource_sh, cycles);
210 }
211 #endif
212
213 void __init time_init(void)
214 {
215         if (board_time_init)
216                 board_time_init();
217
218         clk_init();
219
220         rtc_sh_get_time(&xtime);
221         set_normalized_timespec(&wall_to_monotonic,
222                                 -xtime.tv_sec, -xtime.tv_nsec);
223
224 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
225         local_timer_setup(smp_processor_id());
226 #endif
227
228         /*
229          * Find the timer to use as the system timer, it will be
230          * initialized for us.
231          */
232         sys_timer = get_sys_timer();
233         printk(KERN_INFO "Using %s for system timer\n", sys_timer->name);
234 }