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