1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * linux/drivers/clocksource/timer-sp.c
5 * Copyright (C) 1999 - 2003 ARM Limited
6 * Copyright (C) 2000 Deep Blue Solutions Ltd
9 #include <linux/clocksource.h>
10 #include <linux/clockchips.h>
11 #include <linux/err.h>
12 #include <linux/interrupt.h>
13 #include <linux/irq.h>
16 #include <linux/of_address.h>
17 #include <linux/of_clk.h>
18 #include <linux/of_irq.h>
19 #include <linux/sched_clock.h>
23 /* Hisilicon 64-bit timer(a variant of ARM SP804) */
24 #define HISI_TIMER_1_BASE 0x00
25 #define HISI_TIMER_2_BASE 0x40
26 #define HISI_TIMER_LOAD 0x00
27 #define HISI_TIMER_LOAD_H 0x04
28 #define HISI_TIMER_VALUE 0x08
29 #define HISI_TIMER_VALUE_H 0x0c
30 #define HISI_TIMER_CTRL 0x10
31 #define HISI_TIMER_INTCLR 0x14
32 #define HISI_TIMER_RIS 0x18
33 #define HISI_TIMER_MIS 0x1c
34 #define HISI_TIMER_BGLOAD 0x20
35 #define HISI_TIMER_BGLOAD_H 0x24
37 static struct sp804_timer arm_sp804_timer __initdata = {
41 .intclr = TIMER_INTCLR,
42 .timer_base = {TIMER_1_BASE, TIMER_2_BASE},
46 static struct sp804_timer hisi_sp804_timer __initdata = {
47 .load = HISI_TIMER_LOAD,
48 .load_h = HISI_TIMER_LOAD_H,
49 .value = HISI_TIMER_VALUE,
50 .value_h = HISI_TIMER_VALUE_H,
51 .ctrl = HISI_TIMER_CTRL,
52 .intclr = HISI_TIMER_INTCLR,
53 .timer_base = {HISI_TIMER_1_BASE, HISI_TIMER_2_BASE},
57 static struct sp804_clkevt sp804_clkevt[NR_TIMERS];
59 static long __init sp804_get_clock_rate(struct clk *clk, const char *name)
64 clk = clk_get_sys("sp804", name);
66 pr_err("sp804: %s clock not found: %ld\n", name, PTR_ERR(clk));
70 err = clk_prepare_enable(clk);
72 pr_err("sp804: clock failed to enable: %d\n", err);
77 return clk_get_rate(clk);
80 static struct sp804_clkevt * __init sp804_clkevt_get(void __iomem *base)
84 for (i = 0; i < NR_TIMERS; i++) {
85 if (sp804_clkevt[i].base == base)
86 return &sp804_clkevt[i];
89 /* It's impossible to reach here */
95 static struct sp804_clkevt *sched_clkevt;
97 static u64 notrace sp804_read(void)
99 return ~readl_relaxed(sched_clkevt->value);
102 static int __init sp804_clocksource_and_sched_clock_init(void __iomem *base,
108 struct sp804_clkevt *clkevt;
110 rate = sp804_get_clock_rate(clk, name);
114 clkevt = sp804_clkevt_get(base);
116 writel(0, clkevt->ctrl);
117 writel(0xffffffff, clkevt->load);
118 writel(0xffffffff, clkevt->value);
119 if (clkevt->width == 64) {
120 writel(0xffffffff, clkevt->load_h);
121 writel(0xffffffff, clkevt->value_h);
123 writel(TIMER_CTRL_32BIT | TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC,
126 clocksource_mmio_init(clkevt->value, name,
127 rate, 200, 32, clocksource_mmio_readl_down);
129 if (use_sched_clock) {
130 sched_clkevt = clkevt;
131 sched_clock_register(sp804_read, 32, rate);
138 static struct sp804_clkevt *common_clkevt;
141 * IRQ handler for the timer
143 static irqreturn_t sp804_timer_interrupt(int irq, void *dev_id)
145 struct clock_event_device *evt = dev_id;
147 /* clear the interrupt */
148 writel(1, common_clkevt->intclr);
150 evt->event_handler(evt);
155 static inline void timer_shutdown(struct clock_event_device *evt)
157 writel(0, common_clkevt->ctrl);
160 static int sp804_shutdown(struct clock_event_device *evt)
166 static int sp804_set_periodic(struct clock_event_device *evt)
168 unsigned long ctrl = TIMER_CTRL_32BIT | TIMER_CTRL_IE |
169 TIMER_CTRL_PERIODIC | TIMER_CTRL_ENABLE;
172 writel(common_clkevt->reload, common_clkevt->load);
173 writel(ctrl, common_clkevt->ctrl);
177 static int sp804_set_next_event(unsigned long next,
178 struct clock_event_device *evt)
180 unsigned long ctrl = TIMER_CTRL_32BIT | TIMER_CTRL_IE |
181 TIMER_CTRL_ONESHOT | TIMER_CTRL_ENABLE;
183 writel(next, common_clkevt->load);
184 writel(ctrl, common_clkevt->ctrl);
189 static struct clock_event_device sp804_clockevent = {
190 .features = CLOCK_EVT_FEAT_PERIODIC |
191 CLOCK_EVT_FEAT_ONESHOT |
192 CLOCK_EVT_FEAT_DYNIRQ,
193 .set_state_shutdown = sp804_shutdown,
194 .set_state_periodic = sp804_set_periodic,
195 .set_state_oneshot = sp804_shutdown,
196 .tick_resume = sp804_shutdown,
197 .set_next_event = sp804_set_next_event,
201 static int __init sp804_clockevents_init(void __iomem *base, unsigned int irq,
202 struct clk *clk, const char *name)
204 struct clock_event_device *evt = &sp804_clockevent;
207 rate = sp804_get_clock_rate(clk, name);
211 common_clkevt = sp804_clkevt_get(base);
212 common_clkevt->reload = DIV_ROUND_CLOSEST(rate, HZ);
215 evt->cpumask = cpu_possible_mask;
217 writel(0, common_clkevt->ctrl);
219 if (request_irq(irq, sp804_timer_interrupt, IRQF_TIMER | IRQF_IRQPOLL,
220 "timer", &sp804_clockevent))
221 pr_err("%s: request_irq() failed\n", "timer");
222 clockevents_config_and_register(evt, rate, 0xf, 0xffffffff);
227 static void __init sp804_clkevt_init(struct sp804_timer *timer, void __iomem *base)
231 for (i = 0; i < NR_TIMERS; i++) {
232 void __iomem *timer_base;
233 struct sp804_clkevt *clkevt;
235 timer_base = base + timer->timer_base[i];
236 clkevt = &sp804_clkevt[i];
237 clkevt->base = timer_base;
238 clkevt->load = timer_base + timer->load;
239 clkevt->load_h = timer_base + timer->load_h;
240 clkevt->value = timer_base + timer->value;
241 clkevt->value_h = timer_base + timer->value_h;
242 clkevt->ctrl = timer_base + timer->ctrl;
243 clkevt->intclr = timer_base + timer->intclr;
244 clkevt->width = timer->width;
248 static int __init sp804_of_init(struct device_node *np, struct sp804_timer *timer)
250 static bool initialized = false;
252 void __iomem *timer1_base;
253 void __iomem *timer2_base;
254 int irq, ret = -EINVAL;
256 struct clk *clk1, *clk2;
257 const char *name = of_get_property(np, "compatible", NULL);
259 base = of_iomap(np, 0);
263 timer1_base = base + timer->timer_base[0];
264 timer2_base = base + timer->timer_base[1];
266 /* Ensure timers are disabled */
267 writel(0, timer1_base + timer->ctrl);
268 writel(0, timer2_base + timer->ctrl);
270 if (initialized || !of_device_is_available(np)) {
275 clk1 = of_clk_get(np, 0);
279 /* Get the 2nd clock if the timer has 3 timer clocks */
280 if (of_clk_get_parent_count(np) == 3) {
281 clk2 = of_clk_get(np, 1);
283 pr_err("sp804: %pOFn clock not found: %d\n", np,
290 irq = irq_of_parse_and_map(np, 0);
294 sp804_clkevt_init(timer, base);
296 of_property_read_u32(np, "arm,sp804-has-irq", &irq_num);
299 ret = sp804_clockevents_init(timer2_base, irq, clk2, name);
303 ret = sp804_clocksource_and_sched_clock_init(timer1_base,
309 ret = sp804_clockevents_init(timer1_base, irq, clk1, name);
313 ret = sp804_clocksource_and_sched_clock_init(timer2_base,
326 static int __init arm_sp804_of_init(struct device_node *np)
328 return sp804_of_init(np, &arm_sp804_timer);
330 TIMER_OF_DECLARE(sp804, "arm,sp804", arm_sp804_of_init);
332 static int __init hisi_sp804_of_init(struct device_node *np)
334 return sp804_of_init(np, &hisi_sp804_timer);
336 TIMER_OF_DECLARE(hisi_sp804, "hisilicon,sp804", hisi_sp804_of_init);
338 static int __init integrator_cp_of_init(struct device_node *np)
340 static int init_count = 0;
342 int irq, ret = -EINVAL;
343 const char *name = of_get_property(np, "compatible", NULL);
346 base = of_iomap(np, 0);
348 pr_err("Failed to iomap\n");
352 clk = of_clk_get(np, 0);
354 pr_err("Failed to get clock\n");
358 /* Ensure timer is disabled */
359 writel(0, base + arm_sp804_timer.ctrl);
361 if (init_count == 2 || !of_device_is_available(np))
364 sp804_clkevt_init(&arm_sp804_timer, base);
367 ret = sp804_clocksource_and_sched_clock_init(base,
372 irq = irq_of_parse_and_map(np, 0);
376 ret = sp804_clockevents_init(base, irq, clk, name);
387 TIMER_OF_DECLARE(intcp, "arm,integrator-cp-timer", integrator_cp_of_init);