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