3 * Vikas Manocha, ST Micoelectronics, vikas.manocha@st.com.
5 * SPDX-License-Identifier: GPL-2.0+
10 #include <asm/arch-stv0991/hardware.h>
11 #include <asm/arch-stv0991/stv0991_cgu.h>
12 #include <asm/arch-stv0991/stv0991_gpt.h>
14 static struct stv0991_cgu_regs *const stv0991_cgu_regs = \
15 (struct stv0991_cgu_regs *) (CGU_BASE_ADDR);
17 #define READ_TIMER() (readl(&gpt1_regs_ptr->cnt) & GPT_FREE_RUNNING)
18 #define GPT_RESOLUTION (CONFIG_STV0991_HZ_CLOCK / CONFIG_STV0991_HZ)
20 DECLARE_GLOBAL_DATA_PTR;
22 #define timestamp gd->arch.tbl
23 #define lastdec gd->arch.lastinc
27 /* Timer1 clock configuration */
28 writel(TIMER1_CLK_CFG, &stv0991_cgu_regs->tim_freq);
29 writel(readl(&stv0991_cgu_regs->cgu_enable_2) |
30 TIMER1_CLK_EN, &stv0991_cgu_regs->cgu_enable_2);
33 writel(readl(&gpt1_regs_ptr->cr1) & ~GPT_CR1_CEN, &gpt1_regs_ptr->cr1);
34 writel(GPT_PRESCALER_128, &gpt1_regs_ptr->psc);
35 /* Configure timer for auto-reload */
36 writel(readl(&gpt1_regs_ptr->cr1) | GPT_MODE_AUTO_RELOAD,
39 /* load value for free running */
40 writel(GPT_FREE_RUNNING, &gpt1_regs_ptr->arr);
43 writel(readl(&gpt1_regs_ptr->cr1) | GPT_CR1_CEN,
47 lastdec = READ_TIMER();
54 * timer without interrupts
56 ulong get_timer(ulong base)
58 return (get_timer_masked() / GPT_RESOLUTION) - base;
61 void __udelay(unsigned long usec)
64 ulong start = get_timer_masked();
65 ulong tenudelcnt = CONFIG_STV0991_HZ_CLOCK / (1000 * 100);
68 rndoff = (usec % 10) ? 1 : 0;
70 /* tenudelcnt timer tick gives 10 microsecconds delay */
71 tmo = ((usec / 10) + rndoff) * tenudelcnt;
73 while ((ulong) (get_timer_masked() - start) < tmo)
77 ulong get_timer_masked(void)
79 ulong now = READ_TIMER();
83 timestamp += now - lastdec;
85 /* we have an overflow ... */
86 timestamp += now + GPT_FREE_RUNNING - lastdec;
93 void udelay_masked(unsigned long usec)
99 * This function is derived from PowerPC code (read timebase as long long).
100 * On ARM it just returns the timer value.
102 unsigned long long get_ticks(void)
108 * This function is derived from PowerPC code (timebase clock frequency).
109 * On ARM it returns the number of timer ticks per second.
111 ulong get_tbclk(void)
113 return CONFIG_STV0991_HZ;