Michael Walle | e9e73d7 | 2022-08-17 21:37:51 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | #include <asm/io.h> |
| 3 | #include <common.h> |
Stefan Roese | a68f13a | 2022-09-15 16:20:38 +0200 | [diff] [blame] | 4 | #include <div64.h> |
Michael Walle | e9e73d7 | 2022-08-17 21:37:51 +0200 | [diff] [blame] | 5 | #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 Roese | 89fd0cc | 2022-09-15 16:20:37 +0200 | [diff] [blame] | 15 | enum input_clock_type { |
| 16 | INPUT_CLOCK_NON_FIXED, |
| 17 | INPUT_CLOCK_25MHZ, /* input clock rate is fixed to 25MHz */ |
| 18 | }; |
| 19 | |
Michael Walle | e9e73d7 | 2022-08-17 21:37:51 +0200 | [diff] [blame] | 20 | struct orion_timer_priv { |
| 21 | void *base; |
| 22 | }; |
| 23 | |
Stefan Roese | 89fd0cc | 2022-09-15 16:20:37 +0200 | [diff] [blame] | 24 | #define MVEBU_TIMER_FIXED_RATE_25MHZ 25000000 |
| 25 | |
Stefan Roese | 5387b09 | 2022-12-21 10:18:49 +0100 | [diff] [blame] | 26 | static bool early_init_done(void *base) |
| 27 | { |
Stefan Roese | 9a13a76 | 2023-01-16 09:01:48 +0100 | [diff] [blame] | 28 | if ((readl(base + TIMER_CTRL) & TIMER0_EN) && |
| 29 | (readl(base + TIMER0_RELOAD) == ~0)) |
Stefan Roese | 5387b09 | 2022-12-21 10:18:49 +0100 | [diff] [blame] | 30 | return true; |
| 31 | return false; |
| 32 | } |
Stefan Roese | a68f13a | 2022-09-15 16:20:38 +0200 | [diff] [blame] | 33 | |
| 34 | /* Common functions for early (boot) and DM based timer */ |
| 35 | static void orion_timer_init(void *base, enum input_clock_type type) |
| 36 | { |
Stefan Roese | 37bb396 | 2022-09-21 08:26:42 +0200 | [diff] [blame] | 37 | /* Only init the timer once */ |
Stefan Roese | 5387b09 | 2022-12-21 10:18:49 +0100 | [diff] [blame] | 38 | if (early_init_done(base)) |
Stefan Roese | 37bb396 | 2022-09-21 08:26:42 +0200 | [diff] [blame] | 39 | return; |
Stefan Roese | 37bb396 | 2022-09-21 08:26:42 +0200 | [diff] [blame] | 40 | |
Stefan Roese | a68f13a | 2022-09-15 16:20:38 +0200 | [diff] [blame] | 41 | 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 | |
| 56 | static 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 */ |
| 62 | static void notrace timer_early_init(void) |
| 63 | { |
Stefan Roese | a68f13a | 2022-09-15 16:20:38 +0200 | [diff] [blame] | 64 | 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 Roese | 89fd0cc | 2022-09-15 16:20:37 +0200 | [diff] [blame] | 70 | /** |
| 71 | * timer_early_get_rate() - Get the timer rate before driver model |
| 72 | */ |
| 73 | unsigned long notrace timer_early_get_rate(void) |
| 74 | { |
Stefan Roese | a68f13a | 2022-09-15 16:20:38 +0200 | [diff] [blame] | 75 | timer_early_init(); |
| 76 | |
| 77 | if (IS_ENABLED(CONFIG_ARCH_MVEBU)) |
| 78 | return MVEBU_TIMER_FIXED_RATE_25MHZ; |
| 79 | else |
Tom Rini | 65cc0e2 | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 80 | return CFG_SYS_TCLK; |
Stefan Roese | 89fd0cc | 2022-09-15 16:20:37 +0200 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
| 84 | * timer_early_get_count() - Get the timer count before driver model |
| 85 | * |
| 86 | */ |
| 87 | u64 notrace timer_early_get_count(void) |
| 88 | { |
Stefan Roese | a68f13a | 2022-09-15 16:20:38 +0200 | [diff] [blame] | 89 | timer_early_init(); |
| 90 | |
| 91 | return orion_timer_get_count((void *)MVEBU_TIMER_BASE); |
Stefan Roese | 89fd0cc | 2022-09-15 16:20:37 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Stefan Roese | a68f13a | 2022-09-15 16:20:38 +0200 | [diff] [blame] | 94 | ulong 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 */ |
| 103 | static uint64_t dm_orion_timer_get_count(struct udevice *dev) |
Michael Walle | e9e73d7 | 2022-08-17 21:37:51 +0200 | [diff] [blame] | 104 | { |
| 105 | struct orion_timer_priv *priv = dev_get_priv(dev); |
| 106 | |
Stefan Roese | a68f13a | 2022-09-15 16:20:38 +0200 | [diff] [blame] | 107 | return orion_timer_get_count(priv->base); |
Michael Walle | e9e73d7 | 2022-08-17 21:37:51 +0200 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static int orion_timer_probe(struct udevice *dev) |
| 111 | { |
| 112 | struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Stefan Roese | 89fd0cc | 2022-09-15 16:20:37 +0200 | [diff] [blame] | 113 | enum input_clock_type type = dev_get_driver_data(dev); |
Michael Walle | e9e73d7 | 2022-08-17 21:37:51 +0200 | [diff] [blame] | 114 | 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 Roese | a68f13a | 2022-09-15 16:20:38 +0200 | [diff] [blame] | 122 | if (type == INPUT_CLOCK_25MHZ) |
Stefan Roese | 89fd0cc | 2022-09-15 16:20:37 +0200 | [diff] [blame] | 123 | uc_priv->clock_rate = MVEBU_TIMER_FIXED_RATE_25MHZ; |
Stefan Roese | a68f13a | 2022-09-15 16:20:38 +0200 | [diff] [blame] | 124 | else |
Tom Rini | 65cc0e2 | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 125 | uc_priv->clock_rate = CFG_SYS_TCLK; |
Stefan Roese | a68f13a | 2022-09-15 16:20:38 +0200 | [diff] [blame] | 126 | orion_timer_init(priv->base, type); |
Michael Walle | e9e73d7 | 2022-08-17 21:37:51 +0200 | [diff] [blame] | 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static const struct timer_ops orion_timer_ops = { |
Stefan Roese | a68f13a | 2022-09-15 16:20:38 +0200 | [diff] [blame] | 132 | .get_count = dm_orion_timer_get_count, |
Michael Walle | e9e73d7 | 2022-08-17 21:37:51 +0200 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | static const struct udevice_id orion_timer_ids[] = { |
Stefan Roese | 89fd0cc | 2022-09-15 16:20:37 +0200 | [diff] [blame] | 136 | { .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 Walle | e9e73d7 | 2022-08-17 21:37:51 +0200 | [diff] [blame] | 139 | {} |
| 140 | }; |
| 141 | |
| 142 | U_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 | }; |