blob: 2b2208c9bb32016ba27451a6b6ad5508ea95de0b [file] [log] [blame]
Alessandro Rubinibb930d12009-01-24 18:10:37 +01001/*
2 * (C) Copyright 2003
3 * Texas Instruments <www.ti.com>
4 *
5 * (C) Copyright 2002
6 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Marius Groeger <mgroeger@sysgo.de>
8 *
9 * (C) Copyright 2002
10 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
11 * Alex Zuepke <azu@sysgo.de>
12 *
13 * (C) Copyright 2002-2004
14 * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
15 *
16 * (C) Copyright 2004
17 * Philippe Robin, ARM Ltd. <philippe.robin@arm.com>
18 *
19 * See file CREDITS for list of people who contributed to this
20 * project.
21 *
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License as
24 * published by the Free Software Foundation; either version 2 of
25 * the License, or (at your option) any later version.
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
35 * MA 02111-1307 USA
36 */
37
38#include <common.h>
39#include <asm/io.h>
40#include <arm926ejs.h>
41
42#define TIMER_LOAD_VAL 0xffffffff
43
44/* macro to read the 32 bit timer */
45#define READ_TIMER readl(CONFIG_SYS_TIMERBASE + 20)
46
47static ulong timestamp;
48static ulong lastdec;
49
50/* nothing really to do with interrupts, just starts up a counter. */
51int timer_init(void)
52{
53 /* Load timer with initial value */
54 writel(TIMER_LOAD_VAL, CONFIG_SYS_TIMERBASE + 16);
55
56 /*
57 * Set timer to be enabled, free-running, no interrupts, 256 divider,
58 * 32-bit, wrap-mode
59 */
60 writel(0x8a, CONFIG_SYS_TIMERBASE + 24);
61
62 /* init the timestamp and lastdec value */
63 reset_timer_masked();
64
65 return 0;
66}
67
68/*
69 * timer without interrupts
70 */
71void reset_timer(void)
72{
73 reset_timer_masked();
74}
75
76ulong get_timer(ulong base)
77{
78 return get_timer_masked() - base;
79}
80
81void set_timer(ulong t)
82{
83 timestamp = t;
84}
85
86/* delay x useconds AND perserve advance timstamp value */
87void udelay(unsigned long usec)
88{
89 ulong tmo, tmp;
90
91 if (usec >= 1000) {
92 /* if "big" number, spread normalization to seconds */
93 tmo = usec / 1000; /* start to normalize */
94 tmo *= CONFIG_SYS_HZ; /* find number of "ticks" */
95 tmo /= 1000; /* finish normalize. */
96 } else {
97 /* small number, don't kill it prior to HZ multiply */
98 tmo = usec * CONFIG_SYS_HZ;
99 tmo /= (1000 * 1000);
100 }
101
102 tmp = get_timer(0); /* get current timestamp */
103 if ((tmo + tmp + 1) < tmp) /* will roll time stamp? */
104 reset_timer_masked(); /* reset to 0, set lastdec value */
105 else
106 tmo += tmp;
107
108 while (get_timer_masked() < tmo)
109 /* nothing */ ;
110}
111
112void reset_timer_masked(void)
113{
114 /* reset time */
115 lastdec = READ_TIMER; /* capure current decrementer value time */
116 timestamp = 0; /* start "advancing" time stamp from 0 */
117}
118
119ulong get_timer_masked(void)
120{
121 ulong now = READ_TIMER; /* current tick value */
122
123 if (lastdec >= now) { /* normal mode (non roll) */
124 /* move stamp fordward */
125 timestamp += lastdec - now;
126 } else {
127 /*
128 * An overflow is expected.
129 * nts = ts + ld + (TLV - now)
130 * ts=old stamp, ld=time that passed before passing through -1
131 * (TLV-now) amount of time after passing though -1
132 * nts = new "advancing time stamp"...it could also roll
133 */
134 timestamp += lastdec + TIMER_LOAD_VAL - now;
135 }
136 lastdec = now;
137
138 return timestamp;
139}
140
141/* waits specified delay value and resets timestamp */
142void udelay_masked(unsigned long usec)
143{
144 ulong tmo;
145
146 if (usec >= 1000) {
147 /* if "big" number, spread normalization to seconds */
148 tmo = usec / 1000; /* start to normalize */
149 tmo *= CONFIG_SYS_HZ; /* find number of "ticks" */
150 tmo /= 1000; /* finish normalize. */
151 } else {
152 /* else small number, don't kill it prior to HZ multiply */
153 tmo = usec * CONFIG_SYS_HZ;
154 tmo /= (1000*1000);
155 }
156
157 reset_timer_masked();
158 /* set "advancing" timestamp to 0, set lastdec vaule */
159
160 while (get_timer_masked() < tmo)
161 /* nothing */ ;
162}
163
164/*
165 * This function is derived from PowerPC code (read timebase as long long).
166 * On ARM it just returns the timer value.
167 */
168unsigned long long get_ticks(void)
169{
170 return get_timer(0);
171}
172
173/*
174 * This function is derived from PowerPC code (timebase clock frequency).
175 * On ARM it returns the number of timer ticks per second.
176 */
177ulong get_tbclk(void)
178{
179 ulong tbclk;
180
181 tbclk = CONFIG_SYS_HZ;
182 return tbclk;
183}