blob: 870f927e5d4ff9d6ad0a04d2778120f4c45368ee [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>
Wolfgang Denkd86ec382006-03-13 12:37:35 +010023
24#define TIMER_LOAD_VAL 0xffffffff
25
26/* macro to read the 32 bit timer */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020027#define READ_TIMER (*(volatile ulong *)(CONFIG_SYS_TIMERBASE+4))
Wolfgang Denkd86ec382006-03-13 12:37:35 +010028
Heiko Schocher5504dab2011-01-20 22:56:39 +000029DECLARE_GLOBAL_DATA_PTR;
30
Simon Glass2655ee12012-12-13 20:48:34 +000031#define timestamp gd->arch.tbl
Simon Glassa848da52012-12-13 20:48:35 +000032#define lastdec gd->arch.lastinc
Wolfgang Denkd86ec382006-03-13 12:37:35 +010033
Gururaja Hebbar K Rb675aa32008-08-25 11:11:34 +020034#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)
38
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)
44
Wolfgang Denkd86ec382006-03-13 12:37:35 +010045int timer_init (void)
46{
Gururaja Hebbar K Rb675aa32008-08-25 11:11:34 +020047 ulong tmr_ctrl_val;
48
49 /* 1st disable the Timer */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020050 tmr_ctrl_val = *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 8);
Gururaja Hebbar K Rb675aa32008-08-25 11:11:34 +020051 tmr_ctrl_val &= ~TIMER_ENABLE;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020052 *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 8) = tmr_ctrl_val;
Gururaja Hebbar K Rb675aa32008-08-25 11:11:34 +020053
54 /*
55 * The Timer Control Register has one Undefined/Shouldn't Use Bit
56 * So we should do read/modify/write Operation
57 */
58
59 /*
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
65 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020066 tmr_ctrl_val = *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 8);
Gururaja Hebbar K Rb675aa32008-08-25 11:11:34 +020067 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);
69
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020070 *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 8) = tmr_ctrl_val;
Wolfgang Denkd86ec382006-03-13 12:37:35 +010071
72 /* init the timestamp and lastdec value */
73 reset_timer_masked();
74
75 return 0;
76}
77
78/*
79 * timer without interrupts
80 */
Wolfgang Denkd86ec382006-03-13 12:37:35 +010081ulong get_timer (ulong base)
82{
83 return get_timer_masked () - base;
84}
85
Wolfgang Denkd7cb3552010-05-21 23:13:18 +020086/* delay x useconds AND preserve advance timestamp value */
Ingo van Lilf0f778a2009-11-24 14:09:21 +010087void __udelay (unsigned long usec)
Wolfgang Denkd86ec382006-03-13 12:37:35 +010088{
89 ulong tmo, tmp;
90
91 if(usec >= 1000){ /* if "big" number, spread normalization to seconds */
92 tmo = usec / 1000; /* start to normalize for usec to ticks per sec */
Wolfgang Denkd7cb3552010-05-21 23:13:18 +020093 tmo *= CONFIG_SYS_HZ; /* find number of "ticks" to wait to achieve target */
Wolfgang Denkd86ec382006-03-13 12:37:35 +010094 tmo /= 1000; /* finish normalize. */
95 }else{ /* else small number, don't kill it prior to HZ multiply */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020096 tmo = usec * CONFIG_SYS_HZ;
Wolfgang Denkd86ec382006-03-13 12:37:35 +010097 tmo /= (1000*1000);
98 }
99
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 */
103 else
104 tmo += tmp; /* else, set advancing stamp wake up time */
105
106 while (get_timer_masked () < tmo)/* loop till event */
107 /*NOP*/;
108}
109
110void reset_timer_masked (void)
111{
112 /* reset time */
113 lastdec = READ_TIMER; /* capure current decrementer value time */
114 timestamp = 0; /* start "advancing" time stamp from 0 */
115}
116
117ulong get_timer_masked (void)
118{
119 ulong now = READ_TIMER; /* current tick value */
120
121 if (lastdec >= now) { /* normal mode (non roll) */
122 /* normal mode */
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.
129 */
130 timestamp += lastdec + TIMER_LOAD_VAL - now;
131 }
132 lastdec = now;
133
134 return timestamp;
135}
136
137/* waits specified delay value and resets timestamp */
138void udelay_masked (unsigned long usec)
139{
140 ulong tmo;
141 ulong endtime;
142 signed long diff;
143
144 if (usec >= 1000) { /* if "big" number, spread normalization to seconds */
145 tmo = usec / 1000; /* start to normalize for usec to ticks per sec */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200146 tmo *= CONFIG_SYS_HZ; /* find number of "ticks" to wait to achieve target */
Wolfgang Denkd86ec382006-03-13 12:37:35 +0100147 tmo /= 1000; /* finish normalize. */
148 } else { /* else small number, don't kill it prior to HZ multiply */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200149 tmo = usec * CONFIG_SYS_HZ;
Wolfgang Denkd86ec382006-03-13 12:37:35 +0100150 tmo /= (1000*1000);
151 }
152
153 endtime = get_timer_masked () + tmo;
154
155 do {
156 ulong now = get_timer_masked ();
157 diff = endtime - now;
158 } while (diff >= 0);
159}
160
161/*
162 * This function is derived from PowerPC code (read timebase as long long).
163 * On ARM it just returns the timer value.
164 */
165unsigned long long get_ticks(void)
166{
167 return get_timer(0);
168}
169
170/*
171 * This function is derived from PowerPC code (timebase clock frequency).
172 * On ARM it returns the number of timer ticks per second.
173 */
174ulong get_tbclk (void)
175{
176 ulong tbclk;
177
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200178 tbclk = CONFIG_SYS_HZ;
Wolfgang Denkd86ec382006-03-13 12:37:35 +0100179 return tbclk;
180}