blob: 810a03d549600fe90a411249ef983e25970443d1 [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{
28 if (readl(base + TIMER_CTRL) & TIMER0_EN)
29 return true;
30 return false;
31}
Stefan Roese537f0182022-09-15 16:20:38 +020032
33/* Common functions for early (boot) and DM based timer */
34static void orion_timer_init(void *base, enum input_clock_type type)
35{
Stefan Roesebd758d42022-09-21 08:26:42 +020036 /* Only init the timer once */
Stefan Roese04bcda82022-12-21 10:18:49 +010037 if (early_init_done(base))
Stefan Roesebd758d42022-09-21 08:26:42 +020038 return;
Stefan Roesebd758d42022-09-21 08:26:42 +020039
Stefan Roese537f0182022-09-15 16:20:38 +020040 writel(~0, base + TIMER0_VAL);
41 writel(~0, base + TIMER0_RELOAD);
42
43 if (type == INPUT_CLOCK_25MHZ) {
44 /*
45 * On Armada XP / 38x ..., the 25MHz clock source needs to
46 * be enabled
47 */
48 setbits_le32(base + TIMER_CTRL, BIT(11));
49 }
50
51 /* enable timer */
52 setbits_le32(base + TIMER_CTRL, TIMER0_EN | TIMER0_RELOAD_EN);
53}
54
55static uint64_t orion_timer_get_count(void *base)
56{
57 return timer_conv_64(~readl(base + TIMER0_VAL));
58}
59
60/* Early (e.g. bootstage etc) timer functions */
61static void notrace timer_early_init(void)
62{
Stefan Roese537f0182022-09-15 16:20:38 +020063 if (IS_ENABLED(CONFIG_ARCH_MVEBU))
64 orion_timer_init((void *)MVEBU_TIMER_BASE, INPUT_CLOCK_25MHZ);
65 else
66 orion_timer_init((void *)MVEBU_TIMER_BASE, INPUT_CLOCK_NON_FIXED);
67}
68
Stefan Roese70280f22022-09-15 16:20:37 +020069/**
70 * timer_early_get_rate() - Get the timer rate before driver model
71 */
72unsigned long notrace timer_early_get_rate(void)
73{
Stefan Roese537f0182022-09-15 16:20:38 +020074 timer_early_init();
75
76 if (IS_ENABLED(CONFIG_ARCH_MVEBU))
77 return MVEBU_TIMER_FIXED_RATE_25MHZ;
78 else
Tom Rini6a5dccc2022-11-16 13:10:41 -050079 return CFG_SYS_TCLK;
Stefan Roese70280f22022-09-15 16:20:37 +020080}
81
82/**
83 * timer_early_get_count() - Get the timer count before driver model
84 *
85 */
86u64 notrace timer_early_get_count(void)
87{
Stefan Roese537f0182022-09-15 16:20:38 +020088 timer_early_init();
89
90 return orion_timer_get_count((void *)MVEBU_TIMER_BASE);
Stefan Roese70280f22022-09-15 16:20:37 +020091}
92
Stefan Roese537f0182022-09-15 16:20:38 +020093ulong timer_get_boot_us(void)
94{
95 u64 ticks;
96
97 ticks = timer_early_get_count();
98 return lldiv(ticks * 1000, timer_early_get_rate());
99}
100
101/* DM timer functions */
102static uint64_t dm_orion_timer_get_count(struct udevice *dev)
Michael Wallef874e092022-08-17 21:37:51 +0200103{
104 struct orion_timer_priv *priv = dev_get_priv(dev);
105
Stefan Roese537f0182022-09-15 16:20:38 +0200106 return orion_timer_get_count(priv->base);
Michael Wallef874e092022-08-17 21:37:51 +0200107}
108
109static int orion_timer_probe(struct udevice *dev)
110{
111 struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Stefan Roese70280f22022-09-15 16:20:37 +0200112 enum input_clock_type type = dev_get_driver_data(dev);
Michael Wallef874e092022-08-17 21:37:51 +0200113 struct orion_timer_priv *priv = dev_get_priv(dev);
114
115 priv->base = devfdt_remap_addr_index(dev, 0);
116 if (!priv->base) {
117 debug("unable to map registers\n");
118 return -ENOMEM;
119 }
120
Stefan Roese537f0182022-09-15 16:20:38 +0200121 if (type == INPUT_CLOCK_25MHZ)
Stefan Roese70280f22022-09-15 16:20:37 +0200122 uc_priv->clock_rate = MVEBU_TIMER_FIXED_RATE_25MHZ;
Stefan Roese537f0182022-09-15 16:20:38 +0200123 else
Tom Rini6a5dccc2022-11-16 13:10:41 -0500124 uc_priv->clock_rate = CFG_SYS_TCLK;
Stefan Roese537f0182022-09-15 16:20:38 +0200125 orion_timer_init(priv->base, type);
Michael Wallef874e092022-08-17 21:37:51 +0200126
127 return 0;
128}
129
130static const struct timer_ops orion_timer_ops = {
Stefan Roese537f0182022-09-15 16:20:38 +0200131 .get_count = dm_orion_timer_get_count,
Michael Wallef874e092022-08-17 21:37:51 +0200132};
133
134static const struct udevice_id orion_timer_ids[] = {
Stefan Roese70280f22022-09-15 16:20:37 +0200135 { .compatible = "marvell,orion-timer", .data = INPUT_CLOCK_NON_FIXED },
136 { .compatible = "marvell,armada-370-timer", .data = INPUT_CLOCK_25MHZ },
137 { .compatible = "marvell,armada-xp-timer", .data = INPUT_CLOCK_25MHZ },
Michael Wallef874e092022-08-17 21:37:51 +0200138 {}
139};
140
141U_BOOT_DRIVER(orion_timer) = {
142 .name = "orion_timer",
143 .id = UCLASS_TIMER,
144 .of_match = orion_timer_ids,
145 .probe = orion_timer_probe,
146 .ops = &orion_timer_ops,
147 .priv_auto = sizeof(struct orion_timer_priv),
148};