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> | ||||
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 17 | #include <linux/delay.h> |
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 18 | |
19 | #ifndef CONFIG_WD_PERIOD | ||||
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 20 | # define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */ |
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 21 | #endif |
22 | |||||
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 23 | DECLARE_GLOBAL_DATA_PTR; |
24 | |||||
25 | #ifdef CONFIG_SYS_TIMER_RATE | ||||
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 26 | /* Returns tick rate in ticks per second */ |
Rob Herring | 8dfafdd | 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 | 9fb34b0 | 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 | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 59 | #else |
Rob Herring | 65ba7ad | 2013-11-08 08:40:43 -0600 | [diff] [blame] | 60 | extern unsigned long __weak timer_read_counter(void); |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 61 | #endif |
62 | |||||
Kever Yang | f00c262 | 2019-03-29 22:17:33 +0800 | [diff] [blame] | 63 | #if CONFIG_IS_ENABLED(TIMER) |
Thomas Chou | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 64 | ulong notrace get_tbclk(void) |
65 | { | ||||
Simon Glass | c95fec3 | 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 | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 71 | |
Simon Glass | c95fec3 | 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 | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 77 | |
78 | return timer_get_rate(gd->timer); | ||||
79 | } | ||||
80 | |||||
Bin Meng | 9ca07eb | 2015-11-24 13:31:17 -0700 | [diff] [blame] | 81 | uint64_t notrace get_ticks(void) |
Thomas Chou | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 82 | { |
Bin Meng | 9ca07eb | 2015-11-24 13:31:17 -0700 | [diff] [blame] | 83 | u64 count; |
Thomas Chou | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 84 | int ret; |
85 | |||||
Simon Glass | c95fec3 | 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 | c8a7ba9 | 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 | 9ca07eb | 2015-11-24 13:31:17 -0700 | [diff] [blame] | 104 | |
105 | #else /* !CONFIG_TIMER */ | ||||
Thomas Chou | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 106 | |
Simon Glass | 19ea467 | 2014-10-15 04:38:33 -0600 | [diff] [blame] | 107 | uint64_t __weak notrace get_ticks(void) |
Rob Herring | 8dfafdd | 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 | 19ea467 | 2014-10-15 04:38:33 -0600 | [diff] [blame] | 115 | return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l; |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 116 | } |
117 | |||||
Bin Meng | 9ca07eb | 2015-11-24 13:31:17 -0700 | [diff] [blame] | 118 | #endif /* CONFIG_TIMER */ |
119 | |||||
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 120 | /* Returns time in milliseconds */ |
Simon Glass | 19ea467 | 2014-10-15 04:38:33 -0600 | [diff] [blame] | 121 | static uint64_t notrace tick_to_time(uint64_t tick) |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 122 | { |
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 123 | ulong div = get_tbclk(); |
Rob Herring | 8dfafdd | 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 | de351d6 | 2013-12-19 15:06:12 -0800 | [diff] [blame] | 130 | int __weak timer_init(void) |
131 | { | ||||
132 | return 0; | ||||
133 | } | ||||
134 | |||||
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 135 | /* Returns time in milliseconds */ |
Rob Herring | 8dfafdd | 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 | 80e7e7c | 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 | |||||
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 155 | unsigned long __weak notrace timer_get_us(void) |
156 | { | ||||
157 | return tick_to_time(get_ticks() * 1000); | ||||
158 | } | ||||
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 159 | |
Heinrich Schuchardt | 6a853db | 2019-06-02 21:02:10 +0200 | [diff] [blame] | 160 | uint64_t usec_to_tick(unsigned long usec) |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 161 | { |
Simon Glass | 19ea467 | 2014-10-15 04:38:33 -0600 | [diff] [blame] | 162 | uint64_t tick = usec; |
Stephen Warren | 2cd1b57 | 2013-12-05 12:08:09 -0700 | [diff] [blame] | 163 | tick *= get_tbclk(); |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 164 | do_div(tick, 1000000); |
165 | return tick; | ||||
166 | } | ||||
167 | |||||
168 | void __weak __udelay(unsigned long usec) | ||||
169 | { | ||||
Simon Glass | 19ea467 | 2014-10-15 04:38:33 -0600 | [diff] [blame] | 170 | uint64_t tmp; |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 171 | |
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 172 | tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */ |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 173 | |
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 174 | while (get_ticks() < tmp+1) /* loop till event */ |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 175 | /*NOP*/; |
176 | } | ||||
177 | |||||
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 178 | /* ------------------------------------------------------------------------- */ |
179 | |||||
180 | void udelay(unsigned long usec) | ||||
181 | { | ||||
182 | ulong kv; | ||||
183 | |||||
184 | do { | ||||
185 | WATCHDOG_RESET(); | ||||
186 | kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec; | ||||
Simon Glass | 07e1114 | 2020-05-10 11:40:10 -0600 | [diff] [blame] | 187 | __udelay(kv); |
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 188 | usec -= kv; |
189 | } while(usec); | ||||
190 | } |