1 // SPDX-License-Identifier: GPL-2.0+
4 * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
9 #include <asm/arch/hardware.h>
10 #include <asm/arch/spr_gpt.h>
11 #include <asm/arch/spr_misc.h>
13 #define GPT_RESOLUTION (CONFIG_SPEAR_HZ_CLOCK / CONFIG_SPEAR_HZ)
14 #define READ_TIMER() (readl(&gpt_regs_p->count) & GPT_FREE_RUNNING)
16 static struct gpt_regs *const gpt_regs_p =
17 (struct gpt_regs *)CONFIG_SPEAR_TIMERBASE;
19 static struct misc_regs *const misc_regs_p =
20 (struct misc_regs *)CONFIG_SPEAR_MISCBASE;
22 DECLARE_GLOBAL_DATA_PTR;
24 #define timestamp gd->arch.tbl
25 #define lastdec gd->arch.lastinc
31 /* Prescaler setting */
32 #if defined(CONFIG_SPEAR3XX)
33 writel(MISC_PRSC_CFG, &misc_regs_p->prsc2_clk_cfg);
34 synth = MISC_GPT4SYNTH;
35 #elif defined(CONFIG_SPEAR600)
36 writel(MISC_PRSC_CFG, &misc_regs_p->prsc1_clk_cfg);
37 synth = MISC_GPT3SYNTH;
39 # error Incorrect config. Can only be SPEAR{600|300|310|320}
42 writel(readl(&misc_regs_p->periph_clk_cfg) | synth,
43 &misc_regs_p->periph_clk_cfg);
46 writel(GPT_PRESCALER_1 | GPT_MODE_AUTO_RELOAD, &gpt_regs_p->control);
48 /* load value for free running */
49 writel(GPT_FREE_RUNNING, &gpt_regs_p->compare);
51 /* auto reload, start timer */
52 writel(readl(&gpt_regs_p->control) | GPT_ENABLE, &gpt_regs_p->control);
55 lastdec = READ_TIMER();
62 * timer without interrupts
64 ulong get_timer(ulong base)
66 return (get_timer_masked() / GPT_RESOLUTION) - base;
69 void __udelay(unsigned long usec)
72 ulong start = get_timer_masked();
73 ulong tenudelcnt = CONFIG_SPEAR_HZ_CLOCK / (1000 * 100);
76 rndoff = (usec % 10) ? 1 : 0;
78 /* tenudelcnt timer tick gives 10 microsecconds delay */
79 tmo = ((usec / 10) + rndoff) * tenudelcnt;
81 while ((ulong) (get_timer_masked() - start) < tmo)
85 ulong get_timer_masked(void)
87 ulong now = READ_TIMER();
91 timestamp += now - lastdec;
93 /* we have an overflow ... */
94 timestamp += now + GPT_FREE_RUNNING - lastdec;
102 * This function is derived from PowerPC code (read timebase as long long).
103 * On ARM it just returns the timer value.
105 unsigned long long get_ticks(void)
111 * This function is derived from PowerPC code (timebase clock frequency).
112 * On ARM it returns the number of timer ticks per second.
114 ulong get_tbclk(void)
116 return CONFIG_SPEAR_HZ;