#!/bin/sh
#
# Copyright 2018-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

# shellcheck disable=SC1091
. /usr/local/bin/openbmc-utils.sh

rebind_i2c_dev() {
  dev="$1-00$2"
  dri=$3

  if [ ! -L "${SYSFS_I2C_DEVICES}/$dev/driver" ]; then
    if i2c_bind_driver "$dri" "$dev" >/dev/null; then
      echo "rebind $dev to driver $dri successfully"
    fi
  fi
}

vr_sku=$(($(/usr/bin/kv get mb_sku) & 0x03))  #0x00 : RAA,
                                              #0x01 : INF,
                                              #0x10 : MPS,

MB_1ST_SOURCE="0"
MB_2ND_SOURCE="1"
MB_3RD_SOURCE="2"
MB_4TH_SOURCE="3"

MB_VR_SECOND="1"
MB_VR_THIRD="2"

MB_ADC_MAIN="0"
#MB ADM128D
if [ "$(gpio_get FM_BOARD_BMC_SKU_ID1)" -eq "$MB_ADC_MAIN" ]; then
  i2cset -f -y 20 0x1d 0x0b 0x02
  i2c_device_add 20 0x1d adc128d818
  kv set mb_adc_source "$MB_1ST_SOURCE"
else
  i2c_device_add 20 0x35 max11617
  kv set mb_adc_source "$MB_2ND_SOURCE"
fi

# MB CPU VR Device"
if [ "$vr_sku" -eq "$MB_VR_THIRD" ]; then
  i2c_device_add 20 0x60 mp2971
  i2c_device_add 20 0x61 mp2971
  i2c_device_add 20 0x63 mp2971
  i2c_device_add 20 0x72 mp2971
  i2c_device_add 20 0x74 mp2971
  i2c_device_add 20 0x76 mp2971
  kv set mb_vr_source "$MB_3RD_SOURCE"
elif [ "$vr_sku" -eq "$MB_VR_SECOND"  ]; then
  i2c_device_add 20 0x60 xdpe152c4
  i2c_device_add 20 0x62 xdpe152c4
  i2c_device_add 20 0x64 xdpe152c4
  i2c_device_add 20 0x72 xdpe152c4
  i2c_device_add 20 0x74 xdpe152c4
  i2c_device_add 20 0x76 xdpe152c4
  kv set mb_vr_source "$MB_2ND_SOURCE"
else
  i2c_device_add 20 0x60 isl69260
  i2c_device_add 20 0x61 isl69260
  i2c_device_add 20 0x63 isl69260
  i2c_device_add 20 0x72 isl69260
  i2c_device_add 20 0x74 isl69260
  i2c_device_add 20 0x76 isl69260
  kv set mb_vr_source "$MB_1ST_SOURCE"
fi

# MB HSC Device"
mb_hsc=$(($(gpio_get FM_BOARD_BMC_SKU_ID3) << 1 |
          $(gpio_get FM_BOARD_BMC_SKU_ID2)))

#MB_HSC_MAIN="0"   # mp5990
MB_HSC_SECOND="1"  # ltc4282/ltc4286
MB_HSC_THIRD="2"   # rs31380
mbrev=$(kv get mb_rev)
MB_DVT_BORAD_ID="3"

#kv set mb_hsc_module "0"
if [ "$mb_hsc" -eq "$MB_HSC_THIRD" ]; then
  kv set mb_hsc_source "$MB_4TH_SOURCE"
elif [ "$mb_hsc" -eq "$MB_HSC_SECOND" ]; then
  i2c_device_add 21 0x4C lm75
  i2c_device_add 2 0x51 24c64
  #Read ADC 1.5v:LTC4286 1v:LTC4282
  #DVT ADC INA230; EVT ADC ISL28022
  val=$(i2cget -f -y 21 0x42 0x02)
  data=$(("$val"))

  if [ "$mbrev" -ge "$MB_DVT_BORAD_ID" ]
  then
    if [ "$data" -ge 4 ]
    then
      i2c_device_add 2 0x41 ltc4286
      kv set mb_hsc_source "$MB_3RD_SOURCE"
    else
      i2c_device_add 2 0x41 ltc4282
      kv set mb_hsc_source "$MB_2ND_SOURCE"
    fi
  else
    if [ "$data" -gt 4 ]
    then
      i2c_device_add 2 0x41 ltc4286
      kv set mb_hsc_source "$MB_3RD_SOURCE"
    else
      i2c_device_add 2 0x41 ltc4282
      kv set mb_hsc_source "$MB_2ND_SOURCE"
    fi
  fi
  kv set mb_hsc_module "1"
else
  i2c_device_add 2 0x20 mp5990
  kv set mb_hsc_source "$MB_1ST_SOURCE"
fi

#MB DPM
mbrev=$(kv get mb_rev)
MB_DVT_BOARD_ID="3"

if [ "$mbrev" -ge "$MB_DVT_BOARD_ID" ]; then
  i2c_device_add 34 0x41 ina230
  i2c_device_add 34 0x42 ina230
  i2c_device_add 34 0x43 ina230
  i2c_device_add 34 0x44 ina230
  i2c_device_add 34 0x45 ina230
  kv set mb_dpm_source "$MB_1ST_SOURCE"
fi


#!/bin/bash
#
# Copyright 2022-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          gpio-setup
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description:  Set up GPIO pins as appropriate
### END INIT INFO

# This file contains definitions for the GPIO pins that were not otherwise
# defined in other files.  We should probably move some more of the
# definitions to this file at some point.

. /usr/local/fbpackages/utils/ast-functions

# ==GPIOA==

# ==GPIOB==
# FM_ME_BT_DONE
#gpio_export FM_ME_BT_DONE GPIOB0

# FM_BMC_MUX_CS_SPI_SEL_0
gpio_export FM_BMC_MUX_CS_SPI_SEL_0 GPIOB4
gpio_set FM_BMC_MUX_CS_SPI_SEL_0 0

# FM_ID_LED_N
gpio_export FM_ID_LED_N GPIOB5
gpio_set FM_ID_LED_N 0

# FM_ESPI_PLD_EN
gpio_export FM_ESPI_PLD_EN GPIOB6

# ==GPIOC==
# RST_PLTRST_R_N
gpio_export RST_PLTRST_R_N GPIOC0

#RST_SGPIO_RESET_BMC_N
gpio_export RST_SGPIO_RESET_BMC_N GPIOC3
gpio_set RST_SGPIO_RESET_BMC_N 1

#H_CPU_CATERR_LVC2_R2_N
gpio_export H_CPU_CATERR_LVC2_R2_N GPIOC5

#FM_JTAG_BMC_MUX_SEL
gpio_export FM_JTAG_BMC_MUX_SEL GPIOC6
gpio_set FM_JTAG_BMC_MUX_SEL 1;

#IRQ_SGPIO_BMC_N
gpio_export IRQ_SGPIO_BMC_N GPIOC7

# ==GPIOD==
gpio_export FM_BIC_DEBUG_SW_N GPIOD2
gpio_set FM_BIC_DEBUG_SW_N 0

# ==GPIOE==

# ==GPIOF==

# ==GPIOG==

# ==GPIOH==

# ==GPIOI==
#FLASH_WP_STATUS_R1
gpio_export FLASH_WP_STATUS_R1 GPIOI5

# ==GPIOJ==

# ==GPIOK==

# ==GPIOL==
# FM_DBP_CPU_PREQ_GF_N
gpio_export FM_DBP_CPU_PREQ_GF_N GPIOL4
gpio_set FM_DBP_CPU_PREQ_GF_N 1

# ==GPIOM==
# FM_VIRTUAL_RESEAT_BMC
gpio_export FM_VIRTUAL_RESEAT_BMC GPIOM1
gpio_set FM_VIRTUAL_RESEAT_BMC 0

# PWRGD_PSU_AC_PWROK_BMC
gpio_export PWRGD_PSU_AC_PWROK_BMC GPIOM2

# FM_DBP_BMC_PRDY_N
gpio_export FM_DBP_BMC_PRDY_N GPIOM5

# ==GPION==
# LED_POSTCODE_0
gpio_export LED_POSTCODE_0 GPION0

# LED_POSTCODE_1
gpio_export LED_POSTCODE_1 GPION1

# LED_POSTCODE_2
gpio_export LED_POSTCODE_2 GPION2

# LED_POSTCODE_3
gpio_export LED_POSTCODE_3 GPION3

# LED_POSTCODE_4
gpio_export LED_POSTCODE_4 GPION4

# LED_POSTCODE_5
gpio_export LED_POSTCODE_5 GPION5

# LED_POSTCODE_6
gpio_export LED_POSTCODE_6 GPION6

# LED_POSTCODE_7
gpio_export LED_POSTCODE_7 GPION7

# ==GPIOO==
# FM_PCH_TPM_PRSNT
gpio_export FM_BIOS_TPM_PRSNT_IN GPIOO5

# ==GPIOP==
# SYS_BMC_PWRBTN_IN
gpio_export SYS_BMC_PWRBTN_IN GPIOP0

# SYS_BMC_PWRBTN_OUT
gpio_export SYS_BMC_PWRBTN_OUT GPIOP1
gpio_set SYS_BMC_PWRBTN_OUT 1

# SYS_BMC_RSTBTN_IN
gpio_export ID_RST_BTN_BMC_IN GPIOP2

# BMC_PWR_LED
gpio_export BMC_PWR_LED GPIOP4
gpio_set BMC_PWR_LED 0

# ==GPIOQ==
# IRQ_PCH_TPM_SPI_IN
gpio_export IRQ_PCH_TPM_SPI_IN GPIOQ0

# USB_OC0_REAR_R_IN
gpio_export USB_OC0_REAR_R_IN GPIOQ1

# FM_HDD_LED_OUT
gpio_export FM_HDD_LED_N_OUT GPIOQ4
gpio_set FM_HDD_LED_N_OUT 0

# FM_P12V_HSC_ENABLE_R1
gpio_export FM_P12V_HSC_ENABLE_R1 GPIOQ5
gpio_set FM_P12V_HSC_ENABLE_R1 1

# FM_PCH_BEEP_LED
gpio_export FM_PCH_BEEP_LED_OUT GPIOQ6
gpio_set FM_PCH_BEEP_LED_OUT 0

# FM_SOL_UART_CH_SEL
gpio_export FM_SOL_UART_CH_SEL GPIOQ7

# ==GPIOR==

# ==GPIOS==
# FM_THERMTRIP_N
gpio_export FM_THERMTRIP_N GPIOS2

# FM_BMC_TPM_PRSNT
gpio_export FM_BMC_TPM_PRSNT GPIOS3

# FM_BMC_DEBUG_SW_N
gpio_export FM_BMC_DEBUG_SW_N GPIOS4

# ==GPIU==
# BSM_PRSNT_R_N
gpio_export BSM_PRSNT_R_N GPIOU4

# ==GPIOV==
# RST_RSMRST_R_N
gpio_export RST_RSMRST_R_N GPIOV1

gpio_export BMC_ID_BEEP_SEL_R1 GPIOV2
#gpio_set BMC_ID_BEEP_SEL_R1 1

# BATTERY_DETECT
gpio_export BATTERY_DETECT GPIOV4
gpio_set BATTERY_DETECT 0

# IRQ_BMC_TPM_SPI_IN
gpio_export IRQ_BMC_TPM_SPI_IN GPIOV7

# ==GPIX==
# IRQ_SMI_ACTIVE_BMC_N
gpio_export IRQ_SMI_ACTIVE_BMC_N GPIOX2

# ==GPIOY==
gpio_export RST_WDTRST_PLD_R1_N GPIOY1

#FM_FLASH_LATCH_N_R1
gpio_export FM_FLASH_LATCH_N_R1 GPIOY2

#FM BMC_EMMC_RST_R_N
gpio_export BMC_EMMC_RST_R_N GPIOY3

# ==GPIOZ==
# BMC_READY_N
#gpio_export BMC_READY_N GPIOZ0
#gpio_set BMC_READY_N 0

#FM_DEBUG_PORT_PRSNT_R1_N
gpio_export FM_DEBUG_PORT_PRSNT_N_IN GPIOZ6

# ==GPIO18D==
# EMMC Func GPIO18D0 - GPIO18D7
gpio1v8_export AC_PWR_BMC_BTN_N 0

devmem_set_bit 0x1e7800e0 8
devmem_clear_bit 0x1e7000e4 8

# ==SGPIO Input Common==
sgpio_export GPU_FPGA_READY_ISO_R 8

sgpio_export FM_SYS_THROTTLE_R_N 20
sgpio_export HPDB_HSC_PWRGD_ISO_R 24
sgpio_export FM_UV_ADR_TRIGGER_R_N 26

sgpio_export FM_SWB_BIC_READY_ISO_R_N 32

sgpio_export GPU_FPGA_THERM_OVERT_ISO_R_N 40
sgpio_export GPU_FPGA_OVERT_ISO_R_N 42
sgpio_export BIC_MONITER_ISO_R 46

sgpio_export GPU_HMC_PRSNT_ISO_R_N 52
sgpio_export GPU_PRSNT_N_ISO_R 54

sgpio_export FM_GPU_PWRGD_ISO_R 60
sgpio_export RST_BMC_FROM_SWB_ISO_R_N 62
sgpio_export GPU_BASE_HMC_READY_ISO_R 64

sgpio_export E1S_0_P3V3_ADC_ALERT 96
sgpio_export P12V_SCM_ADC_ALERT 98

sgpio_export M2_0_P3V3_ADC_ALERT 100
sgpio_export OCP_V3_2_P3V3_ADC_ALERT 102
sgpio_export OCP_SFF_P3V3_ADC_ALERT 104
sgpio_export FM_UARTSW_LSB_N 106
sgpio_export FM_UARTSW_MSB_N 108

sgpio_export FM_THERMAL_ALERT_R_N 110

sgpio_export PCIE_M2_SSD0_PRSNT_N 190

sgpio_export FM_SMB_2_ALERT_GPU_ISO_N 208
sgpio_export FM_SMB_1_ALERT_GPU_ISO_N 210

sgpio_export FM_BOARD_BMC_SKU_ID4 212
sgpio_export FM_BOARD_BMC_SKU_ID3 214
sgpio_export FM_BOARD_BMC_SKU_ID2 216
sgpio_export FM_BOARD_BMC_SKU_ID1 218
sgpio_export FM_BOARD_BMC_SKU_ID0 220

sgpio_export FAB_BMC_REV_ID2 222
sgpio_export FAB_BMC_REV_ID1 224
sgpio_export FAB_BMC_REV_ID0 226

kv set mb_rev "$(($(gpio_get FAB_BMC_REV_ID2)<<2 |
                  $(gpio_get FAB_BMC_REV_ID1)<<1 |
                  $(gpio_get FAB_BMC_REV_ID0)))"

kv set mb_sku "$(($(gpio_get FM_BOARD_BMC_SKU_ID4)<<4 |
                  $(gpio_get FM_BOARD_BMC_SKU_ID3)<<3 |
                  $(gpio_get FM_BOARD_BMC_SKU_ID2)<<2 |
                  $(gpio_get FM_BOARD_BMC_SKU_ID1)<<1 |
                  $(gpio_get FM_BOARD_BMC_SKU_ID0)))"

# == SGPIO OUT Common==
sgpio_export IRQ_PCH_SCI_WHEA_R_N 11
gpio_set IRQ_PCH_SCI_WHEA_R_N 1

sgpio_export FM_SPD_REMOTE_EN_R 17
gpio_set FM_SPD_REMOTE_EN_R 0

sgpio_export RST_BMC_RSTBTN_OUT_R_N 21
gpio_set RST_BMC_RSTBTN_OUT_R_N 1

sgpio_export BMC_READY_N 23
gpio_set BMC_READY_N 1

sgpio_export FM_BMC_SGPIO_READY_N 37

sgpio_export GPU_NVS_PWR_BRAKE_R_N 43
gpio_set GPU_NVS_PWR_BRAKE_R_N 1

sgpio_export GPU_WP_HW_CTRL_R_N 45
gpio_set GPU_WP_HW_CTRL_R_N 0

sgpio_export BMC_MONITER 51
gpio_set BMC_MONITER 0

sgpio_export FM_BIOS_TPM_PRSNT_OUT 57
gpio_set FM_BIOS_TPM_PRSNT_OUT "$(gpio_get FM_BIOS_TPM_PRSNT_IN)"


#!/bin/bash
#
# Copyright 2022-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          gpio-cover
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Set up GPIO pins as appropriate can be cover
### END INIT INFO

. /usr/local/fbpackages/utils/ast-functions

# ==SGPIO Input Cover==
sgpio_export FM_OCP_SFF_PWR_GOOD_R 0
sgpio_export IRQ_SML1_PMBUS_PLD_ALERT_N 2
sgpio_export PWRGD_PVNN_MAIN_CPU0 4
sgpio_export PWRGD_PVPP_HBM_CPU1 6

sgpio_export FM_ADR_ACK_R 10
sgpio_export IRQ_PVCCD_CPU0_VRHOT_LVC3_N 12
sgpio_export FM_DIS_PS_PWROK_DLY 14
sgpio_export IRQ_PSYS_CRIT_N 16
sgpio_export IRQ_PVCCD_CPU1_VRHOT_LVC3_N 18

sgpio_export FM_PCH_PLD_GLB_RST_WARN_N 22

sgpio_export FM_PCH_BMC_THERMTRIP_N 28
sgpio_export FM_PCHHOT_R_N 30

sgpio_export FM_PCH_PRSNT_N 34
sgpio_export IRQ_HSC_FAULT_N 36
sgpio_export FM_OCP_V3_2_PWR_GOOD_R 38

sgpio_export PWRGD_PVNN_PCH_AUX 44

sgpio_export PWRGD_PVNN_MAIN_CPU1 48
sgpio_export PWRGD_PVPP_HBM_CPU0 50

sgpio_export PWRGD_P3V3 56
sgpio_export PWRGD_PVCCFA_EHV_FIVRA_CPU0 58

sgpio_export PWRGD_PVCCD_HV_CPU0 66
sgpio_export PWRGD_PVCCIN_CPU0 68
sgpio_export PWRGD_P3V3_AUX_PLD 70
sgpio_export PWRGD_PVCCINFAON_CPU0 72
sgpio_export PWRGD_PVCCIN_CPU1 74
sgpio_export PWRGD_PS_PWROK_PLD_R 76
sgpio_export PWRGD_PVCCD_HV_CPU1 78
sgpio_export PWRGD_PVCCFA_EHV_FIVRA_CPU1 80
sgpio_export PWRGD_PVCCINFAON_CPU1 82
sgpio_export PWRGD_PVCCFA_EHV_CPU1 84
sgpio_export PWRGD_PVCCFA_EHV_CPU0 86
sgpio_export PWRGD_P1V05_PCH_AUX 88
sgpio_export PWRGD_P1V8_PCH_AUX_PLD 90
sgpio_export PWRGD_SYS_PWROK_R 92
sgpio_export PWRGD_PCH_PWROK_R 94

sgpio_export FM_CPU0_SKTOCC_LVT3_PLD_N 112
sgpio_export FM_CPU1_SKTOCC_LVT3_PLD_N 114
sgpio_export PWRGD_CPUPWRGD_LVC2_R1 116
sgpio_export H_CPU1_THERMTRIP_LVC1_N 118
sgpio_export IRQ_CPU1_VRHOT_N 120
sgpio_export H_CPU0_MEMTRIP_LVC1_N 122
sgpio_export H_CPU1_MEMHOT_OUT_LVC1_N 124
sgpio_export PWRGD_FAIL_CPU1_CD_R 126
sgpio_export PWRGD_FAIL_CPU1_GH_R 128
sgpio_export PWRGD_FAIL_CPU0_EF_R 130
sgpio_export PWRGD_FAIL_CPU0_AB_R 132
sgpio_export PWRGD_FAIL_CPU0_CD_R 134
sgpio_export H_CPU0_THERMTRIP_LVC1_N 136
sgpio_export H_CPU0_MEMHOT_OUT_LVC1_N 138
sgpio_export H_CPU_ERR2_LVC2_N 140
sgpio_export H_CPU_ERR1_LVC2_N 142
sgpio_export H_CPU_ERR0_LVC2_N 144
sgpio_export FM_BIOS_POST_CMPLT_BMC_N 146
sgpio_export FM_ME_BT_DONE 148
sgpio_export PWRGD_FAIL_CPU1_AB_R 150
sgpio_export PWRGD_FAIL_CPU1_EF_R 152
sgpio_export IRQ_CPU0_VRHOT_N 154
sgpio_export PWRGD_DSW_PWROK_R3 156
sgpio_export H_CPU1_MEMTRIP_LVC1_N 158
sgpio_export FM_ADR_COMPLETE 160
sgpio_export FM_BMC_DBP_PRESENT_R_N 162
sgpio_export FM_SLPS3_PLD_N 164
sgpio_export FM_SLPS4_PLD_N 166
sgpio_export FM_ADR_TRIGGER_N 168
sgpio_export FM_REMOTE_DEBUG_DET 170
sgpio_export E1S_0_PRSNT_N 172
sgpio_export IRQ_PCH_CPU_NMI_EVENT_N 174
sgpio_export FM_PS_EN_PLD_R 176
sgpio_export IRQ_OC_DETECT_N  178
sgpio_export PWRGD_FAIL_CPU0_GH_R 180
sgpio_export H_CPU_RMCA_LVC2_R2_N 182
#sgpio_export H_CPU_CATERR_LVC2_R2_N 184
sgpio_export H_CPU1_PROCHOT_LVC1_N 186
sgpio_export IRQ_UV_DETECT_N 188

sgpio_export FM_ME_AUTHN_FAIL 192
sgpio_export PRSNT_SWB_R_N 194
sgpio_export IRQ_PSU_ALERT_R_N 196
sgpio_export FM_THROTTLE_R_N 198
sgpio_export RST_PLTRST_PLD_N 200
sgpio_export H_CPU0_PROCHOT_LVC1_N 202
sgpio_export FM_PCH_BEEP_LED 204
sgpio_export FM_HDD_LED_N 206

sgpio_export CPLD_POWER_FAIL_ALERT 240

sgpio_export CPLD_SGPIO_READY_ID0 248
sgpio_export CPLD_SGPIO_READY_ID1 250
sgpio_export CPLD_SGPIO_READY_ID2 252
sgpio_export CPLD_SGPIO_READY_ID3 254

# == SGPIO OUT Cover==
sgpio_export FM_ESPI_PLD_R1_EN 1
gpio_set FM_ESPI_PLD_R1_EN 1

sgpio_export IRQ_BMC_CPU_NMI 3
gpio_set IRQ_BMC_CPU_NMI 0

sgpio_export RST_PCA9548_SENSOR_R_N 5
gpio_set RST_PCA9548_SENSOR_R_N 1

sgpio_export FM_BMC_CRASHLOG_TRIG_R1_N 7
gpio_set FM_BMC_CRASHLOG_TRIG_R1_N 1

sgpio_export FM_DEBUG_PORT_PRSNT_N_OUT 9
gpio_set FM_DEBUG_PORT_PRSNT_N_OUT "$(gpio_get FM_DEBUG_PORT_PRSNT_N_IN)"

sgpio_export FM_PECI_SEL_R_N 13
gpio_set FM_PECI_SEL_R_N 1

#sgpio_export FM_JTAG_BMC_MUX_SEL 15
#gpio_set FM_JTAG_BMC_MUX_SEL 0

sgpio_export FM_ASD_ENABLE 19
gpio_set FM_ASD_ENABLE 1

sgpio_export TEST_SGPIO_EVENT_LOG 25
gpio_set TEST_SGPIO_EVENT_LOG 0

sgpio_export IRQ_BMC_PCH_NMI_N 27
gpio_set IRQ_BMC_PCH_NMI_N 1

sgpio_export BMC_FORCE_NM_THROTTLE_N 29
gpio_set BMC_FORCE_NM_THROTTLE_N 1

sgpio_export NV_DUMP_END 31
gpio_set NV_DUMP_END 0

#sgpio_export RST_RSMRST_R_N 33

sgpio_export FM_BMC_ONCTL_R_N 35
gpio_set FM_BMC_ONCTL_R_N 1

#sgpio_export FM_BMC_SUSACK_R1_N 39

sgpio_export FM_BIOS_DEBUG_EN_R_N 41
gpio_set FM_BIOS_DEBUG_EN_R_N 1

sgpio_export CLK_GEN2_BMC_RC_OE_N 47
gpio_set CLK_GEN2_BMC_RC_OE_N 0

sgpio_export IRQ_SML0_ALERT_R_N 49
gpio_set IRQ_SML0_ALERT_R_N 1

sgpio_export FM_PFR_DSW_PWROK_N 53
gpio_set FM_PFR_DSW_PWROK_N 1

sgpio_export FM_PFR_OVR_RTC_R 55
gpio_set FM_PFR_OVR_RTC_R 0

sgpio_export IRQ_PCH_TPM_SPI_OUT 61
gpio_set IRQ_PCH_TPM_SPI_OUT 1

sgpio_export USB_OC0_REAR_R_OUT 63
gpio_set USB_OC0_REAR_R_OUT 1

sgpio_export GPU_FPGA_RST_N 65
gpio_set GPU_FPGA_RST_N 0

sgpio_export FM_JTAG_TCK_MUX_BMC_SEL 67
gpio_set FM_JTAG_TCK_MUX_BMC_SEL 0

sgpio_export CLK_BUF1_GPU_FPGA_OE_R_N 69
gpio_set CLK_BUF1_GPU_FPGA_OE_R_N 0

sgpio_export FM_BMC_CPU_FBRK_OUT_N 71
gpio_set FM_BMC_CPU_FBRK_OUT_N 1


#!/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

# shellcheck disable=SC1091
. /usr/local/bin/openbmc-utils.sh

VPDB_EVT2_REV_ID="2"

if [ "$(kv get vpdb_rev)" -ge $VPDB_EVT2_REV_ID ]; then
  ln -s /etc/sensors_cfg/evt2.conf /etc/sensors.d/rev.conf
else
  ln -s /etc/sensors_cfg/evt.conf /etc/sensors.d/rev.conf
fi

#VPDB ADM1272 config
for i in {1..5}
do
  rev=$(fruid-util vpdb 2>/dev/null |grep "Board Part Number" | awk -F ":" '{print $2}' | awk '{gsub(/^ +| +$/,"")}1')
  rc=$?

  if [ "$rc" == 1 ] || [ -z "$rev" ]; then
    sleep 1
  elif [ "$rev" == "35F0TPB0000" ]; then
    ln -s /etc/sensors_cfg/vpdb-adm1272-1.conf /etc/sensors.d/vpdb_adm1272.conf
    break
  elif [ "$rev" == "35F0TPB0020" ] || [ "$rev" == "35F0TPB0040" ]; then
    ln -s /etc/sensors_cfg/vpdb-adm1272-2.conf /etc/sensors.d/vpdb_adm1272.conf
    break
  fi

  if [ $i -eq 5 ]; then
    ln -s /etc/sensors_cfg/vpdb-adm1272-1.conf /etc/sensors.d/vpdb_adm1272.conf
  fi
done

#HPDB ADM1272 config
for i in {1..5}
  do
  rev=$(fruid-util hpdb 2>/dev/null |grep "Board Part Number" | awk -F ":" '{print $2}' | awk '{gsub(/^ +| +$/,"")}1')
  rc=$?

  if [ "$rc" == 1 ] || [ -z "$rev" ]; then
    sleep 1
  elif [ "$rev" == "35F0TPB0060" ]; then
    ln -s /etc/sensors_cfg/hpdb-adm1272-2.conf /etc/sensors.d/hpdb_adm1272.conf
    break
  elif [ "$rev" == "35F0TPB0010" ] || [ "$rev" == "35F0TPB0090" ]; then
    ln -s /etc/sensors_cfg/hpdb-adm1272-1.conf /etc/sensors.d/hpdb_adm1272.conf
    break
  fi

  if [ $i -eq 5 ]; then
    ln -s /etc/sensors_cfg/hpdb-adm1272-1.conf /etc/sensors.d/hpdb_adm1272.conf
  fi
done


#!/bin/sh
#
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA

### BEGIN INIT INFO
# Provides:          setup-por
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Power on Server
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

# shellcheck disable=SC1091
. /usr/local/fbpackages/utils/ast-functions

DEF_PWR_ON=1
TO_PWR_ON=

check_por_config() {
  TO_PWR_ON=0

  if POR=$(kv get "server_por_cfg" persistent); then
    if [ "$POR" = "lps" ]; then
      if LS=$(kv get "pwr_server_last_state" persistent); then
        if [ "$LS" = "on" ]; then
          TO_PWR_ON=1;
        fi
      else
        TO_PWR_ON=$DEF_PWR_ON
      fi
    elif [ "$POR" = "on" ]; then
      TO_PWR_ON=1;
    fi
  else
    TO_PWR_ON=$DEF_PWR_ON
  fi
}

mb_server_power_on() {
  echo $TO_PWR_ON
  if [ "$(is_bmc_por)" -eq 1 ]; then
    check_por_config
    if [ $TO_PWR_ON -eq 1 ]; then
      power-util mb on
    fi
  fi
}

mb_server_power_on


#!/bin/bash
#
# Copyright 2022-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

. /usr/local/fbpackages/utils/ast-functions

gpio_set FM_BMC_SGPIO_READY_N 0

cat /tmp/gpionames/FM_BMC_SGPIO_READY_N/value


#!/bin/bash

connect_uart2_4() {
  echo -ne "uart2" > /sys/devices/platform/ahb/ahb:apb/1e789000.lpc/1e789098.uart_routing/uart4
  echo -ne "uart4" > /sys/devices/platform/ahb/ahb:apb/1e789000.lpc/1e789098.uart_routing/uart2
}

connect_uart2_4


#!/bin/bash
#
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA

### BEGIN INIT INFO
# Provides:          setup-por
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Power on Server
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

#MB_HSC_MODULE="1"  # ltc4282/ltc4286
if kv get mb_hsc_module >/dev/null; then
  sed -i "2{s/$/ hsc/}" /etc/sv/sensord/run
fi

#SWB_HSC_MODULE="1"  # ltc4282/ltc4286
if kv get swb_hsc_module >/dev/null; then
  sed -i "2{s/$/ swb_hsc/}" /etc/sv/sensord/run
fi

kv set mb_polling_status      1
kv set swb_polling_status     1
kv set hgx_polling_status     1
kv set nic0_polling_status    1
kv set nic1_polling_status    1
kv set scm_polling_status     1
kv set vpdb_polling_status    1
kv set hpdb_polling_status,   1
kv set fan_bp1_polling_status 1
kv set fan_bp2_polling_status 1
kv set hsc_polling_status     1
kv set swb_hsc_polling_status 1


#!/bin/sh
#
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA

. /usr/local/bin/openbmc-utils.sh
. /usr/local/fbpackages/utils/ast-functions

setup_hgx_eeprom () {
  if [ ! -L "/sys/bus/i2c/drivers/at24/9-0053" ];
  then
    i2c_device_add 9 0x53 24c64
    sleep 1
    dd if=/sys/class/i2c-dev/i2c-9/device/9-0053/eeprom of=/tmp/fruid_hgx.bin bs=512 count=1
  fi
}

ifconfig usb0 192.168.31.2 netmask 255.255.0.0
kv set is_usbnet_ready 1
setup_hgx_eeprom


#!/bin/bash
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

PATH=/sbin:/bin:/usr/sbin:/usr/bin

# shellcheck disable=SC1091
. /usr/local/fbpackages/utils/ast-functions

# Sync BMC's date with the server
sync_date()
{
  sleep 1
  # if no ntp peer has been declared the system peer
  if ! /usr/sbin/ntpq -p | grep '^\*' > /dev/null ; then
    echo Syncing up BMC time with server...
    # Use standard IPMI command 'get-sel-time' to read RTC time
    for i in {1..10};
    do
    #Master
      output=$(/usr/local/bin/ipmb-util 6 0x2c 0x28 0x48)
      if [ ${#output} == 12 ]
      then
        break;
      fi

      if [ ${i} == 10 ]
      then
        exit
      fi
      usleep 300
    done

    sys_date=$(date +%s)
    set_date=$((16#$(echo "$output" | awk '{print $4$3$2$1}')))

    if [ "$sys_date" -ge $set_date ]; then
      echo "Use BMC date"
      set_date="$sys_date"
    fi

    date -s @"$set_date"
    test -x /etc/init.d/hwclock.sh && /etc/init.d/hwclock.sh stop
  fi
}

sync_date


#!/bin/sh
exec /usr/local/bin/pldmd --bus 3


#!/bin/sh
exec /usr/local/bin/pldmd --bus 4


#! /bin/sh
#
# Copyright 2022-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

echo -n "Starting PLDMD Rx/Tx Daemon.."

runsv /etc/sv/pldmd_3 > /dev/null 2>&1 &
runsv /etc/sv/pldmd_4 > /dev/null 2>&1 &

echo "done."


#!/bin/sh
### BEGIN INIT INFO
# Provides:          hwclock
# Required-Start:
# Required-Stop:     $local_fs
# Default-Start:     S
# Default-Stop:      0 6
# Short-Description: Set system clock
# Description:       Set system clock to hardware clock, according to the UTC
#                    setting in /etc/default/rcS (see also rcS(5)).
### END INIT INFO
#
# WARNING:      If your hardware clock is not in UTC/GMT, this script
#               must know the local time zone. This information is
#               stored in /etc/localtime. This might be a problem if
#               your /etc/localtime is a symlink to something in
#               /usr/share/zoneinfo AND /usr isn't in the root
#               partition! The workaround is to define TZ either
#               in /etc/default/rcS, or in the proper place below.

[ ! -x /sbin/hwclock ] && exit 0

[ -f /etc/default/rcS ] && . /etc/default/rcS

[ "$UTC" = "yes" ] && tz="--utc" || tz="--localtime"
case "$1" in
        start)
                if [ "$VERBOSE" != no ]
                then
                        echo "System time was `date`."
                        echo "Setting the System Clock using the Hardware Clock as reference..."
                fi

		if [ "$HWCLOCKACCESS" != no ]
		then
			if [ -z "$TZ" ]
			then
	      hwclock $tz -f /dev/rtc1 --hctosys
			else
			   TZ="$TZ" hwclock $tz -f /dev/rtc1 --hctosys
			fi

      hwclock $tz -f /dev/rtc1 --systohc
		fi

                if [ "$VERBOSE" != no ]
                then
                        echo "System Clock set. System local time is now `date`."
                fi
                ;;
        stop|restart|reload|force-reload)
		#
		# Updates the Hardware Clock with the System Clock time.
		# This will *override* any changes made to the Hardware Clock.
		#
		# WARNING: If you disable this, any changes to the system
		#          clock will not be carried across reboots.
		#
		if [ "$VERBOSE" != no ]
		then
			echo "Saving the System Clock time to the Hardware Clock..."
		fi
		if [ "$HWCLOCKACCESS" != no ]
		then
			hwclock -f /dev/rtc1 $tz --systohc
		fi
		if [ "$VERBOSE" != no ]
		then
			echo "Hardware Clock updated to `date`."
		fi
                exit 0
                ;;
	show)
		if [ "$HWCLOCKACCESS" != no ]
		then
			hwclock -f /dev/rtc1 $tz --show
		fi
		;;
        *)
                echo "Usage: hwclock.sh {start|stop|show|reload|restart}" >&2
		echo "       start sets kernel (system) clock from hardware (RTC) clock" >&2
		echo "       stop and reload set hardware (RTC) clock from kernel (system) clock" >&2
                exit 1
                ;;
esac


#!/bin/bash

. /usr/local/fbpackages/utils/ast-functions

WDT2_CTRL_REG=0x1E78502C

# Disable watchdog-2
devmem_clear_bit $WDT2_CTRL_REG 0

exit 0


#!/bin/bash

KV_CMD=/usr/bin/kv

i2c_scan9=$(i2cdetect -y 9)
i2c_scan10=$(i2cdetect -y 10)

# Scan the i2cbus 9 and 10 and check for PLX switch. If they do not exist, the PCie used is PMC
if ([[ $i2c_scan9 == *"5d"* ]] && [[ $i2c_scan9 == *"5e"* ]] && [[ $i2c_scan9 == *"5f"* ]] \
  && [[ $i2c_scan10 == *"5d"* ]] && [[ $i2c_scan10 == *"5e"* ]] && [[ $i2c_scan10 == *"5f"* ]])
then
  ${KV_CMD} set "pcie_switch_vendor" "PLX"
  echo -e "PLX switch selected"
else
  ${KV_CMD} set "pcie_switch_vendor" "PMC"
  echo -e "PMC switch selected"
fi

exit 0


#!/bin/sh
#
# Copyright 2004-present Facebook. All rights reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

usage() {
    echo "Displays values onto the debug header LEDs."
    echo "Hex and decimal accepted."
    echo "Usage: $0 <value>"
}

. /usr/local/fbpackages/utils/ast-functions

# Function to set the less significant hex digit
display_lower() {
    local bit0=$(expr $1 % 2)
    local bit1=$(expr $1 / 2 % 2)
    local bit2=$(expr $1 / 4 % 2)
    local bit3=$(expr $1 / 8 % 2)

    # Set the pins to the correct operating mode.
    # The relevant pins are GPIOJ[0...3].
    # For GPIO bank J, SCU84[8..11] must be 0.
    devmem_clear_bit $(scu_addr 84) 8
    devmem_clear_bit $(scu_addr 84) 9
    devmem_clear_bit $(scu_addr 84) 10
    devmem_clear_bit $(scu_addr 84) 11

    # Now set the GPIOs to the right binary values
    gpio_set 72 $bit0
    gpio_set 73 $bit1
    gpio_set 74 $bit2
    gpio_set 75 $bit3
}

# Function to set the more significant hex digit
display_upper() {
    local bit0=$(expr $1 % 2)
    local bit1=$(expr $1 / 2 % 2)
    local bit2=$(expr $1 / 4 % 2)
    local bit3=$(expr $1 / 8 % 2)

    # Set the pins to the correct operating mode.
    # The relevant pins are GPIOG[0...3].
    # For GPIO bank G, SCU84[0..3] must be 0.
    devmem_clear_bit $(scu_addr 84) 0
    devmem_clear_bit $(scu_addr 84) 1
    devmem_clear_bit $(scu_addr 84) 2
    devmem_clear_bit $(scu_addr 84) 3

    gpio_set 48 $bit0
    gpio_set 49 $bit1
    gpio_set 50 $bit2
    gpio_set 51 $bit3
}

# Check number of parameters
if [ $# -ne 1 ]
then
    usage
    exit 1
fi

# Make sure input is actually numeric
DEC_VALUE=$(printf "%d" $1 2>/dev/null)
if [ $? -eq 1 ]
then
    echo "Unable to parse input as numeric value."
    exit 1
fi

# Make sure input is within proper range
if [ $DEC_VALUE -lt 0 ] || [ $DEC_VALUE -gt 255 ]
then
    echo "Value $DEC_VALUE is outside of displayable range 0 - 0xff (255)."
    exit 1
fi

# Get upper/lower decimal values
LOWER_DEC_VALUE=$(expr $DEC_VALUE % 16)
UPPER_DEC_VALUE=$(expr $DEC_VALUE / 16)

# Display the results
display_lower $LOWER_DEC_VALUE
display_upper $UPPER_DEC_VALUE


#!/bin/bash
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

usage() {
    echo "Usage: $1 <slot#> <on | off>"
    exit -1
}

. /usr/local/fbpackages/utils/ast-functions

PATH=/sbin:/bin:/usr/sbin:/usr/bin

set -e

if [ $# != 2 ]; then
    usage $0
fi

# Slot#1: GPIOM1(97),Slot#2: GPIOM0(96),Slot#3: GPIOM3(99),Slot#4: GPIOM2(98)
if [ $1 = "1" ]; then
    gpio=M1
elif [ $1 = "2" ]; then
    gpio=M0
elif [ $1 = "3" ]; then
    gpio=M3
elif [ $1 = "4" ]; then
    gpio=M2
else
    usage $0
fi

if [ $2 = "on" ]; then
    val=1
elif [ $2 = "off" ]; then
    val=0
else
    usage $0
fi

gpio_set $gpio $val


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

# setup ADC channels

ADC_PATH="/sys/devices/platform/ast_adc.0"
# channel 0 (ADC0_12V)     :  r1:  5.6K; r2:  1.0K; v2: 0mv
# channel 1 (ADC_P5V_STBY) :  r1:  3.3K; r2:  1.0K; v2: 0mv
# channel 2 (ADC_P3V3_STBY):  r1:  1.0K; r2:  1.0K; v2: 0mv

config_adc() {
    channel=$1
    r1=$2
    r2=$3
    v2=$4
    echo $r1 > ${ADC_PATH}/adc${channel}_r1
    echo $r2 > ${ADC_PATH}/adc${channel}_r2
    echo $v2 > ${ADC_PATH}/adc${channel}_v2
    echo 1 > ${ADC_PATH}/adc${channel}_en
}

config_adc 0 5600 1000 0
config_adc 1 3300 1000 0
config_adc 2 1000 1000 0


#!/bin/bash

. /usr/local/fbpackages/utils/ast-functions
KV_CMD=/usr/bin/kv

MUX_BASE=0x08
I2C_7_MUX_ADDR=0x71
I2C_8_MUX_ADDR=0x72
I2C_M2_MUX_ADDR=0x73
I2C_M2CARD_AMB_ADDR=0x4c
M2_CHANNEL0=0x01

M2_MUX_ADDR=$(echo $I2C_M2_MUX_ADDR | cut -c3-4)
M2CARD_AMB_ADDR=$(echo $I2C_M2CARD_AMB_ADDR | cut -c3-4)

for (( i=0; i<7; i++ ))
do
        NUM=$(($MUX_BASE + $i))
        MUX=$(printf "0x%x" $NUM)

        i2c_set7=$(i2cset -y 7 $I2C_7_MUX_ADDR $MUX 2>&1)
        i2c_get7=$(i2cget -y 7 $I2C_7_MUX_ADDR 2>&1)
        # if i2c command fail, reset ssd mux by enable GPIOQ6
        if ([[ $i2c_set7 == *Error* || $i2c_get7 == *Error* ]])
        then
                gpio_set Q6 0
                sleep 1
                gpio_set Q6 1
                if ([[ $i == 0 ]])
                then
                        break
                fi
                continue
        fi

        i2c_scan7=$(i2cdetect -y 7)
        if ([[ $i2c_scan7 == *$M2_MUX_ADDR* ]])
        then
                i2cset -y 7 $I2C_M2_MUX_ADDR $M2_CHANNEL0
                i2c_scan7=$(i2cdetect -y 7)
                if ([[ $i2c_scan7 == *$M2CARD_AMB_ADDR* ]])
                then
                        ${KV_CMD} set "ssd_sku_info" "M2"
                        exit 0
                fi
        fi
done

for (( i=0; i<8; i++ ))
do
        NUM=$(($MUX_BASE + $i))
        MUX=$(printf "0x%x" $NUM)

        i2c_set8=$(i2cset -y 8 $I2C_8_MUX_ADDR $MUX 2>&1)
        i2c_get8=$(i2cget -y 8 $I2C_8_MUX_ADDR 2>&1)
        # if i2c command fail, reset ssd mux by enable GPIOQ6
        if ([[ $i2c_set8 == *Error* || $i2c_get8 == *Error* ]])
        then
                gpio_set Q6 0
                sleep 1
                gpio_set Q6 1
                if ([[ $i == 0 ]])
                then
                        break
                fi
                continue
        fi

        i2c_scan8=$(i2cdetect -y 8)
        if ([[ $i2c_scan8 == *$M2_MUX_ADDR* ]])
        then
                i2cset -y 8 $I2C_M2_MUX_ADDR $M2_CHANNEL0
                i2c_scan8=$(i2cdetect -y 8)
                if ([[ $i2c_scan8 == *$M2CARD_AMB_ADDR* ]])
                then
                        ${KV_CMD} set "ssd_sku_info" "M2"
                        exit 0
                fi
        fi
done

${KV_CMD} set "ssd_sku_info" "U2"

exit 0


#!/bin/bash

. /usr/local/fbpackages/utils/ast-functions
KV_CMD=/usr/bin/kv

MUX_BASE=0x08
I2C_7_MUX_ADDR=0x71
I2C_8_MUX_ADDR=0x72
I2C_M2_MUX_ADDR=0x73
I2C_NVME_INTF_ADDR=0x6a
I2C_VID_ADDR=0x09
M2_CHANNEL0=0x01

INTEL=0x8680
SEAGATE=0xb11b
SAMSUNG=0x4d14
TOSHIBA=0x7911
WD=0x961b

M2_MUX_ADDR=$(echo $I2C_M2_MUX_ADDR | cut -c3-4)

for (( i=0; i<7; i++ ))
do
        NUM=$((MUX_BASE + i))
        MUX=$(printf "0x%x" $NUM)

        i2c_set7=$(i2cset -y 7 $I2C_7_MUX_ADDR "$MUX" 2>&1)
        i2c_get7=$(i2cget -y 7 $I2C_7_MUX_ADDR 2>&1)
        # if i2c command fail, reset ssd mux by enable GPIOQ6
        if [[ $i2c_set7 == *Error* || $i2c_get7 == *Error* ]]
        then
                gpio_set Q6 0
                sleep 1
                gpio_set Q6 1
                if [[ $i == 0 ]]
                then
                        break
                fi
                continue
        fi

        i2c_scan7=$(i2cdetect -y 7)
        if [[ $i2c_scan7 == *$M2_MUX_ADDR* ]]
        then
                i2cset -y 7 $I2C_M2_MUX_ADDR $M2_CHANNEL0
        fi
        VID=$(i2cget -y 7 $I2C_NVME_INTF_ADDR $I2C_VID_ADDR w)
        if [[ $VID == *$INTEL* ]]
        then
            ${KV_CMD} set "ssd_vendor" "intel"
            exit 0
        elif [[ $VID == *$SEAGATE* ]]
        then
            ${KV_CMD} set "ssd_vendor" "seagate"
            exit 0
        elif [[ $VID == *$SAMSUNG* ]]
        then
            ${KV_CMD} set "ssd_vendor" "samsung"
            exit 0
        elif [[ $VID == *$TOSHIBA* ]]
        then
            ${KV_CMD} set "ssd_vendor" "toshiba"
            exit 0
        elif [[ $VID == *$WD* ]]
        then
            ${KV_CMD} set "ssd_vendor" "wd"
            exit 0
        fi

done

for (( i=0; i<8; i++ ))
do
        NUM=$((MUX_BASE + i))
        MUX=$(printf "0x%x" $NUM)

        i2c_set8=$(i2cset -y 8 $I2C_8_MUX_ADDR "$MUX" 2>&1)
        i2c_get8=$(i2cget -y 8 $I2C_8_MUX_ADDR 2>&1)
        # if i2c command fail, reset ssd mux by enable GPIOQ6
        if [[ $i2c_set8 == *Error* || $i2c_get8 == *Error* ]]
        then
                gpio_set Q6 0
                sleep 1
                gpio_set Q6 1
                if [[ $i == 0 ]]
                then
                        break
                fi
                continue
        fi

        i2c_scan8=$(i2cdetect -y 8)
        if [[ $i2c_scan8 == *$M2_MUX_ADDR* ]]
        then
                i2cset -y 8 $I2C_M2_MUX_ADDR $M2_CHANNEL0
        fi
        VID=$(i2cget -y 8 $I2C_NVME_INTF_ADDR $I2C_VID_ADDR w)
        if [[ $VID == *$INTEL* ]]
        then
            ${KV_CMD} set "ssd_vendor" "intel"
            exit 0
        elif [[ $VID == *$SEAGATE* ]]
        then
            ${KV_CMD} set "ssd_vendor" "seagate"
            exit 0
        elif [[ $VID == *$SAMSUNG* ]]
        then
            ${KV_CMD} set "ssd_vendor" "samsung"
            exit 0
        elif [[ $VID == *$TOSHIBA* ]]
        then
            ${KV_CMD} set "ssd_vendor" "toshiba"
            exit 0
        elif [[ $VID == *$WD* ]]
        then
            ${KV_CMD} set "ssd_vendor" "wd"
            exit 0
        fi
done

${KV_CMD} set "ssd_vendor" "unknown"

exit 0


#!/bin/sh
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
### BEGIN INIT INFO
# Provides:          setup-front-paneld
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Start front panel control daemon
### END INIT INFO

echo -n "Setup Front Panel Daemon.."
runsv /etc/sv/front-paneld > /dev/null 2>&1 &
echo "done."


#!/bin/sh
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#
PWM_DIR=/sys/devices/platform/ast_pwm_tacho.0
TACH_BUS=5
TACH_ADDR=0x2d
TACH_REGISTER_L=0x24
TACH_REGISTER_H=0x25

set -e

# The PWM frequency is

#   clk_source / ((2 ^ division_) * (2 * division_l) * (unit + 1))
#
# Our clk_source is 24Mhz.  4-pin fans are generally supposed to be driven with
# a 25Khz PWM control signal.  Therefore we want the divisor to equal 960.
#
# We also want the unit to be as large as possible, since this controls the
# granularity with which we can modulate the PWM signal.  The following
# settings allow us to set the fan from 0 to 100% in increments of 1/96th.
#
# The AST chip supports 3 different PWM clock configurations, but we only use
# type M for now.
echo 0 > $PWM_DIR/pwm_type_m_division_h
echo 5 > $PWM_DIR/pwm_type_m_division_l
echo 95 > $PWM_DIR/pwm_type_m_unit

# On Lightning, there are 6 fans which are driven by Tacho0.
# Each fan uses same PWM input and provide one tacho output.
# Here is the mapping between the fan and PWN/Tacho,
# staring from the one from the edge
# Fan 0: PWM 0, Tacho0

# For each fan, setting the type, and 100% initially
echo 0 > $PWM_DIR/pwm0_type
echo 0 > $PWM_DIR/pwm0_rising
echo 0 > $PWM_DIR/pwm0_falling
echo 1 > $PWM_DIR/pwm0_en

# Enable Tach 0
i2cset -y $TACH_BUS $TACH_ADDR $TACH_REGISTER_L 0xFF b
i2cset -y $TACH_BUS $TACH_ADDR $TACH_REGISTER_H 0x0F b


#!/bin/sh
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          setup-fan
# Required-Start:    board-id
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Set fan speed
### END INIT INFO

. /usr/local/fbpackages/utils/ast-functions
KV_CMD=/usr/bin/kv

# Get SSD type and vender_device ID from cache
default_fsc_config_path="/etc/fsc-config.json"

# get from cache
flash_type=$(${KV_CMD} get "ssd_sku_info")
ssd_vendor=$(${KV_CMD} get "ssd_vendor")

case "$flash_type" in
   "U2")
        case "$ssd_vendor" in
           "intel") cp /etc/FSC_Lightning_PVT_Intel_U2_4TB_v3_config.json ${default_fsc_config_path}
           ;;
           "samsung") cp /etc/FSC_Lightning_PVT_Samsung_U2_4TB_v3_config.json ${default_fsc_config_path}
           ;;
           *) echo "Using default fan table - Unexpected U.2 SSD vendor."
              cp /etc/FSC_Lightning_PVT_Intel_U2_4TB_v3_config.json ${default_fsc_config_path}
           ;;
        esac
   ;;
   "M2")
        case "$ssd_vendor" in
           "seagate") cp /etc/FSC_Lightning_PVT_Seagate_M2_2TB_v4_config.json ${default_fsc_config_path}
           ;;
           "samsung") cp /etc/FSC_Lightning_PVT_Samsung_M2_2TB_v4_config.json ${default_fsc_config_path}
           ;;
           "toshiba") cp /etc/FSC_Lightning_PVT_Toshiba_M2_2TB_v1_config.json ${default_fsc_config_path}
           ;;
           *) echo "Using default fan table - Unexpected M.2 SSD vendor."
              cp /etc/FSC_Lightning_PVT_Seagate_M2_2TB_v4_config.json ${default_fsc_config_path}
           ;;
         esac
   ;;
   *) echo "Enter into transitional mode - Unexpected flash type!"
      /usr/local/bin/init_pwm.sh
      /usr/local/bin/fan-util --set 70
      exit 1
   ;;
esac

echo -n "Setup fan speed... "
/usr/local/bin/init_pwm.sh
/usr/local/bin/fan-util --set 50
runsv /etc/sv/fscd > /dev/null 2>&1 &
echo "done."


#!/bin/sh
#
# Copyright 2016-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

modprobe lm75
modprobe pmbus

# Enable the ADC inputs;  adc0 - adc7 are connected to various voltage sensors

echo 1 > /sys/devices/platform/ast_adc.0/adc0_en
echo 1 > /sys/devices/platform/ast_adc.0/adc1_en
echo 1 > /sys/devices/platform/ast_adc.0/adc2_en
echo 1 > /sys/devices/platform/ast_adc.0/adc3_en
echo 1 > /sys/devices/platform/ast_adc.0/adc4_en
echo 1 > /sys/devices/platform/ast_adc.0/adc5_en
echo 1 > /sys/devices/platform/ast_adc.0/adc6_en
echo 1 > /sys/devices/platform/ast_adc.0/adc7_en

I2C_BUS_FCB=5

I2C_BUS_PEB=4

# NCT7904 is the fan controller on the FCB board
I2C_ADDR_FCB_NCT7904=0x2d

# ADM1276 is the HSC on FCB board
I2C_ADDR_FCB_HSC=0x22

# PCA9551 is the LED controller on the FCB board
I2C_ADDR_FCB_LED=0x60

# ADM1278 is the HSC on PEB board
I2C_ADDR_PEB_HSC=0x11

# Enable the Pins to sense the Temperature VSEN23_MD and VSEN45_MD
i2cset -y $I2C_BUS_FCB $I2C_ADDR_FCB_NCT7904 0x2e 0x5 b

# Enable the Power IN averaging setting
i2cset -y $I2C_BUS_FCB $I2C_ADDR_FCB_HSC 0xd4 0xaf b

# Light on FCB LED(turn blue)
i2cset -y $I2C_BUS_FCB $I2C_ADDR_FCB_LED 0x05 0x00  #LED 0~3
i2cset -y $I2C_BUS_FCB $I2C_ADDR_FCB_LED 0x06 0x00  #LED 4~5

# Initialize the 7904 monitor_flag register
i2cset -y $I2C_BUS_FCB $I2C_ADDR_FCB_NCT7904 0xba 0x00

# Enable the Power IN averaging setting
i2cset -y $I2C_BUS_PEB $I2C_ADDR_PEB_HSC 0xd4 0x3f1c w

# Enable the PEB HSC
i2cset -y $I2C_BUS_PEB $I2C_ADDR_PEB_HSC 0xd3 0x01 w

#Enable the ADC controller for ADC0~ADC7
devmem 0x1e6e9000 32 0x00ff000f


#!/bin/sh

. /usr/local/fbpackages/utils/ast-functions

/usr/local/bin/init_sensor.sh
exec /usr/local/bin/sensord peb pdpb fcb


#! /bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          usbcons
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:      0 6
# Short-Description: Creates a virtual USB serial device and starts a console
#                    on it.
#
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin
NAME=usbcons
PIDFILE=/run/usbcons.pid
DESC="USB Serial Console"

# source function library
. /etc/init.d/functions

STOPPER=
ACTION="$1"

case "$ACTION" in
  start)
    # Ability to prevent this from starting by editing cmdline in u-boot.
    # Keeping this here until I get gadget switching working properly. (t4906522)
    if grep "nousbcons" /proc/cmdline > /dev/null 2>&1
    then
      echo "USB Console Disabled."
      exit 0
    fi
    echo -n "Starting $DESC: "
    /usr/local/bin/usbmon.sh > /dev/null 2>&1 &
    echo "$NAME."
    ;;
  stop)
    echo -n "Stopping $DESC: "
    killall usbmon.sh
    echo "$NAME."
    ;;
  restart|force-reload)
    echo -n "Restarting $DESC: "
    killall usbmon.sh
    sleep 1
    /usr/local/bin/usbmon.sh > /dev/null 2>&1 &
    echo "$NAME."
    ;;
  status)
    status $DAEMON
    exit $?
    ;;
  *)
    N=${0##*/}
    N=${N#[SK]??}
    echo "Usage: $N {start|stop|status|restart|force-reload}" >&2
    exit 1
    ;;
esac

exit 0


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

modprobe g_cdc host_addr=02:00:00:00:00:02 dev_addr=02:00:00:00:00:01
while true; do
    getty /dev/ttyGS0 57600
    sleep 1
done


#!/bin/sh
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
### BEGIN INIT INFO
# Provides:          setup-bic-cached
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Set Cachcing for Bridge IC info
### END INIT INFO

. /usr/local/fbpackages/utils/ast-functions

echo -n "Setup Caching for Bridge IC info.."
#Get slot type (0:TwinLakes, 1:Crace Flat, 2:Glacier Point 3:Empty Slot)
#get_slot_type is to get slot type to check if the slot type is server
if [[ $(is_server_prsnt 1) == "1" && $(get_slot_type 1) == "0" ]]; then
  /usr/local/bin/bic-cached 1 > /dev/null 2>&1 &
fi

echo "done."


#!/bin/sh
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          setup_bios_util
# Required-Start:    board-id
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Setup bios-util config
### END INIT INFO

echo -n "Setup bios-util config... "

bios_util_config_path="/usr/local/fbpackages/bios-util/bios_support.json"
minilaketb_bios_config_path="/usr/local/fbpackages/bios-util/BIOS_UTIL_MINILAKETB.json"

cp -rf ${minilaketb_bios_config_path} ${bios_util_config_path}

echo "done."


#!/bin/sh
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#
PWM_DIR=/sys/devices/platform/ast_pwm_tacho.0

set -e

# The PWM frequency is

#   clk_source / ((2 ^ division_) * (2 * division_l) * (unit + 1))
#
# Our clk_source is 24Mhz.  4-pin fans are generally supposed to be driven with
# a 25Khz PWM control signal.  Therefore we want the divisor to equal 960.
#
# We also want the unit to be as large as possible, since this controls the
# granularity with which we can modulate the PWM signal.  The following
# settings allow us to set the fan from 0 to 100% in increments of 1/96th.
#
# The AST chip supports 3 different PWM clock configurations, but we only use
# type M for now.
echo 0 > $PWM_DIR/pwm_type_m_division_h
echo 5 > $PWM_DIR/pwm_type_m_division_l
echo 95 > $PWM_DIR/pwm_type_m_unit

# On minilaketb, there are 2 fans connected.
# Each fan uses same PWM input and provide one tacho output.
# Here is the mapping between the fan and PWN/Tacho,
# staring from the one from the edge
# Fan 0: PWM 0, Tacho0
# Fan 1: PWM 0, Tacho1

# For each fan, setting the type, and 100% initially
for pwm in 0 1; do
    echo 0 > $PWM_DIR/pwm${pwm}_type
    echo 0 > $PWM_DIR/pwm${pwm}_rising
    echo 0 > $PWM_DIR/pwm${pwm}_falling
    echo 1 > $PWM_DIR/pwm${pwm}_en
done

# Enable Tach 0..1
echo 0 > $PWM_DIR/tacho0_source
echo 1 > $PWM_DIR/tacho1_source

t=0
while [ $t -le 3 ]; do
    echo 1 > $PWM_DIR/tacho${t}_en
    t=$((t+1))
done


#!/bin/sh
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          setup-fan
# Required-Start:    board-id
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Set fan speed
### END INIT INFO

. /usr/local/fbpackages/utils/ast-functions

default_fsc_config_path="/etc/fsc-config.json"
sku_type=`cat /tmp/slot.bin`
full_config=1

echo "Setup fan speed... "

#for i in `seq 1 1 4`
#do
#   if [ $(is_server_prsnt $i) == "0" ]; then
#     echo "slot$i is empty"
#     full_config=0
#   fi
#done

#if [ $full_config -eq 0 ]; then
#   echo "Enter into transitional mode - Unexpected sku type (system is not full config)!"
#   /usr/local/bin/init_pwm.sh
#   /usr/local/bin/fan-util --set 70
#   exit 1
#fi

case "$sku_type" in
   "0")
       echo "Run FSC 4 TLs Config"
       cp /etc/FSC_MINILAKETB_DVT.json ${default_fsc_config_path}
   ;;
   *)  echo "Unexpected sku type! Use default config"
       cp /etc/FSC_MINILAKETB_DVT.json ${default_fsc_config_path}
   ;;
esac

/usr/local/bin/init_pwm.sh
/usr/local/bin/fan-util --set 50
runsv /etc/sv/fscd > /dev/null 2>&1 &

# Check SLED in/out
if [ $(gpio_get H5) = 1 ]; then
   sv stop fscd
   /usr/local/bin/fan-util --set 100
fi

echo "done."


#!/bin/sh
. /usr/local/fbpackages/utils/ast-functions

# Check for the slots present and run sensord for those slots only.
SLOTS=
  #Get slot type (0:TwinLakes, 1:Crace Flat, 2:Glacier Point 3:Empty Slot)
  #get_slot_type is to get slot type to check if the slot type is server
  if [[ $(is_server_prsnt 1) == "1" && $(get_slot_type 1) == "0" ]]; then
    SLOTS="$SLOTS slot1"
  fi

exec /usr/local/bin/gpiod $SLOTS


#!/bin/sh
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          setup-sensord
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Setup sensor monitoring
### END INIT INFO

. /usr/local/fbpackages/utils/ast-functions

echo -n "Setup gpio monitoring for minilaketb... "

runsv /etc/sv/gpiod > /dev/null 2>&1 &

echo "done."


#!/bin/sh
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          setup-gpiointrd
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Setup sensor monitoring
### END INIT INFO

. /usr/local/fbpackages/utils/ast-functions

echo -n "Setup gpio interrupt monitoring for MINILAKETB... "

runsv /etc/sv/gpiointrd > /dev/null 2>&1 &

echo "done."


#! /bin/sh
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          ipmbd
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:      0 6
# Short-Description: Provides ipmb message tx/rx service
#
### END INIT INFO

. /usr/local/fbpackages/utils/ast-functions

echo "Starting IPMB Rx/Tx Daemon"

ulimit -q 1024000
runsv /etc/sv/ipmbd_2 > /dev/null 2>&1 &
runsv /etc/sv/ipmbd_4 > /dev/null 2>&1 &

#Get slot type (0:TwinLakes, 1:Crace Flat, 2:Glacier Point 3:Empty Slot)
#get_slot_type is to get slot type to check if the slot type is server
if [[ $(is_server_prsnt 1) == "1" && $(get_slot_type 1) != "0" ]]; then
  sv stop ipmbd_2
elif [ $(is_server_prsnt 1) == "0" ]; then
  sv stop ipmbd_2
fi


#!/bin/sh

# PLATFORM MAY CHOOSE TO OVERRIDE THIS FILE
echo -n "Starting mTerm console server..."
runsv /etc/sv/mTerm1 > /dev/null 2>&1 &
echo "done."


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

. /usr/local/fbpackages/utils/ast-functions

get_server_sku()
{
   j=0
   max_retry=3
   server_type=0
   for i in 1 2 3 4
   do
     if [[ $(is_server_prsnt $i) == "1" && $(get_slot_type $i) == "0" ]] ; then
       tmp_server_type=3
       j=0
       while [ ${j} -lt ${max_retry} ]
       do
         # Use standard IPMI command 'get-device-id' to read product id of server board
         output=$(/usr/bin/bic-util slot$i 0x18 0x01)
         # if the command fails and the number of retry times reach max retry, continue to next slot
         if [ $(echo $output | wc -c) == 45 ] ; then
           product_id_l=`echo "$output" | cut -c 28-29`
           product_id_h=`echo "$output" | cut -c 31-32`
           if [[ "$product_id_l" == 43 && "$product_id_h" == 52 ]] ; then
             #RC
             tmp_server_type=1
           elif [[ "$product_id_l" == 50 && "$product_id_h" == 45 ]] ; then
             #EP
             tmp_server_type=2
           elif [[ "$product_id_l" == 39 && "$product_id_h" == 30 ]] ; then
             #TL
             tmp_server_type=0
           else
             #unknown
             tmp_server_type=3
           fi
           break
         fi
         j=$(($j+1))
       done
     else
       tmp_server_type=3
     fi
     server_type=$(($(($tmp_server_type << $((($i-1) * 2)))) + $server_type))
   done

   return $server_type
}

get_server_sku
SERVER_TYPE=$?
echo "Server Type: $SERVER_TYPE "
echo "<SERVER_TYPE[7:0] = {SLOT4, SLOT3, SLOT2, SLOT1}>"

exit $SERVER_TYPE


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

. /usr/local/fbpackages/utils/ast-functions

ADC_VALUE=(adc12_value)
VOL_ENABLE=(O4)

get_sku()
{
   i=0
   sku=0
   while [ "${i}" -lt "${#ADC_VALUE[@]}" ]
   do
     SLOT_VOL=`cat /sys/devices/platform/ast_adc.0/"${ADC_VALUE[$i]}" | cut -d \  -f 1 | awk '{printf ("%.3f\n",$1)}'`
     y=$(($i*2+1))
     if [[ `awk -v a=1.9 -v b=$SLOT_VOL 'BEGIN{print(a>b)?"1":"0"}'` == 1 &&  `awk -v a=$SLOT_VOL -v b=1.5 'BEGIN{print(a>b)?"1":"0"}'` == 1 ]]; then
        tmp_sku=2
        i2cset -y $y 0x40 0x5 0xa w
     elif [[ `awk -v a=1.4 -v b=$SLOT_VOL 'BEGIN{print(a>b)?"1":"0"}'` == 1 && `awk -v a=$SLOT_VOL -v b=1.0 'BEGIN{print(a>b)?"1":"0"}'` == 1 ]]; then
        tmp_sku=1
        i2cset -y $y 0x70 0x5 c
        usleep 10000
        i2cset -y $y 0x40 0x5 0xa w
     else
        tmp_sku=0
     fi

     if [ $(is_server_prsnt $(($i+1))) == "0" ]; then
        tmp_sku=3
     fi

     # Do not replace slotX type when it is 12V off
     if [ -f /tmp/slot.bin ]; then
        if [ $(gpio_get_val ${VOL_ENABLE[$i]}) == "0" ]; then
           tmp_sku=$(get_slot_type $(($i+1)))
        fi
     fi

     sku=$(($(($tmp_sku << $(($i * 2)))) + $sku))
     i=$(($i+1))
   done

   return $sku
}

get_sku
SLOT_TYPE=$?
echo "Slot Type: $SLOT_TYPE "
echo "<SLOT_TYPE[7:0] = {SLOT4, SLOT3, SLOT2, SLOT1}>"

exit $SLOT_TYPE


#!/bin/bash
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          eth0_mac_fixup.sh
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description:  Fixup the MAC address for eth0 based on wedge EEPROM
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

mac=$(weutil  | grep '^Local MAC'  | cut -d' ' -f3 )

if [ -n "$mac" ]; then
    ifconfig eth0 hw ether $mac
    # compare the 'ethaddr' from u-boot env
    ethaddr=$(fw_printenv ethaddr  | cut -d'=' -f2 )
    if [ "$ethaddr" != "$mac" ]; then
        fw_setenv "ethaddr" "$mac"
    fi
fi


#! /bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          usbcons
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:      0 6
# Short-Description: Creates a virtual USB serial device and starts a console
#                    on it.
#
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin
NAME="FC Switcher"
DESC="FC Failover Daemon"

# source function library
. /etc/init.d/functions

. /usr/local/fbpackages/utils/ast-functions

STOPPER=
ACTION="$1"

case "$ACTION" in
  start)
    if [ "$(wedge_board_type)" = "LC" ]; then
      # Ability to prevent this from starting by editing cmdline in u-boot.
      # Keeping this here until I get gadget switching working properly. (t4906522)
      /usr/local/bin/watch-fc.sh > /dev/null 2>&1 &
      echo "$NAME."
    else
      echo 'skipping watch-fc.sh: only necessary on six-pack line cards.'
    fi
    ;;
  stop)
    echo -n "Stopping $DESC: "
    killall watch-fc.sh
    echo "$NAME."
    ;;
  restart|force-reload)
    echo -n "Restarting $DESC: "
    killall watch-fc.sh
    if [ "$(wedge_board_type)" = "LC" ]; then
      sleep 1
      /usr/local/bin/watch-fc.sh > /dev/null 2>&1 &
      echo "$NAME."
    else
      echo 'skipping watch-fc.sh: only necessary on six-pack line cards.'
    fi
    ;;
  status)
    status watch-fc.sh
    exit $?
    ;;
  *)
    N=${0##*/}
    N=${N#[SK]??}
    echo "Usage: $N {start|stop|status|restart|force-reload}" >&2
    exit 1
    ;;
esac

exit 0


. /usr/local/fbpackages/utils/ast-functions

echo "Hot service is triggered, re-init process... "

function device_config() {
      SLOT_N=$1
      SLOT_B=$2

      case $SLOT_N in
          1)
            I2C_REG=0x1e78a084
          ;;
          3)
            I2C_REG=0x1e78a184
          ;;
      esac

      # Delete devices on I2C bus 1 and bus 5 if there are device cards
      # EEPROM, 0xA2
      i2c_remove_device $SLOT_B 0x51
      # Inlet temp sensor, 0x9A
      i2c_remove_device $SLOT_B 0x4d
      # outlet temp sensor, 0x9C
      i2c_remove_device $SLOT_B 0x4e
      # I2C mux, 0xE2
      i2c_remove_device $SLOT_B 0x71
      # I2C GPIO, 0x40
      i2c_remove_device $SLOT_B 0x20
      # Voltage sensor, 0x80
      i2c_remove_device $SLOT_B 0x40

      #if [ $(is_server_prsnt $(($SLOT_N+1))) == "1" ] && [ $(get_slot_type $(($SLOT_N+1))) == "0" ] ; then
        if [ $(is_server_prsnt $SLOT_N) == "1" ] && [ $(get_slot_type $SLOT_N) != "0" ] ; then

           devmem $I2C_REG w 0xFFF77304

           if [ $(get_slot_type $SLOT_N) == "2" ]; then
              # I2C mux, 0xE2
              i2c_add_device $SLOT_B 0x71 pca9551
              # I2C GPIO, 0x40
              i2c_add_device $SLOT_B 0x20 pca9551
           fi

           # EEPROM, 0xA2
           i2c_add_device $SLOT_B 0x51 24c128
           # Inlet temp sensor, 0x9A
           i2c_add_device $SLOT_B 0x4d tmp75
           # outlet temp sensor, 0x9C
           i2c_add_device $SLOT_B 0x4e tmp75
           # Voltage sensor, 0x80
           i2c_add_device $SLOT_B 0x40 ina230
        else

           devmem $I2C_REG w 0xFFF5E700
        fi
      #fi
}

function set_sysconfig() {
      SLOT_N=$1
      SLOT_B=$2

      case $SLOT_N in
          1)
            device_config $SLOT_N $SLOT_B
          ;;
          2)
            # Detect invalid configuration and then power-down the 12V for that slot(e.g. GP/CF plugged in to slot#2/slot#4)
            if [ $(is_server_prsnt $SLOT_N) == "1" ] && [ $(get_slot_type $SLOT_N) != "0" ] ; then
                  gpio_set O5 0
                  logger -p user.crit "Invalid configuration on SLOT$SLOT_N"

                  if [ $(is_server_prsnt $(($SLOT_N-1))) == "1" ] && [ $(get_slot_type $(($SLOT_N-1))) != "0" ] ; then
                     gpio_set O4 0
                  fi
            fi
          ;;
          3)
            device_config $SLOT_N $SLOT_B
          ;;
          4)
            # Detect invalid configuration and then power-down the 12V for that slot(e.g. GP/CF plugged in to slot#2/slot#4)
            if [ $(is_server_prsnt $SLOT_N) == "1" ] && [ $(get_slot_type $SLOT_N) != "0" ] ; then
                  gpio_set O7 0
                  logger -p user.crit "Invalid configuration on SLOT$SLOT_N"
                  if [ $(is_server_prsnt $(($SLOT_N-1))) == "1" ] && [ $(get_slot_type $(($SLOT_N-1))) != "0" ] ; then
                     gpio_set O6 0
                  fi
            fi
          ;;
      esac
}

SLOT=$1
OPTION=$2

case $SLOT in
    slot1)
      SLOT_NUM=1
      PE_BUFF_OE_0=B4
      PE_BUFF_OE_1=B5
      ;;
    slot2)
      SLOT_NUM=2
      PE_BUFF_OE_0=B4
      PE_BUFF_OE_1=B5
      ;;
    slot3)
      SLOT_NUM=3
      PE_BUFF_OE_0=B6
      PE_BUFF_OE_1=B7
      ;;
    slot4)
      SLOT_NUM=4
      PE_BUFF_OE_0=B6
      PE_BUFF_OE_1=B7
      ;;
    *)
      N=${0##*/}
      N=${N#[SK]??}
      echo "Usage: $N {slot1|slot2|slot3|slot4} {start}"
      exit 1
      ;;
esac

SLOT_BUS=$(get_slot_bus $SLOT_NUM)

case $OPTION in
    start)
      if [ $(is_server_prsnt $SLOT_NUM) == 0 ]; then
         exit 1
      fi
      echo "start to re-init for slot$SLOT_NUM insertion"

      # Delay 1 second for slot_type voltage ready
      sleep 2

      # System Configuration
      echo "reset system configuration for $SLOT $OPTION"
      /etc/init.d/setup-platform.sh

      # Remove Service for new device/server
      # Sensor
      sv stop sensord
      rm -rf /tmp/cache_store/$SLOT*
      set_sysconfig $SLOT_NUM $SLOT_BUS

      # GPIO
      sv stop gpiod

      # IPMB
      sv stop ipmbd_$SLOT_BUS

      # Console
      sv stop mTerm$SLOT_NUM

      # Delay to wait for sv stop
      sleep 3

      # Restart Service for new device/server
      echo "restart mTerm for $SLOT $OPTION"
      if [[ $(is_server_prsnt $SLOT_NUM) == "1" && $(get_slot_type $SLOT_NUM) == "0" ]] ; then
         sv start mTerm$SLOT_NUM
      fi

      echo "restart ipmbd for $SLOT $OPTION"
      if [[ $(is_server_prsnt $SLOT_NUM) == "1" && $(get_slot_type $SLOT_NUM) == "0" ]]; then
         sv start ipmbd_$SLOT_BUS
         /usr/local/bin/bic-cached $SLOT_NUM > /dev/null 2>&1 &
      fi

      echo "restart gpiod for $SLOT $OPTION"
      sv start gpiod

      echo "restart sensord for $SLOT $OPTION"
      sv start sensord

      echo "restart rest-api for $SLOT $OPTION"
      ps | grep -v 'grep' | grep 'rest.py' |awk '{print $1}'| xargs kill
      sh /usr/local/fbpackages/rest-api/setup-rest-api.sh

      ;;
    *)
      N=${0##*/}
      N=${N#[SK]??}
      echo "Usage: $N {slot1|slot2|slot3|slot4} {start}"
      exit 1
      ;;
esac

echo "done."


#!/bin/bash
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

. /usr/local/fbpackages/utils/ast-functions

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

LPS_PATH=/mnt/data/power/por/last_state

prog="$0"

usage() {
    echo "Usage: $prog <slot#> <command> [command options]"
    echo
    echo "Commands:"
    echo "  status: Get the current 1S server power status"
    echo
    echo "  on: Power on 1S server if not powered on already"
    echo "    options:"
    echo "      -f: Re-do power on sequence no matter if 1S server has "
    echo "          been powered on or not."
    echo
    echo "  off: Power off 1S server ungracefully"
    echo
    echo
}

do_status() {
    if [ $(is_server_prsnt $slot) == "0" ]; then
      echo  "The given slot is Empty"
      return 0
    fi

    if [[ $(get_slot_type $slot) == "1" || $(get_slot_type $slot) == "2" ]]; then
      echo "The given slot type is not server"
      return 0
    fi

    echo -n "1S Server power for slot#$slot is "
    if [ $(minilaketb_is_server_on $slot) -eq 1 ] ; then
        echo "on"
    else
        echo "off"
    fi
    return 0
}

do_on() {

    if [ $(is_server_prsnt $slot) == "0" ]; then
      echo "The given slot is Empty"
      return 0
    fi

    if [[ $(get_slot_type $slot) == "1" || $(get_slot_type $slot) == "2" ]]; then
      echo "The given slot type is not server"
      return 0
    fi

    local force opt
    force=0
    while getopts "f" opt; do
        case $opt in
            f)
                force=1
                ;;
            *)
                usage
                exit -1
                ;;

        esac
    done
    echo -n "Power on slot#$slot server ..."
    if [ $force -eq 0 ]; then
        # need to check if 1S Server is on or not
        if [ $(minilaketb_is_server_on $slot) -eq 1 ]; then
            echo " Already on. Skip!"
            return 1
        fi
    fi

    # TODO: State the power state change
    echo "on $(date +%s)" > $LPS_PATH

    # disable GPIOD pass-though function
    devmem_set_bit $(scu_addr 7c) 21

    # first make sure, GPIO is high
    gpio_set $gpio 1
    # generate the power on pulse
    gpio_set $gpio 0
    sleep 1
    gpio_set $gpio 1
    sleep 1
    # Turn on the power LED
    /usr/local/bin/power_led.sh $slot on

    # enable GPIOD pass-though function
    devmem_set_bit $(scu_addr 70) 21

    echo " Done"
    return 0
}

do_off() {
    if [ $(is_server_prsnt $slot) == "0" ]; then
      echo "The given slot is Empty"
      return 0
    fi

    if [[ $(get_slot_type $slot) == "1" || $(get_slot_type $slot) == "2" ]]; then
      echo "The given slot type is no server"
      return 0
    fi

    echo -n "Power off slot#$slot server ..."

    #TODO: State the power state change
    echo "off $(date +%s)" > $LPS_PATH

    # disable GPIOD pass-though function
    devmem_set_bit $(scu_addr 7c) 21

    # first make sure, GPIO is high
    gpio_set $gpio 1
    sleep 1
    gpio_set $gpio 0
    sleep 5
    gpio_set $gpio 1
    # Turn off the power LED
    /usr/local/bin/power_led.sh $slot off

    # enable GPIOD pass-though function
    devmem_set_bit $(scu_addr 70) 21

    echo " Done"
    return 0
}

# Slot1: GPIOD1(25), Slot2: GPIOD3(27), Slot3: GPIOD5(29), Slot4: GPIOD7(31)
slot=$1

case $slot in
  1)
    gpio=D1
    ;;
  2)
    gpio=D3
    ;;
  3)
    gpio=D5
    ;;
  4)
    gpio=D7
    ;;
  *)
    gpio=D1
    ;;
esac

command="$2"
shift
shift

case "$command" in
    status)
        do_status $@
        ;;
    on)
        do_on $@
        ;;
    off)
        do_off $@
        ;;
    *)
        usage
        exit -1
        ;;
esac

exit $?


#!/bin/sh
#
# Copyright 2004-present Facebook. All rights reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

usage() {
    echo "Displays values onto the debug header LEDs."
    echo "Hex and decimal accepted."
    echo "Usage: $0 <value>"
}

. /usr/local/fbpackages/utils/ast-functions

# Function to set the less significant hex digit
display_lower() {
    local bit0=$(expr $1 % 2)
    local bit1=$(expr $1 / 2 % 2)
    local bit2=$(expr $1 / 4 % 2)
    local bit3=$(expr $1 / 8 % 2)

    # Set the pins to the correct operating mode.
    # The relevant pins are GPIOG[0...3].
    # For GPIO bank G, SCU84[0..3] must be 0.
    devmem_clear_bit $(scu_addr 84) 0
    devmem_clear_bit $(scu_addr 84) 1
    devmem_clear_bit $(scu_addr 84) 2
    devmem_clear_bit $(scu_addr 84) 3

    # Now set the GPIOs to the right binary values
    gpio_set 48 $bit0
    gpio_set 49 $bit1
    gpio_set 50 $bit2
    gpio_set 51 $bit3
}

# Function to set the more significant hex digit
display_upper() {
    local bit0=$(expr $1 % 2)
    local bit1=$(expr $1 / 2 % 2)
    local bit2=$(expr $1 / 4 % 2)
    local bit3=$(expr $1 / 8 % 2)

    # Set the pins to the correct operating mode.
    # The relevant pins are GPIOP[4...7].

    gpio_set 124 $bit0
    gpio_set 125 $bit1
    gpio_set 126 $bit2
    gpio_set 127 $bit3
}

# Check number of parameters
if [ $# -ne 1 ]
then
    usage
    exit 1
fi

# Make sure input is actually numeric
DEC_VALUE=$(printf "%d" $1 2>/dev/null)
if [ $? -eq 1 ]
then
    echo "Unable to parse input as numeric value."
    exit 1
fi

# Make sure input is within proper range
if [ $DEC_VALUE -lt 0 ] || [ $DEC_VALUE -gt 255 ]
then
    echo "Value $DEC_VALUE is outside of displayable range 0 - 0xff (255)."
    exit 1
fi

# Get upper/lower decimal values
LOWER_DEC_VALUE=$(expr $DEC_VALUE % 16)
UPPER_DEC_VALUE=$(expr $DEC_VALUE / 16)

# Display the results
display_lower $LOWER_DEC_VALUE
display_upper $UPPER_DEC_VALUE


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA

### BEGIN INIT INFO
# Provides:          power-on
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Power on Server
### END INIT INFO
. /usr/local/fbpackages/utils/ast-functions

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

KEYDIR=/mnt/data/kv_store
DEF_PWR_ON=1
TO_PWR_ON=

check_por_config()
{

  TO_PWR_ON=-1

  # Check if the file/key doesn't exist
  if [ ! -f "${KEYDIR}/slot${1}_por_cfg" ]; then
    TO_PWR_ON=$DEF_PWR_ON
  else
    POR=`cat ${KEYDIR}/slot${1}_por_cfg`

    # Case ON
    if [ $POR == "on" ]; then
      TO_PWR_ON=1;

    # Case OFF
    elif [ $POR == "off" ]; then
      TO_PWR_ON=0;

    # Case LPS
    elif [ $POR == "lps" ]; then

      # Check if the file/key doesn't exist
      if [ ! -f "${KEYDIR}/pwr_server${1}_last_state" ]; then
        TO_PWR_ON=$DEF_PWR_ON
      else
        LS=`cat ${KEYDIR}/pwr_server${1}_last_state`
        if [ $LS == "on" ]; then
          TO_PWR_ON=1;
        elif [ $LS == "off" ]; then
          TO_PWR_ON=0;
        fi
      fi
    fi
  fi
}

# Sync BMC's date with one of the four servers
sync_date()
{
  if ! /usr/sbin/ntpq -p | grep '^\*' > /dev/null ; then
    for i in 1
    do
      if [[ $(is_server_prsnt $i) == "1" && $(get_slot_type $i) == "0" ]] ; then
        # Use standard IPMI command 'get-sel-time' to read RTC time
        output=$(/usr/local/bin/me-util slot$i 0x28 0x48)
        # if the command fails, continue to next slot
        [ $(echo $output | wc -c) != 12 ] && continue
        echo Syncing up BMC time with server$i...
        date -s @$((16#$(echo $output | awk '{print $4$3$2$1}')))
        test -x /etc/init.d/hwclock.sh && /etc/init.d/hwclock.sh stop
        break
      fi
    done
  fi
}

sync_date

# Check whether it is fresh power on reset
if [ $(is_bmc_por) -eq 1 ]; then

  # Disable clearing of PWM block on WDT SoC Reset
  devmem_clear_bit $(scu_addr 9c) 17

  check_por_config 1
  if [ $TO_PWR_ON -eq 1 ] && [ $(is_server_prsnt 1) == "1" ] && [ $(get_slot_type 1) == "0" ] ; then
    power-util slot1 on
  fi

fi


#!/bin/bash
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

usage() {
    echo "Usage: $1 <slot#> <on | off>"
    exit -1
}

. /usr/local/fbpackages/utils/ast-functions

PATH=/sbin:/bin:/usr/sbin:/usr/bin

set -e

if [ $# != 2 ]; then
    usage $0
fi

# Slot#1: GPIOM0(96),Slot#2: GPIOM1(97),Slot#3: GPIOM2(98),Slot#4: GPIOM3(99)
if [ $1 = "1" ]; then
    gpio=M0
elif [ $1 = "2" ]; then
    gpio=M1
#elif [ $1 = "3" ]; then
#    gpio=M2
#elif [ $1 = "4" ]; then
#    gpio=M3
else
    usage $0
fi

if [ $2 = "on" ]; then
    val=1
elif [ $2 = "off" ]; then
    val=0
else
    usage $0
fi

gpio_set $gpio $val


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

. /usr/local/fbpackages/utils/ast-functions

#TODO: Add logic to control mux based on front panel switch
echo -n "Set USB Mux to given slot ... "

# USB_MUX_SEL signals: GPIOE4(36), GPIOE5(37)
slot=$1

case $slot in
  1)
    gpio_set E4 0
    gpio_set E5 0
    gpio_set AB3 0
    ;;
  2)
    gpio_set E4 1
    gpio_set E5 0
    gpio_set AB3 0
    ;;
  3)
    gpio_set E4 0
    gpio_set E5 1
    gpio_set AB3 0
    ;;
  4)
    gpio_set E4 1
    gpio_set E5 1
    gpio_set AB3 0
    ;;
  *)
    gpio_set AB3 1
    ;;
esac

echo "Done"


#!/bin/bash
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          gpio-setup
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description:  Set up GPIO pins as appropriate
### END INIT INFO

# This file contains definitions for the GPIO pins that were not otherwise
# defined in other files.  We should probably move some more of the
# definitions to this file at some point.

# The commented-out sections are generally already defined elsewhere,
# and defining them twice generates errors.

# The exception to this is the definition of the GPIO H0, H1, and H2
# pins, which seem to adversely affect the rebooting of the system.
# When defined, the system doesn't reboot cleanly.  We're still
# investigating this.

. /usr/local/fbpackages/utils/ast-functions

echo "Set up GPIO pins....."

# Set up to read the board revision pins, Y0, Y1, Y2
devmem_clear_scu70_bit 19
devmem_clear_bit $(scu_addr a4) 8
devmem_clear_bit $(scu_addr a4) 9
devmem_clear_bit $(scu_addr a4) 10

gpio_export Y0
gpio_export Y1
gpio_export Y2

# SLOT1_PRSNT_N, GPIOAA0 (208)
# GPIOAA0(208): SCU90[31], SCUA4[24] shall be 0
devmem_clear_bit $(scu_addr 90) 31
devmem_clear_bit $(scu_addr a4) 24

gpio_export AA0

# SLOT2_PRSNT_N, GPIOAA1 (209)
# GPIOAA1(209): SCU90[31], SCUA4[25] shall be 0
devmem_clear_bit $(scu_addr a4) 25
#gpio_export AA1 no need in XG1

# SLOT3_PRSNT_N, GPIOAA2 (210)
# GPIOAA2(210): SCU90[31], SCUA4[26] shall be 0
devmem_clear_bit $(scu_addr a4) 26
# T6A change to output gpio, default high
gpio_set AA2 1

# SLOT4_PRSNT_N, GPIOAA3 (211)
# GPIOAA3(211): SCU90[6], SCUA4[27] shall be 0
devmem_clear_bit $(scu_addr a4) 27
#gpio_export AA3 no need in XG1

# BMC_PWR_BTN_IN_N, uServer power button in, on GPIO D0(24)
gpio_export D0

# PWR_SLOT1_BTN_N, 1S Server power out, on GPIO D1
# GPIOD1(25): SCU90[1], SCU8C[8], SCU70[21] shall be 0
devmem_clear_bit $(scu_addr 90) 1
devmem_clear_bit $(scu_addr 8c) 8
devmem_clear_scu70_bit 21
gpio_set D1 1

# PWR_SLOT2_BTN_N, 1S Server power out, on GPIO D3
# Make sure the Power Control Pin is Set properly
# GPIOD3(27): SCU90[1], SCU8C[9], SCU[21] shall be 0

devmem_clear_bit $(scu_addr 8c) 9

gpio_set D3 1

# PWR_SLOT3_BTN_N, 1S Server power out, on GPIO D5
# GPIOD5(29): SCU90[1], SCU8C[10], and SCU70[21] shall be 0
devmem_clear_bit $(scu_addr 8c) 10

gpio_set D5 1

# PWR_SLOT4_BTN_N, 1S Server power out, on GPIO D7
# GPIOD7(31): SCU90[1], SCU8C[11], and SCU70[21] shall be 0
devmem_clear_bit $(scu_addr 8c) 11

gpio_set D7 1

# SMB_SLOT1_NIC_ALERT_N, alert for 1S Server NIC I2C, GPIO B0
gpio_export B0
# SMB_SLOT2_NIC_ALERT_N, alert for 1S Server NIC I2C, GPIO B1
gpio_export B1
# SMB_SLOT3_NIC_ALERT_N, alert for 1S Server NIC I2C, GPIO B2
gpio_export B2
# SMB_SLOT4_NIC_ALERT_N, alert for 1S Server NIC I2C, GPIO B3
gpio_export B3

# Enable P3V3: GPIOAB1(217)
# To use GPIOAB1 (217), SCU90[31]=0, (SCUA8[1] or SCU94[1:0]) must be 0
devmem_clear_bit $(scu_addr a8) 1
devmem_clear_bit $(scu_addr 94) 0
devmem_clear_bit $(scu_addr 94) 1

gpio_set AB1 1

# BMC_SELF_HW_RST: GPIOAB2(218)
# To use GPIOAB2 (218), SCUA8[2] must be 0
devmem_clear_bit $(scu_addr a8) 2
gpio_set AB2 0

# VGA Mux
# To use GPIOJ2 (74), SCU84[10] must be 0
devmem_clear_bit $(scu_addr 84) 10
# To use GPIOJ3 (75), SCU84[11] must be 0
devmem_clear_bit $(scu_addr 84) 11

gpio_export J2
gpio_export J3

#========================================================================================================#
# Setup GPIOs to Mux Enable: GPIOAB3(219), Channel Select: GPIOE4(36), GPIOE5(37)

# To use GPIOAB3 (219), SCUA8[3] must be 0
devmem_clear_bit $(scu_addr a8) 3

#To use GPIOE4 (36), SCU80[20], SCU8C[14], and SCU70[22] must be 0
devmem_clear_bit $(scu_addr 80) 20
devmem_clear_bit $(scu_addr 8C) 14
devmem_clear_scu70_bit 22

# To use GPIOE5 (37), SCU80[21], SCU8C[14], and SCU70[22] must be 0
devmem_clear_bit $(scu_addr 80) 21

gpio_export AB3
gpio_export E4
gpio_export E5
#========================================================================================================#

# USB_OC_N, resettable fuse tripped, GPIO Q6
devmem_clear_bit $(scu_addr 2c) 1

gpio_export Q6

# XG1 : GPIOQ7 : COME_BMC_GPI0
gpio_set Q7 1

# DEBUG UART Controls
# 4 signals: DEBUG_UART_SEL_0/1/2 and DEBUG_UART_RX_SEL_N
# GPIOE0 (32), GPIOE1 (33), GPIOE2 (34) and GPIOE3 (35)

# To enable GPIOE0, SCU80[16], SCU8C[12], and SCU70[22] must be 0
devmem_clear_bit $(scu_addr 80) 16
devmem_clear_bit $(scu_addr 8C) 12
devmem_clear_scu70_bit 22

gpio_export E0

# To enable GPIOE1, SCU80[17], SCU8C[12], and SCU70[22] must be 0
devmem_clear_bit $(scu_addr 80) 17

gpio_export E1

# To enable GPIOE2, SCU80[18], SCU8C[13], and SCU70[22] must be 0
devmem_clear_bit $(scu_addr 80) 18
devmem_clear_bit $(scu_addr 8C) 13

gpio_export E2

# To enable GPIOE3, SCU80[19], SCU8C[13], and SCU70[22] must be 0
devmem_clear_bit $(scu_addr 80) 19

gpio_export E3

# Enable GPIOY3: BoardId(Test system)
devmem_clear_bit $(scu_addr a4) 11
devmem_clear_bit $(scu_addr 94) 11

gpio_export Y3

# Power LED for Slot#2:
# To use GPIOM0 (96), SCU90[4], SCU90[5], and SCU84[24] must be 0
devmem_clear_bit $(scu_addr 90) 4
devmem_clear_bit $(scu_addr 90) 5
devmem_clear_bit $(scu_addr 84) 24

gpio_set M0 1

# Power LED for Slot#1:
# To use GPIOM1 (97), SCU90[4], SCU90[5], and SCU84[25] must be 0
devmem_clear_bit $(scu_addr 84) 25

# XG1 : GPIOM1 : BATLOW_BMC
gpio_set M1 0

# Power LED for Slot#4:
# To use GPIOM2 (98), SCU90[4], SCU90[5], and SCU84[26] must be 0
devmem_clear_bit $(scu_addr 84) 26

#gpio_set M2 1
gpio_export M2

# Power LED for Slot#3:
# To use GPIOM3 (99), SCU90[4], SCU90[5], and SCU84[27] must be 0
devmem_clear_bit $(scu_addr 84) 27

#gpio_set M3 1
gpio_export M3

devmem_clear_bit $(scu_addr 94) 0
devmem_clear_bit $(scu_addr 94) 1
devmem_clear_bit $(scu_addr 84) 29
gpio_export M5

devmem_clear_bit $(scu_addr 84) 30
gpio_export M6

# T6A : To use GPIOM7 as P12V_STBY_MB_BMC_PWROK , default output high.
gpio_set M7 1

# Identify LED for Slot#2:
# To use GPIOF0 (40), SCU80[24], SCU90[30] must be 0
devmem_clear_bit $(scu_addr 80) 24
devmem_clear_bit $(scu_addr 90) 30

gpio_set F0 1

# Identify LED for Slot#1:
# To use GPIOF1 (41), SCU80[25], SCU90[30], must be 0
devmem_clear_bit $(scu_addr 80) 25
gpio_export F1

# Identify LED for Slot#4:
# To use GPIOF2 (42), SCU80[26], SCU90[30],  must be 0
devmem_clear_bit $(scu_addr 80) 26
gpio_set F2 1

# Identify LED for Slot#3:
# To use GPIOF3 (43), SCU80[27], SCU90[30], must be 0
devmem_clear_bit $(scu_addr 80) 27
gpio_set F3 1

# Front Panel Hand Switch GPIO setup
# HAND_SW_ID1: GPIOAA4(212)
# To use GPIOAA4, SCUA4[28] must be 0
devmem_clear_bit $(scu_addr a4) 28

gpio_export AA4

# HAND_SW_ID2: GPIOAA5(213)
# To use GPIOAA5,  SCUA4[29] must be 0
devmem_clear_bit $(scu_addr a4) 29

gpio_export AA5

# HAND_SW_ID4: GPIOAA6(214)
# To use GPIOAA6, SCUA4[30] must be 0
devmem_clear_bit $(scu_addr a4) 30

gpio_export AA6

# HAND_SW_ID8: GPIOAA7(215)
# To use GPIOAA7, SCUA4[31] must be 0
devmem_clear_bit $(scu_addr a4) 31

gpio_export AA7

# SLOT1_LED: GPIOAC0(224)
# gpio_export AC0

# SLOT2_LED: GPIOAC1(225)
# gpio_export AC1

# SLOT3_LED: GPIOAC2(226)
# gpio_export AC2

# SLOT4_LED: GPIOAC3(227)
# gpio_export AC3

# SLED_SEATED_N: GPIOAC7(231)
# gpio_export AC7

# LED POST CODES: 8 GPIO signals

# LED_POSTCODE_0: GPIOG0 (48)
# To use GPIOG0, SCU84[0] must be 0
devmem_clear_bit $(scu_addr 84) 0

gpio_set G0 0

# LED_POSTCODE_1: GPIOG1 (49)
# To use GPIOG1, SCU84[1] must be 0
devmem_clear_bit $(scu_addr 84) 1

gpio_set G1 0

# LED_POSTCODE_2: GPIOG2 (50)
# To use GPIOG2, SCU84[2] must be 0
devmem_clear_bit $(scu_addr 84) 2

gpio_set G2 0

# LED_POSTCODE_3: GPIOG3 (51)
# To use GPIOG3, SCU84[3] must be 0
devmem_clear_bit $(scu_addr 84) 3

gpio_set G3 0

# LED_POSTCODE_4: GPIOP4 (124)
gpio_set P4 0

# LED_POSTCODE_5: GPIOP5 (125)
gpio_set P5 0

# LED_POSTCODE_6: GPIOP6 (126)
gpio_set P6 0

# LED_POSTCODE_7: GPIOP7 (127)
gpio_set P7 0

# BMC_READY_N: GPIOA0 (0)
# To use GPIOA0, SCU80[0] must be 0
devmem_clear_bit $(scu_addr 80) 0
gpio_set A0 0

# BMC_RST_BTN_IN_N: GPIOAB0 (216)
# To use GPIOS0, SCU90[31], (SCUA8[0] or SCU94[1:0]) must be 0
gpio_export AB0

# RESET for all Slots
# RST_SLOT1_SYS_RESET_N: GPIOS0 (144)
# To use GPIOS0, SCU8C[0] must be 0
devmem_clear_bit $(scu_addr 8c) 0

gpio_set S0 1

# RST_SLOT2_SYS_RESET_N: GPIOS1 (145)
# To use GPIOS1, SCU8C[0] must be 0
gpio_set S1 1

# RST_SLOT3_SYS_RESET_N: GPIOS2 (146)
# To use GPIOS2, SCU8C[0] must be 0
gpio_set S2 1

# RST_SLOT4_SYS_RESET_N: GPIOS3 (147)
# To use GPIOS3, SCU8C[0] must be 0
gpio_set S3 1

# DBG_UART_SEL_N : GPIOO4 (116)
# To use GPIOO4, SCU88[12] must be 0
devmem_clear_bit $(scu_addr 88) 12
gpio_get O4

# XG1 P12V_STBY_MB_EN_N is Z0
gpio_export Z0

# P12V_STBY_SLOT2_EN: GPIOO5 (117)
# To use GPIOO5, SCU88[13] must be 0
devmem_clear_bit $(scu_addr 88) 13

gpio_export O5

# P12V_STBY_SLOT3_EN: GPIOO6 (118)
# To use GPIOO6, SCU88[13] must be 0
devmem_clear_bit $(scu_addr 88) 14

gpio_export O6

# P12V_STBY_SLOT4_EN: GPIOO7 (119)
# To use GPIOO7, SCU88[15] must be 0
devmem_clear_bit $(scu_addr 88) 15

gpio_export O7

# SLOT1_EJECTOR_LATCH_DETECT_N: GPIOP0 (120)
# To use GPIOP0, SCU88[16] must be 0
devmem_clear_bit $(scu_addr 88) 16
gpio_export P0

# SLOT2_EJECTOR_LATCH_DETECT_N: GPIOP1 (121)
# To use GPIOP1, SCU88[17] must be 0
devmem_clear_bit $(scu_addr 88) 17
gpio_export P1

# SLOT3_EJECTOR_LATCH_DETECT_N: GPIOP2 (122)
# To use GPIOP2, SCU88[18] must be 0
devmem_clear_bit $(scu_addr 88) 18
gpio_export P2

# SLOT4_EJECTOR_LATCH_DETECT_N: GPIOP3 (123)
# To use GPIOP3, SCU88[19] must be 0
devmem_clear_bit $(scu_addr 88) 19
gpio_export P3

# FAN_LATCH_DETECT : GPIOH5 (61)
# To use GPIOH5, SCU94[7] and SCU90[7] must be 0
devmem_clear_bit $(scu_addr 90) 7
devmem_clear_bit $(scu_addr 94) 7
gpio_export H5

# Enable GPIO pins: I2C_SLOTx_ALERT_N pins for BIC firmware update
devmem_clear_bit $(scu_addr 88) 2
gpio_export N2
devmem_clear_bit $(scu_addr 88) 3
gpio_export N3
devmem_clear_bit $(scu_addr 88) 4
gpio_export N4
devmem_clear_bit $(scu_addr 88) 5
gpio_export N5

# To use GPIOI0~GPIOI7, SCU70[13:12] must be 0
devmem_clear_scu70_bit 12
devmem_clear_scu70_bit 13

# SLOT1_POWER_EN: GPIOI0 (64)
gpio_export I0
# SLOT2_POWER_EN: GPIOI1 (65)
gpio_export I1
# SLOT3_POWER_EN: GPIOI2 (66)
gpio_export I2
# SLOT4_POWER_EN: GPIOI3 (67)
gpio_export I3

# Set SLOT throttle pin
# BMC_THROTTLE_SLOT1_N: GPIOI4 (68)
gpio_set I4 1
# BMC_THROTTLE_SLOT2_N: GPIOI5 (69)
gpio_set I5 1
# BMC_THROTTLE_SLOT3_N: GPIOI6 (70)
gpio_set I6 1
# BMC_THROTTLE_SLOT4_N: GPIOI7 (71)
gpio_set I7 1

# Set FAN disable pin
# DISABLE_FAN_N: GPIOM4 (100)
gpio_set M4 1

# Set FAST PROCHOT pin (Default Enable)
# FAST_PROCHOT_EN: GPIOR4 (140)
gpio_set R4 1

# PE_BUFF_OE_0_N: GPIOB4 (12)
# To use GPIOB4, SCU70[23] must be 0
devmem_clear_scu70_bit 23
gpio_export B4

# PE_BUFF_OE_1_N: GPIOB5 (13)
#To use GPIOB5, SCU80[13] must be 0
devmem_clear_bit $(scu_addr 80) 13
gpio_export B5

# PE_BUFF_OE_2_N: GPIOB6 (14)
devmem_clear_bit $(scu_addr 80) 14
gpio_export B6

# PE_BUFF_OE_3_N: GPIOB7 (15)
gpio_export B7

# CLK_BUFF1_PWR_EN_N: GPIOJ0 (72)
# To use GPIOJ2 (72), SCU84[8] must be 0
devmem_clear_bit $(scu_addr 84) 8
gpio_export J0

# CLK_BUFF2_PWR_EN_N: GPIOJ1 (73)
# To use GPIOJ3 (73), SCU84[9] must be 0
devmem_clear_bit $(scu_addr 84) 9
gpio_export J1

# Disable PWM reset during external reset
devmem_clear_bit $(scu_addr 9c) 17
# Disable PWM reset during WDT1 reset
devmem_clear_bit 0x1e78501c 17

# Set debounce timer #1 value to 0x179A7B0 ~= 2s
$DEVMEM 0x1e780050 32 0x179A7B0

# Select debounce timer #1 for GPIOZ0~GPIOZ3 and GPIOAA0~GPIOAA3
# $DEVMEM 0x1e780194 32 0xF0F00

# Set debounce timer #2 value to 0xF1B3 ~= 5ms
$DEVMEM 0x1e780054 32 0xF1B3

# Select debounce timer #2 for GPIOP0~GPIOP3
$DEVMEM 0x1e780100 32 0xF000000

# enable I2C ctrl reg (SCUA4),
devmem_set_bit $(scu_addr A4) 12 #SCL1 en
devmem_set_bit $(scu_addr A4) 13 #SDA1
devmem_set_bit $(scu_addr A4) 14 #SCL2
devmem_set_bit $(scu_addr A4) 15 #SDA2