cevt-r4k.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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) 2007 MIPS Technologies, Inc.
  7. * Copyright (C) 2007 Ralf Baechle <ralf@linux-mips.org>
  8. */
  9. #include <linux/clockchips.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/percpu.h>
  12. #include <linux/smp.h>
  13. #include <asm/smtc_ipi.h>
  14. #include <asm/time.h>
  15. #include <asm/cevt-r4k.h>
  16. /*
  17. * The SMTC Kernel for the 34K, 1004K, et. al. replaces several
  18. * of these routines with SMTC-specific variants.
  19. */
  20. #ifndef CONFIG_MIPS_MT_SMTC
  21. static int mips_next_event(unsigned long delta,
  22. struct clock_event_device *evt)
  23. {
  24. unsigned int cnt;
  25. int res;
  26. cnt = read_c0_count();
  27. cnt += delta;
  28. write_c0_compare(cnt);
  29. res = ((int)(read_c0_count() - cnt) > 0) ? -ETIME : 0;
  30. return res;
  31. }
  32. #endif /* CONFIG_MIPS_MT_SMTC */
  33. void mips_set_clock_mode(enum clock_event_mode mode,
  34. struct clock_event_device *evt)
  35. {
  36. /* Nothing to do ... */
  37. }
  38. DEFINE_PER_CPU(struct clock_event_device, mips_clockevent_device);
  39. int cp0_timer_irq_installed;
  40. #ifndef CONFIG_MIPS_MT_SMTC
  41. irqreturn_t c0_compare_interrupt(int irq, void *dev_id)
  42. {
  43. const int r2 = cpu_has_mips_r2;
  44. struct clock_event_device *cd;
  45. int cpu = smp_processor_id();
  46. /*
  47. * Suckage alert:
  48. * Before R2 of the architecture there was no way to see if a
  49. * performance counter interrupt was pending, so we have to run
  50. * the performance counter interrupt handler anyway.
  51. */
  52. if (handle_perf_irq(r2))
  53. goto out;
  54. /*
  55. * The same applies to performance counter interrupts. But with the
  56. * above we now know that the reason we got here must be a timer
  57. * interrupt. Being the paranoiacs we are we check anyway.
  58. */
  59. if (!r2 || (read_c0_cause() & (1 << 30))) {
  60. /* Clear Count/Compare Interrupt */
  61. write_c0_compare(read_c0_compare());
  62. cd = &per_cpu(mips_clockevent_device, cpu);
  63. cd->event_handler(cd);
  64. }
  65. out:
  66. return IRQ_HANDLED;
  67. }
  68. #endif /* Not CONFIG_MIPS_MT_SMTC */
  69. struct irqaction c0_compare_irqaction = {
  70. .handler = c0_compare_interrupt,
  71. .flags = IRQF_DISABLED | IRQF_PERCPU | IRQF_TIMER,
  72. .name = "timer",
  73. };
  74. void mips_event_handler(struct clock_event_device *dev)
  75. {
  76. }
  77. /*
  78. * FIXME: This doesn't hold for the relocated E9000 compare interrupt.
  79. */
  80. static int c0_compare_int_pending(void)
  81. {
  82. return (read_c0_cause() >> cp0_compare_irq) & 0x100;
  83. }
  84. /*
  85. * Compare interrupt can be routed and latched outside the core,
  86. * so a single execution hazard barrier may not be enough to give
  87. * it time to clear as seen in the Cause register. 4 time the
  88. * pipeline depth seems reasonably conservative, and empirically
  89. * works better in configurations with high CPU/bus clock ratios.
  90. */
  91. #define compare_change_hazard() \
  92. do { \
  93. irq_disable_hazard(); \
  94. irq_disable_hazard(); \
  95. irq_disable_hazard(); \
  96. irq_disable_hazard(); \
  97. } while (0)
  98. int c0_compare_int_usable(void)
  99. {
  100. unsigned int delta;
  101. unsigned int cnt;
  102. /*
  103. * IP7 already pending? Try to clear it by acking the timer.
  104. */
  105. if (c0_compare_int_pending()) {
  106. write_c0_compare(read_c0_count());
  107. compare_change_hazard();
  108. if (c0_compare_int_pending())
  109. return 0;
  110. }
  111. for (delta = 0x10; delta <= 0x400000; delta <<= 1) {
  112. cnt = read_c0_count();
  113. cnt += delta;
  114. write_c0_compare(cnt);
  115. compare_change_hazard();
  116. if ((int)(read_c0_count() - cnt) < 0)
  117. break;
  118. /* increase delta if the timer was already expired */
  119. }
  120. while ((int)(read_c0_count() - cnt) <= 0)
  121. ; /* Wait for expiry */
  122. compare_change_hazard();
  123. if (!c0_compare_int_pending())
  124. return 0;
  125. write_c0_compare(read_c0_count());
  126. compare_change_hazard();
  127. if (c0_compare_int_pending())
  128. return 0;
  129. /*
  130. * Feels like a real count / compare timer.
  131. */
  132. return 1;
  133. }
  134. #ifndef CONFIG_MIPS_MT_SMTC
  135. int __cpuinit r4k_clockevent_init(void)
  136. {
  137. uint64_t mips_freq = mips_hpt_frequency;
  138. unsigned int cpu = smp_processor_id();
  139. struct clock_event_device *cd;
  140. unsigned int irq;
  141. if (!cpu_has_counter || !mips_hpt_frequency)
  142. return -ENXIO;
  143. if (!c0_compare_int_usable())
  144. return -ENXIO;
  145. /*
  146. * With vectored interrupts things are getting platform specific.
  147. * get_c0_compare_int is a hook to allow a platform to return the
  148. * interrupt number of it's liking.
  149. */
  150. irq = MIPS_CPU_IRQ_BASE + cp0_compare_irq;
  151. if (get_c0_compare_int)
  152. irq = get_c0_compare_int();
  153. cd = &per_cpu(mips_clockevent_device, cpu);
  154. cd->name = "MIPS";
  155. cd->features = CLOCK_EVT_FEAT_ONESHOT;
  156. /* Calculate the min / max delta */
  157. cd->mult = div_sc((unsigned long) mips_freq, NSEC_PER_SEC, 32);
  158. cd->shift = 32;
  159. cd->max_delta_ns = clockevent_delta2ns(0x7fffffff, cd);
  160. cd->min_delta_ns = clockevent_delta2ns(0x300, cd);
  161. cd->rating = 300;
  162. cd->irq = irq;
  163. cd->cpumask = cpumask_of(cpu);
  164. cd->set_next_event = mips_next_event;
  165. cd->set_mode = mips_set_clock_mode;
  166. cd->event_handler = mips_event_handler;
  167. clockevents_register_device(cd);
  168. if (cp0_timer_irq_installed)
  169. return 0;
  170. cp0_timer_irq_installed = 1;
  171. setup_irq(irq, &c0_compare_irqaction);
  172. return 0;
  173. }
  174. #endif /* Not CONFIG_MIPS_MT_SMTC */