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
41set_fan()
42{
43    FAN_ID=$1
44    FAN_DUTY=$2
45    SYSFA_PWM_PATH=""
46
47    for file in /sys/devices/platform/pwm-fan"$FAN_ID"/hwmon/hwmon*/pwm1
48    do
49        if [ -e "$file" ]; then
50            SYSFA_PWM_PATH="$file"
51            break
52        fi
53    done
54
55    if [ -z "$SYSFA_PWM_PATH" ]; then
56        echo "set_fan: pwm file not found, chekc fan id ($FAN_ID)"
57        return 1
58    fi
59
60    if [ "$FAN_DUTY" -lt 0 ] || [ "$FAN_DUTY" -gt 100 ]; then
61        echo "set_fan: incorrect fan duty, $FAN_DUTY"
62        return 1
63    fi
64
65    # convert duty (0-100) to pwm value (0-255)
66    PWM_VAL=$(printf "%.0f" $((FAN_DUTY*255/100)))
67
68    echo -n "set_fan: set fan$FAN_ID = $FAN_DUTY"
69    if ! echo "$PWM_VAL" > "$SYSFA_PWM_PATH"; then
70        echo " failed"
71        return 1
72    fi
73
74    echo " success"
75    return 0
76}
77
78
79is_sled_present()
80{
81    local SLED_ID=$1
82    local SRV_NAME="xyz.openbmc_project.Inventory.Manager"
83    local OBJ_PATH="/xyz/openbmc_project/inventory/system/chassis/presence/presence_sled${SLED_ID}"
84    local INTF_NAME="xyz.openbmc_project.Inventory.Item"
85    local PRST_VAL
86
87    PRST_VAL=$(busctl get-property "${SRV_NAME}" "${OBJ_PATH}" "${INTF_NAME}" Present | awk '{print $2}')
88    if [ "$PRST_VAL" != "true" ]; then
89        # not present
90        return 1
91    fi
92
93    # present
94    return 0
95}
96
97spi2_mux_select()
98{
99    local SLED_INDEX="$1"
100    if [ "$SLED_INDEX" = "0" ]; then
101        set_gpio SEL_SPI2_MUX 1
102        set_gpio SPI2_MUX1 1
103        set_gpio SPI2_MUX2 1
104        set_gpio SPI2_MUX3 1
105    elif [ "$SLED_INDEX" = "1" ]; then
106        set_gpio SEL_SPI2_MUX 0
107        set_gpio SPI2_MUX1 0
108        set_gpio SPI2_MUX2 1
109        set_gpio SPI2_MUX3 1
110    elif [ "$SLED_INDEX" = "2" ]; then
111        set_gpio SEL_SPI2_MUX 1
112        set_gpio SPI2_MUX1 0
113        set_gpio SPI2_MUX2 1
114        set_gpio SPI2_MUX3 1
115    elif [ "$SLED_INDEX" = "3" ]; then
116        set_gpio SEL_SPI2_MUX 0
117        set_gpio SPI2_MUX1 1
118        set_gpio SPI2_MUX2 0
119        set_gpio SPI2_MUX3 1
120    elif [ "$SLED_INDEX" = "4" ]; then
121        set_gpio SEL_SPI2_MUX 1
122        set_gpio SPI2_MUX1 1
123        set_gpio SPI2_MUX2 0
124        set_gpio SPI2_MUX3 1
125    elif [ "$SLED_INDEX" = "5" ]; then
126        set_gpio SEL_SPI2_MUX 0
127        set_gpio SPI2_MUX1 1
128        set_gpio SPI2_MUX2 1
129        set_gpio SPI2_MUX3 0
130    elif [ "$SLED_INDEX" = "6" ]; then
131        set_gpio SEL_SPI2_MUX 1
132        set_gpio SPI2_MUX1 1
133        set_gpio SPI2_MUX2 1
134        set_gpio SPI2_MUX3 0
135    else
136        echo "set_spi2_mux: unknow sled index ($SLED_INDEX)"
137        return 1
138    fi
139
140    return 0
141}
142
143bind_spi2_pnor()
144{
145    local SLED_INDEX="$1"
146
147    if ! spi2_mux_select "$SLED_INDEX"; then
148        return 1
149    fi
150
151    if ! echo -n spi1.0 > /sys/bus/spi/drivers/spi-nor/bind; then
152        echo "Error: flash bind failed"
153        return 1
154    fi
155
156    return 0
157}
158
159unbind_spi2_pnor()
160{
161    echo -n spi1.0 > /sys/bus/spi/drivers/spi-nor/unbind
162    spi2_mux_select 0
163    return 0
164}
165
166find_mtd()
167{
168    m=$(grep -xl "$1" /sys/class/mtd/*/name)
169    m=${m%/name}
170    m=${m##*/}
171    echo "$m"
172}
173