@@ -564,6 +564,15 @@ static inline void npcm_i2c_nack(struct npcm_i2c *bus)
564
564
iowrite8 (val , bus -> reg + NPCM_I2CCTL1 );
565
565
}
566
566
567
+ static inline void npcm_i2c_clear_master_status (struct npcm_i2c * bus )
568
+ {
569
+ u8 val ;
570
+
571
+ /* Clear NEGACK, STASTR and BER bits */
572
+ val = NPCM_I2CST_BER | NPCM_I2CST_NEGACK | NPCM_I2CST_STASTR ;
573
+ iowrite8 (val , bus -> reg + NPCM_I2CST );
574
+ }
575
+
567
576
#if IS_ENABLED (CONFIG_I2C_SLAVE )
568
577
static void npcm_i2c_slave_int_enable (struct npcm_i2c * bus , bool enable )
569
578
{
@@ -643,8 +652,8 @@ static void npcm_i2c_reset(struct npcm_i2c *bus)
643
652
iowrite8 (NPCM_I2CCST_BB , bus -> reg + NPCM_I2CCST );
644
653
iowrite8 (0xFF , bus -> reg + NPCM_I2CST );
645
654
646
- /* Clear EOB bit */
647
- iowrite8 ( NPCM_I2CCST3_EO_BUSY , bus -> reg + NPCM_I2CCST3 );
655
+ /* Clear and disable EOB */
656
+ npcm_i2c_eob_int ( bus , false );
648
657
649
658
/* Clear all fifo bits: */
650
659
iowrite8 (NPCM_I2CFIF_CTS_CLR_FIFO , bus -> reg + NPCM_I2CFIF_CTS );
@@ -656,6 +665,9 @@ static void npcm_i2c_reset(struct npcm_i2c *bus)
656
665
}
657
666
#endif
658
667
668
+ /* clear status bits for spurious interrupts */
669
+ npcm_i2c_clear_master_status (bus );
670
+
659
671
bus -> state = I2C_IDLE ;
660
672
}
661
673
@@ -818,15 +830,6 @@ static void npcm_i2c_read_fifo(struct npcm_i2c *bus, u8 bytes_in_fifo)
818
830
}
819
831
}
820
832
821
- static inline void npcm_i2c_clear_master_status (struct npcm_i2c * bus )
822
- {
823
- u8 val ;
824
-
825
- /* Clear NEGACK, STASTR and BER bits */
826
- val = NPCM_I2CST_BER | NPCM_I2CST_NEGACK | NPCM_I2CST_STASTR ;
827
- iowrite8 (val , bus -> reg + NPCM_I2CST );
828
- }
829
-
830
833
static void npcm_i2c_master_abort (struct npcm_i2c * bus )
831
834
{
832
835
/* Only current master is allowed to issue a stop condition */
@@ -1234,7 +1237,16 @@ static irqreturn_t npcm_i2c_int_slave_handler(struct npcm_i2c *bus)
1234
1237
ret = IRQ_HANDLED ;
1235
1238
} /* SDAST */
1236
1239
1237
- return ret ;
1240
+ /*
1241
+ * if irq is not one of the above, make sure EOB is disabled and all
1242
+ * status bits are cleared.
1243
+ */
1244
+ if (ret == IRQ_NONE ) {
1245
+ npcm_i2c_eob_int (bus , false);
1246
+ npcm_i2c_clear_master_status (bus );
1247
+ }
1248
+
1249
+ return IRQ_HANDLED ;
1238
1250
}
1239
1251
1240
1252
static int npcm_i2c_reg_slave (struct i2c_client * client )
@@ -1470,13 +1482,18 @@ static void npcm_i2c_irq_handle_nack(struct npcm_i2c *bus)
1470
1482
npcm_i2c_eob_int (bus , false);
1471
1483
npcm_i2c_master_stop (bus );
1472
1484
1485
+ /* Clear SDA Status bit (by reading dummy byte) */
1486
+ npcm_i2c_rd_byte (bus );
1487
+
1473
1488
/*
1474
1489
* The bus is released from stall only after the SW clears
1475
1490
* NEGACK bit. Then a Stop condition is sent.
1476
1491
*/
1477
1492
npcm_i2c_clear_master_status (bus );
1478
1493
readx_poll_timeout_atomic (ioread8 , bus -> reg + NPCM_I2CCST , val ,
1479
1494
!(val & NPCM_I2CCST_BUSY ), 10 , 200 );
1495
+ /* verify no status bits are still set after bus is released */
1496
+ npcm_i2c_clear_master_status (bus );
1480
1497
}
1481
1498
bus -> state = I2C_IDLE ;
1482
1499
@@ -1675,10 +1692,10 @@ static int npcm_i2c_recovery_tgclk(struct i2c_adapter *_adap)
1675
1692
int iter = 27 ;
1676
1693
1677
1694
if ((npcm_i2c_get_SDA (_adap ) == 1 ) && (npcm_i2c_get_SCL (_adap ) == 1 )) {
1678
- dev_dbg (bus -> dev , "bus%d recovery skipped, bus not stuck" ,
1679
- bus -> num );
1695
+ dev_dbg (bus -> dev , "bus%d-0x%x recovery skipped, bus not stuck" ,
1696
+ bus -> num , bus -> dest_addr );
1680
1697
npcm_i2c_reset (bus );
1681
- return status ;
1698
+ return 0 ;
1682
1699
}
1683
1700
1684
1701
npcm_i2c_int_enable (bus , false);
@@ -1912,6 +1929,7 @@ static int npcm_i2c_init_module(struct npcm_i2c *bus, enum i2c_mode mode,
1912
1929
bus_freq_hz < I2C_FREQ_MIN_HZ || bus_freq_hz > I2C_FREQ_MAX_HZ )
1913
1930
return - EINVAL ;
1914
1931
1932
+ npcm_i2c_int_enable (bus , false);
1915
1933
npcm_i2c_disable (bus );
1916
1934
1917
1935
/* Configure FIFO mode : */
@@ -1940,10 +1958,17 @@ static int npcm_i2c_init_module(struct npcm_i2c *bus, enum i2c_mode mode,
1940
1958
val = (val | NPCM_I2CCTL1_NMINTE ) & ~NPCM_I2CCTL1_RWS ;
1941
1959
iowrite8 (val , bus -> reg + NPCM_I2CCTL1 );
1942
1960
1943
- npcm_i2c_int_enable (bus , true);
1944
-
1945
1961
npcm_i2c_reset (bus );
1946
1962
1963
+ /* check HW is OK: SDA and SCL should be high at this point. */
1964
+ if ((npcm_i2c_get_SDA (& bus -> adap ) == 0 ) || (npcm_i2c_get_SCL (& bus -> adap ) == 0 )) {
1965
+ dev_err (bus -> dev , "I2C%d init fail: lines are low\n" , bus -> num );
1966
+ dev_err (bus -> dev , "SDA=%d SCL=%d\n" , npcm_i2c_get_SDA (& bus -> adap ),
1967
+ npcm_i2c_get_SCL (& bus -> adap ));
1968
+ return - ENXIO ;
1969
+ }
1970
+
1971
+ npcm_i2c_int_enable (bus , true);
1947
1972
return 0 ;
1948
1973
}
1949
1974
@@ -1991,10 +2016,14 @@ static irqreturn_t npcm_i2c_bus_irq(int irq, void *dev_id)
1991
2016
#if IS_ENABLED (CONFIG_I2C_SLAVE )
1992
2017
if (bus -> slave ) {
1993
2018
bus -> master_or_slave = I2C_SLAVE ;
1994
- return npcm_i2c_int_slave_handler (bus );
2019
+ if (npcm_i2c_int_slave_handler (bus ))
2020
+ return IRQ_HANDLED ;
1995
2021
}
1996
2022
#endif
1997
- return IRQ_NONE ;
2023
+ /* clear status bits for spurious interrupts */
2024
+ npcm_i2c_clear_master_status (bus );
2025
+
2026
+ return IRQ_HANDLED ;
1998
2027
}
1999
2028
2000
2029
static bool npcm_i2c_master_start_xmit (struct npcm_i2c * bus ,
@@ -2051,7 +2080,6 @@ static int npcm_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
2051
2080
u8 * write_data , * read_data ;
2052
2081
u8 slave_addr ;
2053
2082
unsigned long timeout ;
2054
- int ret = 0 ;
2055
2083
bool read_block = false;
2056
2084
bool read_PEC = false;
2057
2085
u8 bus_busy ;
@@ -2141,12 +2169,12 @@ static int npcm_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
2141
2169
bus -> read_block_use = read_block ;
2142
2170
2143
2171
reinit_completion (& bus -> cmd_complete );
2144
- if (!npcm_i2c_master_start_xmit (bus , slave_addr , nwrite , nread ,
2145
- write_data , read_data , read_PEC ,
2146
- read_block ))
2147
- ret = - EBUSY ;
2148
2172
2149
- if (ret != - EBUSY ) {
2173
+ npcm_i2c_int_enable (bus , true);
2174
+
2175
+ if (npcm_i2c_master_start_xmit (bus , slave_addr , nwrite , nread ,
2176
+ write_data , read_data , read_PEC ,
2177
+ read_block )) {
2150
2178
time_left = wait_for_completion_timeout (& bus -> cmd_complete ,
2151
2179
timeout );
2152
2180
@@ -2160,26 +2188,31 @@ static int npcm_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
2160
2188
}
2161
2189
}
2162
2190
}
2163
- ret = bus -> cmd_err ;
2164
2191
2165
2192
/* if there was BER, check if need to recover the bus: */
2166
2193
if (bus -> cmd_err == - EAGAIN )
2167
- ret = i2c_recover_bus (adap );
2194
+ bus -> cmd_err = i2c_recover_bus (adap );
2168
2195
2169
2196
/*
2170
2197
* After any type of error, check if LAST bit is still set,
2171
2198
* due to a HW issue.
2172
2199
* It cannot be cleared without resetting the module.
2173
2200
*/
2174
- if (bus -> cmd_err &&
2175
- (NPCM_I2CRXF_CTL_LAST_PEC & ioread8 (bus -> reg + NPCM_I2CRXF_CTL )))
2201
+ else if (bus -> cmd_err &&
2202
+ (NPCM_I2CRXF_CTL_LAST_PEC & ioread8 (bus -> reg + NPCM_I2CRXF_CTL )))
2176
2203
npcm_i2c_reset (bus );
2177
2204
2205
+ /* after any xfer, successful or not, stall and EOB must be disabled */
2206
+ npcm_i2c_stall_after_start (bus , false);
2207
+ npcm_i2c_eob_int (bus , false);
2208
+
2178
2209
#if IS_ENABLED (CONFIG_I2C_SLAVE )
2179
2210
/* reenable slave if it was enabled */
2180
2211
if (bus -> slave )
2181
2212
iowrite8 ((bus -> slave -> addr & 0x7F ) | NPCM_I2CADDR_SAEN ,
2182
2213
bus -> reg + NPCM_I2CADDR1 );
2214
+ #else
2215
+ npcm_i2c_int_enable (bus , false);
2183
2216
#endif
2184
2217
return bus -> cmd_err ;
2185
2218
}
0 commit comments