time.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. struct ccount_timer {
  49. struct clock_event_device evt;
  50. int irq_enabled;
  51. char name[24];
  52. };
  53. static DEFINE_PER_CPU(struct ccount_timer, ccount_timer);
  54. static int ccount_timer_set_next_event(unsigned long delta,
  55. struct clock_event_device *dev)
  56. {
  57. unsigned long flags, next;
  58. int ret = 0;
  59. local_irq_save(flags);
  60. next = get_ccount() + delta;
  61. set_linux_timer(next);
  62. if (next - get_ccount() > delta)
  63. ret = -ETIME;
  64. local_irq_restore(flags);
  65. return ret;
  66. }
  67. /*
  68. * There is no way to disable the timer interrupt at the device level,
  69. * only at the intenable register itself. Since enable_irq/disable_irq
  70. * calls are nested, we need to make sure that these calls are
  71. * balanced.
  72. */
  73. static int ccount_timer_shutdown(struct clock_event_device *evt)
  74. {
  75. struct ccount_timer *timer =
  76. container_of(evt, struct ccount_timer, evt);
  77. if (timer->irq_enabled) {
  78. disable_irq(evt->irq);
  79. timer->irq_enabled = 0;
  80. }
  81. return 0;
  82. }
  83. static int ccount_timer_set_oneshot(struct clock_event_device *evt)
  84. {
  85. struct ccount_timer *timer =
  86. container_of(evt, struct ccount_timer, evt);
  87. if (!timer->irq_enabled) {
  88. enable_irq(evt->irq);
  89. timer->irq_enabled = 1;
  90. }
  91. return 0;
  92. }
  93. static irqreturn_t timer_interrupt(int irq, void *dev_id);
  94. static struct irqaction timer_irqaction = {
  95. .handler = timer_interrupt,
  96. .flags = IRQF_TIMER,
  97. .name = "timer",
  98. };
  99. void local_timer_setup(unsigned cpu)
  100. {
  101. struct ccount_timer *timer = &per_cpu(ccount_timer, cpu);
  102. struct clock_event_device *clockevent = &timer->evt;
  103. timer->irq_enabled = 1;
  104. clockevent->name = timer->name;
  105. snprintf(timer->name, sizeof(timer->name), "ccount_clockevent_%u", cpu);
  106. clockevent->features = CLOCK_EVT_FEAT_ONESHOT;
  107. clockevent->rating = 300;
  108. clockevent->set_next_event = ccount_timer_set_next_event;
  109. clockevent->set_state_shutdown = ccount_timer_shutdown;
  110. clockevent->set_state_oneshot = ccount_timer_set_oneshot;
  111. clockevent->tick_resume = ccount_timer_set_oneshot;
  112. clockevent->cpumask = cpumask_of(cpu);
  113. clockevent->irq = irq_create_mapping(NULL, LINUX_TIMER_INT);
  114. if (WARN(!clockevent->irq, "error: can't map timer irq"))
  115. return;
  116. clockevents_config_and_register(clockevent, ccount_freq,
  117. 0xf, 0xffffffff);
  118. }
  119. void __init time_init(void)
  120. {
  121. #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
  122. printk("Calibrating CPU frequency ");
  123. platform_calibrate_ccount();
  124. printk("%d.%02d MHz\n", (int)ccount_freq/1000000,
  125. (int)(ccount_freq/10000)%100);
  126. #else
  127. ccount_freq = CONFIG_XTENSA_CPU_CLOCK*1000000UL;
  128. #endif
  129. clocksource_register_hz(&ccount_clocksource, ccount_freq);
  130. local_timer_setup(0);
  131. setup_irq(this_cpu_ptr(&ccount_timer)->evt.irq, &timer_irqaction);
  132. sched_clock_register(ccount_sched_clock_read, 32, ccount_freq);
  133. clocksource_of_init();
  134. }
  135. /*
  136. * The timer interrupt is called HZ times per second.
  137. */
  138. irqreturn_t timer_interrupt(int irq, void *dev_id)
  139. {
  140. struct clock_event_device *evt = &this_cpu_ptr(&ccount_timer)->evt;
  141. set_linux_timer(get_linux_timer());
  142. evt->event_handler(evt);
  143. /* Allow platform to do something useful (Wdog). */
  144. platform_heartbeat();
  145. return IRQ_HANDLED;
  146. }
  147. #ifndef CONFIG_GENERIC_CALIBRATE_DELAY
  148. void calibrate_delay(void)
  149. {
  150. loops_per_jiffy = ccount_freq / HZ;
  151. printk("Calibrating delay loop (skipped)... "
  152. "%lu.%02lu BogoMIPS preset\n",
  153. loops_per_jiffy/(1000000/HZ),
  154. (loops_per_jiffy/(10000/HZ)) % 100);
  155. }
  156. #endif