12
12
#include <linux/interrupt.h>
13
13
#include <linux/i2c.h>
14
14
#include <linux/io.h>
15
+ #include <linux/iopoll.h>
15
16
#include <linux/kernel.h>
16
17
#include <linux/module.h>
17
18
#include <linux/mutex.h>
@@ -495,65 +496,6 @@ static u8 mlxbf_i2c_bus_count;
495
496
496
497
static struct mutex mlxbf_i2c_bus_lock ;
497
498
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
-
557
499
static bool mlxbf_i2c_smbus_transaction_success (u32 master_status ,
558
500
u32 cause_status )
559
501
{
@@ -583,6 +525,7 @@ static int mlxbf_i2c_smbus_check_status(struct mlxbf_i2c_priv *priv)
583
525
{
584
526
u32 master_status_bits ;
585
527
u32 cause_status_bits ;
528
+ u32 bits ;
586
529
587
530
/*
588
531
* 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)
591
534
* then read the cause and master status bits to determine if
592
535
* errors occurred during the transaction.
593
536
*/
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 );
597
540
598
541
/* Read cause status bits. */
599
542
cause_status_bits = readl (priv -> mst_cause -> io +
@@ -740,7 +683,8 @@ mlxbf_i2c_smbus_start_transaction(struct mlxbf_i2c_priv *priv,
740
683
u8 read_en , write_en , block_en , pec_en ;
741
684
u8 slave , flags , addr ;
742
685
u8 * read_buf ;
743
- int ret = 0 ;
686
+ u32 bits ;
687
+ int ret ;
744
688
745
689
if (request -> operation_cnt > MLXBF_I2C_SMBUS_MAX_OP_CNT )
746
690
return - EINVAL ;
@@ -760,11 +704,22 @@ mlxbf_i2c_smbus_start_transaction(struct mlxbf_i2c_priv *priv,
760
704
* Try to acquire the smbus gw lock before any reads of the GW register since
761
705
* a read sets the lock.
762
706
*/
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 ))
764
712
return - EBUSY ;
765
713
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 )) {
768
723
ret = - EBUSY ;
769
724
goto out_unlock ;
770
725
}
@@ -855,7 +810,8 @@ mlxbf_i2c_smbus_start_transaction(struct mlxbf_i2c_priv *priv,
855
810
}
856
811
857
812
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 );
859
815
860
816
return ret ;
861
817
}
@@ -1829,18 +1785,6 @@ static bool mlxbf_i2c_has_coalesce(struct mlxbf_i2c_priv *priv, bool *read,
1829
1785
return true;
1830
1786
}
1831
1787
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
-
1844
1788
static struct i2c_client * mlxbf_i2c_get_slave_from_addr (
1845
1789
struct mlxbf_i2c_priv * priv , u8 addr )
1846
1790
{
@@ -1943,7 +1887,9 @@ static int mlxbf_i2c_irq_send(struct mlxbf_i2c_priv *priv, u8 recv_bytes)
1943
1887
* Wait until the transfer is completed; the driver will wait
1944
1888
* until the GW is idle, a cause will rise on fall of GW busy.
1945
1889
*/
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 );
1947
1893
1948
1894
clear_csr :
1949
1895
/* Release the Slave GW. */
0 commit comments