From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by mx.groups.io with SMTP id smtpd.web10.21751.1661920954209981377 for ; Tue, 30 Aug 2022 21:42:34 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=hFxKjhk5; spf=permerror, err=too many SPF records (domain: intel.com, ip: 192.55.52.115, mailfrom: guo.dong@intel.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661920954; x=1693456954; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=cWSzMOvAcDBU9UZHqRXvwDAmdzHyhmGIwAtYFRHlfqA=; b=hFxKjhk5EDCqr8u+HPCw24ifd8muiutZApCEsfVMGaL1TRpZ2DbGkJQy epnoA2VrMNm5Tx4vaek9A8aYbo8o+RcCvsSagOqXoYxBf5NIrB8TqYhE1 J+8X9SJZiszkBzTemIn7dGMxQjVWFUNp5KSiD6JLZD0HXsBmand6h1mL5 KZLYd6wFgnBieMjwcc5og9bbk0ICmOrvHpIiQKNs2rgqbf6OwDUHLZvLp L6qLcQZk2IVZK34NIRoyUrx8PwNH6+xH0emUyeeT1pk/2Cq9ZrKqN75yw ALxEToIXYysJmgpqXN3MByHmIWJ77gDHDlJGKj/PcSp8oFUoKepDfokTu g==; X-IronPort-AV: E=McAfee;i="6500,9779,10455"; a="295375292" X-IronPort-AV: E=Sophos;i="5.93,276,1654585200"; d="scan'208";a="295375292" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Aug 2022 21:42:33 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,276,1654585200"; d="scan'208";a="754302873" Received: from orsmsx603.amr.corp.intel.com ([10.22.229.16]) by fmsmga001.fm.intel.com with ESMTP; 30 Aug 2022 21:42:33 -0700 Received: from orsmsx602.amr.corp.intel.com (10.22.229.15) by ORSMSX603.amr.corp.intel.com (10.22.229.16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Tue, 30 Aug 2022 21:42:33 -0700 Received: from ORSEDG601.ED.cps.intel.com (10.7.248.6) by orsmsx602.amr.corp.intel.com (10.22.229.15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31 via Frontend Transport; Tue, 30 Aug 2022 21:42:33 -0700 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (104.47.57.176) by edgegateway.intel.com (134.134.137.102) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2375.31; Tue, 30 Aug 2022 21:42:32 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SmfB0uKk7Okl9a7T+V7gHBVOgsTyDUFpGTv0XXyWEGeU+LfkzPNghPhkjz3ixb+ymT6lQDSnGc8CmLWU8UcZzNZRKBRcgsu65c4NylggSldrjKfWqQkI+wB2JFygaBc1r732rtP731G3xFfZcOQboa9LvMOCn7O3225fIly+qqhlfqyzgMmD0wxxOHNQgk+qhzhL3UPV75oLsG4sHKm4IpB8nSAeybuZOCUJoO44trSGxejIIy7pETp4vv2Dqz+pMKY2m2lCUYL6f0QEvCWPtXziEFtkWdFLmdV+0Zroh/aNRgCiZrT4uqVdMN3+gfwDQX7njGqBVpLFg4QYQYV4rg== 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=7zpEoSZre6D7zebZyY0m7n9E0k305DyYgOktaSsXHOA=; b=if+AujUT/lsQe2hoLw8QdE7KCnN2Ux0d/0qtHnLYnFn/aGgeupivW+c8NkQTnJNuhXQGS5NDj+4swM1wRGV0zRIiNtL0evGF1JQPGjeb8dSs25+Scl7mqnBf436I2WomZ3Qi2YLW+77jPRxQP0plLWIvEGH+FmfiURye7L+9Nu8tPny49/fZG7aa85s8c9UTHNhPuSBfbYacDJWWqijsvNmrDbmh3iiUdmlojzs0T5FmLbS/lFrxrpUgIgUayz254CI0vvr5BEoNCBSQxz5jB06qIw8Zsf1qORLxsaRYfIuTZ7gYjAb+sw4UbS+sMZ4C4+Nesuio0baAFNxcx9BB7w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=intel.com; dmarc=pass action=none header.from=intel.com; dkim=pass header.d=intel.com; arc=none Received: from CY5PR11MB6260.namprd11.prod.outlook.com (2603:10b6:930:23::16) by MWHPR1101MB2206.namprd11.prod.outlook.com (2603:10b6:301:51::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5566.15; Wed, 31 Aug 2022 04:42:25 +0000 Received: from CY5PR11MB6260.namprd11.prod.outlook.com ([fe80::a881:6cab:38a6:d6a5]) by CY5PR11MB6260.namprd11.prod.outlook.com ([fe80::a881:6cab:38a6:d6a5%7]) with mapi id 15.20.5588.010; Wed, 31 Aug 2022 04:42:24 +0000 From: "Guo Dong" To: "Sravanthi, K KavyaX" , "devel@edk2.groups.io" CC: "Ni, Ray" , "Rhodes, Sean" , "Guo, Gua" Subject: Re: [PATCH v5 1/2] UefiPayloadPkg: Implement a new SerialPortLib instance Thread-Topic: [PATCH v5 1/2] UefiPayloadPkg: Implement a new SerialPortLib instance Thread-Index: AQHYvDGLxnbahpEMtkCZqsBwXzhEA63Ibg8A Date: Wed, 31 Aug 2022 04:42:24 +0000 Message-ID: References: <20220830052908.1903-1-k.kavyax.sravanthi@intel.com> In-Reply-To: <20220830052908.1903-1-k.kavyax.sravanthi@intel.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: dlp-product: dlpe-windows dlp-reaction: no-action dlp-version: 11.6.500.17 authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=intel.com; x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: eff0ad95-8b7e-4384-3cb2-08da8b0b3349 x-ms-traffictypediagnostic: MWHPR1101MB2206:EE_ x-ld-processed: 46c98d88-e344-4ed4-8496-4ed7712e255d,ExtAddr x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: OFMt3Rn9NjFxan/bWk9TR9uSfASAv1KbPI2svZser+3UWA/Eo0h207WyM9YSBCIKBNOlgX1gkFgVfRo31NGidjI0a+m9+jCyMbQNSQB4CiJbJezwZWvhHoZQrzl2vrgmZUWpDnipALbeeHy668i0qcR1moWS9eZ6nxn6KBuNrlpm6oFgfkylsXepo/8hHM6SBvmO5Pqasz9j8Dh7bDhql22oA58BzHtnWoDr3U3FcHXsMpgWC/L4AP142vULHCQw3VlguOdwibL3x/yqCmsDmnYNavzz7L6MN5WGW/pujy1fMtu6ZrdwDGjE4mHAkfcMRg1RNzmAx7V1BhQEvAfbg3MffU5Z8dOs76XNe5v3LA4Ipia5c/wcULpNL9tvc2x3xdIAxrsA4BR7H/QD/hOv9ANVZYEthAPm+JsbQ1C+cwiL1ZBNNETdKY8C/m8vfs1Gkvp6pz53tLf84DX7xutvdHdng9B0lDQ2i9phOdc3dPupKBXVwkstSFYIfcLYK+f02fGsKYgNholhkfts87w8FUYXhhAHCe6YJmjbTNb8CcTlhy4nHW8xhfBD0zBZWKK+GPeJy00DLoZYHQGoADSu6Aju+T1HNKCzdMa3LwDB88NC4SE3d6s4d9r6CQsV85+V5X8SUetNrKJAkGvDj6sI2XMVcti50vPp9EDWyGvXZbCLSeQfNbQlhlXUOSdurMZZhlar4pVtJb+TNU13fwLz/CkSjL/DRAaAHTNK0IupfUr28euuTzhdTIeZGIAzddDIzRWw/gjAfR3n3dQ9B1Drag== x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CY5PR11MB6260.namprd11.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230016)(39860400002)(396003)(136003)(376002)(346002)(366004)(71200400001)(30864003)(316002)(33656002)(9686003)(26005)(186003)(107886003)(53546011)(2906002)(6506007)(83380400001)(7696005)(38070700005)(86362001)(82960400001)(66476007)(55016003)(478600001)(122000001)(8936002)(41300700001)(54906003)(4326008)(52536014)(38100700002)(66946007)(76116006)(5660300002)(66446008)(110136005)(66556008)(8676002)(64756008)(579004)(559001);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?KFy5rzW7pzTzZK1l3KDnBsQoEU5gf4u0m9fiV3qjk2MOsWUggg1YAh9933UO?= =?us-ascii?Q?C//MsIR3E0zpFTWZFuBPRa+n9kGri3gcqZhGws5wVKK663Jd9qcdx/oxOixf?= =?us-ascii?Q?s4QTHlcFDoEPsupdDN6aDAMeUCYOn8+pVX4WPvVW1UnU5H4QZNtFj9bjeleX?= =?us-ascii?Q?DcOnUpvixSKCJo+rR0JjchRh6Ti1EGV2MSCP/ElND9Qme3mwQo6WKZ+QZWKP?= =?us-ascii?Q?b4hzSTwDhairugld16yPkKH3E6tJKRpmwEWh6d/IZ6JakecvZrl9ZoNNkLDo?= =?us-ascii?Q?r7/4gcOs+nP/97trUVcIltvui5EfMymDZkfqBjQCHNtyIgWpw6v5f9/jUtnY?= =?us-ascii?Q?NZfyVMtAFtL29JAzQT9ji24L013XmNp9pAQfvjD9Pi/9QZStj9J9xKfeCjy1?= =?us-ascii?Q?DxgUe60G48vO0OeuaTpoCSN+pustfIvFfngJv7tjhjNpkF3iEiv38WcWnkWg?= =?us-ascii?Q?z1tuPg9DEjXZpEr7Rud8AeYZ2ok8iftbNqqTpTWRARP7q3ncVXUYrN4EBTlu?= =?us-ascii?Q?BsW+8vsoJTYPqgj8QivaPrvuM0jev/V+pLCbdkGVRCeOJROM7smw+kauaJ7i?= =?us-ascii?Q?2rYzu9R5KTixegAm9n8YFhswhF6V42DuSW0bmTQ+jinOBV0lRAlPLXkjNF7M?= =?us-ascii?Q?cgOjGZ0FxS+y2ZIIwn494fMCOnzFJbLjZfvD3LH7jJKytLmhzvsaG7ozvtox?= =?us-ascii?Q?wP91XxalMzCHNjc4w271A2+lXD0DnroRFf8Y8nRpLc4UHaPveMnGFbZ+Yj+h?= =?us-ascii?Q?1sUQUdPpNzsOZ4GTO7AASIbbq4ecVbu+B2qb1TcUh3o8AuVHcwZqrP27zbGY?= =?us-ascii?Q?WCfqyRmZ+XWY1BPsgw6GMWkmUIEotBsvdRNTKrgMyGYVzEjj3qYjFxEIlkpr?= =?us-ascii?Q?q3ddpihoaayZEFrJ5/Tg6tkhuMbVYK2Kbx+Uol8XENZQqnSo+57EszPM50hN?= =?us-ascii?Q?y3eG7ViO3CdDzcGEdLCbZMM8r3R6A5qkjxr4bpXuUirA2n+RtUW0ADiyUm22?= =?us-ascii?Q?avRl3s7zjiGfIhuCyNhbHIX2Jp5yceNQiptF9Dyr294vchvey5qBA4hUK36q?= =?us-ascii?Q?yg+VKLM3iZcLna9mfYvrUjCM+IxDTqJF0SsfpgaU/yGyIS+Z0ejocINWetdW?= =?us-ascii?Q?f0Gbdrct9pynrjk/lW0hA9bCMhLBy5QdJiSQWd5xNO1CyAy/PJYcxq87seOy?= =?us-ascii?Q?3jKq+F4vzDM2ZUbJ70P55lf1B4U0+Ndv1mA3rdzzz9j+SiyXiolZufJmSZwh?= =?us-ascii?Q?lWSv2QKK5Pv4bbll5NHZdjpzR2ThJtROMaAenfOl8WoM8MF1l5KIda0chvXl?= =?us-ascii?Q?aoR2Rn1wBHPzzfU3gIfOLk4Oxxx26qpkphCzexfQqnTuwKiiAqKEZmItx4OC?= =?us-ascii?Q?xe22Na62Q9lVEp/A97nHT7t3EV7V07VT++XEswBGabAj5pBF/wy0ITS/RySW?= =?us-ascii?Q?xnCUMD8IUncFd1vdyCnWrASL2hZ7oTKVoC5UQub9R0qOtey47bxtDXmPPxVx?= =?us-ascii?Q?jk674i5JEqDCaWQ/DWlXqWYTbUP4yZzPLXRP2jPAZZiadjJoaJAaX3JWrKQx?= =?us-ascii?Q?WYu1tdm3SvSPgxldoBdk5cLQ9/wRQ8OHYHuwfaTJ?= MIME-Version: 1.0 X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: CY5PR11MB6260.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: eff0ad95-8b7e-4384-3cb2-08da8b0b3349 X-MS-Exchange-CrossTenant-originalarrivaltime: 31 Aug 2022 04:42:24.7060 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 46c98d88-e344-4ed4-8496-4ed7712e255d X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: n3Z5skLIO6PhRnepB6yYoalBAybpZOm/s1JNp8f24TlvZgHpTsKjLQBzNvhzWp6g7NrzqqRqY4WXuGE3O9Wbtg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR1101MB2206 Return-Path: guo.dong@intel.com X-OriginatorOrg: intel.com Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Had better read the register first and write register to have a short line = and make it more readable. + SerialPortWriteRegister (SerialRegisterBase, R_UART_LCR, (UINT8)(Seria= lPortReadRegister (SerialRegisterBase, R_UART_LCR, MmioEnable, RegisterStri= de) | B_UART_LCR_DLAB), MmioEnable, RegisterStride); + SerialPortWriteRegister (SerialRegisterBase, R_UART_LCR, (UINT8)(Seria= lPortReadRegister (SerialRegisterBase, R_UART_LCR, MmioEnable, RegisterStri= de) & ~B_UART_LCR_DLAB), MmioEnable, RegisterStride); The other change looks good to me. Reviewed-by: Guo Dong -----Original Message----- From: Sravanthi, K KavyaX =20 Sent: Monday, August 29, 2022 10:29 PM To: devel@edk2.groups.io Cc: Sravanthi, K KavyaX ; Dong, Guo ; Ni, Ray ; Rhodes, Sean ; Guo, Gua Subject: [PATCH v5 1/2] UefiPayloadPkg: Implement a new SerialPortLib insta= nce Add new Serial port library instance that consumes the HOB defined in MdeMo= dulePkg/Include/UniversalPayload/SerialPortInfo.h to support multiple UART'= s. Cc: Guo Dong Cc: Ray Ni Cc: Sean Rhodes Cc: Gua Guo Signed-off-by: Kavya --- UefiPayloadPkg/Library/BaseSerialPortLibHob/BaseSerialPortLibHob.c | 805= ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ UefiPayloadPkg/Library/BaseSerialPortLibHob/BaseSerialPortLibHob.inf | 39= +++++++++++++++++++++++++++++++++++++++ 2 files changed, 844 insertions(+) diff --git a/UefiPayloadPkg/Library/BaseSerialPortLibHob/BaseSerialPortLibH= ob.c b/UefiPayloadPkg/Library/BaseSerialPortLibHob/BaseSerialPortLibHob.c new file mode 100644 index 0000000000..875f4307f7 --- /dev/null +++ b/UefiPayloadPkg/Library/BaseSerialPortLibHob/BaseSerialPortLibHob.c @@ -0,0 +1,805 @@ +/** @file + UART Serial Port library functions. + + Copyright (c) 2022, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// +// 16550 UART register offsets and bitfields // +#define R_UART_RXBUF 0 // LCR_DLAB =3D 0 +#define R_UART_TXBUF 0 // LCR_DLAB =3D 0 +#define R_UART_BAUD_LOW 0 // LCR_DLAB =3D 1 +#define R_UART_BAUD_HIGH 1 // LCR_DLAB =3D 1 +#define R_UART_IER 1 // LCR_DLAB =3D 0 +#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_DTRC BIT0 +#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 +#define B_UART_MSR_RI BIT6 +#define B_UART_MSR_DCD BIT7 + +#define MAX_SIZE 16 + +typedef struct { + UINTN BaseAddress; + BOOLEAN UseMmio; + UINT32 BaudRate; + UINT8 RegisterStride; +} UART_INFO; + +UART_INFO mUartInfo[MAX_SIZE]; +UINT8 mUartCount =3D 0; + +/** + Reads an 8-bit register. If UseMmio is TRUE, then the value is read=20 +from + MMIO space. If UseMmio is FALSE, then the value is read from I/O=20 +space. The + parameter Offset is added to the base address of the register. + + @param Base The base address register of UART device. + @param Offset The offset of the register to read. + @param UseMmio Check if value has to be read from MMIO space o= r IO space. + @param RegisterStride Number of bytes between registers in serial dev= ice. + + @return The value read from the register. + +**/ +UINT8 +SerialPortReadRegister ( + UINTN Base, + UINTN Offset, + BOOLEAN UseMmio, + UINT8 RegisterStride + ) +{ + if (UseMmio) { + return MmioRead8 (Base + Offset * RegisterStride); + } else { + return IoRead8 (Base + Offset * RegisterStride); + } +} + +/** + Writes an 8-bit register.. If UseMmio is TRUE, then the value is=20 +written to + MMIO space. If UseMmio is FALSE, then the value is written to I/O=20 +space. The + parameter Offset is added to the base address of the registers. + + @param Base The base address register of UART device. + @param Offset The offset of the register to write. + @param Value Value to be written. + @param UseMmio Check if value has to be written to MMIO space = or IO space. + @param RegisterStride Number of bytes between registers in serial dev= ice. + + @return The value written to the register. + +**/ +UINT8 +SerialPortWriteRegister ( + UINTN Base, + UINTN Offset, + UINT8 Value, + BOOLEAN UseMmio, + UINT8 RegisterStride + ) +{ + if (UseMmio) { + return MmioWrite8 (Base + Offset * RegisterStride, Value); + } else { + return IoWrite8 (Base + Offset * RegisterStride, Value); + } +} + +/** + 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. + + @retval RETURN_SUCCESS The serial device was initialized. + +**/ +RETURN_STATUS +EFIAPI +SerialPortInitialize ( + VOID + ) +{ + UNIVERSAL_PAYLOAD_SERIAL_PORT_INFO *SerialPortInfo; + EFI_HOB_GUID_TYPE *GuidHob; + UINTN SerialRegisterBase; + UINT8 RegisterStride; + UINT32 Divisor; + UINT32 CurrentDivisor; + UINT32 BaudRate; + BOOLEAN Initialized; + BOOLEAN MmioEnable; + + GuidHob =3D GetFirstGuidHob (&gUniversalPayloadSerialPortInfoGuid); + while (GuidHob !=3D NULL) { + SerialPortInfo =3D (UNIVERSAL_PAYLOAD_SERIAL_PORT_INFO *)GET_GUID_HOB_= DATA (GuidHob); + SerialRegisterBase =3D SerialPortInfo->RegisterBase; + MmioEnable =3D SerialPortInfo->UseMmio; + BaudRate =3D SerialPortInfo->BaudRate; + RegisterStride =3D SerialPortInfo->RegisterStride; + + if (SerialRegisterBase =3D=3D 0) { + GuidHob =3D GET_NEXT_HOB (GuidHob); + GuidHob =3D GetNextGuidHob (&gUniversalPayloadSerialPortInfoGuid, Gu= idHob); + continue; + } + + mUartInfo[mUartCount].BaseAddress =3D SerialRegisterBase; + mUartInfo[mUartCount].UseMmio =3D MmioEnable; + mUartInfo[mUartCount].BaudRate =3D BaudRate; + mUartInfo[mUartCount].RegisterStride =3D RegisterStride; + mUartCount++; + + Divisor =3D PcdGet32 (PcdSerialClockRate) / (BaudRate * 16); + if ((PcdGet32 (PcdSerialClockRate) % (BaudRate * 16)) >=3D BaudRate * = 8) { + Divisor++; + } + + // + // See if the serial port is already initialized + // + Initialized =3D TRUE; + if ((SerialPortReadRegister (SerialRegisterBase, R_UART_LCR, MmioEnabl= e, RegisterStride) & 0x3F) !=3D (PcdGet8 (PcdSerialLineControl) & 0x3F)) { + Initialized =3D FALSE; + } + + SerialPortWriteRegister (SerialRegisterBase, R_UART_LCR, (UINT8)(Seria= lPortReadRegister (SerialRegisterBase, R_UART_LCR, MmioEnable, RegisterStri= de) | B_UART_LCR_DLAB), MmioEnable, RegisterStride); + CurrentDivisor =3D SerialPortReadRegister (SerialRegisterBase, R_UAR= T_BAUD_HIGH, MmioEnable, RegisterStride) << 8; + CurrentDivisor |=3D (UINT32)SerialPortReadRegister (SerialRegisterBase= , R_UART_BAUD_LOW, MmioEnable, RegisterStride); + SerialPortWriteRegister (SerialRegisterBase, R_UART_LCR, (UINT8)(Seria= lPortReadRegister (SerialRegisterBase, R_UART_LCR, MmioEnable, RegisterStri= de) & ~B_UART_LCR_DLAB), MmioEnable, RegisterStride); + if (CurrentDivisor !=3D Divisor) { + Initialized =3D FALSE; + } + + if (Initialized) { + GuidHob =3D GET_NEXT_HOB (GuidHob); + GuidHob =3D GetNextGuidHob (&gUniversalPayloadSerialPortInfoGuid, Gu= idHob); + continue; + } + + // + // Configure baud rate + // + SerialPortWriteRegister (SerialRegisterBase, R_UART_LCR, B_UART_LCR_DL= AB, MmioEnable, RegisterStride); + SerialPortWriteRegister (SerialRegisterBase, R_UART_BAUD_HIGH, (UINT8)= (Divisor >> 8), MmioEnable, RegisterStride); + SerialPortWriteRegister (SerialRegisterBase, R_UART_BAUD_LOW,=20 + (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,=20 + (UINT8)(PcdGet8 (PcdSerialLineControl) & 0x3F), MmioEnable,=20 + RegisterStride); + + // + // Enable and reset FIFOs + // Strip reserved bits from PcdSerialFifoControl + // + SerialPortWriteRegister (SerialRegisterBase, R_UART_FCR, 0x00, MmioEna= ble, RegisterStride); + SerialPortWriteRegister (SerialRegisterBase, R_UART_FCR,=20 + (UINT8)(PcdGet8 (PcdSerialFifoControl) & (B_UART_FCR_FIFOE |=20 + B_UART_FCR_FIFO64)), MmioEnable, RegisterStride); + + // + // Set FIFO Polled Mode by clearing IER after setting FCR + // + SerialPortWriteRegister (SerialRegisterBase, R_UART_IER, 0x00,=20 + MmioEnable, RegisterStride); + + // + // Put Modem Control Register(MCR) into its reset state of 0x00. + // + SerialPortWriteRegister (SerialRegisterBase, R_UART_MCR, 0x00,=20 + MmioEnable, RegisterStride); + + GuidHob =3D GET_NEXT_HOB (GuidHob); + GuidHob =3D GetNextGuidHob (&gUniversalPayloadSerialPortInfoGuid,=20 + GuidHob); } + + 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 return 0. + + 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 write operation failed. + +**/ +UINTN +EFIAPI +SerialPortWrite ( + IN UINT8 *Buffer, + IN UINTN NumberOfBytes + ) +{ + UINTN BaseAddress; + BOOLEAN UseMmio; + UINTN BytesLeft; + UINTN Index; + UINTN FifoSize; + UINT8 *DataBuffer; + UINT8 Count; + UINT8 Stride; + + if (Buffer =3D=3D NULL || NumberOfBytes =3D=3D 0 || mUartCount =3D=3D 0)= { + return 0; + } + + // + // Compute the maximum size of the Tx FIFO // FifoSize =3D 1; if=20 + ((PcdGet8 (PcdSerialFifoControl) & B_UART_FCR_FIFOE) !=3D 0) { + if ((PcdGet8 (PcdSerialFifoControl) & B_UART_FCR_FIFO64) =3D=3D 0) { + FifoSize =3D 16; + } else { + FifoSize =3D PcdGet32 (PcdSerialExtendedTxFifoSize); + } + } + + Count =3D 0; + while (Count < mUartCount) { + BaseAddress =3D mUartInfo[Count].BaseAddress; + UseMmio =3D mUartInfo[Count].UseMmio; + Stride =3D mUartInfo[Count].RegisterStride; + + if (BaseAddress =3D=3D 0) { + Count++; + continue; + } + + DataBuffer =3D Buffer; + BytesLeft =3D NumberOfBytes; + + while (BytesLeft !=3D 0) { + // + // Fill the entire Tx FIFO + // + for (Index =3D 0; Index < FifoSize && BytesLeft !=3D 0; Index++, Byt= esLeft--, DataBuffer++) { + // + // Write byte to the transmit buffer. + // + SerialPortWriteRegister (BaseAddress, R_UART_TXBUF, *DataBuffer, U= seMmio, Stride); + } + MicroSecondDelay (20); + } + Count++; + } + + return NumberOfBytes; +} + +/** + 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 BaseAddress; + BOOLEAN UseMmio; + UINT8 *DataBuffer; + UINTN BytesLeft; + UINTN Result; + UINT8 Mcr; + UINT8 Count; + UINT8 Stride; + + if (Buffer =3D=3D NULL) { + return 0; + } + + Count =3D 0; + while (Count < mUartCount) { + BaseAddress =3D mUartInfo[Count].BaseAddress; + UseMmio =3D mUartInfo[Count].UseMmio; + Stride =3D mUartInfo[Count].RegisterStride; + + if (BaseAddress =3D=3D 0) { + Count++; + continue; + } + + DataBuffer =3D Buffer; + BytesLeft =3D NumberOfBytes; + + Mcr =3D (UINT8)(SerialPortReadRegister (BaseAddress, R_UART_MCR,=20 + UseMmio, Stride) & ~B_UART_MCR_RTS); + + for (Result =3D 0; BytesLeft-- !=3D 0; Result++, DataBuffer++) { + // + // Wait for the serial port to have some data. + // + while ((SerialPortReadRegister (BaseAddress, R_UART_LSR, UseMmio, St= ride) & B_UART_LSR_RXRDY) =3D=3D 0) { + if (PcdGetBool (PcdSerialUseHardwareFlowControl)) { + // + // Set RTS to let the peer send some data + // + SerialPortWriteRegister (BaseAddress, R_UART_MCR, (UINT8)(Mcr | = B_UART_MCR_RTS), UseMmio, Stride); + } + } + + if (PcdGetBool (PcdSerialUseHardwareFlowControl)) { + // + // Clear RTS to prevent peer from sending data + // + SerialPortWriteRegister (BaseAddress, R_UART_MCR, Mcr, UseMmio, St= ride); + } + + // + // Read byte from the receive buffer. + // + *DataBuffer =3D SerialPortReadRegister (BaseAddress, R_UART_RXBUF, U= seMmio, Stride); + } + Count++; + } + + return Result; +} + +/** + Polls a serial device to see if there is any data waiting to be read. + + Polls a serial 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 + ) +{ + UINTN BaseAddress; + BOOLEAN UseMmio; + UINT8 Stride; + UINT8 Count; + BOOLEAN Status; + + Count =3D 0; + Status =3D FALSE; + while (Count < mUartCount) { + BaseAddress =3D mUartInfo[Count].BaseAddress; + UseMmio =3D mUartInfo[Count].UseMmio; + Stride =3D mUartInfo[Count].RegisterStride; + + if (BaseAddress =3D=3D 0) { + Count++; + continue; + } + + Status =3D FALSE; + + // + // Read the serial port status + // + if ((SerialPortReadRegister (BaseAddress, R_UART_LSR, UseMmio, Stride)= & B_UART_LSR_RXRDY) !=3D 0) { + if (PcdGetBool (PcdSerialUseHardwareFlowControl)) { + // + // Clear RTS to prevent peer from sending data + // + SerialPortWriteRegister (BaseAddress, R_UART_MCR, (UINT8)(SerialPo= rtReadRegister (BaseAddress, R_UART_MCR, UseMmio, Stride) & ~B_UART_MCR_RTS= ), UseMmio, Stride); + } + Status =3D TRUE; + } + + if (PcdGetBool (PcdSerialUseHardwareFlowControl)) { + // + // Set RTS to let the peer send some data + // + SerialPortWriteRegister (BaseAddress, R_UART_MCR, (UINT8)(SerialPort= ReadRegister (BaseAddress, R_UART_MCR, UseMmio, Stride) | B_UART_MCR_RTS), = UseMmio, Stride); + } + Count++; + } + return Status; +} + +/** + Sets the control bits on a serial device. + + @param Control Sets the bits of Control that are settable= . + + @retval RETURN_SUCCESS The new control bits were set on the seria= l device. + +**/ +RETURN_STATUS +EFIAPI +SerialPortSetControl ( + IN UINT32 Control + ) +{ + UINTN BaseAddress; + BOOLEAN UseMmio; + UINT8 Mcr; + UINT8 Count; + UINT8 Stride; + + Count =3D 0; + while (Count < mUartCount) { + BaseAddress =3D mUartInfo[Count].BaseAddress; + UseMmio =3D mUartInfo[Count].UseMmio; + Stride =3D mUartInfo[Count].RegisterStride; + + if (BaseAddress =3D=3D 0) { + Count++; + continue; + } + + // + // First determine the parameter is invalid. + // + if ((Control & (~(EFI_SERIAL_REQUEST_TO_SEND | EFI_SERIAL_DATA_TERMINA= L_READY | + EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE))) !=3D 0) + { + Count++; + continue; + } + + // + // Read the Modem Control Register. + // + Mcr =3D SerialPortReadRegister (BaseAddress, R_UART_MCR, UseMmio, Str= ide); + Mcr &=3D (~(B_UART_MCR_DTRC | B_UART_MCR_RTS)); + + if ((Control & EFI_SERIAL_DATA_TERMINAL_READY) =3D=3D EFI_SERIAL_DATA_= TERMINAL_READY) { + Mcr |=3D B_UART_MCR_DTRC; + } + + if ((Control & EFI_SERIAL_REQUEST_TO_SEND) =3D=3D EFI_SERIAL_REQUEST_T= O_SEND) { + Mcr |=3D B_UART_MCR_RTS; + } + + // + // Write the Modem Control Register. + // + SerialPortWriteRegister (BaseAddress, R_UART_MCR, Mcr, UseMmio, Stride= ); + Count++; + } + + return RETURN_SUCCESS; +} + +/** + Retrieve the status of the control bits on a serial device. + + @param Control A pointer to return the current control si= gnals from the serial device. + + @retval RETURN_SUCCESS The control bits were read from the serial= device. + +**/ +RETURN_STATUS +EFIAPI +SerialPortGetControl ( + OUT UINT32 *Control + ) +{ + UINTN BaseAddress; + BOOLEAN UseMmio; + UINT8 Msr; + UINT8 Mcr; + UINT8 Lsr; + UINT8 Count; + UINT8 Stride; + + Count =3D 0; + while (Count < mUartCount) { + BaseAddress =3D mUartInfo[Count].BaseAddress; + UseMmio =3D mUartInfo[Count].UseMmio; + Stride =3D mUartInfo[Count].RegisterStride; + + if (BaseAddress =3D=3D 0) { + Count++; + continue; + } + + *Control =3D 0; + + // + // Read the Modem Status Register. + // + Msr =3D SerialPortReadRegister (BaseAddress, R_UART_MSR, UseMmio,=20 + Stride); + + if ((Msr & B_UART_MSR_CTS) =3D=3D B_UART_MSR_CTS) { + *Control |=3D EFI_SERIAL_CLEAR_TO_SEND; + } + + if ((Msr & B_UART_MSR_DSR) =3D=3D B_UART_MSR_DSR) { + *Control |=3D EFI_SERIAL_DATA_SET_READY; + } + + if ((Msr & B_UART_MSR_RI) =3D=3D B_UART_MSR_RI) { + *Control |=3D EFI_SERIAL_RING_INDICATE; + } + + if ((Msr & B_UART_MSR_DCD) =3D=3D B_UART_MSR_DCD) { + *Control |=3D EFI_SERIAL_CARRIER_DETECT; + } + + // + // Read the Modem Control Register. + // + Mcr =3D SerialPortReadRegister (BaseAddress, R_UART_MCR, UseMmio,=20 + Stride); + + if ((Mcr & B_UART_MCR_DTRC) =3D=3D B_UART_MCR_DTRC) { + *Control |=3D EFI_SERIAL_DATA_TERMINAL_READY; + } + + if ((Mcr & B_UART_MCR_RTS) =3D=3D B_UART_MCR_RTS) { + *Control |=3D EFI_SERIAL_REQUEST_TO_SEND; + } + + if (PcdGetBool (PcdSerialUseHardwareFlowControl)) { + *Control |=3D EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE; + } + + // + // Read the Line Status Register. + // + Lsr =3D SerialPortReadRegister (BaseAddress, R_UART_LSR, UseMmio,=20 + Stride); + + if ((Lsr & (B_UART_LSR_TEMT | B_UART_LSR_TXRDY)) =3D=3D (B_UART_LSR_TE= MT | B_UART_LSR_TXRDY)) { + *Control |=3D EFI_SERIAL_OUTPUT_BUFFER_EMPTY; + } + + if ((Lsr & B_UART_LSR_RXRDY) =3D=3D 0) { + *Control |=3D EFI_SERIAL_INPUT_BUFFER_EMPTY; + } + Count++; + } + + return RETURN_SUCCESS; +} + +/** + Sets the baud rate, receive FIFO depth, transmit/receice time out,=20 +parity, + data bits, and stop bits on a serial device. + + @param BaudRate The requested baud rate. A BaudRate value of 0= will use the + device's default interface speed. + On output, the value actually set. + @param ReveiveFifoDepth The requested depth of the FIFO on the receive= side of the + serial interface. A ReceiveFifoDepth value of = 0 will use + the device's default FIFO depth. + On output, the value actually set. + @param Timeout The requested time out for a single character = in microseconds. + This timeout applies to both the transmit and = receive side of the + interface. A Timeout value of 0 will use the d= evice's default time + out value. + On output, the value actually set. + @param Parity The type of parity to use on this serial devic= e. A Parity value of + DefaultParity will use the device's default pa= rity value. + On output, the value actually set. + @param DataBits The number of data bits to use on the serial d= evice. A DataBits + vaule of 0 will use the device's default data = bit setting. + On output, the value actually set. + @param StopBits The number of stop bits to use on this serial = device. A StopBits + value of DefaultStopBits will use the device's= default number of + stop bits. + On output, the value actually set. + + @retval RETURN_SUCCESS The new attributes were set on the ser= ial device. + +**/ +RETURN_STATUS +EFIAPI +SerialPortSetAttributes ( + IN OUT UINT64 *BaudRate, + IN OUT UINT32 *ReceiveFifoDepth, + IN OUT UINT32 *Timeout, + IN OUT EFI_PARITY_TYPE *Parity, + IN OUT UINT8 *DataBits, + IN OUT EFI_STOP_BITS_TYPE *StopBits + ) +{ + UINTN BaseAddress; + BOOLEAN UseMmio; + UINT32 SerialBaudRate; + UINTN Divisor; + UINT8 Lcr; + UINT8 LcrData; + UINT8 LcrParity; + UINT8 LcrStop; + UINT8 Count; + UINT8 Stride; + + Count =3D 0; + while (Count < mUartCount) { + BaseAddress =3D mUartInfo[Count].BaseAddress; + UseMmio =3D mUartInfo[Count].UseMmio; + Stride =3D mUartInfo[Count].RegisterStride; + + if (BaseAddress =3D=3D 0) { + Count++; + continue; + } + + // + // Check for default settings and fill in actual values. + // + if (*BaudRate =3D=3D 0) { + *BaudRate =3D mUartInfo[Count].BaudRate; + } + + SerialBaudRate =3D (UINT32)*BaudRate; + + if (*DataBits =3D=3D 0) { + LcrData =3D (UINT8)(PcdGet8 (PcdSerialLineControl) & 0x3); + *DataBits =3D LcrData + 5; + } else { + if ((*DataBits < 5) || (*DataBits > 8)) { + Count++; + continue; + } + + // + // Map 5..8 to 0..3 + // + LcrData =3D (UINT8)(*DataBits - (UINT8)5); + } + + if (*Parity =3D=3D DefaultParity) { + LcrParity =3D (UINT8)((PcdGet8 (PcdSerialLineControl) >> 3) & 0x7); + switch (LcrParity) { + case 0: + *Parity =3D NoParity; + break; + + case 3: + *Parity =3D EvenParity; + break; + + case 1: + *Parity =3D OddParity; + break; + + case 7: + *Parity =3D SpaceParity; + break; + + case 5: + *Parity =3D MarkParity; + break; + + default: + break; + } + } else { + switch (*Parity) { + case NoParity: + LcrParity =3D 0; + break; + + case EvenParity: + LcrParity =3D 3; + break; + + case OddParity: + LcrParity =3D 1; + break; + + case SpaceParity: + LcrParity =3D 7; + break; + + case MarkParity: + LcrParity =3D 5; + break; + + default: + Count++; + continue; + } + } + + if (*StopBits =3D=3D DefaultStopBits) { + LcrStop =3D (UINT8)((PcdGet8 (PcdSerialLineControl) >> 2) & 0x1); + switch (LcrStop) { + case 0: + *StopBits =3D OneStopBit; + break; + + case 1: + if (*DataBits =3D=3D 5) { + *StopBits =3D OneFiveStopBits; + } else { + *StopBits =3D TwoStopBits; + } + + break; + + default: + break; + } + } else { + switch (*StopBits) { + case OneStopBit: + LcrStop =3D 0; + break; + + case OneFiveStopBits: + case TwoStopBits: + LcrStop =3D 1; + break; + + default: + Count++; + continue; + } + } + + // + // Calculate divisor for baud generator + // Ref_Clk_Rate / Baud_Rate / 16 + // + Divisor =3D PcdGet32 (PcdSerialClockRate) / (SerialBaudRate * 16); + if ((PcdGet32 (PcdSerialClockRate) % (SerialBaudRate * 16)) >=3D Seria= lBaudRate * 8) { + Divisor++; + } + + // + // Configure baud rate + // + SerialPortWriteRegister (BaseAddress, R_UART_LCR, B_UART_LCR_DLAB, Use= Mmio, Stride); + SerialPortWriteRegister (BaseAddress, R_UART_BAUD_HIGH, (UINT8)(Diviso= r >> 8), UseMmio, Stride); + SerialPortWriteRegister (BaseAddress, R_UART_BAUD_LOW,=20 + (UINT8)(Divisor & 0xff), UseMmio, Stride); + + // + // Clear DLAB and configure Data Bits, Parity, and Stop Bits. + // Strip reserved bits from line control value + // + Lcr =3D (UINT8)((LcrParity << 3) | (LcrStop << 2) | LcrData); + SerialPortWriteRegister (BaseAddress, R_UART_LCR, (UINT8)(Lcr & 0x3F),= UseMmio, Stride); + Count++; + } + + return RETURN_SUCCESS; +} diff --git a/UefiPayloadPkg/Library/BaseSerialPortLibHob/BaseSerialPortLibH= ob.inf b/UefiPayloadPkg/Library/BaseSerialPortLibHob/BaseSerialPortLibHob.i= nf new file mode 100644 index 0000000000..ac857d3eea --- /dev/null +++ b/UefiPayloadPkg/Library/BaseSerialPortLibHob/BaseSerialPortLibHob.i +++ nf @@ -0,0 +1,39 @@ +## @file +# SerialPortLib instance for UART information retrieved from bootloader. +# +# Copyright (c) 2022, Intel Corporation. All rights reserved.
# # =20 +SPDX-License-Identifier: BSD-2-Clause-Patent # ## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D BaseSerialPortLibHob + FILE_GUID =3D d8d22930-e8ec-469f-8184-5a069149b2ff + MODULE_TYPE =3D BASE + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D SerialPortLib + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + +[LibraryClasses] + PcdLib + IoLib + HobLib + TimerLib + +[Sources] + BaseSerialPortLibHob.c + +[Pcd] + gEfiMdeModulePkgTokenSpaceGuid.PcdSerialLineControl + gEfiMdeModulePkgTokenSpaceGuid.PcdSerialFifoControl + gEfiMdeModulePkgTokenSpaceGuid.PcdSerialClockRate + gEfiMdeModulePkgTokenSpaceGuid.PcdSerialExtendedTxFifoSize + gEfiMdeModulePkgTokenSpaceGuid.PcdSerialUseHardwareFlowControl + +[Guids] + gUniversalPayloadSerialPortInfoGuid -- 2.30.2.windows.1