Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 2 | /* |
3 | * (C) Copyright 2000-2009 | ||||
4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. | ||||
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 5 | */ |
6 | |||||
7 | #include <common.h> | ||||
Simon Glass | 1ea9789 | 2020-05-10 11:40:00 -0600 | [diff] [blame] | 8 | #include <bootstage.h> |
Thomas Chou | fb798b1 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 9 | #include <dm.h> |
10 | #include <errno.h> | ||||
Simon Glass | 495a5dc | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 11 | #include <time.h> |
Thomas Chou | fb798b1 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 12 | #include <timer.h> |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 13 | #include <watchdog.h> |
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 14 | #include <div64.h> |
15 | #include <asm/io.h> | ||||
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 16 | |
17 | #ifndef CONFIG_WD_PERIOD | ||||
Pavel Machek | cb21e37 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 18 | # define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */ |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 19 | #endif |
20 | |||||
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 21 | DECLARE_GLOBAL_DATA_PTR; |
22 | |||||
23 | #ifdef CONFIG_SYS_TIMER_RATE | ||||
Pavel Machek | cb21e37 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 24 | /* Returns tick rate in ticks per second */ |
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 25 | ulong notrace get_tbclk(void) |
26 | { | ||||
27 | return CONFIG_SYS_TIMER_RATE; | ||||
28 | } | ||||
29 | #endif | ||||
30 | |||||
31 | #ifdef CONFIG_SYS_TIMER_COUNTER | ||||
32 | unsigned long notrace timer_read_counter(void) | ||||
33 | { | ||||
34 | #ifdef CONFIG_SYS_TIMER_COUNTS_DOWN | ||||
35 | return ~readl(CONFIG_SYS_TIMER_COUNTER); | ||||
36 | #else | ||||
37 | return readl(CONFIG_SYS_TIMER_COUNTER); | ||||
38 | #endif | ||||
39 | } | ||||
Simon Glass | 037ef5c | 2017-05-22 05:05:22 -0600 | [diff] [blame] | 40 | |
41 | ulong timer_get_boot_us(void) | ||||
42 | { | ||||
43 | ulong count = timer_read_counter(); | ||||
44 | |||||
45 | #if CONFIG_SYS_TIMER_RATE == 1000000 | ||||
46 | return count; | ||||
47 | #elif CONFIG_SYS_TIMER_RATE > 1000000 | ||||
48 | return lldiv(count, CONFIG_SYS_TIMER_RATE / 1000000); | ||||
49 | #elif defined(CONFIG_SYS_TIMER_RATE) | ||||
50 | return (unsigned long long)count * 1000000 / CONFIG_SYS_TIMER_RATE; | ||||
51 | #else | ||||
52 | /* Assume the counter is in microseconds */ | ||||
53 | return count; | ||||
54 | #endif | ||||
55 | } | ||||
56 | |||||
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 57 | #else |
Rob Herring | 316c5c8 | 2013-11-08 08:40:43 -0600 | [diff] [blame] | 58 | extern unsigned long __weak timer_read_counter(void); |
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 59 | #endif |
60 | |||||
Kever Yang | f0b5024 | 2019-03-29 22:17:33 +0800 | [diff] [blame] | 61 | #if CONFIG_IS_ENABLED(TIMER) |
Thomas Chou | fb798b1 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 62 | ulong notrace get_tbclk(void) |
63 | { | ||||
Simon Glass | 32f6c17 | 2016-02-24 09:14:49 -0700 | [diff] [blame] | 64 | if (!gd->timer) { |
65 | #ifdef CONFIG_TIMER_EARLY | ||||
66 | return timer_early_get_rate(); | ||||
67 | #else | ||||
68 | int ret; | ||||
Thomas Chou | fb798b1 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 69 | |
Simon Glass | 32f6c17 | 2016-02-24 09:14:49 -0700 | [diff] [blame] | 70 | ret = dm_timer_init(); |
71 | if (ret) | ||||
72 | return ret; | ||||
73 | #endif | ||||
74 | } | ||||
Thomas Chou | fb798b1 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 75 | |
76 | return timer_get_rate(gd->timer); | ||||
77 | } | ||||
78 | |||||
Bin Meng | ab841b6 | 2015-11-24 13:31:17 -0700 | [diff] [blame] | 79 | uint64_t notrace get_ticks(void) |
Thomas Chou | fb798b1 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 80 | { |
Bin Meng | ab841b6 | 2015-11-24 13:31:17 -0700 | [diff] [blame] | 81 | u64 count; |
Thomas Chou | fb798b1 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 82 | int ret; |
83 | |||||
Simon Glass | 32f6c17 | 2016-02-24 09:14:49 -0700 | [diff] [blame] | 84 | if (!gd->timer) { |
85 | #ifdef CONFIG_TIMER_EARLY | ||||
86 | return timer_early_get_count(); | ||||
87 | #else | ||||
88 | int ret; | ||||
89 | |||||
90 | ret = dm_timer_init(); | ||||
91 | if (ret) | ||||
92 | return ret; | ||||
93 | #endif | ||||
94 | } | ||||
Thomas Chou | fb798b1 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 95 | |
96 | ret = timer_get_count(gd->timer, &count); | ||||
97 | if (ret) | ||||
98 | return ret; | ||||
99 | |||||
100 | return count; | ||||
101 | } | ||||
Bin Meng | ab841b6 | 2015-11-24 13:31:17 -0700 | [diff] [blame] | 102 | |
103 | #else /* !CONFIG_TIMER */ | ||||
Thomas Chou | fb798b1 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 104 | |
Simon Glass | 0e10316 | 2014-10-15 04:38:33 -0600 | [diff] [blame] | 105 | uint64_t __weak notrace get_ticks(void) |
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 106 | { |
107 | unsigned long now = timer_read_counter(); | ||||
108 | |||||
109 | /* increment tbu if tbl has rolled over */ | ||||
110 | if (now < gd->timebase_l) | ||||
111 | gd->timebase_h++; | ||||
112 | gd->timebase_l = now; | ||||
Simon Glass | 0e10316 | 2014-10-15 04:38:33 -0600 | [diff] [blame] | 113 | return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l; |
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 114 | } |
115 | |||||
Bin Meng | ab841b6 | 2015-11-24 13:31:17 -0700 | [diff] [blame] | 116 | #endif /* CONFIG_TIMER */ |
117 | |||||
Pavel Machek | cb21e37 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 118 | /* Returns time in milliseconds */ |
Simon Glass | 0e10316 | 2014-10-15 04:38:33 -0600 | [diff] [blame] | 119 | static uint64_t notrace tick_to_time(uint64_t tick) |
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 120 | { |
Pavel Machek | cb21e37 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 121 | ulong div = get_tbclk(); |
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 122 | |
123 | tick *= CONFIG_SYS_HZ; | ||||
124 | do_div(tick, div); | ||||
125 | return tick; | ||||
126 | } | ||||
127 | |||||
Darwin Rambo | f283e4f | 2013-12-19 15:06:12 -0800 | [diff] [blame] | 128 | int __weak timer_init(void) |
129 | { | ||||
130 | return 0; | ||||
131 | } | ||||
132 | |||||
Pavel Machek | cb21e37 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 133 | /* Returns time in milliseconds */ |
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 134 | ulong __weak get_timer(ulong base) |
135 | { | ||||
136 | return tick_to_time(get_ticks()) - base; | ||||
137 | } | ||||
138 | |||||
Marek Vasut | ebc5033 | 2019-10-15 22:43:41 +0200 | [diff] [blame] | 139 | static uint64_t notrace tick_to_time_us(uint64_t tick) |
140 | { | ||||
141 | ulong div = get_tbclk() / 1000; | ||||
142 | |||||
143 | tick *= CONFIG_SYS_HZ; | ||||
144 | do_div(tick, div); | ||||
145 | return tick; | ||||
146 | } | ||||
147 | |||||
148 | uint64_t __weak get_timer_us(uint64_t base) | ||||
149 | { | ||||
150 | return tick_to_time_us(get_ticks()) - base; | ||||
151 | } | ||||
152 | |||||
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 153 | unsigned long __weak notrace timer_get_us(void) |
154 | { | ||||
155 | return tick_to_time(get_ticks() * 1000); | ||||
156 | } | ||||
Pavel Machek | cb21e37 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 157 | |
Heinrich Schuchardt | daf69fc | 2019-06-02 21:02:10 +0200 | [diff] [blame] | 158 | uint64_t usec_to_tick(unsigned long usec) |
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 159 | { |
Simon Glass | 0e10316 | 2014-10-15 04:38:33 -0600 | [diff] [blame] | 160 | uint64_t tick = usec; |
Stephen Warren | ff81b48 | 2013-12-05 12:08:09 -0700 | [diff] [blame] | 161 | tick *= get_tbclk(); |
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 162 | do_div(tick, 1000000); |
163 | return tick; | ||||
164 | } | ||||
165 | |||||
166 | void __weak __udelay(unsigned long usec) | ||||
167 | { | ||||
Simon Glass | 0e10316 | 2014-10-15 04:38:33 -0600 | [diff] [blame] | 168 | uint64_t tmp; |
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 169 | |
Pavel Machek | cb21e37 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 170 | tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */ |
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 171 | |
Pavel Machek | cb21e37 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 172 | while (get_ticks() < tmp+1) /* loop till event */ |
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 173 | /*NOP*/; |
174 | } | ||||
175 | |||||
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 176 | /* ------------------------------------------------------------------------- */ |
177 | |||||
178 | void udelay(unsigned long usec) | ||||
179 | { | ||||
180 | ulong kv; | ||||
181 | |||||
182 | do { | ||||
183 | WATCHDOG_RESET(); | ||||
184 | kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec; | ||||
185 | __udelay (kv); | ||||
186 | usec -= kv; | ||||
187 | } while(usec); | ||||
188 | } |