d5f6cccaae432baa64e7d8f0aaf1f490aabced4e
[platform/kernel/u-boot.git] / arch / arm / cpu / arm926ejs / spear / timer.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2009
4  * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
5  */
6
7 #include <common.h>
8 #include <init.h>
9 #include <time.h>
10 #include <asm/global_data.h>
11 #include <asm/io.h>
12 #include <asm/arch/hardware.h>
13 #include <asm/arch/spr_gpt.h>
14 #include <asm/arch/spr_misc.h>
15 #include <asm/ptrace.h>
16 #include <linux/delay.h>
17
18 #define GPT_RESOLUTION  (CONFIG_SPEAR_HZ_CLOCK / CONFIG_SPEAR_HZ)
19 #define READ_TIMER()    (readl(&gpt_regs_p->count) & GPT_FREE_RUNNING)
20
21 static struct gpt_regs *const gpt_regs_p =
22     (struct gpt_regs *)CONFIG_SPEAR_TIMERBASE;
23
24 static struct misc_regs *const misc_regs_p =
25     (struct misc_regs *)CONFIG_SPEAR_MISCBASE;
26
27 DECLARE_GLOBAL_DATA_PTR;
28
29 static ulong get_timer_masked(void);
30
31 #define timestamp gd->arch.tbl
32 #define lastdec gd->arch.lastinc
33
34 int timer_init(void)
35 {
36         u32 synth;
37
38         /* Prescaler setting */
39 #if defined(CONFIG_SPEAR600)
40         writel(MISC_PRSC_CFG, &misc_regs_p->prsc1_clk_cfg);
41         synth = MISC_GPT3SYNTH;
42 #else
43 # error Incorrect config. Can only be SPEAR{600|300|310|320}
44 #endif
45
46         writel(readl(&misc_regs_p->periph_clk_cfg) | synth,
47                &misc_regs_p->periph_clk_cfg);
48
49         /* disable timers */
50         writel(GPT_PRESCALER_1 | GPT_MODE_AUTO_RELOAD, &gpt_regs_p->control);
51
52         /* load value for free running */
53         writel(GPT_FREE_RUNNING, &gpt_regs_p->compare);
54
55         /* auto reload, start timer */
56         writel(readl(&gpt_regs_p->control) | GPT_ENABLE, &gpt_regs_p->control);
57
58         /* Reset the timer */
59         lastdec = READ_TIMER();
60         timestamp = 0;
61
62         return 0;
63 }
64
65 /*
66  * timer without interrupts
67  */
68 ulong get_timer(ulong base)
69 {
70         return (get_timer_masked() / GPT_RESOLUTION) - base;
71 }
72
73 void __udelay(unsigned long usec)
74 {
75         ulong tmo;
76         ulong start = get_timer_masked();
77         ulong tenudelcnt = CONFIG_SPEAR_HZ_CLOCK / (1000 * 100);
78         ulong rndoff;
79
80         rndoff = (usec % 10) ? 1 : 0;
81
82         /* tenudelcnt timer tick gives 10 microsecconds delay */
83         tmo = ((usec / 10) + rndoff) * tenudelcnt;
84
85         while ((ulong) (get_timer_masked() - start) < tmo)
86                 ;
87 }
88
89 static ulong get_timer_masked(void)
90 {
91         ulong now = READ_TIMER();
92
93         if (now >= lastdec) {
94                 /* normal mode */
95                 timestamp += now - lastdec;
96         } else {
97                 /* we have an overflow ... */
98                 timestamp += now + GPT_FREE_RUNNING - lastdec;
99         }
100         lastdec = now;
101
102         return timestamp;
103 }
104
105 /*
106  * This function is derived from PowerPC code (read timebase as long long).
107  * On ARM it just returns the timer value.
108  */
109 unsigned long long get_ticks(void)
110 {
111         return get_timer(0);
112 }
113
114 /*
115  * This function is derived from PowerPC code (timebase clock frequency).
116  * On ARM it returns the number of timer ticks per second.
117  */
118 ulong get_tbclk(void)
119 {
120         return CONFIG_SPEAR_HZ;
121 }