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/bug.h>
15 #include <linux/clockchips.h>
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/sched.h>
20 #include <linux/param.h>
21 #include <linux/profile.h>
22 #include <linux/time.h>
23 #include <linux/timex.h>
24 #include <linux/smp.h>
25 #include <linux/kernel_stat.h>
26 #include <linux/spinlock.h>
27 #include <linux/interrupt.h>
28 #include <linux/module.h>
29 #include <linux/kallsyms.h>
31 #include <asm/bootinfo.h>
32 #include <asm/cache.h>
33 #include <asm/compiler.h>
35 #include <asm/cpu-features.h>
36 #include <asm/div64.h>
37 #include <asm/sections.h>
38 #include <asm/smtc_ipi.h>
46 DEFINE_SPINLOCK(rtc_lock
);
47 EXPORT_SYMBOL(rtc_lock
);
49 int __weak
rtc_mips_set_time(unsigned long sec
)
53 EXPORT_SYMBOL(rtc_mips_set_time
);
55 int __weak
rtc_mips_set_mmss(unsigned long nowtime
)
57 return rtc_mips_set_time(nowtime
);
60 int update_persistent_clock(struct timespec now
)
62 return rtc_mips_set_mmss(now
.tv_sec
);
66 * Null high precision timer functions for systems lacking one.
68 static cycle_t
null_hpt_read(void)
74 * High precision timer functions for a R4k-compatible timer.
76 static cycle_t
c0_hpt_read(void)
78 return read_c0_count();
81 int (*mips_timer_state
)(void);
84 * local_timer_interrupt() does profiling and process accounting
87 * In UP mode, it is invoked from the (global) timer_interrupt.
89 * In SMP mode, it might invoked by per-CPU timer interrupt, or
90 * a broadcasted inter-processor interrupt which itself is triggered
91 * by the global timer interrupt.
93 void local_timer_interrupt(int irq
, void *dev_id
)
95 profile_tick(CPU_PROFILING
);
96 update_process_times(user_mode(get_irq_regs()));
99 int null_perf_irq(void)
104 EXPORT_SYMBOL(null_perf_irq
);
106 int (*perf_irq
)(void) = null_perf_irq
;
108 EXPORT_SYMBOL(perf_irq
);
111 * time_init() - it does the following things.
113 * 1) plat_time_init() -
114 * a) (optional) set up RTC routines,
115 * b) (optional) calibrate and set the mips_hpt_frequency
116 * (only needed if you intended to use cpu counter as timer interrupt
118 * 2) calculate a couple of cached variables for later usage
121 unsigned int mips_hpt_frequency
;
123 static unsigned int __init
calibrate_hpt(void)
125 cycle_t frequency
, hpt_start
, hpt_end
, hpt_count
, hz
;
127 const int loops
= HZ
/ 10;
132 * We want to calibrate for 0.1s, but to avoid a 64-bit
133 * division we round the number of loops up to the nearest
136 while (loops
> 1 << log_2_loops
)
138 i
= 1 << log_2_loops
;
141 * Wait for a rising edge of the timer interrupt.
143 while (mips_timer_state());
144 while (!mips_timer_state());
147 * Now see how many high precision timer ticks happen
148 * during the calculated number of periods between timer
151 hpt_start
= clocksource_mips
.read();
153 while (mips_timer_state());
154 while (!mips_timer_state());
156 hpt_end
= clocksource_mips
.read();
158 hpt_count
= (hpt_end
- hpt_start
) & clocksource_mips
.mask
;
160 frequency
= hpt_count
* hz
;
162 return frequency
>> log_2_loops
;
165 struct clocksource clocksource_mips
= {
167 .mask
= CLOCKSOURCE_MASK(32),
168 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
171 void __init
clocksource_set_clock(struct clocksource
*cs
, unsigned int clock
)
176 /* Find a shift value */
177 for (shift
= 32; shift
> 0; shift
--) {
178 temp
= (u64
) NSEC_PER_SEC
<< shift
;
180 if ((temp
>> 32) == 0)
184 cs
->mult
= (u32
) temp
;
187 void __cpuinit
clockevent_set_clock(struct clock_event_device
*cd
,
193 /* Find a shift value */
194 for (shift
= 32; shift
> 0; shift
--) {
195 temp
= (u64
) clock
<< shift
;
196 do_div(temp
, NSEC_PER_SEC
);
197 if ((temp
>> 32) == 0)
201 cd
->mult
= (u32
) temp
;
204 static void __init
init_mips_clocksource(void)
206 if (!mips_hpt_frequency
|| clocksource_mips
.read
== null_hpt_read
)
209 /* Calclate a somewhat reasonable rating value */
210 clocksource_mips
.rating
= 200 + mips_hpt_frequency
/ 10000000;
212 clocksource_set_clock(&clocksource_mips
, mips_hpt_frequency
);
214 clocksource_register(&clocksource_mips
);
217 void __init __weak
plat_time_init(void)
222 * This function exists in order to cause an error due to a duplicate
223 * definition if platform code should have its own implementation. The hook
224 * to use instead is plat_time_init. plat_time_init does not receive the
225 * irqaction pointer argument anymore. This is because any function which
226 * initializes an interrupt timer now takes care of its own request_irq rsp.
227 * setup_irq calls and each clock_event_device should use its own
230 void __init
plat_timer_setup(struct irqaction
*irq
)
235 void __init
time_init(void)
239 /* Choose appropriate high precision timer routines. */
240 if (!cpu_has_counter
&& !clocksource_mips
.read
)
241 /* No high precision timer -- sorry. */
242 clocksource_mips
.read
= null_hpt_read
;
243 else if (!mips_hpt_frequency
&& !mips_timer_state
) {
244 /* A high precision timer of unknown frequency. */
245 if (!clocksource_mips
.read
)
246 /* No external high precision timer -- use R4k. */
247 clocksource_mips
.read
= c0_hpt_read
;
249 /* We know counter frequency. Or we can get it. */
250 if (!clocksource_mips
.read
) {
251 /* No external high precision timer -- use R4k. */
252 clocksource_mips
.read
= c0_hpt_read
;
254 if (!mips_hpt_frequency
)
255 mips_hpt_frequency
= calibrate_hpt();
257 /* Report the high precision timer rate for a reference. */
258 printk("Using %u.%03u MHz high precision timer.\n",
259 ((mips_hpt_frequency
+ 500) / 1000) / 1000,
260 ((mips_hpt_frequency
+ 500) / 1000) % 1000);
263 init_mips_clocksource();
264 mips_clockevent_init();