]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.c
ArmPlatformPkg/PL031RealTimeClockLib: drop ArmPlatformSysConfigLib reference
[mirror_edk2.git] / ArmPlatformPkg / Library / PL031RealTimeClockLib / PL031RealTimeClockLib.c
1 /** @file
2 Implement EFI RealTimeClock runtime services via RTC Lib.
3
4 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
5 Copyright (c) 2011 - 2014, ARM Ltd. All rights reserved.<BR>
6
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17 #include <Uefi.h>
18 #include <PiDxe.h>
19 #include <Library/BaseLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/UefiLib.h>
22 #include <Library/IoLib.h>
23 #include <Library/RealTimeClockLib.h>
24 #include <Library/MemoryAllocationLib.h>
25 #include <Library/PcdLib.h>
26 #include <Library/DxeServicesTableLib.h>
27 #include <Library/UefiBootServicesTableLib.h>
28 #include <Library/UefiRuntimeServicesTableLib.h>
29 #include <Library/UefiRuntimeLib.h>
30
31 #include <Protocol/RealTimeClock.h>
32
33 #include <Guid/GlobalVariable.h>
34 #include <Guid/EventGroup.h>
35
36 #include <Drivers/PL031RealTimeClock.h>
37
38 #include <Library/TimeBaseLib.h>
39
40 STATIC BOOLEAN mPL031Initialized = FALSE;
41 STATIC EFI_EVENT mRtcVirtualAddrChangeEvent;
42 STATIC UINTN mPL031RtcBase;
43
44 EFI_STATUS
45 IdentifyPL031 (
46 VOID
47 )
48 {
49 EFI_STATUS Status;
50
51 // Check if this is a PrimeCell Peripheral
52 if ( (MmioRead8 (mPL031RtcBase + PL031_RTC_PCELL_ID0) != 0x0D)
53 || (MmioRead8 (mPL031RtcBase + PL031_RTC_PCELL_ID1) != 0xF0)
54 || (MmioRead8 (mPL031RtcBase + PL031_RTC_PCELL_ID2) != 0x05)
55 || (MmioRead8 (mPL031RtcBase + PL031_RTC_PCELL_ID3) != 0xB1)) {
56 Status = EFI_NOT_FOUND;
57 goto EXIT;
58 }
59
60 // Check if this PrimeCell Peripheral is the PL031 Real Time Clock
61 if ( (MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID0) != 0x31)
62 || (MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID1) != 0x10)
63 || ((MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID2) & 0xF) != 0x04)
64 || (MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID3) != 0x00)) {
65 Status = EFI_NOT_FOUND;
66 goto EXIT;
67 }
68
69 Status = EFI_SUCCESS;
70
71 EXIT:
72 return Status;
73 }
74
75 EFI_STATUS
76 InitializePL031 (
77 VOID
78 )
79 {
80 EFI_STATUS Status;
81
82 // Prepare the hardware
83 Status = IdentifyPL031();
84 if (EFI_ERROR (Status)) {
85 goto EXIT;
86 }
87
88 // Ensure interrupts are masked. We do not want RTC interrupts in UEFI
89 if ((MmioRead32 (mPL031RtcBase + PL031_RTC_IMSC_IRQ_MASK_SET_CLEAR_REGISTER) & PL031_SET_IRQ_MASK) != PL031_SET_IRQ_MASK) {
90 MmioOr32 (mPL031RtcBase + PL031_RTC_IMSC_IRQ_MASK_SET_CLEAR_REGISTER, PL031_SET_IRQ_MASK);
91 }
92
93 // Clear any existing interrupts
94 if ((MmioRead32 (mPL031RtcBase + PL031_RTC_RIS_RAW_IRQ_STATUS_REGISTER) & PL031_IRQ_TRIGGERED) == PL031_IRQ_TRIGGERED) {
95 MmioOr32 (mPL031RtcBase + PL031_RTC_ICR_IRQ_CLEAR_REGISTER, PL031_CLEAR_IRQ);
96 }
97
98 // Start the clock counter
99 if ((MmioRead32 (mPL031RtcBase + PL031_RTC_CR_CONTROL_REGISTER) & PL031_RTC_ENABLED) != PL031_RTC_ENABLED) {
100 MmioOr32 (mPL031RtcBase + PL031_RTC_CR_CONTROL_REGISTER, PL031_RTC_ENABLED);
101 }
102
103 mPL031Initialized = TRUE;
104
105 EXIT:
106 return Status;
107 }
108
109 /**
110 Returns the current time and date information, and the time-keeping capabilities
111 of the hardware platform.
112
113 @param Time A pointer to storage to receive a snapshot of the current time.
114 @param Capabilities An optional pointer to a buffer to receive the real time clock
115 device's capabilities.
116
117 @retval EFI_SUCCESS The operation completed successfully.
118 @retval EFI_INVALID_PARAMETER Time is NULL.
119 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
120 @retval EFI_SECURITY_VIOLATION The time could not be retrieved due to an authentication failure.
121
122 **/
123 EFI_STATUS
124 EFIAPI
125 LibGetTime (
126 OUT EFI_TIME *Time,
127 OUT EFI_TIME_CAPABILITIES *Capabilities
128 )
129 {
130 EFI_STATUS Status = EFI_SUCCESS;
131 UINT32 EpochSeconds;
132
133 // Ensure Time is a valid pointer
134 if (Time == NULL) {
135 return EFI_INVALID_PARAMETER;
136 }
137
138 // Initialize the hardware if not already done
139 if (!mPL031Initialized) {
140 Status = InitializePL031 ();
141 if (EFI_ERROR (Status)) {
142 return Status;
143 }
144 }
145
146 EpochSeconds = MmioRead32 (mPL031RtcBase + PL031_RTC_DR_DATA_REGISTER);
147
148 // Adjust for the correct time zone
149 // The timezone setting also reflects the DST setting of the clock
150 if (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {
151 EpochSeconds += Time->TimeZone * SEC_PER_MIN;
152 } else if ((Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT) {
153 // Convert to adjusted time, i.e. spring forwards one hour
154 EpochSeconds += SEC_PER_HOUR;
155 }
156
157 // Convert from internal 32-bit time to UEFI time
158 EpochToEfiTime (EpochSeconds, Time);
159
160 // Update the Capabilities info
161 if (Capabilities != NULL) {
162 // PL031 runs at frequency 1Hz
163 Capabilities->Resolution = PL031_COUNTS_PER_SECOND;
164 // Accuracy in ppm multiplied by 1,000,000, e.g. for 50ppm set 50,000,000
165 Capabilities->Accuracy = (UINT32)PcdGet32 (PcdPL031RtcPpmAccuracy);
166 // FALSE: Setting the time does not clear the values below the resolution level
167 Capabilities->SetsToZero = FALSE;
168 }
169
170 return EFI_SUCCESS;
171 }
172
173
174 /**
175 Sets the current local time and date information.
176
177 @param Time A pointer to the current time.
178
179 @retval EFI_SUCCESS The operation completed successfully.
180 @retval EFI_INVALID_PARAMETER A time field is out of range.
181 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
182
183 **/
184 EFI_STATUS
185 EFIAPI
186 LibSetTime (
187 IN EFI_TIME *Time
188 )
189 {
190 EFI_STATUS Status;
191 UINTN EpochSeconds;
192
193 // Because the PL031 is a 32-bit counter counting seconds,
194 // the maximum time span is just over 136 years.
195 // Time is stored in Unix Epoch format, so it starts in 1970,
196 // Therefore it can not exceed the year 2106.
197 if ((Time->Year < 1970) || (Time->Year >= 2106)) {
198 return EFI_UNSUPPORTED;
199 }
200
201 // Initialize the hardware if not already done
202 if (!mPL031Initialized) {
203 Status = InitializePL031 ();
204 if (EFI_ERROR (Status)) {
205 return Status;
206 }
207 }
208
209 EpochSeconds = EfiTimeToEpoch (Time);
210
211 // Adjust for the correct time zone, i.e. convert to UTC time zone
212 // The timezone setting also reflects the DST setting of the clock
213 if (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {
214 EpochSeconds -= Time->TimeZone * SEC_PER_MIN;
215 } else if ((Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT) {
216 // Convert to un-adjusted time, i.e. fall back one hour
217 EpochSeconds -= SEC_PER_HOUR;
218 }
219
220 // Set the PL031
221 MmioWrite32 (mPL031RtcBase + PL031_RTC_LR_LOAD_REGISTER, EpochSeconds);
222
223 return EFI_SUCCESS;
224 }
225
226
227 /**
228 Returns the current wakeup alarm clock setting.
229
230 @param Enabled Indicates if the alarm is currently enabled or disabled.
231 @param Pending Indicates if the alarm signal is pending and requires acknowledgement.
232 @param Time The current alarm setting.
233
234 @retval EFI_SUCCESS The alarm settings were returned.
235 @retval EFI_INVALID_PARAMETER Any parameter is NULL.
236 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
237
238 **/
239 EFI_STATUS
240 EFIAPI
241 LibGetWakeupTime (
242 OUT BOOLEAN *Enabled,
243 OUT BOOLEAN *Pending,
244 OUT EFI_TIME *Time
245 )
246 {
247 // Not a required feature
248 return EFI_UNSUPPORTED;
249 }
250
251
252 /**
253 Sets the system wakeup alarm clock time.
254
255 @param Enabled Enable or disable the wakeup alarm.
256 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
257
258 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If
259 Enable is FALSE, then the wakeup alarm was disabled.
260 @retval EFI_INVALID_PARAMETER A time field is out of range.
261 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
262 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
263
264 **/
265 EFI_STATUS
266 EFIAPI
267 LibSetWakeupTime (
268 IN BOOLEAN Enabled,
269 OUT EFI_TIME *Time
270 )
271 {
272 // Not a required feature
273 return EFI_UNSUPPORTED;
274 }
275
276 /**
277 Fixup internal data so that EFI can be call in virtual mode.
278 Call the passed in Child Notify event and convert any pointers in
279 lib to virtual mode.
280
281 @param[in] Event The Event that is being processed
282 @param[in] Context Event Context
283 **/
284 VOID
285 EFIAPI
286 LibRtcVirtualNotifyEvent (
287 IN EFI_EVENT Event,
288 IN VOID *Context
289 )
290 {
291 //
292 // Only needed if you are going to support the OS calling RTC functions in virtual mode.
293 // You will need to call EfiConvertPointer (). To convert any stored physical addresses
294 // to virtual address. After the OS transitions to calling in virtual mode, all future
295 // runtime calls will be made in virtual mode.
296 //
297 EfiConvertPointer (0x0, (VOID**)&mPL031RtcBase);
298 return;
299 }
300
301 /**
302 This is the declaration of an EFI image entry point. This can be the entry point to an application
303 written to this specification, an EFI boot service driver, or an EFI runtime driver.
304
305 @param ImageHandle Handle that identifies the loaded image.
306 @param SystemTable System Table for this image.
307
308 @retval EFI_SUCCESS The operation completed successfully.
309
310 **/
311 EFI_STATUS
312 EFIAPI
313 LibRtcInitialize (
314 IN EFI_HANDLE ImageHandle,
315 IN EFI_SYSTEM_TABLE *SystemTable
316 )
317 {
318 EFI_STATUS Status;
319 EFI_HANDLE Handle;
320
321 // Initialize RTC Base Address
322 mPL031RtcBase = PcdGet32 (PcdPL031RtcBase);
323
324 // Declare the controller as EFI_MEMORY_RUNTIME
325 Status = gDS->AddMemorySpace (
326 EfiGcdMemoryTypeMemoryMappedIo,
327 mPL031RtcBase, SIZE_4KB,
328 EFI_MEMORY_UC | EFI_MEMORY_RUNTIME
329 );
330 if (EFI_ERROR (Status)) {
331 return Status;
332 }
333
334 Status = gDS->SetMemorySpaceAttributes (mPL031RtcBase, SIZE_4KB, EFI_MEMORY_UC | EFI_MEMORY_RUNTIME);
335 if (EFI_ERROR (Status)) {
336 return Status;
337 }
338
339 // Install the protocol
340 Handle = NULL;
341 Status = gBS->InstallMultipleProtocolInterfaces (
342 &Handle,
343 &gEfiRealTimeClockArchProtocolGuid, NULL,
344 NULL
345 );
346 ASSERT_EFI_ERROR (Status);
347
348 //
349 // Register for the virtual address change event
350 //
351 Status = gBS->CreateEventEx (
352 EVT_NOTIFY_SIGNAL,
353 TPL_NOTIFY,
354 LibRtcVirtualNotifyEvent,
355 NULL,
356 &gEfiEventVirtualAddressChangeGuid,
357 &mRtcVirtualAddrChangeEvent
358 );
359 ASSERT_EFI_ERROR (Status);
360
361 return Status;
362 }