blob: 9a5596bf85712adfb47ee8d1c1def51dc67c59ca [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 */
14#include <linux/types.h>
15#include <linux/kernel.h>
16#include <linux/init.h>
17#include <linux/sched.h>
18#include <linux/param.h>
Yoichi Yuasab1043cc2007-09-13 13:13:28 +090019#include <linux/profile.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/time.h>
21#include <linux/timex.h>
22#include <linux/smp.h>
23#include <linux/kernel_stat.h>
24#include <linux/spinlock.h>
25#include <linux/interrupt.h>
26#include <linux/module.h>
27
28#include <asm/bootinfo.h>
Ralf Baechleec74e362005-07-13 11:48:45 +000029#include <asm/cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/compiler.h>
31#include <asm/cpu.h>
32#include <asm/cpu-features.h>
33#include <asm/div64.h>
34#include <asm/sections.h>
35#include <asm/time.h>
36
37/*
38 * The integer part of the number of usecs per jiffy is taken from tick,
39 * but the fractional part is not recorded, so we calculate it using the
40 * initial value of HZ. This aids systems where tick isn't really an
41 * integer (e.g. for HZ = 128).
42 */
43#define USECS_PER_JIFFY TICK_SIZE
44#define USECS_PER_JIFFY_FRAC ((unsigned long)(u32)((1000000ULL << 32) / HZ))
45
46#define TICK_SIZE (tick_nsec / 1000)
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/*
49 * forward reference
50 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051DEFINE_SPINLOCK(rtc_lock);
52
53/*
54 * By default we provide the null RTC ops
55 */
56static unsigned long null_rtc_get_time(void)
57{
58 return mktime(2000, 1, 1, 0, 0, 0);
59}
60
61static int null_rtc_set_time(unsigned long sec)
62{
63 return 0;
64}
65
Yoichi Yuasad23ee8f2006-03-27 01:16:33 -080066unsigned long (*rtc_mips_get_time)(void) = null_rtc_get_time;
67int (*rtc_mips_set_time)(unsigned long) = null_rtc_set_time;
68int (*rtc_mips_set_mmss)(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/* how many counter cycles in a jiffy */
Ralf Baechleec74e362005-07-13 11:48:45 +000072static unsigned long cycles_per_jiffy __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/* expirelo is the count value for next CPU timer interrupt */
75static unsigned int expirelo;
76
77
78/*
79 * Null timer ack for systems not needing one (e.g. i8254).
80 */
81static void null_timer_ack(void) { /* nothing */ }
82
83/*
84 * Null high precision timer functions for systems lacking one.
85 */
Atsushi Nemoto00598562006-11-12 00:10:28 +090086static cycle_t null_hpt_read(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
88 return 0;
89}
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/*
92 * Timer ack for an R4k-compatible timer of a known frequency.
93 */
94static void c0_timer_ack(void)
95{
96 unsigned int count;
97
98 /* Ack this timer interrupt and set the next one. */
99 expirelo += cycles_per_jiffy;
100 write_c0_compare(expirelo);
101
102 /* Check to see if we have missed any timer interrupts. */
Ralf Baechle41c594a2006-04-05 09:45:45 +0100103 while (((count = read_c0_count()) - expirelo) < 0x7fffffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 /* missed_timer_count++; */
105 expirelo = count + cycles_per_jiffy;
106 write_c0_compare(expirelo);
107 }
108}
109
110/*
111 * High precision timer functions for a R4k-compatible timer.
112 */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900113static cycle_t c0_hpt_read(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 return read_c0_count();
116}
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118/* For use both as a high precision timer and an interrupt source. */
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900119static void __init c0_hpt_timer_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900121 expirelo = read_c0_count() + cycles_per_jiffy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 write_c0_compare(expirelo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
125int (*mips_timer_state)(void);
126void (*mips_timer_ack)(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128/* last time when xtime and rtc are sync'ed up */
129static long last_rtc_update;
130
131/*
132 * local_timer_interrupt() does profiling and process accounting
133 * on a per-CPU basis.
134 *
135 * In UP mode, it is invoked from the (global) timer_interrupt.
136 *
137 * In SMP mode, it might invoked by per-CPU timer interrupt, or
138 * a broadcasted inter-processor interrupt which itself is triggered
139 * by the global timer interrupt.
140 */
David Howells7d12e782006-10-05 14:55:46 +0100141void local_timer_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Ralf Baechle937a8012006-10-07 19:44:33 +0100143 profile_tick(CPU_PROFILING);
David Howells7d12e782006-10-05 14:55:46 +0100144 update_process_times(user_mode(get_irq_regs()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147/*
148 * High-level timer interrupt service routines. This function
149 * is set as irqaction->handler and is invoked through do_IRQ.
150 */
David Howells7d12e782006-10-05 14:55:46 +0100151irqreturn_t timer_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Ralf Baechled6bd0e62006-03-14 23:46:58 +0000153 write_seqlock(&xtime_lock);
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 mips_timer_ack();
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 /*
158 * call the generic timer interrupt handling
159 */
Atsushi Nemoto3171a032006-09-29 02:00:32 -0700160 do_timer(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 /*
163 * If we have an externally synchronized Linux clock, then update
Yoichi Yuasad23ee8f2006-03-27 01:16:33 -0800164 * CMOS clock accordingly every ~11 minutes. rtc_mips_set_time() has to be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 * called as close as possible to 500 ms before the new second starts.
166 */
john stultzb149ee22005-09-06 15:17:46 -0700167 if (ntp_synced() &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 xtime.tv_sec > last_rtc_update + 660 &&
169 (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
170 (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
Yoichi Yuasad23ee8f2006-03-27 01:16:33 -0800171 if (rtc_mips_set_mmss(xtime.tv_sec) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 last_rtc_update = xtime.tv_sec;
173 } else {
174 /* do it again in 60 s */
175 last_rtc_update = xtime.tv_sec - 600;
176 }
177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Ralf Baechled6bd0e62006-03-14 23:46:58 +0000179 write_sequnlock(&xtime_lock);
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 /*
182 * In UP mode, we call local_timer_interrupt() to do profiling
183 * and process accouting.
184 *
185 * In SMP mode, local_timer_interrupt() is invoked by appropriate
186 * low-level local timer interrupt handler.
187 */
David Howells7d12e782006-10-05 14:55:46 +0100188 local_timer_interrupt(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 return IRQ_HANDLED;
191}
192
David Howells7d12e782006-10-05 14:55:46 +0100193int null_perf_irq(void)
Ralf Baechleba339c02005-12-09 12:29:38 +0000194{
195 return 0;
196}
197
David Howells7d12e782006-10-05 14:55:46 +0100198int (*perf_irq)(void) = null_perf_irq;
Ralf Baechleba339c02005-12-09 12:29:38 +0000199
200EXPORT_SYMBOL(null_perf_irq);
201EXPORT_SYMBOL(perf_irq);
202
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100203/*
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +0100204 * Timer interrupt
205 */
206int cp0_compare_irq;
207
208/*
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100209 * Performance counter IRQ or -1 if shared with timer
210 */
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +0100211int cp0_perfcount_irq;
212EXPORT_SYMBOL_GPL(cp0_perfcount_irq);
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100213
214/*
215 * Possibly handle a performance counter interrupt.
216 * Return true if the timer interrupt should not be checked
217 */
218static inline int handle_perf_irq (int r2)
219{
220 /*
221 * The performance counter overflow interrupt may be shared with the
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +0100222 * timer interrupt (cp0_perfcount_irq < 0). If it is and a
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100223 * performance counter has overflowed (perf_irq() == IRQ_HANDLED)
224 * and we can't reliably determine if a counter interrupt has also
225 * happened (!r2) then don't check for a timer interrupt.
226 */
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +0100227 return (cp0_perfcount_irq < 0) &&
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100228 perf_irq() == IRQ_HANDLED &&
229 !r2;
230}
231
Ralf Baechle937a8012006-10-07 19:44:33 +0100232asmlinkage void ll_timer_interrupt(int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Ralf Baechleba339c02005-12-09 12:29:38 +0000234 int r2 = cpu_has_mips_r2;
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 irq_enter();
237 kstat_this_cpu.irqs[irq]++;
238
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100239 if (handle_perf_irq(r2))
240 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100242 if (r2 && ((read_c0_cause() & (1 << 30)) == 0))
243 goto out;
244
245 timer_interrupt(irq, NULL);
Ralf Baechleba339c02005-12-09 12:29:38 +0000246
247out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 irq_exit();
249}
250
Ralf Baechle937a8012006-10-07 19:44:33 +0100251asmlinkage void ll_local_timer_interrupt(int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 irq_enter();
254 if (smp_processor_id() != 0)
255 kstat_this_cpu.irqs[irq]++;
256
257 /* we keep interrupt disabled all the time */
David Howells7d12e782006-10-05 14:55:46 +0100258 local_timer_interrupt(irq, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 irq_exit();
261}
262
263/*
264 * time_init() - it does the following things.
265 *
266 * 1) board_time_init() -
267 * a) (optional) set up RTC routines,
268 * b) (optional) calibrate and set the mips_hpt_frequency
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900269 * (only needed if you intended to use cpu counter as timer interrupt
270 * source)
Yoichi Yuasad23ee8f2006-03-27 01:16:33 -0800271 * 2) setup xtime based on rtc_mips_get_time().
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900272 * 3) calculate a couple of cached variables for later usage
273 * 4) plat_timer_setup() -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 * a) (optional) over-write any choices made above by time_init().
275 * b) machine specific code should setup the timer irqaction.
276 * c) enable the timer interrupt
277 */
278
279void (*board_time_init)(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281unsigned int mips_hpt_frequency;
282
283static struct irqaction timer_irqaction = {
284 .handler = timer_interrupt,
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100285 .flags = IRQF_DISABLED | IRQF_PERCPU,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 .name = "timer",
287};
288
289static unsigned int __init calibrate_hpt(void)
290{
Atsushi Nemoto00598562006-11-12 00:10:28 +0900291 cycle_t frequency, hpt_start, hpt_end, hpt_count, hz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 const int loops = HZ / 10;
294 int log_2_loops = 0;
295 int i;
296
297 /*
298 * We want to calibrate for 0.1s, but to avoid a 64-bit
299 * division we round the number of loops up to the nearest
300 * power of 2.
301 */
302 while (loops > 1 << log_2_loops)
303 log_2_loops++;
304 i = 1 << log_2_loops;
305
306 /*
307 * Wait for a rising edge of the timer interrupt.
308 */
309 while (mips_timer_state());
310 while (!mips_timer_state());
311
312 /*
313 * Now see how many high precision timer ticks happen
314 * during the calculated number of periods between timer
315 * interrupts.
316 */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900317 hpt_start = clocksource_mips.read();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 do {
319 while (mips_timer_state());
320 while (!mips_timer_state());
321 } while (--i);
Atsushi Nemoto00598562006-11-12 00:10:28 +0900322 hpt_end = clocksource_mips.read();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Atsushi Nemoto00598562006-11-12 00:10:28 +0900324 hpt_count = (hpt_end - hpt_start) & clocksource_mips.mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 hz = HZ;
Atsushi Nemoto00598562006-11-12 00:10:28 +0900326 frequency = hpt_count * hz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 return frequency >> log_2_loops;
329}
330
Atsushi Nemoto00598562006-11-12 00:10:28 +0900331struct clocksource clocksource_mips = {
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900332 .name = "MIPS",
Franck Bui-Huu55d0b4e2007-05-04 17:36:44 +0200333 .mask = CLOCKSOURCE_MASK(32),
Thomas Gleixner877fe382007-02-16 01:27:40 -0800334 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900335};
336
337static void __init init_mips_clocksource(void)
338{
339 u64 temp;
340 u32 shift;
341
Atsushi Nemoto00598562006-11-12 00:10:28 +0900342 if (!mips_hpt_frequency || clocksource_mips.read == null_hpt_read)
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900343 return;
344
345 /* Calclate a somewhat reasonable rating value */
346 clocksource_mips.rating = 200 + mips_hpt_frequency / 10000000;
347 /* Find a shift value */
348 for (shift = 32; shift > 0; shift--) {
349 temp = (u64) NSEC_PER_SEC << shift;
350 do_div(temp, mips_hpt_frequency);
351 if ((temp >> 32) == 0)
352 break;
353 }
354 clocksource_mips.shift = shift;
355 clocksource_mips.mult = (u32)temp;
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900356
357 clocksource_register(&clocksource_mips);
358}
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360void __init time_init(void)
361{
362 if (board_time_init)
363 board_time_init();
364
Yoichi Yuasad23ee8f2006-03-27 01:16:33 -0800365 if (!rtc_mips_set_mmss)
366 rtc_mips_set_mmss = rtc_mips_set_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Yoichi Yuasad23ee8f2006-03-27 01:16:33 -0800368 xtime.tv_sec = rtc_mips_get_time();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 xtime.tv_nsec = 0;
370
371 set_normalized_timespec(&wall_to_monotonic,
372 -xtime.tv_sec, -xtime.tv_nsec);
373
374 /* Choose appropriate high precision timer routines. */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900375 if (!cpu_has_counter && !clocksource_mips.read)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 /* No high precision timer -- sorry. */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900377 clocksource_mips.read = null_hpt_read;
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900378 else if (!mips_hpt_frequency && !mips_timer_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /* A high precision timer of unknown frequency. */
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 } else {
384 /* We know counter frequency. Or we can get it. */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900385 if (!clocksource_mips.read) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /* No external high precision timer -- use R4k. */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900387 clocksource_mips.read = c0_hpt_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900389 if (!mips_timer_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /* No external timer interrupt -- use R4k. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 mips_timer_ack = c0_timer_ack;
Atsushi Nemotoc87b6eb2006-10-28 01:14:37 +0900392 /* Calculate cache parameters. */
393 cycles_per_jiffy =
394 (mips_hpt_frequency + HZ / 2) / HZ;
395 /*
396 * This sets up the high precision
397 * timer for the first interrupt.
398 */
399 c0_hpt_timer_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401 }
402 if (!mips_hpt_frequency)
403 mips_hpt_frequency = calibrate_hpt();
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 /* Report the high precision timer rate for a reference. */
406 printk("Using %u.%03u MHz high precision timer.\n",
407 ((mips_hpt_frequency + 500) / 1000) / 1000,
408 ((mips_hpt_frequency + 500) / 1000) % 1000);
409 }
410
411 if (!mips_timer_ack)
412 /* No timer interrupt ack (e.g. i8254). */
413 mips_timer_ack = null_timer_ack;
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /*
416 * Call board specific timer interrupt setup.
417 *
418 * this pointer must be setup in machine setup routine.
419 *
420 * Even if a machine chooses to use a low-level timer interrupt,
421 * it still needs to setup the timer_irqaction.
422 * In that case, it might be better to set timer_irqaction.handler
423 * to be NULL function so that we are sure the high-level code
424 * is not invoked accidentally.
425 */
Ralf Baechle54d0a212006-07-09 21:38:56 +0100426 plat_timer_setup(&timer_irqaction);
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900427
428 init_mips_clocksource();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
431#define FEBRUARY 2
432#define STARTOFTIME 1970
433#define SECDAY 86400L
434#define SECYR (SECDAY * 365)
435#define leapyear(y) ((!((y) % 4) && ((y) % 100)) || !((y) % 400))
436#define days_in_year(y) (leapyear(y) ? 366 : 365)
437#define days_in_month(m) (month_days[(m) - 1])
438
439static int month_days[12] = {
440 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
441};
442
443void to_tm(unsigned long tim, struct rtc_time *tm)
444{
445 long hms, day, gday;
446 int i;
447
448 gday = day = tim / SECDAY;
449 hms = tim % SECDAY;
450
451 /* Hours, minutes, seconds are easy */
452 tm->tm_hour = hms / 3600;
453 tm->tm_min = (hms % 3600) / 60;
454 tm->tm_sec = (hms % 3600) % 60;
455
456 /* Number of years in days */
457 for (i = STARTOFTIME; day >= days_in_year(i); i++)
458 day -= days_in_year(i);
459 tm->tm_year = i;
460
461 /* Number of months in days left */
462 if (leapyear(tm->tm_year))
463 days_in_month(FEBRUARY) = 29;
464 for (i = 1; day >= days_in_month(i); i++)
465 day -= days_in_month(i);
466 days_in_month(FEBRUARY) = 28;
467 tm->tm_mon = i - 1; /* tm_mon starts from 0 to 11 */
468
469 /* Days are what is left over (+1) from all that. */
470 tm->tm_mday = day + 1;
471
472 /*
473 * Determine the day of week
474 */
475 tm->tm_wday = (gday + 4) % 7; /* 1970/1/1 was Thursday */
476}
477
478EXPORT_SYMBOL(rtc_lock);
479EXPORT_SYMBOL(to_tm);
Yoichi Yuasad23ee8f2006-03-27 01:16:33 -0800480EXPORT_SYMBOL(rtc_mips_set_time);
481EXPORT_SYMBOL(rtc_mips_get_time);