2 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
3 * Author(s): Patrice Chotard, <patrice.chotard@st.com> for STMicroelectronics.
5 * SPDX-License-Identifier: GPL-2.0+
16 /* Timer control1 register */
17 #define CR1_CEN BIT(0)
18 #define CR1_ARPE BIT(7)
20 /* Event Generation Register register */
23 /* Auto reload register for free running config */
24 #define GPT_FREE_RUNNING 0xFFFFFFFF
26 struct stm32_timer_regs {
50 struct stm32_timer_priv {
51 struct stm32_timer_regs *base;
54 static int stm32_timer_get_count(struct udevice *dev, u64 *count)
56 struct stm32_timer_priv *priv = dev_get_priv(dev);
57 struct stm32_timer_regs *regs = priv->base;
59 *count = readl(®s->cnt);
64 static int stm32_timer_probe(struct udevice *dev)
66 struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
67 struct stm32_timer_priv *priv = dev_get_priv(dev);
68 struct stm32_timer_regs *regs;
74 addr = dev_read_addr(dev);
75 if (addr == FDT_ADDR_T_NONE)
78 priv->base = (struct stm32_timer_regs *)addr;
80 ret = clk_get_by_index(dev, 0, &clk);
84 ret = clk_enable(&clk);
86 dev_err(dev, "failed to enable clock\n");
93 clrbits_le32(®s->cr1, CR1_CEN);
96 rate = clk_get_rate(&clk);
98 /* we set timer prescaler to obtain a 1MHz timer counter frequency */
99 psc = (rate / CONFIG_SYS_HZ_CLOCK) - 1;
100 writel(psc, ®s->psc);
102 /* Set timer frequency to 1MHz */
103 uc_priv->clock_rate = CONFIG_SYS_HZ_CLOCK;
105 /* Configure timer for auto-reload */
106 setbits_le32(®s->cr1, CR1_ARPE);
108 /* load value for auto reload */
109 writel(GPT_FREE_RUNNING, ®s->arr);
112 setbits_le32(®s->cr1, CR1_CEN);
114 /* Update generation */
115 setbits_le32(®s->egr, EGR_UG);
120 static const struct timer_ops stm32_timer_ops = {
121 .get_count = stm32_timer_get_count,
124 static const struct udevice_id stm32_timer_ids[] = {
125 { .compatible = "st,stm32-timer" },
129 U_BOOT_DRIVER(stm32_timer) = {
130 .name = "stm32_timer",
132 .of_match = stm32_timer_ids,
133 .priv_auto_alloc_size = sizeof(struct stm32_timer_priv),
134 .probe = stm32_timer_probe,
135 .ops = &stm32_timer_ops,
136 .flags = DM_FLAG_PRE_RELOC,