mips-gic-timer.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/clockchips.h>
  10. #include <linux/cpu.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/irqchip/mips-gic.h>
  14. #include <linux/notifier.h>
  15. #include <linux/of_irq.h>
  16. #include <linux/percpu.h>
  17. #include <linux/smp.h>
  18. #include <linux/time.h>
  19. static DEFINE_PER_CPU(struct clock_event_device, gic_clockevent_device);
  20. static int gic_timer_irq;
  21. static unsigned int gic_frequency;
  22. static int gic_next_event(unsigned long delta, struct clock_event_device *evt)
  23. {
  24. u64 cnt;
  25. int res;
  26. cnt = gic_read_count();
  27. cnt += (u64)delta;
  28. gic_write_cpu_compare(cnt, cpumask_first(evt->cpumask));
  29. res = ((int)(gic_read_count() - cnt) >= 0) ? -ETIME : 0;
  30. return res;
  31. }
  32. static irqreturn_t gic_compare_interrupt(int irq, void *dev_id)
  33. {
  34. struct clock_event_device *cd = dev_id;
  35. gic_write_compare(gic_read_compare());
  36. cd->event_handler(cd);
  37. return IRQ_HANDLED;
  38. }
  39. struct irqaction gic_compare_irqaction = {
  40. .handler = gic_compare_interrupt,
  41. .percpu_dev_id = &gic_clockevent_device,
  42. .flags = IRQF_PERCPU | IRQF_TIMER,
  43. .name = "timer",
  44. };
  45. static void gic_clockevent_cpu_init(struct clock_event_device *cd)
  46. {
  47. unsigned int cpu = smp_processor_id();
  48. cd->name = "MIPS GIC";
  49. cd->features = CLOCK_EVT_FEAT_ONESHOT |
  50. CLOCK_EVT_FEAT_C3STOP;
  51. cd->rating = 350;
  52. cd->irq = gic_timer_irq;
  53. cd->cpumask = cpumask_of(cpu);
  54. cd->set_next_event = gic_next_event;
  55. clockevents_config_and_register(cd, gic_frequency, 0x300, 0x7fffffff);
  56. enable_percpu_irq(gic_timer_irq, IRQ_TYPE_NONE);
  57. }
  58. static void gic_clockevent_cpu_exit(struct clock_event_device *cd)
  59. {
  60. disable_percpu_irq(gic_timer_irq);
  61. }
  62. static void gic_update_frequency(void *data)
  63. {
  64. unsigned long rate = (unsigned long)data;
  65. clockevents_update_freq(this_cpu_ptr(&gic_clockevent_device), rate);
  66. }
  67. static int gic_cpu_notifier(struct notifier_block *nb, unsigned long action,
  68. void *data)
  69. {
  70. switch (action & ~CPU_TASKS_FROZEN) {
  71. case CPU_STARTING:
  72. gic_clockevent_cpu_init(this_cpu_ptr(&gic_clockevent_device));
  73. break;
  74. case CPU_DYING:
  75. gic_clockevent_cpu_exit(this_cpu_ptr(&gic_clockevent_device));
  76. break;
  77. }
  78. return NOTIFY_OK;
  79. }
  80. static int gic_clk_notifier(struct notifier_block *nb, unsigned long action,
  81. void *data)
  82. {
  83. struct clk_notifier_data *cnd = data;
  84. if (action == POST_RATE_CHANGE)
  85. on_each_cpu(gic_update_frequency, (void *)cnd->new_rate, 1);
  86. return NOTIFY_OK;
  87. }
  88. static struct notifier_block gic_cpu_nb = {
  89. .notifier_call = gic_cpu_notifier,
  90. };
  91. static struct notifier_block gic_clk_nb = {
  92. .notifier_call = gic_clk_notifier,
  93. };
  94. static int gic_clockevent_init(void)
  95. {
  96. int ret;
  97. if (!cpu_has_counter || !gic_frequency)
  98. return -ENXIO;
  99. ret = setup_percpu_irq(gic_timer_irq, &gic_compare_irqaction);
  100. if (ret < 0)
  101. return ret;
  102. ret = register_cpu_notifier(&gic_cpu_nb);
  103. if (ret < 0)
  104. pr_warn("GIC: Unable to register CPU notifier\n");
  105. gic_clockevent_cpu_init(this_cpu_ptr(&gic_clockevent_device));
  106. return 0;
  107. }
  108. static cycle_t gic_hpt_read(struct clocksource *cs)
  109. {
  110. return gic_read_count();
  111. }
  112. static struct clocksource gic_clocksource = {
  113. .name = "GIC",
  114. .read = gic_hpt_read,
  115. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  116. .archdata = { .vdso_clock_mode = VDSO_CLOCK_GIC },
  117. };
  118. static void __init __gic_clocksource_init(void)
  119. {
  120. int ret;
  121. /* Set clocksource mask. */
  122. gic_clocksource.mask = CLOCKSOURCE_MASK(gic_get_count_width());
  123. /* Calculate a somewhat reasonable rating value. */
  124. gic_clocksource.rating = 200 + gic_frequency / 10000000;
  125. ret = clocksource_register_hz(&gic_clocksource, gic_frequency);
  126. if (ret < 0)
  127. pr_warn("GIC: Unable to register clocksource\n");
  128. }
  129. void __init gic_clocksource_init(unsigned int frequency)
  130. {
  131. gic_frequency = frequency;
  132. gic_timer_irq = MIPS_GIC_IRQ_BASE +
  133. GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_COMPARE);
  134. __gic_clocksource_init();
  135. gic_clockevent_init();
  136. /* And finally start the counter */
  137. gic_start_count();
  138. }
  139. static void __init gic_clocksource_of_init(struct device_node *node)
  140. {
  141. struct clk *clk;
  142. int ret;
  143. if (WARN_ON(!gic_present || !node->parent ||
  144. !of_device_is_compatible(node->parent, "mti,gic")))
  145. return;
  146. clk = of_clk_get(node, 0);
  147. if (!IS_ERR(clk)) {
  148. if (clk_prepare_enable(clk) < 0) {
  149. pr_err("GIC failed to enable clock\n");
  150. clk_put(clk);
  151. return;
  152. }
  153. gic_frequency = clk_get_rate(clk);
  154. } else if (of_property_read_u32(node, "clock-frequency",
  155. &gic_frequency)) {
  156. pr_err("GIC frequency not specified.\n");
  157. return;
  158. }
  159. gic_timer_irq = irq_of_parse_and_map(node, 0);
  160. if (!gic_timer_irq) {
  161. pr_err("GIC timer IRQ not specified.\n");
  162. return;
  163. }
  164. __gic_clocksource_init();
  165. ret = gic_clockevent_init();
  166. if (!ret && !IS_ERR(clk)) {
  167. if (clk_notifier_register(clk, &gic_clk_nb) < 0)
  168. pr_warn("GIC: Unable to register clock notifier\n");
  169. }
  170. /* And finally start the counter */
  171. gic_start_count();
  172. }
  173. CLOCKSOURCE_OF_DECLARE(mips_gic_timer, "mti,gic-timer",
  174. gic_clocksource_of_init);