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