13f1e6370SPotin Lai#!/bin/bash
23f1e6370SPotin Lai# shellcheck source=meta-facebook/recipes-fb/obmc_functions/files/fb-common-functions
33f1e6370SPotin Laisource /usr/libexec/fb-common-functions
43f1e6370SPotin Lai
53f1e6370SPotin LaiGPIO_HIGH=1
63f1e6370SPotin LaiGPIO_LOW=0
73f1e6370SPotin Lai
83f1e6370SPotin Laiwait_gpio_value()
93f1e6370SPotin Lai{
103f1e6370SPotin Lai    local net_name=$1
113f1e6370SPotin Lai    local max_retries=$3
123f1e6370SPotin Lai    local delay_secs=$4
133f1e6370SPotin Lai    local expd_val=$2
143f1e6370SPotin Lai
153f1e6370SPotin Lai    local gpio_val=0
163f1e6370SPotin Lai    local trycnt=1
173f1e6370SPotin Lai
183f1e6370SPotin Lai    until [[ $gpio_val -gt 0 || $trycnt -gt $max_retries ]]
193f1e6370SPotin Lai    do
203f1e6370SPotin Lai        gpio_val=$(get_gpio "$net_name")
213f1e6370SPotin Lai        rc=$?
223f1e6370SPotin Lai        if [[ $rc -ne 0 ]]; then
233f1e6370SPotin Lai            err_msg="Unable to read $net_name"
243f1e6370SPotin Lai            echo "$err_msg"
253f1e6370SPotin Lai            return 1
263f1e6370SPotin Lai        fi
273f1e6370SPotin Lai        if [[ $gpio_val -eq $expd_val ]]; then
283f1e6370SPotin Lai            return 0
293f1e6370SPotin Lai        fi
303f1e6370SPotin Lai        sleep "$delay_secs"
313f1e6370SPotin Lai        ((trycnt++))
323f1e6370SPotin Lai    done
333f1e6370SPotin Lai
343f1e6370SPotin Lai    err_msg="wait_gpio_value failed, gpio_val=$gpio_val,  exp_val=$expd_val"
353f1e6370SPotin Lai    echo "$err_msg"
363f1e6370SPotin Lai    return 1
373f1e6370SPotin Lai}
383f1e6370SPotin Lai
393f1e6370SPotin Laibind_i2c_muxes()
403f1e6370SPotin Lai{
413f1e6370SPotin Lai    # Module 0 I2C Mux
423f1e6370SPotin Lai    # i2c24 - i2c27
43279a0ee3SPotin Lai    bind_i2c_device pca954x 0-0071
443f1e6370SPotin Lai    # i2c28 - i2c31
45279a0ee3SPotin Lai    bind_i2c_device pca954x 0-0072
463f1e6370SPotin Lai    # i2c32 - i2c35
47279a0ee3SPotin Lai    bind_i2c_device pca954x 0-0073
483f1e6370SPotin Lai
493f1e6370SPotin Lai    # Module 1 I2C Mux
503f1e6370SPotin Lai    # i2c36 - i2c39
51279a0ee3SPotin Lai    bind_i2c_device pca954x 0-0075
523f1e6370SPotin Lai    # i2c40 - i2c43
53279a0ee3SPotin Lai    bind_i2c_device pca954x 0-0076
543f1e6370SPotin Lai    # i2c44 - i2c47
55279a0ee3SPotin Lai    bind_i2c_device pca954x 0-0077
563f1e6370SPotin Lai
573f1e6370SPotin Lai    # HDD Board I2C Mux, i2c48 - i2c55
58279a0ee3SPotin Lai    bind_i2c_device pca954x 30-0070
593f1e6370SPotin Lai}
603f1e6370SPotin Lai
613f1e6370SPotin Laibind_gpio_expanders()
623f1e6370SPotin Lai{
633f1e6370SPotin Lai    # Module 0 IOEXP
64279a0ee3SPotin Lai    bind_i2c_device pca953x 2-0020
653f1e6370SPotin Lai    # Module 1 IOEXP
66279a0ee3SPotin Lai    bind_i2c_device pca953x 2-0021
673f1e6370SPotin Lai    # HMC IOEXP
68279a0ee3SPotin Lai    bind_i2c_device pca953x 2-0027
693f1e6370SPotin Lai    # BMC IOEXP
70279a0ee3SPotin Lai    bind_i2c_device pca953x 6-0021
713f1e6370SPotin Lai    # IO Mezz 0 IOEXP
72279a0ee3SPotin Lai    bind_i2c_device pca953x 29-0020
733f1e6370SPotin Lai    # IO Mezz 1 IOEXP
74279a0ee3SPotin Lai    bind_i2c_device pca953x 41-0021
753f1e6370SPotin Lai}
763f1e6370SPotin Lai
773f1e6370SPotin Laibind_fru_eeproms()
783f1e6370SPotin Lai{
793f1e6370SPotin Lai    # Module 0 FRU
80279a0ee3SPotin Lai    bind_i2c_device at24 13-0050
813f1e6370SPotin Lai    # Module 1 FRU
82279a0ee3SPotin Lai    bind_i2c_device at24 12-0050
833f1e6370SPotin Lai    # HMC FRU
84279a0ee3SPotin Lai    bind_i2c_device at24 13-0057
853f1e6370SPotin Lai    # Left CBC FRU
86279a0ee3SPotin Lai    bind_i2c_device at24 13-0054
873f1e6370SPotin Lai    # Right CBC FRU
88279a0ee3SPotin Lai    bind_i2c_device at24 13-0055
893f1e6370SPotin Lai    # IO Mezz 0 FRU
90279a0ee3SPotin Lai    bind_i2c_device at24 29-0050
913f1e6370SPotin Lai    # IO Mezz 1 FRU
92279a0ee3SPotin Lai    bind_i2c_device at24 41-0050
934ce60f98SPotin Lai    # HDD Board FRU
94279a0ee3SPotin Lai    bind_i2c_device at24 54-0052
953f1e6370SPotin Lai}
963f1e6370SPotin Lai
973f1e6370SPotin Laireset_host_usb()
983f1e6370SPotin Lai{
99*cfe75236SPotin Lai    set_gpio "USB2_HUB_RESET_L" "$GPIO_LOW"
1003f1e6370SPotin Lai    sleep 1
101*cfe75236SPotin Lai    set_gpio "USB2_HUB_RESET_L" "$GPIO_HIGH"
1023f1e6370SPotin Lai}
1033f1e6370SPotin Lai
1043f1e6370SPotin Lairebind_hmc_usb_network()
1053f1e6370SPotin Lai{
1063f1e6370SPotin Lai    echo 1e6a1000.usb > /sys/bus/platform/drivers/ehci-platform/unbind
1073f1e6370SPotin Lai    echo 1e6a1000.usb > /sys/bus/platform/drivers/ehci-platform/bind
1083f1e6370SPotin Lai}
1093f1e6370SPotin Lai
1103f1e6370SPotin Laiis_stby_good()
1113f1e6370SPotin Lai{
1123f1e6370SPotin Lai    local gpio_val
1133f1e6370SPotin Lai
1143f1e6370SPotin Lai    if ! gpio_val=$(get_gpio "STBY_POWER_PG_3V3"); then
1153f1e6370SPotin Lai        return 1
1163f1e6370SPotin Lai    fi
1173f1e6370SPotin Lai
1183f1e6370SPotin Lai    if [[ $gpio_val -eq 0 ]]; then
1193f1e6370SPotin Lai        return 1
1203f1e6370SPotin Lai    fi
1213f1e6370SPotin Lai
1223f1e6370SPotin Lai    return 0
1233f1e6370SPotin Lai}
1243f1e6370SPotin Lai
1253f1e6370SPotin Laiset_bmc_ready()
1263f1e6370SPotin Lai{
1273f1e6370SPotin Lai    local bmc_ready="/sys/class/leds/bmc_ready_noled/brightness"
1283f1e6370SPotin Lai    local bmc_ready_cpld="/sys/class/leds/bmc_ready_cpld_noled/brightness"
1293f1e6370SPotin Lai
1303f1e6370SPotin Lai    echo 1 > ${bmc_ready}
1313f1e6370SPotin Lai    echo 1 > ${bmc_ready_cpld}
1323f1e6370SPotin Lai    return 0
1333f1e6370SPotin Lai}
1343f1e6370SPotin Lai
1353f1e6370SPotin Laiif ! is_stby_good; then
1363f1e6370SPotin Lai    set_gpio "SCM_HPM_STBY_RST_N" "$GPIO_LOW"
1373f1e6370SPotin Laifi
1383f1e6370SPotin Lai
1393f1e6370SPotin Laiset_gpio "SCM_HPM_STBY_EN" "$GPIO_HIGH"
1403f1e6370SPotin Laiset_gpio "stby_power_en_cpld" "$GPIO_HIGH"
1413f1e6370SPotin Laiif ! wait_gpio_value "STBY_POWER_PG_3V3" "$GPIO_HIGH" 20 1; then
1423f1e6370SPotin Lai    echo "Error: failed to get STBY_POWER_PG_3V3 as high (1) in 20 sec"
1433f1e6370SPotin Lai    exit 1
1443f1e6370SPotin Laifi
1453f1e6370SPotin Laiset_gpio "stby_power_gd_cpld" "$GPIO_HIGH"
1463f1e6370SPotin Lai
1473f1e6370SPotin Laibind_i2c_muxes
1483f1e6370SPotin Laibind_gpio_expanders
1493f1e6370SPotin Lai
1503f1e6370SPotin Laireset_host_usb
1513f1e6370SPotin Lai
1523f1e6370SPotin Laiset_gpio "HMC_PGOOD" "$GPIO_HIGH"
1533f1e6370SPotin Laiset_gpio "EROT_FPGA_RST_L" "$GPIO_HIGH"
1543f1e6370SPotin Laiset_gpio "SEC_EROT_FPGA_RST_L" "$GPIO_HIGH"
1553f1e6370SPotin Laiset_gpio "HMC_EROT_RST_L" "$GPIO_HIGH"
1563f1e6370SPotin Laiset_gpio "SCM_HPM_STBY_RST_N" "$GPIO_HIGH"
1573f1e6370SPotin Lai
1583f1e6370SPotin Laiif ! wait_gpio_value "HMC_READY" "$GPIO_HIGH" 180 1; then
1593f1e6370SPotin Lai    echo "Error: failed to get HMC_READY as high (1) in 180 sec"
1603f1e6370SPotin Lai    exit 1
1613f1e6370SPotin Laifi
1623f1e6370SPotin Lai
1633f1e6370SPotin Lairebind_hmc_usb_network
1643f1e6370SPotin Lai
1653f1e6370SPotin Laiif ! wait_gpio_value "FPGA_READY_BMC" "$GPIO_HIGH" 180 1; then
1663f1e6370SPotin Lai    echo "Error: failed to get FPGA_READY_BMC as high (1) in 180 sec"
1673f1e6370SPotin Lai    exit 1
1683f1e6370SPotin Laifi
1693f1e6370SPotin Lai
1703f1e6370SPotin Laibind_fru_eeproms
1713f1e6370SPotin Laiset_bmc_ready
1723f1e6370SPotin Lai
1733f1e6370SPotin Laiexit 0
174