mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 06/13] power: reset: port Linux generic NVMEM reboot mode driver
Date: Sat, 19 Jun 2021 05:45:09 +0200	[thread overview]
Message-ID: <20210619034516.6737-7-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20210619034516.6737-1-a.fatoum@pengutronix.de>

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@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


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


  parent reply	other threads:[~2021-06-19  3:47 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-19  3:45 [PATCH 00/13] nvmem: misc enhancements Ahmad Fatoum
2021-06-19  3:45 ` [PATCH 01/13] nvmem: bsec: remove unused, left-over, struct member Ahmad Fatoum
2021-06-19  3:45 ` [PATCH 02/13] nvmem: treat devices without nvmem_bus::write as read only Ahmad Fatoum
2021-06-19  3:45 ` [PATCH 03/13] nvmem: add support for new read-only memory (rmem) binding Ahmad Fatoum
2021-06-19  3:45 ` [PATCH 04/13] nvmem: add support for nvmem-cells binding Ahmad Fatoum
2021-06-19  3:45 ` [PATCH 05/13] sandbox: use nvmem on top of stickypage for reset reason Ahmad Fatoum
2021-06-19  3:45 ` Ahmad Fatoum [this message]
2021-06-19  3:45 ` [PATCH 07/13] sandbox: use nvmem-reboot-mode instead of syscon-reboot-mode Ahmad Fatoum
2021-06-19  3:45 ` [PATCH 08/13] sandbox: dts: fix unit-address for state partition Ahmad Fatoum
2021-06-19  3:45 ` [PATCH 09/13] nvmem: add command to list nvmem devices Ahmad Fatoum
2021-06-19  3:45 ` [PATCH 10/13] sandbox: hostfile: move initcall to earlier postcore level Ahmad Fatoum
2021-06-19  3:45 ` [PATCH 11/13] of: of_net: sync of_get_mac_address with Linux for NVMEM support Ahmad Fatoum
2021-06-19  3:45 ` [PATCH 12/13] net: consult device tree for ethernet address in NVMEM as fall-back Ahmad Fatoum
2021-06-19  3:45 ` [PATCH 13/13] sandbox: ship sample environment Ahmad Fatoum
2021-06-21  6:05 ` [PATCH 00/13] nvmem: misc enhancements Sascha Hauer

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=20210619034516.6737-7-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --subject='Re: [PATCH 06/13] power: reset: port Linux generic NVMEM reboot mode driver' \
    /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

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