Merge branch 'devel' of master.kernel.org:/home/rmk/linux-2.6-mmc
[sfrench/cifs-2.6.git] / arch / xtensa / kernel / time.c
1 /*
2  * arch/xtensa/kernel/time.c
3  *
4  * Timer and clock support.
5  *
6  * This file is subject to the terms and conditions of the GNU General Public
7  * License.  See the file "COPYING" in the main directory of this archive
8  * for more details.
9  *
10  * Copyright (C) 2005 Tensilica Inc.
11  *
12  * Chris Zankel <chris@zankel.net>
13  */
14
15 #include <linux/errno.h>
16 #include <linux/time.h>
17 #include <linux/timex.h>
18 #include <linux/interrupt.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/irq.h>
22 #include <linux/profile.h>
23 #include <linux/delay.h>
24
25 #include <asm/timex.h>
26 #include <asm/platform.h>
27
28
29 extern volatile unsigned long wall_jiffies;
30
31 DEFINE_SPINLOCK(rtc_lock);
32 EXPORT_SYMBOL(rtc_lock);
33
34
35 #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
36 unsigned long ccount_per_jiffy;         /* per 1/HZ */
37 unsigned long ccount_nsec;              /* nsec per ccount increment */
38 #endif
39
40 unsigned int last_ccount_stamp;
41 static long last_rtc_update = 0;
42
43 /*
44  * Scheduler clock - returns current tim in nanosec units.
45  */
46
47 unsigned long long sched_clock(void)
48 {
49         return (unsigned long long)jiffies * (1000000000 / HZ);
50 }
51
52 static irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs);
53 static struct irqaction timer_irqaction = {
54         .handler =      timer_interrupt,
55         .flags =        IRQF_DISABLED,
56         .name =         "timer",
57 };
58
59 void __init time_init(void)
60 {
61         time_t sec_o, sec_n = 0;
62
63         /* The platform must provide a function to calibrate the processor
64          * speed for the CALIBRATE.
65          */
66
67 #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
68         printk("Calibrating CPU frequency ");
69         platform_calibrate_ccount();
70         printk("%d.%02d MHz\n", (int)ccount_per_jiffy/(1000000/HZ),
71                         (int)(ccount_per_jiffy/(10000/HZ))%100);
72 #endif
73
74         /* Set time from RTC (if provided) */
75
76         if (platform_get_rtc_time(&sec_o) == 0)
77                 while (platform_get_rtc_time(&sec_n))
78                         if (sec_o != sec_n)
79                                 break;
80
81         xtime.tv_nsec = 0;
82         last_rtc_update = xtime.tv_sec = sec_n;
83         last_ccount_stamp = get_ccount();
84
85         set_normalized_timespec(&wall_to_monotonic,
86                 -xtime.tv_sec, -xtime.tv_nsec);
87
88         /* Initialize the linux timer interrupt. */
89
90         setup_irq(LINUX_TIMER_INT, &timer_irqaction);
91         set_linux_timer(get_ccount() + CCOUNT_PER_JIFFY);
92 }
93
94
95 int do_settimeofday(struct timespec *tv)
96 {
97         time_t wtm_sec, sec = tv->tv_sec;
98         long wtm_nsec, nsec = tv->tv_nsec;
99         unsigned long ccount;
100
101         if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
102                 return -EINVAL;
103
104         write_seqlock_irq(&xtime_lock);
105
106         /* This is revolting. We need to set "xtime" correctly. However, the
107          * value in this location is the value at the most recent update of
108          * wall time.  Discover what correction gettimeofday() would have
109          * made, and then undo it!
110          */
111         ccount = get_ccount();
112         nsec -= (ccount - last_ccount_stamp) * CCOUNT_NSEC;
113         nsec -= (jiffies - wall_jiffies) * CCOUNT_PER_JIFFY * CCOUNT_NSEC;
114
115         wtm_sec  = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
116         wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
117
118         set_normalized_timespec(&xtime, sec, nsec);
119         set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
120
121         ntp_clear();
122         write_sequnlock_irq(&xtime_lock);
123         return 0;
124 }
125
126 EXPORT_SYMBOL(do_settimeofday);
127
128
129 void do_gettimeofday(struct timeval *tv)
130 {
131         unsigned long flags;
132         unsigned long sec, usec, delta, lost, seq;
133
134         do {
135                 seq = read_seqbegin_irqsave(&xtime_lock, flags);
136
137                 delta = get_ccount() - last_ccount_stamp;
138                 sec = xtime.tv_sec;
139                 usec = (xtime.tv_nsec / NSEC_PER_USEC);
140
141                 lost = jiffies - wall_jiffies;
142
143         } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
144
145         usec += lost * (1000000UL/HZ) + (delta * CCOUNT_NSEC) / NSEC_PER_USEC;
146         for (; usec >= 1000000; sec++, usec -= 1000000)
147                 ;
148
149         tv->tv_sec = sec;
150         tv->tv_usec = usec;
151 }
152
153 EXPORT_SYMBOL(do_gettimeofday);
154
155 /*
156  * The timer interrupt is called HZ times per second.
157  */
158
159 irqreturn_t timer_interrupt (int irq, void *dev_id, struct pt_regs *regs)
160 {
161
162         unsigned long next;
163
164         next = get_linux_timer();
165
166 again:
167         while ((signed long)(get_ccount() - next) > 0) {
168
169                 profile_tick(CPU_PROFILING, regs);
170 #ifndef CONFIG_SMP
171                 update_process_times(user_mode(regs));
172 #endif
173
174                 write_seqlock(&xtime_lock);
175
176                 last_ccount_stamp = next;
177                 next += CCOUNT_PER_JIFFY;
178                 do_timer (regs); /* Linux handler in kernel/timer.c */
179
180                 if (ntp_synced() &&
181                     xtime.tv_sec - last_rtc_update >= 659 &&
182                     abs((xtime.tv_nsec/1000)-(1000000-1000000/HZ))<5000000/HZ &&
183                     jiffies - wall_jiffies == 1) {
184
185                         if (platform_set_rtc_time(xtime.tv_sec+1) == 0)
186                                 last_rtc_update = xtime.tv_sec+1;
187                         else
188                                 /* Do it again in 60 s */
189                                 last_rtc_update += 60;
190                 }
191                 write_sequnlock(&xtime_lock);
192         }
193
194         /* NOTE: writing CCOMPAREn clears the interrupt.  */
195
196         set_linux_timer (next);
197
198         /* Make sure we didn't miss any tick... */
199
200         if ((signed long)(get_ccount() - next) > 0)
201                 goto again;
202
203         /* Allow platform to do something useful (Wdog). */
204
205         platform_heartbeat();
206
207         return IRQ_HANDLED;
208 }
209
210 #ifndef CONFIG_GENERIC_CALIBRATE_DELAY
211 void __devinit calibrate_delay(void)
212 {
213         loops_per_jiffy = CCOUNT_PER_JIFFY;
214         printk("Calibrating delay loop (skipped)... "
215                "%lu.%02lu BogoMIPS preset\n",
216                loops_per_jiffy/(1000000/HZ),
217                (loops_per_jiffy/(10000/HZ)) % 100);
218 }
219 #endif
220