blob: 22a4daf5f0c8bd0344c86149d138c07e313556d3 [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 Bresticker4060bbe2014-10-20 12:03:53 -070013#include <linux/irqchip/mips-gic.h>
Andrew Brestickere4752db2014-10-20 12:04:04 -070014#include <linux/notifier.h>
Andrew Brestickere12aa822014-11-12 11:43:39 -080015#include <linux/of_irq.h>
Andrew Brestickera331ce62014-10-20 12:03:59 -070016#include <linux/percpu.h>
17#include <linux/smp.h>
Steven J. Hilldfa762e2013-04-10 16:28:36 -050018#include <linux/time.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
24static int gic_next_event(unsigned long delta, struct clock_event_device *evt)
25{
26 u64 cnt;
27 int res;
28
29 cnt = gic_read_count();
30 cnt += (u64)delta;
31 gic_write_cpu_compare(cnt, cpumask_first(evt->cpumask));
32 res = ((int)(gic_read_count() - cnt) >= 0) ? -ETIME : 0;
33 return res;
34}
35
Andrew Bresticker5fee56e2014-10-20 12:04:00 -070036static void gic_set_clock_mode(enum clock_event_mode mode,
Andrew Brestickera331ce62014-10-20 12:03:59 -070037 struct clock_event_device *evt)
38{
39 /* Nothing to do ... */
40}
41
Andrew Bresticker5fee56e2014-10-20 12:04:00 -070042static irqreturn_t gic_compare_interrupt(int irq, void *dev_id)
Andrew Brestickera331ce62014-10-20 12:03:59 -070043{
Andrew Brestickerf7ea3062014-10-20 12:04:03 -070044 struct clock_event_device *cd = dev_id;
Andrew Brestickera331ce62014-10-20 12:03:59 -070045
46 gic_write_compare(gic_read_compare());
Andrew Brestickera331ce62014-10-20 12:03:59 -070047 cd->event_handler(cd);
48 return IRQ_HANDLED;
49}
50
51struct irqaction gic_compare_irqaction = {
52 .handler = gic_compare_interrupt,
Andrew Brestickerf7ea3062014-10-20 12:04:03 -070053 .percpu_dev_id = &gic_clockevent_device,
Andrew Brestickera331ce62014-10-20 12:03:59 -070054 .flags = IRQF_PERCPU | IRQF_TIMER,
55 .name = "timer",
56};
57
Andrew Brestickere4752db2014-10-20 12:04:04 -070058static void gic_clockevent_cpu_init(struct clock_event_device *cd)
Andrew Brestickera331ce62014-10-20 12:03:59 -070059{
60 unsigned int cpu = smp_processor_id();
Andrew Brestickera331ce62014-10-20 12:03:59 -070061
62 cd->name = "MIPS GIC";
63 cd->features = CLOCK_EVT_FEAT_ONESHOT |
64 CLOCK_EVT_FEAT_C3STOP;
65
Andrew Brestickera45da562014-10-20 12:04:06 -070066 cd->rating = 350;
Andrew Brestickere4752db2014-10-20 12:04:04 -070067 cd->irq = gic_timer_irq;
Andrew Brestickera331ce62014-10-20 12:03:59 -070068 cd->cpumask = cpumask_of(cpu);
69 cd->set_next_event = gic_next_event;
70 cd->set_mode = gic_set_clock_mode;
Andrew Brestickera331ce62014-10-20 12:03:59 -070071
Andrew Brestickerb695d8e2014-10-20 12:04:05 -070072 clockevents_config_and_register(cd, gic_frequency, 0x300, 0x7fffffff);
Andrew Brestickera331ce62014-10-20 12:03:59 -070073
Andrew Brestickere4752db2014-10-20 12:04:04 -070074 enable_percpu_irq(gic_timer_irq, IRQ_TYPE_NONE);
75}
76
77static void gic_clockevent_cpu_exit(struct clock_event_device *cd)
78{
79 disable_percpu_irq(gic_timer_irq);
80}
81
82static int gic_cpu_notifier(struct notifier_block *nb, unsigned long action,
83 void *data)
84{
85 switch (action & ~CPU_TASKS_FROZEN) {
86 case CPU_STARTING:
87 gic_clockevent_cpu_init(this_cpu_ptr(&gic_clockevent_device));
88 break;
89 case CPU_DYING:
90 gic_clockevent_cpu_exit(this_cpu_ptr(&gic_clockevent_device));
91 break;
Andrew Brestickera331ce62014-10-20 12:03:59 -070092 }
93
Andrew Brestickere4752db2014-10-20 12:04:04 -070094 return NOTIFY_OK;
95}
96
97static struct notifier_block gic_cpu_nb = {
98 .notifier_call = gic_cpu_notifier,
99};
100
101static int gic_clockevent_init(void)
102{
Ezequiel Garciaf95ac852015-07-27 15:00:13 +0100103 int ret;
104
Andrew Brestickere4752db2014-10-20 12:04:04 -0700105 if (!cpu_has_counter || !gic_frequency)
106 return -ENXIO;
107
Ezequiel Garciaf95ac852015-07-27 15:00:13 +0100108 ret = setup_percpu_irq(gic_timer_irq, &gic_compare_irqaction);
109 if (ret < 0)
110 return ret;
Andrew Brestickere4752db2014-10-20 12:04:04 -0700111
Ezequiel Garciaf95ac852015-07-27 15:00:13 +0100112 ret = register_cpu_notifier(&gic_cpu_nb);
113 if (ret < 0)
114 pr_warn("GIC: Unable to register CPU notifier\n");
Andrew Brestickere4752db2014-10-20 12:04:04 -0700115
116 gic_clockevent_cpu_init(this_cpu_ptr(&gic_clockevent_device));
Andrew Brestickera331ce62014-10-20 12:03:59 -0700117
118 return 0;
119}
120
Steven J. Hill778eeb12012-12-07 03:51:04 +0000121static cycle_t gic_hpt_read(struct clocksource *cs)
122{
Steven J. Hilldfa762e2013-04-10 16:28:36 -0500123 return gic_read_count();
Steven J. Hill778eeb12012-12-07 03:51:04 +0000124}
125
126static struct clocksource gic_clocksource = {
127 .name = "GIC",
128 .read = gic_hpt_read,
129 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
130};
131
Andrew Brestickere12aa822014-11-12 11:43:39 -0800132static void __init __gic_clocksource_init(void)
Steven J. Hill778eeb12012-12-07 03:51:04 +0000133{
Ezequiel Garciaf95ac852015-07-27 15:00:13 +0100134 int ret;
135
Steven J. Hill778eeb12012-12-07 03:51:04 +0000136 /* Set clocksource mask. */
Andrew Bresticker387904f2014-10-20 12:03:49 -0700137 gic_clocksource.mask = CLOCKSOURCE_MASK(gic_get_count_width());
Steven J. Hill778eeb12012-12-07 03:51:04 +0000138
139 /* Calculate a somewhat reasonable rating value. */
Andrew Brestickere12aa822014-11-12 11:43:39 -0800140 gic_clocksource.rating = 200 + gic_frequency / 10000000;
Steven J. Hill778eeb12012-12-07 03:51:04 +0000141
Ezequiel Garciaf95ac852015-07-27 15:00:13 +0100142 ret = clocksource_register_hz(&gic_clocksource, gic_frequency);
143 if (ret < 0)
144 pr_warn("GIC: Unable to register clocksource\n");
Steven J. Hill778eeb12012-12-07 03:51:04 +0000145}
Andrew Brestickere12aa822014-11-12 11:43:39 -0800146
147void __init gic_clocksource_init(unsigned int frequency)
148{
149 gic_frequency = frequency;
150 gic_timer_irq = MIPS_GIC_IRQ_BASE +
151 GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_COMPARE);
152
153 __gic_clocksource_init();
Ezequiel Garcia67d4e662015-07-27 15:00:14 +0100154 gic_clockevent_init();
155
156 /* And finally start the counter */
157 gic_start_count();
Andrew Brestickere12aa822014-11-12 11:43:39 -0800158}
159
160static void __init gic_clocksource_of_init(struct device_node *node)
161{
Andrew Bresticker5b4e8452015-02-23 18:28:34 -0800162 struct clk *clk;
163
Andrew Brestickere12aa822014-11-12 11:43:39 -0800164 if (WARN_ON(!gic_present || !node->parent ||
165 !of_device_is_compatible(node->parent, "mti,gic")))
166 return;
167
Andrew Bresticker5b4e8452015-02-23 18:28:34 -0800168 clk = of_clk_get(node, 0);
169 if (!IS_ERR(clk)) {
Ezequiel Garciaeb811c72015-07-27 15:00:12 +0100170 if (clk_prepare_enable(clk) < 0) {
171 pr_err("GIC failed to enable clock\n");
172 clk_put(clk);
173 return;
174 }
175
Andrew Bresticker5b4e8452015-02-23 18:28:34 -0800176 gic_frequency = clk_get_rate(clk);
Andrew Bresticker5b4e8452015-02-23 18:28:34 -0800177 } else if (of_property_read_u32(node, "clock-frequency",
178 &gic_frequency)) {
Andrew Brestickere12aa822014-11-12 11:43:39 -0800179 pr_err("GIC frequency not specified.\n");
180 return;
181 }
182 gic_timer_irq = irq_of_parse_and_map(node, 0);
183 if (!gic_timer_irq) {
184 pr_err("GIC timer IRQ not specified.\n");
185 return;
186 }
187
188 __gic_clocksource_init();
Ezequiel Garcia67d4e662015-07-27 15:00:14 +0100189 gic_clockevent_init();
190
191 /* And finally start the counter */
192 gic_start_count();
Andrew Brestickere12aa822014-11-12 11:43:39 -0800193}
194CLOCKSOURCE_OF_DECLARE(mips_gic_timer, "mti,gic-timer",
195 gic_clocksource_of_init);