time.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. See
  7. * Documentation/mips/time.README.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. */
  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/profile.h>
  21. #include <linux/time.h>
  22. #include <linux/timex.h>
  23. #include <linux/smp.h>
  24. #include <linux/kernel_stat.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/module.h>
  28. #include <linux/kallsyms.h>
  29. #include <asm/bootinfo.h>
  30. #include <asm/cache.h>
  31. #include <asm/compiler.h>
  32. #include <asm/cpu.h>
  33. #include <asm/cpu-features.h>
  34. #include <asm/div64.h>
  35. #include <asm/sections.h>
  36. #include <asm/smtc_ipi.h>
  37. #include <asm/time.h>
  38. #include <irq.h>
  39. /*
  40. * forward reference
  41. */
  42. DEFINE_SPINLOCK(rtc_lock);
  43. EXPORT_SYMBOL(rtc_lock);
  44. int __weak rtc_mips_set_time(unsigned long sec)
  45. {
  46. return 0;
  47. }
  48. EXPORT_SYMBOL(rtc_mips_set_time);
  49. int __weak rtc_mips_set_mmss(unsigned long nowtime)
  50. {
  51. return rtc_mips_set_time(nowtime);
  52. }
  53. int update_persistent_clock(struct timespec now)
  54. {
  55. return rtc_mips_set_mmss(now.tv_sec);
  56. }
  57. /*
  58. * Null high precision timer functions for systems lacking one.
  59. */
  60. static cycle_t null_hpt_read(void)
  61. {
  62. return 0;
  63. }
  64. /*
  65. * High precision timer functions for a R4k-compatible timer.
  66. */
  67. static cycle_t c0_hpt_read(void)
  68. {
  69. return read_c0_count();
  70. }
  71. int (*mips_timer_state)(void);
  72. /*
  73. * local_timer_interrupt() does profiling and process accounting
  74. * on a per-CPU basis.
  75. *
  76. * In UP mode, it is invoked from the (global) timer_interrupt.
  77. *
  78. * In SMP mode, it might invoked by per-CPU timer interrupt, or
  79. * a broadcasted inter-processor interrupt which itself is triggered
  80. * by the global timer interrupt.
  81. */
  82. void local_timer_interrupt(int irq, void *dev_id)
  83. {
  84. profile_tick(CPU_PROFILING);
  85. update_process_times(user_mode(get_irq_regs()));
  86. }
  87. int null_perf_irq(void)
  88. {
  89. return 0;
  90. }
  91. EXPORT_SYMBOL(null_perf_irq);
  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. * 3) plat_timer_setup() -
  104. * a) (optional) over-write any choices made above by time_init().
  105. * b) machine specific code should setup the timer irqaction.
  106. * c) enable the timer interrupt
  107. */
  108. unsigned int mips_hpt_frequency;
  109. static unsigned int __init calibrate_hpt(void)
  110. {
  111. cycle_t frequency, hpt_start, hpt_end, hpt_count, hz;
  112. const int loops = HZ / 10;
  113. int log_2_loops = 0;
  114. int i;
  115. /*
  116. * We want to calibrate for 0.1s, but to avoid a 64-bit
  117. * division we round the number of loops up to the nearest
  118. * power of 2.
  119. */
  120. while (loops > 1 << log_2_loops)
  121. log_2_loops++;
  122. i = 1 << log_2_loops;
  123. /*
  124. * Wait for a rising edge of the timer interrupt.
  125. */
  126. while (mips_timer_state());
  127. while (!mips_timer_state());
  128. /*
  129. * Now see how many high precision timer ticks happen
  130. * during the calculated number of periods between timer
  131. * interrupts.
  132. */
  133. hpt_start = clocksource_mips.read();
  134. do {
  135. while (mips_timer_state());
  136. while (!mips_timer_state());
  137. } while (--i);
  138. hpt_end = clocksource_mips.read();
  139. hpt_count = (hpt_end - hpt_start) & clocksource_mips.mask;
  140. hz = HZ;
  141. frequency = hpt_count * hz;
  142. return frequency >> log_2_loops;
  143. }
  144. struct clocksource clocksource_mips = {
  145. .name = "MIPS",
  146. .mask = CLOCKSOURCE_MASK(32),
  147. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  148. };
  149. void __init clocksource_set_clock(struct clocksource *cs, unsigned int clock)
  150. {
  151. u64 temp;
  152. u32 shift;
  153. /* Find a shift value */
  154. for (shift = 32; shift > 0; shift--) {
  155. temp = (u64) NSEC_PER_SEC << shift;
  156. do_div(temp, clock);
  157. if ((temp >> 32) == 0)
  158. break;
  159. }
  160. cs->shift = shift;
  161. cs->mult = (u32) temp;
  162. }
  163. void __cpuinit clockevent_set_clock(struct clock_event_device *cd,
  164. unsigned int clock)
  165. {
  166. u64 temp;
  167. u32 shift;
  168. /* Find a shift value */
  169. for (shift = 32; shift > 0; shift--) {
  170. temp = (u64) NSEC_PER_SEC << shift;
  171. do_div(temp, clock);
  172. if ((temp >> 32) == 0)
  173. break;
  174. }
  175. cd->shift = shift;
  176. cd->mult = (u32) temp;
  177. }
  178. static void __init init_mips_clocksource(void)
  179. {
  180. if (!mips_hpt_frequency || clocksource_mips.read == null_hpt_read)
  181. return;
  182. /* Calclate a somewhat reasonable rating value */
  183. clocksource_mips.rating = 200 + mips_hpt_frequency / 10000000;
  184. clocksource_set_clock(&clocksource_mips, mips_hpt_frequency);
  185. clocksource_register(&clocksource_mips);
  186. }
  187. void __init __weak plat_time_init(void)
  188. {
  189. }
  190. void __init __weak plat_timer_setup(struct irqaction *irq)
  191. {
  192. }
  193. void __init time_init(void)
  194. {
  195. plat_time_init();
  196. /* Choose appropriate high precision timer routines. */
  197. if (!cpu_has_counter && !clocksource_mips.read)
  198. /* No high precision timer -- sorry. */
  199. clocksource_mips.read = null_hpt_read;
  200. else if (!mips_hpt_frequency && !mips_timer_state) {
  201. /* A high precision timer of unknown frequency. */
  202. if (!clocksource_mips.read)
  203. /* No external high precision timer -- use R4k. */
  204. clocksource_mips.read = c0_hpt_read;
  205. } else {
  206. /* We know counter frequency. Or we can get it. */
  207. if (!clocksource_mips.read) {
  208. /* No external high precision timer -- use R4k. */
  209. clocksource_mips.read = c0_hpt_read;
  210. }
  211. if (!mips_hpt_frequency)
  212. mips_hpt_frequency = calibrate_hpt();
  213. /* Report the high precision timer rate for a reference. */
  214. printk("Using %u.%03u MHz high precision timer.\n",
  215. ((mips_hpt_frequency + 500) / 1000) / 1000,
  216. ((mips_hpt_frequency + 500) / 1000) % 1000);
  217. }
  218. init_mips_clocksource();
  219. mips_clockevent_init();
  220. }