blob: d76bf186b6516891f2044a92a3a8c29ff2c2540e [file] [log] [blame]
wdenk7ac16102004-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 Zundelf1b3f2b2009-05-13 10:54:10 +020011 * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
wdenk7ac16102004-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.uke0d81312009-11-17 18:30:34 +090033#ifdef CONFIG_S3C24X0
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +090034
35#include <asm/io.h>
kevin.morfitt@fearnside-systems.co.uke0d81312009-11-17 18:30:34 +090036#include <asm/arch/s3c24x0_cpu.h>
wdenk7ac16102004-08-01 22:48:16 +000037
David Müller (ELSOFT AG)fff486e2011-12-22 01:16:37 +000038DECLARE_GLOBAL_DATA_PTR;
wdenk7ac16102004-08-01 22:48:16 +000039
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +090040int timer_init(void)
wdenk7ac16102004-08-01 22:48:16 +000041{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +090042 struct s3c24x0_timers *timers = s3c24x0_get_base_timers();
43 ulong tmr;
wdenk7ac16102004-08-01 22:48:16 +000044
45 /* use PWM Timer 4 because it has no output */
46 /* prescaler for Timer 4 is 16 */
C Nauman383c43e2010-10-26 23:04:31 +090047 writel(0x0f00, &timers->tcfg0);
Simon Glass8ca15202012-12-13 20:48:33 +000048 if (gd->arch.tbu == 0) {
wdenk7ac16102004-08-01 22:48:16 +000049 /*
50 * for 10 ms clock period @ PCLK with 4 bit divider = 1/2
51 * (default) and prescaler = 16. Should be 10390
52 * @33.25MHz and 15625 @ 50 MHz
53 */
Simon Glass8ca15202012-12-13 20:48:33 +000054 gd->arch.tbu = get_PCLK() / (2 * 16 * 100);
Simon Glass6ed6e032012-12-13 20:48:32 +000055 gd->arch.timer_rate_hz = get_PCLK() / (2 * 16);
wdenk7ac16102004-08-01 22:48:16 +000056 }
57 /* load value for 10 ms timeout */
Simon Glass8ca15202012-12-13 20:48:33 +000058 writel(gd->arch.tbu, &timers->tcntb4);
C Nauman383c43e2010-10-26 23:04:31 +090059 /* auto load, manual update of timer 4 */
60 tmr = (readl(&timers->tcon) & ~0x0700000) | 0x0600000;
61 writel(tmr, &timers->tcon);
62 /* auto load, start timer 4 */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +090063 tmr = (tmr & ~0x0700000) | 0x0500000;
C Nauman383c43e2010-10-26 23:04:31 +090064 writel(tmr, &timers->tcon);
Simon Glassa848da52012-12-13 20:48:35 +000065 gd->arch.lastinc = 0;
Simon Glass2655ee12012-12-13 20:48:34 +000066 gd->arch.tbl = 0;
wdenk7ac16102004-08-01 22:48:16 +000067
David Müller (ELSOFT AG)fff486e2011-12-22 01:16:37 +000068 return 0;
wdenk7ac16102004-08-01 22:48:16 +000069}
70
71/*
72 * timer without interrupts
73 */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +090074ulong get_timer(ulong base)
wdenk7ac16102004-08-01 22:48:16 +000075{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +090076 return get_timer_masked() - base;
wdenk7ac16102004-08-01 22:48:16 +000077}
78
Ingo van Lilf0f778a2009-11-24 14:09:21 +010079void __udelay (unsigned long usec)
wdenk7ac16102004-08-01 22:48:16 +000080{
81 ulong tmo;
kevin.morfitt@fearnside-systems.co.uk34f0cf92009-09-06 00:33:13 +090082 ulong start = get_ticks();
wdenk7ac16102004-08-01 22:48:16 +000083
84 tmo = usec / 1000;
Simon Glass8ca15202012-12-13 20:48:33 +000085 tmo *= (gd->arch.tbu * 100);
wdenk7ac16102004-08-01 22:48:16 +000086 tmo /= 1000;
87
kevin.morfitt@fearnside-systems.co.uk34f0cf92009-09-06 00:33:13 +090088 while ((ulong) (get_ticks() - start) < tmo)
wdenk7ac16102004-08-01 22:48:16 +000089 /*NOP*/;
90}
91
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +090092ulong get_timer_masked(void)
wdenk7ac16102004-08-01 22:48:16 +000093{
kevin.morfitt@fearnside-systems.co.uk34f0cf92009-09-06 00:33:13 +090094 ulong tmr = get_ticks();
wdenk7ac16102004-08-01 22:48:16 +000095
Simon Glass6ed6e032012-12-13 20:48:32 +000096 return tmr / (gd->arch.timer_rate_hz / CONFIG_SYS_HZ);
wdenk7ac16102004-08-01 22:48:16 +000097}
98
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +090099void udelay_masked(unsigned long usec)
wdenk7ac16102004-08-01 22:48:16 +0000100{
101 ulong tmo;
wdenk7af1f9d2005-04-04 12:08:28 +0000102 ulong endtime;
103 signed long diff;
wdenk7ac16102004-08-01 22:48:16 +0000104
wdenk7af1f9d2005-04-04 12:08:28 +0000105 if (usec >= 1000) {
106 tmo = usec / 1000;
Simon Glass8ca15202012-12-13 20:48:33 +0000107 tmo *= (gd->arch.tbu * 100);
wdenk7af1f9d2005-04-04 12:08:28 +0000108 tmo /= 1000;
109 } else {
Simon Glass8ca15202012-12-13 20:48:33 +0000110 tmo = usec * (gd->arch.tbu * 100);
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900111 tmo /= (1000 * 1000);
wdenk7af1f9d2005-04-04 12:08:28 +0000112 }
wdenk7ac16102004-08-01 22:48:16 +0000113
kevin.morfitt@fearnside-systems.co.uk34f0cf92009-09-06 00:33:13 +0900114 endtime = get_ticks() + tmo;
wdenk7ac16102004-08-01 22:48:16 +0000115
wdenk7af1f9d2005-04-04 12:08:28 +0000116 do {
kevin.morfitt@fearnside-systems.co.uk34f0cf92009-09-06 00:33:13 +0900117 ulong now = get_ticks();
wdenk7af1f9d2005-04-04 12:08:28 +0000118 diff = endtime - now;
119 } while (diff >= 0);
wdenk7ac16102004-08-01 22:48:16 +0000120}
121
122/*
123 * This function is derived from PowerPC code (read timebase as long long).
124 * On ARM it just returns the timer value.
125 */
126unsigned long long get_ticks(void)
127{
David Müller (ELSOFT AG)fff486e2011-12-22 01:16:37 +0000128 struct s3c24x0_timers *timers = s3c24x0_get_base_timers();
129 ulong now = readl(&timers->tcnto4) & 0xffff;
kevin.morfitt@fearnside-systems.co.uk34f0cf92009-09-06 00:33:13 +0900130
Simon Glassa848da52012-12-13 20:48:35 +0000131 if (gd->arch.lastinc >= now) {
kevin.morfitt@fearnside-systems.co.uk34f0cf92009-09-06 00:33:13 +0900132 /* normal mode */
Simon Glassa848da52012-12-13 20:48:35 +0000133 gd->arch.tbl += gd->arch.lastinc - now;
kevin.morfitt@fearnside-systems.co.uk34f0cf92009-09-06 00:33:13 +0900134 } else {
135 /* we have an overflow ... */
Simon Glassa848da52012-12-13 20:48:35 +0000136 gd->arch.tbl += gd->arch.lastinc + gd->arch.tbu - now;
kevin.morfitt@fearnside-systems.co.uk34f0cf92009-09-06 00:33:13 +0900137 }
Simon Glassa848da52012-12-13 20:48:35 +0000138 gd->arch.lastinc = now;
kevin.morfitt@fearnside-systems.co.uk34f0cf92009-09-06 00:33:13 +0900139
Simon Glass2655ee12012-12-13 20:48:34 +0000140 return gd->arch.tbl;
wdenk7ac16102004-08-01 22:48:16 +0000141}
142
143/*
144 * This function is derived from PowerPC code (timebase clock frequency).
145 * On ARM it returns the number of timer ticks per second.
146 */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900147ulong get_tbclk(void)
wdenk7ac16102004-08-01 22:48:16 +0000148{
David Müller (ELSOFT AG)fff486e2011-12-22 01:16:37 +0000149 return CONFIG_SYS_HZ;
wdenk7ac16102004-08-01 22:48:16 +0000150}
151
wdenk915b3762005-04-05 22:30:50 +0000152/*
153 * reset the cpu by setting up the watchdog timer and let him time out
154 */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900155void reset_cpu(ulong ignored)
wdenk915b3762005-04-05 22:30:50 +0000156{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900157 struct s3c24x0_watchdog *watchdog;
wdenk915b3762005-04-05 22:30:50 +0000158
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900159 watchdog = s3c24x0_get_base_watchdog();
wdenk915b3762005-04-05 22:30:50 +0000160
161 /* Disable watchdog */
C Nauman383c43e2010-10-26 23:04:31 +0900162 writel(0x0000, &watchdog->wtcon);
wdenk915b3762005-04-05 22:30:50 +0000163
164 /* Initialize watchdog timer count register */
C Nauman383c43e2010-10-26 23:04:31 +0900165 writel(0x0001, &watchdog->wtcnt);
wdenk915b3762005-04-05 22:30:50 +0000166
167 /* Enable watchdog timer; assert reset at timer timeout */
C Nauman383c43e2010-10-26 23:04:31 +0900168 writel(0x0021, &watchdog->wtcon);
wdenk915b3762005-04-05 22:30:50 +0000169
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900170 while (1)
171 /* loop forever and wait for reset to happen */;
wdenk915b3762005-04-05 22:30:50 +0000172
173 /*NOTREACHED*/
174}
175
kevin.morfitt@fearnside-systems.co.uke0d81312009-11-17 18:30:34 +0900176#endif /* CONFIG_S3C24X0 */