Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000-2009 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <watchdog.h> |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 10 | #include <div64.h> |
| 11 | #include <asm/io.h> |
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 12 | |
Rob Herring | 2108f4c | 2013-10-04 08:40:03 -0500 | [diff] [blame] | 13 | #if CONFIG_SYS_HZ != 1000 |
| 14 | #warning "CONFIG_SYS_HZ must be 1000 and should not be defined by platforms" |
| 15 | #endif |
| 16 | |
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 17 | #ifndef CONFIG_WD_PERIOD |
| 18 | # define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default*/ |
| 19 | #endif |
| 20 | |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
| 23 | #ifdef CONFIG_SYS_TIMER_RATE |
| 24 | ulong notrace get_tbclk(void) |
| 25 | { |
| 26 | return CONFIG_SYS_TIMER_RATE; |
| 27 | } |
| 28 | #endif |
| 29 | |
| 30 | #ifdef CONFIG_SYS_TIMER_COUNTER |
| 31 | unsigned long notrace timer_read_counter(void) |
| 32 | { |
| 33 | #ifdef CONFIG_SYS_TIMER_COUNTS_DOWN |
| 34 | return ~readl(CONFIG_SYS_TIMER_COUNTER); |
| 35 | #else |
| 36 | return readl(CONFIG_SYS_TIMER_COUNTER); |
| 37 | #endif |
| 38 | } |
| 39 | #else |
Rob Herring | 65ba7ad | 2013-11-08 08:40:43 -0600 | [diff] [blame] | 40 | extern unsigned long __weak timer_read_counter(void); |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 41 | #endif |
| 42 | |
| 43 | unsigned long long __weak notrace get_ticks(void) |
| 44 | { |
| 45 | unsigned long now = timer_read_counter(); |
| 46 | |
| 47 | /* increment tbu if tbl has rolled over */ |
| 48 | if (now < gd->timebase_l) |
| 49 | gd->timebase_h++; |
| 50 | gd->timebase_l = now; |
| 51 | return ((unsigned long long)gd->timebase_h << 32) | gd->timebase_l; |
| 52 | } |
| 53 | |
Daniel Schwierzeck | d770f39 | 2013-11-09 00:30:14 +0100 | [diff] [blame] | 54 | static unsigned long long notrace tick_to_time(uint64_t tick) |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 55 | { |
| 56 | unsigned int div = get_tbclk(); |
| 57 | |
| 58 | tick *= CONFIG_SYS_HZ; |
| 59 | do_div(tick, div); |
| 60 | return tick; |
| 61 | } |
| 62 | |
Darwin Rambo | de351d6 | 2013-12-19 15:06:12 -0800 | [diff] [blame] | 63 | int __weak timer_init(void) |
| 64 | { |
| 65 | return 0; |
| 66 | } |
| 67 | |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 68 | ulong __weak get_timer(ulong base) |
| 69 | { |
| 70 | return tick_to_time(get_ticks()) - base; |
| 71 | } |
| 72 | |
| 73 | unsigned long __weak notrace timer_get_us(void) |
| 74 | { |
| 75 | return tick_to_time(get_ticks() * 1000); |
| 76 | } |
| 77 | static unsigned long long usec_to_tick(unsigned long usec) |
| 78 | { |
Stephen Warren | 2cd1b57 | 2013-12-05 12:08:09 -0700 | [diff] [blame] | 79 | uint64_t tick = usec; |
| 80 | tick *= get_tbclk(); |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 81 | do_div(tick, 1000000); |
| 82 | return tick; |
| 83 | } |
| 84 | |
| 85 | void __weak __udelay(unsigned long usec) |
| 86 | { |
| 87 | unsigned long long tmp; |
| 88 | ulong tmo; |
| 89 | |
| 90 | tmo = usec_to_tick(usec); |
| 91 | tmp = get_ticks() + tmo; /* get current timestamp */ |
| 92 | |
| 93 | while (get_ticks() < tmp) /* loop till event */ |
| 94 | /*NOP*/; |
| 95 | } |
| 96 | |
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 97 | /* ------------------------------------------------------------------------- */ |
| 98 | |
| 99 | void udelay(unsigned long usec) |
| 100 | { |
| 101 | ulong kv; |
| 102 | |
| 103 | do { |
| 104 | WATCHDOG_RESET(); |
| 105 | kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec; |
| 106 | __udelay (kv); |
| 107 | usec -= kv; |
| 108 | } while(usec); |
| 109 | } |
Anatolij Gustschin | c4c9fbe | 2011-10-12 02:31:39 +0000 | [diff] [blame] | 110 | |
| 111 | void mdelay(unsigned long msec) |
| 112 | { |
| 113 | while (msec--) |
| 114 | udelay(1000); |
| 115 | } |