bootcount: add a DM SPI flash backing store for bootcount

This driver allows to use SPI flash as backing store for
boot counter values with DM enabled.

Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>
diff --git a/drivers/bootcount/Kconfig b/drivers/bootcount/Kconfig
index c8e6fa7..b5ccea0 100644
--- a/drivers/bootcount/Kconfig
+++ b/drivers/bootcount/Kconfig
@@ -108,6 +108,16 @@
 	  pointing to the underlying i2c eeprom device) and an optional 'offset'
 	  property are supported.
 
+config DM_BOOTCOUNT_SPI_FLASH
+	bool "Support SPI flash devices as a backing store for bootcount"
+	depends on DM_SPI_FLASH
+	help
+	  Enabled reading/writing the bootcount in a DM SPI flash device.
+	  The wrapper device is to be specified with the compatible string
+	  'u-boot,bootcount-spi-flash' and the 'spi-flash'-property (a phandle
+	  pointing to the underlying SPI flash device) and an optional 'offset'
+	  property are supported.
+
 config BOOTCOUNT_MEM
 	bool "Support memory based bootcounter"
 	help
diff --git a/drivers/bootcount/Makefile b/drivers/bootcount/Makefile
index 059d40d..51d860b 100644
--- a/drivers/bootcount/Makefile
+++ b/drivers/bootcount/Makefile
@@ -12,3 +12,4 @@
 obj-$(CONFIG_DM_BOOTCOUNT)      += bootcount-uclass.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/spi-flash.c b/drivers/bootcount/spi-flash.c
new file mode 100644
index 0000000..7cd388e
--- /dev/null
+++ b/drivers/bootcount/spi-flash.c
@@ -0,0 +1,125 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * (C) Copyright 2019 Collabora
+ * (C) Copyright 2019 GE
+ */
+
+#include <common.h>
+#include <bootcount.h>
+#include <dm.h>
+#include <spi_flash.h>
+
+static const u8 bootcount_magic = 0xbc;
+
+struct bootcount_spi_flash_priv {
+	struct udevice *spi_flash;
+	u32 offset;
+};
+
+static int bootcount_spi_flash_update(struct udevice *dev, u32 offset, u32 len, const void *buf)
+{
+	struct spi_flash *flash = dev_get_uclass_priv(dev);
+	u32 sector_size = flash->sector_size;
+	u32 sector_offset = offset % sector_size;
+	u32 sector = offset - sector_offset;
+	int err = 0;
+
+	/* code only supports updating a single sector */
+	if (sector_offset + len > sector_size)
+		return -ENOSYS;
+
+	u8 *buffer = malloc(sector_size);
+	if (!buffer)
+		return -ENOMEM;
+
+	err = spi_flash_read_dm(dev, sector, sector_size, buffer);
+	if (err < 0)
+		goto out;
+
+	memcpy(buffer + sector_offset, buf, len);
+
+	err = spi_flash_erase_dm(dev, sector, sector_size);
+	if (err < 0)
+		goto out;
+
+	err = spi_flash_write_dm(dev, sector, sector_size, buffer);
+	if (err < 0)
+		goto out;
+
+out:
+	free(buffer);
+	return err;
+}
+
+static int bootcount_spi_flash_set(struct udevice *dev, const u32 a)
+{
+	struct bootcount_spi_flash_priv *priv = dev_get_priv(dev);
+	const u16 val = bootcount_magic << 8 | (a & 0xff);
+
+	if (bootcount_spi_flash_update(priv->spi_flash, priv->offset, 2, &val) < 0) {
+		debug("%s: write failed\n", __func__);
+		return -EIO;
+	}
+
+	return 0;
+}
+
+static int bootcount_spi_flash_get(struct udevice *dev, u32 *a)
+{
+	struct bootcount_spi_flash_priv *priv = dev_get_priv(dev);
+	u16 val;
+
+	if (spi_flash_read_dm(priv->spi_flash, priv->offset, 2, &val) < 0) {
+		debug("%s: read 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_spi_flash_probe(struct udevice *dev)
+{
+	struct ofnode_phandle_args phandle_args;
+	struct bootcount_spi_flash_priv *priv = dev_get_priv(dev);
+	struct udevice *spi_flash;
+
+	if (dev_read_phandle_with_args(dev, "spi-flash", NULL, 0, 0, &phandle_args)) {
+		debug("%s: spi-flash backing device not specified\n", dev->name);
+		return -ENOENT;
+	}
+
+	if (uclass_get_device_by_ofnode(UCLASS_SPI_FLASH, phandle_args.node, &spi_flash)) {
+		debug("%s: could not get backing device\n", dev->name);
+		return -ENODEV;
+	}
+
+	priv->spi_flash = spi_flash;
+	priv->offset = dev_read_u32_default(dev, "offset", 0);
+
+	return 0;
+}
+
+static const struct bootcount_ops bootcount_spi_flash_ops = {
+	.get = bootcount_spi_flash_get,
+	.set = bootcount_spi_flash_set,
+};
+
+static const struct udevice_id bootcount_spi_flash_ids[] = {
+	{ .compatible = "u-boot,bootcount-spi-flash" },
+	{ }
+};
+
+U_BOOT_DRIVER(bootcount_spi_flash) = {
+	.name	= "bootcount-spi-flash",
+	.id	= UCLASS_BOOTCOUNT,
+	.priv_auto_alloc_size = sizeof(struct bootcount_spi_flash_priv),
+	.probe	= bootcount_spi_flash_probe,
+	.of_match = bootcount_spi_flash_ids,
+	.ops	= &bootcount_spi_flash_ops,
+};