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.
10 #include <asm/global_data.h>
12 #include <asm/arch-stv0991/hardware.h>
13 #include <asm/arch-stv0991/stv0991_cgu.h>
14 #include <asm/arch-stv0991/stv0991_gpt.h>
15 #include <linux/delay.h>
17 static struct stv0991_cgu_regs *const stv0991_cgu_regs = \
18 (struct stv0991_cgu_regs *) (CGU_BASE_ADDR);
20 #define READ_TIMER() (readl(&gpt1_regs_ptr->cnt) & GPT_FREE_RUNNING)
21 #define GPT_RESOLUTION (CONFIG_STV0991_HZ_CLOCK / CONFIG_STV0991_HZ)
23 DECLARE_GLOBAL_DATA_PTR;
25 #define timestamp gd->arch.tbl
26 #define lastdec gd->arch.lastinc
28 static ulong get_timer_masked(void);
32 /* Timer1 clock configuration */
33 writel(TIMER1_CLK_CFG, &stv0991_cgu_regs->tim_freq);
34 writel(readl(&stv0991_cgu_regs->cgu_enable_2) |
35 TIMER1_CLK_EN, &stv0991_cgu_regs->cgu_enable_2);
38 writel(readl(&gpt1_regs_ptr->cr1) & ~GPT_CR1_CEN, &gpt1_regs_ptr->cr1);
39 writel(GPT_PRESCALER_128, &gpt1_regs_ptr->psc);
40 /* Configure timer for auto-reload */
41 writel(readl(&gpt1_regs_ptr->cr1) | GPT_MODE_AUTO_RELOAD,
44 /* load value for free running */
45 writel(GPT_FREE_RUNNING, &gpt1_regs_ptr->arr);
48 writel(readl(&gpt1_regs_ptr->cr1) | GPT_CR1_CEN,
52 lastdec = READ_TIMER();
59 * timer without interrupts
61 ulong get_timer(ulong base)
63 return (get_timer_masked() / GPT_RESOLUTION) - base;
66 void __udelay(unsigned long usec)
69 ulong start = get_timer_masked();
70 ulong tenudelcnt = CONFIG_STV0991_HZ_CLOCK / (1000 * 100);
73 rndoff = (usec % 10) ? 1 : 0;
75 /* tenudelcnt timer tick gives 10 microsecconds delay */
76 tmo = ((usec / 10) + rndoff) * tenudelcnt;
78 while ((ulong) (get_timer_masked() - start) < tmo)
82 static ulong get_timer_masked(void)
84 ulong now = READ_TIMER();
88 timestamp += now - lastdec;
90 /* we have an overflow ... */
91 timestamp += now + GPT_FREE_RUNNING - lastdec;
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;