meta-amd: Add recipe for power control.

This patch adds gpio configuration and power-util script for power on,
off and reset.

Change-Id: I43d80f02ae5dce21c6639430dac76989ada287eb
Signed-off-by: Supreeth Venkatesh <supreeth.venkatesh@amd.com>
diff --git a/recipes-amd/amd-powerctrl/amd-powerctrl.bb b/recipes-amd/amd-powerctrl/amd-powerctrl.bb
new file mode 100644
index 0000000..a94356f
--- /dev/null
+++ b/recipes-amd/amd-powerctrl/amd-powerctrl.bb
@@ -0,0 +1,26 @@
+FILESEXTRAPATHS_append := "${THISDIR}/files:"
+
+inherit obmc-phosphor-systemd
+LICENSE = "Apache-2.0"
+LIC_FILES_CHKSUM = "file://LICENSE;md5=ab6ba4709f1286c534f36392ac1dcd27"
+
+S = "${WORKDIR}/"
+
+SRC_URI = "file://setup_gpio.sh \
+           file://power-util \
+           file://host-gpio.service \
+           file://host-poweroff.service \
+           file://host-poweron.service \
+           file://LICENSE"
+
+DEPENDS = "systemd"
+RDEPENDS_${PN} = "bash"
+
+SYSTEMD_PACKAGES = "${PN}"
+SYSTEMD_SERVICE_${PN} = "host-gpio.service host-poweron.service host-poweroff.service"
+
+do_install() {
+    install -d ${D}/usr/sbin
+    install -m 0755 ${S}setup_gpio.sh ${D}/${sbindir}/
+    install -m 0755 ${S}power-util ${D}/${sbindir}/
+}
diff --git a/recipes-amd/amd-powerctrl/files/LICENSE b/recipes-amd/amd-powerctrl/files/LICENSE
new file mode 100644
index 0000000..e5a0f63
--- /dev/null
+++ b/recipes-amd/amd-powerctrl/files/LICENSE
@@ -0,0 +1,13 @@
+Copyright 2020 AMD Inc
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
diff --git a/recipes-amd/amd-powerctrl/files/host-gpio.service b/recipes-amd/amd-powerctrl/files/host-gpio.service
new file mode 100644
index 0000000..346ff94
--- /dev/null
+++ b/recipes-amd/amd-powerctrl/files/host-gpio.service
@@ -0,0 +1,9 @@
+[Unit]
+Description=Configure GPIOs for EthanolX
+
+[Service]
+Restart=no
+RemainAfterExit=true
+Type=oneshot
+ExecStart=/usr/sbin/setup_gpio.sh
+SyslogIdentifier=setup_gpio.sh
diff --git a/recipes-amd/amd-powerctrl/files/host-poweroff.service b/recipes-amd/amd-powerctrl/files/host-poweroff.service
new file mode 100644
index 0000000..8b00801
--- /dev/null
+++ b/recipes-amd/amd-powerctrl/files/host-poweroff.service
@@ -0,0 +1,9 @@
+[Unit]
+Description=Shutdown Host Server
+Requires=host-gpio.service
+After=host-gpio.service
+
+[Service]
+Type=oneshot
+ExecStart=/usr/sbin/power-util off
+SyslogIdentifier=power-util
diff --git a/recipes-amd/amd-powerctrl/files/host-poweron.service b/recipes-amd/amd-powerctrl/files/host-poweron.service
new file mode 100644
index 0000000..1f52628
--- /dev/null
+++ b/recipes-amd/amd-powerctrl/files/host-poweron.service
@@ -0,0 +1,12 @@
+[Unit]
+Description=Poweron Host Server
+Requires=host-gpio.service
+After=host-gpio.service
+
+[Service]
+Type=oneshot
+ExecStart=/usr/sbin/power-util on
+SyslogIdentifier=power-util
+
+[Install]
+WantedBy=basic.target
diff --git a/recipes-amd/amd-powerctrl/files/power-util b/recipes-amd/amd-powerctrl/files/power-util
new file mode 100755
index 0000000..b67db36
--- /dev/null
+++ b/recipes-amd/amd-powerctrl/files/power-util
@@ -0,0 +1,83 @@
+#!/bin/bash
+
+GPIO_BASE=$(cat /sys/devices/platform/ahb/ahb:apb/1e780000.gpio/gpio/*/base)
+#MGMT_ASSERT_PWR_BTN - Asserts the system power button, GPIO M0, Active High,  (Default = Low)
+GPIO_PWR_BTN=$(($GPIO_BASE + 96 + 0))
+#MGMT_ASSERT_RST_BTN - Assert the system cold reset button, GPIO M1, Active High,  (Default = Low)
+GPIO_RST_BTN=$(($GPIO_BASE + 96 + 1))
+# MGMT_ASSERT_BMC_READY - Hold low until BMC has completed initialization, then drive high, GPIO M7,  (Default = Low)
+GPIO_BMC_READY=$((${GPIO_BASE} + 96 + 7))
+
+# Usage of this utility
+function usage() {
+	echo "usage: power-util [on|off|cycle|status|reset]";
+}
+
+power_off() {
+	echo "Shutting down Server"
+	POWER_STATUS=off
+	printf %s "$POWER_STATUS" > /tmp/power_status
+	echo 1 > /sys/class/gpio/gpio${GPIO_PWR_BTN}/value
+	# Long Press
+	sleep 5
+	echo 0 > /sys/class/gpio/gpio${GPIO_PWR_BTN}/value
+}
+
+power_on() {
+	echo "Powering on Server"
+	echo 1 > /sys/class/gpio/gpio${GPIO_PWR_BTN}/value
+	# Short Press
+	sleep 1
+	echo 0 > /sys/class/gpio/gpio${GPIO_PWR_BTN}/value
+	POWER_STATUS=on
+	printf %s "$POWER_STATUS" > /tmp/power_status
+}
+
+power_status() {
+	POWER_STATUS=$(cat /tmp/power_status)
+	echo ${POWER_STATUS}
+}
+
+power_reset() {
+	echo "Toggle Cold Reset"
+	echo 1 > /sys/class/gpio/gpio${GPIO_RST_BTN}/value
+	sleep 1
+	echo 0 > /sys/class/gpio/gpio${GPIO_RST_BTN}/value
+}
+
+if [ $# -lt 1 ]; then
+	echo "Total number of parameter=$#"
+	echo "Insufficient parameter"
+	usage;
+	exit 0;
+fi
+
+if [ $1 = "on" ]; then
+	if [ $(power_status) == "off" ]; then
+		power_on
+	fi
+elif [ $1 = "off" ]; then
+	if [ $(power_status) == "on" ]; then
+		power_off
+	fi
+elif [ $1 == "cycle" ]; then
+	if [ $(power_status) == "on" ]; then
+		power_off
+	else
+		echo "WARNING: Powering on server"
+		power_on
+	fi
+elif [ $1 == "reset" ]; then
+	if [ $(power_status) == "on" ]; then
+		power_reset
+	else
+		echo "ERROR: Server not powered on"
+	fi
+elif [ $1 == "status" ]; then
+	power_status
+else
+	echo "Invalid parameter=$1"
+	usage;
+fi
+
+exit 0;
diff --git a/recipes-amd/amd-powerctrl/files/setup_gpio.sh b/recipes-amd/amd-powerctrl/files/setup_gpio.sh
new file mode 100755
index 0000000..629bb48
--- /dev/null
+++ b/recipes-amd/amd-powerctrl/files/setup_gpio.sh
@@ -0,0 +1,46 @@
+#!/bin/bash
+
+# Set all output GPIOs as such and drive them with reasonable values.
+function set_gpio_active_low() {
+  if [ $# -ne 3 ]; then
+    echo "set_gpio_active_low: need GPIO#, active_low and initial value";
+    return;
+  fi
+
+  echo $1 > /sys/class/gpio/export
+  echo $2 > /sys/class/gpio/gpio$1/active_low
+  echo $3 > /sys/class/gpio/gpio$1/direction
+}
+
+GPIO_BASE=$(cat /sys/devices/platform/ahb/ahb:apb/1e780000.gpio/gpio/*/base)
+# MGMT_ASSERT_BMC_READY - Hold low until BMC has completed initialization, then drive high, GPIO M7,  (Default = Low)
+GPIO_BMC_READY=$((${GPIO_BASE} + 96 + 7))
+
+#MGMT_ASSERT_PWR_BTN - Asserts the system power button, GPIO M0, Active High,  (Default = Low)
+set_gpio_active_low $((${GPIO_BASE} + 96 + 0)) 0 low
+
+#MGMT_ASSERT_RST_BTN - Assert the system cold reset button, GPIO M1, Active High,  (Default = Low)
+set_gpio_active_low $((${GPIO_BASE} + 96 + 1)) 0 low
+
+#MGMT_ASSERT_NMI_BTN - Asserts the system NMI button, GPIO M2 Active High,  (Default = Low)
+set_gpio_active_low $((${GPIO_BASE} + 96 + 2)) 0 low
+
+#MGMT_ASSERT_P0_PROCHOT - Asserts the P0 PROCHOT_L signal, GPIO M4, Active High,  (Default = Low)
+set_gpio_active_low $((${GPIO_BASE} + 96 + 4)) 0 low
+
+#MGMT_ASSERT_P1_PROCHOT - Asserts the P1 PROCHOT_L signal, GPIO M6, Active High,  (Default = Low)
+set_gpio_active_low $((${GPIO_BASE} + 96 + 5)) 0 low
+
+#MGMT_ASSERT_CLR_CMOS - Clears processor CMOS, assert high for 1s To clear CMOS,  (Default = Low)
+set_gpio_active_low $((${GPIO_BASE} + 96 + 6)) 0 low
+
+# MGMT_ASSERT_BMC_READY - Hold low until BMC has completed initialization, then drive high, GPIO M7,  (Default = Low)
+set_gpio_active_low $((${GPIO_BASE} + 96 + 7)) 0 low
+
+# DRIVE BMC_READY HIGH
+echo 1 > /sys/class/gpio/gpio${GPIO_BMC_READY}/value
+POWER_STATUS=on
+# Write to temp. file named "power_status"
+rm -f /tmp/power_status
+printf %s "$POWER_STATUS" > /tmp/power_status
+exit 0;