]> git.ipfire.org Git - thirdparty/u-boot.git/commitdiff
bootcount: Use predefined count/magic bit masks
authorNiko Mauno <niko.mauno@vaisala.com>
Wed, 4 Feb 2026 13:17:35 +0000 (15:17 +0200)
committerTom Rini <trini@konsulko.com>
Wed, 18 Feb 2026 22:35:07 +0000 (16:35 -0600)
Use predefined bit masks in operations where only the magic half or
only the count half of the 32-bit value are processed.

Signed-off-by: Niko Mauno <niko.mauno@vaisala.com>
Reviewed-by: Tom Rini <trini@konsulko.com>
drivers/bootcount/bootcount.c
drivers/bootcount/bootcount_at91.c
drivers/bootcount/bootcount_davinci.c
include/bootcount.h

index 343b8a34414fb2b86b8dcd00ef5d3d009712ec3a..454e50591df676e4f13419f477e04328ec40231b 100644 (file)
@@ -19,7 +19,7 @@ __weak void bootcount_store(ulong a)
        uintptr_t flush_end;
 
 #if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
-       raw_bootcount_store(reg, (CONFIG_SYS_BOOTCOUNT_MAGIC & 0xffff0000) | a);
+       raw_bootcount_store(reg, (CONFIG_SYS_BOOTCOUNT_MAGIC & BOOTCOUNT_MAGIC_MASK) | a);
 
        flush_end = roundup(CONFIG_SYS_BOOTCOUNT_ADDR + 4,
                            CONFIG_SYS_CACHELINE_SIZE);
@@ -40,10 +40,10 @@ __weak ulong bootcount_load(void)
 #if defined(CONFIG_SYS_BOOTCOUNT_SINGLEWORD)
        u32 tmp = raw_bootcount_load(reg);
 
-       if ((tmp & 0xffff0000) != (CONFIG_SYS_BOOTCOUNT_MAGIC & 0xffff0000))
+       if ((tmp & BOOTCOUNT_MAGIC_MASK) != (CONFIG_SYS_BOOTCOUNT_MAGIC & BOOTCOUNT_MAGIC_MASK))
                return 0;
        else
-               return (tmp & 0x0000ffff);
+               return (tmp & BOOTCOUNT_COUNT_MASK);
 #else
        if (raw_bootcount_load(reg + 4) != CONFIG_SYS_BOOTCOUNT_MAGIC)
                return 0;
@@ -74,10 +74,10 @@ static int bootcount_mem_get(struct udevice *dev, u32 *a)
        if (priv->singleword) {
                u32 tmp = raw_bootcount_load(reg);
 
-               if ((tmp & 0xffff0000) != (magic & 0xffff0000))
+               if ((tmp & BOOTCOUNT_MAGIC_MASK) != (magic & BOOTCOUNT_MAGIC_MASK))
                        return -ENODEV;
 
-               *a = (tmp & 0x0000ffff);
+               *a = (tmp & BOOTCOUNT_COUNT_MASK);
        } else {
                if (raw_bootcount_load(reg + 4) != magic)
                        return -ENODEV;
@@ -98,7 +98,7 @@ static int bootcount_mem_set(struct udevice *dev, const u32 a)
        uintptr_t flush_end;
 
        if (priv->singleword) {
-               raw_bootcount_store(reg, (magic & 0xffff0000) | a);
+               raw_bootcount_store(reg, (magic & BOOTCOUNT_MAGIC_MASK) | a);
                flush_end = roundup(priv->base + 4,
                                    CONFIG_SYS_CACHELINE_SIZE);
        } else {
index 1a06db1fb7406792d6519695a2b404326070246f..1322abe921ef2e1c227aa3c45191979d52899ad5 100644 (file)
@@ -3,6 +3,7 @@
 #include <asm/io.h>
 #include <asm/arch/hardware.h>
 #include <asm/arch/at91_gpbr.h>
+#include <bootcount.h>
 
 /*
  * We combine the CONFIG_SYS_BOOTCOUNT_MAGIC and bootcount in one 32-bit
@@ -13,7 +14,7 @@ void bootcount_store(ulong a)
 {
        at91_gpbr_t *gpbr = (at91_gpbr_t *) ATMEL_BASE_GPBR;
 
-       writel((CONFIG_SYS_BOOTCOUNT_MAGIC & 0xffff0000) | (a & 0x0000ffff),
+       writel((CONFIG_SYS_BOOTCOUNT_MAGIC & BOOTCOUNT_MAGIC_MASK) | (a & BOOTCOUNT_COUNT_MASK),
               &gpbr->reg[AT91_GPBR_INDEX_BOOTCOUNT]);
 }
 
@@ -22,8 +23,8 @@ ulong bootcount_load(void)
        at91_gpbr_t *gpbr = (at91_gpbr_t *) ATMEL_BASE_GPBR;
 
        ulong val = readl(&gpbr->reg[AT91_GPBR_INDEX_BOOTCOUNT]);
-       if ((val & 0xffff0000) != (CONFIG_SYS_BOOTCOUNT_MAGIC & 0xffff0000))
+       if ((val & BOOTCOUNT_MAGIC_MASK) != (CONFIG_SYS_BOOTCOUNT_MAGIC & BOOTCOUNT_MAGIC_MASK))
                return 0;
        else
-               return val & 0x0000ffff;
+               return val & BOOTCOUNT_COUNT_MASK;
 }
index 6326957d7b044f8eff93ef69b834d7ee8880fda1..a03d160a4cd2c221a6cf21d88224a2d5b08cad79 100644 (file)
@@ -24,7 +24,7 @@ void bootcount_store(ulong a)
        writel(RTC_KICK0R_WE, &reg->kick0r);
        writel(RTC_KICK1R_WE, &reg->kick1r);
        raw_bootcount_store(&reg->scratch2,
-               (CONFIG_SYS_BOOTCOUNT_MAGIC & 0xffff0000) | (a & 0x0000ffff));
+               (CONFIG_SYS_BOOTCOUNT_MAGIC & BOOTCOUNT_MAGIC_MASK) | (a & BOOTCOUNT_COUNT_MASK));
 }
 
 ulong bootcount_load(void)
@@ -34,8 +34,8 @@ ulong bootcount_load(void)
                (struct davinci_rtc *)CONFIG_SYS_BOOTCOUNT_ADDR;
 
        val = raw_bootcount_load(&reg->scratch2);
-       if ((val & 0xffff0000) != (CONFIG_SYS_BOOTCOUNT_MAGIC & 0xffff0000))
+       if ((val & BOOTCOUNT_MAGIC_MASK) != (CONFIG_SYS_BOOTCOUNT_MAGIC & BOOTCOUNT_MAGIC_MASK))
                return 0;
        else
-               return val & 0x0000ffff;
+               return val & BOOTCOUNT_COUNT_MASK;
 }
index 847c0f02d9843e9772ab07624e0e6fee32b15c99..86474569d36561e23a50dfe380dd7b5b4e39997b 100644 (file)
@@ -10,6 +10,7 @@
 #include <asm/io.h>
 #include <asm/byteorder.h>
 #include <env.h>
+#include <linux/bitops.h>
 
 #ifdef CONFIG_DM_BOOTCOUNT
 
@@ -59,6 +60,10 @@ int dm_bootcount_set(struct udevice *dev, u32 bootcount);
 
 #endif
 
+/* Bit masks for magic and count parts in single word scheme */
+#define BOOTCOUNT_MAGIC_MASK   GENMASK(31, 16)
+#define BOOTCOUNT_COUNT_MASK   GENMASK(15, 0)
+
 /** bootcount_store() - store the current bootcount */
 void bootcount_store(ulong);