1 // SPDX-License-Identifier: GPL-2.0+
6 #include <dm/fdtaddr.h>
9 #define TIMER_CTRL 0x00
10 #define TIMER0_EN BIT(0)
11 #define TIMER0_RELOAD_EN BIT(1)
12 #define TIMER0_RELOAD 0x10
13 #define TIMER0_VAL 0x14
15 enum input_clock_type {
16 INPUT_CLOCK_NON_FIXED,
17 INPUT_CLOCK_25MHZ, /* input clock rate is fixed to 25MHz */
20 struct orion_timer_priv {
24 #define MVEBU_TIMER_FIXED_RATE_25MHZ 25000000
26 static bool early_init_done __section(".data") = false;
28 /* Common functions for early (boot) and DM based timer */
29 static void orion_timer_init(void *base, enum input_clock_type type)
31 writel(~0, base + TIMER0_VAL);
32 writel(~0, base + TIMER0_RELOAD);
34 if (type == INPUT_CLOCK_25MHZ) {
36 * On Armada XP / 38x ..., the 25MHz clock source needs to
39 setbits_le32(base + TIMER_CTRL, BIT(11));
43 setbits_le32(base + TIMER_CTRL, TIMER0_EN | TIMER0_RELOAD_EN);
46 static uint64_t orion_timer_get_count(void *base)
48 return timer_conv_64(~readl(base + TIMER0_VAL));
51 /* Early (e.g. bootstage etc) timer functions */
52 static void notrace timer_early_init(void)
54 /* Only init the timer once */
57 early_init_done = true;
59 if (IS_ENABLED(CONFIG_ARCH_MVEBU))
60 orion_timer_init((void *)MVEBU_TIMER_BASE, INPUT_CLOCK_25MHZ);
62 orion_timer_init((void *)MVEBU_TIMER_BASE, INPUT_CLOCK_NON_FIXED);
66 * timer_early_get_rate() - Get the timer rate before driver model
68 unsigned long notrace timer_early_get_rate(void)
72 if (IS_ENABLED(CONFIG_ARCH_MVEBU))
73 return MVEBU_TIMER_FIXED_RATE_25MHZ;
75 return CONFIG_SYS_TCLK;
79 * timer_early_get_count() - Get the timer count before driver model
82 u64 notrace timer_early_get_count(void)
86 return orion_timer_get_count((void *)MVEBU_TIMER_BASE);
89 ulong timer_get_boot_us(void)
93 ticks = timer_early_get_count();
94 return lldiv(ticks * 1000, timer_early_get_rate());
97 /* DM timer functions */
98 static uint64_t dm_orion_timer_get_count(struct udevice *dev)
100 struct orion_timer_priv *priv = dev_get_priv(dev);
102 return orion_timer_get_count(priv->base);
105 static int orion_timer_probe(struct udevice *dev)
107 struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
108 enum input_clock_type type = dev_get_driver_data(dev);
109 struct orion_timer_priv *priv = dev_get_priv(dev);
111 priv->base = devfdt_remap_addr_index(dev, 0);
113 debug("unable to map registers\n");
117 if (type == INPUT_CLOCK_25MHZ)
118 uc_priv->clock_rate = MVEBU_TIMER_FIXED_RATE_25MHZ;
120 uc_priv->clock_rate = CONFIG_SYS_TCLK;
121 orion_timer_init(priv->base, type);
126 static const struct timer_ops orion_timer_ops = {
127 .get_count = dm_orion_timer_get_count,
130 static const struct udevice_id orion_timer_ids[] = {
131 { .compatible = "marvell,orion-timer", .data = INPUT_CLOCK_NON_FIXED },
132 { .compatible = "marvell,armada-370-timer", .data = INPUT_CLOCK_25MHZ },
133 { .compatible = "marvell,armada-xp-timer", .data = INPUT_CLOCK_25MHZ },
137 U_BOOT_DRIVER(orion_timer) = {
138 .name = "orion_timer",
140 .of_match = orion_timer_ids,
141 .probe = orion_timer_probe,
142 .ops = &orion_timer_ops,
143 .priv_auto = sizeof(struct orion_timer_priv),