123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- #include <common.h>
- #include <asm/io.h>
- #include <asm/arch/stm32.h>
- #include <asm/arch/gpt.h>
- #include <asm/arch/rcc.h>
- #define READ_TIMER() (readl(&gpt1_regs_ptr->cnt) & GPT_FREE_RUNNING)
- #define GPT_RESOLUTION (CONFIG_SYS_HZ_CLOCK/CONFIG_STM32_HZ)
- DECLARE_GLOBAL_DATA_PTR;
- #define timestamp gd->arch.tbl
- #define lastdec gd->arch.lastinc
- int timer_init(void)
- {
-
- setbits_le32(RCC_BASE + RCC_APB1ENR, RCC_APB1ENR_TIM2EN);
-
- writel(readl(&gpt1_regs_ptr->cr1) & ~GPT_CR1_CEN, &gpt1_regs_ptr->cr1);
- writel((CONFIG_SYS_CLK_FREQ/CONFIG_SYS_HZ_CLOCK) - 1,
- &gpt1_regs_ptr->psc);
-
- writel(readl(&gpt1_regs_ptr->cr1) | GPT_MODE_AUTO_RELOAD,
- &gpt1_regs_ptr->cr1);
-
- writel(GPT_FREE_RUNNING, &gpt1_regs_ptr->arr);
-
- writel(readl(&gpt1_regs_ptr->cr1) | GPT_CR1_CEN, &gpt1_regs_ptr->cr1);
- writel(readl(&gpt1_regs_ptr->egr) | TIM_EGR_UG, &gpt1_regs_ptr->egr);
-
- lastdec = READ_TIMER();
- timestamp = 0;
- return 0;
- }
- ulong get_timer(ulong base)
- {
- return (get_timer_masked() / GPT_RESOLUTION) - base;
- }
- void __udelay(unsigned long usec)
- {
- ulong tmo;
- ulong start = get_timer_masked();
- ulong tenudelcnt = CONFIG_SYS_HZ_CLOCK / (1000 * 100);
- ulong rndoff;
- rndoff = (usec % 10) ? 1 : 0;
-
- tmo = ((usec / 10) + rndoff) * tenudelcnt;
- while ((ulong) (get_timer_masked() - start) < tmo)
- ;
- }
- ulong get_timer_masked(void)
- {
- ulong now = READ_TIMER();
- if (now >= lastdec) {
-
- timestamp += now - lastdec;
- } else {
-
- timestamp += now + GPT_FREE_RUNNING - lastdec;
- }
- lastdec = now;
- return timestamp;
- }
- void udelay_masked(unsigned long usec)
- {
- return udelay(usec);
- }
- unsigned long long get_ticks(void)
- {
- return get_timer(0);
- }
- ulong get_tbclk(void)
- {
- return CONFIG_STM32_HZ;
- }
|