git: cc0b35259aed - main - rk805: add system poweroff support

From: Andriy Gapon <avg_at_FreeBSD.org>
Date: Sat, 06 Nov 2021 18:00:56 UTC
The branch main has been updated by avg:

URL: https://cgit.FreeBSD.org/src/commit/?id=cc0b35259aed73747721718415fa1854108a276a

commit cc0b35259aed73747721718415fa1854108a276a
Author:     Andriy Gapon <avg@FreeBSD.org>
AuthorDate: 2021-11-06 17:58:43 +0000
Commit:     Andriy Gapon <avg@FreeBSD.org>
CommitDate: 2021-11-06 17:58:43 +0000

    rk805: add system poweroff support
    
    On my Rock64 neither EFI nor PSCI shutdown actually power off the board.
    RK805 does the job.
    
    Reviewed by:    manu, peterj
    Differential Revision:  https://reviews.freebsd.org/D30786
---
 sys/arm64/rockchip/rk805.c    | 36 ++++++++++++++++++++++++++++++++++++
 sys/arm64/rockchip/rk805reg.h |  4 ++++
 2 files changed, 40 insertions(+)

diff --git a/sys/arm64/rockchip/rk805.c b/sys/arm64/rockchip/rk805.c
index a2ee53b35a07..d3bda29ae362 100644
--- a/sys/arm64/rockchip/rk805.c
+++ b/sys/arm64/rockchip/rk805.c
@@ -31,8 +31,10 @@ __FBSDID("$FreeBSD$");
 #include <sys/param.h>
 #include <sys/bus.h>
 #include <sys/clock.h>
+#include <sys/eventhandler.h>
 #include <sys/kernel.h>
 #include <sys/module.h>
+#include <sys/reboot.h>
 #include <sys/mutex.h>
 #include <sys/rman.h>
 #include <machine/bus.h>
@@ -844,6 +846,29 @@ rk805_settime(device_t dev, struct timespec *ts)
 	return (error);
 }
 
+static void
+rk805_poweroff(void *arg, int howto)
+{
+	device_t dev = arg;
+	int error;
+	uint8_t val;
+
+	if ((howto & RB_POWEROFF) == 0)
+		return;
+
+	device_printf(dev, "Powering off...\n");
+	error = rk805_read(dev, RK805_DEV_CTRL, &val, 1);
+	if (error == 0) {
+		val |= RK805_DEV_CTRL_OFF;
+		error = rk805_write(dev, RK805_DEV_CTRL, &val, 1);
+
+		/* Wait a bit for the command to take effect. */
+		if (error == 0)
+			DELAY(100);
+	}
+	device_printf(dev, "Power off failed\n");
+}
+
 static int
 rk805_attach(device_t dev)
 {
@@ -907,6 +932,17 @@ rk805_attach(device_t dev)
 		}
 	}
 
+	if (OF_hasprop(ofw_bus_get_node(dev),
+	    "rockchip,system-power-controller")) {
+		/*
+		 * The priority is chosen to override PSCI and EFI shutdown
+		 * methods as those two just hang without powering off on Rock64
+		 * at least.
+		 */
+		EVENTHANDLER_REGISTER(shutdown_final, rk805_poweroff, dev,
+		    SHUTDOWN_PRI_LAST - 2);
+	}
+
 	return (0);
 }
 
diff --git a/sys/arm64/rockchip/rk805reg.h b/sys/arm64/rockchip/rk805reg.h
index b1f4481a5b68..61c6f49abd2c 100644
--- a/sys/arm64/rockchip/rk805reg.h
+++ b/sys/arm64/rockchip/rk805reg.h
@@ -93,6 +93,10 @@
 #define	RK808_LDO8_ON_VSEL	0x49
 #define	RK808_LDO8_SLEEP_VSEL	0x4A
 
+#define	RK805_DEV_CTRL		0x4B
+#define	 RK805_DEV_CTRL_OFF	(1 << 0)
+#define	 RK805_DEV_CTRL_SLP	(1 << 1)
+
 enum rk805_regulator {
 	RK805_DCDC1 = 0,
 	RK805_DCDC2,