xref: /openbmc/openbmc/meta-facebook/meta-catalina/recipes-catalina/plat-svc/files/standby-power-enable (revision 2086ffabc614530a3b963dc94bbd8d8a39b8cc01)
1#!/bin/bash
2# shellcheck source=meta-facebook/recipes-fb/obmc_functions/files/fb-common-functions
3source /usr/libexec/fb-common-functions
4
5GPIO_HIGH=1
6GPIO_LOW=0
7
8wait_gpio_value()
9{
10    local net_name=$1
11    local max_retries=$3
12    local delay_secs=$4
13    local expd_val=$2
14
15    local gpio_val=0
16    local trycnt=1
17
18    until [[ $gpio_val -gt 0 || $trycnt -gt $max_retries ]]
19    do
20        gpio_val=$(get_gpio "$net_name")
21        rc=$?
22        if [[ $rc -ne 0 ]]; then
23            err_msg="Unable to read $net_name"
24            echo "$err_msg"
25            return 1
26        fi
27        if [[ $gpio_val -eq $expd_val ]]; then
28            return 0
29        fi
30        sleep "$delay_secs"
31        ((trycnt++))
32    done
33
34    err_msg="wait_gpio_value failed, gpio_val=$gpio_val,  exp_val=$expd_val"
35    echo "$err_msg"
36    return 1
37}
38
39bind_i2c_muxes()
40{
41    # Module 0 I2C Mux
42    # i2c24 - i2c27
43    bind_i2c_device pca954x 0-0071
44    # i2c28 - i2c31
45    bind_i2c_device pca954x 0-0072
46    # i2c32 - i2c35
47    bind_i2c_device pca954x 0-0073
48
49    # Module 1 I2C Mux
50    # i2c36 - i2c39
51    bind_i2c_device pca954x 0-0075
52    # i2c40 - i2c43
53    bind_i2c_device pca954x 0-0076
54    # i2c44 - i2c47
55    bind_i2c_device pca954x 0-0077
56
57    # HDD Board I2C Mux, i2c48 - i2c55
58    bind_i2c_device pca954x 5-0070
59}
60
61bind_gpio_expanders()
62{
63    # Module 0 IOEXP
64    bind_i2c_device pca953x 2-0020
65    # Module 1 IOEXP
66    bind_i2c_device pca953x 2-0021
67    # HMC IOEXP
68    bind_i2c_device pca953x 2-0027
69    # BMC IOEXP
70    bind_i2c_device pca953x 6-0021
71    # IO Mezz 0 IOEXP
72    bind_i2c_device pca953x 29-0020
73    # IO Mezz 1 IOEXP
74    bind_i2c_device pca953x 41-0021
75}
76
77bind_fru_eeproms()
78{
79    # Module 0 FRU
80    bind_i2c_device at24 13-0050
81    # Module 1 FRU
82    bind_i2c_device at24 12-0050
83    # HMC FRU
84    bind_i2c_device at24 13-0057
85    # Left CBC FRU
86    bind_i2c_device at24 13-0054
87    # Right CBC FRU
88    bind_i2c_device at24 13-0055
89    # IO Mezz 0 FRU
90    bind_i2c_device at24 29-0050
91    # IO Mezz 1 FRU
92    bind_i2c_device at24 41-0050
93    # HDD Board FRU
94    bind_i2c_device at24 54-0052
95}
96
97reset_host_usb()
98{
99    set_gpio "USB2_HUB_RESET_L" "$GPIO_LOW"
100    sleep 1
101    set_gpio "USB2_HUB_RESET_L" "$GPIO_HIGH"
102}
103
104rebind_hmc_usb_network()
105{
106    echo 1e6a1000.usb > /sys/bus/platform/drivers/ehci-platform/unbind
107    echo 1e6a1000.usb > /sys/bus/platform/drivers/ehci-platform/bind
108}
109
110is_stby_good()
111{
112    local gpio_val
113
114    if ! gpio_val=$(get_gpio "STBY_POWER_PG_3V3"); then
115        return 1
116    fi
117
118    if [[ $gpio_val -eq 0 ]]; then
119        return 1
120    fi
121
122    return 0
123}
124
125set_bmc_ready()
126{
127    local bmc_ready="/sys/class/leds/bmc_ready_noled/brightness"
128    local bmc_ready_cpld="/sys/class/leds/bmc_ready_cpld_noled/brightness"
129
130    echo 1 > ${bmc_ready}
131    echo 1 > ${bmc_ready_cpld}
132    return 0
133}
134
135is_hmc_reachable()
136{
137    local max_retry_count="$1"
138    for retry in $(seq 1 "$max_retry_count")
139    do
140        if ping -c 1 -W 1 172.31.13.251; then
141            echo "HMC is reachable"
142            return 0
143        fi
144        echo "HMC is not reachable, retry count: $retry"
145    done
146    return 1
147}
148
149if ! is_stby_good; then
150    set_gpio "SCM_HPM_STBY_RST_N" "$GPIO_LOW"
151fi
152
153set_gpio "SCM_HPM_STBY_EN" "$GPIO_HIGH"
154set_gpio "stby_power_en_cpld" "$GPIO_HIGH"
155if ! wait_gpio_value "STBY_POWER_PG_3V3" "$GPIO_HIGH" 20 1; then
156    echo "Error: failed to get STBY_POWER_PG_3V3 as high (1) in 20 sec"
157    exit 1
158fi
159set_gpio "stby_power_gd_cpld" "$GPIO_HIGH"
160
161bind_i2c_muxes
162bind_gpio_expanders
163
164set_gpio "HMC_PGOOD" "$GPIO_HIGH"
165set_gpio "EROT_FPGA_RST_L" "$GPIO_HIGH"
166set_gpio "SEC_EROT_FPGA_RST_L" "$GPIO_HIGH"
167set_gpio "HMC_EROT_RST_L" "$GPIO_HIGH"
168set_gpio "SCM_HPM_STBY_RST_N" "$GPIO_HIGH"
169
170reset_host_usb
171
172if ! wait_gpio_value "HMC_READY" "$GPIO_HIGH" 180 1; then
173    echo "Error: failed to get HMC_READY as high (1) in 180 sec"
174    exit 1
175fi
176
177if ! is_hmc_reachable 10; then
178    rebind_hmc_usb_network
179fi
180
181if ! wait_gpio_value "FPGA_READY_BMC" "$GPIO_HIGH" 180 1; then
182    echo "Error: failed to get FPGA_READY_BMC as high (1) in 180 sec"
183    exit 1
184fi
185
186bind_fru_eeproms
187set_bmc_ready
188
189exit 0
190