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