blob: 20cedd463967f2339736d110d50874541a5dbb56 [file] [log] [blame]
wdenk281e00a2004-08-01 22:48:16 +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>
wdenk281e00a2004-08-01 22:48:16 +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>
kevin.morfitt@fearnside-systems.co.ukd67cce22009-10-10 13:30:22 +090033#if defined(CONFIG_S3C2400) || \
34 defined(CONFIG_S3C2410) || \
35 defined(CONFIG_TRAB)
36
37#include <asm/io.h>
wdenk281e00a2004-08-01 22:48:16 +000038
wdenk281e00a2004-08-01 22:48:16 +000039#if defined(CONFIG_S3C2400)
40#include <s3c2400.h>
41#elif defined(CONFIG_S3C2410)
42#include <s3c2410.h>
43#endif
44
wdenk281e00a2004-08-01 22:48:16 +000045int timer_load_val = 0;
kevin.morfitt@fearnside-systems.co.ukcd856622009-09-06 00:33:13 +090046static ulong timer_clk;
wdenk281e00a2004-08-01 22:48:16 +000047
48/* macro to read the 16 bit timer */
49static inline ulong READ_TIMER(void)
50{
kevin.morfitt@fearnside-systems.co.ukd67cce22009-10-10 13:30:22 +090051 struct s3c24x0_timers *timers = s3c24x0_get_base_timers();
wdenk281e00a2004-08-01 22:48:16 +000052
kevin.morfitt@fearnside-systems.co.ukd67cce22009-10-10 13:30:22 +090053 return readl(&timers->TCNTO4) & 0xffff;
wdenk281e00a2004-08-01 22:48:16 +000054}
55
56static ulong timestamp;
57static ulong lastdec;
58
kevin.morfitt@fearnside-systems.co.ukd67cce22009-10-10 13:30:22 +090059int timer_init(void)
wdenk281e00a2004-08-01 22:48:16 +000060{
kevin.morfitt@fearnside-systems.co.ukd67cce22009-10-10 13:30:22 +090061 struct s3c24x0_timers *timers = s3c24x0_get_base_timers();
62 ulong tmr;
wdenk281e00a2004-08-01 22:48:16 +000063
64 /* use PWM Timer 4 because it has no output */
65 /* prescaler for Timer 4 is 16 */
kevin.morfitt@fearnside-systems.co.ukd67cce22009-10-10 13:30:22 +090066 writel(0x0f00, &timers->TCFG0);
67 if (timer_load_val == 0) {
wdenk281e00a2004-08-01 22:48:16 +000068 /*
69 * for 10 ms clock period @ PCLK with 4 bit divider = 1/2
70 * (default) and prescaler = 16. Should be 10390
71 * @33.25MHz and 15625 @ 50 MHz
72 */
kevin.morfitt@fearnside-systems.co.ukd67cce22009-10-10 13:30:22 +090073 timer_load_val = get_PCLK() / (2 * 16 * 100);
kevin.morfitt@fearnside-systems.co.ukcd856622009-09-06 00:33:13 +090074 timer_clk = get_PCLK() / (2 * 16);
wdenk281e00a2004-08-01 22:48:16 +000075 }
76 /* load value for 10 ms timeout */
kevin.morfitt@fearnside-systems.co.ukd67cce22009-10-10 13:30:22 +090077 lastdec = timer_load_val;
78 writel(timer_load_val, &timers->TCNTB4);
wdenk281e00a2004-08-01 22:48:16 +000079 /* auto load, manual update of Timer 4 */
kevin.morfitt@fearnside-systems.co.ukd67cce22009-10-10 13:30:22 +090080 tmr = (readl(&timers->TCON) & ~0x0700000) | 0x0600000;
81 writel(tmr, &timers->TCON);
wdenk281e00a2004-08-01 22:48:16 +000082 /* auto load, start Timer 4 */
kevin.morfitt@fearnside-systems.co.ukd67cce22009-10-10 13:30:22 +090083 tmr = (tmr & ~0x0700000) | 0x0500000;
84 writel(tmr, &timers->TCON);
wdenk281e00a2004-08-01 22:48:16 +000085 timestamp = 0;
86
87 return (0);
88}
89
90/*
91 * timer without interrupts
92 */
93
kevin.morfitt@fearnside-systems.co.ukd67cce22009-10-10 13:30:22 +090094void reset_timer(void)
wdenk281e00a2004-08-01 22:48:16 +000095{
kevin.morfitt@fearnside-systems.co.ukd67cce22009-10-10 13:30:22 +090096 reset_timer_masked();
wdenk281e00a2004-08-01 22:48:16 +000097}
98
kevin.morfitt@fearnside-systems.co.ukd67cce22009-10-10 13:30:22 +090099ulong get_timer(ulong base)
wdenk281e00a2004-08-01 22:48:16 +0000100{
kevin.morfitt@fearnside-systems.co.ukd67cce22009-10-10 13:30:22 +0900101 return get_timer_masked() - base;
wdenk281e00a2004-08-01 22:48:16 +0000102}
103
kevin.morfitt@fearnside-systems.co.ukd67cce22009-10-10 13:30:22 +0900104void set_timer(ulong t)
wdenk281e00a2004-08-01 22:48:16 +0000105{
106 timestamp = t;
107}
108
kevin.morfitt@fearnside-systems.co.ukd67cce22009-10-10 13:30:22 +0900109void udelay(unsigned long usec)
wdenk281e00a2004-08-01 22:48:16 +0000110{
111 ulong tmo;
kevin.morfitt@fearnside-systems.co.ukcd856622009-09-06 00:33:13 +0900112 ulong start = get_ticks();
wdenk281e00a2004-08-01 22:48:16 +0000113
114 tmo = usec / 1000;
115 tmo *= (timer_load_val * 100);
116 tmo /= 1000;
117
kevin.morfitt@fearnside-systems.co.ukcd856622009-09-06 00:33:13 +0900118 while ((ulong) (get_ticks() - start) < tmo)
wdenk281e00a2004-08-01 22:48:16 +0000119 /*NOP*/;
120}
121
kevin.morfitt@fearnside-systems.co.ukd67cce22009-10-10 13:30:22 +0900122void reset_timer_masked(void)
wdenk281e00a2004-08-01 22:48:16 +0000123{
124 /* reset time */
125 lastdec = READ_TIMER();
126 timestamp = 0;
127}
128
kevin.morfitt@fearnside-systems.co.ukd67cce22009-10-10 13:30:22 +0900129ulong get_timer_masked(void)
wdenk281e00a2004-08-01 22:48:16 +0000130{
kevin.morfitt@fearnside-systems.co.ukcd856622009-09-06 00:33:13 +0900131 ulong tmr = get_ticks();
wdenk281e00a2004-08-01 22:48:16 +0000132
kevin.morfitt@fearnside-systems.co.ukcd856622009-09-06 00:33:13 +0900133 return tmr / (timer_clk / CONFIG_SYS_HZ);
wdenk281e00a2004-08-01 22:48:16 +0000134}
135
kevin.morfitt@fearnside-systems.co.ukd67cce22009-10-10 13:30:22 +0900136void udelay_masked(unsigned long usec)
wdenk281e00a2004-08-01 22:48:16 +0000137{
138 ulong tmo;
wdenk101e8df2005-04-04 12:08:28 +0000139 ulong endtime;
140 signed long diff;
wdenk281e00a2004-08-01 22:48:16 +0000141
wdenk101e8df2005-04-04 12:08:28 +0000142 if (usec >= 1000) {
143 tmo = usec / 1000;
144 tmo *= (timer_load_val * 100);
145 tmo /= 1000;
146 } else {
147 tmo = usec * (timer_load_val * 100);
kevin.morfitt@fearnside-systems.co.ukd67cce22009-10-10 13:30:22 +0900148 tmo /= (1000 * 1000);
wdenk101e8df2005-04-04 12:08:28 +0000149 }
wdenk281e00a2004-08-01 22:48:16 +0000150
kevin.morfitt@fearnside-systems.co.ukcd856622009-09-06 00:33:13 +0900151 endtime = get_ticks() + tmo;
wdenk281e00a2004-08-01 22:48:16 +0000152
wdenk101e8df2005-04-04 12:08:28 +0000153 do {
kevin.morfitt@fearnside-systems.co.ukcd856622009-09-06 00:33:13 +0900154 ulong now = get_ticks();
wdenk101e8df2005-04-04 12:08:28 +0000155 diff = endtime - now;
156 } while (diff >= 0);
wdenk281e00a2004-08-01 22:48:16 +0000157}
158
159/*
160 * This function is derived from PowerPC code (read timebase as long long).
161 * On ARM it just returns the timer value.
162 */
163unsigned long long get_ticks(void)
164{
kevin.morfitt@fearnside-systems.co.ukcd856622009-09-06 00:33:13 +0900165 ulong now = READ_TIMER();
166
167 if (lastdec >= now) {
168 /* normal mode */
169 timestamp += lastdec - now;
170 } else {
171 /* we have an overflow ... */
172 timestamp += lastdec + timer_load_val - now;
173 }
174 lastdec = now;
175
176 return timestamp;
wdenk281e00a2004-08-01 22:48:16 +0000177}
178
179/*
180 * This function is derived from PowerPC code (timebase clock frequency).
181 * On ARM it returns the number of timer ticks per second.
182 */
kevin.morfitt@fearnside-systems.co.ukd67cce22009-10-10 13:30:22 +0900183ulong get_tbclk(void)
wdenk281e00a2004-08-01 22:48:16 +0000184{
185 ulong tbclk;
186
187#if defined(CONFIG_SMDK2400) || defined(CONFIG_TRAB)
188 tbclk = timer_load_val * 100;
Wolfgang Denk32cb2c72006-07-21 11:31:42 +0200189#elif defined(CONFIG_SBC2410X) || \
190 defined(CONFIG_SMDK2410) || \
191 defined(CONFIG_VCMA9)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200192 tbclk = CONFIG_SYS_HZ;
wdenk281e00a2004-08-01 22:48:16 +0000193#else
194# error "tbclk not configured"
195#endif
196
197 return tbclk;
198}
199
wdenkb304c962005-04-05 22:30:50 +0000200/*
201 * reset the cpu by setting up the watchdog timer and let him time out
202 */
kevin.morfitt@fearnside-systems.co.ukd67cce22009-10-10 13:30:22 +0900203void reset_cpu(ulong ignored)
wdenkb304c962005-04-05 22:30:50 +0000204{
kevin.morfitt@fearnside-systems.co.ukd67cce22009-10-10 13:30:22 +0900205 struct s3c24x0_watchdog *watchdog;
wdenkb304c962005-04-05 22:30:50 +0000206
207#ifdef CONFIG_TRAB
208 disable_vfd();
209#endif
210
kevin.morfitt@fearnside-systems.co.ukd67cce22009-10-10 13:30:22 +0900211 watchdog = s3c24x0_get_base_watchdog();
wdenkb304c962005-04-05 22:30:50 +0000212
213 /* Disable watchdog */
kevin.morfitt@fearnside-systems.co.ukd67cce22009-10-10 13:30:22 +0900214 writel(0x0000, &watchdog->WTCON);
wdenkb304c962005-04-05 22:30:50 +0000215
216 /* Initialize watchdog timer count register */
kevin.morfitt@fearnside-systems.co.ukd67cce22009-10-10 13:30:22 +0900217 writel(0x0001, &watchdog->WTCNT);
wdenkb304c962005-04-05 22:30:50 +0000218
219 /* Enable watchdog timer; assert reset at timer timeout */
kevin.morfitt@fearnside-systems.co.ukd67cce22009-10-10 13:30:22 +0900220 writel(0x0021, &watchdog->WTCON);
wdenkb304c962005-04-05 22:30:50 +0000221
kevin.morfitt@fearnside-systems.co.ukd67cce22009-10-10 13:30:22 +0900222 while (1)
223 /* loop forever and wait for reset to happen */;
wdenkb304c962005-04-05 22:30:50 +0000224
225 /*NOTREACHED*/
226}
227
kevin.morfitt@fearnside-systems.co.ukd67cce22009-10-10 13:30:22 +0900228#endif /* defined(CONFIG_S3C2400) ||
229 defined (CONFIG_S3C2410) ||
230 defined (CONFIG_TRAB) */