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