1501f4c78SPotin Lai#!/bin/bash
2501f4c78SPotin Lai
3501f4c78SPotin Laiget_gpio()
4501f4c78SPotin Lai{
5501f4c78SPotin Lai    local NET_NAME=$1
6501f4c78SPotin Lai    local RET_VAL
7501f4c78SPotin Lai
8501f4c78SPotin Lai    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
9501f4c78SPotin Lai    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
10501f4c78SPotin Lai        echo "get_gpio: can not find gpio, $NET_NAME" >&2
11501f4c78SPotin Lai        return 1
12501f4c78SPotin Lai    fi
13501f4c78SPotin Lai    if ! RET_VAL=$(gpioget "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}") ; then
14501f4c78SPotin Lai        echo "get_gpio: get ${NET_NAME} failed" >&2
15501f4c78SPotin Lai        return 1
16501f4c78SPotin Lai    fi
17501f4c78SPotin Lai    echo "${RET_VAL}"
18501f4c78SPotin Lai    return 0
19501f4c78SPotin Lai}
20501f4c78SPotin Lai
21501f4c78SPotin Laiset_gpio()
22501f4c78SPotin Lai{
23501f4c78SPotin Lai    local NET_NAME=$1
24501f4c78SPotin Lai    local OUT_VAL=$2
25501f4c78SPotin Lai    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
26501f4c78SPotin Lai    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
27501f4c78SPotin Lai        echo "set_gpio: can not find gpio, $NET_NAME"
28501f4c78SPotin Lai        return 1
29501f4c78SPotin Lai    fi
30501f4c78SPotin Lai
31501f4c78SPotin Lai    echo -n "set_gpio: set $NET_NAME = $OUT_VAL"
32501f4c78SPotin Lai    if ! gpioset "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}"="$OUT_VAL"; then
33501f4c78SPotin Lai        echo " failed"
34501f4c78SPotin Lai        return 1
35501f4c78SPotin Lai    fi
36501f4c78SPotin Lai
37501f4c78SPotin Lai    echo " success"
38501f4c78SPotin Lai    return 0
39501f4c78SPotin Lai}
40501f4c78SPotin Lai
41501f4c78SPotin Laiset_fan()
42501f4c78SPotin Lai{
43501f4c78SPotin Lai    FAN_ID=$1
44501f4c78SPotin Lai    FAN_DUTY=$2
45501f4c78SPotin Lai    SYSFA_PWM_PATH=""
46501f4c78SPotin Lai
47501f4c78SPotin Lai    for file in /sys/devices/platform/pwm-fan"$FAN_ID"/hwmon/hwmon*/pwm1
48501f4c78SPotin Lai    do
49501f4c78SPotin Lai        if [ -e "$file" ]; then
50501f4c78SPotin Lai            SYSFA_PWM_PATH="$file"
51501f4c78SPotin Lai            break
52501f4c78SPotin Lai        fi
53501f4c78SPotin Lai    done
54501f4c78SPotin Lai
55501f4c78SPotin Lai    if [ -z "$SYSFA_PWM_PATH" ]; then
56501f4c78SPotin Lai        echo "set_fan: pwm file not found, chekc fan id ($FAN_ID)"
57501f4c78SPotin Lai        return 1
58501f4c78SPotin Lai    fi
59501f4c78SPotin Lai
60501f4c78SPotin Lai    if [ "$FAN_DUTY" -lt 0 ] || [ "$FAN_DUTY" -gt 100 ]; then
61501f4c78SPotin Lai        echo "set_fan: incorrect fan duty, $FAN_DUTY"
62501f4c78SPotin Lai        return 1
63501f4c78SPotin Lai    fi
64501f4c78SPotin Lai
65501f4c78SPotin Lai    # convert duty (0-100) to pwm value (0-255)
66501f4c78SPotin Lai    PWM_VAL=$(printf "%.0f" $((FAN_DUTY*255/100)))
67501f4c78SPotin Lai
68501f4c78SPotin Lai    echo -n "set_fan: set fan$FAN_ID = $FAN_DUTY"
69501f4c78SPotin Lai    if ! echo "$PWM_VAL" > "$SYSFA_PWM_PATH"; then
70501f4c78SPotin Lai        echo " failed"
71501f4c78SPotin Lai        return 1
72501f4c78SPotin Lai    fi
73501f4c78SPotin Lai
74501f4c78SPotin Lai    echo " success"
75501f4c78SPotin Lai    return 0
76501f4c78SPotin Lai}
77501f4c78SPotin Lai
78501f4c78SPotin Lai
79501f4c78SPotin Laiis_sled_present()
80501f4c78SPotin Lai{
81501f4c78SPotin Lai    local SLED_ID=$1
82501f4c78SPotin Lai    local SRV_NAME="xyz.openbmc_project.Inventory.Manager"
83501f4c78SPotin Lai    local OBJ_PATH="/xyz/openbmc_project/inventory/system/chassis/presence/presence_sled${SLED_ID}"
84501f4c78SPotin Lai    local INTF_NAME="xyz.openbmc_project.Inventory.Item"
85501f4c78SPotin Lai    local PRST_VAL
86501f4c78SPotin Lai
87501f4c78SPotin Lai    PRST_VAL=$(busctl get-property "${SRV_NAME}" "${OBJ_PATH}" "${INTF_NAME}" Present | awk '{print $2}')
88501f4c78SPotin Lai    if [ "$PRST_VAL" != "true" ]; then
89501f4c78SPotin Lai        # not present
90501f4c78SPotin Lai        return 1
91501f4c78SPotin Lai    fi
92501f4c78SPotin Lai
93501f4c78SPotin Lai    # present
94501f4c78SPotin Lai    return 0
95501f4c78SPotin Lai}
96*4bdfceb1SPotin Lai
97*4bdfceb1SPotin Laispi2_mux_select()
98*4bdfceb1SPotin Lai{
99*4bdfceb1SPotin Lai    local SLED_INDEX="$1"
100*4bdfceb1SPotin Lai    if [ "$SLED_INDEX" = "0" ]; then
101*4bdfceb1SPotin Lai        set_gpio SEL_SPI2_MUX 1
102*4bdfceb1SPotin Lai        set_gpio SPI2_MUX1 1
103*4bdfceb1SPotin Lai        set_gpio SPI2_MUX2 1
104*4bdfceb1SPotin Lai        set_gpio SPI2_MUX3 1
105*4bdfceb1SPotin Lai    elif [ "$SLED_INDEX" = "1" ]; then
106*4bdfceb1SPotin Lai        set_gpio SEL_SPI2_MUX 0
107*4bdfceb1SPotin Lai        set_gpio SPI2_MUX1 0
108*4bdfceb1SPotin Lai        set_gpio SPI2_MUX2 1
109*4bdfceb1SPotin Lai        set_gpio SPI2_MUX3 1
110*4bdfceb1SPotin Lai    elif [ "$SLED_INDEX" = "2" ]; then
111*4bdfceb1SPotin Lai        set_gpio SEL_SPI2_MUX 1
112*4bdfceb1SPotin Lai        set_gpio SPI2_MUX1 0
113*4bdfceb1SPotin Lai        set_gpio SPI2_MUX2 1
114*4bdfceb1SPotin Lai        set_gpio SPI2_MUX3 1
115*4bdfceb1SPotin Lai    elif [ "$SLED_INDEX" = "3" ]; then
116*4bdfceb1SPotin Lai        set_gpio SEL_SPI2_MUX 0
117*4bdfceb1SPotin Lai        set_gpio SPI2_MUX1 1
118*4bdfceb1SPotin Lai        set_gpio SPI2_MUX2 0
119*4bdfceb1SPotin Lai        set_gpio SPI2_MUX3 1
120*4bdfceb1SPotin Lai    elif [ "$SLED_INDEX" = "4" ]; then
121*4bdfceb1SPotin Lai        set_gpio SEL_SPI2_MUX 1
122*4bdfceb1SPotin Lai        set_gpio SPI2_MUX1 1
123*4bdfceb1SPotin Lai        set_gpio SPI2_MUX2 0
124*4bdfceb1SPotin Lai        set_gpio SPI2_MUX3 1
125*4bdfceb1SPotin Lai    elif [ "$SLED_INDEX" = "5" ]; then
126*4bdfceb1SPotin Lai        set_gpio SEL_SPI2_MUX 0
127*4bdfceb1SPotin Lai        set_gpio SPI2_MUX1 1
128*4bdfceb1SPotin Lai        set_gpio SPI2_MUX2 1
129*4bdfceb1SPotin Lai        set_gpio SPI2_MUX3 0
130*4bdfceb1SPotin Lai    elif [ "$SLED_INDEX" = "6" ]; then
131*4bdfceb1SPotin Lai        set_gpio SEL_SPI2_MUX 1
132*4bdfceb1SPotin Lai        set_gpio SPI2_MUX1 1
133*4bdfceb1SPotin Lai        set_gpio SPI2_MUX2 1
134*4bdfceb1SPotin Lai        set_gpio SPI2_MUX3 0
135*4bdfceb1SPotin Lai    else
136*4bdfceb1SPotin Lai        echo "set_spi2_mux: unknow sled index ($SLED_INDEX)"
137*4bdfceb1SPotin Lai        return 1
138*4bdfceb1SPotin Lai    fi
139*4bdfceb1SPotin Lai
140*4bdfceb1SPotin Lai    return 0
141*4bdfceb1SPotin Lai}
142*4bdfceb1SPotin Lai
143*4bdfceb1SPotin Laibind_spi2_pnor()
144*4bdfceb1SPotin Lai{
145*4bdfceb1SPotin Lai    local SLED_INDEX="$1"
146*4bdfceb1SPotin Lai
147*4bdfceb1SPotin Lai    if ! spi2_mux_select "$SLED_INDEX"; then
148*4bdfceb1SPotin Lai        return 1
149*4bdfceb1SPotin Lai    fi
150*4bdfceb1SPotin Lai
151*4bdfceb1SPotin Lai    if ! echo -n spi1.0 > /sys/bus/spi/drivers/spi-nor/bind; then
152*4bdfceb1SPotin Lai        echo "Error: flash bind failed"
153*4bdfceb1SPotin Lai        return 1
154*4bdfceb1SPotin Lai    fi
155*4bdfceb1SPotin Lai
156*4bdfceb1SPotin Lai    return 0
157*4bdfceb1SPotin Lai}
158*4bdfceb1SPotin Lai
159*4bdfceb1SPotin Laiunbind_spi2_pnor()
160*4bdfceb1SPotin Lai{
161*4bdfceb1SPotin Lai    echo -n spi1.0 > /sys/bus/spi/drivers/spi-nor/unbind
162*4bdfceb1SPotin Lai    spi2_mux_select 0
163*4bdfceb1SPotin Lai    return 0
164*4bdfceb1SPotin Lai}
165*4bdfceb1SPotin Lai
166*4bdfceb1SPotin Laifind_mtd()
167*4bdfceb1SPotin Lai{
168*4bdfceb1SPotin Lai    m=$(grep -xl "$1" /sys/class/mtd/*/name)
169*4bdfceb1SPotin Lai    m=${m%/name}
170*4bdfceb1SPotin Lai    m=${m##*/}
171*4bdfceb1SPotin Lai    echo "$m"
172*4bdfceb1SPotin Lai}
173