[PATCH 06/13] power: reset: port Linux generic NVMEM reboot mode driver

Ahmad Fatoum a.fatoum at pengutronix.de
Fri Jun 18 20:45:09 PDT 2021


Not all systems can realize their reboot mode protocol via the syscon
driver:

 - There might be no registers available for use that survive a warm
   reset
 - There might be no warm resets and every reset resets is a cold one

For these systems, the reboot mode can instead be stored in NVMEM.
Port over the Linux nvmem-reboot-mode driver to make this possible.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 drivers/power/reset/Kconfig             | 10 +++
 drivers/power/reset/Makefile            |  1 +
 drivers/power/reset/nvmem-reboot-mode.c | 83 +++++++++++++++++++++++++
 3 files changed, 94 insertions(+)
 create mode 100644 drivers/power/reset/nvmem-reboot-mode.c

diff --git a/drivers/power/reset/Kconfig b/drivers/power/reset/Kconfig
index dec1482ccd0c..e4151d8bc608 100644
--- a/drivers/power/reset/Kconfig
+++ b/drivers/power/reset/Kconfig
@@ -13,6 +13,16 @@ config SYSCON_REBOOT_MODE
 	  Say y here will enable reboot mode driver. This will
 	  get reboot mode arguments and store it in SYSCON mapped
 	  register, then the bootloader can read it to take different
+
+config NVMEM_REBOOT_MODE
+	bool "Generic NVMEM reboot mode driver"
+	depends on OFDEVICE
+	depends on NVMEM
+	select REBOOT_MODE
+	help
+	  Say y here will enable reboot mode driver. This will
+	  get reboot mode arguments and store it in a NVMEM cell,
+	  then the bootloader can read it and take different
 	  action according to the mode.
 
 config POWER_RESET_SYSCON
diff --git a/drivers/power/reset/Makefile b/drivers/power/reset/Makefile
index 33d29d2d9546..10d6f2a41e22 100644
--- a/drivers/power/reset/Makefile
+++ b/drivers/power/reset/Makefile
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
 obj-$(CONFIG_REBOOT_MODE) += reboot-mode.o
 obj-$(CONFIG_SYSCON_REBOOT_MODE) += syscon-reboot-mode.o
+obj-$(CONFIG_NVMEM_REBOOT_MODE) += nvmem-reboot-mode.o
 obj-$(CONFIG_POWER_RESET_SYSCON) += syscon-reboot.o
 obj-$(CONFIG_POWER_RESET_SYSCON_POWEROFF) += syscon-poweroff.o
 obj-$(CONFIG_POWER_RESET_GPIO) += gpio-poweroff.o
diff --git a/drivers/power/reset/nvmem-reboot-mode.c b/drivers/power/reset/nvmem-reboot-mode.c
new file mode 100644
index 000000000000..b82b37d642e7
--- /dev/null
+++ b/drivers/power/reset/nvmem-reboot-mode.c
@@ -0,0 +1,83 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) Vaisala Oyj. All rights reserved.
+ */
+
+#include <common.h>
+#include <init.h>
+#include <of.h>
+#include <linux/nvmem-consumer.h>
+#include <linux/reboot-mode.h>
+
+struct nvmem_reboot_mode {
+	struct reboot_mode_driver reboot;
+	struct nvmem_cell *cell;
+};
+
+static int nvmem_reboot_mode_write(struct reboot_mode_driver *reboot,
+				   const u32 *_magic)
+{
+	struct nvmem_reboot_mode *nvmem_rbm;
+	u32 magic = *_magic;
+	int ret;
+
+	nvmem_rbm = container_of(reboot, struct nvmem_reboot_mode, reboot);
+
+	ret = nvmem_cell_write(nvmem_rbm->cell, &magic, sizeof(magic));
+	if (ret < 0)
+		dev_err(reboot->dev, "update reboot mode bits failed: %pe\n", ERR_PTR(ret));
+	else if (ret != 4)
+		ret = -EIO;
+	else
+		ret = 0;
+
+	return ret;
+}
+
+static int nvmem_reboot_mode_probe(struct device_d *dev)
+{
+	struct nvmem_reboot_mode *nvmem_rbm;
+	struct nvmem_cell *cell;
+	void *magicbuf;
+	size_t len;
+	int ret;
+
+	cell = nvmem_cell_get(dev, "reboot-mode");
+	if (IS_ERR(cell)) {
+		ret = PTR_ERR(cell);
+		if (ret != -EPROBE_DEFER)
+			dev_err(dev, "failed to get the nvmem cell reboot-mode: %pe\n", cell);
+		return ret;
+	}
+
+	nvmem_rbm = xzalloc(sizeof(*nvmem_rbm));
+
+	nvmem_rbm->cell = cell;
+	nvmem_rbm->reboot.dev = dev;
+	nvmem_rbm->reboot.write = nvmem_reboot_mode_write;
+	nvmem_rbm->reboot.priority = 200;
+
+	magicbuf = nvmem_cell_read(nvmem_rbm->cell, &len);
+	if (IS_ERR(magicbuf) || len != 4) {
+		dev_err(dev, "error reading reboot mode: %pe\n", magicbuf);
+		return PTR_ERR(magicbuf);
+	}
+
+	ret = reboot_mode_register(&nvmem_rbm->reboot, magicbuf, 1);
+	if (ret)
+		dev_err(dev, "can't register reboot mode\n");
+
+	return ret;
+}
+
+static const struct of_device_id nvmem_reboot_mode_of_match[] = {
+	{ .compatible = "nvmem-reboot-mode" },
+	{ /* sentinel */ }
+};
+
+static struct driver_d nvmem_reboot_mode_driver = {
+	.probe = nvmem_reboot_mode_probe,
+	.name = "nvmem-reboot-mode",
+	.of_compatible = nvmem_reboot_mode_of_match,
+};
+coredevice_platform_driver(nvmem_reboot_mode_driver);
-- 
2.29.2




More information about the barebox mailing list