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> | ||||
Thomas Chou | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 9 | #include <dm.h> |
10 | #include <errno.h> | ||||
11 | #include <timer.h> | ||||
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 12 | #include <watchdog.h> |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 13 | #include <div64.h> |
14 | #include <asm/io.h> | ||||
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 15 | |
16 | #ifndef CONFIG_WD_PERIOD | ||||
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 17 | # define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */ |
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 18 | #endif |
19 | |||||
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 20 | DECLARE_GLOBAL_DATA_PTR; |
21 | |||||
22 | #ifdef CONFIG_SYS_TIMER_RATE | ||||
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 23 | /* Returns tick rate in ticks per second */ |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 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 | |||||
Thomas Chou | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 43 | #ifdef CONFIG_TIMER |
Thomas Chou | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 44 | ulong notrace get_tbclk(void) |
45 | { | ||||
46 | int ret; | ||||
47 | |||||
48 | ret = dm_timer_init(); | ||||
49 | if (ret) | ||||
50 | return ret; | ||||
51 | |||||
52 | return timer_get_rate(gd->timer); | ||||
53 | } | ||||
54 | |||||
Bin Meng | 9ca07eb | 2015-11-24 13:31:17 -0700 | [diff] [blame] | 55 | uint64_t notrace get_ticks(void) |
Thomas Chou | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 56 | { |
Bin Meng | 9ca07eb | 2015-11-24 13:31:17 -0700 | [diff] [blame] | 57 | u64 count; |
Thomas Chou | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 58 | int ret; |
59 | |||||
60 | ret = dm_timer_init(); | ||||
61 | if (ret) | ||||
62 | return ret; | ||||
63 | |||||
64 | ret = timer_get_count(gd->timer, &count); | ||||
65 | if (ret) | ||||
66 | return ret; | ||||
67 | |||||
68 | return count; | ||||
69 | } | ||||
Bin Meng | 9ca07eb | 2015-11-24 13:31:17 -0700 | [diff] [blame] | 70 | |
71 | #else /* !CONFIG_TIMER */ | ||||
Thomas Chou | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 72 | |
Simon Glass | 19ea467 | 2014-10-15 04:38:33 -0600 | [diff] [blame] | 73 | uint64_t __weak notrace get_ticks(void) |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 74 | { |
75 | unsigned long now = timer_read_counter(); | ||||
76 | |||||
77 | /* increment tbu if tbl has rolled over */ | ||||
78 | if (now < gd->timebase_l) | ||||
79 | gd->timebase_h++; | ||||
80 | gd->timebase_l = now; | ||||
Simon Glass | 19ea467 | 2014-10-15 04:38:33 -0600 | [diff] [blame] | 81 | return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l; |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 82 | } |
83 | |||||
Bin Meng | 9ca07eb | 2015-11-24 13:31:17 -0700 | [diff] [blame] | 84 | #endif /* CONFIG_TIMER */ |
85 | |||||
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 86 | /* Returns time in milliseconds */ |
Simon Glass | 19ea467 | 2014-10-15 04:38:33 -0600 | [diff] [blame] | 87 | static uint64_t notrace tick_to_time(uint64_t tick) |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 88 | { |
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 89 | ulong div = get_tbclk(); |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 90 | |
91 | tick *= CONFIG_SYS_HZ; | ||||
92 | do_div(tick, div); | ||||
93 | return tick; | ||||
94 | } | ||||
95 | |||||
Darwin Rambo | de351d6 | 2013-12-19 15:06:12 -0800 | [diff] [blame] | 96 | int __weak timer_init(void) |
97 | { | ||||
98 | return 0; | ||||
99 | } | ||||
100 | |||||
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 101 | /* Returns time in milliseconds */ |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 102 | ulong __weak get_timer(ulong base) |
103 | { | ||||
104 | return tick_to_time(get_ticks()) - base; | ||||
105 | } | ||||
106 | |||||
107 | unsigned long __weak notrace timer_get_us(void) | ||||
108 | { | ||||
109 | return tick_to_time(get_ticks() * 1000); | ||||
110 | } | ||||
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 111 | |
Simon Glass | 19ea467 | 2014-10-15 04:38:33 -0600 | [diff] [blame] | 112 | static uint64_t usec_to_tick(unsigned long usec) |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 113 | { |
Simon Glass | 19ea467 | 2014-10-15 04:38:33 -0600 | [diff] [blame] | 114 | uint64_t tick = usec; |
Stephen Warren | 2cd1b57 | 2013-12-05 12:08:09 -0700 | [diff] [blame] | 115 | tick *= get_tbclk(); |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 116 | do_div(tick, 1000000); |
117 | return tick; | ||||
118 | } | ||||
119 | |||||
120 | void __weak __udelay(unsigned long usec) | ||||
121 | { | ||||
Simon Glass | 19ea467 | 2014-10-15 04:38:33 -0600 | [diff] [blame] | 122 | uint64_t tmp; |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 123 | |
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 124 | tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */ |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 125 | |
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 126 | while (get_ticks() < tmp+1) /* loop till event */ |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 127 | /*NOP*/; |
128 | } | ||||
129 | |||||
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 130 | /* ------------------------------------------------------------------------- */ |
131 | |||||
132 | void udelay(unsigned long usec) | ||||
133 | { | ||||
134 | ulong kv; | ||||
135 | |||||
136 | do { | ||||
137 | WATCHDOG_RESET(); | ||||
138 | kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec; | ||||
139 | __udelay (kv); | ||||
140 | usec -= kv; | ||||
141 | } while(usec); | ||||
142 | } | ||||
Anatolij Gustschin | c4c9fbe | 2011-10-12 02:31:39 +0000 | [diff] [blame] | 143 | |
144 | void mdelay(unsigned long msec) | ||||
145 | { | ||||
146 | while (msec--) | ||||
147 | udelay(1000); | ||||
148 | } |