Wolfgang Denk | a48499f | 2008-04-11 15:11:26 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2004 |
| 3 | * Texas Instruments |
| 4 | * Richard Woodruff <r-woodruff2@ti.com> |
| 5 | * |
| 6 | * (C) Copyright 2002 |
| 7 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 8 | * Marius Groeger <mgroeger@sysgo.de> |
| 9 | * Alex Zuepke <azu@sysgo.de> |
| 10 | * |
| 11 | * (C) Copyright 2002 |
Detlev Zundel | f1b3f2b | 2009-05-13 10:54:10 +0200 | [diff] [blame] | 12 | * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de> |
Wolfgang Denk | a48499f | 2008-04-11 15:11:26 +0200 | [diff] [blame] | 13 | * |
| 14 | * See file CREDITS for list of people who contributed to this |
| 15 | * project. |
| 16 | * |
| 17 | * This program is free software; you can redistribute it and/or |
| 18 | * modify it under the terms of the GNU General Public License as |
| 19 | * published by the Free Software Foundation; either version 2 of |
| 20 | * the License, or (at your option) any later version. |
| 21 | * |
| 22 | * This program is distributed in the hope that it will be useful, |
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 | * GNU General Public License for more details. |
| 26 | * |
| 27 | * You should have received a copy of the GNU General Public License |
| 28 | * along with this program; if not, write to the Free Software |
| 29 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 30 | * MA 02111-1307 USA |
| 31 | */ |
| 32 | |
| 33 | #include <common.h> |
| 34 | #include <asm/arch/bits.h> |
Sascha Hauer | 7a19cea | 2008-03-26 20:40:36 +0100 | [diff] [blame] | 35 | #include <asm/arch/omap2420.h> |
Wolfgang Denk | a48499f | 2008-04-11 15:11:26 +0200 | [diff] [blame] | 36 | |
| 37 | #define TIMER_LOAD_VAL 0 |
| 38 | |
| 39 | /* macro to read the 32 bit timer */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 40 | #define READ_TIMER (*((volatile ulong *)(CONFIG_SYS_TIMERBASE+TCRR))) |
Wolfgang Denk | a48499f | 2008-04-11 15:11:26 +0200 | [diff] [blame] | 41 | |
Heiko Schocher | f201595 | 2010-12-09 22:01:15 +0000 | [diff] [blame] | 42 | DECLARE_GLOBAL_DATA_PTR; |
Wolfgang Denk | a48499f | 2008-04-11 15:11:26 +0200 | [diff] [blame] | 43 | |
Jean-Christophe PLAGNIOL-VILLARD | 8c9fc00 | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 44 | int timer_init (void) |
Wolfgang Denk | a48499f | 2008-04-11 15:11:26 +0200 | [diff] [blame] | 45 | { |
| 46 | int32_t val; |
| 47 | |
| 48 | /* Start the counter ticking up */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 49 | *((int32_t *) (CONFIG_SYS_TIMERBASE + TLDR)) = TIMER_LOAD_VAL; /* reload value on overflow*/ |
Ladislav Michl | 993e57d | 2009-03-30 18:58:41 +0200 | [diff] [blame] | 50 | val = (CONFIG_SYS_PTV << 2) | BIT5 | BIT1 | BIT0; /* mask to enable timer*/ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 51 | *((int32_t *) (CONFIG_SYS_TIMERBASE + TCLR)) = val; /* start timer */ |
Wolfgang Denk | a48499f | 2008-04-11 15:11:26 +0200 | [diff] [blame] | 52 | |
| 53 | reset_timer_masked(); /* init the timestamp and lastinc value */ |
| 54 | |
| 55 | return(0); |
| 56 | } |
| 57 | /* |
| 58 | * timer without interrupts |
| 59 | */ |
Wolfgang Denk | a48499f | 2008-04-11 15:11:26 +0200 | [diff] [blame] | 60 | ulong get_timer (ulong base) |
| 61 | { |
| 62 | return get_timer_masked () - base; |
| 63 | } |
| 64 | |
Wolfgang Denk | d7cb355 | 2010-05-21 23:13:18 +0200 | [diff] [blame] | 65 | /* delay x useconds AND preserve advance timestamp value */ |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 66 | void __udelay (unsigned long usec) |
Wolfgang Denk | a48499f | 2008-04-11 15:11:26 +0200 | [diff] [blame] | 67 | { |
| 68 | ulong tmo, tmp; |
| 69 | |
Wolfgang Denk | d7cb355 | 2010-05-21 23:13:18 +0200 | [diff] [blame] | 70 | if (usec >= 1000) { /* if "big" number, spread normalization to seconds */ |
| 71 | tmo = usec / 1000; /* start to normalize for usec to ticks per sec */ |
| 72 | tmo *= CONFIG_SYS_HZ; /* find number of "ticks" to wait to achieve target */ |
| 73 | tmo /= 1000; /* finish normalize. */ |
| 74 | } 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] | 75 | tmo = usec * CONFIG_SYS_HZ; |
Wolfgang Denk | a48499f | 2008-04-11 15:11:26 +0200 | [diff] [blame] | 76 | tmo /= (1000*1000); |
| 77 | } |
| 78 | |
| 79 | tmp = get_timer (0); /* get current timestamp */ |
Wolfgang Denk | d7cb355 | 2010-05-21 23:13:18 +0200 | [diff] [blame] | 80 | if ( (tmo + tmp + 1) < tmp ) /* if setting this forward will roll time stamp */ |
Wolfgang Denk | a48499f | 2008-04-11 15:11:26 +0200 | [diff] [blame] | 81 | reset_timer_masked (); /* reset "advancing" timestamp to 0, set lastinc value */ |
| 82 | else |
Wolfgang Denk | d7cb355 | 2010-05-21 23:13:18 +0200 | [diff] [blame] | 83 | tmo += tmp; /* else, set advancing stamp wake up time */ |
Wolfgang Denk | a48499f | 2008-04-11 15:11:26 +0200 | [diff] [blame] | 84 | while (get_timer_masked () < tmo)/* loop till event */ |
| 85 | /*NOP*/; |
| 86 | } |
| 87 | |
| 88 | void reset_timer_masked (void) |
| 89 | { |
| 90 | /* reset time */ |
Heiko Schocher | f201595 | 2010-12-09 22:01:15 +0000 | [diff] [blame] | 91 | gd->lastinc = READ_TIMER; /* capture current incrementer value time */ |
| 92 | gd->tbl = 0; /* start "advancing" time stamp from 0 */ |
Wolfgang Denk | a48499f | 2008-04-11 15:11:26 +0200 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | ulong get_timer_masked (void) |
| 96 | { |
| 97 | ulong now = READ_TIMER; /* current tick value */ |
| 98 | |
Heiko Schocher | f201595 | 2010-12-09 22:01:15 +0000 | [diff] [blame] | 99 | if (now >= gd->lastinc) /* normal mode (non roll) */ |
| 100 | gd->tbl += (now - gd->lastinc); /* move stamp fordward with absoulte diff ticks */ |
Wolfgang Denk | d7cb355 | 2010-05-21 23:13:18 +0200 | [diff] [blame] | 101 | else /* we have rollover of incrementer */ |
Heiko Schocher | f201595 | 2010-12-09 22:01:15 +0000 | [diff] [blame] | 102 | gd->tbl += (0xFFFFFFFF - gd->lastinc) + now; |
| 103 | gd->lastinc = now; |
| 104 | return gd->tbl; |
Wolfgang Denk | a48499f | 2008-04-11 15:11:26 +0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* waits specified delay value and resets timestamp */ |
| 108 | void udelay_masked (unsigned long usec) |
| 109 | { |
| 110 | ulong tmo; |
| 111 | ulong endtime; |
| 112 | signed long diff; |
| 113 | |
| 114 | if (usec >= 1000) { /* if "big" number, spread normalization to seconds */ |
| 115 | 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] | 116 | tmo *= CONFIG_SYS_HZ; /* find number of "ticks" to wait to achieve target */ |
Wolfgang Denk | a48499f | 2008-04-11 15:11:26 +0200 | [diff] [blame] | 117 | tmo /= 1000; /* finish normalize. */ |
| 118 | } 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] | 119 | tmo = usec * CONFIG_SYS_HZ; |
Wolfgang Denk | a48499f | 2008-04-11 15:11:26 +0200 | [diff] [blame] | 120 | tmo /= (1000*1000); |
| 121 | } |
| 122 | endtime = get_timer_masked () + tmo; |
| 123 | |
| 124 | do { |
| 125 | ulong now = get_timer_masked (); |
| 126 | diff = endtime - now; |
| 127 | } while (diff >= 0); |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * This function is derived from PowerPC code (read timebase as long long). |
| 132 | * On ARM it just returns the timer value. |
| 133 | */ |
| 134 | unsigned long long get_ticks(void) |
| 135 | { |
| 136 | return get_timer(0); |
| 137 | } |
| 138 | /* |
| 139 | * This function is derived from PowerPC code (timebase clock frequency). |
| 140 | * On ARM it returns the number of timer ticks per second. |
| 141 | */ |
| 142 | ulong get_tbclk (void) |
| 143 | { |
| 144 | ulong tbclk; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 145 | tbclk = CONFIG_SYS_HZ; |
Wolfgang Denk | a48499f | 2008-04-11 15:11:26 +0200 | [diff] [blame] | 146 | return tbclk; |
| 147 | } |