1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2014, STMicroelectronics - All Rights Reserved
4 * Author(s): Vikas Manocha, <vikas.manocha@st.com> for STMicroelectronics.
9 #include <asm/arch-stv0991/hardware.h>
10 #include <asm/arch-stv0991/stv0991_cgu.h>
11 #include <asm/arch-stv0991/stv0991_gpt.h>
13 static struct stv0991_cgu_regs *const stv0991_cgu_regs = \
14 (struct stv0991_cgu_regs *) (CGU_BASE_ADDR);
16 #define READ_TIMER() (readl(&gpt1_regs_ptr->cnt) & GPT_FREE_RUNNING)
17 #define GPT_RESOLUTION (CONFIG_STV0991_HZ_CLOCK / CONFIG_STV0991_HZ)
19 DECLARE_GLOBAL_DATA_PTR;
21 #define timestamp gd->arch.tbl
22 #define lastdec gd->arch.lastinc
26 /* Timer1 clock configuration */
27 writel(TIMER1_CLK_CFG, &stv0991_cgu_regs->tim_freq);
28 writel(readl(&stv0991_cgu_regs->cgu_enable_2) |
29 TIMER1_CLK_EN, &stv0991_cgu_regs->cgu_enable_2);
32 writel(readl(&gpt1_regs_ptr->cr1) & ~GPT_CR1_CEN, &gpt1_regs_ptr->cr1);
33 writel(GPT_PRESCALER_128, &gpt1_regs_ptr->psc);
34 /* Configure timer for auto-reload */
35 writel(readl(&gpt1_regs_ptr->cr1) | GPT_MODE_AUTO_RELOAD,
38 /* load value for free running */
39 writel(GPT_FREE_RUNNING, &gpt1_regs_ptr->arr);
42 writel(readl(&gpt1_regs_ptr->cr1) | GPT_CR1_CEN,
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_STV0991_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;
93 * This function is derived from PowerPC code (read timebase as long long).
94 * On ARM it just returns the timer value.
96 unsigned long long get_ticks(void)
102 * This function is derived from PowerPC code (timebase clock frequency).
103 * On ARM it returns the number of timer ticks per second.
105 ulong get_tbclk(void)
107 return CONFIG_STV0991_HZ;