Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / clocksource / timer-owl.c
blobac97420bfa7c5960cfa6a149af0918e8a0f9196b
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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>
18 #include <linux/of.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;
48 if (enabled)
49 ctl |= OWL_Tx_CTL_EN;
50 else
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);
65 return 0;
68 static int owl_timer_set_state_oneshot(struct clock_event_device *evt)
70 owl_timer_reset(owl_clkevt_base);
72 return 0;
75 static int owl_timer_tick_resume(struct clock_event_device *evt)
77 return 0;
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);
91 return 0;
94 static struct clock_event_device owl_clockevent = {
95 .name = "owl_tick",
96 .rating = 200,
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);
113 return IRQ_HANDLED;
116 static int __init owl_timer_init(struct device_node *node)
118 struct clk *clk;
119 unsigned long rate;
120 int timer1_irq, ret;
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");
134 return -EINVAL;
137 clk = of_clk_get(node, 0);
138 if (IS_ERR(clk)) {
139 ret = PTR_ERR(clk);
140 pr_err("Failed to get clock for clocksource (%d)\n", ret);
141 return 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);
152 if (ret) {
153 pr_err("Failed to register clocksource (%d)\n", ret);
154 return ret;
157 owl_timer_reset(owl_clkevt_base);
159 ret = request_irq(timer1_irq, owl_timer1_interrupt, IRQF_TIMER,
160 "owl-timer", &owl_clockevent);
161 if (ret) {
162 pr_err("failed to request irq %d\n", timer1_irq);
163 return ret;
166 owl_clockevent.cpumask = cpumask_of(0);
167 owl_clockevent.irq = timer1_irq;
169 clockevents_config_and_register(&owl_clockevent, rate,
170 0xf, 0xffffffff);
172 return 0;
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);