• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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.
7  *
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.
12  */
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>
27 
28 #include <asm/cpu-features.h>
29 #include <asm/cpu-type.h>
30 #include <asm/div64.h>
31 #include <asm/time.h>
32 
33 #ifdef CONFIG_CPU_FREQ
34 
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;
39 
cpufreq_callback(struct notifier_block * nb,unsigned long val,void * data)40 static int cpufreq_callback(struct notifier_block *nb,
41 			    unsigned long val, void *data)
42 {
43 	int cpu;
44 	struct cpufreq_freqs *freq = data;
45 
46 	/*
47 	 * Skip lpj numbers adjustment if the CPU-freq transition is safe for
48 	 * the loops delay. (Is this possible?)
49 	 */
50 	if (freq->flags & CPUFREQ_CONST_LOOPS)
51 		return NOTIFY_OK;
52 
53 	/* Save the initial values of the lpjes for future scaling. */
54 	if (!glb_lpj_ref) {
55 		glb_lpj_ref = boot_cpu_data.udelay_val;
56 		glb_lpj_ref_freq = freq->old;
57 
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;
62 		}
63 	}
64 
65 	cpu = freq->cpu;
66 	/*
67 	 * Adjust global lpj variable and per-CPU udelay_val number in
68 	 * accordance with the new CPU frequency.
69 	 */
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,
73 						glb_lpj_ref_freq,
74 						freq->new);
75 
76 		cpu_data[cpu].udelay_val = cpufreq_scale(per_cpu(pcp_lpj_ref, cpu),
77 					   per_cpu(pcp_lpj_ref_freq, cpu), freq->new);
78 	}
79 
80 	return NOTIFY_OK;
81 }
82 
83 static struct notifier_block cpufreq_notifier = {
84 	.notifier_call  = cpufreq_callback,
85 };
86 
register_cpufreq_notifier(void)87 static int __init register_cpufreq_notifier(void)
88 {
89 	return cpufreq_register_notifier(&cpufreq_notifier,
90 					 CPUFREQ_TRANSITION_NOTIFIER);
91 }
92 core_initcall(register_cpufreq_notifier);
93 
94 #endif /* CONFIG_CPU_FREQ */
95 
96 /*
97  * forward reference
98  */
99 DEFINE_SPINLOCK(rtc_lock);
100 EXPORT_SYMBOL(rtc_lock);
101 
rtc_mips_set_time(unsigned long sec)102 int __weak rtc_mips_set_time(unsigned long sec)
103 {
104 	return 0;
105 }
106 
rtc_mips_set_mmss(unsigned long nowtime)107 int __weak rtc_mips_set_mmss(unsigned long nowtime)
108 {
109 	return rtc_mips_set_time(nowtime);
110 }
111 
update_persistent_clock(struct timespec now)112 int update_persistent_clock(struct timespec now)
113 {
114 	return rtc_mips_set_mmss(now.tv_sec);
115 }
116 
null_perf_irq(void)117 static int null_perf_irq(void)
118 {
119 	return 0;
120 }
121 
122 int (*perf_irq)(void) = null_perf_irq;
123 
124 EXPORT_SYMBOL(perf_irq);
125 
126 /*
127  * time_init() - it does the following things.
128  *
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
133  *	     source)
134  * 2) calculate a couple of cached variables for later usage
135  */
136 
137 unsigned int mips_hpt_frequency;
138 
139 /*
140  * This function exists in order to cause an error due to a duplicate
141  * definition if platform code should have its own implementation.  The hook
142  * to use instead is plat_time_init.  plat_time_init does not receive the
143  * irqaction pointer argument anymore.	This is because any function which
144  * initializes an interrupt timer now takes care of its own request_irq rsp.
145  * setup_irq calls and each clock_event_device should use its own
146  * struct irqrequest.
147  */
plat_timer_setup(void)148 void __init plat_timer_setup(void)
149 {
150 	BUG();
151 }
152 
cpu_has_mfc0_count_bug(void)153 static __init int cpu_has_mfc0_count_bug(void)
154 {
155 	switch (current_cpu_type()) {
156 	case CPU_R4000PC:
157 	case CPU_R4000SC:
158 	case CPU_R4000MC:
159 		/*
160 		 * V3.0 is documented as suffering from the mfc0 from count bug.
161 		 * Afaik this is the last version of the R4000.	 Later versions
162 		 * were marketed as R4400.
163 		 */
164 		return 1;
165 
166 	case CPU_R4400PC:
167 	case CPU_R4400SC:
168 	case CPU_R4400MC:
169 		/*
170 		 * The published errata for the R4400 up to 3.0 say the CPU
171 		 * has the mfc0 from count bug.
172 		 */
173 		if ((current_cpu_data.processor_id & 0xff) <= 0x30)
174 			return 1;
175 
176 		/*
177 		 * we assume newer revisions are ok
178 		 */
179 		return 0;
180 	}
181 
182 	return 0;
183 }
184 
time_init(void)185 void __init time_init(void)
186 {
187 	plat_time_init();
188 
189 	/*
190 	 * The use of the R4k timer as a clock event takes precedence;
191 	 * if reading the Count register might interfere with the timer
192 	 * interrupt, then we don't use the timer as a clock source.
193 	 * We may still use the timer as a clock source though if the
194 	 * timer interrupt isn't reliable; the interference doesn't
195 	 * matter then, because we don't use the interrupt.
196 	 */
197 	if (mips_clockevent_init() != 0 || !cpu_has_mfc0_count_bug())
198 		init_mips_clocksource();
199 }
200