Skip to content

Commit f8d311b

Browse files
andy-shevAndi Shyti
authored andcommitted
i2c: mlxbf: Use readl_poll_timeout_atomic() for polling
Convert the usage of an open-coded custom tight poll while loop with the provided readl_poll_timeout_atomic() macro. Signed-off-by: Andy Shevchenko <[email protected]> Reviewed-by: Asmaa Mnebhi <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Andi Shyti <[email protected]>
1 parent 8b4da3e commit f8d311b

File tree

1 file changed

+26
-80
lines changed

1 file changed

+26
-80
lines changed

drivers/i2c/busses/i2c-mlxbf.c

Lines changed: 26 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <linux/interrupt.h>
1313
#include <linux/i2c.h>
1414
#include <linux/io.h>
15+
#include <linux/iopoll.h>
1516
#include <linux/kernel.h>
1617
#include <linux/module.h>
1718
#include <linux/mutex.h>
@@ -495,65 +496,6 @@ static u8 mlxbf_i2c_bus_count;
495496

496497
static struct mutex mlxbf_i2c_bus_lock;
497498

498-
/*
499-
* Function to poll a set of bits at a specific address; it checks whether
500-
* the bits are equal to zero when eq_zero is set to 'true', and not equal
501-
* to zero when eq_zero is set to 'false'.
502-
* Note that the timeout is given in microseconds.
503-
*/
504-
static u32 mlxbf_i2c_poll(void __iomem *io, u32 addr, u32 mask,
505-
bool eq_zero, u32 timeout)
506-
{
507-
u32 bits;
508-
509-
timeout = (timeout / MLXBF_I2C_POLL_FREQ_IN_USEC) + 1;
510-
511-
do {
512-
bits = readl(io + addr) & mask;
513-
if (eq_zero ? bits == 0 : bits != 0)
514-
return eq_zero ? 1 : bits;
515-
udelay(MLXBF_I2C_POLL_FREQ_IN_USEC);
516-
} while (timeout-- != 0);
517-
518-
return 0;
519-
}
520-
521-
/*
522-
* SW must make sure that the SMBus Master GW is idle before starting
523-
* a transaction. Accordingly, this function polls the Master FSM stop
524-
* bit; it returns false when the bit is asserted, true if not.
525-
*/
526-
static bool mlxbf_i2c_smbus_master_wait_for_idle(struct mlxbf_i2c_priv *priv)
527-
{
528-
u32 mask = MLXBF_I2C_SMBUS_MASTER_FSM_STOP_MASK;
529-
u32 addr = priv->chip->smbus_master_fsm_off;
530-
u32 timeout = MLXBF_I2C_SMBUS_TIMEOUT;
531-
532-
if (mlxbf_i2c_poll(priv->mst->io, addr, mask, true, timeout))
533-
return true;
534-
535-
return false;
536-
}
537-
538-
/*
539-
* wait for the lock to be released before acquiring it.
540-
*/
541-
static bool mlxbf_i2c_smbus_master_lock(struct mlxbf_i2c_priv *priv)
542-
{
543-
if (mlxbf_i2c_poll(priv->mst->io, MLXBF_I2C_SMBUS_MASTER_GW,
544-
MLXBF_I2C_MASTER_LOCK_BIT, true,
545-
MLXBF_I2C_SMBUS_LOCK_POLL_TIMEOUT))
546-
return true;
547-
548-
return false;
549-
}
550-
551-
static void mlxbf_i2c_smbus_master_unlock(struct mlxbf_i2c_priv *priv)
552-
{
553-
/* Clear the gw to clear the lock */
554-
writel(0, priv->mst->io + MLXBF_I2C_SMBUS_MASTER_GW);
555-
}
556-
557499
static bool mlxbf_i2c_smbus_transaction_success(u32 master_status,
558500
u32 cause_status)
559501
{
@@ -583,6 +525,7 @@ static int mlxbf_i2c_smbus_check_status(struct mlxbf_i2c_priv *priv)
583525
{
584526
u32 master_status_bits;
585527
u32 cause_status_bits;
528+
u32 bits;
586529

587530
/*
588531
* GW busy bit is raised by the driver and cleared by the HW
@@ -591,9 +534,9 @@ static int mlxbf_i2c_smbus_check_status(struct mlxbf_i2c_priv *priv)
591534
* then read the cause and master status bits to determine if
592535
* errors occurred during the transaction.
593536
*/
594-
mlxbf_i2c_poll(priv->mst->io, MLXBF_I2C_SMBUS_MASTER_GW,
595-
MLXBF_I2C_MASTER_BUSY_BIT, true,
596-
MLXBF_I2C_SMBUS_TIMEOUT);
537+
readl_poll_timeout_atomic(priv->mst->io + MLXBF_I2C_SMBUS_MASTER_GW,
538+
bits, !(bits & MLXBF_I2C_MASTER_BUSY_BIT),
539+
MLXBF_I2C_POLL_FREQ_IN_USEC, MLXBF_I2C_SMBUS_TIMEOUT);
597540

598541
/* Read cause status bits. */
599542
cause_status_bits = readl(priv->mst_cause->io +
@@ -740,7 +683,8 @@ mlxbf_i2c_smbus_start_transaction(struct mlxbf_i2c_priv *priv,
740683
u8 read_en, write_en, block_en, pec_en;
741684
u8 slave, flags, addr;
742685
u8 *read_buf;
743-
int ret = 0;
686+
u32 bits;
687+
int ret;
744688

745689
if (request->operation_cnt > MLXBF_I2C_SMBUS_MAX_OP_CNT)
746690
return -EINVAL;
@@ -760,11 +704,22 @@ mlxbf_i2c_smbus_start_transaction(struct mlxbf_i2c_priv *priv,
760704
* Try to acquire the smbus gw lock before any reads of the GW register since
761705
* a read sets the lock.
762706
*/
763-
if (WARN_ON(!mlxbf_i2c_smbus_master_lock(priv)))
707+
ret = readl_poll_timeout_atomic(priv->mst->io + MLXBF_I2C_SMBUS_MASTER_GW,
708+
bits, !(bits & MLXBF_I2C_MASTER_LOCK_BIT),
709+
MLXBF_I2C_POLL_FREQ_IN_USEC,
710+
MLXBF_I2C_SMBUS_LOCK_POLL_TIMEOUT);
711+
if (WARN_ON(ret))
764712
return -EBUSY;
765713

766-
/* Check whether the HW is idle */
767-
if (WARN_ON(!mlxbf_i2c_smbus_master_wait_for_idle(priv))) {
714+
/*
715+
* SW must make sure that the SMBus Master GW is idle before starting
716+
* a transaction. Accordingly, this call polls the Master FSM stop bit;
717+
* it returns -ETIMEDOUT when the bit is asserted, 0 if not.
718+
*/
719+
ret = readl_poll_timeout_atomic(priv->mst->io + priv->chip->smbus_master_fsm_off,
720+
bits, !(bits & MLXBF_I2C_SMBUS_MASTER_FSM_STOP_MASK),
721+
MLXBF_I2C_POLL_FREQ_IN_USEC, MLXBF_I2C_SMBUS_TIMEOUT);
722+
if (WARN_ON(ret)) {
768723
ret = -EBUSY;
769724
goto out_unlock;
770725
}
@@ -855,7 +810,8 @@ mlxbf_i2c_smbus_start_transaction(struct mlxbf_i2c_priv *priv,
855810
}
856811

857812
out_unlock:
858-
mlxbf_i2c_smbus_master_unlock(priv);
813+
/* Clear the gw to clear the lock */
814+
writel(0, priv->mst->io + MLXBF_I2C_SMBUS_MASTER_GW);
859815

860816
return ret;
861817
}
@@ -1829,18 +1785,6 @@ static bool mlxbf_i2c_has_coalesce(struct mlxbf_i2c_priv *priv, bool *read,
18291785
return true;
18301786
}
18311787

1832-
static bool mlxbf_i2c_slave_wait_for_idle(struct mlxbf_i2c_priv *priv,
1833-
u32 timeout)
1834-
{
1835-
u32 mask = MLXBF_I2C_CAUSE_S_GW_BUSY_FALL;
1836-
u32 addr = MLXBF_I2C_CAUSE_ARBITER;
1837-
1838-
if (mlxbf_i2c_poll(priv->slv_cause->io, addr, mask, false, timeout))
1839-
return true;
1840-
1841-
return false;
1842-
}
1843-
18441788
static struct i2c_client *mlxbf_i2c_get_slave_from_addr(
18451789
struct mlxbf_i2c_priv *priv, u8 addr)
18461790
{
@@ -1943,7 +1887,9 @@ static int mlxbf_i2c_irq_send(struct mlxbf_i2c_priv *priv, u8 recv_bytes)
19431887
* Wait until the transfer is completed; the driver will wait
19441888
* until the GW is idle, a cause will rise on fall of GW busy.
19451889
*/
1946-
mlxbf_i2c_slave_wait_for_idle(priv, MLXBF_I2C_SMBUS_TIMEOUT);
1890+
readl_poll_timeout_atomic(priv->slv_cause->io + MLXBF_I2C_CAUSE_ARBITER,
1891+
data32, data32 & MLXBF_I2C_CAUSE_S_GW_BUSY_FALL,
1892+
MLXBF_I2C_POLL_FREQ_IN_USEC, MLXBF_I2C_SMBUS_TIMEOUT);
19471893

19481894
clear_csr:
19491895
/* Release the Slave GW. */

0 commit comments

Comments
 (0)