Blame view

lib/time.c 3.04 KB
3eb90bad6   Ingo van Lil   Generic udelay() ...
1
2
3
4
  /*
   * (C) Copyright 2000-2009
   * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
   *
1a4596601   Wolfgang Denk   Add GPL-2.0+ SPDX...
5
   * SPDX-License-Identifier:	GPL-2.0+
3eb90bad6   Ingo van Lil   Generic udelay() ...
6
7
8
   */
  
  #include <common.h>
c8a7ba9e6   Thomas Chou   dm: implement a T...
9
10
11
  #include <dm.h>
  #include <errno.h>
  #include <timer.h>
3eb90bad6   Ingo van Lil   Generic udelay() ...
12
  #include <watchdog.h>
8dfafdde8   Rob Herring   Introduce common ...
13
14
  #include <div64.h>
  #include <asm/io.h>
3eb90bad6   Ingo van Lil   Generic udelay() ...
15
16
  
  #ifndef CONFIG_WD_PERIOD
fccacd3b4   Pavel Machek   lib/time.c cleanups
17
  # define CONFIG_WD_PERIOD	(10 * 1000 * 1000)	/* 10 seconds default */
3eb90bad6   Ingo van Lil   Generic udelay() ...
18
  #endif
8dfafdde8   Rob Herring   Introduce common ...
19
20
21
  DECLARE_GLOBAL_DATA_PTR;
  
  #ifdef CONFIG_SYS_TIMER_RATE
fccacd3b4   Pavel Machek   lib/time.c cleanups
22
  /* Returns tick rate in ticks per second */
8dfafdde8   Rob Herring   Introduce common ...
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  ulong notrace get_tbclk(void)
  {
  	return CONFIG_SYS_TIMER_RATE;
  }
  #endif
  
  #ifdef CONFIG_SYS_TIMER_COUNTER
  unsigned long notrace timer_read_counter(void)
  {
  #ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
  	return ~readl(CONFIG_SYS_TIMER_COUNTER);
  #else
  	return readl(CONFIG_SYS_TIMER_COUNTER);
  #endif
  }
9fb34b01f   Simon Glass   bootstage: Provid...
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  
  ulong timer_get_boot_us(void)
  {
  	ulong count = timer_read_counter();
  
  #if CONFIG_SYS_TIMER_RATE == 1000000
  	return count;
  #elif CONFIG_SYS_TIMER_RATE > 1000000
  	return lldiv(count, CONFIG_SYS_TIMER_RATE / 1000000);
  #elif defined(CONFIG_SYS_TIMER_RATE)
  	return (unsigned long long)count * 1000000 / CONFIG_SYS_TIMER_RATE;
  #else
  	/* Assume the counter is in microseconds */
  	return count;
  #endif
  }
8dfafdde8   Rob Herring   Introduce common ...
54
  #else
65ba7add0   Rob Herring   time: add weak an...
55
  extern unsigned long __weak timer_read_counter(void);
8dfafdde8   Rob Herring   Introduce common ...
56
  #endif
c8a7ba9e6   Thomas Chou   dm: implement a T...
57
  #ifdef CONFIG_TIMER
c8a7ba9e6   Thomas Chou   dm: implement a T...
58
59
  ulong notrace get_tbclk(void)
  {
c95fec319   Simon Glass   timer: Provide an...
60
61
62
63
64
  	if (!gd->timer) {
  #ifdef CONFIG_TIMER_EARLY
  		return timer_early_get_rate();
  #else
  		int ret;
c8a7ba9e6   Thomas Chou   dm: implement a T...
65

c95fec319   Simon Glass   timer: Provide an...
66
67
68
69
70
  		ret = dm_timer_init();
  		if (ret)
  			return ret;
  #endif
  	}
c8a7ba9e6   Thomas Chou   dm: implement a T...
71
72
73
  
  	return timer_get_rate(gd->timer);
  }
9ca07ebba   Bin Meng   dm: timer: Suppor...
74
  uint64_t notrace get_ticks(void)
c8a7ba9e6   Thomas Chou   dm: implement a T...
75
  {
9ca07ebba   Bin Meng   dm: timer: Suppor...
76
  	u64 count;
c8a7ba9e6   Thomas Chou   dm: implement a T...
77
  	int ret;
c95fec319   Simon Glass   timer: Provide an...
78
79
80
81
82
83
84
85
86
87
88
  	if (!gd->timer) {
  #ifdef CONFIG_TIMER_EARLY
  		return timer_early_get_count();
  #else
  		int ret;
  
  		ret = dm_timer_init();
  		if (ret)
  			return ret;
  #endif
  	}
c8a7ba9e6   Thomas Chou   dm: implement a T...
89
90
91
92
93
94
95
  
  	ret = timer_get_count(gd->timer, &count);
  	if (ret)
  		return ret;
  
  	return count;
  }
9ca07ebba   Bin Meng   dm: timer: Suppor...
96
97
  
  #else /* !CONFIG_TIMER */
c8a7ba9e6   Thomas Chou   dm: implement a T...
98

19ea4678c   Simon Glass   Use uint64_t for ...
99
  uint64_t __weak notrace get_ticks(void)
8dfafdde8   Rob Herring   Introduce common ...
100
101
102
103
104
105
106
  {
  	unsigned long now = timer_read_counter();
  
  	/* increment tbu if tbl has rolled over */
  	if (now < gd->timebase_l)
  		gd->timebase_h++;
  	gd->timebase_l = now;
19ea4678c   Simon Glass   Use uint64_t for ...
107
  	return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
8dfafdde8   Rob Herring   Introduce common ...
108
  }
9ca07ebba   Bin Meng   dm: timer: Suppor...
109
  #endif /* CONFIG_TIMER */
fccacd3b4   Pavel Machek   lib/time.c cleanups
110
  /* Returns time in milliseconds */
19ea4678c   Simon Glass   Use uint64_t for ...
111
  static uint64_t notrace tick_to_time(uint64_t tick)
8dfafdde8   Rob Herring   Introduce common ...
112
  {
fccacd3b4   Pavel Machek   lib/time.c cleanups
113
  	ulong div = get_tbclk();
8dfafdde8   Rob Herring   Introduce common ...
114
115
116
117
118
  
  	tick *= CONFIG_SYS_HZ;
  	do_div(tick, div);
  	return tick;
  }
de351d6be   Darwin Rambo   lib: time: add we...
119
120
121
122
  int __weak timer_init(void)
  {
  	return 0;
  }
fccacd3b4   Pavel Machek   lib/time.c cleanups
123
  /* Returns time in milliseconds */
8dfafdde8   Rob Herring   Introduce common ...
124
125
126
127
128
129
130
131
132
  ulong __weak get_timer(ulong base)
  {
  	return tick_to_time(get_ticks()) - base;
  }
  
  unsigned long __weak notrace timer_get_us(void)
  {
  	return tick_to_time(get_ticks() * 1000);
  }
fccacd3b4   Pavel Machek   lib/time.c cleanups
133

19ea4678c   Simon Glass   Use uint64_t for ...
134
  static uint64_t usec_to_tick(unsigned long usec)
8dfafdde8   Rob Herring   Introduce common ...
135
  {
19ea4678c   Simon Glass   Use uint64_t for ...
136
  	uint64_t tick = usec;
2cd1b5722   Stephen Warren   time: fix usec_to...
137
  	tick *= get_tbclk();
8dfafdde8   Rob Herring   Introduce common ...
138
139
140
141
142
143
  	do_div(tick, 1000000);
  	return tick;
  }
  
  void __weak __udelay(unsigned long usec)
  {
19ea4678c   Simon Glass   Use uint64_t for ...
144
  	uint64_t tmp;
8dfafdde8   Rob Herring   Introduce common ...
145

fccacd3b4   Pavel Machek   lib/time.c cleanups
146
  	tmp = get_ticks() + usec_to_tick(usec);	/* get current timestamp */
8dfafdde8   Rob Herring   Introduce common ...
147

fccacd3b4   Pavel Machek   lib/time.c cleanups
148
  	while (get_ticks() < tmp+1)	/* loop till event */
8dfafdde8   Rob Herring   Introduce common ...
149
150
  		 /*NOP*/;
  }
3eb90bad6   Ingo van Lil   Generic udelay() ...
151
152
153
154
155
156
157
158
159
160
161
162
163
  /* ------------------------------------------------------------------------- */
  
  void udelay(unsigned long usec)
  {
  	ulong kv;
  
  	do {
  		WATCHDOG_RESET();
  		kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
  		__udelay (kv);
  		usec -= kv;
  	} while(usec);
  }