]>
Commit | Line | Data |
---|---|---|
1d5d0ae9 | 1 | /** @file\r |
2 | Implement EFI RealTimeClock runtime services via RTC Lib.\r | |
0f4386e7 | 3 | \r |
1d5d0ae9 | 4 | Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>\r |
28978df0 | 5 | Copyright (c) 2011 - 2020, Arm Limited. All rights reserved.<BR>\r |
7c974d6b | 6 | Copyright (c) 2019, Linaro Ltd. All rights reserved.<BR>\r |
0f4386e7 | 7 | \r |
f4dfad05 | 8 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
1d5d0ae9 | 9 | \r |
10 | **/\r | |
11 | \r | |
12 | #include <PiDxe.h>\r | |
c6e51751 AB |
13 | \r |
14 | #include <Guid/EventGroup.h>\r | |
15 | #include <Guid/GlobalVariable.h>\r | |
16 | \r | |
1d5d0ae9 | 17 | #include <Library/BaseLib.h>\r |
18 | #include <Library/DebugLib.h>\r | |
c6e51751 | 19 | #include <Library/DxeServicesTableLib.h>\r |
1d5d0ae9 | 20 | #include <Library/IoLib.h>\r |
0f4386e7 | 21 | #include <Library/MemoryAllocationLib.h>\r |
5cc45b70 | 22 | #include <Library/PcdLib.h>\r |
c6e51751 AB |
23 | #include <Library/RealTimeClockLib.h>\r |
24 | #include <Library/TimeBaseLib.h>\r | |
0f4386e7 | 25 | #include <Library/UefiBootServicesTableLib.h>\r |
c6e51751 | 26 | #include <Library/UefiLib.h>\r |
0f4386e7 | 27 | #include <Library/UefiRuntimeServicesTableLib.h>\r |
18ee5b6d OM |
28 | #include <Library/UefiRuntimeLib.h>\r |
29 | \r | |
0f4386e7 | 30 | #include <Protocol/RealTimeClock.h>\r |
18ee5b6d | 31 | \r |
c6e51751 | 32 | #include "PL031RealTimeClock.h"\r |
af5fed90 | 33 | \r |
40b0b23e MK |
34 | STATIC BOOLEAN mPL031Initialized = FALSE;\r |
35 | STATIC EFI_EVENT mRtcVirtualAddrChangeEvent;\r | |
36 | STATIC UINTN mPL031RtcBase;\r | |
0f4386e7 | 37 | \r |
38 | EFI_STATUS\r | |
39 | IdentifyPL031 (\r | |
40 | VOID\r | |
41 | )\r | |
42 | {\r | |
40b0b23e | 43 | EFI_STATUS Status;\r |
0f4386e7 | 44 | \r |
45 | // Check if this is a PrimeCell Peripheral\r | |
18ee5b6d | 46 | if ( (MmioRead8 (mPL031RtcBase + PL031_RTC_PCELL_ID0) != 0x0D)\r |
40b0b23e MK |
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 | |
0f4386e7 | 51 | Status = EFI_NOT_FOUND;\r |
52 | goto EXIT;\r | |
53 | }\r | |
54 | \r | |
0db25ccc | 55 | // Check if this PrimeCell Peripheral is the PL031 Real Time Clock\r |
18ee5b6d | 56 | if ( (MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID0) != 0x31)\r |
40b0b23e MK |
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 | |
0f4386e7 | 61 | Status = EFI_NOT_FOUND;\r |
62 | goto EXIT;\r | |
63 | }\r | |
64 | \r | |
65 | Status = EFI_SUCCESS;\r | |
66 | \r | |
40b0b23e | 67 | EXIT:\r |
0f4386e7 | 68 | return Status;\r |
69 | }\r | |
70 | \r | |
71 | EFI_STATUS\r | |
72 | InitializePL031 (\r | |
73 | VOID\r | |
74 | )\r | |
75 | {\r | |
40b0b23e | 76 | EFI_STATUS Status;\r |
0f4386e7 | 77 | \r |
78 | // Prepare the hardware\r | |
40b0b23e | 79 | Status = IdentifyPL031 ();\r |
0f4386e7 | 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 | |
8df52631 AG |
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 | |
0f4386e7 | 87 | }\r |
88 | \r | |
89 | // Clear any existing interrupts\r | |
18ee5b6d OM |
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 | |
0f4386e7 | 92 | }\r |
93 | \r | |
94 | // Start the clock counter\r | |
18ee5b6d OM |
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 | |
0f4386e7 | 97 | }\r |
98 | \r | |
99 | mPL031Initialized = TRUE;\r | |
100 | \r | |
40b0b23e | 101 | EXIT:\r |
0f4386e7 | 102 | return Status;\r |
103 | }\r | |
104 | \r | |
1d5d0ae9 | 105 | /**\r |
106 | Returns the current time and date information, and the time-keeping capabilities\r | |
107 | of the hardware platform.\r | |
108 | \r | |
1e43cdfd | 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 | |
1d5d0ae9 | 112 | \r |
1e43cdfd | 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 | |
1d5d0ae9 | 117 | \r |
118 | **/\r | |
119 | EFI_STATUS\r | |
120 | EFIAPI\r | |
121 | LibGetTime (\r | |
40b0b23e MK |
122 | OUT EFI_TIME *Time,\r |
123 | OUT EFI_TIME_CAPABILITIES *Capabilities\r | |
1d5d0ae9 | 124 | )\r |
125 | {\r | |
28978df0 | 126 | EFI_STATUS Status;\r |
d5cd447b | 127 | UINT32 EpochSeconds;\r |
0f4386e7 | 128 | \r |
0d36a219 AB |
129 | // Ensure Time is a valid pointer\r |
130 | if (Time == NULL) {\r | |
131 | return EFI_INVALID_PARAMETER;\r | |
132 | }\r | |
133 | \r | |
0f4386e7 | 134 | // Initialize the hardware if not already done\r |
5cc45b70 | 135 | if (!mPL031Initialized) {\r |
136 | Status = InitializePL031 ();\r | |
0f4386e7 | 137 | if (EFI_ERROR (Status)) {\r |
26f9ef3a | 138 | return Status;\r |
0f4386e7 | 139 | }\r |
140 | }\r | |
141 | \r | |
0d36a219 | 142 | EpochSeconds = MmioRead32 (mPL031RtcBase + PL031_RTC_DR_DATA_REGISTER);\r |
0f4386e7 | 143 | \r |
26f9ef3a | 144 | // Adjust for the correct time zone\r |
207bc6a3 | 145 | // The timezone setting also reflects the DST setting of the clock\r |
26f9ef3a AB |
146 | if (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {\r |
147 | EpochSeconds += Time->TimeZone * SEC_PER_MIN;\r | |
207bc6a3 | 148 | } else if ((Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT) {\r |
26f9ef3a AB |
149 | // Convert to adjusted time, i.e. spring forwards one hour\r |
150 | EpochSeconds += SEC_PER_HOUR;\r | |
0f4386e7 | 151 | }\r |
152 | \r | |
153 | // Convert from internal 32-bit time to UEFI time\r | |
5cc45b70 | 154 | EpochToEfiTime (EpochSeconds, Time);\r |
0f4386e7 | 155 | \r |
156 | // Update the Capabilities info\r | |
5cc45b70 | 157 | if (Capabilities != NULL) {\r |
158 | // PL031 runs at frequency 1Hz\r | |
40b0b23e | 159 | Capabilities->Resolution = PL031_COUNTS_PER_SECOND;\r |
5cc45b70 | 160 | // Accuracy in ppm multiplied by 1,000,000, e.g. for 50ppm set 50,000,000\r |
40b0b23e | 161 | Capabilities->Accuracy = (UINT32)PcdGet32 (PcdPL031RtcPpmAccuracy);\r |
5cc45b70 | 162 | // FALSE: Setting the time does not clear the values below the resolution level\r |
40b0b23e | 163 | Capabilities->SetsToZero = FALSE;\r |
0f4386e7 | 164 | }\r |
165 | \r | |
26f9ef3a | 166 | return EFI_SUCCESS;\r |
1d5d0ae9 | 167 | }\r |
168 | \r | |
1d5d0ae9 | 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 due to hardware error.\r | |
177 | \r | |
178 | **/\r | |
179 | EFI_STATUS\r | |
180 | EFIAPI\r | |
181 | LibSetTime (\r | |
40b0b23e | 182 | IN EFI_TIME *Time\r |
1d5d0ae9 | 183 | )\r |
184 | {\r | |
0f4386e7 | 185 | EFI_STATUS Status;\r |
7c974d6b | 186 | UINT32 EpochSeconds;\r |
0f4386e7 | 187 | \r |
cc104d19 OM |
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 | |
26f9ef3a | 193 | return EFI_UNSUPPORTED;\r |
cc104d19 OM |
194 | }\r |
195 | \r | |
0f4386e7 | 196 | // Initialize the hardware if not already done\r |
5cc45b70 | 197 | if (!mPL031Initialized) {\r |
198 | Status = InitializePL031 ();\r | |
0f4386e7 | 199 | if (EFI_ERROR (Status)) {\r |
26f9ef3a | 200 | return Status;\r |
0f4386e7 | 201 | }\r |
202 | }\r | |
203 | \r | |
3af6c521 | 204 | EpochSeconds = (UINT32)EfiTimeToEpoch (Time);\r |
0f4386e7 | 205 | \r |
206 | // Adjust for the correct time zone, i.e. convert to UTC time zone\r | |
207bc6a3 | 207 | // The timezone setting also reflects the DST setting of the clock\r |
5cc45b70 | 208 | if (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {\r |
0f4386e7 | 209 | EpochSeconds -= Time->TimeZone * SEC_PER_MIN;\r |
207bc6a3 | 210 | } else if ((Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT) {\r |
0f4386e7 | 211 | // Convert to un-adjusted time, i.e. fall back one hour\r |
212 | EpochSeconds -= SEC_PER_HOUR;\r | |
213 | }\r | |
214 | \r | |
0f4386e7 | 215 | // Set the PL031\r |
18ee5b6d | 216 | MmioWrite32 (mPL031RtcBase + PL031_RTC_LR_LOAD_REGISTER, EpochSeconds);\r |
0f4386e7 | 217 | \r |
26f9ef3a | 218 | return EFI_SUCCESS;\r |
1d5d0ae9 | 219 | }\r |
220 | \r | |
1d5d0ae9 | 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 | |
233 | EFI_STATUS\r | |
234 | EFIAPI\r | |
235 | LibGetWakeupTime (\r | |
40b0b23e MK |
236 | OUT BOOLEAN *Enabled,\r |
237 | OUT BOOLEAN *Pending,\r | |
238 | OUT EFI_TIME *Time\r | |
1d5d0ae9 | 239 | )\r |
240 | {\r | |
241 | // Not a required feature\r | |
242 | return EFI_UNSUPPORTED;\r | |
243 | }\r | |
244 | \r | |
1d5d0ae9 | 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 | |
258 | EFI_STATUS\r | |
259 | EFIAPI\r | |
260 | LibSetWakeupTime (\r | |
40b0b23e MK |
261 | IN BOOLEAN Enabled,\r |
262 | OUT EFI_TIME *Time\r | |
1d5d0ae9 | 263 | )\r |
264 | {\r | |
265 | // Not a required feature\r | |
266 | return EFI_UNSUPPORTED;\r | |
267 | }\r | |
268 | \r | |
18ee5b6d OM |
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 | |
1d5d0ae9 | 273 | \r |
18ee5b6d OM |
274 | @param[in] Event The Event that is being processed\r |
275 | @param[in] Context Event Context\r | |
276 | **/\r | |
277 | VOID\r | |
278 | EFIAPI\r | |
279 | LibRtcVirtualNotifyEvent (\r | |
40b0b23e MK |
280 | IN EFI_EVENT Event,\r |
281 | IN VOID *Context\r | |
18ee5b6d OM |
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 | |
40b0b23e | 290 | EfiConvertPointer (0x0, (VOID **)&mPL031RtcBase);\r |
18ee5b6d OM |
291 | return;\r |
292 | }\r | |
1d5d0ae9 | 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 | |
304 | EFI_STATUS\r | |
305 | EFIAPI\r | |
306 | LibRtcInitialize (\r | |
40b0b23e MK |
307 | IN EFI_HANDLE ImageHandle,\r |
308 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
1d5d0ae9 | 309 | )\r |
310 | {\r | |
40b0b23e MK |
311 | EFI_STATUS Status;\r |
312 | EFI_HANDLE Handle;\r | |
0f4386e7 | 313 | \r |
18ee5b6d OM |
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 | |
40b0b23e MK |
320 | mPL031RtcBase,\r |
321 | SIZE_4KB,\r | |
18ee5b6d OM |
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 | |
0f4386e7 | 333 | // Install the protocol\r |
334 | Handle = NULL;\r | |
335 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
336 | &Handle,\r | |
40b0b23e MK |
337 | &gEfiRealTimeClockArchProtocolGuid,\r |
338 | NULL,\r | |
0f4386e7 | 339 | NULL\r |
40b0b23e | 340 | );\r |
18ee5b6d | 341 | ASSERT_EFI_ERROR (Status);\r |
0f4386e7 | 342 | \r |
1d5d0ae9 | 343 | //\r |
18ee5b6d | 344 | // Register for the virtual address change event\r |
1d5d0ae9 | 345 | //\r |
18ee5b6d OM |
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 | |
1d5d0ae9 | 357 | }\r |