blob: 741035cc6cb25f0473b8e7ee0fb11fd93eb961f0 [file] [log] [blame]
Jean-Christophe PLAGNIOL-VILLARD3c9dc3b2009-05-17 00:58:36 +02001/*
2 * (C) Copyright 2002
3 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
4 * Marius Groeger <mgroeger@sysgo.de>
5 *
6 * (C) Copyright 2002
7 * David Mueller, ELSOFT AG, <d.mueller@elsoft.ch>
8 *
9 * (C) Copyright 2003
10 * Texas Instruments, <www.ti.com>
11 * Kshitij Gupta <Kshitij@ti.com>
12 *
13 * (C) Copyright 2004
14 * ARM Ltd.
15 * Philippe Robin, <philippe.robin@arm.com>
16 *
17 * See file CREDITS for list of people who contributed to this
18 * project.
19 *
20 * This program is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU General Public License as
22 * published by the Free Software Foundation; either version 2 of
23 * the License, or (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
33 * MA 02111-1307 USA
34 */
35
36#include <common.h>
37#include <div64.h>
38
39/* The Integrator/CP timer1 is clocked at 1MHz
40 * can be divided by 16 or 256
41 * and can be set up as a 32-bit timer
42 */
43/* U-Boot expects a 32 bit timer, running at CONFIG_SYS_HZ */
44/* Keep total timer count to avoid losing decrements < div_timer */
45static unsigned long long total_count = 0;
46static unsigned long long lastdec; /* Timer reading at last call */
47static unsigned long long div_clock = 1; /* Divisor applied to timer clock */
48static unsigned long long div_timer = 1; /* Divisor to convert timer reading
49 * change to U-Boot ticks
50 */
51/* CONFIG_SYS_HZ = CONFIG_SYS_HZ_CLOCK/(div_clock * div_timer) */
52static ulong timestamp; /* U-Boot ticks since startup */
53
54#define TIMER_LOAD_VAL ((ulong)0xFFFFFFFF)
55#define READ_TIMER (*(volatile ulong *)(CONFIG_SYS_TIMERBASE+4))
56
57/* all function return values in U-Boot ticks i.e. (1/CONFIG_SYS_HZ) sec
58 * - unless otherwise stated
59 */
60
61/* starts up a counter
62 * - the Integrator/CP timer can be set up to issue an interrupt */
63int timer_init (void)
64{
65 /* Load timer with initial value */
66 *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 0) = TIMER_LOAD_VAL;
67 /* Set timer to be
68 * enabled 1
69 * periodic 1
70 * no interrupts 0
71 * X 0
72 * divider 1 00 == less rounding error
73 * 32 bit 1
74 * wrapping 0
75 */
76 *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 8) = 0x000000C2;
77 /* init the timestamp */
78 total_count = 0ULL;
79 reset_timer_masked();
80
81 div_timer = (unsigned long long)(CONFIG_SYS_HZ_CLOCK / CONFIG_SYS_HZ);
82 div_timer /= div_clock;
83
84 return (0);
85}
86
87/*
88 * timer without interrupts
89 */
90void reset_timer (void)
91{
92 reset_timer_masked ();
93}
94
95ulong get_timer (ulong base_ticks)
96{
97 return get_timer_masked () - base_ticks;
98}
99
100void set_timer (ulong ticks)
101{
102 timestamp = ticks;
103 total_count = (unsigned long long)ticks * div_timer;
104}
105
106/* delay usec useconds */
107void udelay (unsigned long usec)
108{
109 ulong tmo, tmp;
110
111 /* Convert to U-Boot ticks */
112 tmo = usec * CONFIG_SYS_HZ;
113 tmo /= (1000000L);
114
115 tmp = get_timer_masked(); /* get current timestamp */
116 tmo += tmp; /* form target timestamp */
117
118 while (get_timer_masked () < tmo) {/* loop till event */
119 /*NOP*/;
120 }
121}
122
123void reset_timer_masked (void)
124{
125 /* capure current decrementer value */
126 lastdec = (unsigned long long)READ_TIMER;
127 /* start "advancing" time stamp from 0 */
128 timestamp = 0L;
129}
130
131/* converts the timer reading to U-Boot ticks */
132/* the timestamp is the number of ticks since reset */
133ulong get_timer_masked (void)
134{
135 /* get current count */
136 unsigned long long now = (unsigned long long)READ_TIMER;
137
138 if(now > lastdec) {
139 /* Must have wrapped */
140 total_count += lastdec + TIMER_LOAD_VAL + 1 - now;
141 } else {
142 total_count += lastdec - now;
143 }
144 lastdec = now;
145
146 /* Reuse "now" */
147 now = total_count;
148 do_div(now, div_timer);
149 timestamp = now;
150
151 return timestamp;
152}
153
154/* waits specified delay value and resets timestamp */
155void udelay_masked (unsigned long usec)
156{
157 udelay(usec);
158}
159
160/*
161 * This function is derived from PowerPC code (read timebase as long long).
162 * On ARM it just returns the timer value.
163 */
164unsigned long long get_ticks(void)
165{
166 return (unsigned long long)get_timer(0);
167}
168
169/*
170 * Return the timebase clock frequency
171 * i.e. how often the timer decrements
172 */
173ulong get_tbclk (void)
174{
175 return (ulong)(((unsigned long long)CONFIG_SYS_HZ_CLOCK)/div_clock);
176}