Samuel Jiang | a9607d7 | 2019-05-23 14:59:35 +0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | U2_PRESENT=( 148 149 150 151 152 153 154 155 ) |
| 4 | POWER_U2=( 195 196 202 199 198 197 127 126 ) |
| 5 | PWRGD_U2=( 161 162 163 164 165 166 167 168 ) |
| 6 | RST_BMC_U2=( 72 73 74 75 76 77 78 79 ) |
| 7 | PLUGGED=0 |
| 8 | I2C_BUS=8 |
| 9 | CHIP_ADDR=0x68 |
| 10 | CLOCK_GEN_VALUE=$(i2cget -y $I2C_BUS $CHIP_ADDR 0 i 2|cut -f3 -d' ') |
| 11 | |
| 12 | function set_gpio_direction() |
| 13 | { |
| 14 | #$1 gpio pin, $2 'in','high','low' |
| 15 | echo $2 > /sys/class/gpio/gpio$1/direction |
| 16 | } |
| 17 | |
| 18 | function read_gpio_input() |
| 19 | { |
| 20 | #$1 read input gpio pin |
| 21 | echo $(cat /sys/class/gpio/gpio$1/value) |
| 22 | } |
| 23 | |
| 24 | function enable_nvme_power() |
| 25 | { |
| 26 | set_gpio_direction "${POWER_U2[$1]}" "high" |
| 27 | sleep 0.04 |
| 28 | check_powergood $1 |
| 29 | } |
| 30 | |
| 31 | function 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 | |
| 43 | function 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 | |
| 52 | function 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 | } |