blob: 3737aacc104aa5e1e5fd5c22efe2b266a21add64 [file] [log] [blame]
Samuel Jianga9607d72019-05-23 14:59:35 +08001#!/bin/bash
2
3U2_PRESENT=( 148 149 150 151 152 153 154 155 )
4POWER_U2=( 195 196 202 199 198 197 127 126 )
5PWRGD_U2=( 161 162 163 164 165 166 167 168 )
6RST_BMC_U2=( 72 73 74 75 76 77 78 79 )
7PLUGGED=0
8I2C_BUS=8
9CHIP_ADDR=0x68
10CLOCK_GEN_VALUE=$(i2cget -y $I2C_BUS $CHIP_ADDR 0 i 2|cut -f3 -d' ')
11
12function set_gpio_direction()
13{
14 #$1 gpio pin, $2 'in','high','low'
15 echo $2 > /sys/class/gpio/gpio$1/direction
16}
17
18function read_gpio_input()
19{
20 #$1 read input gpio pin
21 echo $(cat /sys/class/gpio/gpio$1/value)
22}
23
24function enable_nvme_power()
25{
26 set_gpio_direction "${POWER_U2[$1]}" "high"
27 sleep 0.04
28 check_powergood $1
29}
30
31function check_powergood()
32{
33 if [ $(read_gpio_input ${PWRGD_U2[$1]}) == 1 ];then
34 sleep 0.005
35 update_clock_gen_chip_register $1 1
36 sleep 0.1
37 set_gpio_direction "${RST_BMC_U2[$1]}" "high"
38 else
39 disable_nvme_power $1
40 fi
41}
42
43function disable_nvme_power()
44{
45 set_gpio_direction "${RST_BMC_U2[$1]}" "low"
46 sleep 0.1
47 update_clock_gen_chip_register $1 0
48 sleep 0.005
49 set_gpio_direction "${POWER_U2[$1]}" "low"
50}
51
52function update_clock_gen_chip_register(){
53 #$1 nvme slot number, $2 enable/disable
54 update_value=$(printf '%x\n' "$((0x01 <<$1))")
55 if [ $2 -eq 1 ];then
56 CLOCK_GEN_VALUE=$(printf '0x%x\n' \
57 "$(($CLOCK_GEN_VALUE | 0x$update_value))")
58 else
59 CLOCK_GEN_VALUE=$(printf '0x%x\n' \
60 "$(($CLOCK_GEN_VALUE & ~0x$update_value))")
61 fi
62 i2cset -y $I2C_BUS $CHIP_ADDR 0 $CLOCK_GEN_VALUE s
63}