blob: ae3167c28b129b6c3d3cc0fbc5c4a0da1a9abacd [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 Bresticker5b4e8452015-02-23 18:28:34 -08008#include <linux/clk.h>
Andrew Brestickera331ce62014-10-20 12:03:59 -07009#include <linux/clockchips.h>
Andrew Brestickere4752db2014-10-20 12:04:04 -070010#include <linux/cpu.h>
Steven J. Hill778eeb12012-12-07 03:51:04 +000011#include <linux/init.h>
Andrew Brestickera331ce62014-10-20 12:03:59 -070012#include <linux/interrupt.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>
Paul Burtone07127a2017-08-12 21:36:11 -070018#include <asm/mips-cps.h>
Steven J. Hill778eeb12012-12-07 03:51:04 +000019
Andrew Bresticker5fee56e2014-10-20 12:04:00 -070020static DEFINE_PER_CPU(struct clock_event_device, gic_clockevent_device);
Andrew Brestickere4752db2014-10-20 12:04:04 -070021static int gic_timer_irq;
Andrew Brestickerb0854512014-10-20 12:04:01 -070022static unsigned int gic_frequency;
Andrew Brestickera331ce62014-10-20 12:03:59 -070023
Paul Burtone07127a2017-08-12 21:36:11 -070024static u64 notrace gic_read_count(void)
25{
26 unsigned int hi, hi2, lo;
27
28 if (mips_cm_is64)
29 return read_gic_counter();
30
31 do {
32 hi = read_gic_counter_32h();
33 lo = read_gic_counter_32l();
34 hi2 = read_gic_counter_32h();
35 } while (hi2 != hi);
36
37 return (((u64) hi) << 32) + lo;
38}
39
Andrew Brestickera331ce62014-10-20 12:03:59 -070040static int gic_next_event(unsigned long delta, struct clock_event_device *evt)
41{
Paul Burtone07127a2017-08-12 21:36:11 -070042 unsigned long flags;
Andrew Brestickera331ce62014-10-20 12:03:59 -070043 u64 cnt;
44 int res;
45
46 cnt = gic_read_count();
47 cnt += (u64)delta;
Paul Burtone07127a2017-08-12 21:36:11 -070048 local_irq_save(flags);
49 write_gic_vl_other(mips_cm_vp_id(cpumask_first(evt->cpumask)));
50 write_gic_vo_compare(cnt);
51 local_irq_restore(flags);
Andrew Brestickera331ce62014-10-20 12:03:59 -070052 res = ((int)(gic_read_count() - cnt) >= 0) ? -ETIME : 0;
53 return res;
54}
55
Andrew Bresticker5fee56e2014-10-20 12:04:00 -070056static irqreturn_t gic_compare_interrupt(int irq, void *dev_id)
Andrew Brestickera331ce62014-10-20 12:03:59 -070057{
Andrew Brestickerf7ea3062014-10-20 12:04:03 -070058 struct clock_event_device *cd = dev_id;
Andrew Brestickera331ce62014-10-20 12:03:59 -070059
Paul Burtone07127a2017-08-12 21:36:11 -070060 write_gic_vl_compare(read_gic_vl_compare());
Andrew Brestickera331ce62014-10-20 12:03:59 -070061 cd->event_handler(cd);
62 return IRQ_HANDLED;
63}
64
65struct irqaction gic_compare_irqaction = {
66 .handler = gic_compare_interrupt,
Andrew Brestickerf7ea3062014-10-20 12:04:03 -070067 .percpu_dev_id = &gic_clockevent_device,
Andrew Brestickera331ce62014-10-20 12:03:59 -070068 .flags = IRQF_PERCPU | IRQF_TIMER,
69 .name = "timer",
70};
71
Richard Cochran2dab9092016-07-13 17:16:44 +000072static void gic_clockevent_cpu_init(unsigned int cpu,
73 struct clock_event_device *cd)
Andrew Brestickera331ce62014-10-20 12:03:59 -070074{
Andrew Brestickera331ce62014-10-20 12:03:59 -070075 cd->name = "MIPS GIC";
76 cd->features = CLOCK_EVT_FEAT_ONESHOT |
77 CLOCK_EVT_FEAT_C3STOP;
78
Andrew Brestickera45da562014-10-20 12:04:06 -070079 cd->rating = 350;
Andrew Brestickere4752db2014-10-20 12:04:04 -070080 cd->irq = gic_timer_irq;
Andrew Brestickera331ce62014-10-20 12:03:59 -070081 cd->cpumask = cpumask_of(cpu);
82 cd->set_next_event = gic_next_event;
Andrew Brestickera331ce62014-10-20 12:03:59 -070083
Andrew Brestickerb695d8e2014-10-20 12:04:05 -070084 clockevents_config_and_register(cd, gic_frequency, 0x300, 0x7fffffff);
Andrew Brestickera331ce62014-10-20 12:03:59 -070085
Andrew Brestickere4752db2014-10-20 12:04:04 -070086 enable_percpu_irq(gic_timer_irq, IRQ_TYPE_NONE);
87}
88
89static void gic_clockevent_cpu_exit(struct clock_event_device *cd)
90{
91 disable_percpu_irq(gic_timer_irq);
92}
93
Ezequiel Garciafc6a6772015-07-27 15:00:15 +010094static void gic_update_frequency(void *data)
95{
96 unsigned long rate = (unsigned long)data;
97
98 clockevents_update_freq(this_cpu_ptr(&gic_clockevent_device), rate);
99}
100
Richard Cochran2dab9092016-07-13 17:16:44 +0000101static int gic_starting_cpu(unsigned int cpu)
Andrew Brestickere4752db2014-10-20 12:04:04 -0700102{
Richard Cochran2dab9092016-07-13 17:16:44 +0000103 gic_clockevent_cpu_init(cpu, this_cpu_ptr(&gic_clockevent_device));
104 return 0;
Andrew Brestickere4752db2014-10-20 12:04:04 -0700105}
106
Ezequiel Garciafc6a6772015-07-27 15:00:15 +0100107static int gic_clk_notifier(struct notifier_block *nb, unsigned long action,
108 void *data)
109{
110 struct clk_notifier_data *cnd = data;
111
112 if (action == POST_RATE_CHANGE)
113 on_each_cpu(gic_update_frequency, (void *)cnd->new_rate, 1);
114
115 return NOTIFY_OK;
116}
117
Richard Cochran2dab9092016-07-13 17:16:44 +0000118static int gic_dying_cpu(unsigned int cpu)
119{
120 gic_clockevent_cpu_exit(this_cpu_ptr(&gic_clockevent_device));
121 return 0;
122}
Andrew Brestickere4752db2014-10-20 12:04:04 -0700123
Ezequiel Garciafc6a6772015-07-27 15:00:15 +0100124static struct notifier_block gic_clk_nb = {
125 .notifier_call = gic_clk_notifier,
126};
127
Andrew Brestickere4752db2014-10-20 12:04:04 -0700128static int gic_clockevent_init(void)
129{
Ezequiel Garciaf95ac852015-07-27 15:00:13 +0100130 int ret;
131
Paul Burton69825302016-09-13 17:56:44 +0100132 if (!gic_frequency)
Andrew Brestickere4752db2014-10-20 12:04:04 -0700133 return -ENXIO;
134
Ezequiel Garciaf95ac852015-07-27 15:00:13 +0100135 ret = setup_percpu_irq(gic_timer_irq, &gic_compare_irqaction);
Paul Burton2fd0c932016-09-13 17:56:43 +0100136 if (ret < 0) {
137 pr_err("GIC timer IRQ %d setup failed: %d\n",
138 gic_timer_irq, ret);
Ezequiel Garciaf95ac852015-07-27 15:00:13 +0100139 return ret;
Paul Burton2fd0c932016-09-13 17:56:43 +0100140 }
Andrew Brestickere4752db2014-10-20 12:04:04 -0700141
Richard Cochran2dab9092016-07-13 17:16:44 +0000142 cpuhp_setup_state(CPUHP_AP_MIPS_GIC_TIMER_STARTING,
Thomas Gleixner73c1b412016-12-21 20:19:54 +0100143 "clockevents/mips/gic/timer:starting",
144 gic_starting_cpu, gic_dying_cpu);
Andrew Brestickera331ce62014-10-20 12:03:59 -0700145 return 0;
146}
147
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100148static u64 gic_hpt_read(struct clocksource *cs)
Steven J. Hill778eeb12012-12-07 03:51:04 +0000149{
Steven J. Hilldfa762e2013-04-10 16:28:36 -0500150 return gic_read_count();
Steven J. Hill778eeb12012-12-07 03:51:04 +0000151}
152
153static struct clocksource gic_clocksource = {
Alex Smitha7f4df42015-10-21 09:57:44 +0100154 .name = "GIC",
155 .read = gic_hpt_read,
156 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
157 .archdata = { .vdso_clock_mode = VDSO_CLOCK_GIC },
Steven J. Hill778eeb12012-12-07 03:51:04 +0000158};
159
Daniel Lezcanod8152bf2016-06-06 17:57:25 +0200160static int __init __gic_clocksource_init(void)
Steven J. Hill778eeb12012-12-07 03:51:04 +0000161{
Paul Burtone07127a2017-08-12 21:36:11 -0700162 unsigned int count_width;
Ezequiel Garciaf95ac852015-07-27 15:00:13 +0100163 int ret;
164
Steven J. Hill778eeb12012-12-07 03:51:04 +0000165 /* Set clocksource mask. */
Paul Burtone07127a2017-08-12 21:36:11 -0700166 count_width = read_gic_config() & GIC_CONFIG_COUNTBITS;
167 count_width >>= __fls(GIC_CONFIG_COUNTBITS);
168 count_width *= 4;
169 count_width += 32;
170 gic_clocksource.mask = CLOCKSOURCE_MASK(count_width);
Steven J. Hill778eeb12012-12-07 03:51:04 +0000171
172 /* Calculate a somewhat reasonable rating value. */
Andrew Brestickere12aa822014-11-12 11:43:39 -0800173 gic_clocksource.rating = 200 + gic_frequency / 10000000;
Steven J. Hill778eeb12012-12-07 03:51:04 +0000174
Ezequiel Garciaf95ac852015-07-27 15:00:13 +0100175 ret = clocksource_register_hz(&gic_clocksource, gic_frequency);
176 if (ret < 0)
177 pr_warn("GIC: Unable to register clocksource\n");
Daniel Lezcanod8152bf2016-06-06 17:57:25 +0200178
179 return ret;
Steven J. Hill778eeb12012-12-07 03:51:04 +0000180}
Andrew Brestickere12aa822014-11-12 11:43:39 -0800181
Paul Gortmakerbe5769e2016-08-17 12:21:35 +0200182static int __init gic_clocksource_of_init(struct device_node *node)
Andrew Brestickere12aa822014-11-12 11:43:39 -0800183{
Andrew Bresticker5b4e8452015-02-23 18:28:34 -0800184 struct clk *clk;
Ezequiel Garciafc6a6772015-07-27 15:00:15 +0100185 int ret;
Andrew Bresticker5b4e8452015-02-23 18:28:34 -0800186
Paul Burtone07127a2017-08-12 21:36:11 -0700187 if (!mips_gic_present() || !node->parent ||
Daniel Lezcanod8152bf2016-06-06 17:57:25 +0200188 !of_device_is_compatible(node->parent, "mti,gic")) {
Rafał Miłeckiac9ce6d2017-03-09 10:47:10 +0100189 pr_warn("No DT definition for the mips gic driver\n");
Daniel Lezcanod8152bf2016-06-06 17:57:25 +0200190 return -ENXIO;
191 }
Andrew Brestickere12aa822014-11-12 11:43:39 -0800192
Andrew Bresticker5b4e8452015-02-23 18:28:34 -0800193 clk = of_clk_get(node, 0);
194 if (!IS_ERR(clk)) {
Christophe Jaillet8c3ecd62017-06-23 21:55:10 +0200195 ret = clk_prepare_enable(clk);
196 if (ret < 0) {
Ezequiel Garciaeb811c72015-07-27 15:00:12 +0100197 pr_err("GIC failed to enable clock\n");
198 clk_put(clk);
Christophe Jaillet8c3ecd62017-06-23 21:55:10 +0200199 return ret;
Ezequiel Garciaeb811c72015-07-27 15:00:12 +0100200 }
201
Andrew Bresticker5b4e8452015-02-23 18:28:34 -0800202 gic_frequency = clk_get_rate(clk);
Andrew Bresticker5b4e8452015-02-23 18:28:34 -0800203 } else if (of_property_read_u32(node, "clock-frequency",
204 &gic_frequency)) {
Andrew Brestickere12aa822014-11-12 11:43:39 -0800205 pr_err("GIC frequency not specified.\n");
Daniel Lezcanod8152bf2016-06-06 17:57:25 +0200206 return -EINVAL;;
Andrew Brestickere12aa822014-11-12 11:43:39 -0800207 }
208 gic_timer_irq = irq_of_parse_and_map(node, 0);
209 if (!gic_timer_irq) {
210 pr_err("GIC timer IRQ not specified.\n");
Daniel Lezcanod8152bf2016-06-06 17:57:25 +0200211 return -EINVAL;;
Andrew Brestickere12aa822014-11-12 11:43:39 -0800212 }
213
Daniel Lezcanod8152bf2016-06-06 17:57:25 +0200214 ret = __gic_clocksource_init();
215 if (ret)
216 return ret;
Ezequiel Garciafc6a6772015-07-27 15:00:15 +0100217
218 ret = gic_clockevent_init();
219 if (!ret && !IS_ERR(clk)) {
220 if (clk_notifier_register(clk, &gic_clk_nb) < 0)
221 pr_warn("GIC: Unable to register clock notifier\n");
222 }
Ezequiel Garcia67d4e662015-07-27 15:00:14 +0100223
224 /* And finally start the counter */
Paul Burtone07127a2017-08-12 21:36:11 -0700225 clear_gic_config(GIC_CONFIG_COUNTSTOP);
Daniel Lezcanod8152bf2016-06-06 17:57:25 +0200226
227 return 0;
Andrew Brestickere12aa822014-11-12 11:43:39 -0800228}
Daniel Lezcano17273392017-05-26 16:56:11 +0200229TIMER_OF_DECLARE(mips_gic_timer, "mti,gic-timer",
Andrew Brestickere12aa822014-11-12 11:43:39 -0800230 gic_clocksource_of_init);