time.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * arch/xtensa/kernel/time.c
  3. *
  4. * Timer and clock support.
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. *
  10. * Copyright (C) 2005 Tensilica Inc.
  11. *
  12. * Chris Zankel <chris@zankel.net>
  13. */
  14. #include <linux/errno.h>
  15. #include <linux/sched.h>
  16. #include <linux/time.h>
  17. #include <linux/clocksource.h>
  18. #include <linux/clockchips.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/irq.h>
  23. #include <linux/profile.h>
  24. #include <linux/delay.h>
  25. #include <linux/irqdomain.h>
  26. #include <linux/sched_clock.h>
  27. #include <asm/timex.h>
  28. #include <asm/platform.h>
  29. unsigned long ccount_freq; /* ccount Hz */
  30. EXPORT_SYMBOL(ccount_freq);
  31. static cycle_t ccount_read(struct clocksource *cs)
  32. {
  33. return (cycle_t)get_ccount();
  34. }
  35. static u64 notrace ccount_sched_clock_read(void)
  36. {
  37. return get_ccount();
  38. }
  39. static struct clocksource ccount_clocksource = {
  40. .name = "ccount",
  41. .rating = 200,
  42. .read = ccount_read,
  43. .mask = CLOCKSOURCE_MASK(32),
  44. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  45. };
  46. static int ccount_timer_set_next_event(unsigned long delta,
  47. struct clock_event_device *dev);
  48. static void ccount_timer_set_mode(enum clock_event_mode mode,
  49. struct clock_event_device *evt);
  50. struct ccount_timer {
  51. struct clock_event_device evt;
  52. int irq_enabled;
  53. char name[24];
  54. };
  55. static DEFINE_PER_CPU(struct ccount_timer, ccount_timer);
  56. static int ccount_timer_set_next_event(unsigned long delta,
  57. struct clock_event_device *dev)
  58. {
  59. unsigned long flags, next;
  60. int ret = 0;
  61. local_irq_save(flags);
  62. next = get_ccount() + delta;
  63. set_linux_timer(next);
  64. if (next - get_ccount() > delta)
  65. ret = -ETIME;
  66. local_irq_restore(flags);
  67. return ret;
  68. }
  69. static void ccount_timer_set_mode(enum clock_event_mode mode,
  70. struct clock_event_device *evt)
  71. {
  72. struct ccount_timer *timer =
  73. container_of(evt, struct ccount_timer, evt);
  74. /*
  75. * There is no way to disable the timer interrupt at the device level,
  76. * only at the intenable register itself. Since enable_irq/disable_irq
  77. * calls are nested, we need to make sure that these calls are
  78. * balanced.
  79. */
  80. switch (mode) {
  81. case CLOCK_EVT_MODE_SHUTDOWN:
  82. case CLOCK_EVT_MODE_UNUSED:
  83. if (timer->irq_enabled) {
  84. disable_irq(evt->irq);
  85. timer->irq_enabled = 0;
  86. }
  87. break;
  88. case CLOCK_EVT_MODE_RESUME:
  89. case CLOCK_EVT_MODE_ONESHOT:
  90. if (!timer->irq_enabled) {
  91. enable_irq(evt->irq);
  92. timer->irq_enabled = 1;
  93. }
  94. default:
  95. break;
  96. }
  97. }
  98. static irqreturn_t timer_interrupt(int irq, void *dev_id);
  99. static struct irqaction timer_irqaction = {
  100. .handler = timer_interrupt,
  101. .flags = IRQF_TIMER,
  102. .name = "timer",
  103. };
  104. void local_timer_setup(unsigned cpu)
  105. {
  106. struct ccount_timer *timer = &per_cpu(ccount_timer, cpu);
  107. struct clock_event_device *clockevent = &timer->evt;
  108. timer->irq_enabled = 1;
  109. clockevent->name = timer->name;
  110. snprintf(timer->name, sizeof(timer->name), "ccount_clockevent_%u", cpu);
  111. clockevent->features = CLOCK_EVT_FEAT_ONESHOT;
  112. clockevent->rating = 300;
  113. clockevent->set_next_event = ccount_timer_set_next_event;
  114. clockevent->set_mode = ccount_timer_set_mode;
  115. clockevent->cpumask = cpumask_of(cpu);
  116. clockevent->irq = irq_create_mapping(NULL, LINUX_TIMER_INT);
  117. if (WARN(!clockevent->irq, "error: can't map timer irq"))
  118. return;
  119. clockevents_config_and_register(clockevent, ccount_freq,
  120. 0xf, 0xffffffff);
  121. }
  122. void __init time_init(void)
  123. {
  124. #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
  125. printk("Calibrating CPU frequency ");
  126. platform_calibrate_ccount();
  127. printk("%d.%02d MHz\n", (int)ccount_freq/1000000,
  128. (int)(ccount_freq/10000)%100);
  129. #else
  130. ccount_freq = CONFIG_XTENSA_CPU_CLOCK*1000000UL;
  131. #endif
  132. clocksource_register_hz(&ccount_clocksource, ccount_freq);
  133. local_timer_setup(0);
  134. setup_irq(this_cpu_ptr(&ccount_timer)->evt.irq, &timer_irqaction);
  135. sched_clock_register(ccount_sched_clock_read, 32, ccount_freq);
  136. clocksource_of_init();
  137. }
  138. /*
  139. * The timer interrupt is called HZ times per second.
  140. */
  141. irqreturn_t timer_interrupt(int irq, void *dev_id)
  142. {
  143. struct clock_event_device *evt = &this_cpu_ptr(&ccount_timer)->evt;
  144. set_linux_timer(get_linux_timer());
  145. evt->event_handler(evt);
  146. /* Allow platform to do something useful (Wdog). */
  147. platform_heartbeat();
  148. return IRQ_HANDLED;
  149. }
  150. #ifndef CONFIG_GENERIC_CALIBRATE_DELAY
  151. void calibrate_delay(void)
  152. {
  153. loops_per_jiffy = ccount_freq / HZ;
  154. printk("Calibrating delay loop (skipped)... "
  155. "%lu.%02lu BogoMIPS preset\n",
  156. loops_per_jiffy/(1000000/HZ),
  157. (loops_per_jiffy/(10000/HZ)) % 100);
  158. }
  159. #endif