1#!/bin/bash
2
3# shellcheck source=meta-facebook/recipes-fb/obmc_functions/files/fb-common-functions
4source /usr/libexec/fb-common-functions
5
6phosphor_log() {
7    busctl call \
8        xyz.openbmc_project.Logging \
9        /xyz/openbmc_project/logging \
10        xyz.openbmc_project.Logging.Create \
11        Create "ssa{ss}" "$1" "$2" 0
12}
13
14phosphor_log_err() {
15    local msg=$1
16    local logErr="xyz.openbmc_project.Logging.Entry.Level.Error"
17    phosphor_log "$msg" "$logErr"
18}
19
20phosphor_log_info() {
21    local msg=$1
22    local logInfo="xyz.openbmc_project.Logging.Entry.Level.Informational"
23    phosphor_log "$msg" "$logInfo"
24}
25
26chassis_power_cycle_ltc4287()
27{
28    if ! i2cset -f -y 20 0x42 0xfd 0x04; then
29        echo "48V HSC1 set reboot delay failed"
30        return 1
31    fi
32
33    if ! i2cset -f -y 20 0x43 0xfd 0x04; then
34        echo "48V HSC2 set reboot delay failed"
35        return 1
36    fi
37
38    if ! i2cset -f -y 20 0x42 0xfd 0x0c; then
39        echo "48V HSC1 set reboot bit failed"
40        return 1
41    fi
42
43    if ! i2cset -f -y 20 0x43 0xfd 0x0c; then
44        echo "48V HSC2 set reboot bit failed"
45        return 1
46    fi
47
48    return 0
49}
50
51chassis_power_cycle()
52{
53    chassis_power_cycle_ltc4287
54    return $?
55}
56
57chassis_power_on()
58{
59    # MB stabdby power should enabled by before BMC ready
60    # So only do checking here.
61    if [ "$(chassis_power_status)" != "on" ]; then
62        return 1
63    fi
64    return 0
65}
66
67chassis_power_status()
68{
69    if [ "$(get_gpio "STBY_POWER_PG_3V3")" -eq 1 ]; then
70        echo "on"
71    else
72        echo "off"
73    fi
74}
75
76host_power_on()
77{
78    if [ "$(host_power_status)" == "off" ]; then
79        press_host_power_button 1
80        if ! wait_host_power_on; then
81            return 1
82        fi
83    fi
84    return 0
85}
86
87host_force_power_off()
88{
89    if [ "$(host_power_status)" == "on" ]; then
90        press_host_power_button 6
91        if ! wait_host_power_off; then
92            return 1
93        fi
94    fi
95    return 0
96}
97
98host_graceful_power_off()
99{
100    if [ "$(host_power_status)" == "on" ]; then
101        press_host_power_button 1
102        if ! wait_host_power_off; then
103            return 1
104        fi
105    fi
106    return 0
107}
108
109host_power_status()
110{
111    if [ "$(get_gpio "host0-ready")" -eq 1 ]; then
112        echo "on"
113    else
114        echo "off"
115    fi
116}
117
118press_host_power_button()
119{
120    local press_delay_sec="$1"
121    set_gpio "SYS_BMC_PWRBTN_R_N" 1
122    set_gpio "SYS_BMC_PWRBTN_R_N" 0
123    sleep "$press_delay_sec"
124    set_gpio "SYS_BMC_PWRBTN_R_N" 1
125}
126
127wait_host_power_change()
128{
129    local exp_val="$1"
130    local count=0
131    until [ $count -gt 10 ]
132    do
133        sleep 1
134        if [ "$(host_power_status)" == "$exp_val" ]; then
135            return 0
136        fi
137        ((count++))
138    done
139
140    return 1
141}
142
143wait_host_power_on()
144{
145    wait_host_power_change "on"
146    return $?
147}
148
149wait_host_power_off()
150{
151    wait_host_power_change "off"
152    return $?
153}