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.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
13 #include <linux/bug.h>
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/time.h>
21 #include <linux/timex.h>
22 #include <linux/smp.h>
23 #include <linux/spinlock.h>
24 #include <linux/export.h>
25 #include <linux/cpufreq.h>
26 #include <linux/delay.h>
28 #include <asm/cpu-features.h>
29 #include <asm/cpu-type.h>
30 #include <asm/div64.h>
33 #ifdef CONFIG_CPU_FREQ
35 static DEFINE_PER_CPU(unsigned long, pcp_lpj_ref);
36 static DEFINE_PER_CPU(unsigned long, pcp_lpj_ref_freq);
37 static unsigned long glb_lpj_ref;
38 static unsigned long glb_lpj_ref_freq;
40 static int cpufreq_callback(struct notifier_block *nb,
41 unsigned long val, void *data)
44 struct cpufreq_freqs *freq = data;
47 * Skip lpj numbers adjustment if the CPU-freq transition is safe for
48 * the loops delay. (Is this possible?)
50 if (freq->flags & CPUFREQ_CONST_LOOPS)
53 /* Save the initial values of the lpjes for future scaling. */
55 glb_lpj_ref = boot_cpu_data.udelay_val;
56 glb_lpj_ref_freq = freq->old;
58 for_each_online_cpu(cpu) {
59 per_cpu(pcp_lpj_ref, cpu) =
60 cpu_data[cpu].udelay_val;
61 per_cpu(pcp_lpj_ref_freq, cpu) = freq->old;
67 * Adjust global lpj variable and per-CPU udelay_val number in
68 * accordance with the new CPU frequency.
70 if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
71 (val == CPUFREQ_POSTCHANGE && freq->old > freq->new)) {
72 loops_per_jiffy = cpufreq_scale(glb_lpj_ref,
76 cpu_data[cpu].udelay_val = cpufreq_scale(per_cpu(pcp_lpj_ref, cpu),
77 per_cpu(pcp_lpj_ref_freq, cpu), freq->new);
83 static struct notifier_block cpufreq_notifier = {
84 .notifier_call = cpufreq_callback,
87 static int __init register_cpufreq_notifier(void)
89 return cpufreq_register_notifier(&cpufreq_notifier,
90 CPUFREQ_TRANSITION_NOTIFIER);
92 core_initcall(register_cpufreq_notifier);
94 #endif /* CONFIG_CPU_FREQ */
99 DEFINE_SPINLOCK(rtc_lock);
100 EXPORT_SYMBOL(rtc_lock);
102 int __weak rtc_mips_set_time(unsigned long sec)
107 int __weak rtc_mips_set_mmss(unsigned long nowtime)
109 return rtc_mips_set_time(nowtime);
112 int update_persistent_clock(struct timespec now)
114 return rtc_mips_set_mmss(now.tv_sec);
117 static int null_perf_irq(void)
122 int (*perf_irq)(void) = null_perf_irq;
124 EXPORT_SYMBOL(perf_irq);
127 * time_init() - it does the following things.
129 * 1) plat_time_init() -
130 * a) (optional) set up RTC routines,
131 * b) (optional) calibrate and set the mips_hpt_frequency
132 * (only needed if you intended to use cpu counter as timer interrupt
134 * 2) calculate a couple of cached variables for later usage
137 unsigned int mips_hpt_frequency;
138 EXPORT_SYMBOL_GPL(mips_hpt_frequency);
140 static __init int cpu_has_mfc0_count_bug(void)
142 switch (current_cpu_type()) {
147 * V3.0 is documented as suffering from the mfc0 from count bug.
148 * Afaik this is the last version of the R4000. Later versions
149 * were marketed as R4400.
157 * The published errata for the R4400 up to 3.0 say the CPU
158 * has the mfc0 from count bug.
160 if ((current_cpu_data.processor_id & 0xff) <= 0x30)
164 * we assume newer revisions are ok
172 void __init time_init(void)
177 * The use of the R4k timer as a clock event takes precedence;
178 * if reading the Count register might interfere with the timer
179 * interrupt, then we don't use the timer as a clock source.
180 * We may still use the timer as a clock source though if the
181 * timer interrupt isn't reliable; the interference doesn't
182 * matter then, because we don't use the interrupt.
184 if (mips_clockevent_init() != 0 || !cpu_has_mfc0_count_bug())
185 init_mips_clocksource();