blob: 33f370dbd0d62f533a0ce0fa2b1c1d674968f677 [file] [log] [blame]
Daniel Lezcano468b8c42015-01-25 22:06:02 +01001/*
2 * Rockchip timer support
3 *
4 * Copyright (C) Daniel Lezcano <daniel.lezcano@linaro.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10#include <linux/clk.h>
11#include <linux/clockchips.h>
12#include <linux/init.h>
13#include <linux/interrupt.h>
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030014#include <linux/sched_clock.h>
15#include <linux/slab.h>
Daniel Lezcano468b8c42015-01-25 22:06:02 +010016#include <linux/of.h>
17#include <linux/of_address.h>
18#include <linux/of_irq.h>
19
20#define TIMER_NAME "rk_timer"
21
Caesar Wanga0d22162015-09-25 10:14:56 +080022#define TIMER_LOAD_COUNT0 0x00
23#define TIMER_LOAD_COUNT1 0x04
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030024#define TIMER_CURRENT_VALUE0 0x08
25#define TIMER_CURRENT_VALUE1 0x0C
Huang, Taobe6af452016-06-16 16:00:08 +020026#define TIMER_CONTROL_REG3288 0x10
27#define TIMER_CONTROL_REG3399 0x1c
Caesar Wanga0d22162015-09-25 10:14:56 +080028#define TIMER_INT_STATUS 0x18
Daniel Lezcano468b8c42015-01-25 22:06:02 +010029
Caesar Wanga0d22162015-09-25 10:14:56 +080030#define TIMER_DISABLE 0x0
31#define TIMER_ENABLE 0x1
32#define TIMER_MODE_FREE_RUNNING (0 << 1)
33#define TIMER_MODE_USER_DEFINED_COUNT (1 << 1)
34#define TIMER_INT_UNMASK (1 << 2)
Daniel Lezcano468b8c42015-01-25 22:06:02 +010035
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030036struct rk_timer {
Daniel Lezcano468b8c42015-01-25 22:06:02 +010037 void __iomem *base;
Huang, Taobe6af452016-06-16 16:00:08 +020038 void __iomem *ctrl;
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030039 struct clk *clk;
40 struct clk *pclk;
Daniel Lezcano468b8c42015-01-25 22:06:02 +010041 u32 freq;
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030042 int irq;
Daniel Lezcano468b8c42015-01-25 22:06:02 +010043};
44
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030045struct rk_clkevt {
46 struct clock_event_device ce;
47 struct rk_timer timer;
48};
Daniel Lezcano468b8c42015-01-25 22:06:02 +010049
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030050static struct rk_clkevt *rk_clkevt;
51static struct rk_timer *rk_clksrc;
52
53static inline struct rk_timer *rk_timer(struct clock_event_device *ce)
Daniel Lezcano468b8c42015-01-25 22:06:02 +010054{
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030055 return &container_of(ce, struct rk_clkevt, ce)->timer;
Daniel Lezcano468b8c42015-01-25 22:06:02 +010056}
57
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030058static inline void rk_timer_disable(struct rk_timer *timer)
Daniel Lezcano468b8c42015-01-25 22:06:02 +010059{
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030060 writel_relaxed(TIMER_DISABLE, timer->ctrl);
Daniel Lezcano468b8c42015-01-25 22:06:02 +010061}
62
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030063static inline void rk_timer_enable(struct rk_timer *timer, u32 flags)
Huang, Taobe6af452016-06-16 16:00:08 +020064{
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030065 writel_relaxed(TIMER_ENABLE | flags, timer->ctrl);
Daniel Lezcano468b8c42015-01-25 22:06:02 +010066}
67
68static void rk_timer_update_counter(unsigned long cycles,
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030069 struct rk_timer *timer)
Daniel Lezcano468b8c42015-01-25 22:06:02 +010070{
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030071 writel_relaxed(cycles, timer->base + TIMER_LOAD_COUNT0);
72 writel_relaxed(0, timer->base + TIMER_LOAD_COUNT1);
Daniel Lezcano468b8c42015-01-25 22:06:02 +010073}
74
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030075static void rk_timer_interrupt_clear(struct rk_timer *timer)
Daniel Lezcano468b8c42015-01-25 22:06:02 +010076{
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030077 writel_relaxed(1, timer->base + TIMER_INT_STATUS);
Daniel Lezcano468b8c42015-01-25 22:06:02 +010078}
79
80static inline int rk_timer_set_next_event(unsigned long cycles,
81 struct clock_event_device *ce)
82{
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030083 struct rk_timer *timer = rk_timer(ce);
84
85 rk_timer_disable(timer);
86 rk_timer_update_counter(cycles, timer);
87 rk_timer_enable(timer, TIMER_MODE_USER_DEFINED_COUNT |
88 TIMER_INT_UNMASK);
Daniel Lezcano468b8c42015-01-25 22:06:02 +010089 return 0;
90}
91
Viresh Kumar99b3fa72015-06-18 16:24:32 +053092static int rk_timer_shutdown(struct clock_event_device *ce)
Daniel Lezcano468b8c42015-01-25 22:06:02 +010093{
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +030094 struct rk_timer *timer = rk_timer(ce);
95
96 rk_timer_disable(timer);
Viresh Kumar99b3fa72015-06-18 16:24:32 +053097 return 0;
98}
99
100static int rk_timer_set_periodic(struct clock_event_device *ce)
101{
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300102 struct rk_timer *timer = rk_timer(ce);
103
104 rk_timer_disable(timer);
105 rk_timer_update_counter(timer->freq / HZ - 1, timer);
106 rk_timer_enable(timer, TIMER_MODE_FREE_RUNNING | TIMER_INT_UNMASK);
Viresh Kumar99b3fa72015-06-18 16:24:32 +0530107 return 0;
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100108}
109
110static irqreturn_t rk_timer_interrupt(int irq, void *dev_id)
111{
112 struct clock_event_device *ce = dev_id;
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300113 struct rk_timer *timer = rk_timer(ce);
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100114
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300115 rk_timer_interrupt_clear(timer);
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100116
Viresh Kumar99b3fa72015-06-18 16:24:32 +0530117 if (clockevent_state_oneshot(ce))
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300118 rk_timer_disable(timer);
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100119
120 ce->event_handler(ce);
121
122 return IRQ_HANDLED;
123}
124
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300125static u64 notrace rk_timer_sched_read(void)
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100126{
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300127 return ~readl_relaxed(rk_clksrc->base + TIMER_CURRENT_VALUE0);
128}
129
130static int __init
131rk_timer_probe(struct rk_timer *timer, struct device_node *np)
132{
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100133 struct clk *timer_clk;
134 struct clk *pclk;
Daniel Lezcano8bdd5a22016-05-31 17:28:55 +0200135 int ret = -EINVAL, irq;
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300136 u32 ctrl_reg = TIMER_CONTROL_REG3288;
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100137
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300138 timer->base = of_iomap(np, 0);
139 if (!timer->base) {
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100140 pr_err("Failed to get base address for '%s'\n", TIMER_NAME);
Daniel Lezcano8bdd5a22016-05-31 17:28:55 +0200141 return -ENXIO;
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100142 }
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300143
144 if (of_device_is_compatible(np, "rockchip,rk3399-timer"))
145 ctrl_reg = TIMER_CONTROL_REG3399;
146
147 timer->ctrl = timer->base + ctrl_reg;
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100148
149 pclk = of_clk_get_by_name(np, "pclk");
150 if (IS_ERR(pclk)) {
Daniel Lezcano8bdd5a22016-05-31 17:28:55 +0200151 ret = PTR_ERR(pclk);
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100152 pr_err("Failed to get pclk for '%s'\n", TIMER_NAME);
Shawn Lin522ed952016-02-15 09:02:09 +0800153 goto out_unmap;
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100154 }
155
Daniel Lezcano8bdd5a22016-05-31 17:28:55 +0200156 ret = clk_prepare_enable(pclk);
157 if (ret) {
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100158 pr_err("Failed to enable pclk for '%s'\n", TIMER_NAME);
Shawn Lin522ed952016-02-15 09:02:09 +0800159 goto out_unmap;
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100160 }
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300161 timer->pclk = pclk;
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100162
163 timer_clk = of_clk_get_by_name(np, "timer");
164 if (IS_ERR(timer_clk)) {
Daniel Lezcano8bdd5a22016-05-31 17:28:55 +0200165 ret = PTR_ERR(timer_clk);
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100166 pr_err("Failed to get timer clock for '%s'\n", TIMER_NAME);
Shawn Lin522ed952016-02-15 09:02:09 +0800167 goto out_timer_clk;
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100168 }
169
Daniel Lezcano8bdd5a22016-05-31 17:28:55 +0200170 ret = clk_prepare_enable(timer_clk);
171 if (ret) {
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100172 pr_err("Failed to enable timer clock\n");
Shawn Lin522ed952016-02-15 09:02:09 +0800173 goto out_timer_clk;
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100174 }
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300175 timer->clk = timer_clk;
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100176
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300177 timer->freq = clk_get_rate(timer_clk);
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100178
179 irq = irq_of_parse_and_map(np, 0);
Daniel Lezcanoccc42592015-09-20 07:00:10 -0700180 if (!irq) {
Daniel Lezcano8bdd5a22016-05-31 17:28:55 +0200181 ret = -EINVAL;
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100182 pr_err("Failed to map interrupts for '%s'\n", TIMER_NAME);
Shawn Lin522ed952016-02-15 09:02:09 +0800183 goto out_irq;
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100184 }
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300185 timer->irq = irq;
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100186
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300187 rk_timer_interrupt_clear(timer);
188 rk_timer_disable(timer);
Daniel Lezcano8bdd5a22016-05-31 17:28:55 +0200189 return 0;
Shawn Lin522ed952016-02-15 09:02:09 +0800190
191out_irq:
192 clk_disable_unprepare(timer_clk);
193out_timer_clk:
194 clk_disable_unprepare(pclk);
195out_unmap:
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300196 iounmap(timer->base);
Daniel Lezcano8bdd5a22016-05-31 17:28:55 +0200197
198 return ret;
Daniel Lezcano468b8c42015-01-25 22:06:02 +0100199}
Caesar Wanga0d22162015-09-25 10:14:56 +0800200
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300201static void __init rk_timer_cleanup(struct rk_timer *timer)
Huang, Taobe6af452016-06-16 16:00:08 +0200202{
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300203 clk_disable_unprepare(timer->clk);
204 clk_disable_unprepare(timer->pclk);
205 iounmap(timer->base);
Huang, Taobe6af452016-06-16 16:00:08 +0200206}
207
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300208static int __init rk_clkevt_init(struct device_node *np)
Huang, Taobe6af452016-06-16 16:00:08 +0200209{
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300210 struct clock_event_device *ce;
211 int ret = -EINVAL;
212
213 rk_clkevt = kzalloc(sizeof(struct rk_clkevt), GFP_KERNEL);
214 if (!rk_clkevt) {
215 ret = -ENOMEM;
216 goto out;
217 }
218
219 ret = rk_timer_probe(&rk_clkevt->timer, np);
220 if (ret)
221 goto out_probe;
222
223 ce = &rk_clkevt->ce;
224 ce->name = TIMER_NAME;
225 ce->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT |
226 CLOCK_EVT_FEAT_DYNIRQ;
227 ce->set_next_event = rk_timer_set_next_event;
228 ce->set_state_shutdown = rk_timer_shutdown;
229 ce->set_state_periodic = rk_timer_set_periodic;
230 ce->irq = rk_clkevt->timer.irq;
231 ce->cpumask = cpu_possible_mask;
232 ce->rating = 250;
233
234 ret = request_irq(rk_clkevt->timer.irq, rk_timer_interrupt, IRQF_TIMER,
235 TIMER_NAME, ce);
236 if (ret) {
237 pr_err("Failed to initialize '%s': %d\n",
238 TIMER_NAME, ret);
239 goto out_irq;
240 }
241
242 clockevents_config_and_register(&rk_clkevt->ce,
243 rk_clkevt->timer.freq, 1, UINT_MAX);
244 return 0;
245
246out_irq:
247 rk_timer_cleanup(&rk_clkevt->timer);
248out_probe:
249 kfree(rk_clkevt);
250out:
251 /* Leave rk_clkevt not NULL to prevent future init */
252 rk_clkevt = ERR_PTR(ret);
253 return ret;
Huang, Taobe6af452016-06-16 16:00:08 +0200254}
255
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300256static int __init rk_clksrc_init(struct device_node *np)
257{
258 int ret = -EINVAL;
259
260 rk_clksrc = kzalloc(sizeof(struct rk_timer), GFP_KERNEL);
261 if (!rk_clksrc) {
262 ret = -ENOMEM;
263 goto out;
264 }
265
266 ret = rk_timer_probe(rk_clksrc, np);
267 if (ret)
268 goto out_probe;
269
270 rk_timer_update_counter(UINT_MAX, rk_clksrc);
271 rk_timer_enable(rk_clksrc, 0);
272
273 ret = clocksource_mmio_init(rk_clksrc->base + TIMER_CURRENT_VALUE0,
274 TIMER_NAME, rk_clksrc->freq, 250, 32,
275 clocksource_mmio_readl_down);
276 if (ret) {
Arvind Yadav25548282017-09-25 13:46:41 +0530277 pr_err("Failed to register clocksource\n");
Alexander Kochetkov5e0a39d2017-01-31 15:43:14 +0300278 goto out_clocksource;
279 }
280
281 sched_clock_register(rk_timer_sched_read, 32, rk_clksrc->freq);
282 return 0;
283
284out_clocksource:
285 rk_timer_cleanup(rk_clksrc);
286out_probe:
287 kfree(rk_clksrc);
288out:
289 /* Leave rk_clksrc not NULL to prevent future init */
290 rk_clksrc = ERR_PTR(ret);
291 return ret;
292}
293
294static int __init rk_timer_init(struct device_node *np)
295{
296 if (!rk_clkevt)
297 return rk_clkevt_init(np);
298
299 if (!rk_clksrc)
300 return rk_clksrc_init(np);
301
302 pr_err("Too many timer definitions for '%s'\n", TIMER_NAME);
303 return -EINVAL;
304}
305
Daniel Lezcano17273392017-05-26 16:56:11 +0200306TIMER_OF_DECLARE(rk3288_timer, "rockchip,rk3288-timer", rk_timer_init);
307TIMER_OF_DECLARE(rk3399_timer, "rockchip,rk3399-timer", rk_timer_init);