]>
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 |
0f4386e7 | 26 | #include <Library/ArmPlatformSysConfigLib.h>\r |
18ee5b6d | 27 | #include <Library/DxeServicesTableLib.h>\r |
0f4386e7 | 28 | #include <Library/UefiBootServicesTableLib.h>\r |
29 | #include <Library/UefiRuntimeServicesTableLib.h>\r | |
18ee5b6d OM |
30 | #include <Library/UefiRuntimeLib.h>\r |
31 | \r | |
0f4386e7 | 32 | #include <Protocol/RealTimeClock.h>\r |
18ee5b6d | 33 | \r |
0f4386e7 | 34 | #include <Guid/GlobalVariable.h>\r |
18ee5b6d OM |
35 | #include <Guid/EventGroup.h>\r |
36 | \r | |
0f4386e7 | 37 | #include <Drivers/PL031RealTimeClock.h>\r |
38 | \r | |
5cc45b70 | 39 | #include <ArmPlatform.h>\r |
40 | \r | |
259ea52b AB |
41 | STATIC CONST CHAR16 mTimeZoneVariableName[] = L"PL031RtcTimeZone";\r |
42 | STATIC CONST CHAR16 mDaylightVariableName[] = L"PL031RtcDaylight";\r | |
43 | STATIC BOOLEAN mPL031Initialized = FALSE;\r | |
44 | STATIC EFI_EVENT mRtcVirtualAddrChangeEvent;\r | |
45 | STATIC UINTN mPL031RtcBase;\r | |
0f4386e7 | 46 | \r |
47 | EFI_STATUS\r | |
48 | IdentifyPL031 (\r | |
49 | VOID\r | |
50 | )\r | |
51 | {\r | |
52 | EFI_STATUS Status;\r | |
53 | \r | |
54 | // Check if this is a PrimeCell Peripheral\r | |
18ee5b6d OM |
55 | if ( (MmioRead8 (mPL031RtcBase + PL031_RTC_PCELL_ID0) != 0x0D)\r |
56 | || (MmioRead8 (mPL031RtcBase + PL031_RTC_PCELL_ID1) != 0xF0)\r | |
57 | || (MmioRead8 (mPL031RtcBase + PL031_RTC_PCELL_ID2) != 0x05)\r | |
58 | || (MmioRead8 (mPL031RtcBase + PL031_RTC_PCELL_ID3) != 0xB1)) {\r | |
0f4386e7 | 59 | Status = EFI_NOT_FOUND;\r |
60 | goto EXIT;\r | |
61 | }\r | |
62 | \r | |
0db25ccc | 63 | // Check if this PrimeCell Peripheral is the PL031 Real Time Clock\r |
18ee5b6d OM |
64 | if ( (MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID0) != 0x31)\r |
65 | || (MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID1) != 0x10)\r | |
66 | || ((MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID2) & 0xF) != 0x04)\r | |
67 | || (MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID3) != 0x00)) {\r | |
0f4386e7 | 68 | Status = EFI_NOT_FOUND;\r |
69 | goto EXIT;\r | |
70 | }\r | |
71 | \r | |
72 | Status = EFI_SUCCESS;\r | |
73 | \r | |
74 | EXIT:\r | |
75 | return Status;\r | |
76 | }\r | |
77 | \r | |
78 | EFI_STATUS\r | |
79 | InitializePL031 (\r | |
80 | VOID\r | |
81 | )\r | |
82 | {\r | |
83 | EFI_STATUS Status;\r | |
84 | \r | |
85 | // Prepare the hardware\r | |
86 | Status = IdentifyPL031();\r | |
87 | if (EFI_ERROR (Status)) {\r | |
88 | goto EXIT;\r | |
89 | }\r | |
90 | \r | |
91 | // Ensure interrupts are masked. We do not want RTC interrupts in UEFI\r | |
18ee5b6d OM |
92 | if ((MmioRead32 (mPL031RtcBase + PL031_RTC_IMSC_IRQ_MASK_SET_CLEAR_REGISTER) & PL031_SET_IRQ_MASK) != PL031_SET_IRQ_MASK) {\r |
93 | MmioOr32 (mPL031RtcBase + PL031_RTC_IMSC_IRQ_MASK_SET_CLEAR_REGISTER, PL031_SET_IRQ_MASK);\r | |
0f4386e7 | 94 | }\r |
95 | \r | |
96 | // Clear any existing interrupts\r | |
18ee5b6d OM |
97 | if ((MmioRead32 (mPL031RtcBase + PL031_RTC_RIS_RAW_IRQ_STATUS_REGISTER) & PL031_IRQ_TRIGGERED) == PL031_IRQ_TRIGGERED) {\r |
98 | MmioOr32 (mPL031RtcBase + PL031_RTC_ICR_IRQ_CLEAR_REGISTER, PL031_CLEAR_IRQ);\r | |
0f4386e7 | 99 | }\r |
100 | \r | |
101 | // Start the clock counter\r | |
18ee5b6d OM |
102 | if ((MmioRead32 (mPL031RtcBase + PL031_RTC_CR_CONTROL_REGISTER) & PL031_RTC_ENABLED) != PL031_RTC_ENABLED) {\r |
103 | MmioOr32 (mPL031RtcBase + PL031_RTC_CR_CONTROL_REGISTER, PL031_RTC_ENABLED);\r | |
0f4386e7 | 104 | }\r |
105 | \r | |
106 | mPL031Initialized = TRUE;\r | |
107 | \r | |
108 | EXIT:\r | |
109 | return Status;\r | |
110 | }\r | |
111 | \r | |
112 | /**\r | |
113 | Converts Epoch seconds (elapsed since 1970 JANUARY 01, 00:00:00 UTC) to EFI_TIME\r | |
114 | **/\r | |
115 | VOID\r | |
116 | EpochToEfiTime (\r | |
117 | IN UINTN EpochSeconds,\r | |
118 | OUT EFI_TIME *Time\r | |
119 | )\r | |
120 | {\r | |
121 | UINTN a;\r | |
122 | UINTN b;\r | |
123 | UINTN c;\r | |
124 | UINTN d;\r | |
125 | UINTN g;\r | |
126 | UINTN j;\r | |
127 | UINTN m;\r | |
128 | UINTN y;\r | |
129 | UINTN da;\r | |
130 | UINTN db;\r | |
131 | UINTN dc;\r | |
132 | UINTN dg;\r | |
133 | UINTN hh;\r | |
134 | UINTN mm;\r | |
135 | UINTN ss;\r | |
136 | UINTN J;\r | |
137 | \r | |
0f4386e7 | 138 | J = (EpochSeconds / 86400) + 2440588;\r |
139 | j = J + 32044;\r | |
140 | g = j / 146097;\r | |
141 | dg = j % 146097;\r | |
142 | c = (((dg / 36524) + 1) * 3) / 4;\r | |
143 | dc = dg - (c * 36524);\r | |
144 | b = dc / 1461;\r | |
145 | db = dc % 1461;\r | |
146 | a = (((db / 365) + 1) * 3) / 4;\r | |
147 | da = db - (a * 365);\r | |
148 | y = (g * 400) + (c * 100) + (b * 4) + a;\r | |
149 | m = (((da * 5) + 308) / 153) - 2;\r | |
150 | d = da - (((m + 4) * 153) / 5) + 122;\r | |
151 | \r | |
152 | Time->Year = y - 4800 + ((m + 2) / 12);\r | |
153 | Time->Month = ((m + 2) % 12) + 1;\r | |
154 | Time->Day = d + 1;\r | |
155 | \r | |
156 | ss = EpochSeconds % 60;\r | |
157 | a = (EpochSeconds - ss) / 60;\r | |
158 | mm = a % 60;\r | |
159 | b = (a - mm) / 60;\r | |
160 | hh = b % 24;\r | |
161 | \r | |
162 | Time->Hour = hh;\r | |
163 | Time->Minute = mm;\r | |
164 | Time->Second = ss;\r | |
165 | Time->Nanosecond = 0;\r | |
166 | \r | |
167 | }\r | |
168 | \r | |
169 | /**\r | |
170 | Converts EFI_TIME to Epoch seconds (elapsed since 1970 JANUARY 01, 00:00:00 UTC)\r | |
171 | **/\r | |
172 | UINTN\r | |
173 | EfiTimeToEpoch (\r | |
174 | IN EFI_TIME *Time\r | |
175 | )\r | |
176 | {\r | |
177 | UINTN a;\r | |
178 | UINTN y;\r | |
179 | UINTN m;\r | |
180 | UINTN JulianDate; // Absolute Julian Date representation of the supplied Time\r | |
181 | UINTN EpochDays; // Number of days elapsed since EPOCH_JULIAN_DAY\r | |
182 | UINTN EpochSeconds;\r | |
183 | \r | |
184 | a = (14 - Time->Month) / 12 ;\r | |
185 | y = Time->Year + 4800 - a;\r | |
186 | m = Time->Month + (12*a) - 3;\r | |
187 | \r | |
188 | JulianDate = Time->Day + ((153*m + 2)/5) + (365*y) + (y/4) - (y/100) + (y/400) - 32045;\r | |
189 | \r | |
cc104d19 | 190 | ASSERT (JulianDate >= EPOCH_JULIAN_DATE);\r |
0f4386e7 | 191 | EpochDays = JulianDate - EPOCH_JULIAN_DATE;\r |
192 | \r | |
193 | EpochSeconds = (EpochDays * SEC_PER_DAY) + ((UINTN)Time->Hour * SEC_PER_HOUR) + (Time->Minute * SEC_PER_MIN) + Time->Second;\r | |
194 | \r | |
195 | return EpochSeconds;\r | |
196 | }\r | |
197 | \r | |
198 | BOOLEAN\r | |
199 | IsLeapYear (\r | |
200 | IN EFI_TIME *Time\r | |
201 | )\r | |
202 | {\r | |
203 | if (Time->Year % 4 == 0) {\r | |
204 | if (Time->Year % 100 == 0) {\r | |
205 | if (Time->Year % 400 == 0) {\r | |
206 | return TRUE;\r | |
207 | } else {\r | |
208 | return FALSE;\r | |
209 | }\r | |
210 | } else {\r | |
211 | return TRUE;\r | |
212 | }\r | |
213 | } else {\r | |
214 | return FALSE;\r | |
215 | }\r | |
216 | }\r | |
217 | \r | |
218 | BOOLEAN\r | |
219 | DayValid (\r | |
220 | IN EFI_TIME *Time\r | |
221 | )\r | |
222 | {\r | |
a23eb77b | 223 | STATIC CONST INTN DayOfMonth[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };\r |
1d5d0ae9 | 224 | \r |
0f4386e7 | 225 | if (Time->Day < 1 ||\r |
226 | Time->Day > DayOfMonth[Time->Month - 1] ||\r | |
227 | (Time->Month == 2 && (!IsLeapYear (Time) && Time->Day > 28))\r | |
5cc45b70 | 228 | ) {\r |
0f4386e7 | 229 | return FALSE;\r |
230 | }\r | |
231 | \r | |
232 | return TRUE;\r | |
233 | }\r | |
1d5d0ae9 | 234 | \r |
235 | /**\r | |
236 | Returns the current time and date information, and the time-keeping capabilities\r | |
237 | of the hardware platform.\r | |
238 | \r | |
1e43cdfd | 239 | @param Time A pointer to storage to receive a snapshot of the current time.\r |
240 | @param Capabilities An optional pointer to a buffer to receive the real time clock\r | |
241 | device's capabilities.\r | |
1d5d0ae9 | 242 | \r |
1e43cdfd | 243 | @retval EFI_SUCCESS The operation completed successfully.\r |
244 | @retval EFI_INVALID_PARAMETER Time is NULL.\r | |
245 | @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.\r | |
246 | @retval EFI_SECURITY_VIOLATION The time could not be retrieved due to an authentication failure.\r | |
1d5d0ae9 | 247 | \r |
248 | **/\r | |
249 | EFI_STATUS\r | |
250 | EFIAPI\r | |
251 | LibGetTime (\r | |
252 | OUT EFI_TIME *Time,\r | |
0f4386e7 | 253 | OUT EFI_TIME_CAPABILITIES *Capabilities\r |
1d5d0ae9 | 254 | )\r |
255 | {\r | |
0f4386e7 | 256 | EFI_STATUS Status = EFI_SUCCESS;\r |
d5cd447b | 257 | UINT32 EpochSeconds;\r |
1e43cdfd | 258 | INT16 TimeZone;\r |
259 | UINT8 Daylight;\r | |
260 | UINTN Size;\r | |
0f4386e7 | 261 | \r |
262 | // Initialize the hardware if not already done\r | |
5cc45b70 | 263 | if (!mPL031Initialized) {\r |
264 | Status = InitializePL031 ();\r | |
0f4386e7 | 265 | if (EFI_ERROR (Status)) {\r |
266 | goto EXIT;\r | |
267 | }\r | |
268 | }\r | |
269 | \r | |
270 | // Snapshot the time as early in the function call as possible\r | |
271 | // On some platforms we may have access to a battery backed up hardware clock.\r | |
272 | // If such RTC exists try to use it first.\r | |
273 | Status = ArmPlatformSysConfigGet (SYS_CFG_RTC, &EpochSeconds);\r | |
274 | if (Status == EFI_UNSUPPORTED) {\r | |
275 | // Battery backed up hardware RTC does not exist, revert to PL031\r | |
18ee5b6d | 276 | EpochSeconds = MmioRead32 (mPL031RtcBase + PL031_RTC_DR_DATA_REGISTER);\r |
0f4386e7 | 277 | Status = EFI_SUCCESS;\r |
278 | } else if (EFI_ERROR (Status)) {\r | |
279 | // Battery backed up hardware RTC exists but could not be read due to error. Abort.\r | |
280 | goto EXIT;\r | |
281 | } else {\r | |
282 | // Battery backed up hardware RTC exists and we read the time correctly from it.\r | |
283 | // Now sync the PL031 to the new time.\r | |
18ee5b6d | 284 | MmioWrite32 (mPL031RtcBase + PL031_RTC_LR_LOAD_REGISTER, EpochSeconds);\r |
0f4386e7 | 285 | }\r |
286 | \r | |
287 | // Ensure Time is a valid pointer\r | |
5cc45b70 | 288 | if (Time == NULL) {\r |
0f4386e7 | 289 | Status = EFI_INVALID_PARAMETER;\r |
290 | goto EXIT;\r | |
291 | }\r | |
292 | \r | |
293 | // Get the current time zone information from non-volatile storage\r | |
1e43cdfd | 294 | Size = sizeof (TimeZone);\r |
fca117fd | 295 | Status = EfiGetVariable (\r |
1e43cdfd | 296 | (CHAR16 *)mTimeZoneVariableName,\r |
297 | &gEfiCallerIdGuid,\r | |
298 | NULL,\r | |
299 | &Size,\r | |
300 | (VOID *)&TimeZone\r | |
301 | );\r | |
302 | \r | |
303 | if (EFI_ERROR (Status)) {\r | |
304 | ASSERT(Status != EFI_INVALID_PARAMETER);\r | |
305 | ASSERT(Status != EFI_BUFFER_TOO_SMALL);\r | |
306 | \r | |
307 | if (Status != EFI_NOT_FOUND)\r | |
308 | goto EXIT;\r | |
0f4386e7 | 309 | \r |
0f4386e7 | 310 | // The time zone variable does not exist in non-volatile storage, so create it.\r |
311 | Time->TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r | |
312 | // Store it\r | |
fca117fd | 313 | Status = EfiSetVariable (\r |
1e43cdfd | 314 | (CHAR16 *)mTimeZoneVariableName,\r |
315 | &gEfiCallerIdGuid,\r | |
316 | EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r | |
317 | Size,\r | |
318 | (VOID *)&(Time->TimeZone)\r | |
319 | );\r | |
0f4386e7 | 320 | if (EFI_ERROR (Status)) {\r |
1e43cdfd | 321 | DEBUG ((\r |
322 | EFI_D_ERROR,\r | |
323 | "LibGetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",\r | |
324 | mTimeZoneVariableName,\r | |
325 | Status\r | |
326 | ));\r | |
0f4386e7 | 327 | goto EXIT;\r |
328 | }\r | |
329 | } else {\r | |
330 | // Got the time zone\r | |
1e43cdfd | 331 | Time->TimeZone = TimeZone;\r |
0f4386e7 | 332 | \r |
333 | // Check TimeZone bounds: -1440 to 1440 or 2047\r | |
5cc45b70 | 334 | if (((Time->TimeZone < -1440) || (Time->TimeZone > 1440))\r |
335 | && (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE)) {\r | |
0f4386e7 | 336 | Time->TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r |
337 | }\r | |
338 | \r | |
339 | // Adjust for the correct time zone\r | |
5cc45b70 | 340 | if (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {\r |
0f4386e7 | 341 | EpochSeconds += Time->TimeZone * SEC_PER_MIN;\r |
342 | }\r | |
343 | }\r | |
344 | \r | |
345 | // Get the current daylight information from non-volatile storage\r | |
1e43cdfd | 346 | Size = sizeof (Daylight);\r |
fca117fd | 347 | Status = EfiGetVariable (\r |
1e43cdfd | 348 | (CHAR16 *)mDaylightVariableName,\r |
349 | &gEfiCallerIdGuid,\r | |
350 | NULL,\r | |
351 | &Size,\r | |
352 | (VOID *)&Daylight\r | |
353 | );\r | |
354 | \r | |
355 | if (EFI_ERROR (Status)) {\r | |
356 | ASSERT(Status != EFI_INVALID_PARAMETER);\r | |
357 | ASSERT(Status != EFI_BUFFER_TOO_SMALL);\r | |
358 | \r | |
359 | if (Status != EFI_NOT_FOUND)\r | |
360 | goto EXIT;\r | |
0f4386e7 | 361 | \r |
0f4386e7 | 362 | // The daylight variable does not exist in non-volatile storage, so create it.\r |
363 | Time->Daylight = 0;\r | |
364 | // Store it\r | |
fca117fd | 365 | Status = EfiSetVariable (\r |
1e43cdfd | 366 | (CHAR16 *)mDaylightVariableName,\r |
367 | &gEfiCallerIdGuid,\r | |
368 | EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r | |
369 | Size,\r | |
370 | (VOID *)&(Time->Daylight)\r | |
371 | );\r | |
0f4386e7 | 372 | if (EFI_ERROR (Status)) {\r |
1e43cdfd | 373 | DEBUG ((\r |
374 | EFI_D_ERROR,\r | |
375 | "LibGetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",\r | |
376 | mDaylightVariableName,\r | |
377 | Status\r | |
378 | ));\r | |
0f4386e7 | 379 | goto EXIT;\r |
380 | }\r | |
381 | } else {\r | |
382 | // Got the daylight information\r | |
1e43cdfd | 383 | Time->Daylight = Daylight;\r |
0f4386e7 | 384 | \r |
385 | // Adjust for the correct period\r | |
5cc45b70 | 386 | if ((Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT) {\r |
0f4386e7 | 387 | // Convert to adjusted time, i.e. spring forwards one hour\r |
388 | EpochSeconds += SEC_PER_HOUR;\r | |
389 | }\r | |
390 | }\r | |
391 | \r | |
392 | // Convert from internal 32-bit time to UEFI time\r | |
5cc45b70 | 393 | EpochToEfiTime (EpochSeconds, Time);\r |
0f4386e7 | 394 | \r |
395 | // Update the Capabilities info\r | |
5cc45b70 | 396 | if (Capabilities != NULL) {\r |
397 | // PL031 runs at frequency 1Hz\r | |
398 | Capabilities->Resolution = PL031_COUNTS_PER_SECOND;\r | |
399 | // Accuracy in ppm multiplied by 1,000,000, e.g. for 50ppm set 50,000,000\r | |
400 | Capabilities->Accuracy = (UINT32)PcdGet32 (PcdPL031RtcPpmAccuracy);\r | |
401 | // FALSE: Setting the time does not clear the values below the resolution level\r | |
402 | Capabilities->SetsToZero = FALSE;\r | |
0f4386e7 | 403 | }\r |
404 | \r | |
405 | EXIT:\r | |
406 | return Status;\r | |
1d5d0ae9 | 407 | }\r |
408 | \r | |
409 | \r | |
410 | /**\r | |
411 | Sets the current local time and date information.\r | |
412 | \r | |
413 | @param Time A pointer to the current time.\r | |
414 | \r | |
415 | @retval EFI_SUCCESS The operation completed successfully.\r | |
416 | @retval EFI_INVALID_PARAMETER A time field is out of range.\r | |
417 | @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.\r | |
418 | \r | |
419 | **/\r | |
420 | EFI_STATUS\r | |
421 | EFIAPI\r | |
422 | LibSetTime (\r | |
0f4386e7 | 423 | IN EFI_TIME *Time\r |
1d5d0ae9 | 424 | )\r |
425 | {\r | |
0f4386e7 | 426 | EFI_STATUS Status;\r |
427 | UINTN EpochSeconds;\r | |
428 | \r | |
cc104d19 OM |
429 | // Check the input parameters are within the range specified by UEFI\r |
430 | if ((Time->Year < 1900) ||\r | |
431 | (Time->Year > 9999) ||\r | |
5cc45b70 | 432 | (Time->Month < 1 ) ||\r |
433 | (Time->Month > 12 ) ||\r | |
434 | (!DayValid (Time) ) ||\r | |
435 | (Time->Hour > 23 ) ||\r | |
436 | (Time->Minute > 59 ) ||\r | |
437 | (Time->Second > 59 ) ||\r | |
438 | (Time->Nanosecond > 999999999) ||\r | |
439 | (!((Time->TimeZone == EFI_UNSPECIFIED_TIMEZONE) || ((Time->TimeZone >= -1440) && (Time->TimeZone <= 1440)))) ||\r | |
440 | (Time->Daylight & (~(EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT)))\r | |
441 | ) {\r | |
0f4386e7 | 442 | Status = EFI_INVALID_PARAMETER;\r |
443 | goto EXIT;\r | |
444 | }\r | |
445 | \r | |
cc104d19 OM |
446 | // Because the PL031 is a 32-bit counter counting seconds,\r |
447 | // the maximum time span is just over 136 years.\r | |
448 | // Time is stored in Unix Epoch format, so it starts in 1970,\r | |
449 | // Therefore it can not exceed the year 2106.\r | |
450 | if ((Time->Year < 1970) || (Time->Year >= 2106)) {\r | |
451 | Status = EFI_UNSUPPORTED;\r | |
452 | goto EXIT;\r | |
453 | }\r | |
454 | \r | |
0f4386e7 | 455 | // Initialize the hardware if not already done\r |
5cc45b70 | 456 | if (!mPL031Initialized) {\r |
457 | Status = InitializePL031 ();\r | |
0f4386e7 | 458 | if (EFI_ERROR (Status)) {\r |
459 | goto EXIT;\r | |
460 | }\r | |
461 | }\r | |
462 | \r | |
5cc45b70 | 463 | EpochSeconds = EfiTimeToEpoch (Time);\r |
0f4386e7 | 464 | \r |
465 | // Adjust for the correct time zone, i.e. convert to UTC time zone\r | |
5cc45b70 | 466 | if (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {\r |
0f4386e7 | 467 | EpochSeconds -= Time->TimeZone * SEC_PER_MIN;\r |
468 | }\r | |
469 | \r | |
470 | // TODO: Automatic Daylight activation\r | |
471 | \r | |
472 | // Adjust for the correct period\r | |
5cc45b70 | 473 | if ((Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT) {\r |
0f4386e7 | 474 | // Convert to un-adjusted time, i.e. fall back one hour\r |
475 | EpochSeconds -= SEC_PER_HOUR;\r | |
476 | }\r | |
477 | \r | |
478 | // On some platforms we may have access to a battery backed up hardware clock.\r | |
1d5d0ae9 | 479 | //\r |
0f4386e7 | 480 | // If such RTC exists then it must be updated first, before the PL031,\r |
481 | // to minimise any time drift. This is important because the battery backed-up\r | |
482 | // RTC maintains the master time for the platform across reboots.\r | |
1d5d0ae9 | 483 | //\r |
0f4386e7 | 484 | // If such RTC does not exist then the following function returns UNSUPPORTED.\r |
485 | Status = ArmPlatformSysConfigSet (SYS_CFG_RTC, EpochSeconds);\r | |
486 | if ((EFI_ERROR (Status)) && (Status != EFI_UNSUPPORTED)){\r | |
487 | // Any status message except SUCCESS and UNSUPPORTED indicates a hardware failure.\r | |
488 | goto EXIT;\r | |
489 | }\r | |
490 | \r | |
491 | \r | |
492 | // Set the PL031\r | |
18ee5b6d | 493 | MmioWrite32 (mPL031RtcBase + PL031_RTC_LR_LOAD_REGISTER, EpochSeconds);\r |
0f4386e7 | 494 | \r |
495 | // The accesses to Variable Services can be very slow, because we may be writing to Flash.\r | |
496 | // Do this after having set the RTC.\r | |
497 | \r | |
498 | // Save the current time zone information into non-volatile storage\r | |
fca117fd | 499 | Status = EfiSetVariable (\r |
1e43cdfd | 500 | (CHAR16 *)mTimeZoneVariableName,\r |
501 | &gEfiCallerIdGuid,\r | |
502 | EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r | |
503 | sizeof (Time->TimeZone),\r | |
504 | (VOID *)&(Time->TimeZone)\r | |
505 | );\r | |
0f4386e7 | 506 | if (EFI_ERROR (Status)) {\r |
1e43cdfd | 507 | DEBUG ((\r |
508 | EFI_D_ERROR,\r | |
509 | "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",\r | |
510 | mTimeZoneVariableName,\r | |
511 | Status\r | |
512 | ));\r | |
0f4386e7 | 513 | goto EXIT;\r |
514 | }\r | |
515 | \r | |
516 | // Save the current daylight information into non-volatile storage\r | |
fca117fd | 517 | Status = EfiSetVariable (\r |
1e43cdfd | 518 | (CHAR16 *)mDaylightVariableName,\r |
519 | &gEfiCallerIdGuid,\r | |
520 | EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r | |
521 | sizeof(Time->Daylight),\r | |
522 | (VOID *)&(Time->Daylight)\r | |
523 | );\r | |
0f4386e7 | 524 | if (EFI_ERROR (Status)) {\r |
1e43cdfd | 525 | DEBUG ((\r |
526 | EFI_D_ERROR,\r | |
527 | "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",\r | |
528 | mDaylightVariableName,\r | |
529 | Status\r | |
530 | ));\r | |
0f4386e7 | 531 | goto EXIT;\r |
532 | }\r | |
533 | \r | |
534 | EXIT:\r | |
535 | return Status;\r | |
1d5d0ae9 | 536 | }\r |
537 | \r | |
538 | \r | |
539 | /**\r | |
540 | Returns the current wakeup alarm clock setting.\r | |
541 | \r | |
542 | @param Enabled Indicates if the alarm is currently enabled or disabled.\r | |
543 | @param Pending Indicates if the alarm signal is pending and requires acknowledgement.\r | |
544 | @param Time The current alarm setting.\r | |
545 | \r | |
546 | @retval EFI_SUCCESS The alarm settings were returned.\r | |
547 | @retval EFI_INVALID_PARAMETER Any parameter is NULL.\r | |
548 | @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.\r | |
549 | \r | |
550 | **/\r | |
551 | EFI_STATUS\r | |
552 | EFIAPI\r | |
553 | LibGetWakeupTime (\r | |
554 | OUT BOOLEAN *Enabled,\r | |
555 | OUT BOOLEAN *Pending,\r | |
556 | OUT EFI_TIME *Time\r | |
557 | )\r | |
558 | {\r | |
559 | // Not a required feature\r | |
560 | return EFI_UNSUPPORTED;\r | |
561 | }\r | |
562 | \r | |
563 | \r | |
564 | /**\r | |
565 | Sets the system wakeup alarm clock time.\r | |
566 | \r | |
567 | @param Enabled Enable or disable the wakeup alarm.\r | |
568 | @param Time If Enable is TRUE, the time to set the wakeup alarm for.\r | |
569 | \r | |
570 | @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If\r | |
571 | Enable is FALSE, then the wakeup alarm was disabled.\r | |
572 | @retval EFI_INVALID_PARAMETER A time field is out of range.\r | |
573 | @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.\r | |
574 | @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.\r | |
575 | \r | |
576 | **/\r | |
577 | EFI_STATUS\r | |
578 | EFIAPI\r | |
579 | LibSetWakeupTime (\r | |
580 | IN BOOLEAN Enabled,\r | |
581 | OUT EFI_TIME *Time\r | |
582 | )\r | |
583 | {\r | |
584 | // Not a required feature\r | |
585 | return EFI_UNSUPPORTED;\r | |
586 | }\r | |
587 | \r | |
18ee5b6d OM |
588 | /**\r |
589 | Fixup internal data so that EFI can be call in virtual mode.\r | |
590 | Call the passed in Child Notify event and convert any pointers in\r | |
591 | lib to virtual mode.\r | |
1d5d0ae9 | 592 | \r |
18ee5b6d OM |
593 | @param[in] Event The Event that is being processed\r |
594 | @param[in] Context Event Context\r | |
595 | **/\r | |
596 | VOID\r | |
597 | EFIAPI\r | |
598 | LibRtcVirtualNotifyEvent (\r | |
599 | IN EFI_EVENT Event,\r | |
600 | IN VOID *Context\r | |
601 | )\r | |
602 | {\r | |
603 | //\r | |
604 | // Only needed if you are going to support the OS calling RTC functions in virtual mode.\r | |
605 | // You will need to call EfiConvertPointer (). To convert any stored physical addresses\r | |
606 | // to virtual address. After the OS transitions to calling in virtual mode, all future\r | |
607 | // runtime calls will be made in virtual mode.\r | |
608 | //\r | |
609 | EfiConvertPointer (0x0, (VOID**)&mPL031RtcBase);\r | |
610 | return;\r | |
611 | }\r | |
1d5d0ae9 | 612 | \r |
613 | /**\r | |
614 | This is the declaration of an EFI image entry point. This can be the entry point to an application\r | |
615 | written to this specification, an EFI boot service driver, or an EFI runtime driver.\r | |
616 | \r | |
617 | @param ImageHandle Handle that identifies the loaded image.\r | |
618 | @param SystemTable System Table for this image.\r | |
619 | \r | |
620 | @retval EFI_SUCCESS The operation completed successfully.\r | |
621 | \r | |
622 | **/\r | |
623 | EFI_STATUS\r | |
624 | EFIAPI\r | |
625 | LibRtcInitialize (\r | |
626 | IN EFI_HANDLE ImageHandle,\r | |
627 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
628 | )\r | |
629 | {\r | |
0f4386e7 | 630 | EFI_STATUS Status;\r |
631 | EFI_HANDLE Handle;\r | |
632 | \r | |
18ee5b6d OM |
633 | // Initialize RTC Base Address\r |
634 | mPL031RtcBase = PcdGet32 (PcdPL031RtcBase);\r | |
635 | \r | |
636 | // Declare the controller as EFI_MEMORY_RUNTIME\r | |
637 | Status = gDS->AddMemorySpace (\r | |
638 | EfiGcdMemoryTypeMemoryMappedIo,\r | |
639 | mPL031RtcBase, SIZE_4KB,\r | |
640 | EFI_MEMORY_UC | EFI_MEMORY_RUNTIME\r | |
641 | );\r | |
642 | if (EFI_ERROR (Status)) {\r | |
643 | return Status;\r | |
644 | }\r | |
645 | \r | |
646 | Status = gDS->SetMemorySpaceAttributes (mPL031RtcBase, SIZE_4KB, EFI_MEMORY_UC | EFI_MEMORY_RUNTIME);\r | |
647 | if (EFI_ERROR (Status)) {\r | |
648 | return Status;\r | |
649 | }\r | |
650 | \r | |
0f4386e7 | 651 | // Install the protocol\r |
652 | Handle = NULL;\r | |
653 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
654 | &Handle,\r | |
655 | &gEfiRealTimeClockArchProtocolGuid, NULL,\r | |
656 | NULL\r | |
5cc45b70 | 657 | );\r |
18ee5b6d | 658 | ASSERT_EFI_ERROR (Status);\r |
0f4386e7 | 659 | \r |
1d5d0ae9 | 660 | //\r |
18ee5b6d | 661 | // Register for the virtual address change event\r |
1d5d0ae9 | 662 | //\r |
18ee5b6d OM |
663 | Status = gBS->CreateEventEx (\r |
664 | EVT_NOTIFY_SIGNAL,\r | |
665 | TPL_NOTIFY,\r | |
666 | LibRtcVirtualNotifyEvent,\r | |
667 | NULL,\r | |
668 | &gEfiEventVirtualAddressChangeGuid,\r | |
669 | &mRtcVirtualAddrChangeEvent\r | |
670 | );\r | |
671 | ASSERT_EFI_ERROR (Status);\r | |
672 | \r | |
673 | return Status;\r | |
1d5d0ae9 | 674 | }\r |