1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+ 2576afd4fSJean-Christophe PLAGNIOL-VILLARD /* 3576afd4fSJean-Christophe PLAGNIOL-VILLARD * (C) Copyright 2002 4576afd4fSJean-Christophe PLAGNIOL-VILLARD * Sysgo Real-Time Solutions, GmbH <www.elinos.com> 5576afd4fSJean-Christophe PLAGNIOL-VILLARD * Marius Groeger <mgroeger@sysgo.de> 6576afd4fSJean-Christophe PLAGNIOL-VILLARD * 7576afd4fSJean-Christophe PLAGNIOL-VILLARD * (C) Copyright 2002 8576afd4fSJean-Christophe PLAGNIOL-VILLARD * David Mueller, ELSOFT AG, <d.mueller@elsoft.ch> 9576afd4fSJean-Christophe PLAGNIOL-VILLARD * 10576afd4fSJean-Christophe PLAGNIOL-VILLARD * (C) Copyright 2003 11576afd4fSJean-Christophe PLAGNIOL-VILLARD * Texas Instruments, <www.ti.com> 12576afd4fSJean-Christophe PLAGNIOL-VILLARD * Kshitij Gupta <Kshitij@ti.com> 13576afd4fSJean-Christophe PLAGNIOL-VILLARD * 14576afd4fSJean-Christophe PLAGNIOL-VILLARD * (C) Copyright 2004 15576afd4fSJean-Christophe PLAGNIOL-VILLARD * ARM Ltd. 16576afd4fSJean-Christophe PLAGNIOL-VILLARD * Philippe Robin, <philippe.robin@arm.com> 17576afd4fSJean-Christophe PLAGNIOL-VILLARD */ 18576afd4fSJean-Christophe PLAGNIOL-VILLARD 19576afd4fSJean-Christophe PLAGNIOL-VILLARD #include <common.h> 20576afd4fSJean-Christophe PLAGNIOL-VILLARD #include <div64.h> 21576afd4fSJean-Christophe PLAGNIOL-VILLARD 22576afd4fSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_ARCH_CINTEGRATOR 23576afd4fSJean-Christophe PLAGNIOL-VILLARD #define DIV_CLOCK_INIT 1 24576afd4fSJean-Christophe PLAGNIOL-VILLARD #define TIMER_LOAD_VAL 0xFFFFFFFFL 25576afd4fSJean-Christophe PLAGNIOL-VILLARD #else 26576afd4fSJean-Christophe PLAGNIOL-VILLARD #define DIV_CLOCK_INIT 256 27576afd4fSJean-Christophe PLAGNIOL-VILLARD #define TIMER_LOAD_VAL 0x0000FFFFL 28576afd4fSJean-Christophe PLAGNIOL-VILLARD #endif 29576afd4fSJean-Christophe PLAGNIOL-VILLARD /* The Integrator/CP timer1 is clocked at 1MHz 30576afd4fSJean-Christophe PLAGNIOL-VILLARD * can be divided by 16 or 256 31576afd4fSJean-Christophe PLAGNIOL-VILLARD * and can be set up as a 32-bit timer 32576afd4fSJean-Christophe PLAGNIOL-VILLARD */ 33576afd4fSJean-Christophe PLAGNIOL-VILLARD /* U-Boot expects a 32 bit timer, running at CONFIG_SYS_HZ */ 34576afd4fSJean-Christophe PLAGNIOL-VILLARD /* Keep total timer count to avoid losing decrements < div_timer */ 35576afd4fSJean-Christophe PLAGNIOL-VILLARD static unsigned long long total_count = 0; 36576afd4fSJean-Christophe PLAGNIOL-VILLARD static unsigned long long lastdec; /* Timer reading at last call */ 37576afd4fSJean-Christophe PLAGNIOL-VILLARD /* Divisor applied to timer clock */ 38576afd4fSJean-Christophe PLAGNIOL-VILLARD static unsigned long long div_clock = DIV_CLOCK_INIT; 39576afd4fSJean-Christophe PLAGNIOL-VILLARD static unsigned long long div_timer = 1; /* Divisor to convert timer reading 40576afd4fSJean-Christophe PLAGNIOL-VILLARD * change to U-Boot ticks 41576afd4fSJean-Christophe PLAGNIOL-VILLARD */ 42576afd4fSJean-Christophe PLAGNIOL-VILLARD /* CONFIG_SYS_HZ = CONFIG_SYS_HZ_CLOCK/(div_clock * div_timer) */ 43576afd4fSJean-Christophe PLAGNIOL-VILLARD static ulong timestamp; /* U-Boot ticks since startup */ 44576afd4fSJean-Christophe PLAGNIOL-VILLARD 45576afd4fSJean-Christophe PLAGNIOL-VILLARD #define READ_TIMER (*(volatile ulong *)(CONFIG_SYS_TIMERBASE+4)) 46576afd4fSJean-Christophe PLAGNIOL-VILLARD 47576afd4fSJean-Christophe PLAGNIOL-VILLARD /* all function return values in U-Boot ticks i.e. (1/CONFIG_SYS_HZ) sec 48576afd4fSJean-Christophe PLAGNIOL-VILLARD * - unless otherwise stated 49576afd4fSJean-Christophe PLAGNIOL-VILLARD */ 50576afd4fSJean-Christophe PLAGNIOL-VILLARD 51576afd4fSJean-Christophe PLAGNIOL-VILLARD /* starts up a counter 52576afd4fSJean-Christophe PLAGNIOL-VILLARD * - the Integrator/CP timer can be set up to issue an interrupt */ 53576afd4fSJean-Christophe PLAGNIOL-VILLARD int timer_init (void) 54576afd4fSJean-Christophe PLAGNIOL-VILLARD { 55576afd4fSJean-Christophe PLAGNIOL-VILLARD /* Load timer with initial value */ 56576afd4fSJean-Christophe PLAGNIOL-VILLARD *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 0) = TIMER_LOAD_VAL; 57576afd4fSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_ARCH_CINTEGRATOR 58576afd4fSJean-Christophe PLAGNIOL-VILLARD /* Set timer to be 59576afd4fSJean-Christophe PLAGNIOL-VILLARD * enabled 1 60576afd4fSJean-Christophe PLAGNIOL-VILLARD * periodic 1 61576afd4fSJean-Christophe PLAGNIOL-VILLARD * no interrupts 0 62576afd4fSJean-Christophe PLAGNIOL-VILLARD * X 0 63576afd4fSJean-Christophe PLAGNIOL-VILLARD * divider 1 00 == less rounding error 64576afd4fSJean-Christophe PLAGNIOL-VILLARD * 32 bit 1 65576afd4fSJean-Christophe PLAGNIOL-VILLARD * wrapping 0 66576afd4fSJean-Christophe PLAGNIOL-VILLARD */ 67576afd4fSJean-Christophe PLAGNIOL-VILLARD *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 8) = 0x000000C2; 68576afd4fSJean-Christophe PLAGNIOL-VILLARD #else 69576afd4fSJean-Christophe PLAGNIOL-VILLARD /* Set timer to be 70576afd4fSJean-Christophe PLAGNIOL-VILLARD * enabled 1 71576afd4fSJean-Christophe PLAGNIOL-VILLARD * free-running 0 72576afd4fSJean-Christophe PLAGNIOL-VILLARD * XX 00 73576afd4fSJean-Christophe PLAGNIOL-VILLARD * divider 256 10 74576afd4fSJean-Christophe PLAGNIOL-VILLARD * XX 00 75576afd4fSJean-Christophe PLAGNIOL-VILLARD */ 76576afd4fSJean-Christophe PLAGNIOL-VILLARD *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 8) = 0x00000088; 77576afd4fSJean-Christophe PLAGNIOL-VILLARD #endif 78576afd4fSJean-Christophe PLAGNIOL-VILLARD 79576afd4fSJean-Christophe PLAGNIOL-VILLARD /* init the timestamp */ 80576afd4fSJean-Christophe PLAGNIOL-VILLARD total_count = 0ULL; 8117659d7dSGraeme Russ /* capure current decrementer value */ 8217659d7dSGraeme Russ lastdec = READ_TIMER; 8317659d7dSGraeme Russ /* start "advancing" time stamp from 0 */ 8417659d7dSGraeme Russ timestamp = 0L; 85576afd4fSJean-Christophe PLAGNIOL-VILLARD 86576afd4fSJean-Christophe PLAGNIOL-VILLARD div_timer = CONFIG_SYS_HZ_CLOCK; 87576afd4fSJean-Christophe PLAGNIOL-VILLARD do_div(div_timer, CONFIG_SYS_HZ); 88576afd4fSJean-Christophe PLAGNIOL-VILLARD do_div(div_timer, div_clock); 89576afd4fSJean-Christophe PLAGNIOL-VILLARD 90576afd4fSJean-Christophe PLAGNIOL-VILLARD return (0); 91576afd4fSJean-Christophe PLAGNIOL-VILLARD } 92576afd4fSJean-Christophe PLAGNIOL-VILLARD 93576afd4fSJean-Christophe PLAGNIOL-VILLARD /* 94576afd4fSJean-Christophe PLAGNIOL-VILLARD * timer without interrupts 95576afd4fSJean-Christophe PLAGNIOL-VILLARD */ 96576afd4fSJean-Christophe PLAGNIOL-VILLARD ulong get_timer (ulong base_ticks) 97576afd4fSJean-Christophe PLAGNIOL-VILLARD { 98576afd4fSJean-Christophe PLAGNIOL-VILLARD return get_timer_masked () - base_ticks; 99576afd4fSJean-Christophe PLAGNIOL-VILLARD } 100576afd4fSJean-Christophe PLAGNIOL-VILLARD 101576afd4fSJean-Christophe PLAGNIOL-VILLARD /* delay usec useconds */ 1023eb90badSIngo van Lil void __udelay (unsigned long usec) 103576afd4fSJean-Christophe PLAGNIOL-VILLARD { 104576afd4fSJean-Christophe PLAGNIOL-VILLARD ulong tmo, tmp; 105576afd4fSJean-Christophe PLAGNIOL-VILLARD 106576afd4fSJean-Christophe PLAGNIOL-VILLARD /* Convert to U-Boot ticks */ 107576afd4fSJean-Christophe PLAGNIOL-VILLARD tmo = usec * CONFIG_SYS_HZ; 108576afd4fSJean-Christophe PLAGNIOL-VILLARD tmo /= (1000000L); 109576afd4fSJean-Christophe PLAGNIOL-VILLARD 110576afd4fSJean-Christophe PLAGNIOL-VILLARD tmp = get_timer_masked(); /* get current timestamp */ 111576afd4fSJean-Christophe PLAGNIOL-VILLARD tmo += tmp; /* form target timestamp */ 112576afd4fSJean-Christophe PLAGNIOL-VILLARD 113576afd4fSJean-Christophe PLAGNIOL-VILLARD while (get_timer_masked () < tmo) {/* loop till event */ 114576afd4fSJean-Christophe PLAGNIOL-VILLARD /*NOP*/; 115576afd4fSJean-Christophe PLAGNIOL-VILLARD } 116576afd4fSJean-Christophe PLAGNIOL-VILLARD } 117576afd4fSJean-Christophe PLAGNIOL-VILLARD 118576afd4fSJean-Christophe PLAGNIOL-VILLARD /* converts the timer reading to U-Boot ticks */ 119576afd4fSJean-Christophe PLAGNIOL-VILLARD /* the timestamp is the number of ticks since reset */ 120576afd4fSJean-Christophe PLAGNIOL-VILLARD ulong get_timer_masked (void) 121576afd4fSJean-Christophe PLAGNIOL-VILLARD { 122576afd4fSJean-Christophe PLAGNIOL-VILLARD /* get current count */ 123576afd4fSJean-Christophe PLAGNIOL-VILLARD unsigned long long now = READ_TIMER; 124576afd4fSJean-Christophe PLAGNIOL-VILLARD 125576afd4fSJean-Christophe PLAGNIOL-VILLARD if(now > lastdec) { 126576afd4fSJean-Christophe PLAGNIOL-VILLARD /* Must have wrapped */ 127576afd4fSJean-Christophe PLAGNIOL-VILLARD total_count += lastdec + TIMER_LOAD_VAL + 1 - now; 128576afd4fSJean-Christophe PLAGNIOL-VILLARD } else { 129576afd4fSJean-Christophe PLAGNIOL-VILLARD total_count += lastdec - now; 130576afd4fSJean-Christophe PLAGNIOL-VILLARD } 131576afd4fSJean-Christophe PLAGNIOL-VILLARD lastdec = now; 132576afd4fSJean-Christophe PLAGNIOL-VILLARD 133576afd4fSJean-Christophe PLAGNIOL-VILLARD /* Reuse "now" */ 134576afd4fSJean-Christophe PLAGNIOL-VILLARD now = total_count; 135576afd4fSJean-Christophe PLAGNIOL-VILLARD do_div(now, div_timer); 136576afd4fSJean-Christophe PLAGNIOL-VILLARD timestamp = now; 137576afd4fSJean-Christophe PLAGNIOL-VILLARD 138576afd4fSJean-Christophe PLAGNIOL-VILLARD return timestamp; 139576afd4fSJean-Christophe PLAGNIOL-VILLARD } 140576afd4fSJean-Christophe PLAGNIOL-VILLARD 141576afd4fSJean-Christophe PLAGNIOL-VILLARD /* waits specified delay value and resets timestamp */ 142576afd4fSJean-Christophe PLAGNIOL-VILLARD void udelay_masked (unsigned long usec) 143576afd4fSJean-Christophe PLAGNIOL-VILLARD { 144576afd4fSJean-Christophe PLAGNIOL-VILLARD udelay(usec); 145576afd4fSJean-Christophe PLAGNIOL-VILLARD } 146576afd4fSJean-Christophe PLAGNIOL-VILLARD 147576afd4fSJean-Christophe PLAGNIOL-VILLARD /* 148576afd4fSJean-Christophe PLAGNIOL-VILLARD * This function is derived from PowerPC code (read timebase as long long). 149576afd4fSJean-Christophe PLAGNIOL-VILLARD * On ARM it just returns the timer value. 150576afd4fSJean-Christophe PLAGNIOL-VILLARD */ 151576afd4fSJean-Christophe PLAGNIOL-VILLARD unsigned long long get_ticks(void) 152576afd4fSJean-Christophe PLAGNIOL-VILLARD { 153576afd4fSJean-Christophe PLAGNIOL-VILLARD return get_timer(0); 154576afd4fSJean-Christophe PLAGNIOL-VILLARD } 155576afd4fSJean-Christophe PLAGNIOL-VILLARD 156576afd4fSJean-Christophe PLAGNIOL-VILLARD /* 157576afd4fSJean-Christophe PLAGNIOL-VILLARD * Return the timebase clock frequency 158576afd4fSJean-Christophe PLAGNIOL-VILLARD * i.e. how often the timer decrements 159576afd4fSJean-Christophe PLAGNIOL-VILLARD */ 160576afd4fSJean-Christophe PLAGNIOL-VILLARD ulong get_tbclk (void) 161576afd4fSJean-Christophe PLAGNIOL-VILLARD { 162576afd4fSJean-Christophe PLAGNIOL-VILLARD unsigned long long tmp = CONFIG_SYS_HZ_CLOCK; 163576afd4fSJean-Christophe PLAGNIOL-VILLARD 164576afd4fSJean-Christophe PLAGNIOL-VILLARD do_div(tmp, div_clock); 165576afd4fSJean-Christophe PLAGNIOL-VILLARD 166576afd4fSJean-Christophe PLAGNIOL-VILLARD return tmp; 167576afd4fSJean-Christophe PLAGNIOL-VILLARD } 168