public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
* [PATCH v2 0/2] Add pmic bootcount driver
@ 2022-03-18 11:04 Philip Oberfichtner
  2022-03-18 11:04 ` [PATCH v2 1/2] power: pfuze100: Add MEMx register definitions Philip Oberfichtner
  2022-03-18 11:04 ` [PATCH v2 2/2] bootcount: Add pmic pfuze100 bootcount driver Philip Oberfichtner
  0 siblings, 2 replies; 5+ messages in thread
From: Philip Oberfichtner @ 2022-03-18 11:04 UTC (permalink / raw)
  To: u-boot
  Cc: Heiko Schocher, matthias.winker, Philip Oberfichtner,
	Gireesh Hiremath, Jaehoon Chung, Lokesh Vutla, Nandor Han,
	Simon Glass


This series adds a new bootcount driver. The required register definitions
and the actual driver are separated in two patches.

Changes in v2:
- Migrated bootcount driver to driver model
- Introduced error handling. Previously errors were ignored.

Philip Oberfichtner (2):
  power: pfuze100: Add MEMx register definitions
  bootcount: Add pmic pfuze100 bootcount driver

 drivers/bootcount/Kconfig         |   7 ++
 drivers/bootcount/Makefile        |   1 +
 drivers/bootcount/pmic_pfuze100.c | 161 ++++++++++++++++++++++++++++++
 include/power/pfuze100_pmic.h     |   5 +
 4 files changed, 174 insertions(+)
 create mode 100644 drivers/bootcount/pmic_pfuze100.c

-- 
2.34.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 1/2] power: pfuze100: Add MEMx register definitions
  2022-03-18 11:04 [PATCH v2 0/2] Add pmic bootcount driver Philip Oberfichtner
@ 2022-03-18 11:04 ` Philip Oberfichtner
  2022-03-26  2:46   ` Tom Rini
  2022-03-18 11:04 ` [PATCH v2 2/2] bootcount: Add pmic pfuze100 bootcount driver Philip Oberfichtner
  1 sibling, 1 reply; 5+ messages in thread
From: Philip Oberfichtner @ 2022-03-18 11:04 UTC (permalink / raw)
  To: u-boot; +Cc: Heiko Schocher, matthias.winker, Philip Oberfichtner,
	Jaehoon Chung

Add missing MEMA - MEMD register definitions for PFUZE100.

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

---

(no changes since v1)

 include/power/pfuze100_pmic.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/include/power/pfuze100_pmic.h b/include/power/pfuze100_pmic.h
index f4383ed778..278f2549bf 100644
--- a/include/power/pfuze100_pmic.h
+++ b/include/power/pfuze100_pmic.h
@@ -18,6 +18,11 @@ enum {
 	PFUZE100_REVID		= 0x03,
 	PFUZE100_FABID		= 0x04,
 
+	PFUZE100_MEMA		= 0x1c,
+	PFUZE100_MEMB		= 0x1d,
+	PFUZE100_MEMC		= 0x1e,
+	PFUZE100_MEMD		= 0x1f,
+
 	PFUZE100_SW1ABVOL	= 0x20,
 	PFUZE100_SW1ABSTBY	= 0x21,
 	PFUZE100_SW1ABOFF	= 0x22,
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 2/2] bootcount: Add pmic pfuze100 bootcount driver
  2022-03-18 11:04 [PATCH v2 0/2] Add pmic bootcount driver Philip Oberfichtner
  2022-03-18 11:04 ` [PATCH v2 1/2] power: pfuze100: Add MEMx register definitions Philip Oberfichtner
@ 2022-03-18 11:04 ` Philip Oberfichtner
  2022-03-26  2:46   ` Tom Rini
  1 sibling, 1 reply; 5+ messages in thread
From: Philip Oberfichtner @ 2022-03-18 11:04 UTC (permalink / raw)
  To: u-boot
  Cc: Heiko Schocher, matthias.winker, Philip Oberfichtner,
	Gireesh Hiremath, Lokesh Vutla, Nandor Han, Simon Glass

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

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

---

Changes in v2:
- Migrated bootcount driver to driver model
- Introduced error handling. Previously errors were ignored.

 drivers/bootcount/Kconfig         |   7 ++
 drivers/bootcount/Makefile        |   1 +
 drivers/bootcount/pmic_pfuze100.c | 161 ++++++++++++++++++++++++++++++
 3 files changed, 169 insertions(+)
 create mode 100644 drivers/bootcount/pmic_pfuze100.c

diff --git a/drivers/bootcount/Kconfig b/drivers/bootcount/Kconfig
index 607027c968..509d01d41e 100644
--- a/drivers/bootcount/Kconfig
+++ b/drivers/bootcount/Kconfig
@@ -127,6 +127,13 @@ config DM_BOOTCOUNT_I2C_EEPROM
 	  pointing to the underlying i2c eeprom device) and an optional 'offset'
 	  property are supported.
 
+config DM_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 DM_BOOTCOUNT_SPI_FLASH
 	bool "Support SPI flash devices as a backing store for bootcount"
 	depends on DM_SPI_FLASH
diff --git a/drivers/bootcount/Makefile b/drivers/bootcount/Makefile
index 3a784bb0a6..b65959a384 100644
--- a/drivers/bootcount/Makefile
+++ b/drivers/bootcount/Makefile
@@ -11,6 +11,7 @@ obj-$(CONFIG_BOOTCOUNT_EXT)	+= bootcount_ext.o
 obj-$(CONFIG_BOOTCOUNT_AM33XX_NVMEM)	+= bootcount_nvmem.o
 
 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_SPI_FLASH)	+= spi-flash.o
diff --git a/drivers/bootcount/pmic_pfuze100.c b/drivers/bootcount/pmic_pfuze100.c
new file mode 100644
index 0000000000..ad3bc03829
--- /dev/null
+++ b/drivers/bootcount/pmic_pfuze100.c
@@ -0,0 +1,161 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2018-2022 Denx Software Engineering GmbH
+ * Heiko Schocher <hs@denx.de>
+ * Philip Oberfichtner <pro@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 <bootcount.h>
+#include <dm.h>
+#include <power/pmic.h>
+#include <power/pfuze100_pmic.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+#define PFUZE_BC_MAGIC 0xdead
+
+struct bootcount_pmic_priv {
+	struct udevice *pmic;
+};
+
+static int pfuze100_get_magic(struct udevice *dev, u32 *magic)
+{
+	int ret;
+
+	ret = pmic_reg_read(dev, PFUZE100_MEMA);
+	if (ret < 0)
+		return ret;
+	*magic = ret;
+
+	ret = pmic_reg_read(dev, PFUZE100_MEMB);
+	if (ret < 0)
+		return ret;
+	*magic += ret << 8;
+
+	return 0;
+}
+
+static int pfuze100_set_magic(struct udevice *dev)
+{
+	int ret;
+
+	ret = pmic_reg_write(dev, PFUZE100_MEMA, PFUZE_BC_MAGIC & 0xff);
+	if (ret)
+		return ret;
+
+	ret = pmic_reg_write(dev, PFUZE100_MEMB, (PFUZE_BC_MAGIC >> 8) & 0xff);
+	return ret;
+}
+
+static int pfuze100_get_value(struct udevice *dev, u32 *a)
+{
+	int ret;
+
+	ret = pmic_reg_read(dev, PFUZE100_MEMC);
+	if (ret < 0)
+		return ret;
+	*a = ret;
+
+	ret = pmic_reg_read(dev, PFUZE100_MEMD);
+	if (ret < 0)
+		return ret;
+	*a += ret << 8;
+
+	return 0;
+}
+
+static int pfuze100_set_value(struct udevice *dev, u32 val)
+{
+	int ret;
+
+	ret = pmic_reg_write(dev, PFUZE100_MEMC, val & 0xff);
+	if (ret)
+		return ret;
+
+	ret = pmic_reg_write(dev, PFUZE100_MEMD, (val >> 8) & 0xff);
+	return ret;
+}
+
+static int bootcount_pmic_set(struct udevice *dev, const u32 a)
+{
+	struct bootcount_pmic_priv *priv = dev_get_priv(dev);
+
+	if (pfuze100_set_magic(priv->pmic)) {
+		debug("%s: writing magic failed\n", __func__);
+		return -EIO;
+	}
+
+	if (pfuze100_set_value(priv->pmic, a)) {
+		debug("%s: writing value failed\n", __func__);
+		return -EIO;
+	}
+
+	return 0;
+}
+
+static int bootcount_pmic_get(struct udevice *dev, u32 *a)
+{
+	struct bootcount_pmic_priv *priv = dev_get_priv(dev);
+	u32 magic;
+
+	if (pfuze100_get_magic(priv->pmic, &magic)) {
+		debug("%s: reading magic failed\n", __func__);
+		return -EIO;
+	}
+
+	if (magic != PFUZE_BC_MAGIC) {
+		*a = 0;
+		return 0;
+	}
+
+	if (pfuze100_get_value(priv->pmic, a)) {
+		debug("%s: reading value failed\n", __func__);
+		return -EIO;
+	}
+
+	return 0;
+}
+
+static int bootcount_pmic_probe(struct udevice *dev)
+{
+	struct ofnode_phandle_args phandle_args;
+	struct bootcount_pmic_priv *priv = dev_get_priv(dev);
+	struct udevice *pmic;
+
+	if (dev_read_phandle_with_args(dev, "pmic", NULL, 0, 0, &phandle_args)) {
+		debug("%s: pmic backing device not specified\n", dev->name);
+		return -ENOENT;
+	}
+
+	if (uclass_get_device_by_ofnode(UCLASS_PMIC, phandle_args.node, &pmic)) {
+		debug("%s: could not get backing device\n", dev->name);
+		return -ENODEV;
+	}
+
+	priv->pmic = pmic;
+
+	return 0;
+}
+
+static const struct bootcount_ops bootcount_pmic_ops = {
+	.get = bootcount_pmic_get,
+	.set = bootcount_pmic_set,
+};
+
+static const struct udevice_id bootcount_pmic_ids[] = {
+	{ .compatible = "u-boot,bootcount-pmic" },
+	{ }
+};
+
+U_BOOT_DRIVER(bootcount_pmic) = {
+	.name	= "bootcount-pmic",
+	.id	= UCLASS_BOOTCOUNT,
+	.priv_auto	= sizeof(struct bootcount_pmic_priv),
+	.probe	= bootcount_pmic_probe,
+	.of_match = bootcount_pmic_ids,
+	.ops	= &bootcount_pmic_ops,
+};
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 1/2] power: pfuze100: Add MEMx register definitions
  2022-03-18 11:04 ` [PATCH v2 1/2] power: pfuze100: Add MEMx register definitions Philip Oberfichtner
@ 2022-03-26  2:46   ` Tom Rini
  0 siblings, 0 replies; 5+ messages in thread
From: Tom Rini @ 2022-03-26  2:46 UTC (permalink / raw)
  To: Philip Oberfichtner
  Cc: u-boot, Heiko Schocher, matthias.winker, Jaehoon Chung

[-- Attachment #1: Type: text/plain, Size: 288 bytes --]

On Fri, Mar 18, 2022 at 12:04:37PM +0100, Philip Oberfichtner wrote:

> Add missing MEMA - MEMD register definitions for PFUZE100.
> 
> Based on work from Heiko Schocher <hs@denx.de>.
> Signed-off-by: Philip Oberfichtner <pro@denx.de>

Applied to u-boot/next, thanks!

-- 
Tom

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 2/2] bootcount: Add pmic pfuze100 bootcount driver
  2022-03-18 11:04 ` [PATCH v2 2/2] bootcount: Add pmic pfuze100 bootcount driver Philip Oberfichtner
@ 2022-03-26  2:46   ` Tom Rini
  0 siblings, 0 replies; 5+ messages in thread
From: Tom Rini @ 2022-03-26  2:46 UTC (permalink / raw)
  To: Philip Oberfichtner
  Cc: u-boot, Heiko Schocher, matthias.winker, Gireesh Hiremath,
	Lokesh Vutla, Nandor Han, Simon Glass

[-- Attachment #1: Type: text/plain, Size: 302 bytes --]

On Fri, Mar 18, 2022 at 12:04:38PM +0100, Philip Oberfichtner wrote:

> Use the MEMA - MEMD registers on the PFUZE100 as bootcount
> registers.
> 
> Based on work from Heiko Schocher <hs@denx.de>.
> Signed-off-by: Philip Oberfichtner <pro@denx.de>

Applied to u-boot/next, thanks!

-- 
Tom

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-03-26  2:48 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-03-18 11:04 [PATCH v2 0/2] Add pmic bootcount driver Philip Oberfichtner
2022-03-18 11:04 ` [PATCH v2 1/2] power: pfuze100: Add MEMx register definitions Philip Oberfichtner
2022-03-26  2:46   ` Tom Rini
2022-03-18 11:04 ` [PATCH v2 2/2] bootcount: Add pmic pfuze100 bootcount driver Philip Oberfichtner
2022-03-26  2:46   ` Tom Rini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox