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