]>
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 |
259ea52b AB |
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 | |
43 | EFI_STATUS Status;\r | |
44 | \r | |
45 | // Check if this is a PrimeCell Peripheral\r | |
18ee5b6d OM |
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 | |
0f4386e7 | 50 | Status = EFI_NOT_FOUND;\r |
51 | goto EXIT;\r | |
52 | }\r | |
53 | \r | |
0db25ccc | 54 | // Check if this PrimeCell Peripheral is the PL031 Real Time Clock\r |
18ee5b6d OM |
55 | if ( (MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID0) != 0x31)\r |
56 | || (MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID1) != 0x10)\r | |
57 | || ((MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID2) & 0xF) != 0x04)\r | |
58 | || (MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID3) != 0x00)) {\r | |
0f4386e7 | 59 | Status = EFI_NOT_FOUND;\r |
60 | goto EXIT;\r | |
61 | }\r | |
62 | \r | |
63 | Status = EFI_SUCCESS;\r | |
64 | \r | |
65 | EXIT:\r | |
66 | return Status;\r | |
67 | }\r | |
68 | \r | |
69 | EFI_STATUS\r | |
70 | InitializePL031 (\r | |
71 | VOID\r | |
72 | )\r | |
73 | {\r | |
74 | EFI_STATUS Status;\r | |
75 | \r | |
76 | // Prepare the hardware\r | |
77 | Status = IdentifyPL031();\r | |
78 | if (EFI_ERROR (Status)) {\r | |
79 | goto EXIT;\r | |
80 | }\r | |
81 | \r | |
82 | // Ensure interrupts are masked. We do not want RTC interrupts in UEFI\r | |
8df52631 AG |
83 | if ((MmioRead32 (mPL031RtcBase + PL031_RTC_IMSC_IRQ_MASK_SET_CLEAR_REGISTER) & PL031_SET_IRQ_MASK) != 0) {\r |
84 | MmioWrite32 (mPL031RtcBase + PL031_RTC_IMSC_IRQ_MASK_SET_CLEAR_REGISTER, 0);\r | |
0f4386e7 | 85 | }\r |
86 | \r | |
87 | // Clear any existing interrupts\r | |
18ee5b6d OM |
88 | if ((MmioRead32 (mPL031RtcBase + PL031_RTC_RIS_RAW_IRQ_STATUS_REGISTER) & PL031_IRQ_TRIGGERED) == PL031_IRQ_TRIGGERED) {\r |
89 | MmioOr32 (mPL031RtcBase + PL031_RTC_ICR_IRQ_CLEAR_REGISTER, PL031_CLEAR_IRQ);\r | |
0f4386e7 | 90 | }\r |
91 | \r | |
92 | // Start the clock counter\r | |
18ee5b6d OM |
93 | if ((MmioRead32 (mPL031RtcBase + PL031_RTC_CR_CONTROL_REGISTER) & PL031_RTC_ENABLED) != PL031_RTC_ENABLED) {\r |
94 | MmioOr32 (mPL031RtcBase + PL031_RTC_CR_CONTROL_REGISTER, PL031_RTC_ENABLED);\r | |
0f4386e7 | 95 | }\r |
96 | \r | |
97 | mPL031Initialized = TRUE;\r | |
98 | \r | |
99 | EXIT:\r | |
100 | return Status;\r | |
101 | }\r | |
102 | \r | |
1d5d0ae9 | 103 | /**\r |
104 | Returns the current time and date information, and the time-keeping capabilities\r | |
105 | of the hardware platform.\r | |
106 | \r | |
1e43cdfd | 107 | @param Time A pointer to storage to receive a snapshot of the current time.\r |
108 | @param Capabilities An optional pointer to a buffer to receive the real time clock\r | |
109 | device's capabilities.\r | |
1d5d0ae9 | 110 | \r |
1e43cdfd | 111 | @retval EFI_SUCCESS The operation completed successfully.\r |
112 | @retval EFI_INVALID_PARAMETER Time is NULL.\r | |
113 | @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.\r | |
114 | @retval EFI_SECURITY_VIOLATION The time could not be retrieved due to an authentication failure.\r | |
1d5d0ae9 | 115 | \r |
116 | **/\r | |
117 | EFI_STATUS\r | |
118 | EFIAPI\r | |
119 | LibGetTime (\r | |
120 | OUT EFI_TIME *Time,\r | |
0f4386e7 | 121 | OUT EFI_TIME_CAPABILITIES *Capabilities\r |
1d5d0ae9 | 122 | )\r |
123 | {\r | |
28978df0 | 124 | EFI_STATUS Status;\r |
d5cd447b | 125 | UINT32 EpochSeconds;\r |
0f4386e7 | 126 | \r |
0d36a219 AB |
127 | // Ensure Time is a valid pointer\r |
128 | if (Time == NULL) {\r | |
129 | return EFI_INVALID_PARAMETER;\r | |
130 | }\r | |
131 | \r | |
0f4386e7 | 132 | // Initialize the hardware if not already done\r |
5cc45b70 | 133 | if (!mPL031Initialized) {\r |
134 | Status = InitializePL031 ();\r | |
0f4386e7 | 135 | if (EFI_ERROR (Status)) {\r |
26f9ef3a | 136 | return Status;\r |
0f4386e7 | 137 | }\r |
138 | }\r | |
139 | \r | |
0d36a219 | 140 | EpochSeconds = MmioRead32 (mPL031RtcBase + PL031_RTC_DR_DATA_REGISTER);\r |
0f4386e7 | 141 | \r |
26f9ef3a | 142 | // Adjust for the correct time zone\r |
207bc6a3 | 143 | // The timezone setting also reflects the DST setting of the clock\r |
26f9ef3a AB |
144 | if (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {\r |
145 | EpochSeconds += Time->TimeZone * SEC_PER_MIN;\r | |
207bc6a3 | 146 | } else if ((Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT) {\r |
26f9ef3a AB |
147 | // Convert to adjusted time, i.e. spring forwards one hour\r |
148 | EpochSeconds += SEC_PER_HOUR;\r | |
0f4386e7 | 149 | }\r |
150 | \r | |
151 | // Convert from internal 32-bit time to UEFI time\r | |
5cc45b70 | 152 | EpochToEfiTime (EpochSeconds, Time);\r |
0f4386e7 | 153 | \r |
154 | // Update the Capabilities info\r | |
5cc45b70 | 155 | if (Capabilities != NULL) {\r |
156 | // PL031 runs at frequency 1Hz\r | |
157 | Capabilities->Resolution = PL031_COUNTS_PER_SECOND;\r | |
158 | // Accuracy in ppm multiplied by 1,000,000, e.g. for 50ppm set 50,000,000\r | |
159 | Capabilities->Accuracy = (UINT32)PcdGet32 (PcdPL031RtcPpmAccuracy);\r | |
160 | // FALSE: Setting the time does not clear the values below the resolution level\r | |
161 | Capabilities->SetsToZero = FALSE;\r | |
0f4386e7 | 162 | }\r |
163 | \r | |
26f9ef3a | 164 | return EFI_SUCCESS;\r |
1d5d0ae9 | 165 | }\r |
166 | \r | |
167 | \r | |
168 | /**\r | |
169 | Sets the current local time and date information.\r | |
170 | \r | |
171 | @param Time A pointer to the current time.\r | |
172 | \r | |
173 | @retval EFI_SUCCESS The operation completed successfully.\r | |
174 | @retval EFI_INVALID_PARAMETER A time field is out of range.\r | |
175 | @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.\r | |
176 | \r | |
177 | **/\r | |
178 | EFI_STATUS\r | |
179 | EFIAPI\r | |
180 | LibSetTime (\r | |
0f4386e7 | 181 | IN EFI_TIME *Time\r |
1d5d0ae9 | 182 | )\r |
183 | {\r | |
0f4386e7 | 184 | EFI_STATUS Status;\r |
7c974d6b | 185 | UINT32 EpochSeconds;\r |
0f4386e7 | 186 | \r |
cc104d19 OM |
187 | // Because the PL031 is a 32-bit counter counting seconds,\r |
188 | // the maximum time span is just over 136 years.\r | |
189 | // Time is stored in Unix Epoch format, so it starts in 1970,\r | |
190 | // Therefore it can not exceed the year 2106.\r | |
191 | if ((Time->Year < 1970) || (Time->Year >= 2106)) {\r | |
26f9ef3a | 192 | return EFI_UNSUPPORTED;\r |
cc104d19 OM |
193 | }\r |
194 | \r | |
0f4386e7 | 195 | // Initialize the hardware if not already done\r |
5cc45b70 | 196 | if (!mPL031Initialized) {\r |
197 | Status = InitializePL031 ();\r | |
0f4386e7 | 198 | if (EFI_ERROR (Status)) {\r |
26f9ef3a | 199 | return Status;\r |
0f4386e7 | 200 | }\r |
201 | }\r | |
202 | \r | |
3af6c521 | 203 | EpochSeconds = (UINT32)EfiTimeToEpoch (Time);\r |
0f4386e7 | 204 | \r |
205 | // Adjust for the correct time zone, i.e. convert to UTC time zone\r | |
207bc6a3 | 206 | // The timezone setting also reflects the DST setting of the clock\r |
5cc45b70 | 207 | if (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {\r |
0f4386e7 | 208 | EpochSeconds -= Time->TimeZone * SEC_PER_MIN;\r |
207bc6a3 | 209 | } else if ((Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT) {\r |
0f4386e7 | 210 | // Convert to un-adjusted time, i.e. fall back one hour\r |
211 | EpochSeconds -= SEC_PER_HOUR;\r | |
212 | }\r | |
213 | \r | |
0f4386e7 | 214 | // Set the PL031\r |
18ee5b6d | 215 | MmioWrite32 (mPL031RtcBase + PL031_RTC_LR_LOAD_REGISTER, EpochSeconds);\r |
0f4386e7 | 216 | \r |
26f9ef3a | 217 | return EFI_SUCCESS;\r |
1d5d0ae9 | 218 | }\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 | |
233 | EFI_STATUS\r | |
234 | EFIAPI\r | |
235 | LibGetWakeupTime (\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 | /**\r | |
247 | Sets the system wakeup alarm clock time.\r | |
248 | \r | |
249 | @param Enabled Enable or disable the wakeup alarm.\r | |
250 | @param Time If Enable is TRUE, the time to set the wakeup alarm for.\r | |
251 | \r | |
252 | @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If\r | |
253 | Enable is FALSE, then the wakeup alarm was disabled.\r | |
254 | @retval EFI_INVALID_PARAMETER A time field is out of range.\r | |
255 | @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.\r | |
256 | @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.\r | |
257 | \r | |
258 | **/\r | |
259 | EFI_STATUS\r | |
260 | EFIAPI\r | |
261 | LibSetWakeupTime (\r | |
262 | IN BOOLEAN Enabled,\r | |
263 | OUT EFI_TIME *Time\r | |
264 | )\r | |
265 | {\r | |
266 | // Not a required feature\r | |
267 | return EFI_UNSUPPORTED;\r | |
268 | }\r | |
269 | \r | |
18ee5b6d OM |
270 | /**\r |
271 | Fixup internal data so that EFI can be call in virtual mode.\r | |
272 | Call the passed in Child Notify event and convert any pointers in\r | |
273 | lib to virtual mode.\r | |
1d5d0ae9 | 274 | \r |
18ee5b6d OM |
275 | @param[in] Event The Event that is being processed\r |
276 | @param[in] Context Event Context\r | |
277 | **/\r | |
278 | VOID\r | |
279 | EFIAPI\r | |
280 | LibRtcVirtualNotifyEvent (\r | |
281 | IN EFI_EVENT Event,\r | |
282 | IN VOID *Context\r | |
283 | )\r | |
284 | {\r | |
285 | //\r | |
286 | // Only needed if you are going to support the OS calling RTC functions in virtual mode.\r | |
287 | // You will need to call EfiConvertPointer (). To convert any stored physical addresses\r | |
288 | // to virtual address. After the OS transitions to calling in virtual mode, all future\r | |
289 | // runtime calls will be made in virtual mode.\r | |
290 | //\r | |
291 | EfiConvertPointer (0x0, (VOID**)&mPL031RtcBase);\r | |
292 | return;\r | |
293 | }\r | |
1d5d0ae9 | 294 | \r |
295 | /**\r | |
296 | This is the declaration of an EFI image entry point. This can be the entry point to an application\r | |
297 | written to this specification, an EFI boot service driver, or an EFI runtime driver.\r | |
298 | \r | |
299 | @param ImageHandle Handle that identifies the loaded image.\r | |
300 | @param SystemTable System Table for this image.\r | |
301 | \r | |
302 | @retval EFI_SUCCESS The operation completed successfully.\r | |
303 | \r | |
304 | **/\r | |
305 | EFI_STATUS\r | |
306 | EFIAPI\r | |
307 | LibRtcInitialize (\r | |
308 | IN EFI_HANDLE ImageHandle,\r | |
309 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
310 | )\r | |
311 | {\r | |
0f4386e7 | 312 | EFI_STATUS Status;\r |
313 | EFI_HANDLE Handle;\r | |
314 | \r | |
18ee5b6d OM |
315 | // Initialize RTC Base Address\r |
316 | mPL031RtcBase = PcdGet32 (PcdPL031RtcBase);\r | |
317 | \r | |
318 | // Declare the controller as EFI_MEMORY_RUNTIME\r | |
319 | Status = gDS->AddMemorySpace (\r | |
320 | EfiGcdMemoryTypeMemoryMappedIo,\r | |
321 | mPL031RtcBase, 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 | |
0f4386e7 | 333 | // Install the protocol\r |
334 | Handle = NULL;\r | |
335 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
336 | &Handle,\r | |
337 | &gEfiRealTimeClockArchProtocolGuid, NULL,\r | |
338 | NULL\r | |
5cc45b70 | 339 | );\r |
18ee5b6d | 340 | ASSERT_EFI_ERROR (Status);\r |
0f4386e7 | 341 | \r |
1d5d0ae9 | 342 | //\r |
18ee5b6d | 343 | // Register for the virtual address change event\r |
1d5d0ae9 | 344 | //\r |
18ee5b6d OM |
345 | Status = gBS->CreateEventEx (\r |
346 | EVT_NOTIFY_SIGNAL,\r | |
347 | TPL_NOTIFY,\r | |
348 | LibRtcVirtualNotifyEvent,\r | |
349 | NULL,\r | |
350 | &gEfiEventVirtualAddressChangeGuid,\r | |
351 | &mRtcVirtualAddrChangeEvent\r | |
352 | );\r | |
353 | ASSERT_EFI_ERROR (Status);\r | |
354 | \r | |
355 | return Status;\r | |
1d5d0ae9 | 356 | }\r |