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