diff mbox

[Linaro-uefi,06/11] Hisilicon/I2CLib: Extend to support Hi1616

Message ID 1476324020-57155-6-git-send-email-heyi.guo@linaro.org
State New
Headers show

Commit Message

gary guo Oct. 13, 2016, 2 a.m. UTC
Hi1616 has 10 I2C ports and the IP of I2C controller is DW v2, which
requires to write BIT9 for the last transfer.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Heyi Guo <heyi.guo@linaro.org>
---
 Chips/Hisilicon/Include/Library/I2CLib.h |  2 +-
 Chips/Hisilicon/Library/I2CLib/I2CLib.c  | 54 +++++++++++---------------------
 2 files changed, 19 insertions(+), 37 deletions(-)

Comments

Leif Lindholm Oct. 13, 2016, 2 p.m. UTC | #1
On Thu, Oct 13, 2016 at 10:00:15AM +0800, Heyi Guo wrote:
> Hi1616 has 10 I2C ports and the IP of I2C controller is DW v2, which

I guess DW is DesignWare, but please spell it out.

> requires to write BIT9 for the last transfer.
> 
> Contributed-under: TianoCore Contribution Agreement 1.0
> Signed-off-by: Heyi Guo <heyi.guo@linaro.org>
> ---
>  Chips/Hisilicon/Include/Library/I2CLib.h |  2 +-
>  Chips/Hisilicon/Library/I2CLib/I2CLib.c  | 54 +++++++++++---------------------
>  2 files changed, 19 insertions(+), 37 deletions(-)
> 
> diff --git a/Chips/Hisilicon/Include/Library/I2CLib.h b/Chips/Hisilicon/Include/Library/I2CLib.h
> index c3597f6..36e9f5f 100644
> --- a/Chips/Hisilicon/Include/Library/I2CLib.h
> +++ b/Chips/Hisilicon/Include/Library/I2CLib.h
> @@ -33,7 +33,7 @@ typedef enum {
>  }SPEED_MODE;
>  
>  
> -#define    I2C_PORT_MAX            9
> +#define    I2C_PORT_MAX            10
>  
>  
>  
> diff --git a/Chips/Hisilicon/Library/I2CLib/I2CLib.c b/Chips/Hisilicon/Library/I2CLib/I2CLib.c
> index 087a4ba..f4910fb 100644
> --- a/Chips/Hisilicon/Library/I2CLib/I2CLib.c
> +++ b/Chips/Hisilicon/Library/I2CLib/I2CLib.c
> @@ -360,7 +360,14 @@ I2CWrite(I2C_DEVICE *I2cInfo, UINT16 InfoOffset, UINT32 ulLength, UINT8 *pBuf)
>              ulFifo = I2C_GetTxStatus(I2cInfo->Socket,I2cInfo->Port);
>          }
>  
> -        I2C_REG_WRITE(Base + I2C_DATA_CMD_OFFSET, *pBuf++);
> +        if (Idx < ulLength - 1)
> +        {
> +            I2C_REG_WRITE(Base + I2C_DATA_CMD_OFFSET, (*pBuf++));
> +        }
> +        else
> +        {
> +            I2C_REG_WRITE(Base + I2C_DATA_CMD_OFFSET, (*pBuf++) | 0x200);

What is 0x200 (apart from BIT9)?
Could we have a more descriptive #define?

> +        }
>      }
>  
>      ulFifo = I2C_GetTxStatus(I2cInfo->Socket,I2cInfo->Port);
> @@ -386,13 +393,10 @@ EFI_STATUS
>  EFIAPI
>  I2CRead(I2C_DEVICE *I2cInfo, UINT16 InfoOffset,UINT32 ulRxLen,UINT8 *pBuf)
>  {
> -    UINT32 ulCnt;
> -    UINT16 usTotalLen = 0;
>      UINT32 ulFifo;
>      UINT32 ulTimes = 0;
>      UINT8  I2CWAddr[2];
>      EFI_STATUS  Status;
> -    UINT32  BytesLeft;
>      UINT32  Idx = 0;
>      UINTN Base;
>  
> @@ -441,42 +445,17 @@ I2CRead(I2C_DEVICE *I2cInfo, UINT16 InfoOffset,UINT32 ulRxLen,UINT8 *pBuf)
>          ulFifo = I2C_GetTxStatus(I2cInfo->Socket,I2cInfo->Port);
>      }
>  
> -    usTotalLen = ulRxLen;
> -    BytesLeft = usTotalLen;
> -
> -    while(BytesLeft >= I2C_DRV_ONCE_READ_BYTES_NUM){
> -
> -
> -        for(ulCnt = 0; ulCnt < I2C_DRV_ONCE_READ_BYTES_NUM; ulCnt++) {
> +    while(ulRxLen > 0)
> +    {

Keep '{' with 'while'.

> +        if (ulRxLen > 1)
> +        {

and with 'if'.

>              I2C_REG_WRITE(Base + I2C_DATA_CMD_OFFSET, I2C_READ_SIGNAL);
>          }
> -
> -
> -        for(ulCnt = 0; ulCnt < I2C_DRV_ONCE_READ_BYTES_NUM; 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);
> -
> -            I2C_REG_READ(Base + I2C_DATA_CMD_OFFSET, pBuf[Idx++]);
> +        else
> +        {

And with 'else'.

> +            I2C_REG_WRITE(Base + I2C_DATA_CMD_OFFSET, I2C_READ_SIGNAL | 0x200);
>          }
> -        BytesLeft -= I2C_DRV_ONCE_READ_BYTES_NUM;
> -    }
>  
> -
> -    for(ulCnt = 0; ulCnt < BytesLeft; ulCnt++) {
> -        I2C_REG_WRITE(Base + I2C_DATA_CMD_OFFSET, I2C_READ_SIGNAL);
> -    }
> -
> -
> -    for(ulCnt = 0; ulCnt < BytesLeft; ulCnt++) {
>          ulTimes = 0;
>          do {
>              I2C_Delay(2);
> @@ -489,7 +468,10 @@ I2CRead(I2C_DEVICE *I2cInfo, UINT16 InfoOffset,UINT32 ulRxLen,UINT8 *pBuf)
>          }while(0 == ulFifo);
>  
>          I2C_REG_READ(Base + I2C_DATA_CMD_OFFSET, pBuf[Idx++]);
> +
> +        ulRxLen --;
>      }
> +

Unrelated whitespace change.

>      (VOID)I2C_Disable(I2cInfo->Socket, I2cInfo->Port);
>  
>      return EFI_SUCCESS;
> -- 
> 1.9.1
>
diff mbox

Patch

diff --git a/Chips/Hisilicon/Include/Library/I2CLib.h b/Chips/Hisilicon/Include/Library/I2CLib.h
index c3597f6..36e9f5f 100644
--- a/Chips/Hisilicon/Include/Library/I2CLib.h
+++ b/Chips/Hisilicon/Include/Library/I2CLib.h
@@ -33,7 +33,7 @@  typedef enum {
 }SPEED_MODE;
 
 
-#define    I2C_PORT_MAX            9
+#define    I2C_PORT_MAX            10
 
 
 
diff --git a/Chips/Hisilicon/Library/I2CLib/I2CLib.c b/Chips/Hisilicon/Library/I2CLib/I2CLib.c
index 087a4ba..f4910fb 100644
--- a/Chips/Hisilicon/Library/I2CLib/I2CLib.c
+++ b/Chips/Hisilicon/Library/I2CLib/I2CLib.c
@@ -360,7 +360,14 @@  I2CWrite(I2C_DEVICE *I2cInfo, UINT16 InfoOffset, UINT32 ulLength, UINT8 *pBuf)
             ulFifo = I2C_GetTxStatus(I2cInfo->Socket,I2cInfo->Port);
         }
 
-        I2C_REG_WRITE(Base + I2C_DATA_CMD_OFFSET, *pBuf++);
+        if (Idx < ulLength - 1)
+        {
+            I2C_REG_WRITE(Base + I2C_DATA_CMD_OFFSET, (*pBuf++));
+        }
+        else
+        {
+            I2C_REG_WRITE(Base + I2C_DATA_CMD_OFFSET, (*pBuf++) | 0x200);
+        }
     }
 
     ulFifo = I2C_GetTxStatus(I2cInfo->Socket,I2cInfo->Port);
@@ -386,13 +393,10 @@  EFI_STATUS
 EFIAPI
 I2CRead(I2C_DEVICE *I2cInfo, UINT16 InfoOffset,UINT32 ulRxLen,UINT8 *pBuf)
 {
-    UINT32 ulCnt;
-    UINT16 usTotalLen = 0;
     UINT32 ulFifo;
     UINT32 ulTimes = 0;
     UINT8  I2CWAddr[2];
     EFI_STATUS  Status;
-    UINT32  BytesLeft;
     UINT32  Idx = 0;
     UINTN Base;
 
@@ -441,42 +445,17 @@  I2CRead(I2C_DEVICE *I2cInfo, UINT16 InfoOffset,UINT32 ulRxLen,UINT8 *pBuf)
         ulFifo = I2C_GetTxStatus(I2cInfo->Socket,I2cInfo->Port);
     }
 
-    usTotalLen = ulRxLen;
-    BytesLeft = usTotalLen;
-
-    while(BytesLeft >= I2C_DRV_ONCE_READ_BYTES_NUM){
-
-
-        for(ulCnt = 0; ulCnt < I2C_DRV_ONCE_READ_BYTES_NUM; ulCnt++) {
+    while(ulRxLen > 0)
+    {
+        if (ulRxLen > 1)
+        {
             I2C_REG_WRITE(Base + I2C_DATA_CMD_OFFSET, I2C_READ_SIGNAL);
         }
-
-
-        for(ulCnt = 0; ulCnt < I2C_DRV_ONCE_READ_BYTES_NUM; 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);
-
-            I2C_REG_READ(Base + I2C_DATA_CMD_OFFSET, pBuf[Idx++]);
+        else
+        {
+            I2C_REG_WRITE(Base + I2C_DATA_CMD_OFFSET, I2C_READ_SIGNAL | 0x200);
         }
-        BytesLeft -= I2C_DRV_ONCE_READ_BYTES_NUM;
-    }
 
-
-    for(ulCnt = 0; ulCnt < BytesLeft; ulCnt++) {
-        I2C_REG_WRITE(Base + I2C_DATA_CMD_OFFSET, I2C_READ_SIGNAL);
-    }
-
-
-    for(ulCnt = 0; ulCnt < BytesLeft; ulCnt++) {
         ulTimes = 0;
         do {
             I2C_Delay(2);
@@ -489,7 +468,10 @@  I2CRead(I2C_DEVICE *I2cInfo, UINT16 InfoOffset,UINT32 ulRxLen,UINT8 *pBuf)
         }while(0 == ulFifo);
 
         I2C_REG_READ(Base + I2C_DATA_CMD_OFFSET, pBuf[Idx++]);
+
+        ulRxLen --;
     }
+
     (VOID)I2C_Disable(I2cInfo->Socket, I2cInfo->Port);
 
     return EFI_SUCCESS;