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