git: 1f39ef9fa810 - stable/13 - rk805: add system poweroff support

From: Andriy Gapon <avg_at_FreeBSD.org>
Date: Sun, 21 Nov 2021 10:05:46 UTC
The branch stable/13 has been updated by avg:

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

commit 1f39ef9fa810201e64f2f1fcd306d6d991bfc58b
Author:     Andriy Gapon <avg@FreeBSD.org>
AuthorDate: 2021-11-06 17:58:43 +0000
Commit:     Andriy Gapon <avg@FreeBSD.org>
CommitDate: 2021-11-21 10:05:18 +0000

    rk805: add system poweroff support
    
    On my Rock64 neither EFI nor PSCI shutdown actually power off the board.
    RK805 does the job.
    
    (cherry picked from commit cc0b35259aed73747721718415fa1854108a276a)
---
 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 d3e04081aeb2..39755fa5ce87 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>
@@ -737,6 +739,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)
 {
@@ -797,6 +822,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,