xref: /openbmc/u-boot/board/armltd/integrator/timer.c (revision cf033e04)
183d290c5STom 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 */
timer_init(void)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  */
96*6180ea7eSPatrick Delaunay 
97*6180ea7eSPatrick Delaunay /* converts the timer reading to U-Boot ticks	       */
98*6180ea7eSPatrick Delaunay /* the timestamp is the number of ticks since reset    */
get_timer_masked(void)99*6180ea7eSPatrick Delaunay static ulong get_timer_masked (void)
100*6180ea7eSPatrick Delaunay {
101*6180ea7eSPatrick Delaunay 	/* get current count */
102*6180ea7eSPatrick Delaunay 	unsigned long long now = READ_TIMER;
103*6180ea7eSPatrick Delaunay 
104*6180ea7eSPatrick Delaunay 	if(now > lastdec) {
105*6180ea7eSPatrick Delaunay 		/* Must have wrapped */
106*6180ea7eSPatrick Delaunay 		total_count += lastdec + TIMER_LOAD_VAL + 1 - now;
107*6180ea7eSPatrick Delaunay 	} else {
108*6180ea7eSPatrick Delaunay 		total_count += lastdec - now;
109*6180ea7eSPatrick Delaunay 	}
110*6180ea7eSPatrick Delaunay 	lastdec	= now;
111*6180ea7eSPatrick Delaunay 
112*6180ea7eSPatrick Delaunay 	/* Reuse "now" */
113*6180ea7eSPatrick Delaunay 	now = total_count;
114*6180ea7eSPatrick Delaunay 	do_div(now, div_timer);
115*6180ea7eSPatrick Delaunay 	timestamp = now;
116*6180ea7eSPatrick Delaunay 
117*6180ea7eSPatrick Delaunay 	return timestamp;
118*6180ea7eSPatrick Delaunay }
119*6180ea7eSPatrick Delaunay 
get_timer(ulong base_ticks)120576afd4fSJean-Christophe PLAGNIOL-VILLARD ulong get_timer (ulong base_ticks)
121576afd4fSJean-Christophe PLAGNIOL-VILLARD {
122576afd4fSJean-Christophe PLAGNIOL-VILLARD 	return get_timer_masked () - base_ticks;
123576afd4fSJean-Christophe PLAGNIOL-VILLARD }
124576afd4fSJean-Christophe PLAGNIOL-VILLARD 
125576afd4fSJean-Christophe PLAGNIOL-VILLARD /* delay usec useconds */
__udelay(unsigned long usec)1263eb90badSIngo van Lil void __udelay (unsigned long usec)
127576afd4fSJean-Christophe PLAGNIOL-VILLARD {
128576afd4fSJean-Christophe PLAGNIOL-VILLARD 	ulong tmo, tmp;
129576afd4fSJean-Christophe PLAGNIOL-VILLARD 
130576afd4fSJean-Christophe PLAGNIOL-VILLARD 	/* Convert to U-Boot ticks */
131576afd4fSJean-Christophe PLAGNIOL-VILLARD 	tmo  = usec * CONFIG_SYS_HZ;
132576afd4fSJean-Christophe PLAGNIOL-VILLARD 	tmo /= (1000000L);
133576afd4fSJean-Christophe PLAGNIOL-VILLARD 
134576afd4fSJean-Christophe PLAGNIOL-VILLARD 	tmp  = get_timer_masked();	/* get current timestamp */
135576afd4fSJean-Christophe PLAGNIOL-VILLARD 	tmo += tmp;			/* form target timestamp */
136576afd4fSJean-Christophe PLAGNIOL-VILLARD 
137576afd4fSJean-Christophe PLAGNIOL-VILLARD 	while (get_timer_masked () < tmo) {/* loop till event */
138576afd4fSJean-Christophe PLAGNIOL-VILLARD 		/*NOP*/;
139576afd4fSJean-Christophe PLAGNIOL-VILLARD 	}
140576afd4fSJean-Christophe PLAGNIOL-VILLARD }
141576afd4fSJean-Christophe PLAGNIOL-VILLARD 
142576afd4fSJean-Christophe PLAGNIOL-VILLARD /*
143576afd4fSJean-Christophe PLAGNIOL-VILLARD  * This function is derived from PowerPC code (read timebase as long long).
144576afd4fSJean-Christophe PLAGNIOL-VILLARD  * On ARM it just returns the timer value.
145576afd4fSJean-Christophe PLAGNIOL-VILLARD  */
get_ticks(void)146576afd4fSJean-Christophe PLAGNIOL-VILLARD unsigned long long get_ticks(void)
147576afd4fSJean-Christophe PLAGNIOL-VILLARD {
148576afd4fSJean-Christophe PLAGNIOL-VILLARD 	return get_timer(0);
149576afd4fSJean-Christophe PLAGNIOL-VILLARD }
150576afd4fSJean-Christophe PLAGNIOL-VILLARD 
151576afd4fSJean-Christophe PLAGNIOL-VILLARD /*
152576afd4fSJean-Christophe PLAGNIOL-VILLARD  * Return the timebase clock frequency
153576afd4fSJean-Christophe PLAGNIOL-VILLARD  * i.e. how often the timer decrements
154576afd4fSJean-Christophe PLAGNIOL-VILLARD  */
get_tbclk(void)155576afd4fSJean-Christophe PLAGNIOL-VILLARD ulong get_tbclk (void)
156576afd4fSJean-Christophe PLAGNIOL-VILLARD {
157576afd4fSJean-Christophe PLAGNIOL-VILLARD 	unsigned long long tmp = CONFIG_SYS_HZ_CLOCK;
158576afd4fSJean-Christophe PLAGNIOL-VILLARD 
159576afd4fSJean-Christophe PLAGNIOL-VILLARD 	do_div(tmp, div_clock);
160576afd4fSJean-Christophe PLAGNIOL-VILLARD 
161576afd4fSJean-Christophe PLAGNIOL-VILLARD 	return tmp;
162576afd4fSJean-Christophe PLAGNIOL-VILLARD }
163