2 * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
3 * Author(s): Vikas Manocha, <vikas.manocha@st.com> for STMicroelectronics.
5 * SPDX-License-Identifier: GPL-2.0+
11 #include <asm/arch/stm32.h>
12 #include <asm/arch/stm32_defs.h>
13 #include <asm/arch/gpt.h>
15 #define READ_TIMER() (readl(&gpt1_regs_ptr->cnt) & GPT_FREE_RUNNING)
16 #define GPT_RESOLUTION (CONFIG_SYS_HZ_CLOCK/CONFIG_STM32_HZ)
18 DECLARE_GLOBAL_DATA_PTR;
20 #define timestamp gd->arch.tbl
21 #define lastdec gd->arch.lastinc
25 /* Timer2 clock configuration */
26 clock_setup(TIMER2_CLOCK_CFG);
28 writel(readl(&gpt1_regs_ptr->cr1) & ~GPT_CR1_CEN, &gpt1_regs_ptr->cr1);
30 writel((CONFIG_SYS_CLK_FREQ / 2 / CONFIG_SYS_HZ_CLOCK) - 1,
33 /* Configure timer for auto-reload */
34 writel(readl(&gpt1_regs_ptr->cr1) | GPT_MODE_AUTO_RELOAD,
37 /* load value for free running */
38 writel(GPT_FREE_RUNNING, &gpt1_regs_ptr->arr);
41 writel(readl(&gpt1_regs_ptr->cr1) | GPT_CR1_CEN, &gpt1_regs_ptr->cr1);
43 writel(readl(&gpt1_regs_ptr->egr) | TIM_EGR_UG, &gpt1_regs_ptr->egr);
46 lastdec = READ_TIMER();
53 * timer without interrupts
55 ulong get_timer(ulong base)
57 return (get_timer_masked() / GPT_RESOLUTION) - base;
60 void __udelay(unsigned long usec)
63 ulong start = get_timer_masked();
64 ulong tenudelcnt = CONFIG_SYS_HZ_CLOCK / (1000 * 100);
67 rndoff = (usec % 10) ? 1 : 0;
69 /* tenudelcnt timer tick gives 10 microsecconds delay */
70 tmo = ((usec / 10) + rndoff) * tenudelcnt;
72 while ((ulong) (get_timer_masked() - start) < tmo)
76 ulong get_timer_masked(void)
78 ulong now = READ_TIMER();
82 timestamp += now - lastdec;
84 /* we have an overflow ... */
85 timestamp += now + GPT_FREE_RUNNING - lastdec;
92 void udelay_masked(unsigned long usec)
98 * This function is derived from PowerPC code (read timebase as long long).
99 * On ARM it just returns the timer value.
101 unsigned long long get_ticks(void)
107 * This function is derived from PowerPC code (timebase clock frequency).
108 * On ARM it returns the number of timer ticks per second.
110 ulong get_tbclk(void)
112 return CONFIG_STM32_HZ;