1 // SPDX-License-Identifier: GPL-2.0+
4 * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
11 #include <asm/arch/hardware.h>
12 #include <asm/arch/spr_gpt.h>
13 #include <asm/arch/spr_misc.h>
14 #include <asm/ptrace.h>
15 #include <linux/delay.h>
17 #define GPT_RESOLUTION (CONFIG_SPEAR_HZ_CLOCK / CONFIG_SPEAR_HZ)
18 #define READ_TIMER() (readl(&gpt_regs_p->count) & GPT_FREE_RUNNING)
20 static struct gpt_regs *const gpt_regs_p =
21 (struct gpt_regs *)CONFIG_SPEAR_TIMERBASE;
23 static struct misc_regs *const misc_regs_p =
24 (struct misc_regs *)CONFIG_SPEAR_MISCBASE;
26 DECLARE_GLOBAL_DATA_PTR;
28 static ulong get_timer_masked(void);
30 #define timestamp gd->arch.tbl
31 #define lastdec gd->arch.lastinc
37 /* Prescaler setting */
38 #if defined(CONFIG_SPEAR3XX)
39 writel(MISC_PRSC_CFG, &misc_regs_p->prsc2_clk_cfg);
40 synth = MISC_GPT4SYNTH;
41 #elif defined(CONFIG_SPEAR600)
42 writel(MISC_PRSC_CFG, &misc_regs_p->prsc1_clk_cfg);
43 synth = MISC_GPT3SYNTH;
45 # error Incorrect config. Can only be SPEAR{600|300|310|320}
48 writel(readl(&misc_regs_p->periph_clk_cfg) | synth,
49 &misc_regs_p->periph_clk_cfg);
52 writel(GPT_PRESCALER_1 | GPT_MODE_AUTO_RELOAD, &gpt_regs_p->control);
54 /* load value for free running */
55 writel(GPT_FREE_RUNNING, &gpt_regs_p->compare);
57 /* auto reload, start timer */
58 writel(readl(&gpt_regs_p->control) | GPT_ENABLE, &gpt_regs_p->control);
61 lastdec = READ_TIMER();
68 * timer without interrupts
70 ulong get_timer(ulong base)
72 return (get_timer_masked() / GPT_RESOLUTION) - base;
75 void __udelay(unsigned long usec)
78 ulong start = get_timer_masked();
79 ulong tenudelcnt = CONFIG_SPEAR_HZ_CLOCK / (1000 * 100);
82 rndoff = (usec % 10) ? 1 : 0;
84 /* tenudelcnt timer tick gives 10 microsecconds delay */
85 tmo = ((usec / 10) + rndoff) * tenudelcnt;
87 while ((ulong) (get_timer_masked() - start) < tmo)
91 static ulong get_timer_masked(void)
93 ulong now = READ_TIMER();
97 timestamp += now - lastdec;
99 /* we have an overflow ... */
100 timestamp += now + GPT_FREE_RUNNING - lastdec;
108 * This function is derived from PowerPC code (read timebase as long long).
109 * On ARM it just returns the timer value.
111 unsigned long long get_ticks(void)
117 * This function is derived from PowerPC code (timebase clock frequency).
118 * On ARM it returns the number of timer ticks per second.
120 ulong get_tbclk(void)
122 return CONFIG_SPEAR_HZ;