]> git.sur5r.net Git - u-boot/blob - cpu/arm1136/omap24xx/interrupts.c
8503b24a8ea494d6ad5aea1fecb73704cea455b2
[u-boot] / cpu / arm1136 / omap24xx / interrupts.c
1 /*
2  * (C) Copyright 2004
3  * Texas Instruments
4  * Richard Woodruff <r-woodruff2@ti.com>
5  *
6  * (C) Copyright 2002
7  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8  * Marius Groeger <mgroeger@sysgo.de>
9  * Alex Zuepke <azu@sysgo.de>
10  *
11  * (C) Copyright 2002
12  * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
13  *
14  * See file CREDITS for list of people who contributed to this
15  * project.
16  *
17  * This program is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU General Public License as
19  * published by the Free Software Foundation; either version 2 of
20  * the License, or (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30  * MA 02111-1307 USA
31  */
32
33 #include <common.h>
34 #include <asm/arch/bits.h>
35 #include <asm/arch/omap2420.h>
36
37 #define TIMER_LOAD_VAL 0
38
39 /* macro to read the 32 bit timer */
40 #define READ_TIMER (*((volatile ulong*)(CFG_TIMERBASE+TCRR)))
41
42 static ulong timestamp;
43 static ulong lastinc;
44
45 /*
46  * nothing really to do with interrupts, just starts up a counter.
47  */
48 int interrupt_init(void)
49 {
50         int32_t val;
51
52         /* Start the counter ticking up */
53         /* reload value on overflow*/
54         *((int32_t *) (CFG_TIMERBASE + TLDR)) = TIMER_LOAD_VAL;
55         /* mask to enable timer*/
56         val = (CFG_PVT << 2) | BIT5 | BIT1 | BIT0;
57         *((int32_t *) (CFG_TIMERBASE + TCLR)) = val;    /* start timer */
58
59         reset_timer_masked(); /* init the timestamp and lastinc value */
60
61         return(0);
62 }
63 /*
64  * timer without interrupts
65  */
66 void reset_timer(void)
67 {
68         reset_timer_masked();
69 }
70
71 ulong get_timer(ulong base)
72 {
73         return get_timer_masked() - base;
74 }
75
76 void set_timer(ulong t)
77 {
78         timestamp = t;
79 }
80
81 /* delay x useconds AND perserve advance timstamp value */
82 void udelay(unsigned long usec)
83 {
84         ulong tmo, tmp;
85
86         /* if "big" number, spread normalization to seconds */
87         if (usec >= 1000) {
88                 /* start to normalize for usec to ticks per sec */
89                 tmo = usec / 1000;
90                 /* find number of "ticks" to wait to achieve target */
91                 tmo *= CFG_HZ;
92                 /* finish normalize. */
93                 tmo /= 1000;
94         } else {
95                 /* else small number, don't kill it prior to HZ multiply */
96                 tmo = usec * CFG_HZ;
97                 tmo /= (1000*1000);
98         }
99         /* get current timestamp */
100         tmp = get_timer(0);
101         if ((tmo + tmp + 1) < tmp)
102                 /* setting this forward will roll time stamp */
103                 /* reset "advancing" timestamp to 0, set lastinc value */
104                 reset_timer_masked();
105         else
106                 /* else, set advancing stamp wake up time */
107                 tmo     += tmp;
108         while (get_timer_masked() < tmo)/* loop till event */
109                 /*NOP*/;
110 }
111
112 void reset_timer_masked(void)
113 {
114         /* reset time */
115         /* capture current incrementer value time */
116         lastinc = READ_TIMER;
117         /* start "advancing" time stamp from 0 */
118         timestamp = 0;
119 }
120
121 ulong get_timer_masked(void)
122 {
123         ulong now = READ_TIMER; /* current tick value */
124
125         /* normal mode (non roll) */
126         if (now >= lastinc)
127                 /* move stamp forward with absolute diff ticks */
128                 timestamp += (now - lastinc);
129         else
130                 /* we have rollover of incrementer */
131                 timestamp += (0xFFFFFFFF - lastinc) + now;
132         lastinc = now;
133         return timestamp;
134 }
135
136 /* waits specified delay value and resets timestamp */
137 void udelay_masked(unsigned long usec)
138 {
139         ulong tmo;
140         ulong endtime;
141         signed long diff;
142
143         if (usec >= 1000) {
144                 /* "big" number, spread normalization to seconds */
145                 /* start to normalize for usec to ticks per sec */
146                 tmo = usec / 1000;
147                 /* find number of "ticks" to wait to achieve target */
148                 tmo *= CFG_HZ;
149                 tmo /= 1000;/* finish normalize. */
150         } else {
151                 /* else small number, don't kill it prior to HZ multiply */
152                 tmo = usec * CFG_HZ;
153                 tmo /= (1000*1000);
154         }
155         endtime = get_timer_masked() + tmo;
156
157         do {
158                 ulong now = get_timer_masked();
159                 diff = endtime - now;
160         } while (diff >= 0);
161 }
162
163 /*
164  * This function is derived from PowerPC code (read timebase as long long).
165  * On ARM it just returns the timer value.
166  */
167 unsigned long long get_ticks(void)
168 {
169         return get_timer(0);
170 }
171 /*
172  * This function is derived from PowerPC code (timebase clock frequency).
173  * On ARM it returns the number of timer ticks per second.
174  */
175 ulong get_tbclk(void)
176 {
177         ulong tbclk;
178         tbclk = CFG_HZ;
179         return tbclk;
180 }