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. 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 u32 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. };
  44. static int ccount_timer_set_next_event(unsigned long delta,
  45. struct clock_event_device *dev);
  46. static void ccount_timer_set_mode(enum clock_event_mode mode,
  47. struct clock_event_device *evt);
  48. static struct ccount_timer_t {
  49. struct clock_event_device evt;
  50. int irq_enabled;
  51. } ccount_timer = {
  52. .evt = {
  53. .name = "ccount_clockevent",
  54. .features = CLOCK_EVT_FEAT_ONESHOT,
  55. .rating = 300,
  56. .set_next_event = ccount_timer_set_next_event,
  57. .set_mode = ccount_timer_set_mode,
  58. },
  59. };
  60. static int ccount_timer_set_next_event(unsigned long delta,
  61. struct clock_event_device *dev)
  62. {
  63. unsigned long flags, next;
  64. int ret = 0;
  65. local_irq_save(flags);
  66. next = get_ccount() + delta;
  67. set_linux_timer(next);
  68. if (next - get_ccount() > delta)
  69. ret = -ETIME;
  70. local_irq_restore(flags);
  71. return ret;
  72. }
  73. static void ccount_timer_set_mode(enum clock_event_mode mode,
  74. struct clock_event_device *evt)
  75. {
  76. struct ccount_timer_t *timer =
  77. container_of(evt, struct ccount_timer_t, evt);
  78. /*
  79. * There is no way to disable the timer interrupt at the device level,
  80. * only at the intenable register itself. Since enable_irq/disable_irq
  81. * calls are nested, we need to make sure that these calls are
  82. * balanced.
  83. */
  84. switch (mode) {
  85. case CLOCK_EVT_MODE_SHUTDOWN:
  86. case CLOCK_EVT_MODE_UNUSED:
  87. if (timer->irq_enabled) {
  88. disable_irq(evt->irq);
  89. timer->irq_enabled = 0;
  90. }
  91. break;
  92. case CLOCK_EVT_MODE_RESUME:
  93. case CLOCK_EVT_MODE_ONESHOT:
  94. if (!timer->irq_enabled) {
  95. enable_irq(evt->irq);
  96. timer->irq_enabled = 1;
  97. }
  98. default:
  99. break;
  100. }
  101. }
  102. static irqreturn_t timer_interrupt(int irq, void *dev_id);
  103. static struct irqaction timer_irqaction = {
  104. .handler = timer_interrupt,
  105. .flags = IRQF_TIMER,
  106. .name = "timer",
  107. .dev_id = &ccount_timer,
  108. };
  109. void __init time_init(void)
  110. {
  111. #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
  112. printk("Calibrating CPU frequency ");
  113. platform_calibrate_ccount();
  114. printk("%d.%02d MHz\n", (int)ccount_freq/1000000,
  115. (int)(ccount_freq/10000)%100);
  116. #else
  117. ccount_freq = CONFIG_XTENSA_CPU_CLOCK*1000000UL;
  118. #endif
  119. clocksource_register_hz(&ccount_clocksource, ccount_freq);
  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_freq / HZ;
  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