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>
43 * The integer part of the number of usecs per jiffy is taken from tick,
44 * but the fractional part is not recorded, so we calculate it using the
45 * initial value of HZ. This aids systems where tick isn't really an
46 * integer (e.g. for HZ = 128).
48 #define USECS_PER_JIFFY TICK_SIZE
49 #define USECS_PER_JIFFY_FRAC ((unsigned long)(u32)((1000000ULL << 32) / HZ))
51 #define TICK_SIZE (tick_nsec / 1000)
56 DEFINE_SPINLOCK(rtc_lock);
57 EXPORT_SYMBOL(rtc_lock);
59 int __weak rtc_mips_set_time(unsigned long sec)
63 EXPORT_SYMBOL(rtc_mips_set_time);
65 int __weak rtc_mips_set_mmss(unsigned long nowtime)
67 return rtc_mips_set_time(nowtime);
70 int update_persistent_clock(struct timespec now)
72 return rtc_mips_set_mmss(now.tv_sec);
76 * Null high precision timer functions for systems lacking one.
78 static cycle_t null_hpt_read(void)
84 * High precision timer functions for a R4k-compatible timer.
86 static cycle_t c0_hpt_read(void)
88 return read_c0_count();
91 int (*mips_timer_state)(void);
94 * local_timer_interrupt() does profiling and process accounting
97 * In UP mode, it is invoked from the (global) timer_interrupt.
99 * In SMP mode, it might invoked by per-CPU timer interrupt, or
100 * a broadcasted inter-processor interrupt which itself is triggered
101 * by the global timer interrupt.
103 void local_timer_interrupt(int irq, void *dev_id)
105 profile_tick(CPU_PROFILING);
106 update_process_times(user_mode(get_irq_regs()));
109 int null_perf_irq(void)
114 EXPORT_SYMBOL(null_perf_irq);
116 int (*perf_irq)(void) = null_perf_irq;
118 EXPORT_SYMBOL(perf_irq);
121 * time_init() - it does the following things.
123 * 1) plat_time_init() -
124 * a) (optional) set up RTC routines,
125 * b) (optional) calibrate and set the mips_hpt_frequency
126 * (only needed if you intended to use cpu counter as timer interrupt
128 * 2) calculate a couple of cached variables for later usage
129 * 3) plat_timer_setup() -
130 * a) (optional) over-write any choices made above by time_init().
131 * b) machine specific code should setup the timer irqaction.
132 * c) enable the timer interrupt
135 unsigned int mips_hpt_frequency;
137 static unsigned int __init calibrate_hpt(void)
139 cycle_t frequency, hpt_start, hpt_end, hpt_count, hz;
141 const int loops = HZ / 10;
146 * We want to calibrate for 0.1s, but to avoid a 64-bit
147 * division we round the number of loops up to the nearest
150 while (loops > 1 << log_2_loops)
152 i = 1 << log_2_loops;
155 * Wait for a rising edge of the timer interrupt.
157 while (mips_timer_state());
158 while (!mips_timer_state());
161 * Now see how many high precision timer ticks happen
162 * during the calculated number of periods between timer
165 hpt_start = clocksource_mips.read();
167 while (mips_timer_state());
168 while (!mips_timer_state());
170 hpt_end = clocksource_mips.read();
172 hpt_count = (hpt_end - hpt_start) & clocksource_mips.mask;
174 frequency = hpt_count * hz;
176 return frequency >> log_2_loops;
179 struct clocksource clocksource_mips = {
181 .mask = CLOCKSOURCE_MASK(32),
182 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
185 static void __init init_mips_clocksource(void)
190 if (!mips_hpt_frequency || clocksource_mips.read == null_hpt_read)
193 /* Calclate a somewhat reasonable rating value */
194 clocksource_mips.rating = 200 + mips_hpt_frequency / 10000000;
195 /* Find a shift value */
196 for (shift = 32; shift > 0; shift--) {
197 temp = (u64) NSEC_PER_SEC << shift;
198 do_div(temp, mips_hpt_frequency);
199 if ((temp >> 32) == 0)
202 clocksource_mips.shift = shift;
203 clocksource_mips.mult = (u32)temp;
205 clocksource_register(&clocksource_mips);
208 void __init __weak plat_time_init(void)
212 void __init __weak plat_timer_setup(struct irqaction *irq)
216 #ifdef CONFIG_MIPS_MT_SMTC
217 DEFINE_PER_CPU(struct clock_event_device, smtc_dummy_clockevent_device);
219 static void smtc_set_mode(enum clock_event_mode mode,
220 struct clock_event_device *evt)
224 static void mips_broadcast(cpumask_t mask)
228 for_each_cpu_mask(cpu, mask)
229 smtc_send_ipi(cpu, SMTC_CLOCK_TICK, 0);
232 static void setup_smtc_dummy_clockevent_device(void)
234 //uint64_t mips_freq = mips_hpt_^frequency;
235 unsigned int cpu = smp_processor_id();
236 struct clock_event_device *cd;
238 cd = &per_cpu(smtc_dummy_clockevent_device, cpu);
241 cd->features = CLOCK_EVT_FEAT_DUMMY;
243 /* Calculate the min / max delta */
244 cd->mult = 0; //div_sc((unsigned long) mips_freq, NSEC_PER_SEC, 32);
246 cd->max_delta_ns = 0; //clockevent_delta2ns(0x7fffffff, cd);
247 cd->min_delta_ns = 0; //clockevent_delta2ns(0x30, cd);
252 // cd->cpumask = CPU_MASK_ALL; // cpumask_of_cpu(cpu);
254 cd->cpumask = cpumask_of_cpu(cpu);
256 cd->set_mode = smtc_set_mode;
258 cd->broadcast = mips_broadcast;
260 clockevents_register_device(cd);
264 void __init time_init(void)
268 /* Choose appropriate high precision timer routines. */
269 if (!cpu_has_counter && !clocksource_mips.read)
270 /* No high precision timer -- sorry. */
271 clocksource_mips.read = null_hpt_read;
272 else if (!mips_hpt_frequency && !mips_timer_state) {
273 /* A high precision timer of unknown frequency. */
274 if (!clocksource_mips.read)
275 /* No external high precision timer -- use R4k. */
276 clocksource_mips.read = c0_hpt_read;
278 /* We know counter frequency. Or we can get it. */
279 if (!clocksource_mips.read) {
280 /* No external high precision timer -- use R4k. */
281 clocksource_mips.read = c0_hpt_read;
283 if (!mips_hpt_frequency)
284 mips_hpt_frequency = calibrate_hpt();
286 /* Report the high precision timer rate for a reference. */
287 printk("Using %u.%03u MHz high precision timer.\n",
288 ((mips_hpt_frequency + 500) / 1000) / 1000,
289 ((mips_hpt_frequency + 500) / 1000) % 1000);
292 init_mips_clocksource();
293 mips_clockevent_init();