time.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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/bug.h>
  15. #include <linux/clockchips.h>
  16. #include <linux/types.h>
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/sched.h>
  20. #include <linux/param.h>
  21. #include <linux/profile.h>
  22. #include <linux/time.h>
  23. #include <linux/timex.h>
  24. #include <linux/smp.h>
  25. #include <linux/kernel_stat.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/module.h>
  29. #include <linux/kallsyms.h>
  30. #include <asm/bootinfo.h>
  31. #include <asm/cache.h>
  32. #include <asm/compiler.h>
  33. #include <asm/cpu.h>
  34. #include <asm/cpu-features.h>
  35. #include <asm/div64.h>
  36. #include <asm/sections.h>
  37. #include <asm/smtc_ipi.h>
  38. #include <asm/time.h>
  39. #include <irq.h>
  40. /*
  41. * forward reference
  42. */
  43. DEFINE_SPINLOCK(rtc_lock);
  44. EXPORT_SYMBOL(rtc_lock);
  45. int __weak rtc_mips_set_time(unsigned long sec)
  46. {
  47. return 0;
  48. }
  49. EXPORT_SYMBOL(rtc_mips_set_time);
  50. int __weak rtc_mips_set_mmss(unsigned long nowtime)
  51. {
  52. return rtc_mips_set_time(nowtime);
  53. }
  54. int update_persistent_clock(struct timespec now)
  55. {
  56. return rtc_mips_set_mmss(now.tv_sec);
  57. }
  58. /*
  59. * Null high precision timer functions for systems lacking one.
  60. */
  61. static cycle_t null_hpt_read(void)
  62. {
  63. return 0;
  64. }
  65. /*
  66. * High precision timer functions for a R4k-compatible timer.
  67. */
  68. static cycle_t c0_hpt_read(void)
  69. {
  70. return read_c0_count();
  71. }
  72. int (*mips_timer_state)(void);
  73. /*
  74. * local_timer_interrupt() does profiling and process accounting
  75. * on a per-CPU basis.
  76. *
  77. * In UP mode, it is invoked from the (global) timer_interrupt.
  78. *
  79. * In SMP mode, it might invoked by per-CPU timer interrupt, or
  80. * a broadcasted inter-processor interrupt which itself is triggered
  81. * by the global timer interrupt.
  82. */
  83. void local_timer_interrupt(int irq, void *dev_id)
  84. {
  85. profile_tick(CPU_PROFILING);
  86. update_process_times(user_mode(get_irq_regs()));
  87. }
  88. int null_perf_irq(void)
  89. {
  90. return 0;
  91. }
  92. EXPORT_SYMBOL(null_perf_irq);
  93. int (*perf_irq)(void) = null_perf_irq;
  94. EXPORT_SYMBOL(perf_irq);
  95. /*
  96. * time_init() - it does the following things.
  97. *
  98. * 1) plat_time_init() -
  99. * a) (optional) set up RTC routines,
  100. * b) (optional) calibrate and set the mips_hpt_frequency
  101. * (only needed if you intended to use cpu counter as timer interrupt
  102. * source)
  103. * 2) calculate a couple of cached variables for later usage
  104. */
  105. unsigned int mips_hpt_frequency;
  106. static unsigned int __init calibrate_hpt(void)
  107. {
  108. cycle_t frequency, hpt_start, hpt_end, hpt_count, hz;
  109. const int loops = HZ / 10;
  110. int log_2_loops = 0;
  111. int i;
  112. /*
  113. * We want to calibrate for 0.1s, but to avoid a 64-bit
  114. * division we round the number of loops up to the nearest
  115. * power of 2.
  116. */
  117. while (loops > 1 << log_2_loops)
  118. log_2_loops++;
  119. i = 1 << log_2_loops;
  120. /*
  121. * Wait for a rising edge of the timer interrupt.
  122. */
  123. while (mips_timer_state());
  124. while (!mips_timer_state());
  125. /*
  126. * Now see how many high precision timer ticks happen
  127. * during the calculated number of periods between timer
  128. * interrupts.
  129. */
  130. hpt_start = clocksource_mips.read();
  131. do {
  132. while (mips_timer_state());
  133. while (!mips_timer_state());
  134. } while (--i);
  135. hpt_end = clocksource_mips.read();
  136. hpt_count = (hpt_end - hpt_start) & clocksource_mips.mask;
  137. hz = HZ;
  138. frequency = hpt_count * hz;
  139. return frequency >> log_2_loops;
  140. }
  141. struct clocksource clocksource_mips = {
  142. .name = "MIPS",
  143. .mask = CLOCKSOURCE_MASK(32),
  144. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  145. };
  146. void __init clocksource_set_clock(struct clocksource *cs, unsigned int clock)
  147. {
  148. u64 temp;
  149. u32 shift;
  150. /* Find a shift value */
  151. for (shift = 32; shift > 0; shift--) {
  152. temp = (u64) NSEC_PER_SEC << shift;
  153. do_div(temp, clock);
  154. if ((temp >> 32) == 0)
  155. break;
  156. }
  157. cs->shift = shift;
  158. cs->mult = (u32) temp;
  159. }
  160. void __cpuinit clockevent_set_clock(struct clock_event_device *cd,
  161. unsigned int clock)
  162. {
  163. u64 temp;
  164. u32 shift;
  165. /* Find a shift value */
  166. for (shift = 32; shift > 0; shift--) {
  167. temp = (u64) clock << shift;
  168. do_div(temp, NSEC_PER_SEC);
  169. if ((temp >> 32) == 0)
  170. break;
  171. }
  172. cd->shift = shift;
  173. cd->mult = (u32) temp;
  174. }
  175. static void __init init_mips_clocksource(void)
  176. {
  177. if (!mips_hpt_frequency || clocksource_mips.read == null_hpt_read)
  178. return;
  179. /* Calclate a somewhat reasonable rating value */
  180. clocksource_mips.rating = 200 + mips_hpt_frequency / 10000000;
  181. clocksource_set_clock(&clocksource_mips, mips_hpt_frequency);
  182. clocksource_register(&clocksource_mips);
  183. }
  184. void __init __weak plat_time_init(void)
  185. {
  186. }
  187. /*
  188. * This function exists in order to cause an error due to a duplicate
  189. * definition if platform code should have its own implementation. The hook
  190. * to use instead is plat_time_init. plat_time_init does not receive the
  191. * irqaction pointer argument anymore. This is because any function which
  192. * initializes an interrupt timer now takes care of its own request_irq rsp.
  193. * setup_irq calls and each clock_event_device should use its own
  194. * struct irqrequest.
  195. */
  196. void __init plat_timer_setup(struct irqaction *irq)
  197. {
  198. BUG();
  199. }
  200. void __init time_init(void)
  201. {
  202. plat_time_init();
  203. /* Choose appropriate high precision timer routines. */
  204. if (!cpu_has_counter && !clocksource_mips.read)
  205. /* No high precision timer -- sorry. */
  206. clocksource_mips.read = null_hpt_read;
  207. else if (!mips_hpt_frequency && !mips_timer_state) {
  208. /* A high precision timer of unknown frequency. */
  209. if (!clocksource_mips.read)
  210. /* No external high precision timer -- use R4k. */
  211. clocksource_mips.read = c0_hpt_read;
  212. } else {
  213. /* We know counter frequency. Or we can get it. */
  214. if (!clocksource_mips.read) {
  215. /* No external high precision timer -- use R4k. */
  216. clocksource_mips.read = c0_hpt_read;
  217. }
  218. if (!mips_hpt_frequency)
  219. mips_hpt_frequency = calibrate_hpt();
  220. /* Report the high precision timer rate for a reference. */
  221. printk("Using %u.%03u MHz high precision timer.\n",
  222. ((mips_hpt_frequency + 500) / 1000) / 1000,
  223. ((mips_hpt_frequency + 500) / 1000) % 1000);
  224. }
  225. init_mips_clocksource();
  226. mips_clockevent_init();
  227. }