Skip to contentSkip to conversation

i2c: bcm2835: Set clock-stretch timeout to 35ms

PATCH In review Ivan T. Ivanov 2 participants 5 messages Jan 17, 2022 – Feb 7, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i2c: bcm2835: Set clock-stretch timeout to 35ms

From: Phil Elwell <[email protected]>

The BCM2835 I2C blocks have a register to set the clock-stretch timeout - how long the device is allowed to hold SCL low - in bus cycles. The current driver doesn't write to the register, therefore the default value of 64 cycles is being used for all devices.

Set the timeout to the value recommended for SMBus - 35ms.

See: https://github.com/raspberrypi/linux/issues/3064

[[email protected] fix small coding style issue]
---
 drivers/i2c/busses/i2c-bcm2835.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/i2c/busses/i2c-bcm2835.c +12 -0
index 37443edbf754..9cce0e15da32 100644
@@ -106,6 +106,7 @@ static int clk_bcm2835_i2c_set_rate(struct clk_hw *hw, unsigned long rate,
 {
 	struct clk_bcm2835_i2c *div = to_clk_bcm2835_i2c(hw);
 	u32 redl, fedl;
+	u32 clk_tout;
 	u32 divider = clk_bcm2835_i2c_calc_divider(rate, parent_rate);
 
 	if (divider == -EINVAL)
@@ -129,6 +130,17 @@ static int clk_bcm2835_i2c_set_rate(struct clk_hw *hw, unsigned long rate,
 	bcm2835_i2c_writel(div->i2c_dev, BCM2835_I2C_DEL,
 			   (fedl << BCM2835_I2C_FEDL_SHIFT) |
 			   (redl << BCM2835_I2C_REDL_SHIFT));
+
+	/*
+	 * Set the clock stretch timeout to the SMBUs-recommended 35ms.
+	 */
+	if (rate > 0xffff * 1000 / 35)
+		clk_tout = 0xffff;
+	else
+		clk_tout = 35 * rate / 1000;
+
+	bcm2835_i2c_writel(div->i2c_dev, BCM2835_I2C_CLKT, clk_tout);
+
 	return 0;
 }
 
Signature · 1 line
2.26.2
#1 lore raw
Quoted 9 lines · Ivan T. Ivanov

Date: Mon, 17 Jan 2022 11:25:04 +0100
From: "Ivan T. Ivanov" <[email protected]>
To: Florian Fainelli <[email protected]>, Ray Jui <[email protected]>,
Scott Branden <[email protected]>, Nicolas Saenz Julienne <[email protected]>
Cc: [email protected], [email protected], Phil
Elwell <[email protected]>, "Ivan T . Ivanov" <[email protected]>
Subject: [PATCH] i2c: bcm2835: Set clock-stretch timeout to 35ms
Message-Id: <[email protected]>

Tags: all arm i2c linux me ring rpi sent

Hi,

Quoted 11 lines

From: Phil Elwell <[email protected]>

The BCM2835 I2C blocks have a register to set the clock-stretch timeout - how long the device is allowed to hold SCL low - in bus cycles. The current driver doesn't write to the register, therefore the default value of 64 cycles is being used for all devices.

Set the timeout to the value recommended for SMBus - 35ms.

See: https://github.com/raspberrypi/linux/issues/3064

It will be nice if we have this fix upstream.

Regards,
Ivan

Quoted 41 lines
[[email protected] fix small coding style issue]
---
 drivers/i2c/busses/i2c-bcm2835.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/i2c/busses/i2c-bcm2835.c +12 -0
index 37443edbf754..9cce0e15da32 100644
@@ -106,6 +106,7 @@ static int clk_bcm2835_i2c_set_rate(struct clk_hw *hw, unsigned long rate,
 {
 	struct clk_bcm2835_i2c *div = to_clk_bcm2835_i2c(hw);
 	u32 redl, fedl;
+	u32 clk_tout;
 	u32 divider = clk_bcm2835_i2c_calc_divider(rate, parent_rate);
 
 	if (divider == -EINVAL)
@@ -129,6 +130,17 @@ static int clk_bcm2835_i2c_set_rate(struct clk_hw *hw, unsigned long rate,
 	bcm2835_i2c_writel(div->i2c_dev, BCM2835_I2C_DEL,
 			   (fedl << BCM2835_I2C_FEDL_SHIFT) |
 			   (redl << BCM2835_I2C_REDL_SHIFT));
+
+	/*
+	 * Set the clock stretch timeout to the SMBUs-recommended 35ms.
+	 */
+	if (rate > 0xffff * 1000 / 35)
+		clk_tout = 0xffff;
+	else
+		clk_tout = 35 * rate / 1000;
+
+	bcm2835_i2c_writel(div->i2c_dev, BCM2835_I2C_CLKT, clk_tout);
+
 	return 0;
 }
 
Signature · 1 line
2.26.2
#2 lore raw
In reply to Ivan T. Ivanov

Hi Ivan,

Quoted 6 lines

The BCM2835 I2C blocks have a register to set the clock-stretch timeout - how long the device is allowed to hold SCL low - in bus cycles. The current driver doesn't write to the register, therefore the default value of 64 cycles is being used for all devices.

Set the timeout to the value recommended for SMBus - 35ms.

By default, busses are I2C and not SMBus, so it looks like we should finally apply this patch which disables CLKT:

http://patchwork.ozlabs.org/project/linux-i2c/patch/[email protected]/

If you really want the SMBus timeout applied, you can check for a "smbus" property in DT and then set CLKT accordingly in a seperate patch.

Kind regards,

Wolfram

#3 lore raw
Quoted 5 lines

By default, busses are I2C and not SMBus, so it looks like we should finally apply this patch which disables CLKT:

http://patchwork.ozlabs.org/project/linux-i2c/patch/[email protected]/

I applied this now to for-current and added stable. That should avoid unwanted timeouts.

Quoted 3 lines

If you really want the SMBus timeout applied, you can check for a "smbus" property in DT and then set CLKT accordingly in a seperate patch.

This still holds.

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#4 lore raw
Quoted 9 lines · Wolfram Sang

Date: Mon, 7 Feb 2022 16:15:24 +0100
From: Wolfram Sang <[email protected]>
To: "Ivan T. Ivanov" <[email protected]>, Florian Fainelli
<[email protected]>, Ray Jui <[email protected]>, Scott Branden <[email protected]>, Nicolas Saenz Julienne <[email protected]>, [email protected], [email protected], Phil Elwell <[email protected]>
Subject: Re: [PATCH] i2c: bcm2835: Set clock-stretch timeout to 35ms
Message-ID: <YgE3jMkzisFZFHb5@shikoro>

Tags: all arm i2c linux me ring rpi

Quoted 9 lines
Quoted 6 lines

By default, busses are I2C and not SMBus, so it looks like we should finally apply this patch which disables CLKT:

http://patchwork.ozlabs.org/project/linux-i2c/patch/[email protected]/

I applied this now to for-current and added stable. That should avoid unwanted timeouts.

Thanks! Unfortunately reporter went silent after I have prepared for him kernel package with _this_ patch [1], so it will be difficult to verify above change on my side.

Quoted 6 lines
Quoted 4 lines

If you really want the SMBus timeout applied, you can check for a "smbus" property in DT and then set CLKT accordingly in a seperate patch.

This still holds.

Sure, will keep it in mind.

Regards,
Ivan

[1] https://bugzilla.suse.com/show_bug.cgi?id=1192976