1576afd4fSJean-Christophe PLAGNIOL-VILLARD /* 2576afd4fSJean-Christophe PLAGNIOL-VILLARD * (C) Copyright 2002 3576afd4fSJean-Christophe PLAGNIOL-VILLARD * Sysgo Real-Time Solutions, GmbH <www.elinos.com> 4576afd4fSJean-Christophe PLAGNIOL-VILLARD * Marius Groeger <mgroeger@sysgo.de> 5576afd4fSJean-Christophe PLAGNIOL-VILLARD * 6576afd4fSJean-Christophe PLAGNIOL-VILLARD * (C) Copyright 2002 7576afd4fSJean-Christophe PLAGNIOL-VILLARD * David Mueller, ELSOFT AG, <d.mueller@elsoft.ch> 8576afd4fSJean-Christophe PLAGNIOL-VILLARD * 9576afd4fSJean-Christophe PLAGNIOL-VILLARD * (C) Copyright 2003 10576afd4fSJean-Christophe PLAGNIOL-VILLARD * Texas Instruments, <www.ti.com> 11576afd4fSJean-Christophe PLAGNIOL-VILLARD * Kshitij Gupta <Kshitij@ti.com> 12576afd4fSJean-Christophe PLAGNIOL-VILLARD * 13576afd4fSJean-Christophe PLAGNIOL-VILLARD * (C) Copyright 2004 14576afd4fSJean-Christophe PLAGNIOL-VILLARD * ARM Ltd. 15576afd4fSJean-Christophe PLAGNIOL-VILLARD * Philippe Robin, <philippe.robin@arm.com> 16576afd4fSJean-Christophe PLAGNIOL-VILLARD * 17576afd4fSJean-Christophe PLAGNIOL-VILLARD * See file CREDITS for list of people who contributed to this 18576afd4fSJean-Christophe PLAGNIOL-VILLARD * project. 19576afd4fSJean-Christophe PLAGNIOL-VILLARD * 20576afd4fSJean-Christophe PLAGNIOL-VILLARD * This program is free software; you can redistribute it and/or 21576afd4fSJean-Christophe PLAGNIOL-VILLARD * modify it under the terms of the GNU General Public License as 22576afd4fSJean-Christophe PLAGNIOL-VILLARD * published by the Free Software Foundation; either version 2 of 23576afd4fSJean-Christophe PLAGNIOL-VILLARD * the License, or (at your option) any later version. 24576afd4fSJean-Christophe PLAGNIOL-VILLARD * 25576afd4fSJean-Christophe PLAGNIOL-VILLARD * This program is distributed in the hope that it will be useful, 26576afd4fSJean-Christophe PLAGNIOL-VILLARD * but WITHOUT ANY WARRANTY; without even the implied warranty of 27576afd4fSJean-Christophe PLAGNIOL-VILLARD * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 28576afd4fSJean-Christophe PLAGNIOL-VILLARD * GNU General Public License for more details. 29576afd4fSJean-Christophe PLAGNIOL-VILLARD * 30576afd4fSJean-Christophe PLAGNIOL-VILLARD * You should have received a copy of the GNU General Public License 31576afd4fSJean-Christophe PLAGNIOL-VILLARD * along with this program; if not, write to the Free Software 32576afd4fSJean-Christophe PLAGNIOL-VILLARD * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 33576afd4fSJean-Christophe PLAGNIOL-VILLARD * MA 02111-1307 USA 34576afd4fSJean-Christophe PLAGNIOL-VILLARD */ 35576afd4fSJean-Christophe PLAGNIOL-VILLARD 36576afd4fSJean-Christophe PLAGNIOL-VILLARD #include <common.h> 37576afd4fSJean-Christophe PLAGNIOL-VILLARD #include <div64.h> 38576afd4fSJean-Christophe PLAGNIOL-VILLARD 39576afd4fSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_ARCH_CINTEGRATOR 40576afd4fSJean-Christophe PLAGNIOL-VILLARD #define DIV_CLOCK_INIT 1 41576afd4fSJean-Christophe PLAGNIOL-VILLARD #define TIMER_LOAD_VAL 0xFFFFFFFFL 42576afd4fSJean-Christophe PLAGNIOL-VILLARD #else 43576afd4fSJean-Christophe PLAGNIOL-VILLARD #define DIV_CLOCK_INIT 256 44576afd4fSJean-Christophe PLAGNIOL-VILLARD #define TIMER_LOAD_VAL 0x0000FFFFL 45576afd4fSJean-Christophe PLAGNIOL-VILLARD #endif 46576afd4fSJean-Christophe PLAGNIOL-VILLARD /* The Integrator/CP timer1 is clocked at 1MHz 47576afd4fSJean-Christophe PLAGNIOL-VILLARD * can be divided by 16 or 256 48576afd4fSJean-Christophe PLAGNIOL-VILLARD * and can be set up as a 32-bit timer 49576afd4fSJean-Christophe PLAGNIOL-VILLARD */ 50576afd4fSJean-Christophe PLAGNIOL-VILLARD /* U-Boot expects a 32 bit timer, running at CONFIG_SYS_HZ */ 51576afd4fSJean-Christophe PLAGNIOL-VILLARD /* Keep total timer count to avoid losing decrements < div_timer */ 52576afd4fSJean-Christophe PLAGNIOL-VILLARD static unsigned long long total_count = 0; 53576afd4fSJean-Christophe PLAGNIOL-VILLARD static unsigned long long lastdec; /* Timer reading at last call */ 54576afd4fSJean-Christophe PLAGNIOL-VILLARD /* Divisor applied to timer clock */ 55576afd4fSJean-Christophe PLAGNIOL-VILLARD static unsigned long long div_clock = DIV_CLOCK_INIT; 56576afd4fSJean-Christophe PLAGNIOL-VILLARD static unsigned long long div_timer = 1; /* Divisor to convert timer reading 57576afd4fSJean-Christophe PLAGNIOL-VILLARD * change to U-Boot ticks 58576afd4fSJean-Christophe PLAGNIOL-VILLARD */ 59576afd4fSJean-Christophe PLAGNIOL-VILLARD /* CONFIG_SYS_HZ = CONFIG_SYS_HZ_CLOCK/(div_clock * div_timer) */ 60576afd4fSJean-Christophe PLAGNIOL-VILLARD static ulong timestamp; /* U-Boot ticks since startup */ 61576afd4fSJean-Christophe PLAGNIOL-VILLARD 62576afd4fSJean-Christophe PLAGNIOL-VILLARD #define READ_TIMER (*(volatile ulong *)(CONFIG_SYS_TIMERBASE+4)) 63576afd4fSJean-Christophe PLAGNIOL-VILLARD 64576afd4fSJean-Christophe PLAGNIOL-VILLARD /* all function return values in U-Boot ticks i.e. (1/CONFIG_SYS_HZ) sec 65576afd4fSJean-Christophe PLAGNIOL-VILLARD * - unless otherwise stated 66576afd4fSJean-Christophe PLAGNIOL-VILLARD */ 67576afd4fSJean-Christophe PLAGNIOL-VILLARD 68576afd4fSJean-Christophe PLAGNIOL-VILLARD /* starts up a counter 69576afd4fSJean-Christophe PLAGNIOL-VILLARD * - the Integrator/CP timer can be set up to issue an interrupt */ 70576afd4fSJean-Christophe PLAGNIOL-VILLARD int timer_init (void) 71576afd4fSJean-Christophe PLAGNIOL-VILLARD { 72576afd4fSJean-Christophe PLAGNIOL-VILLARD /* Load timer with initial value */ 73576afd4fSJean-Christophe PLAGNIOL-VILLARD *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 0) = TIMER_LOAD_VAL; 74576afd4fSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_ARCH_CINTEGRATOR 75576afd4fSJean-Christophe PLAGNIOL-VILLARD /* Set timer to be 76576afd4fSJean-Christophe PLAGNIOL-VILLARD * enabled 1 77576afd4fSJean-Christophe PLAGNIOL-VILLARD * periodic 1 78576afd4fSJean-Christophe PLAGNIOL-VILLARD * no interrupts 0 79576afd4fSJean-Christophe PLAGNIOL-VILLARD * X 0 80576afd4fSJean-Christophe PLAGNIOL-VILLARD * divider 1 00 == less rounding error 81576afd4fSJean-Christophe PLAGNIOL-VILLARD * 32 bit 1 82576afd4fSJean-Christophe PLAGNIOL-VILLARD * wrapping 0 83576afd4fSJean-Christophe PLAGNIOL-VILLARD */ 84576afd4fSJean-Christophe PLAGNIOL-VILLARD *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 8) = 0x000000C2; 85576afd4fSJean-Christophe PLAGNIOL-VILLARD #else 86576afd4fSJean-Christophe PLAGNIOL-VILLARD /* Set timer to be 87576afd4fSJean-Christophe PLAGNIOL-VILLARD * enabled 1 88576afd4fSJean-Christophe PLAGNIOL-VILLARD * free-running 0 89576afd4fSJean-Christophe PLAGNIOL-VILLARD * XX 00 90576afd4fSJean-Christophe PLAGNIOL-VILLARD * divider 256 10 91576afd4fSJean-Christophe PLAGNIOL-VILLARD * XX 00 92576afd4fSJean-Christophe PLAGNIOL-VILLARD */ 93576afd4fSJean-Christophe PLAGNIOL-VILLARD *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 8) = 0x00000088; 94576afd4fSJean-Christophe PLAGNIOL-VILLARD #endif 95576afd4fSJean-Christophe PLAGNIOL-VILLARD 96576afd4fSJean-Christophe PLAGNIOL-VILLARD /* init the timestamp */ 97576afd4fSJean-Christophe PLAGNIOL-VILLARD total_count = 0ULL; 98*17659d7dSGraeme Russ /* capure current decrementer value */ 99*17659d7dSGraeme Russ lastdec = READ_TIMER; 100*17659d7dSGraeme Russ /* start "advancing" time stamp from 0 */ 101*17659d7dSGraeme Russ timestamp = 0L; 102576afd4fSJean-Christophe PLAGNIOL-VILLARD 103576afd4fSJean-Christophe PLAGNIOL-VILLARD div_timer = CONFIG_SYS_HZ_CLOCK; 104576afd4fSJean-Christophe PLAGNIOL-VILLARD do_div(div_timer, CONFIG_SYS_HZ); 105576afd4fSJean-Christophe PLAGNIOL-VILLARD do_div(div_timer, div_clock); 106576afd4fSJean-Christophe PLAGNIOL-VILLARD 107576afd4fSJean-Christophe PLAGNIOL-VILLARD return (0); 108576afd4fSJean-Christophe PLAGNIOL-VILLARD } 109576afd4fSJean-Christophe PLAGNIOL-VILLARD 110576afd4fSJean-Christophe PLAGNIOL-VILLARD /* 111576afd4fSJean-Christophe PLAGNIOL-VILLARD * timer without interrupts 112576afd4fSJean-Christophe PLAGNIOL-VILLARD */ 113576afd4fSJean-Christophe PLAGNIOL-VILLARD ulong get_timer (ulong base_ticks) 114576afd4fSJean-Christophe PLAGNIOL-VILLARD { 115576afd4fSJean-Christophe PLAGNIOL-VILLARD return get_timer_masked () - base_ticks; 116576afd4fSJean-Christophe PLAGNIOL-VILLARD } 117576afd4fSJean-Christophe PLAGNIOL-VILLARD 118576afd4fSJean-Christophe PLAGNIOL-VILLARD /* delay usec useconds */ 1193eb90badSIngo van Lil void __udelay (unsigned long usec) 120576afd4fSJean-Christophe PLAGNIOL-VILLARD { 121576afd4fSJean-Christophe PLAGNIOL-VILLARD ulong tmo, tmp; 122576afd4fSJean-Christophe PLAGNIOL-VILLARD 123576afd4fSJean-Christophe PLAGNIOL-VILLARD /* Convert to U-Boot ticks */ 124576afd4fSJean-Christophe PLAGNIOL-VILLARD tmo = usec * CONFIG_SYS_HZ; 125576afd4fSJean-Christophe PLAGNIOL-VILLARD tmo /= (1000000L); 126576afd4fSJean-Christophe PLAGNIOL-VILLARD 127576afd4fSJean-Christophe PLAGNIOL-VILLARD tmp = get_timer_masked(); /* get current timestamp */ 128576afd4fSJean-Christophe PLAGNIOL-VILLARD tmo += tmp; /* form target timestamp */ 129576afd4fSJean-Christophe PLAGNIOL-VILLARD 130576afd4fSJean-Christophe PLAGNIOL-VILLARD while (get_timer_masked () < tmo) {/* loop till event */ 131576afd4fSJean-Christophe PLAGNIOL-VILLARD /*NOP*/; 132576afd4fSJean-Christophe PLAGNIOL-VILLARD } 133576afd4fSJean-Christophe PLAGNIOL-VILLARD } 134576afd4fSJean-Christophe PLAGNIOL-VILLARD 135576afd4fSJean-Christophe PLAGNIOL-VILLARD /* converts the timer reading to U-Boot ticks */ 136576afd4fSJean-Christophe PLAGNIOL-VILLARD /* the timestamp is the number of ticks since reset */ 137576afd4fSJean-Christophe PLAGNIOL-VILLARD ulong get_timer_masked (void) 138576afd4fSJean-Christophe PLAGNIOL-VILLARD { 139576afd4fSJean-Christophe PLAGNIOL-VILLARD /* get current count */ 140576afd4fSJean-Christophe PLAGNIOL-VILLARD unsigned long long now = READ_TIMER; 141576afd4fSJean-Christophe PLAGNIOL-VILLARD 142576afd4fSJean-Christophe PLAGNIOL-VILLARD if(now > lastdec) { 143576afd4fSJean-Christophe PLAGNIOL-VILLARD /* Must have wrapped */ 144576afd4fSJean-Christophe PLAGNIOL-VILLARD total_count += lastdec + TIMER_LOAD_VAL + 1 - now; 145576afd4fSJean-Christophe PLAGNIOL-VILLARD } else { 146576afd4fSJean-Christophe PLAGNIOL-VILLARD total_count += lastdec - now; 147576afd4fSJean-Christophe PLAGNIOL-VILLARD } 148576afd4fSJean-Christophe PLAGNIOL-VILLARD lastdec = now; 149576afd4fSJean-Christophe PLAGNIOL-VILLARD 150576afd4fSJean-Christophe PLAGNIOL-VILLARD /* Reuse "now" */ 151576afd4fSJean-Christophe PLAGNIOL-VILLARD now = total_count; 152576afd4fSJean-Christophe PLAGNIOL-VILLARD do_div(now, div_timer); 153576afd4fSJean-Christophe PLAGNIOL-VILLARD timestamp = now; 154576afd4fSJean-Christophe PLAGNIOL-VILLARD 155576afd4fSJean-Christophe PLAGNIOL-VILLARD return timestamp; 156576afd4fSJean-Christophe PLAGNIOL-VILLARD } 157576afd4fSJean-Christophe PLAGNIOL-VILLARD 158576afd4fSJean-Christophe PLAGNIOL-VILLARD /* waits specified delay value and resets timestamp */ 159576afd4fSJean-Christophe PLAGNIOL-VILLARD void udelay_masked (unsigned long usec) 160576afd4fSJean-Christophe PLAGNIOL-VILLARD { 161576afd4fSJean-Christophe PLAGNIOL-VILLARD udelay(usec); 162576afd4fSJean-Christophe PLAGNIOL-VILLARD } 163576afd4fSJean-Christophe PLAGNIOL-VILLARD 164576afd4fSJean-Christophe PLAGNIOL-VILLARD /* 165576afd4fSJean-Christophe PLAGNIOL-VILLARD * This function is derived from PowerPC code (read timebase as long long). 166576afd4fSJean-Christophe PLAGNIOL-VILLARD * On ARM it just returns the timer value. 167576afd4fSJean-Christophe PLAGNIOL-VILLARD */ 168576afd4fSJean-Christophe PLAGNIOL-VILLARD unsigned long long get_ticks(void) 169576afd4fSJean-Christophe PLAGNIOL-VILLARD { 170576afd4fSJean-Christophe PLAGNIOL-VILLARD return get_timer(0); 171576afd4fSJean-Christophe PLAGNIOL-VILLARD } 172576afd4fSJean-Christophe PLAGNIOL-VILLARD 173576afd4fSJean-Christophe PLAGNIOL-VILLARD /* 174576afd4fSJean-Christophe PLAGNIOL-VILLARD * Return the timebase clock frequency 175576afd4fSJean-Christophe PLAGNIOL-VILLARD * i.e. how often the timer decrements 176576afd4fSJean-Christophe PLAGNIOL-VILLARD */ 177576afd4fSJean-Christophe PLAGNIOL-VILLARD ulong get_tbclk (void) 178576afd4fSJean-Christophe PLAGNIOL-VILLARD { 179576afd4fSJean-Christophe PLAGNIOL-VILLARD unsigned long long tmp = CONFIG_SYS_HZ_CLOCK; 180576afd4fSJean-Christophe PLAGNIOL-VILLARD 181576afd4fSJean-Christophe PLAGNIOL-VILLARD do_div(tmp, div_clock); 182576afd4fSJean-Christophe PLAGNIOL-VILLARD 183576afd4fSJean-Christophe PLAGNIOL-VILLARD return tmp; 184576afd4fSJean-Christophe PLAGNIOL-VILLARD } 185