mips-gic-timer.c 5.3 KB

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