1 // SPDX-License-Identifier: GPL-2.0+
3 #include <linux/clocksource.h>
4 #include <linux/clockchips.h>
5 #include <linux/cpuhotplug.h>
6 #include <linux/interrupt.h>
8 #include <linux/iopoll.h>
11 #include <linux/of_address.h>
12 #include <linux/of_irq.h>
13 #include <linux/sched_clock.h>
15 #include <linux/clk/clk-conf.h>
17 #include <clocksource/timer-ti-dm.h>
18 #include <dt-bindings/bus/ti-sysc.h>
20 /* For type1, set SYSC_OMAP2_CLOCKACTIVITY for fck off on idle, l4 clock on */
21 #define DMTIMER_TYPE1_ENABLE ((1 << 9) | (SYSC_IDLE_SMART << 3) | \
22 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_AUTOIDLE)
23 #define DMTIMER_TYPE1_DISABLE (SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE)
24 #define DMTIMER_TYPE2_ENABLE (SYSC_IDLE_SMART_WKUP << 2)
25 #define DMTIMER_RESET_WAIT 100000
27 #define DMTIMER_INST_DONT_CARE ~0U
29 static int counter_32k;
30 static u32 clocksource;
31 static u32 clockevent;
34 * Subset of the timer registers we use. Note that the register offsets
35 * depend on the timer revision detected.
37 struct dmtimer_systimer {
53 struct dmtimer_clockevent {
54 struct clock_event_device dev;
55 struct dmtimer_systimer t;
59 struct dmtimer_clocksource {
60 struct clocksource dev;
61 struct dmtimer_systimer t;
65 /* Assumes v1 ip if bits [31:16] are zero */
66 static bool dmtimer_systimer_revision1(struct dmtimer_systimer *t)
68 u32 tidr = readl_relaxed(t->base);
73 static void dmtimer_systimer_enable(struct dmtimer_systimer *t)
77 if (dmtimer_systimer_revision1(t))
78 val = DMTIMER_TYPE1_ENABLE;
80 val = DMTIMER_TYPE2_ENABLE;
82 writel_relaxed(val, t->base + t->sysc);
85 static void dmtimer_systimer_disable(struct dmtimer_systimer *t)
87 if (!dmtimer_systimer_revision1(t))
90 writel_relaxed(DMTIMER_TYPE1_DISABLE, t->base + t->sysc);
93 static int __init dmtimer_systimer_type1_reset(struct dmtimer_systimer *t)
95 void __iomem *syss = t->base + OMAP_TIMER_V1_SYS_STAT_OFFSET;
99 dmtimer_systimer_enable(t);
100 writel_relaxed(BIT(1) | BIT(2), t->base + t->ifctrl);
101 ret = readl_poll_timeout_atomic(syss, l, l & BIT(0), 100,
107 /* Note we must use io_base instead of func_base for type2 OCP regs */
108 static int __init dmtimer_systimer_type2_reset(struct dmtimer_systimer *t)
110 void __iomem *sysc = t->base + t->sysc;
113 dmtimer_systimer_enable(t);
114 l = readl_relaxed(sysc);
116 writel_relaxed(l, sysc);
118 return readl_poll_timeout_atomic(sysc, l, !(l & BIT(0)), 100,
122 static int __init dmtimer_systimer_reset(struct dmtimer_systimer *t)
126 if (dmtimer_systimer_revision1(t))
127 ret = dmtimer_systimer_type1_reset(t);
129 ret = dmtimer_systimer_type2_reset(t);
131 pr_err("%s failed with %i\n", __func__, ret);
139 static const struct of_device_id counter_match_table[] = {
140 { .compatible = "ti,omap-counter32k" },
145 * Check if the SoC als has a usable working 32 KiHz counter. The 32 KiHz
146 * counter is handled by timer-ti-32k, but we need to detect it as it
147 * affects the preferred dmtimer system timer configuration. There is
148 * typically no use for a dmtimer clocksource if the 32 KiHz counter is
149 * present, except on am437x as described below.
151 static void __init dmtimer_systimer_check_counter32k(void)
153 struct device_node *np;
158 np = of_find_matching_node(NULL, counter_match_table);
160 counter_32k = -ENODEV;
165 if (of_device_is_available(np))
168 counter_32k = -ENODEV;
173 static const struct of_device_id dmtimer_match_table[] = {
174 { .compatible = "ti,omap2420-timer", },
175 { .compatible = "ti,omap3430-timer", },
176 { .compatible = "ti,omap4430-timer", },
177 { .compatible = "ti,omap5430-timer", },
178 { .compatible = "ti,am335x-timer", },
179 { .compatible = "ti,am335x-timer-1ms", },
180 { .compatible = "ti,dm814-timer", },
181 { .compatible = "ti,dm816-timer", },
186 * Checks that system timers are configured to not reset and idle during
187 * the generic timer-ti-dm device driver probe. And that the system timer
188 * source clocks are properly configured. Also, let's not hog any DSP and
189 * PWM capable timers unnecessarily as system timers.
191 static bool __init dmtimer_is_preferred(struct device_node *np)
193 if (!of_device_is_available(np))
196 if (!of_property_read_bool(np->parent,
197 "ti,no-reset-on-init"))
200 if (!of_property_read_bool(np->parent, "ti,no-idle"))
203 /* Secure gptimer12 is always clocked with a fixed source */
204 if (!of_property_read_bool(np, "ti,timer-secure")) {
205 if (!of_property_read_bool(np, "assigned-clocks"))
208 if (!of_property_read_bool(np, "assigned-clock-parents"))
212 if (of_property_read_bool(np, "ti,timer-dsp"))
215 if (of_property_read_bool(np, "ti,timer-pwm"))
222 * Finds the first available usable always-on timer, and assigns it to either
223 * clockevent or clocksource depending if the counter_32k is available on the
226 * Some omap3 boards with unreliable oscillator must not use the counter_32k
227 * or dmtimer1 with 32 KiHz source. Additionally, the boards with unreliable
228 * oscillator should really set counter_32k as disabled, and delete dmtimer1
229 * ti,always-on property, but let's not count on it. For these quirky cases,
230 * we prefer using the always-on secure dmtimer12 with the internal 32 KiHz
231 * clock as the clocksource, and any available dmtimer as clockevent.
233 * For am437x, we are using am335x style dmtimer clocksource. It is unclear
234 * if this quirk handling is really needed, but let's change it separately
235 * based on testing as it might cause side effects.
237 static void __init dmtimer_systimer_assign_alwon(void)
239 struct device_node *np;
241 bool quirk_unreliable_oscillator = false;
243 /* Quirk unreliable 32 KiHz oscillator with incomplete dts */
244 if (of_machine_is_compatible("ti,omap3-beagle-ab4")) {
245 quirk_unreliable_oscillator = true;
246 counter_32k = -ENODEV;
249 /* Quirk am437x using am335x style dmtimer clocksource */
250 if (of_machine_is_compatible("ti,am43"))
251 counter_32k = -ENODEV;
253 for_each_matching_node(np, dmtimer_match_table) {
254 if (!dmtimer_is_preferred(np))
257 if (of_property_read_bool(np, "ti,timer-alwon")) {
260 addr = of_get_address(np, 0, NULL, NULL);
261 pa = of_translate_address(np, addr);
263 /* Quirky omap3 boards must use dmtimer12 */
264 if (quirk_unreliable_oscillator &&
274 /* Usually no need for dmtimer clocksource if we have counter32 */
275 if (counter_32k >= 0) {
280 clockevent = DMTIMER_INST_DONT_CARE;
284 /* Finds the first usable dmtimer, used for the don't care case */
285 static u32 __init dmtimer_systimer_find_first_available(void)
287 struct device_node *np;
291 for_each_matching_node(np, dmtimer_match_table) {
292 if (!dmtimer_is_preferred(np))
295 addr = of_get_address(np, 0, NULL, NULL);
296 pa = of_translate_address(np, addr);
298 if (pa == clocksource || pa == clockevent) {
311 /* Selects the best clocksource and clockevent to use */
312 static void __init dmtimer_systimer_select_best(void)
314 dmtimer_systimer_check_counter32k();
315 dmtimer_systimer_assign_alwon();
317 if (clockevent == DMTIMER_INST_DONT_CARE)
318 clockevent = dmtimer_systimer_find_first_available();
320 pr_debug("%s: counter_32k: %i clocksource: %08x clockevent: %08x\n",
321 __func__, counter_32k, clocksource, clockevent);
324 /* Interface clocks are only available on some SoCs variants */
325 static int __init dmtimer_systimer_init_clock(struct dmtimer_systimer *t,
326 struct device_node *np,
335 is_ick = !strncmp(name, "ick", 3);
337 clock = of_clk_get_by_name(np, name);
338 if ((PTR_ERR(clock) == -EINVAL) && is_ick)
340 else if (IS_ERR(clock))
341 return PTR_ERR(clock);
343 error = clk_prepare_enable(clock);
347 r = clk_get_rate(clock);
349 clk_disable_unprepare(clock);
363 static int __init dmtimer_systimer_setup(struct device_node *np,
364 struct dmtimer_systimer *t)
370 if (!of_device_is_compatible(np->parent, "ti,sysc"))
373 t->base = of_iomap(np, 0);
378 * Enable optional assigned-clock-parents configured at the timer
379 * node level. For regular device drivers, this is done automatically
380 * by bus related code such as platform_drv_probe().
382 error = of_clk_set_defaults(np, false);
384 pr_err("%s: clock source init failed: %i\n", __func__, error);
386 /* For ti-sysc, we have timer clocks at the parent module level */
387 error = dmtimer_systimer_init_clock(t, np->parent, "fck", &rate);
393 error = dmtimer_systimer_init_clock(t, np->parent, "ick", &rate);
397 if (dmtimer_systimer_revision1(t)) {
398 t->irq_stat = OMAP_TIMER_V1_STAT_OFFSET;
399 t->irq_ena = OMAP_TIMER_V1_INT_EN_OFFSET;
400 t->pend = _OMAP_TIMER_WRITE_PEND_OFFSET;
403 t->irq_stat = OMAP_TIMER_V2_IRQSTATUS;
404 t->irq_ena = OMAP_TIMER_V2_IRQENABLE_SET;
405 regbase = OMAP_TIMER_V2_FUNC_OFFSET;
406 t->pend = regbase + _OMAP_TIMER_WRITE_PEND_OFFSET;
409 t->sysc = OMAP_TIMER_OCP_CFG_OFFSET;
410 t->load = regbase + _OMAP_TIMER_LOAD_OFFSET;
411 t->counter = regbase + _OMAP_TIMER_COUNTER_OFFSET;
412 t->ctrl = regbase + _OMAP_TIMER_CTRL_OFFSET;
413 t->wakeup = regbase + _OMAP_TIMER_WAKEUP_EN_OFFSET;
414 t->ifctrl = regbase + _OMAP_TIMER_IF_CTRL_OFFSET;
416 dmtimer_systimer_reset(t);
417 dmtimer_systimer_enable(t);
418 pr_debug("dmtimer rev %08x sysc %08x\n", readl_relaxed(t->base),
419 readl_relaxed(t->base + t->sysc));
430 static struct dmtimer_clockevent *
431 to_dmtimer_clockevent(struct clock_event_device *clockevent)
433 return container_of(clockevent, struct dmtimer_clockevent, dev);
436 static irqreturn_t dmtimer_clockevent_interrupt(int irq, void *data)
438 struct dmtimer_clockevent *clkevt = data;
439 struct dmtimer_systimer *t = &clkevt->t;
441 writel_relaxed(OMAP_TIMER_INT_OVERFLOW, t->base + t->irq_stat);
442 clkevt->dev.event_handler(&clkevt->dev);
447 static int dmtimer_set_next_event(unsigned long cycles,
448 struct clock_event_device *evt)
450 struct dmtimer_clockevent *clkevt = to_dmtimer_clockevent(evt);
451 struct dmtimer_systimer *t = &clkevt->t;
452 void __iomem *pend = t->base + t->pend;
454 while (readl_relaxed(pend) & WP_TCRR)
456 writel_relaxed(0xffffffff - cycles, t->base + t->counter);
458 while (readl_relaxed(pend) & WP_TCLR)
460 writel_relaxed(OMAP_TIMER_CTRL_ST, t->base + t->ctrl);
465 static int dmtimer_clockevent_shutdown(struct clock_event_device *evt)
467 struct dmtimer_clockevent *clkevt = to_dmtimer_clockevent(evt);
468 struct dmtimer_systimer *t = &clkevt->t;
469 void __iomem *ctrl = t->base + t->ctrl;
472 l = readl_relaxed(ctrl);
473 if (l & OMAP_TIMER_CTRL_ST) {
475 writel_relaxed(l, ctrl);
476 /* Flush posted write */
477 l = readl_relaxed(ctrl);
478 /* Wait for functional clock period x 3.5 */
479 udelay(3500000 / t->rate + 1);
481 writel_relaxed(OMAP_TIMER_INT_OVERFLOW, t->base + t->irq_stat);
486 static int dmtimer_set_periodic(struct clock_event_device *evt)
488 struct dmtimer_clockevent *clkevt = to_dmtimer_clockevent(evt);
489 struct dmtimer_systimer *t = &clkevt->t;
490 void __iomem *pend = t->base + t->pend;
492 dmtimer_clockevent_shutdown(evt);
494 /* Looks like we need to first set the load value separately */
495 while (readl_relaxed(pend) & WP_TLDR)
497 writel_relaxed(clkevt->period, t->base + t->load);
499 while (readl_relaxed(pend) & WP_TCRR)
501 writel_relaxed(clkevt->period, t->base + t->counter);
503 while (readl_relaxed(pend) & WP_TCLR)
505 writel_relaxed(OMAP_TIMER_CTRL_AR | OMAP_TIMER_CTRL_ST,
511 static void omap_clockevent_idle(struct clock_event_device *evt)
513 struct dmtimer_clockevent *clkevt = to_dmtimer_clockevent(evt);
514 struct dmtimer_systimer *t = &clkevt->t;
516 dmtimer_systimer_disable(t);
520 static void omap_clockevent_unidle(struct clock_event_device *evt)
522 struct dmtimer_clockevent *clkevt = to_dmtimer_clockevent(evt);
523 struct dmtimer_systimer *t = &clkevt->t;
526 error = clk_enable(t->fck);
528 pr_err("could not enable timer fck on resume: %i\n", error);
530 dmtimer_systimer_enable(t);
531 writel_relaxed(OMAP_TIMER_INT_OVERFLOW, t->base + t->irq_ena);
532 writel_relaxed(OMAP_TIMER_INT_OVERFLOW, t->base + t->wakeup);
535 static int __init dmtimer_clkevt_init_common(struct dmtimer_clockevent *clkevt,
536 struct device_node *np,
537 unsigned int features,
538 const struct cpumask *cpumask,
542 struct clock_event_device *dev;
543 struct dmtimer_systimer *t;
550 * We mostly use cpuidle_coupled with ARM local timers for runtime,
551 * so there's probably no use for CLOCK_EVT_FEAT_DYNIRQ here.
553 dev->features = features;
554 dev->rating = rating;
555 dev->set_next_event = dmtimer_set_next_event;
556 dev->set_state_shutdown = dmtimer_clockevent_shutdown;
557 dev->set_state_periodic = dmtimer_set_periodic;
558 dev->set_state_oneshot = dmtimer_clockevent_shutdown;
559 dev->set_state_oneshot_stopped = dmtimer_clockevent_shutdown;
560 dev->tick_resume = dmtimer_clockevent_shutdown;
561 dev->cpumask = cpumask;
563 dev->irq = irq_of_parse_and_map(np, 0);
567 error = dmtimer_systimer_setup(np, &clkevt->t);
571 clkevt->period = 0xffffffff - DIV_ROUND_CLOSEST(t->rate, HZ);
574 * For clock-event timers we never read the timer counter and
575 * so we are not impacted by errata i103 and i767. Therefore,
576 * we can safely ignore this errata for clock-event timers.
578 writel_relaxed(OMAP_TIMER_CTRL_POSTED, t->base + t->ifctrl);
580 error = request_irq(dev->irq, dmtimer_clockevent_interrupt,
581 IRQF_TIMER, name, clkevt);
585 writel_relaxed(OMAP_TIMER_INT_OVERFLOW, t->base + t->irq_ena);
586 writel_relaxed(OMAP_TIMER_INT_OVERFLOW, t->base + t->wakeup);
588 pr_info("TI gptimer %s: %s%lu Hz at %pOF\n",
589 name, of_find_property(np, "ti,timer-alwon", NULL) ?
590 "always-on " : "", t->rate, np->parent);
600 static int __init dmtimer_clockevent_init(struct device_node *np)
602 struct dmtimer_clockevent *clkevt;
605 clkevt = kzalloc(sizeof(*clkevt), GFP_KERNEL);
609 error = dmtimer_clkevt_init_common(clkevt, np,
610 CLOCK_EVT_FEAT_PERIODIC |
611 CLOCK_EVT_FEAT_ONESHOT,
612 cpu_possible_mask, "clockevent",
617 clockevents_config_and_register(&clkevt->dev, clkevt->t.rate,
618 3, /* Timer internal resync latency */
621 if (of_machine_is_compatible("ti,am33xx") ||
622 of_machine_is_compatible("ti,am43")) {
623 clkevt->dev.suspend = omap_clockevent_idle;
624 clkevt->dev.resume = omap_clockevent_unidle;
635 /* Dmtimer as percpu timer. See dra7 ARM architected timer wrap erratum i940 */
636 static DEFINE_PER_CPU(struct dmtimer_clockevent, dmtimer_percpu_timer);
638 static int __init dmtimer_percpu_timer_init(struct device_node *np, int cpu)
640 struct dmtimer_clockevent *clkevt;
643 if (!cpu_possible(cpu))
646 if (!of_property_read_bool(np->parent, "ti,no-reset-on-init") ||
647 !of_property_read_bool(np->parent, "ti,no-idle"))
648 pr_warn("Incomplete dtb for percpu dmtimer %pOF\n", np->parent);
650 clkevt = per_cpu_ptr(&dmtimer_percpu_timer, cpu);
652 error = dmtimer_clkevt_init_common(clkevt, np, CLOCK_EVT_FEAT_ONESHOT,
653 cpumask_of(cpu), "percpu-dmtimer",
661 /* See TRM for timer internal resynch latency */
662 static int omap_dmtimer_starting_cpu(unsigned int cpu)
664 struct dmtimer_clockevent *clkevt = per_cpu_ptr(&dmtimer_percpu_timer, cpu);
665 struct clock_event_device *dev = &clkevt->dev;
666 struct dmtimer_systimer *t = &clkevt->t;
668 clockevents_config_and_register(dev, t->rate, 3, ULONG_MAX);
669 irq_force_affinity(dev->irq, cpumask_of(cpu));
674 static int __init dmtimer_percpu_timer_startup(void)
676 struct dmtimer_clockevent *clkevt = per_cpu_ptr(&dmtimer_percpu_timer, 0);
677 struct dmtimer_systimer *t = &clkevt->t;
680 cpuhp_setup_state(CPUHP_AP_TI_GP_TIMER_STARTING,
681 "clockevents/omap/gptimer:starting",
682 omap_dmtimer_starting_cpu, NULL);
687 subsys_initcall(dmtimer_percpu_timer_startup);
689 static int __init dmtimer_percpu_quirk_init(struct device_node *np, u32 pa)
691 struct device_node *arm_timer;
693 arm_timer = of_find_compatible_node(NULL, NULL, "arm,armv7-timer");
694 if (of_device_is_available(arm_timer)) {
695 pr_warn_once("ARM architected timer wrap issue i940 detected\n");
699 if (pa == 0x4882c000) /* dra7 dmtimer15 */
700 return dmtimer_percpu_timer_init(np, 0);
701 else if (pa == 0x4882e000) /* dra7 dmtimer16 */
702 return dmtimer_percpu_timer_init(np, 1);
708 static struct dmtimer_clocksource *
709 to_dmtimer_clocksource(struct clocksource *cs)
711 return container_of(cs, struct dmtimer_clocksource, dev);
714 static u64 dmtimer_clocksource_read_cycles(struct clocksource *cs)
716 struct dmtimer_clocksource *clksrc = to_dmtimer_clocksource(cs);
717 struct dmtimer_systimer *t = &clksrc->t;
719 return (u64)readl_relaxed(t->base + t->counter);
722 static void __iomem *dmtimer_sched_clock_counter;
724 static u64 notrace dmtimer_read_sched_clock(void)
726 return readl_relaxed(dmtimer_sched_clock_counter);
729 static void dmtimer_clocksource_suspend(struct clocksource *cs)
731 struct dmtimer_clocksource *clksrc = to_dmtimer_clocksource(cs);
732 struct dmtimer_systimer *t = &clksrc->t;
734 clksrc->loadval = readl_relaxed(t->base + t->counter);
735 dmtimer_systimer_disable(t);
739 static void dmtimer_clocksource_resume(struct clocksource *cs)
741 struct dmtimer_clocksource *clksrc = to_dmtimer_clocksource(cs);
742 struct dmtimer_systimer *t = &clksrc->t;
745 error = clk_enable(t->fck);
747 pr_err("could not enable timer fck on resume: %i\n", error);
749 dmtimer_systimer_enable(t);
750 writel_relaxed(clksrc->loadval, t->base + t->counter);
751 writel_relaxed(OMAP_TIMER_CTRL_ST | OMAP_TIMER_CTRL_AR,
755 static int __init dmtimer_clocksource_init(struct device_node *np)
757 struct dmtimer_clocksource *clksrc;
758 struct dmtimer_systimer *t;
759 struct clocksource *dev;
762 clksrc = kzalloc(sizeof(*clksrc), GFP_KERNEL);
769 error = dmtimer_systimer_setup(np, t);
773 dev->name = "dmtimer";
775 dev->read = dmtimer_clocksource_read_cycles;
776 dev->mask = CLOCKSOURCE_MASK(32);
777 dev->flags = CLOCK_SOURCE_IS_CONTINUOUS;
779 /* Unlike for clockevent, legacy code sets suspend only for am4 */
780 if (of_machine_is_compatible("ti,am43")) {
781 dev->suspend = dmtimer_clocksource_suspend;
782 dev->resume = dmtimer_clocksource_resume;
785 writel_relaxed(0, t->base + t->counter);
786 writel_relaxed(OMAP_TIMER_CTRL_ST | OMAP_TIMER_CTRL_AR,
789 pr_info("TI gptimer clocksource: %s%pOF\n",
790 of_find_property(np, "ti,timer-alwon", NULL) ?
791 "always-on " : "", np->parent);
793 if (!dmtimer_sched_clock_counter) {
794 dmtimer_sched_clock_counter = t->base + t->counter;
795 sched_clock_register(dmtimer_read_sched_clock, 32, t->rate);
798 if (clocksource_register_hz(dev, t->rate))
799 pr_err("Could not register clocksource %pOF\n", np);
810 * To detect between a clocksource and clockevent, we assume the device tree
811 * has no interrupts configured for a clocksource timer.
813 static int __init dmtimer_systimer_init(struct device_node *np)
818 /* One time init for the preferred timer configuration */
819 if (!clocksource && !clockevent)
820 dmtimer_systimer_select_best();
822 if (!clocksource && !clockevent) {
823 pr_err("%s: unable to detect system timers, update dtb?\n",
829 addr = of_get_address(np, 0, NULL, NULL);
830 pa = of_translate_address(np, addr);
834 if (counter_32k <= 0 && clocksource == pa)
835 return dmtimer_clocksource_init(np);
837 if (clockevent == pa)
838 return dmtimer_clockevent_init(np);
840 if (of_machine_is_compatible("ti,dra7"))
841 return dmtimer_percpu_quirk_init(np, pa);
846 TIMER_OF_DECLARE(systimer_omap2, "ti,omap2420-timer", dmtimer_systimer_init);
847 TIMER_OF_DECLARE(systimer_omap3, "ti,omap3430-timer", dmtimer_systimer_init);
848 TIMER_OF_DECLARE(systimer_omap4, "ti,omap4430-timer", dmtimer_systimer_init);
849 TIMER_OF_DECLARE(systimer_omap5, "ti,omap5430-timer", dmtimer_systimer_init);
850 TIMER_OF_DECLARE(systimer_am33x, "ti,am335x-timer", dmtimer_systimer_init);
851 TIMER_OF_DECLARE(systimer_am3ms, "ti,am335x-timer-1ms", dmtimer_systimer_init);
852 TIMER_OF_DECLARE(systimer_dm814, "ti,dm814-timer", dmtimer_systimer_init);
853 TIMER_OF_DECLARE(systimer_dm816, "ti,dm816-timer", dmtimer_systimer_init);