1 // SPDX-License-Identifier: GPL-2.0-only
3 * This file contains driver for the Cadence Triple Timer Counter Rev 06
5 * Copyright (C) 2011-2013 Xilinx
7 * based on arch/mips/kernel/time.c timer driver
10 #include <linux/clk.h>
11 #include <linux/interrupt.h>
12 #include <linux/clockchips.h>
13 #include <linux/clocksource.h>
14 #include <linux/of_address.h>
15 #include <linux/of_irq.h>
16 #include <linux/slab.h>
17 #include <linux/sched_clock.h>
18 #include <linux/module.h>
19 #include <linux/of_platform.h>
22 * This driver configures the 2 16/32-bit count-up timers as follows:
24 * T1: Timer 1, clocksource for generic timekeeping
25 * T2: Timer 2, clockevent source for hrtimers
26 * T3: Timer 3, <unused>
28 * The input frequency to the timer module for emulation is 2.5MHz which is
29 * common to all the timer channels (T1, T2, and T3). With a pre-scaler of 32,
30 * the timers are clocked at 78.125KHz (12.8 us resolution).
32 * The input frequency to the timer module in silicon is configurable and
33 * obtained from device tree. The pre-scaler of 32 is used.
37 * Timer Register Offset Definitions of Timer 1, Increment base address by 4
38 * and use same offsets for Timer 2
40 #define TTC_CLK_CNTRL_OFFSET 0x00 /* Clock Control Reg, RW */
41 #define TTC_CNT_CNTRL_OFFSET 0x0C /* Counter Control Reg, RW */
42 #define TTC_COUNT_VAL_OFFSET 0x18 /* Counter Value Reg, RO */
43 #define TTC_INTR_VAL_OFFSET 0x24 /* Interval Count Reg, RW */
44 #define TTC_ISR_OFFSET 0x54 /* Interrupt Status Reg, RO */
45 #define TTC_IER_OFFSET 0x60 /* Interrupt Enable Reg, RW */
47 #define TTC_CNT_CNTRL_DISABLE_MASK 0x1
49 #define TTC_CLK_CNTRL_CSRC_MASK (1 << 5) /* clock source */
50 #define TTC_CLK_CNTRL_PSV_MASK 0x1e
51 #define TTC_CLK_CNTRL_PSV_SHIFT 1
54 * Setup the timers to use pre-scaling, using a fixed value for now that will
55 * work across most input frequency, but it may need to be more dynamic
57 #define PRESCALE_EXPONENT 11 /* 2 ^ PRESCALE_EXPONENT = PRESCALE */
58 #define PRESCALE 2048 /* The exponent must match this */
59 #define CLK_CNTRL_PRESCALE ((PRESCALE_EXPONENT - 1) << 1)
60 #define CLK_CNTRL_PRESCALE_EN 1
61 #define CNT_CNTRL_RESET (1 << 4)
66 * struct ttc_timer - This definition defines local timer structure
68 * @base_addr: Base address of timer
69 * @freq: Timer input clock frequency
70 * @clk: Associated clock source
71 * @clk_rate_change_nb Notifier block for clock rate changes
74 void __iomem
*base_addr
;
77 struct notifier_block clk_rate_change_nb
;
80 #define to_ttc_timer(x) \
81 container_of(x, struct ttc_timer, clk_rate_change_nb)
83 struct ttc_timer_clocksource
{
84 u32 scale_clk_ctrl_reg_old
;
85 u32 scale_clk_ctrl_reg_new
;
87 struct clocksource cs
;
90 #define to_ttc_timer_clksrc(x) \
91 container_of(x, struct ttc_timer_clocksource, cs)
93 struct ttc_timer_clockevent
{
95 struct clock_event_device ce
;
98 #define to_ttc_timer_clkevent(x) \
99 container_of(x, struct ttc_timer_clockevent, ce)
101 static void __iomem
*ttc_sched_clock_val_reg
;
104 * ttc_set_interval - Set the timer interval value
106 * @timer: Pointer to the timer instance
107 * @cycles: Timer interval ticks
109 static void ttc_set_interval(struct ttc_timer
*timer
,
110 unsigned long cycles
)
114 /* Disable the counter, set the counter value and re-enable counter */
115 ctrl_reg
= readl_relaxed(timer
->base_addr
+ TTC_CNT_CNTRL_OFFSET
);
116 ctrl_reg
|= TTC_CNT_CNTRL_DISABLE_MASK
;
117 writel_relaxed(ctrl_reg
, timer
->base_addr
+ TTC_CNT_CNTRL_OFFSET
);
119 writel_relaxed(cycles
, timer
->base_addr
+ TTC_INTR_VAL_OFFSET
);
122 * Reset the counter (0x10) so that it starts from 0, one-shot
123 * mode makes this needed for timing to be right.
125 ctrl_reg
|= CNT_CNTRL_RESET
;
126 ctrl_reg
&= ~TTC_CNT_CNTRL_DISABLE_MASK
;
127 writel_relaxed(ctrl_reg
, timer
->base_addr
+ TTC_CNT_CNTRL_OFFSET
);
131 * ttc_clock_event_interrupt - Clock event timer interrupt handler
133 * @irq: IRQ number of the Timer
134 * @dev_id: void pointer to the ttc_timer instance
136 * returns: Always IRQ_HANDLED - success
138 static irqreturn_t
ttc_clock_event_interrupt(int irq
, void *dev_id
)
140 struct ttc_timer_clockevent
*ttce
= dev_id
;
141 struct ttc_timer
*timer
= &ttce
->ttc
;
143 /* Acknowledge the interrupt and call event handler */
144 readl_relaxed(timer
->base_addr
+ TTC_ISR_OFFSET
);
146 ttce
->ce
.event_handler(&ttce
->ce
);
152 * __ttc_clocksource_read - Reads the timer counter register
154 * returns: Current timer counter register value
156 static u64
__ttc_clocksource_read(struct clocksource
*cs
)
158 struct ttc_timer
*timer
= &to_ttc_timer_clksrc(cs
)->ttc
;
160 return (u64
)readl_relaxed(timer
->base_addr
+
161 TTC_COUNT_VAL_OFFSET
);
164 static u64 notrace
ttc_sched_clock_read(void)
166 return readl_relaxed(ttc_sched_clock_val_reg
);
170 * ttc_set_next_event - Sets the time interval for next event
172 * @cycles: Timer interval ticks
173 * @evt: Address of clock event instance
175 * returns: Always 0 - success
177 static int ttc_set_next_event(unsigned long cycles
,
178 struct clock_event_device
*evt
)
180 struct ttc_timer_clockevent
*ttce
= to_ttc_timer_clkevent(evt
);
181 struct ttc_timer
*timer
= &ttce
->ttc
;
183 ttc_set_interval(timer
, cycles
);
188 * ttc_set_{shutdown|oneshot|periodic} - Sets the state of timer
190 * @evt: Address of clock event instance
192 static int ttc_shutdown(struct clock_event_device
*evt
)
194 struct ttc_timer_clockevent
*ttce
= to_ttc_timer_clkevent(evt
);
195 struct ttc_timer
*timer
= &ttce
->ttc
;
198 ctrl_reg
= readl_relaxed(timer
->base_addr
+ TTC_CNT_CNTRL_OFFSET
);
199 ctrl_reg
|= TTC_CNT_CNTRL_DISABLE_MASK
;
200 writel_relaxed(ctrl_reg
, timer
->base_addr
+ TTC_CNT_CNTRL_OFFSET
);
204 static int ttc_set_periodic(struct clock_event_device
*evt
)
206 struct ttc_timer_clockevent
*ttce
= to_ttc_timer_clkevent(evt
);
207 struct ttc_timer
*timer
= &ttce
->ttc
;
209 ttc_set_interval(timer
,
210 DIV_ROUND_CLOSEST(ttce
->ttc
.freq
, PRESCALE
* HZ
));
214 static int ttc_resume(struct clock_event_device
*evt
)
216 struct ttc_timer_clockevent
*ttce
= to_ttc_timer_clkevent(evt
);
217 struct ttc_timer
*timer
= &ttce
->ttc
;
220 ctrl_reg
= readl_relaxed(timer
->base_addr
+ TTC_CNT_CNTRL_OFFSET
);
221 ctrl_reg
&= ~TTC_CNT_CNTRL_DISABLE_MASK
;
222 writel_relaxed(ctrl_reg
, timer
->base_addr
+ TTC_CNT_CNTRL_OFFSET
);
226 static int ttc_rate_change_clocksource_cb(struct notifier_block
*nb
,
227 unsigned long event
, void *data
)
229 struct clk_notifier_data
*ndata
= data
;
230 struct ttc_timer
*ttc
= to_ttc_timer(nb
);
231 struct ttc_timer_clocksource
*ttccs
= container_of(ttc
,
232 struct ttc_timer_clocksource
, ttc
);
235 case PRE_RATE_CHANGE
:
238 unsigned long factor
, rate_low
, rate_high
;
240 if (ndata
->new_rate
> ndata
->old_rate
) {
241 factor
= DIV_ROUND_CLOSEST(ndata
->new_rate
,
243 rate_low
= ndata
->old_rate
;
244 rate_high
= ndata
->new_rate
;
246 factor
= DIV_ROUND_CLOSEST(ndata
->old_rate
,
248 rate_low
= ndata
->new_rate
;
249 rate_high
= ndata
->old_rate
;
252 if (!is_power_of_2(factor
))
255 if (abs(rate_high
- (factor
* rate_low
)) > MAX_F_ERR
)
258 factor
= __ilog2_u32(factor
);
261 * store timer clock ctrl register so we can restore it in case
264 ttccs
->scale_clk_ctrl_reg_old
=
265 readl_relaxed(ttccs
->ttc
.base_addr
+
266 TTC_CLK_CNTRL_OFFSET
);
268 psv
= (ttccs
->scale_clk_ctrl_reg_old
&
269 TTC_CLK_CNTRL_PSV_MASK
) >>
270 TTC_CLK_CNTRL_PSV_SHIFT
;
271 if (ndata
->new_rate
< ndata
->old_rate
)
276 /* prescaler within legal range? */
277 if (psv
& ~(TTC_CLK_CNTRL_PSV_MASK
>> TTC_CLK_CNTRL_PSV_SHIFT
))
280 ttccs
->scale_clk_ctrl_reg_new
= ttccs
->scale_clk_ctrl_reg_old
&
281 ~TTC_CLK_CNTRL_PSV_MASK
;
282 ttccs
->scale_clk_ctrl_reg_new
|= psv
<< TTC_CLK_CNTRL_PSV_SHIFT
;
285 /* scale down: adjust divider in post-change notification */
286 if (ndata
->new_rate
< ndata
->old_rate
)
289 /* scale up: adjust divider now - before frequency change */
290 writel_relaxed(ttccs
->scale_clk_ctrl_reg_new
,
291 ttccs
->ttc
.base_addr
+ TTC_CLK_CNTRL_OFFSET
);
294 case POST_RATE_CHANGE
:
295 /* scale up: pre-change notification did the adjustment */
296 if (ndata
->new_rate
> ndata
->old_rate
)
299 /* scale down: adjust divider now - after frequency change */
300 writel_relaxed(ttccs
->scale_clk_ctrl_reg_new
,
301 ttccs
->ttc
.base_addr
+ TTC_CLK_CNTRL_OFFSET
);
304 case ABORT_RATE_CHANGE
:
305 /* we have to undo the adjustment in case we scale up */
306 if (ndata
->new_rate
< ndata
->old_rate
)
309 /* restore original register value */
310 writel_relaxed(ttccs
->scale_clk_ctrl_reg_old
,
311 ttccs
->ttc
.base_addr
+ TTC_CLK_CNTRL_OFFSET
);
320 static int __init
ttc_setup_clocksource(struct clk
*clk
, void __iomem
*base
,
323 struct ttc_timer_clocksource
*ttccs
;
326 ttccs
= kzalloc(sizeof(*ttccs
), GFP_KERNEL
);
330 ttccs
->ttc
.clk
= clk
;
332 err
= clk_prepare_enable(ttccs
->ttc
.clk
);
338 ttccs
->ttc
.freq
= clk_get_rate(ttccs
->ttc
.clk
);
340 ttccs
->ttc
.clk_rate_change_nb
.notifier_call
=
341 ttc_rate_change_clocksource_cb
;
342 ttccs
->ttc
.clk_rate_change_nb
.next
= NULL
;
344 err
= clk_notifier_register(ttccs
->ttc
.clk
,
345 &ttccs
->ttc
.clk_rate_change_nb
);
347 pr_warn("Unable to register clock notifier.\n");
349 ttccs
->ttc
.base_addr
= base
;
350 ttccs
->cs
.name
= "ttc_clocksource";
351 ttccs
->cs
.rating
= 200;
352 ttccs
->cs
.read
= __ttc_clocksource_read
;
353 ttccs
->cs
.mask
= CLOCKSOURCE_MASK(timer_width
);
354 ttccs
->cs
.flags
= CLOCK_SOURCE_IS_CONTINUOUS
;
357 * Setup the clock source counter to be an incrementing counter
358 * with no interrupt and it rolls over at 0xFFFF. Pre-scale
359 * it by 32 also. Let it start running now.
361 writel_relaxed(0x0, ttccs
->ttc
.base_addr
+ TTC_IER_OFFSET
);
362 writel_relaxed(CLK_CNTRL_PRESCALE
| CLK_CNTRL_PRESCALE_EN
,
363 ttccs
->ttc
.base_addr
+ TTC_CLK_CNTRL_OFFSET
);
364 writel_relaxed(CNT_CNTRL_RESET
,
365 ttccs
->ttc
.base_addr
+ TTC_CNT_CNTRL_OFFSET
);
367 err
= clocksource_register_hz(&ttccs
->cs
, ttccs
->ttc
.freq
/ PRESCALE
);
373 ttc_sched_clock_val_reg
= base
+ TTC_COUNT_VAL_OFFSET
;
374 sched_clock_register(ttc_sched_clock_read
, timer_width
,
375 ttccs
->ttc
.freq
/ PRESCALE
);
380 static int ttc_rate_change_clockevent_cb(struct notifier_block
*nb
,
381 unsigned long event
, void *data
)
383 struct clk_notifier_data
*ndata
= data
;
384 struct ttc_timer
*ttc
= to_ttc_timer(nb
);
385 struct ttc_timer_clockevent
*ttcce
= container_of(ttc
,
386 struct ttc_timer_clockevent
, ttc
);
389 case POST_RATE_CHANGE
:
390 /* update cached frequency */
391 ttc
->freq
= ndata
->new_rate
;
393 clockevents_update_freq(&ttcce
->ce
, ndata
->new_rate
/ PRESCALE
);
396 case PRE_RATE_CHANGE
:
397 case ABORT_RATE_CHANGE
:
403 static int __init
ttc_setup_clockevent(struct clk
*clk
,
404 void __iomem
*base
, u32 irq
)
406 struct ttc_timer_clockevent
*ttcce
;
409 ttcce
= kzalloc(sizeof(*ttcce
), GFP_KERNEL
);
413 ttcce
->ttc
.clk
= clk
;
415 err
= clk_prepare_enable(ttcce
->ttc
.clk
);
419 ttcce
->ttc
.clk_rate_change_nb
.notifier_call
=
420 ttc_rate_change_clockevent_cb
;
421 ttcce
->ttc
.clk_rate_change_nb
.next
= NULL
;
423 err
= clk_notifier_register(ttcce
->ttc
.clk
,
424 &ttcce
->ttc
.clk_rate_change_nb
);
426 pr_warn("Unable to register clock notifier.\n");
430 ttcce
->ttc
.freq
= clk_get_rate(ttcce
->ttc
.clk
);
432 ttcce
->ttc
.base_addr
= base
;
433 ttcce
->ce
.name
= "ttc_clockevent";
434 ttcce
->ce
.features
= CLOCK_EVT_FEAT_PERIODIC
| CLOCK_EVT_FEAT_ONESHOT
;
435 ttcce
->ce
.set_next_event
= ttc_set_next_event
;
436 ttcce
->ce
.set_state_shutdown
= ttc_shutdown
;
437 ttcce
->ce
.set_state_periodic
= ttc_set_periodic
;
438 ttcce
->ce
.set_state_oneshot
= ttc_shutdown
;
439 ttcce
->ce
.tick_resume
= ttc_resume
;
440 ttcce
->ce
.rating
= 200;
442 ttcce
->ce
.cpumask
= cpu_possible_mask
;
445 * Setup the clock event timer to be an interval timer which
446 * is prescaled by 32 using the interval interrupt. Leave it
449 writel_relaxed(0x23, ttcce
->ttc
.base_addr
+ TTC_CNT_CNTRL_OFFSET
);
450 writel_relaxed(CLK_CNTRL_PRESCALE
| CLK_CNTRL_PRESCALE_EN
,
451 ttcce
->ttc
.base_addr
+ TTC_CLK_CNTRL_OFFSET
);
452 writel_relaxed(0x1, ttcce
->ttc
.base_addr
+ TTC_IER_OFFSET
);
454 err
= request_irq(irq
, ttc_clock_event_interrupt
,
455 IRQF_TIMER
, ttcce
->ce
.name
, ttcce
);
459 clockevents_config_and_register(&ttcce
->ce
,
460 ttcce
->ttc
.freq
/ PRESCALE
, 1, 0xfffe);
469 static int __init
ttc_timer_probe(struct platform_device
*pdev
)
472 void __iomem
*timer_baseaddr
;
473 struct clk
*clk_cs
, *clk_ce
;
474 static int initialized
;
476 u32 timer_width
= 16;
477 struct device_node
*timer
= pdev
->dev
.of_node
;
485 * Get the 1st Triple Timer Counter (TTC) block from the device tree
486 * and use it. Note that the event timer uses the interrupt and it's the
487 * 2nd TTC hence the irq_of_parse_and_map(,1)
489 timer_baseaddr
= of_iomap(timer
, 0);
490 if (!timer_baseaddr
) {
491 pr_err("ERROR: invalid timer base address\n");
495 irq
= irq_of_parse_and_map(timer
, 1);
497 pr_err("ERROR: invalid interrupt number\n");
501 of_property_read_u32(timer
, "timer-width", &timer_width
);
503 clksel
= readl_relaxed(timer_baseaddr
+ TTC_CLK_CNTRL_OFFSET
);
504 clksel
= !!(clksel
& TTC_CLK_CNTRL_CSRC_MASK
);
505 clk_cs
= of_clk_get(timer
, clksel
);
506 if (IS_ERR(clk_cs
)) {
507 pr_err("ERROR: timer input clock not found\n");
508 return PTR_ERR(clk_cs
);
511 clksel
= readl_relaxed(timer_baseaddr
+ 4 + TTC_CLK_CNTRL_OFFSET
);
512 clksel
= !!(clksel
& TTC_CLK_CNTRL_CSRC_MASK
);
513 clk_ce
= of_clk_get(timer
, clksel
);
514 if (IS_ERR(clk_ce
)) {
515 pr_err("ERROR: timer input clock not found\n");
516 return PTR_ERR(clk_ce
);
519 ret
= ttc_setup_clocksource(clk_cs
, timer_baseaddr
, timer_width
);
523 ret
= ttc_setup_clockevent(clk_ce
, timer_baseaddr
+ 4, irq
);
527 pr_info("%pOFn #0 at %p, irq=%d\n", timer
, timer_baseaddr
, irq
);
532 static const struct of_device_id ttc_timer_of_match
[] = {
533 {.compatible
= "cdns,ttc"},
537 MODULE_DEVICE_TABLE(of
, ttc_timer_of_match
);
539 static struct platform_driver ttc_timer_driver
= {
541 .name
= "cdns_ttc_timer",
542 .of_match_table
= ttc_timer_of_match
,
545 builtin_platform_driver_probe(ttc_timer_driver
, ttc_timer_probe
);