blob: abc2c9f048216d060a785259f45d2f9706fe2315 [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.
16 */
17
18#include <linux/init.h>
19#include <linux/platform_device.h>
20#include <linux/kernel.h>
Gregory CLEMENT307c2bf2012-11-17 15:22:25 +010021#include <linux/clk.h>
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +020022#include <linux/timer.h>
23#include <linux/clockchips.h>
24#include <linux/interrupt.h>
25#include <linux/of.h>
26#include <linux/of_irq.h>
27#include <linux/of_address.h>
28#include <linux/irq.h>
29#include <linux/module.h>
Stephen Boyd38ff87f2013-06-01 23:39:40 -070030#include <linux/sched_clock.h>
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +020031
Gregory CLEMENTddd3f692013-01-25 18:32:42 +010032#include <asm/localtimer.h>
33#include <linux/percpu.h>
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +020034/*
35 * Timer block registers.
36 */
37#define TIMER_CTRL_OFF 0x0000
Ezequiel Garciaad48bd62013-08-13 11:43:10 -030038#define TIMER0_EN BIT(0)
39#define TIMER0_RELOAD_EN BIT(1)
40#define TIMER0_25MHZ BIT(11)
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +020041#define TIMER0_DIV(div) ((div) << 19)
Ezequiel Garciaad48bd62013-08-13 11:43:10 -030042#define TIMER1_EN BIT(2)
43#define TIMER1_RELOAD_EN BIT(3)
44#define TIMER1_25MHZ BIT(12)
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +020045#define TIMER1_DIV(div) ((div) << 22)
46#define TIMER_EVENTS_STATUS 0x0004
47#define TIMER0_CLR_MASK (~0x1)
48#define TIMER1_CLR_MASK (~0x100)
49#define TIMER0_RELOAD_OFF 0x0010
50#define TIMER0_VAL_OFF 0x0014
51#define TIMER1_RELOAD_OFF 0x0018
52#define TIMER1_VAL_OFF 0x001c
53
Gregory CLEMENTddd3f692013-01-25 18:32:42 +010054#define LCL_TIMER_EVENTS_STATUS 0x0028
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +020055/* Global timers are connected to the coherency fabric clock, and the
56 below divider reduces their incrementing frequency. */
57#define TIMER_DIVIDER_SHIFT 5
58#define TIMER_DIVIDER (1 << TIMER_DIVIDER_SHIFT)
59
60/*
61 * SoC-specific data.
62 */
Gregory CLEMENTddd3f692013-01-25 18:32:42 +010063static void __iomem *timer_base, *local_base;
64static unsigned int timer_clk;
65static bool timer25Mhz = true;
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +020066
67/*
68 * Number of timer ticks per jiffy.
69 */
70static u32 ticks_per_jiffy;
71
Gregory CLEMENTddd3f692013-01-25 18:32:42 +010072static struct clock_event_device __percpu **percpu_armada_370_xp_evt;
73
Ezequiel Garcia35796982013-08-13 11:43:11 -030074static void timer_ctrl_clrset(u32 clr, u32 set)
75{
76 writel((readl(timer_base + TIMER_CTRL_OFF) & ~clr) | set,
77 timer_base + TIMER_CTRL_OFF);
78}
79
80static void local_timer_ctrl_clrset(u32 clr, u32 set)
81{
82 writel((readl(local_base + TIMER_CTRL_OFF) & ~clr) | set,
83 local_base + TIMER_CTRL_OFF);
84}
85
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +020086static u32 notrace armada_370_xp_read_sched_clock(void)
87{
88 return ~readl(timer_base + TIMER0_VAL_OFF);
89}
90
91/*
92 * Clockevent handling.
93 */
94static int
95armada_370_xp_clkevt_next_event(unsigned long delta,
96 struct clock_event_device *dev)
97{
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +020098 /*
99 * Clear clockevent timer interrupt.
100 */
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100101 writel(TIMER0_CLR_MASK, local_base + LCL_TIMER_EVENTS_STATUS);
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200102
103 /*
104 * Setup new clockevent timer value.
105 */
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100106 writel(delta, local_base + TIMER0_VAL_OFF);
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200107
108 /*
109 * Enable the timer.
110 */
Ezequiel Garcia35796982013-08-13 11:43:11 -0300111 local_timer_ctrl_clrset(TIMER0_RELOAD_EN,
112 TIMER0_EN | TIMER0_DIV(TIMER_DIVIDER_SHIFT));
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200113 return 0;
114}
115
116static void
117armada_370_xp_clkevt_mode(enum clock_event_mode mode,
118 struct clock_event_device *dev)
119{
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200120 if (mode == CLOCK_EVT_MODE_PERIODIC) {
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100121
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200122 /*
123 * Setup timer to fire at 1/HZ intervals.
124 */
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100125 writel(ticks_per_jiffy - 1, local_base + TIMER0_RELOAD_OFF);
126 writel(ticks_per_jiffy - 1, local_base + TIMER0_VAL_OFF);
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200127
128 /*
129 * Enable timer.
130 */
Ezequiel Garcia35796982013-08-13 11:43:11 -0300131 local_timer_ctrl_clrset(0, TIMER0_RELOAD_EN |
132 TIMER0_EN |
133 TIMER0_DIV(TIMER_DIVIDER_SHIFT));
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200134 } else {
135 /*
136 * Disable timer.
137 */
Ezequiel Garcia35796982013-08-13 11:43:11 -0300138 local_timer_ctrl_clrset(TIMER0_EN, 0);
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200139
140 /*
141 * ACK pending timer interrupt.
142 */
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100143 writel(TIMER0_CLR_MASK, local_base + LCL_TIMER_EVENTS_STATUS);
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200144 }
145}
146
147static struct clock_event_device armada_370_xp_clkevt = {
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100148 .name = "armada_370_xp_per_cpu_tick",
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200149 .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
150 .shift = 32,
151 .rating = 300,
152 .set_next_event = armada_370_xp_clkevt_next_event,
153 .set_mode = armada_370_xp_clkevt_mode,
154};
155
156static irqreturn_t armada_370_xp_timer_interrupt(int irq, void *dev_id)
157{
158 /*
159 * ACK timer interrupt and call event handler.
160 */
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100161 struct clock_event_device *evt = *(struct clock_event_device **)dev_id;
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200162
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100163 writel(TIMER0_CLR_MASK, local_base + LCL_TIMER_EVENTS_STATUS);
164 evt->event_handler(evt);
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200165
166 return IRQ_HANDLED;
167}
168
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100169/*
170 * Setup the local clock events for a CPU.
171 */
Paul Gortmaker8c37bb32013-06-19 11:32:08 -0400172static int armada_370_xp_timer_setup(struct clock_event_device *evt)
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100173{
Ezequiel Garcia35796982013-08-13 11:43:11 -0300174 u32 clr = 0, set = 0;
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100175 int cpu = smp_processor_id();
176
177 /* Use existing clock_event for cpu 0 */
178 if (!smp_processor_id())
179 return 0;
180
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100181 if (timer25Mhz)
Ezequiel Garcia35796982013-08-13 11:43:11 -0300182 set = TIMER0_25MHZ;
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100183 else
Ezequiel Garcia35796982013-08-13 11:43:11 -0300184 clr = TIMER0_25MHZ;
185 local_timer_ctrl_clrset(clr, set);
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100186
187 evt->name = armada_370_xp_clkevt.name;
188 evt->irq = armada_370_xp_clkevt.irq;
189 evt->features = armada_370_xp_clkevt.features;
190 evt->shift = armada_370_xp_clkevt.shift;
191 evt->rating = armada_370_xp_clkevt.rating,
192 evt->set_next_event = armada_370_xp_clkevt_next_event,
193 evt->set_mode = armada_370_xp_clkevt_mode,
194 evt->cpumask = cpumask_of(cpu);
195
196 *__this_cpu_ptr(percpu_armada_370_xp_evt) = evt;
197
198 clockevents_config_and_register(evt, timer_clk, 1, 0xfffffffe);
199 enable_percpu_irq(evt->irq, 0);
200
201 return 0;
202}
203
204static void armada_370_xp_timer_stop(struct clock_event_device *evt)
205{
206 evt->set_mode(CLOCK_EVT_MODE_UNUSED, evt);
207 disable_percpu_irq(evt->irq);
208}
209
Paul Gortmaker8c37bb32013-06-19 11:32:08 -0400210static struct local_timer_ops armada_370_xp_local_timer_ops = {
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100211 .setup = armada_370_xp_timer_setup,
212 .stop = armada_370_xp_timer_stop,
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200213};
214
215void __init armada_370_xp_timer_init(void)
216{
Ezequiel Garcia35796982013-08-13 11:43:11 -0300217 u32 clr = 0, set = 0;
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200218 struct device_node *np;
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100219 int res;
220
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200221 np = of_find_compatible_node(NULL, NULL, "marvell,armada-370-xp-timer");
222 timer_base = of_iomap(np, 0);
223 WARN_ON(!timer_base);
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100224 local_base = of_iomap(np, 1);
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200225
226 if (of_find_property(np, "marvell,timer-25Mhz", NULL)) {
227 /* The fixed 25MHz timer is available so let's use it */
Ezequiel Garcia35796982013-08-13 11:43:11 -0300228 set = TIMER0_25MHZ;
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200229 timer_clk = 25000000;
230 } else {
Gregory CLEMENT307c2bf2012-11-17 15:22:25 +0100231 unsigned long rate = 0;
232 struct clk *clk = of_clk_get(np, 0);
233 WARN_ON(IS_ERR(clk));
234 rate = clk_get_rate(clk);
Gregory CLEMENT307c2bf2012-11-17 15:22:25 +0100235 timer_clk = rate / TIMER_DIVIDER;
Ezequiel Garcia35796982013-08-13 11:43:11 -0300236
237 clr = TIMER0_25MHZ;
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100238 timer25Mhz = false;
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200239 }
Ezequiel Garcia35796982013-08-13 11:43:11 -0300240 timer_ctrl_clrset(clr, set);
241 local_timer_ctrl_clrset(clr, set);
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200242
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100243 /*
244 * We use timer 0 as clocksource, and private(local) timer 0
245 * for clockevents
246 */
247 armada_370_xp_clkevt.irq = irq_of_parse_and_map(np, 4);
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200248
249 ticks_per_jiffy = (timer_clk + HZ / 2) / HZ;
250
251 /*
252 * Set scale and timer for sched_clock.
253 */
254 setup_sched_clock(armada_370_xp_read_sched_clock, 32, timer_clk);
255
256 /*
257 * Setup free-running clocksource timer (interrupts
258 * disabled).
259 */
260 writel(0xffffffff, timer_base + TIMER0_VAL_OFF);
261 writel(0xffffffff, timer_base + TIMER0_RELOAD_OFF);
262
Ezequiel Garcia35796982013-08-13 11:43:11 -0300263 timer_ctrl_clrset(0, TIMER0_EN | TIMER0_RELOAD_EN |
264 TIMER0_DIV(TIMER_DIVIDER_SHIFT));
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200265
266 clocksource_mmio_init(timer_base + TIMER0_VAL_OFF,
267 "armada_370_xp_clocksource",
268 timer_clk, 300, 32, clocksource_mmio_readl_down);
269
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100270 /* Register the clockevent on the private timer of CPU 0 */
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200271 armada_370_xp_clkevt.cpumask = cpumask_of(0);
272 clockevents_config_and_register(&armada_370_xp_clkevt,
273 timer_clk, 1, 0xfffffffe);
Gregory CLEMENT6fe9cbd2012-06-13 18:58:09 +0200274
Gregory CLEMENTddd3f692013-01-25 18:32:42 +0100275 percpu_armada_370_xp_evt = alloc_percpu(struct clock_event_device *);
276
277
278 /*
279 * Setup clockevent timer (interrupt-driven).
280 */
281 *__this_cpu_ptr(percpu_armada_370_xp_evt) = &armada_370_xp_clkevt;
282 res = request_percpu_irq(armada_370_xp_clkevt.irq,
283 armada_370_xp_timer_interrupt,
284 armada_370_xp_clkevt.name,
285 percpu_armada_370_xp_evt);
286 if (!res) {
287 enable_percpu_irq(armada_370_xp_clkevt.irq, 0);
288#ifdef CONFIG_LOCAL_TIMERS
289 local_timer_register(&armada_370_xp_local_timer_ops);
290#endif
291 }
292}