Guennadi Liakhovetski | 9b07773 | 2008-08-31 00:39:46 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2003 |
| 3 | * Texas Instruments <www.ti.com> |
| 4 | * |
| 5 | * (C) Copyright 2002 |
| 6 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 7 | * Marius Groeger <mgroeger@sysgo.de> |
| 8 | * |
| 9 | * (C) Copyright 2002 |
| 10 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 11 | * Alex Zuepke <azu@sysgo.de> |
| 12 | * |
| 13 | * (C) Copyright 2002-2004 |
| 14 | * Gary Jennejohn, DENX Software Engineering, <gj@denx.de> |
| 15 | * |
| 16 | * (C) Copyright 2004 |
| 17 | * Philippe Robin, ARM Ltd. <philippe.robin@arm.com> |
| 18 | * |
| 19 | * (C) Copyright 2008 |
| 20 | * Guennadi Liakhovetki, DENX Software Engineering, <lg@denx.de> |
| 21 | * |
| 22 | * See file CREDITS for list of people who contributed to this |
| 23 | * project. |
| 24 | * |
| 25 | * This program is free software; you can redistribute it and/or |
| 26 | * modify it under the terms of the GNU General Public License as |
| 27 | * published by the Free Software Foundation; either version 2 of |
| 28 | * the License, or (at your option) any later version. |
| 29 | * |
| 30 | * This program is distributed in the hope that it will be useful, |
| 31 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 32 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 33 | * GNU General Public License for more details. |
| 34 | * |
| 35 | * You should have received a copy of the GNU General Public License |
| 36 | * along with this program; if not, write to the Free Software |
| 37 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 38 | * MA 02111-1307 USA |
| 39 | */ |
| 40 | |
| 41 | #include <common.h> |
| 42 | #include <asm/proc-armv/ptrace.h> |
| 43 | #include <s3c6400.h> |
| 44 | |
| 45 | static ulong timer_load_val; |
| 46 | |
| 47 | #define PRESCALER 167 |
| 48 | |
| 49 | static s3c64xx_timers *s3c64xx_get_base_timers(void) |
| 50 | { |
| 51 | return (s3c64xx_timers *)ELFIN_TIMER_BASE; |
| 52 | } |
| 53 | |
| 54 | /* macro to read the 16 bit timer */ |
| 55 | static inline ulong read_timer(void) |
| 56 | { |
| 57 | s3c64xx_timers *const timers = s3c64xx_get_base_timers(); |
| 58 | |
| 59 | return timers->TCNTO4; |
| 60 | } |
| 61 | |
| 62 | /* Internal tick units */ |
| 63 | /* Last decremneter snapshot */ |
| 64 | static unsigned long lastdec; |
| 65 | /* Monotonic incrementing timer */ |
| 66 | static unsigned long long timestamp; |
| 67 | |
| 68 | int interrupt_init(void) |
| 69 | { |
| 70 | s3c64xx_timers *const timers = s3c64xx_get_base_timers(); |
| 71 | |
| 72 | /* use PWM Timer 4 because it has no output */ |
| 73 | /* |
| 74 | * We use the following scheme for the timer: |
| 75 | * Prescaler is hard fixed at 167, divider at 1/4. |
| 76 | * This gives at PCLK frequency 66MHz approx. 10us ticks |
| 77 | * The timer is set to wrap after 100s, at 66MHz this obviously |
| 78 | * happens after 10,000,000 ticks. A long variable can thus |
| 79 | * keep values up to 40,000s, i.e., 11 hours. This should be |
| 80 | * enough for most uses:-) Possible optimizations: select a |
| 81 | * binary-friendly frequency, e.g., 1ms / 128. Also calculate |
| 82 | * the prescaler automatically for other PCLK frequencies. |
| 83 | */ |
| 84 | timers->TCFG0 = PRESCALER << 8; |
| 85 | if (timer_load_val == 0) { |
| 86 | timer_load_val = get_PCLK() / PRESCALER * (100 / 4); /* 100s */ |
| 87 | timers->TCFG1 = (timers->TCFG1 & ~0xf0000) | 0x20000; |
| 88 | } |
| 89 | |
| 90 | /* load value for 10 ms timeout */ |
| 91 | lastdec = timers->TCNTB4 = timer_load_val; |
| 92 | /* auto load, manual update of Timer 4 */ |
| 93 | timers->TCON = (timers->TCON & ~0x00700000) | TCON_4_AUTO | |
| 94 | TCON_4_UPDATE; |
| 95 | |
| 96 | /* auto load, start Timer 4 */ |
| 97 | timers->TCON = (timers->TCON & ~0x00700000) | TCON_4_AUTO | COUNT_4_ON; |
| 98 | timestamp = 0; |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * timer without interrupts |
| 105 | */ |
| 106 | |
| 107 | /* |
| 108 | * This function is derived from PowerPC code (read timebase as long long). |
| 109 | * On ARM it just returns the timer value. |
| 110 | */ |
| 111 | unsigned long long get_ticks(void) |
| 112 | { |
| 113 | ulong now = read_timer(); |
| 114 | |
| 115 | if (lastdec >= now) { |
| 116 | /* normal mode */ |
| 117 | timestamp += lastdec - now; |
| 118 | } else { |
| 119 | /* we have an overflow ... */ |
| 120 | timestamp += lastdec + timer_load_val - now; |
| 121 | } |
| 122 | lastdec = now; |
| 123 | |
| 124 | return timestamp; |
| 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 | */ |
| 131 | ulong get_tbclk(void) |
| 132 | { |
| 133 | /* We overrun in 100s */ |
| 134 | return (ulong)(timer_load_val / 100); |
| 135 | } |
| 136 | |
| 137 | void reset_timer_masked(void) |
| 138 | { |
| 139 | /* reset time */ |
| 140 | lastdec = read_timer(); |
| 141 | timestamp = 0; |
| 142 | } |
| 143 | |
| 144 | void reset_timer(void) |
| 145 | { |
| 146 | reset_timer_masked(); |
| 147 | } |
| 148 | |
| 149 | ulong get_timer_masked(void) |
| 150 | { |
| 151 | return get_ticks() / (timer_load_val / (100 * CFG_HZ)); |
| 152 | } |
| 153 | |
| 154 | ulong get_timer(ulong base) |
| 155 | { |
| 156 | return get_timer_masked() - base; |
| 157 | } |
| 158 | |
| 159 | void set_timer(ulong t) |
| 160 | { |
| 161 | timestamp = t * (timer_load_val / (100 * CFG_HZ)); |
| 162 | } |
| 163 | |
| 164 | void udelay(unsigned long usec) |
| 165 | { |
| 166 | unsigned long long tmp; |
| 167 | ulong tmo; |
| 168 | |
| 169 | tmo = (usec + 9) / 10; |
| 170 | tmp = get_ticks() + tmo; /* get current timestamp */ |
| 171 | |
| 172 | while (get_ticks() < tmp)/* loop till event */ |
| 173 | /*NOP*/; |
| 174 | } |