GNU Linux-libre 4.19.264-gnu1
[releases.git] / arch / mips / kernel / time.c
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
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
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
102 static int null_perf_irq(void)
103 {
104         return 0;
105 }
106
107 int (*perf_irq)(void) = null_perf_irq;
108
109 EXPORT_SYMBOL(perf_irq);
110
111 /*
112  * time_init() - it does the following things.
113  *
114  * 1) plat_time_init() -
115  *      a) (optional) set up RTC routines,
116  *      b) (optional) calibrate and set the mips_hpt_frequency
117  *          (only needed if you intended to use cpu counter as timer interrupt
118  *           source)
119  * 2) calculate a couple of cached variables for later usage
120  */
121
122 unsigned int mips_hpt_frequency;
123 EXPORT_SYMBOL_GPL(mips_hpt_frequency);
124
125 static __init int cpu_has_mfc0_count_bug(void)
126 {
127         switch (current_cpu_type()) {
128         case CPU_R4000PC:
129         case CPU_R4000SC:
130         case CPU_R4000MC:
131                 /*
132                  * V3.0 is documented as suffering from the mfc0 from count bug.
133                  * Afaik this is the last version of the R4000.  Later versions
134                  * were marketed as R4400.
135                  */
136                 return 1;
137
138         case CPU_R4400PC:
139         case CPU_R4400SC:
140         case CPU_R4400MC:
141                 /*
142                  * The published errata for the R4400 up to 3.0 say the CPU
143                  * has the mfc0 from count bug.  This seems the last version
144                  * produced.
145                  */
146                 return 1;
147         }
148
149         return 0;
150 }
151
152 void __init time_init(void)
153 {
154         plat_time_init();
155
156         /*
157          * The use of the R4k timer as a clock event takes precedence;
158          * if reading the Count register might interfere with the timer
159          * interrupt, then we don't use the timer as a clock source.
160          * We may still use the timer as a clock source though if the
161          * timer interrupt isn't reliable; the interference doesn't
162          * matter then, because we don't use the interrupt.
163          */
164         if (mips_clockevent_init() != 0 || !cpu_has_mfc0_count_bug())
165                 init_mips_clocksource();
166 }