time.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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/clk.h>
  15. #include <linux/clk-provider.h>
  16. #include <linux/errno.h>
  17. #include <linux/sched.h>
  18. #include <linux/time.h>
  19. #include <linux/clocksource.h>
  20. #include <linux/clockchips.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/irq.h>
  25. #include <linux/profile.h>
  26. #include <linux/delay.h>
  27. #include <linux/irqdomain.h>
  28. #include <linux/sched_clock.h>
  29. #include <asm/timex.h>
  30. #include <asm/platform.h>
  31. unsigned long ccount_freq; /* ccount Hz */
  32. EXPORT_SYMBOL(ccount_freq);
  33. static cycle_t ccount_read(struct clocksource *cs)
  34. {
  35. return (cycle_t)get_ccount();
  36. }
  37. static u64 notrace ccount_sched_clock_read(void)
  38. {
  39. return get_ccount();
  40. }
  41. static struct clocksource ccount_clocksource = {
  42. .name = "ccount",
  43. .rating = 200,
  44. .read = ccount_read,
  45. .mask = CLOCKSOURCE_MASK(32),
  46. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  47. };
  48. static int ccount_timer_set_next_event(unsigned long delta,
  49. struct clock_event_device *dev);
  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. /*
  70. * There is no way to disable the timer interrupt at the device level,
  71. * only at the intenable register itself. Since enable_irq/disable_irq
  72. * calls are nested, we need to make sure that these calls are
  73. * balanced.
  74. */
  75. static int ccount_timer_shutdown(struct clock_event_device *evt)
  76. {
  77. struct ccount_timer *timer =
  78. container_of(evt, struct ccount_timer, evt);
  79. if (timer->irq_enabled) {
  80. disable_irq(evt->irq);
  81. timer->irq_enabled = 0;
  82. }
  83. return 0;
  84. }
  85. static int ccount_timer_set_oneshot(struct clock_event_device *evt)
  86. {
  87. struct ccount_timer *timer =
  88. container_of(evt, struct ccount_timer, evt);
  89. if (!timer->irq_enabled) {
  90. enable_irq(evt->irq);
  91. timer->irq_enabled = 1;
  92. }
  93. return 0;
  94. }
  95. static irqreturn_t timer_interrupt(int irq, void *dev_id);
  96. static struct irqaction timer_irqaction = {
  97. .handler = timer_interrupt,
  98. .flags = IRQF_TIMER,
  99. .name = "timer",
  100. };
  101. void local_timer_setup(unsigned cpu)
  102. {
  103. struct ccount_timer *timer = &per_cpu(ccount_timer, cpu);
  104. struct clock_event_device *clockevent = &timer->evt;
  105. timer->irq_enabled = 1;
  106. clockevent->name = timer->name;
  107. snprintf(timer->name, sizeof(timer->name), "ccount_clockevent_%u", cpu);
  108. clockevent->features = CLOCK_EVT_FEAT_ONESHOT;
  109. clockevent->rating = 300;
  110. clockevent->set_next_event = ccount_timer_set_next_event;
  111. clockevent->set_state_shutdown = ccount_timer_shutdown;
  112. clockevent->set_state_oneshot = ccount_timer_set_oneshot;
  113. clockevent->tick_resume = ccount_timer_set_oneshot;
  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. #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
  122. #ifdef CONFIG_OF
  123. static void __init calibrate_ccount(void)
  124. {
  125. struct device_node *cpu;
  126. struct clk *clk;
  127. cpu = of_find_compatible_node(NULL, NULL, "cdns,xtensa-cpu");
  128. if (cpu) {
  129. clk = of_clk_get(cpu, 0);
  130. if (!IS_ERR(clk)) {
  131. ccount_freq = clk_get_rate(clk);
  132. return;
  133. } else {
  134. pr_warn("%s: CPU input clock not found\n",
  135. __func__);
  136. }
  137. } else {
  138. pr_warn("%s: CPU node not found in the device tree\n",
  139. __func__);
  140. }
  141. platform_calibrate_ccount();
  142. }
  143. #else
  144. static inline void calibrate_ccount(void)
  145. {
  146. platform_calibrate_ccount();
  147. }
  148. #endif
  149. #endif
  150. void __init time_init(void)
  151. {
  152. of_clk_init(NULL);
  153. #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
  154. pr_info("Calibrating CPU frequency ");
  155. calibrate_ccount();
  156. pr_cont("%d.%02d MHz\n",
  157. (int)ccount_freq / 1000000,
  158. (int)(ccount_freq / 10000) % 100);
  159. #else
  160. ccount_freq = CONFIG_XTENSA_CPU_CLOCK*1000000UL;
  161. #endif
  162. WARN(!ccount_freq,
  163. "%s: CPU clock frequency is not set up correctly\n",
  164. __func__);
  165. clocksource_register_hz(&ccount_clocksource, ccount_freq);
  166. local_timer_setup(0);
  167. setup_irq(this_cpu_ptr(&ccount_timer)->evt.irq, &timer_irqaction);
  168. sched_clock_register(ccount_sched_clock_read, 32, ccount_freq);
  169. clocksource_probe();
  170. }
  171. /*
  172. * The timer interrupt is called HZ times per second.
  173. */
  174. irqreturn_t timer_interrupt(int irq, void *dev_id)
  175. {
  176. struct clock_event_device *evt = &this_cpu_ptr(&ccount_timer)->evt;
  177. set_linux_timer(get_linux_timer());
  178. evt->event_handler(evt);
  179. /* Allow platform to do something useful (Wdog). */
  180. platform_heartbeat();
  181. return IRQ_HANDLED;
  182. }
  183. #ifndef CONFIG_GENERIC_CALIBRATE_DELAY
  184. void calibrate_delay(void)
  185. {
  186. loops_per_jiffy = ccount_freq / HZ;
  187. pr_info("Calibrating delay loop (skipped)... %lu.%02lu BogoMIPS preset\n",
  188. loops_per_jiffy / (1000000 / HZ),
  189. (loops_per_jiffy / (10000 / HZ)) % 100);
  190. }
  191. #endif