U-Boot Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Lukasz Majewski <lukma@denx.de>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH 3/3] bootcount: add DM-based backing store for bootcount
Date: Tue, 14 Aug 2018 13:10:13 +0200	[thread overview]
Message-ID: <20180814131013.3b665c38@jawa> (raw)
In-Reply-To: <1534240262-8904-4-git-send-email-philipp.tomsich@theobroma-systems.com>

Hi Philipp,

> The original bootcount methods do not provide an interface to DM and
> rely on a static configuration for I2C devices (e.g. bus, chip-addr,
> etc. are configured through defines statically).  On a modern system
> that exposes multiple devices in a DTS-configurable way, this is less
> than optimal and a interface to DM-based devices will be desirable.
> 
> This adds a simple driver that is DM-aware and configurable via DTS:
> the /chosen/u-boot,bootcount-device property is used to detect the DM
> device storing the bootcount and deviceclass-specific commands are
> used to read/write the bootcount.
> 
> Initially, this provides support for the following DM devices:
>  * RTC devices implementing the read8/write8 ops
> 
> Signed-off-by: Philipp Tomsich <philipp.tomsich@theobroma-systems.com>
> Tested-by: Klaus Goger <klaus.goger@theobroma-systems.com>
> ---
> 
>  doc/device-tree-bindings/chosen.txt | 27 +++++++++++
>  drivers/bootcount/Kconfig           | 12 +++++
>  drivers/bootcount/Makefile          |  1 +
>  drivers/bootcount/bootcount_dm.c    | 93
> +++++++++++++++++++++++++++++++++++++ 4 files changed, 133
> insertions(+) create mode 100644 drivers/bootcount/bootcount_dm.c
> 
> diff --git a/doc/device-tree-bindings/chosen.txt
> b/doc/device-tree-bindings/chosen.txt index da7b4e6..734fd15 100644
> --- a/doc/device-tree-bindings/chosen.txt
> +++ b/doc/device-tree-bindings/chosen.txt
> @@ -42,6 +42,33 @@ Example
>  	};
>  };
>  
> +u-boot,bootcount-device property
> +--------------------------------
> +In a DM-based system, the bootcount may be stored in a device known
> to +the DM framework (e.g. in a battery-backed SRAM area within a RTC
> +device).  To identify the device to be used for the bootcount, the
> +u-boot,bootcount-device property needs to point to the target device.
> +
> +Further configuration in the target device's node may be required
> +(e.g. an offset into an I2C RTC's address space), depending on the
> +UCLASS of the target device.
> +
> +Example
> +-------
> +/ {
> +	chosen {
> +	        u-boot,bootcount-device = &rv3029;
> +	};
> +
> +	i2c2 {
> +	        rv3029: rtc at 56 {
> +		                compatible = "mc,rv3029";
> +		                reg = <0x56>;
> +				u-boot,bootcount-offset = <0x38>;
> +		};
> +	};
> +};
> +
>  u-boot,spl-boot-order property
>  ------------------------------
>  
> diff --git a/drivers/bootcount/Kconfig b/drivers/bootcount/Kconfig
> index d335ed1..cdde7b5 100644
> --- a/drivers/bootcount/Kconfig
> +++ b/drivers/bootcount/Kconfig
> @@ -70,6 +70,18 @@ config BOOTCOUNT_AT91
>  	bool "Boot counter for Atmel AT91SAM9XE"
>  	depends on AT91SAM9XE
>  
> +config BOOTCOUNT_DM
> +        bool "Boot counter in a device-model device"
> +	help
> +	  Enables reading/writing the bootcount in a device-model
> +	  device referenced from the /chosen node.  The type of the
> +	  device is detected from DM and any additional configuration
> +	  information (e.g. the offset into a RTC device that
> supports
> +	  read32/write32) is read from the device's node.
> +
> +	  At this time the following DM device classes are supported:
> +	   * RTC (using read32/write32)
> +
>  endchoice
>  
>  config BOOTCOUNT_ALEN
> diff --git a/drivers/bootcount/Makefile b/drivers/bootcount/Makefile
> index 68bc006..e8ed729 100644
> --- a/drivers/bootcount/Makefile
> +++ b/drivers/bootcount/Makefile
> @@ -7,3 +7,4 @@ obj-$(CONFIG_BOOTCOUNT_RAM)	+= bootcount_ram.o
>  obj-$(CONFIG_BOOTCOUNT_ENV)	+= bootcount_env.o
>  obj-$(CONFIG_BOOTCOUNT_I2C)	+= bootcount_i2c.o
>  obj-$(CONFIG_BOOTCOUNT_EXT)	+= bootcount_ext.o
> +obj-$(CONFIG_BOOTCOUNT_DM)      += bootcount_dm.o
> diff --git a/drivers/bootcount/bootcount_dm.c
> b/drivers/bootcount/bootcount_dm.c new file mode 100644
> index 0000000..99bdb88
> --- /dev/null
> +++ b/drivers/bootcount/bootcount_dm.c
> @@ -0,0 +1,93 @@
> +// 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>
> +
> +const u8 bootcount_magic = 0xbc;
> +
> +static void bootcount_store_rtc(struct udevice *dev, ulong a)
> +{
> +#if CONFIG_IS_ENABLED(DM_RTC)
> +	u32 offset;
> +	const char *offset_propname = "u-boot,bootcount-offset";
> +	const u16 val = bootcount_magic << 8 | (a & 0xff);
> +
> +	if (dev_read_u32(dev, offset_propname, &offset) < 0) {
> +		debug("%s: requires %s\n", __func__,
> offset_propname);
> +		return;
> +	}
> +
> +	if (rtc_write16(dev, offset, val) < 0) {
> +		debug("%s: rtc_write16 failed\n", __func__);
> +		return;
> +	}
> +#endif
> +}
> +
> +static u32 bootcount_load_rtc(struct udevice *dev)
> +{
> +#if CONFIG_IS_ENABLED(DM_RTC)
> +	u32 offset;
> +	const char *offset_propname = "u-boot,bootcount-offset";
> +	u16 val;
> +
> +	if (dev_read_u32(dev, offset_propname, &offset) < 0) {
> +		debug("%s: requires %s\n", __func__,
> offset_propname);
> +		goto fail;
> +	}
> +
> +	if (rtc_read16(dev, offset, &val) < 0) {
> +		debug("%s: rtc_write16 failed\n", __func__);
> +		goto fail;
> +	}
> +
> +	if (val >> 8 == bootcount_magic)
> +		return val & 0xff;
> +
> +	debug("%s: bootcount magic does not match on %04x\n",
> __func__, val);
> + fail:
> +#endif
> +	return 0;
> +}
> +
> +/* Now implement the generic default functions */
> +void bootcount_store(ulong a)
> +{
> +	struct udevice *dev;
> +	ofnode node;
> +	const char *propname = "u-boot,bootcount-device";
> +
> +	node = ofnode_get_chosen_node(propname);
> +	if (!ofnode_valid(node)) {
> +		debug("%s: no '%s'\n", __func__, propname);
> +		return;
> +	}
> +
> +	/* RTC devices */
> +	if (!uclass_get_device_by_ofnode(UCLASS_RTC, node, &dev))
> +		return bootcount_store_rtc(dev, a);
> +}
> +
> +ulong bootcount_load(void)
> +{
> +	struct udevice *dev;
> +	ofnode node;
> +	const char *propname = "u-boot,bootcount-device";
> +
> +	node = ofnode_get_chosen_node(propname);
> +	if (!ofnode_valid(node)) {
> +		debug("%s: no '%s'\n", __func__, propname);
> +		return 0;
> +	}
> +
> +	/* RTC devices */
> +	if (!uclass_get_device_by_ofnode(UCLASS_RTC, node, &dev))
> +		return bootcount_load_rtc(dev);
> +
> +	return 0;
> +}

Thanks for your patch.

However, if I may ask - would it be possible to add support for EEPROM
based bootcount in an easy way?

I mean - do you think that it would be feasible to have
bootcount-uclass, which would support generic load/store functions with
DM drivers for RTC, EEPROM or even simple write to SFR registers (as it
is just an offset in the end)?


Best regards,

Lukasz Majewski

--

DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20180814/414cc4b5/attachment.sig>

  reply	other threads:[~2018-08-14 11:10 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-14  9:50 [U-Boot] [PATCH 0/3] Update RV3029 driver to DM and add DM-backed bootcount support Philipp Tomsich
2018-08-14  9:51 ` [U-Boot] [PATCH 1/3] rtc: rv3029: add to Kconfig Philipp Tomsich
2018-08-14 16:46   ` Heinrich Schuchardt
2018-08-14 16:52     ` Dr. Philipp Tomsich
2018-08-14  9:51 ` [U-Boot] [PATCH 2/3] rtc: rv3029: update to support DM and sync with Linux 4.17 Philipp Tomsich
2018-08-14  9:51 ` [U-Boot] [PATCH 3/3] bootcount: add DM-based backing store for bootcount Philipp Tomsich
2018-08-14 11:10   ` Lukasz Majewski [this message]
2018-08-14 11:39     ` Dr. Philipp Tomsich
2018-08-17 12:49       ` Simon Glass
2018-08-17 12:56         ` Dr. Philipp Tomsich
2018-08-23 10:45           ` Simon Glass
2018-08-23 13:48             ` Dr. Philipp Tomsich
2018-08-30  0:29               ` Simon Glass

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=20180814131013.3b665c38@jawa \
    --to=lukma@denx.de \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox