time.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 <asm/irq.h>
  21. #include <asm/div64.h>
  22. #include <asm/processor.h>
  23. #include <asm/intctl-regs.h>
  24. #include <asm/rtc.h>
  25. #include "internal.h"
  26. static unsigned long mn10300_last_tsc; /* time-stamp counter at last time
  27. * interrupt occurred */
  28. static irqreturn_t timer_interrupt(int irq, void *dev_id);
  29. static struct irqaction timer_irq = {
  30. .handler = timer_interrupt,
  31. .flags = IRQF_DISABLED | IRQF_SHARED | IRQF_TIMER,
  32. .name = "timer",
  33. };
  34. static unsigned long sched_clock_multiplier;
  35. /*
  36. * scheduler clock - returns current time in nanosec units.
  37. */
  38. unsigned long long sched_clock(void)
  39. {
  40. union {
  41. unsigned long long ll;
  42. unsigned l[2];
  43. } tsc64, result;
  44. unsigned long tsc, tmp;
  45. unsigned product[3]; /* 96-bit intermediate value */
  46. /* cnt32_to_63() is not safe with preemption */
  47. preempt_disable();
  48. /* read the TSC value
  49. */
  50. tsc = 0 - get_cycles(); /* get_cycles() counts down */
  51. /* expand to 64-bits.
  52. * - sched_clock() must be called once a minute or better or the
  53. * following will go horribly wrong - see cnt32_to_63()
  54. */
  55. tsc64.ll = cnt32_to_63(tsc) & 0x7fffffffffffffffULL;
  56. preempt_enable();
  57. /* scale the 64-bit TSC value to a nanosecond value via a 96-bit
  58. * intermediate
  59. */
  60. asm("mulu %2,%0,%3,%0 \n" /* LSW * mult -> 0:%3:%0 */
  61. "mulu %2,%1,%2,%1 \n" /* MSW * mult -> %2:%1:0 */
  62. "add %3,%1 \n"
  63. "addc 0,%2 \n" /* result in %2:%1:%0 */
  64. : "=r"(product[0]), "=r"(product[1]), "=r"(product[2]), "=r"(tmp)
  65. : "0"(tsc64.l[0]), "1"(tsc64.l[1]), "2"(sched_clock_multiplier)
  66. : "cc");
  67. result.l[0] = product[1] << 16 | product[0] >> 16;
  68. result.l[1] = product[2] << 16 | product[1] >> 16;
  69. return result.ll;
  70. }
  71. /*
  72. * initialise the scheduler clock
  73. */
  74. static void __init mn10300_sched_clock_init(void)
  75. {
  76. sched_clock_multiplier =
  77. __muldiv64u(NSEC_PER_SEC, 1 << 16, MN10300_TSCCLK);
  78. }
  79. /**
  80. * local_timer_interrupt - Local timer interrupt handler
  81. *
  82. * Handle local timer interrupts for this CPU. They may have been propagated
  83. * to this CPU from the CPU that actually gets them by way of an IPI.
  84. */
  85. irqreturn_t local_timer_interrupt(void)
  86. {
  87. profile_tick(CPU_PROFILING);
  88. update_process_times(user_mode(get_irq_regs()));
  89. return IRQ_HANDLED;
  90. }
  91. /*
  92. * advance the kernel's time keeping clocks (xtime and jiffies)
  93. * - we use Timer 0 & 1 cascaded as a clock to nudge us the next time
  94. * there's a need to update
  95. */
  96. static irqreturn_t timer_interrupt(int irq, void *dev_id)
  97. {
  98. unsigned tsc, elapse;
  99. irqreturn_t ret;
  100. write_seqlock(&xtime_lock);
  101. while (tsc = get_cycles(),
  102. elapse = mn10300_last_tsc - tsc, /* time elapsed since last
  103. * tick */
  104. elapse > MN10300_TSC_PER_HZ
  105. ) {
  106. mn10300_last_tsc -= MN10300_TSC_PER_HZ;
  107. /* advance the kernel's time tracking system */
  108. do_timer(1);
  109. }
  110. write_sequnlock(&xtime_lock);
  111. ret = local_timer_interrupt();
  112. #ifdef CONFIG_SMP
  113. send_IPI_allbutself(LOCAL_TIMER_IPI);
  114. #endif
  115. return ret;
  116. }
  117. /*
  118. * initialise the various timers used by the main part of the kernel
  119. */
  120. void __init time_init(void)
  121. {
  122. /* we need the prescalar running to be able to use IOCLK/8
  123. * - IOCLK runs at 1/4 (ST5 open) or 1/8 (ST5 closed) internal CPU clock
  124. * - IOCLK runs at Fosc rate (crystal speed)
  125. */
  126. TMPSCNT |= TMPSCNT_ENABLE;
  127. startup_timestamp_counter();
  128. printk(KERN_INFO
  129. "timestamp counter I/O clock running at %lu.%02lu"
  130. " (calibrated against RTC)\n",
  131. MN10300_TSCCLK / 1000000, (MN10300_TSCCLK / 10000) % 100);
  132. mn10300_last_tsc = TMTSCBC;
  133. /* use timer 0 & 1 cascaded to tick at as close to HZ as possible */
  134. setup_irq(TMJCIRQ, &timer_irq);
  135. set_intr_level(TMJCIRQ, NUM2GxICR_LEVEL(CONFIG_TIMER_IRQ_LEVEL));
  136. startup_jiffies_counter();
  137. #ifdef CONFIG_MN10300_WD_TIMER
  138. /* start the watchdog timer */
  139. watchdog_go();
  140. #endif
  141. mn10300_sched_clock_init();
  142. }