blob: 3bd31b1321f689188116ae56a5f95f64341dfc7e [file] [log] [blame]
Steven J. Hill778eeb12012-12-07 03:51:04 +00001/*
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) 2012 MIPS Technologies, Inc. All rights reserved.
7 */
Andrew Brestickera331ce62014-10-20 12:03:59 -07008#include <linux/clockchips.h>
Andrew Brestickere4752db2014-10-20 12:04:04 -07009#include <linux/cpu.h>
Steven J. Hill778eeb12012-12-07 03:51:04 +000010#include <linux/init.h>
Andrew Brestickera331ce62014-10-20 12:03:59 -070011#include <linux/interrupt.h>
Andrew Bresticker4060bbe2014-10-20 12:03:53 -070012#include <linux/irqchip/mips-gic.h>
Andrew Brestickere4752db2014-10-20 12:04:04 -070013#include <linux/notifier.h>
Andrew Brestickere12aa822014-11-12 11:43:39 -080014#include <linux/of_irq.h>
Andrew Brestickera331ce62014-10-20 12:03:59 -070015#include <linux/percpu.h>
16#include <linux/smp.h>
Steven J. Hilldfa762e2013-04-10 16:28:36 -050017#include <linux/time.h>
Steven J. Hill778eeb12012-12-07 03:51:04 +000018
Andrew Bresticker5fee56e2014-10-20 12:04:00 -070019static DEFINE_PER_CPU(struct clock_event_device, gic_clockevent_device);
Andrew Brestickere4752db2014-10-20 12:04:04 -070020static int gic_timer_irq;
Andrew Brestickerb0854512014-10-20 12:04:01 -070021static unsigned int gic_frequency;
Andrew Brestickera331ce62014-10-20 12:03:59 -070022
23static int gic_next_event(unsigned long delta, struct clock_event_device *evt)
24{
25 u64 cnt;
26 int res;
27
28 cnt = gic_read_count();
29 cnt += (u64)delta;
30 gic_write_cpu_compare(cnt, cpumask_first(evt->cpumask));
31 res = ((int)(gic_read_count() - cnt) >= 0) ? -ETIME : 0;
32 return res;
33}
34
Andrew Bresticker5fee56e2014-10-20 12:04:00 -070035static void gic_set_clock_mode(enum clock_event_mode mode,
Andrew Brestickera331ce62014-10-20 12:03:59 -070036 struct clock_event_device *evt)
37{
38 /* Nothing to do ... */
39}
40
Andrew Bresticker5fee56e2014-10-20 12:04:00 -070041static irqreturn_t gic_compare_interrupt(int irq, void *dev_id)
Andrew Brestickera331ce62014-10-20 12:03:59 -070042{
Andrew Brestickerf7ea3062014-10-20 12:04:03 -070043 struct clock_event_device *cd = dev_id;
Andrew Brestickera331ce62014-10-20 12:03:59 -070044
45 gic_write_compare(gic_read_compare());
Andrew Brestickera331ce62014-10-20 12:03:59 -070046 cd->event_handler(cd);
47 return IRQ_HANDLED;
48}
49
50struct irqaction gic_compare_irqaction = {
51 .handler = gic_compare_interrupt,
Andrew Brestickerf7ea3062014-10-20 12:04:03 -070052 .percpu_dev_id = &gic_clockevent_device,
Andrew Brestickera331ce62014-10-20 12:03:59 -070053 .flags = IRQF_PERCPU | IRQF_TIMER,
54 .name = "timer",
55};
56
Andrew Brestickere4752db2014-10-20 12:04:04 -070057static void gic_clockevent_cpu_init(struct clock_event_device *cd)
Andrew Brestickera331ce62014-10-20 12:03:59 -070058{
59 unsigned int cpu = smp_processor_id();
Andrew Brestickera331ce62014-10-20 12:03:59 -070060
61 cd->name = "MIPS GIC";
62 cd->features = CLOCK_EVT_FEAT_ONESHOT |
63 CLOCK_EVT_FEAT_C3STOP;
64
Andrew Brestickera45da562014-10-20 12:04:06 -070065 cd->rating = 350;
Andrew Brestickere4752db2014-10-20 12:04:04 -070066 cd->irq = gic_timer_irq;
Andrew Brestickera331ce62014-10-20 12:03:59 -070067 cd->cpumask = cpumask_of(cpu);
68 cd->set_next_event = gic_next_event;
69 cd->set_mode = gic_set_clock_mode;
Andrew Brestickera331ce62014-10-20 12:03:59 -070070
Andrew Brestickerb695d8e2014-10-20 12:04:05 -070071 clockevents_config_and_register(cd, gic_frequency, 0x300, 0x7fffffff);
Andrew Brestickera331ce62014-10-20 12:03:59 -070072
Andrew Brestickere4752db2014-10-20 12:04:04 -070073 enable_percpu_irq(gic_timer_irq, IRQ_TYPE_NONE);
74}
75
76static void gic_clockevent_cpu_exit(struct clock_event_device *cd)
77{
78 disable_percpu_irq(gic_timer_irq);
79}
80
81static int gic_cpu_notifier(struct notifier_block *nb, unsigned long action,
82 void *data)
83{
84 switch (action & ~CPU_TASKS_FROZEN) {
85 case CPU_STARTING:
86 gic_clockevent_cpu_init(this_cpu_ptr(&gic_clockevent_device));
87 break;
88 case CPU_DYING:
89 gic_clockevent_cpu_exit(this_cpu_ptr(&gic_clockevent_device));
90 break;
Andrew Brestickera331ce62014-10-20 12:03:59 -070091 }
92
Andrew Brestickere4752db2014-10-20 12:04:04 -070093 return NOTIFY_OK;
94}
95
96static struct notifier_block gic_cpu_nb = {
97 .notifier_call = gic_cpu_notifier,
98};
99
100static int gic_clockevent_init(void)
101{
102 if (!cpu_has_counter || !gic_frequency)
103 return -ENXIO;
104
Andrew Brestickere4752db2014-10-20 12:04:04 -0700105 setup_percpu_irq(gic_timer_irq, &gic_compare_irqaction);
106
107 register_cpu_notifier(&gic_cpu_nb);
108
109 gic_clockevent_cpu_init(this_cpu_ptr(&gic_clockevent_device));
Andrew Brestickera331ce62014-10-20 12:03:59 -0700110
111 return 0;
112}
113
Steven J. Hill778eeb12012-12-07 03:51:04 +0000114static cycle_t gic_hpt_read(struct clocksource *cs)
115{
Steven J. Hilldfa762e2013-04-10 16:28:36 -0500116 return gic_read_count();
Steven J. Hill778eeb12012-12-07 03:51:04 +0000117}
118
119static struct clocksource gic_clocksource = {
120 .name = "GIC",
121 .read = gic_hpt_read,
122 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
123};
124
Andrew Brestickere12aa822014-11-12 11:43:39 -0800125static void __init __gic_clocksource_init(void)
Steven J. Hill778eeb12012-12-07 03:51:04 +0000126{
Steven J. Hill778eeb12012-12-07 03:51:04 +0000127 /* Set clocksource mask. */
Andrew Bresticker387904f2014-10-20 12:03:49 -0700128 gic_clocksource.mask = CLOCKSOURCE_MASK(gic_get_count_width());
Steven J. Hill778eeb12012-12-07 03:51:04 +0000129
130 /* Calculate a somewhat reasonable rating value. */
Andrew Brestickere12aa822014-11-12 11:43:39 -0800131 gic_clocksource.rating = 200 + gic_frequency / 10000000;
Steven J. Hill778eeb12012-12-07 03:51:04 +0000132
Andrew Brestickere12aa822014-11-12 11:43:39 -0800133 clocksource_register_hz(&gic_clocksource, gic_frequency);
Andrew Brestickere4752db2014-10-20 12:04:04 -0700134
135 gic_clockevent_init();
Steven J. Hill778eeb12012-12-07 03:51:04 +0000136}
Andrew Brestickere12aa822014-11-12 11:43:39 -0800137
138void __init gic_clocksource_init(unsigned int frequency)
139{
140 gic_frequency = frequency;
141 gic_timer_irq = MIPS_GIC_IRQ_BASE +
142 GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_COMPARE);
143
144 __gic_clocksource_init();
145}
146
147static void __init gic_clocksource_of_init(struct device_node *node)
148{
149 if (WARN_ON(!gic_present || !node->parent ||
150 !of_device_is_compatible(node->parent, "mti,gic")))
151 return;
152
153 if (of_property_read_u32(node, "clock-frequency", &gic_frequency)) {
154 pr_err("GIC frequency not specified.\n");
155 return;
156 }
157 gic_timer_irq = irq_of_parse_and_map(node, 0);
158 if (!gic_timer_irq) {
159 pr_err("GIC timer IRQ not specified.\n");
160 return;
161 }
162
163 __gic_clocksource_init();
164}
165CLOCKSOURCE_OF_DECLARE(mips_gic_timer, "mti,gic-timer",
166 gic_clocksource_of_init);