3 * Vikas Manocha, ST Micoelectronics, vikas.manocha@st.com.
5 * SPDX-License-Identifier: GPL-2.0+
10 #include <asm/arch/stm32.h>
11 #include <asm/arch/stm32_defs.h>
12 #include <asm/arch/gpt.h>
14 #define READ_TIMER() (readl(&gpt1_regs_ptr->cnt) & GPT_FREE_RUNNING)
15 #define GPT_RESOLUTION (CONFIG_SYS_HZ_CLOCK/CONFIG_STM32_HZ)
17 DECLARE_GLOBAL_DATA_PTR;
19 #define timestamp gd->arch.tbl
20 #define lastdec gd->arch.lastinc
24 /* Timer2 clock configuration */
25 clock_setup(TIMER2_CLOCK_CFG);
27 writel(readl(&gpt1_regs_ptr->cr1) & ~GPT_CR1_CEN, &gpt1_regs_ptr->cr1);
29 writel((CONFIG_SYS_CLK_FREQ/CONFIG_SYS_HZ_CLOCK) - 1,
32 /* Configure timer for auto-reload */
33 writel(readl(&gpt1_regs_ptr->cr1) | GPT_MODE_AUTO_RELOAD,
36 /* load value for free running */
37 writel(GPT_FREE_RUNNING, &gpt1_regs_ptr->arr);
40 writel(readl(&gpt1_regs_ptr->cr1) | GPT_CR1_CEN, &gpt1_regs_ptr->cr1);
42 writel(readl(&gpt1_regs_ptr->egr) | TIM_EGR_UG, &gpt1_regs_ptr->egr);
45 lastdec = READ_TIMER();
52 * timer without interrupts
54 ulong get_timer(ulong base)
56 return (get_timer_masked() / GPT_RESOLUTION) - base;
59 void __udelay(unsigned long usec)
62 ulong start = get_timer_masked();
63 ulong tenudelcnt = CONFIG_SYS_HZ_CLOCK / (1000 * 100);
66 rndoff = (usec % 10) ? 1 : 0;
68 /* tenudelcnt timer tick gives 10 microsecconds delay */
69 tmo = ((usec / 10) + rndoff) * tenudelcnt;
71 while ((ulong) (get_timer_masked() - start) < tmo)
75 ulong get_timer_masked(void)
77 ulong now = READ_TIMER();
81 timestamp += now - lastdec;
83 /* we have an overflow ... */
84 timestamp += now + GPT_FREE_RUNNING - lastdec;
91 void udelay_masked(unsigned long usec)
97 * This function is derived from PowerPC code (read timebase as long long).
98 * On ARM it just returns the timer value.
100 unsigned long long get_ticks(void)
106 * This function is derived from PowerPC code (timebase clock frequency).
107 * On ARM it returns the number of timer ticks per second.
109 ulong get_tbclk(void)
111 return CONFIG_STM32_HZ;