time.c 4.0 KB

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