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
6 * Copyright (C) 2007 MIPS Technologies, Inc.
7 * Copyright (C) 2007 Ralf Baechle <ralf@linux-mips.org>
9 #include <linux/clockchips.h>
10 #include <linux/interrupt.h>
11 #include <linux/percpu.h>
15 static int mips_next_event(unsigned long delta
,
16 struct clock_event_device
*evt
)
21 #ifdef CONFIG_MIPS_MT_SMTC
23 unsigned long flags
, vpflags
;
24 local_irq_save(flags
);
27 cnt
= read_c0_count();
29 write_c0_compare(cnt
);
30 res
= ((long)(read_c0_count() - cnt
) > 0) ? -ETIME
: 0;
31 #ifdef CONFIG_MIPS_MT_SMTC
33 local_irq_restore(flags
);
39 static void mips_set_mode(enum clock_event_mode mode
,
40 struct clock_event_device
*evt
)
42 /* Nothing to do ... */
45 static DEFINE_PER_CPU(struct clock_event_device
, mips_clockevent_device
);
46 static int cp0_timer_irq_installed
;
49 * Timer ack for an R4k-compatible timer of a known frequency.
51 static void c0_timer_ack(void)
53 write_c0_compare(read_c0_compare());
57 * Possibly handle a performance counter interrupt.
58 * Return true if the timer interrupt should not be checked
60 static inline int handle_perf_irq(int r2
)
63 * The performance counter overflow interrupt may be shared with the
64 * timer interrupt (cp0_perfcount_irq < 0). If it is and a
65 * performance counter has overflowed (perf_irq() == IRQ_HANDLED)
66 * and we can't reliably determine if a counter interrupt has also
67 * happened (!r2) then don't check for a timer interrupt.
69 return (cp0_perfcount_irq
< 0) &&
70 perf_irq() == IRQ_HANDLED
&&
74 static irqreturn_t
c0_compare_interrupt(int irq
, void *dev_id
)
76 const int r2
= cpu_has_mips_r2
;
77 struct clock_event_device
*cd
;
78 int cpu
= smp_processor_id();
82 * Before R2 of the architecture there was no way to see if a
83 * performance counter interrupt was pending, so we have to run
84 * the performance counter interrupt handler anyway.
86 if (handle_perf_irq(r2
))
90 * The same applies to performance counter interrupts. But with the
91 * above we now know that the reason we got here must be a timer
92 * interrupt. Being the paranoiacs we are we check anyway.
94 if (!r2
|| (read_c0_cause() & (1 << 30))) {
96 #ifdef CONFIG_MIPS_MT_SMTC
97 if (cpu_data
[cpu
].vpe_id
)
101 cd
= &per_cpu(mips_clockevent_device
, cpu
);
102 cd
->event_handler(cd
);
109 static struct irqaction c0_compare_irqaction
= {
110 .handler
= c0_compare_interrupt
,
111 #ifdef CONFIG_MIPS_MT_SMTC
112 .flags
= IRQF_DISABLED
,
114 .flags
= IRQF_DISABLED
| IRQF_PERCPU
,
119 #ifdef CONFIG_MIPS_MT_SMTC
120 DEFINE_PER_CPU(struct clock_event_device
, smtc_dummy_clockevent_device
);
122 static void smtc_set_mode(enum clock_event_mode mode
,
123 struct clock_event_device
*evt
)
127 static void mips_broadcast(cpumask_t mask
)
131 for_each_cpu_mask(cpu
, mask
)
132 smtc_send_ipi(cpu
, SMTC_CLOCK_TICK
, 0);
135 static void setup_smtc_dummy_clockevent_device(void)
137 //uint64_t mips_freq = mips_hpt_^frequency;
138 unsigned int cpu
= smp_processor_id();
139 struct clock_event_device
*cd
;
141 cd
= &per_cpu(smtc_dummy_clockevent_device
, cpu
);
144 cd
->features
= CLOCK_EVT_FEAT_DUMMY
;
146 /* Calculate the min / max delta */
147 cd
->mult
= 0; //div_sc((unsigned long) mips_freq, NSEC_PER_SEC, 32);
149 cd
->max_delta_ns
= 0; //clockevent_delta2ns(0x7fffffff, cd);
150 cd
->min_delta_ns
= 0; //clockevent_delta2ns(0x30, cd);
155 // cd->cpumask = CPU_MASK_ALL; // cpumask_of_cpu(cpu);
157 cd
->cpumask
= cpumask_of_cpu(cpu
);
159 cd
->set_mode
= smtc_set_mode
;
161 cd
->broadcast
= mips_broadcast
;
163 clockevents_register_device(cd
);
167 static void mips_event_handler(struct clock_event_device
*dev
)
172 * FIXME: This doesn't hold for the relocated E9000 compare interrupt.
174 static int c0_compare_int_pending(void)
176 return (read_c0_cause() >> cp0_compare_irq
) & 0x100;
179 static int c0_compare_int_usable(void)
181 const unsigned int delta
= 0x300000;
185 * IP7 already pending? Try to clear it by acking the timer.
187 if (c0_compare_int_pending()) {
188 write_c0_compare(read_c0_compare());
189 irq_disable_hazard();
190 if (c0_compare_int_pending())
194 cnt
= read_c0_count();
196 write_c0_compare(cnt
);
198 while ((long)(read_c0_count() - cnt
) <= 0)
199 ; /* Wait for expiry */
201 if (!c0_compare_int_pending())
204 write_c0_compare(read_c0_compare());
205 irq_disable_hazard();
206 if (c0_compare_int_pending())
210 * Feels like a real count / compare timer.
215 void __cpuinit
mips_clockevent_init(void)
217 uint64_t mips_freq
= mips_hpt_frequency
;
218 unsigned int cpu
= smp_processor_id();
219 struct clock_event_device
*cd
;
220 unsigned int irq
= MIPS_CPU_IRQ_BASE
+ 7;
222 if (!cpu_has_counter
)
225 #ifdef CONFIG_MIPS_MT_SMTC
226 setup_smtc_dummy_clockevent_device();
229 * On SMTC we only register VPE0's compare interrupt as clockevent
236 if (!c0_compare_int_usable())
239 cd
= &per_cpu(mips_clockevent_device
, cpu
);
242 cd
->features
= CLOCK_EVT_FEAT_ONESHOT
;
244 /* Calculate the min / max delta */
245 cd
->mult
= div_sc((unsigned long) mips_freq
, NSEC_PER_SEC
, 32);
247 cd
->max_delta_ns
= clockevent_delta2ns(0x7fffffff, cd
);
248 cd
->min_delta_ns
= clockevent_delta2ns(0x300, cd
);
252 #ifdef CONFIG_MIPS_MT_SMTC
253 cd
->cpumask
= CPU_MASK_ALL
;
255 cd
->cpumask
= cpumask_of_cpu(cpu
);
257 cd
->set_next_event
= mips_next_event
;
258 cd
->set_mode
= mips_set_mode
;
259 cd
->event_handler
= mips_event_handler
;
261 clockevents_register_device(cd
);
263 if (!cp0_timer_irq_installed
) {
264 #ifdef CONFIG_MIPS_MT_SMTC
265 #define CPUCTR_IMASKBIT (0x100 << cp0_compare_irq)
266 setup_irq_smtc(irq
, &c0_compare_irqaction
, CPUCTR_IMASKBIT
);
268 setup_irq(irq
, &c0_compare_irqaction
);
269 #endif /* CONFIG_MIPS_MT_SMTC */
270 cp0_timer_irq_installed
= 1;