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) 2012 MIPS Technologies, Inc. All rights reserved.
8 #include <linux/clockchips.h>
9 #include <linux/init.h>
10 #include <linux/interrupt.h>
11 #include <linux/irqchip/mips-gic.h>
12 #include <linux/percpu.h>
13 #include <linux/smp.h>
14 #include <linux/time.h>
18 static DEFINE_PER_CPU(struct clock_event_device
, gic_clockevent_device
);
19 static int gic_timer_irq_installed
;
21 static int gic_next_event(unsigned long delta
, struct clock_event_device
*evt
)
26 cnt
= gic_read_count();
28 gic_write_cpu_compare(cnt
, cpumask_first(evt
->cpumask
));
29 res
= ((int)(gic_read_count() - cnt
) >= 0) ? -ETIME
: 0;
33 static void gic_set_clock_mode(enum clock_event_mode mode
,
34 struct clock_event_device
*evt
)
36 /* Nothing to do ... */
39 static irqreturn_t
gic_compare_interrupt(int irq
, void *dev_id
)
41 struct clock_event_device
*cd
;
42 int cpu
= smp_processor_id();
44 gic_write_compare(gic_read_compare());
45 cd
= &per_cpu(gic_clockevent_device
, cpu
);
46 cd
->event_handler(cd
);
50 struct irqaction gic_compare_irqaction
= {
51 .handler
= gic_compare_interrupt
,
52 .flags
= IRQF_PERCPU
| IRQF_TIMER
,
56 static void gic_event_handler(struct clock_event_device
*dev
)
60 int gic_clockevent_init(void)
62 unsigned int cpu
= smp_processor_id();
63 struct clock_event_device
*cd
;
66 if (!cpu_has_counter
|| !gic_frequency
)
69 irq
= MIPS_GIC_IRQ_BASE
+ GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_COMPARE
);
71 cd
= &per_cpu(gic_clockevent_device
, cpu
);
73 cd
->name
= "MIPS GIC";
74 cd
->features
= CLOCK_EVT_FEAT_ONESHOT
|
75 CLOCK_EVT_FEAT_C3STOP
;
77 clockevent_set_clock(cd
, gic_frequency
);
79 /* Calculate the min / max delta */
80 cd
->max_delta_ns
= clockevent_delta2ns(0x7fffffff, cd
);
81 cd
->min_delta_ns
= clockevent_delta2ns(0x300, cd
);
85 cd
->cpumask
= cpumask_of(cpu
);
86 cd
->set_next_event
= gic_next_event
;
87 cd
->set_mode
= gic_set_clock_mode
;
88 cd
->event_handler
= gic_event_handler
;
90 clockevents_register_device(cd
);
92 if (!gic_timer_irq_installed
) {
93 setup_percpu_irq(irq
, &gic_compare_irqaction
);
94 gic_timer_irq_installed
= 1;
97 enable_percpu_irq(irq
, IRQ_TYPE_NONE
);
102 static cycle_t
gic_hpt_read(struct clocksource
*cs
)
104 return gic_read_count();
107 static struct clocksource gic_clocksource
= {
109 .read
= gic_hpt_read
,
110 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
113 void __init
gic_clocksource_init(unsigned int frequency
)
115 /* Set clocksource mask. */
116 gic_clocksource
.mask
= CLOCKSOURCE_MASK(gic_get_count_width());
118 /* Calculate a somewhat reasonable rating value. */
119 gic_clocksource
.rating
= 200 + frequency
/ 10000000;
121 clocksource_register_hz(&gic_clocksource
, frequency
);