]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.c
ArmPlatformPkg/PL031RealTimeClock: Fixed driver to support UEFI Runtime Services
[mirror_edk2.git] / ArmPlatformPkg / Library / PL031RealTimeClockLib / PL031RealTimeClockLib.c
1 /** @file
2 Implement EFI RealTimeClock runtime services via RTC Lib.
3
4 Currently this driver does not support runtime virtual calling.
5
6 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
7 Copyright (c) 2011 - 2014, ARM Ltd. All rights reserved.<BR>
8
9 This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
13
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16
17 **/
18
19 #include <Uefi.h>
20 #include <PiDxe.h>
21 #include <Library/BaseLib.h>
22 #include <Library/DebugLib.h>
23 #include <Library/UefiLib.h>
24 #include <Library/IoLib.h>
25 #include <Library/RealTimeClockLib.h>
26 #include <Library/MemoryAllocationLib.h>
27 #include <Library/PcdLib.h>
28 #include <Library/ArmPlatformSysConfigLib.h>
29 #include <Library/DxeServicesTableLib.h>
30 #include <Library/UefiBootServicesTableLib.h>
31 #include <Library/UefiRuntimeServicesTableLib.h>
32 #include <Library/UefiRuntimeLib.h>
33
34 #include <Protocol/RealTimeClock.h>
35
36 #include <Guid/GlobalVariable.h>
37 #include <Guid/EventGroup.h>
38
39 #include <Drivers/PL031RealTimeClock.h>
40
41 #include <ArmPlatform.h>
42
43 STATIC CONST CHAR16 mTimeZoneVariableName[] = L"PL031RtcTimeZone";
44 STATIC CONST CHAR16 mDaylightVariableName[] = L"PL031RtcDaylight";
45 STATIC BOOLEAN mPL031Initialized = FALSE;
46 STATIC EFI_EVENT mRtcVirtualAddrChangeEvent;
47 STATIC UINTN mPL031RtcBase;
48
49 EFI_STATUS
50 IdentifyPL031 (
51 VOID
52 )
53 {
54 EFI_STATUS Status;
55
56 // Check if this is a PrimeCell Peripheral
57 if ( (MmioRead8 (mPL031RtcBase + PL031_RTC_PCELL_ID0) != 0x0D)
58 || (MmioRead8 (mPL031RtcBase + PL031_RTC_PCELL_ID1) != 0xF0)
59 || (MmioRead8 (mPL031RtcBase + PL031_RTC_PCELL_ID2) != 0x05)
60 || (MmioRead8 (mPL031RtcBase + PL031_RTC_PCELL_ID3) != 0xB1)) {
61 Status = EFI_NOT_FOUND;
62 goto EXIT;
63 }
64
65 // Check if this PrimeCell Peripheral is the PL031 Real Time Clock
66 if ( (MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID0) != 0x31)
67 || (MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID1) != 0x10)
68 || ((MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID2) & 0xF) != 0x04)
69 || (MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID3) != 0x00)) {
70 Status = EFI_NOT_FOUND;
71 goto EXIT;
72 }
73
74 Status = EFI_SUCCESS;
75
76 EXIT:
77 return Status;
78 }
79
80 EFI_STATUS
81 InitializePL031 (
82 VOID
83 )
84 {
85 EFI_STATUS Status;
86
87 // Prepare the hardware
88 Status = IdentifyPL031();
89 if (EFI_ERROR (Status)) {
90 goto EXIT;
91 }
92
93 // Ensure interrupts are masked. We do not want RTC interrupts in UEFI
94 if ((MmioRead32 (mPL031RtcBase + PL031_RTC_IMSC_IRQ_MASK_SET_CLEAR_REGISTER) & PL031_SET_IRQ_MASK) != PL031_SET_IRQ_MASK) {
95 MmioOr32 (mPL031RtcBase + PL031_RTC_IMSC_IRQ_MASK_SET_CLEAR_REGISTER, PL031_SET_IRQ_MASK);
96 }
97
98 // Clear any existing interrupts
99 if ((MmioRead32 (mPL031RtcBase + PL031_RTC_RIS_RAW_IRQ_STATUS_REGISTER) & PL031_IRQ_TRIGGERED) == PL031_IRQ_TRIGGERED) {
100 MmioOr32 (mPL031RtcBase + PL031_RTC_ICR_IRQ_CLEAR_REGISTER, PL031_CLEAR_IRQ);
101 }
102
103 // Start the clock counter
104 if ((MmioRead32 (mPL031RtcBase + PL031_RTC_CR_CONTROL_REGISTER) & PL031_RTC_ENABLED) != PL031_RTC_ENABLED) {
105 MmioOr32 (mPL031RtcBase + PL031_RTC_CR_CONTROL_REGISTER, PL031_RTC_ENABLED);
106 }
107
108 mPL031Initialized = TRUE;
109
110 EXIT:
111 return Status;
112 }
113
114 /**
115 Converts Epoch seconds (elapsed since 1970 JANUARY 01, 00:00:00 UTC) to EFI_TIME
116 **/
117 VOID
118 EpochToEfiTime (
119 IN UINTN EpochSeconds,
120 OUT EFI_TIME *Time
121 )
122 {
123 UINTN a;
124 UINTN b;
125 UINTN c;
126 UINTN d;
127 UINTN g;
128 UINTN j;
129 UINTN m;
130 UINTN y;
131 UINTN da;
132 UINTN db;
133 UINTN dc;
134 UINTN dg;
135 UINTN hh;
136 UINTN mm;
137 UINTN ss;
138 UINTN J;
139
140 J = (EpochSeconds / 86400) + 2440588;
141 j = J + 32044;
142 g = j / 146097;
143 dg = j % 146097;
144 c = (((dg / 36524) + 1) * 3) / 4;
145 dc = dg - (c * 36524);
146 b = dc / 1461;
147 db = dc % 1461;
148 a = (((db / 365) + 1) * 3) / 4;
149 da = db - (a * 365);
150 y = (g * 400) + (c * 100) + (b * 4) + a;
151 m = (((da * 5) + 308) / 153) - 2;
152 d = da - (((m + 4) * 153) / 5) + 122;
153
154 Time->Year = y - 4800 + ((m + 2) / 12);
155 Time->Month = ((m + 2) % 12) + 1;
156 Time->Day = d + 1;
157
158 ss = EpochSeconds % 60;
159 a = (EpochSeconds - ss) / 60;
160 mm = a % 60;
161 b = (a - mm) / 60;
162 hh = b % 24;
163
164 Time->Hour = hh;
165 Time->Minute = mm;
166 Time->Second = ss;
167 Time->Nanosecond = 0;
168
169 }
170
171 /**
172 Converts EFI_TIME to Epoch seconds (elapsed since 1970 JANUARY 01, 00:00:00 UTC)
173 **/
174 UINTN
175 EfiTimeToEpoch (
176 IN EFI_TIME *Time
177 )
178 {
179 UINTN a;
180 UINTN y;
181 UINTN m;
182 UINTN JulianDate; // Absolute Julian Date representation of the supplied Time
183 UINTN EpochDays; // Number of days elapsed since EPOCH_JULIAN_DAY
184 UINTN EpochSeconds;
185
186 a = (14 - Time->Month) / 12 ;
187 y = Time->Year + 4800 - a;
188 m = Time->Month + (12*a) - 3;
189
190 JulianDate = Time->Day + ((153*m + 2)/5) + (365*y) + (y/4) - (y/100) + (y/400) - 32045;
191
192 ASSERT (JulianDate >= EPOCH_JULIAN_DATE);
193 EpochDays = JulianDate - EPOCH_JULIAN_DATE;
194
195 EpochSeconds = (EpochDays * SEC_PER_DAY) + ((UINTN)Time->Hour * SEC_PER_HOUR) + (Time->Minute * SEC_PER_MIN) + Time->Second;
196
197 return EpochSeconds;
198 }
199
200 BOOLEAN
201 IsLeapYear (
202 IN EFI_TIME *Time
203 )
204 {
205 if (Time->Year % 4 == 0) {
206 if (Time->Year % 100 == 0) {
207 if (Time->Year % 400 == 0) {
208 return TRUE;
209 } else {
210 return FALSE;
211 }
212 } else {
213 return TRUE;
214 }
215 } else {
216 return FALSE;
217 }
218 }
219
220 BOOLEAN
221 DayValid (
222 IN EFI_TIME *Time
223 )
224 {
225 INTN DayOfMonth[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
226
227 if (Time->Day < 1 ||
228 Time->Day > DayOfMonth[Time->Month - 1] ||
229 (Time->Month == 2 && (!IsLeapYear (Time) && Time->Day > 28))
230 ) {
231 return FALSE;
232 }
233
234 return TRUE;
235 }
236
237 /**
238 Returns the current time and date information, and the time-keeping capabilities
239 of the hardware platform.
240
241 @param Time A pointer to storage to receive a snapshot of the current time.
242 @param Capabilities An optional pointer to a buffer to receive the real time clock
243 device's capabilities.
244
245 @retval EFI_SUCCESS The operation completed successfully.
246 @retval EFI_INVALID_PARAMETER Time is NULL.
247 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
248 @retval EFI_SECURITY_VIOLATION The time could not be retrieved due to an authentication failure.
249
250 **/
251 EFI_STATUS
252 EFIAPI
253 LibGetTime (
254 OUT EFI_TIME *Time,
255 OUT EFI_TIME_CAPABILITIES *Capabilities
256 )
257 {
258 EFI_STATUS Status = EFI_SUCCESS;
259 UINT32 EpochSeconds;
260 INT16 TimeZone;
261 UINT8 Daylight;
262 UINTN Size;
263
264 // Initialize the hardware if not already done
265 if (!mPL031Initialized) {
266 Status = InitializePL031 ();
267 if (EFI_ERROR (Status)) {
268 goto EXIT;
269 }
270 }
271
272 // Snapshot the time as early in the function call as possible
273 // On some platforms we may have access to a battery backed up hardware clock.
274 // If such RTC exists try to use it first.
275 Status = ArmPlatformSysConfigGet (SYS_CFG_RTC, &EpochSeconds);
276 if (Status == EFI_UNSUPPORTED) {
277 // Battery backed up hardware RTC does not exist, revert to PL031
278 EpochSeconds = MmioRead32 (mPL031RtcBase + PL031_RTC_DR_DATA_REGISTER);
279 Status = EFI_SUCCESS;
280 } else if (EFI_ERROR (Status)) {
281 // Battery backed up hardware RTC exists but could not be read due to error. Abort.
282 goto EXIT;
283 } else {
284 // Battery backed up hardware RTC exists and we read the time correctly from it.
285 // Now sync the PL031 to the new time.
286 MmioWrite32 (mPL031RtcBase + PL031_RTC_LR_LOAD_REGISTER, EpochSeconds);
287 }
288
289 // Ensure Time is a valid pointer
290 if (Time == NULL) {
291 Status = EFI_INVALID_PARAMETER;
292 goto EXIT;
293 }
294
295 // Get the current time zone information from non-volatile storage
296 Size = sizeof (TimeZone);
297 Status = gRT->GetVariable (
298 (CHAR16 *)mTimeZoneVariableName,
299 &gEfiCallerIdGuid,
300 NULL,
301 &Size,
302 (VOID *)&TimeZone
303 );
304
305 if (EFI_ERROR (Status)) {
306 ASSERT(Status != EFI_INVALID_PARAMETER);
307 ASSERT(Status != EFI_BUFFER_TOO_SMALL);
308
309 if (Status != EFI_NOT_FOUND)
310 goto EXIT;
311
312 // The time zone variable does not exist in non-volatile storage, so create it.
313 Time->TimeZone = EFI_UNSPECIFIED_TIMEZONE;
314 // Store it
315 Status = gRT->SetVariable (
316 (CHAR16 *)mTimeZoneVariableName,
317 &gEfiCallerIdGuid,
318 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
319 Size,
320 (VOID *)&(Time->TimeZone)
321 );
322 if (EFI_ERROR (Status)) {
323 DEBUG ((
324 EFI_D_ERROR,
325 "LibGetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
326 mTimeZoneVariableName,
327 Status
328 ));
329 goto EXIT;
330 }
331 } else {
332 // Got the time zone
333 Time->TimeZone = TimeZone;
334
335 // Check TimeZone bounds: -1440 to 1440 or 2047
336 if (((Time->TimeZone < -1440) || (Time->TimeZone > 1440))
337 && (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE)) {
338 Time->TimeZone = EFI_UNSPECIFIED_TIMEZONE;
339 }
340
341 // Adjust for the correct time zone
342 if (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {
343 EpochSeconds += Time->TimeZone * SEC_PER_MIN;
344 }
345 }
346
347 // Get the current daylight information from non-volatile storage
348 Size = sizeof (Daylight);
349 Status = gRT->GetVariable (
350 (CHAR16 *)mDaylightVariableName,
351 &gEfiCallerIdGuid,
352 NULL,
353 &Size,
354 (VOID *)&Daylight
355 );
356
357 if (EFI_ERROR (Status)) {
358 ASSERT(Status != EFI_INVALID_PARAMETER);
359 ASSERT(Status != EFI_BUFFER_TOO_SMALL);
360
361 if (Status != EFI_NOT_FOUND)
362 goto EXIT;
363
364 // The daylight variable does not exist in non-volatile storage, so create it.
365 Time->Daylight = 0;
366 // Store it
367 Status = gRT->SetVariable (
368 (CHAR16 *)mDaylightVariableName,
369 &gEfiCallerIdGuid,
370 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
371 Size,
372 (VOID *)&(Time->Daylight)
373 );
374 if (EFI_ERROR (Status)) {
375 DEBUG ((
376 EFI_D_ERROR,
377 "LibGetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
378 mDaylightVariableName,
379 Status
380 ));
381 goto EXIT;
382 }
383 } else {
384 // Got the daylight information
385 Time->Daylight = Daylight;
386
387 // Adjust for the correct period
388 if ((Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT) {
389 // Convert to adjusted time, i.e. spring forwards one hour
390 EpochSeconds += SEC_PER_HOUR;
391 }
392 }
393
394 // Convert from internal 32-bit time to UEFI time
395 EpochToEfiTime (EpochSeconds, Time);
396
397 // Update the Capabilities info
398 if (Capabilities != NULL) {
399 // PL031 runs at frequency 1Hz
400 Capabilities->Resolution = PL031_COUNTS_PER_SECOND;
401 // Accuracy in ppm multiplied by 1,000,000, e.g. for 50ppm set 50,000,000
402 Capabilities->Accuracy = (UINT32)PcdGet32 (PcdPL031RtcPpmAccuracy);
403 // FALSE: Setting the time does not clear the values below the resolution level
404 Capabilities->SetsToZero = FALSE;
405 }
406
407 EXIT:
408 return Status;
409 }
410
411
412 /**
413 Sets the current local time and date information.
414
415 @param Time A pointer to the current time.
416
417 @retval EFI_SUCCESS The operation completed successfully.
418 @retval EFI_INVALID_PARAMETER A time field is out of range.
419 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
420
421 **/
422 EFI_STATUS
423 EFIAPI
424 LibSetTime (
425 IN EFI_TIME *Time
426 )
427 {
428 EFI_STATUS Status;
429 UINTN EpochSeconds;
430
431 // Check the input parameters are within the range specified by UEFI
432 if ((Time->Year < 1900) ||
433 (Time->Year > 9999) ||
434 (Time->Month < 1 ) ||
435 (Time->Month > 12 ) ||
436 (!DayValid (Time) ) ||
437 (Time->Hour > 23 ) ||
438 (Time->Minute > 59 ) ||
439 (Time->Second > 59 ) ||
440 (Time->Nanosecond > 999999999) ||
441 (!((Time->TimeZone == EFI_UNSPECIFIED_TIMEZONE) || ((Time->TimeZone >= -1440) && (Time->TimeZone <= 1440)))) ||
442 (Time->Daylight & (~(EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT)))
443 ) {
444 Status = EFI_INVALID_PARAMETER;
445 goto EXIT;
446 }
447
448 // Because the PL031 is a 32-bit counter counting seconds,
449 // the maximum time span is just over 136 years.
450 // Time is stored in Unix Epoch format, so it starts in 1970,
451 // Therefore it can not exceed the year 2106.
452 if ((Time->Year < 1970) || (Time->Year >= 2106)) {
453 Status = EFI_UNSUPPORTED;
454 goto EXIT;
455 }
456
457 // Initialize the hardware if not already done
458 if (!mPL031Initialized) {
459 Status = InitializePL031 ();
460 if (EFI_ERROR (Status)) {
461 goto EXIT;
462 }
463 }
464
465 EpochSeconds = EfiTimeToEpoch (Time);
466
467 // Adjust for the correct time zone, i.e. convert to UTC time zone
468 if (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {
469 EpochSeconds -= Time->TimeZone * SEC_PER_MIN;
470 }
471
472 // TODO: Automatic Daylight activation
473
474 // Adjust for the correct period
475 if ((Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT) {
476 // Convert to un-adjusted time, i.e. fall back one hour
477 EpochSeconds -= SEC_PER_HOUR;
478 }
479
480 // On some platforms we may have access to a battery backed up hardware clock.
481 //
482 // If such RTC exists then it must be updated first, before the PL031,
483 // to minimise any time drift. This is important because the battery backed-up
484 // RTC maintains the master time for the platform across reboots.
485 //
486 // If such RTC does not exist then the following function returns UNSUPPORTED.
487 Status = ArmPlatformSysConfigSet (SYS_CFG_RTC, EpochSeconds);
488 if ((EFI_ERROR (Status)) && (Status != EFI_UNSUPPORTED)){
489 // Any status message except SUCCESS and UNSUPPORTED indicates a hardware failure.
490 goto EXIT;
491 }
492
493
494 // Set the PL031
495 MmioWrite32 (mPL031RtcBase + PL031_RTC_LR_LOAD_REGISTER, EpochSeconds);
496
497 // The accesses to Variable Services can be very slow, because we may be writing to Flash.
498 // Do this after having set the RTC.
499
500 // Save the current time zone information into non-volatile storage
501 Status = gRT->SetVariable (
502 (CHAR16 *)mTimeZoneVariableName,
503 &gEfiCallerIdGuid,
504 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
505 sizeof (Time->TimeZone),
506 (VOID *)&(Time->TimeZone)
507 );
508 if (EFI_ERROR (Status)) {
509 DEBUG ((
510 EFI_D_ERROR,
511 "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
512 mTimeZoneVariableName,
513 Status
514 ));
515 goto EXIT;
516 }
517
518 // Save the current daylight information into non-volatile storage
519 Status = gRT->SetVariable (
520 (CHAR16 *)mDaylightVariableName,
521 &gEfiCallerIdGuid,
522 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
523 sizeof(Time->Daylight),
524 (VOID *)&(Time->Daylight)
525 );
526 if (EFI_ERROR (Status)) {
527 DEBUG ((
528 EFI_D_ERROR,
529 "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
530 mDaylightVariableName,
531 Status
532 ));
533 goto EXIT;
534 }
535
536 EXIT:
537 return Status;
538 }
539
540
541 /**
542 Returns the current wakeup alarm clock setting.
543
544 @param Enabled Indicates if the alarm is currently enabled or disabled.
545 @param Pending Indicates if the alarm signal is pending and requires acknowledgement.
546 @param Time The current alarm setting.
547
548 @retval EFI_SUCCESS The alarm settings were returned.
549 @retval EFI_INVALID_PARAMETER Any parameter is NULL.
550 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
551
552 **/
553 EFI_STATUS
554 EFIAPI
555 LibGetWakeupTime (
556 OUT BOOLEAN *Enabled,
557 OUT BOOLEAN *Pending,
558 OUT EFI_TIME *Time
559 )
560 {
561 // Not a required feature
562 return EFI_UNSUPPORTED;
563 }
564
565
566 /**
567 Sets the system wakeup alarm clock time.
568
569 @param Enabled Enable or disable the wakeup alarm.
570 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
571
572 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If
573 Enable is FALSE, then the wakeup alarm was disabled.
574 @retval EFI_INVALID_PARAMETER A time field is out of range.
575 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
576 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
577
578 **/
579 EFI_STATUS
580 EFIAPI
581 LibSetWakeupTime (
582 IN BOOLEAN Enabled,
583 OUT EFI_TIME *Time
584 )
585 {
586 // Not a required feature
587 return EFI_UNSUPPORTED;
588 }
589
590 /**
591 Fixup internal data so that EFI can be call in virtual mode.
592 Call the passed in Child Notify event and convert any pointers in
593 lib to virtual mode.
594
595 @param[in] Event The Event that is being processed
596 @param[in] Context Event Context
597 **/
598 VOID
599 EFIAPI
600 LibRtcVirtualNotifyEvent (
601 IN EFI_EVENT Event,
602 IN VOID *Context
603 )
604 {
605 //
606 // Only needed if you are going to support the OS calling RTC functions in virtual mode.
607 // You will need to call EfiConvertPointer (). To convert any stored physical addresses
608 // to virtual address. After the OS transitions to calling in virtual mode, all future
609 // runtime calls will be made in virtual mode.
610 //
611 EfiConvertPointer (0x0, (VOID**)&mPL031RtcBase);
612 return;
613 }
614
615 /**
616 This is the declaration of an EFI image entry point. This can be the entry point to an application
617 written to this specification, an EFI boot service driver, or an EFI runtime driver.
618
619 @param ImageHandle Handle that identifies the loaded image.
620 @param SystemTable System Table for this image.
621
622 @retval EFI_SUCCESS The operation completed successfully.
623
624 **/
625 EFI_STATUS
626 EFIAPI
627 LibRtcInitialize (
628 IN EFI_HANDLE ImageHandle,
629 IN EFI_SYSTEM_TABLE *SystemTable
630 )
631 {
632 EFI_STATUS Status;
633 EFI_HANDLE Handle;
634
635 // Initialize RTC Base Address
636 mPL031RtcBase = PcdGet32 (PcdPL031RtcBase);
637
638 // Declare the controller as EFI_MEMORY_RUNTIME
639 Status = gDS->AddMemorySpace (
640 EfiGcdMemoryTypeMemoryMappedIo,
641 mPL031RtcBase, SIZE_4KB,
642 EFI_MEMORY_UC | EFI_MEMORY_RUNTIME
643 );
644 if (EFI_ERROR (Status)) {
645 return Status;
646 }
647
648 Status = gDS->SetMemorySpaceAttributes (mPL031RtcBase, SIZE_4KB, EFI_MEMORY_UC | EFI_MEMORY_RUNTIME);
649 if (EFI_ERROR (Status)) {
650 return Status;
651 }
652
653 // Setup the setters and getters
654 gRT->GetTime = LibGetTime;
655 gRT->SetTime = LibSetTime;
656 gRT->GetWakeupTime = LibGetWakeupTime;
657 gRT->SetWakeupTime = LibSetWakeupTime;
658
659 // Install the protocol
660 Handle = NULL;
661 Status = gBS->InstallMultipleProtocolInterfaces (
662 &Handle,
663 &gEfiRealTimeClockArchProtocolGuid, NULL,
664 NULL
665 );
666 ASSERT_EFI_ERROR (Status);
667
668 //
669 // Register for the virtual address change event
670 //
671 Status = gBS->CreateEventEx (
672 EVT_NOTIFY_SIGNAL,
673 TPL_NOTIFY,
674 LibRtcVirtualNotifyEvent,
675 NULL,
676 &gEfiEventVirtualAddressChangeGuid,
677 &mRtcVirtualAddrChangeEvent
678 );
679 ASSERT_EFI_ERROR (Status);
680
681 return Status;
682 }