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