blob: f9b5be01042d84a6f4b3a01cf8ea4c25fa4a31ce [file] [log] [blame]
wdenkf39748a2004-06-09 13:37:52 +00001/*
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 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8 * Alex Zuepke <azu@sysgo.de>
9 *
10 * (C) Copyright 2002
Detlev Zundel792a09e2009-05-13 10:54:10 +020011 * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
wdenkf39748a2004-06-09 13:37:52 +000012 *
13 * See file CREDITS for list of people who contributed to this
14 * project.
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 * MA 02111-1307 USA
30 */
31
32#include <common.h>
wdenkf39748a2004-06-09 13:37:52 +000033#include <lh7a40x.h>
34
wdenkf39748a2004-06-09 13:37:52 +000035static ulong timer_load_val = 0;
36
37/* macro to read the 16 bit timer */
38static inline ulong READ_TIMER(void)
39{
wdenkf832d8a2004-06-10 21:55:33 +000040 lh7a40x_timers_t* timers = LH7A40X_TIMERS_PTR;
wdenkf39748a2004-06-09 13:37:52 +000041 lh7a40x_timer_t* timer = &timers->timer1;
42
43 return (timer->value & 0x0000ffff);
44}
45
wdenkf39748a2004-06-09 13:37:52 +000046static ulong timestamp;
47static ulong lastdec;
48
Jean-Christophe PLAGNIOL-VILLARDb54384e2009-05-15 23:47:02 +020049int timer_init (void)
wdenkf39748a2004-06-09 13:37:52 +000050{
wdenkf832d8a2004-06-10 21:55:33 +000051 lh7a40x_timers_t* timers = LH7A40X_TIMERS_PTR;
wdenkf39748a2004-06-09 13:37:52 +000052 lh7a40x_timer_t* timer = &timers->timer1;
53
54 /* a periodic timer using the 508kHz source */
55 timer->control = (TIMER_PER | TIMER_CLK508K);
56
57 if (timer_load_val == 0) {
58 /*
59 * 10ms period with 508.469kHz clock = 5084
60 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020061 timer_load_val = CONFIG_SYS_HZ/100;
wdenkf39748a2004-06-09 13:37:52 +000062 }
63
64 /* load value for 10 ms timeout */
65 lastdec = timer->load = timer_load_val;
66
67 /* auto load, start timer */
68 timer->control = timer->control | TIMER_EN;
69 timestamp = 0;
70
71 return (0);
72}
73
74/*
75 * timer without interrupts
76 */
77
78void reset_timer (void)
79{
80 reset_timer_masked ();
81}
82
83ulong get_timer (ulong base)
84{
85 return (get_timer_masked() - base);
86}
87
88void set_timer (ulong t)
89{
90 timestamp = t;
91}
92
93void udelay (unsigned long usec)
94{
95 ulong tmo,tmp;
96
97 /* normalize */
98 if (usec >= 1000) {
99 tmo = usec / 1000;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200100 tmo *= CONFIG_SYS_HZ;
wdenkf39748a2004-06-09 13:37:52 +0000101 tmo /= 1000;
102 }
103 else {
104 if (usec > 1) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200105 tmo = usec * CONFIG_SYS_HZ;
wdenkf39748a2004-06-09 13:37:52 +0000106 tmo /= (1000*1000);
107 }
108 else
109 tmo = 1;
110 }
111
112 /* check for rollover during this delay */
113 tmp = get_timer (0);
114 if ((tmp + tmo) < tmp )
115 reset_timer_masked(); /* timer would roll over */
116 else
117 tmo += tmp;
118
119 while (get_timer_masked () < tmo);
120}
121
122void reset_timer_masked (void)
123{
124 /* reset time */
125 lastdec = READ_TIMER();
126 timestamp = 0;
127}
128
129ulong get_timer_masked (void)
130{
131 ulong now = READ_TIMER();
132
133 if (lastdec >= now) {
134 /* normal mode */
135 timestamp += (lastdec - now);
136 } else {
137 /* we have an overflow ... */
138 timestamp += ((lastdec + timer_load_val) - now);
139 }
140 lastdec = now;
141
142 return timestamp;
143}
144
145void udelay_masked (unsigned long usec)
146{
147 ulong tmo;
wdenk101e8df2005-04-04 12:08:28 +0000148 ulong endtime;
149 signed long diff;
wdenkf39748a2004-06-09 13:37:52 +0000150
151 /* normalize */
152 if (usec >= 1000) {
153 tmo = usec / 1000;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200154 tmo *= CONFIG_SYS_HZ;
wdenkf39748a2004-06-09 13:37:52 +0000155 tmo /= 1000;
wdenk101e8df2005-04-04 12:08:28 +0000156 } else {
wdenkf39748a2004-06-09 13:37:52 +0000157 if (usec > 1) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200158 tmo = usec * CONFIG_SYS_HZ;
wdenkf39748a2004-06-09 13:37:52 +0000159 tmo /= (1000*1000);
wdenk101e8df2005-04-04 12:08:28 +0000160 } else {
wdenkf39748a2004-06-09 13:37:52 +0000161 tmo = 1;
wdenk101e8df2005-04-04 12:08:28 +0000162 }
wdenkf39748a2004-06-09 13:37:52 +0000163 }
164
wdenk101e8df2005-04-04 12:08:28 +0000165 endtime = get_timer_masked () + tmo;
wdenkf39748a2004-06-09 13:37:52 +0000166
wdenk101e8df2005-04-04 12:08:28 +0000167 do {
168 ulong now = get_timer_masked ();
169 diff = endtime - now;
170 } while (diff >= 0);
wdenkf39748a2004-06-09 13:37:52 +0000171}
172
173/*
174 * This function is derived from PowerPC code (read timebase as long long).
175 * On ARM it just returns the timer value.
176 */
177unsigned long long get_ticks(void)
178{
179 return get_timer(0);
180}
181
182/*
183 * This function is derived from PowerPC code (timebase clock frequency).
184 * On ARM it returns the number of timer ticks per second.
185 */
186ulong get_tbclk (void)
187{
188 ulong tbclk;
189
190 tbclk = timer_load_val * 100;
191
192 return tbclk;
193}