arm_generic.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Generic timers support
  3. *
  4. * Copyright (C) 2012 ARM Ltd.
  5. * Author: Marc Zyngier <marc.zyngier@arm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/delay.h>
  22. #include <linux/device.h>
  23. #include <linux/smp.h>
  24. #include <linux/cpu.h>
  25. #include <linux/jiffies.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/clockchips.h>
  28. #include <linux/of_irq.h>
  29. #include <linux/io.h>
  30. #include <clocksource/arm_generic.h>
  31. #include <asm/arm_generic.h>
  32. static u32 arch_timer_rate;
  33. static u64 sched_clock_mult __read_mostly;
  34. static DEFINE_PER_CPU(struct clock_event_device, arch_timer_evt);
  35. static int arch_timer_ppi;
  36. static irqreturn_t arch_timer_handle_irq(int irq, void *dev_id)
  37. {
  38. struct clock_event_device *evt = dev_id;
  39. unsigned long ctrl;
  40. ctrl = arch_timer_reg_read(ARCH_TIMER_REG_CTRL);
  41. if (ctrl & ARCH_TIMER_CTRL_ISTATUS) {
  42. ctrl |= ARCH_TIMER_CTRL_IMASK;
  43. arch_timer_reg_write(ARCH_TIMER_REG_CTRL, ctrl);
  44. evt->event_handler(evt);
  45. return IRQ_HANDLED;
  46. }
  47. return IRQ_NONE;
  48. }
  49. static void arch_timer_stop(void)
  50. {
  51. unsigned long ctrl;
  52. ctrl = arch_timer_reg_read(ARCH_TIMER_REG_CTRL);
  53. ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
  54. arch_timer_reg_write(ARCH_TIMER_REG_CTRL, ctrl);
  55. }
  56. static void arch_timer_set_mode(enum clock_event_mode mode,
  57. struct clock_event_device *clk)
  58. {
  59. switch (mode) {
  60. case CLOCK_EVT_MODE_UNUSED:
  61. case CLOCK_EVT_MODE_SHUTDOWN:
  62. arch_timer_stop();
  63. break;
  64. default:
  65. break;
  66. }
  67. }
  68. static int arch_timer_set_next_event(unsigned long evt,
  69. struct clock_event_device *unused)
  70. {
  71. unsigned long ctrl;
  72. ctrl = arch_timer_reg_read(ARCH_TIMER_REG_CTRL);
  73. ctrl |= ARCH_TIMER_CTRL_ENABLE;
  74. ctrl &= ~ARCH_TIMER_CTRL_IMASK;
  75. arch_timer_reg_write(ARCH_TIMER_REG_TVAL, evt);
  76. arch_timer_reg_write(ARCH_TIMER_REG_CTRL, ctrl);
  77. return 0;
  78. }
  79. static void __cpuinit arch_timer_setup(struct clock_event_device *clk)
  80. {
  81. /* Let's make sure the timer is off before doing anything else */
  82. arch_timer_stop();
  83. clk->features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_C3STOP;
  84. clk->name = "arch_sys_timer";
  85. clk->rating = 400;
  86. clk->set_mode = arch_timer_set_mode;
  87. clk->set_next_event = arch_timer_set_next_event;
  88. clk->irq = arch_timer_ppi;
  89. clk->cpumask = cpumask_of(smp_processor_id());
  90. clockevents_config_and_register(clk, arch_timer_rate,
  91. 0xf, 0x7fffffff);
  92. enable_percpu_irq(clk->irq, 0);
  93. /* Ensure the virtual counter is visible to userspace for the vDSO. */
  94. arch_counter_enable_user_access();
  95. }
  96. static void __init arch_timer_calibrate(void)
  97. {
  98. if (arch_timer_rate == 0) {
  99. arch_timer_reg_write(ARCH_TIMER_REG_CTRL, 0);
  100. arch_timer_rate = arch_timer_reg_read(ARCH_TIMER_REG_FREQ);
  101. /* Check the timer frequency. */
  102. if (arch_timer_rate == 0)
  103. panic("Architected timer frequency is set to zero.\n"
  104. "You must set this in your .dts file\n");
  105. }
  106. /* Cache the sched_clock multiplier to save a divide in the hot path. */
  107. sched_clock_mult = DIV_ROUND_CLOSEST(NSEC_PER_SEC, arch_timer_rate);
  108. pr_info("Architected local timer running at %u.%02uMHz.\n",
  109. arch_timer_rate / 1000000, (arch_timer_rate / 10000) % 100);
  110. }
  111. static cycle_t arch_counter_read(struct clocksource *cs)
  112. {
  113. return arch_counter_get_cntpct();
  114. }
  115. static struct clocksource clocksource_counter = {
  116. .name = "arch_sys_counter",
  117. .rating = 400,
  118. .read = arch_counter_read,
  119. .mask = CLOCKSOURCE_MASK(56),
  120. .flags = (CLOCK_SOURCE_IS_CONTINUOUS | CLOCK_SOURCE_VALID_FOR_HRES),
  121. };
  122. int read_current_timer(unsigned long *timer_value)
  123. {
  124. *timer_value = arch_counter_get_cntpct();
  125. return 0;
  126. }
  127. unsigned long long notrace sched_clock(void)
  128. {
  129. return arch_counter_get_cntvct() * sched_clock_mult;
  130. }
  131. static int __cpuinit arch_timer_cpu_notify(struct notifier_block *self,
  132. unsigned long action, void *hcpu)
  133. {
  134. int cpu = (long)hcpu;
  135. struct clock_event_device *clk = per_cpu_ptr(&arch_timer_evt, cpu);
  136. switch(action) {
  137. case CPU_STARTING:
  138. case CPU_STARTING_FROZEN:
  139. arch_timer_setup(clk);
  140. break;
  141. case CPU_DYING:
  142. case CPU_DYING_FROZEN:
  143. pr_debug("arch_timer_teardown disable IRQ%d cpu #%d\n",
  144. clk->irq, cpu);
  145. disable_percpu_irq(clk->irq);
  146. arch_timer_set_mode(CLOCK_EVT_MODE_UNUSED, clk);
  147. break;
  148. }
  149. return NOTIFY_OK;
  150. }
  151. static struct notifier_block __cpuinitdata arch_timer_cpu_nb = {
  152. .notifier_call = arch_timer_cpu_notify,
  153. };
  154. static const struct of_device_id arch_timer_of_match[] __initconst = {
  155. { .compatible = "arm,armv8-timer" },
  156. {},
  157. };
  158. int __init arm_generic_timer_init(void)
  159. {
  160. struct device_node *np;
  161. int err;
  162. u32 freq;
  163. np = of_find_matching_node(NULL, arch_timer_of_match);
  164. if (!np) {
  165. pr_err("arch_timer: can't find DT node\n");
  166. return -ENODEV;
  167. }
  168. /* Try to determine the frequency from the device tree or CNTFRQ */
  169. if (!of_property_read_u32(np, "clock-frequency", &freq))
  170. arch_timer_rate = freq;
  171. arch_timer_calibrate();
  172. arch_timer_ppi = irq_of_parse_and_map(np, 0);
  173. pr_info("arch_timer: found %s irq %d\n", np->name, arch_timer_ppi);
  174. err = request_percpu_irq(arch_timer_ppi, arch_timer_handle_irq,
  175. np->name, &arch_timer_evt);
  176. if (err) {
  177. pr_err("arch_timer: can't register interrupt %d (%d)\n",
  178. arch_timer_ppi, err);
  179. return err;
  180. }
  181. clocksource_register_hz(&clocksource_counter, arch_timer_rate);
  182. /* Calibrate the delay loop directly */
  183. lpj_fine = DIV_ROUND_CLOSEST(arch_timer_rate, HZ);
  184. /* Immediately configure the timer on the boot CPU */
  185. arch_timer_setup(this_cpu_ptr(&arch_timer_evt));
  186. register_cpu_notifier(&arch_timer_cpu_nb);
  187. return 0;
  188. }