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> |
Wolfgang Denk | d86ec38 | 2006-03-13 12:37:35 +0100 | [diff] [blame] | 23 | |
| 24 | #define TIMER_LOAD_VAL 0xffffffff |
| 25 | |
| 26 | /* macro to read the 32 bit timer */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 27 | #define READ_TIMER (*(volatile ulong *)(CONFIG_SYS_TIMERBASE+4)) |
Wolfgang Denk | d86ec38 | 2006-03-13 12:37:35 +0100 | [diff] [blame] | 28 | |
Heiko Schocher | 5504dab | 2011-01-20 22:56:39 +0000 | [diff] [blame] | 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | |
Simon Glass | 2655ee1 | 2012-12-13 20:48:34 +0000 | [diff] [blame] | 31 | #define timestamp gd->arch.tbl |
Simon Glass | a848da5 | 2012-12-13 20:48:35 +0000 | [diff] [blame] | 32 | #define lastdec gd->arch.lastinc |
Wolfgang Denk | d86ec38 | 2006-03-13 12:37:35 +0100 | [diff] [blame] | 33 | |
Gururaja Hebbar K R | b675aa3 | 2008-08-25 11:11:34 +0200 | [diff] [blame] | 34 | #define TIMER_ENABLE (1 << 7) |
| 35 | #define TIMER_MODE_MSK (1 << 6) |
| 36 | #define TIMER_MODE_FR (0 << 6) |
| 37 | #define TIMER_MODE_PD (1 << 6) |
| 38 | |
| 39 | #define TIMER_INT_EN (1 << 5) |
| 40 | #define TIMER_PRS_MSK (3 << 2) |
| 41 | #define TIMER_PRS_8S (1 << 3) |
| 42 | #define TIMER_SIZE_MSK (1 << 2) |
| 43 | #define TIMER_ONE_SHT (1 << 0) |
| 44 | |
Wolfgang Denk | d86ec38 | 2006-03-13 12:37:35 +0100 | [diff] [blame] | 45 | int timer_init (void) |
| 46 | { |
Gururaja Hebbar K R | b675aa3 | 2008-08-25 11:11:34 +0200 | [diff] [blame] | 47 | ulong tmr_ctrl_val; |
| 48 | |
| 49 | /* 1st disable the Timer */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 50 | tmr_ctrl_val = *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 8); |
Gururaja Hebbar K R | b675aa3 | 2008-08-25 11:11:34 +0200 | [diff] [blame] | 51 | tmr_ctrl_val &= ~TIMER_ENABLE; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 52 | *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 8) = tmr_ctrl_val; |
Gururaja Hebbar K R | b675aa3 | 2008-08-25 11:11:34 +0200 | [diff] [blame] | 53 | |
| 54 | /* |
| 55 | * The Timer Control Register has one Undefined/Shouldn't Use Bit |
| 56 | * So we should do read/modify/write Operation |
| 57 | */ |
| 58 | |
| 59 | /* |
| 60 | * Timer Mode : Free Running |
| 61 | * Interrupt : Disabled |
| 62 | * Prescale : 8 Stage, Clk/256 |
| 63 | * Tmr Siz : 16 Bit Counter |
| 64 | * Tmr in Wrapping Mode |
| 65 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 66 | tmr_ctrl_val = *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 8); |
Gururaja Hebbar K R | b675aa3 | 2008-08-25 11:11:34 +0200 | [diff] [blame] | 67 | tmr_ctrl_val &= ~(TIMER_MODE_MSK | TIMER_INT_EN | TIMER_PRS_MSK | TIMER_SIZE_MSK | TIMER_ONE_SHT ); |
| 68 | tmr_ctrl_val |= (TIMER_ENABLE | TIMER_PRS_8S); |
| 69 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 70 | *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 8) = tmr_ctrl_val; |
Wolfgang Denk | d86ec38 | 2006-03-13 12:37:35 +0100 | [diff] [blame] | 71 | |
| 72 | /* init the timestamp and lastdec value */ |
| 73 | reset_timer_masked(); |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * timer without interrupts |
| 80 | */ |
Wolfgang Denk | d86ec38 | 2006-03-13 12:37:35 +0100 | [diff] [blame] | 81 | ulong get_timer (ulong base) |
| 82 | { |
| 83 | return get_timer_masked () - base; |
| 84 | } |
| 85 | |
Wolfgang Denk | d7cb355 | 2010-05-21 23:13:18 +0200 | [diff] [blame] | 86 | /* delay x useconds AND preserve advance timestamp value */ |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 87 | void __udelay (unsigned long usec) |
Wolfgang Denk | d86ec38 | 2006-03-13 12:37:35 +0100 | [diff] [blame] | 88 | { |
| 89 | ulong tmo, tmp; |
| 90 | |
| 91 | if(usec >= 1000){ /* if "big" number, spread normalization to seconds */ |
| 92 | tmo = usec / 1000; /* start to normalize for usec to ticks per sec */ |
Wolfgang Denk | d7cb355 | 2010-05-21 23:13:18 +0200 | [diff] [blame] | 93 | 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] | 94 | tmo /= 1000; /* finish normalize. */ |
| 95 | }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] | 96 | tmo = usec * CONFIG_SYS_HZ; |
Wolfgang Denk | d86ec38 | 2006-03-13 12:37:35 +0100 | [diff] [blame] | 97 | tmo /= (1000*1000); |
| 98 | } |
| 99 | |
| 100 | tmp = get_timer (0); /* get current timestamp */ |
| 101 | if( (tmo + tmp + 1) < tmp ) /* if setting this fordward will roll time stamp */ |
| 102 | reset_timer_masked (); /* reset "advancing" timestamp to 0, set lastdec value */ |
| 103 | else |
| 104 | tmo += tmp; /* else, set advancing stamp wake up time */ |
| 105 | |
| 106 | while (get_timer_masked () < tmo)/* loop till event */ |
| 107 | /*NOP*/; |
| 108 | } |
| 109 | |
| 110 | void reset_timer_masked (void) |
| 111 | { |
| 112 | /* reset time */ |
| 113 | lastdec = READ_TIMER; /* capure current decrementer value time */ |
| 114 | timestamp = 0; /* start "advancing" time stamp from 0 */ |
| 115 | } |
| 116 | |
| 117 | ulong get_timer_masked (void) |
| 118 | { |
| 119 | ulong now = READ_TIMER; /* current tick value */ |
| 120 | |
| 121 | if (lastdec >= now) { /* normal mode (non roll) */ |
| 122 | /* normal mode */ |
| 123 | timestamp += lastdec - now; /* move stamp fordward with absoulte diff ticks */ |
| 124 | } else { /* we have overflow of the count down timer */ |
| 125 | /* nts = ts + ld + (TLV - now) |
| 126 | * ts=old stamp, ld=time that passed before passing through -1 |
| 127 | * (TLV-now) amount of time after passing though -1 |
| 128 | * nts = new "advancing time stamp"...it could also roll and cause problems. |
| 129 | */ |
| 130 | timestamp += lastdec + TIMER_LOAD_VAL - now; |
| 131 | } |
| 132 | lastdec = now; |
| 133 | |
| 134 | return timestamp; |
| 135 | } |
| 136 | |
| 137 | /* waits specified delay value and resets timestamp */ |
| 138 | void udelay_masked (unsigned long usec) |
| 139 | { |
| 140 | ulong tmo; |
| 141 | ulong endtime; |
| 142 | signed long diff; |
| 143 | |
| 144 | if (usec >= 1000) { /* if "big" number, spread normalization to seconds */ |
| 145 | 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] | 146 | 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] | 147 | tmo /= 1000; /* finish normalize. */ |
| 148 | } 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] | 149 | tmo = usec * CONFIG_SYS_HZ; |
Wolfgang Denk | d86ec38 | 2006-03-13 12:37:35 +0100 | [diff] [blame] | 150 | tmo /= (1000*1000); |
| 151 | } |
| 152 | |
| 153 | endtime = get_timer_masked () + tmo; |
| 154 | |
| 155 | do { |
| 156 | ulong now = get_timer_masked (); |
| 157 | diff = endtime - now; |
| 158 | } while (diff >= 0); |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * This function is derived from PowerPC code (read timebase as long long). |
| 163 | * On ARM it just returns the timer value. |
| 164 | */ |
| 165 | unsigned long long get_ticks(void) |
| 166 | { |
| 167 | return get_timer(0); |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * This function is derived from PowerPC code (timebase clock frequency). |
| 172 | * On ARM it returns the number of timer ticks per second. |
| 173 | */ |
| 174 | ulong get_tbclk (void) |
| 175 | { |
| 176 | ulong tbclk; |
| 177 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 178 | tbclk = CONFIG_SYS_HZ; |
Wolfgang Denk | d86ec38 | 2006-03-13 12:37:35 +0100 | [diff] [blame] | 179 | return tbclk; |
| 180 | } |