1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2022 Svyatoslav Ryhel <clamor95@gmail.com>
12 #include <asm/arch/clock.h>
13 #include <asm/arch/tegra.h>
15 #define TEGRA_OSC_CLK_ENB_L_SET (NV_PA_CLK_RST_BASE + 0x320)
16 #define TEGRA_OSC_SET_CLK_ENB_TMR BIT(5)
18 #define TEGRA_TIMER_USEC_CNTR (NV_PA_TMRUS_BASE + 0)
19 #define TEGRA_TIMER_USEC_CFG (NV_PA_TMRUS_BASE + 4)
21 #define TEGRA_TIMER_RATE 1000000 /* 1 MHz */
24 * On pre-DM stage timer should be left configured by
25 * previous bootloader for correct 1MHz clock.
26 * In the case of reset default value is set to 1/13 of
27 * CLK_M which should be decent enough to safely
30 u64 notrace timer_early_get_count(void)
32 /* At this stage raw timer is used */
33 return readl(TEGRA_TIMER_USEC_CNTR);
36 unsigned long notrace timer_early_get_rate(void)
38 return TEGRA_TIMER_RATE;
41 ulong timer_get_boot_us(void)
43 return timer_early_get_count();
47 * At moment of calling get_count, timer driver is already
48 * probed and is configured to have precise 1MHz clock.
49 * Tegra timer has a step of 1 microsecond which removes
50 * need of using adjusments involving uc_priv->clock_rate.
52 static notrace u64 tegra_timer_get_count(struct udevice *dev)
54 u32 val = timer_early_get_count();
55 return timer_conv_64(val);
58 static int tegra_timer_probe(struct udevice *dev)
60 struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
61 u32 usec_config, value;
63 /* Timer rate has to be set unconditionally */
64 uc_priv->clock_rate = TEGRA_TIMER_RATE;
67 * Configure microsecond timers to have 1MHz clock
68 * Config register is 0xqqww, where qq is "dividend", ww is "divisor"
71 switch (clock_get_rate(CLOCK_ID_CLK_M)) {
73 usec_config = 0x000b; /* (11+1)/(0+1) */
76 usec_config = 0x043f; /* (63+1)/(4+1) */
79 usec_config = 0x000c; /* (12+1)/(0+1) */
82 usec_config = 0x0453; /* (83+1)/(4+1) */
85 usec_config = 0x045f; /* (95+1)/(4+1) */
88 usec_config = 0x0019; /* (25+1)/(0+1) */
91 usec_config = 0x04bf; /* (191+1)/(4+1) */
94 usec_config = 0x002f; /* (47+1)/(0+1) */
100 /* Enable clock to timer hardware */
101 value = readl_relaxed(TEGRA_OSC_CLK_ENB_L_SET);
102 writel_relaxed(value | TEGRA_OSC_SET_CLK_ENB_TMR,
103 TEGRA_OSC_CLK_ENB_L_SET);
105 writel_relaxed(usec_config, TEGRA_TIMER_USEC_CFG);
110 static const struct timer_ops tegra_timer_ops = {
111 .get_count = tegra_timer_get_count,
114 static const struct udevice_id tegra_timer_ids[] = {
115 { .compatible = "nvidia,tegra20-timer" },
116 { .compatible = "nvidia,tegra30-timer" },
117 { .compatible = "nvidia,tegra114-timer" },
118 { .compatible = "nvidia,tegra124-timer" },
119 { .compatible = "nvidia,tegra210-timer" },
123 U_BOOT_DRIVER(tegra_timer) = {
124 .name = "tegra_timer",
126 .of_match = tegra_timer_ids,
127 .probe = tegra_timer_probe,
128 .ops = &tegra_timer_ops,
129 .flags = DM_FLAG_PRE_RELOC,