Skip to contentSkip to conversation

bootcount: Replace I2C legacy implementation by driver model

PATCH Superseded Philip Richard Oberfichtner 2 participants 9 messages Oct 20, 2023 – Oct 31, 2023 I2C

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover bootcount: Replace I2C legacy implementation by driver model

The generic I2C bootcounter driver does not yet adhere to driver model. This patchset intends to replace the legacy implementation.

There are currently no upstream boards using the driver, so it should be safe to just remove it. For downstream users it should be straighforward to switch to the new implementation.

Changes in v2:
- Improved device tree handling by replacing address property with phandle
- Addition of a generic i2c helper function

In v1 there has been some discussion on how to smoothly bind and probe the i2c device. The hereby proposed solution may not fullfill all the requirements stated there, but it is the only variant I actually got to work. Furthermore it is not as concise as I would have liked it to be. Ideas on how to improve it are welcome :)

Philip Richard Oberfichtner (3):
bootcount: Remove legacy I2C driver
i2c: Implement i2c_get_chip_by_phandle()
bootcount: Add driver model I2C driver

 drivers/bootcount/Kconfig            |  34 ++++-----
 drivers/bootcount/Makefile           |   2 +-
 drivers/bootcount/bootcount_dm_i2c.c | 103 +++++++++++++++++++++++++++
 drivers/bootcount/bootcount_i2c.c    |  43 -----------
 drivers/i2c/i2c-uclass.c             |  75 +++++++++++++++++++
 include/i2c.h                        |  12 ++++
 6 files changed, 204 insertions(+), 65 deletions(-)
 create mode 100644 drivers/bootcount/bootcount_dm_i2c.c
 delete mode 100644 drivers/bootcount/bootcount_i2c.c
Signature · 1 line
2.42.0
#1 lore raw

PATCH v2 1/3 bootcount: Remove legacy I2C driver

The legacy I2C bootcounter will hereby be removed and eventually be replaced by a driver model implementation in the follow-up commit.

The legacy driver has the following drawbacks:
- It's not adhering to the driver model
- Settings are grabbed from Kconfig rather than device tree
- i2c_{read,write} are being used instead of dm_i2c_{read,write}

---

Changes in v2: none

 drivers/bootcount/Kconfig         | 24 +++--------------
 drivers/bootcount/Makefile        |  1 -
 drivers/bootcount/bootcount_i2c.c | 43 -------------------------------
 3 files changed, 3 insertions(+), 65 deletions(-)
 delete mode 100644 drivers/bootcount/bootcount_i2c.c
drivers/bootcount/Kconfig +3 -21
index 570252d186..7a2548ace2 100644
@@ -79,14 +79,6 @@ config BOOTCOUNT_RAM
 	  Store the bootcount in DRAM protected against bit errors
 	  due to short power loss or holding a system in RESET.
 
-config BOOTCOUNT_I2C
-	bool "Boot counter on I2C device"
-	help
-	  Enable support for the bootcounter on an i2c (like RTC) device.
-	  CFG_SYS_I2C_RTC_ADDR = i2c chip address
-	  CONFIG_SYS_BOOTCOUNT_ADDR = i2c addr which is used for
-	                              the bootcounter.
-
 config BOOTCOUNT_AT91
 	bool "Boot counter for Atmel AT91SAM9XE"
 	depends on AT91SAM9XE
@@ -175,14 +167,6 @@ config BOOTCOUNT_BOOTLIMIT
 	  counter being cleared.
 	  If set to 0, do not set a boot limit in the environment.
 
-config BOOTCOUNT_ALEN
-	int "I2C address length"
-	default 1
-	depends on BOOTCOUNT_I2C
-	help
-	  Length of the the I2C address at SYS_BOOTCOUNT_ADDR for storing
-	  the boot counter.
-
 config SYS_BOOTCOUNT_SINGLEWORD
 	bool "Use single word to pack boot count and magic value"
 	depends on BOOTCOUNT_GENERIC
@@ -218,7 +202,7 @@ config SYS_BOOTCOUNT_ADDR
 	default 0x44E3E000 if BOOTCOUNT_AM33XX || BOOTCOUNT_AM33XX_NVMEM
 	default 0xE0115FF8 if ARCH_LS1043A || ARCH_LS1021A
 	depends on BOOTCOUNT_AM33XX || BOOTCOUNT_GENERIC || BOOTCOUNT_EXT || \
-		   BOOTCOUNT_I2C || BOOTCOUNT_AM33XX_NVMEM
+		   BOOTCOUNT_AM33XX_NVMEM
 	help
 	  Set the address used for reading and writing the boot counter.
 
@@ -226,13 +210,11 @@ config SYS_BOOTCOUNT_MAGIC
 	hex "Magic value for the boot counter"
 	default 0xB001C041 if BOOTCOUNT_GENERIC || BOOTCOUNT_EXT || \
 			      BOOTCOUNT_AM33XX || BOOTCOUNT_ENV || \
-			      BOOTCOUNT_RAM || BOOTCOUNT_I2C || \
-			      BOOTCOUNT_AT91 || DM_BOOTCOUNT
+			      BOOTCOUNT_RAM || BOOTCOUNT_AT91 || DM_BOOTCOUNT
 	default 0xB0 if BOOTCOUNT_AM33XX_NVMEM
 	depends on BOOTCOUNT_GENERIC || BOOTCOUNT_EXT || \
 		   BOOTCOUNT_AM33XX || BOOTCOUNT_ENV || \
-		   BOOTCOUNT_RAM || BOOTCOUNT_I2C || \
-		   BOOTCOUNT_AT91 || DM_BOOTCOUNT || \
+		   BOOTCOUNT_RAM || BOOTCOUNT_AT91 || DM_BOOTCOUNT || \
 		   BOOTCOUNT_AM33XX_NVMEM
 	help
 	  Set the magic value used for the boot counter.
drivers/bootcount/Makefile +0 -1
index b65959a384..d6d2389c16 100644
@@ -6,7 +6,6 @@ obj-$(CONFIG_BOOTCOUNT_AT91)	+= bootcount_at91.o
 obj-$(CONFIG_BOOTCOUNT_AM33XX)	+= bootcount_davinci.o
 obj-$(CONFIG_BOOTCOUNT_RAM)	+= bootcount_ram.o
 obj-$(CONFIG_BOOTCOUNT_ENV)	+= bootcount_env.o
-obj-$(CONFIG_BOOTCOUNT_I2C)	+= bootcount_i2c.o
 obj-$(CONFIG_BOOTCOUNT_EXT)	+= bootcount_ext.o
 obj-$(CONFIG_BOOTCOUNT_AM33XX_NVMEM)	+= bootcount_nvmem.o
 
drivers/bootcount/bootcount_i2c.c deleted +0 -43

43 lines not on this page · raw message

Signature · 1 line
2.42.0
#3 lore raw

PATCH v2 2/3 i2c: Implement i2c_get_chip_by_phandle()

In reply to Philip Richard Oberfichtner

This new function enhances the i2c_get_chip*() toolbox by implementing a variant that does not require a chip_addr. Instead, the desired device is pointed to by a phandle.

---

Changes in v2: new

 drivers/i2c/i2c-uclass.c | 75 ++++++++++++++++++++++++++++++++++++++++
 include/i2c.h            | 12 +++++++
 2 files changed, 87 insertions(+)
drivers/i2c/i2c-uclass.c +75 -0
index 8867a560bd..5405067861 100644
@@ -388,6 +388,81 @@ int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
 	return 0;
 }
 
+/* Find and probe I2C bus based on a chip attached to it */
+static int i2c_get_parent_bus(ofnode chip, struct udevice **devp)
+{
+	ofnode node;
+	struct udevice *dev;
+	int ret;
+
+	node = ofnode_get_parent(chip);
+	if (!ofnode_valid(node))
+		return -ENODEV;
+
+	ret = uclass_get_device_by_ofnode(UCLASS_I2C, node, &dev);
+	if (ret) {
+		*devp = NULL;
+		return ret;
+	}
+
+	*devp = dev;
+	return 0;
+}
+
+int i2c_get_chip_by_phandle(const struct udevice *parent, const char *prop_name,
+			    struct udevice **devp)
+{
+	ofnode node;
+	uint phandle;
+	struct udevice *bus, *chip;
+	char *dev_name;
+	int ret;
+
+	debug("%s: Searching I2C chip for phandle \"%s\"\n",
+	      __func__, prop_name);
+
+	dev_name = strdup(prop_name);
+	if (!dev_name) {
+		ret = -ENOMEM;
+		goto err_exit;
+	}
+
+	ret = dev_read_u32(parent, "i2cbcdev", &phandle);
+	if (ret)
+		goto err_exit;
+
+	node = ofnode_get_by_phandle(phandle);
+	if (!ofnode_valid(node)) {
+		ret = -ENODEV;
+		goto err_exit;
+	}
+
+	ret = i2c_get_parent_bus(node, &bus);
+	if (ret)
+		goto err_exit;
+
+	ret = device_bind_driver_to_node(bus, "i2c_generic_chip_drv",
+					 dev_name, node, &chip);
+	if (ret)
+		goto err_exit;
+
+	ret = device_probe(chip);
+	if (ret) {
+		device_unbind(chip);
+		goto err_exit;
+	}
+
+	debug("%s succeeded\n", __func__);
+	*devp = chip;
+	return 0;
+
+err_exit:
+	free(dev_name);
+	debug("%s failed, ret = %d\n", __func__, ret);
+	*devp = NULL;
+	return ret;
+}
+
 int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
 		 struct udevice **devp)
 {
include/i2c.h +12 -0
index ef3820eaba..4e59009cd9 100644
@@ -537,6 +537,18 @@ int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
 int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
 			    struct udevice **devp);
 
+/**
+ * i2c_get_chip_by_phandle() - get a device to use to access a chip
+ *			       based on a phandle property pointing to it
+ *
+ * @parent: Parent device containing the phandle pointer
+ * @name:   Name of phandle property in the parent device node
+ * @devp:   Returns pointer to new device or NULL if not found
+ * Return:  0 on success, -ve on failure
+ */
+int i2c_get_chip_by_phandle(const struct udevice *parent, const char *prop_name,
+			    struct udevice **devp);
+
 /**
  * i2c_chip_of_to_plat() - Decode standard I2C platform data
  *
Signature · 1 line
2.42.0
#5 lore raw

PATCH v2 3/3 bootcount: Add driver model I2C driver

In reply to Philip Richard Oberfichtner

This adds a generic I2C bootcounter adhering to driver model to replace the previously removed legacy implementation.

There is no change in functionality, it can be used on any I2C device. The device tree configuration may look like this for example:

	bootcount {
		compatible = "u-boot,bootcount-i2c";
		i2cbcdev = <&i2c_rtc>;
		offset = <0x11>;
	};

---

Changes in v2:
- Adaption of Kconfig help message
- Rename chip to bcdev
- Adapt probe to use i2c_get_chip_by_phandle()

 drivers/bootcount/Kconfig            |  10 +++
 drivers/bootcount/Makefile           |   1 +
 drivers/bootcount/bootcount_dm_i2c.c | 103 +++++++++++++++++++++++++++
 3 files changed, 114 insertions(+)
 create mode 100644 drivers/bootcount/bootcount_dm_i2c.c
drivers/bootcount/Kconfig +10 -0
index 7a2548ace2..3c56253b1e 100644
@@ -109,6 +109,16 @@ config DM_BOOTCOUNT_RTC
 	  Accesses to the backing store are performed using the write16
 	  and read16 ops of DM RTC devices.
 
+config DM_BOOTCOUNT_I2C
+	bool "Driver Model boot counter on I2C device"
+	depends on DM_I2C
+	help
+	  Enable support for the bootcounter on a generic i2c device, like a RTC
+	  or PMIC. The bootcounter is configured in the device tree using the
+	  "u-boot,bootcount-i2c" compatible string. It requires a phandle
+	  'i2cbcdev' for the i2c device and an 'offset' property used within the
+	  device.
+
 config DM_BOOTCOUNT_I2C_EEPROM
 	bool "Support i2c eeprom devices as a backing store for bootcount"
 	depends on I2C_EEPROM
drivers/bootcount/Makefile +1 -0
index d6d2389c16..e7771f5b36 100644
@@ -13,5 +13,6 @@ obj-$(CONFIG_DM_BOOTCOUNT)      += bootcount-uclass.o
 obj-$(CONFIG_DM_BOOTCOUNT_PMIC_PFUZE100) += pmic_pfuze100.o
 obj-$(CONFIG_DM_BOOTCOUNT_RTC)  += rtc.o
 obj-$(CONFIG_DM_BOOTCOUNT_I2C_EEPROM)	+= i2c-eeprom.o
+obj-$(CONFIG_DM_BOOTCOUNT_I2C)	+= bootcount_dm_i2c.o
 obj-$(CONFIG_DM_BOOTCOUNT_SPI_FLASH)	+= spi-flash.o
 obj-$(CONFIG_DM_BOOTCOUNT_SYSCON) += bootcount_syscon.o
drivers/bootcount/bootcount_dm_i2c.c new file +103 -0

103 lines not on this page · raw message

Signature · 1 line
2.42.0
Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] bootcount: Remove legacy I2C driver

In reply to Philip Richard Oberfichtner

Hello Philip.

Quoted 11 lines · Philip Richard Oberfichtner

The legacy I2C bootcounter will hereby be removed and eventually be replaced by a driver model implementation in the follow-up commit.

The legacy driver has the following drawbacks:
- It's not adhering to the driver model
- Settings are grabbed from Kconfig rather than device tree
- i2c_{read,write} are being used instead of dm_i2c_{read,write}

---

Quoted 7 lines

Changes in v2: none

 drivers/bootcount/Kconfig         | 24 +++--------------
 drivers/bootcount/Makefile        |  1 -
 drivers/bootcount/bootcount_i2c.c | 43 -------------------------------
 3 files changed, 3 insertions(+), 65 deletions(-)
 delete mode 100644 drivers/bootcount/bootcount_i2c.c

bye,
Heiko

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Erika Unter
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-52   Fax: +49-8142-66989-80   Email: [email protected]
Reviewed-by on v2 2/3
#4 lore raw

Re: [PATCH v2 2/3] i2c: Implement i2c_get_chip_by_phandle()

In reply to Philip Richard Oberfichtner

Hello Philip,

Quoted 4 lines · Philip Richard Oberfichtner

This new function enhances the i2c_get_chip*() toolbox by implementing a variant that does not require a chip_addr. Instead, the desired device is pointed to by a phandle.

Quoted 8 lines

---

Changes in v2: new

 drivers/i2c/i2c-uclass.c | 75 ++++++++++++++++++++++++++++++++++++++++
 include/i2c.h            | 12 +++++++
 2 files changed, 87 insertions(+)

Looks good to me, thanks!

bye,
Heiko

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Erika Unter
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-52   Fax: +49-8142-66989-80   Email: [email protected]
Reviewed-by on v2 3/3
#6 lore raw

Re: [PATCH v2 3/3] bootcount: Add driver model I2C driver

In reply to Philip Richard Oberfichtner

Hello Philip,

Quoted 20 lines · Philip Richard Oberfichtner

This adds a generic I2C bootcounter adhering to driver model to replace the previously removed legacy implementation.

There is no change in functionality, it can be used on any I2C device. The device tree configuration may look like this for example:

	bootcount {
		compatible = "u-boot,bootcount-i2c";
		i2cbcdev = <&i2c_rtc>;
		offset = <0x11>;
	};

---

Changes in v2:
- Adaption of Kconfig help message
- Rename chip to bcdev
- Adapt probe to use i2c_get_chip_by_phandle()

Quoted 5 lines
 drivers/bootcount/Kconfig            |  10 +++
 drivers/bootcount/Makefile           |   1 +
 drivers/bootcount/bootcount_dm_i2c.c | 103 +++++++++++++++++++++++++++
 3 files changed, 114 insertions(+)
 create mode 100644 drivers/bootcount/bootcount_dm_i2c.c

bye,
Heiko

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Erika Unter
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-52   Fax: +49-8142-66989-80   Email: [email protected]
#7 lore raw

Re: [PATCH v2 3/3] bootcount: Add driver model I2C driver

Hello Philip,

Quoted 23 lines · Heiko Schocher

Hello Philip,

Quoted 20 lines · Philip Richard Oberfichtner

This adds a generic I2C bootcounter adhering to driver model to replace the previously removed legacy implementation.

There is no change in functionality, it can be used on any I2C device. The device tree configuration may look like this for example:

	bootcount {
		compatible = "u-boot,bootcount-i2c";
		i2cbcdev = <&i2c_rtc>;
		offset = <0x11>;
	};

---

Changes in v2:
- Adaption of Kconfig help message
- Rename chip to bcdev
- Adapt probe to use i2c_get_chip_by_phandle()

Quoted 7 lines
Quoted 5 lines
 drivers/bootcount/Kconfig            |  10 +++
 drivers/bootcount/Makefile           |   1 +
 drivers/bootcount/bootcount_dm_i2c.c | 103 +++++++++++++++++++++++++++
 3 files changed, 114 insertions(+)
 create mode 100644 drivers/bootcount/bootcount_dm_i2c.c

Your patch drops an checkpatch error:
"""
ERROR: Do not add common.h to files
#164: FILE: drivers/bootcount/bootcount_dm_i2c.c:10:
+#include <common.h>
"""

Could you please check and fix?

Thanks!

bye,
Heiko

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Erika Unter
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-52   Fax: +49-8142-66989-80   Email: [email protected]
#8 lore raw

Re: [PATCH v2 3/3] bootcount: Add driver model I2C driver

On Mon, Oct 30, 2023 at 06:17:23AM +0100, Heiko Schocher wrote:
<snip>

Quoted 10 lines

Your patch drops an checkpatch error:
"""
ERROR: Do not add common.h to files
#164: FILE: drivers/bootcount/bootcount_dm_i2c.c:10:
+#include <common.h>
"""

Could you please check and fix?

Oops sorry, V3 is on the way.

Regards,
Philip

Quoted 9 lines

Thanks!

bye,
Heiko

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Erika Unter
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-52   Fax: +49-8142-66989-80   Email: [email protected]
Signature · 5 lines
=====================================================================
DENX Software Engineering GmbH,        Managing Director: Erika Unter
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-22  Fax: +49-8142-66989-80   Email: [email protected]
=====================================================================