i2c-bfin-twi: add a timeout to busbusy polling

Some faulty i2c slave devices might hold the sda/scl line and cause the
i2c bus driver to wait indefinitely in the busbusy loop.  Add a timeout
to the loop so the bus master can recover without locking up everything.

Signed-off-by: Sonic Zhang <sonic.zhang@analog.com>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>
diff --git a/drivers/i2c/busses/i2c-bfin-twi.c b/drivers/i2c/busses/i2c-bfin-twi.c
index 77f6082..fcb041d 100644
--- a/drivers/i2c/busses/i2c-bfin-twi.c
+++ b/drivers/i2c/busses/i2c-bfin-twi.c
@@ -294,6 +294,8 @@
 	return IRQ_HANDLED;
 }
 
+#define BFIN_TWI_BUSY_TIMEOUT	1000
+
 /*
  * One i2c master transfer
  */
@@ -303,12 +305,16 @@
 	struct bfin_twi_iface *iface = adap->algo_data;
 	struct i2c_msg *pmsg;
 	int rc = 0;
+	unsigned int busy_timeout = BFIN_TWI_BUSY_TIMEOUT;
 
 	if (!(read_CONTROL(iface) & TWI_ENA))
 		return -ENXIO;
 
-	while (read_MASTER_STAT(iface) & BUSBUSY)
+	while (read_MASTER_STAT(iface) & BUSBUSY) {
+		if (--busy_timeout == 0)
+			return -EBUSY;
 		yield();
+	}
 
 	iface->pmsg = msgs;
 	iface->msg_num = num;
@@ -403,12 +409,16 @@
 {
 	struct bfin_twi_iface *iface = adap->algo_data;
 	int rc = 0;
+	unsigned int busy_timeout = BFIN_TWI_BUSY_TIMEOUT;
 
 	if (!(read_CONTROL(iface) & TWI_ENA))
 		return -ENXIO;
 
-	while (read_MASTER_STAT(iface) & BUSBUSY)
+	while (read_MASTER_STAT(iface) & BUSBUSY) {
+		if (--busy_timeout == 0)
+			return -EBUSY;
 		yield();
+	}
 
 	iface->writeNum = 0;
 	iface->readNum = 0;