Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Power Control tool |
| 4 | # Enable/disable AC relay |
| 5 | # On/off System by step moter to press power key |
| 6 | |
| 7 | export PATH=$PATH:/usr/sbin:/usr/libexec |
| 8 | |
| 9 | DELAY_POWER_ON="0.5" |
| 10 | DELAY_POWER_OFF="5" |
| 11 | POWER_BTN_TIMEOUT_CNT=60 |
| 12 | |
| 13 | #Switch pull low while it be touched |
| 14 | function wait_for_switch() { |
| 15 | TARGET_PIN=$1 |
| 16 | TARGET_SWITCH=1 |
| 17 | TIME_CNT=0 |
| 18 | while [ "$TARGET_SWITCH" -eq 1 ] ;do |
| 19 | TARGET_SWITCH=$(get_gpio "$TARGET_PIN") |
| 20 | sleep 0.1 |
| 21 | TIME_CNT=$(( TIME_CNT +1)) |
| 22 | if [ $TIME_CNT -gt $POWER_BTN_TIMEOUT_CNT ];then |
| 23 | echo "Error: Too long to get target switch, force exit" >&2 |
| 24 | break |
| 25 | fi |
| 26 | done |
| 27 | } |
| 28 | |
| 29 | function trigger_power_button() { |
| 30 | local sled_num=$1 |
Allen.Wang | 6af0dff | 2021-12-28 20:23:05 +0800 | [diff] [blame] | 31 | local delay_time=$2 |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 32 | |
| 33 | #SLED{N}_MS_DETECT1 (initial position) |
| 34 | GPIO_DETECT_PIN1="SLED${sled_num}_MS_DETECT1" |
| 35 | #SLED{N}_MS_DETECT0 (MAC position) |
| 36 | GPIO_DETECT_PIN0="SLED${sled_num}_MS_DETECT0" |
| 37 | |
| 38 | echo "Motor go forward to press Power key" |
| 39 | motor-ctrl "sled${sled_num}" f >/dev/null |
| 40 | wait_for_switch "${GPIO_DETECT_PIN0}" |
| 41 | motor-ctrl "sled${sled_num}" s >/dev/null |
| 42 | |
| 43 | if [ "$(get_gpio "$GPIO_DETECT_PIN0")" -eq 0 ];then |
| 44 | echo "Power key switch triggered" |
| 45 | echo "Press power key for Sled${1} ${delay_time} seconds..." |
| 46 | sleep "$delay_time" |
| 47 | else |
Allen.Wang | 6af0dff | 2021-12-28 20:23:05 +0800 | [diff] [blame] | 48 | echo "Power key switch not trigger, back motor to initial position" |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 49 | fi |
| 50 | |
| 51 | motor-ctrl "sled${sled_num}" r >/dev/null |
| 52 | wait_for_switch "${GPIO_DETECT_PIN1}" |
| 53 | motor-ctrl "sled${sled_num}" s >/dev/null |
| 54 | if [ "$(get_gpio "$GPIO_DETECT_PIN1")" -eq 0 ];then |
| 55 | echo "Motor reverse to initial position successful" |
| 56 | else |
Allen.Wang | 6af0dff | 2021-12-28 20:23:05 +0800 | [diff] [blame] | 57 | echo "Initial position switch not trigger, force stop motor" |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 58 | fi |
| 59 | } |
| 60 | |
Potin Lai | db5648e | 2022-02-16 00:57:55 +0800 | [diff] [blame] | 61 | function release_power_button() { |
| 62 | local sled_num=$1 |
| 63 | GPIO_DETECT_PIN1="SLED${sled_num}_MS_DETECT1" |
| 64 | |
| 65 | if [ "$(get_gpio "$GPIO_DETECT_PIN1")" -eq 0 ]; then |
| 66 | echo "Motor at initial position already" |
| 67 | return 0 |
| 68 | fi |
| 69 | |
| 70 | motor-ctrl "sled${sled_num}" r >/dev/null |
| 71 | wait_for_switch "${GPIO_DETECT_PIN1}" |
| 72 | motor-ctrl "sled${sled_num}" s >/dev/null |
| 73 | if [ "$(get_gpio "$GPIO_DETECT_PIN1")" -eq 0 ];then |
| 74 | echo "Motor reverse to initial position successful" |
| 75 | return 0 |
| 76 | fi |
| 77 | |
| 78 | echo "Error: Initial position switch not trigger" |
| 79 | return 1 |
| 80 | } |
| 81 | |
| 82 | function press_power_button() { |
| 83 | local sled_num=$1 |
| 84 | |
| 85 | GPIO_DETECT_PIN0="SLED${sled_num}_MS_DETECT0" |
| 86 | |
| 87 | echo "Motor go forward to press Power button" |
| 88 | motor-ctrl "sled${sled_num}" f >/dev/null |
| 89 | wait_for_switch "${GPIO_DETECT_PIN0}" |
| 90 | motor-ctrl "sled${sled_num}" s >/dev/null |
| 91 | |
| 92 | if [ "$(get_gpio "$GPIO_DETECT_PIN0")" -eq 0 ];then |
| 93 | echo "Power button switch triggered" |
| 94 | return 0 |
| 95 | fi |
| 96 | |
| 97 | echo "Error: Power button switch not trigger" |
| 98 | return 1 |
| 99 | } |
| 100 | |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 101 | #Get i2c bus number for sledN |
| 102 | function get_bus_num() { |
| 103 | SLED_NUM=$1 |
| 104 | local bus=0 |
Allen.Wang | 5ff992e | 2022-01-04 21:00:38 +0800 | [diff] [blame] | 105 | #Mapping Sled number 1~6 to i2c bus number 0~5 |
| 106 | if [[ "$SLED_NUM" = [1-6] ]]; then |
| 107 | bus=$(( SLED_NUM - 1 )) |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 108 | fi |
| 109 | echo "$bus" |
| 110 | } |
| 111 | |
| 112 | function set_gpio() |
| 113 | { |
| 114 | NET_NAME=$1 |
| 115 | OUT_VAL=$2 |
| 116 | mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME") |
| 117 | if [ "${#GPIO_INFO[@]}" -ne 2 ]; then |
| 118 | echo "set_gpio: can not find gpio, $NET_NAME" |
| 119 | return 1 |
| 120 | fi |
| 121 | echo -n "set_gpio: set $NET_NAME = $OUT_VAL" |
| 122 | if ! gpioset "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}"="$OUT_VAL"; then |
| 123 | echo " failed" |
| 124 | return 1 |
| 125 | fi |
| 126 | echo " success" |
| 127 | return 0 |
| 128 | } |
| 129 | |
| 130 | function get_gpio() |
| 131 | { |
| 132 | NET_NAME=$1 |
| 133 | RET_VAL=2 |
| 134 | |
| 135 | mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME") |
| 136 | if [ "${#GPIO_INFO[@]}" -ne 2 ]; then |
| 137 | echo "get_gpio: can not find gpio, $NET_NAME" >&2 |
| 138 | return 1 |
| 139 | fi |
| 140 | if ! RET_VAL=$(gpioget "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}") ; then |
| 141 | echo "get_gpio: get ${NET_NAME} failed" >&2 |
| 142 | return 1 |
| 143 | fi |
| 144 | echo "${RET_VAL}" |
| 145 | return 0 |
| 146 | } |
| 147 | |
| 148 | function get_ac_status(){ |
| 149 | i2c_bus=$(get_bus_num "$1") |
| 150 | p1_output_reg=$(i2cget -f -y "$i2c_bus" 0x76 0x03) |
| 151 | p1_config_reg=$(i2cget -f -y "$i2c_bus" 0x76 0x07) |
| 152 | host_pwr="$(( (p1_output_reg & 0x80)>>7 ))" |
| 153 | is_output="$(( (~p1_config_reg & 0x80)>>7 ))" |
| 154 | |
| 155 | if [ "$(( host_pwr & is_output ))" -eq 1 ];then |
| 156 | echo "AC on" |
| 157 | else |
| 158 | echo "AC off" |
| 159 | fi |
| 160 | } |
| 161 | |
| 162 | function show_usage(){ |
Allen.Wang | 5ff992e | 2022-01-04 21:00:38 +0800 | [diff] [blame] | 163 | echo "Usage: power-ctrl [sled1 | sled2 | sled3 | sled4 | sled5 | sled6] [on off ac-on ac-off status]" |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 164 | echo " power-ctrl chassis-cycle" |
| 165 | } |
| 166 | |
| 167 | if [ $# -eq 1 ]; then |
| 168 | if [ "$1" = "chassis-cycle" ];then |
| 169 | echo "chassis cycle...." |
| 170 | i2cset -y -f 12 0x11 0xd9 c |
| 171 | exit 0 |
| 172 | else |
| 173 | echo "Invalid argument: [ $1 ]" |
| 174 | show_usage |
| 175 | exit 1; |
| 176 | fi |
| 177 | fi |
| 178 | |
| 179 | if [ $# -gt 2 ]; then |
| 180 | echo "Too many arguments" |
| 181 | show_usage |
| 182 | exit 1; |
| 183 | fi |
| 184 | |
Allen.Wang | 5ff992e | 2022-01-04 21:00:38 +0800 | [diff] [blame] | 185 | if [[ "$1" =~ ^(sled[1-6]{1})$ ]]; then |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 186 | SLED=$1 |
| 187 | ACTION=$2 |
| 188 | SLED_NUM=${SLED:4} |
| 189 | else |
| 190 | echo "invalid sled name: ${1}" |
| 191 | show_usage |
| 192 | exit 1; |
| 193 | fi |
| 194 | |
| 195 | #Check if sled is present |
| 196 | SLED_PRESENT=$(get_gpio "presence-sled${SLED_NUM}") |
| 197 | if [ "$SLED_PRESENT" != 0 ];then |
| 198 | echo "${SLED} is not present!" |
| 199 | exit 1 |
| 200 | fi |
| 201 | |
| 202 | if [[ "$ACTION" == "on" ]]; then |
Allen.Wang | 787c9c6 | 2022-03-02 14:55:03 +0800 | [diff] [blame] | 203 | if [ "$(get_ac_status "$SLED_NUM")" == "AC on" ];then |
| 204 | echo "Power on ${SLED}" |
| 205 | trigger_power_button "$SLED_NUM" "$DELAY_POWER_ON" |
| 206 | else |
| 207 | echo "${SLED} is ac-off, please turn on ac before power on" |
| 208 | exit 1 |
| 209 | fi |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 210 | elif [[ "$ACTION" == "off" ]];then |
Allen.Wang | 787c9c6 | 2022-03-02 14:55:03 +0800 | [diff] [blame] | 211 | if [ "$(get_ac_status "$SLED_NUM")" == "AC on" ];then |
| 212 | echo "Power off ${SLED}" |
| 213 | trigger_power_button "$SLED_NUM" "$DELAY_POWER_OFF" |
| 214 | else |
| 215 | echo "${SLED} is already ac-off" |
| 216 | exit 1 |
| 217 | fi |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 218 | elif [[ "$ACTION" == "status" ]];then |
| 219 | get_ac_status "$SLED_NUM" |
| 220 | #TODO : check or record Host(DC) power status |
| 221 | elif [[ "$ACTION" == "ac-on" ]];then |
| 222 | set_gpio "power-host${SLED_NUM}" 1 |
| 223 | elif [[ "$ACTION" == "ac-off" ]];then |
| 224 | set_gpio "power-host${SLED_NUM}" 0 |
Potin Lai | db5648e | 2022-02-16 00:57:55 +0800 | [diff] [blame] | 225 | elif [[ "$ACTION" == "dfu" ]]; then |
| 226 | echo "Set host$SLED_NUM DFU mode" |
| 227 | |
Allen.Wang | 787c9c6 | 2022-03-02 14:55:03 +0800 | [diff] [blame] | 228 | # turn ac off, and hold for 25 seconds |
Potin Lai | db5648e | 2022-02-16 00:57:55 +0800 | [diff] [blame] | 229 | echo "SLED$SLED_NUM: turn ac-off" |
| 230 | set_gpio "power-host${SLED_NUM}" 0 |
Allen.Wang | 787c9c6 | 2022-03-02 14:55:03 +0800 | [diff] [blame] | 231 | sleep 25 |
Potin Lai | db5648e | 2022-02-16 00:57:55 +0800 | [diff] [blame] | 232 | |
Allen.Wang | 787c9c6 | 2022-03-02 14:55:03 +0800 | [diff] [blame] | 233 | # press power button |
Potin Lai | db5648e | 2022-02-16 00:57:55 +0800 | [diff] [blame] | 234 | echo "SLED$SLED_NUM: pressing power button" |
| 235 | if ! press_power_button "$SLED_NUM"; then |
| 236 | echo "SLED$SLED_NUM: press power button failed" |
| 237 | echo "SLED$SLED_NUM: releasing power button" |
| 238 | release_power_button "$SLED_NUM" |
| 239 | exit 1 |
| 240 | fi |
Allen.Wang | 787c9c6 | 2022-03-02 14:55:03 +0800 | [diff] [blame] | 241 | sleep 1 |
Potin Lai | db5648e | 2022-02-16 00:57:55 +0800 | [diff] [blame] | 242 | |
| 243 | # turn ac on |
| 244 | echo "SLED$SLED_NUM: turn ac-on" |
| 245 | set_gpio "power-host${SLED_NUM}" 1 |
| 246 | sleep 3 |
| 247 | |
| 248 | # release power button |
| 249 | echo "SLED$SLED_NUM: releasing host power button" |
| 250 | if ! release_power_button "$SLED_NUM"; then |
| 251 | echo "SLED$SLED_NUM: release power button failed" |
| 252 | exit 1 |
| 253 | fi |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 254 | else |
| 255 | echo "Unknown action: [ ${ACTION} ]" |
| 256 | show_usage |
| 257 | fi |