Wolfgang Denk | d86ec38 | 2006-03-13 12:37:35 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2003 |
| 3 | * Texas Instruments <www.ti.com> |
| 4 | * |
| 5 | * (C) Copyright 2002 |
| 6 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 7 | * Marius Groeger <mgroeger@sysgo.de> |
| 8 | * |
| 9 | * (C) Copyright 2002 |
| 10 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 11 | * Alex Zuepke <azu@sysgo.de> |
| 12 | * |
| 13 | * (C) Copyright 2002-2004 |
Detlev Zundel | f1b3f2b | 2009-05-13 10:54:10 +0200 | [diff] [blame] | 14 | * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de> |
Wolfgang Denk | d86ec38 | 2006-03-13 12:37:35 +0100 | [diff] [blame] | 15 | * |
| 16 | * (C) Copyright 2004 |
| 17 | * Philippe Robin, ARM Ltd. <philippe.robin@arm.com> |
| 18 | * |
Wolfgang Denk | bd8ec7e | 2013-10-07 13:07:26 +0200 | [diff] [blame] | 19 | * SPDX-License-Identifier: GPL-2.0+ |
Wolfgang Denk | d86ec38 | 2006-03-13 12:37:35 +0100 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include <common.h> |
Jon Hunter | e427c6f | 2013-04-09 16:41:33 -0500 | [diff] [blame] | 23 | #include <asm/io.h> |
Wolfgang Denk | d86ec38 | 2006-03-13 12:37:35 +0100 | [diff] [blame] | 24 | |
Jon Hunter | e427c6f | 2013-04-09 16:41:33 -0500 | [diff] [blame] | 25 | #define TIMER_CLOCK (CONFIG_SYS_CLK_FREQ / (2 << CONFIG_SYS_PTV)) |
| 26 | #define TIMER_LOAD_VAL 0xffffffff |
Wolfgang Denk | d86ec38 | 2006-03-13 12:37:35 +0100 | [diff] [blame] | 27 | |
| 28 | /* macro to read the 32 bit timer */ |
Jon Hunter | e427c6f | 2013-04-09 16:41:33 -0500 | [diff] [blame] | 29 | #define READ_TIMER readl(CONFIG_SYS_TIMERBASE+8) \ |
| 30 | / (TIMER_CLOCK / CONFIG_SYS_HZ) |
Wolfgang Denk | d86ec38 | 2006-03-13 12:37:35 +0100 | [diff] [blame] | 31 | |
Heiko Schocher | 5504dab | 2011-01-20 22:56:39 +0000 | [diff] [blame] | 32 | DECLARE_GLOBAL_DATA_PTR; |
| 33 | |
Simon Glass | 2655ee1 | 2012-12-13 20:48:34 +0000 | [diff] [blame] | 34 | #define timestamp gd->arch.tbl |
Simon Glass | a848da5 | 2012-12-13 20:48:35 +0000 | [diff] [blame] | 35 | #define lastdec gd->arch.lastinc |
Wolfgang Denk | d86ec38 | 2006-03-13 12:37:35 +0100 | [diff] [blame] | 36 | |
| 37 | int timer_init (void) |
| 38 | { |
| 39 | int32_t val; |
| 40 | |
| 41 | /* Start the decrementer ticking down from 0xffffffff */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 42 | *((int32_t *) (CONFIG_SYS_TIMERBASE + LOAD_TIM)) = TIMER_LOAD_VAL; |
Ladislav Michl | 993e57d | 2009-03-30 18:58:41 +0200 | [diff] [blame] | 43 | val = MPUTIM_ST | MPUTIM_AR | MPUTIM_CLOCK_ENABLE | (CONFIG_SYS_PTV << MPUTIM_PTV_BIT); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 44 | *((int32_t *) (CONFIG_SYS_TIMERBASE + CNTL_TIMER)) = val; |
Wolfgang Denk | d86ec38 | 2006-03-13 12:37:35 +0100 | [diff] [blame] | 45 | |
| 46 | /* init the timestamp and lastdec value */ |
| 47 | reset_timer_masked(); |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | * timer without interrupts |
| 54 | */ |
Wolfgang Denk | d86ec38 | 2006-03-13 12:37:35 +0100 | [diff] [blame] | 55 | ulong get_timer (ulong base) |
| 56 | { |
| 57 | return get_timer_masked () - base; |
| 58 | } |
| 59 | |
Wolfgang Denk | d7cb355 | 2010-05-21 23:13:18 +0200 | [diff] [blame] | 60 | /* delay x useconds AND preserve advance timestamp value */ |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 61 | void __udelay (unsigned long usec) |
Wolfgang Denk | d86ec38 | 2006-03-13 12:37:35 +0100 | [diff] [blame] | 62 | { |
| 63 | ulong tmo, tmp; |
| 64 | |
| 65 | if(usec >= 1000){ /* if "big" number, spread normalization to seconds */ |
| 66 | tmo = usec / 1000; /* start to normalize for usec to ticks per sec */ |
Wolfgang Denk | d7cb355 | 2010-05-21 23:13:18 +0200 | [diff] [blame] | 67 | tmo *= CONFIG_SYS_HZ; /* find number of "ticks" to wait to achieve target */ |
Wolfgang Denk | d86ec38 | 2006-03-13 12:37:35 +0100 | [diff] [blame] | 68 | tmo /= 1000; /* finish normalize. */ |
| 69 | }else{ /* else small number, don't kill it prior to HZ multiply */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 70 | tmo = usec * CONFIG_SYS_HZ; |
Wolfgang Denk | d86ec38 | 2006-03-13 12:37:35 +0100 | [diff] [blame] | 71 | tmo /= (1000*1000); |
| 72 | } |
| 73 | |
| 74 | tmp = get_timer (0); /* get current timestamp */ |
| 75 | if( (tmo + tmp + 1) < tmp ) /* if setting this fordward will roll time stamp */ |
| 76 | reset_timer_masked (); /* reset "advancing" timestamp to 0, set lastdec value */ |
| 77 | else |
| 78 | tmo += tmp; /* else, set advancing stamp wake up time */ |
| 79 | |
| 80 | while (get_timer_masked () < tmo)/* loop till event */ |
| 81 | /*NOP*/; |
| 82 | } |
| 83 | |
| 84 | void reset_timer_masked (void) |
| 85 | { |
| 86 | /* reset time */ |
| 87 | lastdec = READ_TIMER; /* capure current decrementer value time */ |
| 88 | timestamp = 0; /* start "advancing" time stamp from 0 */ |
| 89 | } |
| 90 | |
| 91 | ulong get_timer_masked (void) |
| 92 | { |
| 93 | ulong now = READ_TIMER; /* current tick value */ |
| 94 | |
| 95 | if (lastdec >= now) { /* normal mode (non roll) */ |
| 96 | /* normal mode */ |
| 97 | timestamp += lastdec - now; /* move stamp fordward with absoulte diff ticks */ |
| 98 | } else { /* we have overflow of the count down timer */ |
| 99 | /* nts = ts + ld + (TLV - now) |
| 100 | * ts=old stamp, ld=time that passed before passing through -1 |
| 101 | * (TLV-now) amount of time after passing though -1 |
| 102 | * nts = new "advancing time stamp"...it could also roll and cause problems. |
| 103 | */ |
Jon Hunter | e427c6f | 2013-04-09 16:41:33 -0500 | [diff] [blame] | 104 | timestamp += lastdec + (TIMER_LOAD_VAL / (TIMER_CLOCK / |
| 105 | CONFIG_SYS_HZ)) - now; |
Wolfgang Denk | d86ec38 | 2006-03-13 12:37:35 +0100 | [diff] [blame] | 106 | } |
| 107 | lastdec = now; |
| 108 | |
| 109 | return timestamp; |
| 110 | } |
| 111 | |
| 112 | /* waits specified delay value and resets timestamp */ |
| 113 | void udelay_masked (unsigned long usec) |
| 114 | { |
| 115 | ulong tmo; |
| 116 | ulong endtime; |
| 117 | signed long diff; |
| 118 | |
| 119 | if (usec >= 1000) { /* if "big" number, spread normalization to seconds */ |
| 120 | tmo = usec / 1000; /* start to normalize for usec to ticks per sec */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 121 | tmo *= CONFIG_SYS_HZ; /* find number of "ticks" to wait to achieve target */ |
Wolfgang Denk | d86ec38 | 2006-03-13 12:37:35 +0100 | [diff] [blame] | 122 | tmo /= 1000; /* finish normalize. */ |
| 123 | } else { /* else small number, don't kill it prior to HZ multiply */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 124 | tmo = usec * CONFIG_SYS_HZ; |
Wolfgang Denk | d86ec38 | 2006-03-13 12:37:35 +0100 | [diff] [blame] | 125 | tmo /= (1000*1000); |
| 126 | } |
| 127 | |
| 128 | endtime = get_timer_masked () + tmo; |
| 129 | |
| 130 | do { |
| 131 | ulong now = get_timer_masked (); |
| 132 | diff = endtime - now; |
| 133 | } while (diff >= 0); |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * This function is derived from PowerPC code (read timebase as long long). |
| 138 | * On ARM it just returns the timer value. |
| 139 | */ |
| 140 | unsigned long long get_ticks(void) |
| 141 | { |
| 142 | return get_timer(0); |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * This function is derived from PowerPC code (timebase clock frequency). |
| 147 | * On ARM it returns the number of timer ticks per second. |
| 148 | */ |
| 149 | ulong get_tbclk (void) |
| 150 | { |
Jon Hunter | e427c6f | 2013-04-09 16:41:33 -0500 | [diff] [blame] | 151 | return CONFIG_SYS_HZ; |
Wolfgang Denk | d86ec38 | 2006-03-13 12:37:35 +0100 | [diff] [blame] | 152 | } |