Merge tag 'efi-2020-07-rc6' of https://gitlab.denx.de/u-boot/custodians/u-boot-efi
[oweals/u-boot.git] / drivers / i2c / xilinx_xiic.c
index 83114ed5103e4ba89f6aa1459a7f7ac82ed68599..149bd327bdfe7ab42d4513de69adb33c8aa80013 100644 (file)
@@ -15,6 +15,7 @@
 #include <i2c.h>
 #include <wait_bit.h>
 #include <asm/io.h>
+#include <dm/device_compat.h>
 
 struct xilinx_xiic_priv {
        void __iomem            *base;
@@ -149,7 +150,7 @@ static void xiic_fill_tx_fifo(struct xilinx_xiic_priv *priv,
        while (len--) {
                u16 data = msg->buf[pos++];
 
-               if (pos == len && nmsgs == 1) {
+               if ((msg->len - pos == 0) && nmsgs == 1) {
                        /* last message in transfer -> STOP */
                        data |= XIIC_TX_DYN_STOP_MASK;
                }
@@ -266,8 +267,20 @@ static void xiic_reinit(struct xilinx_xiic_priv *priv)
 
 static int xilinx_xiic_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
 {
+       struct xilinx_xiic_priv *priv = dev_get_priv(dev);
        int ret = 0;
 
+       ret = wait_for_bit_8(priv->base + XIIC_SR_REG_OFFSET,
+                            XIIC_SR_BUS_BUSY_MASK, false, 1000, true);
+
+       if (ret == -ETIMEDOUT)
+               dev_err(dev, "timeout waiting for bus not busy condition\n");
+
+       if (ret)
+               return ret;
+
+       xiic_reinit(priv);
+
        for (; nmsgs > 0; nmsgs--, msg++) {
                if (msg->flags & I2C_M_RD)
                        ret = xilinx_xiic_read_common(dev, msg, nmsgs);