3 * Kamil Lulko, <kamil.lulko@gmail.com>
5 * Copyright 2015 ATS Advanced Telematics Systems GmbH
6 * Copyright 2015 Konsulko Group, Matt Porter <mporter@konsulko.com>
8 * SPDX-License-Identifier: GPL-2.0+
13 #include <asm/armv7m.h>
14 #include <asm/arch/stm32.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 #define STM32_TIM2_BASE (STM32_APB1PERIPH_BASE + 0x0000)
20 #define RCC_APB1ENR_TIM2EN (1 << 0)
46 #define TIM_CR1_CEN (1 << 0)
48 #define TIM_EGR_UG (1 << 0)
52 struct stm32_tim2_5 *tim = (struct stm32_tim2_5 *)STM32_TIM2_BASE;
54 setbits_le32(&STM32_RCC->apb1enr, RCC_APB1ENR_TIM2EN);
56 if (clock_get(CLOCK_AHB) == clock_get(CLOCK_APB1))
57 writel((clock_get(CLOCK_APB1) / CONFIG_SYS_HZ_CLOCK) - 1,
60 writel(((clock_get(CLOCK_APB1) * 2) / CONFIG_SYS_HZ_CLOCK) - 1,
63 writel(0xFFFFFFFF, &tim->arr);
64 writel(TIM_CR1_CEN, &tim->cr1);
65 setbits_le32(&tim->egr, TIM_EGR_UG);
74 ulong get_timer(ulong base)
76 return (get_ticks() / (CONFIG_SYS_HZ_CLOCK / CONFIG_SYS_HZ)) - base;
79 unsigned long long get_ticks(void)
81 struct stm32_tim2_5 *tim = (struct stm32_tim2_5 *)STM32_TIM2_BASE;
84 now = readl(&tim->cnt);
86 if (now >= gd->arch.lastinc)
87 gd->arch.tbl += (now - gd->arch.lastinc);
89 gd->arch.tbl += (0xFFFFFFFF - gd->arch.lastinc) + now;
91 gd->arch.lastinc = now;
96 void reset_timer(void)
98 struct stm32_tim2_5 *tim = (struct stm32_tim2_5 *)STM32_TIM2_BASE;
100 gd->arch.lastinc = readl(&tim->cnt);
104 /* delay x useconds */
105 void __udelay(ulong usec)
107 unsigned long long start;
109 start = get_ticks(); /* get current timestamp */
110 while ((get_ticks() - start) < usec)
111 ; /* loop till time has passed */
115 * This function is derived from PowerPC code (timebase clock frequency).
116 * On ARM it returns the number of timer ticks per second.
118 ulong get_tbclk(void)
120 return CONFIG_SYS_HZ_CLOCK;