blob: 628a8ba6da57dd7203b26069cf7c9738e1f0b528 [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
Ralf Baechlef5ff0a22007-08-13 15:26:12 +010070int update_persistent_clock(struct timespec now)
71{
72 return rtc_mips_set_mmss(now.tv_sec);
73}
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/* how many counter cycles in a jiffy */
Ralf Baechleec74e362005-07-13 11:48:45 +000076static unsigned long cycles_per_jiffy __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/* expirelo is the count value for next CPU timer interrupt */
79static unsigned int expirelo;
80
81
82/*
83 * Null timer ack for systems not needing one (e.g. i8254).
84 */
85static void null_timer_ack(void) { /* nothing */ }
86
87/*
88 * Null high precision timer functions for systems lacking one.
89 */
Atsushi Nemoto00598562006-11-12 00:10:28 +090090static cycle_t null_hpt_read(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 return 0;
93}
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/*
96 * Timer ack for an R4k-compatible timer of a known frequency.
97 */
98static void c0_timer_ack(void)
99{
100 unsigned int count;
101
102 /* Ack this timer interrupt and set the next one. */
103 expirelo += cycles_per_jiffy;
104 write_c0_compare(expirelo);
105
106 /* Check to see if we have missed any timer interrupts. */
Ralf Baechle41c594a2006-04-05 09:45:45 +0100107 while (((count = read_c0_count()) - expirelo) < 0x7fffffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 /* missed_timer_count++; */
109 expirelo = count + cycles_per_jiffy;
110 write_c0_compare(expirelo);
111 }
112}
113
114/*
115 * High precision timer functions for a R4k-compatible timer.
116 */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900117static cycle_t c0_hpt_read(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 return read_c0_count();
120}
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/* For use both as a high precision timer and an interrupt source. */
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900123static void __init c0_hpt_timer_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900125 expirelo = read_c0_count() + cycles_per_jiffy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 write_c0_compare(expirelo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129int (*mips_timer_state)(void);
130void (*mips_timer_ack)(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/*
133 * local_timer_interrupt() does profiling and process accounting
134 * on a per-CPU basis.
135 *
136 * In UP mode, it is invoked from the (global) timer_interrupt.
137 *
138 * In SMP mode, it might invoked by per-CPU timer interrupt, or
139 * a broadcasted inter-processor interrupt which itself is triggered
140 * by the global timer interrupt.
141 */
David Howells7d12e782006-10-05 14:55:46 +0100142void local_timer_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Ralf Baechle937a8012006-10-07 19:44:33 +0100144 profile_tick(CPU_PROFILING);
David Howells7d12e782006-10-05 14:55:46 +0100145 update_process_times(user_mode(get_irq_regs()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
148/*
149 * High-level timer interrupt service routines. This function
150 * is set as irqaction->handler and is invoked through do_IRQ.
151 */
David Howells7d12e782006-10-05 14:55:46 +0100152irqreturn_t timer_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Ralf Baechled6bd0e62006-03-14 23:46:58 +0000154 write_seqlock(&xtime_lock);
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 mips_timer_ack();
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 /*
159 * call the generic timer interrupt handling
160 */
Atsushi Nemoto3171a032006-09-29 02:00:32 -0700161 do_timer(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Ralf Baechled6bd0e62006-03-14 23:46:58 +0000163 write_sequnlock(&xtime_lock);
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 /*
166 * In UP mode, we call local_timer_interrupt() to do profiling
167 * and process accouting.
168 *
169 * In SMP mode, local_timer_interrupt() is invoked by appropriate
170 * low-level local timer interrupt handler.
171 */
David Howells7d12e782006-10-05 14:55:46 +0100172 local_timer_interrupt(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 return IRQ_HANDLED;
175}
176
David Howells7d12e782006-10-05 14:55:46 +0100177int null_perf_irq(void)
Ralf Baechleba339c02005-12-09 12:29:38 +0000178{
179 return 0;
180}
181
David Howells7d12e782006-10-05 14:55:46 +0100182int (*perf_irq)(void) = null_perf_irq;
Ralf Baechleba339c02005-12-09 12:29:38 +0000183
184EXPORT_SYMBOL(null_perf_irq);
185EXPORT_SYMBOL(perf_irq);
186
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100187/*
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +0100188 * Timer interrupt
189 */
190int cp0_compare_irq;
191
192/*
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100193 * Performance counter IRQ or -1 if shared with timer
194 */
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +0100195int cp0_perfcount_irq;
196EXPORT_SYMBOL_GPL(cp0_perfcount_irq);
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100197
198/*
199 * Possibly handle a performance counter interrupt.
200 * Return true if the timer interrupt should not be checked
201 */
202static inline int handle_perf_irq (int r2)
203{
204 /*
205 * The performance counter overflow interrupt may be shared with the
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +0100206 * timer interrupt (cp0_perfcount_irq < 0). If it is and a
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100207 * performance counter has overflowed (perf_irq() == IRQ_HANDLED)
208 * and we can't reliably determine if a counter interrupt has also
209 * happened (!r2) then don't check for a timer interrupt.
210 */
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +0100211 return (cp0_perfcount_irq < 0) &&
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100212 perf_irq() == IRQ_HANDLED &&
213 !r2;
214}
215
Ralf Baechle937a8012006-10-07 19:44:33 +0100216asmlinkage void ll_timer_interrupt(int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Ralf Baechleba339c02005-12-09 12:29:38 +0000218 int r2 = cpu_has_mips_r2;
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 irq_enter();
221 kstat_this_cpu.irqs[irq]++;
222
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100223 if (handle_perf_irq(r2))
224 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100226 if (r2 && ((read_c0_cause() & (1 << 30)) == 0))
227 goto out;
228
229 timer_interrupt(irq, NULL);
Ralf Baechleba339c02005-12-09 12:29:38 +0000230
231out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 irq_exit();
233}
234
Ralf Baechle937a8012006-10-07 19:44:33 +0100235asmlinkage void ll_local_timer_interrupt(int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 irq_enter();
238 if (smp_processor_id() != 0)
239 kstat_this_cpu.irqs[irq]++;
240
241 /* we keep interrupt disabled all the time */
David Howells7d12e782006-10-05 14:55:46 +0100242 local_timer_interrupt(irq, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 irq_exit();
245}
246
247/*
248 * time_init() - it does the following things.
249 *
250 * 1) board_time_init() -
251 * a) (optional) set up RTC routines,
252 * b) (optional) calibrate and set the mips_hpt_frequency
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900253 * (only needed if you intended to use cpu counter as timer interrupt
254 * source)
Yoichi Yuasad23ee8f2006-03-27 01:16:33 -0800255 * 2) setup xtime based on rtc_mips_get_time().
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900256 * 3) calculate a couple of cached variables for later usage
257 * 4) plat_timer_setup() -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 * a) (optional) over-write any choices made above by time_init().
259 * b) machine specific code should setup the timer irqaction.
260 * c) enable the timer interrupt
261 */
262
263void (*board_time_init)(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265unsigned int mips_hpt_frequency;
266
267static struct irqaction timer_irqaction = {
268 .handler = timer_interrupt,
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100269 .flags = IRQF_DISABLED | IRQF_PERCPU,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 .name = "timer",
271};
272
273static unsigned int __init calibrate_hpt(void)
274{
Atsushi Nemoto00598562006-11-12 00:10:28 +0900275 cycle_t frequency, hpt_start, hpt_end, hpt_count, hz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 const int loops = HZ / 10;
278 int log_2_loops = 0;
279 int i;
280
281 /*
282 * We want to calibrate for 0.1s, but to avoid a 64-bit
283 * division we round the number of loops up to the nearest
284 * power of 2.
285 */
286 while (loops > 1 << log_2_loops)
287 log_2_loops++;
288 i = 1 << log_2_loops;
289
290 /*
291 * Wait for a rising edge of the timer interrupt.
292 */
293 while (mips_timer_state());
294 while (!mips_timer_state());
295
296 /*
297 * Now see how many high precision timer ticks happen
298 * during the calculated number of periods between timer
299 * interrupts.
300 */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900301 hpt_start = clocksource_mips.read();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 do {
303 while (mips_timer_state());
304 while (!mips_timer_state());
305 } while (--i);
Atsushi Nemoto00598562006-11-12 00:10:28 +0900306 hpt_end = clocksource_mips.read();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Atsushi Nemoto00598562006-11-12 00:10:28 +0900308 hpt_count = (hpt_end - hpt_start) & clocksource_mips.mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 hz = HZ;
Atsushi Nemoto00598562006-11-12 00:10:28 +0900310 frequency = hpt_count * hz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 return frequency >> log_2_loops;
313}
314
Atsushi Nemoto00598562006-11-12 00:10:28 +0900315struct clocksource clocksource_mips = {
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900316 .name = "MIPS",
Franck Bui-Huu55d0b4e2007-05-04 17:36:44 +0200317 .mask = CLOCKSOURCE_MASK(32),
Thomas Gleixner877fe382007-02-16 01:27:40 -0800318 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900319};
320
321static void __init init_mips_clocksource(void)
322{
323 u64 temp;
324 u32 shift;
325
Atsushi Nemoto00598562006-11-12 00:10:28 +0900326 if (!mips_hpt_frequency || clocksource_mips.read == null_hpt_read)
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900327 return;
328
329 /* Calclate a somewhat reasonable rating value */
330 clocksource_mips.rating = 200 + mips_hpt_frequency / 10000000;
331 /* Find a shift value */
332 for (shift = 32; shift > 0; shift--) {
333 temp = (u64) NSEC_PER_SEC << shift;
334 do_div(temp, mips_hpt_frequency);
335 if ((temp >> 32) == 0)
336 break;
337 }
338 clocksource_mips.shift = shift;
339 clocksource_mips.mult = (u32)temp;
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900340
341 clocksource_register(&clocksource_mips);
342}
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344void __init time_init(void)
345{
346 if (board_time_init)
347 board_time_init();
348
Yoichi Yuasad23ee8f2006-03-27 01:16:33 -0800349 if (!rtc_mips_set_mmss)
350 rtc_mips_set_mmss = rtc_mips_set_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Yoichi Yuasad23ee8f2006-03-27 01:16:33 -0800352 xtime.tv_sec = rtc_mips_get_time();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 xtime.tv_nsec = 0;
354
355 set_normalized_timespec(&wall_to_monotonic,
356 -xtime.tv_sec, -xtime.tv_nsec);
357
358 /* Choose appropriate high precision timer routines. */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900359 if (!cpu_has_counter && !clocksource_mips.read)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 /* No high precision timer -- sorry. */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900361 clocksource_mips.read = null_hpt_read;
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900362 else if (!mips_hpt_frequency && !mips_timer_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 /* A high precision timer of unknown frequency. */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900364 if (!clocksource_mips.read)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 /* No external high precision timer -- use R4k. */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900366 clocksource_mips.read = c0_hpt_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 } else {
368 /* We know counter frequency. Or we can get it. */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900369 if (!clocksource_mips.read) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /* No external high precision timer -- use R4k. */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900371 clocksource_mips.read = c0_hpt_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900373 if (!mips_timer_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 /* No external timer interrupt -- use R4k. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 mips_timer_ack = c0_timer_ack;
Atsushi Nemotoc87b6eb2006-10-28 01:14:37 +0900376 /* Calculate cache parameters. */
377 cycles_per_jiffy =
378 (mips_hpt_frequency + HZ / 2) / HZ;
379 /*
380 * This sets up the high precision
381 * timer for the first interrupt.
382 */
383 c0_hpt_timer_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
385 }
386 if (!mips_hpt_frequency)
387 mips_hpt_frequency = calibrate_hpt();
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 /* Report the high precision timer rate for a reference. */
390 printk("Using %u.%03u MHz high precision timer.\n",
391 ((mips_hpt_frequency + 500) / 1000) / 1000,
392 ((mips_hpt_frequency + 500) / 1000) % 1000);
393 }
394
395 if (!mips_timer_ack)
396 /* No timer interrupt ack (e.g. i8254). */
397 mips_timer_ack = null_timer_ack;
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 /*
400 * Call board specific timer interrupt setup.
401 *
402 * this pointer must be setup in machine setup routine.
403 *
404 * Even if a machine chooses to use a low-level timer interrupt,
405 * it still needs to setup the timer_irqaction.
406 * In that case, it might be better to set timer_irqaction.handler
407 * to be NULL function so that we are sure the high-level code
408 * is not invoked accidentally.
409 */
Ralf Baechle54d0a212006-07-09 21:38:56 +0100410 plat_timer_setup(&timer_irqaction);
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900411
412 init_mips_clocksource();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
415#define FEBRUARY 2
416#define STARTOFTIME 1970
417#define SECDAY 86400L
418#define SECYR (SECDAY * 365)
419#define leapyear(y) ((!((y) % 4) && ((y) % 100)) || !((y) % 400))
420#define days_in_year(y) (leapyear(y) ? 366 : 365)
421#define days_in_month(m) (month_days[(m) - 1])
422
423static int month_days[12] = {
424 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
425};
426
427void to_tm(unsigned long tim, struct rtc_time *tm)
428{
429 long hms, day, gday;
430 int i;
431
432 gday = day = tim / SECDAY;
433 hms = tim % SECDAY;
434
435 /* Hours, minutes, seconds are easy */
436 tm->tm_hour = hms / 3600;
437 tm->tm_min = (hms % 3600) / 60;
438 tm->tm_sec = (hms % 3600) % 60;
439
440 /* Number of years in days */
441 for (i = STARTOFTIME; day >= days_in_year(i); i++)
442 day -= days_in_year(i);
443 tm->tm_year = i;
444
445 /* Number of months in days left */
446 if (leapyear(tm->tm_year))
447 days_in_month(FEBRUARY) = 29;
448 for (i = 1; day >= days_in_month(i); i++)
449 day -= days_in_month(i);
450 days_in_month(FEBRUARY) = 28;
451 tm->tm_mon = i - 1; /* tm_mon starts from 0 to 11 */
452
453 /* Days are what is left over (+1) from all that. */
454 tm->tm_mday = day + 1;
455
456 /*
457 * Determine the day of week
458 */
459 tm->tm_wday = (gday + 4) % 7; /* 1970/1/1 was Thursday */
460}
461
462EXPORT_SYMBOL(rtc_lock);
463EXPORT_SYMBOL(to_tm);
Yoichi Yuasad23ee8f2006-03-27 01:16:33 -0800464EXPORT_SYMBOL(rtc_mips_set_time);
465EXPORT_SYMBOL(rtc_mips_get_time);