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