public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Linus Liu" <linus.liu@intel.com>
To: devel@edk2.groups.io
Cc: Benny Lin <benny.lin@intel.com>, Gua Guo <gua.guo@intel.com>,
	Chasel Chiu <chasel.chiu@intel.com>,
	James Lu <james.lu@intel.com>,
	Dhaval Sharma <dhaval@rivosinc.com>
Subject: [edk2-devel] [PATCH v1 3/6] UefiPayloadPkg: Support Debug function when Hob was not avalible.
Date: Thu, 23 May 2024 19:05:52 -0700	[thread overview]
Message-ID: <20240524020552.847-1-linus.liu@intel.com> (raw)

Initialize mUartInfo with PCD for debug message
when Hob was not avalible.

Cc: Benny Lin <benny.lin@intel.com>
Cc: Gua Guo <gua.guo@intel.com>
Cc: Chasel Chiu <chasel.chiu@intel.com>
Cc: James Lu <james.lu@intel.com>
Cc: Dhaval Sharma <dhaval@rivosinc.com>

Signed-off-by: Linus Liu <linus.liu@intel.com>
---
 UefiPayloadPkg/Library/BaseSerialPortLibHob/BaseSerialPortLibHob.c             | 69 ++++++++++++++++++++
 UefiPayloadPkg/Library/DebugPrintErrorLevelLibHob/DebugPrintErrorLevelLibHob.c |  5 ++
 UefiPayloadPkg/Library/PayloadEntryHobLib/Hob.c                                |  7 +-
 UefiPayloadPkg/Library/PlatformHookLib/PlatformHookLib.c                       |  4 ++
 UefiPayloadPkg/Library/BaseSerialPortLibHob/BaseSerialPortLibHob.inf           | 10 +++
 UefiPayloadPkg/Library/BaseSerialPortLibHob/DxeBaseSerialPortLibHob.inf        | 13 ++--
 UefiPayloadPkg/Library/PayloadEntryHobLib/HobLib.inf                           |  1 +
 7 files changed, 104 insertions(+), 5 deletions(-)

diff --git a/UefiPayloadPkg/Library/BaseSerialPortLibHob/BaseSerialPortLibHob.c b/UefiPayloadPkg/Library/BaseSerialPortLibHob/BaseSerialPortLibHob.c
index 82d0dd585508..55e85dce8598 100644
--- a/UefiPayloadPkg/Library/BaseSerialPortLibHob/BaseSerialPortLibHob.c
+++ b/UefiPayloadPkg/Library/BaseSerialPortLibHob/BaseSerialPortLibHob.c
@@ -143,6 +143,75 @@ SerialPortInitialize (
     return RETURN_SUCCESS;
   }
 
+  if (GetHobList () == NULL) {
+    mUartCount         = 0;
+    SerialRegisterBase = PcdGet64 (PcdSerialRegisterBase);
+    MmioEnable         = PcdGetBool (PcdSerialUseMmio);
+    BaudRate           = PcdGet32 (PcdSerialBaudRate);
+    RegisterStride     = (UINT8)PcdGet32 (PcdSerialRegisterStride);
+
+    mUartInfo[mUartCount].BaseAddress    = SerialRegisterBase;
+    mUartInfo[mUartCount].UseMmio        = MmioEnable;
+    mUartInfo[mUartCount].BaudRate       = BaudRate;
+    mUartInfo[mUartCount].RegisterStride = RegisterStride;
+    mUartCount++;
+
+    Divisor = PcdGet32 (PcdSerialClockRate) / (BaudRate * 16);
+    if ((PcdGet32 (PcdSerialClockRate) % (BaudRate * 16)) >= BaudRate * 8) {
+      Divisor++;
+    }
+
+    //
+    // See if the serial port is already initialized
+    //
+    Initialized = TRUE;
+    if ((SerialPortReadRegister (SerialRegisterBase, R_UART_LCR, MmioEnable, RegisterStride) & 0x3F) != (PcdGet8 (PcdSerialLineControl) & 0x3F)) {
+      Initialized = FALSE;
+    }
+
+    Value = (UINT8)(SerialPortReadRegister (SerialRegisterBase, R_UART_LCR, MmioEnable, RegisterStride) | B_UART_LCR_DLAB);
+    SerialPortWriteRegister (SerialRegisterBase, R_UART_LCR, Value, MmioEnable, RegisterStride);
+    CurrentDivisor  =  SerialPortReadRegister (SerialRegisterBase, R_UART_BAUD_HIGH, MmioEnable, RegisterStride) << 8;
+    CurrentDivisor |= (UINT32)SerialPortReadRegister (SerialRegisterBase, R_UART_BAUD_LOW, MmioEnable, RegisterStride);
+    Value           = (UINT8)(SerialPortReadRegister (SerialRegisterBase, R_UART_LCR, MmioEnable, RegisterStride) & ~B_UART_LCR_DLAB);
+    SerialPortWriteRegister (SerialRegisterBase, R_UART_LCR, Value, MmioEnable, RegisterStride);
+    if (CurrentDivisor != Divisor) {
+      Initialized = FALSE;
+    }
+
+    //
+    // Configure baud rate
+    //
+    SerialPortWriteRegister (SerialRegisterBase, R_UART_LCR, B_UART_LCR_DLAB, MmioEnable, RegisterStride);
+    SerialPortWriteRegister (SerialRegisterBase, R_UART_BAUD_HIGH, (UINT8)(Divisor >> 8), MmioEnable, RegisterStride);
+    SerialPortWriteRegister (SerialRegisterBase, R_UART_BAUD_LOW, (UINT8)(Divisor & 0xff), MmioEnable, RegisterStride);
+
+    //
+    // Clear DLAB and configure Data Bits, Parity, and Stop Bits.
+    // Strip reserved bits from PcdSerialLineControl
+    //
+    SerialPortWriteRegister (SerialRegisterBase, R_UART_LCR, (UINT8)(PcdGet8 (PcdSerialLineControl) & 0x3F), MmioEnable, RegisterStride);
+
+    //
+    // Enable and reset FIFOs
+    // Strip reserved bits from PcdSerialFifoControl
+    //
+    SerialPortWriteRegister (SerialRegisterBase, R_UART_FCR, 0x00, MmioEnable, RegisterStride);
+    SerialPortWriteRegister (SerialRegisterBase, R_UART_FCR, (UINT8)(PcdGet8 (PcdSerialFifoControl) & (B_UART_FCR_FIFOE | B_UART_FCR_FIFO64)), MmioEnable, RegisterStride);
+
+    //
+    // Set FIFO Polled Mode by clearing IER after setting FCR
+    //
+    SerialPortWriteRegister (SerialRegisterBase, R_UART_IER, 0x00, MmioEnable, RegisterStride);
+
+    //
+    // Put Modem Control Register(MCR) into its reset state of 0x00.
+    //
+    SerialPortWriteRegister (SerialRegisterBase, R_UART_MCR, 0x00, MmioEnable, RegisterStride);
+
+    return RETURN_SUCCESS;
+  }
+
   GuidHob = GetFirstGuidHob (&gUniversalPayloadSerialPortInfoGuid);
   while (GuidHob != NULL) {
     SerialPortInfo     = (UNIVERSAL_PAYLOAD_SERIAL_PORT_INFO *)GET_GUID_HOB_DATA (GuidHob);
diff --git a/UefiPayloadPkg/Library/DebugPrintErrorLevelLibHob/DebugPrintErrorLevelLibHob.c b/UefiPayloadPkg/Library/DebugPrintErrorLevelLibHob/DebugPrintErrorLevelLibHob.c
index 10bdbe2bbc1f..e9cce86bb853 100644
--- a/UefiPayloadPkg/Library/DebugPrintErrorLevelLibHob/DebugPrintErrorLevelLibHob.c
+++ b/UefiPayloadPkg/Library/DebugPrintErrorLevelLibHob/DebugPrintErrorLevelLibHob.c
@@ -13,6 +13,7 @@
 #include <Library/PcdLib.h>
 #include <Library/HobLib.h>
 #include <Guid/DebugPrintErrorLevel.h>
+#include <Guid/UniversalPayloadBase.h>
 #include <Library/DebugPrintErrorLevelLib.h>
 #include <UniversalPayload/UniversalPayload.h>
 
@@ -35,6 +36,10 @@ GetDebugPrintErrorLevel (
   UNIVERSAL_PAYLOAD_GENERIC_HEADER      *GenericHeader;
   UEFI_PAYLOAD_DEBUG_PRINT_ERROR_LEVEL  *DebugPrintErrorLevel;
 
+  if (GetHobList () == NULL) {
+    return PcdGet32 (PcdDebugPrintErrorLevel);
+  }
+
   if (!gDebugPrintErrorLevelInitialized) {
     gDebugPrintErrorLevelInitialized = TRUE;
     gDebugPrintErrorLevel            = PcdGet32 (PcdDebugPrintErrorLevel);
diff --git a/UefiPayloadPkg/Library/PayloadEntryHobLib/Hob.c b/UefiPayloadPkg/Library/PayloadEntryHobLib/Hob.c
index 51c2e28d7ddd..05cd0ea997f1 100644
--- a/UefiPayloadPkg/Library/PayloadEntryHobLib/Hob.c
+++ b/UefiPayloadPkg/Library/PayloadEntryHobLib/Hob.c
@@ -31,7 +31,6 @@ GetHobList (
   VOID
   )
 {
-  ASSERT (mHobList != NULL);
   return mHobList;
 }
 
@@ -109,6 +108,7 @@ CreateHob (
   VOID                        *Hob;
 
   HandOffHob = GetHobList ();
+  ASSERT (HandOffHob != NULL);
 
   //
   // Check Length to avoid data overflow.
@@ -175,6 +175,7 @@ BuildResourceDescriptorHob (
   Hob->ResourceAttribute = ResourceAttribute;
   Hob->PhysicalStart     = PhysicalStart;
   Hob->ResourceLength    = NumberOfBytes;
+  ZeroMem (&(Hob->Owner), sizeof (EFI_GUID));
 }
 
 /**
@@ -239,6 +240,7 @@ GetFirstHob (
   VOID  *HobList;
 
   HobList = GetHobList ();
+  ASSERT (HobList != NULL);
   return GetNextHob (Type, HobList);
 }
 
@@ -305,6 +307,7 @@ GetFirstGuidHob (
   VOID  *HobList;
 
   HobList = GetHobList ();
+  ASSERT (HobList != NULL);
   return GetNextGuidHob (Guid, HobList);
 }
 
@@ -651,6 +654,7 @@ UpdateStackHob (
   EFI_PEI_HOB_POINTERS  Hob;
 
   Hob.Raw = GetHobList ();
+  ASSERT (Hob.Raw != NULL);
   while ((Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw)) != NULL) {
     if (CompareGuid (&gEfiHobMemoryAllocStackGuid, &(Hob.MemoryAllocationStack->AllocDescriptor.Name))) {
       //
@@ -709,6 +713,7 @@ BuildMemoryAllocationHob (
   }
 
   ZeroMem (&(Hob->AllocDescriptor.Name), sizeof (EFI_GUID));
+
   Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;
   Hob->AllocDescriptor.MemoryLength      = Length;
   Hob->AllocDescriptor.MemoryType        = MemoryType;
diff --git a/UefiPayloadPkg/Library/PlatformHookLib/PlatformHookLib.c b/UefiPayloadPkg/Library/PlatformHookLib/PlatformHookLib.c
index 60a17b8fc256..efaab326bb68 100644
--- a/UefiPayloadPkg/Library/PlatformHookLib/PlatformHookLib.c
+++ b/UefiPayloadPkg/Library/PlatformHookLib/PlatformHookLib.c
@@ -51,6 +51,10 @@ PlatformHookSerialPortInitialize (
   UINT8                               *GuidHob;
   UNIVERSAL_PAYLOAD_GENERIC_HEADER    *GenericHeader;
 
+  if (GetHobList () == NULL) {
+    return RETURN_SUCCESS;
+  }
+
   GuidHob = GetFirstGuidHob (&gUniversalPayloadSerialPortInfoGuid);
   if (GuidHob == NULL) {
     return EFI_NOT_FOUND;
diff --git a/UefiPayloadPkg/Library/BaseSerialPortLibHob/BaseSerialPortLibHob.inf b/UefiPayloadPkg/Library/BaseSerialPortLibHob/BaseSerialPortLibHob.inf
index ac857d3eea7c..76e431c2f102 100644
--- a/UefiPayloadPkg/Library/BaseSerialPortLibHob/BaseSerialPortLibHob.inf
+++ b/UefiPayloadPkg/Library/BaseSerialPortLibHob/BaseSerialPortLibHob.inf
@@ -18,6 +18,7 @@
 [Packages]
   MdePkg/MdePkg.dec
   MdeModulePkg/MdeModulePkg.dec
+  UefiPayloadPkg/UefiPayloadPkg.dec
 
 [LibraryClasses]
   PcdLib
@@ -34,6 +35,15 @@
   gEfiMdeModulePkgTokenSpaceGuid.PcdSerialClockRate
   gEfiMdeModulePkgTokenSpaceGuid.PcdSerialExtendedTxFifoSize
   gEfiMdeModulePkgTokenSpaceGuid.PcdSerialUseHardwareFlowControl
+  gEfiMdeModulePkgTokenSpaceGuid.PcdSerialUseMmio                 ## CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdSerialDetectCable             ## SOMETIMES_CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdSerialRegisterBase            ## CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdSerialBaudRate                ## CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdSerialPciDeviceInfo           ## CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdSerialRegisterStride          ## CONSUMES
+  gUefiPayloadPkgTokenSpaceGuid.PcdHandOffFdtEnable
+
+
 
 [Guids]
   gUniversalPayloadSerialPortInfoGuid
diff --git a/UefiPayloadPkg/Library/BaseSerialPortLibHob/DxeBaseSerialPortLibHob.inf b/UefiPayloadPkg/Library/BaseSerialPortLibHob/DxeBaseSerialPortLibHob.inf
index 7bb3a6ae96dd..552ae6c4f786 100644
--- a/UefiPayloadPkg/Library/BaseSerialPortLibHob/DxeBaseSerialPortLibHob.inf
+++ b/UefiPayloadPkg/Library/BaseSerialPortLibHob/DxeBaseSerialPortLibHob.inf
@@ -6,7 +6,6 @@
 #  SPDX-License-Identifier: BSD-2-Clause-Patent
 #
 ##
-
 [Defines]
   INF_VERSION                    = 0x00010005
   BASE_NAME                      = DxeBaseSerialPortLibHob
@@ -15,27 +14,33 @@
   VERSION_STRING                 = 1.0
   LIBRARY_CLASS                  = SerialPortLib|DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER DXE_SMM_DRIVER UEFI_APPLICATION UEFI_DRIVER
   CONSTRUCTOR                    = DxeBaseSerialPortLibHobConstructor
-
 [Packages]
   MdePkg/MdePkg.dec
   MdeModulePkg/MdeModulePkg.dec
+  UefiPayloadPkg/UefiPayloadPkg.dec
 
 [LibraryClasses]
   PcdLib
   IoLib
   HobLib
   TimerLib
-
+  PlatformHookLib
 [Sources]
   DxeBaseSerialPortLibHob.c
   BaseSerialPortLibHob.c
-
 [Pcd]
   gEfiMdeModulePkgTokenSpaceGuid.PcdSerialLineControl
   gEfiMdeModulePkgTokenSpaceGuid.PcdSerialFifoControl
   gEfiMdeModulePkgTokenSpaceGuid.PcdSerialClockRate
   gEfiMdeModulePkgTokenSpaceGuid.PcdSerialExtendedTxFifoSize
   gEfiMdeModulePkgTokenSpaceGuid.PcdSerialUseHardwareFlowControl
+  gEfiMdeModulePkgTokenSpaceGuid.PcdSerialUseMmio                 ## CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdSerialDetectCable             ## SOMETIMES_CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdSerialRegisterBase            ## CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdSerialBaudRate                ## CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdSerialPciDeviceInfo           ## CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdSerialRegisterStride          ## CONSUMES
+  gUefiPayloadPkgTokenSpaceGuid.PcdHandOffFdtEnable
 
 [Guids]
   gUniversalPayloadSerialPortInfoGuid
diff --git a/UefiPayloadPkg/Library/PayloadEntryHobLib/HobLib.inf b/UefiPayloadPkg/Library/PayloadEntryHobLib/HobLib.inf
index cbb4f02efc15..6a19cfb5c214 100644
--- a/UefiPayloadPkg/Library/PayloadEntryHobLib/HobLib.inf
+++ b/UefiPayloadPkg/Library/PayloadEntryHobLib/HobLib.inf
@@ -36,4 +36,5 @@
 [Guids]
   gEfiHobMemoryAllocModuleGuid
   gEfiHobMemoryAllocStackGuid
+  gUniversalPayloadDeviceTreeGuid
 
-- 
2.39.2.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#119174): https://edk2.groups.io/g/devel/message/119174
Mute This Topic: https://groups.io/mt/106274769/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



                 reply	other threads:[~2024-05-24  2:06 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240524020552.847-1-linus.liu@intel.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox