summaryrefslogtreecommitdiff
path: root/meta-quanta/meta-gsj/recipes-gsj/quanta-nvme-powerctrl/files/nvme_powerctrl_library.sh
blob: 3737aacc104aa5e1e5fd5c22efe2b266a21add64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/bash

U2_PRESENT=( 148 149 150 151 152 153 154 155 )
POWER_U2=( 195 196 202 199 198 197 127 126 )
PWRGD_U2=( 161 162 163 164 165 166 167 168 )
RST_BMC_U2=( 72 73 74 75 76 77 78 79 )
PLUGGED=0
I2C_BUS=8
CHIP_ADDR=0x68
CLOCK_GEN_VALUE=$(i2cget -y $I2C_BUS $CHIP_ADDR 0 i 2|cut -f3 -d' ')

function set_gpio_direction()
{
    #$1 gpio pin, $2 'in','high','low'
    echo $2 > /sys/class/gpio/gpio$1/direction
}

function read_gpio_input()
{
    #$1 read input gpio pin
    echo $(cat /sys/class/gpio/gpio$1/value)
}

function enable_nvme_power()
{
    set_gpio_direction "${POWER_U2[$1]}" "high"
    sleep 0.04
    check_powergood $1
}

function check_powergood()
{
    if [ $(read_gpio_input ${PWRGD_U2[$1]}) == 1 ];then
        sleep 0.005
        update_clock_gen_chip_register $1 1
        sleep 0.1
        set_gpio_direction "${RST_BMC_U2[$1]}" "high"
    else
        disable_nvme_power $1
    fi
}

function disable_nvme_power()
{
    set_gpio_direction "${RST_BMC_U2[$1]}" "low"
    sleep 0.1
    update_clock_gen_chip_register $1 0
    sleep 0.005
    set_gpio_direction "${POWER_U2[$1]}" "low"
}

function update_clock_gen_chip_register(){
    #$1 nvme slot number, $2 enable/disable
    update_value=$(printf '%x\n' "$((0x01 <<$1))")
    if [ $2 -eq 1 ];then
        CLOCK_GEN_VALUE=$(printf '0x%x\n' \
        "$(($CLOCK_GEN_VALUE | 0x$update_value))")
    else
        CLOCK_GEN_VALUE=$(printf '0x%x\n' \
        "$(($CLOCK_GEN_VALUE & ~0x$update_value))")
    fi
    i2cset -y $I2C_BUS $CHIP_ADDR 0 $CLOCK_GEN_VALUE s
}