meta-facebook: yosemite4: Disable/Enable I3C Hub when AC OFF/ON

There is a leakage current on P1V2_STBY (channel 2) and P3V3_STBY (channel 1) when the slot is AC-OFF
This leakage originates from the I3C Hub.

To address this issue,  modified the process as follows:
- Before turning off AC, disable the I3C Hub.
- After turning on AC, enable the I3C Hub.

Tested:
1. Disabled the slot’s corresponding I3C port before AC-OFF - pass
2. Enabled the slot’s corresponding I3C port after confirming AC-ON - pass
3. No longer observed leakage current after AC-OFF - pass

Change-Id: I72f0e0b5245f9dd427546b3f14c700d7fba30f5d
Signed-off-by: Eric Yang <eric.yang.wiwynn@gmail.com>
diff --git a/meta-facebook/meta-yosemite4/recipes-phosphor/state/phosphor-state-manager/chassis-powercycle b/meta-facebook/meta-yosemite4/recipes-phosphor/state/phosphor-state-manager/chassis-powercycle
index c7bb965..4b63347 100644
--- a/meta-facebook/meta-yosemite4/recipes-phosphor/state/phosphor-state-manager/chassis-powercycle
+++ b/meta-facebook/meta-yosemite4/recipes-phosphor/state/phosphor-state-manager/chassis-powercycle
@@ -28,6 +28,99 @@
 
 is_nuvoton_board="$(check_nuvoton_board)"
 
+enable_i3c_hub()
+{
+    local target_slot="$CHASSIS_ID"
+    local hub_path
+    local offset_file
+    local access_file
+    local current_port
+    local mask=1
+
+    echo "Do 12V cycle enable i3c-hub"
+    if [ "$target_slot" -lt 5 ]; then
+        hub_path="/sys/kernel/debug/i3c-hub-0-*/reg"
+        echo "Slot${target_slot} on i3c hub 0."
+    else
+        hub_path="/sys/kernel/debug/i3c-hub-1-*/reg"
+        echo "Slot${target_slot} on i3c hub 1."
+    fi
+
+    for file in $hub_path/{offset,access}; do
+        if [[ $file == *"/offset" ]]; then
+            offset_file="$file"
+        elif [[ $file == *"/access" ]]; then
+            access_file="$file"
+        fi
+    done
+
+    echo "Unlock i3c hub register."
+    echo 16 > "$offset_file"
+    echo 105 > "$access_file"
+
+    echo "Enable slot${target_slot} i3c port."
+    echo 18 > "$offset_file"
+    current_port=$(cat "$access_file")
+    if [ "$target_slot" -gt 4 ]; then
+        ((target_slot=target_slot-4))
+    fi
+    mask=$((mask << (--target_slot)))
+    current_port=$((current_port | mask))
+    echo $current_port > "$access_file"
+    cat "$access_file"
+
+    echo "Lock i3c hub register."
+    echo 16 > "$offset_file"
+    echo 0 > "$access_file"
+}
+
+disable_i3c_hub()
+{
+    local target_slot="$CHASSIS_ID"
+    local hub_path
+    local offset_file
+    local access_file
+    local current_port
+    local mask=1
+
+    echo "Do 12V cycle disable i3c hub"
+    if [ "$target_slot" -lt 5 ]; then
+        hub_path="/sys/kernel/debug/i3c-hub-0-*/reg"
+        echo "Slot$target_slot on i3c hub 0."
+    else
+        hub_path="/sys/kernel/debug/i3c-hub-1-*/reg"
+        echo "Slot$target_slot on i3c hub 1."
+    fi
+
+    for file in $hub_path/{offset,access}; do
+        if [[ $file == *"/offset" ]]; then
+            offset_file="$file"
+        elif [[ $file == *"/access" ]]; then
+            access_file="$file"
+        fi
+    done
+
+    echo "Unlock i3c hub register."
+    echo 16 > "$offset_file"
+    echo 105 > "$access_file"
+
+    echo "Disable slot${target_slot} i3c port."
+    echo 18 > "$offset_file"
+    current_port=$(cat "$access_file")
+    if [ "$target_slot" -gt 4 ]; then
+        ((target_slot=target_slot-4))
+    fi
+    mask=$((mask << (--target_slot)))
+    mask=$((~mask))
+    current_port=$((current_port & mask))
+    echo $current_port > "$access_file"
+    cat "$access_file"
+
+    echo "Lock i3c hub register."
+    echo 16 > "$offset_file"
+    echo 0 > "$access_file"
+}
+
 chassis-power-cycle()
 {
     CHASSIS_ID=$1
@@ -45,6 +138,7 @@
                     echo "Failed to inform management CPLD that slot$1 is 12V off"
                 fi
             fi
+            disable_i3c_hub
             sleep 1
             if ! gpio_set "$GPIOCHIP_IO_EXP_SLOT_PWR_CTRL" "$IO_EXP_SLOT_PWR_CTRL"=1
             then
@@ -74,6 +168,7 @@
                     echo "Failed to inform management CPLD that slot$1 is 12V on"
                 fi
             fi
+            enable_i3c_hub
             busctl set-property "$CHASSIS_BUS_NAME""$CHASSIS_ID" "$CHASSIS_OBJ_PATH""$CHASSIS_ID" "$CHASSIS_INTF_NAME" "$CHASSIS_PROPERTY_NAME" s "$CHASSIS_ON_PROPERTY"
             /usr/libexec/phosphor-state-manager/wait-until-mctp-connection-done "$CHASSIS_ID" && systemctl restart "phosphor-discover-system-state@$CHASSIS_ID.service"
             msg="Chassis$CHASSIS_ID cycle success"
diff --git a/meta-facebook/meta-yosemite4/recipes-phosphor/state/phosphor-state-manager/chassis-poweroff b/meta-facebook/meta-yosemite4/recipes-phosphor/state/phosphor-state-manager/chassis-poweroff
index 17afa6d..c92be7c 100644
--- a/meta-facebook/meta-yosemite4/recipes-phosphor/state/phosphor-state-manager/chassis-poweroff
+++ b/meta-facebook/meta-yosemite4/recipes-phosphor/state/phosphor-state-manager/chassis-poweroff
@@ -22,9 +22,57 @@
 
 is_nuvoton_board="$(check_nuvoton_board)"
 
+disable_i3c_hub()
+{
+    local target_slot="$CHASSIS_ID"
+    local hub_path
+    local offset_file
+    local access_file
+    local current_port
+    local mask=1
+
+    echo "Do 12V off disable i3c hub"
+    if [ "$target_slot" -lt 5 ]; then
+        hub_path="/sys/kernel/debug/i3c-hub-0-*/reg"
+        echo "Slot$target_slot on i3c hub 0."
+    else
+        hub_path="/sys/kernel/debug/i3c-hub-1-*/reg"
+        echo "Slot$target_slot on i3c hub 1."
+    fi
+
+    for file in $hub_path/{offset,access}; do
+        if [[ $file == *"/offset" ]]; then
+            offset_file="$file"
+        elif [[ $file == *"/access" ]]; then
+            access_file="$file"
+        fi
+    done
+
+    echo "Unlock i3c hub register."
+    echo 16 > "$offset_file"
+    echo 105 > "$access_file"
+
+    echo "Disable slot${target_slot} i3c port."
+    echo 18 > "$offset_file"
+    current_port=$(cat "$access_file")
+    if [ "$target_slot" -gt 4 ]; then
+        ((target_slot=target_slot-4))
+    fi
+    mask=$((mask << (--target_slot)))
+    mask=$((~mask))
+    current_port=$((current_port & mask))
+    echo $current_port > "$access_file"
+    cat "$access_file"
+
+    echo "Lock i3c hub register."
+    echo 16 > "$offset_file"
+    echo 0 > "$access_file"
+}
+
 # Server 12v power off
 chassis-power-off()
 {
+        disable_i3c_hub
         if ! gpio_set "$GPIOCHIP_IO_EXP_SLOT_PWR_CTRL" "$IO_EXP_SLOT_PWR_CTRL"=1
         then
                 msg="Failed to set slot$CHASSIS_ID power off"
diff --git a/meta-facebook/meta-yosemite4/recipes-phosphor/state/phosphor-state-manager/chassis-poweron b/meta-facebook/meta-yosemite4/recipes-phosphor/state/phosphor-state-manager/chassis-poweron
index 620c0fc..fb4b3a9 100644
--- a/meta-facebook/meta-yosemite4/recipes-phosphor/state/phosphor-state-manager/chassis-poweron
+++ b/meta-facebook/meta-yosemite4/recipes-phosphor/state/phosphor-state-manager/chassis-poweron
@@ -20,6 +20,53 @@
 
 is_nuvoton_board="$(check_nuvoton_board)"
 
+enable_i3c_hub()
+{
+    local target_slot="$CHASSIS_ID"
+    local hub_path
+    local offset_file
+    local access_file
+    local current_port
+    local mask=1
+
+    echo "Do 12V on enable i3c-hub"
+    sleep 1
+    if [ "$target_slot" -lt 5 ]; then
+        hub_path="/sys/kernel/debug/i3c-hub-0-*/reg"
+        echo "Slot${target_slot} on i3c hub 0."
+    else
+        hub_path="/sys/kernel/debug/i3c-hub-1-*/reg"
+        echo "Slot${target_slot} on i3c hub 1."
+    fi
+
+    for file in $hub_path/{offset,access}; do
+        if [[ $file == *"/offset" ]]; then
+            offset_file="$file"
+        elif [[ $file == *"/access" ]]; then
+            access_file="$file"
+        fi
+    done
+
+    echo "Unlock i3c hub register."
+    echo 16 > "$offset_file"
+    echo 105 > "$access_file"
+
+    echo "Enable slot${target_slot} i3c port."
+    echo 18 > "$offset_file"
+    current_port=$(cat "$access_file")
+    if [ "$target_slot" -gt 4 ]; then
+        ((target_slot=target_slot-4))
+    fi
+    mask=$((mask << (--target_slot)))
+    current_port=$((current_port | mask))
+    echo $current_port > "$access_file"
+    cat "$access_file"
+
+    echo "Lock i3c hub register."
+    echo 16 > "$offset_file"
+    echo 0 > "$access_file"
+}
+
 # Server 12v power on
 chassis-power-on()
 {
@@ -43,6 +90,7 @@
                 echo "Failed to inform management CPLD that slot$1 is 12V on"
             fi
         fi
+        enable_i3c_hub
         busctl set-property "$CHASSIS_BUS_NAME""$CHASSIS_ID" "$CHASSIS_OBJ_PATH""$CHASSIS_ID" "$CHASSIS_INTF_NAME" "$CHASSIS_PROPERTY_NAME" s "$CHASSIS_ON_PROPERTY"
         echo "Chassis$CHASSIS_ID is power on"
     else