]> git.ipfire.org Git - people/ms/u-boot.git/blame - lib/time.c
netconsole loses 2nd character of input
[people/ms/u-boot.git] / lib / time.c
CommitLineData
3eb90bad
IL
1/*
2 * (C) Copyright 2000-2009
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
1a459660 5 * SPDX-License-Identifier: GPL-2.0+
3eb90bad
IL
6 */
7
8#include <common.h>
9#include <watchdog.h>
8dfafdde
RH
10#include <div64.h>
11#include <asm/io.h>
3eb90bad 12
2108f4c4
RH
13#if CONFIG_SYS_HZ != 1000
14#warning "CONFIG_SYS_HZ must be 1000 and should not be defined by platforms"
15#endif
16
3eb90bad
IL
17#ifndef CONFIG_WD_PERIOD
18# define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default*/
19#endif
20
8dfafdde
RH
21DECLARE_GLOBAL_DATA_PTR;
22
23#ifdef CONFIG_SYS_TIMER_RATE
24ulong notrace get_tbclk(void)
25{
26 return CONFIG_SYS_TIMER_RATE;
27}
28#endif
29
30#ifdef CONFIG_SYS_TIMER_COUNTER
31unsigned long notrace timer_read_counter(void)
32{
33#ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
34 return ~readl(CONFIG_SYS_TIMER_COUNTER);
35#else
36 return readl(CONFIG_SYS_TIMER_COUNTER);
37#endif
38}
39#else
40extern unsigned long timer_read_counter(void);
41#endif
42
43unsigned long long __weak notrace get_ticks(void)
44{
45 unsigned long now = timer_read_counter();
46
47 /* increment tbu if tbl has rolled over */
48 if (now < gd->timebase_l)
49 gd->timebase_h++;
50 gd->timebase_l = now;
51 return ((unsigned long long)gd->timebase_h << 32) | gd->timebase_l;
52}
53
54static unsigned long long notrace tick_to_time(unsigned long long tick)
55{
56 unsigned int div = get_tbclk();
57
58 tick *= CONFIG_SYS_HZ;
59 do_div(tick, div);
60 return tick;
61}
62
63ulong __weak get_timer(ulong base)
64{
65 return tick_to_time(get_ticks()) - base;
66}
67
68unsigned long __weak notrace timer_get_us(void)
69{
70 return tick_to_time(get_ticks() * 1000);
71}
72static unsigned long long usec_to_tick(unsigned long usec)
73{
74 unsigned long long tick = usec * get_tbclk();
75 usec *= get_tbclk();
76 do_div(tick, 1000000);
77 return tick;
78}
79
80void __weak __udelay(unsigned long usec)
81{
82 unsigned long long tmp;
83 ulong tmo;
84
85 tmo = usec_to_tick(usec);
86 tmp = get_ticks() + tmo; /* get current timestamp */
87
88 while (get_ticks() < tmp) /* loop till event */
89 /*NOP*/;
90}
91
3eb90bad
IL
92/* ------------------------------------------------------------------------- */
93
94void udelay(unsigned long usec)
95{
96 ulong kv;
97
98 do {
99 WATCHDOG_RESET();
100 kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
101 __udelay (kv);
102 usec -= kv;
103 } while(usec);
104}
c4c9fbeb
AG
105
106void mdelay(unsigned long msec)
107{
108 while (msec--)
109 udelay(1000);
110}