]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.c
459dcc0a0519277f3f921d5ee170d87d895bbb70
[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 // The timezone setting also reflects the DST setting of the clock
168 if (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {
169 EpochSeconds += Time->TimeZone * SEC_PER_MIN;
170 } else if ((Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT) {
171 // Convert to adjusted time, i.e. spring forwards one hour
172 EpochSeconds += SEC_PER_HOUR;
173 }
174
175 // Convert from internal 32-bit time to UEFI time
176 EpochToEfiTime (EpochSeconds, Time);
177
178 // Update the Capabilities info
179 if (Capabilities != NULL) {
180 // PL031 runs at frequency 1Hz
181 Capabilities->Resolution = PL031_COUNTS_PER_SECOND;
182 // Accuracy in ppm multiplied by 1,000,000, e.g. for 50ppm set 50,000,000
183 Capabilities->Accuracy = (UINT32)PcdGet32 (PcdPL031RtcPpmAccuracy);
184 // FALSE: Setting the time does not clear the values below the resolution level
185 Capabilities->SetsToZero = FALSE;
186 }
187
188 return EFI_SUCCESS;
189 }
190
191
192 /**
193 Sets the current local time and date information.
194
195 @param Time A pointer to the current time.
196
197 @retval EFI_SUCCESS The operation completed successfully.
198 @retval EFI_INVALID_PARAMETER A time field is out of range.
199 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
200
201 **/
202 EFI_STATUS
203 EFIAPI
204 LibSetTime (
205 IN EFI_TIME *Time
206 )
207 {
208 EFI_STATUS Status;
209 UINTN EpochSeconds;
210
211 // Because the PL031 is a 32-bit counter counting seconds,
212 // the maximum time span is just over 136 years.
213 // Time is stored in Unix Epoch format, so it starts in 1970,
214 // Therefore it can not exceed the year 2106.
215 if ((Time->Year < 1970) || (Time->Year >= 2106)) {
216 return EFI_UNSUPPORTED;
217 }
218
219 // Initialize the hardware if not already done
220 if (!mPL031Initialized) {
221 Status = InitializePL031 ();
222 if (EFI_ERROR (Status)) {
223 return Status;
224 }
225 }
226
227 EpochSeconds = EfiTimeToEpoch (Time);
228
229 // Adjust for the correct time zone, i.e. convert to UTC time zone
230 // The timezone setting also reflects the DST setting of the clock
231 if (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {
232 EpochSeconds -= Time->TimeZone * SEC_PER_MIN;
233 } else if ((Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT) {
234 // Convert to un-adjusted time, i.e. fall back one hour
235 EpochSeconds -= SEC_PER_HOUR;
236 }
237
238 // On some platforms we may have access to a battery backed up hardware clock.
239 //
240 // If such RTC exists then it must be updated first, before the PL031,
241 // to minimise any time drift. This is important because the battery backed-up
242 // RTC maintains the master time for the platform across reboots.
243 //
244 // If such RTC does not exist then the following function returns UNSUPPORTED.
245 Status = ArmPlatformSysConfigSet (SYS_CFG_RTC, EpochSeconds);
246 if ((EFI_ERROR (Status)) && (Status != EFI_UNSUPPORTED)){
247 // Any status message except SUCCESS and UNSUPPORTED indicates a hardware failure.
248 return Status;
249 }
250
251 // Set the PL031
252 MmioWrite32 (mPL031RtcBase + PL031_RTC_LR_LOAD_REGISTER, EpochSeconds);
253
254 return EFI_SUCCESS;
255 }
256
257
258 /**
259 Returns the current wakeup alarm clock setting.
260
261 @param Enabled Indicates if the alarm is currently enabled or disabled.
262 @param Pending Indicates if the alarm signal is pending and requires acknowledgement.
263 @param Time The current alarm setting.
264
265 @retval EFI_SUCCESS The alarm settings were returned.
266 @retval EFI_INVALID_PARAMETER Any parameter is NULL.
267 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
268
269 **/
270 EFI_STATUS
271 EFIAPI
272 LibGetWakeupTime (
273 OUT BOOLEAN *Enabled,
274 OUT BOOLEAN *Pending,
275 OUT EFI_TIME *Time
276 )
277 {
278 // Not a required feature
279 return EFI_UNSUPPORTED;
280 }
281
282
283 /**
284 Sets the system wakeup alarm clock time.
285
286 @param Enabled Enable or disable the wakeup alarm.
287 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
288
289 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If
290 Enable is FALSE, then the wakeup alarm was disabled.
291 @retval EFI_INVALID_PARAMETER A time field is out of range.
292 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
293 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
294
295 **/
296 EFI_STATUS
297 EFIAPI
298 LibSetWakeupTime (
299 IN BOOLEAN Enabled,
300 OUT EFI_TIME *Time
301 )
302 {
303 // Not a required feature
304 return EFI_UNSUPPORTED;
305 }
306
307 /**
308 Fixup internal data so that EFI can be call in virtual mode.
309 Call the passed in Child Notify event and convert any pointers in
310 lib to virtual mode.
311
312 @param[in] Event The Event that is being processed
313 @param[in] Context Event Context
314 **/
315 VOID
316 EFIAPI
317 LibRtcVirtualNotifyEvent (
318 IN EFI_EVENT Event,
319 IN VOID *Context
320 )
321 {
322 //
323 // Only needed if you are going to support the OS calling RTC functions in virtual mode.
324 // You will need to call EfiConvertPointer (). To convert any stored physical addresses
325 // to virtual address. After the OS transitions to calling in virtual mode, all future
326 // runtime calls will be made in virtual mode.
327 //
328 EfiConvertPointer (0x0, (VOID**)&mPL031RtcBase);
329 return;
330 }
331
332 /**
333 This is the declaration of an EFI image entry point. This can be the entry point to an application
334 written to this specification, an EFI boot service driver, or an EFI runtime driver.
335
336 @param ImageHandle Handle that identifies the loaded image.
337 @param SystemTable System Table for this image.
338
339 @retval EFI_SUCCESS The operation completed successfully.
340
341 **/
342 EFI_STATUS
343 EFIAPI
344 LibRtcInitialize (
345 IN EFI_HANDLE ImageHandle,
346 IN EFI_SYSTEM_TABLE *SystemTable
347 )
348 {
349 EFI_STATUS Status;
350 EFI_HANDLE Handle;
351
352 // Initialize RTC Base Address
353 mPL031RtcBase = PcdGet32 (PcdPL031RtcBase);
354
355 // Declare the controller as EFI_MEMORY_RUNTIME
356 Status = gDS->AddMemorySpace (
357 EfiGcdMemoryTypeMemoryMappedIo,
358 mPL031RtcBase, SIZE_4KB,
359 EFI_MEMORY_UC | EFI_MEMORY_RUNTIME
360 );
361 if (EFI_ERROR (Status)) {
362 return Status;
363 }
364
365 Status = gDS->SetMemorySpaceAttributes (mPL031RtcBase, SIZE_4KB, EFI_MEMORY_UC | EFI_MEMORY_RUNTIME);
366 if (EFI_ERROR (Status)) {
367 return Status;
368 }
369
370 // Install the protocol
371 Handle = NULL;
372 Status = gBS->InstallMultipleProtocolInterfaces (
373 &Handle,
374 &gEfiRealTimeClockArchProtocolGuid, NULL,
375 NULL
376 );
377 ASSERT_EFI_ERROR (Status);
378
379 //
380 // Register for the virtual address change event
381 //
382 Status = gBS->CreateEventEx (
383 EVT_NOTIFY_SIGNAL,
384 TPL_NOTIFY,
385 LibRtcVirtualNotifyEvent,
386 NULL,
387 &gEfiEventVirtualAddressChangeGuid,
388 &mRtcVirtualAddrChangeEvent
389 );
390 ASSERT_EFI_ERROR (Status);
391
392 return Status;
393 }