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