cevt-r4k.c 5.0 KB

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