3 * Texas Instruments <www.ti.com>
6 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Marius Groeger <mgroeger@sysgo.de>
10 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
11 * Alex Zuepke <azu@sysgo.de>
13 * (C) Copyright 2002-2004
14 * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
17 * Philippe Robin, ARM Ltd. <philippe.robin@arm.com>
19 * SPDX-License-Identifier: GPL-2.0+
24 #define TIMER_LOAD_VAL 0xffffffff
26 /* macro to read the 32 bit timer */
27 #define READ_TIMER (*(volatile ulong *)(CONFIG_SYS_TIMERBASE+4))
29 DECLARE_GLOBAL_DATA_PTR;
31 #define timestamp gd->arch.tbl
32 #define lastdec gd->arch.lastinc
34 #define TIMER_ENABLE (1 << 7)
35 #define TIMER_MODE_MSK (1 << 6)
36 #define TIMER_MODE_FR (0 << 6)
37 #define TIMER_MODE_PD (1 << 6)
39 #define TIMER_INT_EN (1 << 5)
40 #define TIMER_PRS_MSK (3 << 2)
41 #define TIMER_PRS_8S (1 << 3)
42 #define TIMER_SIZE_MSK (1 << 2)
43 #define TIMER_ONE_SHT (1 << 0)
49 /* 1st disable the Timer */
50 tmr_ctrl_val = *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 8);
51 tmr_ctrl_val &= ~TIMER_ENABLE;
52 *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 8) = tmr_ctrl_val;
55 * The Timer Control Register has one Undefined/Shouldn't Use Bit
56 * So we should do read/modify/write Operation
60 * Timer Mode : Free Running
61 * Interrupt : Disabled
62 * Prescale : 8 Stage, Clk/256
63 * Tmr Siz : 16 Bit Counter
64 * Tmr in Wrapping Mode
66 tmr_ctrl_val = *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 8);
67 tmr_ctrl_val &= ~(TIMER_MODE_MSK | TIMER_INT_EN | TIMER_PRS_MSK | TIMER_SIZE_MSK | TIMER_ONE_SHT );
68 tmr_ctrl_val |= (TIMER_ENABLE | TIMER_PRS_8S);
70 *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 8) = tmr_ctrl_val;
72 /* init the timestamp and lastdec value */
79 * timer without interrupts
81 ulong get_timer (ulong base)
83 return get_timer_masked () - base;
86 /* delay x useconds AND preserve advance timestamp value */
87 void __udelay (unsigned long usec)
91 if(usec >= 1000){ /* if "big" number, spread normalization to seconds */
92 tmo = usec / 1000; /* start to normalize for usec to ticks per sec */
93 tmo *= CONFIG_SYS_HZ; /* find number of "ticks" to wait to achieve target */
94 tmo /= 1000; /* finish normalize. */
95 }else{ /* else small number, don't kill it prior to HZ multiply */
96 tmo = usec * CONFIG_SYS_HZ;
100 tmp = get_timer (0); /* get current timestamp */
101 if( (tmo + tmp + 1) < tmp ) /* if setting this fordward will roll time stamp */
102 reset_timer_masked (); /* reset "advancing" timestamp to 0, set lastdec value */
104 tmo += tmp; /* else, set advancing stamp wake up time */
106 while (get_timer_masked () < tmo)/* loop till event */
110 void reset_timer_masked (void)
113 lastdec = READ_TIMER; /* capure current decrementer value time */
114 timestamp = 0; /* start "advancing" time stamp from 0 */
117 ulong get_timer_masked (void)
119 ulong now = READ_TIMER; /* current tick value */
121 if (lastdec >= now) { /* normal mode (non roll) */
123 timestamp += lastdec - now; /* move stamp fordward with absoulte diff ticks */
124 } else { /* we have overflow of the count down timer */
125 /* nts = ts + ld + (TLV - now)
126 * ts=old stamp, ld=time that passed before passing through -1
127 * (TLV-now) amount of time after passing though -1
128 * nts = new "advancing time stamp"...it could also roll and cause problems.
130 timestamp += lastdec + TIMER_LOAD_VAL - now;
137 /* waits specified delay value and resets timestamp */
138 void udelay_masked (unsigned long usec)
144 if (usec >= 1000) { /* if "big" number, spread normalization to seconds */
145 tmo = usec / 1000; /* start to normalize for usec to ticks per sec */
146 tmo *= CONFIG_SYS_HZ; /* find number of "ticks" to wait to achieve target */
147 tmo /= 1000; /* finish normalize. */
148 } else { /* else small number, don't kill it prior to HZ multiply */
149 tmo = usec * CONFIG_SYS_HZ;
153 endtime = get_timer_masked () + tmo;
156 ulong now = get_timer_masked ();
157 diff = endtime - now;
162 * This function is derived from PowerPC code (read timebase as long long).
163 * On ARM it just returns the timer value.
165 unsigned long long get_ticks(void)
171 * This function is derived from PowerPC code (timebase clock frequency).
172 * On ARM it returns the number of timer ticks per second.
174 ulong get_tbclk (void)
178 tbclk = CONFIG_SYS_HZ;