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