meta-ampere: power control: restructure source code

ampere-hostctrl package includes the generic codes and the platform
specific codes. The generic sources are applied for all of the Ampere
platforms, they are the service files and the scripts to setting the
GPIO pins. The platform specific sources are the GPIO pin config file
and the gpio monitor config files.
This commit moves the platform specific files to the platform code.

Tested:
    1. Do the power control actions use ipmitool, BMC web and Redfish.
    2. Make sure the server states are changed as expected.

Signed-off-by: ThuBaNguyen <thu@os.amperecomputing.com>
Change-Id: Ic0e261109f7a5dfaf38c883bff0afedab763a0b6
diff --git a/meta-ampere/meta-jade/recipes-ampere/platform/mtjade-utils.bb b/meta-ampere/meta-jade/recipes-ampere/platform/mtjade-utils.bb
new file mode 100644
index 0000000..184d643
--- /dev/null
+++ b/meta-ampere/meta-jade/recipes-ampere/platform/mtjade-utils.bb
@@ -0,0 +1,18 @@
+SUMMARY = "Ampere Platform Environment Definitions"
+DESCRIPTION = "Ampere Platform Environment Definitions"
+PR = "r1"
+LICENSE = "Apache-2.0"
+LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/Apache-2.0;md5=89aea4e17d99a7cacdbeed46a0096b10"
+
+SRC_URI = " \
+          file://gpio-defs.sh \
+          file://ampere_power_util.sh \
+          "
+
+RDEPENDS_${PN} = "bash"
+
+do_install() {
+    install -d ${D}/usr/sbin
+    install -m 0755 ${WORKDIR}/gpio-defs.sh ${D}/${sbindir}/
+    install -m 0755 ${WORKDIR}/ampere_power_util.sh ${D}/${sbindir}/
+}
\ No newline at end of file
diff --git a/meta-ampere/meta-jade/recipes-ampere/platform/mtjade-utils/ampere_power_util.sh b/meta-ampere/meta-jade/recipes-ampere/platform/mtjade-utils/ampere_power_util.sh
new file mode 100644
index 0000000..4ad07a6
--- /dev/null
+++ b/meta-ampere/meta-jade/recipes-ampere/platform/mtjade-utils/ampere_power_util.sh
@@ -0,0 +1,147 @@
+#!/bin/bash
+#ampere_platform_config.sh is platform configuration file
+source /usr/sbin/gpio-defs.sh
+
+# Usage of this utility
+function usage() {
+  echo "usage: power-util mb [on|status|cycle|reset|graceful_reset|force_reset|soft_off]";
+}
+
+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
+}
+
+shutdown_ack() {
+  if [ -f "/run/openbmc/host@0-softpoweroff" ]; then
+    echo "Receive shutdown ACK triggered after softportoff the host."
+    touch /run/openbmc/host@0-softpoweroff-shutdown-ack
+  else
+    echo "Receive shutdown ACK triggered"
+<<<<<<< HEAD
+  fi
+}
+
+graceful_shutdown() {
+  if [ -f "/run/openbmc/host@0-request" ]; then
+    echo "shutdown host immediately"
+    power_off
+  else
+    echo "Triggering graceful shutdown"
+    gpioset -l 0 49=1
+    sleep 1
+    gpioset -l 0 49=0
+    sleep 30s
+=======
+    sleep 3
+    systemctl start obmc-chassis-poweroff@0.target
+>>>>>>> 397e033ef... meta-ampere: power control: refactor host power control
+  fi
+}
+
+soft_off() {
+  # Trigger shutdown_req
+  touch /run/openbmc/host@0-softpoweroff
+  gpioset -l $GPIO_CHIP0_IDX $S0_SHD_REQ_L=1
+  sleep 1s
+  gpioset -l $GPIO_CHIP0_IDX $S0_SHD_REQ_L=0
+
+  # Wait for shutdown_ack from the host in 30 seconds
+  cnt=30
+  while [ $cnt -gt 0 ];
+  do
+    # Wait for SHUTDOWN_ACK and create the host@0-softpoweroff-shutdown-ack
+    if [ -f "/run/openbmc/host@0-softpoweroff-shutdown-ack" ]; then
+      break
+    fi
+    sleep 1
+    cnt=$((cnt - 1))
+  done
+  # Softpoweroff is successed
+  sleep 2
+  rm -rf /run/openbmc/host@0-softpoweroff
+  if [ -f "/run/openbmc/host@0-softpoweroff-shutdown-ack" ]; then
+    rm -rf /run/openbmc/host@0-softpoweroff-shutdown-ack
+  fi
+  echo 0
+}
+
+force_reset() {
+  if [ -f "/run/openbmc/host@0-softpoweroff" ]; then
+    # In graceful host reset, after trigger os shutdown,
+    # the phosphor-state-manager will call force-warm-reset
+    # in this case the force_reset should wait for shutdown_ack from host
+    cnt=30
+    while [ $cnt -gt 0 ];
+    do
+      if [ -f "/run/openbmc/host@0-softpoweroff-shutdown-ack" ]; then
+        break
+      fi
+      echo "Waiting for shutdown-ack count down $cnt"
+      sleep 1
+      cnt=$((cnt - 1))
+    done
+    # The host OS is failed to shutdown
+    if [ $cnt == 0 ]; then
+      echo "Shutdown-ack time out after 30s."
+      exit 0
+    fi
+  fi
+  echo "Triggering sysreset pin"
+  gpioset -l $GPIO_CHIP0_IDX $S0_SYSRESET_L=1
+  sleep 1
+  gpioset -l $GPIO_CHIP0_IDX $S0_SYSRESET_L=0
+}
+
+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
+
+# check if power guard enabled
+dir="/run/systemd/system/"
+file="reboot-guard.conf"
+units=("reboot" "poweroff" "halt")
+for unit in "${units[@]}"; do
+  if [ -f ${dir}${unit}.target.d/${file} ]; then
+    echo "PowerGuard enabled, cannot do power control, exit!!!"
+    exit -1
+  fi
+done
+
+if [ ! -d "/run/openbmc/" ]; then
+  mkdir -p "/run/openbmc/"
+fi
+
+if [ $2 == "shutdown_ack" ]; then
+  shutdown_ack
+elif [ $2 == "status" ]; then
+  power_status
+elif [ $2 == "force_reset" ]; then
+  force_reset
+elif [ $2 == "soft_off" ]; then
+  ret=$(soft_off)
+  if [ $ret == 0 ]; then
+    echo "The host is already softoff"
+  else
+    echo "Failed to softoff the host"
+  fi
+  exit $ret;
+else
+  echo "Invalid parameter2=$2"
+  usage;
+fi
+
+exit 0;
diff --git a/meta-ampere/meta-jade/recipes-ampere/platform/mtjade-utils/gpio-defs.sh b/meta-ampere/meta-jade/recipes-ampere/platform/mtjade-utils/gpio-defs.sh
new file mode 100644
index 0000000..fce59d5
--- /dev/null
+++ b/meta-ampere/meta-jade/recipes-ampere/platform/mtjade-utils/gpio-defs.sh
@@ -0,0 +1,32 @@
+# Index of GPIO device in gpioget/gpioset
+GPIO_CHIP0_IDX=0
+GPIO_CHIP1_IDX=1
+
+# Base of GPIO chip in /sys/class/gpio
+GPIO_CHIP0_BASE=792
+GPIO_CHIP1_BASE=780
+
+### Power control configuration
+# Power control gpios
+S0_SHD_REQ_L=49
+S0_SHD_ACK_L=50
+S0_REBOOT_ACK_L=75
+S0_SYSRESET_L=91
+
+
+### Table 1: GPIO Assignments
+S0_CPU_FW_BOOT_OK=48
+CPU_BMC_OVERTEMP_L=51
+CPU_BMC_HIGHTEMP_L=72
+CPU_FAULT_ALERT=73
+S1_CPU_FW_BOOT_OK=202
+
+### Table 2: Alert and Additional Miscellaneous Signals
+S0_SCP_AUTH_FAILURE=74
+S1_SCP_AUTH_FAILURE=205
+BMC_OK=228
+SLAVE_PRESENT_L=230
+
+### Common GPIOs
+SYS_PSON_L=42
+BMC_READY=229