wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 1 | /* |
wdenk | 2e405bf | 2005-01-10 00:01:04 +0000 | [diff] [blame] | 2 | * (C) Copyright 2004 |
| 3 | * Texas Instruments |
| 4 | * Richard Woodruff <r-woodruff2@ti.com> |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 5 | * |
| 6 | * (C) Copyright 2002 |
| 7 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 8 | * Marius Groeger <mgroeger@sysgo.de> |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 9 | * Alex Zuepke <azu@sysgo.de> |
| 10 | * |
| 11 | * (C) Copyright 2002 |
| 12 | * Gary Jennejohn, DENX Software Engineering, <gj@denx.de> |
| 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> |
Wolfgang Denk | 7f88a5e | 2005-10-06 17:08:18 +0200 | [diff] [blame] | 35 | |
Wolfgang Denk | 88bd743 | 2005-10-09 00:22:48 +0200 | [diff] [blame] | 36 | #if !defined(CONFIG_INTEGRATOR) && ! defined(CONFIG_ARCH_CINTEGRATOR) |
Wolfgang Denk | 7f88a5e | 2005-10-06 17:08:18 +0200 | [diff] [blame] | 37 | # include <asm/arch/omap2420.h> |
| 38 | #endif |
| 39 | |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 40 | #define TIMER_LOAD_VAL 0 |
| 41 | |
| 42 | /* macro to read the 32 bit timer */ |
| 43 | #define READ_TIMER (*(volatile ulong *)(CFG_TIMERBASE+TCRR)) |
| 44 | |
Wolfgang Denk | 7f88a5e | 2005-10-06 17:08:18 +0200 | [diff] [blame] | 45 | #if defined(CONFIG_INTEGRATOR) && defined(CONFIG_ARCH_CINTEGRATOR) |
| 46 | /* Use the IntegratorCP function from board/integratorcp.c */ |
| 47 | #else |
Wolfgang Denk | 88bd743 | 2005-10-09 00:22:48 +0200 | [diff] [blame] | 48 | |
| 49 | static ulong timestamp; |
| 50 | static ulong lastinc; |
| 51 | |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 52 | /* nothing really to do with interrupts, just starts up a counter. */ |
| 53 | int interrupt_init (void) |
| 54 | { |
| 55 | int32_t val; |
| 56 | |
| 57 | /* Start the counter ticking up */ |
| 58 | *((int32_t *) (CFG_TIMERBASE + TLDR)) = TIMER_LOAD_VAL; /* reload value on overflow*/ |
| 59 | val = (CFG_PVT << 2) | BIT5 | BIT1 | BIT0; /* mask to enable timer*/ |
| 60 | *((int32_t *) (CFG_TIMERBASE + TCLR)) = val; /* start timer */ |
| 61 | |
| 62 | reset_timer_masked(); /* init the timestamp and lastinc value */ |
| 63 | |
| 64 | return(0); |
| 65 | } |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 66 | /* |
| 67 | * timer without interrupts |
| 68 | */ |
| 69 | void reset_timer (void) |
| 70 | { |
| 71 | reset_timer_masked (); |
| 72 | } |
| 73 | |
| 74 | ulong get_timer (ulong base) |
| 75 | { |
| 76 | return get_timer_masked () - base; |
| 77 | } |
| 78 | |
| 79 | void set_timer (ulong t) |
| 80 | { |
| 81 | timestamp = t; |
| 82 | } |
| 83 | |
| 84 | /* delay x useconds AND perserve advance timstamp value */ |
| 85 | void udelay (unsigned long usec) |
| 86 | { |
| 87 | ulong tmo, tmp; |
| 88 | |
| 89 | if (usec >= 1000) { /* if "big" number, spread normalization to seconds */ |
| 90 | tmo = usec / 1000; /* start to normalize for usec to ticks per sec */ |
| 91 | tmo *= CFG_HZ; /* find number of "ticks" to wait to achieve target */ |
| 92 | tmo /= 1000; /* finish normalize. */ |
| 93 | } else { /* else small number, don't kill it prior to HZ multiply */ |
| 94 | tmo = usec * CFG_HZ; |
| 95 | tmo /= (1000*1000); |
| 96 | } |
| 97 | |
| 98 | tmp = get_timer (0); /* get current timestamp */ |
| 99 | if ( (tmo + tmp + 1) < tmp )/* if setting this forward will roll time stamp */ |
| 100 | reset_timer_masked (); /* reset "advancing" timestamp to 0, set lastinc value */ |
| 101 | else |
| 102 | tmo += tmp; /* else, set advancing stamp wake up time */ |
| 103 | while (get_timer_masked () < tmo)/* loop till event */ |
| 104 | /*NOP*/; |
| 105 | } |
| 106 | |
| 107 | void reset_timer_masked (void) |
| 108 | { |
| 109 | /* reset time */ |
| 110 | lastinc = READ_TIMER; /* capture current incrementer value time */ |
| 111 | timestamp = 0; /* start "advancing" time stamp from 0 */ |
| 112 | } |
| 113 | |
| 114 | ulong get_timer_masked (void) |
| 115 | { |
| 116 | ulong now = READ_TIMER; /* current tick value */ |
| 117 | |
| 118 | if (now >= lastinc) /* normal mode (non roll) */ |
| 119 | timestamp += (now - lastinc); /* move stamp fordward with absoulte diff ticks */ |
| 120 | else /* we have rollover of incrementer */ |
| 121 | timestamp += (0xFFFFFFFF - lastinc) + now; |
| 122 | lastinc = now; |
| 123 | return timestamp; |
| 124 | } |
| 125 | |
| 126 | /* waits specified delay value and resets timestamp */ |
| 127 | void udelay_masked (unsigned long usec) |
| 128 | { |
| 129 | ulong tmo; |
wdenk | 7af1f9d | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 130 | ulong endtime; |
| 131 | signed long diff; |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 132 | |
| 133 | if (usec >= 1000) { /* if "big" number, spread normalization to seconds */ |
| 134 | tmo = usec / 1000; /* start to normalize for usec to ticks per sec */ |
| 135 | tmo *= CFG_HZ; /* find number of "ticks" to wait to achieve target */ |
| 136 | tmo /= 1000; /* finish normalize. */ |
| 137 | } else { /* else small number, don't kill it prior to HZ multiply */ |
| 138 | tmo = usec * CFG_HZ; |
| 139 | tmo /= (1000*1000); |
| 140 | } |
wdenk | 7af1f9d | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 141 | endtime = get_timer_masked () + tmo; |
| 142 | |
| 143 | do { |
| 144 | ulong now = get_timer_masked (); |
| 145 | diff = endtime - now; |
| 146 | } while (diff >= 0); |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /* |
| 150 | * This function is derived from PowerPC code (read timebase as long long). |
| 151 | * On ARM it just returns the timer value. |
| 152 | */ |
| 153 | unsigned long long get_ticks(void) |
| 154 | { |
| 155 | return get_timer(0); |
| 156 | } |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 157 | /* |
| 158 | * This function is derived from PowerPC code (timebase clock frequency). |
| 159 | * On ARM it returns the number of timer ticks per second. |
| 160 | */ |
| 161 | ulong get_tbclk (void) |
| 162 | { |
| 163 | ulong tbclk; |
| 164 | tbclk = CFG_HZ; |
| 165 | return tbclk; |
| 166 | } |
Wolfgang Denk | 7f88a5e | 2005-10-06 17:08:18 +0200 | [diff] [blame] | 167 | #endif /* !Integrator/CP */ |