meta-hpe: rl300-g11 add power sequencing files

Change-Id: Iabda97c9719dc5f285e5b97c5072f9061dd31d81
Signed-off-by: Charles Kearney <charles.kearney@hpe.com>
diff --git a/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/gpios-manager.sh b/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/gpios-manager.sh
new file mode 100755
index 0000000..919624d
--- /dev/null
+++ b/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/gpios-manager.sh
@@ -0,0 +1,81 @@
+#!/bin/sh
+# File which is managing GPIOS when detected. First parameter is which GPIOs that switched
+if [ "$1" = "up" ]
+then
+    systemctl stop xyz.openbmc_project.ampere_host_error_monitor.service
+    rmmod smpro_hwmon smpro_errmon smpro_misc smpro_mfd
+    busctl set-property xyz.openbmc_project.Chassis.Gpios /xyz/openbmc_project/chassis/gpios xyz.openbmc_project.Chassis.Gpios PGood b true
+    smproStatus=$(lsmod | grep smpro-mfd)
+    if [ "$smproStatus" = "" ]
+    then
+        # sleep 15
+        # We need to wait for the SoC to be ready to communicate - We just received the PGOOD signal
+        # The status is reported through an interrupt that we can poll at 2e within the CPLD address space (0xd100_00e2)
+        max_retry=5
+        sleep_time=5
+        success=0
+        while [ "$max_retry" != "0" ]
+        do
+            waitForSoC=$(devmem 0xd10000e2 8)
+            isAvailable=$(( waitForSoC & 0x2 ))
+            if [ "r$isAvailable" = "r2" ]
+            then
+                max_retry=0
+                success=1
+            else
+                max_retry=$(( max_retry - 1))
+                sleep $sleep_time
+            fi
+        done
+        if [ "$success" = "1" ]
+        then
+            # Activate local UBM (front NVME drive)
+            devmem 0xd1000087 8 5
+            i2ctransfer -y 3 w4@0x40 0x34 0xbf 0x00 0xe8
+            i2ctransfer -y 3 w2@0x40 0x34 0xa7 r1
+
+            devmem 0xd1000087 8 7
+            i2ctransfer -y 3 w4@0x40 0x34 0xbf 0x00 0xe8
+            i2ctransfer -y 3 w2@0x40 0x34 0xa7 r1
+
+            devmem 0xd1000087 8 9
+            i2ctransfer -y 3 w4@0x40 0x34 0xbf 0x00 0xe8
+            i2ctransfer -y 3 w2@0x40 0x34 0xa7 r1
+
+            devmem 0xd1000087 8 0xb
+            i2ctransfer -y 3 w4@0x40 0x34 0xbf 0x00 0xe8
+            i2ctransfer -y 3 w2@0x40 0x34 0xa7 r1
+
+            devmem 0xd1000088 8 5
+            i2ctransfer -y 4 w4@0x40 0x34 0xbf 0x00 0xe8
+            i2ctransfer -y 4 w2@0x40 0x34 0xa7 r1
+
+            modprobe smpro-misc
+            modprobe smpro-errmon
+            modprobe smpro-hwmon
+            modprobe smpro-mfd
+            systemctl restart xyz.openbmc_project.ampere_host_error_monitor.service
+            systemctl restart xyz.openbmc_project.amperecpusensor.service
+            systemctl restart xyz.openbmc_project.EntityManager.service
+            systemctl restart phosphor-pid-control.service
+        fi
+        # We shall add a condition here to make an emergency stop
+    fi
+else
+    if [ "$1" = "down" ]
+    then
+        busctl set-property xyz.openbmc_project.Chassis.Gpios /xyz/openbmc_project/chassis/gpios xyz.openbmc_project.Chassis.Gpios PGood b false
+        # We can re-init the system
+        devmem 0xd1000009 8 0x24
+        # We need to check the reason why we have been shutdown
+        shutdownReason=$(devmem 0x80000074 16)
+        # If bit 10 is set Software initiated a shutdown we need to restart
+        isSet2=$( ${shutdownReason} | 0xFBFF )
+        if [ "$isSet2" = "65535" ]
+        then
+            echo "Restarting" >> /tmp/gpios
+            obmcutil poweron
+        fi
+    fi
+fi
+echo "$1" >> /tmp/gpios.txt
diff --git a/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/gpiosdown.service b/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/gpiosdown.service
new file mode 100644
index 0000000..a2416b6
--- /dev/null
+++ b/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/gpiosdown.service
@@ -0,0 +1,8 @@
+[Unit]
+Description=HPE RL300 PowerOK
+
+[Service]
+ExecStart=/usr/bin/gpios-manager down
+
+[Install]
+# WantedBy=multi-user.target
diff --git a/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/gpiosup.service b/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/gpiosup.service
new file mode 100644
index 0000000..7aed15b
--- /dev/null
+++ b/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/gpiosup.service
@@ -0,0 +1,8 @@
+[Unit]
+Description=HPE RL300 PowerOK
+
+[Service]
+ExecStart=/usr/bin/gpios-manager up
+
+[Install]
+# WantedBy=multi-user.target
diff --git a/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/ipmi-power-button-monitoring.sh b/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/ipmi-power-button-monitoring.sh
new file mode 100644
index 0000000..c38c087
--- /dev/null
+++ b/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/ipmi-power-button-monitoring.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+dbus-monitor --system --profile "type='method_call',path='/xyz/openbmc_project/state/host0',interface='org.freedesktop.DBus.Properties',member='Set'" | awk '/xyz.openbmc_project.State.Host/ { print "DONE" }' | while read -r ; do
+    /usr/bin/rl300Start.sh
+done
diff --git a/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/monitor-ipmi-power-transition.service b/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/monitor-ipmi-power-transition.service
new file mode 100644
index 0000000..5f4340a
--- /dev/null
+++ b/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/monitor-ipmi-power-transition.service
@@ -0,0 +1,11 @@
+[Unit]
+Description=RL300 IPMI power sequencing request
+After=dbus.service
+
+[Service]
+Restart=always
+RestartSec=5
+ExecStart=/usr/bin/ipmi-power-button-monitoring.sh
+
+[Install]
+WantedBy=multi-user.target
diff --git a/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/power-button.sh b/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/power-button.sh
new file mode 100755
index 0000000..6b1b36e
--- /dev/null
+++ b/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/power-button.sh
@@ -0,0 +1,12 @@
+#!/bin/sh
+# File which is managing GPIOS when detected. First parameter is which GPIOs that switched
+if [ "$1" = "pressed" ]
+then
+    busctl set-property xyz.openbmc_project.Chassis.Gpios /xyz/openbmc_project/chassis/gpios xyz.openbmc_project.Chassis.Gpios PButton b true
+else
+    if [ "$1" = "released" ]
+    then
+        busctl set-property xyz.openbmc_project.Chassis.Gpios /xyz/openbmc_project/chassis/gpios xyz.openbmc_project.Chassis.Gpios PButton b false
+    fi
+fi
+echo "$1" >> /tmp/buttons.txt
diff --git a/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/power-sequencing.service b/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/power-sequencing.service
new file mode 100644
index 0000000..ff8db69
--- /dev/null
+++ b/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/power-sequencing.service
@@ -0,0 +1,12 @@
+[Unit]
+Description=RL300 power sequencing manager
+#After=dbus.service
+After=xyz.openbmc_project.Chassis.Control.Power@0.service
+
+[Service]
+Restart=always
+RestartSec=5
+ExecStart=/usr/bin/startMonitoring.sh
+
+[Install]
+WantedBy=multi-user.target
diff --git a/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/powerbuttonpressed.service b/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/powerbuttonpressed.service
new file mode 100644
index 0000000..f3d28ea
--- /dev/null
+++ b/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/powerbuttonpressed.service
@@ -0,0 +1,8 @@
+[Unit]
+Description=HPE RL300 Power Button
+
+[Service]
+ExecStart=/usr/bin/power-button pressed
+
+[Install]
+# WantedBy=multi-user.target
diff --git a/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/powerbuttonreleased.service b/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/powerbuttonreleased.service
new file mode 100644
index 0000000..65e1b40
--- /dev/null
+++ b/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/powerbuttonreleased.service
@@ -0,0 +1,8 @@
+[Unit]
+Description=HPE RL300 Power Button Release
+
+[Service]
+ExecStart=/usr/bin/power-button released
+
+[Install]
+# WantedBy=multi-user.target
diff --git a/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/rl300Start.sh b/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/rl300Start.sh
new file mode 100644
index 0000000..b031ec9
--- /dev/null
+++ b/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/rl300Start.sh
@@ -0,0 +1,16 @@
+#!/bin/sh
+# A power up request has been made we must setup the system properly
+# RL300 requires UEFI VAR ROM export through spi controller 1
+# the machine needs to get out of Hold too
+
+rmmod gxp_spifi_ctrl1
+# vejmarie was 58
+devmem 0xd1000119 32 0x5d
+modprobe gxp_spifi_ctrl1
+
+# clear previous reset reason
+devmem 0x80000074 16 0x0
+# Release the Soc
+currentVal=$(devmem 0xD100011A 8)
+currentVal=$(( currentVal | 1 << 3 ))
+devmem 0xD100011A 8 "0x""${currentVal}"
diff --git a/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/startMonitoring.sh b/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/startMonitoring.sh
new file mode 100644
index 0000000..486591b
--- /dev/null
+++ b/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/startMonitoring.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+# We just started we have to update the Gpios status
+/usr/bin/phosphor-multi-gpio-monitor --config /usr/share/gpios/test.json
diff --git a/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/test.json b/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/test.json
new file mode 100644
index 0000000..3541d62
--- /dev/null
+++ b/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/files/test.json
@@ -0,0 +1,10 @@
+[
+  {
+    "Name": "PowerGoodUp",
+    "LineName": "PS_PWROK",
+    "EventMon": "BOTH",
+    "TargetRising": "gpiosup.service",
+    "TargetFalling": "gpiosdown.service",
+    "Continue": true
+  }
+]
diff --git a/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/power-sequencing.bb b/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/power-sequencing.bb
new file mode 100644
index 0000000..efe4aaa
--- /dev/null
+++ b/meta-hpe/meta-rl300-g11/recipes-hpe/power-sequencing/power-sequencing.bb
@@ -0,0 +1,31 @@
+inherit obmc-phosphor-systemd
+SUMMARY = "Power Sequencing for HPE RL300 Gen11"
+SYSTEMD_AUTO_ENABLE = "enable"
+SYSTEMD_SERVICE:${PN} = "monitor-ipmi-power-transition.service power-sequencing.service gpiosdown.service gpiosup.service powerbuttonpressed.service powerbuttonreleased.service"
+SRC_URI:append = " file://monitor-ipmi-power-transition.service file://ipmi-power-button-monitoring.sh file://rl300Start.sh file://power-sequencing.service file://gpiosdown.service file://gpiosup.service file://gpios-manager.sh file://test.json file://powerbuttonreleased.service file://powerbuttonpressed.service file://power-button.sh file://startMonitoring.sh"
+
+LICENSE = "Apache-2.0"
+LIC_FILES_CHKSUM = "file://${WORKDIR}/LICENSE;md5=89aea4e17d99a7cacdbeed46a0096b10"
+
+FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"
+IMAGE_INSTALL += "power-sequencing "
+
+FILES:${PN} = "/usr/share/gpios/* /usr/bin/* /etc/system/*"
+
+do_install:append() {
+  install -d ${D}${sysconfdir}/system/
+  install -d ${D}/usr/share/gpios
+  install -d ${D}/usr/bin
+  install -m 0644 ${WORKDIR}/test.json ${D}/usr/share/gpios/test.json
+  install -m 0644 ${WORKDIR}/power-sequencing.service ${D}${sysconfdir}/system/power-sequencing.service
+  install -m 0644 ${WORKDIR}/gpiosup.service ${D}${sysconfdir}/system/gpiosup.service
+  install -m 0644 ${WORKDIR}/gpiosdown.service ${D}${sysconfdir}/system/gpiosdown.service
+  install -m 0644 ${WORKDIR}/powerbuttonpressed.service ${D}${sysconfdir}/system/powerbuttonpressed.service
+  install -m 0644 ${WORKDIR}/powerbuttonreleased.service ${D}${sysconfdir}/system/powerbuttonreleased.service
+  install -m 0644 ${WORKDIR}/monitor-ipmi-power-transition.service ${D}${sysconfdir}/system/monitor-ipmi-power-transition.service
+  install -m 0755 ${WORKDIR}/gpios-manager.sh ${D}/usr/bin/gpios-manager.sh
+  install -m 0755 ${WORKDIR}/startMonitoring.sh ${D}/usr/bin/startMonitoring.sh
+  install -m 0755 ${WORKDIR}/rl300Start.sh ${D}/usr/bin/rl300Start.sh
+  install -m 0755 ${WORKDIR}/ipmi-power-button-monitoring.sh ${D}/usr/bin/ipmi-power-button-monitoring.sh
+  install -m 0755 ${WORKDIR}/power-button.sh ${D}/usr/bin/power-button.sh
+}