]>
Commit | Line | Data |
---|---|---|
ff7fefe6 WD |
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 | |
792a09eb | 14 | * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de> |
ff7fefe6 WD |
15 | * |
16 | * (C) Copyright 2004 | |
17 | * Philippe Robin, ARM Ltd. <philippe.robin@arm.com> | |
18 | * | |
3765b3e7 | 19 | * SPDX-License-Identifier: GPL-2.0+ |
ff7fefe6 WD |
20 | */ |
21 | ||
22 | #include <common.h> | |
eef6da03 | 23 | #include <asm/io.h> |
ff7fefe6 | 24 | |
eef6da03 JH |
25 | #define TIMER_CLOCK (CONFIG_SYS_CLK_FREQ / (2 << CONFIG_SYS_PTV)) |
26 | #define TIMER_LOAD_VAL 0xffffffff | |
ff7fefe6 WD |
27 | |
28 | /* macro to read the 32 bit timer */ | |
eef6da03 JH |
29 | #define READ_TIMER readl(CONFIG_SYS_TIMERBASE+8) \ |
30 | / (TIMER_CLOCK / CONFIG_SYS_HZ) | |
ff7fefe6 | 31 | |
c9ac3ba1 HS |
32 | DECLARE_GLOBAL_DATA_PTR; |
33 | ||
66ee6923 | 34 | #define timestamp gd->arch.tbl |
582601da | 35 | #define lastdec gd->arch.lastinc |
ff7fefe6 WD |
36 | |
37 | int timer_init (void) | |
38 | { | |
39 | int32_t val; | |
40 | ||
41 | /* Start the decrementer ticking down from 0xffffffff */ | |
6d0f6bcf | 42 | *((int32_t *) (CONFIG_SYS_TIMERBASE + LOAD_TIM)) = TIMER_LOAD_VAL; |
81472d89 | 43 | val = MPUTIM_ST | MPUTIM_AR | MPUTIM_CLOCK_ENABLE | (CONFIG_SYS_PTV << MPUTIM_PTV_BIT); |
6d0f6bcf | 44 | *((int32_t *) (CONFIG_SYS_TIMERBASE + CNTL_TIMER)) = val; |
ff7fefe6 WD |
45 | |
46 | /* init the timestamp and lastdec value */ | |
47 | reset_timer_masked(); | |
48 | ||
49 | return 0; | |
50 | } | |
51 | ||
52 | /* | |
53 | * timer without interrupts | |
54 | */ | |
ff7fefe6 WD |
55 | ulong get_timer (ulong base) |
56 | { | |
57 | return get_timer_masked () - base; | |
58 | } | |
59 | ||
92381c41 | 60 | /* delay x useconds AND preserve advance timestamp value */ |
3eb90bad | 61 | void __udelay (unsigned long usec) |
ff7fefe6 WD |
62 | { |
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 */ | |
92381c41 | 67 | tmo *= CONFIG_SYS_HZ; /* find number of "ticks" to wait to achieve target */ |
ff7fefe6 WD |
68 | tmo /= 1000; /* finish normalize. */ |
69 | }else{ /* else small number, don't kill it prior to HZ multiply */ | |
6d0f6bcf | 70 | tmo = usec * CONFIG_SYS_HZ; |
ff7fefe6 WD |
71 | 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 | ||
84 | void 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 | ||
91 | ulong 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 | */ | |
eef6da03 JH |
104 | timestamp += lastdec + (TIMER_LOAD_VAL / (TIMER_CLOCK / |
105 | CONFIG_SYS_HZ)) - now; | |
ff7fefe6 WD |
106 | } |
107 | lastdec = now; | |
108 | ||
109 | return timestamp; | |
110 | } | |
111 | ||
112 | /* waits specified delay value and resets timestamp */ | |
113 | void 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 */ | |
6d0f6bcf | 121 | tmo *= CONFIG_SYS_HZ; /* find number of "ticks" to wait to achieve target */ |
ff7fefe6 WD |
122 | tmo /= 1000; /* finish normalize. */ |
123 | } else { /* else small number, don't kill it prior to HZ multiply */ | |
6d0f6bcf | 124 | tmo = usec * CONFIG_SYS_HZ; |
ff7fefe6 WD |
125 | 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 | */ | |
140 | unsigned 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 | */ | |
149 | ulong get_tbclk (void) | |
150 | { | |
eef6da03 | 151 | return CONFIG_SYS_HZ; |
ff7fefe6 | 152 | } |