1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Actions Semi Owl timer
5 * Copyright 2012 Actions Semi Inc.
6 * Author: Actions Semi, Inc.
8 * Copyright (c) 2017 SUSE Linux GmbH
9 * Author: Andreas Färber
12 #include <linux/clk.h>
13 #include <linux/clockchips.h>
14 #include <linux/interrupt.h>
15 #include <linux/irq.h>
16 #include <linux/irqreturn.h>
17 #include <linux/sched_clock.h>
19 #include <linux/of_address.h>
20 #include <linux/of_irq.h>
22 #define OWL_Tx_CTL 0x0
23 #define OWL_Tx_CMP 0x4
24 #define OWL_Tx_VAL 0x8
26 #define OWL_Tx_CTL_PD BIT(0)
27 #define OWL_Tx_CTL_INTEN BIT(1)
28 #define OWL_Tx_CTL_EN BIT(2)
30 static void __iomem *owl_timer_base;
31 static void __iomem *owl_clksrc_base;
32 static void __iomem *owl_clkevt_base;
34 static inline void owl_timer_reset(void __iomem *base)
36 writel(0, base + OWL_Tx_CTL);
37 writel(0, base + OWL_Tx_VAL);
38 writel(0, base + OWL_Tx_CMP);
41 static inline void owl_timer_set_enabled(void __iomem *base, bool enabled)
43 u32 ctl = readl(base + OWL_Tx_CTL);
45 /* PD bit is cleared when set */
46 ctl &= ~OWL_Tx_CTL_PD;
51 ctl &= ~OWL_Tx_CTL_EN;
53 writel(ctl, base + OWL_Tx_CTL);
56 static u64 notrace owl_timer_sched_read(void)
58 return (u64)readl(owl_clksrc_base + OWL_Tx_VAL);
61 static int owl_timer_set_state_shutdown(struct clock_event_device *evt)
63 owl_timer_set_enabled(owl_clkevt_base, false);
68 static int owl_timer_set_state_oneshot(struct clock_event_device *evt)
70 owl_timer_reset(owl_clkevt_base);
75 static int owl_timer_tick_resume(struct clock_event_device *evt)
80 static int owl_timer_set_next_event(unsigned long evt,
81 struct clock_event_device *ev)
83 void __iomem *base = owl_clkevt_base;
85 owl_timer_set_enabled(base, false);
86 writel(OWL_Tx_CTL_INTEN, base + OWL_Tx_CTL);
87 writel(0, base + OWL_Tx_VAL);
88 writel(evt, base + OWL_Tx_CMP);
89 owl_timer_set_enabled(base, true);
94 static struct clock_event_device owl_clockevent = {
97 .features = CLOCK_EVT_FEAT_ONESHOT |
98 CLOCK_EVT_FEAT_DYNIRQ,
99 .set_state_shutdown = owl_timer_set_state_shutdown,
100 .set_state_oneshot = owl_timer_set_state_oneshot,
101 .tick_resume = owl_timer_tick_resume,
102 .set_next_event = owl_timer_set_next_event,
105 static irqreturn_t owl_timer1_interrupt(int irq, void *dev_id)
107 struct clock_event_device *evt = (struct clock_event_device *)dev_id;
109 writel(OWL_Tx_CTL_PD, owl_clkevt_base + OWL_Tx_CTL);
111 evt->event_handler(evt);
116 static int __init owl_timer_init(struct device_node *node)
122 owl_timer_base = of_io_request_and_map(node, 0, "owl-timer");
123 if (IS_ERR(owl_timer_base)) {
124 pr_err("Can't map timer registers\n");
125 return PTR_ERR(owl_timer_base);
128 owl_clksrc_base = owl_timer_base + 0x08;
129 owl_clkevt_base = owl_timer_base + 0x14;
131 timer1_irq = of_irq_get_byname(node, "timer1");
132 if (timer1_irq <= 0) {
133 pr_err("Can't parse timer1 IRQ\n");
137 clk = of_clk_get(node, 0);
140 pr_err("Failed to get clock for clocksource (%d)\n", ret);
144 rate = clk_get_rate(clk);
146 owl_timer_reset(owl_clksrc_base);
147 owl_timer_set_enabled(owl_clksrc_base, true);
149 sched_clock_register(owl_timer_sched_read, 32, rate);
150 ret = clocksource_mmio_init(owl_clksrc_base + OWL_Tx_VAL, node->name,
151 rate, 200, 32, clocksource_mmio_readl_up);
153 pr_err("Failed to register clocksource (%d)\n", ret);
157 owl_timer_reset(owl_clkevt_base);
159 ret = request_irq(timer1_irq, owl_timer1_interrupt, IRQF_TIMER,
160 "owl-timer", &owl_clockevent);
162 pr_err("failed to request irq %d\n", timer1_irq);
166 owl_clockevent.cpumask = cpumask_of(0);
167 owl_clockevent.irq = timer1_irq;
169 clockevents_config_and_register(&owl_clockevent, rate,
174 TIMER_OF_DECLARE(owl_s500, "actions,s500-timer", owl_timer_init);
175 TIMER_OF_DECLARE(owl_s700, "actions,s700-timer", owl_timer_init);
176 TIMER_OF_DECLARE(owl_s900, "actions,s900-timer", owl_timer_init);