From nobody Mon Sep 16 19:40:02 2024 Delivered-To: importer@patchew.org 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+114505+1787277+3901457@groups.io; arc=fail (BodyHash is different from the expected one) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1706248951383343.46324782333215; Thu, 25 Jan 2024 22:02:31 -0800 (PST) Return-Path: DKIM-Signature: a=rsa-sha256; bh=tQ1SGxMXM+awL/6t3dGDDP1MJ4hAeIGr0eWHizyskkI=; c=relaxed/simple; d=groups.io; h=ARC-Seal:ARC-Message-Signature:ARC-Authentication-Results:Received-SPF:From:To:CC:Subject:Date:Message-ID:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding:Content-Type; s=20140610; t=1706248951; v=1; b=DO0ypATKz5+bh7oAJVPlQWaF89i4vA6Zn/cib0Zopq7I98nVZTro33uCrlSB5wEy2QEmcrzb wMoP371mx2JgQuaHC2imMo0VMcLhfbEF/mH58i5WoKGX/ElLWK8tuBChyS1Jv2i5j5tmA/7a7sr SshbkJ2jx5exYLet6oqoCXe4= X-Received: by 127.0.0.2 with SMTP id Djk3YY1788612xwvmfyIYhFw; Thu, 25 Jan 2024 22:02:31 -0800 X-Received: from NAM10-BN7-obe.outbound.protection.outlook.com (NAM10-BN7-obe.outbound.protection.outlook.com [40.107.92.53]) by mx.groups.io with SMTP id smtpd.web11.9685.1706248950087299286 for ; Thu, 25 Jan 2024 22:02:30 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=GeE3aLLwZFMONL5cJC9yZ8GE9/7q1ZeRJlEq16ubnCK11p6g/gKwwbWAyRJkZjeO639jC9wnLLGMJEz/PlkvGRRm32AjnFiwDoe4vnT6ysfbw8ZnRR/a5e5rqPgantDFR15LY7lGJukgCp8pAJc1W7fxON3BicYaJno2O7x2vEbao5KvkfDuhYSrQ5HFa0G5GcEiRITARs7J9spFOVy6M8H4og9HjbPpRiOjhYMBbaJASl/PKcZxUBJslq4385Xuz+olm6WX+5+M+FLztap4iNpIuYSaM2FmH59zHnRkIMDaOyLVEA/2lmaaYdZJv35Rtn/a1+Z1y5PdXw3pwph37Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=shSKek/FRssEOuQZr8bhOk5MysyXKPwVoiZ35Lom8+s=; b=NYbECi8pJQIyr10a5nJQ6GUm34WOHvyeZxBhP+cEvYIgR5scQ/499XZg9FTMTdopgSCi3xVCHpbvRUIVCoGlgXEpibfKnZcBcJxi82ueQUMB9cKH6ZlElYJYDwQtBRMh4FPsiQpLZNCt0TfA5VzoFPRX23QT9/Omi9XrH5RJmZ/C8zRLZXLuWTOHMAvuz20VuR+32dg6N8xjduy8M5ZcoTUSGpz1Pdktu97w/onBR09HUwQzGoEZZQr6syyYQOyJBmR30q+6iqz0+WtGKkNrwy9H97Ifrhs9QrjbexB68A6X5ERmLWLzqEwF5KSJpdN2qbfRkBgG4UVdlu8Yruv7BA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=edk2.groups.io smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) X-Received: from CH0P223CA0013.NAMP223.PROD.OUTLOOK.COM (2603:10b6:610:116::30) by SJ2PR12MB9163.namprd12.prod.outlook.com (2603:10b6:a03:559::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7181.23; Fri, 26 Jan 2024 06:02:24 +0000 X-Received: from DS2PEPF0000343A.namprd02.prod.outlook.com (2603:10b6:610:116:cafe::2d) by CH0P223CA0013.outlook.office365.com (2603:10b6:610:116::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.27 via Frontend Transport; Fri, 26 Jan 2024 06:02:24 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; 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+114505+1787277+3901457@groups.io; helo=mail02.groups.io; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C X-Received: from SATLEXMB04.amd.com (165.204.84.17) by DS2PEPF0000343A.mail.protection.outlook.com (10.167.18.37) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 06:02:24 +0000 X-Received: from SHA-LX-MINGXZHA.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Fri, 26 Jan 2024 00:02:22 -0600 From: "Zhai, MingXin (Duke) via groups.io" To: CC: Eric Xing , Ken Yao , Igniculus Fu , Abner Chang Subject: [edk2-devel] [PATCH V2 12/32] AMD/VanGoghBoard: Check in AMD BaseSerialPortLib Date: Fri, 26 Jan 2024 14:00:30 +0800 Message-ID: <20240126060050.1725-13-duke.zhai@amd.com> In-Reply-To: <20240126060050.1725-1-duke.zhai@amd.com> References: <20240126060050.1725-1-duke.zhai@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS2PEPF0000343A:EE_|SJ2PR12MB9163:EE_ X-MS-Office365-Filtering-Correlation-Id: d26097c9-d8a6-4208-4856-08dc1e345e27 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Message-Info: 22HFKYqZKY8qySaT8dxFKvr/Mff+4VcbvJkNc+zQU1fGRjJre95l79bfjhSi9NaG+uQ0zK0pgTdP4lOcHkO801Etbr6ZqQEILNut+hcWpSKzejJccpHe7sAB328XQ4uEbPpm8rUr8ENJf53V30KgPJcuzO1KIYvTapEk1SlVNuLHpaBDdbF6PQqIvsSbH2S5kCA34iWwaNl3KSCJqqr/bn7EWGnXO+m5mQOMhVkANLUcFGNxGXvnKbfTQPpr1UME8mVqGIxI/7JydZRnABEec+zt7UIAuRk0GH6/SYUl4Kij/J9ZhJdf+Vsn4EL88Y4WagNFhmRq7zlf79d3u3kqATBiw9jQtmFdwsJlm1rjngN4QVK7NRXKo/HK7U/4h8UpzhNwlpgLBEw7tFWZq1arcPa22Z8TNLSvLZNv/yHbVofkXb6yOl0Jj22XlYKvle2VF+KcyzKwmFpurkpIlehjkpCL0UgqQxG4+4DnvpFEs8LApWCGGDLIh0V/yTonkpl9soC2dEcL7B1ptDhxCOigpgviVNf/QLMpdzJB+OAb6LFGZuB4fpSz6RdyZN4BdsEqJuwwy65feOgS7YQ9x07qGxOB1p2X4DCba3mGnxdFmaDxyb74gdkYr5WC30s2J/D44aHnui2YqvJhqWd1Wor9zjUxplxyD0dBn4oubJ6nB0RLPTza3u09YY0B4DcKnG1anmTuIK9+cWmv9tIAj+l9Q2nA4ShlbIZMJjGQGh3fN1NlfMj1H4G03NoeruLm7h4KvOtwmis88CPPnb0riM+oFA== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 06:02:24.5908 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d26097c9-d8a6-4208-4856-08dc1e345e27 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DS2PEPF0000343A.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB9163 Precedence: Bulk 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,duke.zhai@amd.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: 2MPfKZsN5IAOunzzxj0yLwMex1787277AA= Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1706248953216100011 Content-Type: text/plain; charset="utf-8" From: Duke Zhai BZ #:4640 In V2: Improve coding style. 1.Remove the leading underscore and use double underscore at trailing in = C header files. 2.Remove old tianocore licenses and redundant license description. 3.Improve coding style. For example: remove space between @param. In V1: Initial FCH UART port for Serial log output. Chachani board uses this UART for outputting debug log. Signed-off-by: Duke Zhai Cc: Eric Xing Cc: Ken Yao Cc: Igniculus Fu Cc: Abner Chang --- .../BaseSerialPortLib16550AmdFchUart.c | 463 ++++++++++++++++++ .../BaseSerialPortLib16550AmdFchUart.inf | 40 ++ 2 files changed, 503 insertions(+) create mode 100644 Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/Base= SerialPortLib16550AmdFchUart/BaseSerialPortLib16550AmdFchUart.c create mode 100644 Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/Base= SerialPortLib16550AmdFchUart/BaseSerialPortLib16550AmdFchUart.inf diff --git a/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/BaseSerialP= ortLib16550AmdFchUart/BaseSerialPortLib16550AmdFchUart.c b/Platform/AMD/Van= GoghBoard/VanGoghCommonPkg/Library/BaseSerialPortLib16550AmdFchUart/BaseSer= ialPortLib16550AmdFchUart.c new file mode 100644 index 0000000000..665f47f703 --- /dev/null +++ b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/BaseSerialPortLib1= 6550AmdFchUart/BaseSerialPortLib16550AmdFchUart.c @@ -0,0 +1,463 @@ +/** @file + 16550 UART Serial Port library functions + + Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
+ Copyright (C) 2014 Hewlett-Packard Development Company, L.P.
+ Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.
+ Copyright (c) 2020, ARM Limited. All rights reserved. + + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include + +// +// 16550 UART register offsets and bitfields +// +#define R_UART_RXBUF 0 +#define R_UART_TXBUF 0 +#define R_UART_BAUD_LOW 0 +#define R_UART_BAUD_HIGH 1 +#define R_UART_FCR 2 +#define B_UART_FCR_FIFOE BIT0 +#define B_UART_FCR_FIFO64 BIT5 +#define R_UART_LCR 3 +#define B_UART_LCR_DLAB BIT7 +#define R_UART_MCR 4 +#define B_UART_MCR_RTS BIT1 +#define R_UART_LSR 5 +#define B_UART_LSR_RXRDY BIT0 +#define B_UART_LSR_TXRDY BIT5 +#define B_UART_LSR_TEMT BIT6 +#define R_UART_MSR 6 +#define B_UART_MSR_CTS BIT4 +#define B_UART_MSR_DSR BIT5 + +/** + Read an 8-bit 16550 register. The parameter Offset is added to the base= address of the + 16550 registers that is specified by PcdSerialRegisterBase. + @param Offset The offset of the 16550 register to read. + @return The value read from the 16550 register. +**/ +UINT8 +SerialPortReadRegister ( + UINTN Offset + ) +{ + return MmioRead8 ((UINTN)PcdGet64 (PcdSerialRegisterBase) + Offset * 4); +} + +/** + Write an 8-bit 16550 register. The parameter Offset is added to the base= address of the + 16550 registers that is specified by PcdSerialRegisterBase. + @param Offset The offset of the 16550 register to write. + @param Value The value to write to the 16550 register specified by Of= fset. + @return The value written to the 16550 register. +**/ +UINT8 +SerialPortWriteRegister ( + UINTN Offset, + UINT8 Value + ) +{ + return MmioWrite8 ((UINTN)PcdGet64 (PcdSerialRegisterBase) + Offset * 4,= Value); +} + +/** + Return whether the hardware flow control signal allows writing. + + @retval TRUE The serial port is writable. + @retval FALSE The serial port is not writable. +**/ +BOOLEAN +SerialPortWritable ( + VOID + ) +{ + if (PcdGetBool (PcdSerialUseHardwareFlowControl)) { + if (PcdGetBool (PcdSerialDetectCable)) { + // + // Wait for both DSR and CTS to be set + // DSR is set if a cable is connected. + // CTS is set if it is ok to transmit data + // + // DSR CTS Description Action + // =3D=3D=3D =3D=3D=3D =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D =3D=3D=3D=3D=3D=3D=3D=3D + // 0 0 No cable connected. Wait + // 0 1 No cable connected. Wait + // 1 0 Cable connected, but not clear to send. Wait + // 1 1 Cable connected, and clear to send. Transmit + // + return (BOOLEAN)((SerialPortReadRegister (R_UART_MSR) & (B_UART_MSR_= DSR | B_UART_MSR_CTS)) =3D=3D (B_UART_MSR_DSR | B_UART_MSR_CTS)); + } else { + // + // Wait for both DSR and CTS to be set OR for DSR to be clear. + // DSR is set if a cable is connected. + // CTS is set if it is ok to transmit data + // + // DSR CTS Description Action + // =3D=3D=3D =3D=3D=3D =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D =3D=3D=3D=3D=3D=3D=3D=3D + // 0 0 No cable connected. Transmit + // 0 1 No cable connected. Transmit + // 1 0 Cable connected, but not clear to send. Wait + // 1 1 Cable connected, and clar to send. Transmit + // + return (BOOLEAN)((SerialPortReadRegister (R_UART_MSR) & (B_UART_MSR_= DSR | B_UART_MSR_CTS)) !=3D (B_UART_MSR_DSR)); + } + } + + return TRUE; +} + +/** + RCheck Cable connection. + + @retval TRUE RCheck Cable not connect. + @retval FALSE RCheck Cable connect. +**/ +BOOLEAN +CheckCableConnection ( + ) +{ + UINT32 RetryCount; + + // Check Cable connection + RetryCount =3D 200; + if (PcdGetBool (PcdSerialDetectCable)) { + do { + RetryCount--; + } while (((SerialPortReadRegister (R_UART_MSR) & (B_UART_MSR_DSR | B_U= ART_MSR_CTS)) !=3D (B_UART_MSR_DSR | B_UART_MSR_CTS)) && (RetryCount > 0)); + } + + if (RetryCount =3D=3D 0) { + // Time expired + return FALSE; + } + + return TRUE; +} + +/** + Check Serial Port status. + + @retval TRUE The serial port is enable. + @retval FALSE The serial port is not enable. +**/ +BOOLEAN +CheckSerialPort ( + ) +{ + UINTN Divisor; + UINT32 SerialClkDiv16; + + SerialClkDiv16 =3D 48000000/ 16; + // + // See if the serial port is already initialized + // + if ((SerialPortReadRegister (R_UART_FCR) & (B_UART_FCR_FIFOE | B_UART_FC= R_FIFO64)) !=3D + (PcdGet8 (PcdSerialFifoControl) & (B_UART_FCR_FIFOE | B_UART_FC= R_FIFO64))) + { + return FALSE; + } + + if ((SerialPortReadRegister (R_UART_LCR) & 0x3F) !=3D (PcdGet8 (PcdSeria= lLineControl) & 0x3F)) { + return FALSE; + } + + SerialPortWriteRegister (R_UART_LCR, (UINT8)(SerialPortReadRegister (R_U= ART_LCR) | B_UART_LCR_DLAB)); + Divisor =3D SerialPortReadRegister (R_UART_BAUD_HIGH) << 8; + Divisor |=3D SerialPortReadRegister (R_UART_BAUD_LOW); + SerialPortWriteRegister (R_UART_LCR, (UINT8)(SerialPortReadRegister (R_U= ART_LCR) & ~B_UART_LCR_DLAB)); + if (Divisor !=3D SerialClkDiv16 / PcdGet32 (PcdSerialBaudRate)) { + return FALSE; + } + + return TRUE; +} + +/** + Initial Serial Port. + +**/ +VOID +InitSerialPort ( + ) +{ + UINTN Divisor; + UINT32 SerialClkDiv16; + + SerialClkDiv16 =3D 48000000 / 16; + // + // Configure baud rate + // + Divisor =3D SerialClkDiv16 / PcdGet32 (PcdSerialBaudRate); + SerialPortWriteRegister (R_UART_LCR, B_UART_LCR_DLAB); + SerialPortWriteRegister (R_UART_BAUD_HIGH, (UINT8)(Divisor >> 8)); + SerialPortWriteRegister (R_UART_BAUD_LOW, (UINT8)(Divisor & 0xff)); + + // + // Clear DLAB and configure Data Bits, Parity, and Stop Bits. + // Strip reserved bits from PcdSerialLineControl + // + SerialPortWriteRegister (R_UART_LCR, (UINT8)(PcdGet8 (PcdSerialLineContr= ol) & 0x3F)); + + // + // Enable and reset FIFOs + // Strip reserved bits from PcdSerialFifoControl + // + SerialPortWriteRegister (R_UART_FCR, (UINT8)(PcdGet8 (PcdSerialFifoContr= ol) & 0x27)); + + // + // Put Modem Control Register(MCR) into its reset state of 0x00. + // + SerialPortWriteRegister (R_UART_MCR, 0x00); +} + +/** + Initialize the serial device hardware. + + If no initialization is required, then return RETURN_SUCCESS. + If the serial device was successfully initialized, then return RETURN_SU= CCESS. + If the serial device could not be initialized, then return RETURN_DEVICE= _ERROR. + + @retval RETURN_SUCCESS The serial device was initialized. + @retval RETURN_DEVICE_ERROR The serial device could not be initialized. + +**/ +RETURN_STATUS +EFIAPI +SerialPortInitialize ( + VOID + ) +{ + RETURN_STATUS Status; + + Status =3D PlatformHookSerialPortInitialize (); + if (RETURN_ERROR (Status)) { + return Status; + } + + if (!CheckCableConnection ()) { + return RETURN_DEVICE_ERROR; + } + + if (!CheckSerialPort ()) { + InitSerialPort (); + } + + return RETURN_SUCCESS; +} + +/** + Write data from buffer to serial device. + + Writes NumberOfBytes data bytes from Buffer to the serial device. + The number of bytes actually written to the serial device is returned. + If the return value is less than NumberOfBytes, then the write operation= failed. + + If Buffer is NULL, then ASSERT(). + + If NumberOfBytes is zero, then return 0. + + @param Buffer Pointer to the data buffer to be written. + @param NumberOfBytes Number of bytes to written to the serial device. + + @retval 0 NumberOfBytes is 0. + @retval >0 The number of bytes written to the serial devic= e. + If this value is less than NumberOfBytes, then = the read operation failed. + +**/ +UINTN +EFIAPI +SerialPortWrite ( + IN UINT8 *Buffer, + IN UINTN NumberOfBytes + ) +{ + UINTN Result; + UINTN Index; + UINTN FifoSize; + UINT32 RetryCount; + + if (Buffer =3D=3D NULL) { + return 0; + } + + if (!CheckCableConnection ()) { + return 0; + } + + if (NumberOfBytes =3D=3D 0) { + // + // Flush the hardware + // + + // + // Wait for both the transmit FIFO and shift register empty. + // + RetryCount =3D 2000; + do { + RetryCount--; + } while (((SerialPortReadRegister (R_UART_LSR) & B_UART_LSR_TEMT) =3D= =3D 0) && (RetryCount > 0)); + + if (RetryCount =3D=3D 0) { + InitSerialPort (); + } + + // + // Wait for the hardware flow control signal + // + while (!SerialPortWritable ()) { + } + + return 0; + } + + // + // Compute the maximum size of the Tx FIFO + // + FifoSize =3D 1; + if ((PcdGet8 (PcdSerialFifoControl) & B_UART_FCR_FIFOE) !=3D 0) { + if ((PcdGet8 (PcdSerialFifoControl) & B_UART_FCR_FIFO64) =3D=3D 0) { + FifoSize =3D 16; + } else { + FifoSize =3D 64; + } + } + + Result =3D NumberOfBytes; + while (NumberOfBytes !=3D 0) { + // + // Wait for the serial port to be ready, to make sure both the transmi= t FIFO + // and shift register empty. + // + RetryCount =3D 2000; + do { + RetryCount--; + } while (((SerialPortReadRegister (R_UART_LSR) & B_UART_LSR_TEMT) =3D= =3D 0) && (RetryCount > 0)); + + if (RetryCount =3D=3D 0) { + InitSerialPort (); + } + + // + // Fill then entire Tx FIFO + // + for (Index =3D 0; Index < FifoSize && NumberOfBytes !=3D 0; Index++, N= umberOfBytes--, Buffer++) { + // + // Wait for the hardware flow control signal + // + while (!SerialPortWritable ()) { + } + + // + // Write byte to the transmit buffer. + // + SerialPortWriteRegister (R_UART_TXBUF, *Buffer); + } + } + + return Result; +} + +/** + Reads data from a serial device into a buffer. + + @param Buffer Pointer to the data buffer to store the data re= ad from the serial device. + @param NumberOfBytes Number of bytes to read from the serial device. + + @retval 0 NumberOfBytes is 0. + @retval >0 The number of bytes read from the serial device. + If this value is less than NumberOfBytes, then = the read operation failed. + +**/ +UINTN +EFIAPI +SerialPortRead ( + OUT UINT8 *Buffer, + IN UINTN NumberOfBytes + ) +{ + UINTN Result; + UINT8 Mcr; + + if (NULL =3D=3D Buffer) { + return 0; + } + + Mcr =3D (UINT8)(SerialPortReadRegister (R_UART_MCR) & ~B_UART_MCR_RTS); + + for (Result =3D 0; NumberOfBytes-- !=3D 0; Result++, Buffer++) { + // + // Wait for the serial port to have some data. + // + while ((SerialPortReadRegister (R_UART_LSR) & B_UART_LSR_RXRDY) =3D=3D= 0) { + if (PcdGetBool (PcdSerialUseHardwareFlowControl)) { + // + // Set RTS to let the peer send some data + // + SerialPortWriteRegister (R_UART_MCR, (UINT8)(Mcr | B_UART_MCR_RTS)= ); + } + } + + if (PcdGetBool (PcdSerialUseHardwareFlowControl)) { + // + // Clear RTS to prevent peer from sending data + // + SerialPortWriteRegister (R_UART_MCR, Mcr); + } + + // + // Read byte from the receive buffer. + // + *Buffer =3D SerialPortReadRegister (R_UART_RXBUF); + } + + return Result; +} + +/** + Polls a serial device to see if there is any data waiting to be read. + + Polls aserial device to see if there is any data waiting to be read. + If there is data waiting to be read from the serial device, then TRUE is= returned. + If there is no data waiting to be read from the serial device, then FALS= E is returned. + + @retval TRUE Data is waiting to be read from the serial devi= ce. + @retval FALSE There is no data waiting to be read from the se= rial device. + +**/ +BOOLEAN +EFIAPI +SerialPortPoll ( + VOID + ) +{ + // + // Read the serial port status + // + if ((SerialPortReadRegister (R_UART_LSR) & B_UART_LSR_RXRDY) !=3D 0) { + if (PcdGetBool (PcdSerialUseHardwareFlowControl)) { + // + // Clear RTS to prevent peer from sending data + // + SerialPortWriteRegister (R_UART_MCR, (UINT8)(SerialPortReadRegister = (R_UART_MCR) & ~B_UART_MCR_RTS)); + } + + return TRUE; + } + + if (PcdGetBool (PcdSerialUseHardwareFlowControl)) { + // + // Set RTS to let the peer send some data + // + SerialPortWriteRegister (R_UART_MCR, (UINT8)(SerialPortReadRegister (R= _UART_MCR) | B_UART_MCR_RTS)); + } + + return FALSE; +} diff --git a/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/BaseSerialP= ortLib16550AmdFchUart/BaseSerialPortLib16550AmdFchUart.inf b/Platform/AMD/V= anGoghBoard/VanGoghCommonPkg/Library/BaseSerialPortLib16550AmdFchUart/BaseS= erialPortLib16550AmdFchUart.inf new file mode 100644 index 0000000000..e6f824401d --- /dev/null +++ b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/BaseSerialPortLib1= 6550AmdFchUart/BaseSerialPortLib16550AmdFchUart.inf @@ -0,0 +1,40 @@ +## @file +# SerialPortLib instance for 16550 UART. +# +# Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
+# Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.
+# Copyright (c) 2020, ARM Limited. All rights reserved. +# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D BaseSerialPortLib16550AmdFchUart + FILE_GUID =3D A66281AD-66E9-4089-9B1C-9CFC84D8A760 + MODULE_TYPE =3D BASE + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D SerialPortLib + +[Sources] + BaseSerialPortLib16550AmdFchUart.c + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + VanGoghCommonPkg/AmdCommonPkg.dec + +[LibraryClasses] + BaseLib + PcdLib + IoLib + PlatformHookLib + +[Pcd] + gEfiMdeModulePkgTokenSpaceGuid.PcdSerialUseHardwareFlowControl + gEfiMdeModulePkgTokenSpaceGuid.PcdSerialDetectCable + gEfiMdeModulePkgTokenSpaceGuid.PcdSerialRegisterBase + gEfiMdeModulePkgTokenSpaceGuid.PcdSerialBaudRate + gEfiMdeModulePkgTokenSpaceGuid.PcdSerialLineControl + gEfiMdeModulePkgTokenSpaceGuid.PcdSerialFifoControl \ No newline at end of file -- 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 (#114505): https://edk2.groups.io/g/devel/message/114505 Mute This Topic: https://groups.io/mt/103971402/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-