diff mbox series

[1/1] i2c: designware: Fix wrong setting for {ss,fs}_{h,l}cnt registers

Message ID 20240717065917.18399-1-ahuang12@lenovo.com
State New
Headers show
Series [1/1] i2c: designware: Fix wrong setting for {ss,fs}_{h,l}cnt registers | expand

Commit Message

Huang Adrian July 17, 2024, 6:59 a.m. UTC
From: Adrian Huang <ahuang12@lenovo.com>

When disabling CONFIG_X86_AMD_PLATFORM_DEVICE option, the driver
'drivers/acpi/acpi_apd.c' won't be compiled. This leads to a situation
where BMC (Baseboard Management Controller) cannot retrieve the memory
temperature via the i2c interface after i2c DW driver is loaded. Note
that BMC can retrieve the memory temperature before booting into OS.

[Debugging Detail]
  1. dev->pclk and dev->clk are NULL when calling devm_clk_get_optional()
     in dw_i2c_plat_probe().

  2. The callings of i2c_dw_scl_hcnt() in i2c_dw_set_timings_master()
     return 65528 (-8 in integer format) or 65533 (-3 in integer format).
     The following log shows SS's HCNT/LCNT:

       i2c_designware AMDI0010:01: Standard Mode HCNT:LCNT = 65533:65535

  3. The callings of i2c_dw_scl_lcnt() in i2c_dw_set_timings_master()
     return 65535 (-1 in integer format). The following log shows SS's
     HCNT/LCNT:

       i2c_designware AMDI0010:01: Fast Mode HCNT:LCNT = 65533:65535

  4. i2c_dw_init_master() configures the register IC_SS_SCL_HCNT with
     the value 65533. However, the DW i2c databook mentioned the value
     cannot be higher than 65525. Quote from the DW i2c databook:

       NOTE: This register must not be programmed to a value higher than
             65525, because DW_apb_i2c uses a 16-bit counter to flag an
             I2C bus idle condition when this counter reaches a value of
             IC_SS_SCL_HCNT + 10.

  5. Since ss_hcnt, ss_lcnt, fs_hcnt, and fs_lcnt are the invalid
     values, we should not write the corresponding registers.

Fix the issue by returning 0 if ic_clk is 0 in i2c_dw_scl_{h,l}cnt().
Then, do not write the corresponding registers if those values are 0.

Signed-off-by: Adrian Huang <ahuang12@lenovo.com>
Reported-by: Dong Wang <wangdong28@lenovo.com>
Tested-by: Dong Wang <wangdong28@lenovo.com>
---
 drivers/i2c/busses/i2c-designware-common.c |  6 ++++++
 drivers/i2c/busses/i2c-designware-master.c | 16 ++++++++++------
 2 files changed, 16 insertions(+), 6 deletions(-)

Comments

Jarkko Nikula Aug. 1, 2024, 2:10 p.m. UTC | #1
Hi

Sorry the delay.

On 7/17/24 9:59 AM, Adrian Huang wrote:
> From: Adrian Huang <ahuang12@lenovo.com>
> 
> When disabling CONFIG_X86_AMD_PLATFORM_DEVICE option, the driver
> 'drivers/acpi/acpi_apd.c' won't be compiled. This leads to a situation
> where BMC (Baseboard Management Controller) cannot retrieve the memory
> temperature via the i2c interface after i2c DW driver is loaded. Note
> that BMC can retrieve the memory temperature before booting into OS.
> 
> [Debugging Detail]
>    1. dev->pclk and dev->clk are NULL when calling devm_clk_get_optional()
>       in dw_i2c_plat_probe().
> 
>    2. The callings of i2c_dw_scl_hcnt() in i2c_dw_set_timings_master()
>       return 65528 (-8 in integer format) or 65533 (-3 in integer format).
>       The following log shows SS's HCNT/LCNT:
> 
>         i2c_designware AMDI0010:01: Standard Mode HCNT:LCNT = 65533:65535
> 
>    3. The callings of i2c_dw_scl_lcnt() in i2c_dw_set_timings_master()
>       return 65535 (-1 in integer format). The following log shows SS's
>       HCNT/LCNT:
> 
>         i2c_designware AMDI0010:01: Fast Mode HCNT:LCNT = 65533:65535
> 
>    4. i2c_dw_init_master() configures the register IC_SS_SCL_HCNT with
>       the value 65533. However, the DW i2c databook mentioned the value
>       cannot be higher than 65525. Quote from the DW i2c databook:
> 
>         NOTE: This register must not be programmed to a value higher than
>               65525, because DW_apb_i2c uses a 16-bit counter to flag an
>               I2C bus idle condition when this counter reaches a value of
>               IC_SS_SCL_HCNT + 10.
> 
>    5. Since ss_hcnt, ss_lcnt, fs_hcnt, and fs_lcnt are the invalid
>       values, we should not write the corresponding registers.
> 
> Fix the issue by returning 0 if ic_clk is 0 in i2c_dw_scl_{h,l}cnt().
> Then, do not write the corresponding registers if those values are 0.
> 
You are correct, indeed driver miscalculates those timing parameters in 
that case.

However debugging point of view below is misleading since it doesn't 
necessarily match with HW registers since they are not touched and have 
values what bootloader has left there.

	i2c_designware i2c_designware.0: Standard Mode HCNT:LCNT = 0:0

Would it work if patch just reads the dev->ss_hcnt, dev->ss_lcnt and so 
on from HW registers in case they and ic_clk are not set in 
i2c_dw_set_timings_master()? Then debug prints and HW values are in sync.
Huang Adrian Aug. 2, 2024, 1:06 p.m. UTC | #2
Hi,

On Thu, Aug 1, 2024 at 10:10 PM Jarkko Nikula
<jarkko.nikula@linux.intel.com> wrote:
> You are correct, indeed driver miscalculates those timing parameters in
> that case.
>
> However debugging point of view below is misleading since it doesn't
> necessarily match with HW registers since they are not touched and have
> values what bootloader has left there.
>
>         i2c_designware i2c_designware.0: Standard Mode HCNT:LCNT = 0:0

Indeed. The debug prints show the incorrect info. Thanks for reminding this.

> Would it work if patch just reads the dev->ss_hcnt, dev->ss_lcnt and so
> on from HW registers in case they and ic_clk are not set in
> i2c_dw_set_timings_master()? Then debug prints and HW values are in sync.

That makes sense. The v2 version:
https://lore.kernel.org/linux-i2c/20240802130143.26908-1-ahuang12@lenovo.com/T/#u

Please kindly review. Thank you.

-- Adrian
diff mbox series

Patch

diff --git a/drivers/i2c/busses/i2c-designware-common.c b/drivers/i2c/busses/i2c-designware-common.c
index e8a688d04aee..6c7e331c32cb 100644
--- a/drivers/i2c/busses/i2c-designware-common.c
+++ b/drivers/i2c/busses/i2c-designware-common.c
@@ -334,6 +334,9 @@  EXPORT_SYMBOL_GPL(i2c_dw_adjust_bus_speed);
 
 u32 i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset)
 {
+	if (!ic_clk)
+		return 0;
+
 	/*
 	 * DesignWare I2C core doesn't seem to have solid strategy to meet
 	 * the tHD;STA timing spec.  Configuring _HCNT based on tHIGH spec
@@ -374,6 +377,9 @@  u32 i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset)
 
 u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset)
 {
+	if (!ic_clk)
+		return 0;
+
 	/*
 	 * Conditional expression:
 	 *
diff --git a/drivers/i2c/busses/i2c-designware-master.c b/drivers/i2c/busses/i2c-designware-master.c
index c7e56002809a..68c523d9d10b 100644
--- a/drivers/i2c/busses/i2c-designware-master.c
+++ b/drivers/i2c/busses/i2c-designware-master.c
@@ -184,13 +184,17 @@  static int i2c_dw_init_master(struct dw_i2c_dev *dev)
 	/* Disable the adapter */
 	__i2c_dw_disable(dev);
 
-	/* Write standard speed timing parameters */
-	regmap_write(dev->map, DW_IC_SS_SCL_HCNT, dev->ss_hcnt);
-	regmap_write(dev->map, DW_IC_SS_SCL_LCNT, dev->ss_lcnt);
+	if (dev->ss_hcnt && dev->ss_lcnt) {
+		/* Write standard speed timing parameters */
+		regmap_write(dev->map, DW_IC_SS_SCL_HCNT, dev->ss_hcnt);
+		regmap_write(dev->map, DW_IC_SS_SCL_LCNT, dev->ss_lcnt);
+	}
 
-	/* Write fast mode/fast mode plus timing parameters */
-	regmap_write(dev->map, DW_IC_FS_SCL_HCNT, dev->fs_hcnt);
-	regmap_write(dev->map, DW_IC_FS_SCL_LCNT, dev->fs_lcnt);
+	if (dev->fs_hcnt && dev->fs_lcnt) {
+		/* Write fast mode/fast mode plus timing parameters */
+		regmap_write(dev->map, DW_IC_FS_SCL_HCNT, dev->fs_hcnt);
+		regmap_write(dev->map, DW_IC_FS_SCL_LCNT, dev->fs_lcnt);
+	}
 
 	/* Write high speed timing parameters if supported */
 	if (dev->hs_hcnt && dev->hs_lcnt) {