55 * SMBus host driver for PA Semi PWRficient
66 */
77
8+ #include <linux/bitfield.h>
89#include <linux/module.h>
910#include <linux/pci.h>
1011#include <linux/kernel.h>
2021/* Register offsets */
2122#define REG_MTXFIFO 0x00
2223#define REG_MRXFIFO 0x04
24+ #define REG_XFSTA 0x0c
2325#define REG_SMSTA 0x14
2426#define REG_IMASK 0x18
2527#define REG_CTL 0x1c
2628#define REG_REV 0x28
2729
2830/* Register defs */
29- #define MTXFIFO_READ 0x00000400
30- #define MTXFIFO_STOP 0x00000200
31- #define MTXFIFO_START 0x00000100
32- #define MTXFIFO_DATA_M 0x000000ff
33-
34- #define MRXFIFO_EMPTY 0x00000100
35- #define MRXFIFO_DATA_M 0x000000ff
36-
37- #define SMSTA_XEN 0x08000000
38- #define SMSTA_MTN 0x00200000
39-
40- #define CTL_MRR 0x00000400
41- #define CTL_MTR 0x00000200
42- #define CTL_EN 0x00000800
43- #define CTL_CLK_M 0x000000ff
31+ #define MTXFIFO_READ BIT(10)
32+ #define MTXFIFO_STOP BIT(9)
33+ #define MTXFIFO_START BIT(8)
34+ #define MTXFIFO_DATA_M GENMASK(7, 0)
35+
36+ #define MRXFIFO_EMPTY BIT(8)
37+ #define MRXFIFO_DATA_M GENMASK(7, 0)
38+
39+ #define SMSTA_XIP BIT(28)
40+ #define SMSTA_XEN BIT(27)
41+ #define SMSTA_JMD BIT(25)
42+ #define SMSTA_JAM BIT(24)
43+ #define SMSTA_MTO BIT(23)
44+ #define SMSTA_MTA BIT(22)
45+ #define SMSTA_MTN BIT(21)
46+ #define SMSTA_MRNE BIT(19)
47+ #define SMSTA_MTE BIT(16)
48+ #define SMSTA_TOM BIT(6)
49+
50+ #define CTL_EN BIT(11)
51+ #define CTL_MRR BIT(10)
52+ #define CTL_MTR BIT(9)
53+ #define CTL_UJM BIT(8)
54+ #define CTL_CLK_M GENMASK(7, 0)
55+
56+ #define TRANSFER_TIMEOUT_MS 100
4457
4558static inline void reg_write (struct pasemi_smbus * smbus , int reg , int val )
4659{
@@ -61,7 +74,7 @@ static inline int reg_read(struct pasemi_smbus *smbus, int reg)
6174
6275static void pasemi_reset (struct pasemi_smbus * smbus )
6376{
64- u32 val = (CTL_MTR | CTL_MRR | (smbus -> clk_div & CTL_CLK_M ));
77+ u32 val = (CTL_MTR | CTL_MRR | CTL_UJM | (smbus -> clk_div & CTL_CLK_M ));
6578
6679 if (smbus -> hw_rev >= 6 )
6780 val |= CTL_EN ;
@@ -70,23 +83,51 @@ static void pasemi_reset(struct pasemi_smbus *smbus)
7083 reinit_completion (& smbus -> irq_completion );
7184}
7285
73- static void pasemi_smb_clear (struct pasemi_smbus * smbus )
86+ static int pasemi_smb_clear (struct pasemi_smbus * smbus )
7487{
75- unsigned int status ;
88+ unsigned int status , xfstatus ;
89+ int timeout = TRANSFER_TIMEOUT_MS ;
7690
7791 status = reg_read (smbus , REG_SMSTA );
92+
93+ /* First wait for the bus to go idle */
94+ while ((status & (SMSTA_XIP | SMSTA_JAM )) && timeout -- ) {
95+ msleep (1 );
96+ status = reg_read (smbus , REG_SMSTA );
97+ }
98+
99+ xfstatus = reg_read (smbus , REG_XFSTA );
100+
101+ if (timeout < 0 ) {
102+ dev_warn (smbus -> dev , "Bus is still stuck (status 0x%08x xfstatus 0x%08x)\n" ,
103+ status , xfstatus );
104+ return - EIO ;
105+ }
106+
107+ /* If any badness happened or there is data in the FIFOs, reset the FIFOs */
108+ if ((status & (SMSTA_MRNE | SMSTA_JMD | SMSTA_MTO | SMSTA_TOM | SMSTA_MTN | SMSTA_MTA )) ||
109+ !(status & SMSTA_MTE )) {
110+ dev_warn (smbus -> dev , "Issuing reset due to status 0x%08x (xfstatus 0x%08x)\n" ,
111+ status , xfstatus );
112+ pasemi_reset (smbus );
113+ }
114+
115+ /* Clear the flags */
78116 reg_write (smbus , REG_SMSTA , status );
117+
118+ return 0 ;
79119}
80120
81121static int pasemi_smb_waitready (struct pasemi_smbus * smbus )
82122{
83- int timeout = 100 ;
123+ int timeout = TRANSFER_TIMEOUT_MS ;
84124 unsigned int status ;
85125
86126 if (smbus -> use_irq ) {
87127 reinit_completion (& smbus -> irq_completion );
88- reg_write (smbus , REG_IMASK , SMSTA_XEN | SMSTA_MTN );
89- wait_for_completion_timeout (& smbus -> irq_completion , msecs_to_jiffies (100 ));
128+ /* XEN should be set when a transaction terminates, whether due to error or not */
129+ reg_write (smbus , REG_IMASK , SMSTA_XEN );
130+ wait_for_completion_timeout (& smbus -> irq_completion , msecs_to_jiffies (timeout ));
90131 reg_write (smbus , REG_IMASK , 0 );
91132 status = reg_read (smbus , REG_SMSTA );
92133 } else {
@@ -97,16 +138,32 @@ static int pasemi_smb_waitready(struct pasemi_smbus *smbus)
97138 }
98139 }
99140
100- /* Got NACK? */
101- if (status & SMSTA_MTN )
102- return - ENXIO ;
141+ /* Controller timeout? */
142+ if (status & SMSTA_TOM ) {
143+ dev_warn (smbus -> dev , "Controller timeout, status 0x%08x\n" , status );
144+ return - EIO ;
145+ }
103146
104- if ( timeout < 0 ) {
105- dev_warn ( smbus -> dev , "Timeout, status 0x%08x\n" , status );
106- reg_write (smbus , REG_SMSTA , status );
147+ /* Peripheral timeout? */
148+ if ( status & SMSTA_MTO ) {
149+ dev_warn (smbus -> dev , "Peripheral timeout, status 0x%08x\n" , status );
107150 return - ETIME ;
108151 }
109152
153+ /* Still stuck in a transaction? */
154+ if (status & SMSTA_XIP ) {
155+ dev_warn (smbus -> dev , "Bus stuck, status 0x%08x\n" , status );
156+ return - EIO ;
157+ }
158+
159+ /* Arbitration loss? */
160+ if (status & SMSTA_MTA )
161+ return - EBUSY ;
162+
163+ /* Got NACK? */
164+ if (status & SMSTA_MTN )
165+ return - ENXIO ;
166+
110167 /* Clear XEN */
111168 reg_write (smbus , REG_SMSTA , SMSTA_XEN );
112169
@@ -167,7 +224,8 @@ static int pasemi_i2c_xfer(struct i2c_adapter *adapter,
167224 struct pasemi_smbus * smbus = adapter -> algo_data ;
168225 int ret , i ;
169226
170- pasemi_smb_clear (smbus );
227+ if (pasemi_smb_clear (smbus ))
228+ return - EIO ;
171229
172230 ret = 0 ;
173231
@@ -190,7 +248,8 @@ static int pasemi_smb_xfer(struct i2c_adapter *adapter,
190248 addr <<= 1 ;
191249 read_flag = read_write == I2C_SMBUS_READ ;
192250
193- pasemi_smb_clear (smbus );
251+ if (pasemi_smb_clear (smbus ))
252+ return - EIO ;
194253
195254 switch (size ) {
196255 case I2C_SMBUS_QUICK :
0 commit comments