2 Implement EFI RealTimeClock runtime services via RTC Lib.
4 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
5 Copyright (c) 2011 - 2014, ARM Ltd. All rights reserved.<BR>
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
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.
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>
31 #include <Protocol/RealTimeClock.h>
33 #include <Guid/GlobalVariable.h>
34 #include <Guid/EventGroup.h>
36 #include <Drivers/PL031RealTimeClock.h>
38 #include <Library/TimeBaseLib.h>
40 STATIC BOOLEAN mPL031Initialized
= FALSE
;
41 STATIC EFI_EVENT mRtcVirtualAddrChangeEvent
;
42 STATIC UINTN mPL031RtcBase
;
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
;
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
;
82 // Prepare the hardware
83 Status
= IdentifyPL031();
84 if (EFI_ERROR (Status
)) {
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
);
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
);
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
);
103 mPL031Initialized
= TRUE
;
110 Returns the current time and date information, and the time-keeping capabilities
111 of the hardware platform.
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.
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.
127 OUT EFI_TIME_CAPABILITIES
*Capabilities
130 EFI_STATUS Status
= EFI_SUCCESS
;
133 // Ensure Time is a valid pointer
135 return EFI_INVALID_PARAMETER
;
138 // Initialize the hardware if not already done
139 if (!mPL031Initialized
) {
140 Status
= InitializePL031 ();
141 if (EFI_ERROR (Status
)) {
146 EpochSeconds
= MmioRead32 (mPL031RtcBase
+ PL031_RTC_DR_DATA_REGISTER
);
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
;
157 // Convert from internal 32-bit time to UEFI time
158 EpochToEfiTime (EpochSeconds
, Time
);
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
;
175 Sets the current local time and date information.
177 @param Time A pointer to the current time.
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.
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
;
201 // Initialize the hardware if not already done
202 if (!mPL031Initialized
) {
203 Status
= InitializePL031 ();
204 if (EFI_ERROR (Status
)) {
209 EpochSeconds
= EfiTimeToEpoch (Time
);
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
;
221 MmioWrite32 (mPL031RtcBase
+ PL031_RTC_LR_LOAD_REGISTER
, EpochSeconds
);
228 Returns the current wakeup alarm clock setting.
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.
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.
242 OUT BOOLEAN
*Enabled
,
243 OUT BOOLEAN
*Pending
,
247 // Not a required feature
248 return EFI_UNSUPPORTED
;
253 Sets the system wakeup alarm clock time.
255 @param Enabled Enable or disable the wakeup alarm.
256 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
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.
272 // Not a required feature
273 return EFI_UNSUPPORTED
;
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
281 @param[in] Event The Event that is being processed
282 @param[in] Context Event Context
286 LibRtcVirtualNotifyEvent (
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.
297 EfiConvertPointer (0x0, (VOID
**)&mPL031RtcBase
);
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.
305 @param ImageHandle Handle that identifies the loaded image.
306 @param SystemTable System Table for this image.
308 @retval EFI_SUCCESS The operation completed successfully.
314 IN EFI_HANDLE ImageHandle
,
315 IN EFI_SYSTEM_TABLE
*SystemTable
321 // Initialize RTC Base Address
322 mPL031RtcBase
= PcdGet32 (PcdPL031RtcBase
);
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
330 if (EFI_ERROR (Status
)) {
334 Status
= gDS
->SetMemorySpaceAttributes (mPL031RtcBase
, SIZE_4KB
, EFI_MEMORY_UC
| EFI_MEMORY_RUNTIME
);
335 if (EFI_ERROR (Status
)) {
339 // Install the protocol
341 Status
= gBS
->InstallMultipleProtocolInterfaces (
343 &gEfiRealTimeClockArchProtocolGuid
, NULL
,
346 ASSERT_EFI_ERROR (Status
);
349 // Register for the virtual address change event
351 Status
= gBS
->CreateEventEx (
354 LibRtcVirtualNotifyEvent
,
356 &gEfiEventVirtualAddressChangeGuid
,
357 &mRtcVirtualAddrChangeEvent
359 ASSERT_EFI_ERROR (Status
);