blob: 1f95d0aca08f2984cd8f2014e913c41018b71321 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Daniel Lezcano468b8c42015-01-25 22:06:02 +01002/*
3 * Rockchip timer support
4 *
5 * Copyright (C) Daniel Lezcano <daniel.lezcano@linaro.org>
Daniel Lezcano468b8c42015-01-25 22:06:02 +01006 */
7#include <linux/clk.h>
8#include <linux/clockchips.h>
9#include <linux/init.h>
10#include <linux/interrupt.h>
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030011#include <linux/sched_clock.h>
12#include <linux/slab.h>
Daniel Lezcano468b8c42015-01-25 22:06:02 +010013#include <linux/of.h>
14#include <linux/of_address.h>
15#include <linux/of_irq.h>
16
17#define TIMER_NAME "rk_timer"
18
Caesar Wanga0d22162015-09-25 10:14:56 +080019#define TIMER_LOAD_COUNT0 0x00
20#define TIMER_LOAD_COUNT1 0x04
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030021#define TIMER_CURRENT_VALUE0 0x08
22#define TIMER_CURRENT_VALUE1 0x0C
Huang, Taobe6af452016-06-16 16:00:08 +020023#define TIMER_CONTROL_REG3288 0x10
24#define TIMER_CONTROL_REG3399 0x1c
Caesar Wanga0d22162015-09-25 10:14:56 +080025#define TIMER_INT_STATUS 0x18
Daniel Lezcano468b8c42015-01-25 22:06:02 +010026
Caesar Wanga0d22162015-09-25 10:14:56 +080027#define TIMER_DISABLE 0x0
28#define TIMER_ENABLE 0x1
29#define TIMER_MODE_FREE_RUNNING (0 << 1)
30#define TIMER_MODE_USER_DEFINED_COUNT (1 << 1)
31#define TIMER_INT_UNMASK (1 << 2)
Daniel Lezcano468b8c42015-01-25 22:06:02 +010032
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030033struct rk_timer {
Daniel Lezcano468b8c42015-01-25 22:06:02 +010034 void __iomem *base;
Huang, Taobe6af452016-06-16 16:00:08 +020035 void __iomem *ctrl;
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030036 struct clk *clk;
37 struct clk *pclk;
Daniel Lezcano468b8c42015-01-25 22:06:02 +010038 u32 freq;
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030039 int irq;
Daniel Lezcano468b8c42015-01-25 22:06:02 +010040};
41
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030042struct rk_clkevt {
43 struct clock_event_device ce;
44 struct rk_timer timer;
45};
Daniel Lezcano468b8c42015-01-25 22:06:02 +010046
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030047static struct rk_clkevt *rk_clkevt;
48static struct rk_timer *rk_clksrc;
49
50static inline struct rk_timer *rk_timer(struct clock_event_device *ce)
Daniel Lezcano468b8c42015-01-25 22:06:02 +010051{
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030052 return &container_of(ce, struct rk_clkevt, ce)->timer;
Daniel Lezcano468b8c42015-01-25 22:06:02 +010053}
54
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030055static inline void rk_timer_disable(struct rk_timer *timer)
Daniel Lezcano468b8c42015-01-25 22:06:02 +010056{
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030057 writel_relaxed(TIMER_DISABLE, timer->ctrl);
Daniel Lezcano468b8c42015-01-25 22:06:02 +010058}
59
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030060static inline void rk_timer_enable(struct rk_timer *timer, u32 flags)
Huang, Taobe6af452016-06-16 16:00:08 +020061{
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030062 writel_relaxed(TIMER_ENABLE | flags, timer->ctrl);
Daniel Lezcano468b8c42015-01-25 22:06:02 +010063}
64
65static void rk_timer_update_counter(unsigned long cycles,
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030066 struct rk_timer *timer)
Daniel Lezcano468b8c42015-01-25 22:06:02 +010067{
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030068 writel_relaxed(cycles, timer->base + TIMER_LOAD_COUNT0);
69 writel_relaxed(0, timer->base + TIMER_LOAD_COUNT1);
Daniel Lezcano468b8c42015-01-25 22:06:02 +010070}
71
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030072static void rk_timer_interrupt_clear(struct rk_timer *timer)
Daniel Lezcano468b8c42015-01-25 22:06:02 +010073{
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030074 writel_relaxed(1, timer->base + TIMER_INT_STATUS);
Daniel Lezcano468b8c42015-01-25 22:06:02 +010075}
76
77static inline int rk_timer_set_next_event(unsigned long cycles,
78 struct clock_event_device *ce)
79{
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030080 struct rk_timer *timer = rk_timer(ce);
81
82 rk_timer_disable(timer);
83 rk_timer_update_counter(cycles, timer);
84 rk_timer_enable(timer, TIMER_MODE_USER_DEFINED_COUNT |
85 TIMER_INT_UNMASK);
Daniel Lezcano468b8c42015-01-25 22:06:02 +010086 return 0;
87}
88
Viresh Kumar99b3fa72015-06-18 16:24:32 +053089static int rk_timer_shutdown(struct clock_event_device *ce)
Daniel Lezcano468b8c42015-01-25 22:06:02 +010090{
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030091 struct rk_timer *timer = rk_timer(ce);
92
93 rk_timer_disable(timer);
Viresh Kumar99b3fa72015-06-18 16:24:32 +053094 return 0;
95}
96
97static int rk_timer_set_periodic(struct clock_event_device *ce)
98{
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030099 struct rk_timer *timer = rk_timer(ce);
100
101 rk_timer_disable(timer);
102 rk_timer_update_counter(timer->freq / HZ - 1, timer);
103 rk_timer_enable(timer, TIMER_MODE_FREE_RUNNING | TIMER_INT_UNMASK);
Viresh Kumar99b3fa72015-06-18 16:24:32 +0530104 return 0;
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100105}
106
107static irqreturn_t rk_timer_interrupt(int irq, void *dev_id)
108{
109 struct clock_event_device *ce = dev_id;
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300110 struct rk_timer *timer = rk_timer(ce);
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100111
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300112 rk_timer_interrupt_clear(timer);
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100113
Viresh Kumar99b3fa72015-06-18 16:24:32 +0530114 if (clockevent_state_oneshot(ce))
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300115 rk_timer_disable(timer);
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100116
117 ce->event_handler(ce);
118
119 return IRQ_HANDLED;
120}
121
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300122static u64 notrace rk_timer_sched_read(void)
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100123{
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300124 return ~readl_relaxed(rk_clksrc->base + TIMER_CURRENT_VALUE0);
125}
126
127static int __init
128rk_timer_probe(struct rk_timer *timer, struct device_node *np)
129{
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100130 struct clk *timer_clk;
131 struct clk *pclk;
Daniel Lezcano8bdd5a22016-05-31 17:28:55 +0200132 int ret = -EINVAL, irq;
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300133 u32 ctrl_reg = TIMER_CONTROL_REG3288;
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100134
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300135 timer->base = of_iomap(np, 0);
136 if (!timer->base) {
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100137 pr_err("Failed to get base address for '%s'\n", TIMER_NAME);
Daniel Lezcano8bdd5a22016-05-31 17:28:55 +0200138 return -ENXIO;
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100139 }
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300140
141 if (of_device_is_compatible(np, "rockchip,rk3399-timer"))
142 ctrl_reg = TIMER_CONTROL_REG3399;
143
144 timer->ctrl = timer->base + ctrl_reg;
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100145
146 pclk = of_clk_get_by_name(np, "pclk");
147 if (IS_ERR(pclk)) {
Daniel Lezcano8bdd5a22016-05-31 17:28:55 +0200148 ret = PTR_ERR(pclk);
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100149 pr_err("Failed to get pclk for '%s'\n", TIMER_NAME);
Shawn Lin522ed952016-02-15 09:02:09 +0800150 goto out_unmap;
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100151 }
152
Daniel Lezcano8bdd5a22016-05-31 17:28:55 +0200153 ret = clk_prepare_enable(pclk);
154 if (ret) {
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100155 pr_err("Failed to enable pclk for '%s'\n", TIMER_NAME);
Shawn Lin522ed952016-02-15 09:02:09 +0800156 goto out_unmap;
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100157 }
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300158 timer->pclk = pclk;
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100159
160 timer_clk = of_clk_get_by_name(np, "timer");
161 if (IS_ERR(timer_clk)) {
Daniel Lezcano8bdd5a22016-05-31 17:28:55 +0200162 ret = PTR_ERR(timer_clk);
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100163 pr_err("Failed to get timer clock for '%s'\n", TIMER_NAME);
Shawn Lin522ed952016-02-15 09:02:09 +0800164 goto out_timer_clk;
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100165 }
166
Daniel Lezcano8bdd5a22016-05-31 17:28:55 +0200167 ret = clk_prepare_enable(timer_clk);
168 if (ret) {
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100169 pr_err("Failed to enable timer clock\n");
Shawn Lin522ed952016-02-15 09:02:09 +0800170 goto out_timer_clk;
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100171 }
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300172 timer->clk = timer_clk;
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100173
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300174 timer->freq = clk_get_rate(timer_clk);
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100175
176 irq = irq_of_parse_and_map(np, 0);
Daniel Lezcanoccc42592015-09-20 07:00:10 -0700177 if (!irq) {
Daniel Lezcano8bdd5a22016-05-31 17:28:55 +0200178 ret = -EINVAL;
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100179 pr_err("Failed to map interrupts for '%s'\n", TIMER_NAME);
Shawn Lin522ed952016-02-15 09:02:09 +0800180 goto out_irq;
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100181 }
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300182 timer->irq = irq;
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100183
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300184 rk_timer_interrupt_clear(timer);
185 rk_timer_disable(timer);
Daniel Lezcano8bdd5a22016-05-31 17:28:55 +0200186 return 0;
Shawn Lin522ed952016-02-15 09:02:09 +0800187
188out_irq:
189 clk_disable_unprepare(timer_clk);
190out_timer_clk:
191 clk_disable_unprepare(pclk);
192out_unmap:
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300193 iounmap(timer->base);
Daniel Lezcano8bdd5a22016-05-31 17:28:55 +0200194
195 return ret;
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100196}
Caesar Wanga0d22162015-09-25 10:14:56 +0800197
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300198static void __init rk_timer_cleanup(struct rk_timer *timer)
Huang, Taobe6af452016-06-16 16:00:08 +0200199{
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300200 clk_disable_unprepare(timer->clk);
201 clk_disable_unprepare(timer->pclk);
202 iounmap(timer->base);
Huang, Taobe6af452016-06-16 16:00:08 +0200203}
204
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300205static int __init rk_clkevt_init(struct device_node *np)
Huang, Taobe6af452016-06-16 16:00:08 +0200206{
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300207 struct clock_event_device *ce;
208 int ret = -EINVAL;
209
210 rk_clkevt = kzalloc(sizeof(struct rk_clkevt), GFP_KERNEL);
211 if (!rk_clkevt) {
212 ret = -ENOMEM;
213 goto out;
214 }
215
216 ret = rk_timer_probe(&rk_clkevt->timer, np);
217 if (ret)
218 goto out_probe;
219
220 ce = &rk_clkevt->ce;
221 ce->name = TIMER_NAME;
222 ce->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT |
223 CLOCK_EVT_FEAT_DYNIRQ;
224 ce->set_next_event = rk_timer_set_next_event;
225 ce->set_state_shutdown = rk_timer_shutdown;
226 ce->set_state_periodic = rk_timer_set_periodic;
227 ce->irq = rk_clkevt->timer.irq;
228 ce->cpumask = cpu_possible_mask;
229 ce->rating = 250;
230
231 ret = request_irq(rk_clkevt->timer.irq, rk_timer_interrupt, IRQF_TIMER,
232 TIMER_NAME, ce);
233 if (ret) {
234 pr_err("Failed to initialize '%s': %d\n",
235 TIMER_NAME, ret);
236 goto out_irq;
237 }
238
239 clockevents_config_and_register(&rk_clkevt->ce,
240 rk_clkevt->timer.freq, 1, UINT_MAX);
241 return 0;
242
243out_irq:
244 rk_timer_cleanup(&rk_clkevt->timer);
245out_probe:
246 kfree(rk_clkevt);
247out:
248 /* Leave rk_clkevt not NULL to prevent future init */
249 rk_clkevt = ERR_PTR(ret);
250 return ret;
Huang, Taobe6af452016-06-16 16:00:08 +0200251}
252
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300253static int __init rk_clksrc_init(struct device_node *np)
254{
255 int ret = -EINVAL;
256
257 rk_clksrc = kzalloc(sizeof(struct rk_timer), GFP_KERNEL);
258 if (!rk_clksrc) {
259 ret = -ENOMEM;
260 goto out;
261 }
262
263 ret = rk_timer_probe(rk_clksrc, np);
264 if (ret)
265 goto out_probe;
266
267 rk_timer_update_counter(UINT_MAX, rk_clksrc);
268 rk_timer_enable(rk_clksrc, 0);
269
270 ret = clocksource_mmio_init(rk_clksrc->base + TIMER_CURRENT_VALUE0,
271 TIMER_NAME, rk_clksrc->freq, 250, 32,
272 clocksource_mmio_readl_down);
273 if (ret) {
Arvind Yadav25548282017-09-25 13:46:41 +0530274 pr_err("Failed to register clocksource\n");
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300275 goto out_clocksource;
276 }
277
278 sched_clock_register(rk_timer_sched_read, 32, rk_clksrc->freq);
279 return 0;
280
281out_clocksource:
282 rk_timer_cleanup(rk_clksrc);
283out_probe:
284 kfree(rk_clksrc);
285out:
286 /* Leave rk_clksrc not NULL to prevent future init */
287 rk_clksrc = ERR_PTR(ret);
288 return ret;
289}
290
291static int __init rk_timer_init(struct device_node *np)
292{
293 if (!rk_clkevt)
294 return rk_clkevt_init(np);
295
296 if (!rk_clksrc)
297 return rk_clksrc_init(np);
298
299 pr_err("Too many timer definitions for '%s'\n", TIMER_NAME);
300 return -EINVAL;
301}
302
Daniel Lezcano17273392017-05-26 16:56:11 +0200303TIMER_OF_DECLARE(rk3288_timer, "rockchip,rk3288-timer", rk_timer_init);
304TIMER_OF_DECLARE(rk3399_timer, "rockchip,rk3399-timer", rk_timer_init);