blob: 35988847c98a36777e2ef5d7793a2bc37ba3d2a9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright 2001 MontaVista Software Inc.
3 * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
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 */
Ralf Baechle7bcf7712007-10-11 23:46:09 +010014#include <linux/clockchips.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/types.h>
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/sched.h>
19#include <linux/param.h>
Yoichi Yuasab1043cc2007-09-13 13:13:28 +090020#include <linux/profile.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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>
28
29#include <asm/bootinfo.h>
Ralf Baechleec74e362005-07-13 11:48:45 +000030#include <asm/cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/compiler.h>
32#include <asm/cpu.h>
33#include <asm/cpu-features.h>
34#include <asm/div64.h>
35#include <asm/sections.h>
36#include <asm/time.h>
37
Ralf Baechle7bcf7712007-10-11 23:46:09 +010038#include <irq.h>
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/*
41 * The integer part of the number of usecs per jiffy is taken from tick,
42 * but the fractional part is not recorded, so we calculate it using the
43 * initial value of HZ. This aids systems where tick isn't really an
44 * integer (e.g. for HZ = 128).
45 */
46#define USECS_PER_JIFFY TICK_SIZE
47#define USECS_PER_JIFFY_FRAC ((unsigned long)(u32)((1000000ULL << 32) / HZ))
48
49#define TICK_SIZE (tick_nsec / 1000)
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * forward reference
53 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054DEFINE_SPINLOCK(rtc_lock);
Ralf Baechle4b550482007-10-11 23:46:08 +010055EXPORT_SYMBOL(rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Ralf Baechle4b550482007-10-11 23:46:08 +010057int __weak rtc_mips_set_time(unsigned long sec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 return 0;
60}
Ralf Baechle4b550482007-10-11 23:46:08 +010061EXPORT_SYMBOL(rtc_mips_set_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Ralf Baechle4b550482007-10-11 23:46:08 +010063int __weak rtc_mips_set_mmss(unsigned long nowtime)
64{
65 return rtc_mips_set_time(nowtime);
66}
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Ralf Baechlef5ff0a22007-08-13 15:26:12 +010068int update_persistent_clock(struct timespec now)
69{
70 return rtc_mips_set_mmss(now.tv_sec);
71}
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* how many counter cycles in a jiffy */
Ralf Baechleec74e362005-07-13 11:48:45 +000074static unsigned long cycles_per_jiffy __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/*
77 * Null timer ack for systems not needing one (e.g. i8254).
78 */
79static void null_timer_ack(void) { /* nothing */ }
80
81/*
82 * Null high precision timer functions for systems lacking one.
83 */
Atsushi Nemoto00598562006-11-12 00:10:28 +090084static cycle_t null_hpt_read(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 return 0;
87}
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/*
90 * Timer ack for an R4k-compatible timer of a known frequency.
91 */
92static void c0_timer_ack(void)
93{
Ralf Baechle7bcf7712007-10-11 23:46:09 +010094 write_c0_compare(read_c0_compare());
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
97/*
98 * High precision timer functions for a R4k-compatible timer.
99 */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900100static cycle_t c0_hpt_read(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 return read_c0_count();
103}
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105int (*mips_timer_state)(void);
106void (*mips_timer_ack)(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/*
109 * local_timer_interrupt() does profiling and process accounting
110 * on a per-CPU basis.
111 *
112 * In UP mode, it is invoked from the (global) timer_interrupt.
113 *
114 * In SMP mode, it might invoked by per-CPU timer interrupt, or
115 * a broadcasted inter-processor interrupt which itself is triggered
116 * by the global timer interrupt.
117 */
David Howells7d12e782006-10-05 14:55:46 +0100118void local_timer_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Ralf Baechle937a8012006-10-07 19:44:33 +0100120 profile_tick(CPU_PROFILING);
David Howells7d12e782006-10-05 14:55:46 +0100121 update_process_times(user_mode(get_irq_regs()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
David Howells7d12e782006-10-05 14:55:46 +0100124int null_perf_irq(void)
Ralf Baechleba339c02005-12-09 12:29:38 +0000125{
126 return 0;
127}
128
Ralf Baechle91a2fcc2007-10-11 23:46:09 +0100129EXPORT_SYMBOL(null_perf_irq);
130
David Howells7d12e782006-10-05 14:55:46 +0100131int (*perf_irq)(void) = null_perf_irq;
Ralf Baechleba339c02005-12-09 12:29:38 +0000132
Ralf Baechleba339c02005-12-09 12:29:38 +0000133EXPORT_SYMBOL(perf_irq);
134
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100135/*
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +0100136 * Timer interrupt
137 */
138int cp0_compare_irq;
139
140/*
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100141 * Performance counter IRQ or -1 if shared with timer
142 */
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +0100143int cp0_perfcount_irq;
144EXPORT_SYMBOL_GPL(cp0_perfcount_irq);
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100145
146/*
147 * Possibly handle a performance counter interrupt.
148 * Return true if the timer interrupt should not be checked
149 */
150static inline int handle_perf_irq (int r2)
151{
152 /*
153 * The performance counter overflow interrupt may be shared with the
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +0100154 * timer interrupt (cp0_perfcount_irq < 0). If it is and a
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100155 * performance counter has overflowed (perf_irq() == IRQ_HANDLED)
156 * and we can't reliably determine if a counter interrupt has also
157 * happened (!r2) then don't check for a timer interrupt.
158 */
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +0100159 return (cp0_perfcount_irq < 0) &&
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100160 perf_irq() == IRQ_HANDLED &&
161 !r2;
162}
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/*
165 * time_init() - it does the following things.
166 *
Ralf Baechle4b550482007-10-11 23:46:08 +0100167 * 1) plat_time_init() -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * a) (optional) set up RTC routines,
169 * b) (optional) calibrate and set the mips_hpt_frequency
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900170 * (only needed if you intended to use cpu counter as timer interrupt
171 * source)
Ralf Baechle4b550482007-10-11 23:46:08 +0100172 * 2) calculate a couple of cached variables for later usage
173 * 3) plat_timer_setup() -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * a) (optional) over-write any choices made above by time_init().
175 * b) machine specific code should setup the timer irqaction.
176 * c) enable the timer interrupt
177 */
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179unsigned int mips_hpt_frequency;
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181static unsigned int __init calibrate_hpt(void)
182{
Atsushi Nemoto00598562006-11-12 00:10:28 +0900183 cycle_t frequency, hpt_start, hpt_end, hpt_count, hz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 const int loops = HZ / 10;
186 int log_2_loops = 0;
187 int i;
188
189 /*
190 * We want to calibrate for 0.1s, but to avoid a 64-bit
191 * division we round the number of loops up to the nearest
192 * power of 2.
193 */
194 while (loops > 1 << log_2_loops)
195 log_2_loops++;
196 i = 1 << log_2_loops;
197
198 /*
199 * Wait for a rising edge of the timer interrupt.
200 */
201 while (mips_timer_state());
202 while (!mips_timer_state());
203
204 /*
205 * Now see how many high precision timer ticks happen
206 * during the calculated number of periods between timer
207 * interrupts.
208 */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900209 hpt_start = clocksource_mips.read();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 do {
211 while (mips_timer_state());
212 while (!mips_timer_state());
213 } while (--i);
Atsushi Nemoto00598562006-11-12 00:10:28 +0900214 hpt_end = clocksource_mips.read();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Atsushi Nemoto00598562006-11-12 00:10:28 +0900216 hpt_count = (hpt_end - hpt_start) & clocksource_mips.mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 hz = HZ;
Atsushi Nemoto00598562006-11-12 00:10:28 +0900218 frequency = hpt_count * hz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 return frequency >> log_2_loops;
221}
222
Atsushi Nemoto00598562006-11-12 00:10:28 +0900223struct clocksource clocksource_mips = {
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900224 .name = "MIPS",
Franck Bui-Huu55d0b4e2007-05-04 17:36:44 +0200225 .mask = CLOCKSOURCE_MASK(32),
Thomas Gleixner877fe382007-02-16 01:27:40 -0800226 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900227};
228
Ralf Baechle7bcf7712007-10-11 23:46:09 +0100229static int mips_next_event(unsigned long delta,
230 struct clock_event_device *evt)
231{
232 unsigned int cnt;
233
234 cnt = read_c0_count();
235 cnt += delta;
236 write_c0_compare(cnt);
237
238 return ((long)(read_c0_count() - cnt ) > 0) ? -ETIME : 0;
239}
240
241static void mips_set_mode(enum clock_event_mode mode,
242 struct clock_event_device *evt)
243{
244 /* Nothing to do ... */
245}
246
247struct clock_event_device mips_clockevent;
248
249static struct clock_event_device *global_cd[NR_CPUS];
250static int cp0_timer_irq_installed;
251
252static irqreturn_t timer_interrupt(int irq, void *dev_id)
253{
254 const int r2 = cpu_has_mips_r2;
255 struct clock_event_device *cd;
256 int cpu = smp_processor_id();
257
258 /*
259 * Suckage alert:
260 * Before R2 of the architecture there was no way to see if a
261 * performance counter interrupt was pending, so we have to run
262 * the performance counter interrupt handler anyway.
263 */
264 if (handle_perf_irq(r2))
265 goto out;
266
267 /*
268 * The same applies to performance counter interrupts. But with the
269 * above we now know that the reason we got here must be a timer
270 * interrupt. Being the paranoiacs we are we check anyway.
271 */
272 if (!r2 || (read_c0_cause() & (1 << 30))) {
273 c0_timer_ack();
274 cd = global_cd[cpu];
275 cd->event_handler(cd);
276 }
277
278out:
279 return IRQ_HANDLED;
280}
281
282static struct irqaction timer_irqaction = {
283 .handler = timer_interrupt,
284 .flags = IRQF_DISABLED | IRQF_PERCPU,
285 .name = "timer",
286};
287
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900288static void __init init_mips_clocksource(void)
289{
290 u64 temp;
291 u32 shift;
292
Atsushi Nemoto00598562006-11-12 00:10:28 +0900293 if (!mips_hpt_frequency || clocksource_mips.read == null_hpt_read)
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900294 return;
295
296 /* Calclate a somewhat reasonable rating value */
297 clocksource_mips.rating = 200 + mips_hpt_frequency / 10000000;
298 /* Find a shift value */
299 for (shift = 32; shift > 0; shift--) {
300 temp = (u64) NSEC_PER_SEC << shift;
301 do_div(temp, mips_hpt_frequency);
302 if ((temp >> 32) == 0)
303 break;
304 }
305 clocksource_mips.shift = shift;
306 clocksource_mips.mult = (u32)temp;
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900307
308 clocksource_register(&clocksource_mips);
309}
310
Ralf Baechle4b550482007-10-11 23:46:08 +0100311void __init __weak plat_time_init(void)
312{
313}
314
Ralf Baechle7bcf7712007-10-11 23:46:09 +0100315void __init __weak plat_timer_setup(struct irqaction *irq)
316{
317}
318
319void __cpuinit mips_clockevent_init(void)
320{
321 uint64_t mips_freq = mips_hpt_frequency;
322 unsigned int cpu = smp_processor_id();
323 struct clock_event_device *cd;
324 unsigned int irq = MIPS_CPU_IRQ_BASE + 7;
325
326 if (!cpu_has_counter)
327 return;
328
329 if (cpu == 0)
330 cd = &mips_clockevent;
331 else
332 cd = kzalloc(sizeof(*cd), GFP_ATOMIC);
333 if (!cd)
334 return; /* We're probably roadkill ... */
335
336 cd->name = "MIPS";
337 cd->features = CLOCK_EVT_FEAT_ONESHOT;
338
339 /* Calculate the min / max delta */
340 cd->mult = div_sc((unsigned long) mips_freq, NSEC_PER_SEC, 32);
341 cd->shift = 32;
342 cd->max_delta_ns = clockevent_delta2ns(0x7fffffff, cd);
343 cd->min_delta_ns = clockevent_delta2ns(0x30, cd);
344
345 cd->rating = 300;
346 cd->irq = irq;
347 cd->cpumask = cpumask_of_cpu(cpu);
348 cd->set_next_event = mips_next_event;
349 cd->set_mode = mips_set_mode;
350
351 global_cd[cpu] = cd;
352 clockevents_register_device(cd);
353
354 if (!cp0_timer_irq_installed) {
355#ifdef CONFIG_MIPS_MT_SMTC
356#define CPUCTR_IMASKBIT (0x100 << cp0_compare_irq)
357 setup_irq_smtc(irq, &timer_irqaction, CPUCTR_IMASKBIT);
358#else
359 setup_irq(irq, &timer_irqaction);
360#endif /* CONFIG_MIPS_MT_SMTC */
361 cp0_timer_irq_installed = 1;
362 }
363}
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365void __init time_init(void)
366{
Ralf Baechle4b550482007-10-11 23:46:08 +0100367 plat_time_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 /* Choose appropriate high precision timer routines. */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900370 if (!cpu_has_counter && !clocksource_mips.read)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 /* No high precision timer -- sorry. */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900372 clocksource_mips.read = null_hpt_read;
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900373 else if (!mips_hpt_frequency && !mips_timer_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 /* A high precision timer of unknown frequency. */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900375 if (!clocksource_mips.read)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 /* No external high precision timer -- use R4k. */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900377 clocksource_mips.read = c0_hpt_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 } else {
379 /* We know counter frequency. Or we can get it. */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900380 if (!clocksource_mips.read) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 /* No external high precision timer -- use R4k. */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900382 clocksource_mips.read = c0_hpt_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900384 if (!mips_timer_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 /* No external timer interrupt -- use R4k. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 mips_timer_ack = c0_timer_ack;
Atsushi Nemotoc87b6eb2006-10-28 01:14:37 +0900387 /* Calculate cache parameters. */
388 cycles_per_jiffy =
389 (mips_hpt_frequency + HZ / 2) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391 }
392 if (!mips_hpt_frequency)
393 mips_hpt_frequency = calibrate_hpt();
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /* Report the high precision timer rate for a reference. */
396 printk("Using %u.%03u MHz high precision timer.\n",
397 ((mips_hpt_frequency + 500) / 1000) / 1000,
398 ((mips_hpt_frequency + 500) / 1000) % 1000);
Ralf Baechle7bcf7712007-10-11 23:46:09 +0100399
400#ifdef CONFIG_IRQ_CPU
401 setup_irq(MIPS_CPU_IRQ_BASE + 7, &timer_irqaction);
402#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404
405 if (!mips_timer_ack)
406 /* No timer interrupt ack (e.g. i8254). */
407 mips_timer_ack = null_timer_ack;
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /*
410 * Call board specific timer interrupt setup.
411 *
412 * this pointer must be setup in machine setup routine.
413 *
414 * Even if a machine chooses to use a low-level timer interrupt,
415 * it still needs to setup the timer_irqaction.
416 * In that case, it might be better to set timer_irqaction.handler
417 * to be NULL function so that we are sure the high-level code
418 * is not invoked accidentally.
419 */
Ralf Baechle54d0a212006-07-09 21:38:56 +0100420 plat_timer_setup(&timer_irqaction);
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900421
422 init_mips_clocksource();
Ralf Baechle7bcf7712007-10-11 23:46:09 +0100423 mips_clockevent_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}