1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
4 * Author(s): Patrice Chotard, <patrice.chotard@st.com> for STMicroelectronics.
15 /* Timer control1 register */
16 #define CR1_CEN BIT(0)
17 #define CR1_ARPE BIT(7)
19 /* Event Generation Register register */
22 /* Auto reload register for free running config */
23 #define GPT_FREE_RUNNING 0xFFFFFFFF
25 struct stm32_timer_regs
{
49 struct stm32_timer_priv
{
50 struct stm32_timer_regs
*base
;
53 static int stm32_timer_get_count(struct udevice
*dev
, u64
*count
)
55 struct stm32_timer_priv
*priv
= dev_get_priv(dev
);
56 struct stm32_timer_regs
*regs
= priv
->base
;
58 *count
= readl(®s
->cnt
);
63 static int stm32_timer_probe(struct udevice
*dev
)
65 struct timer_dev_priv
*uc_priv
= dev_get_uclass_priv(dev
);
66 struct stm32_timer_priv
*priv
= dev_get_priv(dev
);
67 struct stm32_timer_regs
*regs
;
73 addr
= dev_read_addr(dev
);
74 if (addr
== FDT_ADDR_T_NONE
)
77 priv
->base
= (struct stm32_timer_regs
*)addr
;
79 ret
= clk_get_by_index(dev
, 0, &clk
);
83 ret
= clk_enable(&clk
);
85 dev_err(dev
, "failed to enable clock\n");
92 clrbits_le32(®s
->cr1
, CR1_CEN
);
95 rate
= clk_get_rate(&clk
);
97 /* we set timer prescaler to obtain a 1MHz timer counter frequency */
98 psc
= (rate
/ CONFIG_SYS_HZ_CLOCK
) - 1;
99 writel(psc
, ®s
->psc
);
101 /* Set timer frequency to 1MHz */
102 uc_priv
->clock_rate
= CONFIG_SYS_HZ_CLOCK
;
104 /* Configure timer for auto-reload */
105 setbits_le32(®s
->cr1
, CR1_ARPE
);
107 /* load value for auto reload */
108 writel(GPT_FREE_RUNNING
, ®s
->arr
);
111 setbits_le32(®s
->cr1
, CR1_CEN
);
113 /* Update generation */
114 setbits_le32(®s
->egr
, EGR_UG
);
119 static const struct timer_ops stm32_timer_ops
= {
120 .get_count
= stm32_timer_get_count
,
123 static const struct udevice_id stm32_timer_ids
[] = {
124 { .compatible
= "st,stm32-timer" },
128 U_BOOT_DRIVER(stm32_timer
) = {
129 .name
= "stm32_timer",
131 .of_match
= stm32_timer_ids
,
132 .priv_auto_alloc_size
= sizeof(struct stm32_timer_priv
),
133 .probe
= stm32_timer_probe
,
134 .ops
= &stm32_timer_ops
,