]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/PcatRealTimeClockRuntimeDxe/PcRtc.c
1, Add logo.bmp to fv file for showing it in graphic mode
[mirror_edk2.git] / MdeModulePkg / Universal / PcatRealTimeClockRuntimeDxe / PcRtc.c
CommitLineData
fb0b259e 1/** @file\r
2 RTC Architectural Protocol GUID as defined in DxeCis 0.96.\r
8cd4d17c 3\r
fb0b259e 4Copyright (c) 2006 - 2007, Intel Corporation\r
3cfb790c 5All rights reserved. This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
8cd4d17c 12\r
fb0b259e 13**/\r
8cd4d17c 14\r
15#include "PcRtc.h"\r
16\r
8d85dc31 17/**\r
18 Compare the Hour, Minute and Second of the From time and the To time.\r
19 \r
20 Only compare H/M/S in EFI_TIME and ignore other fields here.\r
21\r
22 @param From the first time\r
23 @param To the second time\r
24\r
25 @return >0 The H/M/S of the From time is later than those of To time\r
26 @return ==0 The H/M/S of the From time is same as those of To time\r
27 @return <0 The H/M/S of the From time is earlier than those of To time\r
28**/\r
8cd4d17c 29INTN\r
30CompareHMS (\r
31 IN EFI_TIME *From,\r
32 IN EFI_TIME *To\r
33 );\r
34\r
8d85dc31 35/**\r
36 To check if second date is later than first date within 24 hours.\r
37\r
38 @param From the first date\r
39 @param To the second date\r
40\r
41 @retval TRUE From is previous to To within 24 hours.\r
42 @retval FALSE From is later, or it is previous to To more than 24 hours.\r
43**/\r
8cd4d17c 44BOOLEAN\r
45IsWithinOneDay (\r
46 IN EFI_TIME *From,\r
47 IN EFI_TIME *To\r
48 );\r
49\r
8d85dc31 50/**\r
51 Read RTC content through its registers.\r
52\r
53 @param Address Address offset of RTC. It is recommended to use macros such as\r
54 RTC_ADDRESS_SECONDS.\r
55\r
56 @return The data of UINT8 type read from RTC.\r
57**/\r
8cd4d17c 58UINT8\r
59RtcRead (\r
60 IN UINT8 Address\r
61 )\r
8cd4d17c 62{\r
63 IoWrite8 (PCAT_RTC_ADDRESS_REGISTER, (UINT8) (Address | (UINT8) (IoRead8 (PCAT_RTC_ADDRESS_REGISTER) & 0x80)));\r
64 return IoRead8 (PCAT_RTC_DATA_REGISTER);\r
65}\r
66\r
8d85dc31 67/**\r
68 Write RTC through its registers.\r
69\r
70 @param Address Address offset of RTC. It is recommended to use macros such as\r
71 RTC_ADDRESS_SECONDS.\r
72 @param Data The content you want to write into RTC.\r
73\r
74**/\r
8cd4d17c 75VOID\r
76RtcWrite (\r
77 IN UINT8 Address,\r
78 IN UINT8 Data\r
79 )\r
8cd4d17c 80{\r
81 IoWrite8 (PCAT_RTC_ADDRESS_REGISTER, (UINT8) (Address | (UINT8) (IoRead8 (PCAT_RTC_ADDRESS_REGISTER) & 0x80)));\r
82 IoWrite8 (PCAT_RTC_DATA_REGISTER, Data);\r
83}\r
84\r
8d85dc31 85/**\r
86 Initialize RTC.\r
87\r
88 @param Global For global use inside this module.\r
89\r
90 @retval EFI_DEVICE_ERROR Initialization failed due to device error.\r
91 @retval EFI_SUCCESS Initialization successful.\r
92\r
93**/\r
8cd4d17c 94EFI_STATUS\r
95PcRtcInit (\r
96 IN PC_RTC_MODULE_GLOBALS *Global\r
97 )\r
8cd4d17c 98{\r
99 EFI_STATUS Status;\r
100 RTC_REGISTER_A RegisterA;\r
101 RTC_REGISTER_B RegisterB;\r
102 RTC_REGISTER_D RegisterD;\r
103 UINT8 Century;\r
104 EFI_TIME Time;\r
ec35e997
LG
105 UINTN DataSize;\r
106 UINT32 TimerVar;\r
8cd4d17c 107\r
108 //\r
109 // Acquire RTC Lock to make access to RTC atomic\r
110 //\r
812acaec 111 //Code here doesn't consider the runtime environment.\r
8cd4d17c 112 if (!EfiAtRuntime ()) {\r
8d85dc31 113 EfiAcquireLock (&Global->RtcLock);\r
8cd4d17c 114 }\r
115 //\r
116 // Initialize RTC Register\r
117 //\r
118 // Make sure Division Chain is properly configured,\r
119 // or RTC clock won't "tick" -- time won't increment\r
120 //\r
121 RegisterA.Data = RTC_INIT_REGISTER_A;\r
122 RtcWrite (RTC_ADDRESS_REGISTER_A, RegisterA.Data);\r
123\r
124 //\r
125 // Read Register B\r
126 //\r
127 RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);\r
128\r
129 //\r
130 // Clear RTC flag register\r
131 //\r
132 RtcRead (RTC_ADDRESS_REGISTER_C);\r
133\r
134 //\r
135 // Clear RTC register D\r
136 //\r
137 RegisterD.Data = RTC_INIT_REGISTER_D;\r
138 RtcWrite (RTC_ADDRESS_REGISTER_D, RegisterD.Data);\r
139\r
140 //\r
141 // Wait for up to 0.1 seconds for the RTC to be updated\r
142 //\r
f8ea3026 143 Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
8cd4d17c 144 if (EFI_ERROR (Status)) {\r
812acaec 145 //Code here doesn't consider the runtime environment.\r
8cd4d17c 146 if (!EfiAtRuntime ()) {\r
8d85dc31 147 EfiReleaseLock (&Global->RtcLock);\r
8cd4d17c 148 }\r
149 return EFI_DEVICE_ERROR;\r
150 }\r
151 //\r
152 // Get the Time/Date/Daylight Savings values.\r
153 //\r
154 Time.Second = RtcRead (RTC_ADDRESS_SECONDS);\r
155 Time.Minute = RtcRead (RTC_ADDRESS_MINUTES);\r
156 Time.Hour = RtcRead (RTC_ADDRESS_HOURS);\r
157 Time.Day = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);\r
158 Time.Month = RtcRead (RTC_ADDRESS_MONTH);\r
159 Time.Year = RtcRead (RTC_ADDRESS_YEAR);\r
160\r
8cd4d17c 161 if (RtcTestCenturyRegister () == EFI_SUCCESS) {\r
254ba247 162 Century = (UINT8) (RtcRead (RTC_ADDRESS_CENTURY) & 0x7f);\r
8cd4d17c 163 } else {\r
254ba247 164 Century = RtcRead (RTC_ADDRESS_CENTURY);\r
8cd4d17c 165 }\r
166\r
8cd4d17c 167 //\r
168 // Set RTC configuration after get original time\r
ec35e997 169 // The value of bit AIE should be reserved.\r
8cd4d17c 170 //\r
f7033880 171 RtcWrite (RTC_ADDRESS_REGISTER_B, (UINT8)(RTC_INIT_REGISTER_B | (RegisterB.Data & BIT5)));\r
8cd4d17c 172\r
173 //\r
174 // Release RTC Lock.\r
175 //\r
812acaec 176 //Code here doesn't consider the runtime environment.\r
8cd4d17c 177 if (!EfiAtRuntime ()) {\r
8d85dc31 178 EfiReleaseLock (&Global->RtcLock);\r
8cd4d17c 179 }\r
254ba247 180 \r
8cd4d17c 181 //\r
182 // Validate time fields\r
183 //\r
254ba247 184 Status = ConvertRtcTimeToEfiTime (&Time, Century, RegisterB);\r
185 if (!EFI_ERROR (Status)) {\r
186 Status = RtcTimeFieldsValid (&Time);\r
187 }\r
8cd4d17c 188 if (EFI_ERROR (Status)) {\r
189 Time.Second = RTC_INIT_SECOND;\r
190 Time.Minute = RTC_INIT_MINUTE;\r
191 Time.Hour = RTC_INIT_HOUR;\r
192 Time.Day = RTC_INIT_DAY;\r
193 Time.Month = RTC_INIT_MONTH;\r
194 Time.Year = RTC_INIT_YEAR;\r
195 }\r
196 //\r
ec35e997
LG
197 // Get the data of Daylight saving and time zone, if they have been\r
198 // stored in NV variable during previous boot.\r
199 //\r
200 DataSize = sizeof (UINT32);\r
201 Status = EfiGetVariable (\r
202 L"TimerVar",\r
203 &gEfiGenericPlatformVariableGuid,\r
204 NULL,\r
205 &DataSize,\r
206 (VOID *) &TimerVar\r
207 );\r
208 if (!EFI_ERROR (Status)) {\r
209 Global->SavedTimeZone = (INT16) TimerVar;\r
210 Global->Daylight = (UINT8) (TimerVar >> 16);\r
211\r
212 Time.TimeZone = Global->SavedTimeZone;\r
213 Time.Daylight = Global->Daylight;\r
214 }\r
215 //\r
8cd4d17c 216 // Reset time value according to new RTC configuration\r
217 //\r
218 PcRtcSetTime (&Time, Global);\r
219\r
220 return EFI_SUCCESS;\r
221}\r
222\r
8d85dc31 223/**\r
224 Returns the current time and date information, and the time-keeping capabilities\r
225 of the hardware platform.\r
8cd4d17c 226\r
8d85dc31 227 @param Time A pointer to storage to receive a snapshot of the current time.\r
228 @param Capabilities An optional pointer to a buffer to receive the real time clock\r
229 device's capabilities.\r
230 @param Global For global use inside this module.\r
8cd4d17c 231\r
8d85dc31 232 @retval EFI_SUCCESS The operation completed successfully.\r
233 @retval EFI_INVALID_PARAMETER Time is NULL.\r
234 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.\r
8cd4d17c 235\r
8d85dc31 236**/\r
237EFI_STATUS\r
238PcRtcGetTime (\r
239 OUT EFI_TIME *Time,\r
240 OUT EFI_TIME_CAPABILITIES *Capabilities, OPTIONAL\r
241 IN PC_RTC_MODULE_GLOBALS *Global\r
242 )\r
8cd4d17c 243{\r
244 EFI_STATUS Status;\r
245 RTC_REGISTER_B RegisterB;\r
246 UINT8 Century;\r
247\r
248 //\r
249 // Check parameters for null pointer\r
250 //\r
251 if (Time == NULL) {\r
252 return EFI_INVALID_PARAMETER;\r
253\r
254 }\r
255 //\r
256 // Acquire RTC Lock to make access to RTC atomic\r
257 //\r
812acaec 258 //Code here doesn't consider the runtime environment.\r
8cd4d17c 259 if (!EfiAtRuntime ()) {\r
8d85dc31 260 EfiAcquireLock (&Global->RtcLock);\r
8cd4d17c 261 }\r
262 //\r
263 // Wait for up to 0.1 seconds for the RTC to be updated\r
264 //\r
f8ea3026 265 Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
8cd4d17c 266 if (EFI_ERROR (Status)) {\r
812acaec 267 //Code here doesn't consider the runtime environment.\r
8cd4d17c 268 if (!EfiAtRuntime ()) {\r
8d85dc31 269 EfiReleaseLock (&Global->RtcLock);\r
8cd4d17c 270 }\r
271 return Status;\r
272 }\r
273 //\r
274 // Read Register B\r
275 //\r
276 RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);\r
277\r
278 //\r
279 // Get the Time/Date/Daylight Savings values.\r
280 //\r
281 Time->Second = RtcRead (RTC_ADDRESS_SECONDS);\r
282 Time->Minute = RtcRead (RTC_ADDRESS_MINUTES);\r
283 Time->Hour = RtcRead (RTC_ADDRESS_HOURS);\r
284 Time->Day = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);\r
285 Time->Month = RtcRead (RTC_ADDRESS_MONTH);\r
286 Time->Year = RtcRead (RTC_ADDRESS_YEAR);\r
287\r
8cd4d17c 288 if (RtcTestCenturyRegister () == EFI_SUCCESS) {\r
254ba247 289 Century = (UINT8) (RtcRead (RTC_ADDRESS_CENTURY) & 0x7f);\r
8cd4d17c 290 } else {\r
254ba247 291 Century = RtcRead (RTC_ADDRESS_CENTURY);\r
8cd4d17c 292 }\r
293\r
8cd4d17c 294 //\r
295 // Release RTC Lock.\r
296 //\r
812acaec 297 //Code here doesn't consider the runtime environment.\r
8cd4d17c 298 if (!EfiAtRuntime ()) {\r
8d85dc31 299 EfiReleaseLock (&Global->RtcLock);\r
8cd4d17c 300 }\r
301 //\r
2d4117c0 302 // Get the variable that contains the TimeZone and Daylight fields\r
8cd4d17c 303 //\r
304 Time->TimeZone = Global->SavedTimeZone;\r
305 Time->Daylight = Global->Daylight;\r
306\r
307 //\r
308 // Make sure all field values are in correct range\r
309 //\r
254ba247 310 Status = ConvertRtcTimeToEfiTime (Time, Century, RegisterB);\r
311 if (!EFI_ERROR (Status)) {\r
312 Status = RtcTimeFieldsValid (Time);\r
313 }\r
8cd4d17c 314 if (EFI_ERROR (Status)) {\r
315 return EFI_DEVICE_ERROR;\r
316 }\r
317 //\r
318 // Fill in Capabilities if it was passed in\r
319 //\r
8d85dc31 320 if (Capabilities != NULL) {\r
8cd4d17c 321 Capabilities->Resolution = 1;\r
322 //\r
323 // 1 hertz\r
324 //\r
325 Capabilities->Accuracy = 50000000;\r
326 //\r
327 // 50 ppm\r
328 //\r
329 Capabilities->SetsToZero = FALSE;\r
330 }\r
331\r
332 return EFI_SUCCESS;\r
333}\r
334\r
8d85dc31 335/**\r
336 Sets the current local time and date information.\r
337\r
338 @param Time A pointer to the current time.\r
339 @param Global For global use inside this module.\r
340\r
341 @retval EFI_SUCCESS The operation completed successfully.\r
342 @retval EFI_INVALID_PARAMETER A time field is out of range.\r
343 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.\r
344\r
345**/\r
8cd4d17c 346EFI_STATUS\r
347PcRtcSetTime (\r
348 IN EFI_TIME *Time,\r
349 IN PC_RTC_MODULE_GLOBALS *Global\r
350 )\r
8cd4d17c 351{\r
352 EFI_STATUS Status;\r
353 EFI_TIME RtcTime;\r
354 RTC_REGISTER_B RegisterB;\r
355 UINT8 Century;\r
ec35e997 356 UINT32 TimerVar;\r
8cd4d17c 357\r
358 if (Time == NULL) {\r
359 return EFI_INVALID_PARAMETER;\r
360 }\r
361 //\r
362 // Make sure that the time fields are valid\r
363 //\r
364 Status = RtcTimeFieldsValid (Time);\r
365 if (EFI_ERROR (Status)) {\r
366 return Status;\r
367 }\r
368\r
369 CopyMem (&RtcTime, Time, sizeof (EFI_TIME));\r
370\r
371 //\r
372 // Acquire RTC Lock to make access to RTC atomic\r
373 //\r
812acaec 374 //Code here doesn't consider the runtime environment.\r
8cd4d17c 375 if (!EfiAtRuntime ()) {\r
8d85dc31 376 EfiAcquireLock (&Global->RtcLock);\r
8cd4d17c 377 }\r
378 //\r
379 // Wait for up to 0.1 seconds for the RTC to be updated\r
380 //\r
f8ea3026 381 Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
8cd4d17c 382 if (EFI_ERROR (Status)) {\r
812acaec 383 //Code here doesn't consider the runtime environment.\r
8cd4d17c 384 if (!EfiAtRuntime ()) {\r
ca08e40b 385 EfiReleaseLock (&Global->RtcLock);\r
8cd4d17c 386 }\r
387 return Status;\r
388 }\r
389 //\r
390 // Read Register B, and inhibit updates of the RTC\r
391 //\r
392 RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);\r
393 RegisterB.Bits.SET = 1;\r
394 RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
395\r
396 ConvertEfiTimeToRtcTime (&RtcTime, RegisterB, &Century);\r
397\r
398 RtcWrite (RTC_ADDRESS_SECONDS, RtcTime.Second);\r
399 RtcWrite (RTC_ADDRESS_MINUTES, RtcTime.Minute);\r
400 RtcWrite (RTC_ADDRESS_HOURS, RtcTime.Hour);\r
401 RtcWrite (RTC_ADDRESS_DAY_OF_THE_MONTH, RtcTime.Day);\r
402 RtcWrite (RTC_ADDRESS_MONTH, RtcTime.Month);\r
403 RtcWrite (RTC_ADDRESS_YEAR, (UINT8) RtcTime.Year);\r
404 if (RtcTestCenturyRegister () == EFI_SUCCESS) {\r
405 Century = (UINT8) ((Century & 0x7f) | (RtcRead (RTC_ADDRESS_CENTURY) & 0x80));\r
406 }\r
407\r
408 RtcWrite (RTC_ADDRESS_CENTURY, Century);\r
409\r
410 //\r
411 // Allow updates of the RTC registers\r
412 //\r
413 RegisterB.Bits.SET = 0;\r
414 RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
415\r
416 //\r
417 // Release RTC Lock.\r
418 //\r
812acaec 419 //Code here doesn't consider the runtime environment.\r
8cd4d17c 420 if (!EfiAtRuntime ()) {\r
8d85dc31 421 EfiReleaseLock (&Global->RtcLock);\r
8cd4d17c 422 }\r
423 //\r
2d4117c0 424 // Set the variable that contains the TimeZone and Daylight fields\r
8cd4d17c 425 //\r
426 Global->SavedTimeZone = Time->TimeZone;\r
427 Global->Daylight = Time->Daylight;\r
ec35e997
LG
428\r
429 TimerVar = Time->Daylight;\r
430 TimerVar = (UINT32) ((TimerVar << 16) | Time->TimeZone);\r
431 Status = EfiSetVariable (\r
432 L"TimerVar",\r
433 &gEfiGenericPlatformVariableGuid,\r
434 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
435 sizeof (TimerVar),\r
436 &TimerVar\r
437 );\r
438 ASSERT_EFI_ERROR (Status);\r
439\r
440 return EFI_SUCCESS;\r
8cd4d17c 441}\r
442\r
8d85dc31 443/**\r
444 Returns the current wakeup alarm clock setting.\r
445\r
446 @param Enabled Indicates if the alarm is currently enabled or disabled.\r
2d4117c0 447 @param Pending Indicates if the alarm signal is pending and requires acknowledgment.\r
8d85dc31 448 @param Time The current alarm setting.\r
449 @param Global For global use inside this module.\r
450\r
451 @retval EFI_SUCCESS The alarm settings were returned.\r
452 @retval EFI_INVALID_PARAMETER Enabled is NULL.\r
453 @retval EFI_INVALID_PARAMETER Pending is NULL.\r
454 @retval EFI_INVALID_PARAMETER Time is NULL.\r
455 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.\r
456 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.\r
457\r
458**/\r
8cd4d17c 459EFI_STATUS\r
460PcRtcGetWakeupTime (\r
461 OUT BOOLEAN *Enabled,\r
462 OUT BOOLEAN *Pending,\r
463 OUT EFI_TIME *Time,\r
8d85dc31 464 IN PC_RTC_MODULE_GLOBALS *Global\r
8cd4d17c 465 )\r
8cd4d17c 466{\r
467 EFI_STATUS Status;\r
468 RTC_REGISTER_B RegisterB;\r
469 RTC_REGISTER_C RegisterC;\r
470 UINT8 Century;\r
471\r
472 //\r
2d4117c0 473 // Check parameters for null pointers\r
8cd4d17c 474 //\r
475 if ((Enabled == NULL) || (Pending == NULL) || (Time == NULL)) {\r
476 return EFI_INVALID_PARAMETER;\r
477\r
478 }\r
479 //\r
480 // Acquire RTC Lock to make access to RTC atomic\r
481 //\r
812acaec 482 //Code here doesn't consider the runtime environment.\r
8cd4d17c 483 if (!EfiAtRuntime ()) {\r
8d85dc31 484 EfiAcquireLock (&Global->RtcLock);\r
8cd4d17c 485 }\r
486 //\r
487 // Wait for up to 0.1 seconds for the RTC to be updated\r
488 //\r
f8ea3026 489 Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
8cd4d17c 490 if (EFI_ERROR (Status)) {\r
812acaec 491 //Code here doesn't consider the runtime environment.\r
8cd4d17c 492 if (!EfiAtRuntime ()) {\r
493 EfiReleaseLock (&Global->RtcLock);\r
494 }\r
495 return EFI_DEVICE_ERROR;\r
496 }\r
497 //\r
498 // Read Register B and Register C\r
499 //\r
500 RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);\r
501 RegisterC.Data = RtcRead (RTC_ADDRESS_REGISTER_C);\r
502\r
503 //\r
504 // Get the Time/Date/Daylight Savings values.\r
505 //\r
506 *Enabled = RegisterB.Bits.AIE;\r
507 if (*Enabled) {\r
508 Time->Second = RtcRead (RTC_ADDRESS_SECONDS_ALARM);\r
509 Time->Minute = RtcRead (RTC_ADDRESS_MINUTES_ALARM);\r
510 Time->Hour = RtcRead (RTC_ADDRESS_HOURS_ALARM);\r
511 Time->Day = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);\r
512 Time->Month = RtcRead (RTC_ADDRESS_MONTH);\r
513 Time->Year = RtcRead (RTC_ADDRESS_YEAR);\r
514 } else {\r
515 Time->Second = 0;\r
516 Time->Minute = 0;\r
517 Time->Hour = 0;\r
518 Time->Day = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);\r
519 Time->Month = RtcRead (RTC_ADDRESS_MONTH);\r
520 Time->Year = RtcRead (RTC_ADDRESS_YEAR);\r
521 }\r
522\r
8cd4d17c 523 if (RtcTestCenturyRegister () == EFI_SUCCESS) {\r
254ba247 524 Century = (UINT8) (RtcRead (RTC_ADDRESS_CENTURY) & 0x7f);\r
8cd4d17c 525 } else {\r
254ba247 526 Century = RtcRead (RTC_ADDRESS_CENTURY);\r
8cd4d17c 527 }\r
528\r
8cd4d17c 529 //\r
530 // Release RTC Lock.\r
531 //\r
812acaec 532 //Code here doesn't consider the runtime environment.\r
8cd4d17c 533 if (!EfiAtRuntime ()) {\r
8d85dc31 534 EfiReleaseLock (&Global->RtcLock);\r
8cd4d17c 535 }\r
536 //\r
537 // Make sure all field values are in correct range\r
538 //\r
254ba247 539 Status = ConvertRtcTimeToEfiTime (Time, Century, RegisterB);\r
540 if (!EFI_ERROR (Status)) {\r
541 Status = RtcTimeFieldsValid (Time);\r
542 }\r
8cd4d17c 543 if (EFI_ERROR (Status)) {\r
544 return EFI_DEVICE_ERROR;\r
545 }\r
546\r
547 *Pending = RegisterC.Bits.AF;\r
548\r
549 return EFI_SUCCESS;\r
550}\r
551\r
8d85dc31 552/**\r
553 Sets the system wakeup alarm clock time.\r
554\r
555 @param Enabled Enable or disable the wakeup alarm.\r
556 @param Time If Enable is TRUE, the time to set the wakeup alarm for.\r
557 If Enable is FALSE, then this parameter is optional, and may be NULL.\r
558 @param Global For global use inside this module.\r
559\r
560 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled.\r
561 If Enable is FALSE, then the wakeup alarm was disabled.\r
562 @retval EFI_INVALID_PARAMETER A time field is out of range.\r
563 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.\r
564 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.\r
565\r
566**/\r
8cd4d17c 567EFI_STATUS\r
568PcRtcSetWakeupTime (\r
569 IN BOOLEAN Enable,\r
8d85dc31 570 IN EFI_TIME *Time, OPTIONAL\r
8cd4d17c 571 IN PC_RTC_MODULE_GLOBALS *Global\r
572 )\r
8cd4d17c 573{\r
574 EFI_STATUS Status;\r
575 EFI_TIME RtcTime;\r
576 RTC_REGISTER_B RegisterB;\r
577 UINT8 Century;\r
578 EFI_TIME_CAPABILITIES Capabilities;\r
579\r
580 if (Enable) {\r
581\r
582 if (Time == NULL) {\r
583 return EFI_INVALID_PARAMETER;\r
584 }\r
585 //\r
586 // Make sure that the time fields are valid\r
587 //\r
588 Status = RtcTimeFieldsValid (Time);\r
589 if (EFI_ERROR (Status)) {\r
590 return EFI_INVALID_PARAMETER;\r
591 }\r
592 //\r
593 // Just support set alarm time within 24 hours\r
594 //\r
595 PcRtcGetTime (&RtcTime, &Capabilities, Global);\r
596 if (!IsWithinOneDay (&RtcTime, Time)) {\r
597 return EFI_UNSUPPORTED;\r
598 }\r
599 //\r
600 // Make a local copy of the time and date\r
601 //\r
602 CopyMem (&RtcTime, Time, sizeof (EFI_TIME));\r
603\r
604 }\r
605 //\r
606 // Acquire RTC Lock to make access to RTC atomic\r
607 //\r
812acaec 608 //Code here doesn't consider the runtime environment.\r
8cd4d17c 609 if (!EfiAtRuntime ()) {\r
8d85dc31 610 EfiAcquireLock (&Global->RtcLock);\r
8cd4d17c 611 }\r
612 //\r
613 // Wait for up to 0.1 seconds for the RTC to be updated\r
614 //\r
f8ea3026 615 Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
8cd4d17c 616 if (EFI_ERROR (Status)) {\r
812acaec 617 //Code here doesn't consider the runtime environment.\r
8cd4d17c 618 if (!EfiAtRuntime ()) {\r
619 EfiReleaseLock (&Global->RtcLock);\r
620 }\r
621 return EFI_DEVICE_ERROR;\r
622 }\r
623 //\r
624 // Read Register B, and inhibit updates of the RTC\r
625 //\r
626 RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);\r
627\r
628 RegisterB.Bits.SET = 1;\r
629 RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
630\r
631 if (Enable) {\r
632 ConvertEfiTimeToRtcTime (&RtcTime, RegisterB, &Century);\r
633\r
634 //\r
635 // Set RTC alarm time\r
636 //\r
637 RtcWrite (RTC_ADDRESS_SECONDS_ALARM, RtcTime.Second);\r
638 RtcWrite (RTC_ADDRESS_MINUTES_ALARM, RtcTime.Minute);\r
639 RtcWrite (RTC_ADDRESS_HOURS_ALARM, RtcTime.Hour);\r
640\r
641 RegisterB.Bits.AIE = 1;\r
642\r
643 } else {\r
644 RegisterB.Bits.AIE = 0;\r
645 }\r
646 //\r
647 // Allow updates of the RTC registers\r
648 //\r
649 RegisterB.Bits.SET = 0;\r
650 RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
651\r
652 //\r
653 // Release RTC Lock.\r
654 //\r
812acaec 655 //Code here doesn't consider the runtime environment.\r
8cd4d17c 656 if (!EfiAtRuntime ()) {\r
8d85dc31 657 EfiReleaseLock (&Global->RtcLock);\r
8cd4d17c 658 }\r
659 return EFI_SUCCESS;\r
660}\r
661\r
8d85dc31 662/**\r
2d4117c0 663 See if century register of RTC is valid.\r
8d85dc31 664\r
665 @retval EFI_SUCCESS Century register is valid.\r
666 @retval EFI_DEVICE_ERROR Century register is NOT valid.\r
667**/\r
8cd4d17c 668EFI_STATUS\r
669RtcTestCenturyRegister (\r
670 VOID\r
671 )\r
8cd4d17c 672{\r
673 UINT8 Century;\r
674 UINT8 Temp;\r
675\r
676 Century = RtcRead (RTC_ADDRESS_CENTURY);\r
8cd4d17c 677 Temp = (UINT8) (RtcRead (RTC_ADDRESS_CENTURY) & 0x7f);\r
678 RtcWrite (RTC_ADDRESS_CENTURY, Century);\r
679 if (Temp == 0x19 || Temp == 0x20) {\r
680 return EFI_SUCCESS;\r
681 }\r
682\r
683 return EFI_DEVICE_ERROR;\r
684}\r
685\r
254ba247 686/**\r
687 Checks an 8-bit BCD value, and converts to an 8-bit value if valid.\r
688\r
689 This function checks the 8-bit BCD value specified by Value.\r
690 If valid, the function converts it to an 8-bit value and returns it.\r
691 Otherwise, return 0xff.\r
692\r
8d85dc31 693 @param Value The 8-bit BCD value to check and convert\r
254ba247 694\r
8d85dc31 695 @return The 8-bit value converted. Or 0xff if Value is invalid.\r
254ba247 696\r
697**/\r
698UINT8\r
699CheckAndConvertBcd8ToDecimal8 (\r
700 IN UINT8 Value\r
8cd4d17c 701 )\r
254ba247 702{\r
703 if ((Value < 0xa0) && ((Value & 0xf) < 0xa)) {\r
704 return BcdToDecimal8 (Value);\r
705 }\r
8cd4d17c 706\r
254ba247 707 return 0xff;\r
708}\r
8cd4d17c 709\r
254ba247 710/**\r
711 Converts time read from RTC to EFI_TIME format defined by UEFI spec.\r
8cd4d17c 712\r
254ba247 713 This function converts raw time data read from RTC to the EFI_TIME format\r
714 defined by UEFI spec.\r
715 If data mode of RTC is BCD, then converts it to decimal,\r
716 If RTC is in 12-hour format, then converts it to 24-hour format.\r
8cd4d17c 717\r
254ba247 718 @param Time On input, the time data read from RTC to convert\r
719 On output, the time converted to UEFI format\r
720 @param Century Value of century read from RTC.\r
721 @param RegisterB Value of Register B of RTC, indicating data mode\r
722 and hour format.\r
8cd4d17c 723\r
8d85dc31 724 @retval EFI_INVALID_PARAMETER Parameters passed in are invalid.\r
725 @retval EFI_SUCCESS Convert RTC time to EFI time successfully.\r
726\r
254ba247 727**/\r
728EFI_STATUS\r
729ConvertRtcTimeToEfiTime (\r
730 IN OUT EFI_TIME *Time,\r
731 IN UINT8 Century,\r
732 IN RTC_REGISTER_B RegisterB\r
733 )\r
8cd4d17c 734{\r
8d85dc31 735 BOOLEAN IsPM;\r
8cd4d17c 736\r
21176834 737 if ((Time->Hour & 0x80) != 0) {\r
8d85dc31 738 IsPM = TRUE;\r
8cd4d17c 739 } else {\r
8d85dc31 740 IsPM = FALSE;\r
8cd4d17c 741 }\r
742\r
743 Time->Hour = (UINT8) (Time->Hour & 0x7f);\r
744\r
745 if (RegisterB.Bits.DM == 0) {\r
254ba247 746 Time->Year = CheckAndConvertBcd8ToDecimal8 ((UINT8) Time->Year);\r
747 Time->Month = CheckAndConvertBcd8ToDecimal8 (Time->Month);\r
748 Time->Day = CheckAndConvertBcd8ToDecimal8 (Time->Day);\r
749 Time->Hour = CheckAndConvertBcd8ToDecimal8 (Time->Hour);\r
750 Time->Minute = CheckAndConvertBcd8ToDecimal8 (Time->Minute);\r
751 Time->Second = CheckAndConvertBcd8ToDecimal8 (Time->Second);\r
752 Century = CheckAndConvertBcd8ToDecimal8 (Century);\r
753 }\r
754\r
755 if (Time->Year == 0xff || Time->Month == 0xff || Time->Day == 0xff ||\r
756 Time->Hour == 0xff || Time->Minute == 0xff || Time->Second == 0xff ||\r
757 Century == 0xff) {\r
758 return EFI_INVALID_PARAMETER;\r
8cd4d17c 759 }\r
254ba247 760\r
761 Time->Year = (UINT16) (Century * 100 + Time->Year);\r
762\r
8cd4d17c 763 //\r
764 // If time is in 12 hour format, convert it to 24 hour format\r
765 //\r
766 if (RegisterB.Bits.MIL == 0) {\r
8d85dc31 767 if (IsPM && Time->Hour < 12) {\r
8cd4d17c 768 Time->Hour = (UINT8) (Time->Hour + 12);\r
769 }\r
770\r
8d85dc31 771 if (!IsPM && Time->Hour == 12) {\r
8cd4d17c 772 Time->Hour = 0;\r
773 }\r
774 }\r
775\r
776 Time->Nanosecond = 0;\r
777 Time->TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r
778 Time->Daylight = 0;\r
254ba247 779\r
780 return EFI_SUCCESS;\r
8cd4d17c 781}\r
782\r
8d85dc31 783/**\r
784 Wait for a period for the RTC to be ready.\r
785\r
786 @param Timeout Tell how long it should take to wait.\r
787\r
788 @retval EFI_DEVICE_ERROR RTC device error.\r
789 @retval EFI_SUCCESS RTC is updated and ready. \r
790**/\r
8cd4d17c 791EFI_STATUS\r
792RtcWaitToUpdate (\r
793 UINTN Timeout\r
794 )\r
8cd4d17c 795{\r
796 RTC_REGISTER_A RegisterA;\r
797 RTC_REGISTER_D RegisterD;\r
798\r
799 //\r
800 // See if the RTC is functioning correctly\r
801 //\r
802 RegisterD.Data = RtcRead (RTC_ADDRESS_REGISTER_D);\r
803\r
804 if (RegisterD.Bits.VRT == 0) {\r
805 return EFI_DEVICE_ERROR;\r
806 }\r
807 //\r
808 // Wait for up to 0.1 seconds for the RTC to be ready.\r
809 //\r
810 Timeout = (Timeout / 10) + 1;\r
811 RegisterA.Data = RtcRead (RTC_ADDRESS_REGISTER_A);\r
812 while (RegisterA.Bits.UIP == 1 && Timeout > 0) {\r
813 MicroSecondDelay (10);\r
814 RegisterA.Data = RtcRead (RTC_ADDRESS_REGISTER_A);\r
815 Timeout--;\r
816 }\r
817\r
818 RegisterD.Data = RtcRead (RTC_ADDRESS_REGISTER_D);\r
819 if (Timeout == 0 || RegisterD.Bits.VRT == 0) {\r
820 return EFI_DEVICE_ERROR;\r
821 }\r
822\r
823 return EFI_SUCCESS;\r
824}\r
825\r
8d85dc31 826/**\r
827 See if all fields of a variable of EFI_TIME type is correct.\r
828\r
829 @param Time The time to be checked.\r
830\r
831 @retval EFI_INVALID_PARAMETER Some fields of Time are not correct.\r
832 @retval EFI_SUCCESS Time is a valid EFI_TIME variable.\r
833\r
834**/\r
8cd4d17c 835EFI_STATUS\r
836RtcTimeFieldsValid (\r
837 IN EFI_TIME *Time\r
838 )\r
8cd4d17c 839{\r
840 if (Time->Year < 1998 ||\r
841 Time->Year > 2099 ||\r
842 Time->Month < 1 ||\r
843 Time->Month > 12 ||\r
844 (!DayValid (Time)) ||\r
845 Time->Hour > 23 ||\r
846 Time->Minute > 59 ||\r
847 Time->Second > 59 ||\r
848 Time->Nanosecond > 999999999 ||\r
849 (!(Time->TimeZone == EFI_UNSPECIFIED_TIMEZONE || (Time->TimeZone >= -1440 && Time->TimeZone <= 1440))) ||\r
21176834 850 ((Time->Daylight & (~(EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT))) != 0)\r
8cd4d17c 851 ) {\r
852 return EFI_INVALID_PARAMETER;\r
853 }\r
854\r
855 return EFI_SUCCESS;\r
856}\r
857\r
8d85dc31 858/**\r
859 See if field Day of an EFI_TIME is correct.\r
860\r
861 @param Time Its Day field is to be checked.\r
862\r
863 @retval TRUE Day field of Time is correct.\r
864 @retval FALSE Day field of Time is NOT correct.\r
865**/\r
8cd4d17c 866BOOLEAN\r
867DayValid (\r
868 IN EFI_TIME *Time\r
869 )\r
8cd4d17c 870{\r
871 INTN DayOfMonth[12];\r
872\r
873 DayOfMonth[0] = 31;\r
874 DayOfMonth[1] = 29;\r
875 DayOfMonth[2] = 31;\r
876 DayOfMonth[3] = 30;\r
877 DayOfMonth[4] = 31;\r
878 DayOfMonth[5] = 30;\r
879 DayOfMonth[6] = 31;\r
880 DayOfMonth[7] = 31;\r
881 DayOfMonth[8] = 30;\r
882 DayOfMonth[9] = 31;\r
883 DayOfMonth[10] = 30;\r
884 DayOfMonth[11] = 31;\r
885\r
886 if (Time->Day < 1 ||\r
887 Time->Day > DayOfMonth[Time->Month - 1] ||\r
888 (Time->Month == 2 && (!IsLeapYear (Time) && Time->Day > 28))\r
889 ) {\r
890 return FALSE;\r
891 }\r
892\r
893 return TRUE;\r
894}\r
895\r
8d85dc31 896/**\r
2d4117c0 897 Check if it is a leap year.\r
8d85dc31 898\r
899 @param Time The time to be checked.\r
900\r
2d4117c0 901 @retval TRUE It is a leap year.\r
902 @retval FALSE It is NOT a leap year.\r
8d85dc31 903**/\r
8cd4d17c 904BOOLEAN\r
905IsLeapYear (\r
906 IN EFI_TIME *Time\r
907 )\r
8cd4d17c 908{\r
909 if (Time->Year % 4 == 0) {\r
910 if (Time->Year % 100 == 0) {\r
911 if (Time->Year % 400 == 0) {\r
912 return TRUE;\r
913 } else {\r
914 return FALSE;\r
915 }\r
916 } else {\r
917 return TRUE;\r
918 }\r
919 } else {\r
920 return FALSE;\r
921 }\r
922}\r
923\r
8d85dc31 924/**\r
925 Converts time from EFI_TIME format defined by UEFI spec to RTC's.\r
8cd4d17c 926\r
8d85dc31 927 This function converts time from EFI_TIME format defined by UEFI spec to RTC's.\r
928 If data mode of RTC is BCD, then converts EFI_TIME to it.\r
929 If RTC is in 12-hour format, then converts EFI_TIME to it.\r
8cd4d17c 930\r
8d85dc31 931 @param Time On input, the time data read from UEFI to convert\r
932 On output, the time converted to RTC format\r
933 @param RegisterB Value of Register B of RTC, indicating data mode\r
934 @param Century It is set according to EFI_TIME Time.\r
8cd4d17c 935\r
8d85dc31 936**/\r
937VOID\r
938ConvertEfiTimeToRtcTime (\r
939 IN OUT EFI_TIME *Time,\r
940 IN RTC_REGISTER_B RegisterB,\r
941 OUT UINT8 *Century\r
942 )\r
8cd4d17c 943{\r
8d85dc31 944 BOOLEAN IsPM;\r
8cd4d17c 945\r
8d85dc31 946 IsPM = TRUE;\r
8cd4d17c 947 //\r
8d85dc31 948 // Adjust hour field if RTC is in 12 hour mode\r
8cd4d17c 949 //\r
950 if (RegisterB.Bits.MIL == 0) {\r
951 if (Time->Hour < 12) {\r
8d85dc31 952 IsPM = FALSE;\r
8cd4d17c 953 }\r
954\r
955 if (Time->Hour >= 13) {\r
956 Time->Hour = (UINT8) (Time->Hour - 12);\r
957 } else if (Time->Hour == 0) {\r
958 Time->Hour = 12;\r
959 }\r
960 }\r
961 //\r
962 // Set the Time/Date/Daylight Savings values.\r
963 //\r
964 *Century = DecimalToBcd8 ((UINT8) (Time->Year / 100));\r
965\r
966 Time->Year = (UINT16) (Time->Year % 100);\r
967\r
968 if (RegisterB.Bits.DM == 0) {\r
969 Time->Year = DecimalToBcd8 ((UINT8) Time->Year);\r
970 Time->Month = DecimalToBcd8 (Time->Month);\r
971 Time->Day = DecimalToBcd8 (Time->Day);\r
972 Time->Hour = DecimalToBcd8 (Time->Hour);\r
973 Time->Minute = DecimalToBcd8 (Time->Minute);\r
974 Time->Second = DecimalToBcd8 (Time->Second);\r
975 }\r
976 //\r
977 // If we are in 12 hour mode and PM is set, then set bit 7 of the Hour field.\r
978 //\r
8d85dc31 979 if (RegisterB.Bits.MIL == 0 && IsPM) {\r
8cd4d17c 980 Time->Hour = (UINT8) (Time->Hour | 0x80);\r
981 }\r
982}\r
983\r
8d85dc31 984/**\r
985 Compare the Hour, Minute and Second of the From time and the To time.\r
986 \r
987 Only compare H/M/S in EFI_TIME and ignore other fields here.\r
988\r
989 @param From the first time\r
990 @param To the second time\r
991\r
992 @return >0 The H/M/S of the From time is later than those of To time\r
993 @return ==0 The H/M/S of the From time is same as those of To time\r
994 @return <0 The H/M/S of the From time is earlier than those of To time\r
995**/\r
8cd4d17c 996INTN\r
997CompareHMS (\r
998 IN EFI_TIME *From,\r
999 IN EFI_TIME *To\r
1000 )\r
8cd4d17c 1001{\r
1002 if ((From->Hour > To->Hour) ||\r
1003 ((From->Hour == To->Hour) && (From->Minute > To->Minute)) ||\r
1004 ((From->Hour == To->Hour) && (From->Minute == To->Minute) && (From->Second > To->Second))) {\r
1005 return 1;\r
1006 } else if ((From->Hour == To->Hour) && (From->Minute == To->Minute) && (From->Second == To->Second)) {\r
1007 return 0;\r
1008 } else {\r
1009 return -1;\r
1010 }\r
1011}\r
1012\r
8d85dc31 1013/**\r
1014 To check if second date is later than first date within 24 hours.\r
1015\r
1016 @param From the first date\r
1017 @param To the second date\r
1018\r
1019 @retval TRUE From is previous to To within 24 hours.\r
1020 @retval FALSE From is later, or it is previous to To more than 24 hours.\r
1021**/\r
8cd4d17c 1022BOOLEAN\r
1023IsWithinOneDay (\r
1024 IN EFI_TIME *From,\r
1025 IN EFI_TIME *To\r
1026 )\r
8cd4d17c 1027{\r
1f4cf7b1 1028 UINT8 DayOfMonth[12];\r
1029 BOOLEAN Adjacent;\r
1030\r
1031 DayOfMonth[0] = 31;\r
1032 DayOfMonth[1] = 29;\r
1033 DayOfMonth[2] = 31;\r
1034 DayOfMonth[3] = 30;\r
1035 DayOfMonth[4] = 31;\r
1036 DayOfMonth[5] = 30;\r
1037 DayOfMonth[6] = 31;\r
1038 DayOfMonth[7] = 31;\r
1039 DayOfMonth[8] = 30;\r
1040 DayOfMonth[9] = 31;\r
1041 DayOfMonth[10] = 30;\r
1042 DayOfMonth[11] = 31;\r
1043\r
1044 Adjacent = FALSE;\r
8cd4d17c 1045\r
1046 if (From->Year == To->Year) {\r
1047 if (From->Month == To->Month) {\r
1048 if ((From->Day + 1) == To->Day) {\r
1049 if ((CompareHMS(From, To) >= 0)) {\r
1050 Adjacent = TRUE;\r
1051 }\r
1052 } else if (From->Day == To->Day) {\r
1053 if ((CompareHMS(From, To) <= 0)) {\r
1054 Adjacent = TRUE;\r
1055 }\r
1056 }\r
1057 } else if (((From->Month + 1) == To->Month) && (To->Day == 1)) {\r
1058 if ((From->Month == 2) && !IsLeapYear(From)) {\r
1059 if (From->Day == 28) {\r
1060 if ((CompareHMS(From, To) >= 0)) {\r
1061 Adjacent = TRUE;\r
1062 }\r
1063 }\r
1064 } else if (From->Day == DayOfMonth[From->Month - 1]) {\r
1065 if ((CompareHMS(From, To) >= 0)) {\r
1066 Adjacent = TRUE;\r
1067 }\r
1068 }\r
1069 }\r
1070 } else if (((From->Year + 1) == To->Year) &&\r
1071 (From->Month == 12) &&\r
1072 (From->Day == 31) &&\r
1073 (To->Month == 1) &&\r
1074 (To->Day == 1)) {\r
1075 if ((CompareHMS(From, To) >= 0)) {\r
1076 Adjacent = TRUE;\r
1077 }\r
1078 }\r
1079\r
1080 return Adjacent;\r
1081}\r
1082\r