]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.c
MdePkg/PciSegmentInfoLib: Add PciSegmentInfoLib class and instance.
[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/ArmPlatformSysConfigLib.h>
27 #include <Library/DxeServicesTableLib.h>
28 #include <Library/UefiBootServicesTableLib.h>
29 #include <Library/UefiRuntimeServicesTableLib.h>
30 #include <Library/UefiRuntimeLib.h>
31
32 #include <Protocol/RealTimeClock.h>
33
34 #include <Guid/GlobalVariable.h>
35 #include <Guid/EventGroup.h>
36
37 #include <Drivers/PL031RealTimeClock.h>
38
39 #include <Library/TimeBaseLib.h>
40
41 #include <ArmPlatform.h>
42
43 STATIC CONST CHAR16 mTimeZoneVariableName[] = L"PL031RtcTimeZone";
44 STATIC CONST CHAR16 mDaylightVariableName[] = L"PL031RtcDaylight";
45 STATIC BOOLEAN mPL031Initialized = FALSE;
46 STATIC EFI_EVENT mRtcVirtualAddrChangeEvent;
47 STATIC UINTN mPL031RtcBase;
48
49 EFI_STATUS
50 IdentifyPL031 (
51 VOID
52 )
53 {
54 EFI_STATUS Status;
55
56 // Check if this is a PrimeCell Peripheral
57 if ( (MmioRead8 (mPL031RtcBase + PL031_RTC_PCELL_ID0) != 0x0D)
58 || (MmioRead8 (mPL031RtcBase + PL031_RTC_PCELL_ID1) != 0xF0)
59 || (MmioRead8 (mPL031RtcBase + PL031_RTC_PCELL_ID2) != 0x05)
60 || (MmioRead8 (mPL031RtcBase + PL031_RTC_PCELL_ID3) != 0xB1)) {
61 Status = EFI_NOT_FOUND;
62 goto EXIT;
63 }
64
65 // Check if this PrimeCell Peripheral is the PL031 Real Time Clock
66 if ( (MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID0) != 0x31)
67 || (MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID1) != 0x10)
68 || ((MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID2) & 0xF) != 0x04)
69 || (MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID3) != 0x00)) {
70 Status = EFI_NOT_FOUND;
71 goto EXIT;
72 }
73
74 Status = EFI_SUCCESS;
75
76 EXIT:
77 return Status;
78 }
79
80 EFI_STATUS
81 InitializePL031 (
82 VOID
83 )
84 {
85 EFI_STATUS Status;
86
87 // Prepare the hardware
88 Status = IdentifyPL031();
89 if (EFI_ERROR (Status)) {
90 goto EXIT;
91 }
92
93 // Ensure interrupts are masked. We do not want RTC interrupts in UEFI
94 if ((MmioRead32 (mPL031RtcBase + PL031_RTC_IMSC_IRQ_MASK_SET_CLEAR_REGISTER) & PL031_SET_IRQ_MASK) != PL031_SET_IRQ_MASK) {
95 MmioOr32 (mPL031RtcBase + PL031_RTC_IMSC_IRQ_MASK_SET_CLEAR_REGISTER, PL031_SET_IRQ_MASK);
96 }
97
98 // Clear any existing interrupts
99 if ((MmioRead32 (mPL031RtcBase + PL031_RTC_RIS_RAW_IRQ_STATUS_REGISTER) & PL031_IRQ_TRIGGERED) == PL031_IRQ_TRIGGERED) {
100 MmioOr32 (mPL031RtcBase + PL031_RTC_ICR_IRQ_CLEAR_REGISTER, PL031_CLEAR_IRQ);
101 }
102
103 // Start the clock counter
104 if ((MmioRead32 (mPL031RtcBase + PL031_RTC_CR_CONTROL_REGISTER) & PL031_RTC_ENABLED) != PL031_RTC_ENABLED) {
105 MmioOr32 (mPL031RtcBase + PL031_RTC_CR_CONTROL_REGISTER, PL031_RTC_ENABLED);
106 }
107
108 mPL031Initialized = TRUE;
109
110 EXIT:
111 return Status;
112 }
113
114 /**
115 Returns the current time and date information, and the time-keeping capabilities
116 of the hardware platform.
117
118 @param Time A pointer to storage to receive a snapshot of the current time.
119 @param Capabilities An optional pointer to a buffer to receive the real time clock
120 device's capabilities.
121
122 @retval EFI_SUCCESS The operation completed successfully.
123 @retval EFI_INVALID_PARAMETER Time is NULL.
124 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
125 @retval EFI_SECURITY_VIOLATION The time could not be retrieved due to an authentication failure.
126
127 **/
128 EFI_STATUS
129 EFIAPI
130 LibGetTime (
131 OUT EFI_TIME *Time,
132 OUT EFI_TIME_CAPABILITIES *Capabilities
133 )
134 {
135 EFI_STATUS Status = EFI_SUCCESS;
136 UINT32 EpochSeconds;
137 INT16 TimeZone;
138 UINT8 Daylight;
139 UINTN Size;
140
141 // Initialize the hardware if not already done
142 if (!mPL031Initialized) {
143 Status = InitializePL031 ();
144 if (EFI_ERROR (Status)) {
145 goto EXIT;
146 }
147 }
148
149 // Snapshot the time as early in the function call as possible
150 // On some platforms we may have access to a battery backed up hardware clock.
151 // If such RTC exists try to use it first.
152 Status = ArmPlatformSysConfigGet (SYS_CFG_RTC, &EpochSeconds);
153 if (Status == EFI_UNSUPPORTED) {
154 // Battery backed up hardware RTC does not exist, revert to PL031
155 EpochSeconds = MmioRead32 (mPL031RtcBase + PL031_RTC_DR_DATA_REGISTER);
156 Status = EFI_SUCCESS;
157 } else if (EFI_ERROR (Status)) {
158 // Battery backed up hardware RTC exists but could not be read due to error. Abort.
159 goto EXIT;
160 } else {
161 // Battery backed up hardware RTC exists and we read the time correctly from it.
162 // Now sync the PL031 to the new time.
163 MmioWrite32 (mPL031RtcBase + PL031_RTC_LR_LOAD_REGISTER, EpochSeconds);
164 }
165
166 // Ensure Time is a valid pointer
167 if (Time == NULL) {
168 Status = EFI_INVALID_PARAMETER;
169 goto EXIT;
170 }
171
172 // Get the current time zone information from non-volatile storage
173 Size = sizeof (TimeZone);
174 Status = EfiGetVariable (
175 (CHAR16 *)mTimeZoneVariableName,
176 &gEfiCallerIdGuid,
177 NULL,
178 &Size,
179 (VOID *)&TimeZone
180 );
181
182 if (EFI_ERROR (Status)) {
183 ASSERT(Status != EFI_INVALID_PARAMETER);
184 ASSERT(Status != EFI_BUFFER_TOO_SMALL);
185
186 if (Status != EFI_NOT_FOUND)
187 goto EXIT;
188
189 // The time zone variable does not exist in non-volatile storage, so create it.
190 Time->TimeZone = EFI_UNSPECIFIED_TIMEZONE;
191 // Store it
192 Status = EfiSetVariable (
193 (CHAR16 *)mTimeZoneVariableName,
194 &gEfiCallerIdGuid,
195 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
196 Size,
197 (VOID *)&(Time->TimeZone)
198 );
199 if (EFI_ERROR (Status)) {
200 DEBUG ((
201 EFI_D_ERROR,
202 "LibGetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
203 mTimeZoneVariableName,
204 Status
205 ));
206 goto EXIT;
207 }
208 } else {
209 // Got the time zone
210 Time->TimeZone = TimeZone;
211
212 // Check TimeZone bounds: -1440 to 1440 or 2047
213 if (((Time->TimeZone < -1440) || (Time->TimeZone > 1440))
214 && (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE)) {
215 Time->TimeZone = EFI_UNSPECIFIED_TIMEZONE;
216 }
217
218 // Adjust for the correct time zone
219 if (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {
220 EpochSeconds += Time->TimeZone * SEC_PER_MIN;
221 }
222 }
223
224 // Get the current daylight information from non-volatile storage
225 Size = sizeof (Daylight);
226 Status = EfiGetVariable (
227 (CHAR16 *)mDaylightVariableName,
228 &gEfiCallerIdGuid,
229 NULL,
230 &Size,
231 (VOID *)&Daylight
232 );
233
234 if (EFI_ERROR (Status)) {
235 ASSERT(Status != EFI_INVALID_PARAMETER);
236 ASSERT(Status != EFI_BUFFER_TOO_SMALL);
237
238 if (Status != EFI_NOT_FOUND)
239 goto EXIT;
240
241 // The daylight variable does not exist in non-volatile storage, so create it.
242 Time->Daylight = 0;
243 // Store it
244 Status = EfiSetVariable (
245 (CHAR16 *)mDaylightVariableName,
246 &gEfiCallerIdGuid,
247 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
248 Size,
249 (VOID *)&(Time->Daylight)
250 );
251 if (EFI_ERROR (Status)) {
252 DEBUG ((
253 EFI_D_ERROR,
254 "LibGetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
255 mDaylightVariableName,
256 Status
257 ));
258 goto EXIT;
259 }
260 } else {
261 // Got the daylight information
262 Time->Daylight = Daylight;
263
264 // Adjust for the correct period
265 if ((Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT) {
266 // Convert to adjusted time, i.e. spring forwards one hour
267 EpochSeconds += SEC_PER_HOUR;
268 }
269 }
270
271 // Convert from internal 32-bit time to UEFI time
272 EpochToEfiTime (EpochSeconds, Time);
273
274 // Update the Capabilities info
275 if (Capabilities != NULL) {
276 // PL031 runs at frequency 1Hz
277 Capabilities->Resolution = PL031_COUNTS_PER_SECOND;
278 // Accuracy in ppm multiplied by 1,000,000, e.g. for 50ppm set 50,000,000
279 Capabilities->Accuracy = (UINT32)PcdGet32 (PcdPL031RtcPpmAccuracy);
280 // FALSE: Setting the time does not clear the values below the resolution level
281 Capabilities->SetsToZero = FALSE;
282 }
283
284 EXIT:
285 return Status;
286 }
287
288
289 /**
290 Sets the current local time and date information.
291
292 @param Time A pointer to the current time.
293
294 @retval EFI_SUCCESS The operation completed successfully.
295 @retval EFI_INVALID_PARAMETER A time field is out of range.
296 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
297
298 **/
299 EFI_STATUS
300 EFIAPI
301 LibSetTime (
302 IN EFI_TIME *Time
303 )
304 {
305 EFI_STATUS Status;
306 UINTN EpochSeconds;
307
308 // Check the input parameters are within the range specified by UEFI
309 if ((Time->Year < 1900) ||
310 (Time->Year > 9999) ||
311 (Time->Month < 1 ) ||
312 (Time->Month > 12 ) ||
313 (!IsDayValid (Time) ) ||
314 (Time->Hour > 23 ) ||
315 (Time->Minute > 59 ) ||
316 (Time->Second > 59 ) ||
317 (Time->Nanosecond > 999999999) ||
318 (!((Time->TimeZone == EFI_UNSPECIFIED_TIMEZONE) || ((Time->TimeZone >= -1440) && (Time->TimeZone <= 1440)))) ||
319 (Time->Daylight & (~(EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT)))
320 ) {
321 Status = EFI_INVALID_PARAMETER;
322 goto EXIT;
323 }
324
325 // Because the PL031 is a 32-bit counter counting seconds,
326 // the maximum time span is just over 136 years.
327 // Time is stored in Unix Epoch format, so it starts in 1970,
328 // Therefore it can not exceed the year 2106.
329 if ((Time->Year < 1970) || (Time->Year >= 2106)) {
330 Status = EFI_UNSUPPORTED;
331 goto EXIT;
332 }
333
334 // Initialize the hardware if not already done
335 if (!mPL031Initialized) {
336 Status = InitializePL031 ();
337 if (EFI_ERROR (Status)) {
338 goto EXIT;
339 }
340 }
341
342 EpochSeconds = EfiTimeToEpoch (Time);
343
344 // Adjust for the correct time zone, i.e. convert to UTC time zone
345 if (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {
346 EpochSeconds -= Time->TimeZone * SEC_PER_MIN;
347 }
348
349 // TODO: Automatic Daylight activation
350
351 // Adjust for the correct period
352 if ((Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT) {
353 // Convert to un-adjusted time, i.e. fall back one hour
354 EpochSeconds -= SEC_PER_HOUR;
355 }
356
357 // On some platforms we may have access to a battery backed up hardware clock.
358 //
359 // If such RTC exists then it must be updated first, before the PL031,
360 // to minimise any time drift. This is important because the battery backed-up
361 // RTC maintains the master time for the platform across reboots.
362 //
363 // If such RTC does not exist then the following function returns UNSUPPORTED.
364 Status = ArmPlatformSysConfigSet (SYS_CFG_RTC, EpochSeconds);
365 if ((EFI_ERROR (Status)) && (Status != EFI_UNSUPPORTED)){
366 // Any status message except SUCCESS and UNSUPPORTED indicates a hardware failure.
367 goto EXIT;
368 }
369
370
371 // Set the PL031
372 MmioWrite32 (mPL031RtcBase + PL031_RTC_LR_LOAD_REGISTER, EpochSeconds);
373
374 // The accesses to Variable Services can be very slow, because we may be writing to Flash.
375 // Do this after having set the RTC.
376
377 // Save the current time zone information into non-volatile storage
378 Status = EfiSetVariable (
379 (CHAR16 *)mTimeZoneVariableName,
380 &gEfiCallerIdGuid,
381 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
382 sizeof (Time->TimeZone),
383 (VOID *)&(Time->TimeZone)
384 );
385 if (EFI_ERROR (Status)) {
386 DEBUG ((
387 EFI_D_ERROR,
388 "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
389 mTimeZoneVariableName,
390 Status
391 ));
392 goto EXIT;
393 }
394
395 // Save the current daylight information into non-volatile storage
396 Status = EfiSetVariable (
397 (CHAR16 *)mDaylightVariableName,
398 &gEfiCallerIdGuid,
399 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
400 sizeof(Time->Daylight),
401 (VOID *)&(Time->Daylight)
402 );
403 if (EFI_ERROR (Status)) {
404 DEBUG ((
405 EFI_D_ERROR,
406 "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
407 mDaylightVariableName,
408 Status
409 ));
410 goto EXIT;
411 }
412
413 EXIT:
414 return Status;
415 }
416
417
418 /**
419 Returns the current wakeup alarm clock setting.
420
421 @param Enabled Indicates if the alarm is currently enabled or disabled.
422 @param Pending Indicates if the alarm signal is pending and requires acknowledgement.
423 @param Time The current alarm setting.
424
425 @retval EFI_SUCCESS The alarm settings were returned.
426 @retval EFI_INVALID_PARAMETER Any parameter is NULL.
427 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
428
429 **/
430 EFI_STATUS
431 EFIAPI
432 LibGetWakeupTime (
433 OUT BOOLEAN *Enabled,
434 OUT BOOLEAN *Pending,
435 OUT EFI_TIME *Time
436 )
437 {
438 // Not a required feature
439 return EFI_UNSUPPORTED;
440 }
441
442
443 /**
444 Sets the system wakeup alarm clock time.
445
446 @param Enabled Enable or disable the wakeup alarm.
447 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
448
449 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If
450 Enable is FALSE, then the wakeup alarm was disabled.
451 @retval EFI_INVALID_PARAMETER A time field is out of range.
452 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
453 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
454
455 **/
456 EFI_STATUS
457 EFIAPI
458 LibSetWakeupTime (
459 IN BOOLEAN Enabled,
460 OUT EFI_TIME *Time
461 )
462 {
463 // Not a required feature
464 return EFI_UNSUPPORTED;
465 }
466
467 /**
468 Fixup internal data so that EFI can be call in virtual mode.
469 Call the passed in Child Notify event and convert any pointers in
470 lib to virtual mode.
471
472 @param[in] Event The Event that is being processed
473 @param[in] Context Event Context
474 **/
475 VOID
476 EFIAPI
477 LibRtcVirtualNotifyEvent (
478 IN EFI_EVENT Event,
479 IN VOID *Context
480 )
481 {
482 //
483 // Only needed if you are going to support the OS calling RTC functions in virtual mode.
484 // You will need to call EfiConvertPointer (). To convert any stored physical addresses
485 // to virtual address. After the OS transitions to calling in virtual mode, all future
486 // runtime calls will be made in virtual mode.
487 //
488 EfiConvertPointer (0x0, (VOID**)&mPL031RtcBase);
489 return;
490 }
491
492 /**
493 This is the declaration of an EFI image entry point. This can be the entry point to an application
494 written to this specification, an EFI boot service driver, or an EFI runtime driver.
495
496 @param ImageHandle Handle that identifies the loaded image.
497 @param SystemTable System Table for this image.
498
499 @retval EFI_SUCCESS The operation completed successfully.
500
501 **/
502 EFI_STATUS
503 EFIAPI
504 LibRtcInitialize (
505 IN EFI_HANDLE ImageHandle,
506 IN EFI_SYSTEM_TABLE *SystemTable
507 )
508 {
509 EFI_STATUS Status;
510 EFI_HANDLE Handle;
511
512 // Initialize RTC Base Address
513 mPL031RtcBase = PcdGet32 (PcdPL031RtcBase);
514
515 // Declare the controller as EFI_MEMORY_RUNTIME
516 Status = gDS->AddMemorySpace (
517 EfiGcdMemoryTypeMemoryMappedIo,
518 mPL031RtcBase, SIZE_4KB,
519 EFI_MEMORY_UC | EFI_MEMORY_RUNTIME
520 );
521 if (EFI_ERROR (Status)) {
522 return Status;
523 }
524
525 Status = gDS->SetMemorySpaceAttributes (mPL031RtcBase, SIZE_4KB, EFI_MEMORY_UC | EFI_MEMORY_RUNTIME);
526 if (EFI_ERROR (Status)) {
527 return Status;
528 }
529
530 // Install the protocol
531 Handle = NULL;
532 Status = gBS->InstallMultipleProtocolInterfaces (
533 &Handle,
534 &gEfiRealTimeClockArchProtocolGuid, NULL,
535 NULL
536 );
537 ASSERT_EFI_ERROR (Status);
538
539 //
540 // Register for the virtual address change event
541 //
542 Status = gBS->CreateEventEx (
543 EVT_NOTIFY_SIGNAL,
544 TPL_NOTIFY,
545 LibRtcVirtualNotifyEvent,
546 NULL,
547 &gEfiEventVirtualAddressChangeGuid,
548 &mRtcVirtualAddrChangeEvent
549 );
550 ASSERT_EFI_ERROR (Status);
551
552 return Status;
553 }