blob: 491c902ace3838224e85621ff92537878a5ea456 [file] [log] [blame]
wdenkf8062712005-01-09 23:16:25 +00001/*
wdenk2e405bf2005-01-10 00:01:04 +00002 * (C) Copyright 2004
3 * Texas Instruments
4 * Richard Woodruff <r-woodruff2@ti.com>
wdenkf8062712005-01-09 23:16:25 +00005 *
6 * (C) Copyright 2002
7 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8 * Marius Groeger <mgroeger@sysgo.de>
wdenkf8062712005-01-09 23:16:25 +00009 * 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>
Wolfgang Denk7f88a5e2005-10-06 17:08:18 +020035
Wolfgang Denk88bd7432005-10-09 00:22:48 +020036#if !defined(CONFIG_INTEGRATOR) && ! defined(CONFIG_ARCH_CINTEGRATOR)
Wolfgang Denk7f88a5e2005-10-06 17:08:18 +020037# include <asm/arch/omap2420.h>
38#endif
39
wdenkf8062712005-01-09 23:16:25 +000040#define TIMER_LOAD_VAL 0
41
42/* macro to read the 32 bit timer */
43#define READ_TIMER (*(volatile ulong *)(CFG_TIMERBASE+TCRR))
44
Wolfgang Denk7f88a5e2005-10-06 17:08:18 +020045#if defined(CONFIG_INTEGRATOR) && defined(CONFIG_ARCH_CINTEGRATOR)
46/* Use the IntegratorCP function from board/integratorcp.c */
47#else
Wolfgang Denk88bd7432005-10-09 00:22:48 +020048
49static ulong timestamp;
50static ulong lastinc;
51
wdenkf8062712005-01-09 23:16:25 +000052/* nothing really to do with interrupts, just starts up a counter. */
53int interrupt_init (void)
54{
55 int32_t val;
56
57 /* Start the counter ticking up */
58 *((int32_t *) (CFG_TIMERBASE + TLDR)) = TIMER_LOAD_VAL; /* reload value on overflow*/
59 val = (CFG_PVT << 2) | BIT5 | BIT1 | BIT0; /* mask to enable timer*/
60 *((int32_t *) (CFG_TIMERBASE + TCLR)) = val; /* start timer */
61
62 reset_timer_masked(); /* init the timestamp and lastinc value */
63
64 return(0);
65}
wdenkf8062712005-01-09 23:16:25 +000066/*
67 * timer without interrupts
68 */
69void reset_timer (void)
70{
71 reset_timer_masked ();
72}
73
74ulong get_timer (ulong base)
75{
76 return get_timer_masked () - base;
77}
78
79void set_timer (ulong t)
80{
81 timestamp = t;
82}
83
84/* delay x useconds AND perserve advance timstamp value */
85void udelay (unsigned long usec)
86{
87 ulong tmo, tmp;
88
89 if (usec >= 1000) { /* if "big" number, spread normalization to seconds */
90 tmo = usec / 1000; /* start to normalize for usec to ticks per sec */
91 tmo *= CFG_HZ; /* find number of "ticks" to wait to achieve target */
92 tmo /= 1000; /* finish normalize. */
93 } else { /* else small number, don't kill it prior to HZ multiply */
94 tmo = usec * CFG_HZ;
95 tmo /= (1000*1000);
96 }
97
98 tmp = get_timer (0); /* get current timestamp */
99 if ( (tmo + tmp + 1) < tmp )/* if setting this forward will roll time stamp */
100 reset_timer_masked (); /* reset "advancing" timestamp to 0, set lastinc value */
101 else
102 tmo += tmp; /* else, set advancing stamp wake up time */
103 while (get_timer_masked () < tmo)/* loop till event */
104 /*NOP*/;
105}
106
107void reset_timer_masked (void)
108{
109 /* reset time */
110 lastinc = READ_TIMER; /* capture current incrementer value time */
111 timestamp = 0; /* start "advancing" time stamp from 0 */
112}
113
114ulong get_timer_masked (void)
115{
116 ulong now = READ_TIMER; /* current tick value */
117
118 if (now >= lastinc) /* normal mode (non roll) */
119 timestamp += (now - lastinc); /* move stamp fordward with absoulte diff ticks */
120 else /* we have rollover of incrementer */
121 timestamp += (0xFFFFFFFF - lastinc) + now;
122 lastinc = now;
123 return timestamp;
124}
125
126/* waits specified delay value and resets timestamp */
127void udelay_masked (unsigned long usec)
128{
129 ulong tmo;
wdenk7af1f9d2005-04-04 12:08:28 +0000130 ulong endtime;
131 signed long diff;
wdenkf8062712005-01-09 23:16:25 +0000132
133 if (usec >= 1000) { /* if "big" number, spread normalization to seconds */
134 tmo = usec / 1000; /* start to normalize for usec to ticks per sec */
135 tmo *= CFG_HZ; /* find number of "ticks" to wait to achieve target */
136 tmo /= 1000; /* finish normalize. */
137 } else { /* else small number, don't kill it prior to HZ multiply */
138 tmo = usec * CFG_HZ;
139 tmo /= (1000*1000);
140 }
wdenk7af1f9d2005-04-04 12:08:28 +0000141 endtime = get_timer_masked () + tmo;
142
143 do {
144 ulong now = get_timer_masked ();
145 diff = endtime - now;
146 } while (diff >= 0);
wdenkf8062712005-01-09 23:16:25 +0000147}
148
149/*
150 * This function is derived from PowerPC code (read timebase as long long).
151 * On ARM it just returns the timer value.
152 */
153unsigned long long get_ticks(void)
154{
155 return get_timer(0);
156}
wdenkf8062712005-01-09 23:16:25 +0000157/*
158 * This function is derived from PowerPC code (timebase clock frequency).
159 * On ARM it returns the number of timer ticks per second.
160 */
161ulong get_tbclk (void)
162{
163 ulong tbclk;
164 tbclk = CFG_HZ;
165 return tbclk;
166}
Wolfgang Denk7f88a5e2005-10-06 17:08:18 +0200167#endif /* !Integrator/CP */