Message ID | 20171103101654.30973-2-ard.biesheuvel@linaro.org |
---|---|
State | Superseded |
Headers | show |
Series | add I2C RTC support to DeveloperBox | expand |
Thanks Ard. This will allow to use PCF8563 on any platform not limiting to targeted platform. Few comments are inline. Regards Udit > -----Original Message----- > From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Ard > Biesheuvel > Sent: Friday, November 03, 2017 3:47 PM > To: edk2-devel@lists.01.org; leif.lindholm@linaro.org; > daniel.thompson@linaro.org > Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org> > Subject: [edk2] [RFC PATCH edk2-platforms 1/3] Silicon/NXP: add RTC support > library for PCF8563 I2C IP > > Add a RealTimeClockLib implementation for the NXP PCF8563 as used on > the Socionext Developer Box board. Note that the standard I2C protocol > stack does not support runtime use, so this driver invokes the I2C master > protocol directly. This requires support from the platform side as well, > and so this driver will only attach to a I2C master that has the > gPcf8563RealTimeClockLibI2cMasterProtolGuid protocol installed on its > handle. It is up to the platform to ensure that the driver producing > the I2C master protocol in question is runtime capable, and is not > shared with the I2C protocol stack (i.e., it should not have the I2C > Bus Configuration Management protocol installed as well). > > Contributed-under: TianoCore Contribution Agreement 1.1 > Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> > --- > Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.c | > 385 ++++++++++++++++++++ > Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.dec | > 29 ++ > Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.inf | > 52 +++ > 3 files changed, 466 insertions(+) > > diff --git > a/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.c > b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.c > new file mode 100644 > index 000000000000..fea65a225d7f > --- /dev/null > +++ > b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.c > @@ -0,0 +1,385 @@ > +/** @file > + > + Copyright (c) 2017, Linaro, Ltd. All rights reserved.<BR> > + > + This program and the accompanying materials > + are licensed and made available under the terms and conditions of the BSD > License > + which accompanies this distribution. The full text of the license may be found > at > + > https://emea01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fopenso > urce.org%2Flicenses%2Fbsd- > license.php&data=02%7C01%7Cudit.kumar%40nxp.com%7C6faa68073eef4ed07 > 25208d522a40d65%7C686ea1d3bc2b4c6fa92cd99c5c301635%7C0%7C0%7C636 > 453010374642660&sdata=vDhroHNG20VCsMtj7%2FggWcWRG8hwt5fbV1Kv4R > Na98Q%3D&reserved=0 > + > + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" > BASIS, > + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER > EXPRESS OR IMPLIED. > + > +**/ > + > +#include <PiDxe.h> > +#include <Library/DebugLib.h> > +#include <Library/IoLib.h> > +#include <Library/RealTimeClockLib.h> > +#include <Library/UefiBootServicesTableLib.h> > +#include <Library/UefiLib.h> > +#include <Library/UefiRuntimeLib.h> > +#include <Protocol/I2cMaster.h> > + > +#define SLAVE_ADDRESS (FixedPcdGet8 (PcdI2cSlaveAddress)) > +#define PCF8563_DATA_REG_OFFSET 0x2 > + > +#define PCF8563_SECONDS_MASK 0x7f > +#define PCF8563_MINUTES_MASK 0x7f > +#define PCF8563_HOURS_MASK 0x3f > +#define PCF8563_DAYS_MASK 0x3f > +#define PCF8563_WEEKDAYS_MASK 0x07 > +#define PCF8563_MONTHS_MASK 0x1f > +#define PCF8563_CENTURY_MASK 0x80 > + > +#define EPOCH_BASE 2000 > + > +STATIC EFI_HANDLE mI2cMasterHandle; > +STATIC VOID *mDriverEventRegistration; > +STATIC EFI_I2C_MASTER_PROTOCOL *mI2cMaster; > +STATIC EFI_EVENT mRtcVirtualAddrChangeEvent; > + > +typedef struct { > + UINTN OperationCount; > + EFI_I2C_OPERATION SetAddressOp; > + EFI_I2C_OPERATION GetSetDateTimeOp; > +} RTC_I2C_REQUEST; > + > +#pragma pack(1) > +typedef struct { > + UINT8 VL_seconds; > + UINT8 Minutes; > + UINT8 Hours; > + UINT8 Days; > + UINT8 Weekdays; > + UINT8 Century_months; > + UINT8 Years; > +} RTC_DATETIME; > +#pragma pack() > + > +/** > + Returns the current time and date information, and the time-keeping > + capabilities of the hardware platform. > + > + @param Time A pointer to storage to receive a snapshot of > + the current time. > + @param Capabilities An optional pointer to a buffer to receive the > + real time clock device's capabilities. > + > + @retval EFI_SUCCESS The operation completed successfully. > + @retval EFI_INVALID_PARAMETER Time is NULL. > + @retval EFI_DEVICE_ERROR The time could not be retrieved due to > hardware > + error. > + > +**/ > +EFI_STATUS > +EFIAPI > +LibGetTime ( > + OUT EFI_TIME *Time, > + OUT EFI_TIME_CAPABILITIES *Capabilities > + ) > +{ > + RTC_I2C_REQUEST Op; > + RTC_DATETIME DateTime; > + EFI_STATUS Status; > + UINT8 Reg; > + > + if (Time == NULL) { > + return EFI_INVALID_PARAMETER; > + } > + > + if (mI2cMaster == NULL) { > + return EFI_DEVICE_ERROR; > + } > + > + Reg = PCF8563_DATA_REG_OFFSET; > + > + Op.OperationCount = 2; > + > + Op.SetAddressOp.Flags = 0; > + Op.SetAddressOp.LengthInBytes = 1; > + Op.SetAddressOp.Buffer = &Reg; > + > + Op.GetSetDateTimeOp.Flags = I2C_FLAG_READ; > + Op.GetSetDateTimeOp.LengthInBytes = sizeof (RTC_DATETIME); > + Op.GetSetDateTimeOp.Buffer = (VOID *)&DateTime; > + > + Status = mI2cMaster->StartRequest (mI2cMaster, SLAVE_ADDRESS, > + (VOID *)&Op, NULL, NULL); > + if (EFI_ERROR (Status)) { > + return EFI_DEVICE_ERROR; > + } > + > + Time->Second = BcdToDecimal8 (DateTime.VL_seconds & > PCF8563_SECONDS_MASK); > + Time->Minute = BcdToDecimal8 (DateTime.Minutes & > PCF8563_MINUTES_MASK); > + Time->Hour = BcdToDecimal8 (DateTime.Hours & PCF8563_HOURS_MASK); > + Time->Day = BcdToDecimal8 (DateTime.Days & PCF8563_DAYS_MASK); > + Time->Month = BcdToDecimal8 (DateTime.Century_months & > PCF8563_MONTHS_MASK); > + Time->Year = BcdToDecimal8 (DateTime.Years) + EPOCH_BASE; > + > + if (DateTime.Century_months & PCF8563_CENTURY_MASK) { > + Time->Year += 100; > + } > + > + Time->Daylight = EFI_TIME_ADJUST_DAYLIGHT; > + Time->TimeZone = 0; /* UTC */ Need to take care of time-zone as well. > + > + if (Capabilities != NULL) { > + Capabilities->Resolution = 1; > + Capabilities->Accuracy = 0; > + Capabilities->SetsToZero = TRUE; > + } > + return EFI_SUCCESS; > +} > + > + > +/** > + Sets the current local time and date information. > + > + @param Time A pointer to the current time. > + > + @retval EFI_SUCCESS The operation completed successfully. > + @retval EFI_INVALID_PARAMETER A time field is out of range. > + @retval EFI_DEVICE_ERROR The time could not be set due due to > hardware > + error. > + > +**/ > +EFI_STATUS > +EFIAPI > +LibSetTime ( > + IN EFI_TIME *Time > + ) > +{ > + RTC_I2C_REQUEST Op; > + RTC_DATETIME DateTime; > + EFI_STATUS Status; > + UINT8 Reg; > + > + if (mI2cMaster == NULL) { > + return EFI_DEVICE_ERROR; > + } Please see, if check can be added for time validity > + > + DateTime.VL_seconds = DecimalToBcd8 (Time->Second); > + DateTime.Minutes = DecimalToBcd8 (Time->Minute); > + DateTime.Hours = DecimalToBcd8 (Time->Hour); > + DateTime.Days = DecimalToBcd8 (Time->Day); > + DateTime.Century_months = DecimalToBcd8 (Time->Month); > + DateTime.Years = DecimalToBcd8 (Time->Year % 100); > + if (Time->Year >= EPOCH_BASE + 100) { > + DateTime.Century_months |= PCF8563_CENTURY_MASK; > + } > + > + Reg = PCF8563_DATA_REG_OFFSET; > + > + Op.OperationCount = 2; > + > + Op.SetAddressOp.Flags = 0; > + Op.SetAddressOp.LengthInBytes = 1; > + Op.SetAddressOp.Buffer = &Reg; > + > + Op.GetSetDateTimeOp.Flags = 0; > + Op.GetSetDateTimeOp.LengthInBytes = sizeof (RTC_DATETIME); > + Op.GetSetDateTimeOp.Buffer = (VOID *)&DateTime; > + > + Status = mI2cMaster->StartRequest (mI2cMaster, SLAVE_ADDRESS, > + (VOID *)&Op, NULL, NULL); > + if (EFI_ERROR (Status)) { > + return EFI_DEVICE_ERROR; > + } > + return EFI_SUCCESS; > +} > + > + > +/** > + Returns the current wakeup alarm clock setting. > + > + @param Enabled Indicates if the alarm is currently enabled or > + disabled. > + @param Pending Indicates if the alarm signal is pending and > + requires acknowledgement. > + @param Time The current alarm setting. > + > + @retval EFI_SUCCESS The alarm settings were returned. > + @retval EFI_INVALID_PARAMETER Any parameter is NULL. > + @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due > to a > + hardware error. > + > +**/ > +EFI_STATUS > +EFIAPI > +LibGetWakeupTime ( > + OUT BOOLEAN *Enabled, > + OUT BOOLEAN *Pending, > + OUT EFI_TIME *Time > + ) > +{ > + return EFI_UNSUPPORTED; > +} > + > + > +/** > + Sets the system wakeup alarm clock time. > + > + @param Enabled Enable or disable the wakeup alarm. > + @param Time If Enable is TRUE, the time to set the wakeup > + alarm for. > + > + @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was > + enabled. If Enable is FALSE, then the wakeup > + alarm was disabled. > + @retval EFI_INVALID_PARAMETER A time field is out of range. > + @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a > + hardware error. > + @retval EFI_UNSUPPORTED A wakeup timer is not supported on this > + platform. > + > +**/ > +EFI_STATUS > +EFIAPI > +LibSetWakeupTime ( > + IN BOOLEAN Enabled, > + OUT EFI_TIME *Time > + ) > +{ > + return EFI_UNSUPPORTED; > +} > + > +STATIC > +VOID > +DriverRegistrationEvent ( > + IN EFI_EVENT Event, > + IN VOID *Context > + ) > +{ > + EFI_HANDLE Handle[2]; > + UINTN BufferSize; > + EFI_STATUS Status; > + EFI_I2C_MASTER_PROTOCOL *I2cMaster; > + UINTN BusFrequency; > + > + // > + // Try to connect the newly registered driver to our handle. > + // > + do { > + BufferSize = sizeof (EFI_HANDLE); > + Status = gBS->LocateHandle (ByRegisterNotify, > + &gEfiDriverBindingProtocolGuid, > + mDriverEventRegistration, > + &BufferSize, > + Handle); > + if (EFI_ERROR (Status)) { > + if (Status != EFI_NOT_FOUND) { > + DEBUG ((DEBUG_WARN, "%a: gBS->LocateHandle () returned %r\n", > + __FUNCTION__, Status)); > + } > + break; > + } > + > + // > + // Check if we can connect our handle to this driver. > + // > + Handle[1] = NULL; > + Status = gBS->ConnectController (mI2cMasterHandle, Handle, NULL, FALSE); > + if (EFI_ERROR (Status)) { > + continue; > + } > + > + DEBUG ((DEBUG_INFO, "%a: found I2C master!\n", __FUNCTION__)); > + > + gBS->CloseEvent (Event); > + > + Status = gBS->OpenProtocol (mI2cMasterHandle, > &gEfiI2cMasterProtocolGuid, > + (VOID **)&I2cMaster, gImageHandle, NULL, > + EFI_OPEN_PROTOCOL_EXCLUSIVE); > + ASSERT_EFI_ERROR (Status); > + > + Status = I2cMaster->Reset (I2cMaster); > + if (EFI_ERROR (Status)) { > + DEBUG ((DEBUG_ERROR, "%a: I2CMaster->Reset () failed - %r\n", > + __FUNCTION__, Status)); > + break; > + } > + > + BusFrequency = FixedPcdGet16 (PcdI2cBusFrequency); > + Status = I2cMaster->SetBusFrequency (I2cMaster, &BusFrequency); > + if (EFI_ERROR (Status)) { > + DEBUG ((DEBUG_ERROR, "%a: I2CMaster->SetBusFrequency () failed - > %r\n", > + __FUNCTION__, Status)); > + break; > + } > + > + mI2cMaster = I2cMaster; > + break; > + } while (TRUE); > +} > + > +/** > + Fixup internal data so that EFI can be call in virtual mode. > + Call the passed in Child Notify event and convert any pointers in > + lib to virtual mode. > + > + @param[in] Event The Event that is being processed > + @param[in] Context Event Context > +**/ > +VOID > +EFIAPI > +LibRtcVirtualNotifyEvent ( > + IN EFI_EVENT Event, > + IN VOID *Context > + ) > +{ > + EfiConvertPointer (0x0, (VOID **)&mI2cMaster); > +} > + > +/** > + Library entry point > + > + @param ImageHandle Handle that identifies the loaded image. > + @param SystemTable System Table for this image. > + > + @retval EFI_SUCCESS The operation completed successfully. > + > +**/ > +EFI_STATUS > +EFIAPI > +LibRtcInitialize ( > + IN EFI_HANDLE ImageHandle, > + IN EFI_SYSTEM_TABLE *SystemTable > + ) > +{ > + EFI_STATUS Status; > + UINTN BufferSize; > + > + // > + // Find the handle that marks the controller > + // that will provide the I2C master protocol. > + // > + BufferSize = sizeof (EFI_HANDLE); > + Status = gBS->LocateHandle (ByProtocol, > + &gPcf8563RealTimeClockLibI2cMasterProtolGuid, NULL, > + &BufferSize, &mI2cMasterHandle); > + ASSERT_EFI_ERROR (Status); > + > + // > + // Register a protocol registration notification callback on the driver > + // binding protocol so we can attempt to connect our I2C master to it > + // as soon as it appears. > + // > + EfiCreateProtocolNotifyEvent ( > + &gEfiDriverBindingProtocolGuid, > + TPL_CALLBACK, > + DriverRegistrationEvent, > + NULL, > + &mDriverEventRegistration); > + > + // > + // Register for the virtual address change event > + // > + Status = gBS->CreateEventEx (EVT_NOTIFY_SIGNAL, TPL_NOTIFY, > + LibRtcVirtualNotifyEvent, NULL, > + &gEfiEventVirtualAddressChangeGuid, > + &mRtcVirtualAddrChangeEvent); > + ASSERT_EFI_ERROR (Status); > + > + return EFI_SUCCESS; > +} > diff --git > a/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.dec > b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.dec > new file mode 100644 > index 000000000000..bc8fc60c0602 > --- /dev/null > +++ > b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.dec > @@ -0,0 +1,29 @@ > +#/** @file > +# > +# Copyright (c) 2017, Linaro, Ltd. All rights reserved.<BR> > +# > +# This program and the accompanying materials > +# are licensed and made available under the terms and conditions of the BSD > License > +# which accompanies this distribution. The full text of the license may be found > at > +# > https://emea01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fopenso > urce.org%2Flicenses%2Fbsd- > license.php&data=02%7C01%7Cudit.kumar%40nxp.com%7C6faa68073eef4ed07 > 25208d522a40d65%7C686ea1d3bc2b4c6fa92cd99c5c301635%7C0%7C0%7C636 > 453010374642660&sdata=vDhroHNG20VCsMtj7%2FggWcWRG8hwt5fbV1Kv4R > Na98Q%3D&reserved=0 > +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" > BASIS, > +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER > EXPRESS OR IMPLIED. > +# > +# > +#**/ > + > +[Defines] > + DEC_SPECIFICATION = 0x0001001A > + PACKAGE_NAME = Pcf8563RealTimeClockLib > + PACKAGE_GUID = 44d63668-c86b-446a-b303-f3a5176702aa > + PACKAGE_VERSION = 0.1 > + > +[Guids] > + gPcf8563RealTimeClockLibTokenSpaceGuid = { 0xaaf5b169, 0x93a0, 0x4d60, { > 0xba, 0xe4, 0x06, 0x07, 0x92, 0x8e, 0x63, 0xdd }} > + > +[Protocols] > + gPcf8563RealTimeClockLibI2cMasterProtolGuid = { 0xa6af18ae, 0x3bd5, > 0x4af9, { 0xbb, 0x6a, 0xdb, 0x85, 0x07, 0x62, 0x81, 0x38 }} > + > +[PcdsFixedAtBuild] > + > gPcf8563RealTimeClockLibTokenSpaceGuid.PcdI2cSlaveAddress|0x51|UINT8|0 > x00000001 > + > gPcf8563RealTimeClockLibTokenSpaceGuid.PcdI2cBusFrequency|400|UINT16|0 > x00000002 > diff --git > a/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.inf > b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.inf > new file mode 100644 > index 000000000000..e1e001162144 > --- /dev/null > +++ > b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.inf > @@ -0,0 +1,52 @@ > +#/** @file > +# > +# Copyright (c) 2017, Linaro, Ltd. All rights reserved.<BR> > +# > +# This program and the accompanying materials > +# are licensed and made available under the terms and conditions of the BSD > License > +# which accompanies this distribution. The full text of the license may be found > at > +# > https://emea01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fopenso > urce.org%2Flicenses%2Fbsd- > license.php&data=02%7C01%7Cudit.kumar%40nxp.com%7C6faa68073eef4ed07 > 25208d522a40d65%7C686ea1d3bc2b4c6fa92cd99c5c301635%7C0%7C0%7C636 > 453010374642660&sdata=vDhroHNG20VCsMtj7%2FggWcWRG8hwt5fbV1Kv4R > Na98Q%3D&reserved=0 > +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" > BASIS, > +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER > EXPRESS OR IMPLIED. > +# > +# > +#**/ > + > +[Defines] > + INF_VERSION = 0x0001001A > + BASE_NAME = Pcf8563RealTimeClockLib > + FILE_GUID = 1e3d33f3-a671-4116-bd81-6b435296c1e0 > + MODULE_TYPE = DXE_DRIVER > + VERSION_STRING = 1.0 > + LIBRARY_CLASS = RealTimeClockLib > + > +[Sources.common] > + Pcf8563RealTimeClockLib.c > + > +[Packages] > + MdePkg/MdePkg.dec > + EmbeddedPkg/EmbeddedPkg.dec > + Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.dec > + > +[LibraryClasses] > + BaseMemoryLib > + DebugLib > + IoLib > + UefiBootServicesTableLib > + UefiLib > + UefiRuntimeLib > + > +[Guids] > + gEfiEventVirtualAddressChangeGuid > + > +[Protocols] > + gEfiDriverBindingProtocolGuid ## CONSUMES > + gEfiI2cMasterProtocolGuid ## CONSUMES > + gPcf8563RealTimeClockLibI2cMasterProtolGuid ## CONSUMES > + > +[FixedPcd] > + gPcf8563RealTimeClockLibTokenSpaceGuid.PcdI2cSlaveAddress > + gPcf8563RealTimeClockLibTokenSpaceGuid.PcdI2cBusFrequency > + > +[Depex] > + gPcf8563RealTimeClockLibI2cMasterProtolGuid > -- > 2.11.0 > > _______________________________________________ > edk2-devel mailing list > edk2-devel@lists.01.org > https://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.01 > .org%2Fmailman%2Flistinfo%2Fedk2- > devel&data=02%7C01%7Cudit.kumar%40nxp.com%7C6faa68073eef4ed072520 > 8d522a40d65%7C686ea1d3bc2b4c6fa92cd99c5c301635%7C0%7C0%7C6364530 > 10374642660&sdata=vr4qAgDuDX0M7MGeuS%2FKAY%2B%2FqhVmz0%2FMrEx > K42tQIbM%3D&reserved=0 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel
On 6 November 2017 at 11:09, Udit Kumar <udit.kumar@nxp.com> wrote: > Thanks Ard. > This will allow to use PCF8563 on any platform not limiting to targeted platform. Yes, that was the intention. > Few comments are inline. > Thanks. > >> -----Original Message----- >> From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Ard >> Biesheuvel >> Sent: Friday, November 03, 2017 3:47 PM >> To: edk2-devel@lists.01.org; leif.lindholm@linaro.org; >> daniel.thompson@linaro.org >> Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org> >> Subject: [edk2] [RFC PATCH edk2-platforms 1/3] Silicon/NXP: add RTC support >> library for PCF8563 I2C IP >> >> Add a RealTimeClockLib implementation for the NXP PCF8563 as used on >> the Socionext Developer Box board. Note that the standard I2C protocol >> stack does not support runtime use, so this driver invokes the I2C master >> protocol directly. This requires support from the platform side as well, >> and so this driver will only attach to a I2C master that has the >> gPcf8563RealTimeClockLibI2cMasterProtolGuid protocol installed on its >> handle. It is up to the platform to ensure that the driver producing >> the I2C master protocol in question is runtime capable, and is not >> shared with the I2C protocol stack (i.e., it should not have the I2C >> Bus Configuration Management protocol installed as well). >> >> Contributed-under: TianoCore Contribution Agreement 1.1 >> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> >> --- >> Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.c | >> 385 ++++++++++++++++++++ >> Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.dec | >> 29 ++ >> Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.inf | >> 52 +++ >> 3 files changed, 466 insertions(+) >> >> diff --git >> a/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.c >> b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.c >> new file mode 100644 >> index 000000000000..fea65a225d7f >> --- /dev/null >> +++ >> b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.c >> @@ -0,0 +1,385 @@ >> +/** @file >> + >> + Copyright (c) 2017, Linaro, Ltd. All rights reserved.<BR> >> + >> + This program and the accompanying materials >> + are licensed and made available under the terms and conditions of the BSD >> License >> + which accompanies this distribution. The full text of the license may be found >> at >> + >> https://emea01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fopenso >> urce.org%2Flicenses%2Fbsd- >> license.php&data=02%7C01%7Cudit.kumar%40nxp.com%7C6faa68073eef4ed07 >> 25208d522a40d65%7C686ea1d3bc2b4c6fa92cd99c5c301635%7C0%7C0%7C636 >> 453010374642660&sdata=vDhroHNG20VCsMtj7%2FggWcWRG8hwt5fbV1Kv4R >> Na98Q%3D&reserved=0 >> + >> + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" >> BASIS, >> + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER >> EXPRESS OR IMPLIED. >> + >> +**/ >> + >> +#include <PiDxe.h> >> +#include <Library/DebugLib.h> >> +#include <Library/IoLib.h> >> +#include <Library/RealTimeClockLib.h> >> +#include <Library/UefiBootServicesTableLib.h> >> +#include <Library/UefiLib.h> >> +#include <Library/UefiRuntimeLib.h> >> +#include <Protocol/I2cMaster.h> >> + >> +#define SLAVE_ADDRESS (FixedPcdGet8 (PcdI2cSlaveAddress)) >> +#define PCF8563_DATA_REG_OFFSET 0x2 >> + >> +#define PCF8563_SECONDS_MASK 0x7f >> +#define PCF8563_MINUTES_MASK 0x7f >> +#define PCF8563_HOURS_MASK 0x3f >> +#define PCF8563_DAYS_MASK 0x3f >> +#define PCF8563_WEEKDAYS_MASK 0x07 >> +#define PCF8563_MONTHS_MASK 0x1f >> +#define PCF8563_CENTURY_MASK 0x80 >> + >> +#define EPOCH_BASE 2000 >> + >> +STATIC EFI_HANDLE mI2cMasterHandle; >> +STATIC VOID *mDriverEventRegistration; >> +STATIC EFI_I2C_MASTER_PROTOCOL *mI2cMaster; >> +STATIC EFI_EVENT mRtcVirtualAddrChangeEvent; >> + >> +typedef struct { >> + UINTN OperationCount; >> + EFI_I2C_OPERATION SetAddressOp; >> + EFI_I2C_OPERATION GetSetDateTimeOp; >> +} RTC_I2C_REQUEST; >> + >> +#pragma pack(1) >> +typedef struct { >> + UINT8 VL_seconds; >> + UINT8 Minutes; >> + UINT8 Hours; >> + UINT8 Days; >> + UINT8 Weekdays; >> + UINT8 Century_months; >> + UINT8 Years; >> +} RTC_DATETIME; >> +#pragma pack() >> + >> +/** >> + Returns the current time and date information, and the time-keeping >> + capabilities of the hardware platform. >> + >> + @param Time A pointer to storage to receive a snapshot of >> + the current time. >> + @param Capabilities An optional pointer to a buffer to receive the >> + real time clock device's capabilities. >> + >> + @retval EFI_SUCCESS The operation completed successfully. >> + @retval EFI_INVALID_PARAMETER Time is NULL. >> + @retval EFI_DEVICE_ERROR The time could not be retrieved due to >> hardware >> + error. >> + >> +**/ >> +EFI_STATUS >> +EFIAPI >> +LibGetTime ( >> + OUT EFI_TIME *Time, >> + OUT EFI_TIME_CAPABILITIES *Capabilities >> + ) >> +{ >> + RTC_I2C_REQUEST Op; >> + RTC_DATETIME DateTime; >> + EFI_STATUS Status; >> + UINT8 Reg; >> + >> + if (Time == NULL) { >> + return EFI_INVALID_PARAMETER; >> + } >> + >> + if (mI2cMaster == NULL) { >> + return EFI_DEVICE_ERROR; >> + } >> + >> + Reg = PCF8563_DATA_REG_OFFSET; >> + >> + Op.OperationCount = 2; >> + >> + Op.SetAddressOp.Flags = 0; >> + Op.SetAddressOp.LengthInBytes = 1; >> + Op.SetAddressOp.Buffer = &Reg; >> + >> + Op.GetSetDateTimeOp.Flags = I2C_FLAG_READ; >> + Op.GetSetDateTimeOp.LengthInBytes = sizeof (RTC_DATETIME); >> + Op.GetSetDateTimeOp.Buffer = (VOID *)&DateTime; >> + >> + Status = mI2cMaster->StartRequest (mI2cMaster, SLAVE_ADDRESS, >> + (VOID *)&Op, NULL, NULL); >> + if (EFI_ERROR (Status)) { >> + return EFI_DEVICE_ERROR; >> + } >> + >> + Time->Second = BcdToDecimal8 (DateTime.VL_seconds & >> PCF8563_SECONDS_MASK); >> + Time->Minute = BcdToDecimal8 (DateTime.Minutes & >> PCF8563_MINUTES_MASK); >> + Time->Hour = BcdToDecimal8 (DateTime.Hours & PCF8563_HOURS_MASK); >> + Time->Day = BcdToDecimal8 (DateTime.Days & PCF8563_DAYS_MASK); >> + Time->Month = BcdToDecimal8 (DateTime.Century_months & >> PCF8563_MONTHS_MASK); >> + Time->Year = BcdToDecimal8 (DateTime.Years) + EPOCH_BASE; >> + >> + if (DateTime.Century_months & PCF8563_CENTURY_MASK) { >> + Time->Year += 100; >> + } >> + >> + Time->Daylight = EFI_TIME_ADJUST_DAYLIGHT; >> + Time->TimeZone = 0; /* UTC */ > > Need to take care of time-zone as well. > No, this does not belong in this library, I have posted patches to add timezone handling and input validation to the core driver. >> + >> + if (Capabilities != NULL) { >> + Capabilities->Resolution = 1; >> + Capabilities->Accuracy = 0; >> + Capabilities->SetsToZero = TRUE; >> + } >> + return EFI_SUCCESS; >> +} >> + >> + >> +/** >> + Sets the current local time and date information. >> + >> + @param Time A pointer to the current time. >> + >> + @retval EFI_SUCCESS The operation completed successfully. >> + @retval EFI_INVALID_PARAMETER A time field is out of range. >> + @retval EFI_DEVICE_ERROR The time could not be set due due to >> hardware >> + error. >> + >> +**/ >> +EFI_STATUS >> +EFIAPI >> +LibSetTime ( >> + IN EFI_TIME *Time >> + ) >> +{ >> + RTC_I2C_REQUEST Op; >> + RTC_DATETIME DateTime; >> + EFI_STATUS Status; >> + UINT8 Reg; >> + >> + if (mI2cMaster == NULL) { >> + return EFI_DEVICE_ERROR; >> + } > > Please see, if check can be added for time validity > Same here. >> + >> + DateTime.VL_seconds = DecimalToBcd8 (Time->Second); >> + DateTime.Minutes = DecimalToBcd8 (Time->Minute); >> + DateTime.Hours = DecimalToBcd8 (Time->Hour); >> + DateTime.Days = DecimalToBcd8 (Time->Day); >> + DateTime.Century_months = DecimalToBcd8 (Time->Month); >> + DateTime.Years = DecimalToBcd8 (Time->Year % 100); >> + if (Time->Year >= EPOCH_BASE + 100) { >> + DateTime.Century_months |= PCF8563_CENTURY_MASK; >> + } >> + >> + Reg = PCF8563_DATA_REG_OFFSET; >> + >> + Op.OperationCount = 2; >> + >> + Op.SetAddressOp.Flags = 0; >> + Op.SetAddressOp.LengthInBytes = 1; >> + Op.SetAddressOp.Buffer = &Reg; >> + >> + Op.GetSetDateTimeOp.Flags = 0; >> + Op.GetSetDateTimeOp.LengthInBytes = sizeof (RTC_DATETIME); >> + Op.GetSetDateTimeOp.Buffer = (VOID *)&DateTime; >> + >> + Status = mI2cMaster->StartRequest (mI2cMaster, SLAVE_ADDRESS, >> + (VOID *)&Op, NULL, NULL); >> + if (EFI_ERROR (Status)) { >> + return EFI_DEVICE_ERROR; >> + } >> + return EFI_SUCCESS; >> +} >> + >> + >> +/** >> + Returns the current wakeup alarm clock setting. >> + >> + @param Enabled Indicates if the alarm is currently enabled or >> + disabled. >> + @param Pending Indicates if the alarm signal is pending and >> + requires acknowledgement. >> + @param Time The current alarm setting. >> + >> + @retval EFI_SUCCESS The alarm settings were returned. >> + @retval EFI_INVALID_PARAMETER Any parameter is NULL. >> + @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due >> to a >> + hardware error. >> + >> +**/ >> +EFI_STATUS >> +EFIAPI >> +LibGetWakeupTime ( >> + OUT BOOLEAN *Enabled, >> + OUT BOOLEAN *Pending, >> + OUT EFI_TIME *Time >> + ) >> +{ >> + return EFI_UNSUPPORTED; >> +} >> + >> + >> +/** >> + Sets the system wakeup alarm clock time. >> + >> + @param Enabled Enable or disable the wakeup alarm. >> + @param Time If Enable is TRUE, the time to set the wakeup >> + alarm for. >> + >> + @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was >> + enabled. If Enable is FALSE, then the wakeup >> + alarm was disabled. >> + @retval EFI_INVALID_PARAMETER A time field is out of range. >> + @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a >> + hardware error. >> + @retval EFI_UNSUPPORTED A wakeup timer is not supported on this >> + platform. >> + >> +**/ >> +EFI_STATUS >> +EFIAPI >> +LibSetWakeupTime ( >> + IN BOOLEAN Enabled, >> + OUT EFI_TIME *Time >> + ) >> +{ >> + return EFI_UNSUPPORTED; >> +} >> + >> +STATIC >> +VOID >> +DriverRegistrationEvent ( >> + IN EFI_EVENT Event, >> + IN VOID *Context >> + ) >> +{ >> + EFI_HANDLE Handle[2]; >> + UINTN BufferSize; >> + EFI_STATUS Status; >> + EFI_I2C_MASTER_PROTOCOL *I2cMaster; >> + UINTN BusFrequency; >> + >> + // >> + // Try to connect the newly registered driver to our handle. >> + // >> + do { >> + BufferSize = sizeof (EFI_HANDLE); >> + Status = gBS->LocateHandle (ByRegisterNotify, >> + &gEfiDriverBindingProtocolGuid, >> + mDriverEventRegistration, >> + &BufferSize, >> + Handle); >> + if (EFI_ERROR (Status)) { >> + if (Status != EFI_NOT_FOUND) { >> + DEBUG ((DEBUG_WARN, "%a: gBS->LocateHandle () returned %r\n", >> + __FUNCTION__, Status)); >> + } >> + break; >> + } >> + >> + // >> + // Check if we can connect our handle to this driver. >> + // >> + Handle[1] = NULL; >> + Status = gBS->ConnectController (mI2cMasterHandle, Handle, NULL, FALSE); >> + if (EFI_ERROR (Status)) { >> + continue; >> + } >> + >> + DEBUG ((DEBUG_INFO, "%a: found I2C master!\n", __FUNCTION__)); >> + >> + gBS->CloseEvent (Event); >> + >> + Status = gBS->OpenProtocol (mI2cMasterHandle, >> &gEfiI2cMasterProtocolGuid, >> + (VOID **)&I2cMaster, gImageHandle, NULL, >> + EFI_OPEN_PROTOCOL_EXCLUSIVE); >> + ASSERT_EFI_ERROR (Status); >> + >> + Status = I2cMaster->Reset (I2cMaster); >> + if (EFI_ERROR (Status)) { >> + DEBUG ((DEBUG_ERROR, "%a: I2CMaster->Reset () failed - %r\n", >> + __FUNCTION__, Status)); >> + break; >> + } >> + >> + BusFrequency = FixedPcdGet16 (PcdI2cBusFrequency); >> + Status = I2cMaster->SetBusFrequency (I2cMaster, &BusFrequency); >> + if (EFI_ERROR (Status)) { >> + DEBUG ((DEBUG_ERROR, "%a: I2CMaster->SetBusFrequency () failed - >> %r\n", >> + __FUNCTION__, Status)); >> + break; >> + } >> + >> + mI2cMaster = I2cMaster; >> + break; >> + } while (TRUE); >> +} >> + >> +/** >> + Fixup internal data so that EFI can be call in virtual mode. >> + Call the passed in Child Notify event and convert any pointers in >> + lib to virtual mode. >> + >> + @param[in] Event The Event that is being processed >> + @param[in] Context Event Context >> +**/ >> +VOID >> +EFIAPI >> +LibRtcVirtualNotifyEvent ( >> + IN EFI_EVENT Event, >> + IN VOID *Context >> + ) >> +{ >> + EfiConvertPointer (0x0, (VOID **)&mI2cMaster); >> +} >> + >> +/** >> + Library entry point >> + >> + @param ImageHandle Handle that identifies the loaded image. >> + @param SystemTable System Table for this image. >> + >> + @retval EFI_SUCCESS The operation completed successfully. >> + >> +**/ >> +EFI_STATUS >> +EFIAPI >> +LibRtcInitialize ( >> + IN EFI_HANDLE ImageHandle, >> + IN EFI_SYSTEM_TABLE *SystemTable >> + ) >> +{ >> + EFI_STATUS Status; >> + UINTN BufferSize; >> + >> + // >> + // Find the handle that marks the controller >> + // that will provide the I2C master protocol. >> + // >> + BufferSize = sizeof (EFI_HANDLE); >> + Status = gBS->LocateHandle (ByProtocol, >> + &gPcf8563RealTimeClockLibI2cMasterProtolGuid, NULL, >> + &BufferSize, &mI2cMasterHandle); >> + ASSERT_EFI_ERROR (Status); >> + >> + // >> + // Register a protocol registration notification callback on the driver >> + // binding protocol so we can attempt to connect our I2C master to it >> + // as soon as it appears. >> + // >> + EfiCreateProtocolNotifyEvent ( >> + &gEfiDriverBindingProtocolGuid, >> + TPL_CALLBACK, >> + DriverRegistrationEvent, >> + NULL, >> + &mDriverEventRegistration); >> + >> + // >> + // Register for the virtual address change event >> + // >> + Status = gBS->CreateEventEx (EVT_NOTIFY_SIGNAL, TPL_NOTIFY, >> + LibRtcVirtualNotifyEvent, NULL, >> + &gEfiEventVirtualAddressChangeGuid, >> + &mRtcVirtualAddrChangeEvent); >> + ASSERT_EFI_ERROR (Status); >> + >> + return EFI_SUCCESS; >> +} >> diff --git >> a/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.dec >> b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.dec >> new file mode 100644 >> index 000000000000..bc8fc60c0602 >> --- /dev/null >> +++ >> b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.dec >> @@ -0,0 +1,29 @@ >> +#/** @file >> +# >> +# Copyright (c) 2017, Linaro, Ltd. All rights reserved.<BR> >> +# >> +# This program and the accompanying materials >> +# are licensed and made available under the terms and conditions of the BSD >> License >> +# which accompanies this distribution. The full text of the license may be found >> at >> +# >> https://emea01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fopenso >> urce.org%2Flicenses%2Fbsd- >> license.php&data=02%7C01%7Cudit.kumar%40nxp.com%7C6faa68073eef4ed07 >> 25208d522a40d65%7C686ea1d3bc2b4c6fa92cd99c5c301635%7C0%7C0%7C636 >> 453010374642660&sdata=vDhroHNG20VCsMtj7%2FggWcWRG8hwt5fbV1Kv4R >> Na98Q%3D&reserved=0 >> +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" >> BASIS, >> +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER >> EXPRESS OR IMPLIED. >> +# >> +# >> +#**/ >> + >> +[Defines] >> + DEC_SPECIFICATION = 0x0001001A >> + PACKAGE_NAME = Pcf8563RealTimeClockLib >> + PACKAGE_GUID = 44d63668-c86b-446a-b303-f3a5176702aa >> + PACKAGE_VERSION = 0.1 >> + >> +[Guids] >> + gPcf8563RealTimeClockLibTokenSpaceGuid = { 0xaaf5b169, 0x93a0, 0x4d60, { >> 0xba, 0xe4, 0x06, 0x07, 0x92, 0x8e, 0x63, 0xdd }} >> + >> +[Protocols] >> + gPcf8563RealTimeClockLibI2cMasterProtolGuid = { 0xa6af18ae, 0x3bd5, >> 0x4af9, { 0xbb, 0x6a, 0xdb, 0x85, 0x07, 0x62, 0x81, 0x38 }} >> + >> +[PcdsFixedAtBuild] >> + >> gPcf8563RealTimeClockLibTokenSpaceGuid.PcdI2cSlaveAddress|0x51|UINT8|0 >> x00000001 >> + >> gPcf8563RealTimeClockLibTokenSpaceGuid.PcdI2cBusFrequency|400|UINT16|0 >> x00000002 >> diff --git >> a/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.inf >> b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.inf >> new file mode 100644 >> index 000000000000..e1e001162144 >> --- /dev/null >> +++ >> b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.inf >> @@ -0,0 +1,52 @@ >> +#/** @file >> +# >> +# Copyright (c) 2017, Linaro, Ltd. All rights reserved.<BR> >> +# >> +# This program and the accompanying materials >> +# are licensed and made available under the terms and conditions of the BSD >> License >> +# which accompanies this distribution. The full text of the license may be found >> at >> +# >> https://emea01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fopenso >> urce.org%2Flicenses%2Fbsd- >> license.php&data=02%7C01%7Cudit.kumar%40nxp.com%7C6faa68073eef4ed07 >> 25208d522a40d65%7C686ea1d3bc2b4c6fa92cd99c5c301635%7C0%7C0%7C636 >> 453010374642660&sdata=vDhroHNG20VCsMtj7%2FggWcWRG8hwt5fbV1Kv4R >> Na98Q%3D&reserved=0 >> +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" >> BASIS, >> +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER >> EXPRESS OR IMPLIED. >> +# >> +# >> +#**/ >> + >> +[Defines] >> + INF_VERSION = 0x0001001A >> + BASE_NAME = Pcf8563RealTimeClockLib >> + FILE_GUID = 1e3d33f3-a671-4116-bd81-6b435296c1e0 >> + MODULE_TYPE = DXE_DRIVER >> + VERSION_STRING = 1.0 >> + LIBRARY_CLASS = RealTimeClockLib >> + >> +[Sources.common] >> + Pcf8563RealTimeClockLib.c >> + >> +[Packages] >> + MdePkg/MdePkg.dec >> + EmbeddedPkg/EmbeddedPkg.dec >> + Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.dec >> + >> +[LibraryClasses] >> + BaseMemoryLib >> + DebugLib >> + IoLib >> + UefiBootServicesTableLib >> + UefiLib >> + UefiRuntimeLib >> + >> +[Guids] >> + gEfiEventVirtualAddressChangeGuid >> + >> +[Protocols] >> + gEfiDriverBindingProtocolGuid ## CONSUMES >> + gEfiI2cMasterProtocolGuid ## CONSUMES >> + gPcf8563RealTimeClockLibI2cMasterProtolGuid ## CONSUMES >> + >> +[FixedPcd] >> + gPcf8563RealTimeClockLibTokenSpaceGuid.PcdI2cSlaveAddress >> + gPcf8563RealTimeClockLibTokenSpaceGuid.PcdI2cBusFrequency >> + >> +[Depex] >> + gPcf8563RealTimeClockLibI2cMasterProtolGuid >> -- >> 2.11.0 >> >> _______________________________________________ >> edk2-devel mailing list >> edk2-devel@lists.01.org >> https://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.01 >> .org%2Fmailman%2Flistinfo%2Fedk2- >> devel&data=02%7C01%7Cudit.kumar%40nxp.com%7C6faa68073eef4ed072520 >> 8d522a40d65%7C686ea1d3bc2b4c6fa92cd99c5c301635%7C0%7C0%7C6364530 >> 10374642660&sdata=vr4qAgDuDX0M7MGeuS%2FKAY%2B%2FqhVmz0%2FMrEx >> K42tQIbM%3D&reserved=0 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel
Thanks Ard, Your another patch set is addressing my concern Regards Udit > -----Original Message----- > From: Ard Biesheuvel [mailto:ard.biesheuvel@linaro.org] > Sent: Monday, November 06, 2017 4:41 PM > To: Udit Kumar <udit.kumar@nxp.com> > Cc: edk2-devel@lists.01.org; leif.lindholm@linaro.org; > daniel.thompson@linaro.org > Subject: Re: [edk2] [RFC PATCH edk2-platforms 1/3] Silicon/NXP: add RTC > support library for PCF8563 I2C IP > > On 6 November 2017 at 11:09, Udit Kumar <udit.kumar@nxp.com> wrote: > > Thanks Ard. > > This will allow to use PCF8563 on any platform not limiting to targeted > platform. > > Yes, that was the intention. > > > Few comments are inline. > > > > Thanks. > > > > >> -----Original Message----- > >> From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf > >> Of Ard Biesheuvel > >> Sent: Friday, November 03, 2017 3:47 PM > >> To: edk2-devel@lists.01.org; leif.lindholm@linaro.org; > >> daniel.thompson@linaro.org > >> Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org> > >> Subject: [edk2] [RFC PATCH edk2-platforms 1/3] Silicon/NXP: add RTC > >> support library for PCF8563 I2C IP > >> > >> Add a RealTimeClockLib implementation for the NXP PCF8563 as used on > >> the Socionext Developer Box board. Note that the standard I2C > >> protocol stack does not support runtime use, so this driver invokes > >> the I2C master protocol directly. This requires support from the > >> platform side as well, and so this driver will only attach to a I2C > >> master that has the gPcf8563RealTimeClockLibI2cMasterProtolGuid > >> protocol installed on its handle. It is up to the platform to ensure > >> that the driver producing the I2C master protocol in question is > >> runtime capable, and is not shared with the I2C protocol stack (i.e., > >> it should not have the I2C Bus Configuration Management protocol installed > as well). > >> > >> Contributed-under: TianoCore Contribution Agreement 1.1 > >> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> > >> --- > >> Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.c > | > >> 385 ++++++++++++++++++++ > >> > >> Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.d > >> ec | > >> 29 ++ > >> > >> Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.i > >> nf | > >> 52 +++ > >> 3 files changed, 466 insertions(+) > >> > >> diff --git > >> a/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib > >> .c > >> b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib > >> .c > >> new file mode 100644 > >> index 000000000000..fea65a225d7f > >> --- /dev/null > >> +++ > >> b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib > >> .c > >> @@ -0,0 +1,385 @@ > >> +/** @file > >> + > >> + Copyright (c) 2017, Linaro, Ltd. All rights reserved.<BR> > >> + > >> + This program and the accompanying materials are licensed and made > >> + available under the terms and conditions of the BSD > >> License > >> + which accompanies this distribution. The full text of the license > >> + may be found > >> at > >> + > >> https://emea01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fope > >> nso > >> urce.org%2Flicenses%2Fbsd- > >> > license.php&data=02%7C01%7Cudit.kumar%40nxp.com%7C6faa68073eef4ed07 > >> > 25208d522a40d65%7C686ea1d3bc2b4c6fa92cd99c5c301635%7C0%7C0%7C636 > >> > 453010374642660&sdata=vDhroHNG20VCsMtj7%2FggWcWRG8hwt5fbV1Kv4R > >> Na98Q%3D&reserved=0 > >> + > >> + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" > >> BASIS, > >> + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER > >> EXPRESS OR IMPLIED. > >> + > >> +**/ > >> + > >> +#include <PiDxe.h> > >> +#include <Library/DebugLib.h> > >> +#include <Library/IoLib.h> > >> +#include <Library/RealTimeClockLib.h> #include > >> +<Library/UefiBootServicesTableLib.h> > >> +#include <Library/UefiLib.h> > >> +#include <Library/UefiRuntimeLib.h> > >> +#include <Protocol/I2cMaster.h> > >> + > >> +#define SLAVE_ADDRESS (FixedPcdGet8 (PcdI2cSlaveAddress)) > >> +#define PCF8563_DATA_REG_OFFSET 0x2 > >> + > >> +#define PCF8563_SECONDS_MASK 0x7f > >> +#define PCF8563_MINUTES_MASK 0x7f > >> +#define PCF8563_HOURS_MASK 0x3f > >> +#define PCF8563_DAYS_MASK 0x3f > >> +#define PCF8563_WEEKDAYS_MASK 0x07 > >> +#define PCF8563_MONTHS_MASK 0x1f > >> +#define PCF8563_CENTURY_MASK 0x80 > >> + > >> +#define EPOCH_BASE 2000 > >> + > >> +STATIC EFI_HANDLE mI2cMasterHandle; > >> +STATIC VOID *mDriverEventRegistration; > >> +STATIC EFI_I2C_MASTER_PROTOCOL *mI2cMaster; > >> +STATIC EFI_EVENT mRtcVirtualAddrChangeEvent; > >> + > >> +typedef struct { > >> + UINTN OperationCount; > >> + EFI_I2C_OPERATION SetAddressOp; > >> + EFI_I2C_OPERATION GetSetDateTimeOp; > >> +} RTC_I2C_REQUEST; > >> + > >> +#pragma pack(1) > >> +typedef struct { > >> + UINT8 VL_seconds; > >> + UINT8 Minutes; > >> + UINT8 Hours; > >> + UINT8 Days; > >> + UINT8 Weekdays; > >> + UINT8 Century_months; > >> + UINT8 Years; > >> +} RTC_DATETIME; > >> +#pragma pack() > >> + > >> +/** > >> + Returns the current time and date information, and the > >> +time-keeping > >> + capabilities of the hardware platform. > >> + > >> + @param Time A pointer to storage to receive a snapshot of > >> + the current time. > >> + @param Capabilities An optional pointer to a buffer to receive the > >> + real time clock device's capabilities. > >> + > >> + @retval EFI_SUCCESS The operation completed successfully. > >> + @retval EFI_INVALID_PARAMETER Time is NULL. > >> + @retval EFI_DEVICE_ERROR The time could not be retrieved due to > >> hardware > >> + error. > >> + > >> +**/ > >> +EFI_STATUS > >> +EFIAPI > >> +LibGetTime ( > >> + OUT EFI_TIME *Time, > >> + OUT EFI_TIME_CAPABILITIES *Capabilities > >> + ) > >> +{ > >> + RTC_I2C_REQUEST Op; > >> + RTC_DATETIME DateTime; > >> + EFI_STATUS Status; > >> + UINT8 Reg; > >> + > >> + if (Time == NULL) { > >> + return EFI_INVALID_PARAMETER; > >> + } > >> + > >> + if (mI2cMaster == NULL) { > >> + return EFI_DEVICE_ERROR; > >> + } > >> + > >> + Reg = PCF8563_DATA_REG_OFFSET; > >> + > >> + Op.OperationCount = 2; > >> + > >> + Op.SetAddressOp.Flags = 0; > >> + Op.SetAddressOp.LengthInBytes = 1; Op.SetAddressOp.Buffer = &Reg; > >> + > >> + Op.GetSetDateTimeOp.Flags = I2C_FLAG_READ; > >> + Op.GetSetDateTimeOp.LengthInBytes = sizeof (RTC_DATETIME); > >> + Op.GetSetDateTimeOp.Buffer = (VOID *)&DateTime; > >> + > >> + Status = mI2cMaster->StartRequest (mI2cMaster, SLAVE_ADDRESS, > >> + (VOID *)&Op, NULL, NULL); if (EFI_ERROR > >> + (Status)) { > >> + return EFI_DEVICE_ERROR; > >> + } > >> + > >> + Time->Second = BcdToDecimal8 (DateTime.VL_seconds & > >> PCF8563_SECONDS_MASK); > >> + Time->Minute = BcdToDecimal8 (DateTime.Minutes & > >> PCF8563_MINUTES_MASK); > >> + Time->Hour = BcdToDecimal8 (DateTime.Hours & > PCF8563_HOURS_MASK); > >> + Time->Day = BcdToDecimal8 (DateTime.Days & PCF8563_DAYS_MASK); > >> + Time->Month = BcdToDecimal8 (DateTime.Century_months & > >> PCF8563_MONTHS_MASK); > >> + Time->Year = BcdToDecimal8 (DateTime.Years) + EPOCH_BASE; > >> + > >> + if (DateTime.Century_months & PCF8563_CENTURY_MASK) { > >> + Time->Year += 100; > >> + } > >> + > >> + Time->Daylight = EFI_TIME_ADJUST_DAYLIGHT; Time->TimeZone = 0; > >> + /* UTC */ > > > > Need to take care of time-zone as well. > > > > No, this does not belong in this library, I have posted patches to add timezone > handling and input validation to the core driver. > > >> + > >> + if (Capabilities != NULL) { > >> + Capabilities->Resolution = 1; > >> + Capabilities->Accuracy = 0; > >> + Capabilities->SetsToZero = TRUE; > >> + } > >> + return EFI_SUCCESS; > >> +} > >> + > >> + > >> +/** > >> + Sets the current local time and date information. > >> + > >> + @param Time A pointer to the current time. > >> + > >> + @retval EFI_SUCCESS The operation completed successfully. > >> + @retval EFI_INVALID_PARAMETER A time field is out of range. > >> + @retval EFI_DEVICE_ERROR The time could not be set due due to > >> hardware > >> + error. > >> + > >> +**/ > >> +EFI_STATUS > >> +EFIAPI > >> +LibSetTime ( > >> + IN EFI_TIME *Time > >> + ) > >> +{ > >> + RTC_I2C_REQUEST Op; > >> + RTC_DATETIME DateTime; > >> + EFI_STATUS Status; > >> + UINT8 Reg; > >> + > >> + if (mI2cMaster == NULL) { > >> + return EFI_DEVICE_ERROR; > >> + } > > > > Please see, if check can be added for time validity > > > > Same here. > > >> + > >> + DateTime.VL_seconds = DecimalToBcd8 (Time->Second); > >> + DateTime.Minutes = DecimalToBcd8 (Time->Minute); > >> + DateTime.Hours = DecimalToBcd8 (Time->Hour); > >> + DateTime.Days = DecimalToBcd8 (Time->Day); > >> + DateTime.Century_months = DecimalToBcd8 (Time->Month); > >> + DateTime.Years = DecimalToBcd8 (Time->Year % 100); > >> + if (Time->Year >= EPOCH_BASE + 100) { > >> + DateTime.Century_months |= PCF8563_CENTURY_MASK; } > >> + > >> + Reg = PCF8563_DATA_REG_OFFSET; > >> + > >> + Op.OperationCount = 2; > >> + > >> + Op.SetAddressOp.Flags = 0; > >> + Op.SetAddressOp.LengthInBytes = 1; Op.SetAddressOp.Buffer = &Reg; > >> + > >> + Op.GetSetDateTimeOp.Flags = 0; > >> + Op.GetSetDateTimeOp.LengthInBytes = sizeof (RTC_DATETIME); > >> + Op.GetSetDateTimeOp.Buffer = (VOID *)&DateTime; > >> + > >> + Status = mI2cMaster->StartRequest (mI2cMaster, SLAVE_ADDRESS, > >> + (VOID *)&Op, NULL, NULL); > >> + if (EFI_ERROR (Status)) { > >> + return EFI_DEVICE_ERROR; > >> + } > >> + return EFI_SUCCESS; > >> +} > >> + > >> + > >> +/** > >> + Returns the current wakeup alarm clock setting. > >> + > >> + @param Enabled Indicates if the alarm is currently enabled or > >> + disabled. > >> + @param Pending Indicates if the alarm signal is pending and > >> + requires acknowledgement. > >> + @param Time The current alarm setting. > >> + > >> + @retval EFI_SUCCESS The alarm settings were returned. > >> + @retval EFI_INVALID_PARAMETER Any parameter is NULL. > >> + @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved > due > >> to a > >> + hardware error. > >> + > >> +**/ > >> +EFI_STATUS > >> +EFIAPI > >> +LibGetWakeupTime ( > >> + OUT BOOLEAN *Enabled, > >> + OUT BOOLEAN *Pending, > >> + OUT EFI_TIME *Time > >> + ) > >> +{ > >> + return EFI_UNSUPPORTED; > >> +} > >> + > >> + > >> +/** > >> + Sets the system wakeup alarm clock time. > >> + > >> + @param Enabled Enable or disable the wakeup alarm. > >> + @param Time If Enable is TRUE, the time to set the wakeup > >> + alarm for. > >> + > >> + @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was > >> + enabled. If Enable is FALSE, then the wakeup > >> + alarm was disabled. > >> + @retval EFI_INVALID_PARAMETER A time field is out of range. > >> + @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a > >> + hardware error. > >> + @retval EFI_UNSUPPORTED A wakeup timer is not supported on this > >> + platform. > >> + > >> +**/ > >> +EFI_STATUS > >> +EFIAPI > >> +LibSetWakeupTime ( > >> + IN BOOLEAN Enabled, > >> + OUT EFI_TIME *Time > >> + ) > >> +{ > >> + return EFI_UNSUPPORTED; > >> +} > >> + > >> +STATIC > >> +VOID > >> +DriverRegistrationEvent ( > >> + IN EFI_EVENT Event, > >> + IN VOID *Context > >> + ) > >> +{ > >> + EFI_HANDLE Handle[2]; > >> + UINTN BufferSize; > >> + EFI_STATUS Status; > >> + EFI_I2C_MASTER_PROTOCOL *I2cMaster; > >> + UINTN BusFrequency; > >> + > >> + // > >> + // Try to connect the newly registered driver to our handle. > >> + // > >> + do { > >> + BufferSize = sizeof (EFI_HANDLE); > >> + Status = gBS->LocateHandle (ByRegisterNotify, > >> + &gEfiDriverBindingProtocolGuid, > >> + mDriverEventRegistration, > >> + &BufferSize, > >> + Handle); > >> + if (EFI_ERROR (Status)) { > >> + if (Status != EFI_NOT_FOUND) { > >> + DEBUG ((DEBUG_WARN, "%a: gBS->LocateHandle () returned %r\n", > >> + __FUNCTION__, Status)); > >> + } > >> + break; > >> + } > >> + > >> + // > >> + // Check if we can connect our handle to this driver. > >> + // > >> + Handle[1] = NULL; > >> + Status = gBS->ConnectController (mI2cMasterHandle, Handle, NULL, > FALSE); > >> + if (EFI_ERROR (Status)) { > >> + continue; > >> + } > >> + > >> + DEBUG ((DEBUG_INFO, "%a: found I2C master!\n", __FUNCTION__)); > >> + > >> + gBS->CloseEvent (Event); > >> + > >> + Status = gBS->OpenProtocol (mI2cMasterHandle, > >> &gEfiI2cMasterProtocolGuid, > >> + (VOID **)&I2cMaster, gImageHandle, NULL, > >> + EFI_OPEN_PROTOCOL_EXCLUSIVE); > >> + ASSERT_EFI_ERROR (Status); > >> + > >> + Status = I2cMaster->Reset (I2cMaster); > >> + if (EFI_ERROR (Status)) { > >> + DEBUG ((DEBUG_ERROR, "%a: I2CMaster->Reset () failed - %r\n", > >> + __FUNCTION__, Status)); > >> + break; > >> + } > >> + > >> + BusFrequency = FixedPcdGet16 (PcdI2cBusFrequency); > >> + Status = I2cMaster->SetBusFrequency (I2cMaster, &BusFrequency); > >> + if (EFI_ERROR (Status)) { > >> + DEBUG ((DEBUG_ERROR, "%a: I2CMaster->SetBusFrequency () failed > >> + - > >> %r\n", > >> + __FUNCTION__, Status)); > >> + break; > >> + } > >> + > >> + mI2cMaster = I2cMaster; > >> + break; > >> + } while (TRUE); > >> +} > >> + > >> +/** > >> + Fixup internal data so that EFI can be call in virtual mode. > >> + Call the passed in Child Notify event and convert any pointers in > >> + lib to virtual mode. > >> + > >> + @param[in] Event The Event that is being processed > >> + @param[in] Context Event Context > >> +**/ > >> +VOID > >> +EFIAPI > >> +LibRtcVirtualNotifyEvent ( > >> + IN EFI_EVENT Event, > >> + IN VOID *Context > >> + ) > >> +{ > >> + EfiConvertPointer (0x0, (VOID **)&mI2cMaster); } > >> + > >> +/** > >> + Library entry point > >> + > >> + @param ImageHandle Handle that identifies the loaded image. > >> + @param SystemTable System Table for this image. > >> + > >> + @retval EFI_SUCCESS The operation completed successfully. > >> + > >> +**/ > >> +EFI_STATUS > >> +EFIAPI > >> +LibRtcInitialize ( > >> + IN EFI_HANDLE ImageHandle, > >> + IN EFI_SYSTEM_TABLE *SystemTable > >> + ) > >> +{ > >> + EFI_STATUS Status; > >> + UINTN BufferSize; > >> + > >> + // > >> + // Find the handle that marks the controller // that will provide > >> + the I2C master protocol. > >> + // > >> + BufferSize = sizeof (EFI_HANDLE); > >> + Status = gBS->LocateHandle (ByProtocol, > >> + &gPcf8563RealTimeClockLibI2cMasterProtolGuid, NULL, > >> + &BufferSize, &mI2cMasterHandle); ASSERT_EFI_ERROR > >> + (Status); > >> + > >> + // > >> + // Register a protocol registration notification callback on the > >> + driver // binding protocol so we can attempt to connect our I2C > >> + master to it // as soon as it appears. > >> + // > >> + EfiCreateProtocolNotifyEvent ( > >> + &gEfiDriverBindingProtocolGuid, > >> + TPL_CALLBACK, > >> + DriverRegistrationEvent, > >> + NULL, > >> + &mDriverEventRegistration); > >> + > >> + // > >> + // Register for the virtual address change event // Status = > >> + gBS->CreateEventEx (EVT_NOTIFY_SIGNAL, TPL_NOTIFY, > >> + LibRtcVirtualNotifyEvent, NULL, > >> + &gEfiEventVirtualAddressChangeGuid, > >> + &mRtcVirtualAddrChangeEvent); ASSERT_EFI_ERROR > >> + (Status); > >> + > >> + return EFI_SUCCESS; > >> +} > >> diff --git > >> a/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib > >> .dec > >> b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib > >> .dec > >> new file mode 100644 > >> index 000000000000..bc8fc60c0602 > >> --- /dev/null > >> +++ > >> b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib > >> .dec > >> @@ -0,0 +1,29 @@ > >> +#/** @file > >> +# > >> +# Copyright (c) 2017, Linaro, Ltd. All rights reserved.<BR> # # > >> +This program and the accompanying materials # are licensed and made > >> +available under the terms and conditions of the BSD > >> License > >> +# which accompanies this distribution. The full text of the license > >> +may be found > >> at > >> +# > >> https://emea01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fope > >> nso > >> urce.org%2Flicenses%2Fbsd- > >> > license.php&data=02%7C01%7Cudit.kumar%40nxp.com%7C6faa68073eef4ed07 > >> > 25208d522a40d65%7C686ea1d3bc2b4c6fa92cd99c5c301635%7C0%7C0%7C636 > >> > 453010374642660&sdata=vDhroHNG20VCsMtj7%2FggWcWRG8hwt5fbV1Kv4R > >> Na98Q%3D&reserved=0 > >> +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" > >> BASIS, > >> +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER > >> EXPRESS OR IMPLIED. > >> +# > >> +# > >> +#**/ > >> + > >> +[Defines] > >> + DEC_SPECIFICATION = 0x0001001A > >> + PACKAGE_NAME = Pcf8563RealTimeClockLib > >> + PACKAGE_GUID = 44d63668-c86b-446a-b303-f3a5176702aa > >> + PACKAGE_VERSION = 0.1 > >> + > >> +[Guids] > >> + gPcf8563RealTimeClockLibTokenSpaceGuid = { 0xaaf5b169, 0x93a0, > >> +0x4d60, { > >> 0xba, 0xe4, 0x06, 0x07, 0x92, 0x8e, 0x63, 0xdd }} > >> + > >> +[Protocols] > >> + gPcf8563RealTimeClockLibI2cMasterProtolGuid = { 0xa6af18ae, > >> +0x3bd5, > >> 0x4af9, { 0xbb, 0x6a, 0xdb, 0x85, 0x07, 0x62, 0x81, 0x38 }} > >> + > >> +[PcdsFixedAtBuild] > >> + > >> > gPcf8563RealTimeClockLibTokenSpaceGuid.PcdI2cSlaveAddress|0x51|UINT8| > >> 0 > >> x00000001 > >> + > >> > gPcf8563RealTimeClockLibTokenSpaceGuid.PcdI2cBusFrequency|400|UINT16| > >> 0 > >> x00000002 > >> diff --git > >> a/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib > >> .inf > >> b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib > >> .inf > >> new file mode 100644 > >> index 000000000000..e1e001162144 > >> --- /dev/null > >> +++ > >> b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib > >> .inf > >> @@ -0,0 +1,52 @@ > >> +#/** @file > >> +# > >> +# Copyright (c) 2017, Linaro, Ltd. All rights reserved.<BR> # # > >> +This program and the accompanying materials # are licensed and made > >> +available under the terms and conditions of the BSD > >> License > >> +# which accompanies this distribution. The full text of the license > >> +may be found > >> at > >> +# > >> https://emea01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fope > >> nso > >> urce.org%2Flicenses%2Fbsd- > >> > license.php&data=02%7C01%7Cudit.kumar%40nxp.com%7C6faa68073eef4ed07 > >> > 25208d522a40d65%7C686ea1d3bc2b4c6fa92cd99c5c301635%7C0%7C0%7C636 > >> > 453010374642660&sdata=vDhroHNG20VCsMtj7%2FggWcWRG8hwt5fbV1Kv4R > >> Na98Q%3D&reserved=0 > >> +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" > >> BASIS, > >> +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER > >> EXPRESS OR IMPLIED. > >> +# > >> +# > >> +#**/ > >> + > >> +[Defines] > >> + INF_VERSION = 0x0001001A > >> + BASE_NAME = Pcf8563RealTimeClockLib > >> + FILE_GUID = 1e3d33f3-a671-4116-bd81-6b435296c1e0 > >> + MODULE_TYPE = DXE_DRIVER > >> + VERSION_STRING = 1.0 > >> + LIBRARY_CLASS = RealTimeClockLib > >> + > >> +[Sources.common] > >> + Pcf8563RealTimeClockLib.c > >> + > >> +[Packages] > >> + MdePkg/MdePkg.dec > >> + EmbeddedPkg/EmbeddedPkg.dec > >> + > >> +Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib. > >> +dec > >> + > >> +[LibraryClasses] > >> + BaseMemoryLib > >> + DebugLib > >> + IoLib > >> + UefiBootServicesTableLib > >> + UefiLib > >> + UefiRuntimeLib > >> + > >> +[Guids] > >> + gEfiEventVirtualAddressChangeGuid > >> + > >> +[Protocols] > >> + gEfiDriverBindingProtocolGuid ## CONSUMES > >> + gEfiI2cMasterProtocolGuid ## CONSUMES > >> + gPcf8563RealTimeClockLibI2cMasterProtolGuid ## CONSUMES > >> + > >> +[FixedPcd] > >> + gPcf8563RealTimeClockLibTokenSpaceGuid.PcdI2cSlaveAddress > >> + gPcf8563RealTimeClockLibTokenSpaceGuid.PcdI2cBusFrequency > >> + > >> +[Depex] > >> + gPcf8563RealTimeClockLibI2cMasterProtolGuid > >> -- > >> 2.11.0 > >> > >> _______________________________________________ > >> edk2-devel mailing list > >> edk2-devel@lists.01.org > >> https://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fli > >> sts.01 > >> .org%2Fmailman%2Flistinfo%2Fedk2- > >> > devel&data=02%7C01%7Cudit.kumar%40nxp.com%7C6faa68073eef4ed072520 > >> > 8d522a40d65%7C686ea1d3bc2b4c6fa92cd99c5c301635%7C0%7C0%7C6364530 > >> > 10374642660&sdata=vr4qAgDuDX0M7MGeuS%2FKAY%2B%2FqhVmz0%2FMrEx > >> K42tQIbM%3D&reserved=0 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel
On Fri, Nov 03, 2017 at 10:16:52AM +0000, Ard Biesheuvel wrote: > Add a RealTimeClockLib implementation for the NXP PCF8563 as used on > the Socionext Developer Box board. Note that the standard I2C protocol > stack does not support runtime use, so this driver invokes the I2C master > protocol directly. This requires support from the platform side as well, > and so this driver will only attach to a I2C master that has the > gPcf8563RealTimeClockLibI2cMasterProtolGuid protocol installed on its > handle. It is up to the platform to ensure that the driver producing > the I2C master protocol in question is runtime capable, and is not > shared with the I2C protocol stack (i.e., it should not have the I2C > Bus Configuration Management protocol installed as well). > > Contributed-under: TianoCore Contribution Agreement 1.1 > Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> > --- > Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.c | 385 ++++++++++++++++++++ > Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.dec | 29 ++ > Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.inf | 52 +++ > 3 files changed, 466 insertions(+) > > diff --git a/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.c b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.c > new file mode 100644 > index 000000000000..fea65a225d7f > --- /dev/null > +++ b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.c > @@ -0,0 +1,385 @@ > +/** @file > + > + Copyright (c) 2017, Linaro, Ltd. All rights reserved.<BR> > + > + This program and the accompanying materials > + are licensed and made available under the terms and conditions of the BSD License > + which accompanies this distribution. The full text of the license may be found at > + http://opensource.org/licenses/bsd-license.php > + > + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, > + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. > + > +**/ > + > +#include <PiDxe.h> > +#include <Library/DebugLib.h> > +#include <Library/IoLib.h> > +#include <Library/RealTimeClockLib.h> > +#include <Library/UefiBootServicesTableLib.h> > +#include <Library/UefiLib.h> > +#include <Library/UefiRuntimeLib.h> > +#include <Protocol/I2cMaster.h> > + > +#define SLAVE_ADDRESS (FixedPcdGet8 (PcdI2cSlaveAddress)) > +#define PCF8563_DATA_REG_OFFSET 0x2 > + > +#define PCF8563_SECONDS_MASK 0x7f > +#define PCF8563_MINUTES_MASK 0x7f > +#define PCF8563_HOURS_MASK 0x3f > +#define PCF8563_DAYS_MASK 0x3f > +#define PCF8563_WEEKDAYS_MASK 0x07 > +#define PCF8563_MONTHS_MASK 0x1f > +#define PCF8563_CENTURY_MASK 0x80 > + > +#define EPOCH_BASE 2000 > + > +STATIC EFI_HANDLE mI2cMasterHandle; > +STATIC VOID *mDriverEventRegistration; > +STATIC EFI_I2C_MASTER_PROTOCOL *mI2cMaster; > +STATIC EFI_EVENT mRtcVirtualAddrChangeEvent; > + > +typedef struct { > + UINTN OperationCount; > + EFI_I2C_OPERATION SetAddressOp; > + EFI_I2C_OPERATION GetSetDateTimeOp; > +} RTC_I2C_REQUEST; > + > +#pragma pack(1) > +typedef struct { > + UINT8 VL_seconds; > + UINT8 Minutes; > + UINT8 Hours; > + UINT8 Days; > + UINT8 Weekdays; > + UINT8 Century_months; > + UINT8 Years; > +} RTC_DATETIME; > +#pragma pack() > + > +/** > + Returns the current time and date information, and the time-keeping > + capabilities of the hardware platform. > + > + @param Time A pointer to storage to receive a snapshot of > + the current time. > + @param Capabilities An optional pointer to a buffer to receive the > + real time clock device's capabilities. > + > + @retval EFI_SUCCESS The operation completed successfully. > + @retval EFI_INVALID_PARAMETER Time is NULL. > + @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware > + error. > + > +**/ > +EFI_STATUS > +EFIAPI > +LibGetTime ( > + OUT EFI_TIME *Time, > + OUT EFI_TIME_CAPABILITIES *Capabilities > + ) > +{ > + RTC_I2C_REQUEST Op; > + RTC_DATETIME DateTime; > + EFI_STATUS Status; > + UINT8 Reg; > + > + if (Time == NULL) { > + return EFI_INVALID_PARAMETER; > + } > + > + if (mI2cMaster == NULL) { > + return EFI_DEVICE_ERROR; > + } > + > + Reg = PCF8563_DATA_REG_OFFSET; > + > + Op.OperationCount = 2; > + > + Op.SetAddressOp.Flags = 0; > + Op.SetAddressOp.LengthInBytes = 1; > + Op.SetAddressOp.Buffer = &Reg; > + > + Op.GetSetDateTimeOp.Flags = I2C_FLAG_READ; > + Op.GetSetDateTimeOp.LengthInBytes = sizeof (RTC_DATETIME); > + Op.GetSetDateTimeOp.Buffer = (VOID *)&DateTime; > + > + Status = mI2cMaster->StartRequest (mI2cMaster, SLAVE_ADDRESS, > + (VOID *)&Op, NULL, NULL); > + if (EFI_ERROR (Status)) { > + return EFI_DEVICE_ERROR; > + } > + > + Time->Second = BcdToDecimal8 (DateTime.VL_seconds & PCF8563_SECONDS_MASK); > + Time->Minute = BcdToDecimal8 (DateTime.Minutes & PCF8563_MINUTES_MASK); > + Time->Hour = BcdToDecimal8 (DateTime.Hours & PCF8563_HOURS_MASK); > + Time->Day = BcdToDecimal8 (DateTime.Days & PCF8563_DAYS_MASK); > + Time->Month = BcdToDecimal8 (DateTime.Century_months & PCF8563_MONTHS_MASK); > + Time->Year = BcdToDecimal8 (DateTime.Years) + EPOCH_BASE; > + > + if (DateTime.Century_months & PCF8563_CENTURY_MASK) { > + Time->Year += 100; Guess we'd better retire all these platforms before the year 2100 then... (Or, given EPOCH_BASE, is it 2070? (Or given EPOCH_BASE being set to 2000, is it 2200?)) Could you add a small comment next to the EPOCH_BASE definition explaining the constrainst? > + } > + > + Time->Daylight = EFI_TIME_ADJUST_DAYLIGHT; > + Time->TimeZone = 0; /* UTC */ > + > + if (Capabilities != NULL) { > + Capabilities->Resolution = 1; > + Capabilities->Accuracy = 0; > + Capabilities->SetsToZero = TRUE; > + } > + return EFI_SUCCESS; > +} > + > + > +/** > + Sets the current local time and date information. > + > + @param Time A pointer to the current time. > + > + @retval EFI_SUCCESS The operation completed successfully. > + @retval EFI_INVALID_PARAMETER A time field is out of range. > + @retval EFI_DEVICE_ERROR The time could not be set due due to hardware > + error. > + > +**/ > +EFI_STATUS > +EFIAPI > +LibSetTime ( > + IN EFI_TIME *Time > + ) > +{ > + RTC_I2C_REQUEST Op; > + RTC_DATETIME DateTime; > + EFI_STATUS Status; > + UINT8 Reg; > + > + if (mI2cMaster == NULL) { > + return EFI_DEVICE_ERROR; > + } > + > + DateTime.VL_seconds = DecimalToBcd8 (Time->Second); > + DateTime.Minutes = DecimalToBcd8 (Time->Minute); > + DateTime.Hours = DecimalToBcd8 (Time->Hour); > + DateTime.Days = DecimalToBcd8 (Time->Day); > + DateTime.Century_months = DecimalToBcd8 (Time->Month); > + DateTime.Years = DecimalToBcd8 (Time->Year % 100); > + if (Time->Year >= EPOCH_BASE + 100) { > + DateTime.Century_months |= PCF8563_CENTURY_MASK; Are you going to slap me if I ask for this to return error if Time->Year >= EPOCH_BASE + 200? > + } > + > + Reg = PCF8563_DATA_REG_OFFSET; > + > + Op.OperationCount = 2; > + > + Op.SetAddressOp.Flags = 0; > + Op.SetAddressOp.LengthInBytes = 1; > + Op.SetAddressOp.Buffer = &Reg; > + > + Op.GetSetDateTimeOp.Flags = 0; > + Op.GetSetDateTimeOp.LengthInBytes = sizeof (RTC_DATETIME); > + Op.GetSetDateTimeOp.Buffer = (VOID *)&DateTime; > + > + Status = mI2cMaster->StartRequest (mI2cMaster, SLAVE_ADDRESS, > + (VOID *)&Op, NULL, NULL); > + if (EFI_ERROR (Status)) { > + return EFI_DEVICE_ERROR; > + } > + return EFI_SUCCESS; > +} > + > + > +/** > + Returns the current wakeup alarm clock setting. > + > + @param Enabled Indicates if the alarm is currently enabled or > + disabled. > + @param Pending Indicates if the alarm signal is pending and > + requires acknowledgement. > + @param Time The current alarm setting. > + > + @retval EFI_SUCCESS The alarm settings were returned. > + @retval EFI_INVALID_PARAMETER Any parameter is NULL. > + @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a > + hardware error. > + > +**/ > +EFI_STATUS > +EFIAPI > +LibGetWakeupTime ( > + OUT BOOLEAN *Enabled, > + OUT BOOLEAN *Pending, > + OUT EFI_TIME *Time > + ) > +{ > + return EFI_UNSUPPORTED; For now, unsupported by current platforms, or unsupportable by this RTC device? > +} > + > + > +/** > + Sets the system wakeup alarm clock time. > + > + @param Enabled Enable or disable the wakeup alarm. > + @param Time If Enable is TRUE, the time to set the wakeup > + alarm for. > + > + @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was > + enabled. If Enable is FALSE, then the wakeup > + alarm was disabled. > + @retval EFI_INVALID_PARAMETER A time field is out of range. > + @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a > + hardware error. > + @retval EFI_UNSUPPORTED A wakeup timer is not supported on this > + platform. > + > +**/ > +EFI_STATUS > +EFIAPI > +LibSetWakeupTime ( > + IN BOOLEAN Enabled, > + OUT EFI_TIME *Time > + ) > +{ > + return EFI_UNSUPPORTED; Same question. > +} > + > +STATIC > +VOID > +DriverRegistrationEvent ( > + IN EFI_EVENT Event, > + IN VOID *Context > + ) > +{ > + EFI_HANDLE Handle[2]; > + UINTN BufferSize; > + EFI_STATUS Status; > + EFI_I2C_MASTER_PROTOCOL *I2cMaster; > + UINTN BusFrequency; > + > + // > + // Try to connect the newly registered driver to our handle. > + // > + do { > + BufferSize = sizeof (EFI_HANDLE); > + Status = gBS->LocateHandle (ByRegisterNotify, > + &gEfiDriverBindingProtocolGuid, > + mDriverEventRegistration, > + &BufferSize, > + Handle); > + if (EFI_ERROR (Status)) { > + if (Status != EFI_NOT_FOUND) { > + DEBUG ((DEBUG_WARN, "%a: gBS->LocateHandle () returned %r\n", > + __FUNCTION__, Status)); > + } > + break; > + } > + > + // > + // Check if we can connect our handle to this driver. > + // > + Handle[1] = NULL; > + Status = gBS->ConnectController (mI2cMasterHandle, Handle, NULL, FALSE); > + if (EFI_ERROR (Status)) { > + continue; > + } > + > + DEBUG ((DEBUG_INFO, "%a: found I2C master!\n", __FUNCTION__)); > + > + gBS->CloseEvent (Event); > + > + Status = gBS->OpenProtocol (mI2cMasterHandle, &gEfiI2cMasterProtocolGuid, > + (VOID **)&I2cMaster, gImageHandle, NULL, > + EFI_OPEN_PROTOCOL_EXCLUSIVE); > + ASSERT_EFI_ERROR (Status); > + > + Status = I2cMaster->Reset (I2cMaster); > + if (EFI_ERROR (Status)) { > + DEBUG ((DEBUG_ERROR, "%a: I2CMaster->Reset () failed - %r\n", > + __FUNCTION__, Status)); > + break; > + } > + > + BusFrequency = FixedPcdGet16 (PcdI2cBusFrequency); > + Status = I2cMaster->SetBusFrequency (I2cMaster, &BusFrequency); > + if (EFI_ERROR (Status)) { > + DEBUG ((DEBUG_ERROR, "%a: I2CMaster->SetBusFrequency () failed - %r\n", > + __FUNCTION__, Status)); > + break; > + } > + > + mI2cMaster = I2cMaster; > + break; > + } while (TRUE); > +} > + > +/** > + Fixup internal data so that EFI can be call in virtual mode. > + Call the passed in Child Notify event and convert any pointers in > + lib to virtual mode. > + > + @param[in] Event The Event that is being processed > + @param[in] Context Event Context > +**/ > +VOID > +EFIAPI > +LibRtcVirtualNotifyEvent ( > + IN EFI_EVENT Event, > + IN VOID *Context > + ) > +{ > + EfiConvertPointer (0x0, (VOID **)&mI2cMaster); > +} > + > +/** > + Library entry point > + > + @param ImageHandle Handle that identifies the loaded image. > + @param SystemTable System Table for this image. > + > + @retval EFI_SUCCESS The operation completed successfully. > + > +**/ > +EFI_STATUS > +EFIAPI > +LibRtcInitialize ( > + IN EFI_HANDLE ImageHandle, > + IN EFI_SYSTEM_TABLE *SystemTable > + ) > +{ > + EFI_STATUS Status; > + UINTN BufferSize; > + > + // > + // Find the handle that marks the controller > + // that will provide the I2C master protocol. > + // > + BufferSize = sizeof (EFI_HANDLE); > + Status = gBS->LocateHandle (ByProtocol, > + &gPcf8563RealTimeClockLibI2cMasterProtolGuid, NULL, > + &BufferSize, &mI2cMasterHandle); > + ASSERT_EFI_ERROR (Status); > + > + // > + // Register a protocol registration notification callback on the driver > + // binding protocol so we can attempt to connect our I2C master to it > + // as soon as it appears. > + // > + EfiCreateProtocolNotifyEvent ( > + &gEfiDriverBindingProtocolGuid, > + TPL_CALLBACK, > + DriverRegistrationEvent, > + NULL, > + &mDriverEventRegistration); > + > + // > + // Register for the virtual address change event > + // > + Status = gBS->CreateEventEx (EVT_NOTIFY_SIGNAL, TPL_NOTIFY, > + LibRtcVirtualNotifyEvent, NULL, > + &gEfiEventVirtualAddressChangeGuid, > + &mRtcVirtualAddrChangeEvent); > + ASSERT_EFI_ERROR (Status); > + > + return EFI_SUCCESS; > +} > diff --git a/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.dec b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.dec > new file mode 100644 > index 000000000000..bc8fc60c0602 > --- /dev/null > +++ b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.dec > @@ -0,0 +1,29 @@ > +#/** @file > +# > +# Copyright (c) 2017, Linaro, Ltd. All rights reserved.<BR> > +# > +# This program and the accompanying materials > +# are licensed and made available under the terms and conditions of the BSD License > +# which accompanies this distribution. The full text of the license may be found at > +# http://opensource.org/licenses/bsd-license.php > +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, > +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. > +# > +# > +#**/ > + > +[Defines] > + DEC_SPECIFICATION = 0x0001001A > + PACKAGE_NAME = Pcf8563RealTimeClockLib > + PACKAGE_GUID = 44d63668-c86b-446a-b303-f3a5176702aa > + PACKAGE_VERSION = 0.1 > + > +[Guids] > + gPcf8563RealTimeClockLibTokenSpaceGuid = { 0xaaf5b169, 0x93a0, 0x4d60, { 0xba, 0xe4, 0x06, 0x07, 0x92, 0x8e, 0x63, 0xdd }} > + > +[Protocols] > + gPcf8563RealTimeClockLibI2cMasterProtolGuid = { 0xa6af18ae, 0x3bd5, 0x4af9, { 0xbb, 0x6a, 0xdb, 0x85, 0x07, 0x62, 0x81, 0x38 }} > + > +[PcdsFixedAtBuild] > + gPcf8563RealTimeClockLibTokenSpaceGuid.PcdI2cSlaveAddress|0x51|UINT8|0x00000001 > + gPcf8563RealTimeClockLibTokenSpaceGuid.PcdI2cBusFrequency|400|UINT16|0x00000002 Could you add a comment clarifying unit? / Leif > diff --git a/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.inf b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.inf > new file mode 100644 > index 000000000000..e1e001162144 > --- /dev/null > +++ b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.inf > @@ -0,0 +1,52 @@ > +#/** @file > +# > +# Copyright (c) 2017, Linaro, Ltd. All rights reserved.<BR> > +# > +# This program and the accompanying materials > +# are licensed and made available under the terms and conditions of the BSD License > +# which accompanies this distribution. The full text of the license may be found at > +# http://opensource.org/licenses/bsd-license.php > +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, > +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. > +# > +# > +#**/ > + > +[Defines] > + INF_VERSION = 0x0001001A > + BASE_NAME = Pcf8563RealTimeClockLib > + FILE_GUID = 1e3d33f3-a671-4116-bd81-6b435296c1e0 > + MODULE_TYPE = DXE_DRIVER > + VERSION_STRING = 1.0 > + LIBRARY_CLASS = RealTimeClockLib > + > +[Sources.common] > + Pcf8563RealTimeClockLib.c > + > +[Packages] > + MdePkg/MdePkg.dec > + EmbeddedPkg/EmbeddedPkg.dec > + Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.dec > + > +[LibraryClasses] > + BaseMemoryLib > + DebugLib > + IoLib > + UefiBootServicesTableLib > + UefiLib > + UefiRuntimeLib > + > +[Guids] > + gEfiEventVirtualAddressChangeGuid > + > +[Protocols] > + gEfiDriverBindingProtocolGuid ## CONSUMES > + gEfiI2cMasterProtocolGuid ## CONSUMES > + gPcf8563RealTimeClockLibI2cMasterProtolGuid ## CONSUMES > + > +[FixedPcd] > + gPcf8563RealTimeClockLibTokenSpaceGuid.PcdI2cSlaveAddress > + gPcf8563RealTimeClockLibTokenSpaceGuid.PcdI2cBusFrequency > + > +[Depex] > + gPcf8563RealTimeClockLibI2cMasterProtolGuid > -- > 2.11.0 > _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel
On Fri, Nov 03, 2017 at 10:16:52AM +0000, Ard Biesheuvel wrote: > Add a RealTimeClockLib implementation for the NXP PCF8563 as used on > the Socionext Developer Box board. Note that the standard I2C protocol > stack does not support runtime use, so this driver invokes the I2C master > protocol directly. This requires support from the platform side as well, > and so this driver will only attach to a I2C master that has the > gPcf8563RealTimeClockLibI2cMasterProtolGuid protocol installed on its > handle. It is up to the platform to ensure that the driver producing > the I2C master protocol in question is runtime capable, and is not > shared with the I2C protocol stack (i.e., it should not have the I2C > Bus Configuration Management protocol installed as well). > > Contributed-under: TianoCore Contribution Agreement 1.1 > Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> Reviewed-by: Leif Lindholm <leif.lindholm@linaro.org> > --- > Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.c | 385 ++++++++++++++++++++ > Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.dec | 29 ++ > Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.inf | 52 +++ > 3 files changed, 466 insertions(+) _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel
On 8 November 2017 at 16:43, Leif Lindholm <leif.lindholm@linaro.org> wrote: > On Fri, Nov 03, 2017 at 10:16:52AM +0000, Ard Biesheuvel wrote: >> Add a RealTimeClockLib implementation for the NXP PCF8563 as used on >> the Socionext Developer Box board. Note that the standard I2C protocol >> stack does not support runtime use, so this driver invokes the I2C master >> protocol directly. This requires support from the platform side as well, >> and so this driver will only attach to a I2C master that has the >> gPcf8563RealTimeClockLibI2cMasterProtolGuid protocol installed on its >> handle. It is up to the platform to ensure that the driver producing >> the I2C master protocol in question is runtime capable, and is not >> shared with the I2C protocol stack (i.e., it should not have the I2C >> Bus Configuration Management protocol installed as well). >> >> Contributed-under: TianoCore Contribution Agreement 1.1 >> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> >> --- >> Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.c | 385 ++++++++++++++++++++ >> Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.dec | 29 ++ >> Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.inf | 52 +++ >> 3 files changed, 466 insertions(+) >> >> diff --git a/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.c b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.c >> new file mode 100644 >> index 000000000000..fea65a225d7f >> --- /dev/null >> +++ b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.c >> @@ -0,0 +1,385 @@ >> +/** @file >> + >> + Copyright (c) 2017, Linaro, Ltd. All rights reserved.<BR> >> + >> + This program and the accompanying materials >> + are licensed and made available under the terms and conditions of the BSD License >> + which accompanies this distribution. The full text of the license may be found at >> + http://opensource.org/licenses/bsd-license.php >> + >> + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, >> + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. >> + >> +**/ >> + >> +#include <PiDxe.h> >> +#include <Library/DebugLib.h> >> +#include <Library/IoLib.h> >> +#include <Library/RealTimeClockLib.h> >> +#include <Library/UefiBootServicesTableLib.h> >> +#include <Library/UefiLib.h> >> +#include <Library/UefiRuntimeLib.h> >> +#include <Protocol/I2cMaster.h> >> + >> +#define SLAVE_ADDRESS (FixedPcdGet8 (PcdI2cSlaveAddress)) >> +#define PCF8563_DATA_REG_OFFSET 0x2 >> + >> +#define PCF8563_SECONDS_MASK 0x7f >> +#define PCF8563_MINUTES_MASK 0x7f >> +#define PCF8563_HOURS_MASK 0x3f >> +#define PCF8563_DAYS_MASK 0x3f >> +#define PCF8563_WEEKDAYS_MASK 0x07 >> +#define PCF8563_MONTHS_MASK 0x1f >> +#define PCF8563_CENTURY_MASK 0x80 >> + >> +#define EPOCH_BASE 2000 >> + >> +STATIC EFI_HANDLE mI2cMasterHandle; >> +STATIC VOID *mDriverEventRegistration; >> +STATIC EFI_I2C_MASTER_PROTOCOL *mI2cMaster; >> +STATIC EFI_EVENT mRtcVirtualAddrChangeEvent; >> + >> +typedef struct { >> + UINTN OperationCount; >> + EFI_I2C_OPERATION SetAddressOp; >> + EFI_I2C_OPERATION GetSetDateTimeOp; >> +} RTC_I2C_REQUEST; >> + >> +#pragma pack(1) >> +typedef struct { >> + UINT8 VL_seconds; >> + UINT8 Minutes; >> + UINT8 Hours; >> + UINT8 Days; >> + UINT8 Weekdays; >> + UINT8 Century_months; >> + UINT8 Years; >> +} RTC_DATETIME; >> +#pragma pack() >> + >> +/** >> + Returns the current time and date information, and the time-keeping >> + capabilities of the hardware platform. >> + >> + @param Time A pointer to storage to receive a snapshot of >> + the current time. >> + @param Capabilities An optional pointer to a buffer to receive the >> + real time clock device's capabilities. >> + >> + @retval EFI_SUCCESS The operation completed successfully. >> + @retval EFI_INVALID_PARAMETER Time is NULL. >> + @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware >> + error. >> + >> +**/ >> +EFI_STATUS >> +EFIAPI >> +LibGetTime ( >> + OUT EFI_TIME *Time, >> + OUT EFI_TIME_CAPABILITIES *Capabilities >> + ) >> +{ >> + RTC_I2C_REQUEST Op; >> + RTC_DATETIME DateTime; >> + EFI_STATUS Status; >> + UINT8 Reg; >> + >> + if (Time == NULL) { >> + return EFI_INVALID_PARAMETER; >> + } >> + >> + if (mI2cMaster == NULL) { >> + return EFI_DEVICE_ERROR; >> + } >> + >> + Reg = PCF8563_DATA_REG_OFFSET; >> + >> + Op.OperationCount = 2; >> + >> + Op.SetAddressOp.Flags = 0; >> + Op.SetAddressOp.LengthInBytes = 1; >> + Op.SetAddressOp.Buffer = &Reg; >> + >> + Op.GetSetDateTimeOp.Flags = I2C_FLAG_READ; >> + Op.GetSetDateTimeOp.LengthInBytes = sizeof (RTC_DATETIME); >> + Op.GetSetDateTimeOp.Buffer = (VOID *)&DateTime; >> + >> + Status = mI2cMaster->StartRequest (mI2cMaster, SLAVE_ADDRESS, >> + (VOID *)&Op, NULL, NULL); >> + if (EFI_ERROR (Status)) { >> + return EFI_DEVICE_ERROR; >> + } >> + >> + Time->Second = BcdToDecimal8 (DateTime.VL_seconds & PCF8563_SECONDS_MASK); >> + Time->Minute = BcdToDecimal8 (DateTime.Minutes & PCF8563_MINUTES_MASK); >> + Time->Hour = BcdToDecimal8 (DateTime.Hours & PCF8563_HOURS_MASK); >> + Time->Day = BcdToDecimal8 (DateTime.Days & PCF8563_DAYS_MASK); >> + Time->Month = BcdToDecimal8 (DateTime.Century_months & PCF8563_MONTHS_MASK); >> + Time->Year = BcdToDecimal8 (DateTime.Years) + EPOCH_BASE; >> + >> + if (DateTime.Century_months & PCF8563_CENTURY_MASK) { >> + Time->Year += 100; > > Guess we'd better retire all these platforms before the year 2100 > then... (Or, given EPOCH_BASE, is it 2070? (Or given EPOCH_BASE being > set to 2000, is it 2200?)) > > Could you add a small comment next to the EPOCH_BASE definition > explaining the constrainst? > Well, ultimately it doesn't really matter, given that this IP treats all years divisible by 4 as leap years, so regardless of which base we choose, it is going to jump to 29/2/2100 no matter what. So the EPOCH_BASE could even be 2016: UEFI fully owns the RTC so it can do whatever it wants. 2000 just seemed the least arbitrary. >> + } >> + >> + Time->Daylight = EFI_TIME_ADJUST_DAYLIGHT; >> + Time->TimeZone = 0; /* UTC */ >> + >> + if (Capabilities != NULL) { >> + Capabilities->Resolution = 1; >> + Capabilities->Accuracy = 0; >> + Capabilities->SetsToZero = TRUE; >> + } >> + return EFI_SUCCESS; >> +} >> + >> + >> +/** >> + Sets the current local time and date information. >> + >> + @param Time A pointer to the current time. >> + >> + @retval EFI_SUCCESS The operation completed successfully. >> + @retval EFI_INVALID_PARAMETER A time field is out of range. >> + @retval EFI_DEVICE_ERROR The time could not be set due due to hardware >> + error. >> + >> +**/ >> +EFI_STATUS >> +EFIAPI >> +LibSetTime ( >> + IN EFI_TIME *Time >> + ) >> +{ >> + RTC_I2C_REQUEST Op; >> + RTC_DATETIME DateTime; >> + EFI_STATUS Status; >> + UINT8 Reg; >> + >> + if (mI2cMaster == NULL) { >> + return EFI_DEVICE_ERROR; >> + } >> + >> + DateTime.VL_seconds = DecimalToBcd8 (Time->Second); >> + DateTime.Minutes = DecimalToBcd8 (Time->Minute); >> + DateTime.Hours = DecimalToBcd8 (Time->Hour); >> + DateTime.Days = DecimalToBcd8 (Time->Day); >> + DateTime.Century_months = DecimalToBcd8 (Time->Month); >> + DateTime.Years = DecimalToBcd8 (Time->Year % 100); >> + if (Time->Year >= EPOCH_BASE + 100) { >> + DateTime.Century_months |= PCF8563_CENTURY_MASK; > > Are you going to slap me if I ask for this to return error if > Time->Year >= EPOCH_BASE + 200? > Actually, I think it would be better to treat the century bit as a toggle, depending on whether (year % 200) >= 100 >> + } >> + >> + Reg = PCF8563_DATA_REG_OFFSET; >> + >> + Op.OperationCount = 2; >> + >> + Op.SetAddressOp.Flags = 0; >> + Op.SetAddressOp.LengthInBytes = 1; >> + Op.SetAddressOp.Buffer = &Reg; >> + >> + Op.GetSetDateTimeOp.Flags = 0; >> + Op.GetSetDateTimeOp.LengthInBytes = sizeof (RTC_DATETIME); >> + Op.GetSetDateTimeOp.Buffer = (VOID *)&DateTime; >> + >> + Status = mI2cMaster->StartRequest (mI2cMaster, SLAVE_ADDRESS, >> + (VOID *)&Op, NULL, NULL); >> + if (EFI_ERROR (Status)) { >> + return EFI_DEVICE_ERROR; >> + } >> + return EFI_SUCCESS; >> +} >> + >> + >> +/** >> + Returns the current wakeup alarm clock setting. >> + >> + @param Enabled Indicates if the alarm is currently enabled or >> + disabled. >> + @param Pending Indicates if the alarm signal is pending and >> + requires acknowledgement. >> + @param Time The current alarm setting. >> + >> + @retval EFI_SUCCESS The alarm settings were returned. >> + @retval EFI_INVALID_PARAMETER Any parameter is NULL. >> + @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a >> + hardware error. >> + >> +**/ >> +EFI_STATUS >> +EFIAPI >> +LibGetWakeupTime ( >> + OUT BOOLEAN *Enabled, >> + OUT BOOLEAN *Pending, >> + OUT EFI_TIME *Time >> + ) >> +{ >> + return EFI_UNSUPPORTED; > > For now, unsupported by current platforms, or unsupportable by this > RTC device? > Well, getting and setting the wakeup time should be fairly trivial to implement, but I can't really test it on the hardware we have (the INT line is not connected), so I didn't bother. I'll add a comment to that effect. >> +} >> + >> + >> +/** >> + Sets the system wakeup alarm clock time. >> + >> + @param Enabled Enable or disable the wakeup alarm. >> + @param Time If Enable is TRUE, the time to set the wakeup >> + alarm for. >> + >> + @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was >> + enabled. If Enable is FALSE, then the wakeup >> + alarm was disabled. >> + @retval EFI_INVALID_PARAMETER A time field is out of range. >> + @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a >> + hardware error. >> + @retval EFI_UNSUPPORTED A wakeup timer is not supported on this >> + platform. >> + >> +**/ >> +EFI_STATUS >> +EFIAPI >> +LibSetWakeupTime ( >> + IN BOOLEAN Enabled, >> + OUT EFI_TIME *Time >> + ) >> +{ >> + return EFI_UNSUPPORTED; > > Same question. > Same answer. Trivial to implement, impossible to test fully atm (especially because I fried my board today) >> +} >> + >> +STATIC >> +VOID >> +DriverRegistrationEvent ( >> + IN EFI_EVENT Event, >> + IN VOID *Context >> + ) >> +{ >> + EFI_HANDLE Handle[2]; >> + UINTN BufferSize; >> + EFI_STATUS Status; >> + EFI_I2C_MASTER_PROTOCOL *I2cMaster; >> + UINTN BusFrequency; >> + >> + // >> + // Try to connect the newly registered driver to our handle. >> + // >> + do { >> + BufferSize = sizeof (EFI_HANDLE); >> + Status = gBS->LocateHandle (ByRegisterNotify, >> + &gEfiDriverBindingProtocolGuid, >> + mDriverEventRegistration, >> + &BufferSize, >> + Handle); >> + if (EFI_ERROR (Status)) { >> + if (Status != EFI_NOT_FOUND) { >> + DEBUG ((DEBUG_WARN, "%a: gBS->LocateHandle () returned %r\n", >> + __FUNCTION__, Status)); >> + } >> + break; >> + } >> + >> + // >> + // Check if we can connect our handle to this driver. >> + // >> + Handle[1] = NULL; >> + Status = gBS->ConnectController (mI2cMasterHandle, Handle, NULL, FALSE); >> + if (EFI_ERROR (Status)) { >> + continue; >> + } >> + >> + DEBUG ((DEBUG_INFO, "%a: found I2C master!\n", __FUNCTION__)); >> + >> + gBS->CloseEvent (Event); >> + >> + Status = gBS->OpenProtocol (mI2cMasterHandle, &gEfiI2cMasterProtocolGuid, >> + (VOID **)&I2cMaster, gImageHandle, NULL, >> + EFI_OPEN_PROTOCOL_EXCLUSIVE); >> + ASSERT_EFI_ERROR (Status); >> + >> + Status = I2cMaster->Reset (I2cMaster); >> + if (EFI_ERROR (Status)) { >> + DEBUG ((DEBUG_ERROR, "%a: I2CMaster->Reset () failed - %r\n", >> + __FUNCTION__, Status)); >> + break; >> + } >> + >> + BusFrequency = FixedPcdGet16 (PcdI2cBusFrequency); >> + Status = I2cMaster->SetBusFrequency (I2cMaster, &BusFrequency); >> + if (EFI_ERROR (Status)) { >> + DEBUG ((DEBUG_ERROR, "%a: I2CMaster->SetBusFrequency () failed - %r\n", >> + __FUNCTION__, Status)); >> + break; >> + } >> + >> + mI2cMaster = I2cMaster; >> + break; >> + } while (TRUE); >> +} >> + >> +/** >> + Fixup internal data so that EFI can be call in virtual mode. >> + Call the passed in Child Notify event and convert any pointers in >> + lib to virtual mode. >> + >> + @param[in] Event The Event that is being processed >> + @param[in] Context Event Context >> +**/ >> +VOID >> +EFIAPI >> +LibRtcVirtualNotifyEvent ( >> + IN EFI_EVENT Event, >> + IN VOID *Context >> + ) >> +{ >> + EfiConvertPointer (0x0, (VOID **)&mI2cMaster); >> +} >> + >> +/** >> + Library entry point >> + >> + @param ImageHandle Handle that identifies the loaded image. >> + @param SystemTable System Table for this image. >> + >> + @retval EFI_SUCCESS The operation completed successfully. >> + >> +**/ >> +EFI_STATUS >> +EFIAPI >> +LibRtcInitialize ( >> + IN EFI_HANDLE ImageHandle, >> + IN EFI_SYSTEM_TABLE *SystemTable >> + ) >> +{ >> + EFI_STATUS Status; >> + UINTN BufferSize; >> + >> + // >> + // Find the handle that marks the controller >> + // that will provide the I2C master protocol. >> + // >> + BufferSize = sizeof (EFI_HANDLE); >> + Status = gBS->LocateHandle (ByProtocol, >> + &gPcf8563RealTimeClockLibI2cMasterProtolGuid, NULL, >> + &BufferSize, &mI2cMasterHandle); >> + ASSERT_EFI_ERROR (Status); >> + >> + // >> + // Register a protocol registration notification callback on the driver >> + // binding protocol so we can attempt to connect our I2C master to it >> + // as soon as it appears. >> + // >> + EfiCreateProtocolNotifyEvent ( >> + &gEfiDriverBindingProtocolGuid, >> + TPL_CALLBACK, >> + DriverRegistrationEvent, >> + NULL, >> + &mDriverEventRegistration); >> + >> + // >> + // Register for the virtual address change event >> + // >> + Status = gBS->CreateEventEx (EVT_NOTIFY_SIGNAL, TPL_NOTIFY, >> + LibRtcVirtualNotifyEvent, NULL, >> + &gEfiEventVirtualAddressChangeGuid, >> + &mRtcVirtualAddrChangeEvent); >> + ASSERT_EFI_ERROR (Status); >> + >> + return EFI_SUCCESS; >> +} >> diff --git a/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.dec b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.dec >> new file mode 100644 >> index 000000000000..bc8fc60c0602 >> --- /dev/null >> +++ b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.dec >> @@ -0,0 +1,29 @@ >> +#/** @file >> +# >> +# Copyright (c) 2017, Linaro, Ltd. All rights reserved.<BR> >> +# >> +# This program and the accompanying materials >> +# are licensed and made available under the terms and conditions of the BSD License >> +# which accompanies this distribution. The full text of the license may be found at >> +# http://opensource.org/licenses/bsd-license.php >> +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, >> +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. >> +# >> +# >> +#**/ >> + >> +[Defines] >> + DEC_SPECIFICATION = 0x0001001A >> + PACKAGE_NAME = Pcf8563RealTimeClockLib >> + PACKAGE_GUID = 44d63668-c86b-446a-b303-f3a5176702aa >> + PACKAGE_VERSION = 0.1 >> + >> +[Guids] >> + gPcf8563RealTimeClockLibTokenSpaceGuid = { 0xaaf5b169, 0x93a0, 0x4d60, { 0xba, 0xe4, 0x06, 0x07, 0x92, 0x8e, 0x63, 0xdd }} >> + >> +[Protocols] >> + gPcf8563RealTimeClockLibI2cMasterProtolGuid = { 0xa6af18ae, 0x3bd5, 0x4af9, { 0xbb, 0x6a, 0xdb, 0x85, 0x07, 0x62, 0x81, 0x38 }} >> + >> +[PcdsFixedAtBuild] >> + gPcf8563RealTimeClockLibTokenSpaceGuid.PcdI2cSlaveAddress|0x51|UINT8|0x00000001 >> + gPcf8563RealTimeClockLibTokenSpaceGuid.PcdI2cBusFrequency|400|UINT16|0x00000002 > > Could you add a comment clarifying unit? > Yep, and actually this is wrong. It should be in Hz not kHz >> diff --git a/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.inf b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.inf >> new file mode 100644 >> index 000000000000..e1e001162144 >> --- /dev/null >> +++ b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.inf >> @@ -0,0 +1,52 @@ >> +#/** @file >> +# >> +# Copyright (c) 2017, Linaro, Ltd. All rights reserved.<BR> >> +# >> +# This program and the accompanying materials >> +# are licensed and made available under the terms and conditions of the BSD License >> +# which accompanies this distribution. The full text of the license may be found at >> +# http://opensource.org/licenses/bsd-license.php >> +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, >> +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. >> +# >> +# >> +#**/ >> + >> +[Defines] >> + INF_VERSION = 0x0001001A >> + BASE_NAME = Pcf8563RealTimeClockLib >> + FILE_GUID = 1e3d33f3-a671-4116-bd81-6b435296c1e0 >> + MODULE_TYPE = DXE_DRIVER >> + VERSION_STRING = 1.0 >> + LIBRARY_CLASS = RealTimeClockLib >> + >> +[Sources.common] >> + Pcf8563RealTimeClockLib.c >> + >> +[Packages] >> + MdePkg/MdePkg.dec >> + EmbeddedPkg/EmbeddedPkg.dec >> + Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.dec >> + >> +[LibraryClasses] >> + BaseMemoryLib >> + DebugLib >> + IoLib >> + UefiBootServicesTableLib >> + UefiLib >> + UefiRuntimeLib >> + >> +[Guids] >> + gEfiEventVirtualAddressChangeGuid >> + >> +[Protocols] >> + gEfiDriverBindingProtocolGuid ## CONSUMES >> + gEfiI2cMasterProtocolGuid ## CONSUMES >> + gPcf8563RealTimeClockLibI2cMasterProtolGuid ## CONSUMES >> + >> +[FixedPcd] >> + gPcf8563RealTimeClockLibTokenSpaceGuid.PcdI2cSlaveAddress >> + gPcf8563RealTimeClockLibTokenSpaceGuid.PcdI2cBusFrequency >> + >> +[Depex] >> + gPcf8563RealTimeClockLibI2cMasterProtolGuid >> -- >> 2.11.0 >> _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel
On 8 November 2017 at 16:47, Leif Lindholm <leif.lindholm@linaro.org> wrote: > On Fri, Nov 03, 2017 at 10:16:52AM +0000, Ard Biesheuvel wrote: >> Add a RealTimeClockLib implementation for the NXP PCF8563 as used on >> the Socionext Developer Box board. Note that the standard I2C protocol >> stack does not support runtime use, so this driver invokes the I2C master >> protocol directly. This requires support from the platform side as well, >> and so this driver will only attach to a I2C master that has the >> gPcf8563RealTimeClockLibI2cMasterProtolGuid protocol installed on its >> handle. It is up to the platform to ensure that the driver producing >> the I2C master protocol in question is runtime capable, and is not >> shared with the I2C protocol stack (i.e., it should not have the I2C >> Bus Configuration Management protocol installed as well). >> >> Contributed-under: TianoCore Contribution Agreement 1.1 >> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> > > Reviewed-by: Leif Lindholm <leif.lindholm@linaro.org> > Ehm, is this in response to the correct patch, given that you already asked for some changes? _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel
On Wed, Nov 08, 2017 at 05:45:11PM +0000, Ard Biesheuvel wrote: > On 8 November 2017 at 16:47, Leif Lindholm <leif.lindholm@linaro.org> wrote: > > On Fri, Nov 03, 2017 at 10:16:52AM +0000, Ard Biesheuvel wrote: > >> Add a RealTimeClockLib implementation for the NXP PCF8563 as used on > >> the Socionext Developer Box board. Note that the standard I2C protocol > >> stack does not support runtime use, so this driver invokes the I2C master > >> protocol directly. This requires support from the platform side as well, > >> and so this driver will only attach to a I2C master that has the > >> gPcf8563RealTimeClockLibI2cMasterProtolGuid protocol installed on its > >> handle. It is up to the platform to ensure that the driver producing > >> the I2C master protocol in question is runtime capable, and is not > >> shared with the I2C protocol stack (i.e., it should not have the I2C > >> Bus Configuration Management protocol installed as well). > >> > >> Contributed-under: TianoCore Contribution Agreement 1.1 > >> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> > > > > Reviewed-by: Leif Lindholm <leif.lindholm@linaro.org> > > Ehm, is this in response to the correct patch, given that you already > asked for some changes? No, that's me getting confused by the patches appearing out of order in my inbox... Apologies. / Leif _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel
diff --git a/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.c b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.c new file mode 100644 index 000000000000..fea65a225d7f --- /dev/null +++ b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.c @@ -0,0 +1,385 @@ +/** @file + + Copyright (c) 2017, Linaro, Ltd. All rights reserved.<BR> + + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BSD License + which accompanies this distribution. The full text of the license may be found at + http://opensource.org/licenses/bsd-license.php + + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +**/ + +#include <PiDxe.h> +#include <Library/DebugLib.h> +#include <Library/IoLib.h> +#include <Library/RealTimeClockLib.h> +#include <Library/UefiBootServicesTableLib.h> +#include <Library/UefiLib.h> +#include <Library/UefiRuntimeLib.h> +#include <Protocol/I2cMaster.h> + +#define SLAVE_ADDRESS (FixedPcdGet8 (PcdI2cSlaveAddress)) +#define PCF8563_DATA_REG_OFFSET 0x2 + +#define PCF8563_SECONDS_MASK 0x7f +#define PCF8563_MINUTES_MASK 0x7f +#define PCF8563_HOURS_MASK 0x3f +#define PCF8563_DAYS_MASK 0x3f +#define PCF8563_WEEKDAYS_MASK 0x07 +#define PCF8563_MONTHS_MASK 0x1f +#define PCF8563_CENTURY_MASK 0x80 + +#define EPOCH_BASE 2000 + +STATIC EFI_HANDLE mI2cMasterHandle; +STATIC VOID *mDriverEventRegistration; +STATIC EFI_I2C_MASTER_PROTOCOL *mI2cMaster; +STATIC EFI_EVENT mRtcVirtualAddrChangeEvent; + +typedef struct { + UINTN OperationCount; + EFI_I2C_OPERATION SetAddressOp; + EFI_I2C_OPERATION GetSetDateTimeOp; +} RTC_I2C_REQUEST; + +#pragma pack(1) +typedef struct { + UINT8 VL_seconds; + UINT8 Minutes; + UINT8 Hours; + UINT8 Days; + UINT8 Weekdays; + UINT8 Century_months; + UINT8 Years; +} RTC_DATETIME; +#pragma pack() + +/** + Returns the current time and date information, and the time-keeping + capabilities of the hardware platform. + + @param Time A pointer to storage to receive a snapshot of + the current time. + @param Capabilities An optional pointer to a buffer to receive the + real time clock device's capabilities. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER Time is NULL. + @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware + error. + +**/ +EFI_STATUS +EFIAPI +LibGetTime ( + OUT EFI_TIME *Time, + OUT EFI_TIME_CAPABILITIES *Capabilities + ) +{ + RTC_I2C_REQUEST Op; + RTC_DATETIME DateTime; + EFI_STATUS Status; + UINT8 Reg; + + if (Time == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (mI2cMaster == NULL) { + return EFI_DEVICE_ERROR; + } + + Reg = PCF8563_DATA_REG_OFFSET; + + Op.OperationCount = 2; + + Op.SetAddressOp.Flags = 0; + Op.SetAddressOp.LengthInBytes = 1; + Op.SetAddressOp.Buffer = &Reg; + + Op.GetSetDateTimeOp.Flags = I2C_FLAG_READ; + Op.GetSetDateTimeOp.LengthInBytes = sizeof (RTC_DATETIME); + Op.GetSetDateTimeOp.Buffer = (VOID *)&DateTime; + + Status = mI2cMaster->StartRequest (mI2cMaster, SLAVE_ADDRESS, + (VOID *)&Op, NULL, NULL); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + Time->Second = BcdToDecimal8 (DateTime.VL_seconds & PCF8563_SECONDS_MASK); + Time->Minute = BcdToDecimal8 (DateTime.Minutes & PCF8563_MINUTES_MASK); + Time->Hour = BcdToDecimal8 (DateTime.Hours & PCF8563_HOURS_MASK); + Time->Day = BcdToDecimal8 (DateTime.Days & PCF8563_DAYS_MASK); + Time->Month = BcdToDecimal8 (DateTime.Century_months & PCF8563_MONTHS_MASK); + Time->Year = BcdToDecimal8 (DateTime.Years) + EPOCH_BASE; + + if (DateTime.Century_months & PCF8563_CENTURY_MASK) { + Time->Year += 100; + } + + Time->Daylight = EFI_TIME_ADJUST_DAYLIGHT; + Time->TimeZone = 0; /* UTC */ + + if (Capabilities != NULL) { + Capabilities->Resolution = 1; + Capabilities->Accuracy = 0; + Capabilities->SetsToZero = TRUE; + } + return EFI_SUCCESS; +} + + +/** + Sets the current local time and date information. + + @param Time A pointer to the current time. + + @retval EFI_SUCCESS The operation completed successfully. + @retval EFI_INVALID_PARAMETER A time field is out of range. + @retval EFI_DEVICE_ERROR The time could not be set due due to hardware + error. + +**/ +EFI_STATUS +EFIAPI +LibSetTime ( + IN EFI_TIME *Time + ) +{ + RTC_I2C_REQUEST Op; + RTC_DATETIME DateTime; + EFI_STATUS Status; + UINT8 Reg; + + if (mI2cMaster == NULL) { + return EFI_DEVICE_ERROR; + } + + DateTime.VL_seconds = DecimalToBcd8 (Time->Second); + DateTime.Minutes = DecimalToBcd8 (Time->Minute); + DateTime.Hours = DecimalToBcd8 (Time->Hour); + DateTime.Days = DecimalToBcd8 (Time->Day); + DateTime.Century_months = DecimalToBcd8 (Time->Month); + DateTime.Years = DecimalToBcd8 (Time->Year % 100); + if (Time->Year >= EPOCH_BASE + 100) { + DateTime.Century_months |= PCF8563_CENTURY_MASK; + } + + Reg = PCF8563_DATA_REG_OFFSET; + + Op.OperationCount = 2; + + Op.SetAddressOp.Flags = 0; + Op.SetAddressOp.LengthInBytes = 1; + Op.SetAddressOp.Buffer = &Reg; + + Op.GetSetDateTimeOp.Flags = 0; + Op.GetSetDateTimeOp.LengthInBytes = sizeof (RTC_DATETIME); + Op.GetSetDateTimeOp.Buffer = (VOID *)&DateTime; + + Status = mI2cMaster->StartRequest (mI2cMaster, SLAVE_ADDRESS, + (VOID *)&Op, NULL, NULL); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + return EFI_SUCCESS; +} + + +/** + Returns the current wakeup alarm clock setting. + + @param Enabled Indicates if the alarm is currently enabled or + disabled. + @param Pending Indicates if the alarm signal is pending and + requires acknowledgement. + @param Time The current alarm setting. + + @retval EFI_SUCCESS The alarm settings were returned. + @retval EFI_INVALID_PARAMETER Any parameter is NULL. + @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a + hardware error. + +**/ +EFI_STATUS +EFIAPI +LibGetWakeupTime ( + OUT BOOLEAN *Enabled, + OUT BOOLEAN *Pending, + OUT EFI_TIME *Time + ) +{ + return EFI_UNSUPPORTED; +} + + +/** + Sets the system wakeup alarm clock time. + + @param Enabled Enable or disable the wakeup alarm. + @param Time If Enable is TRUE, the time to set the wakeup + alarm for. + + @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was + enabled. If Enable is FALSE, then the wakeup + alarm was disabled. + @retval EFI_INVALID_PARAMETER A time field is out of range. + @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a + hardware error. + @retval EFI_UNSUPPORTED A wakeup timer is not supported on this + platform. + +**/ +EFI_STATUS +EFIAPI +LibSetWakeupTime ( + IN BOOLEAN Enabled, + OUT EFI_TIME *Time + ) +{ + return EFI_UNSUPPORTED; +} + +STATIC +VOID +DriverRegistrationEvent ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + EFI_HANDLE Handle[2]; + UINTN BufferSize; + EFI_STATUS Status; + EFI_I2C_MASTER_PROTOCOL *I2cMaster; + UINTN BusFrequency; + + // + // Try to connect the newly registered driver to our handle. + // + do { + BufferSize = sizeof (EFI_HANDLE); + Status = gBS->LocateHandle (ByRegisterNotify, + &gEfiDriverBindingProtocolGuid, + mDriverEventRegistration, + &BufferSize, + Handle); + if (EFI_ERROR (Status)) { + if (Status != EFI_NOT_FOUND) { + DEBUG ((DEBUG_WARN, "%a: gBS->LocateHandle () returned %r\n", + __FUNCTION__, Status)); + } + break; + } + + // + // Check if we can connect our handle to this driver. + // + Handle[1] = NULL; + Status = gBS->ConnectController (mI2cMasterHandle, Handle, NULL, FALSE); + if (EFI_ERROR (Status)) { + continue; + } + + DEBUG ((DEBUG_INFO, "%a: found I2C master!\n", __FUNCTION__)); + + gBS->CloseEvent (Event); + + Status = gBS->OpenProtocol (mI2cMasterHandle, &gEfiI2cMasterProtocolGuid, + (VOID **)&I2cMaster, gImageHandle, NULL, + EFI_OPEN_PROTOCOL_EXCLUSIVE); + ASSERT_EFI_ERROR (Status); + + Status = I2cMaster->Reset (I2cMaster); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: I2CMaster->Reset () failed - %r\n", + __FUNCTION__, Status)); + break; + } + + BusFrequency = FixedPcdGet16 (PcdI2cBusFrequency); + Status = I2cMaster->SetBusFrequency (I2cMaster, &BusFrequency); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: I2CMaster->SetBusFrequency () failed - %r\n", + __FUNCTION__, Status)); + break; + } + + mI2cMaster = I2cMaster; + break; + } while (TRUE); +} + +/** + Fixup internal data so that EFI can be call in virtual mode. + Call the passed in Child Notify event and convert any pointers in + lib to virtual mode. + + @param[in] Event The Event that is being processed + @param[in] Context Event Context +**/ +VOID +EFIAPI +LibRtcVirtualNotifyEvent ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + EfiConvertPointer (0x0, (VOID **)&mI2cMaster); +} + +/** + Library entry point + + @param ImageHandle Handle that identifies the loaded image. + @param SystemTable System Table for this image. + + @retval EFI_SUCCESS The operation completed successfully. + +**/ +EFI_STATUS +EFIAPI +LibRtcInitialize ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + UINTN BufferSize; + + // + // Find the handle that marks the controller + // that will provide the I2C master protocol. + // + BufferSize = sizeof (EFI_HANDLE); + Status = gBS->LocateHandle (ByProtocol, + &gPcf8563RealTimeClockLibI2cMasterProtolGuid, NULL, + &BufferSize, &mI2cMasterHandle); + ASSERT_EFI_ERROR (Status); + + // + // Register a protocol registration notification callback on the driver + // binding protocol so we can attempt to connect our I2C master to it + // as soon as it appears. + // + EfiCreateProtocolNotifyEvent ( + &gEfiDriverBindingProtocolGuid, + TPL_CALLBACK, + DriverRegistrationEvent, + NULL, + &mDriverEventRegistration); + + // + // Register for the virtual address change event + // + Status = gBS->CreateEventEx (EVT_NOTIFY_SIGNAL, TPL_NOTIFY, + LibRtcVirtualNotifyEvent, NULL, + &gEfiEventVirtualAddressChangeGuid, + &mRtcVirtualAddrChangeEvent); + ASSERT_EFI_ERROR (Status); + + return EFI_SUCCESS; +} diff --git a/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.dec b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.dec new file mode 100644 index 000000000000..bc8fc60c0602 --- /dev/null +++ b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.dec @@ -0,0 +1,29 @@ +#/** @file +# +# Copyright (c) 2017, Linaro, Ltd. All rights reserved.<BR> +# +# This program and the accompanying materials +# are licensed and made available under the terms and conditions of the BSD License +# which accompanies this distribution. The full text of the license may be found at +# http://opensource.org/licenses/bsd-license.php +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +# +# +#**/ + +[Defines] + DEC_SPECIFICATION = 0x0001001A + PACKAGE_NAME = Pcf8563RealTimeClockLib + PACKAGE_GUID = 44d63668-c86b-446a-b303-f3a5176702aa + PACKAGE_VERSION = 0.1 + +[Guids] + gPcf8563RealTimeClockLibTokenSpaceGuid = { 0xaaf5b169, 0x93a0, 0x4d60, { 0xba, 0xe4, 0x06, 0x07, 0x92, 0x8e, 0x63, 0xdd }} + +[Protocols] + gPcf8563RealTimeClockLibI2cMasterProtolGuid = { 0xa6af18ae, 0x3bd5, 0x4af9, { 0xbb, 0x6a, 0xdb, 0x85, 0x07, 0x62, 0x81, 0x38 }} + +[PcdsFixedAtBuild] + gPcf8563RealTimeClockLibTokenSpaceGuid.PcdI2cSlaveAddress|0x51|UINT8|0x00000001 + gPcf8563RealTimeClockLibTokenSpaceGuid.PcdI2cBusFrequency|400|UINT16|0x00000002 diff --git a/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.inf b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.inf new file mode 100644 index 000000000000..e1e001162144 --- /dev/null +++ b/Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.inf @@ -0,0 +1,52 @@ +#/** @file +# +# Copyright (c) 2017, Linaro, Ltd. All rights reserved.<BR> +# +# This program and the accompanying materials +# are licensed and made available under the terms and conditions of the BSD License +# which accompanies this distribution. The full text of the license may be found at +# http://opensource.org/licenses/bsd-license.php +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +# +# +#**/ + +[Defines] + INF_VERSION = 0x0001001A + BASE_NAME = Pcf8563RealTimeClockLib + FILE_GUID = 1e3d33f3-a671-4116-bd81-6b435296c1e0 + MODULE_TYPE = DXE_DRIVER + VERSION_STRING = 1.0 + LIBRARY_CLASS = RealTimeClockLib + +[Sources.common] + Pcf8563RealTimeClockLib.c + +[Packages] + MdePkg/MdePkg.dec + EmbeddedPkg/EmbeddedPkg.dec + Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.dec + +[LibraryClasses] + BaseMemoryLib + DebugLib + IoLib + UefiBootServicesTableLib + UefiLib + UefiRuntimeLib + +[Guids] + gEfiEventVirtualAddressChangeGuid + +[Protocols] + gEfiDriverBindingProtocolGuid ## CONSUMES + gEfiI2cMasterProtocolGuid ## CONSUMES + gPcf8563RealTimeClockLibI2cMasterProtolGuid ## CONSUMES + +[FixedPcd] + gPcf8563RealTimeClockLibTokenSpaceGuid.PcdI2cSlaveAddress + gPcf8563RealTimeClockLibTokenSpaceGuid.PcdI2cBusFrequency + +[Depex] + gPcf8563RealTimeClockLibI2cMasterProtolGuid
Add a RealTimeClockLib implementation for the NXP PCF8563 as used on the Socionext Developer Box board. Note that the standard I2C protocol stack does not support runtime use, so this driver invokes the I2C master protocol directly. This requires support from the platform side as well, and so this driver will only attach to a I2C master that has the gPcf8563RealTimeClockLibI2cMasterProtolGuid protocol installed on its handle. It is up to the platform to ensure that the driver producing the I2C master protocol in question is runtime capable, and is not shared with the I2C protocol stack (i.e., it should not have the I2C Bus Configuration Management protocol installed as well). Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> --- Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.c | 385 ++++++++++++++++++++ Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.dec | 29 ++ Silicon/NXP/Library/Pcf8563RealTimeClockLib/Pcf8563RealTimeClockLib.inf | 52 +++ 3 files changed, 466 insertions(+) -- 2.11.0 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel