1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2022 StarFive, Inc. All rights reserved.
12 #include <dm/device-internal.h>
13 #include <linux/err.h>
15 #define STF_TIMER_INT_STATUS 0x00
16 #define STF_TIMER_CTL 0x04
17 #define STF_TIMER_LOAD 0x08
18 #define STF_TIMER_ENABLE 0x10
19 #define STF_TIMER_RELOAD 0x14
20 #define STF_TIMER_VALUE 0x18
21 #define STF_TIMER_INT_CLR 0x20
22 #define STF_TIMER_INT_MASK 0x24
24 struct starfive_timer_priv {
29 static u64 notrace starfive_get_count(struct udevice *dev)
31 struct starfive_timer_priv *priv = dev_get_priv(dev);
33 /* Read decrement timer value and convert to increment value */
34 return priv->timer_size - readl(priv->base + STF_TIMER_VALUE);
37 static const struct timer_ops starfive_ops = {
38 .get_count = starfive_get_count,
41 static int starfive_probe(struct udevice *dev)
43 struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
44 struct starfive_timer_priv *priv = dev_get_priv(dev);
49 priv->base = dev_read_addr_ptr(dev);
53 timer_channel = dev_read_u32_default(dev, "channel", 0);
54 priv->base = priv->base + (0x40 * timer_channel);
56 /* Get clock rate from channel selectecd*/
57 ret = clk_get_by_index(dev, timer_channel, &clk);
61 ret = clk_enable(&clk);
64 uc_priv->clock_rate = clk_get_rate(&clk);
67 * Initiate timer, channel 0
68 * Unmask Interrupt Mask
70 writel(0, priv->base + STF_TIMER_INT_MASK);
71 /* Single run mode Setting */
72 if (dev_read_bool(dev, "single-run"))
73 writel(1, priv->base + STF_TIMER_CTL);
74 /* Set Reload value */
75 priv->timer_size = dev_read_u32_default(dev, "timer-size", -1U);
76 writel(priv->timer_size, priv->base + STF_TIMER_LOAD);
77 /* Enable to start timer */
78 writel(1, priv->base + STF_TIMER_ENABLE);
83 static const struct udevice_id starfive_ids[] = {
84 { .compatible = "starfive,jh8100-timers" },
88 U_BOOT_DRIVER(jh8100_starfive_timer) = {
89 .name = "starfive_timer",
91 .of_match = starfive_ids,
92 .probe = starfive_probe,
94 .priv_auto = sizeof(struct starfive_timer_priv),