]>
git.ipfire.org Git - people/ms/u-boot.git/blob - lib/time.c
2 * (C) Copyright 2000-2009
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
16 #ifndef CONFIG_WD_PERIOD
17 # define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
20 DECLARE_GLOBAL_DATA_PTR
;
22 #ifdef CONFIG_SYS_TIMER_RATE
23 /* Returns tick rate in ticks per second */
24 ulong notrace
get_tbclk(void)
26 return CONFIG_SYS_TIMER_RATE
;
30 #ifdef CONFIG_SYS_TIMER_COUNTER
31 unsigned long notrace
timer_read_counter(void)
33 #ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
34 return ~readl(CONFIG_SYS_TIMER_COUNTER
);
36 return readl(CONFIG_SYS_TIMER_COUNTER
);
40 extern unsigned long __weak
timer_read_counter(void);
44 ulong notrace
get_tbclk(void)
47 #ifdef CONFIG_TIMER_EARLY
48 return timer_early_get_rate();
52 ret
= dm_timer_init();
58 return timer_get_rate(gd
->timer
);
61 uint64_t notrace
get_ticks(void)
67 #ifdef CONFIG_TIMER_EARLY
68 return timer_early_get_count();
72 ret
= dm_timer_init();
78 ret
= timer_get_count(gd
->timer
, &count
);
85 #else /* !CONFIG_TIMER */
87 uint64_t __weak notrace
get_ticks(void)
89 unsigned long now
= timer_read_counter();
91 /* increment tbu if tbl has rolled over */
92 if (now
< gd
->timebase_l
)
95 return ((uint64_t)gd
->timebase_h
<< 32) | gd
->timebase_l
;
98 #endif /* CONFIG_TIMER */
100 /* Returns time in milliseconds */
101 static uint64_t notrace
tick_to_time(uint64_t tick
)
103 ulong div
= get_tbclk();
105 tick
*= CONFIG_SYS_HZ
;
110 int __weak
timer_init(void)
115 /* Returns time in milliseconds */
116 ulong __weak
get_timer(ulong base
)
118 return tick_to_time(get_ticks()) - base
;
121 unsigned long __weak notrace
timer_get_us(void)
123 return tick_to_time(get_ticks() * 1000);
126 static uint64_t usec_to_tick(unsigned long usec
)
128 uint64_t tick
= usec
;
130 do_div(tick
, 1000000);
134 void __weak
__udelay(unsigned long usec
)
138 tmp
= get_ticks() + usec_to_tick(usec
); /* get current timestamp */
140 while (get_ticks() < tmp
+1) /* loop till event */
144 /* ------------------------------------------------------------------------- */
146 void udelay(unsigned long usec
)
152 kv
= usec
> CONFIG_WD_PERIOD
? CONFIG_WD_PERIOD
: usec
;