time.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. int null_perf_irq(void)
  74. {
  75. return 0;
  76. }
  77. EXPORT_SYMBOL(null_perf_irq);
  78. int (*perf_irq)(void) = null_perf_irq;
  79. EXPORT_SYMBOL(perf_irq);
  80. /*
  81. * time_init() - it does the following things.
  82. *
  83. * 1) plat_time_init() -
  84. * a) (optional) set up RTC routines,
  85. * b) (optional) calibrate and set the mips_hpt_frequency
  86. * (only needed if you intended to use cpu counter as timer interrupt
  87. * source)
  88. * 2) calculate a couple of cached variables for later usage
  89. */
  90. unsigned int mips_hpt_frequency;
  91. static unsigned int __init calibrate_hpt(void)
  92. {
  93. cycle_t frequency, hpt_start, hpt_end, hpt_count, hz;
  94. const int loops = HZ / 10;
  95. int log_2_loops = 0;
  96. int i;
  97. /*
  98. * We want to calibrate for 0.1s, but to avoid a 64-bit
  99. * division we round the number of loops up to the nearest
  100. * power of 2.
  101. */
  102. while (loops > 1 << log_2_loops)
  103. log_2_loops++;
  104. i = 1 << log_2_loops;
  105. /*
  106. * Wait for a rising edge of the timer interrupt.
  107. */
  108. while (mips_timer_state());
  109. while (!mips_timer_state());
  110. /*
  111. * Now see how many high precision timer ticks happen
  112. * during the calculated number of periods between timer
  113. * interrupts.
  114. */
  115. hpt_start = clocksource_mips.read();
  116. do {
  117. while (mips_timer_state());
  118. while (!mips_timer_state());
  119. } while (--i);
  120. hpt_end = clocksource_mips.read();
  121. hpt_count = (hpt_end - hpt_start) & clocksource_mips.mask;
  122. hz = HZ;
  123. frequency = hpt_count * hz;
  124. return frequency >> log_2_loops;
  125. }
  126. struct clocksource clocksource_mips = {
  127. .name = "MIPS",
  128. .mask = CLOCKSOURCE_MASK(32),
  129. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  130. };
  131. void __init clocksource_set_clock(struct clocksource *cs, unsigned int clock)
  132. {
  133. u64 temp;
  134. u32 shift;
  135. /* Find a shift value */
  136. for (shift = 32; shift > 0; shift--) {
  137. temp = (u64) NSEC_PER_SEC << shift;
  138. do_div(temp, clock);
  139. if ((temp >> 32) == 0)
  140. break;
  141. }
  142. cs->shift = shift;
  143. cs->mult = (u32) temp;
  144. }
  145. void __cpuinit clockevent_set_clock(struct clock_event_device *cd,
  146. 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) clock << shift;
  153. do_div(temp, NSEC_PER_SEC);
  154. if ((temp >> 32) == 0)
  155. break;
  156. }
  157. cd->shift = shift;
  158. cd->mult = (u32) temp;
  159. }
  160. static void __init init_mips_clocksource(void)
  161. {
  162. if (!mips_hpt_frequency || clocksource_mips.read == null_hpt_read)
  163. return;
  164. /* Calclate a somewhat reasonable rating value */
  165. clocksource_mips.rating = 200 + mips_hpt_frequency / 10000000;
  166. clocksource_set_clock(&clocksource_mips, mips_hpt_frequency);
  167. clocksource_register(&clocksource_mips);
  168. }
  169. void __init __weak plat_time_init(void)
  170. {
  171. }
  172. /*
  173. * This function exists in order to cause an error due to a duplicate
  174. * definition if platform code should have its own implementation. The hook
  175. * to use instead is plat_time_init. plat_time_init does not receive the
  176. * irqaction pointer argument anymore. This is because any function which
  177. * initializes an interrupt timer now takes care of its own request_irq rsp.
  178. * setup_irq calls and each clock_event_device should use its own
  179. * struct irqrequest.
  180. */
  181. void __init plat_timer_setup(struct irqaction *irq)
  182. {
  183. BUG();
  184. }
  185. void __init time_init(void)
  186. {
  187. plat_time_init();
  188. /* Choose appropriate high precision timer routines. */
  189. if (!cpu_has_counter && !clocksource_mips.read)
  190. /* No high precision timer -- sorry. */
  191. clocksource_mips.read = null_hpt_read;
  192. else if (!mips_hpt_frequency && !mips_timer_state) {
  193. /* A high precision timer of unknown frequency. */
  194. if (!clocksource_mips.read)
  195. /* No external high precision timer -- use R4k. */
  196. clocksource_mips.read = c0_hpt_read;
  197. } else {
  198. /* We know counter frequency. Or we can get it. */
  199. if (!clocksource_mips.read) {
  200. /* No external high precision timer -- use R4k. */
  201. clocksource_mips.read = c0_hpt_read;
  202. }
  203. if (!mips_hpt_frequency)
  204. mips_hpt_frequency = calibrate_hpt();
  205. /* Report the high precision timer rate for a reference. */
  206. printk("Using %u.%03u MHz high precision timer.\n",
  207. ((mips_hpt_frequency + 500) / 1000) / 1000,
  208. ((mips_hpt_frequency + 500) / 1000) % 1000);
  209. }
  210. init_mips_clocksource();
  211. mips_clockevent_init();
  212. }