wdenk | 281e00a | 2004-08-01 22:48:16 +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 | 792a09e | 2009-05-13 10:54:10 +0200 | [diff] [blame] | 11 | * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de> |
wdenk | 281e00a | 2004-08-01 22:48:16 +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> |
kevin.morfitt@fearnside-systems.co.uk | ac67804 | 2009-11-17 18:30:34 +0900 | [diff] [blame] | 33 | #ifdef CONFIG_S3C24X0 |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 34 | |
| 35 | #include <asm/io.h> |
kevin.morfitt@fearnside-systems.co.uk | ac67804 | 2009-11-17 18:30:34 +0900 | [diff] [blame] | 36 | #include <asm/arch/s3c24x0_cpu.h> |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 37 | |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 38 | int timer_load_val = 0; |
kevin.morfitt@fearnside-systems.co.uk | cd85662 | 2009-09-06 00:33:13 +0900 | [diff] [blame] | 39 | static ulong timer_clk; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 40 | |
| 41 | /* macro to read the 16 bit timer */ |
| 42 | static inline ulong READ_TIMER(void) |
| 43 | { |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 44 | struct s3c24x0_timers *timers = s3c24x0_get_base_timers(); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 45 | |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 46 | return readl(&timers->TCNTO4) & 0xffff; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | static ulong timestamp; |
| 50 | static ulong lastdec; |
| 51 | |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 52 | int timer_init(void) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 53 | { |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 54 | struct s3c24x0_timers *timers = s3c24x0_get_base_timers(); |
| 55 | ulong tmr; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 56 | |
| 57 | /* use PWM Timer 4 because it has no output */ |
| 58 | /* prescaler for Timer 4 is 16 */ |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 59 | writel(0x0f00, &timers->TCFG0); |
| 60 | if (timer_load_val == 0) { |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 61 | /* |
| 62 | * for 10 ms clock period @ PCLK with 4 bit divider = 1/2 |
| 63 | * (default) and prescaler = 16. Should be 10390 |
| 64 | * @33.25MHz and 15625 @ 50 MHz |
| 65 | */ |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 66 | timer_load_val = get_PCLK() / (2 * 16 * 100); |
kevin.morfitt@fearnside-systems.co.uk | cd85662 | 2009-09-06 00:33:13 +0900 | [diff] [blame] | 67 | timer_clk = get_PCLK() / (2 * 16); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 68 | } |
| 69 | /* load value for 10 ms timeout */ |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 70 | lastdec = timer_load_val; |
| 71 | writel(timer_load_val, &timers->TCNTB4); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 72 | /* auto load, manual update of Timer 4 */ |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 73 | tmr = (readl(&timers->TCON) & ~0x0700000) | 0x0600000; |
| 74 | writel(tmr, &timers->TCON); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 75 | /* auto load, start Timer 4 */ |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 76 | tmr = (tmr & ~0x0700000) | 0x0500000; |
| 77 | writel(tmr, &timers->TCON); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 78 | timestamp = 0; |
| 79 | |
| 80 | return (0); |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * timer without interrupts |
| 85 | */ |
| 86 | |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 87 | void reset_timer(void) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 88 | { |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 89 | reset_timer_masked(); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 90 | } |
| 91 | |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 92 | ulong get_timer(ulong base) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 93 | { |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 94 | return get_timer_masked() - base; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 95 | } |
| 96 | |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 97 | void set_timer(ulong t) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 98 | { |
| 99 | timestamp = t; |
| 100 | } |
| 101 | |
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 102 | void __udelay (unsigned long usec) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 103 | { |
| 104 | ulong tmo; |
kevin.morfitt@fearnside-systems.co.uk | cd85662 | 2009-09-06 00:33:13 +0900 | [diff] [blame] | 105 | ulong start = get_ticks(); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 106 | |
| 107 | tmo = usec / 1000; |
| 108 | tmo *= (timer_load_val * 100); |
| 109 | tmo /= 1000; |
| 110 | |
kevin.morfitt@fearnside-systems.co.uk | cd85662 | 2009-09-06 00:33:13 +0900 | [diff] [blame] | 111 | while ((ulong) (get_ticks() - start) < tmo) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 112 | /*NOP*/; |
| 113 | } |
| 114 | |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 115 | void reset_timer_masked(void) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 116 | { |
| 117 | /* reset time */ |
| 118 | lastdec = READ_TIMER(); |
| 119 | timestamp = 0; |
| 120 | } |
| 121 | |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 122 | ulong get_timer_masked(void) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 123 | { |
kevin.morfitt@fearnside-systems.co.uk | cd85662 | 2009-09-06 00:33:13 +0900 | [diff] [blame] | 124 | ulong tmr = get_ticks(); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 125 | |
kevin.morfitt@fearnside-systems.co.uk | cd85662 | 2009-09-06 00:33:13 +0900 | [diff] [blame] | 126 | return tmr / (timer_clk / CONFIG_SYS_HZ); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 127 | } |
| 128 | |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 129 | void udelay_masked(unsigned long usec) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 130 | { |
| 131 | ulong tmo; |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 132 | ulong endtime; |
| 133 | signed long diff; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 134 | |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 135 | if (usec >= 1000) { |
| 136 | tmo = usec / 1000; |
| 137 | tmo *= (timer_load_val * 100); |
| 138 | tmo /= 1000; |
| 139 | } else { |
| 140 | tmo = usec * (timer_load_val * 100); |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 141 | tmo /= (1000 * 1000); |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 142 | } |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 143 | |
kevin.morfitt@fearnside-systems.co.uk | cd85662 | 2009-09-06 00:33:13 +0900 | [diff] [blame] | 144 | endtime = get_ticks() + tmo; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 145 | |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 146 | do { |
kevin.morfitt@fearnside-systems.co.uk | cd85662 | 2009-09-06 00:33:13 +0900 | [diff] [blame] | 147 | ulong now = get_ticks(); |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 148 | diff = endtime - now; |
| 149 | } while (diff >= 0); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* |
| 153 | * This function is derived from PowerPC code (read timebase as long long). |
| 154 | * On ARM it just returns the timer value. |
| 155 | */ |
| 156 | unsigned long long get_ticks(void) |
| 157 | { |
kevin.morfitt@fearnside-systems.co.uk | cd85662 | 2009-09-06 00:33:13 +0900 | [diff] [blame] | 158 | ulong now = READ_TIMER(); |
| 159 | |
| 160 | if (lastdec >= now) { |
| 161 | /* normal mode */ |
| 162 | timestamp += lastdec - now; |
| 163 | } else { |
| 164 | /* we have an overflow ... */ |
| 165 | timestamp += lastdec + timer_load_val - now; |
| 166 | } |
| 167 | lastdec = now; |
| 168 | |
| 169 | return timestamp; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /* |
| 173 | * This function is derived from PowerPC code (timebase clock frequency). |
| 174 | * On ARM it returns the number of timer ticks per second. |
| 175 | */ |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 176 | ulong get_tbclk(void) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 177 | { |
| 178 | ulong tbclk; |
| 179 | |
| 180 | #if defined(CONFIG_SMDK2400) || defined(CONFIG_TRAB) |
| 181 | tbclk = timer_load_val * 100; |
Wolfgang Denk | 32cb2c7 | 2006-07-21 11:31:42 +0200 | [diff] [blame] | 182 | #elif defined(CONFIG_SBC2410X) || \ |
| 183 | defined(CONFIG_SMDK2410) || \ |
| 184 | defined(CONFIG_VCMA9) |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 185 | tbclk = CONFIG_SYS_HZ; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 186 | #else |
| 187 | # error "tbclk not configured" |
| 188 | #endif |
| 189 | |
| 190 | return tbclk; |
| 191 | } |
| 192 | |
wdenk | b304c96 | 2005-04-05 22:30:50 +0000 | [diff] [blame] | 193 | /* |
| 194 | * reset the cpu by setting up the watchdog timer and let him time out |
| 195 | */ |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 196 | void reset_cpu(ulong ignored) |
wdenk | b304c96 | 2005-04-05 22:30:50 +0000 | [diff] [blame] | 197 | { |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 198 | struct s3c24x0_watchdog *watchdog; |
wdenk | b304c96 | 2005-04-05 22:30:50 +0000 | [diff] [blame] | 199 | |
| 200 | #ifdef CONFIG_TRAB |
Wolfgang Denk | 4713010 | 2009-12-06 01:21:28 +0100 | [diff] [blame] | 201 | extern void disable_vfd(void); |
| 202 | |
wdenk | b304c96 | 2005-04-05 22:30:50 +0000 | [diff] [blame] | 203 | disable_vfd(); |
| 204 | #endif |
| 205 | |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 206 | watchdog = s3c24x0_get_base_watchdog(); |
wdenk | b304c96 | 2005-04-05 22:30:50 +0000 | [diff] [blame] | 207 | |
| 208 | /* Disable watchdog */ |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 209 | writel(0x0000, &watchdog->WTCON); |
wdenk | b304c96 | 2005-04-05 22:30:50 +0000 | [diff] [blame] | 210 | |
| 211 | /* Initialize watchdog timer count register */ |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 212 | writel(0x0001, &watchdog->WTCNT); |
wdenk | b304c96 | 2005-04-05 22:30:50 +0000 | [diff] [blame] | 213 | |
| 214 | /* Enable watchdog timer; assert reset at timer timeout */ |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 215 | writel(0x0021, &watchdog->WTCON); |
wdenk | b304c96 | 2005-04-05 22:30:50 +0000 | [diff] [blame] | 216 | |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 217 | while (1) |
| 218 | /* loop forever and wait for reset to happen */; |
wdenk | b304c96 | 2005-04-05 22:30:50 +0000 | [diff] [blame] | 219 | |
| 220 | /*NOTREACHED*/ |
| 221 | } |
| 222 | |
kevin.morfitt@fearnside-systems.co.uk | ac67804 | 2009-11-17 18:30:34 +0900 | [diff] [blame] | 223 | #endif /* CONFIG_S3C24X0 */ |