meta-facebook: tiogapass: powerctrl

The setup_gpio.sh, power service, and power-util are not applicable to
all platforms.  They will be moved to the tiogapass platform layer.
The setup_gpio.sh uses exported GPIO, which can impact the libgpio
function.

Change-Id: I7a95f8a5466f220d3c5fb8aadb2507d0f23ce857
Signed-off-by: Peter Yin <peter.yin@quantatw.com>
diff --git a/meta-facebook/meta-tiogapass/recipes-phosphor/images/fb-tiogapass-phosphor-image.inc b/meta-facebook/meta-tiogapass/recipes-phosphor/images/fb-tiogapass-phosphor-image.inc
new file mode 100644
index 0000000..b8a2845
--- /dev/null
+++ b/meta-facebook/meta-tiogapass/recipes-phosphor/images/fb-tiogapass-phosphor-image.inc
@@ -0,0 +1,5 @@
+# Tiogapass Image configuration.
+
+OBMC_IMAGE_EXTRA_INSTALL:append = " \
+    fb-powerctrl \
+"
diff --git a/meta-facebook/meta-tiogapass/recipes-tiogapass/fb-powerctrl/fb-powerctrl_0.1.bb b/meta-facebook/meta-tiogapass/recipes-tiogapass/fb-powerctrl/fb-powerctrl_0.1.bb
new file mode 100644
index 0000000..21bd220
--- /dev/null
+++ b/meta-facebook/meta-tiogapass/recipes-tiogapass/fb-powerctrl/fb-powerctrl_0.1.bb
@@ -0,0 +1,28 @@
+LICENSE = "Apache-2.0"
+LIC_FILES_CHKSUM = "file://${COREBASE}/meta/files/common-licenses/Apache-2.0;md5=89aea4e17d99a7cacdbeed46a0096b10"
+
+inherit allarch systemd obmc-phosphor-systemd
+
+RDEPENDS:${PN} += "bash"
+
+SRC_URI += " \
+    file://setup_gpio.sh \
+    file://power-util \
+    file://host-gpio.service \
+    file://host-poweroff.service \
+    file://host-poweron.service \
+    "
+
+SYSTEMD_PACKAGES = "${PN}"
+SYSTEMD_SERVICE:${PN}:append = " \
+    host-gpio.service host-poweron.service \
+    host-poweroff.service \
+    "
+
+S = "${WORKDIR}"
+
+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/meta-facebook/meta-tiogapass/recipes-tiogapass/fb-powerctrl/files/LICENSE b/meta-facebook/meta-tiogapass/recipes-tiogapass/fb-powerctrl/files/LICENSE
new file mode 100644
index 0000000..2caf820
--- /dev/null
+++ b/meta-facebook/meta-tiogapass/recipes-tiogapass/fb-powerctrl/files/LICENSE
@@ -0,0 +1,13 @@
+Copyright 2018 Facebook 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/meta-facebook/meta-tiogapass/recipes-tiogapass/fb-powerctrl/files/host-gpio.service b/meta-facebook/meta-tiogapass/recipes-tiogapass/fb-powerctrl/files/host-gpio.service
new file mode 100644
index 0000000..82ba12e
--- /dev/null
+++ b/meta-facebook/meta-tiogapass/recipes-tiogapass/fb-powerctrl/files/host-gpio.service
@@ -0,0 +1,9 @@
+[Unit]
+Description=Configure GPIOs for Tiogapass
+
+[Service]
+Restart=no
+RemainAfterExit=true
+Type=oneshot
+ExecStart=/usr/sbin/setup_gpio.sh
+SyslogIdentifier=setup_gpio.sh
diff --git a/meta-facebook/meta-tiogapass/recipes-tiogapass/fb-powerctrl/files/host-poweroff.service b/meta-facebook/meta-tiogapass/recipes-tiogapass/fb-powerctrl/files/host-poweroff.service
new file mode 100644
index 0000000..d23ef90
--- /dev/null
+++ b/meta-facebook/meta-tiogapass/recipes-tiogapass/fb-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 mb off
+SyslogIdentifier=power-util
diff --git a/meta-facebook/meta-tiogapass/recipes-tiogapass/fb-powerctrl/files/host-poweron.service b/meta-facebook/meta-tiogapass/recipes-tiogapass/fb-powerctrl/files/host-poweron.service
new file mode 100644
index 0000000..446df38
--- /dev/null
+++ b/meta-facebook/meta-tiogapass/recipes-tiogapass/fb-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 mb on
+SyslogIdentifier=power-util
+
+[Install]
+WantedBy=basic.target
diff --git a/meta-facebook/meta-tiogapass/recipes-tiogapass/fb-powerctrl/files/power-util b/meta-facebook/meta-tiogapass/recipes-tiogapass/fb-powerctrl/files/power-util
new file mode 100755
index 0000000..d00dfde
--- /dev/null
+++ b/meta-facebook/meta-tiogapass/recipes-tiogapass/fb-powerctrl/files/power-util
@@ -0,0 +1,83 @@
+#!/bin/bash
+# Usage of this utility
+function usage() {
+    echo "usage: power-util mb [on|off|status|cycle|reset]";
+    echo "       power-util sled-cycle"
+}
+
+power_off() {
+    echo "Shutting down Server"
+    busctl set-property xyz.openbmc_project.State.Chassis /xyz/openbmc_project/state/chassis0 xyz.openbmc_project.State.Chassis RequestedPowerTransition s xyz.openbmc_project.State.Chassis.Transition.Off
+}
+
+power_on() {
+    echo "Powering on Server"
+    busctl set-property xyz.openbmc_project.State.Chassis /xyz/openbmc_project/state/chassis0 xyz.openbmc_project.State.Chassis RequestedPowerTransition s xyz.openbmc_project.State.Chassis.Transition.On
+}
+
+power_status() {
+    st=$(busctl get-property xyz.openbmc_project.State.Chassis /xyz/openbmc_project/state/chassis0 xyz.openbmc_project.State.Chassis CurrentPowerState | cut -d"." -f6)
+    if [ "$st" == "On\"" ]; then
+        echo "on"
+    else
+        echo "off"
+    fi
+}
+
+power_reset() {
+    echo "Reset on server"
+    busctl set-property xyz.openbmc_project.State.Chassis /xyz/openbmc_project/state/chassis0 xyz.openbmc_project.State.Chassis RequestedPowerTransition s xyz.openbmc_project.State.Chassis.Transition.Reset
+}
+
+sled_cycle() {
+    i2cset -y 7 0x45 0xd9 c
+}
+
+if [ "$1" == "sled-cycle" ]; then
+    echo "SLED_CYCLE starting at $(date)"
+    sled_cycle
+    exit 0;
+fi
+
+if [ $# -lt 2 ]; then
+    echo "Total number of parameter=$#"
+    echo "Insufficient parameter"
+    usage;
+    exit 0;
+fi
+
+if [ "$1" != "mb" ]; then
+    echo "Invalid parameter1=$1"
+    usage;
+    exit 0;
+fi
+
+if [ "$2" = "on" ]; then
+  if [ "$(power_status)" == "off" ]; then
+      power_on
+  fi
+elif [ "$2" = "off" ]; then
+  if [ "$(power_status)" == "on" ]; then
+      power_off
+  fi
+elif [ "$2" == "cycle" ]; then
+  if [ "$(power_status)" == "on" ]; then
+      power_off
+  else
+    echo "WARNING: Powering on server"
+  fi
+    power_on
+elif [ "$2" == "reset" ]; then
+  if [ "$(power_status)" == "on" ]; then
+      power_reset
+  else
+    echo "ERROR: Server not powered on"
+  fi
+elif [ "$2" == "status" ]; then
+    power_status
+else
+    echo "Invalid parameter2=$2"
+    usage;
+fi
+
+exit 0;
diff --git a/meta-facebook/meta-tiogapass/recipes-tiogapass/fb-powerctrl/files/setup_gpio.sh b/meta-facebook/meta-tiogapass/recipes-tiogapass/fb-powerctrl/files/setup_gpio.sh
new file mode 100755
index 0000000..cac3b20
--- /dev/null
+++ b/meta-facebook/meta-tiogapass/recipes-tiogapass/fb-powerctrl/files/setup_gpio.sh
@@ -0,0 +1,22 @@
+#!/bin/bash
+
+# Set all output GPIOs as such and drive them with reasonable values.
+function set_gpio_active_low() {
+  if [ $# -ne 2 ]; then
+    echo "set_gpio_active_low: need both GPIO# and initial level";
+    return;
+  fi
+
+  echo "$1" > /sys/class/gpio/export
+  echo "$2" > "/sys/class/gpio/gpio$1/direction"
+}
+
+GPIO_BASE=$(cat /sys/class/gpio/gpio*/base)
+
+# FM_BMC_READY_N, GPIO S1, active low
+set_gpio_active_low $((GPIO_BASE + 144 +1)) low
+
+# FP_PECI_MUX, active low
+set_gpio_active_low $((GPIO_BASE + 212)) high
+
+exit 0;