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