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.7355.1666336524540971617 for ; Fri, 21 Oct 2022 00:15:25 -0700 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 _____8AxjrcpRlJj01gBAA--.1121S3; Fri, 21 Oct 2022 15:11:37 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Dx_1cjRlJjNIwCAA--.10417S13; Fri, 21 Oct 2022 15:11:37 +0800 (CST) From: "xianglai" To: devel@edk2.groups.io Cc: quic_llindhol@quicinc.com, michael.d.kinney@intel.com, kraxel@redhat.com, ardb@kernel.org, maobibo@loongson.cn Subject: [edk2-platforms][PATCH V4 11/14] Platform/Loongson: Add RealTime Clock lib. Date: Fri, 21 Oct 2022 15:11:28 +0800 Message-Id: X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 X-CM-TRANSID: AQAAf8Dx_1cjRlJjNIwCAA--.10417S13 X-CM-SenderInfo: 5ol0xt5qjotxo6or00hjvr0hdfq/ X-Coremail-Antispam: 1Uk129KBjvAXoW3Zr4xAFyfWr17JF1fXw43Jrb_yoW8Jr17Ao WxtFWSqw48Jr18uasa9rykCrWIgF9Iqa1fXr1FqFWjyan8Ar1UtFyUta42gryfArykAwsx Kr93A3ykJFWaqFW8n29KB7ZKAUJUUUUf529EdanIXcx71UUUUU7KY7ZEXasCq-sGcSsGvf J3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnRJU UUkK1xkIjI8I6I8E6xAIw20EY4v20xvaj40_Wr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64 kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJwA2z4x0Y4vE2Ix0cI8IcVCY 1x0267AKxVWxJVW8Jr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aV CY1x0267AKxVWxJr0_GcWln4kS14v26r126r1DM2AIxVAIcxkEcVAq07x20xvEncxIr21l 57IF6xkI12xvs2x26I8E6xACxx1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6x8ErcxFaV Av8VWrMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwIxGrwCY1x0262kKe7AKxVWU AVWUtwCF04k20xvY0x0EwIxGrwCF04k20xvE74AGY7Cv6cx26rWl4I8I3I0E4IkC6x0Yz7 v_Jr0_Gr1l4IxYO2xFxVAFwI0_JF0_Jw1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8G jcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r126r1DMIIYrxkI7VAKI48JMIIF0xvE2I x0cI8IcVAFwI0_Ar0_tr1lIxAIcVC0I7IYx2IY6xkF7I0E14v26F4j6r4UJwCI42IY6xAI w20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Gr1j6F4UJwCI42IY6I8E87Iv6x kF7I0E14v26r4UJVWxJrUvcSsGvfC2KfnxnUUI43ZEXa7xRiTKZJUUUUU== X-Gw-Check: 6d5c5937937b7219 Content-Transfer-Encoding: quoted-printable This library provides interfaces such as real-time clock initialization to get time and setting time. REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4054 Signed-off-by: xianglai li --- .../LsRealTimeClockLib/LsRealTimeClock.h | 41 +++ .../LsRealTimeClockLib/LsRealTimeClockLib.c | 343 ++++++++++++++++++ .../LsRealTimeClockLib/LsRealTimeClockLib.inf | 41 +++ 3 files changed, 425 insertions(+) create mode 100644 Platform/Loongson/LoongArchQemuPkg/Library/LsRealTimeCl= ockLib/LsRealTimeClock.h create mode 100644 Platform/Loongson/LoongArchQemuPkg/Library/LsRealTimeCl= ockLib/LsRealTimeClockLib.c create mode 100644 Platform/Loongson/LoongArchQemuPkg/Library/LsRealTimeCl= ockLib/LsRealTimeClockLib.inf diff --git a/Platform/Loongson/LoongArchQemuPkg/Library/LsRealTimeClockLib/= LsRealTimeClock.h b/Platform/Loongson/LoongArchQemuPkg/Library/LsRealTimeCl= ockLib/LsRealTimeClock.h new file mode 100644 index 0000000000..c98881e5d0 --- /dev/null +++ b/Platform/Loongson/LoongArchQemuPkg/Library/LsRealTimeClockLib/LsRealT= imeClock.h @@ -0,0 +1,41 @@ +/** @file=0D + Implement EFI RealTimeClock runtime services via RTC Lib.=0D +=0D + Copyright (c) 2021, Loongson Limited. All rights reserved.=0D +=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=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 new file mode 100644 index 0000000000..78420ddfab --- /dev/null +++ b/Platform/Loongson/LoongArchQemuPkg/Library/LsRealTimeClockLib/LsRealT= imeClockLib.c @@ -0,0 +1,343 @@ +/** @file=0D + Implement EFI RealTimeClock runtime services via RTC Lib.=0D +=0D + Copyright (c) 2021, 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 +**/=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 +/**=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 +**/=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 +/**=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 +**/=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 +/**=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 +**/=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 +**/=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 new file mode 100644 index 0000000000..c985f7a727 --- /dev/null +++ b/Platform/Loongson/LoongArchQemuPkg/Library/LsRealTimeClockLib/LsRealT= imeClockLib.inf @@ -0,0 +1,41 @@ +## @file=0D +#=0D +# Copyright (c) 2021, 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 +[Sources.common]=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 --=20 2.31.1