wdenk | a85f9f2 | 2005-04-06 13:52:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Lineo, Inc. <www.lineo.com> |
| 4 | * Bernhard Kuhn <bkuhn@lineo.com> |
| 5 | * |
| 6 | * (C) Copyright 2002 |
| 7 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 8 | * Marius Groeger <mgroeger@sysgo.de> |
| 9 | * |
| 10 | * (C) Copyright 2002 |
| 11 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 12 | * Alex Zuepke <azu@sysgo.de> |
| 13 | * |
| 14 | * See file CREDITS for list of people who contributed to this |
| 15 | * project. |
| 16 | * |
| 17 | * This program is free software; you can redistribute it and/or |
| 18 | * modify it under the terms of the GNU General Public License as |
| 19 | * published by the Free Software Foundation; either version 2 of |
| 20 | * the License, or (at your option) any later version. |
| 21 | * |
| 22 | * This program is distributed in the hope that it will be useful, |
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 | * GNU General Public License for more details. |
| 26 | * |
| 27 | * You should have received a copy of the GNU General Public License |
| 28 | * along with this program; if not, write to the Free Software |
| 29 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 30 | * MA 02111-1307 USA |
| 31 | */ |
| 32 | |
| 33 | #include <common.h> |
| 34 | /*#include <asm/io.h>*/ |
| 35 | #include <asm/arch/hardware.h> |
| 36 | /*#include <asm/proc/ptrace.h>*/ |
| 37 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 38 | /* the number of clocks per CONFIG_SYS_HZ */ |
| 39 | #define TIMER_LOAD_VAL (CONFIG_SYS_HZ_CLOCK/CONFIG_SYS_HZ) |
wdenk | a85f9f2 | 2005-04-06 13:52:31 +0000 | [diff] [blame] | 40 | |
| 41 | /* macro to read the 16 bit timer */ |
| 42 | #define READ_TIMER (tmr->TC_CV & 0x0000ffff) |
| 43 | AT91PS_TC tmr; |
| 44 | |
| 45 | static ulong timestamp; |
| 46 | static ulong lastinc; |
| 47 | |
Jean-Christophe PLAGNIOL-VILLARD | b54384e | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 48 | int timer_init (void) |
wdenk | a85f9f2 | 2005-04-06 13:52:31 +0000 | [diff] [blame] | 49 | { |
| 50 | tmr = AT91C_BASE_TC0; |
| 51 | |
| 52 | /* enables TC1.0 clock */ |
| 53 | *AT91C_PMC_PCER = 1 << AT91C_ID_TC0; /* enable clock */ |
| 54 | |
| 55 | *AT91C_TCB0_BCR = 0; |
| 56 | *AT91C_TCB0_BMR = AT91C_TCB_TC0XC0S_NONE | AT91C_TCB_TC1XC1S_NONE | AT91C_TCB_TC2XC2S_NONE; |
| 57 | tmr->TC_CCR = AT91C_TC_CLKDIS; |
| 58 | #define AT91C_TC_CMR_CPCTRG (1 << 14) |
| 59 | /* set to MCLK/2 and restart the timer when the vlaue in TC_RC is reached */ |
| 60 | tmr->TC_CMR = AT91C_TC_TIMER_DIV1_CLOCK | AT91C_TC_CMR_CPCTRG; |
| 61 | |
| 62 | tmr->TC_IDR = ~0ul; |
| 63 | tmr->TC_RC = TIMER_LOAD_VAL; |
| 64 | lastinc = 0; |
| 65 | tmr->TC_CCR = AT91C_TC_SWTRG | AT91C_TC_CLKEN; |
| 66 | timestamp = 0; |
| 67 | |
| 68 | return (0); |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * timer without interrupts |
| 73 | */ |
wdenk | a85f9f2 | 2005-04-06 13:52:31 +0000 | [diff] [blame] | 74 | ulong get_timer (ulong base) |
| 75 | { |
| 76 | return get_timer_masked () - base; |
| 77 | } |
| 78 | |
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 79 | void __udelay (unsigned long usec) |
wdenk | a85f9f2 | 2005-04-06 13:52:31 +0000 | [diff] [blame] | 80 | { |
| 81 | udelay_masked(usec); |
| 82 | } |
| 83 | |
| 84 | void reset_timer_masked (void) |
| 85 | { |
| 86 | /* reset time */ |
| 87 | lastinc = READ_TIMER; |
| 88 | timestamp = 0; |
| 89 | } |
| 90 | |
| 91 | ulong get_timer_raw (void) |
| 92 | { |
| 93 | ulong now = READ_TIMER; |
| 94 | |
| 95 | if (now >= lastinc) { |
| 96 | /* normal mode */ |
| 97 | timestamp += now - lastinc; |
| 98 | } else { |
| 99 | /* we have an overflow ... */ |
| 100 | timestamp += now + TIMER_LOAD_VAL - lastinc; |
| 101 | } |
| 102 | lastinc = now; |
| 103 | |
| 104 | return timestamp; |
| 105 | } |
| 106 | |
| 107 | ulong get_timer_masked (void) |
| 108 | { |
| 109 | return get_timer_raw()/TIMER_LOAD_VAL; |
| 110 | } |
| 111 | |
| 112 | void udelay_masked (unsigned long usec) |
| 113 | { |
| 114 | ulong tmo; |
| 115 | ulong endtime; |
| 116 | signed long diff; |
| 117 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 118 | tmo = CONFIG_SYS_HZ_CLOCK / 1000; |
wdenk | a85f9f2 | 2005-04-06 13:52:31 +0000 | [diff] [blame] | 119 | tmo *= usec; |
| 120 | tmo /= 1000; |
| 121 | |
| 122 | endtime = get_timer_raw () + tmo; |
| 123 | |
| 124 | do { |
| 125 | ulong now = get_timer_raw (); |
| 126 | diff = endtime - now; |
| 127 | } while (diff >= 0); |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * This function is derived from PowerPC code (read timebase as long long). |
| 132 | * On ARM it just returns the timer value. |
| 133 | */ |
| 134 | unsigned long long get_ticks(void) |
| 135 | { |
| 136 | return get_timer(0); |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * This function is derived from PowerPC code (timebase clock frequency). |
| 141 | * On ARM it returns the number of timer ticks per second. |
| 142 | */ |
| 143 | ulong get_tbclk (void) |
| 144 | { |
| 145 | ulong tbclk; |
| 146 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 147 | tbclk = CONFIG_SYS_HZ; |
wdenk | a85f9f2 | 2005-04-06 13:52:31 +0000 | [diff] [blame] | 148 | return tbclk; |
| 149 | } |