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 | |
David Müller (ELSOFT AG) | d0b3b49 | 2011-12-22 01:16:37 +0000 | [diff] [blame] | 38 | DECLARE_GLOBAL_DATA_PTR; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 39 | |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 40 | int timer_init(void) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 41 | { |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 42 | struct s3c24x0_timers *timers = s3c24x0_get_base_timers(); |
| 43 | ulong tmr; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 44 | |
| 45 | /* use PWM Timer 4 because it has no output */ |
| 46 | /* prescaler for Timer 4 is 16 */ |
C Nauman | d9abba8 | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 47 | writel(0x0f00, &timers->tcfg0); |
David Müller (ELSOFT AG) | d0b3b49 | 2011-12-22 01:16:37 +0000 | [diff] [blame] | 48 | if (gd->tbu == 0) { |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 49 | /* |
| 50 | * for 10 ms clock period @ PCLK with 4 bit divider = 1/2 |
| 51 | * (default) and prescaler = 16. Should be 10390 |
| 52 | * @33.25MHz and 15625 @ 50 MHz |
| 53 | */ |
David Müller (ELSOFT AG) | d0b3b49 | 2011-12-22 01:16:37 +0000 | [diff] [blame] | 54 | gd->tbu = get_PCLK() / (2 * 16 * 100); |
| 55 | gd->timer_rate_hz = get_PCLK() / (2 * 16); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 56 | } |
| 57 | /* load value for 10 ms timeout */ |
David Müller (ELSOFT AG) | d0b3b49 | 2011-12-22 01:16:37 +0000 | [diff] [blame] | 58 | writel(gd->tbu, &timers->tcntb4); |
C Nauman | d9abba8 | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 59 | /* auto load, manual update of timer 4 */ |
| 60 | tmr = (readl(&timers->tcon) & ~0x0700000) | 0x0600000; |
| 61 | writel(tmr, &timers->tcon); |
| 62 | /* auto load, start timer 4 */ |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 63 | tmr = (tmr & ~0x0700000) | 0x0500000; |
C Nauman | d9abba8 | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 64 | writel(tmr, &timers->tcon); |
David Müller (ELSOFT AG) | d0b3b49 | 2011-12-22 01:16:37 +0000 | [diff] [blame] | 65 | gd->lastinc = 0; |
| 66 | gd->tbl = 0; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 67 | |
David Müller (ELSOFT AG) | d0b3b49 | 2011-12-22 01:16:37 +0000 | [diff] [blame] | 68 | return 0; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /* |
| 72 | * timer without interrupts |
| 73 | */ |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 74 | ulong get_timer(ulong base) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 75 | { |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 76 | return get_timer_masked() - base; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 79 | void __udelay (unsigned long usec) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 80 | { |
| 81 | ulong tmo; |
kevin.morfitt@fearnside-systems.co.uk | cd85662 | 2009-09-06 00:33:13 +0900 | [diff] [blame] | 82 | ulong start = get_ticks(); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 83 | |
| 84 | tmo = usec / 1000; |
David Müller (ELSOFT AG) | d0b3b49 | 2011-12-22 01:16:37 +0000 | [diff] [blame] | 85 | tmo *= (gd->tbu * 100); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 86 | tmo /= 1000; |
| 87 | |
kevin.morfitt@fearnside-systems.co.uk | cd85662 | 2009-09-06 00:33:13 +0900 | [diff] [blame] | 88 | while ((ulong) (get_ticks() - start) < tmo) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 89 | /*NOP*/; |
| 90 | } |
| 91 | |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 92 | ulong get_timer_masked(void) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 93 | { |
kevin.morfitt@fearnside-systems.co.uk | cd85662 | 2009-09-06 00:33:13 +0900 | [diff] [blame] | 94 | ulong tmr = get_ticks(); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 95 | |
David Müller (ELSOFT AG) | d0b3b49 | 2011-12-22 01:16:37 +0000 | [diff] [blame] | 96 | return tmr / (gd->timer_rate_hz / CONFIG_SYS_HZ); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 97 | } |
| 98 | |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 99 | void udelay_masked(unsigned long usec) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 100 | { |
| 101 | ulong tmo; |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 102 | ulong endtime; |
| 103 | signed long diff; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 104 | |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 105 | if (usec >= 1000) { |
| 106 | tmo = usec / 1000; |
David Müller (ELSOFT AG) | d0b3b49 | 2011-12-22 01:16:37 +0000 | [diff] [blame] | 107 | tmo *= (gd->tbu * 100); |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 108 | tmo /= 1000; |
| 109 | } else { |
David Müller (ELSOFT AG) | d0b3b49 | 2011-12-22 01:16:37 +0000 | [diff] [blame] | 110 | tmo = usec * (gd->tbu * 100); |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 111 | tmo /= (1000 * 1000); |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 112 | } |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 113 | |
kevin.morfitt@fearnside-systems.co.uk | cd85662 | 2009-09-06 00:33:13 +0900 | [diff] [blame] | 114 | endtime = get_ticks() + tmo; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 115 | |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 116 | do { |
kevin.morfitt@fearnside-systems.co.uk | cd85662 | 2009-09-06 00:33:13 +0900 | [diff] [blame] | 117 | ulong now = get_ticks(); |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 118 | diff = endtime - now; |
| 119 | } while (diff >= 0); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /* |
| 123 | * This function is derived from PowerPC code (read timebase as long long). |
| 124 | * On ARM it just returns the timer value. |
| 125 | */ |
| 126 | unsigned long long get_ticks(void) |
| 127 | { |
David Müller (ELSOFT AG) | d0b3b49 | 2011-12-22 01:16:37 +0000 | [diff] [blame] | 128 | struct s3c24x0_timers *timers = s3c24x0_get_base_timers(); |
| 129 | ulong now = readl(&timers->tcnto4) & 0xffff; |
kevin.morfitt@fearnside-systems.co.uk | cd85662 | 2009-09-06 00:33:13 +0900 | [diff] [blame] | 130 | |
David Müller (ELSOFT AG) | d0b3b49 | 2011-12-22 01:16:37 +0000 | [diff] [blame] | 131 | if (gd->lastinc >= now) { |
kevin.morfitt@fearnside-systems.co.uk | cd85662 | 2009-09-06 00:33:13 +0900 | [diff] [blame] | 132 | /* normal mode */ |
David Müller (ELSOFT AG) | d0b3b49 | 2011-12-22 01:16:37 +0000 | [diff] [blame] | 133 | gd->tbl += gd->lastinc - now; |
kevin.morfitt@fearnside-systems.co.uk | cd85662 | 2009-09-06 00:33:13 +0900 | [diff] [blame] | 134 | } else { |
| 135 | /* we have an overflow ... */ |
David Müller (ELSOFT AG) | d0b3b49 | 2011-12-22 01:16:37 +0000 | [diff] [blame] | 136 | gd->tbl += gd->lastinc + gd->tbu - now; |
kevin.morfitt@fearnside-systems.co.uk | cd85662 | 2009-09-06 00:33:13 +0900 | [diff] [blame] | 137 | } |
David Müller (ELSOFT AG) | d0b3b49 | 2011-12-22 01:16:37 +0000 | [diff] [blame] | 138 | gd->lastinc = now; |
kevin.morfitt@fearnside-systems.co.uk | cd85662 | 2009-09-06 00:33:13 +0900 | [diff] [blame] | 139 | |
David Müller (ELSOFT AG) | d0b3b49 | 2011-12-22 01:16:37 +0000 | [diff] [blame] | 140 | return gd->tbl; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /* |
| 144 | * This function is derived from PowerPC code (timebase clock frequency). |
| 145 | * On ARM it returns the number of timer ticks per second. |
| 146 | */ |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 147 | ulong get_tbclk(void) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 148 | { |
David Müller (ELSOFT AG) | d0b3b49 | 2011-12-22 01:16:37 +0000 | [diff] [blame] | 149 | return CONFIG_SYS_HZ; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 150 | } |
| 151 | |
wdenk | b304c96 | 2005-04-05 22:30:50 +0000 | [diff] [blame] | 152 | /* |
| 153 | * reset the cpu by setting up the watchdog timer and let him time out |
| 154 | */ |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 155 | void reset_cpu(ulong ignored) |
wdenk | b304c96 | 2005-04-05 22:30:50 +0000 | [diff] [blame] | 156 | { |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 157 | struct s3c24x0_watchdog *watchdog; |
wdenk | b304c96 | 2005-04-05 22:30:50 +0000 | [diff] [blame] | 158 | |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 159 | watchdog = s3c24x0_get_base_watchdog(); |
wdenk | b304c96 | 2005-04-05 22:30:50 +0000 | [diff] [blame] | 160 | |
| 161 | /* Disable watchdog */ |
C Nauman | d9abba8 | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 162 | writel(0x0000, &watchdog->wtcon); |
wdenk | b304c96 | 2005-04-05 22:30:50 +0000 | [diff] [blame] | 163 | |
| 164 | /* Initialize watchdog timer count register */ |
C Nauman | d9abba8 | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 165 | writel(0x0001, &watchdog->wtcnt); |
wdenk | b304c96 | 2005-04-05 22:30:50 +0000 | [diff] [blame] | 166 | |
| 167 | /* Enable watchdog timer; assert reset at timer timeout */ |
C Nauman | d9abba8 | 2010-10-26 23:04:31 +0900 | [diff] [blame] | 168 | writel(0x0021, &watchdog->wtcon); |
wdenk | b304c96 | 2005-04-05 22:30:50 +0000 | [diff] [blame] | 169 | |
kevin.morfitt@fearnside-systems.co.uk | d67cce2 | 2009-10-10 13:30:22 +0900 | [diff] [blame] | 170 | while (1) |
| 171 | /* loop forever and wait for reset to happen */; |
wdenk | b304c96 | 2005-04-05 22:30:50 +0000 | [diff] [blame] | 172 | |
| 173 | /*NOTREACHED*/ |
| 174 | } |
| 175 | |
kevin.morfitt@fearnside-systems.co.uk | ac67804 | 2009-11-17 18:30:34 +0900 | [diff] [blame] | 176 | #endif /* CONFIG_S3C24X0 */ |