blob: 9c965e04f93f2435785c3e0adebf2a98032ee030 [file] [log] [blame]
wdenkf6f96f72003-07-15 20:04:06 +00001/*
Ladislav Michla16482e2009-04-22 01:12:04 +02002 * (C) Copyright 2009
3 * 2N Telekomunikace, <www.2n.cz>
4 *
wdenkf6f96f72003-07-15 20:04:06 +00005 * (C) Copyright 2003
6 * Texas Instruments, <www.ti.com>
7 *
8 * (C) Copyright 2002
9 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
10 * Marius Groeger <mgroeger@sysgo.de>
11 *
12 * (C) Copyright 2002
13 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
14 * Alex Zuepke <azu@sysgo.de>
15 *
16 * (C) Copyright 2002
Detlev Zundelf1b3f2b2009-05-13 10:54:10 +020017 * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
wdenkf6f96f72003-07-15 20:04:06 +000018 *
19 * See file CREDITS for list of people who contributed to this
20 * project.
21 *
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License as
24 * published by the Free Software Foundation; either version 2 of
25 * the License, or (at your option) any later version.
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wdenk29e7f5a2004-03-12 00:14:09 +000029 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wdenkf6f96f72003-07-15 20:04:06 +000030 * GNU General Public License for more details.
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
35 * MA 02111-1307 USA
36 */
37
38#include <common.h>
39#include <arm925t.h>
40#include <configs/omap1510.h>
Ladislav Michlaa3cc9a2009-03-30 18:58:41 +020041#include <asm/io.h>
wdenkf6f96f72003-07-15 20:04:06 +000042
Ladislav Michla16482e2009-04-22 01:12:04 +020043#define TIMER_LOAD_VAL 0xffffffff
44#define TIMER_CLOCK (CONFIG_SYS_CLK_FREQ / (2 << CONFIG_SYS_PTV))
wdenkf6f96f72003-07-15 20:04:06 +000045
Ladislav Michlaa3cc9a2009-03-30 18:58:41 +020046static uint32_t timestamp;
47static uint32_t lastdec;
wdenkf6f96f72003-07-15 20:04:06 +000048
49/* nothing really to do with interrupts, just starts up a counter. */
Jean-Christophe PLAGNIOL-VILLARD8c9fc002009-05-15 23:47:02 +020050int timer_init (void)
wdenkf6f96f72003-07-15 20:04:06 +000051{
wdenk3da587e2003-10-19 23:22:11 +000052 /* Start the decrementer ticking down from 0xffffffff */
Ladislav Michlaa3cc9a2009-03-30 18:58:41 +020053 __raw_writel(TIMER_LOAD_VAL, CONFIG_SYS_TIMERBASE + LOAD_TIM);
54 __raw_writel(MPUTIM_ST | MPUTIM_AR | MPUTIM_CLOCK_ENABLE |
55 (CONFIG_SYS_PTV << MPUTIM_PTV_BIT),
56 CONFIG_SYS_TIMERBASE + CNTL_TIMER);
wdenk3da587e2003-10-19 23:22:11 +000057
58 /* init the timestamp and lastdec value */
59 reset_timer_masked();
60
Ladislav Michl173a1082009-03-30 18:58:40 +020061 return 0;
wdenkf6f96f72003-07-15 20:04:06 +000062}
63
64/*
65 * timer without interrupts
66 */
wdenkf6f96f72003-07-15 20:04:06 +000067ulong get_timer (ulong base)
68{
69 return get_timer_masked () - base;
70}
71
wdenk7b370962004-04-25 14:37:29 +000072/* delay x useconds AND preserve advance timestamp value */
Ingo van Lilf0f778a2009-11-24 14:09:21 +010073void __udelay (unsigned long usec)
wdenkf6f96f72003-07-15 20:04:06 +000074{
Ladislav Michla16482e2009-04-22 01:12:04 +020075 int32_t tmo = usec * (TIMER_CLOCK / 1000) / 1000;
76 uint32_t now, last = __raw_readl(CONFIG_SYS_TIMERBASE + READ_TIM);
wdenkf6f96f72003-07-15 20:04:06 +000077
Ladislav Michla16482e2009-04-22 01:12:04 +020078 while (tmo > 0) {
79 now = __raw_readl(CONFIG_SYS_TIMERBASE + READ_TIM);
80 if (last < now) /* count down timer underflow */
81 tmo -= TIMER_LOAD_VAL - now + last;
82 else
83 tmo -= last - now;
84 last = now;
wdenk3da587e2003-10-19 23:22:11 +000085 }
wdenkf6f96f72003-07-15 20:04:06 +000086}
87
88void reset_timer_masked (void)
89{
90 /* reset time */
Ladislav Michla16482e2009-04-22 01:12:04 +020091 lastdec = __raw_readl(CONFIG_SYS_TIMERBASE + READ_TIM) /
92 (TIMER_CLOCK / CONFIG_SYS_HZ);
wdenk29e7f5a2004-03-12 00:14:09 +000093 timestamp = 0; /* start "advancing" time stamp from 0 */
wdenkf6f96f72003-07-15 20:04:06 +000094}
95
96ulong get_timer_masked (void)
97{
Ladislav Michla16482e2009-04-22 01:12:04 +020098 uint32_t now = __raw_readl(CONFIG_SYS_TIMERBASE + READ_TIM) /
99 (TIMER_CLOCK / CONFIG_SYS_HZ);
100 if (lastdec < now) /* count down timer underflow */
101 timestamp += TIMER_LOAD_VAL / (TIMER_CLOCK / CONFIG_SYS_HZ) -
102 now + lastdec;
103 else
104 timestamp += lastdec - now;
wdenkf6f96f72003-07-15 20:04:06 +0000105 lastdec = now;
106
107 return timestamp;
108}
109
wdenkf6f96f72003-07-15 20:04:06 +0000110/*
111 * This function is derived from PowerPC code (read timebase as long long).
112 * On ARM it just returns the timer value.
113 */
114unsigned long long get_ticks(void)
115{
116 return get_timer(0);
117}
118
119/*
120 * This function is derived from PowerPC code (timebase clock frequency).
121 * On ARM it returns the number of timer ticks per second.
122 */
123ulong get_tbclk (void)
wdenk3da587e2003-10-19 23:22:11 +0000124{
Ladislav Michl173a1082009-03-30 18:58:40 +0200125 return CONFIG_SYS_HZ;
wdenkf6f96f72003-07-15 20:04:06 +0000126}