Skip to contentSkip to conversation

Starfive reboot fix & cleanup

PATCH Superseded Nam Cao 2 participants 17 messages Feb 2, 2024 – Feb 6, 2024

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Nam Cao

author
#0 lore raw

v3 cover Starfive reboot fix & cleanup

Hi,

I previously sent 2 separate series fixing the reboot of the Visionfive2 board. However the Starfive folks did not receive both series, and also because the 2 series are closely related; I am merging those 2 separate series together and re-send them as a v3.

This series:
- Rename device tree compatible strings to match U-Boot
- Fix code that extract I2C address from device tree
- Cleanup

The device tree nodes in this series have just been merged to U-Boot a few days ago. So please use most recent upstream U-Boot for testing this series.

Nam Cao (7):
platform: starfive: correct system clock device tree node
platform: starfive: get I2C offset address from clocks property
platform: starfive: remove redundant compatibility check in pmic_ops
platform: starfive: rename "stf,axp15060-regulator" ->

    "x-powers,axp15060"
  platform: starfive: drop "compatible" from struct pmic
  platform: starfive: return error if needed devices are not present
  platform: starfive: call starfive_jh7110_inst_init() in
    pm_reset_init()
 platform/generic/starfive/jh7110.c | 70 ++++++++++++++++--------------
 1 file changed, 38 insertions(+), 32 deletions(-)
Signature · 1 line
2.39.2
author
#1 lore raw

PATCH v3 1/7 platform: starfive: correct system clock device tree node

Starfive names the system clock device tree node "starfive,jh7110-clkgen" in all their git repositories. However, a different name is used in upstream U-Boot (and also Linux): "starfive,jh7110-syscrg". Since OpenSBI gets the device tree from U-Boot, this inconsistency leads the problem that OpenSBI doesn't know the system clock device exists.

Correct this name to keep the consistency.

---
 platform/generic/starfive/jh7110.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
platform/generic/starfive/jh7110.c +1 -1
index dcd6306..4b22175 100644
@@ -252,7 +252,7 @@ static int starfive_jh7110_inst_init(void *fdt)
 		jh7110_inst.pmu_reg_base = addr;
 	}
 
-	noff = fdt_node_offset_by_compatible(fdt, -1, "starfive,jh7110-clkgen");
+	noff = fdt_node_offset_by_compatible(fdt, -1, "starfive,jh7110-syscrg");
 	if (-1 < noff) {
 		rc = fdt_get_node_addr_size(fdt, noff, 0, &addr, NULL);
 		if (rc)
Signature · 1 line
2.39.2
author
#2 lore raw

PATCH v3 2/7 platform: starfive: get I2C offset address from clocks property

In reply to Nam Cao

The current code gets the I2C offset address using the device tree node
name: it get the I2C device index from the 4th character in the node name (for example, "i2c5" -> i2c device 5). However, the device tree node's name in U-Boot is actually just "i2c" without the number, so the current code cannot be used with the device tree from U-Boot.

Get the I2C offset address from the "clocks" property instead.

---
 platform/generic/starfive/jh7110.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
platform/generic/starfive/jh7110.c +10 -9
index 4b22175..846b068 100644
@@ -29,7 +29,7 @@ struct pmic {
 struct jh7110 {
 	u64 pmu_reg_base;
 	u64 clk_reg_base;
-	u32 i2c_index;
+	u32 i2c_clk_offset;
 };
 
 static struct pmic pmic_inst;
@@ -163,10 +163,7 @@ static void pmic_i2c_clk_enable(void)
 	unsigned long clock_base;
 	unsigned int val;
 
-	clock_base = jh7110_inst.clk_reg_base +
-		I2C_APB_CLK_OFFSET +
-		(jh7110_inst.i2c_index << 2);
-
+	clock_base = jh7110_inst.clk_reg_base + jh7110_inst.i2c_clk_offset;
 	val = readl((void *)clock_base);
 
 	if (!val)
@@ -241,7 +238,8 @@ static struct fdt_reset fdt_reset_pmic = {
 static int starfive_jh7110_inst_init(void *fdt)
 {
 	int noff, rc = 0;
-	const char *name;
+	const fdt32_t *val;
+	int len;
 	u64 addr;
 
 	noff = fdt_node_offset_by_compatible(fdt, -1, "starfive,jh7110-pmu");
@@ -261,9 +259,12 @@ static int starfive_jh7110_inst_init(void *fdt)
 	}
 
 	if (pmic_inst.adapter) {
-		name = fdt_get_name(fdt, pmic_inst.adapter->id, NULL);
-		if (!sbi_strncmp(name, "i2c", 3))
-			jh7110_inst.i2c_index = name[3] - '0';
+		val = fdt_getprop(fdt, pmic_inst.adapter->id, "clocks", &len);
+		/* The clocks property looks like this: clocks = <&syscrg JH7110_SYSCLK_I2C5_APB>;
+		 * So check that the length is 8 bytes, and get the offset from the second value
+		 */
+		if (val && len == 8)
+			jh7110_inst.i2c_clk_offset = fdt32_to_cpu(val[1]) << 2;
 		else
 			rc = SBI_EINVAL;
 	}
Signature · 1 line
2.39.2
author
#6 lore raw

PATCH v3 3/7 platform: starfive: remove redundant compatibility check in pmic_ops

In reply to Nam Cao

pmic_ops() is only called if a compatible device is found in device tree. It is redundant for this function to check the compability again. Remove this check.

---
 platform/generic/starfive/jh7110.c | 36 ++++++++++++++----------------
 1 file changed, 17 insertions(+), 19 deletions(-)
platform/generic/starfive/jh7110.c +17 -19
index 846b068..d3647c2 100644
@@ -134,28 +134,26 @@ static void pmic_ops(struct pmic *pmic, int type)
 	if (ret)
 		return;
 
-	if (!sbi_strcmp("stf,axp15060-regulator", pmic->compatible)) {
-		ret = i2c_adapter_reg_read(pmic->adapter, pmic->dev_addr,
-					   AXP15060_POWER_REG, &val);
-
-		if (ret) {
-			sbi_printf("%s: cannot read pmic power register\n",
-				   __func__);
-			return;
-		}
+	ret = i2c_adapter_reg_read(pmic->adapter, pmic->dev_addr,
+					AXP15060_POWER_REG, &val);
+
+	if (ret) {
+		sbi_printf("%s: cannot read pmic power register\n",
+				__func__);
+		return;
+	}
 
+	val |= AXP15060_POWER_OFF_BIT;
+	if (type == SBI_SRST_RESET_TYPE_SHUTDOWN)
 		val |= AXP15060_POWER_OFF_BIT;
-		if (type == SBI_SRST_RESET_TYPE_SHUTDOWN)
-			val |= AXP15060_POWER_OFF_BIT;
-		else
-			val |= AXP15060_RESET_BIT;
+	else
+		val |= AXP15060_RESET_BIT;
 
-		ret = i2c_adapter_reg_write(pmic->adapter, pmic->dev_addr,
-					    AXP15060_POWER_REG, val);
-		if (ret)
-			sbi_printf("%s: cannot write pmic power register\n",
-				   __func__);
-	}
+	ret = i2c_adapter_reg_write(pmic->adapter, pmic->dev_addr,
+					AXP15060_POWER_REG, val);
+	if (ret)
+		sbi_printf("%s: cannot write pmic power register\n",
+				__func__);
 }
 
 static void pmic_i2c_clk_enable(void)
Signature · 1 line
2.39.2
author
#7 lore raw

PATCH v3 4/7 platform: starfive: rename "stf,axp15060-regulator" -> "x-powers,axp15060"

In reply to Nam Cao

OpenSBI uses the device tree compatible string "stf,axp15060-regulator" for the regulator node. However, the string used by U-Boot (and Linux) is actually "x-powers,axp15060". As OpenSBI gets the device tree from U-Boot, this causes the regulator device to be undetected, and OpenSBI does not use this device to perform board reset/shutdown.

Rename this device tree compatible string to match U-Boot (and Linux).

---
 platform/generic/starfive/jh7110.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
platform/generic/starfive/jh7110.c +1 -1
index d3647c2..385e09c 100644
@@ -224,7 +224,7 @@ static int pm_reset_init(void *fdt, int nodeoff,
 }
 
 static const struct fdt_match pm_reset_match[] = {
-	{ .compatible = "stf,axp15060-regulator", .data = (void *)true },
+	{ .compatible = "x-powers,axp15060", .data = (void *)true },
 	{ },
 };
 
Signature · 1 line
2.39.2
author
#10 lore raw

PATCH v3 5/7 platform: starfive: drop "compatible" from struct pmic

In reply to Nam Cao

The member "compatible" of struct pmic is not used. Delete it.

---
 platform/generic/starfive/jh7110.c | 2 --
 1 file changed, 2 deletions(-)
platform/generic/starfive/jh7110.c +0 -2
index 385e09c..f64e016 100644
@@ -23,7 +23,6 @@
 struct pmic {
 	struct i2c_adapter *adapter;
 	u32 dev_addr;
-	const char *compatible;
 };
 
 struct jh7110 {
@@ -205,7 +204,6 @@ static int pm_reset_init(void *fdt, int nodeoff,
 		return rc;
 
 	pmic_inst.dev_addr = addr;
-	pmic_inst.compatible = match->compatible;
 
 	i2c_bus = fdt_parent_offset(fdt, nodeoff);
 	if (i2c_bus < 0)
Signature · 1 line
2.39.2
author
#13 lore raw

PATCH v3 6/7 platform: starfive: return error if needed devices are not present

In reply to Nam Cao

Jh7110's reset driver needs power management device and clock controller device to work. Currently, the driver proceed anyway without these devices, and invalid addresses (jh7110_inst.pmu_reg_base and jh7110_inst.clk_reg_base) are used during reboot, which causes unpredictable broken behaviors.

If these devices are not present, return -SBI_ENODEV.

---
 platform/generic/starfive/jh7110.c | 4 ++++
 1 file changed, 4 insertions(+)
platform/generic/starfive/jh7110.c +4 -0
index f64e016..63baddb 100644
@@ -244,6 +244,8 @@ static int starfive_jh7110_inst_init(void *fdt)
 		if (rc)
 			goto err;
 		jh7110_inst.pmu_reg_base = addr;
+	} else {
+		return -SBI_ENODEV;
 	}
 
 	noff = fdt_node_offset_by_compatible(fdt, -1, "starfive,jh7110-syscrg");
@@ -252,6 +254,8 @@ static int starfive_jh7110_inst_init(void *fdt)
 		if (rc)
 			goto err;
 		jh7110_inst.clk_reg_base = addr;
+	} else {
+		return -SBI_ENODEV;
 	}
 
 	if (pmic_inst.adapter) {
Signature · 1 line
2.39.2
author
#14 lore raw

PATCH v3 7/7 platform: starfive: call starfive_jh7110_inst_init() in pm_reset_init()

In reply to Nam Cao

The function starfive_jh7110_inst_init() initialize some power management unit address and clock addresses, needed for the reset driver. It doesn't do anything else, and also the reset driver doesn't work without calling this function. Thus, it does not make much sense that this function is independent from pm_reset_init().

Delete the separate call to starfive_jh7110_inst_init(), and instead just call this function inside pm_reset_init().

Doing this also fixes another problem: if starfive_jh7110_inst_init() returns an error code, it gets propagated to final_init() and OpenSBI hangs. This hang is not necessary, because failures within starfive_jh7110_inst_init() only mean OpenSBI cannot perform reboot or shutdown, but the system can still function normally.

---
 platform/generic/starfive/jh7110.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
platform/generic/starfive/jh7110.c +6 -1
index 63baddb..1577cb4 100644
@@ -191,6 +191,8 @@ static struct sbi_system_reset_device pm_reset = {
 	.system_reset = pm_system_reset
 };
 
+static int starfive_jh7110_inst_init(void *fdt);
+
 static int pm_reset_init(void *fdt, int nodeoff,
 			 const struct fdt_match *match)
 {
@@ -216,6 +218,10 @@ static int pm_reset_init(void *fdt, int nodeoff,
 
 	pmic_inst.adapter = adapter;
 
+	rc = starfive_jh7110_inst_init(fdt);
+	if (rc)
+		return rc;
+
 	sbi_system_reset_add_device(&pm_reset);
 
 	return 0;
@@ -279,7 +285,6 @@ static int starfive_jh7110_final_init(bool cold_boot,
 
 	if (cold_boot) {
 		fdt_reset_driver_init(fdt, &fdt_reset_pmic);
-		return starfive_jh7110_inst_init(fdt);
 	}
 
 	return 0;
Signature · 1 line
2.39.2
#3 lore raw

PATCH v3 2/7 platform: starfive: get I2C offset address from clocks property

In reply to Nam Cao
Quoted 30 lines

The current code gets the I2C offset address using the device tree node
name: it get the I2C device index from the 4th character in the node name (for example, "i2c5" -> i2c device 5). However, the device tree node's name in U-Boot is actually just "i2c" without the number, so the current code cannot be used with the device tree from U-Boot.

Get the I2C offset address from the "clocks" property instead.

---
 platform/generic/starfive/jh7110.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
platform/generic/starfive/jh7110.c +1 -1
index 4b22175..846b068 100644
@@ -29,7 +29,7 @@ struct pmic {
 struct jh7110 {
 	u64 pmu_reg_base;
 	u64 clk_reg_base;
-	u32 i2c_index;
+	u32 i2c_clk_offset;
 };
 
 static struct pmic pmic_inst;

platform/generic/starfive/jh7110.c static void pmic_i2c_clk_enable(void) · line 168

Quoted 10 lines
platform/generic/starfive/jh7110.c +1 -4
@@ -163,10 +163,7 @@ static void pmic_i2c_clk_enable(void)
 	unsigned long clock_base;
 	unsigned int val;
 
-	clock_base = jh7110_inst.clk_reg_base +
-		I2C_APB_CLK_OFFSET +
-		(jh7110_inst.i2c_index << 2);
-
+	clock_base = jh7110_inst.clk_reg_base + jh7110_inst.i2c_clk_offset;
 	val = readl((void *)clock_base);
 

Yes , I want to upstream this patch, Thanks
Delete this macro too.

-#define I2C_APB_CLK_OFFSET 0x228

Quoted 30 lines
platform/generic/starfive/jh7110.c +4 -5
 	if (!val)
@@ -241,7 +238,8 @@ static struct fdt_reset fdt_reset_pmic = {  static int
 starfive_jh7110_inst_init(void *fdt)  {
 	int noff, rc = 0;
-	const char *name;
+	const fdt32_t *val;
+	int len;
 	u64 addr;
 
 	noff = fdt_node_offset_by_compatible(fdt, -1, "starfive,jh7110-pmu"); @@
-261,9 +259,12 @@ static int starfive_jh7110_inst_init(void *fdt)
 	}
 
 	if (pmic_inst.adapter) {
-		name = fdt_get_name(fdt, pmic_inst.adapter->id, NULL);
-		if (!sbi_strncmp(name, "i2c", 3))
-			jh7110_inst.i2c_index = name[3] - '0';
+		val = fdt_getprop(fdt, pmic_inst.adapter->id, "clocks", &len);
+		/* The clocks property looks like this: clocks = <&syscrg

JH7110_SYSCLK_I2C5_APB>;

platform/generic/starfive/jh7110.c +1 -0
+		 * So check that the length is 8 bytes, and get the offset from the

second value

platform/generic/starfive/jh7110.c +3 -1
+		 */
+		if (val && len == 8)
+			jh7110_inst.i2c_clk_offset = fdt32_to_cpu(val[1]) << 2;
 		else
 			rc = SBI_EINVAL;
 	}
--

2.39.2

#8 lore raw

PATCH v3 4/7 platform: starfive: rename "stf,axp15060-regulator" -> "x-powers,axp15060"

In reply to Nam Cao
Quoted 22 lines

OpenSBI uses the device tree compatible string "stf,axp15060-regulator" for the regulator node. However, the string used by U-Boot (and Linux) is actually "x-powers,axp15060". As OpenSBI gets the device tree from U-Boot, this causes the regulator device to be undetected, and OpenSBI does not use this device to perform board reset/shutdown.

Rename this device tree compatible string to match U-Boot (and Linux).

---
 platform/generic/starfive/jh7110.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
platform/generic/starfive/jh7110.c +0 -0
index d3647c2..385e09c 100644

platform/generic/starfive/jh7110.c static int pm_reset_init(void *fdt, int nodeoff, } · line 229

Quoted 7 lines
platform/generic/starfive/jh7110.c +1 -1
@@ -224,7 +224,7 @@ static int pm_reset_init(void *fdt, int nodeoff,  }
 
 static const struct fdt_match pm_reset_match[] = {
-	{ .compatible = "stf,axp15060-regulator", .data = (void *)true },
+	{ .compatible = "x-powers,axp15060", .data = (void *)true },
 	{ },
 };
 

It is OK.
But upstream u-boot jh7110 dts file do not contain this compatible? Can you also upstream this in u-boot? or I will do this myself

Quoted 2 lines
platform/generic/starfive/jh7110.c +0 -1
--

2.39.2

#15 lore raw

PATCH v3 7/7 platform: starfive: call starfive_jh7110_inst_init() in pm_reset_init()

In reply to Nam Cao
Quoted 56 lines

The function starfive_jh7110_inst_init() initialize some power management unit address and clock addresses, needed for the reset driver. It doesn't do anything else, and also the reset driver doesn't work without calling this function. Thus, it does not make much sense that this function is independent from pm_reset_init().

Delete the separate call to starfive_jh7110_inst_init(), and instead just call this function inside pm_reset_init().

Doing this also fixes another problem: if starfive_jh7110_inst_init() returns an error code, it gets propagated to final_init() and OpenSBI hangs. This hang is not necessary, because failures within
starfive_jh7110_inst_init() only mean OpenSBI cannot perform reboot or shutdown, but the system can still function normally.

---
 platform/generic/starfive/jh7110.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
platform/generic/starfive/jh7110.c +6 -2
index 63baddb..1577cb4 100644
@@ -191,6 +191,8 @@ static struct sbi_system_reset_device pm_reset = {
 	.system_reset = pm_system_reset
 };
 
+static int starfive_jh7110_inst_init(void *fdt);
+
 static int pm_reset_init(void *fdt, int nodeoff,
 			 const struct fdt_match *match)
 {
@@ -216,6 +218,10 @@ static int pm_reset_init(void *fdt, int nodeoff,
 
 	pmic_inst.adapter = adapter;
 
+	rc = starfive_jh7110_inst_init(fdt);
+	if (rc)
+		return rc;
+
 	sbi_system_reset_add_device(&pm_reset);
 
 	return 0;
@@ -279,7 +285,6 @@ static int starfive_jh7110_final_init(bool cold_boot,
 
 	if (cold_boot) {
 		fdt_reset_driver_init(fdt, &fdt_reset_pmic);
-		return starfive_jh7110_inst_init(fdt);
 	}
 
 	return 0;
--
Quoted 1 line

2.39.2

Test with this series patch? function is work fine.
This patch need Anup review.

Reviewed-by on v3 2/7
#4 lore raw

回复: [PATCH v3 2/7] platform: starfive: get I2C offset address from clocks property

In reply to Nam Cao
Quoted 79 lines

-----????-----
???: Nam Cao <[email protected]>
????: 2024?2?2? 21:43
???: opensbi at lists.infradead.org; Cheehong Ang <[email protected]>; Wei Liang Lim <[email protected]>; Minda Chen <[email protected]> ??: Nam Cao <[email protected]>
??: [PATCH v3 2/7] platform: starfive: get I2C offset address from clocks property

The current code gets the I2C offset address using the device tree node
name: it get the I2C device index from the 4th character in the node name (for example, "i2c5" -> i2c device 5). However, the device tree node's name in U-Boot is actually just "i2c" without the number, so the current code cannot be used with the device tree from U-Boot.

Get the I2C offset address from the "clocks" property instead.

---
 platform/generic/starfive/jh7110.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
platform/generic/starfive/jh7110.c +6 -10
index 4b22175..846b068 100644
@@ -29,7 +29,7 @@ struct pmic {
 struct jh7110 {
 	u64 pmu_reg_base;
 	u64 clk_reg_base;
-	u32 i2c_index;
+	u32 i2c_clk_offset;
 };
 
 static struct pmic pmic_inst;
@@ -163,10 +163,7 @@ static void pmic_i2c_clk_enable(void)
 	unsigned long clock_base;
 	unsigned int val;
 
-	clock_base = jh7110_inst.clk_reg_base +
-		I2C_APB_CLK_OFFSET +
-		(jh7110_inst.i2c_index << 2);
-
+	clock_base = jh7110_inst.clk_reg_base + jh7110_inst.i2c_clk_offset;
 	val = readl((void *)clock_base);
 
 	if (!val)
@@ -241,7 +238,8 @@ static struct fdt_reset fdt_reset_pmic = {  static int
 starfive_jh7110_inst_init(void *fdt)  {
 	int noff, rc = 0;
-	const char *name;
+	const fdt32_t *val;
+	int len;
 	u64 addr;
 
 	noff = fdt_node_offset_by_compatible(fdt, -1, "starfive,jh7110-pmu"); @@
-261,9 +259,12 @@ static int starfive_jh7110_inst_init(void *fdt)
 	}
 
 	if (pmic_inst.adapter) {
-		name = fdt_get_name(fdt, pmic_inst.adapter->id, NULL);
-		if (!sbi_strncmp(name, "i2c", 3))
-			jh7110_inst.i2c_index = name[3] - '0';
+		val = fdt_getprop(fdt, pmic_inst.adapter->id, "clocks", &len);
+		/* The clocks property looks like this: clocks = <&syscrg

JH7110_SYSCLK_I2C5_APB>;

platform/generic/starfive/jh7110.c +1 -0
+		 * So check that the length is 8 bytes, and get the offset from the

second value

platform/generic/starfive/jh7110.c +3 -1
+		 */
+		if (val && len == 8)
+			jh7110_inst.i2c_clk_offset = fdt32_to_cpu(val[1]) << 2;
 		else
 			rc = SBI_EINVAL;
 	}
--
Quoted 1 line

2.39.2

Please delete this macro
-#define I2C_APB_CLK_OFFSET 0x228

author
#9 lore raw

PATCH v3 4/7 platform: starfive: rename "stf,axp15060-regulator" -> "x-powers,axp15060"

In reply to Minda Chen
Quoted 21 lines · On 04/Feb/2024 Minda Chen wrote:
Quoted 21 lines

OpenSBI uses the device tree compatible string "stf,axp15060-regulator" for the regulator node. However, the string used by U-Boot (and Linux) is actually "x-powers,axp15060". As OpenSBI gets the device tree from U-Boot, this causes the regulator device to be undetected, and OpenSBI does not use this device to perform board reset/shutdown.

Rename this device tree compatible string to match U-Boot (and Linux).

---
 platform/generic/starfive/jh7110.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
platform/generic/starfive/jh7110.c +0 -0
index d3647c2..385e09c 100644
@@ -224,7 +224,7 @@ static int pm_reset_init(void *fdt, int nodeoff,  }
 
Quoted 9 lines

platform/generic/starfive/jh7110.c static int pm_reset_init(void *fdt, int nodeoff, · line 230

Quoted 6 lines
platform/generic/starfive/jh7110.c +1 -1
 static const struct fdt_match pm_reset_match[] = {
-	{ .compatible = "stf,axp15060-regulator", .data = (void *)true },
+	{ .compatible = "x-powers,axp15060", .data = (void *)true },
 	{ },
 };
 

It is OK.
But upstream u-boot jh7110 dts file do not contain this compatible? Can you also upstream this in u-boot? or I will do this myself

It is in upstream U-Boot, just a few days ago:
https://source.denx.de/u-boot/u-boot/-/commit/6882255ac3107c58e1153311df8a8270087f8cb3

Best regards,
Nam

author
#5 lore raw

回复: [PATCH v3 2/7] platform: starfive: get I2C offset address from clocks property

In reply to Minda Chen
Quoted 73 lines · On 05/Feb/2024 Minda Chen wrote:
Quoted 73 lines

-----????-----
???: Nam Cao <[email protected]>
????: 2024?2?2? 21:43
???: opensbi at lists.infradead.org; Cheehong Ang <[email protected]>; Wei Liang Lim <[email protected]>; Minda Chen <[email protected]> ??: Nam Cao <[email protected]>
??: [PATCH v3 2/7] platform: starfive: get I2C offset address from clocks property

The current code gets the I2C offset address using the device tree node
name: it get the I2C device index from the 4th character in the node name (for example, "i2c5" -> i2c device 5). However, the device tree node's name in U-Boot is actually just "i2c" without the number, so the current code cannot be used with the device tree from U-Boot.

Get the I2C offset address from the "clocks" property instead.

---
 platform/generic/starfive/jh7110.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
platform/generic/starfive/jh7110.c +6 -10
index 4b22175..846b068 100644
@@ -29,7 +29,7 @@ struct pmic {
 struct jh7110 {
 	u64 pmu_reg_base;
 	u64 clk_reg_base;
-	u32 i2c_index;
+	u32 i2c_clk_offset;
 };
 
 static struct pmic pmic_inst;
@@ -163,10 +163,7 @@ static void pmic_i2c_clk_enable(void)
 	unsigned long clock_base;
 	unsigned int val;
 
-	clock_base = jh7110_inst.clk_reg_base +
-		I2C_APB_CLK_OFFSET +
-		(jh7110_inst.i2c_index << 2);
-
+	clock_base = jh7110_inst.clk_reg_base + jh7110_inst.i2c_clk_offset;
 	val = readl((void *)clock_base);
 
 	if (!val)
@@ -241,7 +238,8 @@ static struct fdt_reset fdt_reset_pmic = {  static int
 starfive_jh7110_inst_init(void *fdt)  {
 	int noff, rc = 0;
-	const char *name;
+	const fdt32_t *val;
+	int len;
 	u64 addr;
 
 	noff = fdt_node_offset_by_compatible(fdt, -1, "starfive,jh7110-pmu"); @@
-261,9 +259,12 @@ static int starfive_jh7110_inst_init(void *fdt)
 	}
 
 	if (pmic_inst.adapter) {
-		name = fdt_get_name(fdt, pmic_inst.adapter->id, NULL);
-		if (!sbi_strncmp(name, "i2c", 3))
-			jh7110_inst.i2c_index = name[3] - '0';
+		val = fdt_getprop(fdt, pmic_inst.adapter->id, "clocks", &len);
+		/* The clocks property looks like this: clocks = <&syscrg  

JH7110_SYSCLK_I2C5_APB>;

platform/generic/starfive/jh7110.c +1 -0
+		 * So check that the length is 8 bytes, and get the offset from the

second value

platform/generic/starfive/jh7110.c +3 -0
+		 */
+		if (val && len == 8)
+			jh7110_inst.i2c_clk_offset = fdt32_to_cpu(val[1]) << 2;
Quoted 8 lines
Quoted 5 lines
platform/generic/starfive/jh7110.c +0 -1
 		else
 			rc = SBI_EINVAL;
 	}
--

2.39.2

Please delete this macro
-#define I2C_APB_CLK_OFFSET 0x228

Ahh, someone already made this comment in my first revision. But I think I messed it up when I resent. Will remove this in the next version.

Quoted 1 line

Thanks for the review.

Best regards,
Nam

#11 lore raw

回复: [PATCH v3 5/7] platform: starfive: drop "compatible" from struct pmic

In reply to Nam Cao
Quoted 31 lines

The member "compatible" of struct pmic is not used. Delete it.

---
 platform/generic/starfive/jh7110.c | 2 --
 1 file changed, 2 deletions(-)
platform/generic/starfive/jh7110.c +0 -3
index 385e09c..f64e016 100644
@@ -23,7 +23,6 @@
 struct pmic {
 	struct i2c_adapter *adapter;
 	u32 dev_addr;
-	const char *compatible;
 };
 
 struct jh7110 {
@@ -205,7 +204,6 @@ static int pm_reset_init(void *fdt, int nodeoff,
 		return rc;
 
 	pmic_inst.dev_addr = addr;
-	pmic_inst.compatible = match->compatible;
 
 	i2c_bus = fdt_parent_offset(fdt, nodeoff);
 	if (i2c_bus < 0)
--
Quoted 1 line

2.39.2

Maybe this patch can be squash to 3rd patch. It is my sugguestion.

author
#12 lore raw

回复: [PATCH v3 5/7] platform: starfive: drop "compatible" from struct pmic

Quoted 23 lines · On 05/Feb/2024 Minda Chen wrote:
Quoted 23 lines

The member "compatible" of struct pmic is not used. Delete it.

---
 platform/generic/starfive/jh7110.c | 2 --
 1 file changed, 2 deletions(-)
platform/generic/starfive/jh7110.c +0 -1
index 385e09c..f64e016 100644
@@ -23,7 +23,6 @@
 struct pmic {
 	struct i2c_adapter *adapter;
 	u32 dev_addr;
-	const char *compatible;
 };
 
 struct jh7110 {
@@ -205,7 +204,6 @@ static int pm_reset_init(void *fdt, int nodeoff,
 		return rc;
 
Quoted 8 lines
Quoted 7 lines
platform/generic/starfive/jh7110.c +0 -2
 	pmic_inst.dev_addr = addr;
-	pmic_inst.compatible = match->compatible;
 
 	i2c_bus = fdt_parent_offset(fdt, nodeoff);
 	if (i2c_bus < 0)
--

2.39.2

Maybe this patch can be squash to 3rd patch. It is my sugguestion.

Makes sense. Unfortunately I just sent a v4 a few seconds before this. I think it's not too important, so I don't really want to send a new series just for this.

If okay with you, I will wait and see if there are any other comments. If v5 is needed, I will squash them then.

Best regards,
Nam

author
#16 lore raw

PATCH v3 7/7 platform: starfive: call starfive_jh7110_inst_init() in pm_reset_init()

In reply to Minda Chen
Quoted 53 lines · On 05/Feb/2024 Minda Chen wrote:
Quoted 53 lines

The function starfive_jh7110_inst_init() initialize some power management unit address and clock addresses, needed for the reset driver. It doesn't do anything else, and also the reset driver doesn't work without calling this function. Thus, it does not make much sense that this function is independent from pm_reset_init().

Delete the separate call to starfive_jh7110_inst_init(), and instead just call this function inside pm_reset_init().

Doing this also fixes another problem: if starfive_jh7110_inst_init() returns an error code, it gets propagated to final_init() and OpenSBI hangs. This hang is not necessary, because failures within
starfive_jh7110_inst_init() only mean OpenSBI cannot perform reboot or shutdown, but the system can still function normally.

---
 platform/generic/starfive/jh7110.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
platform/generic/starfive/jh7110.c +6 -1
index 63baddb..1577cb4 100644
@@ -191,6 +191,8 @@ static struct sbi_system_reset_device pm_reset = {
 	.system_reset = pm_system_reset
 };
 
+static int starfive_jh7110_inst_init(void *fdt);
+
 static int pm_reset_init(void *fdt, int nodeoff,
 			 const struct fdt_match *match)
 {
@@ -216,6 +218,10 @@ static int pm_reset_init(void *fdt, int nodeoff,
 
 	pmic_inst.adapter = adapter;
 
+	rc = starfive_jh7110_inst_init(fdt);
+	if (rc)
+		return rc;
+
 	sbi_system_reset_add_device(&pm_reset);
 
 	return 0;
@@ -279,7 +285,6 @@ static int starfive_jh7110_final_init(bool cold_boot,
 
 	if (cold_boot) {
 		fdt_reset_driver_init(fdt, &fdt_reset_pmic);
-		return starfive_jh7110_inst_init(fdt);
 	}
 
Quoted 5 lines
Quoted 3 lines
platform/generic/starfive/jh7110.c +0 -1
 	return 0;
--

2.39.2

Test with this series patch? function is work fine.

Can I have your Tested-by tag?

Best regards,
Nam