1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000-2009
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6 
7 #include <clock_legacy.h>
8 #include <bootstage.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <init.h>
12 #include <spl.h>
13 #include <time.h>
14 #include <timer.h>
15 #include <watchdog.h>
16 #include <div64.h>
17 #include <asm/global_data.h>
18 #include <asm/io.h>
19 #include <linux/delay.h>
20 #include <uthread.h>
21 
22 #ifndef CFG_WD_PERIOD
23 # define CFG_WD_PERIOD	(10 * 1000 * 1000)	/* 10 seconds default */
24 #endif
25 
26 DECLARE_GLOBAL_DATA_PTR;
27 
28 #ifdef CFG_SYS_TIMER_RATE
29 /* Returns tick rate in ticks per second */
get_tbclk(void)30 ulong notrace get_tbclk(void)
31 {
32 	return CFG_SYS_TIMER_RATE;
33 }
34 #endif
35 
36 #ifdef CFG_SYS_TIMER_COUNTER
timer_read_counter(void)37 unsigned long notrace timer_read_counter(void)
38 {
39 #ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
40 	return ~readl(CFG_SYS_TIMER_COUNTER);
41 #else
42 	return readl(CFG_SYS_TIMER_COUNTER);
43 #endif
44 }
45 
timer_get_boot_us(void)46 ulong timer_get_boot_us(void)
47 {
48 	ulong count = timer_read_counter();
49 
50 #ifdef CFG_SYS_TIMER_RATE
51 	const ulong timer_rate = CFG_SYS_TIMER_RATE;
52 
53 	if (timer_rate == 1000000)
54 		return count;
55 	else if (timer_rate > 1000000)
56 		return lldiv(count, timer_rate / 1000000);
57 	else
58 		return (unsigned long long)count * 1000000 / timer_rate;
59 #else
60 	/* Assume the counter is in microseconds */
61 	return count;
62 #endif
63 }
64 
65 #else
66 extern unsigned long timer_read_counter(void);
67 #endif
68 
69 #if CONFIG_IS_ENABLED(TIMER)
get_tbclk(void)70 ulong notrace get_tbclk(void)
71 {
72 	if (!gd->timer) {
73 		int ret;
74 
75 		if (IS_ENABLED(CONFIG_TIMER_EARLY))
76 			return timer_early_get_rate();
77 
78 		ret = dm_timer_init();
79 		if (ret)
80 			return ret;
81 	}
82 
83 	return timer_get_rate(gd->timer);
84 }
85 
get_ticks(void)86 uint64_t notrace get_ticks(void)
87 {
88 	u64 count;
89 	int ret;
90 
91 	if (!gd->timer) {
92 		int ret;
93 
94 		if (IS_ENABLED(CONFIG_TIMER_EARLY))
95 			return timer_early_get_count();
96 
97 		ret = dm_timer_init();
98 		if (ret)
99 			panic("Could not initialize timer (err %d)\n", ret);
100 	}
101 
102 	ret = timer_get_count(gd->timer, &count);
103 	if (ret) {
104 		if (xpl_phase() > PHASE_TPL)
105 			panic("Could not read count from timer (err %d)\n",
106 			      ret);
107 		else
108 			panic("no timer (err %d)\n", ret);
109 	}
110 
111 	return count;
112 }
113 
114 #else /* !CONFIG_TIMER */
115 
get_ticks(void)116 uint64_t __weak notrace get_ticks(void)
117 {
118 	unsigned long now = timer_read_counter();
119 
120 	/* increment tbu if tbl has rolled over */
121 	if (now < gd->timebase_l)
122 		gd->timebase_h++;
123 	gd->timebase_l = now;
124 	return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
125 }
126 
127 #endif /* CONFIG_TIMER */
128 
129 /* Returns time in milliseconds */
tick_to_time(uint64_t tick)130 static uint64_t notrace tick_to_time(uint64_t tick)
131 {
132 	ulong div = get_tbclk();
133 
134 	tick *= CONFIG_SYS_HZ;
135 	do_div(tick, div);
136 	return tick;
137 }
138 
timer_init(void)139 int __weak timer_init(void)
140 {
141 	return 0;
142 }
143 
144 /* Returns time in milliseconds */
get_timer(ulong base)145 ulong __weak get_timer(ulong base)
146 {
147 	return tick_to_time(get_ticks()) - base;
148 }
149 
tick_to_time_us(uint64_t tick)150 static uint64_t notrace tick_to_time_us(uint64_t tick)
151 {
152 	ulong div = get_tbclk() / 1000;
153 
154 	tick *= CONFIG_SYS_HZ;
155 	do_div(tick, div);
156 	return tick;
157 }
158 
get_timer_us(uint64_t base)159 uint64_t __weak get_timer_us(uint64_t base)
160 {
161 	return tick_to_time_us(get_ticks()) - base;
162 }
163 
get_timer_us_long(unsigned long base)164 unsigned long __weak get_timer_us_long(unsigned long base)
165 {
166 	return timer_get_us() - base;
167 }
168 
timer_get_us(void)169 unsigned long __weak notrace timer_get_us(void)
170 {
171 	return tick_to_time(get_ticks() * 1000);
172 }
173 
usec_to_tick(unsigned long usec)174 uint64_t usec_to_tick(unsigned long usec)
175 {
176 	uint64_t tick = usec;
177 	tick *= get_tbclk();
178 	do_div(tick, 1000000);
179 	return tick;
180 }
181 
__udelay(unsigned long usec)182 void __weak __udelay(unsigned long usec)
183 {
184 	uint64_t tmp;
185 
186 	tmp = get_ticks() + usec_to_tick(usec);	/* get current timestamp */
187 
188 	while (get_ticks() < tmp+1)	/* loop till event */
189 		 /*NOP*/;
190 }
191 
192 /* ------------------------------------------------------------------------- */
193 
udelay(unsigned long usec)194 void udelay(unsigned long usec)
195 {
196 	ulong kv;
197 
198 	do {
199 		schedule();
200 		kv = usec > CFG_WD_PERIOD ? CFG_WD_PERIOD : usec;
201 		if (CONFIG_IS_ENABLED(UTHREAD)) {
202 			ulong t0 = timer_get_us();
203 			while (timer_get_us() - t0 < kv)
204 				uthread_schedule();
205 		} else {
206 			__udelay(kv);
207 		}
208 		usec -= kv;
209 	} while(usec);
210 }
211