^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright 2001 MontaVista Software Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2003, 2004 Maciej W. Rozycki
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Common time service routines for MIPS machines.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/bug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/clockchips.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/param.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/timex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/smp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/cpufreq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <asm/cpu-features.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <asm/cpu-type.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <asm/div64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <asm/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #ifdef CONFIG_CPU_FREQ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static DEFINE_PER_CPU(unsigned long, pcp_lpj_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static DEFINE_PER_CPU(unsigned long, pcp_lpj_ref_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static unsigned long glb_lpj_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static unsigned long glb_lpj_ref_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static int cpufreq_callback(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) unsigned long val, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct cpufreq_freqs *freq = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct cpumask *cpus = freq->policy->cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) unsigned long lpj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * Skip lpj numbers adjustment if the CPU-freq transition is safe for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * the loops delay. (Is this possible?)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (freq->flags & CPUFREQ_CONST_LOOPS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /* Save the initial values of the lpjes for future scaling. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (!glb_lpj_ref) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) glb_lpj_ref = boot_cpu_data.udelay_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) glb_lpj_ref_freq = freq->old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) for_each_online_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) per_cpu(pcp_lpj_ref, cpu) =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) cpu_data[cpu].udelay_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) per_cpu(pcp_lpj_ref_freq, cpu) = freq->old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * Adjust global lpj variable and per-CPU udelay_val number in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * accordance with the new CPU frequency.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) (val == CPUFREQ_POSTCHANGE && freq->old > freq->new)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) loops_per_jiffy = cpufreq_scale(glb_lpj_ref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) glb_lpj_ref_freq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) freq->new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) for_each_cpu(cpu, cpus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) lpj = cpufreq_scale(per_cpu(pcp_lpj_ref, cpu),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) per_cpu(pcp_lpj_ref_freq, cpu),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) freq->new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) cpu_data[cpu].udelay_val = (unsigned int)lpj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static struct notifier_block cpufreq_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) .notifier_call = cpufreq_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static int __init register_cpufreq_notifier(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return cpufreq_register_notifier(&cpufreq_notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) CPUFREQ_TRANSITION_NOTIFIER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) core_initcall(register_cpufreq_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) #endif /* CONFIG_CPU_FREQ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * forward reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) DEFINE_SPINLOCK(rtc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) EXPORT_SYMBOL(rtc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static int null_perf_irq(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int (*perf_irq)(void) = null_perf_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) EXPORT_SYMBOL(perf_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * time_init() - it does the following things.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * 1) plat_time_init() -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * a) (optional) set up RTC routines,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * b) (optional) calibrate and set the mips_hpt_frequency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * (only needed if you intended to use cpu counter as timer interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * source)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * 2) calculate a couple of cached variables for later usage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) unsigned int mips_hpt_frequency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) EXPORT_SYMBOL_GPL(mips_hpt_frequency);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static __init int cpu_has_mfc0_count_bug(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) switch (current_cpu_type()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) case CPU_R4000PC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) case CPU_R4000SC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) case CPU_R4000MC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * V3.0 is documented as suffering from the mfc0 from count bug.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * Afaik this is the last version of the R4000. Later versions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * were marketed as R4400.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) case CPU_R4400PC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) case CPU_R4400SC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) case CPU_R4400MC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * The published errata for the R4400 up to 3.0 say the CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * has the mfc0 from count bug.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if ((current_cpu_data.processor_id & 0xff) <= 0x30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * we assume newer revisions are ok
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) void __init time_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) plat_time_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * The use of the R4k timer as a clock event takes precedence;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * if reading the Count register might interfere with the timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * interrupt, then we don't use the timer as a clock source.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * We may still use the timer as a clock source though if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * timer interrupt isn't reliable; the interference doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * matter then, because we don't use the interrupt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (mips_clockevent_init() != 0 || !cpu_has_mfc0_count_bug())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) init_mips_clocksource();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }