time.c 4.1 KB

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