blob: e56b576b18be00b7f754d243817b26dcbe317a0d [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 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +020019 * SPDX-License-Identifier: GPL-2.0+
wdenkf6f96f72003-07-15 20:04:06 +000020 */
21
22#include <common.h>
23#include <arm925t.h>
24#include <configs/omap1510.h>
Ladislav Michlaa3cc9a2009-03-30 18:58:41 +020025#include <asm/io.h>
wdenkf6f96f72003-07-15 20:04:06 +000026
Ladislav Michla16482e2009-04-22 01:12:04 +020027#define TIMER_LOAD_VAL 0xffffffff
28#define TIMER_CLOCK (CONFIG_SYS_CLK_FREQ / (2 << CONFIG_SYS_PTV))
wdenkf6f96f72003-07-15 20:04:06 +000029
Ladislav Michlaa3cc9a2009-03-30 18:58:41 +020030static uint32_t timestamp;
31static uint32_t lastdec;
wdenkf6f96f72003-07-15 20:04:06 +000032
33/* nothing really to do with interrupts, just starts up a counter. */
Jean-Christophe PLAGNIOL-VILLARD8c9fc002009-05-15 23:47:02 +020034int timer_init (void)
wdenkf6f96f72003-07-15 20:04:06 +000035{
wdenk3da587e2003-10-19 23:22:11 +000036 /* Start the decrementer ticking down from 0xffffffff */
Ladislav Michlaa3cc9a2009-03-30 18:58:41 +020037 __raw_writel(TIMER_LOAD_VAL, CONFIG_SYS_TIMERBASE + LOAD_TIM);
38 __raw_writel(MPUTIM_ST | MPUTIM_AR | MPUTIM_CLOCK_ENABLE |
39 (CONFIG_SYS_PTV << MPUTIM_PTV_BIT),
40 CONFIG_SYS_TIMERBASE + CNTL_TIMER);
wdenk3da587e2003-10-19 23:22:11 +000041
42 /* init the timestamp and lastdec value */
Graeme Russ944a7fe2011-07-15 02:21:14 +000043 lastdec = __raw_readl(CONFIG_SYS_TIMERBASE + READ_TIM) /
44 (TIMER_CLOCK / CONFIG_SYS_HZ);
45 timestamp = 0; /* start "advancing" time stamp from 0 */
wdenk3da587e2003-10-19 23:22:11 +000046
Ladislav Michl173a1082009-03-30 18:58:40 +020047 return 0;
wdenkf6f96f72003-07-15 20:04:06 +000048}
49
50/*
51 * timer without interrupts
52 */
wdenkf6f96f72003-07-15 20:04:06 +000053ulong get_timer (ulong base)
54{
55 return get_timer_masked () - base;
56}
57
wdenk7b370962004-04-25 14:37:29 +000058/* delay x useconds AND preserve advance timestamp value */
Ingo van Lilf0f778a2009-11-24 14:09:21 +010059void __udelay (unsigned long usec)
wdenkf6f96f72003-07-15 20:04:06 +000060{
Ladislav Michla16482e2009-04-22 01:12:04 +020061 int32_t tmo = usec * (TIMER_CLOCK / 1000) / 1000;
62 uint32_t now, last = __raw_readl(CONFIG_SYS_TIMERBASE + READ_TIM);
wdenkf6f96f72003-07-15 20:04:06 +000063
Ladislav Michla16482e2009-04-22 01:12:04 +020064 while (tmo > 0) {
65 now = __raw_readl(CONFIG_SYS_TIMERBASE + READ_TIM);
66 if (last < now) /* count down timer underflow */
67 tmo -= TIMER_LOAD_VAL - now + last;
68 else
69 tmo -= last - now;
70 last = now;
wdenk3da587e2003-10-19 23:22:11 +000071 }
wdenkf6f96f72003-07-15 20:04:06 +000072}
73
wdenkf6f96f72003-07-15 20:04:06 +000074ulong get_timer_masked (void)
75{
Ladislav Michla16482e2009-04-22 01:12:04 +020076 uint32_t now = __raw_readl(CONFIG_SYS_TIMERBASE + READ_TIM) /
77 (TIMER_CLOCK / CONFIG_SYS_HZ);
78 if (lastdec < now) /* count down timer underflow */
79 timestamp += TIMER_LOAD_VAL / (TIMER_CLOCK / CONFIG_SYS_HZ) -
80 now + lastdec;
81 else
82 timestamp += lastdec - now;
wdenkf6f96f72003-07-15 20:04:06 +000083 lastdec = now;
84
85 return timestamp;
86}
87
wdenkf6f96f72003-07-15 20:04:06 +000088/*
89 * This function is derived from PowerPC code (read timebase as long long).
90 * On ARM it just returns the timer value.
91 */
92unsigned long long get_ticks(void)
93{
94 return get_timer(0);
95}
96
97/*
98 * This function is derived from PowerPC code (timebase clock frequency).
99 * On ARM it returns the number of timer ticks per second.
100 */
101ulong get_tbclk (void)
wdenk3da587e2003-10-19 23:22:11 +0000102{
Ladislav Michl173a1082009-03-30 18:58:40 +0200103 return CONFIG_SYS_HZ;
wdenkf6f96f72003-07-15 20:04:06 +0000104}