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