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