time.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. * The integer part of the number of usecs per jiffy is taken from tick,
  41. * but the fractional part is not recorded, so we calculate it using the
  42. * initial value of HZ. This aids systems where tick isn't really an
  43. * integer (e.g. for HZ = 128).
  44. */
  45. #define USECS_PER_JIFFY TICK_SIZE
  46. #define USECS_PER_JIFFY_FRAC ((unsigned long)(u32)((1000000ULL << 32) / HZ))
  47. #define TICK_SIZE (tick_nsec / 1000)
  48. /*
  49. * forward reference
  50. */
  51. DEFINE_SPINLOCK(rtc_lock);
  52. EXPORT_SYMBOL(rtc_lock);
  53. int __weak rtc_mips_set_time(unsigned long sec)
  54. {
  55. return 0;
  56. }
  57. EXPORT_SYMBOL(rtc_mips_set_time);
  58. int __weak rtc_mips_set_mmss(unsigned long nowtime)
  59. {
  60. return rtc_mips_set_time(nowtime);
  61. }
  62. int update_persistent_clock(struct timespec now)
  63. {
  64. return rtc_mips_set_mmss(now.tv_sec);
  65. }
  66. /*
  67. * Null high precision timer functions for systems lacking one.
  68. */
  69. static cycle_t null_hpt_read(void)
  70. {
  71. return 0;
  72. }
  73. /*
  74. * High precision timer functions for a R4k-compatible timer.
  75. */
  76. static cycle_t c0_hpt_read(void)
  77. {
  78. return read_c0_count();
  79. }
  80. int (*mips_timer_state)(void);
  81. /*
  82. * local_timer_interrupt() does profiling and process accounting
  83. * on a per-CPU basis.
  84. *
  85. * In UP mode, it is invoked from the (global) timer_interrupt.
  86. *
  87. * In SMP mode, it might invoked by per-CPU timer interrupt, or
  88. * a broadcasted inter-processor interrupt which itself is triggered
  89. * by the global timer interrupt.
  90. */
  91. void local_timer_interrupt(int irq, void *dev_id)
  92. {
  93. profile_tick(CPU_PROFILING);
  94. update_process_times(user_mode(get_irq_regs()));
  95. }
  96. int null_perf_irq(void)
  97. {
  98. return 0;
  99. }
  100. EXPORT_SYMBOL(null_perf_irq);
  101. int (*perf_irq)(void) = null_perf_irq;
  102. EXPORT_SYMBOL(perf_irq);
  103. /*
  104. * time_init() - it does the following things.
  105. *
  106. * 1) plat_time_init() -
  107. * a) (optional) set up RTC routines,
  108. * b) (optional) calibrate and set the mips_hpt_frequency
  109. * (only needed if you intended to use cpu counter as timer interrupt
  110. * source)
  111. * 2) calculate a couple of cached variables for later usage
  112. * 3) plat_timer_setup() -
  113. * a) (optional) over-write any choices made above by time_init().
  114. * b) machine specific code should setup the timer irqaction.
  115. * c) enable the timer interrupt
  116. */
  117. unsigned int mips_hpt_frequency;
  118. static unsigned int __init calibrate_hpt(void)
  119. {
  120. cycle_t frequency, hpt_start, hpt_end, hpt_count, hz;
  121. const int loops = HZ / 10;
  122. int log_2_loops = 0;
  123. int i;
  124. /*
  125. * We want to calibrate for 0.1s, but to avoid a 64-bit
  126. * division we round the number of loops up to the nearest
  127. * power of 2.
  128. */
  129. while (loops > 1 << log_2_loops)
  130. log_2_loops++;
  131. i = 1 << log_2_loops;
  132. /*
  133. * Wait for a rising edge of the timer interrupt.
  134. */
  135. while (mips_timer_state());
  136. while (!mips_timer_state());
  137. /*
  138. * Now see how many high precision timer ticks happen
  139. * during the calculated number of periods between timer
  140. * interrupts.
  141. */
  142. hpt_start = clocksource_mips.read();
  143. do {
  144. while (mips_timer_state());
  145. while (!mips_timer_state());
  146. } while (--i);
  147. hpt_end = clocksource_mips.read();
  148. hpt_count = (hpt_end - hpt_start) & clocksource_mips.mask;
  149. hz = HZ;
  150. frequency = hpt_count * hz;
  151. return frequency >> log_2_loops;
  152. }
  153. struct clocksource clocksource_mips = {
  154. .name = "MIPS",
  155. .mask = CLOCKSOURCE_MASK(32),
  156. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  157. };
  158. static void __init init_mips_clocksource(void)
  159. {
  160. u64 temp;
  161. u32 shift;
  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. /* Find a shift value */
  167. for (shift = 32; shift > 0; shift--) {
  168. temp = (u64) NSEC_PER_SEC << shift;
  169. do_div(temp, mips_hpt_frequency);
  170. if ((temp >> 32) == 0)
  171. break;
  172. }
  173. clocksource_mips.shift = shift;
  174. clocksource_mips.mult = (u32)temp;
  175. clocksource_register(&clocksource_mips);
  176. }
  177. void __init __weak plat_time_init(void)
  178. {
  179. }
  180. void __init __weak plat_timer_setup(struct irqaction *irq)
  181. {
  182. }
  183. #ifdef CONFIG_MIPS_MT_SMTC
  184. DEFINE_PER_CPU(struct clock_event_device, smtc_dummy_clockevent_device);
  185. static void smtc_set_mode(enum clock_event_mode mode,
  186. struct clock_event_device *evt)
  187. {
  188. }
  189. static void mips_broadcast(cpumask_t mask)
  190. {
  191. unsigned int cpu;
  192. for_each_cpu_mask(cpu, mask)
  193. smtc_send_ipi(cpu, SMTC_CLOCK_TICK, 0);
  194. }
  195. static void setup_smtc_dummy_clockevent_device(void)
  196. {
  197. //uint64_t mips_freq = mips_hpt_^frequency;
  198. unsigned int cpu = smp_processor_id();
  199. struct clock_event_device *cd;
  200. cd = &per_cpu(smtc_dummy_clockevent_device, cpu);
  201. cd->name = "SMTC";
  202. cd->features = CLOCK_EVT_FEAT_DUMMY;
  203. /* Calculate the min / max delta */
  204. cd->mult = 0; //div_sc((unsigned long) mips_freq, NSEC_PER_SEC, 32);
  205. cd->shift = 0; //32;
  206. cd->max_delta_ns = 0; //clockevent_delta2ns(0x7fffffff, cd);
  207. cd->min_delta_ns = 0; //clockevent_delta2ns(0x30, cd);
  208. cd->rating = 200;
  209. cd->irq = 17; //-1;
  210. // if (cpu)
  211. // cd->cpumask = CPU_MASK_ALL; // cpumask_of_cpu(cpu);
  212. // else
  213. cd->cpumask = cpumask_of_cpu(cpu);
  214. cd->set_mode = smtc_set_mode;
  215. cd->broadcast = mips_broadcast;
  216. clockevents_register_device(cd);
  217. }
  218. #endif
  219. void __init time_init(void)
  220. {
  221. plat_time_init();
  222. /* Choose appropriate high precision timer routines. */
  223. if (!cpu_has_counter && !clocksource_mips.read)
  224. /* No high precision timer -- sorry. */
  225. clocksource_mips.read = null_hpt_read;
  226. else if (!mips_hpt_frequency && !mips_timer_state) {
  227. /* A high precision timer of unknown frequency. */
  228. if (!clocksource_mips.read)
  229. /* No external high precision timer -- use R4k. */
  230. clocksource_mips.read = c0_hpt_read;
  231. } else {
  232. /* We know counter frequency. Or we can get it. */
  233. if (!clocksource_mips.read) {
  234. /* No external high precision timer -- use R4k. */
  235. clocksource_mips.read = c0_hpt_read;
  236. }
  237. if (!mips_hpt_frequency)
  238. mips_hpt_frequency = calibrate_hpt();
  239. /* Report the high precision timer rate for a reference. */
  240. printk("Using %u.%03u MHz high precision timer.\n",
  241. ((mips_hpt_frequency + 500) / 1000) / 1000,
  242. ((mips_hpt_frequency + 500) / 1000) % 1000);
  243. }
  244. init_mips_clocksource();
  245. mips_clockevent_init();
  246. }