From mboxrd@z Thu Jan 1 00:00:00 1970 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 Authentication-Results: mx.groups.io; dkim=pass header.i=@hpe.com header.s=pps0720 header.b=TW5vdlY9; spf=temperror, err=temporary DNS error (domain: hpe.com, ip: 148.163.147.86, mailfrom: prvs=0926d272d9=abner.chang@hpe.com) 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hpe.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pps0720; bh=hCCCu3v+QQOnnclwXuQaEa8C370mYlkqBI4KAoRe2e8=; b=TW5vdlY97Q9m5O1xJFeDdCNLeEP8WelehK6YJr69imcT8RyckvCsZBLtLjCc8Ln+GIam Po3iaP59GpNicfelc2HTSJQI4J37inxR2nvTJZ7r3zpjTjLARsQLhUESa4gki4EgmmlM f3Yqc1eGHIyCteo7jBf6Ngd/sJ+CVBAgq8XWgAGPRLdJCW5lntVlRM+CUD1FLcN4fvKm q7QaLhzsv9OnsC7ziaGREsPBUwhsBLk2tH79sbVwkuq+ngdwuHAiAlWBYaghQTT5wknd FXByVzJzP8z7/CiwBfKHUY1PJdkIrH5AJquDBnTgnGJkv1xl/U1HEHjx/5Ennj5WkR1B 2g== 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 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) 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-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> X-Mailer: git-send-email 2.31.1 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 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.182.1,Aquarius:18.0.790,Hydra:6.0.425,FMLib:17.0.607.475 definitions=2021-10-18_07,2021-10-18_01,2020-04-07_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 adultscore=0 spamscore=0 suspectscore=0 bulkscore=0 phishscore=0 clxscore=1015 mlxlogscore=999 malwarescore=0 priorityscore=1501 lowpriorityscore=0 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2109230001 definitions=main-2110190056 Content-Transfer-Encoding: quoted-printable 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_=0D #define SIFIVE_U5_SERIES_UART_H_=0D =0D +#include =0D #include =0D =0D #endif=0D 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 @@ **/=0D =0D #include =0D +#include =0D #include =0D #include =0D #include =0D @@ -41,7 +42,7 @@ #define UART_BAUDRATE 115200=0D #define SYS_CLK FixedPcdGet32(PcdU5PlatformSystemClock)=0D =0D -BOOLEAN Initiated =3D FALSE;=0D +BOOLEAN Initiated =3D TRUE;=0D =0D /**=0D Get value from serial port register.=0D @@ -55,7 +56,9 @@ UINT32 GetReg ( IN UINT32 RegIndex=0D )=0D {=0D - return MmioRead32 (FixedPcdGet32(PcdU5UartBase) + (RegIndex * 0x4));=0D + STATIC volatile UINT32 * const uart =3D (UINT32 *)FixedPcdGet32(PcdU5Uar= tBase);=0D +=0D + return readl ((volatile void *)(uart + RegIndex));=0D }=0D =0D /**=0D @@ -70,7 +73,9 @@ VOID SetReg ( IN UINT32 Value=0D )=0D {=0D - MmioWrite32 (Value, FixedPcdGet32(PcdU5UartBase) + (RegIndex * 0x4));=0D + STATIC volatile UINT32 * const uart =3D (UINT32 *)FixedPcdGet32(PcdU5Uar= tBase);=0D +=0D + writel (Value, (volatile void *)(uart + RegIndex));=0D }=0D =0D /**=0D @@ -104,7 +109,36 @@ UINT32 SifiveUartGetChar (VOID) }=0D return -1;=0D }=0D +/**=0D + Find minimum divisor divides in_freq to max_target_hz;=0D + Based on uart driver n SiFive FSBL.=0D +=0D + f_baud =3D f_in / (div + 1) =3D> div =3D (f_in / f_baud) - 1=0D + The nearest integer solution requires rounding up as to not exceed max_t= arget_hz.=0D + div =3D ceil(f_in / f_baud) - 1=0D + =3D floor((f_in - 1 + f_baud) / f_baud) - 1=0D + This should not overflow as long as (f_in - 1 + f_baud) does not exceed= =0D + 2^32 - 1, which is unlikely since we represent frequencies in kHz.=0D +=0D + @param Freq The given clock to UART.=0D + @param MaxTargetHZ Target baudrate.=0D =0D +**/=0D +UINT32=0D +UartMinClkDivisor (=0D + IN UINT64 Freq,=0D + IN UINT64 MaxTargetHZ=0D + )=0D +{=0D + UINT64 Quotient;=0D +=0D + Quotient =3D (Freq + MaxTargetHZ - 1) / (MaxTargetHZ);=0D + if (Quotient =3D=3D 0) {=0D + return 0;=0D + } else {=0D + return Quotient - 1;=0D + }=0D +}=0D /**=0D Initialize the serial device hardware.=0D =0D @@ -116,20 +150,24 @@ UINT32 SifiveUartGetChar (VOID) @retval RETURN_DEVICE_ERROR The serail device could not be initialized= .=0D =0D **/=0D -RETURN_STATUS=0D +EFI_STATUS=0D EFIAPI=0D SerialPortInitialize (=0D VOID=0D )=0D {=0D - if (Initiated) {=0D - return RETURN_SUCCESS;=0D + UINT32 Divisor;=0D + UINT32 CurrentDivisor;=0D +=0D + Divisor =3D UartMinClkDivisor (SYS_CLK / 2, UART_BAUDRATE);=0D + if (Divisor =3D=3D 0) {=0D + return EFI_INVALID_PARAMETER;=0D }=0D - if (sifive_uart_init (FixedPcdGet32(PcdU5UartBase), SYS_CLK / 2, UART_BA= UDRATE) !=3D 0) {=0D - return EFI_DEVICE_ERROR;=0D + CurrentDivisor =3D GetReg(UART_REG_DIV);=0D + if (Divisor !=3D CurrentDivisor) {=0D + sifive_uart_init (FixedPcdGet32(PcdU5UartBase), SYS_CLK / 2, UART_BAUD= RATE);=0D }=0D - Initiated =3D TRUE;=0D - return RETURN_SUCCESS;=0D + return EFI_SUCCESS;=0D }=0D =0D /**=0D --=20 2.31.1