wdenk | f6f96f7 | 2003-07-15 20:04:06 +0000 | [diff] [blame] | 1 | /* |
Ladislav Michl | a16482e | 2009-04-22 01:12:04 +0200 | [diff] [blame] | 2 | * (C) Copyright 2009 |
| 3 | * 2N Telekomunikace, <www.2n.cz> |
| 4 | * |
wdenk | f6f96f7 | 2003-07-15 20:04:06 +0000 | [diff] [blame] | 5 | * (C) Copyright 2003 |
| 6 | * Texas Instruments, <www.ti.com> |
| 7 | * |
| 8 | * (C) Copyright 2002 |
| 9 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 10 | * Marius Groeger <mgroeger@sysgo.de> |
| 11 | * |
| 12 | * (C) Copyright 2002 |
| 13 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 14 | * Alex Zuepke <azu@sysgo.de> |
| 15 | * |
| 16 | * (C) Copyright 2002 |
Detlev Zundel | f1b3f2b | 2009-05-13 10:54:10 +0200 | [diff] [blame] | 17 | * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de> |
wdenk | f6f96f7 | 2003-07-15 20:04:06 +0000 | [diff] [blame] | 18 | * |
| 19 | * See file CREDITS for list of people who contributed to this |
| 20 | * project. |
| 21 | * |
| 22 | * This program is free software; you can redistribute it and/or |
| 23 | * modify it under the terms of the GNU General Public License as |
| 24 | * published by the Free Software Foundation; either version 2 of |
| 25 | * the License, or (at your option) any later version. |
| 26 | * |
| 27 | * This program is distributed in the hope that it will be useful, |
| 28 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 29 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wdenk | f6f96f7 | 2003-07-15 20:04:06 +0000 | [diff] [blame] | 30 | * GNU General Public License for more details. |
| 31 | * |
| 32 | * You should have received a copy of the GNU General Public License |
| 33 | * along with this program; if not, write to the Free Software |
| 34 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 35 | * MA 02111-1307 USA |
| 36 | */ |
| 37 | |
| 38 | #include <common.h> |
| 39 | #include <arm925t.h> |
| 40 | #include <configs/omap1510.h> |
Ladislav Michl | aa3cc9a | 2009-03-30 18:58:41 +0200 | [diff] [blame] | 41 | #include <asm/io.h> |
wdenk | f6f96f7 | 2003-07-15 20:04:06 +0000 | [diff] [blame] | 42 | |
Ladislav Michl | a16482e | 2009-04-22 01:12:04 +0200 | [diff] [blame] | 43 | #define TIMER_LOAD_VAL 0xffffffff |
| 44 | #define TIMER_CLOCK (CONFIG_SYS_CLK_FREQ / (2 << CONFIG_SYS_PTV)) |
wdenk | f6f96f7 | 2003-07-15 20:04:06 +0000 | [diff] [blame] | 45 | |
Ladislav Michl | aa3cc9a | 2009-03-30 18:58:41 +0200 | [diff] [blame] | 46 | static uint32_t timestamp; |
| 47 | static uint32_t lastdec; |
wdenk | f6f96f7 | 2003-07-15 20:04:06 +0000 | [diff] [blame] | 48 | |
| 49 | /* nothing really to do with interrupts, just starts up a counter. */ |
Jean-Christophe PLAGNIOL-VILLARD | 8c9fc00 | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 50 | int timer_init (void) |
wdenk | f6f96f7 | 2003-07-15 20:04:06 +0000 | [diff] [blame] | 51 | { |
wdenk | 3da587e | 2003-10-19 23:22:11 +0000 | [diff] [blame] | 52 | /* Start the decrementer ticking down from 0xffffffff */ |
Ladislav Michl | aa3cc9a | 2009-03-30 18:58:41 +0200 | [diff] [blame] | 53 | __raw_writel(TIMER_LOAD_VAL, CONFIG_SYS_TIMERBASE + LOAD_TIM); |
| 54 | __raw_writel(MPUTIM_ST | MPUTIM_AR | MPUTIM_CLOCK_ENABLE | |
| 55 | (CONFIG_SYS_PTV << MPUTIM_PTV_BIT), |
| 56 | CONFIG_SYS_TIMERBASE + CNTL_TIMER); |
wdenk | 3da587e | 2003-10-19 23:22:11 +0000 | [diff] [blame] | 57 | |
| 58 | /* init the timestamp and lastdec value */ |
Graeme Russ | 944a7fe | 2011-07-15 02:21:14 +0000 | [diff] [blame] | 59 | lastdec = __raw_readl(CONFIG_SYS_TIMERBASE + READ_TIM) / |
| 60 | (TIMER_CLOCK / CONFIG_SYS_HZ); |
| 61 | timestamp = 0; /* start "advancing" time stamp from 0 */ |
wdenk | 3da587e | 2003-10-19 23:22:11 +0000 | [diff] [blame] | 62 | |
Ladislav Michl | 173a108 | 2009-03-30 18:58:40 +0200 | [diff] [blame] | 63 | return 0; |
wdenk | f6f96f7 | 2003-07-15 20:04:06 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /* |
| 67 | * timer without interrupts |
| 68 | */ |
wdenk | f6f96f7 | 2003-07-15 20:04:06 +0000 | [diff] [blame] | 69 | ulong get_timer (ulong base) |
| 70 | { |
| 71 | return get_timer_masked () - base; |
| 72 | } |
| 73 | |
wdenk | 7b37096 | 2004-04-25 14:37:29 +0000 | [diff] [blame] | 74 | /* delay x useconds AND preserve advance timestamp value */ |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 75 | void __udelay (unsigned long usec) |
wdenk | f6f96f7 | 2003-07-15 20:04:06 +0000 | [diff] [blame] | 76 | { |
Ladislav Michl | a16482e | 2009-04-22 01:12:04 +0200 | [diff] [blame] | 77 | int32_t tmo = usec * (TIMER_CLOCK / 1000) / 1000; |
| 78 | uint32_t now, last = __raw_readl(CONFIG_SYS_TIMERBASE + READ_TIM); |
wdenk | f6f96f7 | 2003-07-15 20:04:06 +0000 | [diff] [blame] | 79 | |
Ladislav Michl | a16482e | 2009-04-22 01:12:04 +0200 | [diff] [blame] | 80 | while (tmo > 0) { |
| 81 | now = __raw_readl(CONFIG_SYS_TIMERBASE + READ_TIM); |
| 82 | if (last < now) /* count down timer underflow */ |
| 83 | tmo -= TIMER_LOAD_VAL - now + last; |
| 84 | else |
| 85 | tmo -= last - now; |
| 86 | last = now; |
wdenk | 3da587e | 2003-10-19 23:22:11 +0000 | [diff] [blame] | 87 | } |
wdenk | f6f96f7 | 2003-07-15 20:04:06 +0000 | [diff] [blame] | 88 | } |
| 89 | |
wdenk | f6f96f7 | 2003-07-15 20:04:06 +0000 | [diff] [blame] | 90 | ulong get_timer_masked (void) |
| 91 | { |
Ladislav Michl | a16482e | 2009-04-22 01:12:04 +0200 | [diff] [blame] | 92 | uint32_t now = __raw_readl(CONFIG_SYS_TIMERBASE + READ_TIM) / |
| 93 | (TIMER_CLOCK / CONFIG_SYS_HZ); |
| 94 | if (lastdec < now) /* count down timer underflow */ |
| 95 | timestamp += TIMER_LOAD_VAL / (TIMER_CLOCK / CONFIG_SYS_HZ) - |
| 96 | now + lastdec; |
| 97 | else |
| 98 | timestamp += lastdec - now; |
wdenk | f6f96f7 | 2003-07-15 20:04:06 +0000 | [diff] [blame] | 99 | lastdec = now; |
| 100 | |
| 101 | return timestamp; |
| 102 | } |
| 103 | |
wdenk | f6f96f7 | 2003-07-15 20:04:06 +0000 | [diff] [blame] | 104 | /* |
| 105 | * This function is derived from PowerPC code (read timebase as long long). |
| 106 | * On ARM it just returns the timer value. |
| 107 | */ |
| 108 | unsigned long long get_ticks(void) |
| 109 | { |
| 110 | return get_timer(0); |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * This function is derived from PowerPC code (timebase clock frequency). |
| 115 | * On ARM it returns the number of timer ticks per second. |
| 116 | */ |
| 117 | ulong get_tbclk (void) |
wdenk | 3da587e | 2003-10-19 23:22:11 +0000 | [diff] [blame] | 118 | { |
Ladislav Michl | 173a108 | 2009-03-30 18:58:40 +0200 | [diff] [blame] | 119 | return CONFIG_SYS_HZ; |
wdenk | f6f96f7 | 2003-07-15 20:04:06 +0000 | [diff] [blame] | 120 | } |