1#!/bin/bash
2
3get_gpio()
4{
5    local NET_NAME=$1
6    local RET_VAL
7
8    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
9    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
10        echo "get_gpio: can not find gpio, $NET_NAME" >&2
11        return 1
12    fi
13    if ! RET_VAL=$(gpioget "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}") ; then
14        echo "get_gpio: get ${NET_NAME} failed" >&2
15        return 1
16    fi
17    echo "${RET_VAL}"
18    return 0
19}
20
21set_gpio()
22{
23    local NET_NAME=$1
24    local OUT_VAL=$2
25    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
26    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
27        echo "set_gpio: can not find gpio, $NET_NAME"
28        return 1
29    fi
30
31    echo -n "set_gpio: set $NET_NAME = $OUT_VAL"
32    if ! gpioset "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}"="$OUT_VAL"; then
33        echo " failed"
34        return 1
35    fi
36
37    echo " success"
38    return 0
39}
40
41wait_gpio_falling()
42{
43    local NET_NAME=$1
44    local TIMEOUT_SEC=$2
45
46    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
47    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
48        echo "wait_gpio_falling: can not find gpio, $NET_NAME"
49        return 1
50    fi
51
52    timeout "$TIMEOUT_SEC" gpiomon --silent --falling-edge --num-events=1 "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}"
53}
54
55set_fan()
56{
57    FAN_ID=$1
58    FAN_DUTY=$2
59    SYSFA_PWM_PATH=""
60
61    for file in /sys/devices/platform/pwm-fan"$FAN_ID"/hwmon/hwmon*/pwm1
62    do
63        if [ -e "$file" ]; then
64            SYSFA_PWM_PATH="$file"
65            break
66        fi
67    done
68
69    if [ -z "$SYSFA_PWM_PATH" ]; then
70        echo "set_fan: pwm file not found, chekc fan id ($FAN_ID)"
71        return 1
72    fi
73
74    if [ "$FAN_DUTY" -lt 0 ] || [ "$FAN_DUTY" -gt 100 ]; then
75        echo "set_fan: incorrect fan duty, $FAN_DUTY"
76        return 1
77    fi
78
79    # convert duty (0-100) to pwm value (0-255)
80    PWM_VAL=$(printf "%.0f" $((FAN_DUTY*255/100)))
81
82    echo -n "set_fan: set fan$FAN_ID = $FAN_DUTY"
83    if ! echo "$PWM_VAL" > "$SYSFA_PWM_PATH"; then
84        echo " failed"
85        return 1
86    fi
87
88    echo " success"
89    return 0
90}
91
92
93is_sled_present()
94{
95    local SLED_ID=$1
96    local SRV_NAME="xyz.openbmc_project.Inventory.Manager"
97    local OBJ_PATH="/xyz/openbmc_project/inventory/system/chassis/presence/presence_sled${SLED_ID}"
98    local INTF_NAME="xyz.openbmc_project.Inventory.Item"
99    local PRST_VAL
100
101    PRST_VAL=$(busctl get-property "${SRV_NAME}" "${OBJ_PATH}" "${INTF_NAME}" Present | awk '{print $2}')
102    if [ "$PRST_VAL" != "true" ]; then
103        # not present
104        return 1
105    fi
106
107    # present
108    return 0
109}
110
111spi2_mux_select()
112{
113    local SLED_INDEX="$1"
114    if [ "$SLED_INDEX" = "0" ]; then
115        set_gpio SEL_SPI2_MUX 1
116        set_gpio SPI2_MUX1 1
117        set_gpio SPI2_MUX2 1
118        set_gpio SPI2_MUX3 1
119    elif [ "$SLED_INDEX" = "1" ]; then
120        set_gpio SEL_SPI2_MUX 0
121        set_gpio SPI2_MUX1 0
122        set_gpio SPI2_MUX2 1
123        set_gpio SPI2_MUX3 1
124    elif [ "$SLED_INDEX" = "2" ]; then
125        set_gpio SEL_SPI2_MUX 1
126        set_gpio SPI2_MUX1 0
127        set_gpio SPI2_MUX2 1
128        set_gpio SPI2_MUX3 1
129    elif [ "$SLED_INDEX" = "3" ]; then
130        set_gpio SEL_SPI2_MUX 0
131        set_gpio SPI2_MUX1 1
132        set_gpio SPI2_MUX2 0
133        set_gpio SPI2_MUX3 1
134    elif [ "$SLED_INDEX" = "4" ]; then
135        set_gpio SEL_SPI2_MUX 1
136        set_gpio SPI2_MUX1 1
137        set_gpio SPI2_MUX2 0
138        set_gpio SPI2_MUX3 1
139    elif [ "$SLED_INDEX" = "5" ]; then
140        set_gpio SEL_SPI2_MUX 0
141        set_gpio SPI2_MUX1 1
142        set_gpio SPI2_MUX2 1
143        set_gpio SPI2_MUX3 0
144    elif [ "$SLED_INDEX" = "6" ]; then
145        set_gpio SEL_SPI2_MUX 1
146        set_gpio SPI2_MUX1 1
147        set_gpio SPI2_MUX2 1
148        set_gpio SPI2_MUX3 0
149    else
150        echo "set_spi2_mux: unknow sled index ($SLED_INDEX)"
151        return 1
152    fi
153
154    return 0
155}
156
157bind_spi2_pnor()
158{
159    local SLED_INDEX="$1"
160
161    if ! spi2_mux_select "$SLED_INDEX"; then
162        return 1
163    fi
164
165    if ! echo -n spi1.0 > /sys/bus/spi/drivers/spi-nor/bind; then
166        echo "Error: flash bind failed"
167        return 1
168    fi
169
170    return 0
171}
172
173unbind_spi2_pnor()
174{
175    echo -n spi1.0 > /sys/bus/spi/drivers/spi-nor/unbind
176    spi2_mux_select 0
177    return 0
178}
179
180find_mtd()
181{
182    m=$(grep -xl "$1" /sys/class/mtd/*/name)
183    m=${m%/name}
184    m=${m##*/}
185    echo "$m"
186}
187