blob: 8dd8d7b008d83f3223b68ca0d4b9e0ebc7a73ef1 [file] [log] [blame]
Wolfgang Denk950a3922008-04-11 15:11:26 +02001/*
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
Detlev Zundel792a09e2009-05-13 10:54:10 +020012 * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
Wolfgang Denk950a3922008-04-11 15:11:26 +020013 *
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>
Sascha Hauer5252ed92008-03-26 20:40:36 +010035#include <asm/arch/omap2420.h>
Wolfgang Denk950a3922008-04-11 15:11:26 +020036
37#define TIMER_LOAD_VAL 0
38
39/* macro to read the 32 bit timer */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020040#define READ_TIMER (*((volatile ulong *)(CONFIG_SYS_TIMERBASE+TCRR)))
Wolfgang Denk950a3922008-04-11 15:11:26 +020041
Wolfgang Denk950a3922008-04-11 15:11:26 +020042static ulong timestamp;
43static ulong lastinc;
44
Jean-Christophe PLAGNIOL-VILLARDb54384e2009-05-15 23:47:02 +020045int timer_init (void)
Wolfgang Denk950a3922008-04-11 15:11:26 +020046{
47 int32_t val;
48
49 /* Start the counter ticking up */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020050 *((int32_t *) (CONFIG_SYS_TIMERBASE + TLDR)) = TIMER_LOAD_VAL; /* reload value on overflow*/
Ladislav Michl81472d82009-03-30 18:58:41 +020051 val = (CONFIG_SYS_PTV << 2) | BIT5 | BIT1 | BIT0; /* mask to enable timer*/
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020052 *((int32_t *) (CONFIG_SYS_TIMERBASE + TCLR)) = val; /* start timer */
Wolfgang Denk950a3922008-04-11 15:11:26 +020053
54 reset_timer_masked(); /* init the timestamp and lastinc value */
55
56 return(0);
57}
58/*
59 * timer without interrupts
60 */
61void reset_timer (void)
62{
63 reset_timer_masked ();
64}
65
66ulong get_timer (ulong base)
67{
68 return get_timer_masked () - base;
69}
70
71void set_timer (ulong t)
72{
73 timestamp = t;
74}
75
76/* delay x useconds AND perserve advance timstamp value */
77void udelay (unsigned long usec)
78{
79 ulong tmo, tmp;
80
81 if (usec >= 1000) { /* if "big" number, spread normalization to seconds */
82 tmo = usec / 1000; /* start to normalize for usec to ticks per sec */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020083 tmo *= CONFIG_SYS_HZ; /* find number of "ticks" to wait to achieve target */
Wolfgang Denk950a3922008-04-11 15:11:26 +020084 tmo /= 1000; /* finish normalize. */
85 } else { /* else small number, don't kill it prior to HZ multiply */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020086 tmo = usec * CONFIG_SYS_HZ;
Wolfgang Denk950a3922008-04-11 15:11:26 +020087 tmo /= (1000*1000);
88 }
89
90 tmp = get_timer (0); /* get current timestamp */
91 if ( (tmo + tmp + 1) < tmp )/* if setting this forward will roll time stamp */
92 reset_timer_masked (); /* reset "advancing" timestamp to 0, set lastinc value */
93 else
94 tmo += tmp; /* else, set advancing stamp wake up time */
95 while (get_timer_masked () < tmo)/* loop till event */
96 /*NOP*/;
97}
98
99void reset_timer_masked (void)
100{
101 /* reset time */
102 lastinc = READ_TIMER; /* capture current incrementer value time */
103 timestamp = 0; /* start "advancing" time stamp from 0 */
104}
105
106ulong get_timer_masked (void)
107{
108 ulong now = READ_TIMER; /* current tick value */
109
110 if (now >= lastinc) /* normal mode (non roll) */
111 timestamp += (now - lastinc); /* move stamp fordward with absoulte diff ticks */
112 else /* we have rollover of incrementer */
113 timestamp += (0xFFFFFFFF - lastinc) + now;
114 lastinc = now;
115 return timestamp;
116}
117
118/* waits specified delay value and resets timestamp */
119void udelay_masked (unsigned long usec)
120{
121 ulong tmo;
122 ulong endtime;
123 signed long diff;
124
125 if (usec >= 1000) { /* if "big" number, spread normalization to seconds */
126 tmo = usec / 1000; /* start to normalize for usec to ticks per sec */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200127 tmo *= CONFIG_SYS_HZ; /* find number of "ticks" to wait to achieve target */
Wolfgang Denk950a3922008-04-11 15:11:26 +0200128 tmo /= 1000; /* finish normalize. */
129 } else { /* else small number, don't kill it prior to HZ multiply */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200130 tmo = usec * CONFIG_SYS_HZ;
Wolfgang Denk950a3922008-04-11 15:11:26 +0200131 tmo /= (1000*1000);
132 }
133 endtime = get_timer_masked () + tmo;
134
135 do {
136 ulong now = get_timer_masked ();
137 diff = endtime - now;
138 } while (diff >= 0);
139}
140
141/*
142 * This function is derived from PowerPC code (read timebase as long long).
143 * On ARM it just returns the timer value.
144 */
145unsigned long long get_ticks(void)
146{
147 return get_timer(0);
148}
149/*
150 * This function is derived from PowerPC code (timebase clock frequency).
151 * On ARM it returns the number of timer ticks per second.
152 */
153ulong get_tbclk (void)
154{
155 ulong tbclk;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200156 tbclk = CONFIG_SYS_HZ;
Wolfgang Denk950a3922008-04-11 15:11:26 +0200157 return tbclk;
158}