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