time.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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/module.h>
  25. #include <asm/cpu-features.h>
  26. #include <asm/div64.h>
  27. #include <asm/smtc_ipi.h>
  28. #include <asm/time.h>
  29. /*
  30. * forward reference
  31. */
  32. DEFINE_SPINLOCK(rtc_lock);
  33. EXPORT_SYMBOL(rtc_lock);
  34. int __weak rtc_mips_set_time(unsigned long sec)
  35. {
  36. return 0;
  37. }
  38. EXPORT_SYMBOL(rtc_mips_set_time);
  39. int __weak rtc_mips_set_mmss(unsigned long nowtime)
  40. {
  41. return rtc_mips_set_time(nowtime);
  42. }
  43. int update_persistent_clock(struct timespec now)
  44. {
  45. return rtc_mips_set_mmss(now.tv_sec);
  46. }
  47. /*
  48. * High precision timer functions for a R4k-compatible timer.
  49. */
  50. static cycle_t c0_hpt_read(void)
  51. {
  52. return read_c0_count();
  53. }
  54. int (*mips_timer_state)(void);
  55. int null_perf_irq(void)
  56. {
  57. return 0;
  58. }
  59. EXPORT_SYMBOL(null_perf_irq);
  60. int (*perf_irq)(void) = null_perf_irq;
  61. EXPORT_SYMBOL(perf_irq);
  62. /*
  63. * time_init() - it does the following things.
  64. *
  65. * 1) plat_time_init() -
  66. * a) (optional) set up RTC routines,
  67. * b) (optional) calibrate and set the mips_hpt_frequency
  68. * (only needed if you intended to use cpu counter as timer interrupt
  69. * source)
  70. * 2) calculate a couple of cached variables for later usage
  71. */
  72. unsigned int mips_hpt_frequency;
  73. static struct clocksource clocksource_mips = {
  74. .name = "MIPS",
  75. .read = c0_hpt_read,
  76. .mask = CLOCKSOURCE_MASK(32),
  77. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  78. };
  79. static unsigned int __init calibrate_hpt(void)
  80. {
  81. cycle_t frequency, hpt_start, hpt_end, hpt_count, hz;
  82. const int loops = HZ / 10;
  83. int log_2_loops = 0;
  84. int i;
  85. /*
  86. * We want to calibrate for 0.1s, but to avoid a 64-bit
  87. * division we round the number of loops up to the nearest
  88. * power of 2.
  89. */
  90. while (loops > 1 << log_2_loops)
  91. log_2_loops++;
  92. i = 1 << log_2_loops;
  93. /*
  94. * Wait for a rising edge of the timer interrupt.
  95. */
  96. while (mips_timer_state());
  97. while (!mips_timer_state());
  98. /*
  99. * Now see how many high precision timer ticks happen
  100. * during the calculated number of periods between timer
  101. * interrupts.
  102. */
  103. hpt_start = clocksource_mips.read();
  104. do {
  105. while (mips_timer_state());
  106. while (!mips_timer_state());
  107. } while (--i);
  108. hpt_end = clocksource_mips.read();
  109. hpt_count = (hpt_end - hpt_start) & clocksource_mips.mask;
  110. hz = HZ;
  111. frequency = hpt_count * hz;
  112. return frequency >> log_2_loops;
  113. }
  114. void __init clocksource_set_clock(struct clocksource *cs, unsigned int clock)
  115. {
  116. u64 temp;
  117. u32 shift;
  118. /* Find a shift value */
  119. for (shift = 32; shift > 0; shift--) {
  120. temp = (u64) NSEC_PER_SEC << shift;
  121. do_div(temp, clock);
  122. if ((temp >> 32) == 0)
  123. break;
  124. }
  125. cs->shift = shift;
  126. cs->mult = (u32) temp;
  127. }
  128. void __cpuinit clockevent_set_clock(struct clock_event_device *cd,
  129. unsigned int clock)
  130. {
  131. u64 temp;
  132. u32 shift;
  133. /* Find a shift value */
  134. for (shift = 32; shift > 0; shift--) {
  135. temp = (u64) clock << shift;
  136. do_div(temp, NSEC_PER_SEC);
  137. if ((temp >> 32) == 0)
  138. break;
  139. }
  140. cd->shift = shift;
  141. cd->mult = (u32) temp;
  142. }
  143. static void __init init_mips_clocksource(void)
  144. {
  145. /* Calclate a somewhat reasonable rating value */
  146. clocksource_mips.rating = 200 + mips_hpt_frequency / 10000000;
  147. clocksource_set_clock(&clocksource_mips, mips_hpt_frequency);
  148. clocksource_register(&clocksource_mips);
  149. }
  150. void __init __weak plat_time_init(void)
  151. {
  152. }
  153. /*
  154. * This function exists in order to cause an error due to a duplicate
  155. * definition if platform code should have its own implementation. The hook
  156. * to use instead is plat_time_init. plat_time_init does not receive the
  157. * irqaction pointer argument anymore. This is because any function which
  158. * initializes an interrupt timer now takes care of its own request_irq rsp.
  159. * setup_irq calls and each clock_event_device should use its own
  160. * struct irqrequest.
  161. */
  162. void __init plat_timer_setup(void)
  163. {
  164. BUG();
  165. }
  166. void __init time_init(void)
  167. {
  168. plat_time_init();
  169. if (cpu_has_counter && (mips_hpt_frequency || mips_timer_state)) {
  170. /* We know counter frequency. Or we can get it. */
  171. if (!mips_hpt_frequency)
  172. mips_hpt_frequency = calibrate_hpt();
  173. /* Report the high precision timer rate for a reference. */
  174. printk("Using %u.%03u MHz high precision timer.\n",
  175. ((mips_hpt_frequency + 500) / 1000) / 1000,
  176. ((mips_hpt_frequency + 500) / 1000) % 1000);
  177. init_mips_clocksource();
  178. }
  179. mips_clockevent_init();
  180. }