]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/arm/cpu/arm926ejs/versatile/timer.c
54361622b47e0fbd3bdac722aa663a736e552c9b
[karo-tx-uboot.git] / arch / arm / cpu / arm926ejs / versatile / timer.c
1 /*
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
14  * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
15  *
16  * (C) Copyright 2004
17  * Philippe Robin, ARM Ltd. <philippe.robin@arm.com>
18  *
19  * SPDX-License-Identifier:     GPL-2.0+ 
20  */
21
22 #include <common.h>
23
24 #define TIMER_LOAD_VAL 0xffffffff
25
26 /* macro to read the 32 bit timer */
27 #define READ_TIMER (*(volatile ulong *)(CONFIG_SYS_TIMERBASE+4))
28
29 DECLARE_GLOBAL_DATA_PTR;
30
31 #define timestamp gd->arch.tbl
32 #define lastdec gd->arch.lastinc
33
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)
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
45 int timer_init (void)
46 {
47         ulong   tmr_ctrl_val;
48
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;
53
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          */
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);
69
70         *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 8) = tmr_ctrl_val;
71
72         /* init the timestamp and lastdec value */
73         reset_timer_masked();
74
75         return 0;
76 }
77
78 /*
79  * timer without interrupts
80  */
81 ulong get_timer (ulong base)
82 {
83         return get_timer_masked () - base;
84 }
85
86 /* delay x useconds AND preserve advance timestamp value */
87 void __udelay (unsigned long usec)
88 {
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 */
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;
97                 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
110 void 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
117 ulong 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 */
138 void 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 */
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;
150                 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  */
165 unsigned 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  */
174 ulong get_tbclk (void)
175 {
176         ulong tbclk;
177
178         tbclk = CONFIG_SYS_HZ;
179         return tbclk;
180 }