blob: e535f86efa2f3f4fc79b7784cdceae9bebea8d7c [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 */
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +090014#include <linux/clocksource.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>
20#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 */
86static unsigned int null_hpt_read(void)
87{
88 return 0;
89}
90
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +090091static void __init null_hpt_init(void)
Ralf Baechleec74e362005-07-13 11:48:45 +000092{
93 /* nothing */
94}
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96
97/*
98 * Timer ack for an R4k-compatible timer of a known frequency.
99 */
100static void c0_timer_ack(void)
101{
102 unsigned int count;
103
Pete Popovbdf21b12005-07-14 17:47:57 +0000104#ifndef CONFIG_SOC_PNX8550 /* pnx8550 resets to zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 /* Ack this timer interrupt and set the next one. */
106 expirelo += cycles_per_jiffy;
Pete Popovbdf21b12005-07-14 17:47:57 +0000107#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 write_c0_compare(expirelo);
109
110 /* Check to see if we have missed any timer interrupts. */
Ralf Baechle41c594a2006-04-05 09:45:45 +0100111 while (((count = read_c0_count()) - expirelo) < 0x7fffffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 /* missed_timer_count++; */
113 expirelo = count + cycles_per_jiffy;
114 write_c0_compare(expirelo);
115 }
116}
117
118/*
119 * High precision timer functions for a R4k-compatible timer.
120 */
121static unsigned int c0_hpt_read(void)
122{
123 return read_c0_count();
124}
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126/* For use both as a high precision timer and an interrupt source. */
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900127static void __init c0_hpt_timer_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900129 expirelo = read_c0_count() + cycles_per_jiffy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 write_c0_compare(expirelo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
133int (*mips_timer_state)(void);
134void (*mips_timer_ack)(void);
135unsigned int (*mips_hpt_read)(void);
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900136void (*mips_hpt_init)(void) __initdata = null_hpt_init;
137unsigned int mips_hpt_mask = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139/* last time when xtime and rtc are sync'ed up */
140static long last_rtc_update;
141
142/*
143 * local_timer_interrupt() does profiling and process accounting
144 * on a per-CPU basis.
145 *
146 * In UP mode, it is invoked from the (global) timer_interrupt.
147 *
148 * In SMP mode, it might invoked by per-CPU timer interrupt, or
149 * a broadcasted inter-processor interrupt which itself is triggered
150 * by the global timer interrupt.
151 */
David Howells7d12e782006-10-05 14:55:46 +0100152void local_timer_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Ralf Baechle937a8012006-10-07 19:44:33 +0100154 profile_tick(CPU_PROFILING);
David Howells7d12e782006-10-05 14:55:46 +0100155 update_process_times(user_mode(get_irq_regs()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158/*
159 * High-level timer interrupt service routines. This function
160 * is set as irqaction->handler and is invoked through do_IRQ.
161 */
David Howells7d12e782006-10-05 14:55:46 +0100162irqreturn_t timer_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Ralf Baechled6bd0e62006-03-14 23:46:58 +0000164 write_seqlock(&xtime_lock);
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 mips_timer_ack();
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 /*
169 * call the generic timer interrupt handling
170 */
Atsushi Nemoto3171a032006-09-29 02:00:32 -0700171 do_timer(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 /*
174 * If we have an externally synchronized Linux clock, then update
Yoichi Yuasad23ee8f2006-03-27 01:16:33 -0800175 * CMOS clock accordingly every ~11 minutes. rtc_mips_set_time() has to be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 * called as close as possible to 500 ms before the new second starts.
177 */
john stultzb149ee22005-09-06 15:17:46 -0700178 if (ntp_synced() &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 xtime.tv_sec > last_rtc_update + 660 &&
180 (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
181 (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
Yoichi Yuasad23ee8f2006-03-27 01:16:33 -0800182 if (rtc_mips_set_mmss(xtime.tv_sec) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 last_rtc_update = xtime.tv_sec;
184 } else {
185 /* do it again in 60 s */
186 last_rtc_update = xtime.tv_sec - 600;
187 }
188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Ralf Baechled6bd0e62006-03-14 23:46:58 +0000190 write_sequnlock(&xtime_lock);
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 /*
193 * In UP mode, we call local_timer_interrupt() to do profiling
194 * and process accouting.
195 *
196 * In SMP mode, local_timer_interrupt() is invoked by appropriate
197 * low-level local timer interrupt handler.
198 */
David Howells7d12e782006-10-05 14:55:46 +0100199 local_timer_interrupt(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 return IRQ_HANDLED;
202}
203
David Howells7d12e782006-10-05 14:55:46 +0100204int null_perf_irq(void)
Ralf Baechleba339c02005-12-09 12:29:38 +0000205{
206 return 0;
207}
208
David Howells7d12e782006-10-05 14:55:46 +0100209int (*perf_irq)(void) = null_perf_irq;
Ralf Baechleba339c02005-12-09 12:29:38 +0000210
211EXPORT_SYMBOL(null_perf_irq);
212EXPORT_SYMBOL(perf_irq);
213
Ralf Baechle937a8012006-10-07 19:44:33 +0100214asmlinkage void ll_timer_interrupt(int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Ralf Baechleba339c02005-12-09 12:29:38 +0000216 int r2 = cpu_has_mips_r2;
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 irq_enter();
219 kstat_this_cpu.irqs[irq]++;
220
Ralf Baechleba339c02005-12-09 12:29:38 +0000221 /*
222 * Suckage alert:
223 * Before R2 of the architecture there was no way to see if a
224 * performance counter interrupt was pending, so we have to run the
225 * performance counter interrupt handler anyway.
226 */
227 if (!r2 || (read_c0_cause() & (1 << 26)))
David Howells7d12e782006-10-05 14:55:46 +0100228 if (perf_irq())
Ralf Baechleba339c02005-12-09 12:29:38 +0000229 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Ralf Baechleba339c02005-12-09 12:29:38 +0000231 /* we keep interrupt disabled all the time */
232 if (!r2 || (read_c0_cause() & (1 << 30)))
David Howells7d12e782006-10-05 14:55:46 +0100233 timer_interrupt(irq, NULL);
Ralf Baechleba339c02005-12-09 12:29:38 +0000234
235out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 irq_exit();
237}
238
Ralf Baechle937a8012006-10-07 19:44:33 +0100239asmlinkage void ll_local_timer_interrupt(int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 irq_enter();
242 if (smp_processor_id() != 0)
243 kstat_this_cpu.irqs[irq]++;
244
245 /* we keep interrupt disabled all the time */
David Howells7d12e782006-10-05 14:55:46 +0100246 local_timer_interrupt(irq, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 irq_exit();
249}
250
251/*
252 * time_init() - it does the following things.
253 *
254 * 1) board_time_init() -
255 * a) (optional) set up RTC routines,
256 * b) (optional) calibrate and set the mips_hpt_frequency
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900257 * (only needed if you intended to use cpu counter as timer interrupt
258 * source)
Yoichi Yuasad23ee8f2006-03-27 01:16:33 -0800259 * 2) setup xtime based on rtc_mips_get_time().
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900260 * 3) calculate a couple of cached variables for later usage
261 * 4) plat_timer_setup() -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 * a) (optional) over-write any choices made above by time_init().
263 * b) machine specific code should setup the timer irqaction.
264 * c) enable the timer interrupt
265 */
266
267void (*board_time_init)(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269unsigned int mips_hpt_frequency;
270
271static struct irqaction timer_irqaction = {
272 .handler = timer_interrupt,
Thomas Gleixnerf40298f2006-07-01 19:29:20 -0700273 .flags = IRQF_DISABLED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 .name = "timer",
275};
276
277static unsigned int __init calibrate_hpt(void)
278{
279 u64 frequency;
280 u32 hpt_start, hpt_end, hpt_count, hz;
281
282 const int loops = HZ / 10;
283 int log_2_loops = 0;
284 int i;
285
286 /*
287 * We want to calibrate for 0.1s, but to avoid a 64-bit
288 * division we round the number of loops up to the nearest
289 * power of 2.
290 */
291 while (loops > 1 << log_2_loops)
292 log_2_loops++;
293 i = 1 << log_2_loops;
294
295 /*
296 * Wait for a rising edge of the timer interrupt.
297 */
298 while (mips_timer_state());
299 while (!mips_timer_state());
300
301 /*
302 * Now see how many high precision timer ticks happen
303 * during the calculated number of periods between timer
304 * interrupts.
305 */
306 hpt_start = mips_hpt_read();
307 do {
308 while (mips_timer_state());
309 while (!mips_timer_state());
310 } while (--i);
311 hpt_end = mips_hpt_read();
312
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900313 hpt_count = (hpt_end - hpt_start) & mips_hpt_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 hz = HZ;
315 frequency = (u64)hpt_count * (u64)hz;
316
317 return frequency >> log_2_loops;
318}
319
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900320static cycle_t read_mips_hpt(void)
321{
322 return (cycle_t)mips_hpt_read();
323}
324
325static struct clocksource clocksource_mips = {
326 .name = "MIPS",
327 .read = read_mips_hpt,
328 .is_continuous = 1,
329};
330
331static void __init init_mips_clocksource(void)
332{
333 u64 temp;
334 u32 shift;
335
336 if (!mips_hpt_frequency || mips_hpt_read == null_hpt_read)
337 return;
338
339 /* Calclate a somewhat reasonable rating value */
340 clocksource_mips.rating = 200 + mips_hpt_frequency / 10000000;
341 /* Find a shift value */
342 for (shift = 32; shift > 0; shift--) {
343 temp = (u64) NSEC_PER_SEC << shift;
344 do_div(temp, mips_hpt_frequency);
345 if ((temp >> 32) == 0)
346 break;
347 }
348 clocksource_mips.shift = shift;
349 clocksource_mips.mult = (u32)temp;
350 clocksource_mips.mask = mips_hpt_mask;
351
352 clocksource_register(&clocksource_mips);
353}
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355void __init time_init(void)
356{
357 if (board_time_init)
358 board_time_init();
359
Yoichi Yuasad23ee8f2006-03-27 01:16:33 -0800360 if (!rtc_mips_set_mmss)
361 rtc_mips_set_mmss = rtc_mips_set_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Yoichi Yuasad23ee8f2006-03-27 01:16:33 -0800363 xtime.tv_sec = rtc_mips_get_time();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 xtime.tv_nsec = 0;
365
366 set_normalized_timespec(&wall_to_monotonic,
367 -xtime.tv_sec, -xtime.tv_nsec);
368
369 /* Choose appropriate high precision timer routines. */
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900370 if (!cpu_has_counter && !mips_hpt_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 /* No high precision timer -- sorry. */
372 mips_hpt_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 Nemoto16b7b2a2006-10-24 00:21:27 +0900375 if (!mips_hpt_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 /* No external high precision timer -- use R4k. */
377 mips_hpt_read = c0_hpt_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 } else {
379 /* We know counter frequency. Or we can get it. */
380 if (!mips_hpt_read) {
381 /* No external high precision timer -- use R4k. */
382 mips_hpt_read = c0_hpt_read;
383
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. */
386 mips_hpt_init = c0_hpt_timer_init;
387 mips_timer_ack = c0_timer_ack;
388 }
389 }
390 if (!mips_hpt_frequency)
391 mips_hpt_frequency = calibrate_hpt();
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 /* Calculate cache parameters. */
394 cycles_per_jiffy = (mips_hpt_frequency + HZ / 2) / HZ;
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 /* Report the high precision timer rate for a reference. */
397 printk("Using %u.%03u MHz high precision timer.\n",
398 ((mips_hpt_frequency + 500) / 1000) / 1000,
399 ((mips_hpt_frequency + 500) / 1000) % 1000);
400 }
401
402 if (!mips_timer_ack)
403 /* No timer interrupt ack (e.g. i8254). */
404 mips_timer_ack = null_timer_ack;
405
406 /* This sets up the high precision timer for the first interrupt. */
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900407 mips_hpt_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 /*
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();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
425#define FEBRUARY 2
426#define STARTOFTIME 1970
427#define SECDAY 86400L
428#define SECYR (SECDAY * 365)
429#define leapyear(y) ((!((y) % 4) && ((y) % 100)) || !((y) % 400))
430#define days_in_year(y) (leapyear(y) ? 366 : 365)
431#define days_in_month(m) (month_days[(m) - 1])
432
433static int month_days[12] = {
434 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
435};
436
437void to_tm(unsigned long tim, struct rtc_time *tm)
438{
439 long hms, day, gday;
440 int i;
441
442 gday = day = tim / SECDAY;
443 hms = tim % SECDAY;
444
445 /* Hours, minutes, seconds are easy */
446 tm->tm_hour = hms / 3600;
447 tm->tm_min = (hms % 3600) / 60;
448 tm->tm_sec = (hms % 3600) % 60;
449
450 /* Number of years in days */
451 for (i = STARTOFTIME; day >= days_in_year(i); i++)
452 day -= days_in_year(i);
453 tm->tm_year = i;
454
455 /* Number of months in days left */
456 if (leapyear(tm->tm_year))
457 days_in_month(FEBRUARY) = 29;
458 for (i = 1; day >= days_in_month(i); i++)
459 day -= days_in_month(i);
460 days_in_month(FEBRUARY) = 28;
461 tm->tm_mon = i - 1; /* tm_mon starts from 0 to 11 */
462
463 /* Days are what is left over (+1) from all that. */
464 tm->tm_mday = day + 1;
465
466 /*
467 * Determine the day of week
468 */
469 tm->tm_wday = (gday + 4) % 7; /* 1970/1/1 was Thursday */
470}
471
472EXPORT_SYMBOL(rtc_lock);
473EXPORT_SYMBOL(to_tm);
Yoichi Yuasad23ee8f2006-03-27 01:16:33 -0800474EXPORT_SYMBOL(rtc_mips_set_time);
475EXPORT_SYMBOL(rtc_mips_get_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477unsigned long long sched_clock(void)
478{
479 return (unsigned long long)jiffies*(1000000000/HZ);
480}