From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from loongson.cn (loongson.cn [114.242.206.163]) by mx.groups.io with SMTP id smtpd.web10.3739.1668160053227486076 for ; Fri, 11 Nov 2022 01:47:34 -0800 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: loongson.cn, ip: 114.242.206.163, mailfrom: lichao@loongson.cn) Received: from loongson.cn (unknown [10.40.24.149]) by gateway (Coremail) with SMTP id _____8AxXbY0Gm5jmxYGAA--.7074S3; Fri, 11 Nov 2022 17:47:32 +0800 (CST) Received: from lichao-PC (unknown [10.40.24.149]) by localhost.localdomain (Coremail) with SMTP id AQAAf8AxPuAzGm5jrdMQAA--.46156S2; Fri, 11 Nov 2022 17:47:31 +0800 (CST) Date: Fri, 11 Nov 2022 17:47:31 +0800 From: "Chao Li" To: xianglai li Cc: "=?utf-8?Q?devel=40edk2.groups.io?=" , Bibo Mao , Leif Lindholm , Liming Gao , Michael D Kinney Message-ID: <9803BC03-2208-43CA-9D63-FF4C764F42CF@getmailspring.com> In-Reply-To: References: Subject: Re: [edk2-platforms][PATCH V5 11/15] Platform/Loongson: Add RealTime Clock lib. X-Mailer: Mailspring MIME-Version: 1.0 X-CM-TRANSID: AQAAf8AxPuAzGm5jrdMQAA--.46156S2 X-CM-SenderInfo: xolfxt3r6o00pqjv00gofq/1tbiAQAHCGNs6eQZ0AAlsg X-Coremail-Antispam: 1Uk129KBjvAXoW3CFy7tr18tr1DXrWkWFWfKrg_yoW8JrWfGo WxtFWSqw48Jr18uasa934kCrWIgFZIqa1Sqr1FqFWjyan8Ar1DtFyUta42gryfArykAwsx KryfA3ykJFWaqFW8n29KB7ZKAUJUUUUP529EdanIXcx71UUUUU7KY7ZEXasCq-sGcSsGvf J3Ec02F40Eb7x2x7xS6r1j6r4UMc02F40EFcxC0VAKzVAqx4xG6I80ewAqx4xG64kEw2xG 04xIwI0_Xr0_WrUv73VFW2AGmfu7bjvjm3AaLaJ3UjIYCTnIWjp_UUUOS7kC6x804xWl14 x267AKxVWUJVW8JwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0rVWrJVCq3wAFIxvE14AKwVWU XVWUAwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK021l84ACjcxK6xIIjxv20xvE14 v26F1j6w1UM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26F4j6r4UJwA2z4x0Y4vEx4A2jsIE 14v26F4UJVW0owA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Cr1j6rxdM2kKe7AKxVWUtVW8Zw AS0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07AIYIkI8VC2zVCFFI0UMc02F40Eb7x2 x7xS6r1j6r4UMc02F40EFcxC0VAKzVAqx4xG6I80ewAqx4xG64kEw2xG04xIwI0_Xr0_Wr 1lYx0E2Ix0cI8IcVAFwI0_Wrv_ZF1lYx0Ex4A2jsIE14v26r4j6F4UMcvjeVCFs4IE7xkE bVWUJVW8JwACjcxG0xvY0x0EwIxGrwCjr7xvwVCIw2I0I7xG6c02F41lc7CjxVAaw2AFwI 0_Jw0_GFyl42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG 67AKxVWUGVWUWwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r126r1DMI IYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Ar0_tr1lIxAIcVC0I7IYx2IY6xkF7I0E 14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVW8JV WxJwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUT5Ef DUUUU Content-Type: multipart/alternative; boundary="636e1a33_1370bfa4_1e57b" --636e1a33_1370bfa4_1e57b Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Reviewed-by: Chao Li Thanks, Chao -------- On 11=E6=9C=88 11 2022, at 5:12 =E4=B8=8B=E5=8D=88, xianglai li wrote: > This library provides interfaces such as > > real-time clock initialization > to get time and setting time. > > > RE=46: https://bugzilla.tianocore.org/show=5Fbug.cgi=3Fid=3D4054 > > > Cc: Bibo Mao > Cc: Chao Li > Cc: Leif Lindholm > Cc: Liming Gao > Cc: Michael D Kinney > Signed-off-by: xianglai li > --- > .../LsRealTimeClockLib/LsRealTimeClock.h =7C 40 +++ > .../LsRealTimeClockLib/LsRealTimeClockLib.c =7C 335 ++++++++++++++++++ > .../LsRealTimeClockLib/LsRealTimeClockLib.inf =7C 44 +++ > 3 files changed, 419 insertions(+) > create mode 100644 Platform/Loongson/LoongArchQemuPkg/Library/LsRealTim= eClockLib/LsRealTimeClock.h > create mode 100644 Platform/Loongson/LoongArchQemuPkg/Library/LsRealTim= eClockLib/LsRealTimeClockLib.c > create mode 100644 Platform/Loongson/LoongArchQemuPkg/Library/LsRealTim= eClockLib/LsRealTimeClockLib.inf > > > diff --git a/Platform/Loongson/LoongArchQemuPkg/Library/LsRealTimeClock= Lib/LsRealTimeClock.h b/Platform/Loongson/LoongArchQemuPkg/Library/LsReal= TimeClockLib/LsRealTimeClock.h > new file mode 100644 > index 0000000000..6567ec80db > --- /dev/null > +++ b/Platform/Loongson/LoongArchQemuPkg/Library/LsRealTimeClockLib/LsR= ealTimeClock.h > =40=40 -0,0 +1,40 =40=40 > +/** =40file > + Implement E=46I RealTimeClock runtime services via RTC Lib. > + > + Copyright (c) 2022, Loongson Limited. All rights reserved. > + > + SPDX-License-Identifier: BSD-2-Clause-Patent > + > +**/ > + > +=23ifndef LS=5FREAL=5FTIME=5FCLOCK=5FH=5F > +=23define LS=5FREAL=5FTIME=5FCLOCK=5FH=5F > + > +=23define TOY=5FWRITE0=5FREG 0x24 > +=23define TOY=5FWRITE1=5FREG 0x28 > +=23define TOY=5FREAD0=5FREG 0x2c > +=23define TOY=5FREAD1=5FREG 0x30 > +=23define RTC=5FCTRL=5FREG 0x40 > + > +/* TOY Enable bits */ > +=23define RTC=5FENABLE=5FBIT (1UL << 13) > +=23define TOY=5FENABLE=5FBIT (1UL << 11) > +=23define OSC=5FENABLE=5FBIT (1UL << 8) > + > +/* > + * shift bits and filed mask > + */ > +=23define TOY=5FMON=5FMASK 0x3f > +=23define TOY=5FDAY=5FMASK 0x1f > +=23define TOY=5FHOUR=5FMASK 0x1f > +=23define TOY=5FMIN=5FMASK 0x3f > +=23define TOY=5FSEC=5FMASK 0x3f > +=23define TOY=5FMSEC=5FMASK 0xf > + > +=23define TOY=5FMON=5FSHI=46T 26 > +=23define TOY=5FDAY=5FSHI=46T 21 > +=23define TOY=5FHOUR=5FSHI=46T 16 > +=23define TOY=5FMIN=5FSHI=46T 10 > +=23define TOY=5FSEC=5FSHI=46T 4 > + > +=23endif // LS=5FREAL=5FTIME=5FCLOCK=5FH=5F > diff --git a/Platform/Loongson/LoongArchQemuPkg/Library/LsRealTimeClock= Lib/LsRealTimeClockLib.c b/Platform/Loongson/LoongArchQemuPkg/Library/LsR= ealTimeClockLib/LsRealTimeClockLib.c > new file mode 100644 > index 0000000000..dc651ccaaf > --- /dev/null > +++ b/Platform/Loongson/LoongArchQemuPkg/Library/LsRealTimeClockLib/LsR= ealTimeClockLib.c > =40=40 -0,0 +1,335 =40=40 > +/** =40file > + Implement E=46I RealTimeClock runtime services via RTC Lib. > + > + Copyright (c) 2022, Loongson Limited. All rights reserved. > + > + SPDX-License-Identifier: BSD-2-Clause-Patent > + > +**/ > + > +=23include > +=23include > + > +=23include > +=23include > + > +=23include > +=23include > +=23include > +=23include > +=23include > +=23include > +=23include > +=23include > +=23include > +=23include > +=23include > +=23include > +=23include =22LsRealTimeClock.h=22 > + > +STATIC BOOLEAN mInitialized =3D =46ALSE; > +STATIC E=46I=5FEVENT mRtcVirtualAddrChangeEvent; > +STATIC UINTN mRtcBase =3D 0x100d0100; > +/* > + Enable Real-time clock. > + > + =40param VOID > + > + =40retval VOID > + */ > +VOID > +InitRtc ( > + VOID > + ) > +=7B > + UINTN Val; > + > + if (=21mInitialized) =7B > + /* enable rtc */ > + Val =3D MmioRead32 (mRtcBase + RTC=5FCTRL=5FREG); > + Val =7C=3D TOY=5FENABLE=5FBIT =7C OSC=5FENABLE=5FBIT; > + MmioWrite32 (mRtcBase + RTC=5FCTRL=5FREG, Val); > + mInitialized =3D TRUE; > + =7D > +=7D > + > +/** > + Returns the current time and date information, and the time-keeping c= apabilities > + of the hardware platform. > + > + =40param Time A pointer to storage to receive a snapshot of the curre= nt time. > + =40param Capabilities An optional pointer to a buffer to receive the = real time clock > + device's capabilities. > + > + =40retval E=46I=5FSUCCESS The operation completed successfully. > + =40retval E=46I=5FINVALID=5FPARAMETER Time is NULL. > + =40retval E=46I=5FDEVICE=5FERROR The time could not be retrieved due = to hardware error. > + =40retval E=46I=5FSECURITY=5FVIOLATION The time could not be retrieve= d due to an authentication failure. > +**/ > +E=46I=5FSTATUS > +E=46IAPI > +LibGetTime ( > + OUT E=46I=5FTIME *Time, > + OUT E=46I=5FTIME=5FCAPABILITIES *Capabilities > + ) > +=7B > + UINT32 Val; > + > + // Ensure Time is a valid pointer > + if (Time =3D=3D NULL) =7B > + return E=46I=5FINVALID=5FPARAMETER; > + =7D > + > + InitRtc (); > + Val =3D MmioRead32 (mRtcBase + TOY=5FREAD1=5FREG); > + Time->Year =3D Val + 1900; > + > + Val =3D MmioRead32 (mRtcBase + TOY=5FREAD0=5FREG); > + Time->Month =3D (Val >> TOY=5FMON=5FSHI=46T) & TOY=5FMON=5FMASK; > + Time->Day =3D (Val >> TOY=5FDAY=5FSHI=46T) & TOY=5FDAY=5FMASK; > + Time->Hour =3D (Val >> TOY=5FHOUR=5FSHI=46T) & TOY=5FHOUR=5FMASK; > + Time->Minute =3D (Val >> TOY=5FMIN=5FSHI=46T) & TOY=5FMIN=5FMASK; > + Time->Second =3D (Val >> TOY=5FSEC=5FSHI=46T) & TOY=5FSEC=5FMASK; > + Time->Nanosecond =3D 0; > + return E=46I=5FSUCCESS; > +=7D > + > +/** > + Sets the current local time and date information. > + > + =40param Time A pointer to the current time. > + > + =40retval E=46I=5FSUCCESS The operation completed successfully. > + =40retval E=46I=5FINVALID=5FPARAMETER A time field is out of range. > + =40retval E=46I=5FDEVICE=5FERROR The time could not be set due due to= hardware error. > +**/ > +E=46I=5FSTATUS > +E=46IAPI > +LibSetTime ( > + IN E=46I=5FTIME *Time > + ) > +=7B > + UINT32 Val; > + > + // Initialize the hardware if not already done > + InitRtc (); > + > + Val =3D 0; > + Val =7C=3D (Time->Second << TOY=5FSEC=5FSHI=46T); > + Val =7C=3D (Time->Minute << TOY=5FMIN=5FSHI=46T); > + Val =7C=3D (Time->Hour << TOY=5FHOUR=5FSHI=46T); > + Val =7C=3D (Time->Day << TOY=5FDAY=5FSHI=46T); > + Val =7C=3D (Time->Month << TOY=5FMON=5FSHI=46T); > + MmioWrite32 (mRtcBase + TOY=5FWRITE0=5FREG, Val); > + > + Val =3D Time->Year - 1900; > + MmioWrite32 (mRtcBase + TOY=5FWRITE1=5FREG, Val); > + return E=46I=5FSUCCESS; > +=7D > + > +/** > + Returns the current wakeup alarm clock setting. > + > + =40param Enabled Indicates if the alarm is currently enabled or disab= led. > + =40param Pending Indicates if the alarm signal is pending and require= s acknowledgement. > + =40param Time The current alarm setting. > + > + =40retval E=46I=5FSUCCESS The alarm settings were returned. > + =40retval E=46I=5FINVALID=5FPARAMETER Any parameter is NULL. > + =40retval E=46I=5FDEVICE=5FERROR The wakeup time could not be retriev= ed due to a hardware error. > +**/ > +E=46I=5FSTATUS > +E=46IAPI > +LibGetWakeupTime ( > + OUT BOOLEAN *Enabled, > + OUT BOOLEAN *Pending, > + OUT E=46I=5FTIME *Time > + ) > +=7B > + // Not a required feature > + return E=46I=5FUNSUPPORTED; > +=7D > + > +/** > + Sets the system wakeup alarm clock time. > + > + =40param Enabled Enable or disable the wakeup alarm. > + =40param Time If Enable is TRUE, the time to set the wakeup alarm for= . > + > + =40retval E=46I=5FSUCCESS If Enable is TRUE, then the wakeup alarm wa= s enabled. If > + Enable is =46ALSE, then the wakeup alarm was disabled. > + =40retval E=46I=5FINVALID=5FPARAMETER A time field is out of range. > + =40retval E=46I=5FDEVICE=5FERROR The wakeup time could not be set due= to a hardware error. > + =40retval E=46I=5FUNSUPPORTED A wakeup timer is not supported on this= platform. > +**/ > +E=46I=5FSTATUS > +E=46IAPI > +LibSetWakeupTime ( > + IN BOOLEAN Enabled, > + OUT E=46I=5FTIME *Time > + ) > +=7B > + // Not a required feature > + return E=46I=5FUNSUPPORTED; > +=7D > + > +/** > + =46ixup internal data so that E=46I can be call in virtual mode. > + Call the passed in Child Notify event and convert any pointers in > + lib to virtual mode. > + > + =40param=5Bin=5D Event The Event that is being processed > + =40param=5Bin=5D Context Event Context > +**/ > +VOID > +E=46IAPI > +LibRtcVirtualNotifyEvent ( > + IN E=46I=5FEVENT Event, > + IN VOID *Context > + ) > +=7B > + // > + // Only needed if you are going to support the OS calling RTC functio= ns in virtual mode. > + // You will need to call EfiConvertPointer (). To convert any stored = physical addresses > + // to virtual address. After the OS transitions to calling in virtual= mode, all future > + // runtime calls will be made in virtual mode. > + // > + EfiConvertPointer (0x0, (VOID**)&mRtcBase); > + return; > +=7D > + > +/** Add the RTC controller address range to the memory map. > + > + =40param =5Bin=5D ImageHandle The handle to the image. > + =40param =5Bin=5D RtcPageBase Base address of the RTC controller. > + > + =40retval E=46I=5FSUCCESS Success. > + =40retval E=46I=5FINVALID=5FPARAMETER A parameter is invalid. > + =40retval E=46I=5FNOT=5F=46OUND =46lash device not found. > +**/ > +E=46I=5FSTATUS > +KvmtoolRtcMapMemory ( > + IN E=46I=5FHANDLE ImageHandle, > + IN E=46I=5FPHYSICAL=5FADDRESS RtcPageBase > + ) > +=7B > + E=46I=5FSTATUS Status; > + > + Status =3D gDS->AddMemorySpace ( > + EfiGcdMemoryTypeMemoryMappedIo, > + RtcPageBase, > + E=46I=5FPAGE=5FSIZE, > + E=46I=5FMEMORY=5FUC =7C E=46I=5FMEMORY=5FRUNTIME > + ); > + if (E=46I=5FERROR (Status)) =7B > + DEBUG (( > + DEBUG=5FERROR, =22=46ailed to add memory space. Status =3D %r=5Cn=22,= > + Status > + )); > + return Status; > + =7D > + > + Status =3D gDS->AllocateMemorySpace ( > + EfiGcdAllocateAddress, > + EfiGcdMemoryTypeMemoryMappedIo, > + 0, > + E=46I=5FPAGE=5FSIZE, > + &RtcPageBase, > + ImageHandle, > + NULL > + ); > + if (E=46I=5FERROR (Status)) =7B > + DEBUG (( > + DEBUG=5FERROR, > + =22=46ailed to allocate memory space. Status =3D %r=5Cn=22, > + Status > + )); > + gDS->RemoveMemorySpace ( > + RtcPageBase, > + E=46I=5FPAGE=5FSIZE > + ); > + return Status; > + =7D > + > + Status =3D gDS->SetMemorySpaceAttributes ( > + RtcPageBase, > + E=46I=5FPAGE=5FSIZE, > + E=46I=5FMEMORY=5FUC =7C E=46I=5FMEMORY=5FRUNTIME > + ); > + if (E=46I=5FERROR (Status)) =7B > + DEBUG (( > + DEBUG=5FERROR, > + =22=46ailed to set memory attributes. Status =3D %r=5Cn=22, > + Status > + )); > + > + gDS->=46reeMemorySpace ( > + RtcPageBase, > + E=46I=5FPAGE=5FSIZE > + ); > + > + gDS->RemoveMemorySpace ( > + RtcPageBase, > + E=46I=5FPAGE=5FSIZE > + ); > + =7D > + > + return Status; > +=7D > + > +/** > + This is the declaration of an E=46I image entry point. This can be th= e entry point to an application > + written to this specification, an E=46I boot service driver, or an E=46= I runtime driver. > + > + =40param ImageHandle Handle that identifies the loaded image. > + =40param SystemTable System Table for this image. > + > + =40retval E=46I=5FSUCCESS The operation completed successfully. > +**/ > +E=46I=5FSTATUS > +E=46IAPI > +LibRtcInitialize ( > + IN E=46I=5FHANDLE ImageHandle, > + IN E=46I=5FSYSTEM=5FTABLE *SystemTable > + ) > +=7B > + E=46I=5FSTATUS Status; > + E=46I=5FHANDLE Handle; > + > + Status =3D KvmtoolRtcMapMemory (ImageHandle, (mRtcBase & =7EE=46I=5FP= AGE=5FMASK)); > + if (E=46I=5FERROR (Status)) =7B > + DEBUG (( > + DEBUG=5FERROR, > + =22=46ailed to map memory for loongson 7A RTC. Status =3D %r=5Cn=22, > + Status > + )); > + return Status; > + =7D > + > + // Setup the setters and getters > + gRT->GetTime =3D LibGetTime; > + gRT->SetTime =3D LibSetTime; > + > + // Install the protocol > + Handle =3D NULL; > + Status =3D gBS->InstallMultipleProtocolInterfaces ( > + &Handle, > + &gEfiRealTimeClockArchProtocolGuid, NULL, > + NULL > + ); > + ASSERT=5FE=46I=5FERROR (Status); > + > + // > + // Register for the virtual address change event > + // > + Status =3D gBS->CreateEventEx ( > + EVT=5FNOTI=46Y=5FSIGNAL, > + TPL=5FNOTI=46Y, > + LibRtcVirtualNotifyEvent, > + NULL, > + &gEfiEventVirtualAddressChangeGuid, > + &mRtcVirtualAddrChangeEvent > + ); > + ASSERT=5FE=46I=5FERROR (Status); > + return Status; > +=7D > diff --git a/Platform/Loongson/LoongArchQemuPkg/Library/LsRealTimeClock= Lib/LsRealTimeClockLib.inf b/Platform/Loongson/LoongArchQemuPkg/Library/L= sRealTimeClockLib/LsRealTimeClockLib.inf > new file mode 100644 > index 0000000000..6f7bf467b9 > --- /dev/null > +++ b/Platform/Loongson/LoongArchQemuPkg/Library/LsRealTimeClockLib/LsR= ealTimeClockLib.inf > =40=40 -0,0 +1,44 =40=40 > +=23=23 =40file > +=23 > +=23 Copyright (c) 2022, Loongson Limited. All rights reserved. > +=23 > +=23 SPDX-License-Identifier: BSD-2-Clause-Patent > +=23 > +=23=23 > + > +=5BDefines=5D > + IN=46=5FVERSION =3D 0x00010005 > + BASE=5FNAME =3D LsRealTimeClockLib > + =46ILE=5FGUID =3D 9793a3da-1869-4fdf-88b1-c6484341f50b > + MODULE=5FTYPE =3D BASE > + VERSION=5FSTRING =3D 1.0 > + LIBRARY=5FCLASS =3D RealTimeClockLib > + > +=23 > +=23 VALID=5FARCHITECTURES =3D LOONGARCH64 > +=23 > + > +=5BSources=5D > + LsRealTimeClockLib.c > + > +=5BPackages=5D > + MdePkg/MdePkg.dec > + EmbeddedPkg/EmbeddedPkg.dec > + Platform/Loongson/LoongArchQemuPkg/Loongson.dec > + > +=5BLibraryClasses=5D > + IoLib > + UefiLib > + DebugLib > + PcdLib > + DxeServicesTableLib > + UefiRuntimeLib > + > +=5BGuids=5D > + gEfiEventVirtualAddressChangeGuid > + > +=5BProtocols=5D > + gEfiRealTimeClockArchProtocolGuid =23 PROTOCOL ALWAYS=5FPRODUCED > + > +=5BDepex=5D > + TRUE > -- > 2.31.1 --636e1a33_1370bfa4_1e57b Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline
Reviewed-by: Chao Li  <lichao=40loongson.cn>


Thanks,
Chao
--------

On 11=E6=9C=88 11 2= 022, at 5:12 =E4=B8=8B=E5=8D=88, xianglai li <lixianglai=40loongson.cn= > wrote:
This library provides interfaces s= uch as

real-time clock initialization

to get = time and setting time.



RE=46: https://bugzilla.tian= ocore.org/show=5Fbug.cgi=3Fid=3D4054



Cc: Bibo Mao &= lt;maobibo=40loongson.cn>

Cc: Chao Li <lichao=40loong= son.cn>

Cc: Leif Lindholm <quic=5Fllindhol=40quicinc.= com>

Cc: Liming Gao <gaoliming=40byosoft.com.cn>
Cc: Michael D Kinney <michael.d.kinney=40intel.com>
Signed-off-by: xianglai li <lixianglai=40loongson.cn>
---

.../LsRealTimeClockLib/LsRealTimeClock.h =7C= 40 +++

.../LsRealTimeClockLib/LsRealTimeClockLib.c =7C 335= ++++++++++++++++++

.../LsRealTimeClockLib/LsRealTimeClockL= ib.inf =7C 44 +++

3 files changed, 419 insertions(+)
<= br>
create mode 100644 Platform/Loongson/LoongArchQemuPkg/Library/LsR= ealTimeClockLib/LsRealTimeClock.h

create mode 100644 Platfo= rm/Loongson/LoongArchQemuPkg/Library/LsRealTimeClockLib/LsRealTimeClockLi= b.c

create mode 100644 Platform/Loongson/LoongArchQemuPkg/L= ibrary/LsRealTimeClockLib/LsRealTimeClockLib.inf



di= ff --git a/Platform/Loongson/LoongArchQemuPkg/Library/LsRealTimeClockLib/= LsRealTimeClock.h b/Platform/Loongson/LoongArchQemuPkg/Library/LsRealTime= ClockLib/LsRealTimeClock.h

new file mode 100644

index 0000000000..6567ec80db

--- /dev/null

+++ b/Platform/Loongson/LoongArchQemuPkg/Library/LsRealTimeClockLib/LsRe= alTimeClock.h

=40=40 -0,0 +1,40 =40=40

+/** =40= file

+ Implement E=46I RealTimeClock runtime services via R= TC Lib.

+

+ Copyright (c) 2022, Loongson Limi= ted. All rights reserved.

+

+ SPDX-License-Id= entifier: BSD-2-Clause-Patent

+

+**/
+

+=23ifndef LS=5FREAL=5FTIME=5FCLOCK=5FH=5F
+=23define LS=5FREAL=5FTIME=5FCLOCK=5FH=5F

+
+=23define TOY=5FWRITE0=5FREG 0x24

+=23define TOY=5FW= RITE1=5FREG 0x28

+=23define TOY=5FREAD0=5FREG 0x2c
+=23define TOY=5FREAD1=5FREG 0x30

+=23define RTC=5FCT= RL=5FREG 0x40

+

+/* TOY Enable bits */
<= br>
+=23define RTC=5FENABLE=5FBIT (1UL << 13)

+=23= define TOY=5FENABLE=5FBIT (1UL << 11)

+=23define OSC=5F= ENABLE=5FBIT (1UL << 8)

+

+/*

=
+ * shift bits and filed mask

+ */

+=23d= efine TOY=5FMON=5FMASK 0x3f

+=23define TOY=5FDAY=5FMASK 0x1= f

+=23define TOY=5FHOUR=5FMASK 0x1f

+=23defin= e TOY=5FMIN=5FMASK 0x3f

+=23define TOY=5FSEC=5FMASK 0x3f
+=23define TOY=5FMSEC=5FMASK 0xf

+

+=23define TOY=5FMON=5FSHI=46T 26

+=23define TOY=5FDAY=5F= SHI=46T 21

+=23define TOY=5FHOUR=5FSHI=46T 16

+=23define TOY=5FMIN=5FSHI=46T 10

+=23define TOY=5FSEC=5FS= HI=46T 4

+

+=23endif // LS=5FREAL=5FTIME=5FCL= OCK=5FH=5F

diff --git a/Platform/Loongson/LoongArchQemuPkg/= Library/LsRealTimeClockLib/LsRealTimeClockLib.c b/Platform/Loongson/Loong= ArchQemuPkg/Library/LsRealTimeClockLib/LsRealTimeClockLib.c

new file mode 100644

index 0000000000..dc651ccaaf
--- /dev/null

+++ b/Platform/Loongson/LoongArchQemuPk= g/Library/LsRealTimeClockLib/LsRealTimeClockLib.c

=40=40 -0= ,0 +1,335 =40=40

+/** =40file

+ Implement E=46= I RealTimeClock runtime services via RTC Lib.

+

+ Copyright (c) 2022, Loongson Limited. All rights reserved.
=
+

+ SPDX-License-Identifier: BSD-2-Clause-Patent
=
+

+**/

+

+=23include &= lt;Uefi.h>

+=23include <PiDxe.h>

+
+=23include <Guid/EventGroup.h>

+=23incl= ude <Guid/GlobalVariable.h>

+

+=23inclu= de <Library/BaseLib.h>

+=23include <Library/DebugL= ib.h>

+=23include <Library/DxeServicesTableLib.h><= /div>
+=23include <Library/IoLib.h>

+=23inclu= de <Library/MemoryAllocationLib.h>

+=23include <Li= brary/PcdLib.h>

+=23include <Library/RealTimeClockLib= .h>

+=23include <Library/TimeBaseLib.h>

<= div>+=23include <Library/UefiBootServicesTableLib.h>

= +=23include <Library/UefiRuntimeServicesTableLib.h>

+= =23include <Library/UefiRuntimeLib.h>

+=23include <= ;Protocol/RealTimeClock.h>

+=23include =22LsRealTimeCloc= k.h=22

+

+STATIC BOOLEAN mInitialized =3D =46= ALSE;

+STATIC E=46I=5FEVENT mRtcVirtualAddrChangeEvent;
+STATIC UINTN mRtcBase =3D 0x100d0100;

+/*
=
+ Enable Real-time clock.

+

+ =40par= am VOID

+

+ =40retval VOID

+ */=

+VOID

+InitRtc (

+ VOID
<= br>
+ )

+=7B

+ UINTN Val;

+=

+ if (=21mInitialized) =7B

+ /* enable rtc *= /

+ Val =3D MmioRead32 (mRtcBase + RTC=5FCTRL=5FREG);
=
+ Val =7C=3D TOY=5FENABLE=5FBIT =7C OSC=5FENABLE=5FBIT;
+ MmioWrite32 (mRtcBase + RTC=5FCTRL=5FREG, Val);

+ m= Initialized =3D TRUE;

+ =7D

+=7D

+

+/**

+ Returns the current time and date = information, and the time-keeping capabilities

+ of the har= dware platform.

+

+ =40param Time A pointer t= o storage to receive a snapshot of the current time.

+ =40p= aram Capabilities An optional pointer to a buffer to receive the real tim= e clock

+ device's capabilities.

+

<= div>+ =40retval E=46I=5FSUCCESS The operation completed successfully.
+ =40retval E=46I=5FINVALID=5FPARAMETER Time is NULL.
+ =40retval E=46I=5FDEVICE=5FERROR The time could not be retrieved = due to hardware error.

+ =40retval E=46I=5FSECURITY=5FVIOLA= TION The time could not be retrieved due to an authentication failure.
+**/

+E=46I=5FSTATUS

+E=46IAPI
+LibGetTime (

+ OUT E=46I=5FTIME *Time,
+ OUT E=46I=5FTIME=5FCAPABILITIES *Capabilities

+ )<= /div>
+=7B

+ UINT32 Val;

+

<= div>+ // Ensure Time is a valid pointer

+ if (Time =3D=3D N= ULL) =7B

+ return E=46I=5FINVALID=5FPARAMETER;

+ =7D

+

+ InitRtc ();

+ Val =3D= MmioRead32 (mRtcBase + TOY=5FREAD1=5FREG);

+ Time->Year= =3D Val + 1900;

+

+ Val =3D MmioRead32 (mRtc= Base + TOY=5FREAD0=5FREG);

+ Time->Month =3D (Val >&g= t; TOY=5FMON=5FSHI=46T) & TOY=5FMON=5FMASK;

+ Time->= Day =3D (Val >> TOY=5FDAY=5FSHI=46T) & TOY=5FDAY=5FMASK;
<= br>
+ Time->Hour =3D (Val >> TOY=5FHOUR=5FSHI=46T) & TOY= =5FHOUR=5FMASK;

+ Time->Minute =3D (Val >> TOY=5FM= IN=5FSHI=46T) & TOY=5FMIN=5FMASK;

+ Time->Second =3D= (Val >> TOY=5FSEC=5FSHI=46T) & TOY=5FSEC=5FMASK;

+ Time->Nanosecond =3D 0;

+ return E=46I=5FSUCCESS;
+=7D

+

+/**

+ Sets t= he current local time and date information.

+

+ =40param Time A pointer to the current time.

+

=
+ =40retval E=46I=5FSUCCESS The operation completed successfully.
+ =40retval E=46I=5FINVALID=5FPARAMETER A time field is out o= f range.

+ =40retval E=46I=5FDEVICE=5FERROR The time could = not be set due due to hardware error.

+**/

+E= =46I=5FSTATUS

+E=46IAPI

+LibSetTime (
+ IN E=46I=5FTIME *Time

+ )

+=7B
=
+ UINT32 Val;

+

+ // Initialize the = hardware if not already done

+ InitRtc ();

+<= /div>
+ Val =3D 0;

+ Val =7C=3D (Time->Second &l= t;< TOY=5FSEC=5FSHI=46T);

+ Val =7C=3D (Time->Minute = << TOY=5FMIN=5FSHI=46T);

+ Val =7C=3D (Time->Hour = << TOY=5FHOUR=5FSHI=46T);

+ Val =7C=3D (Time->Day = << TOY=5FDAY=5FSHI=46T);

+ Val =7C=3D (Time->Month= << TOY=5FMON=5FSHI=46T);

+ MmioWrite32 (mRtcBase + T= OY=5FWRITE0=5FREG, Val);

+

+ Val =3D Time->= ;Year - 1900;

+ MmioWrite32 (mRtcBase + TOY=5FWRITE1=5FREG,= Val);

+ return E=46I=5FSUCCESS;

+=7D
+

+/**

+ Returns the current wakeup al= arm clock setting.

+

+ =40param Enabled Indic= ates if the alarm is currently enabled or disabled.

+ =40pa= ram Pending Indicates if the alarm signal is pending and requires acknowl= edgement.

+ =40param Time The current alarm setting.
<= br>
+

+ =40retval E=46I=5FSUCCESS The alarm settings we= re returned.

+ =40retval E=46I=5FINVALID=5FPARAMETER Any pa= rameter is NULL.

+ =40retval E=46I=5FDEVICE=5FERROR The wak= eup time could not be retrieved due to a hardware error.

+*= */

+E=46I=5FSTATUS

+E=46IAPI

+L= ibGetWakeupTime (

+ OUT BOOLEAN *Enabled,

+ O= UT BOOLEAN *Pending,

+ OUT E=46I=5FTIME *Time

+ )

+=7B

+ // Not a required feature
+ return E=46I=5FUNSUPPORTED;

+=7D

+
+/**

+ Sets the system wakeup alarm clock time= .

+

+ =40param Enabled Enable or disable the = wakeup alarm.

+ =40param Time If Enable is TRUE, the time t= o set the wakeup alarm for.

+

+ =40retval E=46= I=5FSUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If
+ Enable is =46ALSE, then the wakeup alarm was disabled.
<= br>
+ =40retval E=46I=5FINVALID=5FPARAMETER A time field is out of ra= nge.

+ =40retval E=46I=5FDEVICE=5FERROR The wakeup time cou= ld not be set due to a hardware error.

+ =40retval E=46I=5F= UNSUPPORTED A wakeup timer is not supported on this platform.

+**/

+E=46I=5FSTATUS

+E=46IAPI

+LibSetWakeupTime (

+ IN BOOLEAN Enabled,

= + OUT E=46I=5FTIME *Time

+ )

+=7B

+ // Not a required feature

+ return E=46I=5FUNSUPPORTED= ;

+=7D

+

+/**

+ =46= ixup internal data so that E=46I can be call in virtual mode.

+ Call the passed in Child Notify event and convert any pointers in
+ lib to virtual mode.

+

+ =40par= am=5Bin=5D Event The Event that is being processed

+ =40par= am=5Bin=5D Context Event Context

+**/

+VOID
+E=46IAPI

+LibRtcVirtualNotifyEvent (
+ IN E=46I=5FEVENT Event,

+ IN VOID *Context
+ )

+=7B

+ //

+ // Only n= eeded if you are going to support the OS calling RTC functions in virtual= mode.

+ // You will need to call EfiConvertPointer (). To = convert any stored physical addresses

+ // to virtual addre= ss. After the OS transitions to calling in virtual mode, all future
=
+ // runtime calls will be made in virtual mode.

+= //

+ EfiConvertPointer (0x0, (VOID**)&mRtcBase);
=
+ return;

+=7D

+

+/** = Add the RTC controller address range to the memory map.

+
+ =40param =5Bin=5D ImageHandle The handle to the image.
+ =40param =5Bin=5D RtcPageBase Base address of the RTC contro= ller.

+

+ =40retval E=46I=5FSUCCESS Success.<= /div>
+ =40retval E=46I=5FINVALID=5FPARAMETER A parameter is inva= lid.

+ =40retval E=46I=5FNOT=5F=46OUND =46lash device not f= ound.

+**/

+E=46I=5FSTATUS

+Kvm= toolRtcMapMemory (

+ IN E=46I=5FHANDLE ImageHandle,
+ IN E=46I=5FPHYSICAL=5FADDRESS RtcPageBase

+ )
+=7B

+ E=46I=5FSTATUS Status;

+
+ Status =3D gDS->AddMemorySpace (

+ EfiGcdMe= moryTypeMemoryMappedIo,

+ RtcPageBase,

+ E=46= I=5FPAGE=5FSIZE,

+ E=46I=5FMEMORY=5FUC =7C E=46I=5FMEMORY=5F= RUNTIME

+ );

+ if (E=46I=5FERROR (Status)) =7B=

+ DEBUG ((

+ DEBUG=5FERROR, =22=46ailed to a= dd memory space. Status =3D %r=5Cn=22,

+ Status

+ ));

+ return Status;

+ =7D

+

+ Status =3D gDS->AllocateMemorySpace (

+ EfiGcdAllocateAddress,

+ EfiGcdMemoryTypeMemoryMappedIo,=

+ 0,

+ E=46I=5FPAGE=5FSIZE,

+ = &RtcPageBase,

+ ImageHandle,

+ NULL
=
+ );

+ if (E=46I=5FERROR (Status)) =7B

+ DEBUG ((

+ DEBUG=5FERROR,

+ =22=46ailed = to allocate memory space. Status =3D %r=5Cn=22,

+ Status
+ ));

+ gDS->RemoveMemorySpace (

+ RtcPageBase,

+ E=46I=5FPAGE=5FSIZE

+ );<= /div>
+ return Status;

+ =7D

+
<= br>
+ Status =3D gDS->SetMemorySpaceAttributes (

+ R= tcPageBase,

+ E=46I=5FPAGE=5FSIZE,

+ E=46I=5F= MEMORY=5FUC =7C E=46I=5FMEMORY=5FRUNTIME

+ );

+ if (E=46I=5FERROR (Status)) =7B

+ DEBUG ((

+ DEBUG=5FERROR,

+ =22=46ailed to set memory attributes. S= tatus =3D %r=5Cn=22,

+ Status

+ ));

=
+

+ gDS->=46reeMemorySpace (

+ RtcPag= eBase,

+ E=46I=5FPAGE=5FSIZE

+ );

+

+ gDS->RemoveMemorySpace (

+ RtcPageB= ase,

+ E=46I=5FPAGE=5FSIZE

+ );

+ =7D

+

+ return Status;

+=7D<= /div>
+

+/**

+ This is the declaratio= n of an E=46I image entry point. This can be the entry point to an applic= ation

+ written to this specification, an E=46I boot servic= e driver, or an E=46I runtime driver.

+

+ =40= param ImageHandle Handle that identifies the loaded image.

= + =40param SystemTable System Table for this image.

+
=
+ =40retval E=46I=5FSUCCESS The operation completed successfully= .

+**/

+E=46I=5FSTATUS

+E=46IAP= I

+LibRtcInitialize (

+ IN E=46I=5FHANDLE Ima= geHandle,

+ IN E=46I=5FSYSTEM=5FTABLE *SystemTable
+ )

+=7B

+ E=46I=5FSTATUS Status;
=
+ E=46I=5FHANDLE Handle;

+

+ Status = =3D KvmtoolRtcMapMemory (ImageHandle, (mRtcBase & =7EE=46I=5FPAGE=5FM= ASK));

+ if (E=46I=5FERROR (Status)) =7B

+ DE= BUG ((

+ DEBUG=5FERROR,

+ =22=46ailed to map = memory for loongson 7A RTC. Status =3D %r=5Cn=22,

+ Status<= /div>
+ ));

+ return Status;

+ =7D
+

+ // Setup the setters and getters

<= div>+ gRT->GetTime =3D LibGetTime;

+ gRT->SetTime =3D= LibSetTime;

+

+ // Install the protocol
+ Handle =3D NULL;

+ Status =3D gBS->InstallMu= ltipleProtocolInterfaces (

+ &Handle,

+ &= amp;gEfiRealTimeClockArchProtocolGuid, NULL,

+ NULL
+ );

+ ASSERT=5FE=46I=5FERROR (Status);

+

+ //

+ // Register for the virtual addres= s change event

+ //

+ Status =3D gBS->Crea= teEventEx (

+ EVT=5FNOTI=46Y=5FSIGNAL,

+ TPL=5F= NOTI=46Y,

+ LibRtcVirtualNotifyEvent,

+ NULL,=

+ &gEfiEventVirtualAddressChangeGuid,

+ = &mRtcVirtualAddrChangeEvent

+ );

+ ASSERT= =5FE=46I=5FERROR (Status);

+ return Status;

+= =7D

diff --git a/Platform/Loongson/LoongArchQemuPkg/Library= /LsRealTimeClockLib/LsRealTimeClockLib.inf b/Platform/Loongson/LoongArchQ= emuPkg/Library/LsRealTimeClockLib/LsRealTimeClockLib.inf

ne= w file mode 100644

index 0000000000..6f7bf467b9

--- /dev/null

+++ b/Platform/Loongson/LoongArchQemuPkg/L= ibrary/LsRealTimeClockLib/LsRealTimeClockLib.inf

=40=40 -0,= 0 +1,44 =40=40

+=23=23 =40file

+=23

=
+=23 Copyright (c) 2022, Loongson Limited. All rights reserved.
+=23

+=23 SPDX-License-Identifier: BSD-2-Clause-P= atent

+=23

+=23=23

+

<= div>+=5BDefines=5D

+ IN=46=5FVERSION =3D 0x00010005
+ BASE=5FNAME =3D LsRealTimeClockLib

+ =46ILE=5FGUID= =3D 9793a3da-1869-4fdf-88b1-c6484341f50b

+ MODULE=5FTYPE =3D= BASE

+ VERSION=5FSTRING =3D 1.0

+ LIBRARY=5F= CLASS =3D RealTimeClockLib

+

+=23

+=23 VALID=5FARCHITECTURES =3D LOONGARCH64

+=23
+

+=5BSources=5D

+ LsRealTimeClockLib.c=

+

+=5BPackages=5D

+ MdePkg/Mde= Pkg.dec

+ EmbeddedPkg/EmbeddedPkg.dec

+ Platf= orm/Loongson/LoongArchQemuPkg/Loongson.dec

+

= +=5BLibraryClasses=5D

+ IoLib

+ UefiLib
=
+ DebugLib

+ PcdLib

+ DxeServicesTab= leLib

+ UefiRuntimeLib

+

+=5BGu= ids=5D

+ gEfiEventVirtualAddressChangeGuid

+<= /div>
+=5BProtocols=5D

+ gEfiRealTimeClockArchProto= colGuid =23 PROTOCOL ALWAYS=5FPRODUCED

+

+=5B= Depex=5D

+ TRUE

--

2.31.1
=
--636e1a33_1370bfa4_1e57b--