blob: 523eb26d4dd5df6c514c010e4c9cc1ed5d4d3b3b [file] [log] [blame]
Allen.Wang4a0948d2021-12-15 13:41:18 +08001#!/bin/bash
2#
3# Power Control tool
4# Enable/disable AC relay
5# On/off System by step moter to press power key
6
7export PATH=$PATH:/usr/sbin:/usr/libexec
8
9DELAY_POWER_ON="0.5"
10DELAY_POWER_OFF="5"
11POWER_BTN_TIMEOUT_CNT=60
12
13#Switch pull low while it be touched
14function 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
29function trigger_power_button() {
30 local sled_num=$1
Allen.Wang6af0dff2021-12-28 20:23:05 +080031 local delay_time=$2
Allen.Wang4a0948d2021-12-15 13:41:18 +080032
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.Wang6af0dff2021-12-28 20:23:05 +080048 echo "Power key switch not trigger, back motor to initial position"
Allen.Wang4a0948d2021-12-15 13:41:18 +080049 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.Wang6af0dff2021-12-28 20:23:05 +080057 echo "Initial position switch not trigger, force stop motor"
Allen.Wang4a0948d2021-12-15 13:41:18 +080058 fi
59}
60
Potin Laidb5648e2022-02-16 00:57:55 +080061function 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
82function 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.Wang4a0948d2021-12-15 13:41:18 +0800101#Get i2c bus number for sledN
102function get_bus_num() {
103 SLED_NUM=$1
104 local bus=0
Allen.Wang5ff992e2022-01-04 21:00:38 +0800105 #Mapping Sled number 1~6 to i2c bus number 0~5
106 if [[ "$SLED_NUM" = [1-6] ]]; then
107 bus=$(( SLED_NUM - 1 ))
Allen.Wang4a0948d2021-12-15 13:41:18 +0800108 fi
109 echo "$bus"
110}
111
112function 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
130function 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
148function 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
Potin Laidc251662022-04-01 11:16:50 +0800162function get_host_status()
163{
164 SLED_NUM=$1
165 MDIO_BUS=0
166 HOST_ST_UNKNOW="Unknow"
167 HOST_ST_ON="On"
168 HOST_ST_OFF="Off"
169 declare -a PORT_MAP=(0 3 2 1 7 6 5)
170
171 # check /dev/mem
172 if ! create_dev_mem; then
173 return 1
174 fi
175
176 CHECK_CNT=0
177 MDIO_ERR_CNT=0
178 CUR_HOST_ST=$HOST_ST_UNKNOW
179
180 while true
181 do
182 if POST_ST_VAL=$(mdio-util c22 r $MDIO_BUS "${PORT_MAP[SLED_NUM]}" 0); then
183 if [ $((POST_ST_VAL&16#0800)) -eq $((16#0800)) ]; then
184 TMP_HOST_ST="$HOST_ST_ON"
185 else
186 TMP_HOST_ST="$HOST_ST_OFF"
187 fi
188
189 if [ "$CUR_HOST_ST" == "$TMP_HOST_ST" ]; then
190 CHECK_CNT=$((CHECK_CNT+1))
191 else
192 CUR_HOST_ST=$TMP_HOST_ST
193 CHECK_CNT=0
194 fi
195
196 if [ "$CHECK_CNT" -ge 5 ]; then
197 echo "$CUR_HOST_ST"
198 break
199 fi
200 else
201 MDIO_ERR_CNT=$((MDIO_ERR_CNT+1))
202 if [ "$MDIO_ERR_CNT" -ge 5 ]; then
203 echo "$HOST_ST_UNKNOW"
204 return 1
205 fi
206 fi
207 done
208
209 return 0
210}
211
212function create_dev_mem()
213{
214 CHECK_CNT=0
215 while true
216 do
217 CHECK_CNT=$((CHECK_CNT+1))
218 if [ -c /dev/mem ]; then
219 # /dev/mem already exist
220 return 0
221 elif mknod /dev/mem c 1 1; then
222 # mknod success
223 return 0
224 elif [ "$CHECK_CNT" -ge 5 ]; then
225 break
226 fi
227 sleep 1
228 done
229
230 echo "create /dev/mem failed"
231 return 1
232}
233
Allen.Wang4a0948d2021-12-15 13:41:18 +0800234function show_usage(){
Allen.Wang5ff992e2022-01-04 21:00:38 +0800235 echo "Usage: power-ctrl [sled1 | sled2 | sled3 | sled4 | sled5 | sled6] [on off ac-on ac-off status]"
Allen.Wang4a0948d2021-12-15 13:41:18 +0800236 echo " power-ctrl chassis-cycle"
237}
238
239if [ $# -eq 1 ]; then
240 if [ "$1" = "chassis-cycle" ];then
241 echo "chassis cycle...."
242 i2cset -y -f 12 0x11 0xd9 c
243 exit 0
244 else
245 echo "Invalid argument: [ $1 ]"
246 show_usage
247 exit 1;
248 fi
249fi
250
251if [ $# -gt 2 ]; then
252 echo "Too many arguments"
253 show_usage
254 exit 1;
255fi
256
Allen.Wang5ff992e2022-01-04 21:00:38 +0800257if [[ "$1" =~ ^(sled[1-6]{1})$ ]]; then
Allen.Wang4a0948d2021-12-15 13:41:18 +0800258 SLED=$1
259 ACTION=$2
260 SLED_NUM=${SLED:4}
261else
262 echo "invalid sled name: ${1}"
263 show_usage
264 exit 1;
265fi
266
267#Check if sled is present
268SLED_PRESENT=$(get_gpio "presence-sled${SLED_NUM}")
269if [ "$SLED_PRESENT" != 0 ];then
270 echo "${SLED} is not present!"
271 exit 1
272fi
273
274if [[ "$ACTION" == "on" ]]; then
Allen.Wang787c9c62022-03-02 14:55:03 +0800275 if [ "$(get_ac_status "$SLED_NUM")" == "AC on" ];then
Potin Laidc251662022-04-01 11:16:50 +0800276 host_power_st=$(get_host_status "$SLED_NUM")
277 if [ "$host_power_st" == "On" ]; then
278 echo "${SLED} is already On"
279 elif [ "$host_power_st" == "Off" ]; then
280 echo "Power on ${SLED}"
281 trigger_power_button "$SLED_NUM" "$DELAY_POWER_ON"
282 else
283 echo "${SLED} power state is $host_power_st"
284 exit 1
285 fi
Allen.Wang787c9c62022-03-02 14:55:03 +0800286 else
287 echo "${SLED} is ac-off, please turn on ac before power on"
288 exit 1
289 fi
Allen.Wang4a0948d2021-12-15 13:41:18 +0800290elif [[ "$ACTION" == "off" ]];then
Allen.Wang787c9c62022-03-02 14:55:03 +0800291 if [ "$(get_ac_status "$SLED_NUM")" == "AC on" ];then
Potin Laidc251662022-04-01 11:16:50 +0800292 host_power_st=$(get_host_status "$SLED_NUM")
293 if [ "$host_power_st" == "On" ]; then
294 echo "Power off ${SLED}"
295 trigger_power_button "$SLED_NUM" "$DELAY_POWER_OFF"
296 elif [ "$host_power_st" == "Off" ]; then
297 echo "${SLED} is already Off"
298 else
299 echo "${SLED} power state is $host_power_st"
300 exit 1
301 fi
Allen.Wang787c9c62022-03-02 14:55:03 +0800302 else
303 echo "${SLED} is already ac-off"
304 exit 1
305 fi
Allen.Wang4a0948d2021-12-15 13:41:18 +0800306elif [[ "$ACTION" == "status" ]];then
Potin Laidc251662022-04-01 11:16:50 +0800307 AC_ST=$(get_ac_status "$SLED_NUM")
308 if [ "$AC_ST" == "AC on" ]; then
309 # check host power status if AC is on
310 get_host_status "$SLED_NUM" || exit 1
311 else
312 # AC off
313 echo "$AC_ST"
314 fi
Allen.Wang4a0948d2021-12-15 13:41:18 +0800315elif [[ "$ACTION" == "ac-on" ]];then
316 set_gpio "power-host${SLED_NUM}" 1
317elif [[ "$ACTION" == "ac-off" ]];then
318 set_gpio "power-host${SLED_NUM}" 0
Potin Laidb5648e2022-02-16 00:57:55 +0800319elif [[ "$ACTION" == "dfu" ]]; then
320 echo "Set host$SLED_NUM DFU mode"
321
Allen.Wang787c9c62022-03-02 14:55:03 +0800322 # turn ac off, and hold for 25 seconds
Potin Laidb5648e2022-02-16 00:57:55 +0800323 echo "SLED$SLED_NUM: turn ac-off"
324 set_gpio "power-host${SLED_NUM}" 0
Allen.Wang787c9c62022-03-02 14:55:03 +0800325 sleep 25
Potin Laidb5648e2022-02-16 00:57:55 +0800326
Allen.Wang787c9c62022-03-02 14:55:03 +0800327 # press power button
Potin Laidb5648e2022-02-16 00:57:55 +0800328 echo "SLED$SLED_NUM: pressing power button"
329 if ! press_power_button "$SLED_NUM"; then
330 echo "SLED$SLED_NUM: press power button failed"
331 echo "SLED$SLED_NUM: releasing power button"
332 release_power_button "$SLED_NUM"
333 exit 1
334 fi
Allen.Wang787c9c62022-03-02 14:55:03 +0800335 sleep 1
Potin Laidb5648e2022-02-16 00:57:55 +0800336
337 # turn ac on
338 echo "SLED$SLED_NUM: turn ac-on"
339 set_gpio "power-host${SLED_NUM}" 1
340 sleep 3
341
342 # release power button
343 echo "SLED$SLED_NUM: releasing host power button"
344 if ! release_power_button "$SLED_NUM"; then
345 echo "SLED$SLED_NUM: release power button failed"
346 exit 1
347 fi
Allen.Wang4a0948d2021-12-15 13:41:18 +0800348else
349 echo "Unknown action: [ ${ACTION} ]"
350 show_usage
351fi