time.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 u64 ccount_read(struct clocksource *cs)
  34. {
  35. return (u64)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. struct ccount_timer {
  49. struct clock_event_device evt;
  50. int irq_enabled;
  51. char name[24];
  52. };
  53. static int ccount_timer_set_next_event(unsigned long delta,
  54. struct clock_event_device *dev)
  55. {
  56. unsigned long flags, next;
  57. int ret = 0;
  58. local_irq_save(flags);
  59. next = get_ccount() + delta;
  60. set_linux_timer(next);
  61. if (next - get_ccount() > delta)
  62. ret = -ETIME;
  63. local_irq_restore(flags);
  64. return ret;
  65. }
  66. /*
  67. * There is no way to disable the timer interrupt at the device level,
  68. * only at the intenable register itself. Since enable_irq/disable_irq
  69. * calls are nested, we need to make sure that these calls are
  70. * balanced.
  71. */
  72. static int ccount_timer_shutdown(struct clock_event_device *evt)
  73. {
  74. struct ccount_timer *timer =
  75. container_of(evt, struct ccount_timer, evt);
  76. if (timer->irq_enabled) {
  77. disable_irq_nosync(evt->irq);
  78. timer->irq_enabled = 0;
  79. }
  80. return 0;
  81. }
  82. static int ccount_timer_set_oneshot(struct clock_event_device *evt)
  83. {
  84. struct ccount_timer *timer =
  85. container_of(evt, struct ccount_timer, evt);
  86. if (!timer->irq_enabled) {
  87. enable_irq(evt->irq);
  88. timer->irq_enabled = 1;
  89. }
  90. return 0;
  91. }
  92. static DEFINE_PER_CPU(struct ccount_timer, ccount_timer) = {
  93. .evt = {
  94. .features = CLOCK_EVT_FEAT_ONESHOT,
  95. .rating = 300,
  96. .set_next_event = ccount_timer_set_next_event,
  97. .set_state_shutdown = ccount_timer_shutdown,
  98. .set_state_oneshot = ccount_timer_set_oneshot,
  99. .tick_resume = ccount_timer_set_oneshot,
  100. },
  101. };
  102. static irqreturn_t timer_interrupt(int irq, void *dev_id)
  103. {
  104. struct clock_event_device *evt = &this_cpu_ptr(&ccount_timer)->evt;
  105. set_linux_timer(get_linux_timer());
  106. evt->event_handler(evt);
  107. /* Allow platform to do something useful (Wdog). */
  108. platform_heartbeat();
  109. return IRQ_HANDLED;
  110. }
  111. void local_timer_setup(unsigned cpu)
  112. {
  113. struct ccount_timer *timer = &per_cpu(ccount_timer, cpu);
  114. struct clock_event_device *clockevent = &timer->evt;
  115. timer->irq_enabled = 1;
  116. snprintf(timer->name, sizeof(timer->name), "ccount_clockevent_%u", cpu);
  117. clockevent->name = timer->name;
  118. clockevent->cpumask = cpumask_of(cpu);
  119. clockevent->irq = irq_create_mapping(NULL, LINUX_TIMER_INT);
  120. if (WARN(!clockevent->irq, "error: can't map timer irq"))
  121. return;
  122. clockevents_config_and_register(clockevent, ccount_freq,
  123. 0xf, 0xffffffff);
  124. }
  125. #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
  126. #ifdef CONFIG_OF
  127. static void __init calibrate_ccount(void)
  128. {
  129. struct device_node *cpu;
  130. struct clk *clk;
  131. cpu = of_find_compatible_node(NULL, NULL, "cdns,xtensa-cpu");
  132. if (cpu) {
  133. clk = of_clk_get(cpu, 0);
  134. if (!IS_ERR(clk)) {
  135. ccount_freq = clk_get_rate(clk);
  136. return;
  137. } else {
  138. pr_warn("%s: CPU input clock not found\n",
  139. __func__);
  140. }
  141. } else {
  142. pr_warn("%s: CPU node not found in the device tree\n",
  143. __func__);
  144. }
  145. platform_calibrate_ccount();
  146. }
  147. #else
  148. static inline void calibrate_ccount(void)
  149. {
  150. platform_calibrate_ccount();
  151. }
  152. #endif
  153. #endif
  154. void __init time_init(void)
  155. {
  156. int irq;
  157. of_clk_init(NULL);
  158. #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
  159. pr_info("Calibrating CPU frequency ");
  160. calibrate_ccount();
  161. pr_cont("%d.%02d MHz\n",
  162. (int)ccount_freq / 1000000,
  163. (int)(ccount_freq / 10000) % 100);
  164. #else
  165. ccount_freq = CONFIG_XTENSA_CPU_CLOCK*1000000UL;
  166. #endif
  167. WARN(!ccount_freq,
  168. "%s: CPU clock frequency is not set up correctly\n",
  169. __func__);
  170. clocksource_register_hz(&ccount_clocksource, ccount_freq);
  171. local_timer_setup(0);
  172. irq = this_cpu_ptr(&ccount_timer)->evt.irq;
  173. if (request_irq(irq, timer_interrupt, IRQF_TIMER, "timer", NULL))
  174. pr_err("Failed to request irq %d (timer)\n", irq);
  175. sched_clock_register(ccount_sched_clock_read, 32, ccount_freq);
  176. timer_probe();
  177. }
  178. #ifndef CONFIG_GENERIC_CALIBRATE_DELAY
  179. void calibrate_delay(void)
  180. {
  181. loops_per_jiffy = ccount_freq / HZ;
  182. pr_info("Calibrating delay loop (skipped)... %lu.%02lu BogoMIPS preset\n",
  183. loops_per_jiffy / (1000000 / HZ),
  184. (loops_per_jiffy / (10000 / HZ)) % 100);
  185. }
  186. #endif