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