Skip to contentSkip to conversation

I2C: i.MX: early: Use a custom delay on i.MX6

PATCH Applied John Watts 2 participants 3 messages Feb 1, 2023 – Feb 2, 2023

Subsystems (2)
lore
Share & tools
lore

John Watts

author
#0 lore raw

PATCH 1/1 I2C: i.MX: early: Use a custom delay on i.MX6

The i.MX early I2C code requires waiting for the controller to settle after configuration. This is currently done using udelay which is supported on ARMv8 but not on ARMv7.

For the i.MX6 we will have to use a custom delay. This was only tested on the i.MX6Q but should work on all other i.MX6 chips.

---
 drivers/i2c/busses/i2c-imx-early.c | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)
drivers/i2c/busses/i2c-imx-early.c +23 -1
index 6c8bdc7904..30cc65ca86 100644
@@ -90,6 +90,28 @@ static int i2c_fsl_acked(struct fsl_i2c *fsl_i2c)
 	return i2c_fsl_poll_status(fsl_i2c, 0, I2SR_RXAK);
 }
 
+static void i2c_fsl_settle(struct fsl_i2c *fsl_i2c)
+{
+#ifdef CPU_ARCH_ARMv8
+	udelay(100);
+#else
+	/*
+	 * We lack udelay on the i.MX6 prebootloader, so delay
+	 * manually: On an i.MX6 with a 66Mhz I2C peripheral clock
+	 * one cycle of this loop takes 1.30us,
+	 * this means we have to wait for 76.9 microseconds.
+	 *
+	 * This clock may be 44Mhz if the BT_FREQ eFUSE is set,
+	 * this just means it will take 150 microseconds.
+	 */
+
+	volatile int i = 0;
+
+	for (i = 0; i < 77; i++)
+		fsl_i2c_read_reg(fsl_i2c, FSL_I2C_I2SR);
+#endif
+}
+
 static int i2c_fsl_start(struct fsl_i2c *fsl_i2c)
 {
 	unsigned int temp = 0;
@@ -104,7 +126,7 @@ static int i2c_fsl_start(struct fsl_i2c *fsl_i2c)
 			  fsl_i2c, FSL_I2C_I2CR);
 
 	/* Wait controller to be stable */
-	udelay(100);
+	i2c_fsl_settle(fsl_i2c);
 
 	/* Start I2C transaction */
 	temp = fsl_i2c_read_reg(fsl_i2c, FSL_I2C_I2CR);
Signature · 1 line
2.39.1
Applied
#1 lore raw
Quoted 24 lines · John Watts

The i.MX early I2C code requires waiting for the controller to settle after configuration. This is currently done using udelay which is supported on ARMv8 but not on ARMv7.

For the i.MX6 we will have to use a custom delay. This was only tested on the i.MX6Q but should work on all other i.MX6 chips.

---
 drivers/i2c/busses/i2c-imx-early.c | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)
drivers/i2c/busses/i2c-imx-early.c +4 -0
index 6c8bdc7904..30cc65ca86 100644
@@ -90,6 +90,28 @@ static int i2c_fsl_acked(struct fsl_i2c *fsl_i2c)
 	return i2c_fsl_poll_status(fsl_i2c, 0, I2SR_RXAK);
 }
 
+static void i2c_fsl_settle(struct fsl_i2c *fsl_i2c)
+{
+#ifdef CPU_ARCH_ARMv8
+	udelay(100);

drivers/i2c/busses/i2c-imx-early.c static int i2c_fsl_acked(struct fsl_i2c *fsl_i2c) · line 106

Quoted 10 lines
drivers/i2c/busses/i2c-imx-early.c +10 -0
+#else
+	/*
+	 * We lack udelay on the i.MX6 prebootloader, so delay
+	 * manually: On an i.MX6 with a 66Mhz I2C peripheral clock
+	 * one cycle of this loop takes 1.30us,
+	 * this means we have to wait for 76.9 microseconds.
+	 *
+	 * This clock may be 44Mhz if the BT_FREQ eFUSE is set,
+	 * this just means it will take 150 microseconds.
+	 */

I rounded up to 100 cycles and rephrased this to be a little less i.MX6 centric:

/*
* We lack udelay on the 32bit i.MX SoCs, so delay manually: On an
* i.MX6 with a 66Mhz I2C peripheral clock one cycle of this loop
* takes 1.30us. Let's be generous and round up to 100 cycles. Other
* i.MX SoCs do not have a higher peripheral clock, so we should be
* safe here as well.
*/

In the end it doesn't hurt when it takes a little longer.

Applied, thanks

Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
author
#2 lore raw
Quoted 11 lines · Sascha Hauer

I rounded up to 100 cycles and rephrased this to be a little less i.MX6 centric:

/*
* We lack udelay on the 32bit i.MX SoCs, so delay manually: On an
* i.MX6 with a 66Mhz I2C peripheral clock one cycle of this loop
* takes 1.30us. Let's be generous and round up to 100 cycles. Other
* i.MX SoCs do not have a higher peripheral clock, so we should be
* safe here as well.
*/

Quoted 1 line

In the end it doesn't hurt when it takes a little longer.

Makes sense. Thanks!

Quoted 3 lines

Applied, thanks

Sascha

John.