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