Commit | Line | Data |
---|---|---|
5a0015d6 CZ |
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 | ||
5a0015d6 CZ |
15 | #include <linux/errno.h> |
16 | #include <linux/time.h> | |
5a0015d6 CZ |
17 | #include <linux/interrupt.h> |
18 | #include <linux/module.h> | |
19 | #include <linux/init.h> | |
20 | #include <linux/irq.h> | |
21 | #include <linux/profile.h> | |
22 | #include <linux/delay.h> | |
23 | ||
24 | #include <asm/timex.h> | |
25 | #include <asm/platform.h> | |
26 | ||
5a0015d6 CZ |
27 | #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT |
28 | unsigned long ccount_per_jiffy; /* per 1/HZ */ | |
2b8aea74 | 29 | unsigned long nsec_per_ccount; /* nsec per ccount increment */ |
5a0015d6 CZ |
30 | #endif |
31 | ||
fd43fe19 | 32 | static irqreturn_t timer_interrupt(int irq, void *dev_id); |
5a0015d6 CZ |
33 | static struct irqaction timer_irqaction = { |
34 | .handler = timer_interrupt, | |
85ac3ab2 | 35 | .flags = IRQF_DISABLED, |
5a0015d6 CZ |
36 | .name = "timer", |
37 | }; | |
38 | ||
39 | void __init time_init(void) | |
40 | { | |
5a0015d6 CZ |
41 | /* The platform must provide a function to calibrate the processor |
42 | * speed for the CALIBRATE. | |
43 | */ | |
44 | ||
288a60cf | 45 | #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT |
5a0015d6 CZ |
46 | printk("Calibrating CPU frequency "); |
47 | platform_calibrate_ccount(); | |
48 | printk("%d.%02d MHz\n", (int)ccount_per_jiffy/(1000000/HZ), | |
49 | (int)(ccount_per_jiffy/(10000/HZ))%100); | |
50 | #endif | |
51 | ||
5a0015d6 | 52 | xtime.tv_nsec = 0; |
4476c967 | 53 | xtime.tv_sec = read_persistent_clock(); |
5a0015d6 CZ |
54 | |
55 | set_normalized_timespec(&wall_to_monotonic, | |
56 | -xtime.tv_sec, -xtime.tv_nsec); | |
57 | ||
58 | /* Initialize the linux timer interrupt. */ | |
59 | ||
60 | setup_irq(LINUX_TIMER_INT, &timer_irqaction); | |
61 | set_linux_timer(get_ccount() + CCOUNT_PER_JIFFY); | |
62 | } | |
63 | ||
64 | ||
65 | int do_settimeofday(struct timespec *tv) | |
66 | { | |
67 | time_t wtm_sec, sec = tv->tv_sec; | |
68 | long wtm_nsec, nsec = tv->tv_nsec; | |
2b8aea74 | 69 | unsigned long delta; |
5a0015d6 CZ |
70 | |
71 | if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC) | |
72 | return -EINVAL; | |
73 | ||
74 | write_seqlock_irq(&xtime_lock); | |
75 | ||
76 | /* This is revolting. We need to set "xtime" correctly. However, the | |
77 | * value in this location is the value at the most recent update of | |
78 | * wall time. Discover what correction gettimeofday() would have | |
79 | * made, and then undo it! | |
80 | */ | |
2b8aea74 CZ |
81 | |
82 | delta = CCOUNT_PER_JIFFY; | |
83 | delta += get_ccount() - get_linux_timer(); | |
84 | nsec -= delta * NSEC_PER_CCOUNT; | |
5a0015d6 CZ |
85 | |
86 | wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec); | |
87 | wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec); | |
88 | ||
89 | set_normalized_timespec(&xtime, sec, nsec); | |
90 | set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec); | |
91 | ||
b149ee22 | 92 | ntp_clear(); |
5a0015d6 CZ |
93 | write_sequnlock_irq(&xtime_lock); |
94 | return 0; | |
95 | } | |
96 | ||
97 | EXPORT_SYMBOL(do_settimeofday); | |
98 | ||
99 | ||
100 | void do_gettimeofday(struct timeval *tv) | |
101 | { | |
102 | unsigned long flags; | |
2b8aea74 | 103 | unsigned long volatile sec, usec, delta, seq; |
5a0015d6 CZ |
104 | |
105 | do { | |
106 | seq = read_seqbegin_irqsave(&xtime_lock, flags); | |
107 | ||
5a0015d6 CZ |
108 | sec = xtime.tv_sec; |
109 | usec = (xtime.tv_nsec / NSEC_PER_USEC); | |
2b8aea74 CZ |
110 | |
111 | delta = get_linux_timer() - get_ccount(); | |
112 | ||
5a0015d6 CZ |
113 | } while (read_seqretry_irqrestore(&xtime_lock, seq, flags)); |
114 | ||
2b8aea74 CZ |
115 | usec += (((unsigned long) CCOUNT_PER_JIFFY - delta) |
116 | * (unsigned long) NSEC_PER_CCOUNT) / NSEC_PER_USEC; | |
117 | ||
5a0015d6 CZ |
118 | for (; usec >= 1000000; sec++, usec -= 1000000) |
119 | ; | |
120 | ||
121 | tv->tv_sec = sec; | |
122 | tv->tv_usec = usec; | |
123 | } | |
124 | ||
125 | EXPORT_SYMBOL(do_gettimeofday); | |
126 | ||
127 | /* | |
128 | * The timer interrupt is called HZ times per second. | |
129 | */ | |
130 | ||
fd43fe19 | 131 | irqreturn_t timer_interrupt (int irq, void *dev_id) |
5a0015d6 CZ |
132 | { |
133 | ||
134 | unsigned long next; | |
135 | ||
136 | next = get_linux_timer(); | |
137 | ||
138 | again: | |
139 | while ((signed long)(get_ccount() - next) > 0) { | |
140 | ||
fd43fe19 | 141 | profile_tick(CPU_PROFILING); |
5a0015d6 | 142 | #ifndef CONFIG_SMP |
fd43fe19 | 143 | update_process_times(user_mode(get_irq_regs())); |
5a0015d6 CZ |
144 | #endif |
145 | ||
146 | write_seqlock(&xtime_lock); | |
147 | ||
2b8aea74 CZ |
148 | do_timer(1); /* Linux handler in kernel/timer.c */ |
149 | ||
150 | /* Note that writing CCOMPARE clears the interrupt. */ | |
151 | ||
5a0015d6 | 152 | next += CCOUNT_PER_JIFFY; |
2b8aea74 | 153 | set_linux_timer(next); |
5a0015d6 | 154 | |
5a0015d6 CZ |
155 | write_sequnlock(&xtime_lock); |
156 | } | |
157 | ||
2b8aea74 | 158 | /* Allow platform to do something useful (Wdog). */ |
5a0015d6 | 159 | |
2b8aea74 | 160 | platform_heartbeat(); |
5a0015d6 CZ |
161 | |
162 | /* Make sure we didn't miss any tick... */ | |
163 | ||
164 | if ((signed long)(get_ccount() - next) > 0) | |
165 | goto again; | |
166 | ||
5a0015d6 CZ |
167 | return IRQ_HANDLED; |
168 | } | |
169 | ||
170 | #ifndef CONFIG_GENERIC_CALIBRATE_DELAY | |
6c81c32f | 171 | void __cpuinit calibrate_delay(void) |
5a0015d6 CZ |
172 | { |
173 | loops_per_jiffy = CCOUNT_PER_JIFFY; | |
174 | printk("Calibrating delay loop (skipped)... " | |
175 | "%lu.%02lu BogoMIPS preset\n", | |
176 | loops_per_jiffy/(1000000/HZ), | |
177 | (loops_per_jiffy/(10000/HZ)) % 100); | |
178 | } | |
179 | #endif | |
180 |