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