From nobody Mon Feb 9 09:22:49 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+82337+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+82337+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=hpe.com ARC-Seal: i=1; a=rsa-sha256; t=1634634532; cv=none; d=zohomail.com; s=zohoarc; b=PfwBve5UhTxjCieei9C6WgePWxB28j/IaPljjAn/GPr6NwXCG5DCi7eOmdAFHEX4PpURKNjgEXZKoDJo1IKGn9IWRAeTjmM61PkccHaXe+RzkFrLeOADFUBIU2ievxtI18OZj6EEGmQNIN4qtX35m33/oFkjQ+bLHJja7zdMzWQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1634634532; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=KPU2Cx2DF9/rfbRLIlfzppNXAv/TQu/lKZsFSENjK1E=; b=CpUs3n+2aGnkbiYkU+3u+iChNGYEu2xeSPr+FRaqxgGJ+E5GriQnsToOYbDUUBbneUWkWdxZieXAFvmusoSeirOe4EZ/isvi/z1w5gYcJD8ksrzO/v4roxo+gkKzoBengsydayqDw238F4kxCrGiIGaLDyMY0gR39fV0GKC4qzo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+82337+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1634634532702515.1458682185058; Tue, 19 Oct 2021 02:08:52 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id AA46YY1788612xJTuWt1rEjH; Tue, 19 Oct 2021 02:08:52 -0700 X-Received: from mx0a-002e3701.pphosted.com (mx0a-002e3701.pphosted.com [148.163.147.86]) by mx.groups.io with SMTP id smtpd.web11.7157.1634634529572297913 for ; Tue, 19 Oct 2021 02:08:51 -0700 X-Received: from pps.filterd (m0134421.ppops.net [127.0.0.1]) by mx0b-002e3701.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 19J81sZG029192; Tue, 19 Oct 2021 09:08:49 GMT X-Received: from g4t3426.houston.hpe.com (g4t3426.houston.hpe.com [15.241.140.75]) by mx0b-002e3701.pphosted.com with ESMTP id 3bsd6x5gv8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 19 Oct 2021 09:08:49 +0000 X-Received: from g9t2301.houston.hpecorp.net (g9t2301.houston.hpecorp.net [16.220.97.129]) by g4t3426.houston.hpe.com (Postfix) with ESMTP id A597B4E; Tue, 19 Oct 2021 09:08:48 +0000 (UTC) X-Received: from UB16Abner.asiapacific.hpqcorp.net (ub16abner.asiapacific.hpqcorp.net [15.119.209.229]) by g9t2301.houston.hpecorp.net (Postfix) with ESMTP id 979404C; Tue, 19 Oct 2021 09:08:47 +0000 (UTC) From: "Abner Chang" To: devel@edk2.groups.io Cc: abner.chang@hpe.com, Sunil V L , Daniel Schaefer Subject: [edk2-devel] [edk2-platforms][PATCH 26/30] SiFive/SerialPortLib: Remove global variable Date: Tue, 19 Oct 2021 16:10:03 +0800 Message-Id: <20211019081007.31165-27-abner.chang@hpe.com> In-Reply-To: <20211019081007.31165-1-abner.chang@hpe.com> References: <20211019081007.31165-1-abner.chang@hpe.com> MIME-Version: 1.0 X-Proofpoint-GUID: Feh_XBAT73RNdk51gsm1xtFViXRmAJSE X-Proofpoint-ORIG-GUID: Feh_XBAT73RNdk51gsm1xtFViXRmAJSE X-HPE-SCL: -1 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,abner.chang@hpe.com X-Gm-Message-State: zh8Jj8nZ61waghyxlSfvn0wmx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1634634532; bh=owEHPeZBxxGZvpWT9k0GuRiePTXFGVi27AQMHB+IW3M=; h=Cc:Date:From:Reply-To:Subject:To; b=prEa1/+3tzG6QGh9TljxzRkbIq46wdVYOEZHp0u0or0IMQE1PIJBPAP366/RIqvnRsv bJbq+eOYUnFOI19TTvNE5VvJ6nEBX984HdVd0kM/LTddFmbrlef68AfNFDGhCdmXXDSrI LfshsD/Mjo5db4fhhEAxmhKOz+s7QOCiUfE= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1634634533959100001 Content-Type: text/plain; charset="utf-8" Remove global variable from SerialPortLib because this module is not necessarily executed in memory. Cc: Sunil V L Cc: Daniel Schaefer Signed-off-by: Abner Chang --- .../SiFive/U5SeriesPkg/Include/SifiveU5Uart.h | 1 + .../Library/SerialIoLib/SerialPortLib.c | 58 +++++++++++++++---- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/Platform/SiFive/U5SeriesPkg/Include/SifiveU5Uart.h b/Platform/= SiFive/U5SeriesPkg/Include/SifiveU5Uart.h index 0ccb98e874..be8dce8c96 100644 --- a/Platform/SiFive/U5SeriesPkg/Include/SifiveU5Uart.h +++ b/Platform/SiFive/U5SeriesPkg/Include/SifiveU5Uart.h @@ -10,6 +10,7 @@ #ifndef SIFIVE_U5_SERIES_UART_H_ #define SIFIVE_U5_SERIES_UART_H_ =20 +#include #include =20 #endif diff --git a/Platform/SiFive/U5SeriesPkg/Library/SerialIoLib/SerialPortLib.= c b/Platform/SiFive/U5SeriesPkg/Library/SerialIoLib/SerialPortLib.c index 7bc73a0b82..42e5aa7b76 100644 --- a/Platform/SiFive/U5SeriesPkg/Library/SerialIoLib/SerialPortLib.c +++ b/Platform/SiFive/U5SeriesPkg/Library/SerialIoLib/SerialPortLib.c @@ -8,6 +8,7 @@ **/ =20 #include +#include #include #include #include @@ -41,7 +42,7 @@ #define UART_BAUDRATE 115200 #define SYS_CLK FixedPcdGet32(PcdU5PlatformSystemClock) =20 -BOOLEAN Initiated =3D FALSE; +BOOLEAN Initiated =3D TRUE; =20 /** Get value from serial port register. @@ -55,7 +56,9 @@ UINT32 GetReg ( IN UINT32 RegIndex ) { - return MmioRead32 (FixedPcdGet32(PcdU5UartBase) + (RegIndex * 0x4)); + STATIC volatile UINT32 * const uart =3D (UINT32 *)FixedPcdGet32(PcdU5Uar= tBase); + + return readl ((volatile void *)(uart + RegIndex)); } =20 /** @@ -70,7 +73,9 @@ VOID SetReg ( IN UINT32 Value ) { - MmioWrite32 (Value, FixedPcdGet32(PcdU5UartBase) + (RegIndex * 0x4)); + STATIC volatile UINT32 * const uart =3D (UINT32 *)FixedPcdGet32(PcdU5Uar= tBase); + + writel (Value, (volatile void *)(uart + RegIndex)); } =20 /** @@ -104,7 +109,36 @@ UINT32 SifiveUartGetChar (VOID) } return -1; } +/** + Find minimum divisor divides in_freq to max_target_hz; + Based on uart driver n SiFive FSBL. + + f_baud =3D f_in / (div + 1) =3D> div =3D (f_in / f_baud) - 1 + The nearest integer solution requires rounding up as to not exceed max_t= arget_hz. + div =3D ceil(f_in / f_baud) - 1 + =3D floor((f_in - 1 + f_baud) / f_baud) - 1 + This should not overflow as long as (f_in - 1 + f_baud) does not exceed + 2^32 - 1, which is unlikely since we represent frequencies in kHz. + + @param Freq The given clock to UART. + @param MaxTargetHZ Target baudrate. =20 +**/ +UINT32 +UartMinClkDivisor ( + IN UINT64 Freq, + IN UINT64 MaxTargetHZ + ) +{ + UINT64 Quotient; + + Quotient =3D (Freq + MaxTargetHZ - 1) / (MaxTargetHZ); + if (Quotient =3D=3D 0) { + return 0; + } else { + return Quotient - 1; + } +} /** Initialize the serial device hardware. =20 @@ -116,20 +150,24 @@ UINT32 SifiveUartGetChar (VOID) @retval RETURN_DEVICE_ERROR The serail device could not be initialized. =20 **/ -RETURN_STATUS +EFI_STATUS EFIAPI SerialPortInitialize ( VOID ) { - if (Initiated) { - return RETURN_SUCCESS; + UINT32 Divisor; + UINT32 CurrentDivisor; + + Divisor =3D UartMinClkDivisor (SYS_CLK / 2, UART_BAUDRATE); + if (Divisor =3D=3D 0) { + return EFI_INVALID_PARAMETER; } - if (sifive_uart_init (FixedPcdGet32(PcdU5UartBase), SYS_CLK / 2, UART_BA= UDRATE) !=3D 0) { - return EFI_DEVICE_ERROR; + CurrentDivisor =3D GetReg(UART_REG_DIV); + if (Divisor !=3D CurrentDivisor) { + sifive_uart_init (FixedPcdGet32(PcdU5UartBase), SYS_CLK / 2, UART_BAUD= RATE); } - Initiated =3D TRUE; - return RETURN_SUCCESS; + return EFI_SUCCESS; } =20 /** --=20 2.31.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#82337): https://edk2.groups.io/g/devel/message/82337 Mute This Topic: https://groups.io/mt/86435691/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-