Patch by Andreas Engel, 16 Aug 2004:
[kernel/u-boot.git] / cpu / arm920t / s3c24x0_interrupts.c
1 /*
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 #if defined(CONFIG_S3C2400) || defined (CONFIG_S3C2410) || defined (CONFIG_TRAB)
34
35 #include <arm920t.h>
36 #if defined(CONFIG_S3C2400)
37 #include <s3c2400.h>
38 #elif defined(CONFIG_S3C2410)
39 #include <s3c2410.h>
40 #endif
41
42 extern void reset_cpu(ulong addr);
43 int timer_load_val = 0;
44
45 /* macro to read the 16 bit timer */
46 static inline ulong READ_TIMER(void)
47 {
48         S3C24X0_TIMERS * const timers = S3C24X0_GetBase_TIMERS();
49
50         return (timers->TCNTO4 & 0xffff);
51 }
52
53 static ulong timestamp;
54 static ulong lastdec;
55
56 int interrupt_init (void)
57 {
58         S3C24X0_TIMERS * const timers = S3C24X0_GetBase_TIMERS();
59
60         /* use PWM Timer 4 because it has no output */
61         /* prescaler for Timer 4 is 16 */
62         timers->TCFG0 = 0x0f00;
63         if (timer_load_val == 0)
64         {
65                 /*
66                  * for 10 ms clock period @ PCLK with 4 bit divider = 1/2
67                  * (default) and prescaler = 16. Should be 10390
68                  * @33.25MHz and 15625 @ 50 MHz
69                  */
70                 timer_load_val = get_PCLK()/(2 * 16 * 100);
71         }
72         /* load value for 10 ms timeout */
73         lastdec = timers->TCNTB4 = timer_load_val;
74         /* auto load, manual update of Timer 4 */
75         timers->TCON = (timers->TCON & ~0x0700000) | 0x600000;
76         /* auto load, start Timer 4 */
77         timers->TCON = (timers->TCON & ~0x0700000) | 0x500000;
78         timestamp = 0;
79
80         return (0);
81 }
82
83 /*
84  * timer without interrupts
85  */
86
87 void reset_timer (void)
88 {
89         reset_timer_masked ();
90 }
91
92 ulong get_timer (ulong base)
93 {
94         return get_timer_masked () - base;
95 }
96
97 void set_timer (ulong t)
98 {
99         timestamp = t;
100 }
101
102 void udelay (unsigned long usec)
103 {
104         ulong tmo;
105         ulong start = get_timer(0);
106
107         tmo = usec / 1000;
108         tmo *= (timer_load_val * 100);
109         tmo /= 1000;
110
111         while ((ulong)(get_timer_masked () - start) < tmo)
112                 /*NOP*/;
113 }
114
115 void reset_timer_masked (void)
116 {
117         /* reset time */
118         lastdec = READ_TIMER();
119         timestamp = 0;
120 }
121
122 ulong get_timer_masked (void)
123 {
124         ulong now = READ_TIMER();
125
126         if (lastdec >= now) {
127                 /* normal mode */
128                 timestamp += lastdec - now;
129         } else {
130                 /* we have an overflow ... */
131                 timestamp += lastdec + timer_load_val - now;
132         }
133         lastdec = now;
134
135         return timestamp;
136 }
137
138 void udelay_masked (unsigned long usec)
139 {
140         ulong tmo;
141
142         tmo = usec / 1000;
143         tmo *= (timer_load_val * 100);
144         tmo /= 1000;
145
146         reset_timer_masked ();
147
148         while (get_timer_masked () < tmo)
149                 /*NOP*/;
150 }
151
152 /*
153  * This function is derived from PowerPC code (read timebase as long long).
154  * On ARM it just returns the timer value.
155  */
156 unsigned long long get_ticks(void)
157 {
158         return get_timer(0);
159 }
160
161 /*
162  * This function is derived from PowerPC code (timebase clock frequency).
163  * On ARM it returns the number of timer ticks per second.
164  */
165 ulong get_tbclk (void)
166 {
167         ulong tbclk;
168
169 #if defined(CONFIG_SMDK2400) || defined(CONFIG_TRAB)
170         tbclk = timer_load_val * 100;
171 #elif defined(CONFIG_SMDK2410) || defined(CONFIG_VCMA9)
172         tbclk = CFG_HZ;
173 #else
174 #       error "tbclk not configured"
175 #endif
176
177         return tbclk;
178 }
179
180 #endif /* defined(CONFIG_S3C2400) || defined (CONFIG_S3C2410) || defined (CONFIG_TRAB) */