blob: 22949be4a5d8cc3c5d62f93b8779a67c3ecd451f [file] [log] [blame]
Chris Zankel5a0015d2005-06-23 22:01:16 -07001/*
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
Chris Zankel5a0015d2005-06-23 22:01:16 -070015#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
Ingo Molnar34af9462006-06-27 02:53:55 -070029DEFINE_SPINLOCK(rtc_lock);
Chris Zankel5a0015d2005-06-23 22:01:16 -070030EXPORT_SYMBOL(rtc_lock);
31
32
33#ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
34unsigned long ccount_per_jiffy; /* per 1/HZ */
35unsigned long ccount_nsec; /* nsec per ccount increment */
36#endif
37
38unsigned int last_ccount_stamp;
39static long last_rtc_update = 0;
40
Chris Zankelfd43fe12006-12-10 02:18:47 -080041static irqreturn_t timer_interrupt(int irq, void *dev_id);
Chris Zankel5a0015d2005-06-23 22:01:16 -070042static struct irqaction timer_irqaction = {
43 .handler = timer_interrupt,
Thomas Gleixner85ac3ab2006-07-01 19:29:31 -070044 .flags = IRQF_DISABLED,
Chris Zankel5a0015d2005-06-23 22:01:16 -070045 .name = "timer",
46};
47
48void __init time_init(void)
49{
50 time_t sec_o, sec_n = 0;
51
52 /* The platform must provide a function to calibrate the processor
53 * speed for the CALIBRATE.
54 */
55
Chris Zankel288a60c2005-09-22 21:44:23 -070056#ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
Chris Zankel5a0015d2005-06-23 22:01:16 -070057 printk("Calibrating CPU frequency ");
58 platform_calibrate_ccount();
59 printk("%d.%02d MHz\n", (int)ccount_per_jiffy/(1000000/HZ),
60 (int)(ccount_per_jiffy/(10000/HZ))%100);
61#endif
62
63 /* Set time from RTC (if provided) */
64
65 if (platform_get_rtc_time(&sec_o) == 0)
66 while (platform_get_rtc_time(&sec_n))
67 if (sec_o != sec_n)
68 break;
69
70 xtime.tv_nsec = 0;
71 last_rtc_update = xtime.tv_sec = sec_n;
72 last_ccount_stamp = get_ccount();
73
74 set_normalized_timespec(&wall_to_monotonic,
75 -xtime.tv_sec, -xtime.tv_nsec);
76
77 /* Initialize the linux timer interrupt. */
78
79 setup_irq(LINUX_TIMER_INT, &timer_irqaction);
80 set_linux_timer(get_ccount() + CCOUNT_PER_JIFFY);
81}
82
83
84int do_settimeofday(struct timespec *tv)
85{
86 time_t wtm_sec, sec = tv->tv_sec;
87 long wtm_nsec, nsec = tv->tv_nsec;
88 unsigned long ccount;
89
90 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
91 return -EINVAL;
92
93 write_seqlock_irq(&xtime_lock);
94
95 /* This is revolting. We need to set "xtime" correctly. However, the
96 * value in this location is the value at the most recent update of
97 * wall time. Discover what correction gettimeofday() would have
98 * made, and then undo it!
99 */
100 ccount = get_ccount();
101 nsec -= (ccount - last_ccount_stamp) * CCOUNT_NSEC;
Chris Zankel5a0015d2005-06-23 22:01:16 -0700102
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
john stultzb149ee22005-09-06 15:17:46 -0700109 ntp_clear();
Chris Zankel5a0015d2005-06-23 22:01:16 -0700110 write_sequnlock_irq(&xtime_lock);
111 return 0;
112}
113
114EXPORT_SYMBOL(do_settimeofday);
115
116
117void do_gettimeofday(struct timeval *tv)
118{
119 unsigned long flags;
Atsushi Nemoto8ef38602006-09-30 23:28:31 -0700120 unsigned long sec, usec, delta, seq;
Chris Zankel5a0015d2005-06-23 22:01:16 -0700121
122 do {
123 seq = read_seqbegin_irqsave(&xtime_lock, flags);
124
125 delta = get_ccount() - last_ccount_stamp;
126 sec = xtime.tv_sec;
127 usec = (xtime.tv_nsec / NSEC_PER_USEC);
Chris Zankel5a0015d2005-06-23 22:01:16 -0700128 } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
129
Atsushi Nemoto8ef38602006-09-30 23:28:31 -0700130 usec += (delta * CCOUNT_NSEC) / NSEC_PER_USEC;
Chris Zankel5a0015d2005-06-23 22:01:16 -0700131 for (; usec >= 1000000; sec++, usec -= 1000000)
132 ;
133
134 tv->tv_sec = sec;
135 tv->tv_usec = usec;
136}
137
138EXPORT_SYMBOL(do_gettimeofday);
139
140/*
141 * The timer interrupt is called HZ times per second.
142 */
143
Chris Zankelfd43fe12006-12-10 02:18:47 -0800144irqreturn_t timer_interrupt (int irq, void *dev_id)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700145{
146
147 unsigned long next;
148
149 next = get_linux_timer();
150
151again:
152 while ((signed long)(get_ccount() - next) > 0) {
153
Chris Zankelfd43fe12006-12-10 02:18:47 -0800154 profile_tick(CPU_PROFILING);
Chris Zankel5a0015d2005-06-23 22:01:16 -0700155#ifndef CONFIG_SMP
Chris Zankelfd43fe12006-12-10 02:18:47 -0800156 update_process_times(user_mode(get_irq_regs()));
Chris Zankel5a0015d2005-06-23 22:01:16 -0700157#endif
158
159 write_seqlock(&xtime_lock);
160
161 last_ccount_stamp = next;
162 next += CCOUNT_PER_JIFFY;
Atsushi Nemoto3171a032006-09-29 02:00:32 -0700163 do_timer (1); /* Linux handler in kernel/timer.c */
Chris Zankel5a0015d2005-06-23 22:01:16 -0700164
john stultzb149ee22005-09-06 15:17:46 -0700165 if (ntp_synced() &&
Chris Zankel5a0015d2005-06-23 22:01:16 -0700166 xtime.tv_sec - last_rtc_update >= 659 &&
Atsushi Nemoto8ef38602006-09-30 23:28:31 -0700167 abs((xtime.tv_nsec/1000)-(1000000-1000000/HZ))<5000000/HZ) {
Chris Zankel5a0015d2005-06-23 22:01:16 -0700168
169 if (platform_set_rtc_time(xtime.tv_sec+1) == 0)
170 last_rtc_update = xtime.tv_sec+1;
171 else
172 /* Do it again in 60 s */
173 last_rtc_update += 60;
174 }
175 write_sequnlock(&xtime_lock);
176 }
177
178 /* NOTE: writing CCOMPAREn clears the interrupt. */
179
180 set_linux_timer (next);
181
182 /* Make sure we didn't miss any tick... */
183
184 if ((signed long)(get_ccount() - next) > 0)
185 goto again;
186
Adrian Bunka58a4142006-01-10 00:08:17 +0100187 /* Allow platform to do something useful (Wdog). */
Chris Zankel5a0015d2005-06-23 22:01:16 -0700188
189 platform_heartbeat();
190
191 return IRQ_HANDLED;
192}
193
194#ifndef CONFIG_GENERIC_CALIBRATE_DELAY
195void __devinit calibrate_delay(void)
196{
197 loops_per_jiffy = CCOUNT_PER_JIFFY;
198 printk("Calibrating delay loop (skipped)... "
199 "%lu.%02lu BogoMIPS preset\n",
200 loops_per_jiffy/(1000000/HZ),
201 (loops_per_jiffy/(10000/HZ)) % 100);
202}
203#endif
204