TianoCore EDK2 master
Loading...
Searching...
No Matches
LsRealTimeClockLib.c
Go to the documentation of this file.
1
10#include <Library/DebugLib.h>
12#include <Library/HobLib.h>
13#include <Library/IoLib.h>
14#include <Library/PcdLib.h>
17
18#include "LsRealTimeClock.h"
19
20STATIC BOOLEAN mInitialized = FALSE;
21STATIC EFI_EVENT mRtcVirtualAddrChangeEvent;
22STATIC UINTN mRtcBase;
23
24/*
25 Enable Real-time clock.
26
27 @param VOID
28
29 @retval VOID
30 */
32VOID
33InitRtc (
34 VOID
35 )
36{
37 UINTN Val;
38 EFI_HOB_GUID_TYPE *GuidHob = NULL;
39 VOID *DataInHob = NULL;
40
41 if (!mInitialized) {
42 /* Enable rtc */
43 GuidHob = GetFirstGuidHob (&gRtcRegisterBaseAddressHobGuid);
44 if (GuidHob) {
45 DataInHob = GET_GUID_HOB_DATA (GuidHob);
46 mRtcBase = (UINT64)(*(UINTN *)DataInHob);
47 Val = MmioRead32 (mRtcBase + RTC_CTRL_REG);
48 Val |= TOY_ENABLE_BIT | OSC_ENABLE_BIT;
49 MmioWrite32 (mRtcBase + RTC_CTRL_REG, Val);
50 mInitialized = TRUE;
51 } else {
52 DebugPrint (DEBUG_INFO, "RTC register address not found!\n");
53 ASSERT (FALSE);
54 }
55 }
56}
57
72EFIAPI
74 OUT EFI_TIME *Time,
75 OUT EFI_TIME_CAPABILITIES *Capabilities
76 )
77{
78 UINT32 Val;
79
80 // Ensure Time is a valid pointer
81 if (Time == NULL) {
82 return EFI_INVALID_PARAMETER;
83 }
84
85 Val = MmioRead32 (mRtcBase + TOY_READ1_REG);
86 Time->Year = Val + 1900;
87
88 Val = MmioRead32 (mRtcBase + TOY_READ0_REG);
89 Time->Month = (Val >> TOY_MON_SHIFT) & TOY_MON_MASK;
90 Time->Day = (Val >> TOY_DAY_SHIFT) & TOY_DAY_MASK;
91 Time->Hour = (Val >> TOY_HOUR_SHIFT) & TOY_HOUR_MASK;
92 Time->Minute = (Val >> TOY_MIN_SHIFT) & TOY_MIN_MASK;
93 Time->Second = (Val >> TOY_SEC_SHIFT) & TOY_SEC_MASK;
94 Time->Nanosecond = 0;
95 return EFI_SUCCESS;
96}
97
108EFIAPI
110 IN EFI_TIME *Time
111 )
112{
113 UINT32 Val;
114
115 // Initialize the hardware if not already done
116
117 Val = 0;
118 Val |= (Time->Second << TOY_SEC_SHIFT);
119 Val |= (Time->Minute << TOY_MIN_SHIFT);
120 Val |= (Time->Hour << TOY_HOUR_SHIFT);
121 Val |= (Time->Day << TOY_DAY_SHIFT);
122 Val |= (Time->Month << TOY_MON_SHIFT);
123 MmioWrite32 (mRtcBase + TOY_WRITE0_REG, Val);
124
125 Val = Time->Year - 1900;
126 MmioWrite32 (mRtcBase + TOY_WRITE1_REG, Val);
127 return EFI_SUCCESS;
128}
129
142EFIAPI
144 OUT BOOLEAN *Enabled,
145 OUT BOOLEAN *Pending,
146 OUT EFI_TIME *Time
147 )
148{
149 // Not a required feature
150 return EFI_UNSUPPORTED;
151}
152
166EFIAPI
168 IN BOOLEAN Enabled,
169 OUT EFI_TIME *Time
170 )
171{
172 // Not a required feature
173 return EFI_UNSUPPORTED;
174}
175
184STATIC
185VOID
186EFIAPI
188 IN EFI_EVENT Event,
189 IN VOID *Context
190 )
191{
192 //
193 // Only needed if you are going to support the OS calling RTC functions in virtual mode.
194 // You will need to call EfiConvertPointer (). To convert any stored physical addresses
195 // to virtual address. After the OS transitions to calling in virtual mode, all future
196 // runtime calls will be made in virtual mode.
197 //
198 EfiConvertPointer (0x0, (VOID **)&mRtcBase);
199 return;
200}
201
211STATIC
214 IN EFI_HANDLE ImageHandle,
215 IN EFI_PHYSICAL_ADDRESS RtcPageBase
216 )
217{
218 EFI_STATUS Status;
219
220 Status = gDS->AddMemorySpace (
222 RtcPageBase,
223 EFI_PAGE_SIZE,
224 EFI_MEMORY_UC | EFI_MEMORY_RUNTIME
225 );
226 if (EFI_ERROR (Status)) {
227 DEBUG ((
228 DEBUG_ERROR,
229 "Failed to add memory space. Status = %r\n",
230 Status
231 ));
232 return Status;
233 }
234
235 Status = gDS->AllocateMemorySpace (
238 0,
239 EFI_PAGE_SIZE,
240 &RtcPageBase,
241 ImageHandle,
242 NULL
243 );
244 if (EFI_ERROR (Status)) {
245 DEBUG ((
246 DEBUG_ERROR,
247 "Failed to allocate memory space. Status = %r\n",
248 Status
249 ));
250 gDS->RemoveMemorySpace (
251 RtcPageBase,
252 EFI_PAGE_SIZE
253 );
254 return Status;
255 }
256
257 Status = gDS->SetMemorySpaceAttributes (
258 RtcPageBase,
259 EFI_PAGE_SIZE,
260 EFI_MEMORY_UC | EFI_MEMORY_RUNTIME
261 );
262 if (EFI_ERROR (Status)) {
263 DEBUG ((
264 DEBUG_ERROR,
265 "Failed to set memory attributes. Status = %r\n",
266 Status
267 ));
268
269 gDS->FreeMemorySpace (
270 RtcPageBase,
271 EFI_PAGE_SIZE
272 );
273
274 gDS->RemoveMemorySpace (
275 RtcPageBase,
276 EFI_PAGE_SIZE
277 );
278 }
279
280 return Status;
281}
282
293EFIAPI
295 IN EFI_HANDLE ImageHandle,
296 IN EFI_SYSTEM_TABLE *SystemTable
297 )
298{
299 EFI_STATUS Status;
300
301 InitRtc ();
302 Status = MapRtcResources (ImageHandle, (mRtcBase & ~EFI_PAGE_MASK));
303 if (EFI_ERROR (Status)) {
304 DEBUG ((
305 DEBUG_ERROR,
306 "Failed to map memory for loongson 7A RTC. Status = %r\n",
307 Status
308 ));
309 return Status;
310 }
311
312 //
313 // Register for the virtual address change event
314 //
315 Status = gBS->CreateEventEx (
316 EVT_NOTIFY_SIGNAL,
317 TPL_NOTIFY,
319 NULL,
320 &gEfiEventVirtualAddressChangeGuid,
321 &mRtcVirtualAddrChangeEvent
322 );
323 ASSERT_EFI_ERROR (Status);
324 return Status;
325}
UINT64 UINTN
VOID EFIAPI DebugPrint(IN UINTN ErrorLevel, IN CONST CHAR8 *Format,...)
Definition: DebugLib.c:45
VOID *EFIAPI GetFirstGuidHob(IN CONST EFI_GUID *Guid)
Definition: HobLib.c:215
EFI_DXE_SERVICES * gDS
UINT32 EFIAPI MmioRead32(IN UINTN Address)
Definition: IoLib.c:262
UINT32 EFIAPI MmioWrite32(IN UINTN Address, IN UINT32 Value)
Definition: IoLib.c:309
STATIC VOID EFIAPI VirtualNotifyEvent(IN EFI_EVENT Event, IN VOID *Context)
EFI_STATUS EFIAPI LibSetWakeupTime(IN BOOLEAN Enabled, OUT EFI_TIME *Time)
STATIC EFI_STATUS MapRtcResources(IN EFI_HANDLE ImageHandle, IN EFI_PHYSICAL_ADDRESS RtcPageBase)
EFI_STATUS EFIAPI LibRtcInitialize(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
EFI_STATUS EFIAPI LibGetWakeupTime(OUT BOOLEAN *Enabled, OUT BOOLEAN *Pending, OUT EFI_TIME *Time)
EFI_STATUS EFIAPI LibSetTime(IN EFI_TIME *Time)
EFI_STATUS EFIAPI LibGetTime(OUT EFI_TIME *Time, OUT EFI_TIME_CAPABILITIES *Capabilities)
#define NULL
Definition: Base.h:319
#define STATIC
Definition: Base.h:264
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
#define DEBUG(Expression)
Definition: DebugLib.h:434
@ EfiGcdMemoryTypeMemoryMappedIo
Definition: PiDxeCis.h:44
@ EfiGcdAllocateAddress
Definition: PiDxeCis.h:107
UINT64 EFI_PHYSICAL_ADDRESS
Definition: UefiBaseType.h:50
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_EVENT
Definition: UefiBaseType.h:37
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_BOOT_SERVICES * gBS
EFI_STATUS EFIAPI EfiConvertPointer(IN UINTN DebugDisposition, IN OUT VOID **Address)
Definition: RuntimeLib.c:561