Timer: Remove set_timer completely
[platform/kernel/u-boot.git] / arch / arm / cpu / arm920t / s3c24x0 / timer.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, <garyj@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 #ifdef CONFIG_S3C24X0
34
35 #include <asm/io.h>
36 #include <asm/arch/s3c24x0_cpu.h>
37
38 int timer_load_val = 0;
39 static ulong timer_clk;
40
41 /* macro to read the 16 bit timer */
42 static inline ulong READ_TIMER(void)
43 {
44         struct s3c24x0_timers *timers = s3c24x0_get_base_timers();
45
46         return readl(&timers->tcnto4) & 0xffff;
47 }
48
49 static ulong timestamp;
50 static ulong lastdec;
51
52 int timer_init(void)
53 {
54         struct s3c24x0_timers *timers = s3c24x0_get_base_timers();
55         ulong tmr;
56
57         /* use PWM Timer 4 because it has no output */
58         /* prescaler for Timer 4 is 16 */
59         writel(0x0f00, &timers->tcfg0);
60         if (timer_load_val == 0) {
61                 /*
62                  * for 10 ms clock period @ PCLK with 4 bit divider = 1/2
63                  * (default) and prescaler = 16. Should be 10390
64                  * @33.25MHz and 15625 @ 50 MHz
65                  */
66                 timer_load_val = get_PCLK() / (2 * 16 * 100);
67                 timer_clk = get_PCLK() / (2 * 16);
68         }
69         /* load value for 10 ms timeout */
70         lastdec = timer_load_val;
71         writel(timer_load_val, &timers->tcntb4);
72         /* auto load, manual update of timer 4 */
73         tmr = (readl(&timers->tcon) & ~0x0700000) | 0x0600000;
74         writel(tmr, &timers->tcon);
75         /* auto load, start timer 4 */
76         tmr = (tmr & ~0x0700000) | 0x0500000;
77         writel(tmr, &timers->tcon);
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 __udelay (unsigned long usec)
98 {
99         ulong tmo;
100         ulong start = get_ticks();
101
102         tmo = usec / 1000;
103         tmo *= (timer_load_val * 100);
104         tmo /= 1000;
105
106         while ((ulong) (get_ticks() - start) < tmo)
107                 /*NOP*/;
108 }
109
110 void reset_timer_masked(void)
111 {
112         /* reset time */
113         lastdec = READ_TIMER();
114         timestamp = 0;
115 }
116
117 ulong get_timer_masked(void)
118 {
119         ulong tmr = get_ticks();
120
121         return tmr / (timer_clk / CONFIG_SYS_HZ);
122 }
123
124 void udelay_masked(unsigned long usec)
125 {
126         ulong tmo;
127         ulong endtime;
128         signed long diff;
129
130         if (usec >= 1000) {
131                 tmo = usec / 1000;
132                 tmo *= (timer_load_val * 100);
133                 tmo /= 1000;
134         } else {
135                 tmo = usec * (timer_load_val * 100);
136                 tmo /= (1000 * 1000);
137         }
138
139         endtime = get_ticks() + tmo;
140
141         do {
142                 ulong now = get_ticks();
143                 diff = endtime - now;
144         } while (diff >= 0);
145 }
146
147 /*
148  * This function is derived from PowerPC code (read timebase as long long).
149  * On ARM it just returns the timer value.
150  */
151 unsigned long long get_ticks(void)
152 {
153         ulong now = READ_TIMER();
154
155         if (lastdec >= now) {
156                 /* normal mode */
157                 timestamp += lastdec - now;
158         } else {
159                 /* we have an overflow ... */
160                 timestamp += lastdec + timer_load_val - now;
161         }
162         lastdec = now;
163
164         return timestamp;
165 }
166
167 /*
168  * This function is derived from PowerPC code (timebase clock frequency).
169  * On ARM it returns the number of timer ticks per second.
170  */
171 ulong get_tbclk(void)
172 {
173         ulong tbclk;
174
175 #if defined(CONFIG_SMDK2400)
176         tbclk = timer_load_val * 100;
177 #elif defined(CONFIG_SBC2410X) || \
178       defined(CONFIG_SMDK2410) || \
179         defined(CONFIG_S3C2440) || \
180       defined(CONFIG_VCMA9)
181         tbclk = CONFIG_SYS_HZ;
182 #else
183 #       error "tbclk not configured"
184 #endif
185
186         return tbclk;
187 }
188
189 /*
190  * reset the cpu by setting up the watchdog timer and let him time out
191  */
192 void reset_cpu(ulong ignored)
193 {
194         struct s3c24x0_watchdog *watchdog;
195
196         watchdog = s3c24x0_get_base_watchdog();
197
198         /* Disable watchdog */
199         writel(0x0000, &watchdog->wtcon);
200
201         /* Initialize watchdog timer count register */
202         writel(0x0001, &watchdog->wtcnt);
203
204         /* Enable watchdog timer; assert reset at timer timeout */
205         writel(0x0021, &watchdog->wtcon);
206
207         while (1)
208                 /* loop forever and wait for reset to happen */;
209
210         /*NOTREACHED*/
211 }
212
213 #endif /* CONFIG_S3C24X0 */