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.web11.3591.1668157965452876158 for ; Fri, 11 Nov 2022 01:12:46 -0800 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: loongson.cn, ip: 114.242.206.163, mailfrom: lixianglai@loongson.cn) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8AxbdoLEm5joBMGAA--.19111S3; Fri, 11 Nov 2022 17:12:43 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8BxFlcAEm5jXc0QAA--.27651S13; Fri, 11 Nov 2022 17:12:42 +0800 (CST) From: "xianglai" To: devel@edk2.groups.io Cc: Bibo Mao , Chao Li , Leif Lindholm , Liming Gao , Michael D Kinney Subject: [edk2-platforms][PATCH V5 11/15] Platform/Loongson: Add RealTime Clock lib. Date: Fri, 11 Nov 2022 17:12:26 +0800 Message-Id: X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 X-CM-TRANSID: AQAAf8BxFlcAEm5jXc0QAA--.27651S13 X-CM-SenderInfo: 5ol0xt5qjotxo6or00hjvr0hdfq/ X-Coremail-Antispam: 1Uk129KBjvAXoW3CFy7tr18tr1DXrWkWFWfKrg_yoW8Jry7uo WxJFWSqw48Jr18uasa934kCrWIgFZIqa1Sqr1FqFWjyan8Ar1DtFyUta42gryxArykAwsx KryfA3ykJFWaqFW8n29KB7ZKAUJUUUU3529EdanIXcx71UUUUU7KY7ZEXasCq-sGcSsGvf J3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnRJU UUkG1xkIjI8I6I8E6xAIw20EY4v20xvaj40_Wr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64 kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJwA2z4x0Y4vE2Ix0cI8IcVCY 1x0267AKxVWxJVW8Jr1l84ACjcxK6I8E87Iv67AKxVWxJr0_GcWl84ACjcxK6I8E87Iv6x kF7I0E14v26F4UJVW0owAaw2AFwI0_JF0_Jw1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAq jxCEc2xF0cIa020Ex4CE44I27wAqx4xG64xvF2IEw4CE5I8CrVC2j2WlYx0E74AGY7Cv6c x26rWlOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcxkI7VAKI48JMxkF7I0En4kS14v26r12 6r1DMxAIw28IcxkI7VAKI48JMxAIw28IcVCjz48v1sIEY20_WwCFx2IqxVCFs4IE7xkEbV WUJVW8JwCFI7km07C267AKxVWUAVWUtwC20s026c02F40E14v26r1j6r18MI8I3I0E7480 Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_JF0_Jw1lIxkGc2Ij64vIr41lIxAIcVC0I7 IYx2IY67AKxVW7JVWDJwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1lIxAIcVCF04k2 6cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r4UJVWxJr1lIxAIcVC2z280aVCY1x 0267AKxVW8Jr0_Cr1UYxBIdaVFxhVjvjDU0xZFpf9x0zR9iSdUUUUU= Content-Transfer-Encoding: quoted-printable This library provides interfaces such as=0D real-time clock initialization=0D to get time and setting time.=0D =0D REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4054=0D =0D Cc: Bibo Mao =0D Cc: Chao Li =0D Cc: Leif Lindholm =0D Cc: Liming Gao =0D Cc: Michael D Kinney =0D Signed-off-by: xianglai li =0D ---=0D .../LsRealTimeClockLib/LsRealTimeClock.h | 40 +++=0D .../LsRealTimeClockLib/LsRealTimeClockLib.c | 335 ++++++++++++++++++=0D .../LsRealTimeClockLib/LsRealTimeClockLib.inf | 44 +++=0D 3 files changed, 419 insertions(+)=0D create mode 100644 Platform/Loongson/LoongArchQemuPkg/Library/LsRealTimeCl= ockLib/LsRealTimeClock.h=0D create mode 100644 Platform/Loongson/LoongArchQemuPkg/Library/LsRealTimeCl= ockLib/LsRealTimeClockLib.c=0D create mode 100644 Platform/Loongson/LoongArchQemuPkg/Library/LsRealTimeCl= ockLib/LsRealTimeClockLib.inf=0D =0D diff --git a/Platform/Loongson/LoongArchQemuPkg/Library/LsRealTimeClockLib/= LsRealTimeClock.h b/Platform/Loongson/LoongArchQemuPkg/Library/LsRealTimeCl= ockLib/LsRealTimeClock.h=0D new file mode 100644=0D index 0000000000..6567ec80db=0D --- /dev/null=0D +++ b/Platform/Loongson/LoongArchQemuPkg/Library/LsRealTimeClockLib/LsRealT= imeClock.h=0D @@ -0,0 +1,40 @@=0D +/** @file=0D + Implement EFI RealTimeClock runtime services via RTC Lib.=0D +=0D + Copyright (c) 2022, Loongson Limited. All rights reserved.=0D +=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#ifndef LS_REAL_TIME_CLOCK_H_=0D +#define LS_REAL_TIME_CLOCK_H_=0D +=0D +#define TOY_WRITE0_REG 0x24=0D +#define TOY_WRITE1_REG 0x28=0D +#define TOY_READ0_REG 0x2c=0D +#define TOY_READ1_REG 0x30=0D +#define RTC_CTRL_REG 0x40=0D +=0D +/* TOY Enable bits */=0D +#define RTC_ENABLE_BIT (1UL << 13)=0D +#define TOY_ENABLE_BIT (1UL << 11)=0D +#define OSC_ENABLE_BIT (1UL << 8)=0D +=0D +/*=0D + * shift bits and filed mask=0D + */=0D +#define TOY_MON_MASK 0x3f=0D +#define TOY_DAY_MASK 0x1f=0D +#define TOY_HOUR_MASK 0x1f=0D +#define TOY_MIN_MASK 0x3f=0D +#define TOY_SEC_MASK 0x3f=0D +#define TOY_MSEC_MASK 0xf=0D +=0D +#define TOY_MON_SHIFT 26=0D +#define TOY_DAY_SHIFT 21=0D +#define TOY_HOUR_SHIFT 16=0D +#define TOY_MIN_SHIFT 10=0D +#define TOY_SEC_SHIFT 4=0D +=0D +#endif // LS_REAL_TIME_CLOCK_H_=0D diff --git a/Platform/Loongson/LoongArchQemuPkg/Library/LsRealTimeClockLib/= LsRealTimeClockLib.c b/Platform/Loongson/LoongArchQemuPkg/Library/LsRealTim= eClockLib/LsRealTimeClockLib.c=0D new file mode 100644=0D index 0000000000..dc651ccaaf=0D --- /dev/null=0D +++ b/Platform/Loongson/LoongArchQemuPkg/Library/LsRealTimeClockLib/LsRealT= imeClockLib.c=0D @@ -0,0 +1,335 @@=0D +/** @file=0D + Implement EFI RealTimeClock runtime services via RTC Lib.=0D +=0D + Copyright (c) 2022, Loongson Limited. All rights reserved.=0D +=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include =0D +#include =0D +=0D +#include =0D +#include =0D +=0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include "LsRealTimeClock.h"=0D +=0D +STATIC BOOLEAN mInitialized =3D FALSE;=0D +STATIC EFI_EVENT mRtcVirtualAddrChangeEvent;=0D +STATIC UINTN mRtcBase =3D 0x100d0100;=0D +/*=0D + Enable Real-time clock.=0D +=0D + @param VOID=0D +=0D + @retval VOID=0D + */=0D +VOID=0D +InitRtc (=0D + VOID=0D + )=0D +{=0D + UINTN Val;=0D +=0D + if (!mInitialized) {=0D + /* enable rtc */=0D + Val =3D MmioRead32 (mRtcBase + RTC_CTRL_REG);=0D + Val |=3D TOY_ENABLE_BIT | OSC_ENABLE_BIT;=0D + MmioWrite32 (mRtcBase + RTC_CTRL_REG, Val);=0D + mInitialized =3D TRUE;=0D + }=0D +}=0D +=0D +/**=0D + Returns the current time and date information, and the time-keeping capa= bilities=0D + of the hardware platform.=0D +=0D + @param Time A pointer to storage to receive a snapsho= t of the current time.=0D + @param Capabilities An optional pointer to a buffer to receiv= e the real time clock=0D + device's capabilities.=0D +=0D + @retval EFI_SUCCESS The operation completed successfully.=0D + @retval EFI_INVALID_PARAMETER Time is NULL.=0D + @retval EFI_DEVICE_ERROR The time could not be retrieved due to ha= rdware error.=0D + @retval EFI_SECURITY_VIOLATION The time could not be retrieved due to an= authentication failure.=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +LibGetTime (=0D + OUT EFI_TIME *Time,=0D + OUT EFI_TIME_CAPABILITIES *Capabilities=0D + )=0D +{=0D + UINT32 Val;=0D +=0D + // Ensure Time is a valid pointer=0D + if (Time =3D=3D NULL) {=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + InitRtc ();=0D + Val =3D MmioRead32 (mRtcBase + TOY_READ1_REG);=0D + Time->Year =3D Val + 1900;=0D +=0D + Val =3D MmioRead32 (mRtcBase + TOY_READ0_REG);=0D + Time->Month =3D (Val >> TOY_MON_SHIFT) & TOY_MON_MASK;=0D + Time->Day =3D (Val >> TOY_DAY_SHIFT) & TOY_DAY_MASK;=0D + Time->Hour =3D (Val >> TOY_HOUR_SHIFT) & TOY_HOUR_MASK;=0D + Time->Minute =3D (Val >> TOY_MIN_SHIFT) & TOY_MIN_MASK;=0D + Time->Second =3D (Val >> TOY_SEC_SHIFT) & TOY_SEC_MASK;=0D + Time->Nanosecond =3D 0;=0D + return EFI_SUCCESS;=0D +}=0D +=0D +/**=0D + Sets the current local time and date information.=0D +=0D + @param Time A pointer to the current time.=0D +=0D + @retval EFI_SUCCESS The operation completed successfully.=0D + @retval EFI_INVALID_PARAMETER A time field is out of range.=0D + @retval EFI_DEVICE_ERROR The time could not be set due due to hardw= are error.=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +LibSetTime (=0D + IN EFI_TIME *Time=0D + )=0D +{=0D + UINT32 Val;=0D +=0D + // Initialize the hardware if not already done=0D + InitRtc ();=0D +=0D + Val =3D 0;=0D + Val |=3D (Time->Second << TOY_SEC_SHIFT);=0D + Val |=3D (Time->Minute << TOY_MIN_SHIFT);=0D + Val |=3D (Time->Hour << TOY_HOUR_SHIFT);=0D + Val |=3D (Time->Day << TOY_DAY_SHIFT);=0D + Val |=3D (Time->Month << TOY_MON_SHIFT);=0D + MmioWrite32 (mRtcBase + TOY_WRITE0_REG, Val);=0D +=0D + Val =3D Time->Year - 1900;=0D + MmioWrite32 (mRtcBase + TOY_WRITE1_REG, Val);=0D + return EFI_SUCCESS;=0D +}=0D +=0D +/**=0D + Returns the current wakeup alarm clock setting.=0D +=0D + @param Enabled Indicates if the alarm is currently enable= d or disabled.=0D + @param Pending Indicates if the alarm signal is pending a= nd requires acknowledgement.=0D + @param Time The current alarm setting.=0D +=0D + @retval EFI_SUCCESS The alarm settings were returned.=0D + @retval EFI_INVALID_PARAMETER Any parameter is NULL.=0D + @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due= to a hardware error.=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +LibGetWakeupTime (=0D + OUT BOOLEAN *Enabled,=0D + OUT BOOLEAN *Pending,=0D + OUT EFI_TIME *Time=0D + )=0D +{=0D + // Not a required feature=0D + return EFI_UNSUPPORTED;=0D +}=0D +=0D +/**=0D + Sets the system wakeup alarm clock time.=0D +=0D + @param Enabled Enable or disable the wakeup alarm.=0D + @param Time If Enable is TRUE, the time to set the wak= eup alarm for.=0D +=0D + @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm w= as enabled. If=0D + Enable is FALSE, then the wakeup alarm was= disabled.=0D + @retval EFI_INVALID_PARAMETER A time field is out of range.=0D + @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a = hardware error.=0D + @retval EFI_UNSUPPORTED A wakeup timer is not supported on this pl= atform.=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +LibSetWakeupTime (=0D + IN BOOLEAN Enabled,=0D + OUT EFI_TIME *Time=0D + )=0D +{=0D + // Not a required feature=0D + return EFI_UNSUPPORTED;=0D +}=0D +=0D +/**=0D + Fixup internal data so that EFI can be call in virtual mode.=0D + Call the passed in Child Notify event and convert any pointers in=0D + lib to virtual mode.=0D +=0D + @param[in] Event The Event that is being processed=0D + @param[in] Context Event Context=0D +**/=0D +VOID=0D +EFIAPI=0D +LibRtcVirtualNotifyEvent (=0D + IN EFI_EVENT Event,=0D + IN VOID *Context=0D + )=0D +{=0D + //=0D + // Only needed if you are going to support the OS calling RTC functions = in virtual mode.=0D + // You will need to call EfiConvertPointer (). To convert any stored phy= sical addresses=0D + // to virtual address. After the OS transitions to calling in virtual mo= de, all future=0D + // runtime calls will be made in virtual mode.=0D + //=0D + EfiConvertPointer (0x0, (VOID**)&mRtcBase);=0D + return;=0D +}=0D +=0D +/** Add the RTC controller address range to the memory map.=0D +=0D + @param [in] ImageHandle The handle to the image.=0D + @param [in] RtcPageBase Base address of the RTC controller.=0D +=0D + @retval EFI_SUCCESS Success.=0D + @retval EFI_INVALID_PARAMETER A parameter is invalid.=0D + @retval EFI_NOT_FOUND Flash device not found.=0D +**/=0D +EFI_STATUS=0D +KvmtoolRtcMapMemory (=0D + IN EFI_HANDLE ImageHandle,=0D + IN EFI_PHYSICAL_ADDRESS RtcPageBase=0D + )=0D +{=0D + EFI_STATUS Status;=0D +=0D + Status =3D gDS->AddMemorySpace (=0D + EfiGcdMemoryTypeMemoryMappedIo,=0D + RtcPageBase,=0D + EFI_PAGE_SIZE,=0D + EFI_MEMORY_UC | EFI_MEMORY_RUNTIME=0D + );=0D + if (EFI_ERROR (Status)) {=0D + DEBUG ((=0D + DEBUG_ERROR, "Failed to add memory space. Status =3D %r\n",=0D + Status=0D + ));=0D + return Status;=0D + }=0D +=0D + Status =3D gDS->AllocateMemorySpace (=0D + EfiGcdAllocateAddress,=0D + EfiGcdMemoryTypeMemoryMappedIo,=0D + 0,=0D + EFI_PAGE_SIZE,=0D + &RtcPageBase,=0D + ImageHandle,=0D + NULL=0D + );=0D + if (EFI_ERROR (Status)) {=0D + DEBUG ((=0D + DEBUG_ERROR,=0D + "Failed to allocate memory space. Status =3D %r\n",=0D + Status=0D + ));=0D + gDS->RemoveMemorySpace (=0D + RtcPageBase,=0D + EFI_PAGE_SIZE=0D + );=0D + return Status;=0D + }=0D +=0D + Status =3D gDS->SetMemorySpaceAttributes (=0D + RtcPageBase,=0D + EFI_PAGE_SIZE,=0D + EFI_MEMORY_UC | EFI_MEMORY_RUNTIME=0D + );=0D + if (EFI_ERROR (Status)) {=0D + DEBUG ((=0D + DEBUG_ERROR,=0D + "Failed to set memory attributes. Status =3D %r\n",=0D + Status=0D + ));=0D +=0D + gDS->FreeMemorySpace (=0D + RtcPageBase,=0D + EFI_PAGE_SIZE=0D + );=0D +=0D + gDS->RemoveMemorySpace (=0D + RtcPageBase,=0D + EFI_PAGE_SIZE=0D + );=0D + }=0D +=0D + return Status;=0D +}=0D +=0D +/**=0D + This is the declaration of an EFI image entry point. This can be the ent= ry point to an application=0D + written to this specification, an EFI boot service driver, or an EFI run= time driver.=0D +=0D + @param ImageHandle Handle that identifies the loaded image.=0D + @param SystemTable System Table for this image.=0D +=0D + @retval EFI_SUCCESS The operation completed successfully.=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +LibRtcInitialize (=0D + IN EFI_HANDLE ImageHandle,=0D + IN EFI_SYSTEM_TABLE *SystemTable=0D + )=0D +{=0D + EFI_STATUS Status;=0D + EFI_HANDLE Handle;=0D +=0D + Status =3D KvmtoolRtcMapMemory (ImageHandle, (mRtcBase & ~EFI_PAGE_MASK)= );=0D + if (EFI_ERROR (Status)) {=0D + DEBUG ((=0D + DEBUG_ERROR,=0D + "Failed to map memory for loongson 7A RTC. Status =3D %r\n",=0D + Status=0D + ));=0D + return Status;=0D + }=0D +=0D + // Setup the setters and getters=0D + gRT->GetTime =3D LibGetTime;=0D + gRT->SetTime =3D LibSetTime;=0D +=0D + // Install the protocol=0D + Handle =3D NULL;=0D + Status =3D gBS->InstallMultipleProtocolInterfaces (=0D + &Handle,=0D + &gEfiRealTimeClockArchProtocolGuid, NULL,=0D + NULL=0D + );=0D + ASSERT_EFI_ERROR (Status);=0D +=0D + //=0D + // Register for the virtual address change event=0D + //=0D + Status =3D gBS->CreateEventEx (=0D + EVT_NOTIFY_SIGNAL,=0D + TPL_NOTIFY,=0D + LibRtcVirtualNotifyEvent,=0D + NULL,=0D + &gEfiEventVirtualAddressChangeGuid,=0D + &mRtcVirtualAddrChangeEvent=0D + );=0D + ASSERT_EFI_ERROR (Status);=0D + return Status;=0D +}=0D diff --git a/Platform/Loongson/LoongArchQemuPkg/Library/LsRealTimeClockLib/= LsRealTimeClockLib.inf b/Platform/Loongson/LoongArchQemuPkg/Library/LsRealT= imeClockLib/LsRealTimeClockLib.inf=0D new file mode 100644=0D index 0000000000..6f7bf467b9=0D --- /dev/null=0D +++ b/Platform/Loongson/LoongArchQemuPkg/Library/LsRealTimeClockLib/LsRealT= imeClockLib.inf=0D @@ -0,0 +1,44 @@=0D +## @file=0D +#=0D +# Copyright (c) 2022, Loongson Limited. All rights reserved.=0D +#=0D +# SPDX-License-Identifier: BSD-2-Clause-Patent=0D +#=0D +##=0D +=0D +[Defines]=0D + INF_VERSION =3D 0x00010005=0D + BASE_NAME =3D LsRealTimeClockLib=0D + FILE_GUID =3D 9793a3da-1869-4fdf-88b1-c6484341f50b= =0D + MODULE_TYPE =3D BASE=0D + VERSION_STRING =3D 1.0=0D + LIBRARY_CLASS =3D RealTimeClockLib=0D +=0D +#=0D +# VALID_ARCHITECTURES =3D LOONGARCH64=0D +#=0D +=0D +[Sources]=0D + LsRealTimeClockLib.c=0D +=0D +[Packages]=0D + MdePkg/MdePkg.dec=0D + EmbeddedPkg/EmbeddedPkg.dec=0D + Platform/Loongson/LoongArchQemuPkg/Loongson.dec=0D +=0D +[LibraryClasses]=0D + IoLib=0D + UefiLib=0D + DebugLib=0D + PcdLib=0D + DxeServicesTableLib=0D + UefiRuntimeLib=0D +=0D +[Guids]=0D + gEfiEventVirtualAddressChangeGuid=0D +=0D +[Protocols]=0D + gEfiRealTimeClockArchProtocolGuid # PROTOCOL ALWAYS_PRODUCED= =0D +=0D +[Depex]=0D + TRUE=0D -- =0D 2.31.1=0D =0D