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