]> git.sur5r.net Git - u-boot/blob - arch/arm/cpu/s3c44b0/timer.c
d30e1aab8c364b8446d37c48fa5aba505c8bd1d4
[u-boot] / arch / arm / cpu / s3c44b0 / timer.c
1 /*
2  * (C) Copyright 2004
3  * DAVE Srl
4  * http://www.dave-tech.it
5  * http://www.wawnet.biz
6  * mailto:info@wawnet.biz
7  *
8  * See file CREDITS for list of people who contributed to this
9  * project.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation; either version 2 of
14  * the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24  * MA 02111-1307 USA
25  */
26
27 #include <common.h>
28 #include <asm/hardware.h>
29
30 /* we always count down the max. */
31 #define TIMER_LOAD_VAL 0xffff
32
33 /* macro to read the 16 bit timer */
34 #define READ_TIMER (TCNTO1 & 0xffff)
35
36 #ifdef CONFIG_USE_IRQ
37 #error CONFIG_USE_IRQ NOT supported
38 #endif
39
40 static ulong timestamp;
41 static ulong lastdec;
42
43 int timer_init (void)
44 {
45         TCFG0 = 0x000000E9;
46         TCFG1 = 0x00000004;
47         TCON = 0x00000900;
48         TCNTB1 = TIMER_LOAD_VAL;
49         TCMPB1 = 0;
50         TCON = 0x00000B00;
51         TCON = 0x00000900;
52
53
54         lastdec = TCNTB1 = TIMER_LOAD_VAL;
55         timestamp = 0;
56         return 0;
57 }
58
59 /*
60  * timer without interrupts
61  */
62
63 void reset_timer (void)
64 {
65         reset_timer_masked ();
66 }
67
68 ulong get_timer (ulong base)
69 {
70         return get_timer_masked () - base;
71 }
72
73 void __udelay (unsigned long usec)
74 {
75         ulong tmo;
76
77         tmo = usec / 1000;
78         tmo *= CONFIG_SYS_HZ;
79         tmo /= 8;
80
81         tmo += get_timer (0);
82
83         while (get_timer_masked () < tmo)
84                 /*NOP*/;
85 }
86
87 void reset_timer_masked (void)
88 {
89         /* reset time */
90         lastdec = READ_TIMER;
91         timestamp = 0;
92 }
93
94 ulong get_timer_masked (void)
95 {
96         ulong now = READ_TIMER;
97
98         if (lastdec >= now) {
99                 /* normal mode */
100                 timestamp += lastdec - now;
101         } else {
102                 /* we have an overflow ... */
103                 timestamp += lastdec + TIMER_LOAD_VAL - now;
104         }
105         lastdec = now;
106
107         return timestamp;
108 }
109
110 void udelay_masked (unsigned long usec)
111 {
112         ulong tmo;
113         ulong endtime;
114         signed long diff;
115
116         if (usec >= 1000) {
117                 tmo = usec / 1000;
118                 tmo *= CONFIG_SYS_HZ;
119                 tmo /= 8;
120         } else {
121                 tmo = usec * CONFIG_SYS_HZ;
122                 tmo /= (1000*8);
123         }
124
125         endtime = get_timer(0) + tmo;
126
127         do {
128                 ulong now = get_timer_masked ();
129                 diff = endtime - now;
130         } while (diff >= 0);
131 }