From nobody Mon Dec 15 01:40:37 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1534234281938255.98551539481787; Tue, 14 Aug 2018 01:11:21 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 786B3210F16C8; Tue, 14 Aug 2018 01:10:40 -0700 (PDT) Received: from mail-pl0-x229.google.com (mail-pl0-x229.google.com [IPv6:2607:f8b0:400e:c01::229]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 6511A210F1542 for ; Tue, 14 Aug 2018 01:10:39 -0700 (PDT) Received: by mail-pl0-x229.google.com with SMTP id g1-v6so346986plo.2 for ; Tue, 14 Aug 2018 01:10:39 -0700 (PDT) Received: from localhost.localdomain ([120.31.149.194]) by smtp.gmail.com with ESMTPSA id h130-v6sm72905670pgc.88.2018.08.14.01.10.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 14 Aug 2018 01:10:38 -0700 (PDT) X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=2607:f8b0:400e:c01::229; helo=mail-pl0-x229.google.com; envelope-from=ming.huang@linaro.org; receiver=edk2-devel@lists.01.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=zVLkioRfjr6SAofRo0hnkWa4eepOQ03l/pbrG1Rl8Sk=; b=edZUYVKRTFwrJTMFiPa5kZmFjNnBmECZ3IbhU59ysxDtipkZOcHRmi//XZDI6IdauY fsiDGHEAyBCjTWWjdD9ePzBj23kE/sToPIPh9jxN/nS+FDzKp0j/BprTkglQUYyjAugO FlqOD4cn8mc43kUPfwJD/kZp8qxCs6KxSmq04= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=zVLkioRfjr6SAofRo0hnkWa4eepOQ03l/pbrG1Rl8Sk=; b=iO63BQBaAKTQ4fHni59besl2p0tgNOiOnVz2xtupC17PtRNgmMyHqlxBUYBsmNHg6T Jp4ypgmA+32g6/EC75Cd14ZB1g3tZldjM4mpBTu2vCXmMNOk+mioWoIKHwKdRZ+Ty/Zh EwUV+RDBaIOwyAwJCuDfc6Ao1sKifNsnBWFnqdlYpFnsgHD0Fw5MHVTLh6WI0ZPLdiv+ misjRWX/jw/GWlqqNk7YO7NYQUApXJH8bmBy6wLjMMB37iPdOByOhaGjHylqnQP6AClq 6J3sC5a4kZttCE32IHm1sRvpVz9eLDOdSxV6pqI8DBkzlvRdoDQHthch2yQZxqXh8Lnu 99Cw== X-Gm-Message-State: AOUpUlHA0d1y8GuI+o7ZpYOZrZRXKmNhBajq/CDF75WCAmQjYKQHn23t xs9dLLF/VhW1AOPJikkDJXrSiw== X-Google-Smtp-Source: AA+uWPw9U5CGljpyeaapmdhxWgKrxjJbQg5b9PMRgxv/9PEAHOitTQzw5F0YXItZ+SLmxs8N8Lq9rA== X-Received: by 2002:a17:902:543:: with SMTP id 61-v6mr19758743plf.126.1534234238939; Tue, 14 Aug 2018 01:10:38 -0700 (PDT) From: Ming Huang To: leif.lindholm@linaro.org, linaro-uefi@lists.linaro.org, edk2-devel@lists.01.org, graeme.gregory@linaro.org Date: Tue, 14 Aug 2018 16:08:41 +0800 Message-Id: <20180814080903.50466-22-ming.huang@linaro.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180814080903.50466-1-ming.huang@linaro.org> References: <20180814080903.50466-1-ming.huang@linaro.org> Subject: [edk2] [PATCH edk2-platforms v2 21/43] Silicon/Hisilicon/I2C: Refactor I2C library X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: huangming23@huawei.com, xinliang.liu@linaro.org, john.garry@huawei.com, zhangjinsong2@huawei.com, guoheyi@huawei.com, huangdaode@hisilicon.com, michael.d.kinney@intel.com, lersek@redhat.com, wanghuiqiang@huawei.com MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDMRC_1 RDKM_2 RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" The hunk of waiting TX/TX finish is used by ten times, so move there hunks to a function CheckI2CTimeOut(). Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Ming Huang --- Silicon/Hisilicon/Library/I2CLib/I2CHw.h | 4 + Silicon/Hisilicon/Library/I2CLib/I2CLib.c | 161 ++++++++------------ 2 files changed, 66 insertions(+), 99 deletions(-) diff --git a/Silicon/Hisilicon/Library/I2CLib/I2CHw.h b/Silicon/Hisilicon/L= ibrary/I2CLib/I2CHw.h index aa561e929c..fa954c7937 100644 --- a/Silicon/Hisilicon/Library/I2CLib/I2CHw.h +++ b/Silicon/Hisilicon/Library/I2CLib/I2CHw.h @@ -265,5 +265,9 @@ UINT32 Val32; } I2C0_ENABLE_STATUS_U; =20 +typedef enum { + I2CTx, + I2CRx +} I2CTransfer; =20 #endif diff --git a/Silicon/Hisilicon/Library/I2CLib/I2CLib.c b/Silicon/Hisilicon/= Library/I2CLib/I2CLib.c index f03d55d6b2..9174e50dd4 100644 --- a/Silicon/Hisilicon/Library/I2CLib/I2CLib.c +++ b/Silicon/Hisilicon/Library/I2CLib/I2CLib.c @@ -238,6 +238,45 @@ I2C_GetRxStatus ( return Fifo.bits.rxflr; } =20 +EFI_STATUS +EFIAPI +CheckI2CTimeOut ( + UINT32 Socket, + UINT8 Port, + I2CTransfer Transfer + ) +{ + UINT32 Times =3D 0; + UINT32 Fifo; + + if (Transfer =3D=3D I2CTx) { + Fifo =3D I2C_GetTxStatus (Socket, Port); + while (Fifo !=3D 0) { + // This is a empirical value for I2C delay. MemoryFance is no need h= ere. + I2C_Delay (2); + if (++Times > I2C_READ_TIMEOUT) { + (VOID)I2C_Disable (Socket, Port); + return EFI_TIMEOUT; + } + Fifo =3D I2C_GetTxStatus (Socket, Port); + } + } else { + Fifo =3D I2C_GetRxStatus (Socket, Port); + while (Fifo =3D=3D 0) { + // This is a empirical value for I2C delay. MemoryFance is no need h= ere. + I2C_Delay (2); + if (++Times > I2C_READ_TIMEOUT) { + (VOID)I2C_Disable (Socket, Port); + return EFI_TIMEOUT; + } + Fifo =3D I2C_GetRxStatus (Socket, Port); + } + } + + return EFI_SUCCESS; +} + + EFI_STATUS EFIAPI WriteBeforeRead ( @@ -254,15 +293,11 @@ WriteBeforeRead ( =20 I2C_SetTarget (I2cInfo->Socket, I2cInfo->Port, I2cInfo->SlaveDeviceAddre= ss); =20 - Fifo =3D I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); - while (Fifo !=3D 0) { - I2C_Delay (2); - if (++Times > I2C_READ_TIMEOUT) { - return EFI_TIMEOUT; - } - Fifo =3D I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); + if (CheckI2CTimeOut (I2cInfo->Socket, I2cInfo->Port, I2CTx) =3D=3D EFI_T= IMEOUT) { + return EFI_TIMEOUT; } =20 + Fifo =3D 0; for (Count =3D 0; Count < Length; Count++) { Times =3D 0; while (Fifo > I2C_TXRX_THRESHOLD) { @@ -277,15 +312,8 @@ WriteBeforeRead ( Fifo =3D I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); } =20 - Fifo =3D I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); - Times =3D 0; - while (Fifo !=3D 0) { - I2C_Delay (2); - - if (++Times > I2C_READ_TIMEOUT) { - return EFI_TIMEOUT; - } - Fifo =3D I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); + if (CheckI2CTimeOut (I2cInfo->Socket, I2cInfo->Port, I2CTx) =3D=3D EFI_T= IMEOUT) { + return EFI_TIMEOUT; } =20 return EFI_SUCCESS; @@ -316,14 +344,8 @@ I2CWrite( =20 I2C_SetTarget(I2cInfo->Socket, I2cInfo->Port, I2cInfo->SlaveDeviceAddres= s); =20 - Fifo =3D I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); - while (Fifo !=3D 0) { - I2C_Delay (2); - if (++Times > I2C_READ_TIMEOUT) { - (VOID)I2C_Disable (I2cInfo->Socket, I2cInfo->Port); - return EFI_TIMEOUT; - } - Fifo =3D I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); + if (CheckI2CTimeOut (I2cInfo->Socket, I2cInfo->Port, I2CTx) =3D=3D EFI_T= IMEOUT) { + return EFI_TIMEOUT; } =20 if (I2cInfo->DeviceType) { @@ -333,15 +355,8 @@ I2CWrite( I2C_REG_WRITE (Base + I2C_DATA_CMD_OFFSET, InfoOffset & 0xff); } =20 - Fifo =3D I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); - Times =3D 0; - while (Fifo !=3D 0) { - I2C_Delay (2); - if (++Times > I2C_READ_TIMEOUT) { - (VOID)I2C_Disable (I2cInfo->Socket, I2cInfo->Port); - return EFI_TIMEOUT; - } - Fifo =3D I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); + if (CheckI2CTimeOut (I2cInfo->Socket, I2cInfo->Port, I2CTx) =3D=3D EFI_T= IMEOUT) { + return EFI_TIMEOUT; } =20 for (Idx =3D 0; Idx < Length; Idx++) { @@ -364,17 +379,8 @@ I2CWrite( } } =20 - Fifo =3D I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); - Times =3D 0; - while (Fifo !=3D 0) { - I2C_Delay (2); - - if (++Times > I2C_READ_TIMEOUT) { - DEBUG ((DEBUG_ERROR, "I2C Write try to finished,time out!\n")); - (VOID)I2C_Disable (I2cInfo->Socket, I2cInfo->Port); - return EFI_TIMEOUT; - } - Fifo =3D I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); + if (CheckI2CTimeOut (I2cInfo->Socket, I2cInfo->Port, I2CTx) =3D=3D EFI_T= IMEOUT) { + return EFI_TIMEOUT; } (VOID)I2C_Disable (I2cInfo->Socket, I2cInfo->Port); =20 @@ -390,8 +396,6 @@ I2CRead( UINT8 *pBuf ) { - UINT32 Fifo; - UINT32 Times =3D 0; UINT8 I2CWAddr[2]; EFI_STATUS Status; UINT32 Idx =3D 0; @@ -422,15 +426,8 @@ I2CRead( =20 I2C_SetTarget (I2cInfo->Socket, I2cInfo->Port, I2cInfo->SlaveDeviceAddre= ss); =20 - Fifo =3D I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); - while (Fifo !=3D 0) { - I2C_Delay (2); - - while (++Times > I2C_READ_TIMEOUT) { - (VOID)I2C_Disable (I2cInfo->Socket, I2cInfo->Port); - return EFI_TIMEOUT; - } - Fifo =3D I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); + if (CheckI2CTimeOut (I2cInfo->Socket, I2cInfo->Port, I2CTx) =3D=3D EFI_T= IMEOUT) { + return EFI_TIMEOUT; } =20 while (RxLen > 0) { @@ -441,16 +438,9 @@ I2CRead( I2C_REG_WRITE (Base + I2C_DATA_CMD_OFFSET, I2C_READ_SIGNAL | I2C_CMD= _STOP_BIT); } =20 - Times =3D 0; - do { - I2C_Delay (2); - - while (++Times > I2C_READ_TIMEOUT) { - (VOID)I2C_Disable (I2cInfo->Socket, I2cInfo->Port); - return EFI_TIMEOUT; - } - Fifo =3D I2C_GetRxStatus (I2cInfo->Socket, I2cInfo->Port); - } while (Fifo =3D=3D 0); + if (CheckI2CTimeOut (I2cInfo->Socket, I2cInfo->Port, I2CRx) =3D=3D EFI= _TIMEOUT) { + return EFI_TIMEOUT; + } =20 I2C_REG_READ (Base + I2C_DATA_CMD_OFFSET, pBuf[Idx++]); =20 @@ -472,8 +462,6 @@ I2CReadMultiByte ( { UINT32 Count; UINT16 TotalLen =3D 0; - UINT32 Fifo; - UINT32 Times =3D 0; UINT8 I2CWAddr[4]; EFI_STATUS Status; UINT32 BytesLeft; @@ -531,16 +519,9 @@ I2CReadMultiByte ( } =20 for (Count =3D 0; Count < BytesLeft; Count++) { - Times =3D 0; - do { - I2C_Delay (2); - - while (++Times > I2C_READ_TIMEOUT) { - (VOID)I2C_Disable (I2cInfo->Socket, I2cInfo->Port); - return EFI_TIMEOUT; - } - Fifo =3D I2C_GetRxStatus (I2cInfo->Socket, I2cInfo->Port); - } while (Fifo =3D=3D 0); + if (CheckI2CTimeOut (I2cInfo->Socket, I2cInfo->Port, I2CRx) =3D=3D EFI= _TIMEOUT) { + return EFI_TIMEOUT; + } =20 I2C_REG_READ (Base + I2C_DATA_CMD_OFFSET, pBuf[Idx++]); } @@ -558,8 +539,6 @@ I2CWriteMultiByte( UINT8 *pBuf ) { - UINT32 Fifo; - UINT32 Times =3D 0; UINT32 Idx; UINTN Base; =20 @@ -573,14 +552,8 @@ I2CWriteMultiByte( =20 I2C_SetTarget(I2cInfo->Socket, I2cInfo->Port, I2cInfo->SlaveDeviceAddres= s); =20 - Fifo =3D I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); - while (0 !=3D Fifo) { - I2C_Delay (2); - if (++Times > I2C_READ_TIMEOUT) { - (VOID)I2C_Disable (I2cInfo->Socket, I2cInfo->Port); - return EFI_TIMEOUT; - } - Fifo =3D I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); + if (CheckI2CTimeOut (I2cInfo->Socket, I2cInfo->Port, I2CTx) =3D=3D EFI_T= IMEOUT) { + return EFI_TIMEOUT; } =20 if (I2cInfo->DeviceType =3D=3D DEVICE_TYPE_CPLD_3BYTE_OPERANDS) { @@ -595,22 +568,12 @@ I2CWriteMultiByte( } else { } =20 - Times =3D 0; for (Idx =3D 0; Idx < Length; Idx++) { I2C_REG_WRITE (Base + I2C_DATA_CMD_OFFSET, *pBuf++); } =20 - Fifo =3D I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); - Times =3D 0; - while (Fifo !=3D 0) { - I2C_Delay (2); - - if (++Times > I2C_READ_TIMEOUT) { - DEBUG ((DEBUG_ERROR, "I2C Write try to finished,time out!\n")); - (VOID)I2C_Disable (I2cInfo->Socket, I2cInfo->Port); - return EFI_TIMEOUT; - } - Fifo =3D I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); + if (CheckI2CTimeOut (I2cInfo->Socket, I2cInfo->Port, I2CTx) =3D=3D EFI_T= IMEOUT) { + return EFI_TIMEOUT; } (VOID)I2C_Disable (I2cInfo->Socket, I2cInfo->Port); =20 --=20 2.17.0 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel