Use the MEMA - MEMD registers on the PFUZE100 as bootcount
registers.

Based on work from Heiko Schocher <h...@denx.de>.
Signed-off-by: Philip Oberfichtner <p...@denx.de>

---

 drivers/bootcount/Kconfig                   |  7 ++
 drivers/bootcount/Makefile                  |  1 +
 drivers/bootcount/bootcount_pmic_pfuze100.c | 83 +++++++++++++++++++++
 3 files changed, 91 insertions(+)
 create mode 100644 drivers/bootcount/bootcount_pmic_pfuze100.c

diff --git a/drivers/bootcount/Kconfig b/drivers/bootcount/Kconfig
index 607027c968..9c37fa8ec8 100644
--- a/drivers/bootcount/Kconfig
+++ b/drivers/bootcount/Kconfig
@@ -73,6 +73,13 @@ config BOOTCOUNT_ENV
          So the Userspace Application must set the "upgrade_available"
          and "bootcount" variable to 0, if a boot was successfully.
 
+config BOOTCOUNT_PMIC_PFUZE100
+       bool "Enable Bootcount driver for PMIC PFUZE100"
+       depends on DM_PMIC_PFUZE100
+       help
+         Enable support for the bootcounter using PMIC PFUZE100 registers.
+         This works only, if the PMIC is not connected.
+
 config BOOTCOUNT_RAM
        bool "Boot counter in RAM"
        help
diff --git a/drivers/bootcount/Makefile b/drivers/bootcount/Makefile
index 3a784bb0a6..d167815b3c 100644
--- a/drivers/bootcount/Makefile
+++ b/drivers/bootcount/Makefile
@@ -9,6 +9,7 @@ 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
+obj-$(CONFIG_BOOTCOUNT_PMIC_PFUZE100)  += bootcount_pmic_pfuze100.o
 
 obj-$(CONFIG_DM_BOOTCOUNT)      += bootcount-uclass.o
 obj-$(CONFIG_DM_BOOTCOUNT_RTC)  += rtc.o
diff --git a/drivers/bootcount/bootcount_pmic_pfuze100.c 
b/drivers/bootcount/bootcount_pmic_pfuze100.c
new file mode 100644
index 0000000000..d732618218
--- /dev/null
+++ b/drivers/bootcount/bootcount_pmic_pfuze100.c
@@ -0,0 +1,83 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2018 Denx Software Engineering GmbH
+ * Heiko Schocher <h...@denx.de>
+ *
+ * A bootcount driver using the registers MEMA - MEMD on the PFUZE100.
+ * This works only, if the PMIC is not connected.
+ */
+
+#include <common.h>
+#include <fdtdec.h>
+#include <errno.h>
+#include <dm.h>
+#include <i2c.h>
+#include <power/pmic.h>
+#include <power/regulator.h>
+#include <power/pfuze100_pmic.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+#define PFUZE_BC_MAGIC 0xdead
+
+static ulong pfuze100_get_magic(struct udevice *dev)
+{
+       ulong magic;
+
+       magic = pmic_reg_read(dev, PFUZE100_MEMA);
+       magic += pmic_reg_read(dev, PFUZE100_MEMB) << 8;
+       return magic;
+}
+
+static void pfuze100_set_magic(struct udevice *dev)
+{
+       pmic_reg_write(dev, PFUZE100_MEMA, PFUZE_BC_MAGIC & 0xff);
+       pmic_reg_write(dev, PFUZE100_MEMB, (PFUZE_BC_MAGIC >> 8) & 0xff);
+}
+
+static ulong pfuze100_get_value(struct udevice *dev)
+{
+       ulong val;
+
+       val = pmic_reg_read(dev, PFUZE100_MEMC);
+       val += pmic_reg_read(dev, PFUZE100_MEMD) << 8;
+       return val;
+}
+
+static void pfuze100_set_val(struct udevice *dev, ulong val)
+{
+       pmic_reg_write(dev, PFUZE100_MEMC, val & 0xff);
+       pmic_reg_write(dev, PFUZE100_MEMD, (val >> 8) & 0xff);
+}
+
+void bootcount_store(ulong a)
+{
+       struct udevice *dev;
+       int ret;
+
+       ret = pmic_get("pfuze100@8", &dev);
+       if (ret != 0)
+               return;
+
+       pfuze100_set_magic(dev);
+       pfuze100_set_val(dev, a);
+}
+
+ulong bootcount_load(void)
+{
+       struct udevice *dev;
+       int ret;
+       ulong val;
+       ulong magic;
+
+       ret = pmic_get("pfuze100@8", &dev);
+       if (ret != 0)
+               return 0;
+
+       magic = pfuze100_get_magic(dev);
+       if (magic != PFUZE_BC_MAGIC)
+               return 0;
+
+       val = pfuze100_get_value(dev);
+       return val;
+}
-- 
2.34.1

Reply via email to