Re: [edk2] [PATCH 2/4] Platforms: Add ZX RTC driver for Sanchip SoC

2017-08-17 Thread Leif Lindholm
On Thu, Aug 17, 2017 at 11:43:37PM +0800, Jun Nie wrote:
> On 2017年08月10日 22:03, Leif Lindholm wrote:
> > On Wed, Aug 09, 2017 at 10:12:37PM +0800, Jun Nie wrote:
> > > Runtime service is not supported yet.
> > > 
> > > Contributed-under: TianoCore Contribution Agreement 1.0
> > > Signed-off-by: Jun Nie 
> > > ---
> > >   .../Zx6718RealTimeClockLib/Zx296718RealTimeClock.c | 376 
> > > +
> > >   .../Zx6718RealTimeClockLib/Zx296718RealTimeClock.h | 102 ++
> > >   .../Zx296718RealTimeClock.inf  |  42 +++
> > >   3 files changed, 520 insertions(+)
> > >   create mode 100644 
> > > Silicon/Sanchip/Library/Zx6718RealTimeClockLib/Zx296718RealTimeClock.c
> > >   create mode 100644 
> > > Silicon/Sanchip/Library/Zx6718RealTimeClockLib/Zx296718RealTimeClock.h
> > >   create mode 100644 
> > > Silicon/Sanchip/Library/Zx6718RealTimeClockLib/Zx296718RealTimeClock.inf
> > > 
> > > diff --git 
> > > a/Silicon/Sanchip/Library/Zx6718RealTimeClockLib/Zx296718RealTimeClock.c 
> > > b/Silicon/Sanchip/Library/Zx6718RealTimeClockLib/Zx296718RealTimeClock.c
> > > new file mode 100644
> > > index 000..af6e5bd
> > > --- /dev/null
> > > +++ 
> > > b/Silicon/Sanchip/Library/Zx6718RealTimeClockLib/Zx296718RealTimeClock.c
> > > @@ -0,0 +1,376 @@
> > > +/** @file
> > > +  Implement EFI RealTimeClock runtime services via RTC Lib.
> > > +
> > > +  Currently this driver does not support runtime virtual calling.
> > > +
> > > +  Copyright (C) 2017 Sanechips Technology Co., Ltd.
> > > +  Copyright (c) 2017, Linaro Limited.
> > > +
> > > +  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.
> > > +
> > > +  Based on the files under 
> > > ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.inf
> > > +
> > > +**/
> > > +
> > > +#include 
> > > +#include 
> > 
> > P before U.
> > 
> > > +#include 
> > > +#include 
> > > +#include 
> > > +#include 
> > > +#include 
> > > +#include 
> > > +#include 
> > > +#include 
> > > +#include 
> > > +#include 
> > > +// Use EfiAtRuntime to check stage
> > > +#include 
> > 
> > L (UefiRuntimeLib) before S (UefiRuntimeServices...).
> > No need for a comment explaining why we include headers.
> > 
> > > +#include 
> > > +#include "Zx296718RealTimeClock.h"
> > > +
> > > +STATIC UINTN   RtcBase;
> > 
> > mRtcBase.
> > 
> > > +STATIC BOOLEAN RTCInitialized = FALSE;
> > 
> > mRTCInitialized.
> > 
> > > +
> > > +BOOLEAN
> > > +EFIAPI
> > > +IsTimeValid(
> > > +  IN EFI_TIME *Time
> > > +  )
> > > +{
> > > +  // Check the input parameters are within the range specified by UEFI
> > > +  if ((Time->Year  < 2000) ||
> > > + (Time->Year   > 2099) ||
> > > + (Time->Month  < 1   ) ||
> > > + (Time->Month  > 12  ) ||
> > > + (Time->Day< 1   ) ||
> > > + (Time->Day> 31  ) ||
> > > + (Time->Hour   > 23  ) ||
> > > + (Time->Minute > 59  ) ||
> > > + (Time->Second > 59  ) ||
> > > + (Time->Nanosecond > 9) ||
> > > + (!((Time->TimeZone == EFI_UNSPECIFIED_TIMEZONE) || ((Time->TimeZone 
> > > >= -1440) && (Time->TimeZone <= 1440 ||
> > > + (Time->Daylight & (~(EFI_TIME_ADJUST_DAYLIGHT | 
> > > EFI_TIME_IN_DAYLIGHT)))
> > > +  ) {
> > > +return FALSE;
> > > +  }
> > > +
> > > +  return TRUE;
> > > +}
> > 
> > This appears a direct copy of the version in
> > EmbeddedPkg/Library/TimeBaseLib. Can you add that TimeBaseLib library
> > dependency to decrease duplication?
> > (This may not have existed as a standalone library at the time you
> > started this port.)
> > 
> > > +
> > > +VOID
> > 
> > A lot of the functions in this file could do with a STATIC prefix,
> > since they are only used internally.
> > 
> > > +Wait4Busy (
> > 
> > Semantically, this name is incorrect.
> > The function is waiting _while_ the state is RTC_BUSY, so seems to me
> > it should be called WaitBusy.
> > 
> > > +  VOID
> > > +  )
> > > +{
> > > +  UINT32 Val, Retry = 1000;
> > > +  do {
> > > +MicroSecondDelay (200);
> > 
> > Why 200?
> 
> It is just a suggested delay time according to RTC clock frequency. You want
> a RTC_WAIT_DELAY macro here for better understanding, right?

That would be more clear, yes.
It is also good if next to that definition there is some description
of how the value was detemined. (I.e., are we waiting for a particular
number of cycles, or is this simply a determined to be "good enough".)

/
Leif
___
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel


Re: [edk2] [PATCH 2/4] Platforms: Add ZX RTC driver for Sanchip SoC

2017-08-17 Thread Jun Nie

On 2017年08月10日 22:03, Leif Lindholm wrote:

On Wed, Aug 09, 2017 at 10:12:37PM +0800, Jun Nie wrote:

Runtime service is not supported yet.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jun Nie 
---
  .../Zx6718RealTimeClockLib/Zx296718RealTimeClock.c | 376 +
  .../Zx6718RealTimeClockLib/Zx296718RealTimeClock.h | 102 ++
  .../Zx296718RealTimeClock.inf  |  42 +++
  3 files changed, 520 insertions(+)
  create mode 100644 
Silicon/Sanchip/Library/Zx6718RealTimeClockLib/Zx296718RealTimeClock.c
  create mode 100644 
Silicon/Sanchip/Library/Zx6718RealTimeClockLib/Zx296718RealTimeClock.h
  create mode 100644 
Silicon/Sanchip/Library/Zx6718RealTimeClockLib/Zx296718RealTimeClock.inf

diff --git 
a/Silicon/Sanchip/Library/Zx6718RealTimeClockLib/Zx296718RealTimeClock.c 
b/Silicon/Sanchip/Library/Zx6718RealTimeClockLib/Zx296718RealTimeClock.c
new file mode 100644
index 000..af6e5bd
--- /dev/null
+++ b/Silicon/Sanchip/Library/Zx6718RealTimeClockLib/Zx296718RealTimeClock.c
@@ -0,0 +1,376 @@
+/** @file
+  Implement EFI RealTimeClock runtime services via RTC Lib.
+
+  Currently this driver does not support runtime virtual calling.
+
+  Copyright (C) 2017 Sanechips Technology Co., Ltd.
+  Copyright (c) 2017, Linaro Limited.
+
+  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.
+
+  Based on the files under 
ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.inf
+
+**/
+
+#include 
+#include 


P before U.


+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+// Use EfiAtRuntime to check stage
+#include 


L (UefiRuntimeLib) before S (UefiRuntimeServices...).
No need for a comment explaining why we include headers.


+#include 
+#include "Zx296718RealTimeClock.h"
+
+STATIC UINTN   RtcBase;


mRtcBase.


+STATIC BOOLEAN RTCInitialized = FALSE;


mRTCInitialized.


+
+BOOLEAN
+EFIAPI
+IsTimeValid(
+  IN EFI_TIME *Time
+  )
+{
+  // Check the input parameters are within the range specified by UEFI
+  if ((Time->Year  < 2000) ||
+ (Time->Year   > 2099) ||
+ (Time->Month  < 1   ) ||
+ (Time->Month  > 12  ) ||
+ (Time->Day< 1   ) ||
+ (Time->Day> 31  ) ||
+ (Time->Hour   > 23  ) ||
+ (Time->Minute > 59  ) ||
+ (Time->Second > 59  ) ||
+ (Time->Nanosecond > 9) ||
+ (!((Time->TimeZone == EFI_UNSPECIFIED_TIMEZONE) || ((Time->TimeZone >= -1440) 
&& (Time->TimeZone <= 1440 ||
+ (Time->Daylight & (~(EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT)))
+  ) {
+return FALSE;
+  }
+
+  return TRUE;
+}


This appears a direct copy of the version in
EmbeddedPkg/Library/TimeBaseLib. Can you add that TimeBaseLib library
dependency to decrease duplication?
(This may not have existed as a standalone library at the time you
started this port.)


+
+VOID


A lot of the functions in this file could do with a STATIC prefix,
since they are only used internally.


+Wait4Busy (


Semantically, this name is incorrect.
The function is waiting _while_ the state is RTC_BUSY, so seems to me
it should be called WaitBusy.


+  VOID
+  )
+{
+  UINT32 Val, Retry = 1000;
+  do {
+MicroSecondDelay (200);


Why 200?


It is just a suggested delay time according to RTC clock frequency. You 
want a RTC_WAIT_DELAY macro here for better understanding, right?



+Val = MmioRead32 (RtcBase + RTCSTS);


MmioRead32 does not imply any barrier semantics.
If this component will only ever be supported on ARM platforms, you
could insert an ArmDataMemoryBarrier (). Otherwise, MemoryFence ().


+  } while(Val & RTC_BUSY && Retry--);


Space before (.


+
+  if (!Retry)
+DEBUG((DEBUG_ERROR, "%a Rtc busy retry timeout\n", __func__));


Space after DEBUG.


+}
+
+VOID
+RTCWriteReg (
+  IN UINT32 Reg,
+  IN UINT32 Val
+  )
+{
+  Wait4Busy ();
+  MmioWrite32 (RtcBase + RTCCFGID, CONFIG_PARMETER);
+  Wait4Busy ();
+  MmioWrite32 (RtcBase + Reg, Val);
+}
+
+UINT32
+RTCReadReg (
+  IN UINT32 Reg
+  )
+{
+  Wait4Busy ();
+  return MmioRead32 (RtcBase + Reg);
+}
+
+VOID
+InitializeRTC (
+  VOID
+  )
+{
+  UINTN Val = (UINTN)FixedPcdGet64 (PcdZxRtcClockFreq);
+
+  RTCWriteReg (RTCCLKCNT, Val - 1);
+  Val = RTCReadReg (RTCPOWERINIT1);
+  if (RTC_POWER_INI1_PARA != Val) {
+RTCWriteReg (RTCCTL, 0);
+MicroSecondDelay (INIT_DELAY);
+Val = RTCReadReg (RTCCTL);
+Val |= RTC_CTRL_BIT6_1;
+RTCWriteReg (RTCCTL, Val);
+Val = RTCReadReg (RTCCTL);
+Val &= RTC_CTRL_MODULE24 | RTC_CTRL_STOP;
+RTCWriteReg (RTCCTL, Val);
+  }
+  Val = RTCReadReg (RTCINT);

Re: [edk2] [PATCH 2/4] Platforms: Add ZX RTC driver for Sanchip SoC

2017-08-10 Thread Leif Lindholm
On Wed, Aug 09, 2017 at 10:12:37PM +0800, Jun Nie wrote:
> Runtime service is not supported yet.
> 
> Contributed-under: TianoCore Contribution Agreement 1.0
> Signed-off-by: Jun Nie 
> ---
>  .../Zx6718RealTimeClockLib/Zx296718RealTimeClock.c | 376 
> +
>  .../Zx6718RealTimeClockLib/Zx296718RealTimeClock.h | 102 ++
>  .../Zx296718RealTimeClock.inf  |  42 +++
>  3 files changed, 520 insertions(+)
>  create mode 100644 
> Silicon/Sanchip/Library/Zx6718RealTimeClockLib/Zx296718RealTimeClock.c
>  create mode 100644 
> Silicon/Sanchip/Library/Zx6718RealTimeClockLib/Zx296718RealTimeClock.h
>  create mode 100644 
> Silicon/Sanchip/Library/Zx6718RealTimeClockLib/Zx296718RealTimeClock.inf
> 
> diff --git 
> a/Silicon/Sanchip/Library/Zx6718RealTimeClockLib/Zx296718RealTimeClock.c 
> b/Silicon/Sanchip/Library/Zx6718RealTimeClockLib/Zx296718RealTimeClock.c
> new file mode 100644
> index 000..af6e5bd
> --- /dev/null
> +++ b/Silicon/Sanchip/Library/Zx6718RealTimeClockLib/Zx296718RealTimeClock.c
> @@ -0,0 +1,376 @@
> +/** @file
> +  Implement EFI RealTimeClock runtime services via RTC Lib.
> +
> +  Currently this driver does not support runtime virtual calling.
> +
> +  Copyright (C) 2017 Sanechips Technology Co., Ltd.
> +  Copyright (c) 2017, Linaro Limited.
> +
> +  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.
> +
> +  Based on the files under 
> ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.inf
> +
> +**/
> +
> +#include 
> +#include 

P before U.

> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +// Use EfiAtRuntime to check stage
> +#include 

L (UefiRuntimeLib) before S (UefiRuntimeServices...).
No need for a comment explaining why we include headers.

> +#include 
> +#include "Zx296718RealTimeClock.h"
> +
> +STATIC UINTN   RtcBase;

mRtcBase.

> +STATIC BOOLEAN RTCInitialized = FALSE;

mRTCInitialized.

> +
> +BOOLEAN
> +EFIAPI
> +IsTimeValid(
> +  IN EFI_TIME *Time
> +  )
> +{
> +  // Check the input parameters are within the range specified by UEFI
> +  if ((Time->Year  < 2000) ||
> + (Time->Year   > 2099) ||
> + (Time->Month  < 1   ) ||
> + (Time->Month  > 12  ) ||
> + (Time->Day< 1   ) ||
> + (Time->Day> 31  ) ||
> + (Time->Hour   > 23  ) ||
> + (Time->Minute > 59  ) ||
> + (Time->Second > 59  ) ||
> + (Time->Nanosecond > 9) ||
> + (!((Time->TimeZone == EFI_UNSPECIFIED_TIMEZONE) || ((Time->TimeZone >= 
> -1440) && (Time->TimeZone <= 1440 ||
> + (Time->Daylight & (~(EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT)))
> +  ) {
> +return FALSE;
> +  }
> +
> +  return TRUE;
> +}

This appears a direct copy of the version in
EmbeddedPkg/Library/TimeBaseLib. Can you add that TimeBaseLib library
dependency to decrease duplication?
(This may not have existed as a standalone library at the time you
started this port.)

> +
> +VOID

A lot of the functions in this file could do with a STATIC prefix,
since they are only used internally.

> +Wait4Busy (

Semantically, this name is incorrect.
The function is waiting _while_ the state is RTC_BUSY, so seems to me
it should be called WaitBusy.

> +  VOID
> +  )
> +{
> +  UINT32 Val, Retry = 1000;
> +  do {
> +MicroSecondDelay (200);

Why 200?

> +Val = MmioRead32 (RtcBase + RTCSTS);

MmioRead32 does not imply any barrier semantics.
If this component will only ever be supported on ARM platforms, you
could insert an ArmDataMemoryBarrier (). Otherwise, MemoryFence ().

> +  } while(Val & RTC_BUSY && Retry--);

Space before (.

> +
> +  if (!Retry)
> +DEBUG((DEBUG_ERROR, "%a Rtc busy retry timeout\n", __func__));

Space after DEBUG.

> +}
> +
> +VOID
> +RTCWriteReg (
> +  IN UINT32 Reg,
> +  IN UINT32 Val
> +  )
> +{
> +  Wait4Busy ();
> +  MmioWrite32 (RtcBase + RTCCFGID, CONFIG_PARMETER);
> +  Wait4Busy ();
> +  MmioWrite32 (RtcBase + Reg, Val);
> +}
> +
> +UINT32
> +RTCReadReg (
> +  IN UINT32 Reg
> +  )
> +{
> +  Wait4Busy ();
> +  return MmioRead32 (RtcBase + Reg);
> +}
> +
> +VOID
> +InitializeRTC (
> +  VOID
> +  )
> +{
> +  UINTN Val = (UINTN)FixedPcdGet64 (PcdZxRtcClockFreq);
> +
> +  RTCWriteReg (RTCCLKCNT, Val - 1);
> +  Val = RTCReadReg (RTCPOWERINIT1);
> +  if (RTC_POWER_INI1_PARA != Val) {
> +RTCWriteReg (RTCCTL, 0);
> +MicroSecondDelay (INIT_DELAY);
> +Val = RTCReadReg (RTCCTL);
> +Val |= RTC_CTRL_BIT6_1;
> +RTCWriteReg (RTCCTL, Val);
> +Val = RTCReadReg (RTCCTL);
> +Val &= RTC_CTRL_MODULE24 |