All of lore.kernel.org
 help / color / mirror / Atom feed
From: Philipp Tomsich <philipp.tomsich@theobroma-systems.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v2 2/2] bootcount: add a DM RTC backing store for bootcount
Date: Tue, 27 Nov 2018 23:00:19 +0100	[thread overview]
Message-ID: <1543356019-47135-3-git-send-email-philipp.tomsich@theobroma-systems.com> (raw)
In-Reply-To: <1543356019-47135-1-git-send-email-philipp.tomsich@theobroma-systems.com>

This implements a driver using a RTC-based backing store for the DM
bootcount implementation.  The node configuring this feature will be
compatible with 'u-boot,bootcount-rtc' and the underlying RTC device
shall be reference through the property 'rtc'. An offset into the RTC
device's register space can be provided through the 'offset' property.

Tested on a RK3399-Q7 on a Flamingo carrier board using the SRAM area
of the carrier board's RV3029 RTC.

Signed-off-by: Philipp Tomsich <philipp.tomsich@theobroma-systems.com>
---

Changes in v2: None

 drivers/bootcount/Kconfig  | 20 +++++++++++
 drivers/bootcount/Makefile |  1 +
 drivers/bootcount/rtc.c    | 89 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 110 insertions(+)
 create mode 100644 drivers/bootcount/rtc.c

diff --git a/drivers/bootcount/Kconfig b/drivers/bootcount/Kconfig
index 46571eb..b7c29f2 100644
--- a/drivers/bootcount/Kconfig
+++ b/drivers/bootcount/Kconfig
@@ -80,6 +80,26 @@ config DM_BOOTCOUNT
 
 endchoice
 
+if DM_BOOTCOUNT
+
+menu "Backing stores for device-model backed bootcount"
+config DM_BOOTCOUNT_RTC
+	bool "Support RTC devices as a backing store for bootcount"
+	depends on DM_RTC
+	help
+	  Enabled reading/writing the bootcount in a DM RTC device.
+	  The wrapper device is to be specified with the compatible string
+	  'u-boot,bootcount-rtc' and the 'rtc'-property (a phandle pointing
+	  to the underlying RTC device) and an optional 'offset' property
+	  are supported.
+
+	  Accesses to the backing store are performed using the write16
+	  and read16 ops of DM RTC devices.
+
+endmenu
+
+endif
+
 config BOOTCOUNT_BOOTLIMIT
 	int "Maximum number of reboot cycles allowed"
 	default 0
diff --git a/drivers/bootcount/Makefile b/drivers/bootcount/Makefile
index 81980b3..f9841d8 100644
--- a/drivers/bootcount/Makefile
+++ b/drivers/bootcount/Makefile
@@ -9,3 +9,4 @@ obj-$(CONFIG_BOOTCOUNT_I2C)	+= bootcount_i2c.o
 obj-$(CONFIG_BOOTCOUNT_EXT)	+= bootcount_ext.o
 
 obj-$(CONFIG_DM_BOOTCOUNT)      += bootcount-uclass.o
+obj-$(CONFIG_DM_BOOTCOUNT_RTC)  += rtc.o
diff --git a/drivers/bootcount/rtc.c b/drivers/bootcount/rtc.c
new file mode 100644
index 0000000..db89fa3
--- /dev/null
+++ b/drivers/bootcount/rtc.c
@@ -0,0 +1,89 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * (C) Copyright 2018 Theobroma Systems Design und Consulting GmbH
+ */
+
+#include <common.h>
+#include <bootcount.h>
+#include <dm.h>
+#include <rtc.h>
+
+static const u8 bootcount_magic = 0xbc;
+
+struct bootcount_rtc_priv {
+	struct udevice *rtc;
+	u32 offset;
+};
+
+static int bootcount_rtc_set(struct udevice *dev, const u32 a)
+{
+	struct bootcount_rtc_priv *priv = dev_get_priv(dev);
+	const u16 val = bootcount_magic << 8 | (a & 0xff);
+
+	if (rtc_write16(priv->rtc, priv->offset, val) < 0) {
+		debug("%s: rtc_write16 failed\n", __func__);
+		return -EIO;
+	}
+
+	return 0;
+}
+
+static int bootcount_rtc_get(struct udevice *dev, u32 *a)
+{
+	struct bootcount_rtc_priv *priv = dev_get_priv(dev);
+	u16 val;
+
+	if (rtc_read16(priv->rtc, priv->offset, &val) < 0) {
+		debug("%s: rtc_write16 failed\n", __func__);
+		return -EIO;
+	}
+
+	if (val >> 8 == bootcount_magic) {
+		*a = val & 0xff;
+		return 0;
+	}
+
+	debug("%s: bootcount magic does not match on %04x\n", __func__, val);
+	return -EIO;
+}
+
+static int bootcount_rtc_probe(struct udevice *dev)
+{
+	struct ofnode_phandle_args phandle_args;
+	struct bootcount_rtc_priv *priv = dev_get_priv(dev);
+	struct udevice *rtc;
+
+	if (dev_read_phandle_with_args(dev, "rtc", NULL, 0, 0, &phandle_args)) {
+		debug("%s: rtc backing device not specified\n", dev->name);
+		return -ENOENT;
+	}
+
+	if (uclass_get_device_by_ofnode(UCLASS_RTC, phandle_args.node, &rtc)) {
+		debug("%s: could not get backing device\n", dev->name);
+		return -ENODEV;
+	}
+
+	priv->rtc = rtc;
+	priv->offset = dev_read_u32_default(dev, "offset", 0);
+
+	return 0;
+}
+
+static const struct bootcount_ops bootcount_rtc_ops = {
+	.get = bootcount_rtc_get,
+	.set = bootcount_rtc_set,
+};
+
+static const struct udevice_id bootcount_rtc_ids[] = {
+	{ .compatible = "u-boot,bootcount-rtc" },
+	{ }
+};
+
+U_BOOT_DRIVER(bootcount_rtc) = {
+	.name	= "bootcount-rtc",
+	.id	= UCLASS_BOOTCOUNT,
+	.priv_auto_alloc_size = sizeof(struct bootcount_rtc_priv),
+	.probe	= bootcount_rtc_probe,
+	.of_match = bootcount_rtc_ids,
+	.ops	= &bootcount_rtc_ops,
+};
-- 
2.1.4

  parent reply	other threads:[~2018-11-27 22:00 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-27 22:00 [U-Boot] [PATCH v2 0/2] Update RV3029 driver to DM and add DM-backed bootcount support Philipp Tomsich
2018-11-27 22:00 ` [U-Boot] [PATCH v2 1/2] bootcount: add uclass for bootcount Philipp Tomsich
2018-12-10  0:01   ` [U-Boot] [U-Boot,v2,1/2] " Philipp Tomsich
2018-12-11  1:07   ` [U-Boot] [PATCH v2 1/2] " Simon Glass
2018-12-14 15:37     ` Simon Glass
2018-12-14 16:04       ` Philipp Tomsich
2018-12-14 16:06         ` Simon Glass
2018-12-14 20:16           ` Philipp Tomsich
2018-11-27 22:00 ` Philipp Tomsich [this message]
2018-12-10  0:01   ` [U-Boot] [U-Boot, v2, 2/2] bootcount: add a DM RTC backing store " Philipp Tomsich
2018-11-28  6:29 ` [U-Boot] EXT: [PATCH v2 0/2] Update RV3029 driver to DM and add DM-backed bootcount support Ray, Ian
2018-11-28  9:25   ` Philipp Tomsich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1543356019-47135-3-git-send-email-philipp.tomsich@theobroma-systems.com \
    --to=philipp.tomsich@theobroma-systems.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.