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.5270.1646210711092986004 for ; Wed, 02 Mar 2022 00:45:11 -0800 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: loongson.cn, ip: 114.242.206.163, mailfrom: lixianglai@loongson.cn) Received: from localhost.localdomain (unknown [10.2.5.185]) by mail.loongson.cn (Coremail) with SMTP id AQAAf9Dxn89_Lh9i7TkBAA--.6283S14; Wed, 02 Mar 2022 16:44:54 +0800 (CST) From: "xianglai" To: devel@edk2.groups.io Subject: [edk2-platforms][PATCH V1 12/15] Platform/Loongson: Add RealTime Clock lib. Date: Wed, 2 Mar 2022 03:44:44 -0500 Message-Id: <20220302084447.2991355-13-lixianglai@loongson.cn> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220302084447.2991355-1-lixianglai@loongson.cn> References: <20220302084447.2991355-1-lixianglai@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: AQAAf9Dxn89_Lh9i7TkBAA--.6283S14 X-Coremail-Antispam: 1UD129KBjvJXoW3Zw48WF4rAr1DXr4xJF1xGrg_yoWkKw4fpF 4DuFZ8Kr4UJr4Sva93Xw4xGFs5Zas0kFW5JrZ8urnFk347Jr1DZr1qvFn5XFW7Xw47Ka4k Xay2gw4UuF98ur7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnUUvcSsGvfC2KfnxnUUI43ZEXa7xR_UUUUUUUUU== X-CM-SenderInfo: 5ol0xt5qjotxo6or00hjvr0hdfq/ Content-Transfer-Encoding: quoted-printable This library provides interfaces such as real-time clock initialization to get time and setting time. 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..74a5c629ab --- /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..cc5e426f03 --- /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) - 1;=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 + 1) << 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.27.0