From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by mx.groups.io with SMTP id smtpd.web08.43689.1658252616178897430 for ; Tue, 19 Jul 2022 10:43:36 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=ZECC///r; spf=pass (domain: intel.com, ip: 134.134.136.100, mailfrom: ashraf.ali.s@intel.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1658252616; x=1689788616; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=jK5bx/e/VKQ9tMDpPcB+UgyZjf0dmo1n9NKUxzE4PMo=; b=ZECC///rLIcGyoeYU8mLXcL4cJa1wG069RZjAl5qCcChC3MpYs8B5r0m G/+C7jK2hib406T/mUwIFzvfxQfvHmovSRbOmBelMXgEDtS4diwpeT5bD L2NyYIesDBAY5v1npaoI+5aviEGiYn+GE+Q01Jg5VL/I+Ms9XuJAMnXdg YbCUCaT5SieX8DM+gjjHzI4hEVCvyiEdI8JfaZBgA7Lxy1FZ1twd1jT6J w2ZUidiCnkyz4n/cfeqB2GJMXKHpkpQ0LfyxaUR+ygFemAe8WKpJFv8sR +o/x7Wxj+zOfINvrzCKw7jdyNzlzQGoPhGjPRg4AAaIIVv2Eb+D51vjdK A==; X-IronPort-AV: E=McAfee;i="6400,9594,10413"; a="350520853" X-IronPort-AV: E=Sophos;i="5.92,284,1650956400"; d="scan'208";a="350520853" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Jul 2022 10:43:35 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.92,284,1650956400"; d="scan'208";a="843735368" Received: from orsmsx601.amr.corp.intel.com ([10.22.229.14]) by fmsmga006.fm.intel.com with ESMTP; 19 Jul 2022 10:43:34 -0700 Received: from orsmsx612.amr.corp.intel.com (10.22.229.25) by ORSMSX601.amr.corp.intel.com (10.22.229.14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.28; Tue, 19 Jul 2022 10:43:34 -0700 Received: from orsmsx602.amr.corp.intel.com (10.22.229.15) by ORSMSX612.amr.corp.intel.com (10.22.229.25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.27; Tue, 19 Jul 2022 10:43: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.28 via Frontend Transport; Tue, 19 Jul 2022 10:43:33 -0700 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (104.47.59.172) 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.2308.27; Tue, 19 Jul 2022 10:43:32 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YJEJHK6A97aTAbVxzPw9fvzqecKPvg0qnFmQabl5aQy3+CvxnTxqCs8QMQNmykXoYzwA/rTGVUCv0hJxnw4u8MH3BmjL5SH5SI5vFhh0JY8yUXQfiD27f6LTae2nPPyXMRpSVQd7Jq8DEdZ82C+H4P4FFOqNMPDnoPkmLHOfHM6nlooGo2OaezAQKUxWxt7zsLFSjq7Zh38YfEcUd7vWgSPmcYsK/+gHbbCru7TNTcZwASHcoFOgHF2R+QrAUAj/smTGUpGkq3aYM46998UpBYzZbEuKJhRF1TC1dZ1vCb449RbLHTytRT6XOEZcUnjZgitk7QwzTr2J05vcvYrDHQ== 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=r+adM/i2FRzxoIFSvbAYqBjIyU1UReyN1bmgBpiOUYU=; b=JgsvO4ermfuLHc0mLE7ptrFdUFExtkr7vj2z9HOK/fnooNbXtB1oj6CThIxcwxl8NWitx3mEh45q/o9VdqHoS0sVZCXu0KRG5A3d/2c/6s5V5EA72OJ8CERg0hfK/hkuJ7qDo9jp/Uq5NYZudlrj0Bglq5QZ8X5XCjhObvQWqB21fArxf91jEfdA2fwNZd1hsF3chFfz+eOwJojD6Lb2TB12q6lh9z3UmS1DwAFi0R6NbgLVOQjNLm6fTyltD7wRXDpLSB7rnlU68OR6X645egOWtkRprbEm72Ja1Je25TPXnkka9tHvDrBWmij2kt8lUTSe9cCxZFLmXi3ZEGNiKg== 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 BN9PR11MB5275.namprd11.prod.outlook.com (2603:10b6:408:134::24) by SA2PR11MB5017.namprd11.prod.outlook.com (2603:10b6:806:11e::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5438.19; Tue, 19 Jul 2022 17:43:29 +0000 Received: from BN9PR11MB5275.namprd11.prod.outlook.com ([fe80::1d86:8e1b:69ee:930f]) by BN9PR11MB5275.namprd11.prod.outlook.com ([fe80::1d86:8e1b:69ee:930f%6]) with mapi id 15.20.5438.023; Tue, 19 Jul 2022 17:43:29 +0000 From: "Ashraf Ali S" To: "devel@edk2.groups.io" , "Sravanthi, K KavyaX" CC: "Dong, Guo" , "Ni, Ray" , Maurice Ma , "You, Benjamin" , "Rhodes, Sean" Subject: Re: [edk2-devel] [PATCH v2 1/2] UefiPayloadPkg: Implement a new SerialPortLib instance Thread-Topic: [edk2-devel] [PATCH v2 1/2] UefiPayloadPkg: Implement a new SerialPortLib instance Thread-Index: AQHYmz46gy2MYSOL20u8SXpD08DYv62F9qxw Date: Tue, 19 Jul 2022 17:43:29 +0000 Message-ID: References: <20220719070702.1282-1-k.kavyax.sravanthi@intel.com> In-Reply-To: <20220719070702.1282-1-k.kavyax.sravanthi@intel.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: 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: 6396a35a-9153-4fab-2f7f-08da69ae3131 x-ms-traffictypediagnostic: SA2PR11MB5017: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: Zf+EM+kbw08aD2QcLOhl0rAr1Zw50ExjVEqaPxZsZ2Od1IvNVBeLbrPtxSiVfPUmTU+S1uQzOHF5WE4bJyMd1+SUoeEck2Wys8ayqb8tM/Ev9v0OmlXMsyzqqqBCPy2H7AlSZPVpmt+W6+4j9MF/2q1BgnbCfK4E41ofsJCWGZ4LX5cJGQfA3EiE3NwxBTeoSmYXV4QHZ6mJwBxHIbx04EVoh2afkW13Kt41s3UcuONMDtDRm3aozRkzL/0Wth4/41L4yFMM8QCZ0e/B7NjEmfNnuX+wdpPdqNDY+DVzWaUHv3TUmT+tphYPsodkc4QwqnvGdpGUbDPDU7AzyouIoTVwAIe0hpaRBFs0gRpAiM5msOUTsVBysfvlaw8Cgoepih+E+ocXyXPKgw0mI8d90iCeO4pri10uZywT082bVudGad5MN3qBe6TspuIXM8XBzbobaVVE3N+VuzmEDPEaKDWDMxsEsVX3ffVd8mSLI+HutEX7lDZPFYuNC0DgnSGNRZRqIGm40l0w9Y0/nScCZUlRApf1DE4/qaCPPkmHWyvVZb+ILRWgvLylhhdeNgdXTjMhNNBM1s+2suR/UG/Gfq9tMw2Em3DmHzzo7Or1ZvZB+h9umaWSTUmFcKUn6Wv2FCbRC9VMXOvl6AyMcksgnbPEYWqDhyg5xNjee5AyAhWSu0WiKbxeNoX12oOBSFjw9FhEupznMosZF7sxSfqI+LFyom+NDCaHDo9EqB4A5e5+A7zeo2Jzo7Oj3T8XclUOkf9vM2tjMTrAVMPy9qA4PdvKjasdsOQqX6Ox2hBAVyi4dtNIkbSNac8UxRZ9c0LQSC5cHDr1jHpZFsqnTOxRZwb+qGHxBqu6vCOhWmYeT/2Ct42L1v06GcFsbXLsVGVT x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BN9PR11MB5275.namprd11.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230016)(136003)(39860400002)(366004)(376002)(346002)(396003)(122000001)(316002)(54906003)(9686003)(110136005)(86362001)(6506007)(7696005)(6636002)(41300700001)(26005)(2906002)(38070700005)(71200400001)(33656002)(966005)(4326008)(38100700002)(5660300002)(8676002)(82960400001)(66446008)(64756008)(55016003)(66556008)(66476007)(66946007)(52536014)(83380400001)(478600001)(53546011)(8936002)(186003)(76116006)(30864003)(559001)(579004);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?Q?npNDDDuZms6ZcT/GzY2ssNDwV8Fpr0X60gaYlH4mu7OFYRwhBLS0Qw3Cf2?= =?iso-8859-1?Q?vDEwuuia7nyQrJnRV5t3/1QnW+OAMXxLxOZOGWXO4ytzWOWRdc0yw8hndD?= =?iso-8859-1?Q?ALzgTP3ZQ4y4IiF0xXRGGFHg68udemeeoP1/M1W11g4E+pWgln8kMC8CW2?= =?iso-8859-1?Q?fb2IrbfbNIOpdHg9JPwOYaVxNl62N/gqNv+plYLngGD3C/NKHoggUh2bQJ?= =?iso-8859-1?Q?uVPD0nPELT4d4L5ncviwfjvdbm0Ggj+JVoF5PSOX/CXbFvUSrccughpVBr?= =?iso-8859-1?Q?f3vKaL8R+iqonFa2Z4TeeFEcjPmFPHkilS/l3I4q/lQLWavd7BGSh8bne5?= =?iso-8859-1?Q?8DE3bav2QLomaIXRE+0+uDJ3l/NP/qCwfqm6fq1pLyKRX6toPIJAh4vq5Z?= =?iso-8859-1?Q?r1Qm8lvnRaPdkePw60VekIvll4YIvizP4hQrvFGJhIYrmu7HMQkbc8NEJ/?= =?iso-8859-1?Q?u4CFcxXCEPhafcxxokke2QszaxmadL2DAoGOztJAkWIvrSCssJlB5B9tRG?= =?iso-8859-1?Q?uRVJ2Pc4MSMvwsYGfSA/nPdtWEb0H41fWZ/HKaUY40fV7m9mAOkQT9ifHO?= =?iso-8859-1?Q?kaWlGmsedZFQCX6awQ+OO873EUBYiBZyrGkrWRBk8LrEQqc0PCvJNxW6bi?= =?iso-8859-1?Q?xxElLzu21KeW6mmNg0ea6LUKE4ebDeT20Tb9bMLRoHvRNJSg0MU3TmD2Io?= =?iso-8859-1?Q?pmSWgq9YnIclXbMh4S27O16CaWGDXofVkVs3oXc6Euples/AYSafLlilGs?= =?iso-8859-1?Q?fFeW9QE+aDQEOogx0/HwzNi4+y8y3nQNVrafO6SEkuL9Id4IyfD2w1PFGd?= =?iso-8859-1?Q?m4jO4Jf7UqVfflENWDJ8tnMSUwelvdOPOALxd4R6SU0crOGrSoAGi+yiW3?= =?iso-8859-1?Q?MHWTdgQLqa9qI9lslKIrO+H0WglhZjTJ8ftuvv1hQuj25Ug2Ym2T4AtCSt?= =?iso-8859-1?Q?SkS+nZ5c9VdRGkj/cU3r/CssvHV5FK5jrut+ETRlj56DuRu6WVoQfu4l9i?= =?iso-8859-1?Q?X2/3+nh5/f+Wel1hgkLkyhAZ2A66fc0Ny/AyNHQURAwzitKaYAjXDcwvuT?= =?iso-8859-1?Q?hlFJGBJg0wwLNevH0BCP2v8O2M43fIfhswUIMWNovm1Pcpcz6URDRg/w1A?= =?iso-8859-1?Q?KbymgcsAEvQsTxnpCunKkuqKLo8B85tC4CRPesMJdvx86SVJTSFPfvn4JF?= =?iso-8859-1?Q?OOlqKXPmSQ5W6TmH9xZmF+IpVWsmIxbfM10SbmaV4CAGFWpUn0lc8MTPb+?= =?iso-8859-1?Q?MWpmGZZtfL4OIkHH70Zqpn2NaM9SgOiMvbnqxQMGCRYdoKAqQ6+e40aVCM?= =?iso-8859-1?Q?JFsj4fn0eVChWpdIDYE9FXUWlngBYz8Rto15If2rsTwv0qMmVQwclYOAh9?= =?iso-8859-1?Q?O/LelVmhEAvWqBmJJczsNrzaV3N109PokObRgdYLX69imLt4q11JehCI+O?= =?iso-8859-1?Q?veCEXXhvIOQKevq7HIKzGAS4pPaVa7DiqCgdxM94/shCpVA/+/vPdb1usY?= =?iso-8859-1?Q?vZOH+U9K4WbutJCoIjjJVia3C8agyJuYxs7KA++Phu5JNCsFRGp3YECdM6?= =?iso-8859-1?Q?U/5ZJxNeL7yyDgIPNM6BfjbPP6kJFs+BSVcOT5+E+kGjCDOwawWayEaaHl?= =?iso-8859-1?Q?p5vNneIKBRc2fnwyxWUWZiR0nt9zFEHbBB?= MIME-Version: 1.0 X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: BN9PR11MB5275.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6396a35a-9153-4fab-2f7f-08da69ae3131 X-MS-Exchange-CrossTenant-originalarrivaltime: 19 Jul 2022 17:43:29.6723 (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: FvZJpQ3k9MXFpHGzTUVA6X1iBHruRovuoz5P2Kk5BHl/E1LI9sv60G9HLSzhvp3/FLm36v7+/Zb0lNHSzqy0rA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA2PR11MB5017 Return-Path: ashraf.ali.s@intel.com X-OriginatorOrg: intel.com Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi, For the register offset doing the division operation of having a size UINTN= in below code, I think we should use DivU64x32 function for Division operation right, corr= ect me if I'm wrong. Regards, Ashraf Ali S Intel Technology India Pvt. Ltd.=A0 -----Original Message----- From: devel@edk2.groups.io On Behalf Of kavya Sent: Tuesday, July 19, 2022 12:37 PM To: devel@edk2.groups.io Cc: Sravanthi, K KavyaX ; Dong, Guo ; Ni, Ray ; Maurice Ma ;= You, Benjamin ; Rhodes, Sean Subject: [edk2-devel] [PATCH v2 1/2] UefiPayloadPkg: Implement a new Serial= PortLib instance 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: Maurice Ma Cc: Benjamin You Cc: Sean Rhodes Signed-off-by: Kavya --- UefiPayloadPkg/Library/BaseSerialPortLibHob/BaseSerialPortLibHob.c | 794= ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= +++++++++++++++++++++++++++++++++++++++++++++ UefiPayloadPkg/Library/BaseSerialPortLibHob/BaseSerialPortLibHob.inf | 40= ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 834 insertions(+) diff --git a/UefiPayloadPkg/Library/BaseSerialPortLibHob/BaseSerialPortLibH= ob.c b/UefiPayloadPkg/Library/BaseSerialPortLibHob/BaseSerialPortLibHob.c new file mode 100644 index 0000000000..8c39befafe --- /dev/null +++ b/UefiPayloadPkg/Library/BaseSerialPortLibHob/BaseSerialPortLibHob.c @@ -0,0 +1,794 @@ +/** @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 =20 +#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 + +typedef struct { + UINTN BaseAddress; + BOOLEAN UseMmio; + UINT32 BaudRate; +} UART_INFO; + +UART_INFO **mUartInfo; +UINT8 mUartCount; + +/** + 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 or IO spa= ce. + + @return The value read from the register. + +**/ +UINT8 +SerialPortReadRegister ( + UINTN Base, + UINTN Offset, + BOOLEAN UseMmio + ) +{ + if (UseMmio) { + return MmioRead8 (Base + Offset/4); + } else { + return IoRead8 (Base + Offset); + } +} + +/** + 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 sp= ace. + + @return The value written to the register. + +**/ +UINT8 +SerialPortWriteRegister ( + UINTN Base, + UINTN Offset, + UINT8 Value, + BOOLEAN UseMmio + ) +{ + if (UseMmio) { + return MmioWrite8 (Base + Offset/4, Value); + } else { + return IoWrite8 (Base + Offset, 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. + 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 + ) +{ + UNIVERSAL_PAYLOAD_SERIAL_PORT_INFO *SerialPortInfo; + EFI_HOB_GUID_TYPE *GuidHob; + UINTN SerialRegisterBase; + UINT32 Divisor; + UINT32 CurrentDivisor; + UINT32 BaudRate; + BOOLEAN Initialized; + BOOLEAN MmioEnable; + UINT8 Index; + UINT8 Count; + + Count =3D 0; + + GuidHob =3D GetFirstGuidHob (&gUniversalPayloadSerialPortInfoGuid); + while (GuidHob !=3D NULL) { + Count++; + GuidHob =3D GET_NEXT_HOB (GuidHob); + GuidHob =3D GetNextGuidHob (&gUniversalPayloadSerialPortInfoGuid,=20 + GuidHob); } + + mUartCount =3D Count; + + mUartInfo =3D (UART_INFO **)AllocateZeroPool (sizeof (UART_INFO *) *=20 + Count); ASSERT (mUartInfo !=3D NULL); + + for (Index =3D 0; Index < Count; Index++) { + mUartInfo[Index] =3D (UART_INFO *)AllocateZeroPool (sizeof=20 + (UART_INFO)); } + + Index =3D 0; + 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; + + mUartInfo[Index]->BaseAddress =3D SerialRegisterBase; + mUartInfo[Index]->UseMmio =3D MmioEnable; + mUartInfo[Index]->BaudRate =3D BaudRate; + Index++; + + if (SerialRegisterBase =3D=3D 0) { + return RETURN_DEVICE_ERROR; + } + + 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) & 0x3F) !=3D (PcdGet8 (PcdSerialLineControl) & 0x3F)) { + Initialized =3D FALSE; + } + + SerialPortWriteRegister (SerialRegisterBase, R_UART_LCR, (UINT8)(Seria= lPortReadRegister (SerialRegisterBase, R_UART_LCR, MmioEnable) | B_UART_LCR= _DLAB), MmioEnable); + CurrentDivisor =3D SerialPortReadRegister (SerialRegisterBase, R_UAR= T_BAUD_HIGH, MmioEnable) << 8; + CurrentDivisor |=3D (UINT32)SerialPortReadRegister (SerialRegisterBase= , R_UART_BAUD_LOW, MmioEnable); + SerialPortWriteRegister (SerialRegisterBase, R_UART_LCR, (UINT8)(Seria= lPortReadRegister (SerialRegisterBase, R_UART_LCR, MmioEnable) & ~B_UART_LC= R_DLAB), MmioEnable); + 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); + SerialPortWriteRegister (SerialRegisterBase, R_UART_BAUD_HIGH, (UINT8)= (Divisor >> 8), MmioEnable); + SerialPortWriteRegister (SerialRegisterBase, R_UART_BAUD_LOW,=20 + (UINT8)(Divisor & 0xff), MmioEnable); + + // + // 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); + + // + // Enable and reset FIFOs + // Strip reserved bits from PcdSerialFifoControl + // + SerialPortWriteRegister (SerialRegisterBase, R_UART_FCR, 0x00, MmioEna= ble); + SerialPortWriteRegister (SerialRegisterBase, R_UART_FCR,=20 + (UINT8)(PcdGet8 (PcdSerialFifoControl) & (B_UART_FCR_FIFOE |=20 + B_UART_FCR_FIFO64)), MmioEnable); + + // + // Set FIFO Polled Mode by clearing IER after setting FCR + // + SerialPortWriteRegister (SerialRegisterBase, R_UART_IER, 0x00,=20 + MmioEnable); + + // + // Put Modem Control Register(MCR) into its reset state of 0x00. + // + SerialPortWriteRegister (SerialRegisterBase, R_UART_MCR, 0x00,=20 + MmioEnable); + + 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; + + if (Buffer =3D=3D NULL || NumberOfBytes =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; + + 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); + } + 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; + + if (Buffer =3D=3D NULL) { + return 0; + } + + Count =3D 0; + while (Count < mUartCount) { + BaseAddress =3D mUartInfo[Count]->BaseAddress; + UseMmio =3D mUartInfo[Count]->UseMmio; + + DataBuffer =3D Buffer; + BytesLeft =3D NumberOfBytes; + + if (BaseAddress =3D=3D 0) { + return 0; + } + + Mcr =3D (UINT8)(SerialPortReadRegister (BaseAddress, R_UART_MCR,=20 + UseMmio) & ~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) & = 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); + } + } + + if (PcdGetBool (PcdSerialUseHardwareFlowControl)) { + // + // Clear RTS to prevent peer from sending data + // + SerialPortWriteRegister (BaseAddress, R_UART_MCR, Mcr, UseMmio); + } + + // + // Read byte from the receive buffer. + // + *DataBuffer =3D SerialPortReadRegister (BaseAddress, R_UART_RXBUF, U= seMmio); + } + 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 Count; + + Count =3D 0; + while (Count < mUartCount) { + BaseAddress =3D mUartInfo[Count]->BaseAddress; + UseMmio =3D mUartInfo[Count]->UseMmio; + + if (BaseAddress =3D=3D 0) { + return FALSE; + } + + // + // Read the serial port status + // + if ((SerialPortReadRegister (BaseAddress, R_UART_LSR, UseMmio) & B_UAR= T_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) & ~B_UART_MCR_RTS), UseMm= io); + } + return 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) | B_UART_MCR_RTS), UseMmio)= ; + } + Count++; + } + return FALSE; +} + +/** + 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. + @retval RETURN_UNSUPPORTED The serial device does not support this op= eration. + @retval RETURN_DEVICE_ERROR The serial device is not functioning corre= ctly. + +**/ +RETURN_STATUS +EFIAPI +SerialPortSetControl ( + IN UINT32 Control + ) +{ + UINTN BaseAddress; + BOOLEAN UseMmio; + UINT8 Mcr; + UINT8 Count; + + Count =3D 0; + while (Count < mUartCount) { + BaseAddress =3D mUartInfo[Count]->BaseAddress; + UseMmio =3D mUartInfo[Count]->UseMmio; + + if (BaseAddress =3D=3D 0) { + return RETURN_UNSUPPORTED; + } + + // + // 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) + { + return RETURN_UNSUPPORTED; + } + + // + // Read the Modem Control Register. + // + Mcr =3D SerialPortReadRegister (BaseAddress, R_UART_MCR, UseMmio); + 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); + 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. + @retval RETURN_UNSUPPORTED The serial device does not support this op= eration. + @retval RETURN_DEVICE_ERROR The serial device is not functioning corre= ctly. + +**/ +RETURN_STATUS +EFIAPI +SerialPortGetControl ( + OUT UINT32 *Control + ) +{ + UINTN BaseAddress; + BOOLEAN UseMmio; + UINT8 Msr; + UINT8 Mcr; + UINT8 Lsr; + UINT8 Count; + + Count =3D 0; + while (Count < mUartCount) { + BaseAddress =3D mUartInfo[Count]->BaseAddress; + UseMmio =3D mUartInfo[Count]->UseMmio; + + if (BaseAddress =3D=3D 0) { + return RETURN_UNSUPPORTED; + } + + *Control =3D 0; + + // + // Read the Modem Status Register. + // + Msr =3D SerialPortReadRegister (BaseAddress, R_UART_MSR, UseMmio); + + 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); + + 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); + + 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. + @retval RETURN_UNSUPPORTED The serial device does not support thi= s operation. + @retval RETURN_INVALID_PARAMETER One or more of the attributes has an u= nsupported value. + @retval RETURN_DEVICE_ERROR The serial device is not functioning c= orrectly. + +**/ +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; + + Count =3D 0; + while (Count < mUartCount) { + BaseAddress =3D mUartInfo[Count]->BaseAddress; + UseMmio =3D mUartInfo[Count]->UseMmio; + + if (BaseAddress =3D=3D 0) { + return RETURN_UNSUPPORTED; + } + + // + // 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)) { + return RETURN_INVALID_PARAMETER; + } + + // + // 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: + return RETURN_INVALID_PARAMETER; + } + } + + 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: + return RETURN_INVALID_PARAMETER; + } + } + + // + // 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); + SerialPortWriteRegister (BaseAddress, R_UART_BAUD_HIGH, (UINT8)(Diviso= r >> 8), UseMmio); + SerialPortWriteRegister (BaseAddress, R_UART_BAUD_LOW,=20 + (UINT8)(Divisor & 0xff), UseMmio); + + // + // 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); + 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..1dbe107dbd --- /dev/null +++ b/UefiPayloadPkg/Library/BaseSerialPortLibHob/BaseSerialPortLibHob.i +++ nf @@ -0,0 +1,40 @@ +## @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 + MemoryAllocationLib + +[Sources] + BaseSerialPortLibHob.c + +[Pcd] + gEfiMdeModulePkgTokenSpaceGuid.PcdSerialLineControl + gEfiMdeModulePkgTokenSpaceGuid.PcdSerialFifoControl + gEfiMdeModulePkgTokenSpaceGuid.PcdSerialClockRate + gEfiMdeModulePkgTokenSpaceGuid.PcdSerialExtendedTxFifoSize + gEfiMdeModulePkgTokenSpaceGuid.PcdSerialUseHardwareFlowControl + +[Guids] + gUniversalPayloadSerialPortInfoGuid -- 2.30.2.windows.1