]> git.ipfire.org Git - people/ms/u-boot.git/blame - lib/time.c
include/net.h: add max_speed member in struct eth_pdata
[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>
c8a7ba9e
TC
9#include <dm.h>
10#include <errno.h>
11#include <timer.h>
3eb90bad 12#include <watchdog.h>
8dfafdde
RH
13#include <div64.h>
14#include <asm/io.h>
3eb90bad
IL
15
16#ifndef CONFIG_WD_PERIOD
fccacd3b 17# define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
3eb90bad
IL
18#endif
19
8dfafdde
RH
20DECLARE_GLOBAL_DATA_PTR;
21
22#ifdef CONFIG_SYS_TIMER_RATE
fccacd3b 23/* Returns tick rate in ticks per second */
8dfafdde
RH
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
65ba7add 40extern unsigned long __weak timer_read_counter(void);
8dfafdde
RH
41#endif
42
c8a7ba9e
TC
43#ifdef CONFIG_TIMER
44static int notrace dm_timer_init(void)
45{
46 struct udevice *dev;
47 int ret;
48
49 if (!gd->timer) {
50 ret = uclass_first_device(UCLASS_TIMER, &dev);
51 if (ret)
52 return ret;
53 if (!dev)
54 return -ENODEV;
55 gd->timer = dev;
56 }
57
58 return 0;
59}
60
61ulong notrace get_tbclk(void)
62{
63 int ret;
64
65 ret = dm_timer_init();
66 if (ret)
67 return ret;
68
69 return timer_get_rate(gd->timer);
70}
71
9ca07ebb 72uint64_t notrace get_ticks(void)
c8a7ba9e 73{
9ca07ebb 74 u64 count;
c8a7ba9e
TC
75 int ret;
76
77 ret = dm_timer_init();
78 if (ret)
79 return ret;
80
81 ret = timer_get_count(gd->timer, &count);
82 if (ret)
83 return ret;
84
85 return count;
86}
9ca07ebb
BM
87
88#else /* !CONFIG_TIMER */
c8a7ba9e 89
19ea4678 90uint64_t __weak notrace get_ticks(void)
8dfafdde
RH
91{
92 unsigned long now = timer_read_counter();
93
94 /* increment tbu if tbl has rolled over */
95 if (now < gd->timebase_l)
96 gd->timebase_h++;
97 gd->timebase_l = now;
19ea4678 98 return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
8dfafdde
RH
99}
100
9ca07ebb
BM
101#endif /* CONFIG_TIMER */
102
fccacd3b 103/* Returns time in milliseconds */
19ea4678 104static uint64_t notrace tick_to_time(uint64_t tick)
8dfafdde 105{
fccacd3b 106 ulong div = get_tbclk();
8dfafdde
RH
107
108 tick *= CONFIG_SYS_HZ;
109 do_div(tick, div);
110 return tick;
111}
112
de351d6b
DR
113int __weak timer_init(void)
114{
115 return 0;
116}
117
fccacd3b 118/* Returns time in milliseconds */
8dfafdde
RH
119ulong __weak get_timer(ulong base)
120{
121 return tick_to_time(get_ticks()) - base;
122}
123
124unsigned long __weak notrace timer_get_us(void)
125{
126 return tick_to_time(get_ticks() * 1000);
127}
fccacd3b 128
19ea4678 129static uint64_t usec_to_tick(unsigned long usec)
8dfafdde 130{
19ea4678 131 uint64_t tick = usec;
2cd1b572 132 tick *= get_tbclk();
8dfafdde
RH
133 do_div(tick, 1000000);
134 return tick;
135}
136
137void __weak __udelay(unsigned long usec)
138{
19ea4678 139 uint64_t tmp;
8dfafdde 140
fccacd3b 141 tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
8dfafdde 142
fccacd3b 143 while (get_ticks() < tmp+1) /* loop till event */
8dfafdde
RH
144 /*NOP*/;
145}
146
3eb90bad
IL
147/* ------------------------------------------------------------------------- */
148
149void udelay(unsigned long usec)
150{
151 ulong kv;
152
153 do {
154 WATCHDOG_RESET();
155 kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
156 __udelay (kv);
157 usec -= kv;
158 } while(usec);
159}
c4c9fbeb
AG
160
161void mdelay(unsigned long msec)
162{
163 while (msec--)
164 udelay(1000);
165}