xtensa: use generic sched_clock()
[platform/adaptation/renesas_rcar/renesas_kernel.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 DEFINE_SPINLOCK(rtc_lock);
30 EXPORT_SYMBOL(rtc_lock);
31
32
33 #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
34 unsigned long ccount_per_jiffy;         /* per 1/HZ */
35 unsigned long nsec_per_ccount;          /* nsec per ccount increment */
36 #endif
37
38 static long last_rtc_update = 0;
39
40 static irqreturn_t timer_interrupt(int irq, void *dev_id);
41 static struct irqaction timer_irqaction = {
42         .handler =      timer_interrupt,
43         .flags =        IRQF_DISABLED,
44         .name =         "timer",
45 };
46
47 void __init time_init(void)
48 {
49         time_t sec_o, sec_n = 0;
50
51         /* The platform must provide a function to calibrate the processor
52          * speed for the CALIBRATE.
53          */
54
55 #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
56         printk("Calibrating CPU frequency ");
57         platform_calibrate_ccount();
58         printk("%d.%02d MHz\n", (int)ccount_per_jiffy/(1000000/HZ),
59                         (int)(ccount_per_jiffy/(10000/HZ))%100);
60 #endif
61
62         /* Set time from RTC (if provided) */
63
64         if (platform_get_rtc_time(&sec_o) == 0)
65                 while (platform_get_rtc_time(&sec_n))
66                         if (sec_o != sec_n)
67                                 break;
68
69         xtime.tv_nsec = 0;
70         last_rtc_update = xtime.tv_sec = sec_n;
71
72         set_normalized_timespec(&wall_to_monotonic,
73                 -xtime.tv_sec, -xtime.tv_nsec);
74
75         /* Initialize the linux timer interrupt. */
76
77         setup_irq(LINUX_TIMER_INT, &timer_irqaction);
78         set_linux_timer(get_ccount() + CCOUNT_PER_JIFFY);
79 }
80
81
82 int do_settimeofday(struct timespec *tv)
83 {
84         time_t wtm_sec, sec = tv->tv_sec;
85         long wtm_nsec, nsec = tv->tv_nsec;
86         unsigned long delta;
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
99         delta = CCOUNT_PER_JIFFY;
100         delta += get_ccount() - get_linux_timer();
101         nsec -= delta * NSEC_PER_CCOUNT;
102
103         wtm_sec  = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
104         wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
105
106         set_normalized_timespec(&xtime, sec, nsec);
107         set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
108
109         ntp_clear();
110         write_sequnlock_irq(&xtime_lock);
111         return 0;
112 }
113
114 EXPORT_SYMBOL(do_settimeofday);
115
116
117 void do_gettimeofday(struct timeval *tv)
118 {
119         unsigned long flags;
120         unsigned long volatile sec, usec, delta, seq;
121
122         do {
123                 seq = read_seqbegin_irqsave(&xtime_lock, flags);
124
125                 sec = xtime.tv_sec;
126                 usec = (xtime.tv_nsec / NSEC_PER_USEC);
127
128                 delta = get_linux_timer() - get_ccount();
129
130         } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
131
132         usec += (((unsigned long) CCOUNT_PER_JIFFY - delta)
133                  * (unsigned long) NSEC_PER_CCOUNT) / NSEC_PER_USEC;
134
135         for (; usec >= 1000000; sec++, usec -= 1000000)
136                 ;
137
138         tv->tv_sec = sec;
139         tv->tv_usec = usec;
140 }
141
142 EXPORT_SYMBOL(do_gettimeofday);
143
144 /*
145  * The timer interrupt is called HZ times per second.
146  */
147
148 irqreturn_t timer_interrupt (int irq, void *dev_id)
149 {
150
151         unsigned long next;
152
153         next = get_linux_timer();
154
155 again:
156         while ((signed long)(get_ccount() - next) > 0) {
157
158                 profile_tick(CPU_PROFILING);
159 #ifndef CONFIG_SMP
160                 update_process_times(user_mode(get_irq_regs()));
161 #endif
162
163                 write_seqlock(&xtime_lock);
164
165                 do_timer(1); /* Linux handler in kernel/timer.c */
166
167                 /* Note that writing CCOMPARE clears the interrupt. */
168
169                 next += CCOUNT_PER_JIFFY;
170                 set_linux_timer(next);
171
172                 if (ntp_synced() &&
173                     xtime.tv_sec - last_rtc_update >= 659 &&
174                     abs((xtime.tv_nsec/1000)-(1000000-1000000/HZ))<5000000/HZ) {
175
176                         if (platform_set_rtc_time(xtime.tv_sec+1) == 0)
177                                 last_rtc_update = xtime.tv_sec+1;
178                         else
179                                 /* Do it again in 60 s */
180                                 last_rtc_update += 60;
181                 }
182                 write_sequnlock(&xtime_lock);
183         }
184
185         /* Allow platform to do something useful (Wdog). */
186
187         platform_heartbeat();
188
189         /* Make sure we didn't miss any tick... */
190
191         if ((signed long)(get_ccount() - next) > 0)
192                 goto again;
193
194         return IRQ_HANDLED;
195 }
196
197 #ifndef CONFIG_GENERIC_CALIBRATE_DELAY
198 void __cpuinit calibrate_delay(void)
199 {
200         loops_per_jiffy = CCOUNT_PER_JIFFY;
201         printk("Calibrating delay loop (skipped)... "
202                "%lu.%02lu BogoMIPS preset\n",
203                loops_per_jiffy/(1000000/HZ),
204                (loops_per_jiffy/(10000/HZ)) % 100);
205 }
206 #endif
207