1 // SPDX-License-Identifier: GPL-2.0-only
3 * TI DaVinci clocksource driver
5 * Copyright (C) 2019 Texas Instruments
6 * Author: Bartosz Golaszewski <bgolaszewski@baylibre.com>
7 * (with tiny parts adopted from code by Kevin Hilman <khilman@baylibre.com>)
10 #define pr_fmt(fmt) "%s: " fmt, __func__
12 #include <linux/clk.h>
13 #include <linux/clockchips.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/of_address.h>
17 #include <linux/of_irq.h>
18 #include <linux/sched_clock.h>
20 #include <clocksource/timer-davinci.h>
22 #define DAVINCI_TIMER_REG_TIM12 0x10
23 #define DAVINCI_TIMER_REG_TIM34 0x14
24 #define DAVINCI_TIMER_REG_PRD12 0x18
25 #define DAVINCI_TIMER_REG_PRD34 0x1c
26 #define DAVINCI_TIMER_REG_TCR 0x20
27 #define DAVINCI_TIMER_REG_TGCR 0x24
29 #define DAVINCI_TIMER_TIMMODE_MASK GENMASK(3, 2)
30 #define DAVINCI_TIMER_RESET_MASK GENMASK(1, 0)
31 #define DAVINCI_TIMER_TIMMODE_32BIT_UNCHAINED BIT(2)
32 #define DAVINCI_TIMER_UNRESET GENMASK(1, 0)
34 #define DAVINCI_TIMER_ENAMODE_MASK GENMASK(1, 0)
35 #define DAVINCI_TIMER_ENAMODE_DISABLED 0x00
36 #define DAVINCI_TIMER_ENAMODE_ONESHOT BIT(0)
37 #define DAVINCI_TIMER_ENAMODE_PERIODIC BIT(1)
39 #define DAVINCI_TIMER_ENAMODE_SHIFT_TIM12 6
40 #define DAVINCI_TIMER_ENAMODE_SHIFT_TIM34 22
42 #define DAVINCI_TIMER_MIN_DELTA 0x01
43 #define DAVINCI_TIMER_MAX_DELTA 0xfffffffe
45 #define DAVINCI_TIMER_CLKSRC_BITS 32
47 #define DAVINCI_TIMER_TGCR_DEFAULT \
48 (DAVINCI_TIMER_TIMMODE_32BIT_UNCHAINED | DAVINCI_TIMER_UNRESET)
50 struct davinci_clockevent
{
51 struct clock_event_device dev
;
57 * This must be globally accessible by davinci_timer_read_sched_clock(), so
61 struct clocksource dev
;
64 } davinci_clocksource
;
66 static struct davinci_clockevent
*
67 to_davinci_clockevent(struct clock_event_device
*clockevent
)
69 return container_of(clockevent
, struct davinci_clockevent
, dev
);
73 davinci_clockevent_read(struct davinci_clockevent
*clockevent
,
76 return readl_relaxed(clockevent
->base
+ reg
);
79 static void davinci_clockevent_write(struct davinci_clockevent
*clockevent
,
80 unsigned int reg
, unsigned int val
)
82 writel_relaxed(val
, clockevent
->base
+ reg
);
85 static void davinci_tim12_shutdown(void __iomem
*base
)
89 tcr
= DAVINCI_TIMER_ENAMODE_DISABLED
<<
90 DAVINCI_TIMER_ENAMODE_SHIFT_TIM12
;
92 * This function is only ever called if we're using both timer
93 * halves. In this case TIM34 runs in periodic mode and we must
96 tcr
|= DAVINCI_TIMER_ENAMODE_PERIODIC
<<
97 DAVINCI_TIMER_ENAMODE_SHIFT_TIM34
;
99 writel_relaxed(tcr
, base
+ DAVINCI_TIMER_REG_TCR
);
102 static void davinci_tim12_set_oneshot(void __iomem
*base
)
106 tcr
= DAVINCI_TIMER_ENAMODE_ONESHOT
<<
107 DAVINCI_TIMER_ENAMODE_SHIFT_TIM12
;
109 tcr
|= DAVINCI_TIMER_ENAMODE_PERIODIC
<<
110 DAVINCI_TIMER_ENAMODE_SHIFT_TIM34
;
112 writel_relaxed(tcr
, base
+ DAVINCI_TIMER_REG_TCR
);
115 static int davinci_clockevent_shutdown(struct clock_event_device
*dev
)
117 struct davinci_clockevent
*clockevent
;
119 clockevent
= to_davinci_clockevent(dev
);
121 davinci_tim12_shutdown(clockevent
->base
);
126 static int davinci_clockevent_set_oneshot(struct clock_event_device
*dev
)
128 struct davinci_clockevent
*clockevent
= to_davinci_clockevent(dev
);
130 davinci_clockevent_write(clockevent
, DAVINCI_TIMER_REG_TIM12
, 0x0);
132 davinci_tim12_set_oneshot(clockevent
->base
);
138 davinci_clockevent_set_next_event_std(unsigned long cycles
,
139 struct clock_event_device
*dev
)
141 struct davinci_clockevent
*clockevent
= to_davinci_clockevent(dev
);
143 davinci_clockevent_shutdown(dev
);
145 davinci_clockevent_write(clockevent
, DAVINCI_TIMER_REG_TIM12
, 0x0);
146 davinci_clockevent_write(clockevent
, DAVINCI_TIMER_REG_PRD12
, cycles
);
148 davinci_clockevent_set_oneshot(dev
);
154 davinci_clockevent_set_next_event_cmp(unsigned long cycles
,
155 struct clock_event_device
*dev
)
157 struct davinci_clockevent
*clockevent
= to_davinci_clockevent(dev
);
158 unsigned int curr_time
;
160 curr_time
= davinci_clockevent_read(clockevent
,
161 DAVINCI_TIMER_REG_TIM12
);
162 davinci_clockevent_write(clockevent
,
163 clockevent
->cmp_off
, curr_time
+ cycles
);
168 static irqreturn_t
davinci_timer_irq_timer(int irq
, void *data
)
170 struct davinci_clockevent
*clockevent
= data
;
172 if (!clockevent_state_oneshot(&clockevent
->dev
))
173 davinci_tim12_shutdown(clockevent
->base
);
175 clockevent
->dev
.event_handler(&clockevent
->dev
);
180 static u64 notrace
davinci_timer_read_sched_clock(void)
182 return readl_relaxed(davinci_clocksource
.base
+
183 davinci_clocksource
.tim_off
);
186 static u64
davinci_clocksource_read(struct clocksource
*dev
)
188 return davinci_timer_read_sched_clock();
192 * Standard use-case: we're using tim12 for clockevent and tim34 for
193 * clocksource. The default is making the former run in oneshot mode
194 * and the latter in periodic mode.
196 static void davinci_clocksource_init_tim34(void __iomem
*base
)
200 tcr
= DAVINCI_TIMER_ENAMODE_PERIODIC
<<
201 DAVINCI_TIMER_ENAMODE_SHIFT_TIM34
;
202 tcr
|= DAVINCI_TIMER_ENAMODE_ONESHOT
<<
203 DAVINCI_TIMER_ENAMODE_SHIFT_TIM12
;
205 writel_relaxed(0x0, base
+ DAVINCI_TIMER_REG_TIM34
);
206 writel_relaxed(UINT_MAX
, base
+ DAVINCI_TIMER_REG_PRD34
);
207 writel_relaxed(tcr
, base
+ DAVINCI_TIMER_REG_TCR
);
211 * Special use-case on da830: the DSP may use tim34. We're using tim12 for
212 * both clocksource and clockevent. We set tim12 to periodic and don't touch
215 static void davinci_clocksource_init_tim12(void __iomem
*base
)
219 tcr
= DAVINCI_TIMER_ENAMODE_PERIODIC
<<
220 DAVINCI_TIMER_ENAMODE_SHIFT_TIM12
;
222 writel_relaxed(0x0, base
+ DAVINCI_TIMER_REG_TIM12
);
223 writel_relaxed(UINT_MAX
, base
+ DAVINCI_TIMER_REG_PRD12
);
224 writel_relaxed(tcr
, base
+ DAVINCI_TIMER_REG_TCR
);
227 static void davinci_timer_init(void __iomem
*base
)
229 /* Set clock to internal mode and disable it. */
230 writel_relaxed(0x0, base
+ DAVINCI_TIMER_REG_TCR
);
232 * Reset both 32-bit timers, set no prescaler for timer 34, set the
233 * timer to dual 32-bit unchained mode, unreset both 32-bit timers.
235 writel_relaxed(DAVINCI_TIMER_TGCR_DEFAULT
,
236 base
+ DAVINCI_TIMER_REG_TGCR
);
237 /* Init both counters to zero. */
238 writel_relaxed(0x0, base
+ DAVINCI_TIMER_REG_TIM12
);
239 writel_relaxed(0x0, base
+ DAVINCI_TIMER_REG_TIM34
);
242 int __init
davinci_timer_register(struct clk
*clk
,
243 const struct davinci_timer_cfg
*timer_cfg
)
245 struct davinci_clockevent
*clockevent
;
246 unsigned int tick_rate
;
250 rv
= clk_prepare_enable(clk
);
252 pr_err("Unable to prepare and enable the timer clock\n");
256 if (!request_mem_region(timer_cfg
->reg
.start
,
257 resource_size(&timer_cfg
->reg
),
259 pr_err("Unable to request memory region\n");
261 goto exit_clk_disable
;
264 base
= ioremap(timer_cfg
->reg
.start
, resource_size(&timer_cfg
->reg
));
266 pr_err("Unable to map the register range\n");
268 goto exit_mem_region
;
271 davinci_timer_init(base
);
272 tick_rate
= clk_get_rate(clk
);
274 clockevent
= kzalloc(sizeof(*clockevent
), GFP_KERNEL
);
277 goto exit_iounmap_base
;
280 clockevent
->dev
.name
= "tim12";
281 clockevent
->dev
.features
= CLOCK_EVT_FEAT_ONESHOT
;
282 clockevent
->dev
.cpumask
= cpumask_of(0);
283 clockevent
->base
= base
;
285 if (timer_cfg
->cmp_off
) {
286 clockevent
->cmp_off
= timer_cfg
->cmp_off
;
287 clockevent
->dev
.set_next_event
=
288 davinci_clockevent_set_next_event_cmp
;
290 clockevent
->dev
.set_next_event
=
291 davinci_clockevent_set_next_event_std
;
292 clockevent
->dev
.set_state_oneshot
=
293 davinci_clockevent_set_oneshot
;
294 clockevent
->dev
.set_state_shutdown
=
295 davinci_clockevent_shutdown
;
298 rv
= request_irq(timer_cfg
->irq
[DAVINCI_TIMER_CLOCKEVENT_IRQ
].start
,
299 davinci_timer_irq_timer
, IRQF_TIMER
,
300 "clockevent/tim12", clockevent
);
302 pr_err("Unable to request the clockevent interrupt\n");
303 goto exit_free_clockevent
;
306 davinci_clocksource
.dev
.rating
= 300;
307 davinci_clocksource
.dev
.read
= davinci_clocksource_read
;
308 davinci_clocksource
.dev
.mask
=
309 CLOCKSOURCE_MASK(DAVINCI_TIMER_CLKSRC_BITS
);
310 davinci_clocksource
.dev
.flags
= CLOCK_SOURCE_IS_CONTINUOUS
;
311 davinci_clocksource
.base
= base
;
313 if (timer_cfg
->cmp_off
) {
314 davinci_clocksource
.dev
.name
= "tim12";
315 davinci_clocksource
.tim_off
= DAVINCI_TIMER_REG_TIM12
;
316 davinci_clocksource_init_tim12(base
);
318 davinci_clocksource
.dev
.name
= "tim34";
319 davinci_clocksource
.tim_off
= DAVINCI_TIMER_REG_TIM34
;
320 davinci_clocksource_init_tim34(base
);
323 clockevents_config_and_register(&clockevent
->dev
, tick_rate
,
324 DAVINCI_TIMER_MIN_DELTA
,
325 DAVINCI_TIMER_MAX_DELTA
);
327 rv
= clocksource_register_hz(&davinci_clocksource
.dev
, tick_rate
);
329 pr_err("Unable to register clocksource\n");
333 sched_clock_register(davinci_timer_read_sched_clock
,
334 DAVINCI_TIMER_CLKSRC_BITS
, tick_rate
);
339 free_irq(timer_cfg
->irq
[DAVINCI_TIMER_CLOCKEVENT_IRQ
].start
,
341 exit_free_clockevent
:
346 release_mem_region(timer_cfg
->reg
.start
,
347 resource_size(&timer_cfg
->reg
));
349 clk_disable_unprepare(clk
);
353 static int __init
of_davinci_timer_register(struct device_node
*np
)
355 struct davinci_timer_cfg timer_cfg
= { };
359 rv
= of_address_to_resource(np
, 0, &timer_cfg
.reg
);
361 pr_err("Unable to get the register range for timer\n");
365 rv
= of_irq_to_resource_table(np
, timer_cfg
.irq
,
366 DAVINCI_TIMER_NUM_IRQS
);
367 if (rv
!= DAVINCI_TIMER_NUM_IRQS
) {
368 pr_err("Unable to get the interrupts for timer\n");
372 clk
= of_clk_get(np
, 0);
374 pr_err("Unable to get the timer clock\n");
378 rv
= davinci_timer_register(clk
, &timer_cfg
);
384 TIMER_OF_DECLARE(davinci_timer
, "ti,da830-timer", of_davinci_timer_register
);