From patchwork Tue Jul 24 07:08:59 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Huang X-Patchwork-Id: 142719 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp6934117ljj; Tue, 24 Jul 2018 00:16:13 -0700 (PDT) X-Google-Smtp-Source: AAOMgpc0gIpeD71MPlxYlwQHaKibmN9PR+iRnuEhe0fo8RpOiv3Q0fQLUPPNq+TAAYd46sa6Td2G X-Received: by 2002:a63:c114:: with SMTP id w20-v6mr15085151pgf.234.1532416573823; Tue, 24 Jul 2018 00:16:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532416573; cv=none; d=google.com; s=arc-20160816; b=K3IOAmbdPmH5HlKxcH2vzCHTt3CiIIUfNHWrmUl343TCsin/sgijxHCJ2UdFaUQoyP WaK/Vcgus5x7WzlEnzcymSMdrpThCs3lGgw51kVt6JLaXz9XN4WQRcOHbLM0ziUyn9y4 BeIBqoTlfhipB/YIljKnQ1NaHzsPblsbu0VGvCRPx9PMx8cjqMLEYyVT2Wx2/HrOyZxt 2FWELnyCvF5l4hLnBOcdYoBsX0lFBY/eUuk3X/7bOf6344RwtAA5Fa0VpDq7pH0dheOJ JUjjg8RBexedR8VEScA3BvYDL7p05TT9MHmKh10eMD1VO3KaYnzz4GD/SM9L/AmRvY9Y tfLA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to:arc-authentication-results; bh=2TOCIiHxM1sqsah/wD93MXx1SMDI6/Yj8JLZroWYyZ8=; b=kiHQnKvjkLSxYq0ukTnW1BBmkOtluyRr5aiuv2dM3kdJYDffT85EQ3m4wyaxFMvuBE 1J8wz9Wsav3O06U05FgNuLOz2S7bZSh7O8/+2FCCkCdA3Nj4xVthNSYm7n5GBpDuyIgm EUnE8rvO8PQ1Rfxdeble0qrIbYpN4kHwqvF2mVN2VGTGreJhDmjPJ892lBRrXyUw4mKI a2IclVBWgqqpXTzdMvtIwpwvITW7AzlSPPLrjGQ7cU2kzVGYbwFArCz51okY3yc2kl8B LX947bygjunCJ2GHIhfLwasRPqBhCgPAly9EZbpHeE3AkRTjsppzhP1oHNfDf6h+cPUu aq+w== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=YML0Wtb9; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 2001:19d0:306:5::1 as permitted sender) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from ml01.01.org (ml01.01.org. [2001:19d0:306:5::1]) by mx.google.com with ESMTPS id 138-v6si10737495pga.188.2018.07.24.00.16.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 24 Jul 2018 00:16:13 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 2001:19d0:306:5::1 as permitted sender) client-ip=2001:19d0:306:5::1; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=YML0Wtb9; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 2001:19d0:306:5::1 as permitted sender) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 758FB210C1246; Tue, 24 Jul 2018 00:16:13 -0700 (PDT) X-Original-To: edk2-devel@lists.01.org Delivered-To: edk2-devel@lists.01.org Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=2607:f8b0:400e:c01::230; helo=mail-pl0-x230.google.com; envelope-from=ming.huang@linaro.org; receiver=edk2-devel@lists.01.org Received: from mail-pl0-x230.google.com (mail-pl0-x230.google.com [IPv6:2607:f8b0:400e:c01::230]) (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 3E110210C1240 for ; Tue, 24 Jul 2018 00:16:12 -0700 (PDT) Received: by mail-pl0-x230.google.com with SMTP id z7-v6so1347088plo.9 for ; Tue, 24 Jul 2018 00:16:12 -0700 (PDT) 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=nwdIwIa89WC8fnNV80FElCEtuIxJh1jXUJ3KIsz4oHc=; b=YML0Wtb9EqMaEw6vCC/3aqbcfe8WFCmW8GiHwTkyAoqv0h/1X9Nw5UnrnNcfYWUjs6 C2XRRLi1qpKw1GLRC7KGkqfw24PWKu07aWM6qjGMT4Szh72y7R1MwZIwGG9J2QmW0lnN TKLGc0M+TwX6Pb3TkJ8oYjCOHQea12YopNc2M= 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=nwdIwIa89WC8fnNV80FElCEtuIxJh1jXUJ3KIsz4oHc=; b=qDoJQWD4JrjAHZpPOsOat2W4ivixm3zBHHuSwDqdRIb3mxwDPduk0YEheRJ5kQXOoC ythCpQEuxQblNz//UEvTDlwPdlthvVosP7vbKCJqnklyMc44ZREnqOgsstdsHTP9oEJ8 9PncwbxuIB42WIXI7VqPzEZzQj/jDxn8FBYwQXT+PLoVOjbo+u9Bhjw2zfHKbTTvkZTe vK5yeWm4fS7pI5jTO0Nl83fynYI0FUBXtppf6oI1K9f8e6ftCbITuOJDQn3WxHd9Zrkh RI8utGEoGvqpTpBiL3EeJOJSPeDDgn3l8hyNYwrQxY50pynFY2/sL9GawpExETCYnEZT wf+A== X-Gm-Message-State: AOUpUlGKnbSS5sDEI99pJ2CXEp+zR4MOW16Xsj1l4fALRRQOJQjOxfg9 YhfdMIZju8ac1GUWk57CjT8IiA== X-Received: by 2002:a17:902:9042:: with SMTP id w2-v6mr7575471plz.61.1532416571816; Tue, 24 Jul 2018 00:16:11 -0700 (PDT) Received: from localhost.localdomain ([120.31.149.194]) by smtp.gmail.com with ESMTPSA id t14-v6sm11449788pgu.0.2018.07.24.00.15.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 24 Jul 2018 00:16:11 -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, 24 Jul 2018 15:08:59 +0800 Message-Id: <20180724070922.63362-16-ming.huang@linaro.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180724070922.63362-1-ming.huang@linaro.org> References: <20180724070922.63362-1-ming.huang@linaro.org> Subject: [edk2] [PATCH edk2-platforms v1 15/38] Silicon/Hisilicon/I2C: Optimize 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, huangdaode@hisilicon.com, guoheyi@huawei.com, wanghuiqiang@huawei.com MIME-Version: 1.0 Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" The hunt of waiting TX/TX finish is used by ten times, so move there hunts to a function CheckI2CTimeOut. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Ming Huang Signed-off-by: Heyi Guo --- Silicon/Hisilicon/Library/I2CLib/I2CHw.h | 4 + Silicon/Hisilicon/Library/I2CLib/I2CLib.c | 176 +++++++------------- 2 files changed, 65 insertions(+), 115 deletions(-) -- 2.17.0 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel diff --git a/Silicon/Hisilicon/Library/I2CLib/I2CHw.h b/Silicon/Hisilicon/Library/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; +typedef enum { + I2CTx, + I2CRx +} I2CTransfer; #endif diff --git a/Silicon/Hisilicon/Library/I2CLib/I2CLib.c b/Silicon/Hisilicon/Library/I2CLib/I2CLib.c index ecd2f07c4d..16636987a6 100644 --- a/Silicon/Hisilicon/Library/I2CLib/I2CLib.c +++ b/Silicon/Hisilicon/Library/I2CLib/I2CLib.c @@ -234,6 +234,42 @@ I2C_GetRxStatus(UINT32 Socket,UINT8 Port) return ulFifo.bits.rxflr; } +EFI_STATUS +EFIAPI +CheckI2CTimeOut ( + UINT32 Socket, + UINT8 Port, + I2CTransfer Transfer +) +{ + UINT32 ulTimes = 0; + UINT32 ulFifo; + + if (Transfer == I2CTx) { + ulFifo = I2C_GetTxStatus (Socket,Port); + while (ulFifo != 0) { + I2C_Delay(2); + if (++ulTimes > I2C_READ_TIMEOUT) { + (VOID)I2C_Disable (Socket, Port); + return EFI_TIMEOUT; + } + ulFifo = I2C_GetTxStatus (Socket,Port); + } + } + else { + ulFifo = I2C_GetRxStatus (Socket,Port); + while (ulFifo == 0) { + I2C_Delay(2); + if (++ulTimes > I2C_READ_TIMEOUT) { + (VOID)I2C_Disable (Socket, Port); + return EFI_TIMEOUT; + } + ulFifo = I2C_GetRxStatus (Socket,Port); + } + } + return EFI_SUCCESS; +} + EFI_STATUS EFIAPI WriteBeforeRead(I2C_DEVICE *I2cInfo, UINT32 ulLength, UINT8 *pBuf) @@ -247,17 +283,11 @@ WriteBeforeRead(I2C_DEVICE *I2cInfo, UINT32 ulLength, UINT8 *pBuf) I2C_SetTarget(I2cInfo->Socket,I2cInfo->Port,I2cInfo->SlaveDeviceAddress); - ulFifo = I2C_GetTxStatus(I2cInfo->Socket,I2cInfo->Port); - while(0 != ulFifo) - { - I2C_Delay(2); - if(++ulTimes > I2C_READ_TIMEOUT) - { - return EFI_TIMEOUT; - } - ulFifo = I2C_GetTxStatus(I2cInfo->Socket,I2cInfo->Port); + if (CheckI2CTimeOut (I2cInfo->Socket,I2cInfo->Port,I2CTx) == EFI_TIMEOUT) { + return EFI_TIMEOUT; } + ulFifo = 0; for(ulCnt = 0; ulCnt < ulLength; ulCnt++) { ulTimes = 0; @@ -275,17 +305,8 @@ WriteBeforeRead(I2C_DEVICE *I2cInfo, UINT32 ulLength, UINT8 *pBuf) ulFifo = I2C_GetTxStatus(I2cInfo->Socket,I2cInfo->Port); } - ulFifo = I2C_GetTxStatus(I2cInfo->Socket,I2cInfo->Port); - ulTimes = 0; - while(0 != ulFifo) - { - I2C_Delay(2); - - if(++ulTimes > I2C_READ_TIMEOUT) - { - return EFI_TIMEOUT; - } - ulFifo = I2C_GetTxStatus(I2cInfo->Socket,I2cInfo->Port); + if (CheckI2CTimeOut (I2cInfo->Socket,I2cInfo->Port,I2CTx) == EFI_TIMEOUT) { + return EFI_TIMEOUT; } return EFI_SUCCESS; @@ -313,16 +334,8 @@ I2CWrite(I2C_DEVICE *I2cInfo, UINT16 InfoOffset, UINT32 ulLength, UINT8 *pBuf) I2C_SetTarget(I2cInfo->Socket,I2cInfo->Port,I2cInfo->SlaveDeviceAddress); - ulFifo = I2C_GetTxStatus(I2cInfo->Socket,I2cInfo->Port); - while(0 != ulFifo) - { - I2C_Delay(2); - if(++ulTimes > I2C_READ_TIMEOUT) - { - (VOID)I2C_Disable(I2cInfo->Socket, I2cInfo->Port); - return EFI_TIMEOUT; - } - ulFifo = I2C_GetTxStatus(I2cInfo->Socket,I2cInfo->Port); + if (CheckI2CTimeOut (I2cInfo->Socket,I2cInfo->Port,I2CTx) == EFI_TIMEOUT) { + return EFI_TIMEOUT; } @@ -336,17 +349,8 @@ I2CWrite(I2C_DEVICE *I2cInfo, UINT16 InfoOffset, UINT32 ulLength, UINT8 *pBuf) I2C_REG_WRITE(Base + I2C_DATA_CMD_OFFSET, InfoOffset & 0xff); } - ulFifo = I2C_GetTxStatus(I2cInfo->Socket,I2cInfo->Port); - ulTimes = 0; - while(0 != ulFifo) - { - I2C_Delay(2); - if(++ulTimes > I2C_READ_TIMEOUT) - { - (VOID)I2C_Disable(I2cInfo->Socket, I2cInfo->Port); - return EFI_TIMEOUT; - } - ulFifo = I2C_GetTxStatus(I2cInfo->Socket,I2cInfo->Port); + if (CheckI2CTimeOut (I2cInfo->Socket,I2cInfo->Port,I2CTx) == EFI_TIMEOUT) { + return EFI_TIMEOUT; } for(Idx = 0; Idx < ulLength; Idx++) @@ -372,20 +376,10 @@ I2CWrite(I2C_DEVICE *I2cInfo, UINT16 InfoOffset, UINT32 ulLength, UINT8 *pBuf) } } - ulFifo = I2C_GetTxStatus(I2cInfo->Socket,I2cInfo->Port); - ulTimes = 0; - while(0 != ulFifo) - { - I2C_Delay(2); - - if(++ulTimes > I2C_READ_TIMEOUT) - { - DEBUG ((EFI_D_ERROR, "I2C Write try to finished,time out!\n")); - (VOID)I2C_Disable(I2cInfo->Socket, I2cInfo->Port); - return EFI_TIMEOUT; - } - ulFifo = I2C_GetTxStatus(I2cInfo->Socket,I2cInfo->Port); + if (CheckI2CTimeOut (I2cInfo->Socket,I2cInfo->Port,I2CTx) == EFI_TIMEOUT) { + return EFI_TIMEOUT; } + (VOID)I2C_Disable(I2cInfo->Socket, I2cInfo->Port); return EFI_SUCCESS; @@ -395,8 +389,6 @@ EFI_STATUS EFIAPI I2CRead(I2C_DEVICE *I2cInfo, UINT16 InfoOffset,UINT32 ulRxLen,UINT8 *pBuf) { - UINT32 ulFifo; - UINT32 ulTimes = 0; UINT8 I2CWAddr[2]; EFI_STATUS Status; UINT32 Idx = 0; @@ -434,17 +426,8 @@ I2CRead(I2C_DEVICE *I2cInfo, UINT16 InfoOffset,UINT32 ulRxLen,UINT8 *pBuf) I2C_SetTarget(I2cInfo->Socket,I2cInfo->Port,I2cInfo->SlaveDeviceAddress); - ulFifo = I2C_GetTxStatus(I2cInfo->Socket,I2cInfo->Port); - while(0 != ulFifo) - { - I2C_Delay(2); - - while(++ulTimes > I2C_READ_TIMEOUT) - { - (VOID)I2C_Disable(I2cInfo->Socket, I2cInfo->Port); - return EFI_TIMEOUT; - } - ulFifo = I2C_GetTxStatus(I2cInfo->Socket,I2cInfo->Port); + if (CheckI2CTimeOut (I2cInfo->Socket,I2cInfo->Port,I2CTx) == EFI_TIMEOUT) { + return EFI_TIMEOUT; } while (ulRxLen > 0) { @@ -455,16 +438,9 @@ I2CRead(I2C_DEVICE *I2cInfo, UINT16 InfoOffset,UINT32 ulRxLen,UINT8 *pBuf) I2C_REG_WRITE(Base + I2C_DATA_CMD_OFFSET, I2C_READ_SIGNAL | I2C_CMD_STOP_BIT); } - ulTimes = 0; - do { - I2C_Delay(2); - - while(++ulTimes > I2C_READ_TIMEOUT) { - (VOID)I2C_Disable(I2cInfo->Socket, I2cInfo->Port); - return EFI_TIMEOUT; - } - ulFifo = I2C_GetRxStatus(I2cInfo->Socket,I2cInfo->Port); - }while(0 == ulFifo); + if (CheckI2CTimeOut (I2cInfo->Socket,I2cInfo->Port,I2CRx) == EFI_TIMEOUT) { + return EFI_TIMEOUT; + } I2C_REG_READ(Base + I2C_DATA_CMD_OFFSET, pBuf[Idx++]); @@ -481,8 +457,6 @@ I2CReadMultiByte(I2C_DEVICE *I2cInfo, UINT32 InfoOffset,UINT32 ulRxLen,UINT8 *pB { UINT32 ulCnt; UINT16 usTotalLen = 0; - UINT32 ulFifo; - UINT32 ulTimes = 0; UINT8 I2CWAddr[4]; EFI_STATUS Status; UINT32 BytesLeft; @@ -558,16 +532,9 @@ I2CReadMultiByte(I2C_DEVICE *I2cInfo, UINT32 InfoOffset,UINT32 ulRxLen,UINT8 *pB for(ulCnt = 0; ulCnt < BytesLeft; ulCnt++) { - ulTimes = 0; - do { - I2C_Delay(2); - - while(++ulTimes > I2C_READ_TIMEOUT) { - (VOID)I2C_Disable(I2cInfo->Socket, I2cInfo->Port); - return EFI_TIMEOUT; - } - ulFifo = I2C_GetRxStatus(I2cInfo->Socket,I2cInfo->Port); - }while(0 == ulFifo); + if (CheckI2CTimeOut (I2cInfo->Socket,I2cInfo->Port,I2CRx) == EFI_TIMEOUT) { + return EFI_TIMEOUT; + } I2C_REG_READ(Base + I2C_DATA_CMD_OFFSET, pBuf[Idx++]); } @@ -580,8 +547,6 @@ EFI_STATUS EFIAPI I2CWriteMultiByte(I2C_DEVICE *I2cInfo, UINT32 InfoOffset, UINT32 ulLength, UINT8 *pBuf) { - UINT32 ulFifo; - UINT32 ulTimes = 0; UINT32 Idx; UINTN Base; @@ -597,16 +562,8 @@ I2CWriteMultiByte(I2C_DEVICE *I2cInfo, UINT32 InfoOffset, UINT32 ulLength, UINT8 I2C_SetTarget(I2cInfo->Socket,I2cInfo->Port,I2cInfo->SlaveDeviceAddress); - ulFifo = I2C_GetTxStatus(I2cInfo->Socket,I2cInfo->Port); - while(0 != ulFifo) - { - I2C_Delay(2); - if(++ulTimes > I2C_READ_TIMEOUT) - { - (VOID)I2C_Disable(I2cInfo->Socket, I2cInfo->Port); - return EFI_TIMEOUT; - } - ulFifo = I2C_GetTxStatus(I2cInfo->Socket,I2cInfo->Port); + if (CheckI2CTimeOut (I2cInfo->Socket,I2cInfo->Port,I2CTx) == EFI_TIMEOUT) { + return EFI_TIMEOUT; } @@ -630,7 +587,6 @@ I2CWriteMultiByte(I2C_DEVICE *I2cInfo, UINT32 InfoOffset, UINT32 ulLength, UINT8 } - ulTimes = 0; for(Idx = 0; Idx < ulLength; Idx++) { @@ -638,20 +594,10 @@ I2CWriteMultiByte(I2C_DEVICE *I2cInfo, UINT32 InfoOffset, UINT32 ulLength, UINT8 } - ulFifo = I2C_GetTxStatus(I2cInfo->Socket,I2cInfo->Port); - ulTimes = 0; - while(0 != ulFifo) - { - I2C_Delay(2); - - if(++ulTimes > I2C_READ_TIMEOUT) - { - DEBUG ((EFI_D_ERROR, "I2C Write try to finished,time out!\n")); - (VOID)I2C_Disable(I2cInfo->Socket, I2cInfo->Port); - return EFI_TIMEOUT; - } - ulFifo = I2C_GetTxStatus(I2cInfo->Socket,I2cInfo->Port); + if (CheckI2CTimeOut (I2cInfo->Socket,I2cInfo->Port,I2CTx) == EFI_TIMEOUT) { + return EFI_TIMEOUT; } + (VOID)I2C_Disable(I2cInfo->Socket, I2cInfo->Port); return EFI_SUCCESS;