1 // SPDX-License-Identifier: GPL-2.0-only
3 * Rockchip timer support
5 * Copyright (C) Daniel Lezcano <daniel.lezcano@linaro.org>
8 #include <linux/clockchips.h>
9 #include <linux/init.h>
10 #include <linux/interrupt.h>
11 #include <linux/sched_clock.h>
12 #include <linux/slab.h>
14 #include <linux/of_address.h>
15 #include <linux/of_irq.h>
17 #define TIMER_NAME "rk_timer"
19 #define TIMER_LOAD_COUNT0 0x00
20 #define TIMER_LOAD_COUNT1 0x04
21 #define TIMER_CURRENT_VALUE0 0x08
22 #define TIMER_CURRENT_VALUE1 0x0C
23 #define TIMER_CONTROL_REG3288 0x10
24 #define TIMER_CONTROL_REG3399 0x1c
25 #define TIMER_INT_STATUS 0x18
27 #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)
43 struct clock_event_device ce
;
44 struct rk_timer timer
;
47 static struct rk_clkevt
*rk_clkevt
;
48 static struct rk_timer
*rk_clksrc
;
50 static inline struct rk_timer
*rk_timer(struct clock_event_device
*ce
)
52 return &container_of(ce
, struct rk_clkevt
, ce
)->timer
;
55 static inline void rk_timer_disable(struct rk_timer
*timer
)
57 writel_relaxed(TIMER_DISABLE
, timer
->ctrl
);
60 static inline void rk_timer_enable(struct rk_timer
*timer
, u32 flags
)
62 writel_relaxed(TIMER_ENABLE
| flags
, timer
->ctrl
);
65 static void rk_timer_update_counter(unsigned long cycles
,
66 struct rk_timer
*timer
)
68 writel_relaxed(cycles
, timer
->base
+ TIMER_LOAD_COUNT0
);
69 writel_relaxed(0, timer
->base
+ TIMER_LOAD_COUNT1
);
72 static void rk_timer_interrupt_clear(struct rk_timer
*timer
)
74 writel_relaxed(1, timer
->base
+ TIMER_INT_STATUS
);
77 static inline int rk_timer_set_next_event(unsigned long cycles
,
78 struct clock_event_device
*ce
)
80 struct rk_timer
*timer
= rk_timer(ce
);
82 rk_timer_disable(timer
);
83 rk_timer_update_counter(cycles
, timer
);
84 rk_timer_enable(timer
, TIMER_MODE_USER_DEFINED_COUNT
|
89 static int rk_timer_shutdown(struct clock_event_device
*ce
)
91 struct rk_timer
*timer
= rk_timer(ce
);
93 rk_timer_disable(timer
);
97 static int rk_timer_set_periodic(struct clock_event_device
*ce
)
99 struct rk_timer
*timer
= rk_timer(ce
);
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
);
107 static irqreturn_t
rk_timer_interrupt(int irq
, void *dev_id
)
109 struct clock_event_device
*ce
= dev_id
;
110 struct rk_timer
*timer
= rk_timer(ce
);
112 rk_timer_interrupt_clear(timer
);
114 if (clockevent_state_oneshot(ce
))
115 rk_timer_disable(timer
);
117 ce
->event_handler(ce
);
122 static u64 notrace
rk_timer_sched_read(void)
124 return ~readl_relaxed(rk_clksrc
->base
+ TIMER_CURRENT_VALUE0
);
128 rk_timer_probe(struct rk_timer
*timer
, struct device_node
*np
)
130 struct clk
*timer_clk
;
132 int ret
= -EINVAL
, irq
;
133 u32 ctrl_reg
= TIMER_CONTROL_REG3288
;
135 timer
->base
= of_iomap(np
, 0);
137 pr_err("Failed to get base address for '%s'\n", TIMER_NAME
);
141 if (of_device_is_compatible(np
, "rockchip,rk3399-timer"))
142 ctrl_reg
= TIMER_CONTROL_REG3399
;
144 timer
->ctrl
= timer
->base
+ ctrl_reg
;
146 pclk
= of_clk_get_by_name(np
, "pclk");
149 pr_err("Failed to get pclk for '%s'\n", TIMER_NAME
);
153 ret
= clk_prepare_enable(pclk
);
155 pr_err("Failed to enable pclk for '%s'\n", TIMER_NAME
);
160 timer_clk
= of_clk_get_by_name(np
, "timer");
161 if (IS_ERR(timer_clk
)) {
162 ret
= PTR_ERR(timer_clk
);
163 pr_err("Failed to get timer clock for '%s'\n", TIMER_NAME
);
167 ret
= clk_prepare_enable(timer_clk
);
169 pr_err("Failed to enable timer clock\n");
172 timer
->clk
= timer_clk
;
174 timer
->freq
= clk_get_rate(timer_clk
);
176 irq
= irq_of_parse_and_map(np
, 0);
179 pr_err("Failed to map interrupts for '%s'\n", TIMER_NAME
);
184 rk_timer_interrupt_clear(timer
);
185 rk_timer_disable(timer
);
189 clk_disable_unprepare(timer_clk
);
191 clk_disable_unprepare(pclk
);
193 iounmap(timer
->base
);
198 static void __init
rk_timer_cleanup(struct rk_timer
*timer
)
200 clk_disable_unprepare(timer
->clk
);
201 clk_disable_unprepare(timer
->pclk
);
202 iounmap(timer
->base
);
205 static int __init
rk_clkevt_init(struct device_node
*np
)
207 struct clock_event_device
*ce
;
210 rk_clkevt
= kzalloc(sizeof(struct rk_clkevt
), GFP_KERNEL
);
216 ret
= rk_timer_probe(&rk_clkevt
->timer
, np
);
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
;
231 ret
= request_irq(rk_clkevt
->timer
.irq
, rk_timer_interrupt
, IRQF_TIMER
,
234 pr_err("Failed to initialize '%s': %d\n",
239 clockevents_config_and_register(&rk_clkevt
->ce
,
240 rk_clkevt
->timer
.freq
, 1, UINT_MAX
);
244 rk_timer_cleanup(&rk_clkevt
->timer
);
248 /* Leave rk_clkevt not NULL to prevent future init */
249 rk_clkevt
= ERR_PTR(ret
);
253 static int __init
rk_clksrc_init(struct device_node
*np
)
257 rk_clksrc
= kzalloc(sizeof(struct rk_timer
), GFP_KERNEL
);
263 ret
= rk_timer_probe(rk_clksrc
, np
);
267 rk_timer_update_counter(UINT_MAX
, rk_clksrc
);
268 rk_timer_enable(rk_clksrc
, 0);
270 ret
= clocksource_mmio_init(rk_clksrc
->base
+ TIMER_CURRENT_VALUE0
,
271 TIMER_NAME
, rk_clksrc
->freq
, 250, 32,
272 clocksource_mmio_readl_down
);
274 pr_err("Failed to register clocksource\n");
275 goto out_clocksource
;
278 sched_clock_register(rk_timer_sched_read
, 32, rk_clksrc
->freq
);
282 rk_timer_cleanup(rk_clksrc
);
286 /* Leave rk_clksrc not NULL to prevent future init */
287 rk_clksrc
= ERR_PTR(ret
);
291 static int __init
rk_timer_init(struct device_node
*np
)
294 return rk_clkevt_init(np
);
297 return rk_clksrc_init(np
);
299 pr_err("Too many timer definitions for '%s'\n", TIMER_NAME
);
303 TIMER_OF_DECLARE(rk3288_timer
, "rockchip,rk3288-timer", rk_timer_init
);
304 TIMER_OF_DECLARE(rk3399_timer
, "rockchip,rk3399-timer", rk_timer_init
);