blob: 86a354cca2154b8de98912f9f7d39cce8ea83340 [file] [log] [blame]
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +02001/*
2 * Marvell Armada 370/XP SoC timer handling.
3 *
4 * Copyright (C) 2012 Marvell
5 *
6 * Lior Amsalem <alior@marvell.com>
7 * Gregory CLEMENT <gregory.clement@free-electrons.com>
8 * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
9 *
10 * This file is licensed under the terms of the GNU General Public
11 * License version 2. This program is licensed "as is" without any
12 * warranty of any kind, whether express or implied.
13 *
14 * Timer 0 is used as free-running clocksource, while timer 1 is
15 * used as clock_event_device.
Ezequiel Garcia7cd63922013-08-13 11:43:13 -030016 *
17 * ---
18 * Clocksource driver for Armada 370 and Armada XP SoC.
19 * This driver implements one compatible string for each SoC, given
20 * each has its own characteristics:
21 *
22 * * Armada 370 has no 25 MHz fixed timer.
23 *
24 * * Armada XP cannot work properly without such 25 MHz fixed timer as
25 * doing otherwise leads to using a clocksource whose frequency varies
26 * when doing cpufreq frequency changes.
27 *
28 * See Documentation/devicetree/bindings/timer/marvell,armada-370-xp-timer.txt
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +020029 */
30
31#include <linux/init.h>
32#include <linux/platform_device.h>
33#include <linux/kernel.h>
Gregory CLEMENT307c2bf2012-11-17 15:22:25 +010034#include <linux/clk.h>
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +020035#include <linux/timer.h>
36#include <linux/clockchips.h>
37#include <linux/interrupt.h>
38#include <linux/of.h>
39#include <linux/of_irq.h>
40#include <linux/of_address.h>
41#include <linux/irq.h>
42#include <linux/module.h>
Stephen Boyd38ff87f2013-06-01 23:39:40 -070043#include <linux/sched_clock.h>
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +020044
Gregory CLEMENTddd3f692013-01-25 18:32:42 +010045#include <asm/localtimer.h>
46#include <linux/percpu.h>
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +020047/*
48 * Timer block registers.
49 */
50#define TIMER_CTRL_OFF 0x0000
Ezequiel Garciaad48bd62013-08-13 11:43:10 -030051#define TIMER0_EN BIT(0)
52#define TIMER0_RELOAD_EN BIT(1)
53#define TIMER0_25MHZ BIT(11)
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +020054#define TIMER0_DIV(div) ((div) << 19)
Ezequiel Garciaad48bd62013-08-13 11:43:10 -030055#define TIMER1_EN BIT(2)
56#define TIMER1_RELOAD_EN BIT(3)
57#define TIMER1_25MHZ BIT(12)
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +020058#define TIMER1_DIV(div) ((div) << 22)
59#define TIMER_EVENTS_STATUS 0x0004
60#define TIMER0_CLR_MASK (~0x1)
61#define TIMER1_CLR_MASK (~0x100)
62#define TIMER0_RELOAD_OFF 0x0010
63#define TIMER0_VAL_OFF 0x0014
64#define TIMER1_RELOAD_OFF 0x0018
65#define TIMER1_VAL_OFF 0x001c
66
Gregory CLEMENTddd3f692013-01-25 18:32:42 +010067#define LCL_TIMER_EVENTS_STATUS 0x0028
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +020068/* Global timers are connected to the coherency fabric clock, and the
69 below divider reduces their incrementing frequency. */
70#define TIMER_DIVIDER_SHIFT 5
71#define TIMER_DIVIDER (1 << TIMER_DIVIDER_SHIFT)
72
73/*
74 * SoC-specific data.
75 */
Gregory CLEMENTddd3f692013-01-25 18:32:42 +010076static void __iomem *timer_base, *local_base;
77static unsigned int timer_clk;
78static bool timer25Mhz = true;
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +020079
80/*
81 * Number of timer ticks per jiffy.
82 */
83static u32 ticks_per_jiffy;
84
Gregory CLEMENTddd3f692013-01-25 18:32:42 +010085static struct clock_event_device __percpu **percpu_armada_370_xp_evt;
86
Ezequiel Garcia35796982013-08-13 11:43:11 -030087static void timer_ctrl_clrset(u32 clr, u32 set)
88{
89 writel((readl(timer_base + TIMER_CTRL_OFF) & ~clr) | set,
90 timer_base + TIMER_CTRL_OFF);
91}
92
93static void local_timer_ctrl_clrset(u32 clr, u32 set)
94{
95 writel((readl(local_base + TIMER_CTRL_OFF) & ~clr) | set,
96 local_base + TIMER_CTRL_OFF);
97}
98
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +020099static u32 notrace armada_370_xp_read_sched_clock(void)
100{
101 return ~readl(timer_base + TIMER0_VAL_OFF);
102}
103
104/*
105 * Clockevent handling.
106 */
107static int
108armada_370_xp_clkevt_next_event(unsigned long delta,
109 struct clock_event_device *dev)
110{
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200111 /*
112 * Clear clockevent timer interrupt.
113 */
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100114 writel(TIMER0_CLR_MASK, local_base + LCL_TIMER_EVENTS_STATUS);
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200115
116 /*
117 * Setup new clockevent timer value.
118 */
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100119 writel(delta, local_base + TIMER0_VAL_OFF);
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200120
121 /*
122 * Enable the timer.
123 */
Ezequiel Garcia35796982013-08-13 11:43:11 -0300124 local_timer_ctrl_clrset(TIMER0_RELOAD_EN,
125 TIMER0_EN | TIMER0_DIV(TIMER_DIVIDER_SHIFT));
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200126 return 0;
127}
128
129static void
130armada_370_xp_clkevt_mode(enum clock_event_mode mode,
131 struct clock_event_device *dev)
132{
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200133 if (mode == CLOCK_EVT_MODE_PERIODIC) {
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100134
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200135 /*
136 * Setup timer to fire at 1/HZ intervals.
137 */
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100138 writel(ticks_per_jiffy - 1, local_base + TIMER0_RELOAD_OFF);
139 writel(ticks_per_jiffy - 1, local_base + TIMER0_VAL_OFF);
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200140
141 /*
142 * Enable timer.
143 */
Ezequiel Garcia35796982013-08-13 11:43:11 -0300144 local_timer_ctrl_clrset(0, TIMER0_RELOAD_EN |
145 TIMER0_EN |
146 TIMER0_DIV(TIMER_DIVIDER_SHIFT));
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200147 } else {
148 /*
149 * Disable timer.
150 */
Ezequiel Garcia35796982013-08-13 11:43:11 -0300151 local_timer_ctrl_clrset(TIMER0_EN, 0);
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200152
153 /*
154 * ACK pending timer interrupt.
155 */
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100156 writel(TIMER0_CLR_MASK, local_base + LCL_TIMER_EVENTS_STATUS);
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200157 }
158}
159
160static struct clock_event_device armada_370_xp_clkevt = {
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100161 .name = "armada_370_xp_per_cpu_tick",
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200162 .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
163 .shift = 32,
164 .rating = 300,
165 .set_next_event = armada_370_xp_clkevt_next_event,
166 .set_mode = armada_370_xp_clkevt_mode,
167};
168
169static irqreturn_t armada_370_xp_timer_interrupt(int irq, void *dev_id)
170{
171 /*
172 * ACK timer interrupt and call event handler.
173 */
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100174 struct clock_event_device *evt = *(struct clock_event_device **)dev_id;
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200175
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100176 writel(TIMER0_CLR_MASK, local_base + LCL_TIMER_EVENTS_STATUS);
177 evt->event_handler(evt);
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200178
179 return IRQ_HANDLED;
180}
181
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100182/*
183 * Setup the local clock events for a CPU.
184 */
Paul Gortmaker8c37bb32013-06-19 11:32:08 -0400185static int armada_370_xp_timer_setup(struct clock_event_device *evt)
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100186{
Ezequiel Garcia35796982013-08-13 11:43:11 -0300187 u32 clr = 0, set = 0;
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100188 int cpu = smp_processor_id();
189
190 /* Use existing clock_event for cpu 0 */
191 if (!smp_processor_id())
192 return 0;
193
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100194 if (timer25Mhz)
Ezequiel Garcia35796982013-08-13 11:43:11 -0300195 set = TIMER0_25MHZ;
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100196 else
Ezequiel Garcia35796982013-08-13 11:43:11 -0300197 clr = TIMER0_25MHZ;
198 local_timer_ctrl_clrset(clr, set);
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100199
200 evt->name = armada_370_xp_clkevt.name;
201 evt->irq = armada_370_xp_clkevt.irq;
202 evt->features = armada_370_xp_clkevt.features;
203 evt->shift = armada_370_xp_clkevt.shift;
204 evt->rating = armada_370_xp_clkevt.rating,
205 evt->set_next_event = armada_370_xp_clkevt_next_event,
206 evt->set_mode = armada_370_xp_clkevt_mode,
207 evt->cpumask = cpumask_of(cpu);
208
209 *__this_cpu_ptr(percpu_armada_370_xp_evt) = evt;
210
211 clockevents_config_and_register(evt, timer_clk, 1, 0xfffffffe);
212 enable_percpu_irq(evt->irq, 0);
213
214 return 0;
215}
216
217static void armada_370_xp_timer_stop(struct clock_event_device *evt)
218{
219 evt->set_mode(CLOCK_EVT_MODE_UNUSED, evt);
220 disable_percpu_irq(evt->irq);
221}
222
Paul Gortmaker8c37bb32013-06-19 11:32:08 -0400223static struct local_timer_ops armada_370_xp_local_timer_ops = {
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100224 .setup = armada_370_xp_timer_setup,
225 .stop = armada_370_xp_timer_stop,
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200226};
227
Ezequiel Garcia7cd63922013-08-13 11:43:13 -0300228static void __init armada_370_xp_timer_common_init(struct device_node *np)
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200229{
Ezequiel Garcia35796982013-08-13 11:43:11 -0300230 u32 clr = 0, set = 0;
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100231 int res;
232
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200233 timer_base = of_iomap(np, 0);
234 WARN_ON(!timer_base);
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100235 local_base = of_iomap(np, 1);
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200236
Ezequiel Garcia7cd63922013-08-13 11:43:13 -0300237 if (timer25Mhz)
Ezequiel Garcia35796982013-08-13 11:43:11 -0300238 set = TIMER0_25MHZ;
Ezequiel Garcia7cd63922013-08-13 11:43:13 -0300239 else
Ezequiel Garcia35796982013-08-13 11:43:11 -0300240 clr = TIMER0_25MHZ;
Ezequiel Garcia35796982013-08-13 11:43:11 -0300241 timer_ctrl_clrset(clr, set);
242 local_timer_ctrl_clrset(clr, set);
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200243
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100244 /*
245 * We use timer 0 as clocksource, and private(local) timer 0
246 * for clockevents
247 */
248 armada_370_xp_clkevt.irq = irq_of_parse_and_map(np, 4);
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200249
250 ticks_per_jiffy = (timer_clk + HZ / 2) / HZ;
251
252 /*
253 * Set scale and timer for sched_clock.
254 */
255 setup_sched_clock(armada_370_xp_read_sched_clock, 32, timer_clk);
256
257 /*
258 * Setup free-running clocksource timer (interrupts
259 * disabled).
260 */
261 writel(0xffffffff, timer_base + TIMER0_VAL_OFF);
262 writel(0xffffffff, timer_base + TIMER0_RELOAD_OFF);
263
Ezequiel Garcia35796982013-08-13 11:43:11 -0300264 timer_ctrl_clrset(0, TIMER0_EN | TIMER0_RELOAD_EN |
265 TIMER0_DIV(TIMER_DIVIDER_SHIFT));
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200266
267 clocksource_mmio_init(timer_base + TIMER0_VAL_OFF,
268 "armada_370_xp_clocksource",
269 timer_clk, 300, 32, clocksource_mmio_readl_down);
270
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100271 /* Register the clockevent on the private timer of CPU 0 */
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200272 armada_370_xp_clkevt.cpumask = cpumask_of(0);
273 clockevents_config_and_register(&armada_370_xp_clkevt,
274 timer_clk, 1, 0xfffffffe);
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200275
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100276 percpu_armada_370_xp_evt = alloc_percpu(struct clock_event_device *);
277
278
279 /*
280 * Setup clockevent timer (interrupt-driven).
281 */
282 *__this_cpu_ptr(percpu_armada_370_xp_evt) = &armada_370_xp_clkevt;
283 res = request_percpu_irq(armada_370_xp_clkevt.irq,
284 armada_370_xp_timer_interrupt,
285 armada_370_xp_clkevt.name,
286 percpu_armada_370_xp_evt);
287 if (!res) {
288 enable_percpu_irq(armada_370_xp_clkevt.irq, 0);
289#ifdef CONFIG_LOCAL_TIMERS
290 local_timer_register(&armada_370_xp_local_timer_ops);
291#endif
292 }
293}
Ezequiel Garcia7cd63922013-08-13 11:43:13 -0300294
295static void __init armada_xp_timer_init(struct device_node *np)
296{
297 /* The fixed 25MHz timer is required, timer25Mhz is true by default */
298 timer_clk = 25000000;
299
300 armada_370_xp_timer_common_init(np);
301}
302CLOCKSOURCE_OF_DECLARE(armada_xp, "marvell,armada-xp-timer",
303 armada_xp_timer_init);
304
305static void __init armada_370_timer_init(struct device_node *np)
306{
307 struct clk *clk = of_clk_get(np, 0);
308
309 WARN_ON(IS_ERR(clk));
310 timer_clk = clk_get_rate(clk) / TIMER_DIVIDER;
311 timer25Mhz = false;
312
313 armada_370_xp_timer_common_init(np);
314}
315CLOCKSOURCE_OF_DECLARE(armada_370, "marvell,armada-370-timer",
316 armada_370_timer_init);