1 // SPDX-License-Identifier: GPL-2.0+
3 #include <linux/clocksource.h>
4 #include <linux/clockchips.h>
5 #include <linux/interrupt.h>
7 #include <linux/iopoll.h>
10 #include <linux/of_address.h>
11 #include <linux/of_irq.h>
12 #include <linux/sched_clock.h>
14 #include <linux/clk/clk-conf.h>
16 #include <clocksource/timer-ti-dm.h>
17 #include <dt-bindings/bus/ti-sysc.h>
19 /* For type1, set SYSC_OMAP2_CLOCKACTIVITY for fck off on idle, l4 clock on */
20 #define DMTIMER_TYPE1_ENABLE ((1 << 9) | (SYSC_IDLE_SMART << 3) | \
21 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_AUTOIDLE)
22 #define DMTIMER_TYPE1_DISABLE (SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE)
23 #define DMTIMER_TYPE2_ENABLE (SYSC_IDLE_SMART_WKUP << 2)
24 #define DMTIMER_RESET_WAIT 100000
26 #define DMTIMER_INST_DONT_CARE ~0U
28 static int counter_32k
;
29 static u32 clocksource
;
30 static u32 clockevent
;
33 * Subset of the timer registers we use. Note that the register offsets
34 * depend on the timer revision detected.
36 struct dmtimer_systimer
{
52 struct dmtimer_clockevent
{
53 struct clock_event_device dev
;
54 struct dmtimer_systimer t
;
58 struct dmtimer_clocksource
{
59 struct clocksource dev
;
60 struct dmtimer_systimer t
;
64 /* Assumes v1 ip if bits [31:16] are zero */
65 static bool dmtimer_systimer_revision1(struct dmtimer_systimer
*t
)
67 u32 tidr
= readl_relaxed(t
->base
);
72 static int __init
dmtimer_systimer_type1_reset(struct dmtimer_systimer
*t
)
74 void __iomem
*syss
= t
->base
+ OMAP_TIMER_V1_SYS_STAT_OFFSET
;
78 writel_relaxed(BIT(1) | BIT(2), t
->base
+ t
->ifctrl
);
79 ret
= readl_poll_timeout_atomic(syss
, l
, l
& BIT(0), 100,
85 /* Note we must use io_base instead of func_base for type2 OCP regs */
86 static int __init
dmtimer_systimer_type2_reset(struct dmtimer_systimer
*t
)
88 void __iomem
*sysc
= t
->base
+ t
->sysc
;
91 l
= readl_relaxed(sysc
);
93 writel_relaxed(l
, sysc
);
95 return readl_poll_timeout_atomic(sysc
, l
, !(l
& BIT(0)), 100,
99 static int __init
dmtimer_systimer_reset(struct dmtimer_systimer
*t
)
103 if (dmtimer_systimer_revision1(t
))
104 ret
= dmtimer_systimer_type1_reset(t
);
106 ret
= dmtimer_systimer_type2_reset(t
);
108 pr_err("%s failed with %i\n", __func__
, ret
);
116 static const struct of_device_id counter_match_table
[] = {
117 { .compatible
= "ti,omap-counter32k" },
122 * Check if the SoC als has a usable working 32 KiHz counter. The 32 KiHz
123 * counter is handled by timer-ti-32k, but we need to detect it as it
124 * affects the preferred dmtimer system timer configuration. There is
125 * typically no use for a dmtimer clocksource if the 32 KiHz counter is
126 * present, except on am437x as described below.
128 static void __init
dmtimer_systimer_check_counter32k(void)
130 struct device_node
*np
;
135 np
= of_find_matching_node(NULL
, counter_match_table
);
137 counter_32k
= -ENODEV
;
142 if (of_device_is_available(np
))
145 counter_32k
= -ENODEV
;
150 static const struct of_device_id dmtimer_match_table
[] = {
151 { .compatible
= "ti,omap2420-timer", },
152 { .compatible
= "ti,omap3430-timer", },
153 { .compatible
= "ti,omap4430-timer", },
154 { .compatible
= "ti,omap5430-timer", },
155 { .compatible
= "ti,am335x-timer", },
156 { .compatible
= "ti,am335x-timer-1ms", },
157 { .compatible
= "ti,dm814-timer", },
158 { .compatible
= "ti,dm816-timer", },
163 * Checks that system timers are configured to not reset and idle during
164 * the generic timer-ti-dm device driver probe. And that the system timer
165 * source clocks are properly configured. Also, let's not hog any DSP and
166 * PWM capable timers unnecessarily as system timers.
168 static bool __init
dmtimer_is_preferred(struct device_node
*np
)
170 if (!of_device_is_available(np
))
173 if (!of_property_read_bool(np
->parent
,
174 "ti,no-reset-on-init"))
177 if (!of_property_read_bool(np
->parent
, "ti,no-idle"))
180 /* Secure gptimer12 is always clocked with a fixed source */
181 if (!of_property_read_bool(np
, "ti,timer-secure")) {
182 if (!of_property_read_bool(np
, "assigned-clocks"))
185 if (!of_property_read_bool(np
, "assigned-clock-parents"))
189 if (of_property_read_bool(np
, "ti,timer-dsp"))
192 if (of_property_read_bool(np
, "ti,timer-pwm"))
199 * Finds the first available usable always-on timer, and assigns it to either
200 * clockevent or clocksource depending if the counter_32k is available on the
203 * Some omap3 boards with unreliable oscillator must not use the counter_32k
204 * or dmtimer1 with 32 KiHz source. Additionally, the boards with unreliable
205 * oscillator should really set counter_32k as disabled, and delete dmtimer1
206 * ti,always-on property, but let's not count on it. For these quirky cases,
207 * we prefer using the always-on secure dmtimer12 with the internal 32 KiHz
208 * clock as the clocksource, and any available dmtimer as clockevent.
210 * For am437x, we are using am335x style dmtimer clocksource. It is unclear
211 * if this quirk handling is really needed, but let's change it separately
212 * based on testing as it might cause side effects.
214 static void __init
dmtimer_systimer_assign_alwon(void)
216 struct device_node
*np
;
218 bool quirk_unreliable_oscillator
= false;
220 /* Quirk unreliable 32 KiHz oscillator with incomplete dts */
221 if (of_machine_is_compatible("ti,omap3-beagle") ||
222 of_machine_is_compatible("timll,omap3-devkit8000")) {
223 quirk_unreliable_oscillator
= true;
224 counter_32k
= -ENODEV
;
227 /* Quirk am437x using am335x style dmtimer clocksource */
228 if (of_machine_is_compatible("ti,am43"))
229 counter_32k
= -ENODEV
;
231 for_each_matching_node(np
, dmtimer_match_table
) {
232 if (!dmtimer_is_preferred(np
))
235 if (of_property_read_bool(np
, "ti,timer-alwon")) {
238 addr
= of_get_address(np
, 0, NULL
, NULL
);
239 pa
= of_translate_address(np
, addr
);
241 /* Quirky omap3 boards must use dmtimer12 */
242 if (quirk_unreliable_oscillator
&&
252 /* Usually no need for dmtimer clocksource if we have counter32 */
253 if (counter_32k
>= 0) {
258 clockevent
= DMTIMER_INST_DONT_CARE
;
262 /* Finds the first usable dmtimer, used for the don't care case */
263 static u32 __init
dmtimer_systimer_find_first_available(void)
265 struct device_node
*np
;
269 for_each_matching_node(np
, dmtimer_match_table
) {
270 if (!dmtimer_is_preferred(np
))
273 addr
= of_get_address(np
, 0, NULL
, NULL
);
274 pa
= of_translate_address(np
, addr
);
276 if (pa
== clocksource
|| pa
== clockevent
) {
289 /* Selects the best clocksource and clockevent to use */
290 static void __init
dmtimer_systimer_select_best(void)
292 dmtimer_systimer_check_counter32k();
293 dmtimer_systimer_assign_alwon();
295 if (clockevent
== DMTIMER_INST_DONT_CARE
)
296 clockevent
= dmtimer_systimer_find_first_available();
298 pr_debug("%s: counter_32k: %i clocksource: %08x clockevent: %08x\n",
299 __func__
, counter_32k
, clocksource
, clockevent
);
302 /* Interface clocks are only available on some SoCs variants */
303 static int __init
dmtimer_systimer_init_clock(struct dmtimer_systimer
*t
,
304 struct device_node
*np
,
313 is_ick
= !strncmp(name
, "ick", 3);
315 clock
= of_clk_get_by_name(np
, name
);
316 if ((PTR_ERR(clock
) == -EINVAL
) && is_ick
)
318 else if (IS_ERR(clock
))
319 return PTR_ERR(clock
);
321 error
= clk_prepare_enable(clock
);
325 r
= clk_get_rate(clock
);
339 static void dmtimer_systimer_enable(struct dmtimer_systimer
*t
)
343 if (dmtimer_systimer_revision1(t
))
344 val
= DMTIMER_TYPE1_ENABLE
;
346 val
= DMTIMER_TYPE2_ENABLE
;
348 writel_relaxed(val
, t
->base
+ t
->sysc
);
351 static void dmtimer_systimer_disable(struct dmtimer_systimer
*t
)
353 if (!dmtimer_systimer_revision1(t
))
356 writel_relaxed(DMTIMER_TYPE1_DISABLE
, t
->base
+ t
->sysc
);
359 static int __init
dmtimer_systimer_setup(struct device_node
*np
,
360 struct dmtimer_systimer
*t
)
366 if (!of_device_is_compatible(np
->parent
, "ti,sysc"))
369 t
->base
= of_iomap(np
, 0);
374 * Enable optional assigned-clock-parents configured at the timer
375 * node level. For regular device drivers, this is done automatically
376 * by bus related code such as platform_drv_probe().
378 error
= of_clk_set_defaults(np
, false);
380 pr_err("%s: clock source init failed: %i\n", __func__
, error
);
382 /* For ti-sysc, we have timer clocks at the parent module level */
383 error
= dmtimer_systimer_init_clock(t
, np
->parent
, "fck", &rate
);
389 error
= dmtimer_systimer_init_clock(t
, np
->parent
, "ick", &rate
);
393 if (dmtimer_systimer_revision1(t
)) {
394 t
->irq_stat
= OMAP_TIMER_V1_STAT_OFFSET
;
395 t
->irq_ena
= OMAP_TIMER_V1_INT_EN_OFFSET
;
396 t
->pend
= _OMAP_TIMER_WRITE_PEND_OFFSET
;
399 t
->irq_stat
= OMAP_TIMER_V2_IRQSTATUS
;
400 t
->irq_ena
= OMAP_TIMER_V2_IRQENABLE_SET
;
401 regbase
= OMAP_TIMER_V2_FUNC_OFFSET
;
402 t
->pend
= regbase
+ _OMAP_TIMER_WRITE_PEND_OFFSET
;
405 t
->sysc
= OMAP_TIMER_OCP_CFG_OFFSET
;
406 t
->load
= regbase
+ _OMAP_TIMER_LOAD_OFFSET
;
407 t
->counter
= regbase
+ _OMAP_TIMER_COUNTER_OFFSET
;
408 t
->ctrl
= regbase
+ _OMAP_TIMER_CTRL_OFFSET
;
409 t
->wakeup
= regbase
+ _OMAP_TIMER_WAKEUP_EN_OFFSET
;
410 t
->ifctrl
= regbase
+ _OMAP_TIMER_IF_CTRL_OFFSET
;
412 dmtimer_systimer_enable(t
);
413 dmtimer_systimer_reset(t
);
414 pr_debug("dmtimer rev %08x sysc %08x\n", readl_relaxed(t
->base
),
415 readl_relaxed(t
->base
+ t
->sysc
));
426 static struct dmtimer_clockevent
*
427 to_dmtimer_clockevent(struct clock_event_device
*clockevent
)
429 return container_of(clockevent
, struct dmtimer_clockevent
, dev
);
432 static irqreturn_t
dmtimer_clockevent_interrupt(int irq
, void *data
)
434 struct dmtimer_clockevent
*clkevt
= data
;
435 struct dmtimer_systimer
*t
= &clkevt
->t
;
437 writel_relaxed(OMAP_TIMER_INT_OVERFLOW
, t
->base
+ t
->irq_stat
);
438 clkevt
->dev
.event_handler(&clkevt
->dev
);
443 static int dmtimer_set_next_event(unsigned long cycles
,
444 struct clock_event_device
*evt
)
446 struct dmtimer_clockevent
*clkevt
= to_dmtimer_clockevent(evt
);
447 struct dmtimer_systimer
*t
= &clkevt
->t
;
448 void __iomem
*pend
= t
->base
+ t
->pend
;
450 writel_relaxed(0xffffffff - cycles
, t
->base
+ t
->counter
);
451 while (readl_relaxed(pend
) & WP_TCRR
)
454 writel_relaxed(OMAP_TIMER_CTRL_ST
, t
->base
+ t
->ctrl
);
455 while (readl_relaxed(pend
) & WP_TCLR
)
461 static int dmtimer_clockevent_shutdown(struct clock_event_device
*evt
)
463 struct dmtimer_clockevent
*clkevt
= to_dmtimer_clockevent(evt
);
464 struct dmtimer_systimer
*t
= &clkevt
->t
;
465 void __iomem
*ctrl
= t
->base
+ t
->ctrl
;
468 l
= readl_relaxed(ctrl
);
469 if (l
& OMAP_TIMER_CTRL_ST
) {
471 writel_relaxed(l
, ctrl
);
472 /* Flush posted write */
473 l
= readl_relaxed(ctrl
);
474 /* Wait for functional clock period x 3.5 */
475 udelay(3500000 / t
->rate
+ 1);
477 writel_relaxed(OMAP_TIMER_INT_OVERFLOW
, t
->base
+ t
->irq_stat
);
482 static int dmtimer_set_periodic(struct clock_event_device
*evt
)
484 struct dmtimer_clockevent
*clkevt
= to_dmtimer_clockevent(evt
);
485 struct dmtimer_systimer
*t
= &clkevt
->t
;
486 void __iomem
*pend
= t
->base
+ t
->pend
;
488 dmtimer_clockevent_shutdown(evt
);
490 /* Looks like we need to first set the load value separately */
491 writel_relaxed(clkevt
->period
, t
->base
+ t
->load
);
492 while (readl_relaxed(pend
) & WP_TLDR
)
495 writel_relaxed(clkevt
->period
, t
->base
+ t
->counter
);
496 while (readl_relaxed(pend
) & WP_TCRR
)
499 writel_relaxed(OMAP_TIMER_CTRL_AR
| OMAP_TIMER_CTRL_ST
,
501 while (readl_relaxed(pend
) & WP_TCLR
)
507 static void omap_clockevent_idle(struct clock_event_device
*evt
)
509 struct dmtimer_clockevent
*clkevt
= to_dmtimer_clockevent(evt
);
510 struct dmtimer_systimer
*t
= &clkevt
->t
;
512 dmtimer_systimer_disable(t
);
516 static void omap_clockevent_unidle(struct clock_event_device
*evt
)
518 struct dmtimer_clockevent
*clkevt
= to_dmtimer_clockevent(evt
);
519 struct dmtimer_systimer
*t
= &clkevt
->t
;
522 error
= clk_enable(t
->fck
);
524 pr_err("could not enable timer fck on resume: %i\n", error
);
526 dmtimer_systimer_enable(t
);
527 writel_relaxed(OMAP_TIMER_INT_OVERFLOW
, t
->base
+ t
->irq_ena
);
528 writel_relaxed(OMAP_TIMER_INT_OVERFLOW
, t
->base
+ t
->wakeup
);
531 static int __init
dmtimer_clockevent_init(struct device_node
*np
)
533 struct dmtimer_clockevent
*clkevt
;
534 struct clock_event_device
*dev
;
535 struct dmtimer_systimer
*t
;
538 clkevt
= kzalloc(sizeof(*clkevt
), GFP_KERNEL
);
546 * We mostly use cpuidle_coupled with ARM local timers for runtime,
547 * so there's probably no use for CLOCK_EVT_FEAT_DYNIRQ here.
549 dev
->features
= CLOCK_EVT_FEAT_PERIODIC
| CLOCK_EVT_FEAT_ONESHOT
;
551 dev
->set_next_event
= dmtimer_set_next_event
;
552 dev
->set_state_shutdown
= dmtimer_clockevent_shutdown
;
553 dev
->set_state_periodic
= dmtimer_set_periodic
;
554 dev
->set_state_oneshot
= dmtimer_clockevent_shutdown
;
555 dev
->tick_resume
= dmtimer_clockevent_shutdown
;
556 dev
->cpumask
= cpu_possible_mask
;
558 dev
->irq
= irq_of_parse_and_map(np
, 0);
564 error
= dmtimer_systimer_setup(np
, &clkevt
->t
);
568 clkevt
->period
= 0xffffffff - DIV_ROUND_CLOSEST(t
->rate
, HZ
);
571 * For clock-event timers we never read the timer counter and
572 * so we are not impacted by errata i103 and i767. Therefore,
573 * we can safely ignore this errata for clock-event timers.
575 writel_relaxed(OMAP_TIMER_CTRL_POSTED
, t
->base
+ t
->ifctrl
);
577 error
= request_irq(dev
->irq
, dmtimer_clockevent_interrupt
,
578 IRQF_TIMER
, "clockevent", clkevt
);
582 writel_relaxed(OMAP_TIMER_INT_OVERFLOW
, t
->base
+ t
->irq_ena
);
583 writel_relaxed(OMAP_TIMER_INT_OVERFLOW
, t
->base
+ t
->wakeup
);
585 pr_info("TI gptimer clockevent: %s%lu Hz at %pOF\n",
586 of_find_property(np
, "ti,timer-alwon", NULL
) ?
587 "always-on " : "", t
->rate
, np
->parent
);
589 clockevents_config_and_register(dev
, t
->rate
,
590 3, /* Timer internal resynch latency */
593 if (of_machine_is_compatible("ti,am33xx") ||
594 of_machine_is_compatible("ti,am43")) {
595 dev
->suspend
= omap_clockevent_idle
;
596 dev
->resume
= omap_clockevent_unidle
;
611 static struct dmtimer_clocksource
*
612 to_dmtimer_clocksource(struct clocksource
*cs
)
614 return container_of(cs
, struct dmtimer_clocksource
, dev
);
617 static u64
dmtimer_clocksource_read_cycles(struct clocksource
*cs
)
619 struct dmtimer_clocksource
*clksrc
= to_dmtimer_clocksource(cs
);
620 struct dmtimer_systimer
*t
= &clksrc
->t
;
622 return (u64
)readl_relaxed(t
->base
+ t
->counter
);
625 static void __iomem
*dmtimer_sched_clock_counter
;
627 static u64 notrace
dmtimer_read_sched_clock(void)
629 return readl_relaxed(dmtimer_sched_clock_counter
);
632 static void dmtimer_clocksource_suspend(struct clocksource
*cs
)
634 struct dmtimer_clocksource
*clksrc
= to_dmtimer_clocksource(cs
);
635 struct dmtimer_systimer
*t
= &clksrc
->t
;
637 clksrc
->loadval
= readl_relaxed(t
->base
+ t
->counter
);
638 dmtimer_systimer_disable(t
);
642 static void dmtimer_clocksource_resume(struct clocksource
*cs
)
644 struct dmtimer_clocksource
*clksrc
= to_dmtimer_clocksource(cs
);
645 struct dmtimer_systimer
*t
= &clksrc
->t
;
648 error
= clk_enable(t
->fck
);
650 pr_err("could not enable timer fck on resume: %i\n", error
);
652 dmtimer_systimer_enable(t
);
653 writel_relaxed(clksrc
->loadval
, t
->base
+ t
->counter
);
654 writel_relaxed(OMAP_TIMER_CTRL_ST
| OMAP_TIMER_CTRL_AR
,
658 static int __init
dmtimer_clocksource_init(struct device_node
*np
)
660 struct dmtimer_clocksource
*clksrc
;
661 struct dmtimer_systimer
*t
;
662 struct clocksource
*dev
;
665 clksrc
= kzalloc(sizeof(*clksrc
), GFP_KERNEL
);
672 error
= dmtimer_systimer_setup(np
, t
);
676 dev
->name
= "dmtimer";
678 dev
->read
= dmtimer_clocksource_read_cycles
;
679 dev
->mask
= CLOCKSOURCE_MASK(32);
680 dev
->flags
= CLOCK_SOURCE_IS_CONTINUOUS
;
682 /* Unlike for clockevent, legacy code sets suspend only for am4 */
683 if (of_machine_is_compatible("ti,am43")) {
684 dev
->suspend
= dmtimer_clocksource_suspend
;
685 dev
->resume
= dmtimer_clocksource_resume
;
688 writel_relaxed(0, t
->base
+ t
->counter
);
689 writel_relaxed(OMAP_TIMER_CTRL_ST
| OMAP_TIMER_CTRL_AR
,
692 pr_info("TI gptimer clocksource: %s%pOF\n",
693 of_find_property(np
, "ti,timer-alwon", NULL
) ?
694 "always-on " : "", np
->parent
);
696 if (!dmtimer_sched_clock_counter
) {
697 dmtimer_sched_clock_counter
= t
->base
+ t
->counter
;
698 sched_clock_register(dmtimer_read_sched_clock
, 32, t
->rate
);
701 if (clocksource_register_hz(dev
, t
->rate
))
702 pr_err("Could not register clocksource %pOF\n", np
);
713 * To detect between a clocksource and clockevent, we assume the device tree
714 * has no interrupts configured for a clocksource timer.
716 static int __init
dmtimer_systimer_init(struct device_node
*np
)
721 /* One time init for the preferred timer configuration */
722 if (!clocksource
&& !clockevent
)
723 dmtimer_systimer_select_best();
725 if (!clocksource
&& !clockevent
) {
726 pr_err("%s: unable to detect system timers, update dtb?\n",
732 addr
= of_get_address(np
, 0, NULL
, NULL
);
733 pa
= of_translate_address(np
, addr
);
737 if (counter_32k
<= 0 && clocksource
== pa
)
738 return dmtimer_clocksource_init(np
);
740 if (clockevent
== pa
)
741 return dmtimer_clockevent_init(np
);
746 TIMER_OF_DECLARE(systimer_omap2
, "ti,omap2420-timer", dmtimer_systimer_init
);
747 TIMER_OF_DECLARE(systimer_omap3
, "ti,omap3430-timer", dmtimer_systimer_init
);
748 TIMER_OF_DECLARE(systimer_omap4
, "ti,omap4430-timer", dmtimer_systimer_init
);
749 TIMER_OF_DECLARE(systimer_omap5
, "ti,omap5430-timer", dmtimer_systimer_init
);
750 TIMER_OF_DECLARE(systimer_am33x
, "ti,am335x-timer", dmtimer_systimer_init
);
751 TIMER_OF_DECLARE(systimer_am3ms
, "ti,am335x-timer-1ms", dmtimer_systimer_init
);
752 TIMER_OF_DECLARE(systimer_dm814
, "ti,dm814-timer", dmtimer_systimer_init
);
753 TIMER_OF_DECLARE(systimer_dm816
, "ti,dm816-timer", dmtimer_systimer_init
);