blob: ba1e616b8246245ab4cab87ed42678144a49a4ab [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 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +020013 * SPDX-License-Identifier: GPL-2.0+
wdenk7ac16102004-08-01 22:48:16 +000014 */
15
16#include <common.h>
kevin.morfitt@fearnside-systems.co.uke0d81312009-11-17 18:30:34 +090017#ifdef CONFIG_S3C24X0
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +090018
19#include <asm/io.h>
kevin.morfitt@fearnside-systems.co.uke0d81312009-11-17 18:30:34 +090020#include <asm/arch/s3c24x0_cpu.h>
wdenk7ac16102004-08-01 22:48:16 +000021
David Müller (ELSOFT AG)fff486e2011-12-22 01:16:37 +000022DECLARE_GLOBAL_DATA_PTR;
wdenk7ac16102004-08-01 22:48:16 +000023
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +090024int timer_init(void)
wdenk7ac16102004-08-01 22:48:16 +000025{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +090026 struct s3c24x0_timers *timers = s3c24x0_get_base_timers();
27 ulong tmr;
wdenk7ac16102004-08-01 22:48:16 +000028
29 /* use PWM Timer 4 because it has no output */
30 /* prescaler for Timer 4 is 16 */
C Nauman383c43e2010-10-26 23:04:31 +090031 writel(0x0f00, &timers->tcfg0);
Simon Glass8ca15202012-12-13 20:48:33 +000032 if (gd->arch.tbu == 0) {
wdenk7ac16102004-08-01 22:48:16 +000033 /*
34 * for 10 ms clock period @ PCLK with 4 bit divider = 1/2
35 * (default) and prescaler = 16. Should be 10390
36 * @33.25MHz and 15625 @ 50 MHz
37 */
Simon Glass8ca15202012-12-13 20:48:33 +000038 gd->arch.tbu = get_PCLK() / (2 * 16 * 100);
Simon Glass6ed6e032012-12-13 20:48:32 +000039 gd->arch.timer_rate_hz = get_PCLK() / (2 * 16);
wdenk7ac16102004-08-01 22:48:16 +000040 }
41 /* load value for 10 ms timeout */
Simon Glass8ca15202012-12-13 20:48:33 +000042 writel(gd->arch.tbu, &timers->tcntb4);
C Nauman383c43e2010-10-26 23:04:31 +090043 /* auto load, manual update of timer 4 */
44 tmr = (readl(&timers->tcon) & ~0x0700000) | 0x0600000;
45 writel(tmr, &timers->tcon);
46 /* auto load, start timer 4 */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +090047 tmr = (tmr & ~0x0700000) | 0x0500000;
C Nauman383c43e2010-10-26 23:04:31 +090048 writel(tmr, &timers->tcon);
Simon Glassa848da52012-12-13 20:48:35 +000049 gd->arch.lastinc = 0;
Simon Glass2655ee12012-12-13 20:48:34 +000050 gd->arch.tbl = 0;
wdenk7ac16102004-08-01 22:48:16 +000051
David Müller (ELSOFT AG)fff486e2011-12-22 01:16:37 +000052 return 0;
wdenk7ac16102004-08-01 22:48:16 +000053}
54
55/*
56 * timer without interrupts
57 */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +090058ulong get_timer(ulong base)
wdenk7ac16102004-08-01 22:48:16 +000059{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +090060 return get_timer_masked() - base;
wdenk7ac16102004-08-01 22:48:16 +000061}
62
Ingo van Lilf0f778a2009-11-24 14:09:21 +010063void __udelay (unsigned long usec)
wdenk7ac16102004-08-01 22:48:16 +000064{
65 ulong tmo;
kevin.morfitt@fearnside-systems.co.uk34f0cf92009-09-06 00:33:13 +090066 ulong start = get_ticks();
wdenk7ac16102004-08-01 22:48:16 +000067
68 tmo = usec / 1000;
Simon Glass8ca15202012-12-13 20:48:33 +000069 tmo *= (gd->arch.tbu * 100);
wdenk7ac16102004-08-01 22:48:16 +000070 tmo /= 1000;
71
kevin.morfitt@fearnside-systems.co.uk34f0cf92009-09-06 00:33:13 +090072 while ((ulong) (get_ticks() - start) < tmo)
wdenk7ac16102004-08-01 22:48:16 +000073 /*NOP*/;
74}
75
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +090076ulong get_timer_masked(void)
wdenk7ac16102004-08-01 22:48:16 +000077{
kevin.morfitt@fearnside-systems.co.uk34f0cf92009-09-06 00:33:13 +090078 ulong tmr = get_ticks();
wdenk7ac16102004-08-01 22:48:16 +000079
Simon Glass6ed6e032012-12-13 20:48:32 +000080 return tmr / (gd->arch.timer_rate_hz / CONFIG_SYS_HZ);
wdenk7ac16102004-08-01 22:48:16 +000081}
82
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +090083void udelay_masked(unsigned long usec)
wdenk7ac16102004-08-01 22:48:16 +000084{
85 ulong tmo;
wdenk7af1f9d2005-04-04 12:08:28 +000086 ulong endtime;
87 signed long diff;
wdenk7ac16102004-08-01 22:48:16 +000088
wdenk7af1f9d2005-04-04 12:08:28 +000089 if (usec >= 1000) {
90 tmo = usec / 1000;
Simon Glass8ca15202012-12-13 20:48:33 +000091 tmo *= (gd->arch.tbu * 100);
wdenk7af1f9d2005-04-04 12:08:28 +000092 tmo /= 1000;
93 } else {
Simon Glass8ca15202012-12-13 20:48:33 +000094 tmo = usec * (gd->arch.tbu * 100);
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +090095 tmo /= (1000 * 1000);
wdenk7af1f9d2005-04-04 12:08:28 +000096 }
wdenk7ac16102004-08-01 22:48:16 +000097
kevin.morfitt@fearnside-systems.co.uk34f0cf92009-09-06 00:33:13 +090098 endtime = get_ticks() + tmo;
wdenk7ac16102004-08-01 22:48:16 +000099
wdenk7af1f9d2005-04-04 12:08:28 +0000100 do {
kevin.morfitt@fearnside-systems.co.uk34f0cf92009-09-06 00:33:13 +0900101 ulong now = get_ticks();
wdenk7af1f9d2005-04-04 12:08:28 +0000102 diff = endtime - now;
103 } while (diff >= 0);
wdenk7ac16102004-08-01 22:48:16 +0000104}
105
106/*
107 * This function is derived from PowerPC code (read timebase as long long).
108 * On ARM it just returns the timer value.
109 */
110unsigned long long get_ticks(void)
111{
David Müller (ELSOFT AG)fff486e2011-12-22 01:16:37 +0000112 struct s3c24x0_timers *timers = s3c24x0_get_base_timers();
113 ulong now = readl(&timers->tcnto4) & 0xffff;
kevin.morfitt@fearnside-systems.co.uk34f0cf92009-09-06 00:33:13 +0900114
Simon Glassa848da52012-12-13 20:48:35 +0000115 if (gd->arch.lastinc >= now) {
kevin.morfitt@fearnside-systems.co.uk34f0cf92009-09-06 00:33:13 +0900116 /* normal mode */
Simon Glassa848da52012-12-13 20:48:35 +0000117 gd->arch.tbl += gd->arch.lastinc - now;
kevin.morfitt@fearnside-systems.co.uk34f0cf92009-09-06 00:33:13 +0900118 } else {
119 /* we have an overflow ... */
Simon Glassa848da52012-12-13 20:48:35 +0000120 gd->arch.tbl += gd->arch.lastinc + gd->arch.tbu - now;
kevin.morfitt@fearnside-systems.co.uk34f0cf92009-09-06 00:33:13 +0900121 }
Simon Glassa848da52012-12-13 20:48:35 +0000122 gd->arch.lastinc = now;
kevin.morfitt@fearnside-systems.co.uk34f0cf92009-09-06 00:33:13 +0900123
Simon Glass2655ee12012-12-13 20:48:34 +0000124 return gd->arch.tbl;
wdenk7ac16102004-08-01 22:48:16 +0000125}
126
127/*
128 * This function is derived from PowerPC code (timebase clock frequency).
129 * On ARM it returns the number of timer ticks per second.
130 */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900131ulong get_tbclk(void)
wdenk7ac16102004-08-01 22:48:16 +0000132{
David Müller (ELSOFT AG)fff486e2011-12-22 01:16:37 +0000133 return CONFIG_SYS_HZ;
wdenk7ac16102004-08-01 22:48:16 +0000134}
135
wdenk915b3762005-04-05 22:30:50 +0000136/*
137 * reset the cpu by setting up the watchdog timer and let him time out
138 */
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900139void reset_cpu(ulong ignored)
wdenk915b3762005-04-05 22:30:50 +0000140{
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900141 struct s3c24x0_watchdog *watchdog;
wdenk915b3762005-04-05 22:30:50 +0000142
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900143 watchdog = s3c24x0_get_base_watchdog();
wdenk915b3762005-04-05 22:30:50 +0000144
145 /* Disable watchdog */
C Nauman383c43e2010-10-26 23:04:31 +0900146 writel(0x0000, &watchdog->wtcon);
wdenk915b3762005-04-05 22:30:50 +0000147
148 /* Initialize watchdog timer count register */
C Nauman383c43e2010-10-26 23:04:31 +0900149 writel(0x0001, &watchdog->wtcnt);
wdenk915b3762005-04-05 22:30:50 +0000150
151 /* Enable watchdog timer; assert reset at timer timeout */
C Nauman383c43e2010-10-26 23:04:31 +0900152 writel(0x0021, &watchdog->wtcon);
wdenk915b3762005-04-05 22:30:50 +0000153
kevin.morfitt@fearnside-systems.co.ukd1cacc72009-10-10 13:30:22 +0900154 while (1)
155 /* loop forever and wait for reset to happen */;
wdenk915b3762005-04-05 22:30:50 +0000156
157 /*NOTREACHED*/
158}
159
kevin.morfitt@fearnside-systems.co.uke0d81312009-11-17 18:30:34 +0900160#endif /* CONFIG_S3C24X0 */