meta-facebook: minerva: Add initial GPIO setting

Summary:
To initial GPIO setting by following minerva hardware setting

Change-Id: If005ae6a352bc93db3255e43ecea5c7475ddc78e
Signed-off-by: Yang Chen <yang.chen@quantatw.com>
diff --git a/meta-facebook/meta-minerva/recipes-minerva/plat-svc/files/minerva-early-sys-init b/meta-facebook/meta-minerva/recipes-minerva/plat-svc/files/minerva-early-sys-init
new file mode 100644
index 0000000..9763fef
--- /dev/null
+++ b/meta-facebook/meta-minerva/recipes-minerva/plat-svc/files/minerva-early-sys-init
@@ -0,0 +1,94 @@
+#!/bin/bash -e
+# shellcheck source=meta-facebook/meta-minerva/recipes-minerva/plat-tool/files/minerva-common-functions
+source /usr/libexec/minerva-common-functions
+
+# set initial value for GPIO output pins
+set_gpio PWREN_MTIA_BLADE0_EN 0
+set_gpio PWREN_MTIA_BLADE1_EN 0
+set_gpio PWREN_MTIA_BLADE2_EN 0
+set_gpio PWREN_MTIA_BLADE3_EN 0
+set_gpio PWREN_MTIA_BLADE4_EN 0
+set_gpio PWREN_MTIA_BLADE5_EN 0
+set_gpio PWREN_MTIA_BLADE6_EN 0
+set_gpio PWREN_MTIA_BLADE7_EN 0
+set_gpio PWREN_MTIA_BLADE8_EN 0
+set_gpio PWREN_MTIA_BLADE9_EN 0
+set_gpio PWREN_MTIA_BLADE10_EN 0
+set_gpio PWREN_MTIA_BLADE11_EN 0
+set_gpio PWREN_MTIA_BLADE12_EN 0
+set_gpio PWREN_MTIA_BLADE13_EN 0
+set_gpio PWREN_MTIA_BLADE14_EN 0
+set_gpio PWREN_MTIA_BLADE15_EN 0
+
+set_gpio PWREN_NW_BLADE0_EN 0
+set_gpio PWREN_NW_BLADE1_EN 0
+set_gpio PWREN_NW_BLADE2_EN 0
+set_gpio PWREN_NW_BLADE3_EN 0
+set_gpio PWREN_NW_BLADE4_EN 0
+set_gpio PWREN_NW_BLADE5_EN 0
+
+set_gpio PWREN_MTIA_BLADE0_HSC_EN 0
+set_gpio PWREN_MTIA_BLADE1_HSC_EN 0
+set_gpio PWREN_MTIA_BLADE2_HSC_EN 0
+set_gpio PWREN_MTIA_BLADE3_HSC_EN 0 
+set_gpio PWREN_MTIA_BLADE4_HSC_EN 0
+set_gpio PWREN_MTIA_BLADE5_HSC_EN 0
+set_gpio PWREN_MTIA_BLADE6_HSC_EN 0
+set_gpio PWREN_MTIA_BLADE7_HSC_EN 0
+set_gpio PWREN_MTIA_BLADE8_HSC_EN 0
+set_gpio PWREN_MTIA_BLADE9_HSC_EN 0
+set_gpio PWREN_MTIA_BLADE10_HSC_EN 0
+set_gpio PWREN_MTIA_BLADE11_HSC_EN 0
+set_gpio PWREN_MTIA_BLADE12_HSC_EN 0
+set_gpio PWREN_MTIA_BLADE13_HSC_EN 0
+set_gpio PWREN_MTIA_BLADE14_HSC_EN 0
+set_gpio PWREN_MTIA_BLADE15_HSC_EN 0
+
+set_gpio PWREN_NW_BLADE0_HSC_EN 0
+set_gpio PWREN_NW_BLADE1_HSC_EN 0
+set_gpio PWREN_NW_BLADE2_HSC_EN 0
+set_gpio PWREN_NW_BLADE3_HSC_EN 0
+set_gpio PWREN_NW_BLADE4_HSC_EN 0
+set_gpio PWREN_NW_BLADE5_HSC_EN 0
+
+set_gpio PWREN_FCB_TOP_L_EN 0
+set_gpio PWREN_FCB_TOP_R_EN 0
+set_gpio PWREN_FCB_MIDDLE_L_EN 0
+set_gpio PWREN_FCB_MIDDLE_R_EN 0
+set_gpio PWREN_FCB_BOTTOM_L_EN 0
+set_gpio PWREN_FCB_BOTTOM_R_EN 0
+
+set_gpio FM_CMM_AC_CYCLE_N 1
+
+# if MGMT_SFP_PRSNT_N = 0 -> MGMT_SFP_TX_DIS = 0
+# if MGMT_SFP_PRSNT_N = 1 -> MGMT_SFP_TX_DIS = 1
+MGMT_SFP_PRSNT_N_VALUE=$(get_gpio MGMT_SFP_PRSNT_N)
+if [ "$MGMT_SFP_PRSNT_N_VALUE" = "0" ]; then
+    set_gpio MGMT_SFP_TX_DIS 0
+else
+    set_gpio MGMT_SFP_TX_DIS 1
+fi
+
+set_gpio RST_I2CRST_MTIA_BLADE0_1_N 1
+set_gpio RST_I2CRST_MTIA_BLADE2_3_N 1
+set_gpio RST_I2CRST_MTIA_BLADE4_5_N 1
+set_gpio RST_I2CRST_MTIA_BLADE6_7_N 1
+set_gpio RST_I2CRST_MTIA_BLADE8_9_N 1
+set_gpio RST_I2CRST_MTIA_BLADE10_11_N 1
+set_gpio RST_I2CRST_MTIA_BLADE12_13_N 1
+set_gpio RST_I2CRST_MTIA_BLADE14_15_N 1
+set_gpio RST_I2CRST_NW_BLADE0_1_2_N 1
+set_gpio RST_I2CRST_NW_BLADE3_4_5_N 1
+
+set_gpio RST_I2CRST_FCB_N 1
+set_gpio RST_I2CRST_FCB_B_L_N 1
+set_gpio RST_I2CRST_FCB_B_R_N 1
+set_gpio RST_I2CRST_FCB_M_L_N 1
+set_gpio RST_I2CRST_FCB_M_R_N 1
+set_gpio RST_I2CRST_FCB_T_L_N 1
+set_gpio RST_I2CRST_FCB_T_R_N 1
+
+set_gpio wFM_88E6393X_BIN_UPDATE_EN_N 1
+set_gpio WATER_VALVE_CLOSED_N 0
+set_gpio BMC_READY 1
+exit 0
diff --git a/meta-facebook/meta-minerva/recipes-minerva/plat-svc/files/minerva-sys-init.service b/meta-facebook/meta-minerva/recipes-minerva/plat-svc/files/minerva-sys-init.service
new file mode 100644
index 0000000..b909adf
--- /dev/null
+++ b/meta-facebook/meta-minerva/recipes-minerva/plat-svc/files/minerva-sys-init.service
@@ -0,0 +1,11 @@
+[Unit]
+Description=Minerva Early System Init
+Before=phosphor-pid-control.service
+
+[Service]
+Type=oneshot
+ExecStart=/usr/libexec/minerva-early-sys-init
+SyslogIdentifier=Minerva Early Init
+
+[Install]
+WantedBy=multi-user.target
diff --git a/meta-facebook/meta-minerva/recipes-minerva/plat-svc/plat-svc_0.1.bb b/meta-facebook/meta-minerva/recipes-minerva/plat-svc/plat-svc_0.1.bb
new file mode 100644
index 0000000..57611bb
--- /dev/null
+++ b/meta-facebook/meta-minerva/recipes-minerva/plat-svc/plat-svc_0.1.bb
@@ -0,0 +1,24 @@
+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"
+RDEPENDS:${PN} += "libgpiod-tools"
+RDEPENDS:${PN} += "minerva-common-functions"
+
+SRC_URI += " \
+    file://minerva-sys-init.service \
+    file://minerva-early-sys-init \
+    "
+
+SYSTEMD_PACKAGES = "${PN}"
+SYSTEMD_SERVICE:${PN}:append = " \
+    minerva-sys-init.service \
+    "
+
+do_install() {
+    install -d ${D}${libexecdir}
+    install -m 0755 ${WORKDIR}/minerva-early-sys-init ${D}${libexecdir}
+}
+
diff --git a/meta-facebook/meta-minerva/recipes-minerva/plat-tool/files/minerva-common-functions b/meta-facebook/meta-minerva/recipes-minerva/plat-tool/files/minerva-common-functions
new file mode 100644
index 0000000..81b90b1
--- /dev/null
+++ b/meta-facebook/meta-minerva/recipes-minerva/plat-tool/files/minerva-common-functions
@@ -0,0 +1,40 @@
+#!/bin/bash
+
+set_gpio()
+{
+    local NET_NAME=$1
+    local OUT_VAL=$2
+    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
+    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
+        echo "set_gpio: can not find gpio, $NET_NAME"
+        return 1
+    fi
+
+    echo -n "set_gpio: set $NET_NAME = $OUT_VAL"
+    if ! gpioset "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}"="$OUT_VAL"; then
+        echo " failed"
+        return 1
+    fi
+
+    echo " success"
+    return 0
+}
+
+get_gpio()
+{
+    local NET_NAME=$1
+    local RET_VAL
+
+    mapfile -t -d " " GPIO_INFO < <(gpiofind "${NET_NAME}")
+    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
+        logger -p user.info "get_gpio: can not find gpio, ${NET_NAME}"
+        return 1
+    fi
+    if ! RET_VAL=$(gpioget "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}") ; then
+        logger -p user.info "get_gpio: get ${NET_NAME} failed"
+        return 1
+    fi
+    logger -p user.info "get_gpio: get ${NET_NAME} = ${RET_VAL}"
+    echo "${RET_VAL}"
+    return 0
+}
diff --git a/meta-facebook/meta-minerva/recipes-minerva/plat-tool/minerva-common-functions_0.1.bb b/meta-facebook/meta-minerva/recipes-minerva/plat-tool/minerva-common-functions_0.1.bb
new file mode 100644
index 0000000..2f470be
--- /dev/null
+++ b/meta-facebook/meta-minerva/recipes-minerva/plat-tool/minerva-common-functions_0.1.bb
@@ -0,0 +1,13 @@
+LICENSE = "Apache-2.0"
+LIC_FILES_CHKSUM = "file://${COREBASE}/meta/files/common-licenses/Apache-2.0;md5=89aea4e17d99a7cacdbeed46a0096b10"
+
+RDEPENDS:${PN} += " bash libgpiod-tools"
+
+SRC_URI += " \
+    file://minerva-common-functions \
+    "
+
+do_install() {
+    install -d ${D}${libexecdir}
+    install -m 0755 ${WORKDIR}/minerva-common-functions ${D}${libexecdir}
+}
diff --git a/meta-facebook/meta-minerva/recipes-phosphor/images/fb-minerva-phosphor-image.inc b/meta-facebook/meta-minerva/recipes-phosphor/images/fb-minerva-phosphor-image.inc
index 9c652cf..6c9d5bc 100644
--- a/meta-facebook/meta-minerva/recipes-phosphor/images/fb-minerva-phosphor-image.inc
+++ b/meta-facebook/meta-minerva/recipes-phosphor/images/fb-minerva-phosphor-image.inc
@@ -4,3 +4,8 @@
 OBMC_IMAGE_EXTRA_INSTALL:append = " \
     phosphor-gpio-monitor \
 "
+
+# Temporarily add plat-svc for gpio initialization
+OBMC_IMAGE_EXTRA_INSTALL:append = " \
+    plat-svc \
+"