]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.c
f1eb0deb324931f4f7b3c79905e49226d0d208ce
[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 BOOLEAN mPL031Initialized = FALSE;
44 STATIC EFI_EVENT mRtcVirtualAddrChangeEvent;
45 STATIC UINTN mPL031RtcBase;
46
47 EFI_STATUS
48 IdentifyPL031 (
49 VOID
50 )
51 {
52 EFI_STATUS Status;
53
54 // Check if this is a PrimeCell Peripheral
55 if ( (MmioRead8 (mPL031RtcBase + PL031_RTC_PCELL_ID0) != 0x0D)
56 || (MmioRead8 (mPL031RtcBase + PL031_RTC_PCELL_ID1) != 0xF0)
57 || (MmioRead8 (mPL031RtcBase + PL031_RTC_PCELL_ID2) != 0x05)
58 || (MmioRead8 (mPL031RtcBase + PL031_RTC_PCELL_ID3) != 0xB1)) {
59 Status = EFI_NOT_FOUND;
60 goto EXIT;
61 }
62
63 // Check if this PrimeCell Peripheral is the PL031 Real Time Clock
64 if ( (MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID0) != 0x31)
65 || (MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID1) != 0x10)
66 || ((MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID2) & 0xF) != 0x04)
67 || (MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID3) != 0x00)) {
68 Status = EFI_NOT_FOUND;
69 goto EXIT;
70 }
71
72 Status = EFI_SUCCESS;
73
74 EXIT:
75 return Status;
76 }
77
78 EFI_STATUS
79 InitializePL031 (
80 VOID
81 )
82 {
83 EFI_STATUS Status;
84
85 // Prepare the hardware
86 Status = IdentifyPL031();
87 if (EFI_ERROR (Status)) {
88 goto EXIT;
89 }
90
91 // Ensure interrupts are masked. We do not want RTC interrupts in UEFI
92 if ((MmioRead32 (mPL031RtcBase + PL031_RTC_IMSC_IRQ_MASK_SET_CLEAR_REGISTER) & PL031_SET_IRQ_MASK) != PL031_SET_IRQ_MASK) {
93 MmioOr32 (mPL031RtcBase + PL031_RTC_IMSC_IRQ_MASK_SET_CLEAR_REGISTER, PL031_SET_IRQ_MASK);
94 }
95
96 // Clear any existing interrupts
97 if ((MmioRead32 (mPL031RtcBase + PL031_RTC_RIS_RAW_IRQ_STATUS_REGISTER) & PL031_IRQ_TRIGGERED) == PL031_IRQ_TRIGGERED) {
98 MmioOr32 (mPL031RtcBase + PL031_RTC_ICR_IRQ_CLEAR_REGISTER, PL031_CLEAR_IRQ);
99 }
100
101 // Start the clock counter
102 if ((MmioRead32 (mPL031RtcBase + PL031_RTC_CR_CONTROL_REGISTER) & PL031_RTC_ENABLED) != PL031_RTC_ENABLED) {
103 MmioOr32 (mPL031RtcBase + PL031_RTC_CR_CONTROL_REGISTER, PL031_RTC_ENABLED);
104 }
105
106 mPL031Initialized = TRUE;
107
108 EXIT:
109 return Status;
110 }
111
112 /**
113 Returns the current time and date information, and the time-keeping capabilities
114 of the hardware platform.
115
116 @param Time A pointer to storage to receive a snapshot of the current time.
117 @param Capabilities An optional pointer to a buffer to receive the real time clock
118 device's capabilities.
119
120 @retval EFI_SUCCESS The operation completed successfully.
121 @retval EFI_INVALID_PARAMETER Time is NULL.
122 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
123 @retval EFI_SECURITY_VIOLATION The time could not be retrieved due to an authentication failure.
124
125 **/
126 EFI_STATUS
127 EFIAPI
128 LibGetTime (
129 OUT EFI_TIME *Time,
130 OUT EFI_TIME_CAPABILITIES *Capabilities
131 )
132 {
133 EFI_STATUS Status = EFI_SUCCESS;
134 UINT32 EpochSeconds;
135
136 // Initialize the hardware if not already done
137 if (!mPL031Initialized) {
138 Status = InitializePL031 ();
139 if (EFI_ERROR (Status)) {
140 return Status;
141 }
142 }
143
144 // Snapshot the time as early in the function call as possible
145 // On some platforms we may have access to a battery backed up hardware clock.
146 // If such RTC exists try to use it first.
147 Status = ArmPlatformSysConfigGet (SYS_CFG_RTC, &EpochSeconds);
148 if (Status == EFI_UNSUPPORTED) {
149 // Battery backed up hardware RTC does not exist, revert to PL031
150 EpochSeconds = MmioRead32 (mPL031RtcBase + PL031_RTC_DR_DATA_REGISTER);
151 Status = EFI_SUCCESS;
152 } else if (EFI_ERROR (Status)) {
153 // Battery backed up hardware RTC exists but could not be read due to error. Abort.
154 return Status;
155 } else {
156 // Battery backed up hardware RTC exists and we read the time correctly from it.
157 // Now sync the PL031 to the new time.
158 MmioWrite32 (mPL031RtcBase + PL031_RTC_LR_LOAD_REGISTER, EpochSeconds);
159 }
160
161 // Ensure Time is a valid pointer
162 if (Time == NULL) {
163 return EFI_INVALID_PARAMETER;
164 }
165
166 // Adjust for the correct time zone
167 if (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {
168 EpochSeconds += Time->TimeZone * SEC_PER_MIN;
169 }
170
171 // Adjust for the correct period
172 if ((Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT) {
173 // Convert to adjusted time, i.e. spring forwards one hour
174 EpochSeconds += SEC_PER_HOUR;
175 }
176
177 // Convert from internal 32-bit time to UEFI time
178 EpochToEfiTime (EpochSeconds, Time);
179
180 // Update the Capabilities info
181 if (Capabilities != NULL) {
182 // PL031 runs at frequency 1Hz
183 Capabilities->Resolution = PL031_COUNTS_PER_SECOND;
184 // Accuracy in ppm multiplied by 1,000,000, e.g. for 50ppm set 50,000,000
185 Capabilities->Accuracy = (UINT32)PcdGet32 (PcdPL031RtcPpmAccuracy);
186 // FALSE: Setting the time does not clear the values below the resolution level
187 Capabilities->SetsToZero = FALSE;
188 }
189
190 return EFI_SUCCESS;
191 }
192
193
194 /**
195 Sets the current local time and date information.
196
197 @param Time A pointer to the current time.
198
199 @retval EFI_SUCCESS The operation completed successfully.
200 @retval EFI_INVALID_PARAMETER A time field is out of range.
201 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
202
203 **/
204 EFI_STATUS
205 EFIAPI
206 LibSetTime (
207 IN EFI_TIME *Time
208 )
209 {
210 EFI_STATUS Status;
211 UINTN EpochSeconds;
212
213 // Because the PL031 is a 32-bit counter counting seconds,
214 // the maximum time span is just over 136 years.
215 // Time is stored in Unix Epoch format, so it starts in 1970,
216 // Therefore it can not exceed the year 2106.
217 if ((Time->Year < 1970) || (Time->Year >= 2106)) {
218 return EFI_UNSUPPORTED;
219 }
220
221 // Initialize the hardware if not already done
222 if (!mPL031Initialized) {
223 Status = InitializePL031 ();
224 if (EFI_ERROR (Status)) {
225 return Status;
226 }
227 }
228
229 EpochSeconds = EfiTimeToEpoch (Time);
230
231 // Adjust for the correct time zone, i.e. convert to UTC time zone
232 if (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {
233 EpochSeconds -= Time->TimeZone * SEC_PER_MIN;
234 }
235
236 // Adjust for the correct period
237 if ((Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT) {
238 // Convert to un-adjusted time, i.e. fall back one hour
239 EpochSeconds -= SEC_PER_HOUR;
240 }
241
242 // On some platforms we may have access to a battery backed up hardware clock.
243 //
244 // If such RTC exists then it must be updated first, before the PL031,
245 // to minimise any time drift. This is important because the battery backed-up
246 // RTC maintains the master time for the platform across reboots.
247 //
248 // If such RTC does not exist then the following function returns UNSUPPORTED.
249 Status = ArmPlatformSysConfigSet (SYS_CFG_RTC, EpochSeconds);
250 if ((EFI_ERROR (Status)) && (Status != EFI_UNSUPPORTED)){
251 // Any status message except SUCCESS and UNSUPPORTED indicates a hardware failure.
252 return Status;
253 }
254
255 // Set the PL031
256 MmioWrite32 (mPL031RtcBase + PL031_RTC_LR_LOAD_REGISTER, EpochSeconds);
257
258 return EFI_SUCCESS;
259 }
260
261
262 /**
263 Returns the current wakeup alarm clock setting.
264
265 @param Enabled Indicates if the alarm is currently enabled or disabled.
266 @param Pending Indicates if the alarm signal is pending and requires acknowledgement.
267 @param Time The current alarm setting.
268
269 @retval EFI_SUCCESS The alarm settings were returned.
270 @retval EFI_INVALID_PARAMETER Any parameter is NULL.
271 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
272
273 **/
274 EFI_STATUS
275 EFIAPI
276 LibGetWakeupTime (
277 OUT BOOLEAN *Enabled,
278 OUT BOOLEAN *Pending,
279 OUT EFI_TIME *Time
280 )
281 {
282 // Not a required feature
283 return EFI_UNSUPPORTED;
284 }
285
286
287 /**
288 Sets the system wakeup alarm clock time.
289
290 @param Enabled Enable or disable the wakeup alarm.
291 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
292
293 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If
294 Enable is FALSE, then the wakeup alarm was disabled.
295 @retval EFI_INVALID_PARAMETER A time field is out of range.
296 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
297 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
298
299 **/
300 EFI_STATUS
301 EFIAPI
302 LibSetWakeupTime (
303 IN BOOLEAN Enabled,
304 OUT EFI_TIME *Time
305 )
306 {
307 // Not a required feature
308 return EFI_UNSUPPORTED;
309 }
310
311 /**
312 Fixup internal data so that EFI can be call in virtual mode.
313 Call the passed in Child Notify event and convert any pointers in
314 lib to virtual mode.
315
316 @param[in] Event The Event that is being processed
317 @param[in] Context Event Context
318 **/
319 VOID
320 EFIAPI
321 LibRtcVirtualNotifyEvent (
322 IN EFI_EVENT Event,
323 IN VOID *Context
324 )
325 {
326 //
327 // Only needed if you are going to support the OS calling RTC functions in virtual mode.
328 // You will need to call EfiConvertPointer (). To convert any stored physical addresses
329 // to virtual address. After the OS transitions to calling in virtual mode, all future
330 // runtime calls will be made in virtual mode.
331 //
332 EfiConvertPointer (0x0, (VOID**)&mPL031RtcBase);
333 return;
334 }
335
336 /**
337 This is the declaration of an EFI image entry point. This can be the entry point to an application
338 written to this specification, an EFI boot service driver, or an EFI runtime driver.
339
340 @param ImageHandle Handle that identifies the loaded image.
341 @param SystemTable System Table for this image.
342
343 @retval EFI_SUCCESS The operation completed successfully.
344
345 **/
346 EFI_STATUS
347 EFIAPI
348 LibRtcInitialize (
349 IN EFI_HANDLE ImageHandle,
350 IN EFI_SYSTEM_TABLE *SystemTable
351 )
352 {
353 EFI_STATUS Status;
354 EFI_HANDLE Handle;
355
356 // Initialize RTC Base Address
357 mPL031RtcBase = PcdGet32 (PcdPL031RtcBase);
358
359 // Declare the controller as EFI_MEMORY_RUNTIME
360 Status = gDS->AddMemorySpace (
361 EfiGcdMemoryTypeMemoryMappedIo,
362 mPL031RtcBase, SIZE_4KB,
363 EFI_MEMORY_UC | EFI_MEMORY_RUNTIME
364 );
365 if (EFI_ERROR (Status)) {
366 return Status;
367 }
368
369 Status = gDS->SetMemorySpaceAttributes (mPL031RtcBase, SIZE_4KB, EFI_MEMORY_UC | EFI_MEMORY_RUNTIME);
370 if (EFI_ERROR (Status)) {
371 return Status;
372 }
373
374 // Install the protocol
375 Handle = NULL;
376 Status = gBS->InstallMultipleProtocolInterfaces (
377 &Handle,
378 &gEfiRealTimeClockArchProtocolGuid, NULL,
379 NULL
380 );
381 ASSERT_EFI_ERROR (Status);
382
383 //
384 // Register for the virtual address change event
385 //
386 Status = gBS->CreateEventEx (
387 EVT_NOTIFY_SIGNAL,
388 TPL_NOTIFY,
389 LibRtcVirtualNotifyEvent,
390 NULL,
391 &gEfiEventVirtualAddressChangeGuid,
392 &mRtcVirtualAddrChangeEvent
393 );
394 ASSERT_EFI_ERROR (Status);
395
396 return Status;
397 }