]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcRtc.c
Replaced MAX_EXTENDED_DATA_SIZE by EFI_STATUS_CODE_DATA_MAX_SIZE.
[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 - 2010, 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
237EFIAPI\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
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
258 if (!EfiAtRuntime ()) {\r
259 EfiAcquireLock (&Global->RtcLock);\r
260 }\r
261 //\r
262 // Wait for up to 0.1 seconds for the RTC to be updated\r
263 //\r
264 Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
265 if (EFI_ERROR (Status)) {\r
266 if (!EfiAtRuntime ()) {\r
267 EfiReleaseLock (&Global->RtcLock);\r
268 }\r
269 return Status;\r
270 }\r
271 //\r
272 // Read Register B\r
273 //\r
274 RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);\r
275\r
276 //\r
277 // Get the Time/Date/Daylight Savings values.\r
278 //\r
279 Time->Second = RtcRead (RTC_ADDRESS_SECONDS);\r
280 Time->Minute = RtcRead (RTC_ADDRESS_MINUTES);\r
281 Time->Hour = RtcRead (RTC_ADDRESS_HOURS);\r
282 Time->Day = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);\r
283 Time->Month = RtcRead (RTC_ADDRESS_MONTH);\r
284 Time->Year = RtcRead (RTC_ADDRESS_YEAR);\r
285\r
286 Century = RtcRead (RTC_ADDRESS_CENTURY);\r
287 \r
288 //\r
289 // Release RTC Lock.\r
290 //\r
291 if (!EfiAtRuntime ()) {\r
292 EfiReleaseLock (&Global->RtcLock);\r
293 }\r
294\r
295 //\r
296 // Get the variable that contains the TimeZone and Daylight fields\r
297 //\r
298 Time->TimeZone = Global->SavedTimeZone;\r
299 Time->Daylight = Global->Daylight;\r
300\r
301 //\r
302 // Make sure all field values are in correct range\r
303 //\r
304 Status = ConvertRtcTimeToEfiTime (Time, Century, RegisterB);\r
305 if (!EFI_ERROR (Status)) {\r
306 Status = RtcTimeFieldsValid (Time);\r
307 }\r
308 if (EFI_ERROR (Status)) {\r
309 return EFI_DEVICE_ERROR;\r
310 }\r
311\r
312 //\r
313 // Fill in Capabilities if it was passed in\r
314 //\r
315 if (Capabilities != NULL) {\r
316 Capabilities->Resolution = 1;\r
317 //\r
318 // 1 hertz\r
319 //\r
320 Capabilities->Accuracy = 50000000;\r
321 //\r
322 // 50 ppm\r
323 //\r
324 Capabilities->SetsToZero = FALSE;\r
325 }\r
326\r
327 return EFI_SUCCESS;\r
328}\r
329\r
330/**\r
331 Sets the current local time and date information.\r
332\r
333 @param Time A pointer to the current time.\r
334 @param Global For global use inside this module.\r
335\r
336 @retval EFI_SUCCESS The operation completed successfully.\r
337 @retval EFI_INVALID_PARAMETER A time field is out of range.\r
338 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.\r
339\r
340**/\r
341EFI_STATUS\r
342EFIAPI\r
343PcRtcSetTime (\r
344 IN EFI_TIME *Time,\r
345 IN PC_RTC_MODULE_GLOBALS *Global\r
346 )\r
347{\r
348 EFI_STATUS Status;\r
349 EFI_TIME RtcTime;\r
350 RTC_REGISTER_B RegisterB;\r
351 UINT8 Century;\r
352 UINT32 TimerVar;\r
353\r
354 if (Time == NULL) {\r
355 return EFI_INVALID_PARAMETER;\r
356 }\r
357 //\r
358 // Make sure that the time fields are valid\r
359 //\r
360 Status = RtcTimeFieldsValid (Time);\r
361 if (EFI_ERROR (Status)) {\r
362 return Status;\r
363 }\r
364\r
365 CopyMem (&RtcTime, Time, sizeof (EFI_TIME));\r
366\r
367 //\r
368 // Acquire RTC Lock to make access to RTC atomic\r
369 //\r
370 if (!EfiAtRuntime ()) {\r
371 EfiAcquireLock (&Global->RtcLock);\r
372 }\r
373 //\r
374 // Wait for up to 0.1 seconds for the RTC to be updated\r
375 //\r
376 Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
377 if (EFI_ERROR (Status)) {\r
378 if (!EfiAtRuntime ()) {\r
379 EfiReleaseLock (&Global->RtcLock);\r
380 }\r
381 return Status;\r
382 }\r
383 //\r
384 // Read Register B, and inhibit updates of the RTC\r
385 //\r
386 RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);\r
387 RegisterB.Bits.Set = 1;\r
388 RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
389\r
390 ConvertEfiTimeToRtcTime (&RtcTime, RegisterB, &Century);\r
391\r
392 RtcWrite (RTC_ADDRESS_SECONDS, RtcTime.Second);\r
393 RtcWrite (RTC_ADDRESS_MINUTES, RtcTime.Minute);\r
394 RtcWrite (RTC_ADDRESS_HOURS, RtcTime.Hour);\r
395 RtcWrite (RTC_ADDRESS_DAY_OF_THE_MONTH, RtcTime.Day);\r
396 RtcWrite (RTC_ADDRESS_MONTH, RtcTime.Month);\r
397 RtcWrite (RTC_ADDRESS_YEAR, (UINT8) RtcTime.Year);\r
398 RtcWrite (RTC_ADDRESS_CENTURY, Century);\r
399\r
400 //\r
401 // Allow updates of the RTC registers\r
402 //\r
403 RegisterB.Bits.Set = 0;\r
404 RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
405\r
406 //\r
407 // Release RTC Lock.\r
408 //\r
409 if (!EfiAtRuntime ()) {\r
410 EfiReleaseLock (&Global->RtcLock);\r
411 }\r
412 //\r
413 // Set the variable that contains the TimeZone and Daylight fields\r
414 //\r
415 Global->SavedTimeZone = Time->TimeZone;\r
416 Global->Daylight = Time->Daylight;\r
417\r
418 TimerVar = Time->Daylight;\r
419 TimerVar = (UINT32) ((TimerVar << 16) | Time->TimeZone);\r
420 Status = EfiSetVariable (\r
421 L"RTC",\r
422 &gEfiCallerIdGuid,\r
423 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
424 sizeof (TimerVar),\r
425 &TimerVar\r
426 );\r
427 ASSERT_EFI_ERROR (Status);\r
428\r
429 return EFI_SUCCESS;\r
430}\r
431\r
432/**\r
433 Returns the current wakeup alarm clock setting.\r
434\r
435 @param Enabled Indicates if the alarm is currently enabled or disabled.\r
436 @param Pending Indicates if the alarm signal is pending and requires acknowledgment.\r
437 @param Time The current alarm setting.\r
438 @param Global For global use inside this module.\r
439\r
440 @retval EFI_SUCCESS The alarm settings were returned.\r
441 @retval EFI_INVALID_PARAMETER Enabled is NULL.\r
442 @retval EFI_INVALID_PARAMETER Pending is NULL.\r
443 @retval EFI_INVALID_PARAMETER Time is NULL.\r
444 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.\r
445 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.\r
446\r
447**/\r
448EFI_STATUS\r
449PcRtcGetWakeupTime (\r
450 OUT BOOLEAN *Enabled,\r
451 OUT BOOLEAN *Pending,\r
452 OUT EFI_TIME *Time,\r
453 IN PC_RTC_MODULE_GLOBALS *Global\r
454 )\r
455{\r
456 EFI_STATUS Status;\r
457 RTC_REGISTER_B RegisterB;\r
458 RTC_REGISTER_C RegisterC;\r
459 UINT8 Century;\r
460 EFI_TIME RtcTime;\r
461 UINTN DataSize;\r
462\r
463 //\r
464 // Check parameters for null pointers\r
465 //\r
466 if ((Enabled == NULL) || (Pending == NULL) || (Time == NULL)) {\r
467 return EFI_INVALID_PARAMETER;\r
468\r
469 }\r
470 //\r
471 // Acquire RTC Lock to make access to RTC atomic\r
472 //\r
473 if (!EfiAtRuntime ()) {\r
474 EfiAcquireLock (&Global->RtcLock);\r
475 }\r
476 //\r
477 // Wait for up to 0.1 seconds for the RTC to be updated\r
478 //\r
479 Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
480 if (EFI_ERROR (Status)) {\r
481 if (!EfiAtRuntime ()) {\r
482 EfiReleaseLock (&Global->RtcLock);\r
483 }\r
484 return EFI_DEVICE_ERROR;\r
485 }\r
486 //\r
487 // Read Register B and Register C\r
488 //\r
489 RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);\r
490 RegisterC.Data = RtcRead (RTC_ADDRESS_REGISTER_C);\r
491\r
492 //\r
493 // Get the Time/Date/Daylight Savings values.\r
494 //\r
495 *Enabled = RegisterB.Bits.Aie;\r
496 *Pending = RegisterC.Bits.Af;\r
497\r
498 Time->Second = RtcRead (RTC_ADDRESS_SECONDS_ALARM);\r
499 Time->Minute = RtcRead (RTC_ADDRESS_MINUTES_ALARM);\r
500 Time->Hour = RtcRead (RTC_ADDRESS_HOURS_ALARM);\r
501 Time->Day = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);\r
502 Time->Month = RtcRead (RTC_ADDRESS_MONTH);\r
503 Time->Year = RtcRead (RTC_ADDRESS_YEAR);\r
504 Time->TimeZone = Global->SavedTimeZone;\r
505 Time->Daylight = Global->Daylight;\r
506\r
507 Century = RtcRead (RTC_ADDRESS_CENTURY);\r
508\r
509 //\r
510 // Get the alarm info from variable\r
511 //\r
512 Status = EfiGetVariable (\r
513 L"RTCALARM",\r
514 &gEfiCallerIdGuid,\r
515 NULL,\r
516 &DataSize,\r
517 &RtcTime\r
518 );\r
519 if (!EFI_ERROR (Status)) {\r
520 //\r
521 // The alarm variable exists. In this case, we read variable to get info.\r
522 //\r
523 Time->Day = RtcTime.Day;\r
524 Time->Month = RtcTime.Month;\r
525 Time->Year = RtcTime.Year;\r
526 }\r
527\r
528 //\r
529 // Release RTC Lock.\r
530 //\r
531 if (!EfiAtRuntime ()) {\r
532 EfiReleaseLock (&Global->RtcLock);\r
533 }\r
534\r
535 //\r
536 // Make sure all field values are in correct range\r
537 //\r
538 Status = ConvertRtcTimeToEfiTime (Time, Century, RegisterB);\r
539 if (!EFI_ERROR (Status)) {\r
540 Status = RtcTimeFieldsValid (Time);\r
541 }\r
542 if (EFI_ERROR (Status)) {\r
543 return EFI_DEVICE_ERROR;\r
544 }\r
545\r
546 return EFI_SUCCESS;\r
547}\r
548\r
549/**\r
550 Sets the system wakeup alarm clock time.\r
551\r
552 @param Enabled Enable or disable the wakeup alarm.\r
553 @param Time If Enable is TRUE, the time to set the wakeup alarm for.\r
554 If Enable is FALSE, then this parameter is optional, and may be NULL.\r
555 @param Global For global use inside this module.\r
556\r
557 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled.\r
558 If Enable is FALSE, then the wakeup alarm was disabled.\r
559 @retval EFI_INVALID_PARAMETER A time field is out of range.\r
560 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.\r
561 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.\r
562\r
563**/\r
564EFI_STATUS\r
565EFIAPI\r
566PcRtcSetWakeupTime (\r
567 IN BOOLEAN Enable,\r
568 IN EFI_TIME *Time, OPTIONAL\r
569 IN PC_RTC_MODULE_GLOBALS *Global\r
570 )\r
571{\r
572 EFI_STATUS Status;\r
573 EFI_TIME RtcTime;\r
574 RTC_REGISTER_B RegisterB;\r
575 UINT8 Century;\r
576 EFI_TIME_CAPABILITIES Capabilities;\r
577\r
578 ZeroMem (RtcTime);\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 Status = RtcTimeFieldsValid (&RtcTime);\r
597 if (EFI_ERROR (Status)) {\r
598 return EFI_DEVICE_ERROR;\r
599 }\r
600 if (!IsWithinOneDay (&RtcTime, Time)) {\r
601 return EFI_UNSUPPORTED;\r
602 }\r
603 //\r
604 // Make a local copy of the time and date\r
605 //\r
606 CopyMem (&RtcTime, Time, sizeof (EFI_TIME));\r
607\r
608 }\r
609 //\r
610 // Acquire RTC Lock to make access to RTC atomic\r
611 //\r
612 if (!EfiAtRuntime ()) {\r
613 EfiAcquireLock (&Global->RtcLock);\r
614 }\r
615 //\r
616 // Wait for up to 0.1 seconds for the RTC to be updated\r
617 //\r
618 Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
619 if (EFI_ERROR (Status)) {\r
620 if (!EfiAtRuntime ()) {\r
621 EfiReleaseLock (&Global->RtcLock);\r
622 }\r
623 return EFI_DEVICE_ERROR;\r
624 }\r
625 //\r
626 // Read Register B, and inhibit updates of the RTC\r
627 //\r
628 RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);\r
629\r
630 RegisterB.Bits.Set = 1;\r
631 RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
632\r
633 if (Enable) {\r
634 ConvertEfiTimeToRtcTime (&RtcTime, RegisterB, &Century);\r
635\r
636 //\r
637 // Set RTC alarm time\r
638 //\r
639 RtcWrite (RTC_ADDRESS_SECONDS_ALARM, RtcTime.Second);\r
640 RtcWrite (RTC_ADDRESS_MINUTES_ALARM, RtcTime.Minute);\r
641 RtcWrite (RTC_ADDRESS_HOURS_ALARM, RtcTime.Hour);\r
642\r
643 RegisterB.Bits.Aie = 1;\r
644\r
645 } else {\r
646 RegisterB.Bits.Aie = 0;\r
647 //\r
648 // if the alarm is disable, record the current setting.\r
649 //\r
650 RtcTime.Second = RtcRead (RTC_ADDRESS_SECONDS_ALARM);\r
651 RtcTime.Minute = RtcRead (RTC_ADDRESS_MINUTES_ALARM);\r
652 RtcTime.Hour = RtcRead (RTC_ADDRESS_HOURS_ALARM);\r
653 RtcTime.Day = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);\r
654 RtcTime.Month = RtcRead (RTC_ADDRESS_MONTH);\r
655 RtcTime.Year = RtcRead (RTC_ADDRESS_YEAR);\r
656 RtcTime.TimeZone = Global->SavedTimeZone;\r
657 RtcTime.Daylight = Global->Daylight;\r
658 }\r
659 //\r
660 // Allow updates of the RTC registers\r
661 //\r
662 RegisterB.Bits.Set = 0;\r
663 RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
664\r
665 //\r
666 // Set the Y/M/D info to variable as it has no corresponding hw registers.\r
667 //\r
668 Status = EfiSetVariable (\r
669 L"RTCALARM",\r
670 &gEfiCallerIdGuid,\r
671 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
672 sizeof (RtcTime),\r
673 &RtcTime\r
674 );\r
675 if (EFI_ERROR (Status)) {\r
676 return EFI_DEVICE_ERROR;\r
677 }\r
678\r
679 //\r
680 // Release RTC Lock.\r
681 //\r
682 if (!EfiAtRuntime ()) {\r
683 EfiReleaseLock (&Global->RtcLock);\r
684 }\r
685 return EFI_SUCCESS;\r
686}\r
687\r
688\r
689/**\r
690 Checks an 8-bit BCD value, and converts to an 8-bit value if valid.\r
691\r
692 This function checks the 8-bit BCD value specified by Value.\r
693 If valid, the function converts it to an 8-bit value and returns it.\r
694 Otherwise, return 0xff.\r
695\r
696 @param Value The 8-bit BCD value to check and convert\r
697\r
698 @return The 8-bit value converted. Or 0xff if Value is invalid.\r
699\r
700**/\r
701UINT8\r
702CheckAndConvertBcd8ToDecimal8 (\r
703 IN UINT8 Value\r
704 )\r
705{\r
706 if ((Value < 0xa0) && ((Value & 0xf) < 0xa)) {\r
707 return BcdToDecimal8 (Value);\r
708 }\r
709\r
710 return 0xff;\r
711}\r
712\r
713/**\r
714 Converts time read from RTC to EFI_TIME format defined by UEFI spec.\r
715\r
716 This function converts raw time data read from RTC to the EFI_TIME format\r
717 defined by UEFI spec.\r
718 If data mode of RTC is BCD, then converts it to decimal,\r
719 If RTC is in 12-hour format, then converts it to 24-hour format.\r
720\r
721 @param Time On input, the time data read from RTC to convert\r
722 On output, the time converted to UEFI format\r
723 @param Century Value of century read from RTC.\r
724 @param RegisterB Value of Register B of RTC, indicating data mode\r
725 and hour format.\r
726\r
727 @retval EFI_INVALID_PARAMETER Parameters passed in are invalid.\r
728 @retval EFI_SUCCESS Convert RTC time to EFI time successfully.\r
729\r
730**/\r
731EFI_STATUS\r
732ConvertRtcTimeToEfiTime (\r
733 IN OUT EFI_TIME *Time,\r
734 IN UINT8 Century,\r
735 IN RTC_REGISTER_B RegisterB\r
736 )\r
737{\r
738 BOOLEAN IsPM;\r
739\r
740 if ((Time->Hour & 0x80) != 0) {\r
741 IsPM = TRUE;\r
742 } else {\r
743 IsPM = FALSE;\r
744 }\r
745\r
746 Time->Hour = (UINT8) (Time->Hour & 0x7f);\r
747\r
748 if (RegisterB.Bits.Dm == 0) {\r
749 Time->Year = CheckAndConvertBcd8ToDecimal8 ((UINT8) Time->Year);\r
750 Time->Month = CheckAndConvertBcd8ToDecimal8 (Time->Month);\r
751 Time->Day = CheckAndConvertBcd8ToDecimal8 (Time->Day);\r
752 Time->Hour = CheckAndConvertBcd8ToDecimal8 (Time->Hour);\r
753 Time->Minute = CheckAndConvertBcd8ToDecimal8 (Time->Minute);\r
754 Time->Second = CheckAndConvertBcd8ToDecimal8 (Time->Second);\r
755 }\r
756 Century = CheckAndConvertBcd8ToDecimal8 (Century);\r
757\r
758 if (Time->Year == 0xff || Time->Month == 0xff || Time->Day == 0xff ||\r
759 Time->Hour == 0xff || Time->Minute == 0xff || Time->Second == 0xff ||\r
760 Century == 0xff) {\r
761 return EFI_INVALID_PARAMETER;\r
762 }\r
763\r
764 Time->Year = (UINT16) (Century * 100 + Time->Year);\r
765\r
766 //\r
767 // If time is in 12 hour format, convert it to 24 hour format\r
768 //\r
769 if (RegisterB.Bits.Mil == 0) {\r
770 if (IsPM && Time->Hour < 12) {\r
771 Time->Hour = (UINT8) (Time->Hour + 12);\r
772 }\r
773\r
774 if (!IsPM && Time->Hour == 12) {\r
775 Time->Hour = 0;\r
776 }\r
777 }\r
778\r
779 Time->Nanosecond = 0;\r
780\r
781 return EFI_SUCCESS;\r
782}\r
783\r
784/**\r
785 Wait for a period for the RTC to be ready.\r
786\r
787 @param Timeout Tell how long it should take to wait.\r
788\r
789 @retval EFI_DEVICE_ERROR RTC device error.\r
790 @retval EFI_SUCCESS RTC is updated and ready. \r
791**/\r
792EFI_STATUS\r
793RtcWaitToUpdate (\r
794 UINTN Timeout\r
795 )\r
796{\r
797 RTC_REGISTER_A RegisterA;\r
798 RTC_REGISTER_D RegisterD;\r
799\r
800 //\r
801 // See if the RTC is functioning correctly\r
802 //\r
803 RegisterD.Data = RtcRead (RTC_ADDRESS_REGISTER_D);\r
804\r
805 if (RegisterD.Bits.Vrt == 0) {\r
806 return EFI_DEVICE_ERROR;\r
807 }\r
808 //\r
809 // Wait for up to 0.1 seconds for the RTC to be ready.\r
810 //\r
811 Timeout = (Timeout / 10) + 1;\r
812 RegisterA.Data = RtcRead (RTC_ADDRESS_REGISTER_A);\r
813 while (RegisterA.Bits.Uip == 1 && Timeout > 0) {\r
814 MicroSecondDelay (10);\r
815 RegisterA.Data = RtcRead (RTC_ADDRESS_REGISTER_A);\r
816 Timeout--;\r
817 }\r
818\r
819 RegisterD.Data = RtcRead (RTC_ADDRESS_REGISTER_D);\r
820 if (Timeout == 0 || RegisterD.Bits.Vrt == 0) {\r
821 return EFI_DEVICE_ERROR;\r
822 }\r
823\r
824 return EFI_SUCCESS;\r
825}\r
826\r
827/**\r
828 See if all fields of a variable of EFI_TIME type is correct.\r
829\r
830 @param Time The time to be checked.\r
831\r
832 @retval EFI_INVALID_PARAMETER Some fields of Time are not correct.\r
833 @retval EFI_SUCCESS Time is a valid EFI_TIME variable.\r
834\r
835**/\r
836EFI_STATUS\r
837RtcTimeFieldsValid (\r
838 IN EFI_TIME *Time\r
839 )\r
840{\r
841 if (Time->Year < 1998 ||\r
842 Time->Year > 2099 ||\r
843 Time->Month < 1 ||\r
844 Time->Month > 12 ||\r
845 (!DayValid (Time)) ||\r
846 Time->Hour > 23 ||\r
847 Time->Minute > 59 ||\r
848 Time->Second > 59 ||\r
849 Time->Nanosecond > 999999999 ||\r
850 (!(Time->TimeZone == EFI_UNSPECIFIED_TIMEZONE || (Time->TimeZone >= -1440 && Time->TimeZone <= 1440))) ||\r
851 ((Time->Daylight & (~(EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT))) != 0)) {\r
852 return EFI_INVALID_PARAMETER;\r
853 }\r
854\r
855 return EFI_SUCCESS;\r
856}\r
857\r
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
866BOOLEAN\r
867DayValid (\r
868 IN EFI_TIME *Time\r
869 )\r
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 //\r
887 // The validity of Time->Month field should be checked before\r
888 //\r
889 ASSERT (Time->Month >=1);\r
890 ASSERT (Time->Month <=12);\r
891 if (Time->Day < 1 ||\r
892 Time->Day > DayOfMonth[Time->Month - 1] ||\r
893 (Time->Month == 2 && (!IsLeapYear (Time) && Time->Day > 28))\r
894 ) {\r
895 return FALSE;\r
896 }\r
897\r
898 return TRUE;\r
899}\r
900\r
901/**\r
902 Check if it is a leap year.\r
903\r
904 @param Time The time to be checked.\r
905\r
906 @retval TRUE It is a leap year.\r
907 @retval FALSE It is NOT a leap year.\r
908**/\r
909BOOLEAN\r
910IsLeapYear (\r
911 IN EFI_TIME *Time\r
912 )\r
913{\r
914 if (Time->Year % 4 == 0) {\r
915 if (Time->Year % 100 == 0) {\r
916 if (Time->Year % 400 == 0) {\r
917 return TRUE;\r
918 } else {\r
919 return FALSE;\r
920 }\r
921 } else {\r
922 return TRUE;\r
923 }\r
924 } else {\r
925 return FALSE;\r
926 }\r
927}\r
928\r
929/**\r
930 Converts time from EFI_TIME format defined by UEFI spec to RTC's.\r
931\r
932 This function converts time from EFI_TIME format defined by UEFI spec to RTC's.\r
933 If data mode of RTC is BCD, then converts EFI_TIME to it.\r
934 If RTC is in 12-hour format, then converts EFI_TIME to it.\r
935\r
936 @param Time On input, the time data read from UEFI to convert\r
937 On output, the time converted to RTC format\r
938 @param RegisterB Value of Register B of RTC, indicating data mode\r
939 @param Century It is set according to EFI_TIME Time.\r
940\r
941**/\r
942VOID\r
943ConvertEfiTimeToRtcTime (\r
944 IN OUT EFI_TIME *Time,\r
945 IN RTC_REGISTER_B RegisterB,\r
946 OUT UINT8 *Century\r
947 )\r
948{\r
949 BOOLEAN IsPM;\r
950\r
951 IsPM = TRUE;\r
952 //\r
953 // Adjust hour field if RTC is in 12 hour mode\r
954 //\r
955 if (RegisterB.Bits.Mil == 0) {\r
956 if (Time->Hour < 12) {\r
957 IsPM = FALSE;\r
958 }\r
959\r
960 if (Time->Hour >= 13) {\r
961 Time->Hour = (UINT8) (Time->Hour - 12);\r
962 } else if (Time->Hour == 0) {\r
963 Time->Hour = 12;\r
964 }\r
965 }\r
966 //\r
967 // Set the Time/Date/Daylight Savings values.\r
968 //\r
969 *Century = DecimalToBcd8 ((UINT8) (Time->Year / 100));\r
970\r
971 Time->Year = (UINT16) (Time->Year % 100);\r
972\r
973 if (RegisterB.Bits.Dm == 0) {\r
974 Time->Year = DecimalToBcd8 ((UINT8) Time->Year);\r
975 Time->Month = DecimalToBcd8 (Time->Month);\r
976 Time->Day = DecimalToBcd8 (Time->Day);\r
977 Time->Hour = DecimalToBcd8 (Time->Hour);\r
978 Time->Minute = DecimalToBcd8 (Time->Minute);\r
979 Time->Second = DecimalToBcd8 (Time->Second);\r
980 }\r
981 //\r
982 // If we are in 12 hour mode and PM is set, then set bit 7 of the Hour field.\r
983 //\r
984 if (RegisterB.Bits.Mil == 0 && IsPM) {\r
985 Time->Hour = (UINT8) (Time->Hour | 0x80);\r
986 }\r
987}\r
988\r
989/**\r
990 Compare the Hour, Minute and Second of the From time and the To time.\r
991 \r
992 Only compare H/M/S in EFI_TIME and ignore other fields here.\r
993\r
994 @param From the first time\r
995 @param To the second time\r
996\r
997 @return >0 The H/M/S of the From time is later than those of To time\r
998 @return ==0 The H/M/S of the From time is same as those of To time\r
999 @return <0 The H/M/S of the From time is earlier than those of To time\r
1000**/\r
1001INTN\r
1002CompareHMS (\r
1003 IN EFI_TIME *From,\r
1004 IN EFI_TIME *To\r
1005 )\r
1006{\r
1007 if ((From->Hour > To->Hour) ||\r
1008 ((From->Hour == To->Hour) && (From->Minute > To->Minute)) ||\r
1009 ((From->Hour == To->Hour) && (From->Minute == To->Minute) && (From->Second > To->Second))) {\r
1010 return 1;\r
1011 } else if ((From->Hour == To->Hour) && (From->Minute == To->Minute) && (From->Second == To->Second)) {\r
1012 return 0;\r
1013 } else {\r
1014 return -1;\r
1015 }\r
1016}\r
1017\r
1018/**\r
1019 To check if second date is later than first date within 24 hours.\r
1020\r
1021 @param From the first date\r
1022 @param To the second date\r
1023\r
1024 @retval TRUE From is previous to To within 24 hours.\r
1025 @retval FALSE From is later, or it is previous to To more than 24 hours.\r
1026**/\r
1027BOOLEAN\r
1028IsWithinOneDay (\r
1029 IN EFI_TIME *From,\r
1030 IN EFI_TIME *To\r
1031 )\r
1032{\r
1033 UINT8 DayOfMonth[12];\r
1034 BOOLEAN Adjacent;\r
1035\r
1036 DayOfMonth[0] = 31;\r
1037 DayOfMonth[1] = 29;\r
1038 DayOfMonth[2] = 31;\r
1039 DayOfMonth[3] = 30;\r
1040 DayOfMonth[4] = 31;\r
1041 DayOfMonth[5] = 30;\r
1042 DayOfMonth[6] = 31;\r
1043 DayOfMonth[7] = 31;\r
1044 DayOfMonth[8] = 30;\r
1045 DayOfMonth[9] = 31;\r
1046 DayOfMonth[10] = 30;\r
1047 DayOfMonth[11] = 31;\r
1048\r
1049 Adjacent = FALSE;\r
1050\r
1051 //\r
1052 // The validity of From->Month field should be checked before\r
1053 //\r
1054 ASSERT (From->Month >=1);\r
1055 ASSERT (From->Month <=12);\r
1056 \r
1057 if (From->Year == To->Year) {\r
1058 if (From->Month == To->Month) {\r
1059 if ((From->Day + 1) == To->Day) {\r
1060 if ((CompareHMS(From, To) >= 0)) {\r
1061 Adjacent = TRUE;\r
1062 }\r
1063 } else if (From->Day == To->Day) {\r
1064 if ((CompareHMS(From, To) <= 0)) {\r
1065 Adjacent = TRUE;\r
1066 }\r
1067 }\r
1068 } else if (((From->Month + 1) == To->Month) && (To->Day == 1)) {\r
1069 if ((From->Month == 2) && !IsLeapYear(From)) {\r
1070 if (From->Day == 28) {\r
1071 if ((CompareHMS(From, To) >= 0)) {\r
1072 Adjacent = TRUE;\r
1073 }\r
1074 }\r
1075 } else if (From->Day == DayOfMonth[From->Month - 1]) {\r
1076 if ((CompareHMS(From, To) >= 0)) {\r
1077 Adjacent = TRUE;\r
1078 }\r
1079 }\r
1080 }\r
1081 } else if (((From->Year + 1) == To->Year) &&\r
1082 (From->Month == 12) &&\r
1083 (From->Day == 31) &&\r
1084 (To->Month == 1) &&\r
1085 (To->Day == 1)) {\r
1086 if ((CompareHMS(From, To) >= 0)) {\r
1087 Adjacent = TRUE;\r
1088 }\r
1089 }\r
1090\r
1091 return Adjacent;\r
1092}\r
1093\r