CLOCKSOURCE: mips-gic: Update clockevent frequency on clock rate changes
[deliverable/linux.git] / drivers / clocksource / mips-gic-timer.c
CommitLineData
778eeb1b
SH
1/*
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 */
5b4e8453 8#include <linux/clk.h>
a331ce63 9#include <linux/clockchips.h>
e4752dbb 10#include <linux/cpu.h>
778eeb1b 11#include <linux/init.h>
a331ce63 12#include <linux/interrupt.h>
4060bbe9 13#include <linux/irqchip/mips-gic.h>
e4752dbb 14#include <linux/notifier.h>
e12aa828 15#include <linux/of_irq.h>
a331ce63
AB
16#include <linux/percpu.h>
17#include <linux/smp.h>
dfa762e1 18#include <linux/time.h>
778eeb1b 19
5fee56e0 20static DEFINE_PER_CPU(struct clock_event_device, gic_clockevent_device);
e4752dbb 21static int gic_timer_irq;
b0854514 22static unsigned int gic_frequency;
a331ce63
AB
23
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
5fee56e0 36static void gic_set_clock_mode(enum clock_event_mode mode,
a331ce63
AB
37 struct clock_event_device *evt)
38{
39 /* Nothing to do ... */
40}
41
5fee56e0 42static irqreturn_t gic_compare_interrupt(int irq, void *dev_id)
a331ce63 43{
f7ea3060 44 struct clock_event_device *cd = dev_id;
a331ce63
AB
45
46 gic_write_compare(gic_read_compare());
a331ce63
AB
47 cd->event_handler(cd);
48 return IRQ_HANDLED;
49}
50
51struct irqaction gic_compare_irqaction = {
52 .handler = gic_compare_interrupt,
f7ea3060 53 .percpu_dev_id = &gic_clockevent_device,
a331ce63
AB
54 .flags = IRQF_PERCPU | IRQF_TIMER,
55 .name = "timer",
56};
57
e4752dbb 58static void gic_clockevent_cpu_init(struct clock_event_device *cd)
a331ce63
AB
59{
60 unsigned int cpu = smp_processor_id();
a331ce63
AB
61
62 cd->name = "MIPS GIC";
63 cd->features = CLOCK_EVT_FEAT_ONESHOT |
64 CLOCK_EVT_FEAT_C3STOP;
65
a45da565 66 cd->rating = 350;
e4752dbb 67 cd->irq = gic_timer_irq;
a331ce63
AB
68 cd->cpumask = cpumask_of(cpu);
69 cd->set_next_event = gic_next_event;
70 cd->set_mode = gic_set_clock_mode;
a331ce63 71
b695d8e6 72 clockevents_config_and_register(cd, gic_frequency, 0x300, 0x7fffffff);
a331ce63 73
e4752dbb
AB
74 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
fc6a6772
EG
82static void gic_update_frequency(void *data)
83{
84 unsigned long rate = (unsigned long)data;
85
86 clockevents_update_freq(this_cpu_ptr(&gic_clockevent_device), rate);
87}
88
e4752dbb
AB
89static int gic_cpu_notifier(struct notifier_block *nb, unsigned long action,
90 void *data)
91{
92 switch (action & ~CPU_TASKS_FROZEN) {
93 case CPU_STARTING:
94 gic_clockevent_cpu_init(this_cpu_ptr(&gic_clockevent_device));
95 break;
96 case CPU_DYING:
97 gic_clockevent_cpu_exit(this_cpu_ptr(&gic_clockevent_device));
98 break;
a331ce63
AB
99 }
100
e4752dbb
AB
101 return NOTIFY_OK;
102}
103
fc6a6772
EG
104static int gic_clk_notifier(struct notifier_block *nb, unsigned long action,
105 void *data)
106{
107 struct clk_notifier_data *cnd = data;
108
109 if (action == POST_RATE_CHANGE)
110 on_each_cpu(gic_update_frequency, (void *)cnd->new_rate, 1);
111
112 return NOTIFY_OK;
113}
114
115
e4752dbb
AB
116static struct notifier_block gic_cpu_nb = {
117 .notifier_call = gic_cpu_notifier,
118};
119
fc6a6772
EG
120static struct notifier_block gic_clk_nb = {
121 .notifier_call = gic_clk_notifier,
122};
123
e4752dbb
AB
124static int gic_clockevent_init(void)
125{
f95ac855
EG
126 int ret;
127
e4752dbb
AB
128 if (!cpu_has_counter || !gic_frequency)
129 return -ENXIO;
130
f95ac855
EG
131 ret = setup_percpu_irq(gic_timer_irq, &gic_compare_irqaction);
132 if (ret < 0)
133 return ret;
e4752dbb 134
f95ac855
EG
135 ret = register_cpu_notifier(&gic_cpu_nb);
136 if (ret < 0)
137 pr_warn("GIC: Unable to register CPU notifier\n");
e4752dbb
AB
138
139 gic_clockevent_cpu_init(this_cpu_ptr(&gic_clockevent_device));
a331ce63
AB
140
141 return 0;
142}
143
778eeb1b
SH
144static cycle_t gic_hpt_read(struct clocksource *cs)
145{
dfa762e1 146 return gic_read_count();
778eeb1b
SH
147}
148
149static struct clocksource gic_clocksource = {
150 .name = "GIC",
151 .read = gic_hpt_read,
152 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
153};
154
e12aa828 155static void __init __gic_clocksource_init(void)
778eeb1b 156{
f95ac855
EG
157 int ret;
158
778eeb1b 159 /* Set clocksource mask. */
387904ff 160 gic_clocksource.mask = CLOCKSOURCE_MASK(gic_get_count_width());
778eeb1b
SH
161
162 /* Calculate a somewhat reasonable rating value. */
e12aa828 163 gic_clocksource.rating = 200 + gic_frequency / 10000000;
778eeb1b 164
f95ac855
EG
165 ret = clocksource_register_hz(&gic_clocksource, gic_frequency);
166 if (ret < 0)
167 pr_warn("GIC: Unable to register clocksource\n");
778eeb1b 168}
e12aa828
AB
169
170void __init gic_clocksource_init(unsigned int frequency)
171{
172 gic_frequency = frequency;
173 gic_timer_irq = MIPS_GIC_IRQ_BASE +
174 GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_COMPARE);
175
176 __gic_clocksource_init();
67d4e669
EG
177 gic_clockevent_init();
178
179 /* And finally start the counter */
180 gic_start_count();
e12aa828
AB
181}
182
183static void __init gic_clocksource_of_init(struct device_node *node)
184{
5b4e8453 185 struct clk *clk;
fc6a6772 186 int ret;
5b4e8453 187
e12aa828
AB
188 if (WARN_ON(!gic_present || !node->parent ||
189 !of_device_is_compatible(node->parent, "mti,gic")))
190 return;
191
5b4e8453
AB
192 clk = of_clk_get(node, 0);
193 if (!IS_ERR(clk)) {
eb811c73
EG
194 if (clk_prepare_enable(clk) < 0) {
195 pr_err("GIC failed to enable clock\n");
196 clk_put(clk);
197 return;
198 }
199
5b4e8453 200 gic_frequency = clk_get_rate(clk);
5b4e8453
AB
201 } else if (of_property_read_u32(node, "clock-frequency",
202 &gic_frequency)) {
e12aa828
AB
203 pr_err("GIC frequency not specified.\n");
204 return;
205 }
206 gic_timer_irq = irq_of_parse_and_map(node, 0);
207 if (!gic_timer_irq) {
208 pr_err("GIC timer IRQ not specified.\n");
209 return;
210 }
211
212 __gic_clocksource_init();
fc6a6772
EG
213
214 ret = gic_clockevent_init();
215 if (!ret && !IS_ERR(clk)) {
216 if (clk_notifier_register(clk, &gic_clk_nb) < 0)
217 pr_warn("GIC: Unable to register clock notifier\n");
218 }
67d4e669
EG
219
220 /* And finally start the counter */
221 gic_start_count();
e12aa828
AB
222}
223CLOCKSOURCE_OF_DECLARE(mips_gic_timer, "mti,gic-timer",
224 gic_clocksource_of_init);
This page took 0.14577 seconds and 5 git commands to generate.