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
41d2e7f202SPotin Laiwait_gpio_falling()
42d2e7f202SPotin Lai{
43d2e7f202SPotin Lai    local NET_NAME=$1
44d2e7f202SPotin Lai    local TIMEOUT_SEC=$2
45d2e7f202SPotin Lai
46d2e7f202SPotin Lai    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
47d2e7f202SPotin Lai    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
48d2e7f202SPotin Lai        echo "wait_gpio_falling: can not find gpio, $NET_NAME"
49d2e7f202SPotin Lai        return 1
50d2e7f202SPotin Lai    fi
51d2e7f202SPotin Lai
52*bc3087faSPotin Lai    timeout "$TIMEOUT_SEC" gpiomon --silent --falling-edge --num-events=1 "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}"
53d2e7f202SPotin Lai}
54d2e7f202SPotin Lai
55501f4c78SPotin Laiset_fan()
56501f4c78SPotin Lai{
57501f4c78SPotin Lai    FAN_ID=$1
58501f4c78SPotin Lai    FAN_DUTY=$2
59501f4c78SPotin Lai    SYSFA_PWM_PATH=""
60501f4c78SPotin Lai
61501f4c78SPotin Lai    for file in /sys/devices/platform/pwm-fan"$FAN_ID"/hwmon/hwmon*/pwm1
62501f4c78SPotin Lai    do
63501f4c78SPotin Lai        if [ -e "$file" ]; then
64501f4c78SPotin Lai            SYSFA_PWM_PATH="$file"
65501f4c78SPotin Lai            break
66501f4c78SPotin Lai        fi
67501f4c78SPotin Lai    done
68501f4c78SPotin Lai
69501f4c78SPotin Lai    if [ -z "$SYSFA_PWM_PATH" ]; then
70501f4c78SPotin Lai        echo "set_fan: pwm file not found, chekc fan id ($FAN_ID)"
71501f4c78SPotin Lai        return 1
72501f4c78SPotin Lai    fi
73501f4c78SPotin Lai
74501f4c78SPotin Lai    if [ "$FAN_DUTY" -lt 0 ] || [ "$FAN_DUTY" -gt 100 ]; then
75501f4c78SPotin Lai        echo "set_fan: incorrect fan duty, $FAN_DUTY"
76501f4c78SPotin Lai        return 1
77501f4c78SPotin Lai    fi
78501f4c78SPotin Lai
79501f4c78SPotin Lai    # convert duty (0-100) to pwm value (0-255)
80501f4c78SPotin Lai    PWM_VAL=$(printf "%.0f" $((FAN_DUTY*255/100)))
81501f4c78SPotin Lai
82501f4c78SPotin Lai    echo -n "set_fan: set fan$FAN_ID = $FAN_DUTY"
83501f4c78SPotin Lai    if ! echo "$PWM_VAL" > "$SYSFA_PWM_PATH"; then
84501f4c78SPotin Lai        echo " failed"
85501f4c78SPotin Lai        return 1
86501f4c78SPotin Lai    fi
87501f4c78SPotin Lai
88501f4c78SPotin Lai    echo " success"
89501f4c78SPotin Lai    return 0
90501f4c78SPotin Lai}
91501f4c78SPotin Lai
92501f4c78SPotin Lai
93501f4c78SPotin Laiis_sled_present()
94501f4c78SPotin Lai{
95501f4c78SPotin Lai    local SLED_ID=$1
96501f4c78SPotin Lai    local SRV_NAME="xyz.openbmc_project.Inventory.Manager"
97501f4c78SPotin Lai    local OBJ_PATH="/xyz/openbmc_project/inventory/system/chassis/presence/presence_sled${SLED_ID}"
98501f4c78SPotin Lai    local INTF_NAME="xyz.openbmc_project.Inventory.Item"
99501f4c78SPotin Lai    local PRST_VAL
100501f4c78SPotin Lai
101501f4c78SPotin Lai    PRST_VAL=$(busctl get-property "${SRV_NAME}" "${OBJ_PATH}" "${INTF_NAME}" Present | awk '{print $2}')
102501f4c78SPotin Lai    if [ "$PRST_VAL" != "true" ]; then
103501f4c78SPotin Lai        # not present
104501f4c78SPotin Lai        return 1
105501f4c78SPotin Lai    fi
106501f4c78SPotin Lai
107501f4c78SPotin Lai    # present
108501f4c78SPotin Lai    return 0
109501f4c78SPotin Lai}
1104bdfceb1SPotin Lai
1114bdfceb1SPotin Laispi2_mux_select()
1124bdfceb1SPotin Lai{
1134bdfceb1SPotin Lai    local SLED_INDEX="$1"
1144bdfceb1SPotin Lai    if [ "$SLED_INDEX" = "0" ]; then
1154bdfceb1SPotin Lai        set_gpio SEL_SPI2_MUX 1
1164bdfceb1SPotin Lai        set_gpio SPI2_MUX1 1
1174bdfceb1SPotin Lai        set_gpio SPI2_MUX2 1
1184bdfceb1SPotin Lai        set_gpio SPI2_MUX3 1
1194bdfceb1SPotin Lai    elif [ "$SLED_INDEX" = "1" ]; then
1204bdfceb1SPotin Lai        set_gpio SEL_SPI2_MUX 0
1214bdfceb1SPotin Lai        set_gpio SPI2_MUX1 0
1224bdfceb1SPotin Lai        set_gpio SPI2_MUX2 1
1234bdfceb1SPotin Lai        set_gpio SPI2_MUX3 1
1244bdfceb1SPotin Lai    elif [ "$SLED_INDEX" = "2" ]; then
1254bdfceb1SPotin Lai        set_gpio SEL_SPI2_MUX 1
1264bdfceb1SPotin Lai        set_gpio SPI2_MUX1 0
1274bdfceb1SPotin Lai        set_gpio SPI2_MUX2 1
1284bdfceb1SPotin Lai        set_gpio SPI2_MUX3 1
1294bdfceb1SPotin Lai    elif [ "$SLED_INDEX" = "3" ]; then
1304bdfceb1SPotin Lai        set_gpio SEL_SPI2_MUX 0
1314bdfceb1SPotin Lai        set_gpio SPI2_MUX1 1
1324bdfceb1SPotin Lai        set_gpio SPI2_MUX2 0
1334bdfceb1SPotin Lai        set_gpio SPI2_MUX3 1
1344bdfceb1SPotin Lai    elif [ "$SLED_INDEX" = "4" ]; then
1354bdfceb1SPotin Lai        set_gpio SEL_SPI2_MUX 1
1364bdfceb1SPotin Lai        set_gpio SPI2_MUX1 1
1374bdfceb1SPotin Lai        set_gpio SPI2_MUX2 0
1384bdfceb1SPotin Lai        set_gpio SPI2_MUX3 1
1394bdfceb1SPotin Lai    elif [ "$SLED_INDEX" = "5" ]; then
1404bdfceb1SPotin Lai        set_gpio SEL_SPI2_MUX 0
1414bdfceb1SPotin Lai        set_gpio SPI2_MUX1 1
1424bdfceb1SPotin Lai        set_gpio SPI2_MUX2 1
1434bdfceb1SPotin Lai        set_gpio SPI2_MUX3 0
1444bdfceb1SPotin Lai    elif [ "$SLED_INDEX" = "6" ]; then
1454bdfceb1SPotin Lai        set_gpio SEL_SPI2_MUX 1
1464bdfceb1SPotin Lai        set_gpio SPI2_MUX1 1
1474bdfceb1SPotin Lai        set_gpio SPI2_MUX2 1
1484bdfceb1SPotin Lai        set_gpio SPI2_MUX3 0
1494bdfceb1SPotin Lai    else
1504bdfceb1SPotin Lai        echo "set_spi2_mux: unknow sled index ($SLED_INDEX)"
1514bdfceb1SPotin Lai        return 1
1524bdfceb1SPotin Lai    fi
1534bdfceb1SPotin Lai
1544bdfceb1SPotin Lai    return 0
1554bdfceb1SPotin Lai}
1564bdfceb1SPotin Lai
1574bdfceb1SPotin Laibind_spi2_pnor()
1584bdfceb1SPotin Lai{
1594bdfceb1SPotin Lai    local SLED_INDEX="$1"
1604bdfceb1SPotin Lai
1614bdfceb1SPotin Lai    if ! spi2_mux_select "$SLED_INDEX"; then
1624bdfceb1SPotin Lai        return 1
1634bdfceb1SPotin Lai    fi
1644bdfceb1SPotin Lai
1654bdfceb1SPotin Lai    if ! echo -n spi1.0 > /sys/bus/spi/drivers/spi-nor/bind; then
1664bdfceb1SPotin Lai        echo "Error: flash bind failed"
1674bdfceb1SPotin Lai        return 1
1684bdfceb1SPotin Lai    fi
1694bdfceb1SPotin Lai
1704bdfceb1SPotin Lai    return 0
1714bdfceb1SPotin Lai}
1724bdfceb1SPotin Lai
1734bdfceb1SPotin Laiunbind_spi2_pnor()
1744bdfceb1SPotin Lai{
1754bdfceb1SPotin Lai    echo -n spi1.0 > /sys/bus/spi/drivers/spi-nor/unbind
1764bdfceb1SPotin Lai    spi2_mux_select 0
1774bdfceb1SPotin Lai    return 0
1784bdfceb1SPotin Lai}
1794bdfceb1SPotin Lai
1804bdfceb1SPotin Laifind_mtd()
1814bdfceb1SPotin Lai{
1824bdfceb1SPotin Lai    m=$(grep -xl "$1" /sys/class/mtd/*/name)
1834bdfceb1SPotin Lai    m=${m%/name}
1844bdfceb1SPotin Lai    m=${m##*/}
1854bdfceb1SPotin Lai    echo "$m"
1864bdfceb1SPotin Lai}
187