]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Copyright 2001 MontaVista Software Inc. | |
3 | * Author: Jun Sun, [email protected] or [email protected] | |
4 | * Copyright (c) 2003, 2004 Maciej W. Rozycki | |
5 | * | |
6 | * Common time service routines for MIPS machines. See | |
7 | * Documentation/mips/time.README. | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify it | |
10 | * under the terms of the GNU General Public License as published by the | |
11 | * Free Software Foundation; either version 2 of the License, or (at your | |
12 | * option) any later version. | |
13 | */ | |
7bcf7717 | 14 | #include <linux/clockchips.h> |
1da177e4 LT |
15 | #include <linux/types.h> |
16 | #include <linux/kernel.h> | |
17 | #include <linux/init.h> | |
18 | #include <linux/sched.h> | |
19 | #include <linux/param.h> | |
b1043cc7 | 20 | #include <linux/profile.h> |
1da177e4 LT |
21 | #include <linux/time.h> |
22 | #include <linux/timex.h> | |
23 | #include <linux/smp.h> | |
24 | #include <linux/kernel_stat.h> | |
25 | #include <linux/spinlock.h> | |
26 | #include <linux/interrupt.h> | |
27 | #include <linux/module.h> | |
ea580401 | 28 | #include <linux/kallsyms.h> |
1da177e4 LT |
29 | |
30 | #include <asm/bootinfo.h> | |
ec74e361 | 31 | #include <asm/cache.h> |
1da177e4 LT |
32 | #include <asm/compiler.h> |
33 | #include <asm/cpu.h> | |
34 | #include <asm/cpu-features.h> | |
35 | #include <asm/div64.h> | |
36 | #include <asm/sections.h> | |
ea580401 | 37 | #include <asm/smtc_ipi.h> |
1da177e4 LT |
38 | #include <asm/time.h> |
39 | ||
7bcf7717 RB |
40 | #include <irq.h> |
41 | ||
1da177e4 LT |
42 | /* |
43 | * forward reference | |
44 | */ | |
1da177e4 | 45 | DEFINE_SPINLOCK(rtc_lock); |
4b550488 | 46 | EXPORT_SYMBOL(rtc_lock); |
1da177e4 | 47 | |
4b550488 | 48 | int __weak rtc_mips_set_time(unsigned long sec) |
1da177e4 | 49 | { |
4b550488 | 50 | return 0; |
1da177e4 | 51 | } |
4b550488 | 52 | EXPORT_SYMBOL(rtc_mips_set_time); |
1da177e4 | 53 | |
4b550488 | 54 | int __weak rtc_mips_set_mmss(unsigned long nowtime) |
1da177e4 | 55 | { |
4b550488 | 56 | return rtc_mips_set_time(nowtime); |
1da177e4 LT |
57 | } |
58 | ||
f5ff0a28 RB |
59 | int update_persistent_clock(struct timespec now) |
60 | { | |
61 | return rtc_mips_set_mmss(now.tv_sec); | |
62 | } | |
1da177e4 | 63 | |
1da177e4 LT |
64 | /* |
65 | * Null high precision timer functions for systems lacking one. | |
66 | */ | |
00598560 | 67 | static cycle_t null_hpt_read(void) |
1da177e4 LT |
68 | { |
69 | return 0; | |
70 | } | |
71 | ||
1da177e4 LT |
72 | /* |
73 | * High precision timer functions for a R4k-compatible timer. | |
74 | */ | |
00598560 | 75 | static cycle_t c0_hpt_read(void) |
1da177e4 LT |
76 | { |
77 | return read_c0_count(); | |
78 | } | |
79 | ||
1da177e4 | 80 | int (*mips_timer_state)(void); |
1da177e4 | 81 | |
1da177e4 LT |
82 | /* |
83 | * local_timer_interrupt() does profiling and process accounting | |
84 | * on a per-CPU basis. | |
85 | * | |
86 | * In UP mode, it is invoked from the (global) timer_interrupt. | |
87 | * | |
88 | * In SMP mode, it might invoked by per-CPU timer interrupt, or | |
89 | * a broadcasted inter-processor interrupt which itself is triggered | |
90 | * by the global timer interrupt. | |
91 | */ | |
7d12e780 | 92 | void local_timer_interrupt(int irq, void *dev_id) |
1da177e4 | 93 | { |
937a8015 | 94 | profile_tick(CPU_PROFILING); |
7d12e780 | 95 | update_process_times(user_mode(get_irq_regs())); |
1da177e4 LT |
96 | } |
97 | ||
7d12e780 | 98 | int null_perf_irq(void) |
ba339c03 RB |
99 | { |
100 | return 0; | |
101 | } | |
102 | ||
91a2fcc8 RB |
103 | EXPORT_SYMBOL(null_perf_irq); |
104 | ||
7d12e780 | 105 | int (*perf_irq)(void) = null_perf_irq; |
ba339c03 | 106 | |
ba339c03 RB |
107 | EXPORT_SYMBOL(perf_irq); |
108 | ||
1da177e4 LT |
109 | /* |
110 | * time_init() - it does the following things. | |
111 | * | |
4b550488 | 112 | * 1) plat_time_init() - |
1da177e4 LT |
113 | * a) (optional) set up RTC routines, |
114 | * b) (optional) calibrate and set the mips_hpt_frequency | |
16b7b2ac AN |
115 | * (only needed if you intended to use cpu counter as timer interrupt |
116 | * source) | |
4b550488 RB |
117 | * 2) calculate a couple of cached variables for later usage |
118 | * 3) plat_timer_setup() - | |
1da177e4 LT |
119 | * a) (optional) over-write any choices made above by time_init(). |
120 | * b) machine specific code should setup the timer irqaction. | |
121 | * c) enable the timer interrupt | |
122 | */ | |
123 | ||
1da177e4 LT |
124 | unsigned int mips_hpt_frequency; |
125 | ||
1da177e4 LT |
126 | static unsigned int __init calibrate_hpt(void) |
127 | { | |
00598560 | 128 | cycle_t frequency, hpt_start, hpt_end, hpt_count, hz; |
1da177e4 LT |
129 | |
130 | const int loops = HZ / 10; | |
131 | int log_2_loops = 0; | |
132 | int i; | |
133 | ||
134 | /* | |
135 | * We want to calibrate for 0.1s, but to avoid a 64-bit | |
136 | * division we round the number of loops up to the nearest | |
137 | * power of 2. | |
138 | */ | |
139 | while (loops > 1 << log_2_loops) | |
140 | log_2_loops++; | |
141 | i = 1 << log_2_loops; | |
142 | ||
143 | /* | |
144 | * Wait for a rising edge of the timer interrupt. | |
145 | */ | |
146 | while (mips_timer_state()); | |
147 | while (!mips_timer_state()); | |
148 | ||
149 | /* | |
150 | * Now see how many high precision timer ticks happen | |
151 | * during the calculated number of periods between timer | |
152 | * interrupts. | |
153 | */ | |
00598560 | 154 | hpt_start = clocksource_mips.read(); |
1da177e4 LT |
155 | do { |
156 | while (mips_timer_state()); | |
157 | while (!mips_timer_state()); | |
158 | } while (--i); | |
00598560 | 159 | hpt_end = clocksource_mips.read(); |
1da177e4 | 160 | |
00598560 | 161 | hpt_count = (hpt_end - hpt_start) & clocksource_mips.mask; |
1da177e4 | 162 | hz = HZ; |
00598560 | 163 | frequency = hpt_count * hz; |
1da177e4 LT |
164 | |
165 | return frequency >> log_2_loops; | |
166 | } | |
167 | ||
00598560 | 168 | struct clocksource clocksource_mips = { |
16b7b2ac | 169 | .name = "MIPS", |
55d0b4e3 | 170 | .mask = CLOCKSOURCE_MASK(32), |
877fe380 | 171 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
16b7b2ac AN |
172 | }; |
173 | ||
93c846f9 | 174 | void __init clocksource_set_clock(struct clocksource *cs, unsigned int clock) |
16b7b2ac AN |
175 | { |
176 | u64 temp; | |
177 | u32 shift; | |
178 | ||
93c846f9 RB |
179 | /* Find a shift value */ |
180 | for (shift = 32; shift > 0; shift--) { | |
181 | temp = (u64) NSEC_PER_SEC << shift; | |
182 | do_div(temp, clock); | |
183 | if ((temp >> 32) == 0) | |
184 | break; | |
185 | } | |
186 | cs->shift = shift; | |
187 | cs->mult = (u32) temp; | |
188 | } | |
189 | ||
190 | void __cpuinit clockevent_set_clock(struct clock_event_device *cd, | |
191 | unsigned int clock) | |
192 | { | |
193 | u64 temp; | |
194 | u32 shift; | |
16b7b2ac | 195 | |
16b7b2ac AN |
196 | /* Find a shift value */ |
197 | for (shift = 32; shift > 0; shift--) { | |
508a775a AN |
198 | temp = (u64) clock << shift; |
199 | do_div(temp, NSEC_PER_SEC); | |
16b7b2ac AN |
200 | if ((temp >> 32) == 0) |
201 | break; | |
202 | } | |
93c846f9 RB |
203 | cd->shift = shift; |
204 | cd->mult = (u32) temp; | |
205 | } | |
206 | ||
207 | static void __init init_mips_clocksource(void) | |
208 | { | |
209 | if (!mips_hpt_frequency || clocksource_mips.read == null_hpt_read) | |
210 | return; | |
211 | ||
212 | /* Calclate a somewhat reasonable rating value */ | |
213 | clocksource_mips.rating = 200 + mips_hpt_frequency / 10000000; | |
214 | ||
215 | clocksource_set_clock(&clocksource_mips, mips_hpt_frequency); | |
16b7b2ac AN |
216 | |
217 | clocksource_register(&clocksource_mips); | |
218 | } | |
219 | ||
4b550488 | 220 | void __init __weak plat_time_init(void) |
1da177e4 | 221 | { |
4b550488 | 222 | } |
1da177e4 | 223 | |
7bcf7717 RB |
224 | void __init __weak plat_timer_setup(struct irqaction *irq) |
225 | { | |
226 | } | |
227 | ||
4b550488 RB |
228 | void __init time_init(void) |
229 | { | |
230 | plat_time_init(); | |
1da177e4 LT |
231 | |
232 | /* Choose appropriate high precision timer routines. */ | |
00598560 | 233 | if (!cpu_has_counter && !clocksource_mips.read) |
1da177e4 | 234 | /* No high precision timer -- sorry. */ |
00598560 | 235 | clocksource_mips.read = null_hpt_read; |
16b7b2ac | 236 | else if (!mips_hpt_frequency && !mips_timer_state) { |
1da177e4 | 237 | /* A high precision timer of unknown frequency. */ |
00598560 | 238 | if (!clocksource_mips.read) |
1da177e4 | 239 | /* No external high precision timer -- use R4k. */ |
00598560 | 240 | clocksource_mips.read = c0_hpt_read; |
1da177e4 LT |
241 | } else { |
242 | /* We know counter frequency. Or we can get it. */ | |
00598560 | 243 | if (!clocksource_mips.read) { |
1da177e4 | 244 | /* No external high precision timer -- use R4k. */ |
00598560 | 245 | clocksource_mips.read = c0_hpt_read; |
1da177e4 LT |
246 | } |
247 | if (!mips_hpt_frequency) | |
248 | mips_hpt_frequency = calibrate_hpt(); | |
249 | ||
1da177e4 LT |
250 | /* Report the high precision timer rate for a reference. */ |
251 | printk("Using %u.%03u MHz high precision timer.\n", | |
252 | ((mips_hpt_frequency + 500) / 1000) / 1000, | |
253 | ((mips_hpt_frequency + 500) / 1000) % 1000); | |
254 | } | |
255 | ||
16b7b2ac | 256 | init_mips_clocksource(); |
7bcf7717 | 257 | mips_clockevent_init(); |
1da177e4 | 258 | } |