mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] RISC-V: virt: support poweroff/restart on tinyemu
@ 2021-09-16  9:35 Ahmad Fatoum
  2021-10-04 13:09 ` Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Ahmad Fatoum @ 2021-09-16  9:35 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

QEMU Virt on RISC-V has syscon-reboot and syscon-poweroff compatible
devices and describes them in the device tree. TinyEMU's Virt machine
is different and has a HTIF based poweroff and no dedicated reset
mechanism. Add board support for the HTIF poweroff and use a poor man's
reset that jumps back to the reset vector.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 arch/riscv/Kconfig.socs             |  8 +++
 arch/riscv/boards/Makefile          |  1 +
 arch/riscv/boards/riscvemu/Makefile |  1 +
 arch/riscv/boards/riscvemu/board.c  | 76 +++++++++++++++++++++++++++++
 4 files changed, 86 insertions(+)
 create mode 100644 arch/riscv/boards/riscvemu/Makefile
 create mode 100644 arch/riscv/boards/riscvemu/board.c

diff --git a/arch/riscv/Kconfig.socs b/arch/riscv/Kconfig.socs
index 221ea133d4d0..1bd9b4d579d9 100644
--- a/arch/riscv/Kconfig.socs
+++ b/arch/riscv/Kconfig.socs
@@ -22,6 +22,14 @@ config SOC_VIRT
 	  Generates an image tht can be be booted by QEMU. The image is called
 	  barebox-dt-2nd.img
 
+config BOARD_RISCVEMU
+	depends on SOC_VIRT
+	bool "TinyEMU Virt Machine (riscvemu)"
+	default y
+	help
+	  TinyEMU's Virt machine differs from QEMU in poweroff and restart
+	  mechanisms. This adds the necessary support.
+
 config CPU_SIFIVE
 	bool
 	select HAS_CACHE
diff --git a/arch/riscv/boards/Makefile b/arch/riscv/boards/Makefile
index cb28a25d8bc8..99630266d126 100644
--- a/arch/riscv/boards/Makefile
+++ b/arch/riscv/boards/Makefile
@@ -2,3 +2,4 @@
 obj-$(CONFIG_BOARD_ERIZO_GENERIC)	+= erizo/
 obj-$(CONFIG_BOARD_HIFIVE)		+= hifive/
 obj-$(CONFIG_BOARD_BEAGLEV)		+= beaglev/
+obj-$(CONFIG_BOARD_RISCVEMU)		+= riscvemu/
diff --git a/arch/riscv/boards/riscvemu/Makefile b/arch/riscv/boards/riscvemu/Makefile
new file mode 100644
index 000000000000..dcfc2937d325
--- /dev/null
+++ b/arch/riscv/boards/riscvemu/Makefile
@@ -0,0 +1 @@
+obj-y += board.o
diff --git a/arch/riscv/boards/riscvemu/board.c b/arch/riscv/boards/riscvemu/board.c
new file mode 100644
index 000000000000..7dbf9afe4c2d
--- /dev/null
+++ b/arch/riscv/boards/riscvemu/board.c
@@ -0,0 +1,76 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2021 Ahmad Fatoum, Pengutronix
+ */
+
+#include <common.h>
+#include <driver.h>
+#include <poweroff.h>
+#include <restart.h>
+#include <asm/system.h>
+#include <asm/barebox-riscv.h>
+
+struct riscvemu_priv {
+	struct restart_handler rst;
+	void __noreturn (*restart)(unsigned long, void *);
+
+};
+
+#define HTIF_BASE_ADDR		IOMEM(0x40008000)
+#define HTIF_TOHOST_LOW		(HTIF_BASE_ADDR + 0)
+#define HTIF_TOHOST_HIGH	(HTIF_BASE_ADDR + 4)
+
+static void __noreturn riscvemu_poweroff(struct poweroff_handler *pwr)
+{
+	writel(1, HTIF_TOHOST_LOW);
+	writel(0, HTIF_TOHOST_HIGH);
+
+	__builtin_unreachable();
+}
+
+static void __noreturn riscvemu_restart(struct restart_handler *rst)
+{
+	struct riscvemu_priv *priv = container_of(rst, struct riscvemu_priv, rst);
+
+	/*
+	 * barebox PBL relocates itself to end of RAM early on, so unless
+	 * something explicitly scrubbed the initial PBL, we can jump back to
+	 * the reset vector to "reset".
+	 */
+	priv->restart(riscv_hartid(), barebox_riscv_boot_dtb());
+}
+
+static int riscvemu_probe(struct device_d *dev)
+{
+	struct device_node *of_chosen;
+	struct riscvemu_priv *priv;
+	u64 start;
+
+	if (of_find_compatible_node(NULL, NULL, "ucb,htif0"))
+		poweroff_handler_register_fn(riscvemu_poweroff);
+
+	of_chosen = of_find_node_by_path("/chosen");
+
+	if (of_property_read_u64(of_chosen, "riscv,kernel-start", &start))
+		return 0;
+
+	priv = xzalloc(sizeof(*priv));
+
+	priv->restart = (void *)(uintptr_t)start;
+	priv->rst.restart = riscvemu_restart;
+	priv->rst.name = "vector";
+
+	return restart_handler_register(&priv->rst);
+}
+
+static const struct of_device_id riscvemu_of_match[] = {
+	{ .compatible = "ucbbar,riscvemu-bar_dev" },
+	{ /* sentinel */ },
+};
+
+static struct driver_d riscvemu_board_driver = {
+	.name = "board-riscvemu",
+	.probe = riscvemu_probe,
+	.of_compatible = riscvemu_of_match,
+};
+device_platform_driver(riscvemu_board_driver);
-- 
2.30.2


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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-10-04 13:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-16  9:35 [PATCH] RISC-V: virt: support poweroff/restart on tinyemu Ahmad Fatoum
2021-10-04 13:09 ` Sascha Hauer

mail archive of the barebox mailing list

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://lore.barebox.org/barebox/0 barebox/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 barebox barebox/ https://lore.barebox.org/barebox \
		barebox@lists.infradead.org
	public-inbox-index barebox

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git