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 static long __init sp804_get_clock_rate(struct clk *clk, const char *name)
29 clk = clk_get_sys("sp804", name);
31 pr_err("sp804: %s clock not found: %ld\n", name, PTR_ERR(clk));
35 err = clk_prepare(clk);
37 pr_err("sp804: clock failed to prepare: %d\n", err);
42 err = clk_enable(clk);
44 pr_err("sp804: clock failed to enable: %d\n", err);
50 rate = clk_get_rate(clk);
52 pr_err("sp804: clock failed to get rate: %ld\n", rate);
61 static void __iomem *sched_clock_base;
63 static u64 notrace sp804_read(void)
65 return ~readl_relaxed(sched_clock_base + TIMER_VALUE);
68 int __init sp804_clocksource_and_sched_clock_init(void __iomem *base,
75 rate = sp804_get_clock_rate(clk, name);
79 /* setup timer 0 as free-running clocksource */
80 writel(0, base + TIMER_CTRL);
81 writel(0xffffffff, base + TIMER_LOAD);
82 writel(0xffffffff, base + TIMER_VALUE);
83 writel(TIMER_CTRL_32BIT | TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC,
86 clocksource_mmio_init(base + TIMER_VALUE, name,
87 rate, 200, 32, clocksource_mmio_readl_down);
89 if (use_sched_clock) {
90 sched_clock_base = base;
91 sched_clock_register(sp804_read, 32, rate);
98 static void __iomem *clkevt_base;
99 static unsigned long clkevt_reload;
102 * IRQ handler for the timer
104 static irqreturn_t sp804_timer_interrupt(int irq, void *dev_id)
106 struct clock_event_device *evt = dev_id;
108 /* clear the interrupt */
109 writel(1, clkevt_base + TIMER_INTCLR);
111 evt->event_handler(evt);
116 static inline void timer_shutdown(struct clock_event_device *evt)
118 writel(0, clkevt_base + TIMER_CTRL);
121 static int sp804_shutdown(struct clock_event_device *evt)
127 static int sp804_set_periodic(struct clock_event_device *evt)
129 unsigned long ctrl = TIMER_CTRL_32BIT | TIMER_CTRL_IE |
130 TIMER_CTRL_PERIODIC | TIMER_CTRL_ENABLE;
133 writel(clkevt_reload, clkevt_base + TIMER_LOAD);
134 writel(ctrl, clkevt_base + TIMER_CTRL);
138 static int sp804_set_next_event(unsigned long next,
139 struct clock_event_device *evt)
141 unsigned long ctrl = TIMER_CTRL_32BIT | TIMER_CTRL_IE |
142 TIMER_CTRL_ONESHOT | TIMER_CTRL_ENABLE;
144 writel(next, clkevt_base + TIMER_LOAD);
145 writel(ctrl, clkevt_base + TIMER_CTRL);
150 static struct clock_event_device sp804_clockevent = {
151 .features = CLOCK_EVT_FEAT_PERIODIC |
152 CLOCK_EVT_FEAT_ONESHOT |
153 CLOCK_EVT_FEAT_DYNIRQ,
154 .set_state_shutdown = sp804_shutdown,
155 .set_state_periodic = sp804_set_periodic,
156 .set_state_oneshot = sp804_shutdown,
157 .tick_resume = sp804_shutdown,
158 .set_next_event = sp804_set_next_event,
162 int __init sp804_clockevents_init(void __iomem *base, unsigned int irq,
163 struct clk *clk, const char *name)
165 struct clock_event_device *evt = &sp804_clockevent;
168 rate = sp804_get_clock_rate(clk, name);
173 clkevt_reload = DIV_ROUND_CLOSEST(rate, HZ);
176 evt->cpumask = cpu_possible_mask;
178 writel(0, base + TIMER_CTRL);
180 if (request_irq(irq, sp804_timer_interrupt, IRQF_TIMER | IRQF_IRQPOLL,
181 "timer", &sp804_clockevent))
182 pr_err("%s: request_irq() failed\n", "timer");
183 clockevents_config_and_register(evt, rate, 0xf, 0xffffffff);
188 static int __init sp804_of_init(struct device_node *np)
190 static bool initialized = false;
192 int irq, ret = -EINVAL;
194 struct clk *clk1, *clk2;
195 const char *name = of_get_property(np, "compatible", NULL);
197 base = of_iomap(np, 0);
201 /* Ensure timers are disabled */
202 writel(0, base + TIMER_CTRL);
203 writel(0, base + TIMER_2_BASE + TIMER_CTRL);
205 if (initialized || !of_device_is_available(np)) {
210 clk1 = of_clk_get(np, 0);
214 /* Get the 2nd clock if the timer has 3 timer clocks */
215 if (of_clk_get_parent_count(np) == 3) {
216 clk2 = of_clk_get(np, 1);
218 pr_err("sp804: %pOFn clock not found: %d\n", np,
225 irq = irq_of_parse_and_map(np, 0);
229 of_property_read_u32(np, "arm,sp804-has-irq", &irq_num);
232 ret = sp804_clockevents_init(base + TIMER_2_BASE, irq, clk2, name);
236 ret = sp804_clocksource_and_sched_clock_init(base,
242 ret = sp804_clockevents_init(base, irq, clk1, name);
246 ret = sp804_clocksource_and_sched_clock_init(base + TIMER_2_BASE,
258 TIMER_OF_DECLARE(sp804, "arm,sp804", sp804_of_init);
260 static int __init integrator_cp_of_init(struct device_node *np)
262 static int init_count = 0;
264 int irq, ret = -EINVAL;
265 const char *name = of_get_property(np, "compatible", NULL);
268 base = of_iomap(np, 0);
270 pr_err("Failed to iomap\n");
274 clk = of_clk_get(np, 0);
276 pr_err("Failed to get clock\n");
280 /* Ensure timer is disabled */
281 writel(0, base + TIMER_CTRL);
283 if (init_count == 2 || !of_device_is_available(np))
287 ret = sp804_clocksource_and_sched_clock_init(base,
292 irq = irq_of_parse_and_map(np, 0);
296 ret = sp804_clockevents_init(base, irq, clk, name);
307 TIMER_OF_DECLARE(intcp, "arm,integrator-cp-timer", integrator_cp_of_init);