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