1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
4 * Author(s): Patrice Chotard, <patrice.chotard@foss.st.com> for STMicroelectronics.
7 #define LOG_CATEGORY UCLASS_TIMER
14 #include <dm/device_compat.h>
15 #include <linux/bitops.h>
19 /* Timer control1 register */
20 #define CR1_CEN BIT(0)
21 #define CR1_ARPE BIT(7)
23 /* Event Generation Register register */
26 /* Auto reload register for free running config */
27 #define GPT_FREE_RUNNING 0xFFFFFFFF
29 struct stm32_timer_regs {
53 struct stm32_timer_priv {
54 struct stm32_timer_regs *base;
57 static u64 stm32_timer_get_count(struct udevice *dev)
59 struct stm32_timer_priv *priv = dev_get_priv(dev);
60 struct stm32_timer_regs *regs = priv->base;
62 return readl(®s->cnt);
65 static int stm32_timer_probe(struct udevice *dev)
67 struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
68 struct stm32_timer_priv *priv = dev_get_priv(dev);
69 struct stm32_timer_regs *regs;
75 addr = dev_read_addr(dev);
76 if (addr == FDT_ADDR_T_NONE)
79 priv->base = (struct stm32_timer_regs *)addr;
81 ret = clk_get_by_index(dev, 0, &clk);
85 ret = clk_enable(&clk);
87 dev_err(dev, "failed to enable clock\n");
94 clrbits_le32(®s->cr1, CR1_CEN);
97 rate = clk_get_rate(&clk);
99 /* we set timer prescaler to obtain a 1MHz timer counter frequency */
100 psc = (rate / CFG_SYS_HZ_CLOCK) - 1;
101 writel(psc, ®s->psc);
103 /* Set timer frequency to 1MHz */
104 uc_priv->clock_rate = CFG_SYS_HZ_CLOCK;
106 /* Configure timer for auto-reload */
107 setbits_le32(®s->cr1, CR1_ARPE);
109 /* load value for auto reload */
110 writel(GPT_FREE_RUNNING, ®s->arr);
113 setbits_le32(®s->cr1, CR1_CEN);
115 /* Update generation */
116 setbits_le32(®s->egr, EGR_UG);
121 static const struct timer_ops stm32_timer_ops = {
122 .get_count = stm32_timer_get_count,
125 static const struct udevice_id stm32_timer_ids[] = {
126 { .compatible = "st,stm32-timer" },
130 U_BOOT_DRIVER(stm32_timer) = {
131 .name = "stm32_timer",
133 .of_match = stm32_timer_ids,
134 .priv_auto = sizeof(struct stm32_timer_priv),
135 .probe = stm32_timer_probe,
136 .ops = &stm32_timer_ops,