From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail05.groups.io (mail05.groups.io [45.79.224.7]) by spool.mail.gandi.net (Postfix) with ESMTPS id 08EE1AC0F6D for ; Thu, 1 Aug 2024 09:39:06 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=fJjg3XfktydcfUtWv8gH7vR3PnHdZnifbBtye8UowlI=; c=relaxed/simple; d=groups.io; h=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:Resent-Date:Resent-From:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding:Content-Type; s=20240206; t=1722505146; v=1; b=2usdFFrDSqPgoY4kQOrVOCGIWSnKwXFP9FiS+UfEK3tKSAeTuxBje5fuE22xKSho6dF9yr2R l3K2hYoTjQ+0pin8UX7Q8D4nT6akaC/ByFGKLZnMPOP3X4l7AKygg2ueOmMvV85YIAJGJVkKhgk +eITSrF1TzLHrUKpltnJksMH+DAhx50czhefrO3/6xJnxjZx2s1ttbNY8/Rdpre63EEMSOJD2N9 YL/rQr/XD5+WXpxJiAmDNiXWia6txc+LyBCaTIe9CD5lwlcnhPNtkGjMu0STERCjLnYRWCJh9LJ t0mIU9Oq8YzTLmiCR5AVuY010kyDK/sWHl8rttb4DGyyw== X-Received: by 127.0.0.2 with SMTP id HV00YY7687511xnT5JuXPFop; Thu, 01 Aug 2024 02:39:05 -0700 X-Received: from CY4PR05CU001.outbound.protection.outlook.com (CY4PR05CU001.outbound.protection.outlook.com [40.93.198.132]) by mx.groups.io with SMTP id smtpd.web11.64315.1722505144954646770 for ; Thu, 01 Aug 2024 02:39:05 -0700 X-Received: from PH0PR01MB7287.prod.exchangelabs.com (2603:10b6:510:10a::21) by PH7PR01MB7959.prod.exchangelabs.com (2603:10b6:510:277::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7828.22; Thu, 1 Aug 2024 09:39:02 +0000 X-Received: from PH0PR01MB7287.prod.exchangelabs.com ([fe80::fc79:e629:93aa:8b8f]) by PH0PR01MB7287.prod.exchangelabs.com ([fe80::fc79:e629:93aa:8b8f%3]) with mapi id 15.20.7807.026; Thu, 1 Aug 2024 09:39:02 +0000 From: "Nhi Pham via groups.io" To: devel@edk2.groups.io CC: quic_llindhol@quicinc.com, chuong@os.amperecomputing.com, rebecca@os.amperecomputing.com, nhi@os.amperecomputing.com Subject: [edk2-devel] [edk2-platforms][PATCH 1/5] AmpereAltraPkg/DwI2cLib: Add SmbusRead() function Date: Thu, 1 Aug 2024 16:36:14 +0700 Message-ID: <20240801093618.191274-2-nhi@os.amperecomputing.com> In-Reply-To: <20240801093618.191274-1-nhi@os.amperecomputing.com> References: <20240801093618.191274-1-nhi@os.amperecomputing.com> X-ClientProxiedBy: SI2PR01CA0051.apcprd01.prod.exchangelabs.com (2603:1096:4:193::6) To PH0PR01MB7287.prod.exchangelabs.com (2603:10b6:510:10a::21) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PH0PR01MB7287:EE_|PH7PR01MB7959:EE_ X-MS-Office365-Filtering-Correlation-Id: d7b2c287-2650-4153-fcd4-08dcb20dc71e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?tZLStZXTsRZjnR77YuGmwKGh7UVp2/kb5sdfnLg5GNfjOQq9tejFTnp9UyVN?= =?us-ascii?Q?FIflAEh+eM8h/FSt4UVadRM7WwxQ9kicFU9nIk/eAhNsASyqkZtQc9saDPT4?= =?us-ascii?Q?7UPcmEztTui+8Q/870g3ctXtZg702N7XY+QIqLTvSEOWy2n0icA1xR/w3Nw1?= =?us-ascii?Q?I4pfdoIAA+ew/LhWzX5zMuDZKIVngvqjzEP9pijQFI+5hdGgUUKywJXt/9OO?= =?us-ascii?Q?J96HWez9i6F3J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?= =?us-ascii?Q?ue4hCrMLQ54fZXS5WC77c0xOKz/QfR+zwzmL6546Tf19qiWcPqXFyQu+R8Fm?= =?us-ascii?Q?YBx/DhPUGM4vMUR0w6LucvES+Gy5rhKGbqcBM1Jqbk/Ic6FeqmKPQXDMZnwO?= =?us-ascii?Q?QXH85lIEqU4WGH6hDCPpSLEo03IyUCooTYfR2YOXRETGmClsioZ2ok8LXwSc?= =?us-ascii?Q?KJdJwU0p4ibejO8btvtFNfLCKH9YInhHwHtoYZySukxPU1xuOIr3BiXHeBds?= =?us-ascii?Q?beSw4WrAUStHm0HQ23NKNS55h0Wp6A6JQjqRHroFmHL/XCcr9g=3D=3D?= X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?YtRHRKcC6UiTHQ3BwKCOFWkzoQfLkwRyPdc5eeqa+G3hw+A44xudypHAUePN?= =?us-ascii?Q?eMsJEWaYSZZrkztGU+fiDB0kWT4hepp4/moUOkPtXAb0jj1S2DBXPkzEcsq6?= =?us-ascii?Q?ah58RWE9PTu1fq1FEK/GfKeJ4ZHApliP3NKz/BD73tk/JX4k4+bR5aCJgimw?= =?us-ascii?Q?0rqdkjgUvGUvSTNA4XPnConECAy/8qxuH6wJb3seVYaxIhzhE0uiPR80e+8H?= =?us-ascii?Q?F+cNcNQ3YjMFOmHWS/CK7f0Wy/zcO+vsoyHH00aw7EWS/Y1LRblgOvld9Jqv?= =?us-ascii?Q?ODWBD7sv0QolevA72nn3pvsT1dZb8pFxMBqFclhBEI2So/h95nsA9ZvmpYPh?= =?us-ascii?Q?1wFnShGIvlaP9j/ybiPyfkWE84EIAyAjDEfHTupwiyuIUGyoNBS7lWtmobXy?= =?us-ascii?Q?x6OfkceU0YuKJ0DfU0rqIahZHB9esfpmMlIl5ugQ3G+I2RxesfqNIZQg8/Cs?= =?us-ascii?Q?F7Ayu+SKPjZOB0b3xICLkOgfo4RznyaG8R3c/j8lbDBysy2Ut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8CQyMHjMLpI6yD7fab1lVwxkvw0iZ34gjBzb?= =?us-ascii?Q?XLRK0ad+RJDT233QVr3UlQip8cUtb9SK+Rz2PzG2uNixOGgLHm0xqcaZhtmH?= =?us-ascii?Q?+vYhZGqOaTk/K6OrpkrVSpbWyZaj4vg/9XXrwNSieVNY2h3AlVEhCSV+9I/1?= =?us-ascii?Q?Ac7ko1FuAfJdxQ20v6vHRkt15rntglNxd+oZl9e5YrPz0YcetLTWT++aIidJ?= =?us-ascii?Q?jey4BuBwEHb/JPWp2xIdJLQ=3D?= X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: d7b2c287-2650-4153-fcd4-08dcb20dc71e X-MS-Exchange-CrossTenant-AuthSource: PH0PR01MB7287.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Aug 2024 09:39:02.5626 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3bc2b170-fd94-476d-b0ce-4229bdc904a7 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: nNK5nMRnp0YsYMHQ/At+4YQamyKLXMxLBQmyg0ph6PfFzGEUzrc8xKgJsWj1PiYqo+iVaLlqhU6PNLN7dyinir2R0fr9kpMot+ZPOM+TcTw= X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR01MB7959 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 Resent-Date: Thu, 01 Aug 2024 02:39:05 -0700 Resent-From: nhi@os.amperecomputing.com Reply-To: devel@edk2.groups.io,nhi@os.amperecomputing.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: T4PrNJJwl4hGOgncAVYmlbabx7686176AA= Content-Transfer-Encoding: quoted-printable Content-Type: text/plain X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b=2usdFFrD; dmarc=pass (policy=none) header.from=groups.io; spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 45.79.224.7 as permitted sender) smtp.mailfrom=bounce@groups.io This adds the SmbusRead() function designed for SMBUS transaction to support the extraction of the data lenth byte from the initial byte of the SMBUS Block Read, allowing the I2C master to accurately read the SMBUS response with the exact data length. This addresses the issue where the SmbusLib:SmBusReadBlock() function consistently reads a 32-byte block of data. Signed-off-by: Nhi Pham --- Silicon/Ampere/AmpereAltraPkg/Include/Library/I2cLib.h | 31 +++++ Silicon/Ampere/AmpereAltraPkg/Library/DwI2cLib/DwI2cLib.c | 137 ++++++++++= +++++++++- 2 files changed, 163 insertions(+), 5 deletions(-) diff --git a/Silicon/Ampere/AmpereAltraPkg/Include/Library/I2cLib.h b/Silic= on/Ampere/AmpereAltraPkg/Include/Library/I2cLib.h index f13794171029..d460f49efccb 100644 --- a/Silicon/Ampere/AmpereAltraPkg/Include/Library/I2cLib.h +++ b/Silicon/Ampere/AmpereAltraPkg/Include/Library/I2cLib.h @@ -65,6 +65,37 @@ I2cRead ( IN OUT UINT32 *ReadLength ); =20 +/** + SMBUS block read. + + @param[in] Bus I2C bus Id. + @param[in] SlaveAddr The address of slave device on the bus. + @param[in] BufCmd Buffer where to send the command. + @param[in] CmdLength Length of BufCmd. + @param[in,out] Buf Buffer where to put the read data to. + @param[in,out] ReadLength Pointer to length of buffer. + @param[in] PecCheck If Packet Error Code (PEC) checking is requi= red for this operation. + + @retval EFI_SUCCESS Read successfully. + @retval EFI_INVALID_PARAMETER A parameter is invalid. + @retval EFI_UNSUPPORTED The bus is not supported. + @retval EFI_NOT_READY The device/bus is not ready. + @retval EFI_TIMEOUT Timeout when transferring data. + @retval EFI_CRC_ERROR There are errors on receiving data. + +**/ +EFI_STATUS +EFIAPI +SmbusRead ( + IN UINT32 Bus, + IN UINT32 SlaveAddr, + IN UINT8 *BufCmd, + IN UINT32 CmdLength, + IN OUT UINT8 *Buf, + IN OUT UINT32 *ReadLength, + IN BOOLEAN PecCheck + ); + /** Setup new transaction with I2C slave device. =20 diff --git a/Silicon/Ampere/AmpereAltraPkg/Library/DwI2cLib/DwI2cLib.c b/Si= licon/Ampere/AmpereAltraPkg/Library/DwI2cLib/DwI2cLib.c index 669ba2ea98a4..9e52ae69e7cd 100644 --- a/Silicon/Ampere/AmpereAltraPkg/Library/DwI2cLib/DwI2cLib.c +++ b/Silicon/Ampere/AmpereAltraPkg/Library/DwI2cLib/DwI2cLib.c @@ -337,6 +337,11 @@ I2cWaitTxData ( DEBUG ((DEBUG_ERROR, "%a: Timeout waiting for TX buffer available\n"= , __FUNCTION__)); return EFI_TIMEOUT; } + + if ((I2cCheckErrors (Bus) & DW_IC_INTR_TX_ABRT) !=3D 0) { + return EFI_ABORTED; + } + MicroSecondDelay (mI2cBusList[Bus].PollingTime); } =20 @@ -542,13 +547,61 @@ InternalI2cWrite ( return Status; } =20 +EFI_STATUS +InternalSmbusReadDataLength ( + UINT32 Bus, + UINT32 *Length + ) +{ + EFI_STATUS Status; + UINTN Base; + UINT32 CmdSend; + + Base =3D mI2cBusList[Bus].Base; + + CmdSend =3D DW_IC_DATA_CMD_CMD; + MmioWrite32 (Base + DW_IC_DATA_CMD, CmdSend); + I2cSync (); + + if (I2cCheckErrors (Bus) !=3D 0) { + DEBUG ((DEBUG_ERROR, "%a: Sending reading command error\n", __func__))= ; + return EFI_CRC_ERROR; + } + + Status =3D I2cWaitRxData (Bus); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, + "%a: Reading Smbus data length failed to wait data\n", + __func__ + )); + + if (Status !=3D EFI_ABORTED) { + MmioWrite32 (Base + DW_IC_DATA_CMD, DW_IC_DATA_CMD_STOP); + I2cSync (); + } + + return Status; + } + + *Length =3D MmioRead32 (Base + DW_IC_DATA_CMD) & DW_IC_DATA_CMD_DAT_MASK= ; + I2cSync (); + + if (I2cCheckErrors (Bus) !=3D 0) { + DEBUG ((DEBUG_ERROR, "%a: Sending reading command error\n", __func__))= ; + return EFI_CRC_ERROR; + } + return EFI_SUCCESS; +} + EFI_STATUS InternalI2cRead ( UINT32 Bus, - UINT8 *BufCmd, - UINT32 CmdLength, - UINT8 *Buf, - UINT32 *Length + UINT8 *BufCmd, + UINT32 CmdLength, + UINT8 *Buf, + UINT32 *Length, + BOOLEAN IsSmbus, + BOOLEAN PecCheck ) { EFI_STATUS Status; @@ -559,6 +612,7 @@ InternalI2cRead ( UINTN Count; UINTN ReadCount; UINTN WriteCount; + UINT32 ResponseLen; =20 Status =3D EFI_SUCCESS; Base =3D mI2cBusList[Bus].Base; @@ -601,6 +655,36 @@ InternalI2cRead ( } =20 WriteCount =3D 0; + if (IsSmbus) { + // + // Read Smbus Data Length, first byte of the Smbus response data. + // + Status =3D InternalSmbusReadDataLength (Bus, &ResponseLen); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: InternalSmbusReadDataLength failed\n", __f= unc__)); + goto Exit; + } + + WriteCount++; + Buf[ReadCount++] =3D ResponseLen; + + // + // Abort the transaction when the requested length is shorter than the= actual response data + // or if there is no response data when PEC disabled. + // + if ((*Length < (ResponseLen + 2)) || (!PecCheck && ResponseLen =3D=3D = 0)) { + MmioWrite32 (Base + DW_IC_DATA_CMD, DW_IC_DATA_CMD_CMD | DW_IC_DATA_= CMD_STOP); + I2cSync (); + Status =3D EFI_INVALID_PARAMETER; + goto Exit; + } + + *Length =3D ResponseLen + 1; // Response Data Length + 8-bit Byte Coun= t field + if (PecCheck) { + *Length +=3D 1; // ++ 8-bit PEC field + } + } + while ((*Length - ReadCount) !=3D 0) { TxLimit =3D mI2cBusList[Bus].TxFifo - MmioRead32 (Base + DW_IC_TXFLR); RxLimit =3D mI2cBusList[Bus].RxFifo - MmioRead32 (Base + DW_IC_RXFLR); @@ -742,7 +826,50 @@ I2cRead ( =20 I2cSetSlaveAddr (Bus, SlaveAddr); =20 - return InternalI2cRead (Bus, BufCmd, CmdLength, Buf, ReadLength); + return InternalI2cRead (Bus, BufCmd, CmdLength, Buf, ReadLength, FALSE, = FALSE); +} + +/** + SMBUS block read. + + @param[in] Bus I2C bus Id. + @param[in] SlaveAddr The address of slave device on the bus. + @param[in] BufCmd Buffer where to send the command. + @param[in] CmdLength Length of BufCmd. + @param[in,out] Buf Buffer where to put the read data to. + @param[in,out] ReadLength Pointer to length of buffer. + @param[in] PecCheck If Packet Error Code (PEC) checking is requi= red for this operation. + + @retval EFI_SUCCESS Read successfully. + @retval EFI_INVALID_PARAMETER A parameter is invalid. + @retval EFI_UNSUPPORTED The bus is not supported. + @retval EFI_NOT_READY The device/bus is not ready. + @retval EFI_TIMEOUT Timeout when transferring data. + @retval EFI_CRC_ERROR There are errors on receiving data. + +**/ +EFI_STATUS +EFIAPI +SmbusRead ( + IN UINT32 Bus, + IN UINT32 SlaveAddr, + IN UINT8 *BufCmd, + IN UINT32 CmdLength, + IN OUT UINT8 *Buf, + IN OUT UINT32 *ReadLength, + IN BOOLEAN PecCheck + ) +{ + if (Bus >=3D AC01_I2C_MAX_BUS_NUM + || Buf =3D=3D NULL + || ReadLength =3D=3D NULL) + { + return EFI_INVALID_PARAMETER; + } + + I2cSetSlaveAddr (Bus, SlaveAddr); + + return InternalI2cRead (Bus, BufCmd, CmdLength, Buf, ReadLength, TRUE, P= ecCheck); } =20 /** --=20 2.25.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 (#120163): https://edk2.groups.io/g/devel/message/120163 Mute This Topic: https://groups.io/mt/107662232/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-