From patchwork Thu Aug 16 12:12:17 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Huang X-Patchwork-Id: 144364 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp2000291ljj; Thu, 16 Aug 2018 05:14:07 -0700 (PDT) X-Google-Smtp-Source: AA+uWPw/43s/+X/2846x9x6kggG1QcdrGVBaVn5p8q1M79LnbyGkEuZMDv/CRo1CMgEEAH1OeE0e X-Received: by 2002:a17:902:9a01:: with SMTP id v1-v6mr29194040plp.20.1534421647031; Thu, 16 Aug 2018 05:14:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534421647; cv=none; d=google.com; s=arc-20160816; b=BlVpN0WYofjBBTJxikkUta8pVw7VDiLCD2QaDar1htxDjh3Ugm915p0s7dihdOI557 wMwz5YRsqQesIa1oVvNUgHScBZ0T3zJ27lpY1dNl9lFFcyOAvyKXo8ZxBl5rGn5DTTam NoX7siBsbM4mLx65aQc1lwxnnWu6NBbAlQdEvifiv/ZKuMHLn9yqz9t1aPu1wPxSNL3C Ec/fZBb110N40Y3dx2uTU2hMElFvNVRyyOvyA7f4O/a/FwMKwE4ffa2t83E+ZKyyBHdq t4IchLrxBpgm9JpDCdbCvJXxEqFJeqnkmVOsH7Yotahlx0WUB4uZLpzw4fwvC+UXKT62 5YEw== 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=HZ6eO+fFa5z1GMM1sk1YkICool/0BXYNvoOZAbOCjDY=; b=O1nQuda5PMPco2Qx5G2WnA//6mnFoKcolv2v38UfGKmleEch3JPMP/bTEN27AUR0c2 Nx4SlRT7lQf97Bv4giLGCvsQcIj8kDahbIRUtHYtnqMue9Cqu4HqXYcPJx497pde93Nl hg4K9GcQ34FNtYABcSYaZHDX4GaB1QfW4ABbuqaL/yqQN8gfgXpDMPjVXsSIBi+R3H5S X4n+YO95Sb+npB/f+d9N5DSeYYTUOzGZ0ZUCpdlhoEpNY+ibYTyJXDee7gB2yqUIjSjF ZHWPoKTX0XaKhka81zV+vttKVvTxevI+NrFcR3FhneAG6/YLhGMraD0bIa6zbZocn7ls 6D7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=iMStV6kA; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 198.145.21.10 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. [198.145.21.10]) by mx.google.com with ESMTPS id e189-v6si28513642pfe.206.2018.08.16.05.14.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 16 Aug 2018 05:14:07 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 198.145.21.10 as permitted sender) client-ip=198.145.21.10; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=iMStV6kA; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 198.145.21.10 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 625EE210F376E; Thu, 16 Aug 2018 05:13:54 -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:4864:20::42c; helo=mail-pf1-x42c.google.com; envelope-from=ming.huang@linaro.org; receiver=edk2-devel@lists.01.org Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) (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 56F6521A143EF for ; Thu, 16 Aug 2018 05:13:53 -0700 (PDT) Received: by mail-pf1-x42c.google.com with SMTP id p12-v6so1960923pfh.2 for ; Thu, 16 Aug 2018 05:13:53 -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=zVLkioRfjr6SAofRo0hnkWa4eepOQ03l/pbrG1Rl8Sk=; b=iMStV6kAs6kTXesA6TkKXxRBVpVRFfFJJ1WEq3qaL9UQGap75JDjZyMdkL+bhLyZY3 qBdx037N8GzMh8vuM/yRwEaB3UcR8eMrkMCzbU5/Y8qu5c1iOCum4MfbAehnctcNnyYH K03zFdewwIDGcgc3VbkvHfvhQtcHzES0hj0Oo= 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=ftAiHQidNA5fPP88+PLE77LAujylkF4firfsU//i+Wp7f00ylHLr8BjAr7fW6DadP4 gY1nS0H6y5wTyfaSDgf1F7i6XyHpqXaAXjj8vYuUkr+h66+Cl1l7//pZEocsnO2614j7 M7pC4voAPKQVs0jaSflfgBUWwueTyyuclTx3JdV66FLwwJJ4RdXYiYPdbMWkg0JRmfrE 97VTx2gL4UMdYZcLWl+sipE2BdvVvKbmfweY8b7BZ7Std+t7Q0cKGkhN6ADVO2iIIxGk LXm95+QotFc/jVUA4wPKbawzzJphplIyKgYH/3OBn77K/27tJRVLM6NHJACH197lLQwu MGpA== X-Gm-Message-State: AOUpUlFoiMVG16f/PHxVl9oNlPspEtGidhvkzcLOfIeQwVHc+BLPv+S/ lXytUQJg4JxCh+p5UpKeamqBhA== X-Received: by 2002:a63:d15:: with SMTP id c21-v6mr29161911pgl.322.1534421632870; Thu, 16 Aug 2018 05:13:52 -0700 (PDT) Received: from localhost.localdomain ([120.31.149.194]) by smtp.gmail.com with ESMTPSA id j27-v6sm46736311pfj.91.2018.08.16.05.13.49 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 16 Aug 2018 05:13:52 -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: Thu, 16 Aug 2018 20:12:17 +0800 Message-Id: <20180816121239.44129-15-ming.huang@linaro.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180816121239.44129-1-ming.huang@linaro.org> References: <20180816121239.44129-1-ming.huang@linaro.org> Subject: [edk2] [PATCH edk2-platforms v3 14/36] 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 Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" 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(-) -- 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 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; } +EFI_STATUS +EFIAPI +CheckI2CTimeOut ( + UINT32 Socket, + UINT8 Port, + I2CTransfer Transfer + ) +{ + UINT32 Times = 0; + UINT32 Fifo; + + if (Transfer == I2CTx) { + Fifo = I2C_GetTxStatus (Socket, Port); + while (Fifo != 0) { + // This is a empirical value for I2C delay. MemoryFance is no need here. + I2C_Delay (2); + if (++Times > I2C_READ_TIMEOUT) { + (VOID)I2C_Disable (Socket, Port); + return EFI_TIMEOUT; + } + Fifo = I2C_GetTxStatus (Socket, Port); + } + } else { + Fifo = I2C_GetRxStatus (Socket, Port); + while (Fifo == 0) { + // This is a empirical value for I2C delay. MemoryFance is no need here. + I2C_Delay (2); + if (++Times > I2C_READ_TIMEOUT) { + (VOID)I2C_Disable (Socket, Port); + return EFI_TIMEOUT; + } + Fifo = I2C_GetRxStatus (Socket, Port); + } + } + + return EFI_SUCCESS; +} + + EFI_STATUS EFIAPI WriteBeforeRead ( @@ -254,15 +293,11 @@ WriteBeforeRead ( I2C_SetTarget (I2cInfo->Socket, I2cInfo->Port, I2cInfo->SlaveDeviceAddress); - Fifo = I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); - while (Fifo != 0) { - I2C_Delay (2); - if (++Times > I2C_READ_TIMEOUT) { - return EFI_TIMEOUT; - } - Fifo = I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); + if (CheckI2CTimeOut (I2cInfo->Socket, I2cInfo->Port, I2CTx) == EFI_TIMEOUT) { + return EFI_TIMEOUT; } + Fifo = 0; for (Count = 0; Count < Length; Count++) { Times = 0; while (Fifo > I2C_TXRX_THRESHOLD) { @@ -277,15 +312,8 @@ WriteBeforeRead ( Fifo = I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); } - Fifo = I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); - Times = 0; - while (Fifo != 0) { - I2C_Delay (2); - - if (++Times > I2C_READ_TIMEOUT) { - return EFI_TIMEOUT; - } - Fifo = I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); + if (CheckI2CTimeOut (I2cInfo->Socket, I2cInfo->Port, I2CTx) == EFI_TIMEOUT) { + return EFI_TIMEOUT; } return EFI_SUCCESS; @@ -316,14 +344,8 @@ I2CWrite( I2C_SetTarget(I2cInfo->Socket, I2cInfo->Port, I2cInfo->SlaveDeviceAddress); - Fifo = I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); - while (Fifo != 0) { - I2C_Delay (2); - if (++Times > I2C_READ_TIMEOUT) { - (VOID)I2C_Disable (I2cInfo->Socket, I2cInfo->Port); - return EFI_TIMEOUT; - } - Fifo = I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); + if (CheckI2CTimeOut (I2cInfo->Socket, I2cInfo->Port, I2CTx) == EFI_TIMEOUT) { + return EFI_TIMEOUT; } if (I2cInfo->DeviceType) { @@ -333,15 +355,8 @@ I2CWrite( I2C_REG_WRITE (Base + I2C_DATA_CMD_OFFSET, InfoOffset & 0xff); } - Fifo = I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); - Times = 0; - while (Fifo != 0) { - I2C_Delay (2); - if (++Times > I2C_READ_TIMEOUT) { - (VOID)I2C_Disable (I2cInfo->Socket, I2cInfo->Port); - return EFI_TIMEOUT; - } - Fifo = I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); + if (CheckI2CTimeOut (I2cInfo->Socket, I2cInfo->Port, I2CTx) == EFI_TIMEOUT) { + return EFI_TIMEOUT; } for (Idx = 0; Idx < Length; Idx++) { @@ -364,17 +379,8 @@ I2CWrite( } } - Fifo = I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); - Times = 0; - while (Fifo != 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 = 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); @@ -390,8 +396,6 @@ I2CRead( UINT8 *pBuf ) { - UINT32 Fifo; - UINT32 Times = 0; UINT8 I2CWAddr[2]; EFI_STATUS Status; UINT32 Idx = 0; @@ -422,15 +426,8 @@ I2CRead( I2C_SetTarget (I2cInfo->Socket, I2cInfo->Port, I2cInfo->SlaveDeviceAddress); - Fifo = I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); - while (Fifo != 0) { - I2C_Delay (2); - - while (++Times > I2C_READ_TIMEOUT) { - (VOID)I2C_Disable (I2cInfo->Socket, I2cInfo->Port); - return EFI_TIMEOUT; - } - Fifo = I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); + if (CheckI2CTimeOut (I2cInfo->Socket, I2cInfo->Port, I2CTx) == EFI_TIMEOUT) { + return EFI_TIMEOUT; } while (RxLen > 0) { @@ -441,16 +438,9 @@ I2CRead( I2C_REG_WRITE (Base + I2C_DATA_CMD_OFFSET, I2C_READ_SIGNAL | I2C_CMD_STOP_BIT); } - Times = 0; - do { - I2C_Delay (2); - - while (++Times > I2C_READ_TIMEOUT) { - (VOID)I2C_Disable (I2cInfo->Socket, I2cInfo->Port); - return EFI_TIMEOUT; - } - Fifo = I2C_GetRxStatus (I2cInfo->Socket, I2cInfo->Port); - } while (Fifo == 0); + if (CheckI2CTimeOut (I2cInfo->Socket, I2cInfo->Port, I2CRx) == EFI_TIMEOUT) { + return EFI_TIMEOUT; + } I2C_REG_READ (Base + I2C_DATA_CMD_OFFSET, pBuf[Idx++]); @@ -472,8 +462,6 @@ I2CReadMultiByte ( { UINT32 Count; UINT16 TotalLen = 0; - UINT32 Fifo; - UINT32 Times = 0; UINT8 I2CWAddr[4]; EFI_STATUS Status; UINT32 BytesLeft; @@ -531,16 +519,9 @@ I2CReadMultiByte ( } for (Count = 0; Count < BytesLeft; Count++) { - Times = 0; - do { - I2C_Delay (2); - - while (++Times > I2C_READ_TIMEOUT) { - (VOID)I2C_Disable (I2cInfo->Socket, I2cInfo->Port); - return EFI_TIMEOUT; - } - Fifo = I2C_GetRxStatus (I2cInfo->Socket, I2cInfo->Port); - } while (Fifo == 0); + if (CheckI2CTimeOut (I2cInfo->Socket, I2cInfo->Port, I2CRx) == EFI_TIMEOUT) { + return EFI_TIMEOUT; + } I2C_REG_READ (Base + I2C_DATA_CMD_OFFSET, pBuf[Idx++]); } @@ -558,8 +539,6 @@ I2CWriteMultiByte( UINT8 *pBuf ) { - UINT32 Fifo; - UINT32 Times = 0; UINT32 Idx; UINTN Base; @@ -573,14 +552,8 @@ I2CWriteMultiByte( I2C_SetTarget(I2cInfo->Socket, I2cInfo->Port, I2cInfo->SlaveDeviceAddress); - Fifo = I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); - while (0 != Fifo) { - I2C_Delay (2); - if (++Times > I2C_READ_TIMEOUT) { - (VOID)I2C_Disable (I2cInfo->Socket, I2cInfo->Port); - return EFI_TIMEOUT; - } - Fifo = I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); + if (CheckI2CTimeOut (I2cInfo->Socket, I2cInfo->Port, I2CTx) == EFI_TIMEOUT) { + return EFI_TIMEOUT; } if (I2cInfo->DeviceType == DEVICE_TYPE_CPLD_3BYTE_OPERANDS) { @@ -595,22 +568,12 @@ I2CWriteMultiByte( } else { } - Times = 0; for (Idx = 0; Idx < Length; Idx++) { I2C_REG_WRITE (Base + I2C_DATA_CMD_OFFSET, *pBuf++); } - Fifo = I2C_GetTxStatus (I2cInfo->Socket, I2cInfo->Port); - Times = 0; - while (Fifo != 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 = 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);