summaryrefslogtreecommitdiff
path: root/drivers/bootcount/bootcount_dm_i2c.c
diff options
context:
space:
mode:
authorTom Rini <[email protected]>2026-02-18 16:35:12 -0600
committerTom Rini <[email protected]>2026-02-18 16:35:12 -0600
commitcf6aa7cf731d3b1b296802e47e9800b0d6bed65a (patch)
tree84dfbc1bb014bcae37f1f9239075d8cb4b082f62 /drivers/bootcount/bootcount_dm_i2c.c
parent68e542d4f59f37889b8822f9a98d526c720f4273 (diff)
parentf97642853cd379ef69d00bca6c3928bce226dc5e (diff)
Merge patch series "bootcount: Small clean-up and fix, and dm_i2c single-word support"
Niko Mauno <[email protected]> says: In this series, we first introduce a clean-up where we switch to use predefined bit masks instead of hard-coded values for count and magic halves in the single-word (32-bit) boot count scheme. Then we fix a case of missing boot count value masking in single-word scenario in bootcount.c, which allowed clobbering of the magic half when storing the value. With this change the clobbering preventing behavior becomes consistent with existing single word bootcount storing implementations in bootcount_at91.c and bootcount_davinci.c. Finally, we enable the DM I2C bootcount driver to work also in single word (4 byte) mode, in addition to the pre-existing half-word (2 byte) mode. By default the driver still operates in half word mode as so far, but can now be used alternatively in single word mode by adding 'size = <0x4>;' in the associated device tree node. Link: https://lore.kernel.org/r/[email protected]
Diffstat (limited to 'drivers/bootcount/bootcount_dm_i2c.c')
-rw-r--r--drivers/bootcount/bootcount_dm_i2c.c74
1 files changed, 54 insertions, 20 deletions
diff --git a/drivers/bootcount/bootcount_dm_i2c.c b/drivers/bootcount/bootcount_dm_i2c.c
index e27034cbeb0..07359ecfa6c 100644
--- a/drivers/bootcount/bootcount_dm_i2c.c
+++ b/drivers/bootcount/bootcount_dm_i2c.c
@@ -15,6 +15,7 @@
struct bootcount_i2c_priv {
struct udevice *bcdev;
unsigned int offset;
+ unsigned int size;
};
static int bootcount_i2c_set(struct udevice *dev, const u32 val)
@@ -22,13 +23,22 @@ static int bootcount_i2c_set(struct udevice *dev, const u32 val)
int ret;
struct bootcount_i2c_priv *priv = dev_get_priv(dev);
- ret = dm_i2c_reg_write(priv->bcdev, priv->offset, BC_MAGIC);
- if (ret < 0)
- goto err_exit;
-
- ret = dm_i2c_reg_write(priv->bcdev, priv->offset + 1, val & 0xff);
- if (ret < 0)
- goto err_exit;
+ if (priv->size == 4) {
+ u32 bc = (CONFIG_SYS_BOOTCOUNT_MAGIC & BOOTCOUNT_MAGIC_MASK)
+ | (val & BOOTCOUNT_COUNT_MASK);
+
+ ret = dm_i2c_write(priv->bcdev, priv->offset, (uint8_t *)&bc, sizeof(bc));
+ if (ret < 0)
+ goto err_exit;
+ } else {
+ ret = dm_i2c_reg_write(priv->bcdev, priv->offset, BC_MAGIC);
+ if (ret < 0)
+ goto err_exit;
+
+ ret = dm_i2c_reg_write(priv->bcdev, priv->offset + 1, val & 0xff);
+ if (ret < 0)
+ goto err_exit;
+ }
return 0;
@@ -42,21 +52,39 @@ static int bootcount_i2c_get(struct udevice *dev, u32 *val)
int ret;
struct bootcount_i2c_priv *priv = dev_get_priv(dev);
- ret = dm_i2c_reg_read(priv->bcdev, priv->offset);
- if (ret < 0)
- goto err_exit;
-
- if ((ret & 0xff) != BC_MAGIC) {
- log_debug("%s: Invalid Magic, reset bootcounter.\n", __func__);
- *val = 0;
- return bootcount_i2c_set(dev, 0);
+ if (priv->size == 4) {
+ u32 bc;
+
+ ret = dm_i2c_read(priv->bcdev, priv->offset, (uint8_t *)&bc, sizeof(bc));
+ if (ret < 0)
+ goto err_exit;
+
+ if ((bc & BOOTCOUNT_MAGIC_MASK) !=
+ (CONFIG_SYS_BOOTCOUNT_MAGIC & BOOTCOUNT_MAGIC_MASK)) {
+ log_debug("%s: Invalid Magic, reset bootcounter.\n", __func__);
+ *val = 0;
+ return bootcount_i2c_set(dev, 0);
+ }
+
+ *val = (bc & BOOTCOUNT_COUNT_MASK);
+ } else {
+ ret = dm_i2c_reg_read(priv->bcdev, priv->offset);
+ if (ret < 0)
+ goto err_exit;
+
+ if ((ret & 0xff) != BC_MAGIC) {
+ log_debug("%s: Invalid Magic, reset bootcounter.\n", __func__);
+ *val = 0;
+ return bootcount_i2c_set(dev, 0);
+ }
+
+ ret = dm_i2c_reg_read(priv->bcdev, priv->offset + 1);
+ if (ret < 0)
+ goto err_exit;
+
+ *val = ret;
}
- ret = dm_i2c_reg_read(priv->bcdev, priv->offset + 1);
- if (ret < 0)
- goto err_exit;
-
- *val = ret;
return 0;
err_exit:
@@ -73,6 +101,12 @@ static int bootcount_i2c_probe(struct udevice *dev)
if (ret)
goto exit;
+ priv->size = dev_read_u32_default(dev, "size", 2);
+ if (priv->size != 2 && priv->size != 4) {
+ ret = -EINVAL;
+ goto exit;
+ }
+
ret = i2c_get_chip_by_phandle(dev, "i2cbcdev", &priv->bcdev);
exit: