wdenk | 3c71176 | 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 |
Detlev Zundel | f1b3f2b | 2009-05-13 10:54:10 +0200 | [diff] [blame] | 11 | * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de> |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 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> |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 33 | #include <lh7a40x.h> |
| 34 | |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 35 | static ulong timer_load_val = 0; |
| 36 | |
| 37 | /* macro to read the 16 bit timer */ |
| 38 | static inline ulong READ_TIMER(void) |
| 39 | { |
wdenk | 37bdfdf | 2004-06-10 21:55:33 +0000 | [diff] [blame] | 40 | lh7a40x_timers_t* timers = LH7A40X_TIMERS_PTR; |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 41 | lh7a40x_timer_t* timer = &timers->timer1; |
| 42 | |
| 43 | return (timer->value & 0x0000ffff); |
| 44 | } |
| 45 | |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 46 | static ulong timestamp; |
| 47 | static ulong lastdec; |
| 48 | |
Jean-Christophe PLAGNIOL-VILLARD | 8c9fc00 | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 49 | int timer_init (void) |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 50 | { |
wdenk | 37bdfdf | 2004-06-10 21:55:33 +0000 | [diff] [blame] | 51 | lh7a40x_timers_t* timers = LH7A40X_TIMERS_PTR; |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 52 | lh7a40x_timer_t* timer = &timers->timer1; |
| 53 | |
| 54 | /* a periodic timer using the 508kHz source */ |
| 55 | timer->control = (TIMER_PER | TIMER_CLK508K); |
| 56 | |
| 57 | if (timer_load_val == 0) { |
| 58 | /* |
| 59 | * 10ms period with 508.469kHz clock = 5084 |
| 60 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 61 | timer_load_val = CONFIG_SYS_HZ/100; |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /* load value for 10 ms timeout */ |
| 65 | lastdec = timer->load = timer_load_val; |
| 66 | |
| 67 | /* auto load, start timer */ |
| 68 | timer->control = timer->control | TIMER_EN; |
| 69 | timestamp = 0; |
| 70 | |
| 71 | return (0); |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * timer without interrupts |
| 76 | */ |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 77 | ulong get_timer (ulong base) |
| 78 | { |
| 79 | return (get_timer_masked() - base); |
| 80 | } |
| 81 | |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 82 | void __udelay (unsigned long usec) |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 83 | { |
| 84 | ulong tmo,tmp; |
| 85 | |
| 86 | /* normalize */ |
| 87 | if (usec >= 1000) { |
| 88 | tmo = usec / 1000; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 89 | tmo *= CONFIG_SYS_HZ; |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 90 | tmo /= 1000; |
| 91 | } |
| 92 | else { |
| 93 | if (usec > 1) { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 94 | tmo = usec * CONFIG_SYS_HZ; |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 95 | tmo /= (1000*1000); |
| 96 | } |
| 97 | else |
| 98 | tmo = 1; |
| 99 | } |
| 100 | |
| 101 | /* check for rollover during this delay */ |
| 102 | tmp = get_timer (0); |
| 103 | if ((tmp + tmo) < tmp ) |
| 104 | reset_timer_masked(); /* timer would roll over */ |
| 105 | else |
| 106 | tmo += tmp; |
| 107 | |
| 108 | while (get_timer_masked () < tmo); |
| 109 | } |
| 110 | |
| 111 | void reset_timer_masked (void) |
| 112 | { |
| 113 | /* reset time */ |
| 114 | lastdec = READ_TIMER(); |
| 115 | timestamp = 0; |
| 116 | } |
| 117 | |
| 118 | ulong get_timer_masked (void) |
| 119 | { |
| 120 | ulong now = READ_TIMER(); |
| 121 | |
| 122 | if (lastdec >= now) { |
| 123 | /* normal mode */ |
| 124 | timestamp += (lastdec - now); |
| 125 | } else { |
| 126 | /* we have an overflow ... */ |
| 127 | timestamp += ((lastdec + timer_load_val) - now); |
| 128 | } |
| 129 | lastdec = now; |
| 130 | |
| 131 | return timestamp; |
| 132 | } |
| 133 | |
| 134 | void udelay_masked (unsigned long usec) |
| 135 | { |
| 136 | ulong tmo; |
wdenk | 7af1f9d | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 137 | ulong endtime; |
| 138 | signed long diff; |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 139 | |
| 140 | /* normalize */ |
| 141 | if (usec >= 1000) { |
| 142 | tmo = usec / 1000; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 143 | tmo *= CONFIG_SYS_HZ; |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 144 | tmo /= 1000; |
wdenk | 7af1f9d | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 145 | } else { |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 146 | if (usec > 1) { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 147 | tmo = usec * CONFIG_SYS_HZ; |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 148 | tmo /= (1000*1000); |
wdenk | 7af1f9d | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 149 | } else { |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 150 | tmo = 1; |
wdenk | 7af1f9d | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 151 | } |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 152 | } |
| 153 | |
wdenk | 7af1f9d | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 154 | endtime = get_timer_masked () + tmo; |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 155 | |
wdenk | 7af1f9d | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 156 | do { |
| 157 | ulong now = get_timer_masked (); |
| 158 | diff = endtime - now; |
| 159 | } while (diff >= 0); |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /* |
| 163 | * This function is derived from PowerPC code (read timebase as long long). |
| 164 | * On ARM it just returns the timer value. |
| 165 | */ |
| 166 | unsigned long long get_ticks(void) |
| 167 | { |
| 168 | return get_timer(0); |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * This function is derived from PowerPC code (timebase clock frequency). |
| 173 | * On ARM it returns the number of timer ticks per second. |
| 174 | */ |
| 175 | ulong get_tbclk (void) |
| 176 | { |
| 177 | ulong tbclk; |
| 178 | |
| 179 | tbclk = timer_load_val * 100; |
| 180 | |
| 181 | return tbclk; |
| 182 | } |