mips-gic-timer.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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/clockchips.h>
  9. #include <linux/cpu.h>
  10. #include <linux/init.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/irqchip/mips-gic.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. static DEFINE_PER_CPU(struct clock_event_device, gic_clockevent_device);
  19. static int gic_timer_irq;
  20. static unsigned int gic_frequency;
  21. static int gic_next_event(unsigned long delta, struct clock_event_device *evt)
  22. {
  23. u64 cnt;
  24. int res;
  25. cnt = gic_read_count();
  26. cnt += (u64)delta;
  27. gic_write_cpu_compare(cnt, cpumask_first(evt->cpumask));
  28. res = ((int)(gic_read_count() - cnt) >= 0) ? -ETIME : 0;
  29. return res;
  30. }
  31. static void gic_set_clock_mode(enum clock_event_mode mode,
  32. struct clock_event_device *evt)
  33. {
  34. /* Nothing to do ... */
  35. }
  36. static irqreturn_t gic_compare_interrupt(int irq, void *dev_id)
  37. {
  38. struct clock_event_device *cd = dev_id;
  39. gic_write_compare(gic_read_compare());
  40. cd->event_handler(cd);
  41. return IRQ_HANDLED;
  42. }
  43. struct irqaction gic_compare_irqaction = {
  44. .handler = gic_compare_interrupt,
  45. .percpu_dev_id = &gic_clockevent_device,
  46. .flags = IRQF_PERCPU | IRQF_TIMER,
  47. .name = "timer",
  48. };
  49. static void gic_clockevent_cpu_init(struct clock_event_device *cd)
  50. {
  51. unsigned int cpu = smp_processor_id();
  52. cd->name = "MIPS GIC";
  53. cd->features = CLOCK_EVT_FEAT_ONESHOT |
  54. CLOCK_EVT_FEAT_C3STOP;
  55. cd->rating = 350;
  56. cd->irq = gic_timer_irq;
  57. cd->cpumask = cpumask_of(cpu);
  58. cd->set_next_event = gic_next_event;
  59. cd->set_mode = gic_set_clock_mode;
  60. clockevents_config_and_register(cd, gic_frequency, 0x300, 0x7fffffff);
  61. enable_percpu_irq(gic_timer_irq, IRQ_TYPE_NONE);
  62. }
  63. static void gic_clockevent_cpu_exit(struct clock_event_device *cd)
  64. {
  65. disable_percpu_irq(gic_timer_irq);
  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 struct notifier_block gic_cpu_nb = {
  81. .notifier_call = gic_cpu_notifier,
  82. };
  83. static int gic_clockevent_init(void)
  84. {
  85. if (!cpu_has_counter || !gic_frequency)
  86. return -ENXIO;
  87. setup_percpu_irq(gic_timer_irq, &gic_compare_irqaction);
  88. register_cpu_notifier(&gic_cpu_nb);
  89. gic_clockevent_cpu_init(this_cpu_ptr(&gic_clockevent_device));
  90. return 0;
  91. }
  92. static cycle_t gic_hpt_read(struct clocksource *cs)
  93. {
  94. return gic_read_count();
  95. }
  96. static struct clocksource gic_clocksource = {
  97. .name = "GIC",
  98. .read = gic_hpt_read,
  99. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  100. };
  101. static void __init __gic_clocksource_init(void)
  102. {
  103. /* Set clocksource mask. */
  104. gic_clocksource.mask = CLOCKSOURCE_MASK(gic_get_count_width());
  105. /* Calculate a somewhat reasonable rating value. */
  106. gic_clocksource.rating = 200 + gic_frequency / 10000000;
  107. clocksource_register_hz(&gic_clocksource, gic_frequency);
  108. gic_clockevent_init();
  109. }
  110. void __init gic_clocksource_init(unsigned int frequency)
  111. {
  112. gic_frequency = frequency;
  113. gic_timer_irq = MIPS_GIC_IRQ_BASE +
  114. GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_COMPARE);
  115. __gic_clocksource_init();
  116. }
  117. static void __init gic_clocksource_of_init(struct device_node *node)
  118. {
  119. if (WARN_ON(!gic_present || !node->parent ||
  120. !of_device_is_compatible(node->parent, "mti,gic")))
  121. return;
  122. if (of_property_read_u32(node, "clock-frequency", &gic_frequency)) {
  123. pr_err("GIC frequency not specified.\n");
  124. return;
  125. }
  126. gic_timer_irq = irq_of_parse_and_map(node, 0);
  127. if (!gic_timer_irq) {
  128. pr_err("GIC timer IRQ not specified.\n");
  129. return;
  130. }
  131. __gic_clocksource_init();
  132. }
  133. CLOCKSOURCE_OF_DECLARE(mips_gic_timer, "mti,gic-timer",
  134. gic_clocksource_of_init);