1fa5635a2SAndrew Bresticker /*
2fa5635a2SAndrew Bresticker  * This file is subject to the terms and conditions of the GNU General Public
3fa5635a2SAndrew Bresticker  * License.  See the file "COPYING" in the main directory of this archive
4fa5635a2SAndrew Bresticker  * for more details.
5fa5635a2SAndrew Bresticker  *
6fa5635a2SAndrew Bresticker  * Copyright (C) 2012 MIPS Technologies, Inc.  All rights reserved.
7fa5635a2SAndrew Bresticker  */
83ca5768dSMatt Redfearn 
93ca5768dSMatt Redfearn #define pr_fmt(fmt) "mips-gic-timer: " fmt
103ca5768dSMatt Redfearn 
115b4e8453SAndrew Bresticker #include <linux/clk.h>
12a331ce63SAndrew Bresticker #include <linux/clockchips.h>
13e4752dbbSAndrew Bresticker #include <linux/cpu.h>
14fa5635a2SAndrew Bresticker #include <linux/init.h>
15a331ce63SAndrew Bresticker #include <linux/interrupt.h>
16e4752dbbSAndrew Bresticker #include <linux/notifier.h>
17e12aa828SAndrew Bresticker #include <linux/of_irq.h>
18a331ce63SAndrew Bresticker #include <linux/percpu.h>
1948016e78SPaul Burton #include <linux/sched_clock.h>
20a331ce63SAndrew Bresticker #include <linux/smp.h>
21fa5635a2SAndrew Bresticker #include <linux/time.h>
22e07127a0SPaul Burton #include <asm/mips-cps.h>
23fa5635a2SAndrew Bresticker 
245fee56e0SAndrew Bresticker static DEFINE_PER_CPU(struct clock_event_device, gic_clockevent_device);
25e4752dbbSAndrew Bresticker static int gic_timer_irq;
26b0854514SAndrew Bresticker static unsigned int gic_frequency;
27*7d7de1a6SSerge Semin static bool __read_mostly gic_clock_unstable;
28*7d7de1a6SSerge Semin 
29*7d7de1a6SSerge Semin static void gic_clocksource_unstable(char *reason);
30a331ce63SAndrew Bresticker 
3148016e78SPaul Burton static u64 notrace gic_read_count_2x32(void)
32e07127a0SPaul Burton {
33e07127a0SPaul Burton 	unsigned int hi, hi2, lo;
34e07127a0SPaul Burton 
35e07127a0SPaul Burton 	do {
36e07127a0SPaul Burton 		hi = read_gic_counter_32h();
37e07127a0SPaul Burton 		lo = read_gic_counter_32l();
38e07127a0SPaul Burton 		hi2 = read_gic_counter_32h();
39e07127a0SPaul Burton 	} while (hi2 != hi);
40e07127a0SPaul Burton 
41e07127a0SPaul Burton 	return (((u64) hi) << 32) + lo;
42e07127a0SPaul Burton }
43e07127a0SPaul Burton 
4448016e78SPaul Burton static u64 notrace gic_read_count_64(void)
4548016e78SPaul Burton {
4648016e78SPaul Burton 	return read_gic_counter();
4748016e78SPaul Burton }
4848016e78SPaul Burton 
4948016e78SPaul Burton static u64 notrace gic_read_count(void)
5048016e78SPaul Burton {
5148016e78SPaul Burton 	if (mips_cm_is64)
5248016e78SPaul Burton 		return gic_read_count_64();
5348016e78SPaul Burton 
5448016e78SPaul Burton 	return gic_read_count_2x32();
5548016e78SPaul Burton }
5648016e78SPaul Burton 
57a331ce63SAndrew Bresticker static int gic_next_event(unsigned long delta, struct clock_event_device *evt)
58a331ce63SAndrew Bresticker {
59f16ff2bdSMatt Redfearn 	int cpu = cpumask_first(evt->cpumask);
60a331ce63SAndrew Bresticker 	u64 cnt;
61a331ce63SAndrew Bresticker 	int res;
62a331ce63SAndrew Bresticker 
63a331ce63SAndrew Bresticker 	cnt = gic_read_count();
64a331ce63SAndrew Bresticker 	cnt += (u64)delta;
65f16ff2bdSMatt Redfearn 	if (cpu == raw_smp_processor_id()) {
66f16ff2bdSMatt Redfearn 		write_gic_vl_compare(cnt);
67f16ff2bdSMatt Redfearn 	} else {
68f16ff2bdSMatt Redfearn 		write_gic_vl_other(mips_cm_vp_id(cpu));
69e07127a0SPaul Burton 		write_gic_vo_compare(cnt);
70f16ff2bdSMatt Redfearn 	}
71a331ce63SAndrew Bresticker 	res = ((int)(gic_read_count() - cnt) >= 0) ? -ETIME : 0;
72a331ce63SAndrew Bresticker 	return res;
73a331ce63SAndrew Bresticker }
74a331ce63SAndrew Bresticker 
755fee56e0SAndrew Bresticker static irqreturn_t gic_compare_interrupt(int irq, void *dev_id)
76a331ce63SAndrew Bresticker {
77f7ea3060SAndrew Bresticker 	struct clock_event_device *cd = dev_id;
78a331ce63SAndrew Bresticker 
79e07127a0SPaul Burton 	write_gic_vl_compare(read_gic_vl_compare());
80a331ce63SAndrew Bresticker 	cd->event_handler(cd);
81a331ce63SAndrew Bresticker 	return IRQ_HANDLED;
82a331ce63SAndrew Bresticker }
83a331ce63SAndrew Bresticker 
849039de40SYueHaibing static struct irqaction gic_compare_irqaction = {
85a331ce63SAndrew Bresticker 	.handler = gic_compare_interrupt,
86f7ea3060SAndrew Bresticker 	.percpu_dev_id = &gic_clockevent_device,
87a331ce63SAndrew Bresticker 	.flags = IRQF_PERCPU | IRQF_TIMER,
88a331ce63SAndrew Bresticker 	.name = "timer",
89a331ce63SAndrew Bresticker };
90a331ce63SAndrew Bresticker 
912dab9093SRichard Cochran static void gic_clockevent_cpu_init(unsigned int cpu,
922dab9093SRichard Cochran 				    struct clock_event_device *cd)
93a331ce63SAndrew Bresticker {
94a331ce63SAndrew Bresticker 	cd->name		= "MIPS GIC";
95a331ce63SAndrew Bresticker 	cd->features		= CLOCK_EVT_FEAT_ONESHOT |
96a331ce63SAndrew Bresticker 				  CLOCK_EVT_FEAT_C3STOP;
97a331ce63SAndrew Bresticker 
98a45da565SAndrew Bresticker 	cd->rating		= 350;
99e4752dbbSAndrew Bresticker 	cd->irq			= gic_timer_irq;
100a331ce63SAndrew Bresticker 	cd->cpumask		= cpumask_of(cpu);
101a331ce63SAndrew Bresticker 	cd->set_next_event	= gic_next_event;
102a331ce63SAndrew Bresticker 
103b695d8e6SAndrew Bresticker 	clockevents_config_and_register(cd, gic_frequency, 0x300, 0x7fffffff);
104a331ce63SAndrew Bresticker 
105e4752dbbSAndrew Bresticker 	enable_percpu_irq(gic_timer_irq, IRQ_TYPE_NONE);
106a331ce63SAndrew Bresticker }
107a331ce63SAndrew Bresticker 
108e4752dbbSAndrew Bresticker static void gic_clockevent_cpu_exit(struct clock_event_device *cd)
109e4752dbbSAndrew Bresticker {
110e4752dbbSAndrew Bresticker 	disable_percpu_irq(gic_timer_irq);
111e4752dbbSAndrew Bresticker }
112e4752dbbSAndrew Bresticker 
113fc6a6772SEzequiel Garcia static void gic_update_frequency(void *data)
114fc6a6772SEzequiel Garcia {
115fc6a6772SEzequiel Garcia 	unsigned long rate = (unsigned long)data;
116fc6a6772SEzequiel Garcia 
117fc6a6772SEzequiel Garcia 	clockevents_update_freq(this_cpu_ptr(&gic_clockevent_device), rate);
118fc6a6772SEzequiel Garcia }
119fc6a6772SEzequiel Garcia 
1202dab9093SRichard Cochran static int gic_starting_cpu(unsigned int cpu)
121e4752dbbSAndrew Bresticker {
1222dab9093SRichard Cochran 	gic_clockevent_cpu_init(cpu, this_cpu_ptr(&gic_clockevent_device));
1232dab9093SRichard Cochran 	return 0;
124e4752dbbSAndrew Bresticker }
125e4752dbbSAndrew Bresticker 
126fc6a6772SEzequiel Garcia static int gic_clk_notifier(struct notifier_block *nb, unsigned long action,
127fc6a6772SEzequiel Garcia 			    void *data)
128fc6a6772SEzequiel Garcia {
129fc6a6772SEzequiel Garcia 	struct clk_notifier_data *cnd = data;
130fc6a6772SEzequiel Garcia 
131*7d7de1a6SSerge Semin 	if (action == POST_RATE_CHANGE) {
132*7d7de1a6SSerge Semin 		gic_clocksource_unstable("ref clock rate change");
133fc6a6772SEzequiel Garcia 		on_each_cpu(gic_update_frequency, (void *)cnd->new_rate, 1);
134*7d7de1a6SSerge Semin 	}
135fc6a6772SEzequiel Garcia 
136fc6a6772SEzequiel Garcia 	return NOTIFY_OK;
137fc6a6772SEzequiel Garcia }
138fc6a6772SEzequiel Garcia 
1392dab9093SRichard Cochran static int gic_dying_cpu(unsigned int cpu)
1402dab9093SRichard Cochran {
1412dab9093SRichard Cochran 	gic_clockevent_cpu_exit(this_cpu_ptr(&gic_clockevent_device));
1422dab9093SRichard Cochran 	return 0;
1432dab9093SRichard Cochran }
144e4752dbbSAndrew Bresticker 
145fc6a6772SEzequiel Garcia static struct notifier_block gic_clk_nb = {
146fc6a6772SEzequiel Garcia 	.notifier_call = gic_clk_notifier,
147fc6a6772SEzequiel Garcia };
148fc6a6772SEzequiel Garcia 
149e4752dbbSAndrew Bresticker static int gic_clockevent_init(void)
150e4752dbbSAndrew Bresticker {
151f95ac855SEzequiel Garcia 	int ret;
152f95ac855SEzequiel Garcia 
1536982530eSPaul Burton 	if (!gic_frequency)
154e4752dbbSAndrew Bresticker 		return -ENXIO;
155e4752dbbSAndrew Bresticker 
156f95ac855SEzequiel Garcia 	ret = setup_percpu_irq(gic_timer_irq, &gic_compare_irqaction);
1572fd0c93cSPaul Burton 	if (ret < 0) {
1583ca5768dSMatt Redfearn 		pr_err("IRQ %d setup failed (%d)\n", gic_timer_irq, ret);
159f95ac855SEzequiel Garcia 		return ret;
1602fd0c93cSPaul Burton 	}
161e4752dbbSAndrew Bresticker 
1622dab9093SRichard Cochran 	cpuhp_setup_state(CPUHP_AP_MIPS_GIC_TIMER_STARTING,
16373c1b41eSThomas Gleixner 			  "clockevents/mips/gic/timer:starting",
16473c1b41eSThomas Gleixner 			  gic_starting_cpu, gic_dying_cpu);
165a331ce63SAndrew Bresticker 	return 0;
166a331ce63SAndrew Bresticker }
167a331ce63SAndrew Bresticker 
168a5a1d1c2SThomas Gleixner static u64 gic_hpt_read(struct clocksource *cs)
169fa5635a2SAndrew Bresticker {
170fa5635a2SAndrew Bresticker 	return gic_read_count();
171fa5635a2SAndrew Bresticker }
172fa5635a2SAndrew Bresticker 
173fa5635a2SAndrew Bresticker static struct clocksource gic_clocksource = {
174fa5635a2SAndrew Bresticker 	.name			= "GIC",
175fa5635a2SAndrew Bresticker 	.read			= gic_hpt_read,
176fa5635a2SAndrew Bresticker 	.flags			= CLOCK_SOURCE_IS_CONTINUOUS,
177e1bdb22eSThomas Gleixner 	.vdso_clock_mode	= VDSO_CLOCKMODE_GIC,
178fa5635a2SAndrew Bresticker };
179fa5635a2SAndrew Bresticker 
180*7d7de1a6SSerge Semin static void gic_clocksource_unstable(char *reason)
181*7d7de1a6SSerge Semin {
182*7d7de1a6SSerge Semin 	if (gic_clock_unstable)
183*7d7de1a6SSerge Semin 		return;
184*7d7de1a6SSerge Semin 
185*7d7de1a6SSerge Semin 	gic_clock_unstable = true;
186*7d7de1a6SSerge Semin 
187*7d7de1a6SSerge Semin 	pr_info("GIC timer is unstable due to %s\n", reason);
188*7d7de1a6SSerge Semin 
189*7d7de1a6SSerge Semin 	clocksource_mark_unstable(&gic_clocksource);
190*7d7de1a6SSerge Semin }
191*7d7de1a6SSerge Semin 
192d8152bf8SDaniel Lezcano static int __init __gic_clocksource_init(void)
193fa5635a2SAndrew Bresticker {
194e07127a0SPaul Burton 	unsigned int count_width;
195f95ac855SEzequiel Garcia 	int ret;
196f95ac855SEzequiel Garcia 
197fa5635a2SAndrew Bresticker 	/* Set clocksource mask. */
198e07127a0SPaul Burton 	count_width = read_gic_config() & GIC_CONFIG_COUNTBITS;
1995753405eSFelix Fietkau 	count_width >>= __ffs(GIC_CONFIG_COUNTBITS);
200e07127a0SPaul Burton 	count_width *= 4;
201e07127a0SPaul Burton 	count_width += 32;
202e07127a0SPaul Burton 	gic_clocksource.mask = CLOCKSOURCE_MASK(count_width);
203fa5635a2SAndrew Bresticker 
204fa5635a2SAndrew Bresticker 	/* Calculate a somewhat reasonable rating value. */
205e12aa828SAndrew Bresticker 	gic_clocksource.rating = 200 + gic_frequency / 10000000;
206fa5635a2SAndrew Bresticker 
207f95ac855SEzequiel Garcia 	ret = clocksource_register_hz(&gic_clocksource, gic_frequency);
208f95ac855SEzequiel Garcia 	if (ret < 0)
2093ca5768dSMatt Redfearn 		pr_warn("Unable to register clocksource\n");
210d8152bf8SDaniel Lezcano 
211d8152bf8SDaniel Lezcano 	return ret;
212fa5635a2SAndrew Bresticker }
213e12aa828SAndrew Bresticker 
214be5769e2SPaul Gortmaker static int __init gic_clocksource_of_init(struct device_node *node)
215e12aa828SAndrew Bresticker {
2165b4e8453SAndrew Bresticker 	struct clk *clk;
217fc6a6772SEzequiel Garcia 	int ret;
2185b4e8453SAndrew Bresticker 
219e07127a0SPaul Burton 	if (!mips_gic_present() || !node->parent ||
220d8152bf8SDaniel Lezcano 	    !of_device_is_compatible(node->parent, "mti,gic")) {
2213ca5768dSMatt Redfearn 		pr_warn("No DT definition\n");
222d8152bf8SDaniel Lezcano 		return -ENXIO;
223d8152bf8SDaniel Lezcano 	}
224e12aa828SAndrew Bresticker 
2255b4e8453SAndrew Bresticker 	clk = of_clk_get(node, 0);
2265b4e8453SAndrew Bresticker 	if (!IS_ERR(clk)) {
2278c3ecd60SChristophe Jaillet 		ret = clk_prepare_enable(clk);
2288c3ecd60SChristophe Jaillet 		if (ret < 0) {
2293ca5768dSMatt Redfearn 			pr_err("Failed to enable clock\n");
2305b4e8453SAndrew Bresticker 			clk_put(clk);
2318c3ecd60SChristophe Jaillet 			return ret;
232eb811c73SEzequiel Garcia 		}
233eb811c73SEzequiel Garcia 
234eb811c73SEzequiel Garcia 		gic_frequency = clk_get_rate(clk);
2355b4e8453SAndrew Bresticker 	} else if (of_property_read_u32(node, "clock-frequency",
2365b4e8453SAndrew Bresticker 					&gic_frequency)) {
2373ca5768dSMatt Redfearn 		pr_err("Frequency not specified\n");
238ed7158baSIngo Molnar 		return -EINVAL;
239e12aa828SAndrew Bresticker 	}
240e12aa828SAndrew Bresticker 	gic_timer_irq = irq_of_parse_and_map(node, 0);
241e12aa828SAndrew Bresticker 	if (!gic_timer_irq) {
2423ca5768dSMatt Redfearn 		pr_err("IRQ not specified\n");
243ed7158baSIngo Molnar 		return -EINVAL;
244e12aa828SAndrew Bresticker 	}
245e12aa828SAndrew Bresticker 
246d8152bf8SDaniel Lezcano 	ret = __gic_clocksource_init();
247d8152bf8SDaniel Lezcano 	if (ret)
248d8152bf8SDaniel Lezcano 		return ret;
249fc6a6772SEzequiel Garcia 
250fc6a6772SEzequiel Garcia 	ret = gic_clockevent_init();
251fc6a6772SEzequiel Garcia 	if (!ret && !IS_ERR(clk)) {
252fc6a6772SEzequiel Garcia 		if (clk_notifier_register(clk, &gic_clk_nb) < 0)
2533ca5768dSMatt Redfearn 			pr_warn("Unable to register clock notifier\n");
254fc6a6772SEzequiel Garcia 	}
25567d4e669SEzequiel Garcia 
25667d4e669SEzequiel Garcia 	/* And finally start the counter */
257e07127a0SPaul Burton 	clear_gic_config(GIC_CONFIG_COUNTSTOP);
258d8152bf8SDaniel Lezcano 
25948016e78SPaul Burton 	/*
26048016e78SPaul Burton 	 * It's safe to use the MIPS GIC timer as a sched clock source only if
26148016e78SPaul Burton 	 * its ticks are stable, which is true on either the platforms with
26248016e78SPaul Burton 	 * stable CPU frequency or on the platforms with CM3 and CPU frequency
26348016e78SPaul Burton 	 * change performed by the CPC core clocks divider.
26448016e78SPaul Burton 	 */
26548016e78SPaul Burton 	if (mips_cm_revision() >= CM_REV_CM3 || !IS_ENABLED(CONFIG_CPU_FREQ)) {
26648016e78SPaul Burton 		sched_clock_register(mips_cm_is64 ?
26748016e78SPaul Burton 				     gic_read_count_64 : gic_read_count_2x32,
26848016e78SPaul Burton 				     64, gic_frequency);
26948016e78SPaul Burton 	}
27048016e78SPaul Burton 
271d8152bf8SDaniel Lezcano 	return 0;
272e12aa828SAndrew Bresticker }
27317273395SDaniel Lezcano TIMER_OF_DECLARE(mips_gic_timer, "mti,gic-timer",
274e12aa828SAndrew Bresticker 		       gic_clocksource_of_init);
275