Jean-Christophe PLAGNIOL-VILLARD | 3c9dc3b | 2009-05-17 00:58:36 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 4 | * Marius Groeger <mgroeger@sysgo.de> |
| 5 | * |
| 6 | * (C) Copyright 2002 |
| 7 | * David Mueller, ELSOFT AG, <d.mueller@elsoft.ch> |
| 8 | * |
| 9 | * (C) Copyright 2003 |
| 10 | * Texas Instruments, <www.ti.com> |
| 11 | * Kshitij Gupta <Kshitij@ti.com> |
| 12 | * |
| 13 | * (C) Copyright 2004 |
| 14 | * ARM Ltd. |
| 15 | * Philippe Robin, <philippe.robin@arm.com> |
| 16 | * |
| 17 | * See file CREDITS for list of people who contributed to this |
| 18 | * project. |
| 19 | * |
| 20 | * This program is free software; you can redistribute it and/or |
| 21 | * modify it under the terms of the GNU General Public License as |
| 22 | * published by the Free Software Foundation; either version 2 of |
| 23 | * the License, or (at your option) any later version. |
| 24 | * |
| 25 | * This program is distributed in the hope that it will be useful, |
| 26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 28 | * GNU General Public License for more details. |
| 29 | * |
| 30 | * You should have received a copy of the GNU General Public License |
| 31 | * along with this program; if not, write to the Free Software |
| 32 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 33 | * MA 02111-1307 USA |
| 34 | */ |
| 35 | |
| 36 | #include <common.h> |
| 37 | #include <div64.h> |
| 38 | |
Jean-Christophe PLAGNIOL-VILLARD | 693a7ae | 2009-05-17 00:58:37 +0200 | [diff] [blame] | 39 | #ifdef CONFIG_ARCH_CINTEGRATOR |
| 40 | #define DIV_CLOCK_INIT 1 |
| 41 | #define TIMER_LOAD_VAL 0xFFFFFFFFL |
| 42 | #else |
| 43 | #define DIV_CLOCK_INIT 256 |
| 44 | #define TIMER_LOAD_VAL 0x0000FFFFL |
| 45 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 3c9dc3b | 2009-05-17 00:58:36 +0200 | [diff] [blame] | 46 | /* The Integrator/CP timer1 is clocked at 1MHz |
| 47 | * can be divided by 16 or 256 |
| 48 | * and can be set up as a 32-bit timer |
| 49 | */ |
| 50 | /* U-Boot expects a 32 bit timer, running at CONFIG_SYS_HZ */ |
| 51 | /* Keep total timer count to avoid losing decrements < div_timer */ |
| 52 | static unsigned long long total_count = 0; |
| 53 | static unsigned long long lastdec; /* Timer reading at last call */ |
Jean-Christophe PLAGNIOL-VILLARD | 693a7ae | 2009-05-17 00:58:37 +0200 | [diff] [blame] | 54 | /* Divisor applied to timer clock */ |
| 55 | static unsigned long long div_clock = DIV_CLOCK_INIT; |
Jean-Christophe PLAGNIOL-VILLARD | 3c9dc3b | 2009-05-17 00:58:36 +0200 | [diff] [blame] | 56 | static unsigned long long div_timer = 1; /* Divisor to convert timer reading |
| 57 | * change to U-Boot ticks |
| 58 | */ |
| 59 | /* CONFIG_SYS_HZ = CONFIG_SYS_HZ_CLOCK/(div_clock * div_timer) */ |
Jean-Christophe PLAGNIOL-VILLARD | 693a7ae | 2009-05-17 00:58:37 +0200 | [diff] [blame] | 60 | static ulong timestamp; /* U-Boot ticks since startup */ |
Jean-Christophe PLAGNIOL-VILLARD | 3c9dc3b | 2009-05-17 00:58:36 +0200 | [diff] [blame] | 61 | |
Jean-Christophe PLAGNIOL-VILLARD | 3c9dc3b | 2009-05-17 00:58:36 +0200 | [diff] [blame] | 62 | #define READ_TIMER (*(volatile ulong *)(CONFIG_SYS_TIMERBASE+4)) |
| 63 | |
| 64 | /* all function return values in U-Boot ticks i.e. (1/CONFIG_SYS_HZ) sec |
| 65 | * - unless otherwise stated |
| 66 | */ |
| 67 | |
| 68 | /* starts up a counter |
| 69 | * - the Integrator/CP timer can be set up to issue an interrupt */ |
| 70 | int timer_init (void) |
| 71 | { |
| 72 | /* Load timer with initial value */ |
| 73 | *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 0) = TIMER_LOAD_VAL; |
Jean-Christophe PLAGNIOL-VILLARD | 693a7ae | 2009-05-17 00:58:37 +0200 | [diff] [blame] | 74 | #ifdef CONFIG_ARCH_CINTEGRATOR |
Jean-Christophe PLAGNIOL-VILLARD | 3c9dc3b | 2009-05-17 00:58:36 +0200 | [diff] [blame] | 75 | /* Set timer to be |
Jean-Christophe PLAGNIOL-VILLARD | 693a7ae | 2009-05-17 00:58:37 +0200 | [diff] [blame] | 76 | * enabled 1 |
| 77 | * periodic 1 |
| 78 | * no interrupts 0 |
| 79 | * X 0 |
| 80 | * divider 1 00 == less rounding error |
| 81 | * 32 bit 1 |
| 82 | * wrapping 0 |
Jean-Christophe PLAGNIOL-VILLARD | 3c9dc3b | 2009-05-17 00:58:36 +0200 | [diff] [blame] | 83 | */ |
| 84 | *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 8) = 0x000000C2; |
Jean-Christophe PLAGNIOL-VILLARD | 693a7ae | 2009-05-17 00:58:37 +0200 | [diff] [blame] | 85 | #else |
| 86 | /* Set timer to be |
| 87 | * enabled 1 |
| 88 | * free-running 0 |
| 89 | * XX 00 |
| 90 | * divider 256 10 |
| 91 | * XX 00 |
| 92 | */ |
| 93 | *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 8) = 0x00000088; |
| 94 | #endif |
| 95 | |
Jean-Christophe PLAGNIOL-VILLARD | 3c9dc3b | 2009-05-17 00:58:36 +0200 | [diff] [blame] | 96 | /* init the timestamp */ |
| 97 | total_count = 0ULL; |
Graeme Russ | 944a7fe | 2011-07-15 02:21:14 +0000 | [diff] [blame] | 98 | /* capure current decrementer value */ |
| 99 | lastdec = READ_TIMER; |
| 100 | /* start "advancing" time stamp from 0 */ |
| 101 | timestamp = 0L; |
Jean-Christophe PLAGNIOL-VILLARD | 3c9dc3b | 2009-05-17 00:58:36 +0200 | [diff] [blame] | 102 | |
Jean-Christophe PLAGNIOL-VILLARD | 693a7ae | 2009-05-17 00:58:37 +0200 | [diff] [blame] | 103 | div_timer = CONFIG_SYS_HZ_CLOCK; |
| 104 | do_div(div_timer, CONFIG_SYS_HZ); |
| 105 | do_div(div_timer, div_clock); |
Jean-Christophe PLAGNIOL-VILLARD | 3c9dc3b | 2009-05-17 00:58:36 +0200 | [diff] [blame] | 106 | |
| 107 | return (0); |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * timer without interrupts |
| 112 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 3c9dc3b | 2009-05-17 00:58:36 +0200 | [diff] [blame] | 113 | ulong get_timer (ulong base_ticks) |
| 114 | { |
| 115 | return get_timer_masked () - base_ticks; |
| 116 | } |
| 117 | |
Jean-Christophe PLAGNIOL-VILLARD | 3c9dc3b | 2009-05-17 00:58:36 +0200 | [diff] [blame] | 118 | /* delay usec useconds */ |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 119 | void __udelay (unsigned long usec) |
Jean-Christophe PLAGNIOL-VILLARD | 3c9dc3b | 2009-05-17 00:58:36 +0200 | [diff] [blame] | 120 | { |
| 121 | ulong tmo, tmp; |
| 122 | |
| 123 | /* Convert to U-Boot ticks */ |
| 124 | tmo = usec * CONFIG_SYS_HZ; |
| 125 | tmo /= (1000000L); |
| 126 | |
| 127 | tmp = get_timer_masked(); /* get current timestamp */ |
| 128 | tmo += tmp; /* form target timestamp */ |
| 129 | |
| 130 | while (get_timer_masked () < tmo) {/* loop till event */ |
| 131 | /*NOP*/; |
| 132 | } |
| 133 | } |
| 134 | |
Jean-Christophe PLAGNIOL-VILLARD | 3c9dc3b | 2009-05-17 00:58:36 +0200 | [diff] [blame] | 135 | /* converts the timer reading to U-Boot ticks */ |
| 136 | /* the timestamp is the number of ticks since reset */ |
| 137 | ulong get_timer_masked (void) |
| 138 | { |
| 139 | /* get current count */ |
Jean-Christophe PLAGNIOL-VILLARD | 693a7ae | 2009-05-17 00:58:37 +0200 | [diff] [blame] | 140 | unsigned long long now = READ_TIMER; |
Jean-Christophe PLAGNIOL-VILLARD | 3c9dc3b | 2009-05-17 00:58:36 +0200 | [diff] [blame] | 141 | |
| 142 | if(now > lastdec) { |
| 143 | /* Must have wrapped */ |
| 144 | total_count += lastdec + TIMER_LOAD_VAL + 1 - now; |
| 145 | } else { |
| 146 | total_count += lastdec - now; |
| 147 | } |
Jean-Christophe PLAGNIOL-VILLARD | 693a7ae | 2009-05-17 00:58:37 +0200 | [diff] [blame] | 148 | lastdec = now; |
Jean-Christophe PLAGNIOL-VILLARD | 3c9dc3b | 2009-05-17 00:58:36 +0200 | [diff] [blame] | 149 | |
| 150 | /* Reuse "now" */ |
| 151 | now = total_count; |
| 152 | do_div(now, div_timer); |
| 153 | timestamp = now; |
| 154 | |
| 155 | return timestamp; |
| 156 | } |
| 157 | |
| 158 | /* waits specified delay value and resets timestamp */ |
| 159 | void udelay_masked (unsigned long usec) |
| 160 | { |
| 161 | udelay(usec); |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * This function is derived from PowerPC code (read timebase as long long). |
| 166 | * On ARM it just returns the timer value. |
| 167 | */ |
| 168 | unsigned long long get_ticks(void) |
| 169 | { |
Jean-Christophe PLAGNIOL-VILLARD | 693a7ae | 2009-05-17 00:58:37 +0200 | [diff] [blame] | 170 | return get_timer(0); |
Jean-Christophe PLAGNIOL-VILLARD | 3c9dc3b | 2009-05-17 00:58:36 +0200 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /* |
| 174 | * Return the timebase clock frequency |
| 175 | * i.e. how often the timer decrements |
| 176 | */ |
| 177 | ulong get_tbclk (void) |
| 178 | { |
Jean-Christophe PLAGNIOL-VILLARD | 693a7ae | 2009-05-17 00:58:37 +0200 | [diff] [blame] | 179 | unsigned long long tmp = CONFIG_SYS_HZ_CLOCK; |
| 180 | |
| 181 | do_div(tmp, div_clock); |
| 182 | |
| 183 | return tmp; |
Jean-Christophe PLAGNIOL-VILLARD | 3c9dc3b | 2009-05-17 00:58:36 +0200 | [diff] [blame] | 184 | } |