Skip to contentSkip to conversation

i2c: tegra: Fix HS mode clock calculations

PATCH Superseded Jiangtian Feng 2 participants 7 messages Nov 26 – Nov 27

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

#3 lore raw
Quoted 4 lines · Jiangtian Feng
Quoted 2 lines · Akhil R

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

Quoted 9 lines

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?

Thanks for pointing this out. I overlooked how clk_multiplier is used for HS mode.

Quoted 3 lines

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?

The controller clock is derived from its source using discrete divider settings, so its actual frequency may differ from the requested rate. With the intended clock configuration, the master-code frequency remains within the Fast Mode limit even when HS timings are used to calculate the multiplier. Also the non-HS divisor should not be modified since those are calibrated values to comply with the I2C specs.

Quoted 5 lines

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.

I would prefer a smaller change like the one below. Let me know if you would want to take it up or I can push this separately.

drivers/i2c/busses/i2c-tegra.c +6 -3
index 76d21bc51c618..adb6c6c0b887b 100644
@@ -976,8 +976,10 @@ static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
 	if (i2c_dev->hw->has_interface_timing_reg && tsu_thd)
 		i2c_writel(i2c_dev, tsu_thd, i2c_dev->hw->regs->interface_timing_1);
 
+	clk_multiplier = (tlow + thigh + 2) * (non_hs_mode + 1);
+
 	/* Write HS mode registers. These will get used only for HS mode*/
-	if (i2c_dev->hw->enable_hs_mode_support) {
+	if (i2c_dev->hw->enable_hs_mode_support && t->bus_freq_hz > I2C_MAX_FAST_MODE_PLUS_FREQ) {
 		tlow = i2c_dev->hw->tlow_hs_mode;
 		thigh = i2c_dev->hw->thigh_hs_mode;
 		tsu_thd = i2c_dev->hw->setup_hold_time_hs_mode;
@@ -986,9 +988,10 @@ static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
 			FIELD_PREP(I2C_HS_INTERFACE_TIMING_TLOW, tlow);
 		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);
-	}
 
-	clk_multiplier = (tlow + thigh + 2) * (non_hs_mode + 1);
+		clk_multiplier = (tlow + thigh + 2) *
+				 (i2c_dev->hw->clk_divisor_hs_mode + 1);
+	}
 
 	err = clk_set_rate(i2c_dev->div_clk,
 			   t->bus_freq_hz * clk_multiplier);

Best Regards,
Akhil

#4 lore raw

Hi Akhil,

Thanks, this version addresses both the non-HS timing overwrite and the HS timing/divisor selection.

You are right that my 457.7/412.1 kHz figures were calculated from the requested clk_rate, not from the post-rounding clk_get_rate(), so they do not by themselves prove that the calibrated divisors need changing.

Could you share the intended post-clk_set_rate div-clk rates, or the relevant firmware rounding/table entries, for Tegra256 and Tegra264 at a 3.4 MHz request? With the default Fast divisors, the master code stays at or below 400 kHz if the actual rates are at most 208 MHz and 52.8 MHz, respectively.

If that is the case, I agree that we should leave non_hs_mode unchanged and I will drop the cap in favor of your smaller change. If the actual rates are higher, we can treat the master-code issue separately using the actual clock rate.

Best regards,
Jiangtian

#5 lore raw
Quoted 9 lines · Jiangtian Feng

Hi Akhil,

Thanks, this version addresses both the non-HS timing overwrite and the HS timing/divisor selection.

You are right that my 457.7/412.1 kHz figures were calculated from the requested clk_rate, not from the post-rounding clk_get_rate(), so they do not by themselves prove that the calibrated divisors need changing.

Quoted 5 lines

Could you share the intended post-clk_set_rate div-clk rates, or the relevant firmware rounding/table entries, for Tegra256 and Tegra264 at a 3.4 MHz request? With the default Fast divisors, the master code stays at or below 400 kHz if the actual rates are at most 208 MHz and 52.8 MHz, respectively.

For Tegra264, with PLLP_OUT0 at 202.5 MHz and clock divisor 3, the actual controller clock is 50.625 MHz, giving a master-code frequency of approx. 383.5 kHz. Tegra256 uses a fixed-clock of 208 MHz.

Quoted 6 lines

If that is the case, I agree that we should leave non_hs_mode unchanged and I will drop the cap in favor of your smaller change. If the actual rates are higher, we can treat the master-code issue separately using the actual clock rate.

Best Regards,
Akhil R

#6 lore raw

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

In reply to 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.

Calculate the non-HS multiplier before writing the HS registers. For an actual HS request, use the HS timing and clk_divisor_hs_mode; other modes retain their calibrated timing and divisors.

Keep non_hs_mode unchanged. Akhil provided the intended clock configurations which keep the master code within the Fast-mode limit:
Tegra256 uses a fixed 208MHz clock, resulting in 400kHz with the default Fast divisor, and Tegra264 uses 202.5MHz/3 = 50.625MHz, resulting in about 383.5kHz. These are platform configuration values, not measurements from this submission.

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 | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/i2c/busses/i2c-tegra.c +7 -3
index a81ff2c339f2..a63beb06fa10 100644
@@ -976,8 +976,11 @@ static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
 	if (i2c_dev->hw->has_interface_timing_reg && tsu_thd)
 		i2c_writel(i2c_dev, tsu_thd, i2c_dev->hw->regs->interface_timing_1);
 
+	clk_multiplier = (tlow + thigh + 2) * (non_hs_mode + 1);
+
 	/* Write HS mode registers. These will get used only for HS mode*/
-	if (i2c_dev->hw->enable_hs_mode_support) {
+	if (i2c_dev->hw->enable_hs_mode_support &&
+	    t->bus_freq_hz > I2C_MAX_FAST_MODE_PLUS_FREQ) {
 		tlow = i2c_dev->hw->tlow_hs_mode;
 		thigh = i2c_dev->hw->thigh_hs_mode;
 		tsu_thd = i2c_dev->hw->setup_hold_time_hs_mode;
@@ -986,9 +989,10 @@ static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
 			FIELD_PREP(I2C_HS_INTERFACE_TIMING_TLOW, tlow);
 		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);
-	}
 
-	clk_multiplier = (tlow + thigh + 2) * (non_hs_mode + 1);
+		clk_multiplier = (tlow + thigh + 2) *
+				 (i2c_dev->hw->clk_divisor_hs_mode + 1);
+	}
 
 	err = clk_set_rate(i2c_dev->div_clk,
 			   t->bus_freq_hz * clk_multiplier);
Signature · 1 line
2.47.3