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