mips-gic-timer.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. #define pr_fmt(fmt) "mips-gic-timer: " fmt
  9. #include <linux/clk.h>
  10. #include <linux/clockchips.h>
  11. #include <linux/cpu.h>
  12. #include <linux/init.h>
  13. #include <linux/interrupt.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. #include <asm/mips-cps.h>
  20. static DEFINE_PER_CPU(struct clock_event_device, gic_clockevent_device);
  21. static int gic_timer_irq;
  22. static unsigned int gic_frequency;
  23. static u64 notrace gic_read_count(void)
  24. {
  25. unsigned int hi, hi2, lo;
  26. if (mips_cm_is64)
  27. return read_gic_counter();
  28. do {
  29. hi = read_gic_counter_32h();
  30. lo = read_gic_counter_32l();
  31. hi2 = read_gic_counter_32h();
  32. } while (hi2 != hi);
  33. return (((u64) hi) << 32) + lo;
  34. }
  35. static int gic_next_event(unsigned long delta, struct clock_event_device *evt)
  36. {
  37. int cpu = cpumask_first(evt->cpumask);
  38. u64 cnt;
  39. int res;
  40. cnt = gic_read_count();
  41. cnt += (u64)delta;
  42. if (cpu == raw_smp_processor_id()) {
  43. write_gic_vl_compare(cnt);
  44. } else {
  45. write_gic_vl_other(mips_cm_vp_id(cpu));
  46. write_gic_vo_compare(cnt);
  47. }
  48. res = ((int)(gic_read_count() - cnt) >= 0) ? -ETIME : 0;
  49. return res;
  50. }
  51. static irqreturn_t gic_compare_interrupt(int irq, void *dev_id)
  52. {
  53. struct clock_event_device *cd = dev_id;
  54. write_gic_vl_compare(read_gic_vl_compare());
  55. cd->event_handler(cd);
  56. return IRQ_HANDLED;
  57. }
  58. struct irqaction gic_compare_irqaction = {
  59. .handler = gic_compare_interrupt,
  60. .percpu_dev_id = &gic_clockevent_device,
  61. .flags = IRQF_PERCPU | IRQF_TIMER,
  62. .name = "timer",
  63. };
  64. static void gic_clockevent_cpu_init(unsigned int cpu,
  65. struct clock_event_device *cd)
  66. {
  67. cd->name = "MIPS GIC";
  68. cd->features = CLOCK_EVT_FEAT_ONESHOT |
  69. CLOCK_EVT_FEAT_C3STOP;
  70. cd->rating = 350;
  71. cd->irq = gic_timer_irq;
  72. cd->cpumask = cpumask_of(cpu);
  73. cd->set_next_event = gic_next_event;
  74. clockevents_config_and_register(cd, gic_frequency, 0x300, 0x7fffffff);
  75. enable_percpu_irq(gic_timer_irq, IRQ_TYPE_NONE);
  76. }
  77. static void gic_clockevent_cpu_exit(struct clock_event_device *cd)
  78. {
  79. disable_percpu_irq(gic_timer_irq);
  80. }
  81. static void gic_update_frequency(void *data)
  82. {
  83. unsigned long rate = (unsigned long)data;
  84. clockevents_update_freq(this_cpu_ptr(&gic_clockevent_device), rate);
  85. }
  86. static int gic_starting_cpu(unsigned int cpu)
  87. {
  88. gic_clockevent_cpu_init(cpu, this_cpu_ptr(&gic_clockevent_device));
  89. return 0;
  90. }
  91. static int gic_clk_notifier(struct notifier_block *nb, unsigned long action,
  92. void *data)
  93. {
  94. struct clk_notifier_data *cnd = data;
  95. if (action == POST_RATE_CHANGE)
  96. on_each_cpu(gic_update_frequency, (void *)cnd->new_rate, 1);
  97. return NOTIFY_OK;
  98. }
  99. static int gic_dying_cpu(unsigned int cpu)
  100. {
  101. gic_clockevent_cpu_exit(this_cpu_ptr(&gic_clockevent_device));
  102. return 0;
  103. }
  104. static struct notifier_block gic_clk_nb = {
  105. .notifier_call = gic_clk_notifier,
  106. };
  107. static int gic_clockevent_init(void)
  108. {
  109. int ret;
  110. if (!gic_frequency)
  111. return -ENXIO;
  112. ret = setup_percpu_irq(gic_timer_irq, &gic_compare_irqaction);
  113. if (ret < 0) {
  114. pr_err("IRQ %d setup failed (%d)\n", gic_timer_irq, ret);
  115. return ret;
  116. }
  117. cpuhp_setup_state(CPUHP_AP_MIPS_GIC_TIMER_STARTING,
  118. "clockevents/mips/gic/timer:starting",
  119. gic_starting_cpu, gic_dying_cpu);
  120. return 0;
  121. }
  122. static u64 gic_hpt_read(struct clocksource *cs)
  123. {
  124. return gic_read_count();
  125. }
  126. static struct clocksource gic_clocksource = {
  127. .name = "GIC",
  128. .read = gic_hpt_read,
  129. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  130. .archdata = { .vdso_clock_mode = VDSO_CLOCK_GIC },
  131. };
  132. static int __init __gic_clocksource_init(void)
  133. {
  134. unsigned int count_width;
  135. int ret;
  136. /* Set clocksource mask. */
  137. count_width = read_gic_config() & GIC_CONFIG_COUNTBITS;
  138. count_width >>= __ffs(GIC_CONFIG_COUNTBITS);
  139. count_width *= 4;
  140. count_width += 32;
  141. gic_clocksource.mask = CLOCKSOURCE_MASK(count_width);
  142. /* Calculate a somewhat reasonable rating value. */
  143. gic_clocksource.rating = 200 + gic_frequency / 10000000;
  144. ret = clocksource_register_hz(&gic_clocksource, gic_frequency);
  145. if (ret < 0)
  146. pr_warn("Unable to register clocksource\n");
  147. return ret;
  148. }
  149. static int __init gic_clocksource_of_init(struct device_node *node)
  150. {
  151. struct clk *clk;
  152. int ret;
  153. if (!mips_gic_present() || !node->parent ||
  154. !of_device_is_compatible(node->parent, "mti,gic")) {
  155. pr_warn("No DT definition\n");
  156. return -ENXIO;
  157. }
  158. clk = of_clk_get(node, 0);
  159. if (!IS_ERR(clk)) {
  160. ret = clk_prepare_enable(clk);
  161. if (ret < 0) {
  162. pr_err("Failed to enable clock\n");
  163. clk_put(clk);
  164. return ret;
  165. }
  166. gic_frequency = clk_get_rate(clk);
  167. } else if (of_property_read_u32(node, "clock-frequency",
  168. &gic_frequency)) {
  169. pr_err("Frequency not specified\n");
  170. return -EINVAL;
  171. }
  172. gic_timer_irq = irq_of_parse_and_map(node, 0);
  173. if (!gic_timer_irq) {
  174. pr_err("IRQ not specified\n");
  175. return -EINVAL;
  176. }
  177. ret = __gic_clocksource_init();
  178. if (ret)
  179. return ret;
  180. ret = gic_clockevent_init();
  181. if (!ret && !IS_ERR(clk)) {
  182. if (clk_notifier_register(clk, &gic_clk_nb) < 0)
  183. pr_warn("Unable to register clock notifier\n");
  184. }
  185. /* And finally start the counter */
  186. clear_gic_config(GIC_CONFIG_COUNTSTOP);
  187. return 0;
  188. }
  189. TIMER_OF_DECLARE(mips_gic_timer, "mti,gic-timer",
  190. gic_clocksource_of_init);