wdenk | f39748a | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 4 | * Marius Groeger <mgroeger@sysgo.de> |
| 5 | * |
| 6 | * (C) Copyright 2002 |
| 7 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 8 | * Alex Zuepke <azu@sysgo.de> |
| 9 | * |
| 10 | * (C) Copyright 2002 |
| 11 | * Gary Jennejohn, DENX Software Engineering, <gj@denx.de> |
| 12 | * |
| 13 | * See file CREDITS for list of people who contributed to this |
| 14 | * project. |
| 15 | * |
| 16 | * This program is free software; you can redistribute it and/or |
| 17 | * modify it under the terms of the GNU General Public License as |
| 18 | * published by the Free Software Foundation; either version 2 of |
| 19 | * the License, or (at your option) any later version. |
| 20 | * |
| 21 | * This program is distributed in the hope that it will be useful, |
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | * GNU General Public License for more details. |
| 25 | * |
| 26 | * You should have received a copy of the GNU General Public License |
| 27 | * along with this program; if not, write to the Free Software |
| 28 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 29 | * MA 02111-1307 USA |
| 30 | */ |
| 31 | |
| 32 | #include <common.h> |
| 33 | #include <arm920t.h> |
| 34 | #include <lh7a40x.h> |
| 35 | |
wdenk | f39748a | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 36 | static ulong timer_load_val = 0; |
| 37 | |
| 38 | /* macro to read the 16 bit timer */ |
| 39 | static inline ulong READ_TIMER(void) |
| 40 | { |
wdenk | f832d8a | 2004-06-10 21:55:33 +0000 | [diff] [blame] | 41 | lh7a40x_timers_t* timers = LH7A40X_TIMERS_PTR; |
wdenk | f39748a | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 42 | lh7a40x_timer_t* timer = &timers->timer1; |
| 43 | |
| 44 | return (timer->value & 0x0000ffff); |
| 45 | } |
| 46 | |
wdenk | f39748a | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 47 | static ulong timestamp; |
| 48 | static ulong lastdec; |
| 49 | |
| 50 | int interrupt_init (void) |
| 51 | { |
wdenk | f832d8a | 2004-06-10 21:55:33 +0000 | [diff] [blame] | 52 | lh7a40x_timers_t* timers = LH7A40X_TIMERS_PTR; |
wdenk | f39748a | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 53 | lh7a40x_timer_t* timer = &timers->timer1; |
| 54 | |
| 55 | /* a periodic timer using the 508kHz source */ |
| 56 | timer->control = (TIMER_PER | TIMER_CLK508K); |
| 57 | |
| 58 | if (timer_load_val == 0) { |
| 59 | /* |
| 60 | * 10ms period with 508.469kHz clock = 5084 |
| 61 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame^] | 62 | timer_load_val = CONFIG_SYS_HZ/100; |
wdenk | f39748a | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /* load value for 10 ms timeout */ |
| 66 | lastdec = timer->load = timer_load_val; |
| 67 | |
| 68 | /* auto load, start timer */ |
| 69 | timer->control = timer->control | TIMER_EN; |
| 70 | timestamp = 0; |
| 71 | |
| 72 | return (0); |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * timer without interrupts |
| 77 | */ |
| 78 | |
| 79 | void reset_timer (void) |
| 80 | { |
| 81 | reset_timer_masked (); |
| 82 | } |
| 83 | |
| 84 | ulong get_timer (ulong base) |
| 85 | { |
| 86 | return (get_timer_masked() - base); |
| 87 | } |
| 88 | |
| 89 | void set_timer (ulong t) |
| 90 | { |
| 91 | timestamp = t; |
| 92 | } |
| 93 | |
| 94 | void udelay (unsigned long usec) |
| 95 | { |
| 96 | ulong tmo,tmp; |
| 97 | |
| 98 | /* normalize */ |
| 99 | if (usec >= 1000) { |
| 100 | tmo = usec / 1000; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame^] | 101 | tmo *= CONFIG_SYS_HZ; |
wdenk | f39748a | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 102 | tmo /= 1000; |
| 103 | } |
| 104 | else { |
| 105 | if (usec > 1) { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame^] | 106 | tmo = usec * CONFIG_SYS_HZ; |
wdenk | f39748a | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 107 | tmo /= (1000*1000); |
| 108 | } |
| 109 | else |
| 110 | tmo = 1; |
| 111 | } |
| 112 | |
| 113 | /* check for rollover during this delay */ |
| 114 | tmp = get_timer (0); |
| 115 | if ((tmp + tmo) < tmp ) |
| 116 | reset_timer_masked(); /* timer would roll over */ |
| 117 | else |
| 118 | tmo += tmp; |
| 119 | |
| 120 | while (get_timer_masked () < tmo); |
| 121 | } |
| 122 | |
| 123 | void reset_timer_masked (void) |
| 124 | { |
| 125 | /* reset time */ |
| 126 | lastdec = READ_TIMER(); |
| 127 | timestamp = 0; |
| 128 | } |
| 129 | |
| 130 | ulong get_timer_masked (void) |
| 131 | { |
| 132 | ulong now = READ_TIMER(); |
| 133 | |
| 134 | if (lastdec >= now) { |
| 135 | /* normal mode */ |
| 136 | timestamp += (lastdec - now); |
| 137 | } else { |
| 138 | /* we have an overflow ... */ |
| 139 | timestamp += ((lastdec + timer_load_val) - now); |
| 140 | } |
| 141 | lastdec = now; |
| 142 | |
| 143 | return timestamp; |
| 144 | } |
| 145 | |
| 146 | void udelay_masked (unsigned long usec) |
| 147 | { |
| 148 | ulong tmo; |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 149 | ulong endtime; |
| 150 | signed long diff; |
wdenk | f39748a | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 151 | |
| 152 | /* normalize */ |
| 153 | if (usec >= 1000) { |
| 154 | tmo = usec / 1000; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame^] | 155 | tmo *= CONFIG_SYS_HZ; |
wdenk | f39748a | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 156 | tmo /= 1000; |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 157 | } else { |
wdenk | f39748a | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 158 | if (usec > 1) { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame^] | 159 | tmo = usec * CONFIG_SYS_HZ; |
wdenk | f39748a | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 160 | tmo /= (1000*1000); |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 161 | } else { |
wdenk | f39748a | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 162 | tmo = 1; |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 163 | } |
wdenk | f39748a | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 164 | } |
| 165 | |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 166 | endtime = get_timer_masked () + tmo; |
wdenk | f39748a | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 167 | |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 168 | do { |
| 169 | ulong now = get_timer_masked (); |
| 170 | diff = endtime - now; |
| 171 | } while (diff >= 0); |
wdenk | f39748a | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /* |
| 175 | * This function is derived from PowerPC code (read timebase as long long). |
| 176 | * On ARM it just returns the timer value. |
| 177 | */ |
| 178 | unsigned long long get_ticks(void) |
| 179 | { |
| 180 | return get_timer(0); |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * This function is derived from PowerPC code (timebase clock frequency). |
| 185 | * On ARM it returns the number of timer ticks per second. |
| 186 | */ |
| 187 | ulong get_tbclk (void) |
| 188 | { |
| 189 | ulong tbclk; |
| 190 | |
| 191 | tbclk = timer_load_val * 100; |
| 192 | |
| 193 | return tbclk; |
| 194 | } |