Skip to contentSkip to conversation

bootcount: zynqmp: Extend ZynqMP specific bootcount support

PATCH In review Marek Vasut 3 participants 4 messages Jan 30, 2025 Arm/Zynq architecture

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bootcount: zynqmp: Extend ZynqMP specific bootcount support

The PGGS area contains four registers, each of which can hold up to 32bit of data. Extend the boot counter to support storing boot count in either of the four registers with arbitrary mask to avoid limiting the boot counter only to the PGGS register 2 LSByte.

Drop U_BOOT_DRVINFO() as that would instantiate the boot counter even if there is no DT node associated with it, that is undesired behavior.

The boot counter configuration is implemented using three new DT properties:
reg - PGGS register address, fallback to PGGS 2 at 0xffd80058 u-boot,bootcount-reg-mask - Register mask, applied on PGGS register u-boot,bootcount-mask - Boot counter mask, applied on boot counter u-boot,bootcount-magic - Boot counter magic, applied on boot counter

Example configuration of 16bit boot counter with 0xb0010000 magic in PGGS0:

&amba {
	bootcount@0xffd80050 {
		compatible = "u-boot,bootcount-zynqmp";
		reg = <0 0xffd80050 0 0x4>;
		u-boot,bootcount-magic = <0xb0010000>;
		u-boot,bootcount-mask = <0xffff>;
		u-boot,bootcount-reg-mask = <0xffffffff>;
	};
};

---
Cc: Heiko Schocher <[email protected]>
Cc: Michal Simek <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: Vasileios Amoiridis <[email protected]>
Cc: [email protected]

---
 drivers/bootcount/bootcount_zynqmp.c | 79 +++++++++++++++++++++++++---
 1 file changed, 71 insertions(+), 8 deletions(-)
drivers/bootcount/bootcount_zynqmp.c +71 -8
index bc0984e2d26..6613b80caec 100644
@@ -3,16 +3,32 @@
 
 #include <bootcount.h>
 #include <dm.h>
+#include <dm/device_compat.h>
 #include <stdio.h>
 #include <zynqmp_firmware.h>
 #include <asm/arch/hardware.h>
 #include <dm/platdata.h>
 
+struct bootcount_zynqmp_priv {
+	void __iomem	*reg;		/* PGGS register to use for boot counter */
+	u32		regmask;	/* Mask over the valid register bits */
+	u32		mask;		/* Mask over the valid boot counter bits */
+	u32		magic;		/* Magic to place in the rest of the register */
+};
+
 static int bootcount_zynqmp_set(struct udevice *dev, const u32 val)
 {
+	struct bootcount_zynqmp_priv *priv = dev_get_priv(dev);
+	u32 rwval = val;
 	int ret;
 
-	ret = zynqmp_mmio_write((ulong)&pmu_base->pers_gen_storage2, 0xFF, val);
+	/* Apply magic to detect whether the boot counter is valid. */
+	rwval &= priv->mask;	/* Clear all non-bootcounter bits */
+	rwval |= priv->magic;	/* Apply magic value */
+
+	rwval &= priv->regmask;	/* Apply register mask */
+
+	ret = zynqmp_mmio_write((ulong)priv->reg, priv->regmask, rwval);
 	if (ret)
 		pr_info("%s write fail\n", __func__);
 
@@ -21,27 +37,74 @@ static int bootcount_zynqmp_set(struct udevice *dev, const u32 val)
 
 static int bootcount_zynqmp_get(struct udevice *dev, u32 *val)
 {
+	struct bootcount_zynqmp_priv *priv = dev_get_priv(dev);
 	int ret;
 
 	*val = 0;
-	ret = zynqmp_mmio_read((ulong)&pmu_base->pers_gen_storage2, val);
-	if (ret)
+	ret = zynqmp_mmio_read((ulong)priv->reg, val);
+	if (ret) {
 		pr_info("%s read fail\n", __func__);
+		return ret;
+	}
 
-	return ret;
-}
+	/* Clear out unrelated register content. */
+	*val &= priv->regmask;
 
-U_BOOT_DRVINFO(bootcount_zynqmp) = {
-	.name = "bootcount_zynqmp",
-};
+	/* Check whether magic is valid, if not, boot counter value is 0. */
+	if ((*val & ~priv->mask) != priv->magic)
+		*val = 0;
+
+	/* Remove magic from boot counter value. */
+	*val &= priv->mask;
+
+	return 0;
+}
 
 static const struct bootcount_ops bootcount_zynqmp_ops = {
 	.get = bootcount_zynqmp_get,
 	.set = bootcount_zynqmp_set,
 };
 
+static int bootcount_zynqmp_probe(struct udevice *dev)
+{
+	struct bootcount_zynqmp_priv *priv = dev_get_priv(dev);
+
+	/*
+	 * ZynqMP PGGS register to use for boot counter,
+	 * default to PGGS2 to maintain backward compatibility.
+	 */
+	priv->reg = dev_read_addr_ptr(dev);
+	if (!priv->reg)
+		priv->reg = &pmu_base->pers_gen_storage2;
+
+	/*
+	 * Boot counter mask and register mask, default to 0x000000ff
+	 * to maintain backward compatibility.
+	 */
+	priv->mask = dev_read_u32_default(dev, "u-boot,bootcount-mask", 0xff);
+	priv->regmask = dev_read_u32_default(dev, "u-boot,bootcount-reg-mask",
+					     priv->mask);
+
+	/*
+	 * Boot counter magic, default to 0x00000000
+	 * to maintain backward compatibility.
+	 */
+	priv->magic = dev_read_u32_default(dev, "u-boot,bootcount-magic", 0);
+	priv->magic &= ~priv->mask;
+
+	return 0;
+}
+
+static const struct udevice_id bootcount_zynqmp_ids[] = {
+	{ .compatible = "u-boot,bootcount-zynqmp" },
+	{ }
+};
+
 U_BOOT_DRIVER(bootcount_zynqmp) = {
 	.name = "bootcount_zynqmp",
 	.id = UCLASS_BOOTCOUNT,
 	.ops = &bootcount_zynqmp_ops,
+	.probe = bootcount_zynqmp_probe,
+	.of_match = bootcount_zynqmp_ids,
+	.priv_auto = sizeof(struct bootcount_zynqmp_priv),
 };
Signature · 1 line
2.45.2
Reviewed-by on v1 1/1
#1 lore raw

Hello Marek,

Quoted 33 lines · Marek Vasut

The PGGS area contains four registers, each of which can hold up to 32bit of data. Extend the boot counter to support storing boot count in either of the four registers with arbitrary mask to avoid limiting the boot counter only to the PGGS register 2 LSByte.

Drop U_BOOT_DRVINFO() as that would instantiate the boot counter even if there is no DT node associated with it, that is undesired behavior.

The boot counter configuration is implemented using three new DT properties:
reg - PGGS register address, fallback to PGGS 2 at 0xffd80058 u-boot,bootcount-reg-mask - Register mask, applied on PGGS register u-boot,bootcount-mask - Boot counter mask, applied on boot counter u-boot,bootcount-magic - Boot counter magic, applied on boot counter

Example configuration of 16bit boot counter with 0xb0010000 magic in PGGS0:

&amba {
	bootcount@0xffd80050 {
		compatible = "u-boot,bootcount-zynqmp";
		reg = <0 0xffd80050 0 0x4>;
		u-boot,bootcount-magic = <0xb0010000>;
		u-boot,bootcount-mask = <0xffff>;
		u-boot,bootcount-reg-mask = <0xffffffff>;
	};
};

---
Cc: Heiko Schocher <[email protected]>
Cc: Michal Simek <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: Vasileios Amoiridis <[email protected]>
Cc: [email protected]

Quoted 3 lines
---
  drivers/bootcount/bootcount_zynqmp.c | 79 +++++++++++++++++++++++++---
  1 file changed, 71 insertions(+), 8 deletions(-)

Nitpick: Should there not be a change in a file where the new properties and the compatible entry is documented?

Beside of that:

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]
#2 lore raw
Quoted 40 lines · Heiko Schocher

Hello Marek,

Quoted 35 lines · Marek Vasut

The PGGS area contains four registers, each of which can hold up to 32bit of data. Extend the boot counter to support storing boot count in either of the four registers with arbitrary mask to avoid limiting the boot counter only to the PGGS register 2 LSByte.

Drop U_BOOT_DRVINFO() as that would instantiate the boot counter even if there is no DT node associated with it, that is undesired behavior.

The boot counter configuration is implemented using three new DT properties:
reg - PGGS register address, fallback to PGGS 2 at 0xffd80058 u-boot,bootcount-reg-mask - Register mask, applied on PGGS register u-boot,bootcount-mask - Boot counter mask, applied on boot counter u-boot,bootcount-magic - Boot counter magic, applied on boot counter

Example configuration of 16bit boot counter with 0xb0010000 magic in PGGS0:

&amba {
    bootcount@0xffd80050 {
        compatible = "u-boot,bootcount-zynqmp";
        reg = <0 0xffd80050 0 0x4>;
        u-boot,bootcount-magic = <0xb0010000>;
        u-boot,bootcount-mask = <0xffff>;
        u-boot,bootcount-reg-mask = <0xffffffff>;
    };
};

---
Cc: Heiko Schocher <[email protected]>
Cc: Michal Simek <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: Vasileios Amoiridis <[email protected]>
Cc: [email protected]
---
  drivers/bootcount/bootcount_zynqmp.c | 79 +++++++++++++++++++++++++---

Quoted 1 line
  1 file changed, 71 insertions(+), 8 deletions(-)
Quoted 2 lines

Nitpick: Should there not be a change in a file where the new properties and the compatible entry is documented?

Do we have one ? I don't see one with git grep, but I can write one.

That said, I would very much like to get feedback from the CERN person, to make sure this change does not break their setup.

maintainer
#3 lore raw
Quoted 41 lines · Marek Vasut
Quoted 40 lines · Heiko Schocher

Hello Marek,

Quoted 36 lines · Marek Vasut

The PGGS area contains four registers, each of which can hold up to 32bit of data. Extend the boot counter to support storing boot count in either of the four registers with arbitrary mask to avoid limiting the boot counter only to the PGGS register 2 LSByte.

Drop U_BOOT_DRVINFO() as that would instantiate the boot counter even if there is no DT node associated with it, that is undesired behavior.

The boot counter configuration is implemented using three new DT properties:
reg - PGGS register address, fallback to PGGS 2 at 0xffd80058 u-boot,bootcount-reg-mask - Register mask, applied on PGGS register u-boot,bootcount-mask - Boot counter mask, applied on boot counter u-boot,bootcount-magic - Boot counter magic, applied on boot counter

Example configuration of 16bit boot counter with 0xb0010000 magic in PGGS0:

&amba {
    bootcount@0xffd80050 {
        compatible = "u-boot,bootcount-zynqmp";
        reg = <0 0xffd80050 0 0x4>;
        u-boot,bootcount-magic = <0xb0010000>;
        u-boot,bootcount-mask = <0xffff>;
        u-boot,bootcount-reg-mask = <0xffffffff>;
    };
};

---
Cc: Heiko Schocher <[email protected]>
Cc: Michal Simek <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: Vasileios Amoiridis <[email protected]>
Cc: [email protected]
---
  drivers/bootcount/bootcount_zynqmp.c | 79 +++++++++++++++++++++++++---

  1 file changed, 71 insertions(+), 8 deletions(-)
Quoted 4 lines
Quoted 2 lines

Nitpick: Should there not be a change in a file where the new properties and the compatible entry is documented?

Do we have one ? I don't see one with git grep, but I can write one.

DT binding is something what should be solved first because it is not in linux kernel or dt-schema. And I don't think it is going to be straight forward path. Definitely I don't want to introduce u-boot specific binding which won't pass checks.

It means one thing is description for bootcount itself which is pretty much SoC independent and likely can just go to dt-schema to u-boot node itself.

The second is how to pass information about location. In this particular ZynqMP case I think ggs/pggs should be described via existing xlnx,zynqmp-nvmem-fw node and generic bootcount nvmem driver should just get pointer to that location which holds this value. That would solve description and pointer can be stored in /options/u-boot/ node ("u-boot,config" compatible string).

And get this to u-boot means to create new UCLASS_NVMEM which doesn't exist today and write nvmem driver at least for these new offsets.

Or ignore DT binding part and just extend driver and use SYS_BOOTCOUNT_ADDR/SYS_BOOTCOUNT_MAGIC/SYS_BOOTCOUNT_SINGLEWORD and only introduce MASK config option that can be changed to support your case.

Thanks,
Michal