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
6 * Common time service routines for MIPS machines. See
7 * Documentation/mips/time.README.
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.
14 #include <linux/clockchips.h>
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>
20 #include <linux/profile.h>
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>
28 #include <linux/kallsyms.h>
30 #include <asm/bootinfo.h>
31 #include <asm/cache.h>
32 #include <asm/compiler.h>
34 #include <asm/cpu-features.h>
35 #include <asm/div64.h>
36 #include <asm/sections.h>
37 #include <asm/smtc_ipi.h>
45 DEFINE_SPINLOCK(rtc_lock);
46 EXPORT_SYMBOL(rtc_lock);
48 int __weak rtc_mips_set_time(unsigned long sec)
52 EXPORT_SYMBOL(rtc_mips_set_time);
54 int __weak rtc_mips_set_mmss(unsigned long nowtime)
56 return rtc_mips_set_time(nowtime);
59 int update_persistent_clock(struct timespec now)
61 return rtc_mips_set_mmss(now.tv_sec);
65 * Null high precision timer functions for systems lacking one.
67 static cycle_t null_hpt_read(void)
73 * High precision timer functions for a R4k-compatible timer.
75 static cycle_t c0_hpt_read(void)
77 return read_c0_count();
80 int (*mips_timer_state)(void);
83 * local_timer_interrupt() does profiling and process accounting
86 * In UP mode, it is invoked from the (global) timer_interrupt.
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.
92 void local_timer_interrupt(int irq, void *dev_id)
94 profile_tick(CPU_PROFILING);
95 update_process_times(user_mode(get_irq_regs()));
98 int null_perf_irq(void)
103 EXPORT_SYMBOL(null_perf_irq);
105 int (*perf_irq)(void) = null_perf_irq;
107 EXPORT_SYMBOL(perf_irq);
110 * time_init() - it does the following things.
112 * 1) plat_time_init() -
113 * a) (optional) set up RTC routines,
114 * b) (optional) calibrate and set the mips_hpt_frequency
115 * (only needed if you intended to use cpu counter as timer interrupt
117 * 2) calculate a couple of cached variables for later usage
118 * 3) plat_timer_setup() -
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
124 unsigned int mips_hpt_frequency;
126 static unsigned int __init calibrate_hpt(void)
128 cycle_t frequency, hpt_start, hpt_end, hpt_count, hz;
130 const int loops = HZ / 10;
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
139 while (loops > 1 << log_2_loops)
141 i = 1 << log_2_loops;
144 * Wait for a rising edge of the timer interrupt.
146 while (mips_timer_state());
147 while (!mips_timer_state());
150 * Now see how many high precision timer ticks happen
151 * during the calculated number of periods between timer
154 hpt_start = clocksource_mips.read();
156 while (mips_timer_state());
157 while (!mips_timer_state());
159 hpt_end = clocksource_mips.read();
161 hpt_count = (hpt_end - hpt_start) & clocksource_mips.mask;
163 frequency = hpt_count * hz;
165 return frequency >> log_2_loops;
168 struct clocksource clocksource_mips = {
170 .mask = CLOCKSOURCE_MASK(32),
171 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
174 void __init clocksource_set_clock(struct clocksource *cs, unsigned int clock)
179 /* Find a shift value */
180 for (shift = 32; shift > 0; shift--) {
181 temp = (u64) NSEC_PER_SEC << shift;
183 if ((temp >> 32) == 0)
187 cs->mult = (u32) temp;
190 void __cpuinit clockevent_set_clock(struct clock_event_device *cd,
196 /* Find a shift value */
197 for (shift = 32; shift > 0; shift--) {
198 temp = (u64) clock << shift;
199 do_div(temp, NSEC_PER_SEC);
200 if ((temp >> 32) == 0)
204 cd->mult = (u32) temp;
207 static void __init init_mips_clocksource(void)
209 if (!mips_hpt_frequency || clocksource_mips.read == null_hpt_read)
212 /* Calclate a somewhat reasonable rating value */
213 clocksource_mips.rating = 200 + mips_hpt_frequency / 10000000;
215 clocksource_set_clock(&clocksource_mips, mips_hpt_frequency);
217 clocksource_register(&clocksource_mips);
220 void __init __weak plat_time_init(void)
224 void __init __weak plat_timer_setup(struct irqaction *irq)
228 void __init time_init(void)
232 /* Choose appropriate high precision timer routines. */
233 if (!cpu_has_counter && !clocksource_mips.read)
234 /* No high precision timer -- sorry. */
235 clocksource_mips.read = null_hpt_read;
236 else if (!mips_hpt_frequency && !mips_timer_state) {
237 /* A high precision timer of unknown frequency. */
238 if (!clocksource_mips.read)
239 /* No external high precision timer -- use R4k. */
240 clocksource_mips.read = c0_hpt_read;
242 /* We know counter frequency. Or we can get it. */
243 if (!clocksource_mips.read) {
244 /* No external high precision timer -- use R4k. */
245 clocksource_mips.read = c0_hpt_read;
247 if (!mips_hpt_frequency)
248 mips_hpt_frequency = calibrate_hpt();
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);
256 init_mips_clocksource();
257 mips_clockevent_init();