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  */
8*3ca5768dSMatt Redfearn 
9*3ca5768dSMatt Redfearn #define pr_fmt(fmt) "mips-gic-timer: " fmt
10*3ca5768dSMatt 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>
19a331ce63SAndrew Bresticker #include <linux/smp.h>
20fa5635a2SAndrew Bresticker #include <linux/time.h>
21e07127a0SPaul Burton #include <asm/mips-cps.h>
22fa5635a2SAndrew Bresticker 
235fee56e0SAndrew Bresticker static DEFINE_PER_CPU(struct clock_event_device, gic_clockevent_device);
24e4752dbbSAndrew Bresticker static int gic_timer_irq;
25b0854514SAndrew Bresticker static unsigned int gic_frequency;
26a331ce63SAndrew Bresticker 
27e07127a0SPaul Burton static u64 notrace gic_read_count(void)
28e07127a0SPaul Burton {
29e07127a0SPaul Burton 	unsigned int hi, hi2, lo;
30e07127a0SPaul Burton 
31e07127a0SPaul Burton 	if (mips_cm_is64)
32e07127a0SPaul Burton 		return read_gic_counter();
33e07127a0SPaul Burton 
34e07127a0SPaul Burton 	do {
35e07127a0SPaul Burton 		hi = read_gic_counter_32h();
36e07127a0SPaul Burton 		lo = read_gic_counter_32l();
37e07127a0SPaul Burton 		hi2 = read_gic_counter_32h();
38e07127a0SPaul Burton 	} while (hi2 != hi);
39e07127a0SPaul Burton 
40e07127a0SPaul Burton 	return (((u64) hi) << 32) + lo;
41e07127a0SPaul Burton }
42e07127a0SPaul Burton 
43a331ce63SAndrew Bresticker static int gic_next_event(unsigned long delta, struct clock_event_device *evt)
44a331ce63SAndrew Bresticker {
45f16ff2bdSMatt Redfearn 	int cpu = cpumask_first(evt->cpumask);
46a331ce63SAndrew Bresticker 	u64 cnt;
47a331ce63SAndrew Bresticker 	int res;
48a331ce63SAndrew Bresticker 
49a331ce63SAndrew Bresticker 	cnt = gic_read_count();
50a331ce63SAndrew Bresticker 	cnt += (u64)delta;
51f16ff2bdSMatt Redfearn 	if (cpu == raw_smp_processor_id()) {
52f16ff2bdSMatt Redfearn 		write_gic_vl_compare(cnt);
53f16ff2bdSMatt Redfearn 	} else {
54f16ff2bdSMatt Redfearn 		write_gic_vl_other(mips_cm_vp_id(cpu));
55e07127a0SPaul Burton 		write_gic_vo_compare(cnt);
56f16ff2bdSMatt Redfearn 	}
57a331ce63SAndrew Bresticker 	res = ((int)(gic_read_count() - cnt) >= 0) ? -ETIME : 0;
58a331ce63SAndrew Bresticker 	return res;
59a331ce63SAndrew Bresticker }
60a331ce63SAndrew Bresticker 
615fee56e0SAndrew Bresticker static irqreturn_t gic_compare_interrupt(int irq, void *dev_id)
62a331ce63SAndrew Bresticker {
63f7ea3060SAndrew Bresticker 	struct clock_event_device *cd = dev_id;
64a331ce63SAndrew Bresticker 
65e07127a0SPaul Burton 	write_gic_vl_compare(read_gic_vl_compare());
66a331ce63SAndrew Bresticker 	cd->event_handler(cd);
67a331ce63SAndrew Bresticker 	return IRQ_HANDLED;
68a331ce63SAndrew Bresticker }
69a331ce63SAndrew Bresticker 
70a331ce63SAndrew Bresticker struct irqaction gic_compare_irqaction = {
71a331ce63SAndrew Bresticker 	.handler = gic_compare_interrupt,
72f7ea3060SAndrew Bresticker 	.percpu_dev_id = &gic_clockevent_device,
73a331ce63SAndrew Bresticker 	.flags = IRQF_PERCPU | IRQF_TIMER,
74a331ce63SAndrew Bresticker 	.name = "timer",
75a331ce63SAndrew Bresticker };
76a331ce63SAndrew Bresticker 
772dab9093SRichard Cochran static void gic_clockevent_cpu_init(unsigned int cpu,
782dab9093SRichard Cochran 				    struct clock_event_device *cd)
79a331ce63SAndrew Bresticker {
80a331ce63SAndrew Bresticker 	cd->name		= "MIPS GIC";
81a331ce63SAndrew Bresticker 	cd->features		= CLOCK_EVT_FEAT_ONESHOT |
82a331ce63SAndrew Bresticker 				  CLOCK_EVT_FEAT_C3STOP;
83a331ce63SAndrew Bresticker 
84a45da565SAndrew Bresticker 	cd->rating		= 350;
85e4752dbbSAndrew Bresticker 	cd->irq			= gic_timer_irq;
86a331ce63SAndrew Bresticker 	cd->cpumask		= cpumask_of(cpu);
87a331ce63SAndrew Bresticker 	cd->set_next_event	= gic_next_event;
88a331ce63SAndrew Bresticker 
89b695d8e6SAndrew Bresticker 	clockevents_config_and_register(cd, gic_frequency, 0x300, 0x7fffffff);
90a331ce63SAndrew Bresticker 
91e4752dbbSAndrew Bresticker 	enable_percpu_irq(gic_timer_irq, IRQ_TYPE_NONE);
92a331ce63SAndrew Bresticker }
93a331ce63SAndrew Bresticker 
94e4752dbbSAndrew Bresticker static void gic_clockevent_cpu_exit(struct clock_event_device *cd)
95e4752dbbSAndrew Bresticker {
96e4752dbbSAndrew Bresticker 	disable_percpu_irq(gic_timer_irq);
97e4752dbbSAndrew Bresticker }
98e4752dbbSAndrew Bresticker 
99fc6a6772SEzequiel Garcia static void gic_update_frequency(void *data)
100fc6a6772SEzequiel Garcia {
101fc6a6772SEzequiel Garcia 	unsigned long rate = (unsigned long)data;
102fc6a6772SEzequiel Garcia 
103fc6a6772SEzequiel Garcia 	clockevents_update_freq(this_cpu_ptr(&gic_clockevent_device), rate);
104fc6a6772SEzequiel Garcia }
105fc6a6772SEzequiel Garcia 
1062dab9093SRichard Cochran static int gic_starting_cpu(unsigned int cpu)
107e4752dbbSAndrew Bresticker {
1082dab9093SRichard Cochran 	gic_clockevent_cpu_init(cpu, this_cpu_ptr(&gic_clockevent_device));
1092dab9093SRichard Cochran 	return 0;
110e4752dbbSAndrew Bresticker }
111e4752dbbSAndrew Bresticker 
112fc6a6772SEzequiel Garcia static int gic_clk_notifier(struct notifier_block *nb, unsigned long action,
113fc6a6772SEzequiel Garcia 			    void *data)
114fc6a6772SEzequiel Garcia {
115fc6a6772SEzequiel Garcia 	struct clk_notifier_data *cnd = data;
116fc6a6772SEzequiel Garcia 
117fc6a6772SEzequiel Garcia 	if (action == POST_RATE_CHANGE)
118fc6a6772SEzequiel Garcia 		on_each_cpu(gic_update_frequency, (void *)cnd->new_rate, 1);
119fc6a6772SEzequiel Garcia 
120fc6a6772SEzequiel Garcia 	return NOTIFY_OK;
121fc6a6772SEzequiel Garcia }
122fc6a6772SEzequiel Garcia 
1232dab9093SRichard Cochran static int gic_dying_cpu(unsigned int cpu)
1242dab9093SRichard Cochran {
1252dab9093SRichard Cochran 	gic_clockevent_cpu_exit(this_cpu_ptr(&gic_clockevent_device));
1262dab9093SRichard Cochran 	return 0;
1272dab9093SRichard Cochran }
128e4752dbbSAndrew Bresticker 
129fc6a6772SEzequiel Garcia static struct notifier_block gic_clk_nb = {
130fc6a6772SEzequiel Garcia 	.notifier_call = gic_clk_notifier,
131fc6a6772SEzequiel Garcia };
132fc6a6772SEzequiel Garcia 
133e4752dbbSAndrew Bresticker static int gic_clockevent_init(void)
134e4752dbbSAndrew Bresticker {
135f95ac855SEzequiel Garcia 	int ret;
136f95ac855SEzequiel Garcia 
1376982530eSPaul Burton 	if (!gic_frequency)
138e4752dbbSAndrew Bresticker 		return -ENXIO;
139e4752dbbSAndrew Bresticker 
140f95ac855SEzequiel Garcia 	ret = setup_percpu_irq(gic_timer_irq, &gic_compare_irqaction);
1412fd0c93cSPaul Burton 	if (ret < 0) {
142*3ca5768dSMatt Redfearn 		pr_err("IRQ %d setup failed (%d)\n", gic_timer_irq, ret);
143f95ac855SEzequiel Garcia 		return ret;
1442fd0c93cSPaul Burton 	}
145e4752dbbSAndrew Bresticker 
1462dab9093SRichard Cochran 	cpuhp_setup_state(CPUHP_AP_MIPS_GIC_TIMER_STARTING,
14773c1b41eSThomas Gleixner 			  "clockevents/mips/gic/timer:starting",
14873c1b41eSThomas Gleixner 			  gic_starting_cpu, gic_dying_cpu);
149a331ce63SAndrew Bresticker 	return 0;
150a331ce63SAndrew Bresticker }
151a331ce63SAndrew Bresticker 
152a5a1d1c2SThomas Gleixner static u64 gic_hpt_read(struct clocksource *cs)
153fa5635a2SAndrew Bresticker {
154fa5635a2SAndrew Bresticker 	return gic_read_count();
155fa5635a2SAndrew Bresticker }
156fa5635a2SAndrew Bresticker 
157fa5635a2SAndrew Bresticker static struct clocksource gic_clocksource = {
158fa5635a2SAndrew Bresticker 	.name		= "GIC",
159fa5635a2SAndrew Bresticker 	.read		= gic_hpt_read,
160fa5635a2SAndrew Bresticker 	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
161a7f4df4eSAlex Smith 	.archdata	= { .vdso_clock_mode = VDSO_CLOCK_GIC },
162fa5635a2SAndrew Bresticker };
163fa5635a2SAndrew Bresticker 
164d8152bf8SDaniel Lezcano static int __init __gic_clocksource_init(void)
165fa5635a2SAndrew Bresticker {
166e07127a0SPaul Burton 	unsigned int count_width;
167f95ac855SEzequiel Garcia 	int ret;
168f95ac855SEzequiel Garcia 
169fa5635a2SAndrew Bresticker 	/* Set clocksource mask. */
170e07127a0SPaul Burton 	count_width = read_gic_config() & GIC_CONFIG_COUNTBITS;
1715753405eSFelix Fietkau 	count_width >>= __ffs(GIC_CONFIG_COUNTBITS);
172e07127a0SPaul Burton 	count_width *= 4;
173e07127a0SPaul Burton 	count_width += 32;
174e07127a0SPaul Burton 	gic_clocksource.mask = CLOCKSOURCE_MASK(count_width);
175fa5635a2SAndrew Bresticker 
176fa5635a2SAndrew Bresticker 	/* Calculate a somewhat reasonable rating value. */
177e12aa828SAndrew Bresticker 	gic_clocksource.rating = 200 + gic_frequency / 10000000;
178fa5635a2SAndrew Bresticker 
179f95ac855SEzequiel Garcia 	ret = clocksource_register_hz(&gic_clocksource, gic_frequency);
180f95ac855SEzequiel Garcia 	if (ret < 0)
181*3ca5768dSMatt Redfearn 		pr_warn("Unable to register clocksource\n");
182d8152bf8SDaniel Lezcano 
183d8152bf8SDaniel Lezcano 	return ret;
184fa5635a2SAndrew Bresticker }
185e12aa828SAndrew Bresticker 
186be5769e2SPaul Gortmaker static int __init gic_clocksource_of_init(struct device_node *node)
187e12aa828SAndrew Bresticker {
1885b4e8453SAndrew Bresticker 	struct clk *clk;
189fc6a6772SEzequiel Garcia 	int ret;
1905b4e8453SAndrew Bresticker 
191e07127a0SPaul Burton 	if (!mips_gic_present() || !node->parent ||
192d8152bf8SDaniel Lezcano 	    !of_device_is_compatible(node->parent, "mti,gic")) {
193*3ca5768dSMatt Redfearn 		pr_warn("No DT definition\n");
194d8152bf8SDaniel Lezcano 		return -ENXIO;
195d8152bf8SDaniel Lezcano 	}
196e12aa828SAndrew Bresticker 
1975b4e8453SAndrew Bresticker 	clk = of_clk_get(node, 0);
1985b4e8453SAndrew Bresticker 	if (!IS_ERR(clk)) {
1998c3ecd60SChristophe Jaillet 		ret = clk_prepare_enable(clk);
2008c3ecd60SChristophe Jaillet 		if (ret < 0) {
201*3ca5768dSMatt Redfearn 			pr_err("Failed to enable clock\n");
2025b4e8453SAndrew Bresticker 			clk_put(clk);
2038c3ecd60SChristophe Jaillet 			return ret;
204eb811c73SEzequiel Garcia 		}
205eb811c73SEzequiel Garcia 
206eb811c73SEzequiel Garcia 		gic_frequency = clk_get_rate(clk);
2075b4e8453SAndrew Bresticker 	} else if (of_property_read_u32(node, "clock-frequency",
2085b4e8453SAndrew Bresticker 					&gic_frequency)) {
209*3ca5768dSMatt Redfearn 		pr_err("Frequency not specified\n");
210ed7158baSIngo Molnar 		return -EINVAL;
211e12aa828SAndrew Bresticker 	}
212e12aa828SAndrew Bresticker 	gic_timer_irq = irq_of_parse_and_map(node, 0);
213e12aa828SAndrew Bresticker 	if (!gic_timer_irq) {
214*3ca5768dSMatt Redfearn 		pr_err("IRQ not specified\n");
215ed7158baSIngo Molnar 		return -EINVAL;
216e12aa828SAndrew Bresticker 	}
217e12aa828SAndrew Bresticker 
218d8152bf8SDaniel Lezcano 	ret = __gic_clocksource_init();
219d8152bf8SDaniel Lezcano 	if (ret)
220d8152bf8SDaniel Lezcano 		return ret;
221fc6a6772SEzequiel Garcia 
222fc6a6772SEzequiel Garcia 	ret = gic_clockevent_init();
223fc6a6772SEzequiel Garcia 	if (!ret && !IS_ERR(clk)) {
224fc6a6772SEzequiel Garcia 		if (clk_notifier_register(clk, &gic_clk_nb) < 0)
225*3ca5768dSMatt Redfearn 			pr_warn("Unable to register clock notifier\n");
226fc6a6772SEzequiel Garcia 	}
22767d4e669SEzequiel Garcia 
22867d4e669SEzequiel Garcia 	/* And finally start the counter */
229e07127a0SPaul Burton 	clear_gic_config(GIC_CONFIG_COUNTSTOP);
230d8152bf8SDaniel Lezcano 
231d8152bf8SDaniel Lezcano 	return 0;
232e12aa828SAndrew Bresticker }
23317273395SDaniel Lezcano TIMER_OF_DECLARE(mips_gic_timer, "mti,gic-timer",
234e12aa828SAndrew Bresticker 		       gic_clocksource_of_init);
235