]> git.proxmox.com Git - mirror_edk2.git/blob - ArmRealViewEbPkg/Library/TimerLib/TimerLib.c
Remove ArmEbPkg and replace with ArmRealViewEbPkg. Ported ArmRealViewEbPkg to have...
[mirror_edk2.git] / ArmRealViewEbPkg / Library / TimerLib / TimerLib.c
1 /** @file
2 TimerLib for ARM EB. Hardcoded to 100ns period
3
4 This library assume the following initialization, usually done in SEC.
5
6 // configure SP810 to use 1MHz clock and disable
7 MmioAndThenOr32 (EB_SP810_CTRL_BASE + SP810_SYS_CTRL_REG, ~SP810_SYS_CTRL_TIMER2_EN, SP810_SYS_CTRL_TIMER2_TIMCLK);
8 // Enable
9 MmioOr32 (EB_SP810_CTRL_BASE + SP810_SYS_CTRL_REG, SP810_SYS_CTRL_TIMER2_EN);
10
11 // configure timer 2 for one shot operation, 32 bits, no prescaler, and interrupt disabled
12 MmioOr32 (EB_SP804_TIMER2_BASE + SP804_TIMER_CONTROL_REG, SP804_TIMER_CTRL_ONESHOT | SP804_TIMER_CTRL_32BIT | SP804_PRESCALE_DIV_1);
13
14 // preload the timer count register
15 MmioWrite32 (EB_SP804_TIMER2_BASE + SP804_TIMER_LOAD_REG, 1);
16
17 // enable the timer
18 MmioOr32 (EB_SP804_TIMER2_BASE + SP804_TIMER_CONTROL_REG, SP804_TIMER_CTRL_ENABLE);
19
20
21 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
22
23 This program and the accompanying materials
24 are licensed and made available under the terms and conditions of the BSD License
25 which accompanies this distribution. The full text of the license may be found at
26 http://opensource.org/licenses/bsd-license.php
27
28 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
29 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
30
31 **/
32
33 #include <Base.h>
34
35 #include <Library/BaseLib.h>
36 #include <Library/TimerLib.h>
37 #include <Library/DebugLib.h>
38 #include <Library/PcdLib.h>
39 #include <Library/IoLib.h>
40
41 #include <ArmEb/ArmEb.h>
42
43
44 /**
45 Stalls the CPU for at least the given number of microseconds.
46
47 Stalls the CPU for the number of microseconds specified by MicroSeconds.
48
49 @param MicroSeconds The minimum number of microseconds to delay.
50
51 @return The value of MicroSeconds inputted.
52
53 **/
54 UINTN
55 EFIAPI
56 MicroSecondDelay (
57 IN UINTN MicroSeconds
58 )
59 {
60 UINT64 NanoSeconds;
61
62 NanoSeconds = MultU64x32 (MicroSeconds, 1000);
63
64 while (NanoSeconds > (UINTN)-1) {
65 NanoSecondDelay((UINTN)-1);
66 NanoSeconds -= (UINTN)-1;
67 }
68
69 NanoSecondDelay (NanoSeconds);
70
71 return MicroSeconds;
72 }
73
74 /**
75 Stalls the CPU for at least the given number of nanoseconds.
76
77 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
78
79 @param NanoSeconds The minimum number of nanoseconds to delay.
80
81 @return The value of NanoSeconds inputted.
82
83 **/
84 UINTN
85 EFIAPI
86 NanoSecondDelay (
87 IN UINTN NanoSeconds
88 )
89 {
90 UINT32 TickNumber;
91
92 if (NanoSeconds == 0) {
93 return NanoSeconds;
94 }
95
96 // Round up to 100ns Tick Number
97 TickNumber = (UINT32)NanoSeconds / 100;
98 TickNumber += ((UINT32)NanoSeconds % 100) == 0 ? 0 : 1;
99
100 // load the timer count register
101 MmioWrite32 (EB_SP804_TIMER2_BASE + SP804_TIMER_LOAD_REG, TickNumber);
102
103 while (MmioRead32 (EB_SP804_TIMER2_BASE + SP804_TIMER_CURRENT_REG) > 0) {
104 ;
105 }
106
107 return NanoSeconds;
108 }
109
110 /**
111 Retrieves the current value of a 64-bit free running performance counter.
112
113 The counter can either count up by 1 or count down by 1. If the physical
114 performance counter counts by a larger increment, then the counter values
115 must be translated. The properties of the counter can be retrieved from
116 GetPerformanceCounterProperties().
117
118 @return The current value of the free running performance counter.
119
120 **/
121 UINT64
122 EFIAPI
123 GetPerformanceCounter (
124 VOID
125 )
126 {
127 // Free running 64-bit/32-bit counter is needed here.
128 // Don't think we need this to boot, just to do performance profile
129 ASSERT (FALSE);
130 return (UINT64)0ULL;
131 }
132
133
134 /**
135 Retrieves the 64-bit frequency in Hz and the range of performance counter
136 values.
137
138 If StartValue is not NULL, then the value that the performance counter starts
139 with immediately after is it rolls over is returned in StartValue. If
140 EndValue is not NULL, then the value that the performance counter end with
141 immediately before it rolls over is returned in EndValue. The 64-bit
142 frequency of the performance counter in Hz is always returned. If StartValue
143 is less than EndValue, then the performance counter counts up. If StartValue
144 is greater than EndValue, then the performance counter counts down. For
145 example, a 64-bit free running counter that counts up would have a StartValue
146 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
147 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
148
149 @param StartValue The value the performance counter starts with when it
150 rolls over.
151 @param EndValue The value that the performance counter ends with before
152 it rolls over.
153
154 @return The frequency in Hz.
155
156 **/
157 UINT64
158 EFIAPI
159 GetPerformanceCounterProperties (
160 OUT UINT64 *StartValue, OPTIONAL
161 OUT UINT64 *EndValue OPTIONAL
162 )
163 {
164 if (StartValue != NULL) {
165 // Timer starts with the reload value
166 *StartValue = (UINT64)0ULL;
167 }
168
169 if (EndValue != NULL) {
170 // Timer counts up to 0xFFFFFFFF
171 *EndValue = 0xFFFFFFFF;
172 }
173
174 return 100;
175 }
176
177