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 #include <linux/clk.h>
11 #include <linux/clockchips.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/of_address.h>
15 #include <linux/of_irq.h>
16 #include <linux/sched_clock.h>
18 #include <clocksource/timer-davinci.h>
21 #define pr_fmt(fmt) "%s: " fmt "\n", __func__
23 #define DAVINCI_TIMER_REG_TIM12 0x10
24 #define DAVINCI_TIMER_REG_TIM34 0x14
25 #define DAVINCI_TIMER_REG_PRD12 0x18
26 #define DAVINCI_TIMER_REG_PRD34 0x1c
27 #define DAVINCI_TIMER_REG_TCR 0x20
28 #define DAVINCI_TIMER_REG_TGCR 0x24
30 #define DAVINCI_TIMER_TIMMODE_MASK GENMASK(3, 2)
31 #define DAVINCI_TIMER_RESET_MASK GENMASK(1, 0)
32 #define DAVINCI_TIMER_TIMMODE_32BIT_UNCHAINED BIT(2)
33 #define DAVINCI_TIMER_UNRESET GENMASK(1, 0)
35 #define DAVINCI_TIMER_ENAMODE_MASK GENMASK(1, 0)
36 #define DAVINCI_TIMER_ENAMODE_DISABLED 0x00
37 #define DAVINCI_TIMER_ENAMODE_ONESHOT BIT(0)
38 #define DAVINCI_TIMER_ENAMODE_PERIODIC BIT(1)
40 #define DAVINCI_TIMER_ENAMODE_SHIFT_TIM12 6
41 #define DAVINCI_TIMER_ENAMODE_SHIFT_TIM34 22
43 #define DAVINCI_TIMER_MIN_DELTA 0x01
44 #define DAVINCI_TIMER_MAX_DELTA 0xfffffffe
46 #define DAVINCI_TIMER_CLKSRC_BITS 32
48 #define DAVINCI_TIMER_TGCR_DEFAULT \
49 (DAVINCI_TIMER_TIMMODE_32BIT_UNCHAINED | DAVINCI_TIMER_UNRESET)
51 struct davinci_clockevent
{
52 struct clock_event_device dev
;
58 * This must be globally accessible by davinci_timer_read_sched_clock(), so
62 struct clocksource dev
;
65 } davinci_clocksource
;
67 static struct davinci_clockevent
*
68 to_davinci_clockevent(struct clock_event_device
*clockevent
)
70 return container_of(clockevent
, struct davinci_clockevent
, dev
);
74 davinci_clockevent_read(struct davinci_clockevent
*clockevent
,
77 return readl_relaxed(clockevent
->base
+ reg
);
80 static void davinci_clockevent_write(struct davinci_clockevent
*clockevent
,
81 unsigned int reg
, unsigned int val
)
83 writel_relaxed(val
, clockevent
->base
+ reg
);
86 static void davinci_tim12_shutdown(void __iomem
*base
)
90 tcr
= DAVINCI_TIMER_ENAMODE_DISABLED
<<
91 DAVINCI_TIMER_ENAMODE_SHIFT_TIM12
;
93 * This function is only ever called if we're using both timer
94 * halves. In this case TIM34 runs in periodic mode and we must
97 tcr
|= DAVINCI_TIMER_ENAMODE_PERIODIC
<<
98 DAVINCI_TIMER_ENAMODE_SHIFT_TIM34
;
100 writel_relaxed(tcr
, base
+ DAVINCI_TIMER_REG_TCR
);
103 static void davinci_tim12_set_oneshot(void __iomem
*base
)
107 tcr
= DAVINCI_TIMER_ENAMODE_ONESHOT
<<
108 DAVINCI_TIMER_ENAMODE_SHIFT_TIM12
;
110 tcr
|= DAVINCI_TIMER_ENAMODE_PERIODIC
<<
111 DAVINCI_TIMER_ENAMODE_SHIFT_TIM34
;
113 writel_relaxed(tcr
, base
+ DAVINCI_TIMER_REG_TCR
);
116 static int davinci_clockevent_shutdown(struct clock_event_device
*dev
)
118 struct davinci_clockevent
*clockevent
;
120 clockevent
= to_davinci_clockevent(dev
);
122 davinci_tim12_shutdown(clockevent
->base
);
127 static int davinci_clockevent_set_oneshot(struct clock_event_device
*dev
)
129 struct davinci_clockevent
*clockevent
= to_davinci_clockevent(dev
);
131 davinci_clockevent_write(clockevent
, DAVINCI_TIMER_REG_TIM12
, 0x0);
133 davinci_tim12_set_oneshot(clockevent
->base
);
139 davinci_clockevent_set_next_event_std(unsigned long cycles
,
140 struct clock_event_device
*dev
)
142 struct davinci_clockevent
*clockevent
= to_davinci_clockevent(dev
);
144 davinci_clockevent_shutdown(dev
);
146 davinci_clockevent_write(clockevent
, DAVINCI_TIMER_REG_TIM12
, 0x0);
147 davinci_clockevent_write(clockevent
, DAVINCI_TIMER_REG_PRD12
, cycles
);
149 davinci_clockevent_set_oneshot(dev
);
155 davinci_clockevent_set_next_event_cmp(unsigned long cycles
,
156 struct clock_event_device
*dev
)
158 struct davinci_clockevent
*clockevent
= to_davinci_clockevent(dev
);
159 unsigned int curr_time
;
161 curr_time
= davinci_clockevent_read(clockevent
,
162 DAVINCI_TIMER_REG_TIM12
);
163 davinci_clockevent_write(clockevent
,
164 clockevent
->cmp_off
, curr_time
+ cycles
);
169 static irqreturn_t
davinci_timer_irq_timer(int irq
, void *data
)
171 struct davinci_clockevent
*clockevent
= data
;
173 if (!clockevent_state_oneshot(&clockevent
->dev
))
174 davinci_tim12_shutdown(clockevent
->base
);
176 clockevent
->dev
.event_handler(&clockevent
->dev
);
181 static u64 notrace
davinci_timer_read_sched_clock(void)
183 return readl_relaxed(davinci_clocksource
.base
+
184 davinci_clocksource
.tim_off
);
187 static u64
davinci_clocksource_read(struct clocksource
*dev
)
189 return davinci_timer_read_sched_clock();
193 * Standard use-case: we're using tim12 for clockevent and tim34 for
194 * clocksource. The default is making the former run in oneshot mode
195 * and the latter in periodic mode.
197 static void davinci_clocksource_init_tim34(void __iomem
*base
)
201 tcr
= DAVINCI_TIMER_ENAMODE_PERIODIC
<<
202 DAVINCI_TIMER_ENAMODE_SHIFT_TIM34
;
203 tcr
|= DAVINCI_TIMER_ENAMODE_ONESHOT
<<
204 DAVINCI_TIMER_ENAMODE_SHIFT_TIM12
;
206 writel_relaxed(0x0, base
+ DAVINCI_TIMER_REG_TIM34
);
207 writel_relaxed(UINT_MAX
, base
+ DAVINCI_TIMER_REG_PRD34
);
208 writel_relaxed(tcr
, base
+ DAVINCI_TIMER_REG_TCR
);
212 * Special use-case on da830: the DSP may use tim34. We're using tim12 for
213 * both clocksource and clockevent. We set tim12 to periodic and don't touch
216 static void davinci_clocksource_init_tim12(void __iomem
*base
)
220 tcr
= DAVINCI_TIMER_ENAMODE_PERIODIC
<<
221 DAVINCI_TIMER_ENAMODE_SHIFT_TIM12
;
223 writel_relaxed(0x0, base
+ DAVINCI_TIMER_REG_TIM12
);
224 writel_relaxed(UINT_MAX
, base
+ DAVINCI_TIMER_REG_PRD12
);
225 writel_relaxed(tcr
, base
+ DAVINCI_TIMER_REG_TCR
);
228 static void davinci_timer_init(void __iomem
*base
)
230 /* Set clock to internal mode and disable it. */
231 writel_relaxed(0x0, base
+ DAVINCI_TIMER_REG_TCR
);
233 * Reset both 32-bit timers, set no prescaler for timer 34, set the
234 * timer to dual 32-bit unchained mode, unreset both 32-bit timers.
236 writel_relaxed(DAVINCI_TIMER_TGCR_DEFAULT
,
237 base
+ DAVINCI_TIMER_REG_TGCR
);
238 /* Init both counters to zero. */
239 writel_relaxed(0x0, base
+ DAVINCI_TIMER_REG_TIM12
);
240 writel_relaxed(0x0, base
+ DAVINCI_TIMER_REG_TIM34
);
243 int __init
davinci_timer_register(struct clk
*clk
,
244 const struct davinci_timer_cfg
*timer_cfg
)
246 struct davinci_clockevent
*clockevent
;
247 unsigned int tick_rate
;
251 rv
= clk_prepare_enable(clk
);
253 pr_err("Unable to prepare and enable the timer clock");
257 if (!request_mem_region(timer_cfg
->reg
.start
,
258 resource_size(&timer_cfg
->reg
),
260 pr_err("Unable to request memory region");
264 base
= ioremap(timer_cfg
->reg
.start
, resource_size(&timer_cfg
->reg
));
266 pr_err("Unable to map the register range");
270 davinci_timer_init(base
);
271 tick_rate
= clk_get_rate(clk
);
273 clockevent
= kzalloc(sizeof(*clockevent
), GFP_KERNEL
| __GFP_NOFAIL
);
275 pr_err("Error allocating memory for clockevent data");
279 clockevent
->dev
.name
= "tim12";
280 clockevent
->dev
.features
= CLOCK_EVT_FEAT_ONESHOT
;
281 clockevent
->dev
.cpumask
= cpumask_of(0);
282 clockevent
->base
= base
;
284 if (timer_cfg
->cmp_off
) {
285 clockevent
->cmp_off
= timer_cfg
->cmp_off
;
286 clockevent
->dev
.set_next_event
=
287 davinci_clockevent_set_next_event_cmp
;
289 clockevent
->dev
.set_next_event
=
290 davinci_clockevent_set_next_event_std
;
291 clockevent
->dev
.set_state_oneshot
=
292 davinci_clockevent_set_oneshot
;
293 clockevent
->dev
.set_state_shutdown
=
294 davinci_clockevent_shutdown
;
297 rv
= request_irq(timer_cfg
->irq
[DAVINCI_TIMER_CLOCKEVENT_IRQ
].start
,
298 davinci_timer_irq_timer
, IRQF_TIMER
,
299 "clockevent/tim12", clockevent
);
301 pr_err("Unable to request the clockevent interrupt");
305 davinci_clocksource
.dev
.rating
= 300;
306 davinci_clocksource
.dev
.read
= davinci_clocksource_read
;
307 davinci_clocksource
.dev
.mask
=
308 CLOCKSOURCE_MASK(DAVINCI_TIMER_CLKSRC_BITS
);
309 davinci_clocksource
.dev
.flags
= CLOCK_SOURCE_IS_CONTINUOUS
;
310 davinci_clocksource
.base
= base
;
312 if (timer_cfg
->cmp_off
) {
313 davinci_clocksource
.dev
.name
= "tim12";
314 davinci_clocksource
.tim_off
= DAVINCI_TIMER_REG_TIM12
;
315 davinci_clocksource_init_tim12(base
);
317 davinci_clocksource
.dev
.name
= "tim34";
318 davinci_clocksource
.tim_off
= DAVINCI_TIMER_REG_TIM34
;
319 davinci_clocksource_init_tim34(base
);
322 clockevents_config_and_register(&clockevent
->dev
, tick_rate
,
323 DAVINCI_TIMER_MIN_DELTA
,
324 DAVINCI_TIMER_MAX_DELTA
);
326 rv
= clocksource_register_hz(&davinci_clocksource
.dev
, tick_rate
);
328 pr_err("Unable to register clocksource");
332 sched_clock_register(davinci_timer_read_sched_clock
,
333 DAVINCI_TIMER_CLKSRC_BITS
, tick_rate
);
338 static int __init
of_davinci_timer_register(struct device_node
*np
)
340 struct davinci_timer_cfg timer_cfg
= { };
344 rv
= of_address_to_resource(np
, 0, &timer_cfg
.reg
);
346 pr_err("Unable to get the register range for timer");
350 rv
= of_irq_to_resource_table(np
, timer_cfg
.irq
,
351 DAVINCI_TIMER_NUM_IRQS
);
352 if (rv
!= DAVINCI_TIMER_NUM_IRQS
) {
353 pr_err("Unable to get the interrupts for timer");
357 clk
= of_clk_get(np
, 0);
359 pr_err("Unable to get the timer clock");
363 rv
= davinci_timer_register(clk
, &timer_cfg
);
369 TIMER_OF_DECLARE(davinci_timer
, "ti,da830-timer", of_davinci_timer_register
);