blob: b9715656c8f3743b880169a0cedbc1e93515df71 [file] [log] [blame]
Wolfgang Denkd86ec382006-03-13 12:37:35 +01001/*
2 * (C) Copyright 2003
3 * Texas Instruments <www.ti.com>
4 *
5 * (C) Copyright 2002
6 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Marius Groeger <mgroeger@sysgo.de>
8 *
9 * (C) Copyright 2002
10 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
11 * Alex Zuepke <azu@sysgo.de>
12 *
13 * (C) Copyright 2002-2004
Detlev Zundelf1b3f2b2009-05-13 10:54:10 +020014 * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
Wolfgang Denkd86ec382006-03-13 12:37:35 +010015 *
16 * (C) Copyright 2004
17 * Philippe Robin, ARM Ltd. <philippe.robin@arm.com>
18 *
Wolfgang Denkbd8ec7e2013-10-07 13:07:26 +020019 * SPDX-License-Identifier: GPL-2.0+
Wolfgang Denkd86ec382006-03-13 12:37:35 +010020 */
21
22#include <common.h>
Jon Huntere427c6f2013-04-09 16:41:33 -050023#include <asm/io.h>
Wolfgang Denkd86ec382006-03-13 12:37:35 +010024
Jon Huntere427c6f2013-04-09 16:41:33 -050025#define TIMER_CLOCK (CONFIG_SYS_CLK_FREQ / (2 << CONFIG_SYS_PTV))
26#define TIMER_LOAD_VAL 0xffffffff
Wolfgang Denkd86ec382006-03-13 12:37:35 +010027
28/* macro to read the 32 bit timer */
Jon Huntere427c6f2013-04-09 16:41:33 -050029#define READ_TIMER readl(CONFIG_SYS_TIMERBASE+8) \
30 / (TIMER_CLOCK / CONFIG_SYS_HZ)
Wolfgang Denkd86ec382006-03-13 12:37:35 +010031
Heiko Schocher5504dab2011-01-20 22:56:39 +000032DECLARE_GLOBAL_DATA_PTR;
33
Simon Glass2655ee12012-12-13 20:48:34 +000034#define timestamp gd->arch.tbl
Simon Glassa848da52012-12-13 20:48:35 +000035#define lastdec gd->arch.lastinc
Wolfgang Denkd86ec382006-03-13 12:37:35 +010036
37int timer_init (void)
38{
39 int32_t val;
40
41 /* Start the decrementer ticking down from 0xffffffff */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020042 *((int32_t *) (CONFIG_SYS_TIMERBASE + LOAD_TIM)) = TIMER_LOAD_VAL;
Ladislav Michl993e57d2009-03-30 18:58:41 +020043 val = MPUTIM_ST | MPUTIM_AR | MPUTIM_CLOCK_ENABLE | (CONFIG_SYS_PTV << MPUTIM_PTV_BIT);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020044 *((int32_t *) (CONFIG_SYS_TIMERBASE + CNTL_TIMER)) = val;
Wolfgang Denkd86ec382006-03-13 12:37:35 +010045
46 /* init the timestamp and lastdec value */
47 reset_timer_masked();
48
49 return 0;
50}
51
52/*
53 * timer without interrupts
54 */
Wolfgang Denkd86ec382006-03-13 12:37:35 +010055ulong get_timer (ulong base)
56{
57 return get_timer_masked () - base;
58}
59
Wolfgang Denkd7cb3552010-05-21 23:13:18 +020060/* delay x useconds AND preserve advance timestamp value */
Ingo van Lilf0f778a2009-11-24 14:09:21 +010061void __udelay (unsigned long usec)
Wolfgang Denkd86ec382006-03-13 12:37:35 +010062{
63 ulong tmo, tmp;
64
65 if(usec >= 1000){ /* if "big" number, spread normalization to seconds */
66 tmo = usec / 1000; /* start to normalize for usec to ticks per sec */
Wolfgang Denkd7cb3552010-05-21 23:13:18 +020067 tmo *= CONFIG_SYS_HZ; /* find number of "ticks" to wait to achieve target */
Wolfgang Denkd86ec382006-03-13 12:37:35 +010068 tmo /= 1000; /* finish normalize. */
69 }else{ /* else small number, don't kill it prior to HZ multiply */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020070 tmo = usec * CONFIG_SYS_HZ;
Wolfgang Denkd86ec382006-03-13 12:37:35 +010071 tmo /= (1000*1000);
72 }
73
74 tmp = get_timer (0); /* get current timestamp */
75 if( (tmo + tmp + 1) < tmp ) /* if setting this fordward will roll time stamp */
76 reset_timer_masked (); /* reset "advancing" timestamp to 0, set lastdec value */
77 else
78 tmo += tmp; /* else, set advancing stamp wake up time */
79
80 while (get_timer_masked () < tmo)/* loop till event */
81 /*NOP*/;
82}
83
84void reset_timer_masked (void)
85{
86 /* reset time */
87 lastdec = READ_TIMER; /* capure current decrementer value time */
88 timestamp = 0; /* start "advancing" time stamp from 0 */
89}
90
91ulong get_timer_masked (void)
92{
93 ulong now = READ_TIMER; /* current tick value */
94
95 if (lastdec >= now) { /* normal mode (non roll) */
96 /* normal mode */
97 timestamp += lastdec - now; /* move stamp fordward with absoulte diff ticks */
98 } else { /* we have overflow of the count down timer */
99 /* nts = ts + ld + (TLV - now)
100 * ts=old stamp, ld=time that passed before passing through -1
101 * (TLV-now) amount of time after passing though -1
102 * nts = new "advancing time stamp"...it could also roll and cause problems.
103 */
Jon Huntere427c6f2013-04-09 16:41:33 -0500104 timestamp += lastdec + (TIMER_LOAD_VAL / (TIMER_CLOCK /
105 CONFIG_SYS_HZ)) - now;
Wolfgang Denkd86ec382006-03-13 12:37:35 +0100106 }
107 lastdec = now;
108
109 return timestamp;
110}
111
112/* waits specified delay value and resets timestamp */
113void udelay_masked (unsigned long usec)
114{
115 ulong tmo;
116 ulong endtime;
117 signed long diff;
118
119 if (usec >= 1000) { /* if "big" number, spread normalization to seconds */
120 tmo = usec / 1000; /* start to normalize for usec to ticks per sec */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200121 tmo *= CONFIG_SYS_HZ; /* find number of "ticks" to wait to achieve target */
Wolfgang Denkd86ec382006-03-13 12:37:35 +0100122 tmo /= 1000; /* finish normalize. */
123 } else { /* else small number, don't kill it prior to HZ multiply */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200124 tmo = usec * CONFIG_SYS_HZ;
Wolfgang Denkd86ec382006-03-13 12:37:35 +0100125 tmo /= (1000*1000);
126 }
127
128 endtime = get_timer_masked () + tmo;
129
130 do {
131 ulong now = get_timer_masked ();
132 diff = endtime - now;
133 } while (diff >= 0);
134}
135
136/*
137 * This function is derived from PowerPC code (read timebase as long long).
138 * On ARM it just returns the timer value.
139 */
140unsigned long long get_ticks(void)
141{
142 return get_timer(0);
143}
144
145/*
146 * This function is derived from PowerPC code (timebase clock frequency).
147 * On ARM it returns the number of timer ticks per second.
148 */
149ulong get_tbclk (void)
150{
Jon Huntere427c6f2013-04-09 16:41:33 -0500151 return CONFIG_SYS_HZ;
Wolfgang Denkd86ec382006-03-13 12:37:35 +0100152}