cevt-r4k.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. static int mips_next_event(unsigned long delta,
  15. struct clock_event_device *evt)
  16. {
  17. unsigned int cnt;
  18. int res;
  19. #ifdef CONFIG_MIPS_MT_SMTC
  20. {
  21. unsigned long flags, vpflags;
  22. local_irq_save(flags);
  23. vpflags = dvpe();
  24. #endif
  25. cnt = read_c0_count();
  26. cnt += delta;
  27. write_c0_compare(cnt);
  28. res = ((int)(read_c0_count() - cnt) > 0) ? -ETIME : 0;
  29. #ifdef CONFIG_MIPS_MT_SMTC
  30. evpe(vpflags);
  31. local_irq_restore(flags);
  32. }
  33. #endif
  34. return res;
  35. }
  36. static void mips_set_mode(enum clock_event_mode mode,
  37. struct clock_event_device *evt)
  38. {
  39. /* Nothing to do ... */
  40. }
  41. static DEFINE_PER_CPU(struct clock_event_device, mips_clockevent_device);
  42. static int cp0_timer_irq_installed;
  43. /*
  44. * Timer ack for an R4k-compatible timer of a known frequency.
  45. */
  46. static void c0_timer_ack(void)
  47. {
  48. write_c0_compare(read_c0_compare());
  49. }
  50. /*
  51. * Possibly handle a performance counter interrupt.
  52. * Return true if the timer interrupt should not be checked
  53. */
  54. static inline int handle_perf_irq(int r2)
  55. {
  56. /*
  57. * The performance counter overflow interrupt may be shared with the
  58. * timer interrupt (cp0_perfcount_irq < 0). If it is and a
  59. * performance counter has overflowed (perf_irq() == IRQ_HANDLED)
  60. * and we can't reliably determine if a counter interrupt has also
  61. * happened (!r2) then don't check for a timer interrupt.
  62. */
  63. return (cp0_perfcount_irq < 0) &&
  64. perf_irq() == IRQ_HANDLED &&
  65. !r2;
  66. }
  67. static irqreturn_t c0_compare_interrupt(int irq, void *dev_id)
  68. {
  69. const int r2 = cpu_has_mips_r2;
  70. struct clock_event_device *cd;
  71. int cpu = smp_processor_id();
  72. /*
  73. * Suckage alert:
  74. * Before R2 of the architecture there was no way to see if a
  75. * performance counter interrupt was pending, so we have to run
  76. * the performance counter interrupt handler anyway.
  77. */
  78. if (handle_perf_irq(r2))
  79. goto out;
  80. /*
  81. * The same applies to performance counter interrupts. But with the
  82. * above we now know that the reason we got here must be a timer
  83. * interrupt. Being the paranoiacs we are we check anyway.
  84. */
  85. if (!r2 || (read_c0_cause() & (1 << 30))) {
  86. c0_timer_ack();
  87. #ifdef CONFIG_MIPS_MT_SMTC
  88. if (cpu_data[cpu].vpe_id)
  89. goto out;
  90. cpu = 0;
  91. #endif
  92. cd = &per_cpu(mips_clockevent_device, cpu);
  93. cd->event_handler(cd);
  94. }
  95. out:
  96. return IRQ_HANDLED;
  97. }
  98. static struct irqaction c0_compare_irqaction = {
  99. .handler = c0_compare_interrupt,
  100. #ifdef CONFIG_MIPS_MT_SMTC
  101. .flags = IRQF_DISABLED,
  102. #else
  103. .flags = IRQF_DISABLED | IRQF_PERCPU,
  104. #endif
  105. .name = "timer",
  106. };
  107. #ifdef CONFIG_MIPS_MT_SMTC
  108. DEFINE_PER_CPU(struct clock_event_device, smtc_dummy_clockevent_device);
  109. static void smtc_set_mode(enum clock_event_mode mode,
  110. struct clock_event_device *evt)
  111. {
  112. }
  113. static void mips_broadcast(cpumask_t mask)
  114. {
  115. unsigned int cpu;
  116. for_each_cpu_mask(cpu, mask)
  117. smtc_send_ipi(cpu, SMTC_CLOCK_TICK, 0);
  118. }
  119. static void setup_smtc_dummy_clockevent_device(void)
  120. {
  121. //uint64_t mips_freq = mips_hpt_^frequency;
  122. unsigned int cpu = smp_processor_id();
  123. struct clock_event_device *cd;
  124. cd = &per_cpu(smtc_dummy_clockevent_device, cpu);
  125. cd->name = "SMTC";
  126. cd->features = CLOCK_EVT_FEAT_DUMMY;
  127. /* Calculate the min / max delta */
  128. cd->mult = 0; //div_sc((unsigned long) mips_freq, NSEC_PER_SEC, 32);
  129. cd->shift = 0; //32;
  130. cd->max_delta_ns = 0; //clockevent_delta2ns(0x7fffffff, cd);
  131. cd->min_delta_ns = 0; //clockevent_delta2ns(0x30, cd);
  132. cd->rating = 200;
  133. cd->irq = 17; //-1;
  134. // if (cpu)
  135. // cd->cpumask = CPU_MASK_ALL; // cpumask_of_cpu(cpu);
  136. // else
  137. cd->cpumask = cpumask_of_cpu(cpu);
  138. cd->set_mode = smtc_set_mode;
  139. cd->broadcast = mips_broadcast;
  140. clockevents_register_device(cd);
  141. }
  142. #endif
  143. static void mips_event_handler(struct clock_event_device *dev)
  144. {
  145. }
  146. /*
  147. * FIXME: This doesn't hold for the relocated E9000 compare interrupt.
  148. */
  149. static int c0_compare_int_pending(void)
  150. {
  151. return (read_c0_cause() >> cp0_compare_irq) & 0x100;
  152. }
  153. static int c0_compare_int_usable(void)
  154. {
  155. unsigned int delta;
  156. unsigned int cnt;
  157. /*
  158. * IP7 already pending? Try to clear it by acking the timer.
  159. */
  160. if (c0_compare_int_pending()) {
  161. write_c0_compare(read_c0_count());
  162. irq_disable_hazard();
  163. if (c0_compare_int_pending())
  164. return 0;
  165. }
  166. for (delta = 0x10; delta <= 0x400000; delta <<= 1) {
  167. cnt = read_c0_count();
  168. cnt += delta;
  169. write_c0_compare(cnt);
  170. irq_disable_hazard();
  171. if ((int)(read_c0_count() - cnt) < 0)
  172. break;
  173. /* increase delta if the timer was already expired */
  174. }
  175. while ((int)(read_c0_count() - cnt) <= 0)
  176. ; /* Wait for expiry */
  177. if (!c0_compare_int_pending())
  178. return 0;
  179. write_c0_compare(read_c0_count());
  180. irq_disable_hazard();
  181. if (c0_compare_int_pending())
  182. return 0;
  183. /*
  184. * Feels like a real count / compare timer.
  185. */
  186. return 1;
  187. }
  188. int __cpuinit mips_clockevent_init(void)
  189. {
  190. uint64_t mips_freq = mips_hpt_frequency;
  191. unsigned int cpu = smp_processor_id();
  192. struct clock_event_device *cd;
  193. unsigned int irq;
  194. if (!cpu_has_counter || !mips_hpt_frequency)
  195. return -ENXIO;
  196. #ifdef CONFIG_MIPS_MT_SMTC
  197. setup_smtc_dummy_clockevent_device();
  198. /*
  199. * On SMTC we only register VPE0's compare interrupt as clockevent
  200. * device.
  201. */
  202. if (cpu)
  203. return 0;
  204. #endif
  205. if (!c0_compare_int_usable())
  206. return -ENXIO;
  207. /*
  208. * With vectored interrupts things are getting platform specific.
  209. * get_c0_compare_int is a hook to allow a platform to return the
  210. * interrupt number of it's liking.
  211. */
  212. irq = MIPS_CPU_IRQ_BASE + cp0_compare_irq;
  213. if (get_c0_compare_int)
  214. irq = get_c0_compare_int();
  215. cd = &per_cpu(mips_clockevent_device, cpu);
  216. cd->name = "MIPS";
  217. cd->features = CLOCK_EVT_FEAT_ONESHOT;
  218. /* Calculate the min / max delta */
  219. cd->mult = div_sc((unsigned long) mips_freq, NSEC_PER_SEC, 32);
  220. cd->shift = 32;
  221. cd->max_delta_ns = clockevent_delta2ns(0x7fffffff, cd);
  222. cd->min_delta_ns = clockevent_delta2ns(0x300, cd);
  223. cd->rating = 300;
  224. cd->irq = irq;
  225. #ifdef CONFIG_MIPS_MT_SMTC
  226. cd->cpumask = CPU_MASK_ALL;
  227. #else
  228. cd->cpumask = cpumask_of_cpu(cpu);
  229. #endif
  230. cd->set_next_event = mips_next_event;
  231. cd->set_mode = mips_set_mode;
  232. cd->event_handler = mips_event_handler;
  233. clockevents_register_device(cd);
  234. if (cp0_timer_irq_installed)
  235. return 0;
  236. cp0_timer_irq_installed = 1;
  237. #ifdef CONFIG_MIPS_MT_SMTC
  238. #define CPUCTR_IMASKBIT (0x100 << cp0_compare_irq)
  239. setup_irq_smtc(irq, &c0_compare_irqaction, CPUCTR_IMASKBIT);
  240. #else
  241. setup_irq(irq, &c0_compare_irqaction);
  242. #endif
  243. return 0;
  244. }