blob: 5acfe1a44976d8b9e1e95493026657b844505544 [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
11 * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
12 *
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>
33#include <arm920t.h>
34#include <lh7a40x.h>
35
wdenkf39748a2004-06-09 13:37:52 +000036static ulong timer_load_val = 0;
37
38/* macro to read the 16 bit timer */
39static inline ulong READ_TIMER(void)
40{
wdenkf832d8a2004-06-10 21:55:33 +000041 lh7a40x_timers_t* timers = LH7A40X_TIMERS_PTR;
wdenkf39748a2004-06-09 13:37:52 +000042 lh7a40x_timer_t* timer = &timers->timer1;
43
44 return (timer->value & 0x0000ffff);
45}
46
wdenkf39748a2004-06-09 13:37:52 +000047static ulong timestamp;
48static ulong lastdec;
49
50int interrupt_init (void)
51{
wdenkf832d8a2004-06-10 21:55:33 +000052 lh7a40x_timers_t* timers = LH7A40X_TIMERS_PTR;
wdenkf39748a2004-06-09 13:37:52 +000053 lh7a40x_timer_t* timer = &timers->timer1;
54
55 /* a periodic timer using the 508kHz source */
56 timer->control = (TIMER_PER | TIMER_CLK508K);
57
58 if (timer_load_val == 0) {
59 /*
60 * 10ms period with 508.469kHz clock = 5084
61 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020062 timer_load_val = CONFIG_SYS_HZ/100;
wdenkf39748a2004-06-09 13:37:52 +000063 }
64
65 /* load value for 10 ms timeout */
66 lastdec = timer->load = timer_load_val;
67
68 /* auto load, start timer */
69 timer->control = timer->control | TIMER_EN;
70 timestamp = 0;
71
72 return (0);
73}
74
75/*
76 * timer without interrupts
77 */
78
79void reset_timer (void)
80{
81 reset_timer_masked ();
82}
83
84ulong get_timer (ulong base)
85{
86 return (get_timer_masked() - base);
87}
88
89void set_timer (ulong t)
90{
91 timestamp = t;
92}
93
94void udelay (unsigned long usec)
95{
96 ulong tmo,tmp;
97
98 /* normalize */
99 if (usec >= 1000) {
100 tmo = usec / 1000;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200101 tmo *= CONFIG_SYS_HZ;
wdenkf39748a2004-06-09 13:37:52 +0000102 tmo /= 1000;
103 }
104 else {
105 if (usec > 1) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200106 tmo = usec * CONFIG_SYS_HZ;
wdenkf39748a2004-06-09 13:37:52 +0000107 tmo /= (1000*1000);
108 }
109 else
110 tmo = 1;
111 }
112
113 /* check for rollover during this delay */
114 tmp = get_timer (0);
115 if ((tmp + tmo) < tmp )
116 reset_timer_masked(); /* timer would roll over */
117 else
118 tmo += tmp;
119
120 while (get_timer_masked () < tmo);
121}
122
123void reset_timer_masked (void)
124{
125 /* reset time */
126 lastdec = READ_TIMER();
127 timestamp = 0;
128}
129
130ulong get_timer_masked (void)
131{
132 ulong now = READ_TIMER();
133
134 if (lastdec >= now) {
135 /* normal mode */
136 timestamp += (lastdec - now);
137 } else {
138 /* we have an overflow ... */
139 timestamp += ((lastdec + timer_load_val) - now);
140 }
141 lastdec = now;
142
143 return timestamp;
144}
145
146void udelay_masked (unsigned long usec)
147{
148 ulong tmo;
wdenk101e8df2005-04-04 12:08:28 +0000149 ulong endtime;
150 signed long diff;
wdenkf39748a2004-06-09 13:37:52 +0000151
152 /* normalize */
153 if (usec >= 1000) {
154 tmo = usec / 1000;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200155 tmo *= CONFIG_SYS_HZ;
wdenkf39748a2004-06-09 13:37:52 +0000156 tmo /= 1000;
wdenk101e8df2005-04-04 12:08:28 +0000157 } else {
wdenkf39748a2004-06-09 13:37:52 +0000158 if (usec > 1) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200159 tmo = usec * CONFIG_SYS_HZ;
wdenkf39748a2004-06-09 13:37:52 +0000160 tmo /= (1000*1000);
wdenk101e8df2005-04-04 12:08:28 +0000161 } else {
wdenkf39748a2004-06-09 13:37:52 +0000162 tmo = 1;
wdenk101e8df2005-04-04 12:08:28 +0000163 }
wdenkf39748a2004-06-09 13:37:52 +0000164 }
165
wdenk101e8df2005-04-04 12:08:28 +0000166 endtime = get_timer_masked () + tmo;
wdenkf39748a2004-06-09 13:37:52 +0000167
wdenk101e8df2005-04-04 12:08:28 +0000168 do {
169 ulong now = get_timer_masked ();
170 diff = endtime - now;
171 } while (diff >= 0);
wdenkf39748a2004-06-09 13:37:52 +0000172}
173
174/*
175 * This function is derived from PowerPC code (read timebase as long long).
176 * On ARM it just returns the timer value.
177 */
178unsigned long long get_ticks(void)
179{
180 return get_timer(0);
181}
182
183/*
184 * This function is derived from PowerPC code (timebase clock frequency).
185 * On ARM it returns the number of timer ticks per second.
186 */
187ulong get_tbclk (void)
188{
189 ulong tbclk;
190
191 tbclk = timer_load_val * 100;
192
193 return tbclk;
194}