meta-facebook: catalina: add catalina early init service
Add catalina early init service for turn on module standby power
at early stage.
Change-Id: Id19d1f147cb65790c49ffebed996cce8e205ce8e
Signed-off-by: Potin Lai <potin.lai@quantatw.com>
diff --git a/meta-facebook/meta-catalina/recipes-catalina/plat-svc/files/catalina-early-sys-init b/meta-facebook/meta-catalina/recipes-catalina/plat-svc/files/catalina-early-sys-init
new file mode 100644
index 0000000..2037f89
--- /dev/null
+++ b/meta-facebook/meta-catalina/recipes-catalina/plat-svc/files/catalina-early-sys-init
@@ -0,0 +1,22 @@
+#!/bin/bash -e
+# shellcheck source=meta-facebook/recipes-fb/obmc_functions/files/fb-common-functions
+source /usr/libexec/fb-common-functions
+
+# Enable standby power and bind i2c components
+/usr/libexec/catalina/standby-power-enable
+
+# Workaround
+# rebind PDB i2c mux
+echo 1-0070 > /sys/bus/i2c/drivers/pca954x/bind
+# rebind PDB Fan IC
+echo 18-0021 > /sys/bus/i2c/drivers/max31790/bind
+echo 18-0027 > /sys/bus/i2c/drivers/max31790/bind
+# rebind PDB FRU
+echo 21-0054 > /sys/bus/i2c/drivers/at24/bind
+# rebind OSFP FRU
+echo 22-0051 > /sys/bus/i2c/drivers/at24/bind
+# rebind FIO FRU
+echo 23-0053 > /sys/bus/i2c/drivers/at24/bind
+
+
+exit 0
diff --git a/meta-facebook/meta-catalina/recipes-catalina/plat-svc/files/catalina-sys-init.service b/meta-facebook/meta-catalina/recipes-catalina/plat-svc/files/catalina-sys-init.service
new file mode 100644
index 0000000..4000491
--- /dev/null
+++ b/meta-facebook/meta-catalina/recipes-catalina/plat-svc/files/catalina-sys-init.service
@@ -0,0 +1,16 @@
+[Unit]
+Description=Catalina Early System Init
+Before=phosphor-multi-gpio-monitor.service
+Before=phosphor-multi-gpio-presence.service
+Before=phosphor-pid-control.service
+Before=xyz.openbmc_project.EntityManager.service
+Before=xyz.openbmc_project.fansensor.service
+Before=systemd-networkd.service
+
+[Service]
+Type=oneshot
+ExecStart=/usr/libexec/catalina/catalina-early-sys-init
+SyslogIdentifier=Catalina Early Init
+
+[Install]
+WantedBy=multi-user.target
diff --git a/meta-facebook/meta-catalina/recipes-catalina/plat-svc/files/standby-power-enable b/meta-facebook/meta-catalina/recipes-catalina/plat-svc/files/standby-power-enable
new file mode 100644
index 0000000..3998a17
--- /dev/null
+++ b/meta-facebook/meta-catalina/recipes-catalina/plat-svc/files/standby-power-enable
@@ -0,0 +1,173 @@
+#!/bin/bash
+# shellcheck source=meta-facebook/recipes-fb/obmc_functions/files/fb-common-functions
+source /usr/libexec/fb-common-functions
+
+GPIO_HIGH=1
+GPIO_LOW=0
+
+wait_gpio_value()
+{
+ local net_name=$1
+ local max_retries=$3
+ local delay_secs=$4
+ local expd_val=$2
+
+ local gpio_val=0
+ local trycnt=1
+
+ until [[ $gpio_val -gt 0 || $trycnt -gt $max_retries ]]
+ do
+ gpio_val=$(get_gpio "$net_name")
+ rc=$?
+ if [[ $rc -ne 0 ]]; then
+ err_msg="Unable to read $net_name"
+ echo "$err_msg"
+ return 1
+ fi
+ if [[ $gpio_val -eq $expd_val ]]; then
+ return 0
+ fi
+ sleep "$delay_secs"
+ ((trycnt++))
+ done
+
+ err_msg="wait_gpio_value failed, gpio_val=$gpio_val, exp_val=$expd_val"
+ echo "$err_msg"
+ return 1
+}
+
+bind_i2c_muxes()
+{
+ # Module 0 I2C Mux
+ # i2c24 - i2c27
+ echo "0-0071" > /sys/bus/i2c/drivers/pca954x/bind
+ # i2c28 - i2c31
+ echo "0-0072" > /sys/bus/i2c/drivers/pca954x/bind
+ # i2c32 - i2c35
+ echo "0-0073" > /sys/bus/i2c/drivers/pca954x/bind
+
+ # Module 1 I2C Mux
+ # i2c36 - i2c39
+ echo "0-0075" > /sys/bus/i2c/drivers/pca954x/bind
+ # i2c40 - i2c43
+ echo "0-0076" > /sys/bus/i2c/drivers/pca954x/bind
+ # i2c44 - i2c47
+ echo "0-0077" > /sys/bus/i2c/drivers/pca954x/bind
+
+ # HDD Board I2C Mux, i2c48 - i2c55
+ echo "33-0070" > /sys/bus/i2c/drivers/pca954x/bind
+
+}
+
+bind_gpio_expanders()
+{
+ # Module 0 IOEXP
+ echo "2-0020" > /sys/bus/i2c/drivers/pca953x/bind
+ # Module 1 IOEXP
+ echo "2-0021" > /sys/bus/i2c/drivers/pca953x/bind
+ # HMC IOEXP
+ echo "2-0027" > /sys/bus/i2c/drivers/pca953x/bind
+ # BMC IOEXP
+ echo "6-0021" > /sys/bus/i2c/drivers/pca953x/bind
+ # IO Mezz 0 IOEXP
+ echo "29-0020" > /sys/bus/i2c/drivers/pca953x/bind
+ # IO Mezz 1 IOEXP
+ echo "41-0021" > /sys/bus/i2c/drivers/pca953x/bind
+}
+
+bind_fru_eeproms()
+{
+ # Module 0 FRU
+ echo "13-0050" /sys/bus/i2c/drivers/at24/bind
+ # Module 1 FRU
+ echo "12-0050" /sys/bus/i2c/drivers/at24/bind
+ # HMC FRU
+ echo "13-0057" /sys/bus/i2c/drivers/at24/bind
+ # Left CBC FRU
+ echo "13-0054" /sys/bus/i2c/drivers/at24/bind
+ # Right CBC FRU
+ echo "13-0055" /sys/bus/i2c/drivers/at24/bind
+ # IO Mezz 0 FRU
+ echo "29-0050" /sys/bus/i2c/drivers/at24/bind
+ # IO Mezz 1 FRU
+ echo "41-0050" /sys/bus/i2c/drivers/at24/bind
+}
+
+reset_host_usb()
+{
+ set_gpio "USB_HUB_RESET_L" "$GPIO_LOW"
+ sleep 1
+ set_gpio "USB_HUB_RESET_L" "$GPIO_HIGH"
+}
+
+rebind_hmc_usb_network()
+{
+ echo 1e6a1000.usb > /sys/bus/platform/drivers/ehci-platform/unbind
+ echo 1e6a1000.usb > /sys/bus/platform/drivers/ehci-platform/bind
+}
+
+is_stby_good()
+{
+ local gpio_val
+
+ if ! gpio_val=$(get_gpio "STBY_POWER_PG_3V3"); then
+ return 1
+ fi
+
+ if [[ $gpio_val -eq 0 ]]; then
+ return 1
+ fi
+
+ return 0
+}
+
+set_bmc_ready()
+{
+ local bmc_ready="/sys/class/leds/bmc_ready_noled/brightness"
+ local bmc_ready_cpld="/sys/class/leds/bmc_ready_cpld_noled/brightness"
+
+ echo 1 > ${bmc_ready}
+ echo 1 > ${bmc_ready_cpld}
+ return 0
+}
+
+if ! is_stby_good; then
+ set_gpio "SCM_HPM_STBY_RST_N" "$GPIO_LOW"
+fi
+
+set_gpio "SCM_HPM_STBY_EN" "$GPIO_HIGH"
+set_gpio "stby_power_en_cpld" "$GPIO_HIGH"
+if ! wait_gpio_value "STBY_POWER_PG_3V3" "$GPIO_HIGH" 20 1; then
+ echo "Error: failed to get STBY_POWER_PG_3V3 as high (1) in 20 sec"
+ exit 1
+fi
+set_gpio "stby_power_gd_cpld" "$GPIO_HIGH"
+
+bind_i2c_muxes
+bind_gpio_expanders
+
+set_gpio "BMC_12V_CTRL" "$GPIO_HIGH"
+reset_host_usb
+
+set_gpio "HMC_PGOOD" "$GPIO_HIGH"
+set_gpio "EROT_FPGA_RST_L" "$GPIO_HIGH"
+set_gpio "SEC_EROT_FPGA_RST_L" "$GPIO_HIGH"
+set_gpio "HMC_EROT_RST_L" "$GPIO_HIGH"
+set_gpio "SCM_HPM_STBY_RST_N" "$GPIO_HIGH"
+
+if ! wait_gpio_value "HMC_READY" "$GPIO_HIGH" 180 1; then
+ echo "Error: failed to get HMC_READY as high (1) in 180 sec"
+ exit 1
+fi
+
+rebind_hmc_usb_network
+
+if ! wait_gpio_value "FPGA_READY_BMC" "$GPIO_HIGH" 180 1; then
+ echo "Error: failed to get FPGA_READY_BMC as high (1) in 180 sec"
+ exit 1
+fi
+
+bind_fru_eeproms
+set_bmc_ready
+
+exit 0
diff --git a/meta-facebook/meta-catalina/recipes-catalina/plat-svc/plat-svc_0.1.bb b/meta-facebook/meta-catalina/recipes-catalina/plat-svc/plat-svc_0.1.bb
new file mode 100644
index 0000000..22fd0a6
--- /dev/null
+++ b/meta-facebook/meta-catalina/recipes-catalina/plat-svc/plat-svc_0.1.bb
@@ -0,0 +1,26 @@
+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} += "fb-common-functions"
+
+SRC_URI += " \
+ file://catalina-sys-init.service \
+ file://catalina-early-sys-init \
+ file://standby-power-enable \
+ "
+
+SYSTEMD_PACKAGES = "${PN}"
+SYSTEMD_SERVICE:${PN}:append = " \
+ catalina-sys-init.service \
+ "
+
+do_install() {
+ CATALINA_LIBEXECDIR="${D}${libexecdir}/catalina"
+ install -d ${CATALINA_LIBEXECDIR}
+ install -m 0755 ${WORKDIR}/catalina-early-sys-init ${CATALINA_LIBEXECDIR}
+ install -m 0755 ${WORKDIR}/standby-power-enable ${CATALINA_LIBEXECDIR}
+}
diff --git a/meta-facebook/meta-catalina/recipes-phosphor/images/fb-catalina-phosphor-image.inc b/meta-facebook/meta-catalina/recipes-phosphor/images/fb-catalina-phosphor-image.inc
index ad6a4e4..5b98f30 100644
--- a/meta-facebook/meta-catalina/recipes-phosphor/images/fb-catalina-phosphor-image.inc
+++ b/meta-facebook/meta-catalina/recipes-phosphor/images/fb-catalina-phosphor-image.inc
@@ -10,3 +10,8 @@
OBMC_IMAGE_EXTRA_INSTALL:append = " \
phosphor-gpio-monitor-presence \
"
+
+# Catalina platform services
+OBMC_IMAGE_EXTRA_INSTALL:append = " \
+ plat-svc \
+"