meta-google: host-power-ctrl: Import gpio-host-pwr from gBMC

Upstream common package for gpio-host-pwr.

Change-Id: I08550d46d2b062f6b793cb5417d26231c7e4ca20
Signed-off-by: Tom Tung <shes050117@gmail.com>
diff --git a/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-ensure-off.service b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-ensure-off.service
new file mode 100644
index 0000000..96c6d20
--- /dev/null
+++ b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-ensure-off.service
@@ -0,0 +1,11 @@
+[Unit]
+Description=Ensure host is off on clean AC cycle
+Before=host-poweron.service
+Before=firmware-updates-pre.target
+
+[Service]
+Type=oneshot
+ExecStart=/usr/bin/host_ensure_off.sh
+
+[Install]
+WantedBy=multi-user.target
diff --git a/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-powercycle-watchdog.service b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-powercycle-watchdog.service
new file mode 100644
index 0000000..1b1236e
--- /dev/null
+++ b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-powercycle-watchdog.service
@@ -0,0 +1,7 @@
+[Unit]
+Description=Powercycle host
+
+[Service]
+Type=oneshot
+Environment=DONT_STOP_WATCHDOG=1
+ExecStart=/usr/bin/host_powercycle.sh
diff --git a/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-powercycle.service b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-powercycle.service
new file mode 100644
index 0000000..7ff2244
--- /dev/null
+++ b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-powercycle.service
@@ -0,0 +1,6 @@
+[Unit]
+Description=Powercycle host
+
+[Service]
+Type=oneshot
+ExecStart=/usr/bin/host_powercycle.sh
diff --git a/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-poweroff-watchdog.service b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-poweroff-watchdog.service
new file mode 100644
index 0000000..175dd1f
--- /dev/null
+++ b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-poweroff-watchdog.service
@@ -0,0 +1,7 @@
+[Unit]
+Description=Power off host
+
+[Service]
+Type=oneshot
+Environment=DONT_STOP_WATCHDOG=1
+ExecStart=/usr/bin/host_poweroff.sh
diff --git a/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-poweroff.service b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-poweroff.service
new file mode 100644
index 0000000..803e010
--- /dev/null
+++ b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-poweroff.service
@@ -0,0 +1,6 @@
+[Unit]
+Description=Power off host
+
+[Service]
+Type=oneshot
+ExecStart=/usr/bin/host_poweroff.sh
diff --git a/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-poweron.service b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-poweron.service
new file mode 100644
index 0000000..1837248
--- /dev/null
+++ b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-poweron.service
@@ -0,0 +1,10 @@
+[Unit]
+Description=Power on host
+After=firmware-updates.target
+
+[Service]
+Type=oneshot
+ExecStart=/usr/bin/host_poweron.sh
+
+[Install]
+WantedBy=multi-user.target
diff --git a/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-reset-cold-watchdog.service b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-reset-cold-watchdog.service
new file mode 100644
index 0000000..d60872c
--- /dev/null
+++ b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-reset-cold-watchdog.service
@@ -0,0 +1,7 @@
+[Unit]
+Description=Cold Reset Host
+
+[Service]
+Type=oneshot
+Environment=DONT_STOP_WATCHDOG=1
+ExecStart=/usr/bin/host_reset.sh cold
diff --git a/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-reset-cold.service b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-reset-cold.service
new file mode 100644
index 0000000..a014350
--- /dev/null
+++ b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-reset-cold.service
@@ -0,0 +1,6 @@
+[Unit]
+Description=Cold Reset Host
+
+[Service]
+Type=oneshot
+ExecStart=/usr/bin/host_reset.sh cold
diff --git a/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-reset-warm-watchdog.service b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-reset-warm-watchdog.service
new file mode 100644
index 0000000..374380e
--- /dev/null
+++ b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-reset-warm-watchdog.service
@@ -0,0 +1,7 @@
+[Unit]
+Description=Warm Reset Host
+
+[Service]
+Type=oneshot
+Environment=DONT_STOP_WATCHDOG=1
+ExecStart=/usr/bin/host_reset.sh warm
diff --git a/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-reset-warm.service b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-reset-warm.service
new file mode 100644
index 0000000..2dd4a26
--- /dev/null
+++ b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host-reset-warm.service
@@ -0,0 +1,6 @@
+[Unit]
+Description=Warm Reset Host
+
+[Service]
+Type=oneshot
+ExecStart=/usr/bin/host_reset.sh warm
diff --git a/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host_ensure_off.sh b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host_ensure_off.sh
new file mode 100755
index 0000000..ab9f472
--- /dev/null
+++ b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host_ensure_off.sh
@@ -0,0 +1,22 @@
+#!/bin/bash
+# Copyright 2021 Google LLC
+#
+# 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.
+
+if ! was_ac_reset.sh; then
+  echo "Not a tray reset, ignoring" >&2
+  exit 0
+fi
+
+echo "Powering off the host" >&2
+host_poweroff.sh
diff --git a/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host_isoff.sh b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host_isoff.sh
new file mode 100755
index 0000000..459eca7
--- /dev/null
+++ b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host_isoff.sh
@@ -0,0 +1,21 @@
+#!/bin/bash
+# Copyright 2021 Google LLC
+#
+# 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.
+
+source /usr/share/gpio-host-pwr/lib.sh || exit
+
+gpio_build_cache 10 "$HOST_GPIO_PGOOD" || return 255
+pgood="$(gpio_get_value "$HOST_GPIO_PGOOD")" || return 255
+echo "HOST_PGOOD=$pgood" >&2
+(( pgood == 0 ))
diff --git a/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host_powercycle.sh b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host_powercycle.sh
new file mode 100644
index 0000000..3c8f002
--- /dev/null
+++ b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host_powercycle.sh
@@ -0,0 +1,24 @@
+#!/bin/bash
+# Copyright 2021 Google LLC
+#
+# 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.
+
+source /usr/share/gpio-host-pwr/lib.sh || exit
+gpio_build_cache 10 "$HOST_GPIO_PGOOD" "$HOST_GPIO_PWR_BTN" || exit
+
+rc=0
+host_poweroff.sh || rc=$?
+# We want to ensure we aren't too quickly bouncing the power button
+sleep 0.5
+host_poweron.sh || rc=$?
+exit $rc
diff --git a/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host_poweroff.sh b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host_poweroff.sh
new file mode 100755
index 0000000..58c1da8
--- /dev/null
+++ b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host_poweroff.sh
@@ -0,0 +1,65 @@
+#!/bin/bash
+# Copyright 2021 Google LLC
+#
+# 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.
+
+source /usr/share/gpio-host-pwr/lib.sh || exit
+
+gpio_build_cache 10 "$HOST_GPIO_PGOOD" "$HOST_GPIO_PWR_BTN" || exit
+gpio_init "$HOST_GPIO_PGOOD" || exit
+
+# Set the power status LED
+if [ -n "$HOST_LED_PWR" ]; then
+  echo 'none' > /sys/class/leds/"$HOST_LED_PWR"/trigger || true
+  echo '0' > /sys/class/leds/"$HOST_LED_PWR"/brightness || true
+fi
+
+# Ensure the watchdog is no longer going to run
+host_pwr_stop_watchdog || true
+
+# We don't want to do anything if the machine is already off
+pgood="$(gpio_get_value "$HOST_GPIO_PGOOD")" || exit
+if (( pgood == 0 )); then
+  echo 'Host is already off, doing nothing' >&2
+  exit 0
+fi
+
+# Do a long push of the button if PGOOD
+echo 'Stopping host power' >&2
+rc=0
+gpio_set_value "$HOST_GPIO_PWR_BTN" 1 || rc=$?
+
+# Loop until we realize that host power is off
+# Limit the loop count to 10 seconds as we should never
+# have to wait this long for poweroff
+s=$SECONDS
+while true; do
+  if ! pgood="$(gpio_get_value "$HOST_GPIO_PGOOD")"; then
+    rc=1
+    break
+  fi
+  if (( pgood == 0 )); then
+    echo 'Host is now off' >&2
+    break
+  fi
+  if (( SECONDS - s > 10 )); then
+    echo 'Poweroff timed out, terminating' >&2
+    rc=2
+    break
+  fi
+  sleep 0.1
+done
+
+gpio_set_value "$HOST_GPIO_PWR_BTN" 0 || rc=$?
+
+exit $rc
diff --git a/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host_poweron.sh b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host_poweron.sh
new file mode 100755
index 0000000..2e6eb02
--- /dev/null
+++ b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host_poweron.sh
@@ -0,0 +1,65 @@
+#!/bin/bash
+# Copyright 2021 Google LLC
+#
+# 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.
+
+source /usr/share/gpio-host-pwr/lib.sh || exit
+
+gpio_build_cache 10 "$HOST_GPIO_PGOOD" "$HOST_GPIO_PWR_BTN" || exit
+gpio_init "$HOST_GPIO_PGOOD" || exit
+
+# Set the power status LED
+if [ -n "$HOST_LED_PWR" ]; then
+  echo 'none' > /sys/class/leds/"$HOST_LED_PWR"/trigger || true
+  echo '255' > /sys/class/leds/"$HOST_LED_PWR"/brightness || true
+fi
+
+# Ensure the watchdog is available before the host starts
+host_pwr_start_watchdog || true
+
+# We don't want to do anything if the machine is already on
+pgood="$(gpio_get_value "$HOST_GPIO_PGOOD")" || exit
+if (( pgood == 1 )); then
+  echo 'Host is already running, doing nothing' >&2
+  exit 0
+fi
+
+# Do a quick push of the button if PGOOD
+echo "Starting host power" >&2
+rc=0
+gpio_set_value "$HOST_GPIO_PWR_BTN" 1 || rc=$?
+sleep 0.1
+gpio_set_value "$HOST_GPIO_PWR_BTN" 0 || rc=$?
+
+# Loop until we realize that host power is on
+# Limit the loop count to 10 seconds as we should never
+# have to wait this long for poweroff
+s=$SECONDS
+while true; do
+  if ! pgood="$(gpio_get_value "$HOST_GPIO_PGOOD")"; then
+    rc=1
+    break
+  fi
+  if (( pgood == 1 )); then
+    echo 'Host is now on' >&2
+    break
+  fi
+  if (( SECONDS - s > 10 )); then
+    echo 'Poweron timed out, terminating' >&2
+    rc=2
+    break
+  fi
+  sleep 0.1
+done
+
+exit $rc
diff --git a/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host_reset.sh b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host_reset.sh
new file mode 100755
index 0000000..2f48a12
--- /dev/null
+++ b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/host_reset.sh
@@ -0,0 +1,39 @@
+#!/bin/bash
+# Copyright 2021 Google LLC
+#
+# 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.
+
+source /usr/share/gpio-host-pwr/lib.sh || exit
+
+if [[ "${1-}" == "warm" ]]; then
+  rst_txt='WARM' >&2
+  rst_gpio="$HOST_GPIO_WARM_RESET"
+else
+  rst_txt='COLD' >&2
+  rst_gpio="$HOST_GPIO_COLD_RESET"
+fi
+
+gpio_build_cache 10 "$rst_gpio" || exit
+
+# Do a quick push of the button if PGOOD
+echo "Issuing $rst_txt reset" >&2
+rc=0
+gpio_set_value "$rst_gpio" 1 || rc=$?
+sleep 0.1
+gpio_set_value "$rst_gpio" 0 || rc=$?
+
+# Make sure the watchdog is stopped while the host is in reset
+# and can't possibly restart it.
+host_pwr_stop_watchdog || true
+
+exit $rc
diff --git a/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/lib.sh b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/lib.sh
new file mode 100644
index 0000000..16b36a5
--- /dev/null
+++ b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr/lib.sh
@@ -0,0 +1,69 @@
+#!/bin/bash
+# Copyright 2021 Google LLC
+#
+# 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.
+
+# This is intended to be used as a library for managing gpio line values.
+# Executing this directly will do nothing.
+[ -n "${host_pwr_init-}" ] && return
+
+source /usr/share/gpio-ctrl/lib.sh || exit
+
+# Read by the tooling to determine if the machine is powered on or off
+HOST_GPIO_PGOOD='unset'
+# Set according to whether the host is powered on or off
+HOST_LED_PWR=''
+# Written by the tooling to assert the power button
+HOST_GPIO_PWR_BTN='unset'
+# Written by the tooling to assert a cold reset
+HOST_GPIO_COLD_RESET='unset'
+# Written by the tooling to assert a warm reset
+HOST_GPIO_WARM_RESET='unset'
+
+# Load configurations from a known location in the filesystem to populate
+# named GPIOs
+shopt -s nullglob
+for conf in /usr/share/gpio-host-pwr/conf.d/*.sh; do
+  source "$conf"
+done
+
+##################################################
+# Stop the host watchdog
+# Return:
+#   0 if success, non-zero if error
+##################################################
+host_pwr_stop_watchdog() {
+  # Check to see if we can stop the watchdog safely
+  # We don't want to stop if we are called from the watchdog itself
+  if [ -n "${DONT_STOP_WATCHDOG-}" ]; then
+    return 0
+  fi
+
+  echo 'Stopping the host watchdog' >&2
+  systemctl stop phosphor-watchdog@host0
+}
+
+##################################################
+# Start the host watchdog
+# Return:
+#   0 if success, non-zero if error
+##################################################
+host_pwr_start_watchdog() {
+  echo 'Starting the host watchdog' >&2
+  systemctl start phosphor-watchdog@host0
+}
+
+host_pwr_init=1
+return 0 2>/dev/null
+echo "gpio-host-pwr is a library, not executed directly" >&2
+exit 1
diff --git a/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr_git.bb b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr_git.bb
new file mode 100644
index 0000000..b2eee79
--- /dev/null
+++ b/meta-google/recipes-google/host-power-ctrl/gpio-host-pwr_git.bb
@@ -0,0 +1,57 @@
+SUMMARY = "GPIO based powercontrol for a host system"
+DESCRIPTION = "GPIO based powercontrol for a host system."
+PR = "r1"
+LICENSE = "Apache-2.0"
+LIC_FILES_CHKSUM = "file://${COREBASE}/meta/files/common-licenses/Apache-2.0;md5=89aea4e17d99a7cacdbeed46a0096b10"
+
+inherit systemd
+
+RDEPENDS_${PN} += " \
+  bash \
+  gpio-ctrl \
+  "
+
+SRC_URI += " \
+  file://host-ensure-off.service \
+  file://host-powercycle-watchdog.service \
+  file://host-powercycle.service \
+  file://host-poweroff-watchdog.service \
+  file://host-poweroff.service \
+  file://host-poweron.service \
+  file://host-reset-cold-watchdog.service \
+  file://host-reset-cold.service \
+  file://host-reset-warm-watchdog.service \
+  file://host-reset-warm.service \
+  file://host_ensure_off.sh \
+  file://host_isoff.sh \
+  file://host_powercycle.sh \
+  file://host_poweroff.sh \
+  file://host_poweron.sh \
+  file://host_reset.sh \
+  file://lib.sh \
+  "
+
+SYSTEMD_PACKAGES = "${PN}"
+SYSTEMD_SERVICE_${PN} += " \
+  host-reset-cold.service \
+  host-reset-cold-watchdog.service \
+  host-reset-warm.service \
+  host-reset-warm-watchdog.service \
+  host-ensure-off.service \
+  host-powercycle.service \
+  host-powercycle-watchdog.service \
+  host-poweroff.service \
+  host-poweroff-watchdog.service \
+  host-poweron.service \
+  "
+
+do_install() {
+    install -d ${D}${bindir}
+    install -m 0755 ${WORKDIR}/host_*.sh ${D}${bindir}/
+
+    install -d ${D}${datadir}/gpio-host-pwr
+    install -m 0755 ${WORKDIR}/lib.sh ${D}${datadir}/gpio-host-pwr/
+
+    install -d ${D}${systemd_system_unitdir}
+    install -m 0644 ${WORKDIR}/*.service ${D}${systemd_system_unitdir}/
+}