blob: 9cab27f2e48b51ef3b6585c2b8889578385e5e7d [file] [log] [blame]
Michael Wallef874e092022-08-17 21:37:51 +02001// SPDX-License-Identifier: GPL-2.0+
2#include <asm/io.h>
3#include <common.h>
Stefan Roese537f0182022-09-15 16:20:38 +02004#include <div64.h>
Michael Wallef874e092022-08-17 21:37:51 +02005#include <dm/device.h>
6#include <dm/fdtaddr.h>
7#include <timer.h>
8
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
14
Stefan Roese70280f22022-09-15 16:20:37 +020015enum input_clock_type {
16 INPUT_CLOCK_NON_FIXED,
17 INPUT_CLOCK_25MHZ, /* input clock rate is fixed to 25MHz */
18};
19
Michael Wallef874e092022-08-17 21:37:51 +020020struct orion_timer_priv {
21 void *base;
22};
23
Stefan Roese70280f22022-09-15 16:20:37 +020024#define MVEBU_TIMER_FIXED_RATE_25MHZ 25000000
25
Stefan Roese04bcda82022-12-21 10:18:49 +010026static bool early_init_done(void *base)
27{
Stefan Roese20cd6db2023-01-16 09:01:48 +010028 if ((readl(base + TIMER_CTRL) & TIMER0_EN) &&
29 (readl(base + TIMER0_RELOAD) == ~0))
Stefan Roese04bcda82022-12-21 10:18:49 +010030 return true;
31 return false;
32}
Stefan Roese537f0182022-09-15 16:20:38 +020033
34/* Common functions for early (boot) and DM based timer */
35static void orion_timer_init(void *base, enum input_clock_type type)
36{
Stefan Roesebd758d42022-09-21 08:26:42 +020037 /* Only init the timer once */
Stefan Roese04bcda82022-12-21 10:18:49 +010038 if (early_init_done(base))
Stefan Roesebd758d42022-09-21 08:26:42 +020039 return;
Stefan Roesebd758d42022-09-21 08:26:42 +020040
Stefan Roese537f0182022-09-15 16:20:38 +020041 writel(~0, base + TIMER0_VAL);
42 writel(~0, base + TIMER0_RELOAD);
43
44 if (type == INPUT_CLOCK_25MHZ) {
45 /*
46 * On Armada XP / 38x ..., the 25MHz clock source needs to
47 * be enabled
48 */
49 setbits_le32(base + TIMER_CTRL, BIT(11));
50 }
51
52 /* enable timer */
53 setbits_le32(base + TIMER_CTRL, TIMER0_EN | TIMER0_RELOAD_EN);
54}
55
56static uint64_t orion_timer_get_count(void *base)
57{
58 return timer_conv_64(~readl(base + TIMER0_VAL));
59}
60
61/* Early (e.g. bootstage etc) timer functions */
62static void notrace timer_early_init(void)
63{
Stefan Roese537f0182022-09-15 16:20:38 +020064 if (IS_ENABLED(CONFIG_ARCH_MVEBU))
65 orion_timer_init((void *)MVEBU_TIMER_BASE, INPUT_CLOCK_25MHZ);
66 else
67 orion_timer_init((void *)MVEBU_TIMER_BASE, INPUT_CLOCK_NON_FIXED);
68}
69
Stefan Roese70280f22022-09-15 16:20:37 +020070/**
71 * timer_early_get_rate() - Get the timer rate before driver model
72 */
73unsigned long notrace timer_early_get_rate(void)
74{
Stefan Roese537f0182022-09-15 16:20:38 +020075 timer_early_init();
76
77 if (IS_ENABLED(CONFIG_ARCH_MVEBU))
78 return MVEBU_TIMER_FIXED_RATE_25MHZ;
79 else
Tom Rini6a5dccc2022-11-16 13:10:41 -050080 return CFG_SYS_TCLK;
Stefan Roese70280f22022-09-15 16:20:37 +020081}
82
83/**
84 * timer_early_get_count() - Get the timer count before driver model
85 *
86 */
87u64 notrace timer_early_get_count(void)
88{
Stefan Roese537f0182022-09-15 16:20:38 +020089 timer_early_init();
90
91 return orion_timer_get_count((void *)MVEBU_TIMER_BASE);
Stefan Roese70280f22022-09-15 16:20:37 +020092}
93
Stefan Roese537f0182022-09-15 16:20:38 +020094ulong timer_get_boot_us(void)
95{
96 u64 ticks;
97
98 ticks = timer_early_get_count();
99 return lldiv(ticks * 1000, timer_early_get_rate());
100}
101
102/* DM timer functions */
103static uint64_t dm_orion_timer_get_count(struct udevice *dev)
Michael Wallef874e092022-08-17 21:37:51 +0200104{
105 struct orion_timer_priv *priv = dev_get_priv(dev);
106
Stefan Roese537f0182022-09-15 16:20:38 +0200107 return orion_timer_get_count(priv->base);
Michael Wallef874e092022-08-17 21:37:51 +0200108}
109
110static int orion_timer_probe(struct udevice *dev)
111{
112 struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Stefan Roese70280f22022-09-15 16:20:37 +0200113 enum input_clock_type type = dev_get_driver_data(dev);
Michael Wallef874e092022-08-17 21:37:51 +0200114 struct orion_timer_priv *priv = dev_get_priv(dev);
115
116 priv->base = devfdt_remap_addr_index(dev, 0);
117 if (!priv->base) {
118 debug("unable to map registers\n");
119 return -ENOMEM;
120 }
121
Stefan Roese537f0182022-09-15 16:20:38 +0200122 if (type == INPUT_CLOCK_25MHZ)
Stefan Roese70280f22022-09-15 16:20:37 +0200123 uc_priv->clock_rate = MVEBU_TIMER_FIXED_RATE_25MHZ;
Stefan Roese537f0182022-09-15 16:20:38 +0200124 else
Tom Rini6a5dccc2022-11-16 13:10:41 -0500125 uc_priv->clock_rate = CFG_SYS_TCLK;
Stefan Roese537f0182022-09-15 16:20:38 +0200126 orion_timer_init(priv->base, type);
Michael Wallef874e092022-08-17 21:37:51 +0200127
128 return 0;
129}
130
131static const struct timer_ops orion_timer_ops = {
Stefan Roese537f0182022-09-15 16:20:38 +0200132 .get_count = dm_orion_timer_get_count,
Michael Wallef874e092022-08-17 21:37:51 +0200133};
134
135static const struct udevice_id orion_timer_ids[] = {
Stefan Roese70280f22022-09-15 16:20:37 +0200136 { .compatible = "marvell,orion-timer", .data = INPUT_CLOCK_NON_FIXED },
137 { .compatible = "marvell,armada-370-timer", .data = INPUT_CLOCK_25MHZ },
138 { .compatible = "marvell,armada-xp-timer", .data = INPUT_CLOCK_25MHZ },
Michael Wallef874e092022-08-17 21:37:51 +0200139 {}
140};
141
142U_BOOT_DRIVER(orion_timer) = {
143 .name = "orion_timer",
144 .id = UCLASS_TIMER,
145 .of_match = orion_timer_ids,
146 .probe = orion_timer_probe,
147 .ops = &orion_timer_ops,
148 .priv_auto = sizeof(struct orion_timer_priv),
149};