xref: /OK3568_Linux_fs/u-boot/lib/time.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * (C) Copyright 2000-2009
3*4882a593Smuzhiyun  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <common.h>
9*4882a593Smuzhiyun #include <dm.h>
10*4882a593Smuzhiyun #include <errno.h>
11*4882a593Smuzhiyun #include <timer.h>
12*4882a593Smuzhiyun #include <watchdog.h>
13*4882a593Smuzhiyun #include <div64.h>
14*4882a593Smuzhiyun #include <asm/io.h>
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #ifndef CONFIG_WD_PERIOD
17*4882a593Smuzhiyun # define CONFIG_WD_PERIOD	(10 * 1000 * 1000)	/* 10 seconds default */
18*4882a593Smuzhiyun #endif
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #ifdef CONFIG_SYS_TIMER_RATE
23*4882a593Smuzhiyun /* Returns tick rate in ticks per second */
get_tbclk(void)24*4882a593Smuzhiyun ulong notrace get_tbclk(void)
25*4882a593Smuzhiyun {
26*4882a593Smuzhiyun 	return CONFIG_SYS_TIMER_RATE;
27*4882a593Smuzhiyun }
28*4882a593Smuzhiyun #endif
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #ifdef CONFIG_SYS_TIMER_COUNTER
timer_read_counter(void)31*4882a593Smuzhiyun unsigned long notrace timer_read_counter(void)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun #ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
34*4882a593Smuzhiyun 	return ~readl(CONFIG_SYS_TIMER_COUNTER);
35*4882a593Smuzhiyun #else
36*4882a593Smuzhiyun 	return readl(CONFIG_SYS_TIMER_COUNTER);
37*4882a593Smuzhiyun #endif
38*4882a593Smuzhiyun }
39*4882a593Smuzhiyun 
timer_get_boot_us(void)40*4882a593Smuzhiyun ulong timer_get_boot_us(void)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun 	ulong count = timer_read_counter();
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun #if CONFIG_SYS_TIMER_RATE == 1000000
45*4882a593Smuzhiyun 	return count;
46*4882a593Smuzhiyun #elif CONFIG_SYS_TIMER_RATE > 1000000
47*4882a593Smuzhiyun 	return lldiv(count, CONFIG_SYS_TIMER_RATE / 1000000);
48*4882a593Smuzhiyun #elif defined(CONFIG_SYS_TIMER_RATE)
49*4882a593Smuzhiyun 	return (unsigned long long)count * 1000000 / CONFIG_SYS_TIMER_RATE;
50*4882a593Smuzhiyun #else
51*4882a593Smuzhiyun 	/* Assume the counter is in microseconds */
52*4882a593Smuzhiyun 	return count;
53*4882a593Smuzhiyun #endif
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun #else
57*4882a593Smuzhiyun extern unsigned long __weak timer_read_counter(void);
58*4882a593Smuzhiyun #endif
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun #ifdef CONFIG_TIMER
get_tbclk(void)61*4882a593Smuzhiyun ulong notrace get_tbclk(void)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun 	if (!gd->timer) {
64*4882a593Smuzhiyun #ifdef CONFIG_TIMER_EARLY
65*4882a593Smuzhiyun 		return timer_early_get_rate();
66*4882a593Smuzhiyun #else
67*4882a593Smuzhiyun 		int ret;
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 		ret = dm_timer_init();
70*4882a593Smuzhiyun 		if (ret)
71*4882a593Smuzhiyun 			return ret;
72*4882a593Smuzhiyun #endif
73*4882a593Smuzhiyun 	}
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	return timer_get_rate(gd->timer);
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun 
get_ticks(void)78*4882a593Smuzhiyun uint64_t notrace get_ticks(void)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun 	u64 count;
81*4882a593Smuzhiyun 	int ret;
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	if (!gd->timer) {
84*4882a593Smuzhiyun #ifdef CONFIG_TIMER_EARLY
85*4882a593Smuzhiyun 		return timer_early_get_count();
86*4882a593Smuzhiyun #else
87*4882a593Smuzhiyun 		int ret;
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 		ret = dm_timer_init();
90*4882a593Smuzhiyun 		if (ret)
91*4882a593Smuzhiyun 			return ret;
92*4882a593Smuzhiyun #endif
93*4882a593Smuzhiyun 	}
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	ret = timer_get_count(gd->timer, &count);
96*4882a593Smuzhiyun 	if (ret)
97*4882a593Smuzhiyun 		return ret;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	return count;
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun #else /* !CONFIG_TIMER */
103*4882a593Smuzhiyun 
get_ticks(void)104*4882a593Smuzhiyun uint64_t __weak notrace get_ticks(void)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun 	unsigned long now = timer_read_counter();
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	/* increment tbu if tbl has rolled over */
109*4882a593Smuzhiyun 	if (now < gd->timebase_l)
110*4882a593Smuzhiyun 		gd->timebase_h++;
111*4882a593Smuzhiyun 	gd->timebase_l = now;
112*4882a593Smuzhiyun 	return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun #endif /* CONFIG_TIMER */
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun /* Returns time in milliseconds */
tick_to_time(uint64_t tick)118*4882a593Smuzhiyun static uint64_t notrace tick_to_time(uint64_t tick)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun 	ulong div = get_tbclk();
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	tick *= CONFIG_SYS_HZ;
123*4882a593Smuzhiyun 	do_div(tick, div);
124*4882a593Smuzhiyun 	return tick;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun 
timer_init(void)127*4882a593Smuzhiyun int __weak timer_init(void)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun 	return 0;
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun /* Returns time in milliseconds */
get_timer(ulong base)133*4882a593Smuzhiyun ulong __weak get_timer(ulong base)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun 	return tick_to_time(get_ticks()) - base;
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun 
timer_get_us(void)138*4882a593Smuzhiyun unsigned long __weak notrace timer_get_us(void)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun 	return tick_to_time(get_ticks() * 1000);
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun 
usec_to_tick(unsigned long usec)143*4882a593Smuzhiyun static uint64_t usec_to_tick(unsigned long usec)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun 	uint64_t tick = usec;
146*4882a593Smuzhiyun 	tick *= get_tbclk();
147*4882a593Smuzhiyun 	do_div(tick, 1000000);
148*4882a593Smuzhiyun 	return tick;
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun 
__udelay(unsigned long usec)151*4882a593Smuzhiyun void __weak __udelay(unsigned long usec)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun 	uint64_t tmp;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	tmp = get_ticks() + usec_to_tick(usec);	/* get current timestamp */
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	while (get_ticks() < tmp+1)	/* loop till event */
158*4882a593Smuzhiyun 		 /*NOP*/;
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun /* ------------------------------------------------------------------------- */
162*4882a593Smuzhiyun 
udelay(unsigned long usec)163*4882a593Smuzhiyun void udelay(unsigned long usec)
164*4882a593Smuzhiyun {
165*4882a593Smuzhiyun 	ulong kv;
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	do {
168*4882a593Smuzhiyun 		WATCHDOG_RESET();
169*4882a593Smuzhiyun 		kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
170*4882a593Smuzhiyun 		__udelay (kv);
171*4882a593Smuzhiyun 		usec -= kv;
172*4882a593Smuzhiyun 	} while(usec);
173*4882a593Smuzhiyun }
174