Skip to contentSkip to conversation

i2c: tegra: Fix HS mode clock calculations

PATCH New Jiangtian Feng 2 participants 3 messages Nov 26

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

PATCH 1/1 i2c: tegra: Fix HS mode clock calculations

The HS mode support overwrites the selected non-HS tlow/thigh values with HS timing before calculating clk_multiplier. This makes Standard, Fast and Fast+ modes use HS timing with a non-HS divisor, while HS mode still uses the non-HS divisor. The resulting div-clk is incorrect and can produce an out-of-spec SCL frequency or make clk_set_rate() fail.

Keep the non-HS timing values unchanged when programming the HS registers, and calculate the clock multiplier for the active mode. HS mode uses its own timing and clk_divisor_hs_mode; other modes retain their original timing and divisors.

The higher HS div-clk also affects the master code, which is sent using Fast-mode timing. At 3.4MHz, the default Fast divisors on Tegra256 and Tegra264 would result in master-code frequencies of about 457.7kHz and 412.1kHz respectively. Increase the non-HS divisor when necessary to keep the master code at or below 400kHz without changing the HS data rate.

Qoder, an AI coding assistant, helped analyze the issue, develop the fix and arithmetic model, and draft this description.

Fixes 978b3ccbbac3 ("i2c: tegra: Add HS mode support")
---
 drivers/i2c/busses/i2c-tegra.c | 36 ++++++++++++++++++++++------------
 1 file changed, 24 insertions(+), 12 deletions(-)
drivers/i2c/busses/i2c-tegra.c +24 -12
index a81ff2c33..4da98fe4a 100644
@@ -877,7 +877,8 @@ static int tegra_i2c_master_reset(struct tegra_i2c_dev *i2c_dev)
 
 static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
 {
-	u32 val, clk_divisor, clk_multiplier, tsu_thd, tlow, thigh, non_hs_mode;
+	u32 val, clk_divisor, clk_multiplier, clk_rate;
+	u32 tsu_thd, tlow, thigh, non_hs_mode;
 	u32 max_bus_freq_hz;
 	struct i2c_timings *t = &i2c_dev->timings;
 	int err;
@@ -957,6 +958,21 @@ static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
 		non_hs_mode = i2c_dev->hw->clk_divisor_fast_mode;
 	}
 
+	if (t->bus_freq_hz > I2C_MAX_FAST_MODE_PLUS_FREQ) {
+		clk_multiplier = (i2c_dev->hw->tlow_hs_mode +
+				  i2c_dev->hw->thigh_hs_mode + 2) *
+				 (i2c_dev->hw->clk_divisor_hs_mode + 1);
+		clk_rate = t->bus_freq_hz * clk_multiplier;
+
+		/* The master code must not exceed the Fast-mode frequency. */
+		non_hs_mode = max(non_hs_mode,
+				  DIV_ROUND_UP(clk_rate, I2C_MAX_FAST_MODE_FREQ *
+					       (tlow + thigh + 2)) - 1);
+	} else {
+		clk_multiplier = (tlow + thigh + 2) * (non_hs_mode + 1);
+		clk_rate = t->bus_freq_hz * clk_multiplier;
+	}
+
 	/* make sure clock divisor programmed correctly */
 	clk_divisor = FIELD_PREP(I2C_CLK_DIVISOR_HSMODE,
 				 i2c_dev->hw->clk_divisor_hs_mode) |
@@ -978,20 +994,16 @@ static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
 
 	/* Write HS mode registers. These will get used only for HS mode*/
 	if (i2c_dev->hw->enable_hs_mode_support) {
-		tlow = i2c_dev->hw->tlow_hs_mode;
-		thigh = i2c_dev->hw->thigh_hs_mode;
-		tsu_thd = i2c_dev->hw->setup_hold_time_hs_mode;
-
-		val = FIELD_PREP(I2C_HS_INTERFACE_TIMING_THIGH, thigh) |
-			FIELD_PREP(I2C_HS_INTERFACE_TIMING_TLOW, tlow);
+		val = FIELD_PREP(I2C_HS_INTERFACE_TIMING_THIGH,
+				 i2c_dev->hw->thigh_hs_mode) |
+		      FIELD_PREP(I2C_HS_INTERFACE_TIMING_TLOW,
+				 i2c_dev->hw->tlow_hs_mode);
 		i2c_writel(i2c_dev, val, i2c_dev->hw->regs->hs_interface_timing_0);
-		i2c_writel(i2c_dev, tsu_thd, i2c_dev->hw->regs->hs_interface_timing_1);
+		i2c_writel(i2c_dev, i2c_dev->hw->setup_hold_time_hs_mode,
+			   i2c_dev->hw->regs->hs_interface_timing_1);
 	}
 
-	clk_multiplier = (tlow + thigh + 2) * (non_hs_mode + 1);
-
-	err = clk_set_rate(i2c_dev->div_clk,
-			   t->bus_freq_hz * clk_multiplier);
+	err = clk_set_rate(i2c_dev->div_clk, clk_rate);
 	if (err) {
 		dev_err(i2c_dev->dev, "failed to set div-clk rate: %d\n", err);
 		return err;

base-commit: 8d7fe9885c62fc9a95bf55d9db244d9672a8aadc

Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/1 i2c: tegra: Fix HS mode clock calculations

Hi Jiangtian Feng,

Quoted 17 lines · Jiangtian Feng

The HS mode support overwrites the selected non-HS tlow/thigh values with HS timing before calculating clk_multiplier. This makes Standard, Fast and Fast+ modes use HS timing with a non-HS divisor, while HS mode still uses the non-HS divisor. The resulting div-clk is incorrect and can produce an out-of-spec SCL frequency or make clk_set_rate() fail.

Keep the non-HS timing values unchanged when programming the HS registers, and calculate the clock multiplier for the active mode. HS mode uses its own timing and clk_divisor_hs_mode; other modes retain their original timing and divisors.

The higher HS div-clk also affects the master code, which is sent using Fast-mode timing. At 3.4MHz, the default Fast divisors on Tegra256 and Tegra264 would result in master-code frequencies of about 457.7kHz and 412.1kHz respectively. Increase the non-HS divisor when necessary to keep the master code at or below 400kHz without changing the HS data rate.

Quoted 6 lines

Qoder, an AI coding assistant, helped analyze the issue, develop the fix and arithmetic model, and draft this description.

Fixes 978b3ccbbac3 ("i2c: tegra: Add HS mode support")

---

Thanks for sharing the patch, but I had a different change which should fix the issue -
https://lore.kernel.org/linux-tegra/[email protected]/T/#t

Hi Andi,
Did you get a chance to check the above fix which I posted a couple of weeks back? Please let me know if you see any concerns with that.

Regards,
Akhil

#2 lore raw

Hi Akhil,

Quoted 2 lines · Akhil R

Thanks for sharing the patch, but I had a different change which should fix the issue.

Thank you for pointing me to your earlier patch. Moving clk_multiplier before the HS register setup fixes the non-HS timing overwrite.

Could you clarify two cases that appear not to be covered by that version?

For an HS request, the multiplier still uses non_hs_mode rather than clk_divisor_hs_mode. Is that intentional for the controller, or should HS use its own timing/divisor?

At 3.4MHz on Tegra256 and Tegra264, the default Fast divisors also produce master-code frequencies of about 457.7kHz and 412.1kHz. Is a separate fix needed to keep the master code at or below 400kHz?

Our patch attempts to cover both cases by selecting the HS timing/divisor for HS transfers and increasing the non-HS divisor only when necessary to cap the master-code rate. I would appreciate your feedback on whether this matches the controller behavior and how best to align with your patch.

Best regards,
Jiangtian