2323
2424#include <linux/clk.h>
2525#include <linux/io.h>
26+ #include <linux/iopoll.h>
2627#include <linux/fsl_devices.h>
2728#include <linux/i2c.h>
2829#include <linux/interrupt.h>
4950#define CCR_MTX 0x10
5051#define CCR_TXAK 0x08
5152#define CCR_RSTA 0x04
53+ #define CCR_RSVD 0x02
5254
5355#define CSR_MCF 0x80
5456#define CSR_MAAS 0x40
@@ -70,6 +72,7 @@ struct mpc_i2c {
7072 u8 fdr , dfsrr ;
7173#endif
7274 struct clk * clk_per ;
75+ bool has_errata_A004447 ;
7376};
7477
7578struct mpc_i2c_divider {
@@ -176,6 +179,75 @@ static int i2c_wait(struct mpc_i2c *i2c, unsigned timeout, int writing)
176179 return 0 ;
177180}
178181
182+ static int i2c_mpc_wait_sr (struct mpc_i2c * i2c , int mask )
183+ {
184+ void __iomem * addr = i2c -> base + MPC_I2C_SR ;
185+ u8 val ;
186+
187+ return readb_poll_timeout (addr , val , val & mask , 0 , 100 );
188+ }
189+
190+ /*
191+ * Workaround for Erratum A004447. From the P2040CE Rev Q
192+ *
193+ * 1. Set up the frequency divider and sampling rate.
194+ * 2. I2CCR - a0h
195+ * 3. Poll for I2CSR[MBB] to get set.
196+ * 4. If I2CSR[MAL] is set (an indication that SDA is stuck low), then go to
197+ * step 5. If MAL is not set, then go to step 13.
198+ * 5. I2CCR - 00h
199+ * 6. I2CCR - 22h
200+ * 7. I2CCR - a2h
201+ * 8. Poll for I2CSR[MBB] to get set.
202+ * 9. Issue read to I2CDR.
203+ * 10. Poll for I2CSR[MIF] to be set.
204+ * 11. I2CCR - 82h
205+ * 12. Workaround complete. Skip the next steps.
206+ * 13. Issue read to I2CDR.
207+ * 14. Poll for I2CSR[MIF] to be set.
208+ * 15. I2CCR - 80h
209+ */
210+ static void mpc_i2c_fixup_A004447 (struct mpc_i2c * i2c )
211+ {
212+ int ret ;
213+ u32 val ;
214+
215+ writeccr (i2c , CCR_MEN | CCR_MSTA );
216+ ret = i2c_mpc_wait_sr (i2c , CSR_MBB );
217+ if (ret ) {
218+ dev_err (i2c -> dev , "timeout waiting for CSR_MBB\n" );
219+ return ;
220+ }
221+
222+ val = readb (i2c -> base + MPC_I2C_SR );
223+
224+ if (val & CSR_MAL ) {
225+ writeccr (i2c , 0x00 );
226+ writeccr (i2c , CCR_MSTA | CCR_RSVD );
227+ writeccr (i2c , CCR_MEN | CCR_MSTA | CCR_RSVD );
228+ ret = i2c_mpc_wait_sr (i2c , CSR_MBB );
229+ if (ret ) {
230+ dev_err (i2c -> dev , "timeout waiting for CSR_MBB\n" );
231+ return ;
232+ }
233+ val = readb (i2c -> base + MPC_I2C_DR );
234+ ret = i2c_mpc_wait_sr (i2c , CSR_MIF );
235+ if (ret ) {
236+ dev_err (i2c -> dev , "timeout waiting for CSR_MIF\n" );
237+ return ;
238+ }
239+ writeccr (i2c , CCR_MEN | CCR_RSVD );
240+ } else {
241+ val = readb (i2c -> base + MPC_I2C_DR );
242+ ret = i2c_mpc_wait_sr (i2c , CSR_MIF );
243+ if (ret ) {
244+ dev_err (i2c -> dev , "timeout waiting for CSR_MIF\n" );
245+ return ;
246+ }
247+ writeccr (i2c , CCR_MEN );
248+ }
249+ }
250+
179251#if defined(CONFIG_PPC_MPC52xx ) || defined(CONFIG_PPC_MPC512x )
180252static const struct mpc_i2c_divider mpc_i2c_dividers_52xx [] = {
181253 {20 , 0x20 }, {22 , 0x21 }, {24 , 0x22 }, {26 , 0x23 },
@@ -641,7 +713,10 @@ static int fsl_i2c_bus_recovery(struct i2c_adapter *adap)
641713{
642714 struct mpc_i2c * i2c = i2c_get_adapdata (adap );
643715
644- mpc_i2c_fixup (i2c );
716+ if (i2c -> has_errata_A004447 )
717+ mpc_i2c_fixup_A004447 (i2c );
718+ else
719+ mpc_i2c_fixup (i2c );
645720
646721 return 0 ;
647722}
@@ -745,6 +820,8 @@ static int fsl_i2c_probe(struct platform_device *op)
745820 dev_info (i2c -> dev , "timeout %u us\n" , mpc_ops .timeout * 1000000 / HZ );
746821
747822 platform_set_drvdata (op , i2c );
823+ if (of_property_read_bool (op -> dev .of_node , "fsl,i2c-erratum-a004447" ))
824+ i2c -> has_errata_A004447 = true;
748825
749826 i2c -> adap = mpc_ops ;
750827 of_address_to_resource (op -> dev .of_node , 0 , & res );
0 commit comments