time.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* MN10300 Low level time management
  2. *
  3. * Copyright (C) 2007-2008 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. * - Derived from arch/i386/kernel/time.c
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public Licence
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the Licence, or (at your option) any later version.
  11. */
  12. #include <linux/sched.h>
  13. #include <linux/kernel.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/time.h>
  16. #include <linux/init.h>
  17. #include <linux/smp.h>
  18. #include <linux/profile.h>
  19. #include <linux/cnt32_to_63.h>
  20. #include <linux/clocksource.h>
  21. #include <linux/clockchips.h>
  22. #include <asm/irq.h>
  23. #include <asm/div64.h>
  24. #include <asm/processor.h>
  25. #include <asm/intctl-regs.h>
  26. #include <asm/rtc.h>
  27. #include "internal.h"
  28. static unsigned long mn10300_last_tsc; /* time-stamp counter at last time
  29. * interrupt occurred */
  30. static unsigned long sched_clock_multiplier;
  31. /*
  32. * scheduler clock - returns current time in nanosec units.
  33. */
  34. unsigned long long sched_clock(void)
  35. {
  36. union {
  37. unsigned long long ll;
  38. unsigned l[2];
  39. } tsc64, result;
  40. unsigned long tsc, tmp;
  41. unsigned product[3]; /* 96-bit intermediate value */
  42. /* cnt32_to_63() is not safe with preemption */
  43. preempt_disable();
  44. /* read the TSC value
  45. */
  46. tsc = get_cycles();
  47. /* expand to 64-bits.
  48. * - sched_clock() must be called once a minute or better or the
  49. * following will go horribly wrong - see cnt32_to_63()
  50. */
  51. tsc64.ll = cnt32_to_63(tsc) & 0x7fffffffffffffffULL;
  52. preempt_enable();
  53. /* scale the 64-bit TSC value to a nanosecond value via a 96-bit
  54. * intermediate
  55. */
  56. asm("mulu %2,%0,%3,%0 \n" /* LSW * mult -> 0:%3:%0 */
  57. "mulu %2,%1,%2,%1 \n" /* MSW * mult -> %2:%1:0 */
  58. "add %3,%1 \n"
  59. "addc 0,%2 \n" /* result in %2:%1:%0 */
  60. : "=r"(product[0]), "=r"(product[1]), "=r"(product[2]), "=r"(tmp)
  61. : "0"(tsc64.l[0]), "1"(tsc64.l[1]), "2"(sched_clock_multiplier)
  62. : "cc");
  63. result.l[0] = product[1] << 16 | product[0] >> 16;
  64. result.l[1] = product[2] << 16 | product[1] >> 16;
  65. return result.ll;
  66. }
  67. /*
  68. * initialise the scheduler clock
  69. */
  70. static void __init mn10300_sched_clock_init(void)
  71. {
  72. sched_clock_multiplier =
  73. __muldiv64u(NSEC_PER_SEC, 1 << 16, MN10300_TSCCLK);
  74. }
  75. /**
  76. * local_timer_interrupt - Local timer interrupt handler
  77. *
  78. * Handle local timer interrupts for this CPU. They may have been propagated
  79. * to this CPU from the CPU that actually gets them by way of an IPI.
  80. */
  81. irqreturn_t local_timer_interrupt(void)
  82. {
  83. profile_tick(CPU_PROFILING);
  84. update_process_times(user_mode(get_irq_regs()));
  85. return IRQ_HANDLED;
  86. }
  87. #ifndef CONFIG_GENERIC_TIME
  88. /*
  89. * advance the kernel's time keeping clocks (xtime and jiffies)
  90. * - we use Timer 0 & 1 cascaded as a clock to nudge us the next time
  91. * there's a need to update
  92. */
  93. static irqreturn_t timer_interrupt(int irq, void *dev_id)
  94. {
  95. unsigned tsc, elapse;
  96. irqreturn_t ret;
  97. write_seqlock(&xtime_lock);
  98. while (tsc = get_cycles(),
  99. elapse = tsc - mn10300_last_tsc, /* time elapsed since last
  100. * tick */
  101. elapse > MN10300_TSC_PER_HZ
  102. ) {
  103. mn10300_last_tsc += MN10300_TSC_PER_HZ;
  104. /* advance the kernel's time tracking system */
  105. do_timer(1);
  106. }
  107. write_sequnlock(&xtime_lock);
  108. ret = local_timer_interrupt();
  109. #ifdef CONFIG_SMP
  110. send_IPI_allbutself(LOCAL_TIMER_IPI);
  111. #endif
  112. return ret;
  113. }
  114. static struct irqaction timer_irq = {
  115. .handler = timer_interrupt,
  116. .flags = IRQF_DISABLED | IRQF_SHARED | IRQF_TIMER,
  117. .name = "timer",
  118. };
  119. #endif /* CONFIG_GENERIC_TIME */
  120. #ifdef CONFIG_CSRC_MN10300
  121. void __init clocksource_set_clock(struct clocksource *cs, unsigned int clock)
  122. {
  123. u64 temp;
  124. u32 shift;
  125. /* Find a shift value */
  126. for (shift = 32; shift > 0; shift--) {
  127. temp = (u64) NSEC_PER_SEC << shift;
  128. do_div(temp, clock);
  129. if ((temp >> 32) == 0)
  130. break;
  131. }
  132. cs->shift = shift;
  133. cs->mult = (u32) temp;
  134. }
  135. #endif
  136. #if CONFIG_CEVT_MN10300
  137. void __cpuinit clockevent_set_clock(struct clock_event_device *cd,
  138. unsigned int clock)
  139. {
  140. u64 temp;
  141. u32 shift;
  142. /* Find a shift value */
  143. for (shift = 32; shift > 0; shift--) {
  144. temp = (u64) clock << shift;
  145. do_div(temp, NSEC_PER_SEC);
  146. if ((temp >> 32) == 0)
  147. break;
  148. }
  149. cd->shift = shift;
  150. cd->mult = (u32) temp;
  151. }
  152. #endif
  153. /*
  154. * initialise the various timers used by the main part of the kernel
  155. */
  156. void __init time_init(void)
  157. {
  158. /* we need the prescalar running to be able to use IOCLK/8
  159. * - IOCLK runs at 1/4 (ST5 open) or 1/8 (ST5 closed) internal CPU clock
  160. * - IOCLK runs at Fosc rate (crystal speed)
  161. */
  162. TMPSCNT |= TMPSCNT_ENABLE;
  163. #ifdef CONFIG_GENERIC_TIME
  164. init_clocksource();
  165. #else
  166. startup_timestamp_counter();
  167. #endif
  168. printk(KERN_INFO
  169. "timestamp counter I/O clock running at %lu.%02lu"
  170. " (calibrated against RTC)\n",
  171. MN10300_TSCCLK / 1000000, (MN10300_TSCCLK / 10000) % 100);
  172. mn10300_last_tsc = read_timestamp_counter();
  173. #ifdef CONFIG_GENERIC_CLOCKEVENTS
  174. init_clockevents();
  175. #else
  176. reload_jiffies_counter(MN10300_JC_PER_HZ - 1);
  177. setup_jiffies_interrupt(TMJCIRQ, &timer_irq, CONFIG_TIMER_IRQ_LEVEL);
  178. #endif
  179. #ifdef CONFIG_MN10300_WD_TIMER
  180. /* start the watchdog timer */
  181. watchdog_go();
  182. #endif
  183. mn10300_sched_clock_init();
  184. }