time.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #include <asm/cpu-features.h>
  28. #include <asm/cpu-type.h>
  29. #include <asm/div64.h>
  30. #include <asm/time.h>
  31. #ifdef CONFIG_CPU_FREQ
  32. static DEFINE_PER_CPU(unsigned long, pcp_lpj_ref);
  33. static DEFINE_PER_CPU(unsigned long, pcp_lpj_ref_freq);
  34. static unsigned long glb_lpj_ref;
  35. static unsigned long glb_lpj_ref_freq;
  36. static int cpufreq_callback(struct notifier_block *nb,
  37. unsigned long val, void *data)
  38. {
  39. int cpu;
  40. struct cpufreq_freqs *freq = data;
  41. /*
  42. * Skip lpj numbers adjustment if the CPU-freq transition is safe for
  43. * the loops delay. (Is this possible?)
  44. */
  45. if (freq->flags & CPUFREQ_CONST_LOOPS)
  46. return NOTIFY_OK;
  47. /* Save the initial values of the lpjes for future scaling. */
  48. if (!glb_lpj_ref) {
  49. glb_lpj_ref = boot_cpu_data.udelay_val;
  50. glb_lpj_ref_freq = freq->old;
  51. for_each_online_cpu(cpu) {
  52. per_cpu(pcp_lpj_ref, cpu) =
  53. cpu_data[cpu].udelay_val;
  54. per_cpu(pcp_lpj_ref_freq, cpu) = freq->old;
  55. }
  56. }
  57. cpu = freq->cpu;
  58. /*
  59. * Adjust global lpj variable and per-CPU udelay_val number in
  60. * accordance with the new CPU frequency.
  61. */
  62. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  63. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new)) {
  64. loops_per_jiffy = cpufreq_scale(glb_lpj_ref,
  65. glb_lpj_ref_freq,
  66. freq->new);
  67. cpu_data[cpu].udelay_val = cpufreq_scale(per_cpu(pcp_lpj_ref, cpu),
  68. per_cpu(pcp_lpj_ref_freq, cpu), freq->new);
  69. }
  70. return NOTIFY_OK;
  71. }
  72. static struct notifier_block cpufreq_notifier = {
  73. .notifier_call = cpufreq_callback,
  74. };
  75. static int __init register_cpufreq_notifier(void)
  76. {
  77. return cpufreq_register_notifier(&cpufreq_notifier,
  78. CPUFREQ_TRANSITION_NOTIFIER);
  79. }
  80. core_initcall(register_cpufreq_notifier);
  81. #endif /* CONFIG_CPU_FREQ */
  82. /*
  83. * forward reference
  84. */
  85. DEFINE_SPINLOCK(rtc_lock);
  86. EXPORT_SYMBOL(rtc_lock);
  87. static int null_perf_irq(void)
  88. {
  89. return 0;
  90. }
  91. int (*perf_irq)(void) = null_perf_irq;
  92. EXPORT_SYMBOL(perf_irq);
  93. /*
  94. * time_init() - it does the following things.
  95. *
  96. * 1) plat_time_init() -
  97. * a) (optional) set up RTC routines,
  98. * b) (optional) calibrate and set the mips_hpt_frequency
  99. * (only needed if you intended to use cpu counter as timer interrupt
  100. * source)
  101. * 2) calculate a couple of cached variables for later usage
  102. */
  103. unsigned int mips_hpt_frequency;
  104. EXPORT_SYMBOL_GPL(mips_hpt_frequency);
  105. static __init int cpu_has_mfc0_count_bug(void)
  106. {
  107. switch (current_cpu_type()) {
  108. case CPU_R4000PC:
  109. case CPU_R4000SC:
  110. case CPU_R4000MC:
  111. /*
  112. * V3.0 is documented as suffering from the mfc0 from count bug.
  113. * Afaik this is the last version of the R4000. Later versions
  114. * were marketed as R4400.
  115. */
  116. return 1;
  117. case CPU_R4400PC:
  118. case CPU_R4400SC:
  119. case CPU_R4400MC:
  120. /*
  121. * The published errata for the R4400 up to 3.0 say the CPU
  122. * has the mfc0 from count bug.
  123. */
  124. if ((current_cpu_data.processor_id & 0xff) <= 0x30)
  125. return 1;
  126. /*
  127. * we assume newer revisions are ok
  128. */
  129. return 0;
  130. }
  131. return 0;
  132. }
  133. void __init time_init(void)
  134. {
  135. plat_time_init();
  136. /*
  137. * The use of the R4k timer as a clock event takes precedence;
  138. * if reading the Count register might interfere with the timer
  139. * interrupt, then we don't use the timer as a clock source.
  140. * We may still use the timer as a clock source though if the
  141. * timer interrupt isn't reliable; the interference doesn't
  142. * matter then, because we don't use the interrupt.
  143. */
  144. if (mips_clockevent_init() != 0 || !cpu_has_mfc0_count_bug())
  145. init_mips_clocksource();
  146. }